2 * Parallel flash driver for the Atheros AR91xx SoC
4 * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
12 #include <linux/module.h>
13 #include <linux/types.h>
14 #include <linux/kernel.h>
15 #include <linux/init.h>
16 #include <linux/slab.h>
17 #include <linux/device.h>
18 #include <linux/platform_device.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/map.h>
21 #include <linux/mtd/partitions.h>
24 #include <asm/mach-ar71xx/ar71xx.h>
25 #include <asm/mach-ar71xx/ar91xx_flash.h>
27 #define DRV_NAME "ar91xx-flash"
29 struct ar91xx_flash_info {
32 #ifdef CONFIG_MTD_PARTITIONS
34 struct mtd_partition *parts;
38 static map_word ar91xx_flash_read(struct map_info *map, unsigned long ofs)
42 if (map_bankwidth_is_1(map))
43 val.x[0] = __raw_readb(map->virt + (ofs ^ 3));
44 else if (map_bankwidth_is_2(map))
45 val.x[0] = __raw_readw(map->virt + (ofs ^ 2));
47 val = map_word_ff(map);
52 static void ar91xx_flash_write(struct map_info *map, map_word d,
55 if (map_bankwidth_is_1(map))
56 __raw_writeb(d.x[0], map->virt + (ofs ^ 3));
57 else if (map_bankwidth_is_2(map))
58 __raw_writew(d.x[0], map->virt + (ofs ^ 2));
63 static map_word ar91xx_flash_read_lock(struct map_info *map, unsigned long ofs)
67 ar71xx_flash_acquire();
68 ret = ar91xx_flash_read(map, ofs);
69 ar71xx_flash_release();
74 static void ar91xx_flash_write_lock(struct map_info *map, map_word d,
77 ar71xx_flash_acquire();
78 ar91xx_flash_write(map, d, ofs);
79 ar71xx_flash_release();
82 static void ar91xx_flash_copy_from_lock(struct map_info *map, void *to,
83 unsigned long from, ssize_t len)
85 ar71xx_flash_acquire();
86 inline_map_copy_from(map, to, from, len);
87 ar71xx_flash_release();
90 static void ar91xx_flash_copy_to_lock(struct map_info *map, unsigned long to,
91 const void *from, ssize_t len)
93 ar71xx_flash_acquire();
94 inline_map_copy_to(map, to, from, len);
95 ar71xx_flash_release();
98 static int ar91xx_flash_remove(struct platform_device *pdev)
100 struct ar91xx_flash_platform_data *pdata;
101 struct ar91xx_flash_info *info;
103 info = platform_get_drvdata(pdev);
107 platform_set_drvdata(pdev, NULL);
109 if (info->mtd == NULL)
112 pdata = pdev->dev.platform_data;
113 #ifdef CONFIG_MTD_PARTITIONS
114 if (info->nr_parts) {
115 del_mtd_partitions(info->mtd);
117 } else if (pdata->nr_parts) {
118 del_mtd_partitions(info->mtd);
120 del_mtd_device(info->mtd);
123 del_mtd_device(info->mtd);
125 map_destroy(info->mtd);
130 static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
131 #ifdef CONFIG_MTD_PARTITIONS
132 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
135 static int ar91xx_flash_probe(struct platform_device *pdev)
137 struct ar91xx_flash_platform_data *pdata;
138 struct ar91xx_flash_info *info;
139 struct resource *res;
140 struct resource *region;
141 const char **probe_type;
144 pdata = pdev->dev.platform_data;
148 info = devm_kzalloc(&pdev->dev, sizeof(struct ar91xx_flash_info),
155 platform_set_drvdata(pdev, info);
157 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
163 dev_info(&pdev->dev, "%.8llx at %.8llx\n",
164 (unsigned long long)(res->end - res->start + 1),
165 (unsigned long long)res->start);
167 region = devm_request_mem_region(&pdev->dev,
168 res->start, res->end - res->start + 1,
169 dev_name(&pdev->dev));
170 if (region == NULL) {
171 dev_err(&pdev->dev, "could not reserve memory region\n");
176 info->map.name = dev_name(&pdev->dev);
177 info->map.phys = res->start;
178 info->map.size = res->end - res->start + 1;
179 info->map.bankwidth = pdata->width;
181 info->map.virt = devm_ioremap(&pdev->dev, info->map.phys,
183 if (info->map.virt == NULL) {
184 dev_err(&pdev->dev, "failed to ioremap flash region\n");
189 simple_map_init(&info->map);
190 if (pdata->is_shared) {
191 info->map.read = ar91xx_flash_read_lock;
192 info->map.write = ar91xx_flash_write_lock;
193 info->map.copy_from = ar91xx_flash_copy_from_lock;
194 info->map.copy_to = ar91xx_flash_copy_to_lock;
196 info->map.read = ar91xx_flash_read;
197 info->map.write = ar91xx_flash_write;
200 probe_type = rom_probe_types;
201 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
202 info->mtd = do_map_probe(*probe_type, &info->map);
204 if (info->mtd == NULL) {
205 dev_err(&pdev->dev, "map_probe failed\n");
210 info->mtd->owner = THIS_MODULE;
212 #ifdef CONFIG_MTD_PARTITIONS
213 if (pdata->nr_parts) {
214 dev_info(&pdev->dev, "using static partition mapping\n");
215 add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
219 err = parse_mtd_partitions(info->mtd, part_probe_types,
222 add_mtd_partitions(info->mtd, info->parts, err);
227 add_mtd_device(info->mtd);
231 ar91xx_flash_remove(pdev);
236 static int ar91xx_flash_suspend(struct platform_device *dev, pm_message_t state)
238 struct ar91xx_flash_info *info = platform_get_drvdata(dev);
241 if (info->mtd->suspend)
242 ret = info->mtd->suspend(info->mtd);
250 if (info->mtd->suspend) {
251 BUG_ON(!info->mtd->resume);
252 info->mtd->resume(info->mtd);
258 static int ar91xx_flash_resume(struct platform_device *pdev)
260 struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
262 if (info->mtd->resume)
263 info->mtd->resume(info->mtd);
268 static void ar91xx_flash_shutdown(struct platform_device *pdev)
270 struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
272 if (info->mtd->suspend && info->mtd->resume)
273 if (info->mtd->suspend(info->mtd) == 0)
274 info->mtd->resume(info->mtd);
277 #define ar91xx_flash_suspend NULL
278 #define ar91xx_flash_resume NULL
279 #define ar91xx_flash_shutdown NULL
282 static struct platform_driver ar91xx_flash_driver = {
283 .probe = ar91xx_flash_probe,
284 .remove = ar91xx_flash_remove,
285 .suspend = ar91xx_flash_suspend,
286 .resume = ar91xx_flash_resume,
287 .shutdown = ar91xx_flash_shutdown,
290 .owner = THIS_MODULE,
294 static int __init ar91xx_flash_init(void)
296 return platform_driver_register(&ar91xx_flash_driver);
299 static void __exit ar91xx_flash_exit(void)
301 platform_driver_unregister(&ar91xx_flash_driver);
304 module_init(ar91xx_flash_init);
305 module_exit(ar91xx_flash_exit);
307 MODULE_LICENSE("GPL v2");
308 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
309 MODULE_DESCRIPTION("Parallel flash driver for the Atheros AR91xx SoC");
310 MODULE_ALIAS("platform:" DRV_NAME);