2 * Platform driver for NOR flash devices on ADM5120 based boards
4 * Copyright (C) 2007-2008 Gabor Juhos <juhosg@openwrt.org>
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
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.
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>
22 #include <linux/device.h>
23 #include <linux/platform_device.h>
25 #include <linux/mtd/mtd.h>
26 #include <linux/mtd/map.h>
27 #include <linux/mtd/partitions.h>
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>
34 #define DRV_NAME "adm5120-flash"
35 #define DRV_DESC "ADM5120 flash MAP driver"
36 #define MAX_PARSED_PARTS 8
38 #ifdef ADM5120_FLASH_DEBUG
39 #define MAP_DBG(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
41 #define MAP_DBG(m, f, a...) do {} while (0)
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)
46 struct adm5120_map_info
{
48 void (*switch_bank
)(unsigned);
49 unsigned long window_size
;
52 struct adm5120_flash_info
{
55 struct platform_device
*dev
;
56 struct adm5120_map_info amap
;
57 #ifdef CONFIG_MTD_PARTITIONS
59 struct mtd_partition
*parts
[MAX_PARSED_PARTS
];
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)
75 static u32 flash_bankwidths
[4] = { 1, 2, 4, 0 };
77 static u32 flash_sizes
[8] = {
78 0, 512*1024, 1024*1024, 2*1024*1024,
82 static struct flash_desc flash_descs
[2] = {
84 .phys
= ADM5120_SRAM0_BASE
,
85 .srs_shift
= MEMCTRL_SRS0_SHIFT
,
87 .phys
= ADM5120_SRAM1_BASE
,
88 .srs_shift
= MEMCTRL_SRS1_SHIFT
,
92 static const char *probe_types
[] = {
99 #ifdef CONFIG_MTD_PARTITIONS
100 static const char *parse_types
[] = {
102 #ifdef CONFIG_MTD_REDBOOT_PARTS
105 #ifdef CONFIG_MTD_MYLOADER_PARTS
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)
116 static inline struct adm5120_map_info
*map_to_amap(struct map_info
*map
)
118 return (struct adm5120_map_info
*)map
;
121 static void adm5120_flash_switchbank(struct map_info
*map
,
124 struct adm5120_map_info
*amap
= map_to_amap(map
);
127 if (amap
->switch_bank
== NULL
)
130 bank
= (ofs
& BANK_START_MASK
) >> 21;
134 MAP_DBG(map
, "switching to bank %u, ofs=%lX\n", bank
, ofs
);
135 amap
->switch_bank(bank
);
138 static map_word
adm5120_flash_read(struct map_info
*map
, unsigned long ofs
)
140 struct adm5120_map_info
*amap
= map_to_amap(map
);
143 MAP_DBG(map
, "reading from ofs %lX\n", ofs
);
145 if (ofs
>= amap
->window_size
)
146 return map_word_ff(map
);
149 adm5120_flash_switchbank(map
, ofs
);
150 ret
= inline_map_read(map
, (ofs
& (amap
->window_size
-1)));
156 static void adm5120_flash_write(struct map_info
*map
, const map_word datum
,
159 struct adm5120_map_info
*amap
= map_to_amap(map
);
161 MAP_DBG(map
, "writing to ofs %lX\n", ofs
);
163 if (ofs
> amap
->window_size
)
167 adm5120_flash_switchbank(map
, ofs
);
168 inline_map_write(map
, datum
, (ofs
& (amap
->window_size
-1)));
172 static void adm5120_flash_copy_from(struct map_info
*map
, void *to
,
173 unsigned long from
, ssize_t len
)
175 struct adm5120_map_info
*amap
= map_to_amap(map
);
179 MAP_DBG(map
, "copy_from, to=%lX, from=%lX, len=%lX\n",
180 (unsigned long)to
, from
, (unsigned long)len
);
182 if (from
> amap
->window_size
)
188 if ((from
< BANK_SIZE
) && ((from
+len
) > BANK_SIZE
))
192 MAP_DBG(map
, "copying %lu byte(s) from %lX to %lX\n",
193 (unsigned long)t
, (from
& (amap
->window_size
-1)),
195 adm5120_flash_switchbank(map
, from
);
196 inline_map_copy_from(map
, p
, (from
& (amap
->window_size
-1)), t
);
204 static int adm5120_flash_initres(struct adm5120_flash_info
*info
)
206 struct map_info
*map
= &info
->amap
.map
;
209 info
->res
= request_mem_region(map
->phys
, info
->amap
.window_size
,
211 if (info
->res
== NULL
) {
212 MAP_ERR(map
, "could not reserve memory region\n");
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");
228 static int adm5120_flash_initinfo(struct adm5120_flash_info
*info
,
229 struct platform_device
*dev
)
231 struct map_info
*map
= &info
->amap
.map
;
232 struct adm5120_flash_platform_data
*pdata
= dev
->dev
.platform_data
;
233 struct flash_desc
*fdesc
;
236 map
->name
= dev
->dev
.bus_id
;
239 MAP_ERR(map
, "invalid flash id\n");
243 fdesc
= &flash_descs
[dev
->id
];
246 info
->amap
.window_size
= pdata
->window_size
;
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
];
255 if (info
->amap
.window_size
== 0) {
256 MAP_ERR(map
, "unable to determine window size\n");
260 /* get flash bus width */
263 t
= MPMC_READ_REG(SC1
) & SC_MW_MASK
;
266 t
= MPMC_READ_REG(SC0
) & SC_MW_MASK
;
269 map
->bankwidth
= flash_bankwidths
[t
];
270 if (map
->bankwidth
== 0) {
271 MAP_ERR(map
, "invalid bus width detected\n");
275 map
->phys
= fdesc
->phys
;
276 map
->size
= BANK_SIZE_MAX
;
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
;
284 map
->set_vpp
= pdata
->set_vpp
;
285 info
->amap
.switch_bank
= pdata
->switch_bank
;
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,
301 static void adm5120_flash_initbanks(struct adm5120_flash_info
*info
)
303 struct map_info
*map
= &info
->amap
.map
;
305 if (info
->mtd
->size
<= BANK_SIZE
)
306 /* no bank switching needed */
309 if (info
->amap
.switch_bank
) {
310 info
->amap
.window_size
= info
->mtd
->size
;
314 MAP_ERR(map
, "reduce visibility from %ldKiB to %ldKiB\n",
315 (unsigned long)map
->size
>> 10,
316 (unsigned long)info
->mtd
->size
>> 10);
318 info
->mtd
->size
= info
->amap
.window_size
;
321 #ifdef CONFIG_MTD_PARTITIONS
322 static int adm5120_flash_initparts(struct adm5120_flash_info
*info
)
324 struct adm5120_flash_platform_data
*pdata
;
325 struct map_info
*map
= &info
->amap
.map
;
327 const char *parser
[2];
334 pdata
= info
->dev
->dev
.platform_data
;
338 if (pdata
->nr_parts
) {
339 MAP_INFO(map
, "adding static partitions\n");
340 err
= add_mtd_partitions(info
->mtd
, pdata
->parts
,
343 info
->nr_parts
+= pdata
->nr_parts
;
348 num_parsers
= ARRAY_SIZE(parse_types
);
349 if (num_parsers
> MAX_PARSED_PARTS
)
350 num_parsers
= MAX_PARSED_PARTS
;
353 for (i
= 0; i
< num_parsers
; i
++) {
354 parser
[0] = parse_types
[i
];
356 MAP_INFO(map
, "parsing \"%s\" partitions\n",
358 nr_parts
= parse_mtd_partitions(info
->mtd
, parser
,
364 MAP_INFO(map
, "adding \"%s\" partitions\n",
367 err
= add_mtd_partitions(info
->mtd
, info
->parts
[i
], nr_parts
);
371 info
->nr_parts
+= nr_parts
;
377 static int adm5120_flash_initparts(struct adm5120_flash_info
*info
)
381 #endif /* CONFIG_MTD_PARTITIONS */
383 #ifdef CONFIG_MTD_PARTITIONS
384 static void adm5120_flash_remove_mtd(struct adm5120_flash_info
*info
)
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
]);
394 del_mtd_device(info
->mtd
);
398 static void adm5120_flash_remove_mtd(struct adm5120_flash_info
*info
)
400 del_mtd_device(info
->mtd
);
404 static int adm5120_flash_remove(struct platform_device
*dev
)
406 struct adm5120_flash_info
*info
;
408 info
= platform_get_drvdata(dev
);
412 platform_set_drvdata(dev
, NULL
);
414 if (info
->mtd
!= NULL
) {
415 adm5120_flash_remove_mtd(info
);
416 map_destroy(info
->mtd
);
419 if (info
->amap
.map
.virt
!= NULL
)
420 iounmap(info
->amap
.map
.virt
);
422 if (info
->res
!= NULL
) {
423 release_resource(info
->res
);
430 static int adm5120_flash_probe(struct platform_device
*dev
)
432 struct adm5120_flash_info
*info
;
433 struct map_info
*map
;
434 const char **probe_type
;
437 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
443 platform_set_drvdata(dev
, info
);
445 err
= adm5120_flash_initinfo(info
, dev
);
449 err
= adm5120_flash_initres(info
);
453 map
= &info
->amap
.map
;
454 for (probe_type
= probe_types
; info
->mtd
== NULL
&& *probe_type
!= NULL
;
456 info
->mtd
= do_map_probe(*probe_type
, map
);
458 if (info
->mtd
== NULL
) {
459 MAP_ERR(map
, "map_probe failed\n");
464 adm5120_flash_initbanks(info
);
466 if (info
->mtd
->size
< info
->amap
.window_size
) {
467 /* readjust resources */
469 release_resource(info
->res
);
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
);
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,
485 info
->mtd
->owner
= THIS_MODULE
;
487 err
= adm5120_flash_initparts(info
);
491 if (info
->nr_parts
== 0) {
492 MAP_INFO(map
, "no partitions available, registering "
494 add_mtd_device(info
->mtd
);
500 adm5120_flash_remove(dev
);
505 static int adm5120_flash_suspend(struct platform_device
*dev
,
508 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
512 ret
= info
->mtd
->suspend(info
->mtd
);
517 static int adm5120_flash_resume(struct platform_device
*dev
)
519 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
522 info
->mtd
->resume(info
->mtd
);
527 static void adm5120_flash_shutdown(struct platform_device
*dev
)
529 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
531 if (info
&& info
->mtd
->suspend(info
->mtd
) == 0)
532 info
->mtd
->resume(info
->mtd
);
536 static struct platform_driver adm5120_flash_driver
= {
537 .probe
= adm5120_flash_probe
,
538 .remove
= adm5120_flash_remove
,
540 .suspend
= adm5120_flash_suspend
,
541 .resume
= adm5120_flash_resume
,
542 .shutdown
= adm5120_flash_shutdown
,
549 static int __init
adm5120_flash_init(void)
553 err
= platform_driver_register(&adm5120_flash_driver
);
558 static void __exit
adm5120_flash_exit(void)
560 platform_driver_unregister(&adm5120_flash_driver
);
563 module_init(adm5120_flash_init
);
564 module_exit(adm5120_flash_exit
);
566 MODULE_LICENSE("GPL v2");
567 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
568 MODULE_DESCRIPTION(DRV_DESC
);