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