58cf277438d38bfe9976bb4e70548434ebd2f3db
[librecmc/librecmc.git] / target / linux / adm5120 / files / drivers / mtd / maps / adm5120-flash.c
1 /*
2  *  Platform driver for NOR flash devices on ADM5120 based boards
3  *
4  *  Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
5  *
6  *  This file was derived from: drivers/mtd/map/physmap.c
7  *      Copyright (C) 2003 MontaVista Software Inc.
8  *      Author: Jun Sun, jsun@mvista.com or jsun@junsun.net
9  *
10  *  This program is free software; you can redistribute it and/or modify it
11  *  under the terms of the GNU General Public License version 2 as published
12  *  by the Free Software Foundation.
13  *
14  */
15
16 #include <linux/module.h>
17 #include <linux/types.h>
18 #include <linux/kernel.h>
19 #include <linux/init.h>
20 #include <linux/slab.h>
21 #include <linux/io.h>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
24
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/map.h>
27 #include <linux/mtd/partitions.h>
28
29 #include <asm/mach-adm5120/adm5120_defs.h>
30 #include <asm/mach-adm5120/adm5120_switch.h>
31 #include <asm/mach-adm5120/adm5120_mpmc.h>
32 #include <asm/mach-adm5120/adm5120_platform.h>
33
34 #define DRV_NAME        "adm5120-flash"
35 #define DRV_DESC        "ADM5120 flash MAP driver"
36 #define MAX_PARSED_PARTS 8
37
38 #ifdef ADM5120_FLASH_DEBUG
39 #define MAP_DBG(m, f, a...)     printk(KERN_INFO "%s: " f, (m->name) , ## a)
40 #else
41 #define MAP_DBG(m, f, a...)     do {} while (0)
42 #endif
43 #define MAP_ERR(m, f, a...)     printk(KERN_ERR "%s: " f, (m->name) , ## a)
44 #define MAP_INFO(m, f, a...)    printk(KERN_INFO "%s: " f, (m->name) , ## a)
45
46 struct adm5120_map_info {
47         struct map_info map;
48         void            (*switch_bank)(unsigned);
49         unsigned long   window_size;
50 };
51
52 struct adm5120_flash_info {
53         struct mtd_info         *mtd;
54         struct resource         *res;
55         struct platform_device  *dev;
56         struct adm5120_map_info amap;
57 };
58
59 struct flash_desc {
60         u32     phys;
61         u32     srs_shift;
62 };
63
64 /*
65  * Globals
66  */
67 static DEFINE_SPINLOCK(adm5120_flash_spin);
68 #define FLASH_LOCK()    spin_lock(&adm5120_flash_spin)
69 #define FLASH_UNLOCK()  spin_unlock(&adm5120_flash_spin)
70
71 static u32 flash_bankwidths[4] = { 1, 2, 4, 0 };
72
73 static u32 flash_sizes[8] = {
74         0, 512*1024, 1024*1024, 2*1024*1024,
75         4*1024*1024, 0, 0, 0
76 };
77
78 static struct flash_desc flash_descs[2] = {
79         {
80                 .phys           = ADM5120_SRAM0_BASE,
81                 .srs_shift      = MEMCTRL_SRS0_SHIFT,
82         }, {
83                 .phys           = ADM5120_SRAM1_BASE,
84                 .srs_shift      = MEMCTRL_SRS1_SHIFT,
85         }
86 };
87
88 static const char const *probe_types[] = {
89         "cfi_probe",
90         "jedec_probe",
91         "map_rom",
92         NULL
93 };
94
95 static const char const *parse_types[] = {
96         "cmdlinepart",
97 #ifdef CONFIG_MTD_REDBOOT_PARTS
98         "RedBoot",
99 #endif
100 #ifdef CONFIG_MTD_MYLOADER_PARTS
101         "MyLoader",
102 #endif
103 };
104
105 #define BANK_SIZE       (2<<20)
106 #define BANK_SIZE_MAX   (4<<20)
107 #define BANK_OFFS_MASK  (BANK_SIZE-1)
108 #define BANK_START_MASK (~BANK_OFFS_MASK)
109
110 static inline struct adm5120_map_info *map_to_amap(struct map_info *map)
111 {
112         return (struct adm5120_map_info *)map;
113 }
114
115 static void adm5120_flash_switchbank(struct map_info *map,
116                 unsigned long ofs)
117 {
118         struct adm5120_map_info *amap = map_to_amap(map);
119         unsigned bank;
120
121         if (amap->switch_bank == NULL)
122                 return;
123
124         bank = (ofs & BANK_START_MASK) >> 21;
125         if (bank > 1)
126                 BUG();
127
128         MAP_DBG(map, "switching to bank %u, ofs=%lX\n", bank, ofs);
129         amap->switch_bank(bank);
130 }
131
132 static map_word adm5120_flash_read(struct map_info *map, unsigned long ofs)
133 {
134         struct adm5120_map_info *amap = map_to_amap(map);
135         map_word ret;
136
137         MAP_DBG(map, "reading from ofs %lX\n", ofs);
138
139         if (ofs >= amap->window_size)
140                 return map_word_ff(map);
141
142         FLASH_LOCK();
143         adm5120_flash_switchbank(map, ofs);
144         ret = inline_map_read(map, (ofs & (amap->window_size-1)));
145         FLASH_UNLOCK();
146
147         return ret;
148 }
149
150 static void adm5120_flash_write(struct map_info *map, const map_word datum,
151                 unsigned long ofs)
152 {
153         struct adm5120_map_info *amap = map_to_amap(map);
154
155         MAP_DBG(map, "writing to ofs %lX\n", ofs);
156
157         if (ofs > amap->window_size)
158                 return;
159
160         FLASH_LOCK();
161         adm5120_flash_switchbank(map, ofs);
162         inline_map_write(map, datum, (ofs & (amap->window_size-1)));
163         FLASH_UNLOCK();
164 }
165
166 static void adm5120_flash_copy_from(struct map_info *map, void *to,
167                 unsigned long from, ssize_t len)
168 {
169         struct adm5120_map_info *amap = map_to_amap(map);
170         char *p;
171         ssize_t t;
172
173         MAP_DBG(map, "copy_from, to=%lX, from=%lX, len=%lX\n",
174                 (unsigned long)to, from, (unsigned long)len);
175
176         if (from > amap->window_size)
177                 return;
178
179         p = (char *)to;
180         while (len > 0) {
181                 t = len;
182                 if ((from < BANK_SIZE) && ((from+len) > BANK_SIZE))
183                         t = BANK_SIZE-from;
184
185                 FLASH_LOCK();
186                 MAP_DBG(map, "copying %lu byte(s) from %lX to %lX\n",
187                         (unsigned long)t, (from & (amap->window_size-1)),
188                         (unsigned long)p);
189                 adm5120_flash_switchbank(map, from);
190                 inline_map_copy_from(map, p, (from & (amap->window_size-1)), t);
191                 FLASH_UNLOCK();
192                 p += t;
193                 from += t;
194                 len -= t;
195         }
196 }
197
198 static int adm5120_flash_initres(struct adm5120_flash_info *info)
199 {
200         struct map_info *map = &info->amap.map;
201         int err = 0;
202
203         info->res = request_mem_region(map->phys, info->amap.window_size,
204                         map->name);
205         if (info->res == NULL) {
206                 MAP_ERR(map, "could not reserve memory region\n");
207                 err = -ENOMEM;
208                 goto out;
209         }
210
211         map->virt = ioremap_nocache(map->phys, info->amap.window_size);
212         if (map->virt == NULL) {
213                 MAP_ERR(map, "failed to ioremap flash region\n");
214                 err = -ENOMEM;
215                 goto out;
216         }
217
218 out:
219         return err;
220 }
221
222 static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
223                 struct platform_device *dev)
224 {
225         struct map_info *map = &info->amap.map;
226         struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
227         struct flash_desc *fdesc;
228         u32 t = 0;
229
230         map->name = dev_name(&dev->dev);
231
232         if (dev->id > 1) {
233                 MAP_ERR(map, "invalid flash id\n");
234                 goto err_out;
235         }
236
237         fdesc = &flash_descs[dev->id];
238
239         if (pdata)
240                 info->amap.window_size = pdata->window_size;
241
242         if (info->amap.window_size == 0) {
243                 /* get memory window size */
244                 t = SW_READ_REG(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
245                 t &= MEMCTRL_SRS_MASK;
246                 info->amap.window_size = flash_sizes[t];
247         }
248
249         if (info->amap.window_size == 0) {
250                 MAP_ERR(map, "unable to determine window size\n");
251                 goto err_out;
252         }
253
254         /* get flash bus width */
255         switch (dev->id) {
256         case 0:
257                 t = MPMC_READ_REG(SC1) & SC_MW_MASK;
258                 break;
259         case 1:
260                 t = MPMC_READ_REG(SC0) & SC_MW_MASK;
261                 break;
262         }
263         map->bankwidth = flash_bankwidths[t];
264         if (map->bankwidth == 0) {
265                 MAP_ERR(map, "invalid bus width detected\n");
266                 goto err_out;
267         }
268
269         map->phys = fdesc->phys;
270         map->size = BANK_SIZE_MAX;
271
272         simple_map_init(map);
273         map->read = adm5120_flash_read;
274         map->write = adm5120_flash_write;
275         map->copy_from = adm5120_flash_copy_from;
276
277         if (pdata) {
278                 map->set_vpp = pdata->set_vpp;
279                 info->amap.switch_bank = pdata->switch_bank;
280         }
281
282         info->dev = dev;
283
284         MAP_INFO(map, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
285                 (unsigned long)map->phys,
286                 (unsigned long)info->amap.window_size >> 10,
287                 map->bankwidth*8);
288
289         return 0;
290
291 err_out:
292         return -ENODEV;
293 }
294
295 static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
296 {
297         struct map_info *map = &info->amap.map;
298
299         if (info->mtd->size <= BANK_SIZE)
300                 /* no bank switching needed */
301                 return;
302
303         if (info->amap.switch_bank) {
304                 info->amap.window_size = info->mtd->size;
305                 return;
306         }
307
308         MAP_ERR(map, "reduce visibility from %ldKiB to %ldKiB\n",
309                 (unsigned long)map->size >> 10,
310                 (unsigned long)info->mtd->size >> 10);
311
312         info->mtd->size = info->amap.window_size;
313 }
314
315 static int adm5120_flash_remove(struct platform_device *dev)
316 {
317         struct adm5120_flash_info *info;
318
319         info = platform_get_drvdata(dev);
320         if (info == NULL)
321                 return 0;
322
323         platform_set_drvdata(dev, NULL);
324
325         if (info->mtd != NULL) {
326                 mtd_device_unregister(info->mtd);
327                 map_destroy(info->mtd);
328         }
329
330         if (info->amap.map.virt != NULL)
331                 iounmap(info->amap.map.virt);
332
333         if (info->res != NULL) {
334                 release_resource(info->res);
335                 kfree(info->res);
336         }
337
338         return 0;
339 }
340
341 static int adm5120_flash_probe(struct platform_device *dev)
342 {
343         struct adm5120_flash_platform_data *pdata;
344         struct adm5120_flash_info *info;
345         struct map_info *map;
346         const char **probe_type;
347         int err;
348
349         pdata = dev->dev.platform_data;
350         if (!pdata) {
351                 dev_err(&dev->dev, "no platform data\n");
352                 return -EINVAL;
353         }
354
355         info = kzalloc(sizeof(*info), GFP_KERNEL);
356         if (info == NULL) {
357                 err = -ENOMEM;
358                 goto err_out;
359         }
360
361         platform_set_drvdata(dev, info);
362
363         err = adm5120_flash_initinfo(info, dev);
364         if (err)
365                 goto err_out;
366
367         err = adm5120_flash_initres(info);
368         if (err)
369                 goto err_out;
370
371         map = &info->amap.map;
372         for (probe_type = probe_types; info->mtd == NULL && *probe_type != NULL;
373                 probe_type++)
374                 info->mtd = do_map_probe(*probe_type, map);
375
376         if (info->mtd == NULL) {
377                 MAP_ERR(map, "map_probe failed\n");
378                 err = -ENXIO;
379                 goto err_out;
380         }
381
382         adm5120_flash_initbanks(info);
383
384         if (info->mtd->size < info->amap.window_size) {
385                 /* readjust resources */
386                 iounmap(map->virt);
387                 release_resource(info->res);
388                 kfree(info->res);
389
390                 info->amap.window_size = info->mtd->size;
391                 map->size = info->mtd->size;
392                 MAP_INFO(map, "reducing map size to %ldKiB\n",
393                         (unsigned long)map->size >> 10);
394                 err = adm5120_flash_initres(info);
395                 if (err)
396                         goto err_out;
397         }
398
399         MAP_INFO(map, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
400                 (unsigned long)map->phys, (unsigned long)info->mtd->size >> 10,
401                 map->bankwidth*8);
402
403         info->mtd->owner = THIS_MODULE;
404
405         err = mtd_device_parse_register(info->mtd, parse_types, 0,
406                                         pdata->parts, pdata->nr_parts);
407         if (err)
408                 goto err_out;
409
410         return 0;
411
412 err_out:
413         adm5120_flash_remove(dev);
414         return err;
415 }
416
417 #ifdef CONFIG_PM
418 static int adm5120_flash_suspend(struct platform_device *dev,
419                 pm_message_t state)
420 {
421         struct adm5120_flash_info *info = platform_get_drvdata(dev);
422         int ret = 0;
423
424         if (info)
425                 ret = info->mtd->suspend(info->mtd);
426
427         return ret;
428 }
429
430 static int adm5120_flash_resume(struct platform_device *dev)
431 {
432         struct adm5120_flash_info *info = platform_get_drvdata(dev);
433
434         if (info)
435                 info->mtd->resume(info->mtd);
436
437         return 0;
438 }
439
440 static void adm5120_flash_shutdown(struct platform_device *dev)
441 {
442         struct adm5120_flash_info *info = platform_get_drvdata(dev);
443
444         if (info && info->mtd->suspend(info->mtd) == 0)
445                 info->mtd->resume(info->mtd);
446 }
447 #endif
448
449 static struct platform_driver adm5120_flash_driver = {
450         .probe          = adm5120_flash_probe,
451         .remove         = adm5120_flash_remove,
452 #ifdef CONFIG_PM
453         .suspend        = adm5120_flash_suspend,
454         .resume         = adm5120_flash_resume,
455         .shutdown       = adm5120_flash_shutdown,
456 #endif
457         .driver         = {
458                 .name   = DRV_NAME,
459         },
460 };
461
462 static int __init adm5120_flash_init(void)
463 {
464         int err;
465
466         err = platform_driver_register(&adm5120_flash_driver);
467
468         return err;
469 }
470
471 static void __exit adm5120_flash_exit(void)
472 {
473         platform_driver_unregister(&adm5120_flash_driver);
474 }
475
476 module_init(adm5120_flash_init);
477 module_exit(adm5120_flash_exit);
478
479 MODULE_LICENSE("GPL v2");
480 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
481 MODULE_DESCRIPTION(DRV_DESC);