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