4 * Platform driver for NOR flash devices on ADM5120 based boards
6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
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
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.
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>
25 #include <linux/device.h>
26 #include <linux/platform_device.h>
28 #include <linux/mtd/mtd.h>
29 #include <linux/mtd/map.h>
30 #include <linux/mtd/partitions.h>
32 #include <adm5120_defs.h>
33 #include <adm5120_switch.h>
34 #include <adm5120_mpmc.h>
35 #include <adm5120_platform.h>
37 #define DRV_NAME "adm5120-flash"
38 #define DRV_DESC "ADM5120 flash MAP driver"
39 #define MAX_PARSED_PARTS 8
41 #ifdef ADM5120_FLASH_DEBUG
42 #define MAP_DBG(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
44 #define MAP_DBG(m, f, a...) do {} while (0)
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)
49 struct adm5120_map_info
{
51 void (*switch_bank
)(unsigned);
52 unsigned long window_size
;
55 struct adm5120_flash_info
{
58 struct platform_device
*dev
;
59 struct adm5120_map_info amap
;
60 #ifdef CONFIG_MTD_PARTITIONS
62 struct mtd_partition
*parts
[MAX_PARSED_PARTS
];
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)
78 static u32 flash_bankwidths
[4] = { 1, 2, 4, 0 };
80 static u32 flash_sizes
[8] = {
81 0, 512*1024, 1024*1024, 2*1024*1024,
85 static struct flash_desc flash_descs
[2] = {
87 .phys
= ADM5120_SRAM0_BASE
,
88 .srs_shift
= MEMCTRL_SRS0_SHIFT
,
90 .phys
= ADM5120_SRAM1_BASE
,
91 .srs_shift
= MEMCTRL_SRS1_SHIFT
,
95 static const char *probe_types
[] = {
102 #ifdef CONFIG_MTD_PARTITIONS
103 static const char *parse_types
[] = {
105 #ifdef CONFIG_MTD_REDBOOT_PARTS
108 #ifdef CONFIG_MTD_MYLOADER_PARTS
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)
119 static inline struct adm5120_map_info
*map_to_amap(struct map_info
*map
)
121 return (struct adm5120_map_info
*)map
;
124 static void adm5120_flash_switchbank(struct map_info
*map
,
127 struct adm5120_map_info
*amap
= map_to_amap(map
);
130 if (amap
->switch_bank
== NULL
)
133 bank
= (ofs
& BANK_START_MASK
) >> 21;
137 MAP_DBG(map
, "switching to bank %u, ofs=%lX\n", bank
, ofs
);
138 amap
->switch_bank(bank
);
141 static map_word
adm5120_flash_read(struct map_info
*map
, unsigned long ofs
)
143 struct adm5120_map_info
*amap
= map_to_amap(map
);
146 MAP_DBG(map
, "reading from ofs %lX\n", ofs
);
148 if (ofs
>= amap
->window_size
)
149 return map_word_ff(map
);
152 adm5120_flash_switchbank(map
, ofs
);
153 ret
= inline_map_read(map
, (ofs
& (amap
->window_size
-1)));
159 static void adm5120_flash_write(struct map_info
*map
, const map_word datum
,
162 struct adm5120_map_info
*amap
= map_to_amap(map
);
164 MAP_DBG(map
, "writing to ofs %lX\n", ofs
);
166 if (ofs
> amap
->window_size
)
170 adm5120_flash_switchbank(map
, ofs
);
171 inline_map_write(map
, datum
, (ofs
& (amap
->window_size
-1)));
175 static void adm5120_flash_copy_from(struct map_info
*map
, void *to
,
176 unsigned long from
, ssize_t len
)
178 struct adm5120_map_info
*amap
= map_to_amap(map
);
182 MAP_DBG(map
, "copy_from, to=%lX, from=%lX, len=%lX\n",
183 (unsigned long)to
, from
, (unsigned long)len
);
185 if (from
> amap
->window_size
)
191 if ((from
< BANK_SIZE
) && ((from
+len
) > BANK_SIZE
))
195 MAP_DBG(map
, "copying %lu byte(s) from %lX to %lX\n",
196 (unsigned long)t
, (from
& (amap
->window_size
-1)),
198 adm5120_flash_switchbank(map
, from
);
199 inline_map_copy_from(map
, p
, (from
& (amap
->window_size
-1)), t
);
207 static int adm5120_flash_initres(struct adm5120_flash_info
*info
)
209 struct map_info
*map
= &info
->amap
.map
;
212 info
->res
= request_mem_region(map
->phys
, info
->amap
.window_size
,
214 if (info
->res
== NULL
) {
215 MAP_ERR(map
, "could not reserve memory region\n");
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");
231 static int adm5120_flash_initinfo(struct adm5120_flash_info
*info
,
232 struct platform_device
*dev
)
234 struct map_info
*map
= &info
->amap
.map
;
235 struct adm5120_flash_platform_data
*pdata
= dev
->dev
.platform_data
;
236 struct flash_desc
*fdesc
;
239 map
->name
= dev
->dev
.bus_id
;
242 MAP_ERR(map
, "invalid flash id\n");
246 fdesc
= &flash_descs
[dev
->id
];
249 info
->amap
.window_size
= pdata
->window_size
;
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
];
258 if (info
->amap
.window_size
== 0) {
259 MAP_ERR(map
, "unable to determine window size\n");
263 /* get flash bus width */
266 t
= MPMC_READ_REG(SC1
) & SC_MW_MASK
;
269 t
= MPMC_READ_REG(SC0
) & SC_MW_MASK
;
272 map
->bankwidth
= flash_bankwidths
[t
];
273 if (map
->bankwidth
== 0) {
274 MAP_ERR(map
, "invalid bus width detected\n");
278 map
->phys
= fdesc
->phys
;
279 map
->size
= BANK_SIZE_MAX
;
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
;
287 map
->set_vpp
= pdata
->set_vpp
;
288 info
->amap
.switch_bank
= pdata
->switch_bank
;
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,
304 static void adm5120_flash_initbanks(struct adm5120_flash_info
*info
)
306 struct map_info
*map
= &info
->amap
.map
;
308 if (info
->mtd
->size
<= BANK_SIZE
)
309 /* no bank switching needed */
312 if (info
->amap
.switch_bank
) {
313 info
->amap
.window_size
= info
->mtd
->size
;
317 MAP_ERR(map
, "reduce visibility from %ldKiB to %ldKiB\n",
318 (unsigned long)map
->size
>> 10,
319 (unsigned long)info
->mtd
->size
>> 10);
321 info
->mtd
->size
= info
->amap
.window_size
;
324 #ifdef CONFIG_MTD_PARTITIONS
325 static int adm5120_flash_initparts(struct adm5120_flash_info
*info
)
327 struct adm5120_flash_platform_data
*pdata
;
328 struct map_info
*map
= &info
->amap
.map
;
330 const char *parser
[2];
337 pdata
= info
->dev
->dev
.platform_data
;
341 if (pdata
->nr_parts
) {
342 MAP_INFO(map
, "adding static partitions\n");
343 err
= add_mtd_partitions(info
->mtd
, pdata
->parts
,
346 info
->nr_parts
+= pdata
->nr_parts
;
351 num_parsers
= ARRAY_SIZE(parse_types
);
352 if (num_parsers
> MAX_PARSED_PARTS
)
353 num_parsers
= MAX_PARSED_PARTS
;
356 for (i
= 0; i
< num_parsers
; i
++) {
357 parser
[0] = parse_types
[i
];
359 MAP_INFO(map
, "parsing \"%s\" partitions\n",
361 nr_parts
= parse_mtd_partitions(info
->mtd
, parser
,
367 MAP_INFO(map
, "adding \"%s\" partitions\n",
370 err
= add_mtd_partitions(info
->mtd
, info
->parts
[i
], nr_parts
);
374 info
->nr_parts
+= nr_parts
;
380 static int adm5120_flash_initparts(struct adm5120_flash_info
*info
)
384 #endif /* CONFIG_MTD_PARTITIONS */
386 #ifdef CONFIG_MTD_PARTITIONS
387 static void adm5120_flash_remove_mtd(struct adm5120_flash_info
*info
)
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
]);
397 del_mtd_device(info
->mtd
);
401 static void adm5120_flash_remove_mtd(struct adm5120_flash_info
*info
)
403 del_mtd_device(info
->mtd
);
407 static int adm5120_flash_remove(struct platform_device
*dev
)
409 struct adm5120_flash_info
*info
;
411 info
= platform_get_drvdata(dev
);
415 platform_set_drvdata(dev
, NULL
);
417 if (info
->mtd
!= NULL
) {
418 adm5120_flash_remove_mtd(info
);
419 map_destroy(info
->mtd
);
422 if (info
->amap
.map
.virt
!= NULL
)
423 iounmap(info
->amap
.map
.virt
);
425 if (info
->res
!= NULL
) {
426 release_resource(info
->res
);
433 static int adm5120_flash_probe(struct platform_device
*dev
)
435 struct adm5120_flash_info
*info
;
436 struct map_info
*map
;
437 const char **probe_type
;
440 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
446 platform_set_drvdata(dev
, info
);
448 err
= adm5120_flash_initinfo(info
, dev
);
452 err
= adm5120_flash_initres(info
);
456 map
= &info
->amap
.map
;
457 for (probe_type
= probe_types
; info
->mtd
== NULL
&& *probe_type
!= NULL
;
459 info
->mtd
= do_map_probe(*probe_type
, map
);
461 if (info
->mtd
== NULL
) {
462 MAP_ERR(map
, "map_probe failed\n");
467 adm5120_flash_initbanks(info
);
469 if (info
->mtd
->size
< info
->amap
.window_size
) {
470 /* readjust resources */
472 release_resource(info
->res
);
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
);
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,
488 info
->mtd
->owner
= THIS_MODULE
;
490 err
= adm5120_flash_initparts(info
);
494 if (info
->nr_parts
== 0) {
495 MAP_INFO(map
, "no partitions available, registering "
497 add_mtd_device(info
->mtd
);
503 adm5120_flash_remove(dev
);
508 static int adm5120_flash_suspend(struct platform_device
*dev
,
511 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
515 ret
= info
->mtd
->suspend(info
->mtd
);
520 static int adm5120_flash_resume(struct platform_device
*dev
)
522 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
525 info
->mtd
->resume(info
->mtd
);
530 static void adm5120_flash_shutdown(struct platform_device
*dev
)
532 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
534 if (info
&& info
->mtd
->suspend(info
->mtd
) == 0)
535 info
->mtd
->resume(info
->mtd
);
539 static struct platform_driver adm5120_flash_driver
= {
540 .probe
= adm5120_flash_probe
,
541 .remove
= adm5120_flash_remove
,
543 .suspend
= adm5120_flash_suspend
,
544 .resume
= adm5120_flash_resume
,
545 .shutdown
= adm5120_flash_shutdown
,
552 static int __init
adm5120_flash_init(void)
556 err
= platform_driver_register(&adm5120_flash_driver
);
561 static void __exit
adm5120_flash_exit(void)
563 platform_driver_unregister(&adm5120_flash_driver
);
566 module_init(adm5120_flash_init
);
567 module_exit(adm5120_flash_exit
);
569 MODULE_LICENSE("GPL v2");
570 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
571 MODULE_DESCRIPTION(DRV_DESC
);