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