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;
67 static DEFINE_SPINLOCK(adm5120_flash_spin);
68 #define FLASH_LOCK() spin_lock(&adm5120_flash_spin)
69 #define FLASH_UNLOCK() spin_unlock(&adm5120_flash_spin)
71 static u32 flash_bankwidths[4] = { 1, 2, 4, 0 };
73 static u32 flash_sizes[8] = {
74 0, 512*1024, 1024*1024, 2*1024*1024,
78 static struct flash_desc flash_descs[2] = {
80 .phys = ADM5120_SRAM0_BASE,
81 .srs_shift = MEMCTRL_SRS0_SHIFT,
83 .phys = ADM5120_SRAM1_BASE,
84 .srs_shift = MEMCTRL_SRS1_SHIFT,
88 static const char const *probe_types[] = {
95 static const char const *parse_types[] = {
97 #ifdef CONFIG_MTD_REDBOOT_PARTS
100 #ifdef CONFIG_MTD_MYLOADER_PARTS
105 #define BANK_SIZE (2<<20)
106 #define BANK_SIZE_MAX (4<<20)
107 #define BANK_OFFS_MASK (BANK_SIZE-1)
108 #define BANK_START_MASK (~BANK_OFFS_MASK)
110 static inline struct adm5120_map_info *map_to_amap(struct map_info *map)
112 return (struct adm5120_map_info *)map;
115 static void adm5120_flash_switchbank(struct map_info *map,
118 struct adm5120_map_info *amap = map_to_amap(map);
121 if (amap->switch_bank == NULL)
124 bank = (ofs & BANK_START_MASK) >> 21;
128 MAP_DBG(map, "switching to bank %u, ofs=%lX\n", bank, ofs);
129 amap->switch_bank(bank);
132 static map_word adm5120_flash_read(struct map_info *map, unsigned long ofs)
134 struct adm5120_map_info *amap = map_to_amap(map);
137 MAP_DBG(map, "reading from ofs %lX\n", ofs);
139 if (ofs >= amap->window_size)
140 return map_word_ff(map);
143 adm5120_flash_switchbank(map, ofs);
144 ret = inline_map_read(map, (ofs & (amap->window_size-1)));
150 static void adm5120_flash_write(struct map_info *map, const map_word datum,
153 struct adm5120_map_info *amap = map_to_amap(map);
155 MAP_DBG(map, "writing to ofs %lX\n", ofs);
157 if (ofs > amap->window_size)
161 adm5120_flash_switchbank(map, ofs);
162 inline_map_write(map, datum, (ofs & (amap->window_size-1)));
166 static void adm5120_flash_copy_from(struct map_info *map, void *to,
167 unsigned long from, ssize_t len)
169 struct adm5120_map_info *amap = map_to_amap(map);
173 MAP_DBG(map, "copy_from, to=%lX, from=%lX, len=%lX\n",
174 (unsigned long)to, from, (unsigned long)len);
176 if (from > amap->window_size)
182 if ((from < BANK_SIZE) && ((from+len) > BANK_SIZE))
186 MAP_DBG(map, "copying %lu byte(s) from %lX to %lX\n",
187 (unsigned long)t, (from & (amap->window_size-1)),
189 adm5120_flash_switchbank(map, from);
190 inline_map_copy_from(map, p, (from & (amap->window_size-1)), t);
198 static int adm5120_flash_initres(struct adm5120_flash_info *info)
200 struct map_info *map = &info->amap.map;
203 info->res = request_mem_region(map->phys, info->amap.window_size,
205 if (info->res == NULL) {
206 MAP_ERR(map, "could not reserve memory region\n");
211 map->virt = ioremap_nocache(map->phys, info->amap.window_size);
212 if (map->virt == NULL) {
213 MAP_ERR(map, "failed to ioremap flash region\n");
222 static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
223 struct platform_device *dev)
225 struct map_info *map = &info->amap.map;
226 struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
227 struct flash_desc *fdesc;
230 map->name = dev_name(&dev->dev);
233 MAP_ERR(map, "invalid flash id\n");
237 fdesc = &flash_descs[dev->id];
240 info->amap.window_size = pdata->window_size;
242 if (info->amap.window_size == 0) {
243 /* get memory window size */
244 t = SW_READ_REG(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
245 t &= MEMCTRL_SRS_MASK;
246 info->amap.window_size = flash_sizes[t];
249 if (info->amap.window_size == 0) {
250 MAP_ERR(map, "unable to determine window size\n");
254 /* get flash bus width */
257 t = MPMC_READ_REG(SC1) & SC_MW_MASK;
260 t = MPMC_READ_REG(SC0) & SC_MW_MASK;
263 map->bankwidth = flash_bankwidths[t];
264 if (map->bankwidth == 0) {
265 MAP_ERR(map, "invalid bus width detected\n");
269 map->phys = fdesc->phys;
270 map->size = BANK_SIZE_MAX;
272 simple_map_init(map);
273 map->read = adm5120_flash_read;
274 map->write = adm5120_flash_write;
275 map->copy_from = adm5120_flash_copy_from;
278 map->set_vpp = pdata->set_vpp;
279 info->amap.switch_bank = pdata->switch_bank;
284 MAP_INFO(map, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
285 (unsigned long)map->phys,
286 (unsigned long)info->amap.window_size >> 10,
295 static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
297 struct map_info *map = &info->amap.map;
299 if (info->mtd->size <= BANK_SIZE)
300 /* no bank switching needed */
303 if (info->amap.switch_bank) {
304 info->amap.window_size = info->mtd->size;
308 MAP_ERR(map, "reduce visibility from %ldKiB to %ldKiB\n",
309 (unsigned long)map->size >> 10,
310 (unsigned long)info->mtd->size >> 10);
312 info->mtd->size = info->amap.window_size;
315 static int adm5120_flash_remove(struct platform_device *dev)
317 struct adm5120_flash_info *info;
319 info = platform_get_drvdata(dev);
323 platform_set_drvdata(dev, NULL);
325 if (info->mtd != NULL) {
326 mtd_device_unregister(info->mtd);
327 map_destroy(info->mtd);
330 if (info->amap.map.virt != NULL)
331 iounmap(info->amap.map.virt);
333 if (info->res != NULL) {
334 release_resource(info->res);
341 static int adm5120_flash_probe(struct platform_device *dev)
343 struct adm5120_flash_platform_data *pdata;
344 struct adm5120_flash_info *info;
345 struct map_info *map;
346 const char **probe_type;
349 pdata = dev->dev.platform_data;
351 dev_err(&dev->dev, "no platform data\n");
355 info = kzalloc(sizeof(*info), GFP_KERNEL);
361 platform_set_drvdata(dev, info);
363 err = adm5120_flash_initinfo(info, dev);
367 err = adm5120_flash_initres(info);
371 map = &info->amap.map;
372 for (probe_type = probe_types; info->mtd == NULL && *probe_type != NULL;
374 info->mtd = do_map_probe(*probe_type, map);
376 if (info->mtd == NULL) {
377 MAP_ERR(map, "map_probe failed\n");
382 adm5120_flash_initbanks(info);
384 if (info->mtd->size < info->amap.window_size) {
385 /* readjust resources */
387 release_resource(info->res);
390 info->amap.window_size = info->mtd->size;
391 map->size = info->mtd->size;
392 MAP_INFO(map, "reducing map size to %ldKiB\n",
393 (unsigned long)map->size >> 10);
394 err = adm5120_flash_initres(info);
399 MAP_INFO(map, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
400 (unsigned long)map->phys, (unsigned long)info->mtd->size >> 10,
403 info->mtd->owner = THIS_MODULE;
405 err = mtd_device_parse_register(info->mtd, parse_types, 0,
406 pdata->parts, pdata->nr_parts);
413 adm5120_flash_remove(dev);
418 static int adm5120_flash_suspend(struct platform_device *dev,
421 struct adm5120_flash_info *info = platform_get_drvdata(dev);
425 ret = info->mtd->suspend(info->mtd);
430 static int adm5120_flash_resume(struct platform_device *dev)
432 struct adm5120_flash_info *info = platform_get_drvdata(dev);
435 info->mtd->resume(info->mtd);
440 static void adm5120_flash_shutdown(struct platform_device *dev)
442 struct adm5120_flash_info *info = platform_get_drvdata(dev);
444 if (info && info->mtd->suspend(info->mtd) == 0)
445 info->mtd->resume(info->mtd);
449 static struct platform_driver adm5120_flash_driver = {
450 .probe = adm5120_flash_probe,
451 .remove = adm5120_flash_remove,
453 .suspend = adm5120_flash_suspend,
454 .resume = adm5120_flash_resume,
455 .shutdown = adm5120_flash_shutdown,
462 static int __init adm5120_flash_init(void)
466 err = platform_driver_register(&adm5120_flash_driver);
471 static void __exit adm5120_flash_exit(void)
473 platform_driver_unregister(&adm5120_flash_driver);
476 module_init(adm5120_flash_init);
477 module_exit(adm5120_flash_exit);
479 MODULE_LICENSE("GPL v2");
480 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
481 MODULE_DESCRIPTION(DRV_DESC);