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/platform.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 int ar91xx_flash_remove(struct platform_device *pdev)
65 struct ar91xx_flash_platform_data *pdata;
66 struct ar91xx_flash_info *info;
68 info = platform_get_drvdata(pdev);
72 platform_set_drvdata(pdev, NULL);
74 if (info->mtd == NULL)
77 pdata = pdev->dev.platform_data;
78 #ifdef CONFIG_MTD_PARTITIONS
80 del_mtd_partitions(info->mtd);
82 } else if (pdata->nr_parts) {
83 del_mtd_partitions(info->mtd);
85 del_mtd_device(info->mtd);
88 del_mtd_device(info->mtd);
90 map_destroy(info->mtd);
95 static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
96 #ifdef CONFIG_MTD_PARTITIONS
97 static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
100 static int ar91xx_flash_probe(struct platform_device *pdev)
102 struct ar91xx_flash_platform_data *pdata;
103 struct ar91xx_flash_info *info;
104 struct resource *res;
105 struct resource *region;
106 const char **probe_type;
109 pdata = pdev->dev.platform_data;
113 info = devm_kzalloc(&pdev->dev, sizeof(struct ar91xx_flash_info),
120 platform_set_drvdata(pdev, info);
122 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
128 dev_info(&pdev->dev, "%.8llx at %.8llx\n",
129 (unsigned long long)(res->end - res->start + 1),
130 (unsigned long long)res->start);
132 region = devm_request_mem_region(&pdev->dev,
133 res->start, res->end - res->start + 1,
134 dev_name(&pdev->dev));
135 if (region == NULL) {
136 dev_err(&pdev->dev, "could not reserve memory region\n");
141 info->map.name = dev_name(&pdev->dev);
142 info->map.phys = res->start;
143 info->map.size = res->end - res->start + 1;
144 info->map.bankwidth = pdata->width;
146 info->map.virt = devm_ioremap(&pdev->dev, info->map.phys,
148 if (info->map.virt == NULL) {
149 dev_err(&pdev->dev, "failed to ioremap flash region\n");
154 simple_map_init(&info->map);
155 info->map.read = ar91xx_flash_read;
156 info->map.write = ar91xx_flash_write;
158 probe_type = rom_probe_types;
159 for (; info->mtd == NULL && *probe_type != NULL; probe_type++)
160 info->mtd = do_map_probe(*probe_type, &info->map);
162 if (info->mtd == NULL) {
163 dev_err(&pdev->dev, "map_probe failed\n");
168 info->mtd->owner = THIS_MODULE;
170 #ifdef CONFIG_MTD_PARTITIONS
171 if (pdata->nr_parts) {
172 dev_info(&pdev->dev, "using static partition mapping\n");
173 add_mtd_partitions(info->mtd, pdata->parts, pdata->nr_parts);
177 err = parse_mtd_partitions(info->mtd, part_probe_types,
180 add_mtd_partitions(info->mtd, info->parts, err);
185 add_mtd_device(info->mtd);
189 ar91xx_flash_remove(pdev);
194 static int ar91xx_flash_suspend(struct platform_device *dev, pm_message_t state)
196 struct ar91xx_flash_info *info = platform_get_drvdata(dev);
199 if (info->mtd->suspend)
200 ret = info->mtd->suspend(info->mtd);
208 if (info->mtd->suspend) {
209 BUG_ON(!info->mtd->resume);
210 info->mtd->resume(info->mtd);
216 static int ar91xx_flash_resume(struct platform_device *pdev)
218 struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
220 if (info->mtd->resume)
221 info->mtd->resume(info->mtd);
226 static void ar91xx_flash_shutdown(struct platform_device *pdev)
228 struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
230 if (info->mtd->suspend && info->mtd->resume)
231 if (info->mtd->suspend(info->mtd) == 0)
232 info->mtd->resume(info->mtd);
235 #define ar91xx_flash_suspend NULL
236 #define ar91xx_flash_resume NULL
237 #define ar91xx_flash_shutdown NULL
240 static struct platform_driver ar91xx_flash_driver = {
241 .probe = ar91xx_flash_probe,
242 .remove = ar91xx_flash_remove,
243 .suspend = ar91xx_flash_suspend,
244 .resume = ar91xx_flash_resume,
245 .shutdown = ar91xx_flash_shutdown,
248 .owner = THIS_MODULE,
252 static int __init ar91xx_flash_init(void)
254 return platform_driver_register(&ar91xx_flash_driver);
257 static void __exit ar91xx_flash_exit(void)
259 platform_driver_unregister(&ar91xx_flash_driver);
262 module_init(ar91xx_flash_init);
263 module_exit(ar91xx_flash_exit);
265 MODULE_LICENSE("GPL v2");
266 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
267 MODULE_DESCRIPTION("Parallel flash driver for the Atheros AR91xx SoC");
268 MODULE_ALIAS("platform:" DRV_NAME);