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