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
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.
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.
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.
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>
36 #include <linux/device.h>
37 #include <linux/platform_device.h>
39 #include <linux/mtd/mtd.h>
40 #include <linux/mtd/map.h>
41 #include <linux/mtd/partitions.h>
43 #include <adm5120_defs.h>
44 #include <adm5120_switch.h>
45 #include <adm5120_mpmc.h>
46 #include <adm5120_platform.h>
48 #define DRV_NAME "adm5120-flash"
49 #define DRV_DESC "ADM5120 flash MAP driver"
50 #define MAX_PARSED_PARTS 8
52 #ifdef ADM5120_FLASH_DEBUG
53 #define MAP_DBG(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
55 #define MAP_DBG(m, f, a...) do {} while (0)
57 #define MAP_ERR(m, f, a...) printk(KERN_ERR "%s: " f, (m->name) , ## a)
58 #define MAP_INFO(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
60 struct adm5120_map_info
{
62 void (*switch_bank
)(unsigned);
63 unsigned long window_size
;
66 struct adm5120_flash_info
{
69 struct platform_device
*dev
;
70 struct adm5120_map_info amap
;
71 #ifdef CONFIG_MTD_PARTITIONS
73 struct mtd_partition
*parts
[MAX_PARSED_PARTS
];
85 static DEFINE_SPINLOCK(adm5120_flash_spin
);
86 #define FLASH_LOCK() spin_lock(&adm5120_flash_spin)
87 #define FLASH_UNLOCK() spin_unlock(&adm5120_flash_spin)
89 static u32 flash_bankwidths
[4] = { 1, 2, 4, 0 };
91 static u32 flash_sizes
[8] = {
92 0, 512*1024, 1024*1024, 2*1024*1024,
96 static struct flash_desc flash_descs
[2] = {
98 .phys
= ADM5120_SRAM0_BASE
,
99 .srs_shift
= MEMCTRL_SRS0_SHIFT
,
101 .phys
= ADM5120_SRAM1_BASE
,
102 .srs_shift
= MEMCTRL_SRS1_SHIFT
,
106 static const char *probe_types
[] = {
113 #ifdef CONFIG_MTD_PARTITIONS
114 static const char *parse_types
[] = {
116 #ifdef CONFIG_MTD_REDBOOT_PARTS
119 #ifdef CONFIG_MTD_MYLOADER_PARTS
125 #define BANK_SIZE (2<<20)
126 #define BANK_SIZE_MAX (4<<20)
127 #define BANK_OFFS_MASK (BANK_SIZE-1)
128 #define BANK_START_MASK (~BANK_OFFS_MASK)
130 static inline struct adm5120_map_info
*map_to_amap(struct map_info
*map
)
132 return (struct adm5120_map_info
*)map
;
135 static void adm5120_flash_switchbank(struct map_info
*map
,
138 struct adm5120_map_info
*amap
= map_to_amap(map
);
141 if (amap
->switch_bank
== NULL
)
144 bank
= (ofs
& BANK_START_MASK
) >> 21;
148 MAP_DBG(map
, "switching to bank %u, ofs=%lX\n", bank
, ofs
);
149 amap
->switch_bank(bank
);
152 static map_word
adm5120_flash_read(struct map_info
*map
, unsigned long ofs
)
154 struct adm5120_map_info
*amap
= map_to_amap(map
);
157 MAP_DBG(map
, "reading from ofs %lX\n", ofs
);
159 if (ofs
>= amap
->window_size
)
160 return map_word_ff(map
);
163 adm5120_flash_switchbank(map
, ofs
);
164 ret
= inline_map_read(map
, (ofs
& (amap
->window_size
-1)));
170 static void adm5120_flash_write(struct map_info
*map
, const map_word datum
,
173 struct adm5120_map_info
*amap
= map_to_amap(map
);
175 MAP_DBG(map
, "writing to ofs %lX\n", ofs
);
177 if (ofs
> amap
->window_size
)
181 adm5120_flash_switchbank(map
, ofs
);
182 inline_map_write(map
, datum
, (ofs
& (amap
->window_size
-1)));
186 static void adm5120_flash_copy_from(struct map_info
*map
, void *to
,
187 unsigned long from
, ssize_t len
)
189 struct adm5120_map_info
*amap
= map_to_amap(map
);
193 MAP_DBG(map
, "copy_from, to=%lX, from=%lX, len=%lX\n",
194 (unsigned long)to
, from
, (unsigned long)len
);
196 if (from
> amap
->window_size
)
202 if ((from
< BANK_SIZE
) && ((from
+len
) > BANK_SIZE
))
206 MAP_DBG(map
, "copying %lu byte(s) from %lX to %lX\n",
207 (unsigned long)t
, (from
& (amap
->window_size
-1)),
209 adm5120_flash_switchbank(map
, from
);
210 inline_map_copy_from(map
, p
, (from
& (amap
->window_size
-1)), t
);
218 static int adm5120_flash_initres(struct adm5120_flash_info
*info
)
220 struct map_info
*map
= &info
->amap
.map
;
223 info
->res
= request_mem_region(map
->phys
, info
->amap
.window_size
,
225 if (info
->res
== NULL
) {
226 MAP_ERR(map
, "could not reserve memory region\n");
231 map
->virt
= ioremap_nocache(map
->phys
, info
->amap
.window_size
);
232 if (map
->virt
== NULL
) {
233 MAP_ERR(map
, "failed to ioremap flash region\n");
242 static int adm5120_flash_initinfo(struct adm5120_flash_info
*info
,
243 struct platform_device
*dev
)
245 struct map_info
*map
= &info
->amap
.map
;
246 struct adm5120_flash_platform_data
*pdata
= dev
->dev
.platform_data
;
247 struct flash_desc
*fdesc
;
250 map
->name
= dev
->dev
.bus_id
;
253 MAP_ERR(map
, "invalid flash id\n");
257 fdesc
= &flash_descs
[dev
->id
];
260 info
->amap
.window_size
= pdata
->window_size
;
262 if (info
->amap
.window_size
== 0) {
263 /* get memory window size */
264 t
= SW_READ_REG(MEMCTRL
) >> fdesc
->srs_shift
;
265 t
&= MEMCTRL_SRS_MASK
;
266 info
->amap
.window_size
= flash_sizes
[t
];
269 if (info
->amap
.window_size
== 0) {
270 MAP_ERR(map
, "unable to determine window size\n");
274 /* get flash bus width */
277 t
= MPMC_READ_REG(SC1
) & SC_MW_MASK
;
280 t
= MPMC_READ_REG(SC0
) & SC_MW_MASK
;
283 map
->bankwidth
= flash_bankwidths
[t
];
284 if (map
->bankwidth
== 0) {
285 MAP_ERR(map
, "invalid bus width detected\n");
289 map
->phys
= fdesc
->phys
;
290 map
->size
= BANK_SIZE_MAX
;
292 simple_map_init(map
);
293 map
->read
= adm5120_flash_read
;
294 map
->write
= adm5120_flash_write
;
295 map
->copy_from
= adm5120_flash_copy_from
;
298 map
->set_vpp
= pdata
->set_vpp
;
299 info
->amap
.switch_bank
= pdata
->switch_bank
;
304 MAP_INFO(map
, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
305 (unsigned long)map
->phys
,
306 (unsigned long)info
->amap
.window_size
>> 10,
315 static void adm5120_flash_initbanks(struct adm5120_flash_info
*info
)
317 struct map_info
*map
= &info
->amap
.map
;
319 if (info
->mtd
->size
<= BANK_SIZE
)
320 /* no bank switching needed */
323 if (info
->amap
.switch_bank
) {
324 info
->amap
.window_size
= info
->mtd
->size
;
328 MAP_ERR(map
, "reduce visibility from %ldKiB to %ldKiB\n",
329 (unsigned long)map
->size
>> 10,
330 (unsigned long)info
->mtd
->size
>> 10);
332 info
->mtd
->size
= info
->amap
.window_size
;
335 #ifdef CONFIG_MTD_PARTITIONS
336 static int adm5120_flash_initparts(struct adm5120_flash_info
*info
)
338 struct adm5120_flash_platform_data
*pdata
;
339 struct map_info
*map
= &info
->amap
.map
;
341 const char *parser
[2];
348 pdata
= info
->dev
->dev
.platform_data
;
352 if (pdata
->nr_parts
) {
353 MAP_INFO(map
, "adding static partitions\n");
354 err
= add_mtd_partitions(info
->mtd
, pdata
->parts
,
357 info
->nr_parts
+= pdata
->nr_parts
;
362 num_parsers
= ARRAY_SIZE(parse_types
);
363 if (num_parsers
> MAX_PARSED_PARTS
)
364 num_parsers
= MAX_PARSED_PARTS
;
367 for (i
= 0; i
< num_parsers
; i
++) {
368 parser
[0] = parse_types
[i
];
370 MAP_INFO(map
, "parsing \"%s\" partitions\n",
372 nr_parts
= parse_mtd_partitions(info
->mtd
, parser
,
378 MAP_INFO(map
, "adding \"%s\" partitions\n",
381 err
= add_mtd_partitions(info
->mtd
, info
->parts
[i
], nr_parts
);
385 info
->nr_parts
+= nr_parts
;
391 static int adm5120_flash_initparts(struct adm5120_flash_info
*info
)
395 #endif /* CONFIG_MTD_PARTITIONS */
397 #ifdef CONFIG_MTD_PARTITIONS
398 static void adm5120_flash_remove_mtd(struct adm5120_flash_info
*info
)
402 if (info
->nr_parts
) {
403 del_mtd_partitions(info
->mtd
);
404 for (i
= 0; i
< MAX_PARSED_PARTS
; i
++)
405 if (info
->parts
[i
] != NULL
)
406 kfree(info
->parts
[i
]);
408 del_mtd_device(info
->mtd
);
412 static void adm5120_flash_remove_mtd(struct adm5120_flash_info
*info
)
414 del_mtd_device(info
->mtd
);
418 static int adm5120_flash_remove(struct platform_device
*dev
)
420 struct adm5120_flash_info
*info
;
422 info
= platform_get_drvdata(dev
);
426 platform_set_drvdata(dev
, NULL
);
428 if (info
->mtd
!= NULL
) {
429 adm5120_flash_remove_mtd(info
);
430 map_destroy(info
->mtd
);
433 if (info
->amap
.map
.virt
!= NULL
)
434 iounmap(info
->amap
.map
.virt
);
436 if (info
->res
!= NULL
) {
437 release_resource(info
->res
);
444 static int adm5120_flash_probe(struct platform_device
*dev
)
446 struct adm5120_flash_info
*info
;
447 struct map_info
*map
;
448 const char **probe_type
;
451 info
= kzalloc(sizeof(*info
), GFP_KERNEL
);
457 platform_set_drvdata(dev
, info
);
459 err
= adm5120_flash_initinfo(info
, dev
);
463 err
= adm5120_flash_initres(info
);
467 map
= &info
->amap
.map
;
468 for (probe_type
= probe_types
; info
->mtd
== NULL
&& *probe_type
!= NULL
;
470 info
->mtd
= do_map_probe(*probe_type
, map
);
472 if (info
->mtd
== NULL
) {
473 MAP_ERR(map
, "map_probe failed\n");
478 adm5120_flash_initbanks(info
);
480 if (info
->mtd
->size
< info
->amap
.window_size
) {
481 /* readjust resources */
483 release_resource(info
->res
);
486 info
->amap
.window_size
= info
->mtd
->size
;
487 map
->size
= info
->mtd
->size
;
488 MAP_INFO(map
, "reducing map size to %ldKiB\n",
489 (unsigned long)map
->size
>> 10);
490 err
= adm5120_flash_initres(info
);
495 MAP_INFO(map
, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
496 (unsigned long)map
->phys
, (unsigned long)info
->mtd
->size
>> 10,
499 info
->mtd
->owner
= THIS_MODULE
;
501 err
= adm5120_flash_initparts(info
);
505 if (info
->nr_parts
== 0) {
506 MAP_INFO(map
, "no partitions available, registering "
508 add_mtd_device(info
->mtd
);
514 adm5120_flash_remove(dev
);
519 static int adm5120_flash_suspend(struct platform_device
*dev
,
522 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
526 ret
= info
->mtd
->suspend(info
->mtd
);
531 static int adm5120_flash_resume(struct platform_device
*dev
)
533 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
536 info
->mtd
->resume(info
->mtd
);
541 static void adm5120_flash_shutdown(struct platform_device
*dev
)
543 struct adm5120_flash_info
*info
= platform_get_drvdata(dev
);
545 if (info
&& info
->mtd
->suspend(info
->mtd
) == 0)
546 info
->mtd
->resume(info
->mtd
);
550 static struct platform_driver adm5120_flash_driver
= {
551 .probe
= adm5120_flash_probe
,
552 .remove
= adm5120_flash_remove
,
554 .suspend
= adm5120_flash_suspend
,
555 .resume
= adm5120_flash_resume
,
556 .shutdown
= adm5120_flash_shutdown
,
563 static int __init
adm5120_flash_init(void)
567 err
= platform_driver_register(&adm5120_flash_driver
);
572 static void __exit
adm5120_flash_exit(void)
574 platform_driver_unregister(&adm5120_flash_driver
);
577 module_init(adm5120_flash_init
);
578 module_exit(adm5120_flash_exit
);
580 MODULE_LICENSE("GPL");
581 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
582 MODULE_DESCRIPTION(DRV_DESC
);