4 * Platform driver for NOR flash devices on ADM5120 based boards
6 * Copyright (C) 2007 OpenWrt.org
7 * Copyright (C) 2007 Gabor Juhos <juhosg@freemail.hu>
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>
35 #include <linux/device.h>
37 #include <linux/platform_device.h>
38 #include <linux/mtd/mtd.h>
39 #include <linux/mtd/map.h>
40 #include <linux/mtd/partitions.h>
44 #include <asm/mach-adm5120/adm5120_defs.h>
45 #include <asm/mach-adm5120/adm5120_switch.h>
46 #include <asm/mach-adm5120/adm5120_mpmc.h>
47 #include <asm/mach-adm5120/adm5120_platform.h>
49 #define DRV_NAME "adm5120-flash"
50 #define DRV_DESC "ADM5120 flash MAP driver"
51 #define MAX_PARSED_PARTS 8
53 #ifdef ADM5120_FLASH_DEBUG
54 #define MAP_DBG(m, f, a...) printk(KERN_INFO "%s: " f, (m->name) , ## a)
56 #define MAP_DBG(m, f, a...) do {} while (0)
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)
61 struct adm5120_map_info {
63 void (*switch_bank)(unsigned);
64 unsigned long chip_size;
67 struct adm5120_flash_info {
70 struct platform_device *dev;
71 struct adm5120_map_info amap;
72 #ifdef CONFIG_MTD_PARTITIONS
74 struct mtd_partition *parts[MAX_PARSED_PARTS];
87 static DEFINE_SPINLOCK(adm5120_flash_spin);
88 #define FLASH_LOCK() spin_lock(&adm5120_flash_spin)
89 #define FLASH_UNLOCK() spin_unlock(&adm5120_flash_spin)
91 static u32 flash_bankwidths[4] = { 1, 2, 4, 0 };
93 static u32 flash_sizes[8] = {
94 0, 512*1024, 1024*1024, 2*1024*1024,
98 static struct flash_desc flash_descs[2] = {
100 .phys = ADM5120_SRAM0_BASE,
101 .mpmc_reg = MPMC_REG_SC1,
102 .srs_shift = MEMCTRL_SRS0_SHIFT,
104 .phys = ADM5120_SRAM1_BASE,
105 .mpmc_reg = MPMC_REG_SC0,
106 .srs_shift = MEMCTRL_SRS1_SHIFT,
110 static const char *probe_types[] = {
117 #ifdef CONFIG_MTD_PARTITIONS
118 static const char *parse_types[] = {
120 #ifdef CONFIG_MTD_REDBOOT_PARTS
123 #ifdef CONFIG_MTD_MYLOADER_PARTS
129 #define BANK_SIZE (2<<20)
130 #define BANK_SIZE_MAX (4<<20)
131 #define BANK_OFFS_MASK (BANK_SIZE-1)
132 #define BANK_START_MASK (~BANK_OFFS_MASK)
134 static inline struct adm5120_map_info *map_to_amap(struct map_info *map)
136 return (struct adm5120_map_info *)map;
139 static void adm5120_flash_switchbank(struct map_info *map,
142 struct adm5120_map_info *amap = map_to_amap(map);
145 if (amap->switch_bank == NULL)
148 bank = (ofs & BANK_START_MASK) >> 21;
152 MAP_DBG(map, "switching to bank %u, ofs=%lX\n", bank, ofs);
153 amap->switch_bank(bank);
156 static map_word adm5120_flash_read(struct map_info *map, unsigned long ofs)
158 struct adm5120_map_info *amap = map_to_amap(map);
161 MAP_DBG(map, "reading from ofs %lu\n", ofs);
163 if (ofs >= amap->chip_size)
164 return map_word_ff(map);
167 adm5120_flash_switchbank(map, ofs);
168 ret = inline_map_read(map, (ofs & BANK_OFFS_MASK));
174 static void adm5120_flash_write(struct map_info *map, const map_word datum,
177 struct adm5120_map_info *amap = map_to_amap(map);
179 MAP_DBG(map,"writing to ofs %lu\n", ofs);
181 if (ofs > amap->chip_size)
185 adm5120_flash_switchbank(map, ofs);
186 inline_map_write(map, datum, (ofs & BANK_OFFS_MASK));
190 static void adm5120_flash_copy_from(struct map_info *map, void *to,
191 unsigned long from, ssize_t len)
193 struct adm5120_map_info *amap = map_to_amap(map);
197 MAP_DBG(map, "copy_from, to=%lX, from=%lX, len=%lX\n",
198 (unsigned long)to, from, (unsigned long)len);
200 if (from > amap->chip_size)
206 if (from < BANK_SIZE && from+len > BANK_SIZE)
210 MAP_DBG(map, "copying %lu byte(s) from %lX to %lX\n",
211 (unsigned long)t, (from & BANK_OFFS_MASK),
213 adm5120_flash_switchbank(map, from);
214 inline_map_copy_from(map, p, (from & BANK_OFFS_MASK), t);
222 static int adm5120_flash_initres(struct adm5120_flash_info *info)
224 struct map_info *map = &info->amap.map;
227 info->res = request_mem_region(map->phys, map->size, map->name);
228 if (info->res == NULL) {
229 MAP_ERR(map, "could not reserve memory region\n");
234 map->virt = ioremap_nocache(map->phys, map->size);
235 if (map->virt == NULL) {
236 MAP_ERR(map, "failed to ioremap flash region\n");
245 #define SWITCH_READ(r) *(u32 *)(KSEG1ADDR(ADM5120_SWITCH_BASE)+(r))
246 #define SWITCH_WRITE(r,v) *(u32 *)(KSEG1ADDR(ADM5120_SWITCH_BASE)+(r))=(v)
247 #define MPMC_READ(r) *(u32 *)(KSEG1ADDR(ADM5120_MPMC_BASE)+(r))
248 #define MPMC_WRITE(r,v) *(u32 *)(KSEG1ADDR(ADM5120_MPMC_BASE)+(r))=(v)
250 static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
251 struct platform_device *dev)
253 struct map_info *map = &info->amap.map;
254 struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
255 struct flash_desc *fdesc;
258 map->name = dev->dev.bus_id;
261 MAP_ERR(map, "invalid flash id\n");
265 fdesc = &flash_descs[dev->id];
267 /* get memory window size */
268 t = SWITCH_READ(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
269 t &= MEMCTRL_SRS_MASK;
270 info->amap.chip_size = flash_sizes[t];
271 if (info->amap.chip_size == 0) {
272 MAP_ERR(map, "invalid flash size detected\n");
276 /* get flash bus width */
277 t = MPMC_READ(fdesc->mpmc_reg) & SC_MW_MASK;
278 map->bankwidth = flash_bankwidths[t];
279 if (map->bankwidth == 0) {
280 MAP_ERR(map, "invalid bus width detected\n");
284 map->phys = fdesc->phys;
285 map->size = BANK_SIZE_MAX;
287 simple_map_init(map);
288 map->read = adm5120_flash_read;
289 map->write = adm5120_flash_write;
290 map->copy_from = adm5120_flash_copy_from;
293 map->set_vpp = pdata->set_vpp;
294 info->amap.switch_bank = pdata->switch_bank;
299 MAP_INFO(map, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
300 (unsigned long)map->phys,
301 (unsigned long)info->amap.chip_size >> 10,
310 static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
312 struct map_info *map = &info->amap.map;
314 if (info->mtd->size <= BANK_SIZE)
315 /* no bank switching needed */
318 if (info->amap.switch_bank) {
319 info->amap.chip_size = info->mtd->size;
323 MAP_ERR(map, "reduce visibility from %ldKiB to %ldKiB\n",
324 (unsigned long)map->size >> 10,
325 (unsigned long)info->mtd->size >> 10);
327 info->mtd->size = info->amap.chip_size;
330 #ifdef CONFIG_MTD_PARTITIONS
331 static int adm5120_flash_initparts(struct adm5120_flash_info *info)
333 struct adm5120_flash_platform_data *pdata = info->dev->dev.platform_data;
334 struct map_info *map = &info->amap.map;
336 const char *parser[2];
346 if (pdata->nr_parts) {
347 MAP_INFO(map, "adding static partitions\n");
348 err = add_mtd_partitions(info->mtd, pdata->parts,
351 info->nr_parts += pdata->nr_parts;
356 num_parsers = ARRAY_SIZE(parse_types);
357 if (num_parsers > MAX_PARSED_PARTS)
358 num_parsers = MAX_PARSED_PARTS;
361 for (i=0; i<num_parsers; i++) {
362 parser[0] = parse_types[i];
364 MAP_INFO(map, "parsing \"%s\" partitions\n",
366 nr_parts = parse_mtd_partitions(info->mtd, parser,
372 MAP_INFO(map, "adding \"%s\" partitions\n",
375 err = add_mtd_partitions(info->mtd, info->parts[i], nr_parts);
379 info->nr_parts += nr_parts;
385 static int adm5120_flash_initparts(struct adm5120_flash_info *info)
389 #endif /* CONFIG_MTD_PARTITIONS */
391 #ifdef CONFIG_MTD_PARTITIONS
392 static void adm5120_flash_remove_mtd(struct adm5120_flash_info *info)
396 if (info->nr_parts) {
397 del_mtd_partitions(info->mtd);
398 for (i=0; i<MAX_PARSED_PARTS; i++)
399 if (info->parts[i] != NULL)
400 kfree(info->parts[i]);
402 del_mtd_device(info->mtd);
406 static void adm5120_flash_remove_mtd(struct adm5120_flash_info *info)
408 del_mtd_device(info->mtd);
412 static int adm5120_flash_remove(struct platform_device *dev)
414 struct adm5120_flash_info *info;
416 info = platform_get_drvdata(dev);
420 platform_set_drvdata(dev, NULL);
422 if (info->mtd != NULL) {
423 adm5120_flash_remove_mtd(info);
424 map_destroy(info->mtd);
427 if (info->amap.map.virt != NULL)
428 iounmap(info->amap.map.virt);
430 if (info->res != NULL) {
431 release_resource(info->res);
438 static int adm5120_flash_probe(struct platform_device *dev)
440 struct adm5120_flash_info *info;
441 struct map_info *map;
442 const char **probe_type;
445 info = kzalloc(sizeof(*info), GFP_KERNEL);
451 platform_set_drvdata(dev, info);
453 err = adm5120_flash_initinfo(info, dev);
457 err = adm5120_flash_initres(info);
461 map = &info->amap.map;
462 for (probe_type = probe_types; info->mtd == NULL && *probe_type != NULL;
464 info->mtd = do_map_probe(*probe_type, map);
466 if (info->mtd == NULL) {
467 MAP_ERR(map, "map_probe failed\n");
472 adm5120_flash_initbanks(info);
474 if (info->mtd->size < info->amap.chip_size) {
475 /* readjust resources */
477 release_resource(info->res);
480 info->amap.chip_size = info->mtd->size;
481 map->size = info->mtd->size;
482 MAP_INFO(map, "reducing map size to %ldKiB\n",
483 (unsigned long)map->size >> 10);
484 err = adm5120_flash_initres(info);
489 MAP_INFO(map, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
490 (unsigned long)map->phys, (unsigned long)info->mtd->size >> 10,
493 info->mtd->owner = THIS_MODULE;
495 err = adm5120_flash_initparts(info);
499 if (info->nr_parts == 0) {
500 MAP_INFO(map, "no partitions available, registering whole flash\n");
501 add_mtd_device(info->mtd);
507 adm5120_flash_remove(dev);
512 static int adm5120_flash_suspend(struct platform_device *dev, pm_message_t state)
514 struct adm5120_flash_info *info = platform_get_drvdata(dev);
518 ret = info->mtd->suspend(info->mtd);
523 static int adm5120_flash_resume(struct platform_device *dev)
525 struct adm5120_flash_info *info = platform_get_drvdata(dev);
528 info->mtd->resume(info->mtd);
533 static void adm5120_flash_shutdown(struct platform_device *dev)
535 struct adm5120_flash_info *info = platform_get_drvdata(dev);
537 if (info && info->mtd->suspend(info->mtd) == 0)
538 info->mtd->resume(info->mtd);
542 static struct platform_driver adm5120_flash_driver = {
543 .probe = adm5120_flash_probe,
544 .remove = adm5120_flash_remove,
546 .suspend = adm5120_flash_suspend,
547 .resume = adm5120_flash_resume,
548 .shutdown = adm5120_flash_shutdown,
555 static int __init adm5120_flash_init(void)
559 err = platform_driver_register(&adm5120_flash_driver);
564 static void __exit adm5120_flash_exit(void)
566 platform_driver_unregister(&adm5120_flash_driver);
569 module_init(adm5120_flash_init);
570 module_exit(adm5120_flash_exit);
572 MODULE_LICENSE("GPL");
573 MODULE_AUTHOR("Gabor Juhos <juhosg@freemail.hu>");
574 MODULE_DESCRIPTION(DRV_DESC);