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
106 #define BANK_SIZE (2<<20)
107 #define BANK_SIZE_MAX (4<<20)
108 #define BANK_OFFS_MASK (BANK_SIZE-1)
109 #define BANK_START_MASK (~BANK_OFFS_MASK)
111 static inline struct adm5120_map_info *map_to_amap(struct map_info *map)
113 return (struct adm5120_map_info *)map;
116 static void adm5120_flash_switchbank(struct map_info *map,
119 struct adm5120_map_info *amap = map_to_amap(map);
122 if (amap->switch_bank == NULL)
125 bank = (ofs & BANK_START_MASK) >> 21;
129 MAP_DBG(map, "switching to bank %u, ofs=%lX\n", bank, ofs);
130 amap->switch_bank(bank);
133 static map_word adm5120_flash_read(struct map_info *map, unsigned long ofs)
135 struct adm5120_map_info *amap = map_to_amap(map);
138 MAP_DBG(map, "reading from ofs %lX\n", ofs);
140 if (ofs >= amap->window_size)
141 return map_word_ff(map);
144 adm5120_flash_switchbank(map, ofs);
145 ret = inline_map_read(map, (ofs & (amap->window_size-1)));
151 static void adm5120_flash_write(struct map_info *map, const map_word datum,
154 struct adm5120_map_info *amap = map_to_amap(map);
156 MAP_DBG(map, "writing to ofs %lX\n", ofs);
158 if (ofs > amap->window_size)
162 adm5120_flash_switchbank(map, ofs);
163 inline_map_write(map, datum, (ofs & (amap->window_size-1)));
167 static void adm5120_flash_copy_from(struct map_info *map, void *to,
168 unsigned long from, ssize_t len)
170 struct adm5120_map_info *amap = map_to_amap(map);
174 MAP_DBG(map, "copy_from, to=%lX, from=%lX, len=%lX\n",
175 (unsigned long)to, from, (unsigned long)len);
177 if (from > amap->window_size)
183 if ((from < BANK_SIZE) && ((from+len) > BANK_SIZE))
187 MAP_DBG(map, "copying %lu byte(s) from %lX to %lX\n",
188 (unsigned long)t, (from & (amap->window_size-1)),
190 adm5120_flash_switchbank(map, from);
191 inline_map_copy_from(map, p, (from & (amap->window_size-1)), t);
199 static int adm5120_flash_initres(struct adm5120_flash_info *info)
201 struct map_info *map = &info->amap.map;
204 info->res = request_mem_region(map->phys, info->amap.window_size,
206 if (info->res == NULL) {
207 MAP_ERR(map, "could not reserve memory region\n");
212 map->virt = ioremap_nocache(map->phys, info->amap.window_size);
213 if (map->virt == NULL) {
214 MAP_ERR(map, "failed to ioremap flash region\n");
223 static int adm5120_flash_initinfo(struct adm5120_flash_info *info,
224 struct platform_device *dev)
226 struct map_info *map = &info->amap.map;
227 struct adm5120_flash_platform_data *pdata = dev->dev.platform_data;
228 struct flash_desc *fdesc;
231 map->name = dev_name(&dev->dev);
234 MAP_ERR(map, "invalid flash id\n");
238 fdesc = &flash_descs[dev->id];
241 info->amap.window_size = pdata->window_size;
243 if (info->amap.window_size == 0) {
244 /* get memory window size */
245 t = SW_READ_REG(SWITCH_REG_MEMCTRL) >> fdesc->srs_shift;
246 t &= MEMCTRL_SRS_MASK;
247 info->amap.window_size = flash_sizes[t];
250 if (info->amap.window_size == 0) {
251 MAP_ERR(map, "unable to determine window size\n");
255 /* get flash bus width */
258 t = MPMC_READ_REG(SC1) & SC_MW_MASK;
261 t = MPMC_READ_REG(SC0) & SC_MW_MASK;
264 map->bankwidth = flash_bankwidths[t];
265 if (map->bankwidth == 0) {
266 MAP_ERR(map, "invalid bus width detected\n");
270 map->phys = fdesc->phys;
271 map->size = BANK_SIZE_MAX;
273 simple_map_init(map);
274 map->read = adm5120_flash_read;
275 map->write = adm5120_flash_write;
276 map->copy_from = adm5120_flash_copy_from;
279 map->set_vpp = pdata->set_vpp;
280 info->amap.switch_bank = pdata->switch_bank;
285 MAP_INFO(map, "probing at 0x%lX, size:%ldKiB, width:%d bits\n",
286 (unsigned long)map->phys,
287 (unsigned long)info->amap.window_size >> 10,
296 static void adm5120_flash_initbanks(struct adm5120_flash_info *info)
298 struct map_info *map = &info->amap.map;
300 if (info->mtd->size <= BANK_SIZE)
301 /* no bank switching needed */
304 if (info->amap.switch_bank) {
305 info->amap.window_size = info->mtd->size;
309 MAP_ERR(map, "reduce visibility from %ldKiB to %ldKiB\n",
310 (unsigned long)map->size >> 10,
311 (unsigned long)info->mtd->size >> 10);
313 info->mtd->size = info->amap.window_size;
316 static int adm5120_flash_remove(struct platform_device *dev)
318 struct adm5120_flash_info *info;
320 info = platform_get_drvdata(dev);
324 platform_set_drvdata(dev, NULL);
326 if (info->mtd != NULL) {
327 mtd_device_unregister(info->mtd);
328 map_destroy(info->mtd);
331 if (info->amap.map.virt != NULL)
332 iounmap(info->amap.map.virt);
334 if (info->res != NULL) {
335 release_resource(info->res);
342 static int adm5120_flash_probe(struct platform_device *dev)
344 struct adm5120_flash_platform_data *pdata;
345 struct adm5120_flash_info *info;
346 struct map_info *map;
347 const char **probe_type;
350 pdata = dev->dev.platform_data;
352 dev_err(&dev->dev, "no platform data\n");
356 info = kzalloc(sizeof(*info), GFP_KERNEL);
362 platform_set_drvdata(dev, info);
364 err = adm5120_flash_initinfo(info, dev);
368 err = adm5120_flash_initres(info);
372 map = &info->amap.map;
373 for (probe_type = probe_types; info->mtd == NULL && *probe_type != NULL;
375 info->mtd = do_map_probe(*probe_type, map);
377 if (info->mtd == NULL) {
378 MAP_ERR(map, "map_probe failed\n");
383 adm5120_flash_initbanks(info);
385 if (info->mtd->size < info->amap.window_size) {
386 /* readjust resources */
388 release_resource(info->res);
391 info->amap.window_size = info->mtd->size;
392 map->size = info->mtd->size;
393 MAP_INFO(map, "reducing map size to %ldKiB\n",
394 (unsigned long)map->size >> 10);
395 err = adm5120_flash_initres(info);
400 MAP_INFO(map, "found at 0x%lX, size:%ldKiB, width:%d bits\n",
401 (unsigned long)map->phys, (unsigned long)info->mtd->size >> 10,
404 info->mtd->owner = THIS_MODULE;
406 err = mtd_device_parse_register(info->mtd, parse_types, 0,
407 pdata->parts, pdata->nr_parts);
414 adm5120_flash_remove(dev);
419 static int adm5120_flash_suspend(struct platform_device *dev,
422 struct adm5120_flash_info *info = platform_get_drvdata(dev);
426 ret = info->mtd->suspend(info->mtd);
431 static int adm5120_flash_resume(struct platform_device *dev)
433 struct adm5120_flash_info *info = platform_get_drvdata(dev);
436 info->mtd->resume(info->mtd);
441 static void adm5120_flash_shutdown(struct platform_device *dev)
443 struct adm5120_flash_info *info = platform_get_drvdata(dev);
445 if (info && info->mtd->suspend(info->mtd) == 0)
446 info->mtd->resume(info->mtd);
450 static struct platform_driver adm5120_flash_driver = {
451 .probe = adm5120_flash_probe,
452 .remove = adm5120_flash_remove,
454 .suspend = adm5120_flash_suspend,
455 .resume = adm5120_flash_resume,
456 .shutdown = adm5120_flash_shutdown,
463 static int __init adm5120_flash_init(void)
467 err = platform_driver_register(&adm5120_flash_driver);
472 static void __exit adm5120_flash_exit(void)
474 platform_driver_unregister(&adm5120_flash_driver);
477 module_init(adm5120_flash_init);
478 module_exit(adm5120_flash_exit);
480 MODULE_LICENSE("GPL v2");
481 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
482 MODULE_DESCRIPTION(DRV_DESC);