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