add parallel flash driver
[librecmc/librecmc.git] / target / linux / ar71xx / files / drivers / mtd / maps / ar91xx_flash.c
1 /*
2  * Parallel flash driver for the Atheros AR91xx SoC
3  *
4  * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
5  *
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.
9  *
10  */
11
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>
22 #include <linux/io.h>
23
24 #include <asm/mach-ar71xx/ar71xx.h>
25 #include <asm/mach-ar71xx/platform.h>
26
27 #define DRV_NAME        "ar91xx-flash"
28
29 struct ar91xx_flash_info {
30         struct mtd_info         *mtd;
31         struct map_info         map;
32 #ifdef CONFIG_MTD_PARTITIONS
33         int                     nr_parts;
34         struct mtd_partition    *parts;
35 #endif
36 };
37
38 static map_word ar91xx_flash_read(struct map_info *map, unsigned long ofs)
39 {
40         map_word val;
41
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));
46         else
47                 val = map_word_ff(map);
48
49         return val;
50 }
51
52 static void ar91xx_flash_write(struct map_info *map, map_word d,
53                                unsigned long ofs)
54 {
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));
59
60         mb();
61 }
62
63 static int ar91xx_flash_remove(struct platform_device *pdev)
64 {
65         struct ar91xx_flash_platform_data *pdata;
66         struct ar91xx_flash_info *info;
67
68         info = platform_get_drvdata(pdev);
69         if (info == NULL)
70                 return 0;
71
72         platform_set_drvdata(pdev, NULL);
73
74         if (info->mtd == NULL)
75                 return 0;
76
77         pdata = pdev->dev.platform_data;
78 #ifdef CONFIG_MTD_PARTITIONS
79         if (info->nr_parts) {
80                 del_mtd_partitions(info->mtd);
81                 kfree(info->parts);
82         } else if (pdata->nr_parts) {
83                 del_mtd_partitions(info->mtd);
84         } else {
85                 del_mtd_device(info->mtd);
86         }
87 #else
88         del_mtd_device(info->mtd);
89 #endif
90         map_destroy(info->mtd);
91
92         return 0;
93 }
94
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 };
98 #endif
99
100 static int ar91xx_flash_probe(struct platform_device *pdev)
101 {
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;
107         int err = 0;
108
109         pdata = pdev->dev.platform_data;
110         if (pdata == NULL)
111                 return -EINVAL;
112
113         info = devm_kzalloc(&pdev->dev, sizeof(struct ar91xx_flash_info),
114                             GFP_KERNEL);
115         if (info == NULL) {
116                 err = -ENOMEM;
117                 goto err_out;
118         }
119
120         platform_set_drvdata(pdev, info);
121
122         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123         if (res == NULL) {
124                 err = -ENOENT;
125                 goto err_out;
126         }
127
128         dev_info(&pdev->dev, "%.8llx at %.8llx\n",
129                  (unsigned long long)(res->end - res->start + 1),
130                  (unsigned long long)res->start);
131
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");
137                 err = -ENOMEM;
138                 goto err_out;
139         }
140
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;
145
146         info->map.virt = devm_ioremap(&pdev->dev, info->map.phys,
147                                       info->map.size);
148         if (info->map.virt == NULL) {
149                 dev_err(&pdev->dev, "failed to ioremap flash region\n");
150                 err = -EIO;
151                 goto err_out;
152         }
153
154         simple_map_init(&info->map);
155         info->map.read = ar91xx_flash_read;
156         info->map.write = ar91xx_flash_write;
157
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);
161
162         if (info->mtd == NULL) {
163                 dev_err(&pdev->dev, "map_probe failed\n");
164                 err = -ENXIO;
165                 goto err_out;
166         }
167
168         info->mtd->owner = THIS_MODULE;
169
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);
174                 return 0;
175         }
176
177         err = parse_mtd_partitions(info->mtd, part_probe_types,
178                                    &info->parts, 0);
179         if (err > 0) {
180                 add_mtd_partitions(info->mtd, info->parts, err);
181                 return 0;
182         }
183 #endif
184
185         add_mtd_device(info->mtd);
186         return 0;
187
188  err_out:
189         ar91xx_flash_remove(pdev);
190         return err;
191 }
192
193 #ifdef CONFIG_PM
194 static int ar91xx_flash_suspend(struct platform_device *dev, pm_message_t state)
195 {
196         struct ar91xx_flash_info *info = platform_get_drvdata(dev);
197         int ret = 0;
198
199         if (info->mtd->suspend)
200                 ret = info->mtd->suspend(info->mtd);
201
202         if (ret)
203                 goto fail;
204
205         return 0;
206
207  fail:
208         if (info->mtd->suspend) {
209                 BUG_ON(!info->mtd->resume);
210                 info->mtd->resume(info->mtd);
211         }
212
213         return ret;
214 }
215
216 static int ar91xx_flash_resume(struct platform_device *pdev)
217 {
218         struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
219
220         if (info->mtd->resume)
221                 info->mtd->resume(info->mtd);
222
223         return 0;
224 }
225
226 static void ar91xx_flash_shutdown(struct platform_device *pdev)
227 {
228         struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
229
230         if (info->mtd->suspend && info->mtd->resume)
231                 if (info->mtd->suspend(info->mtd) == 0)
232                         info->mtd->resume(info->mtd);
233 }
234 #else
235 #define ar91xx_flash_suspend    NULL
236 #define ar91xx_flash_resume     NULL
237 #define ar91xx_flash_shutdown   NULL
238 #endif
239
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,
246         .driver         = {
247                 .name   = DRV_NAME,
248                 .owner  = THIS_MODULE,
249         },
250 };
251
252 static int __init ar91xx_flash_init(void)
253 {
254         return platform_driver_register(&ar91xx_flash_driver);
255 }
256
257 static void __exit ar91xx_flash_exit(void)
258 {
259         platform_driver_unregister(&ar91xx_flash_driver);
260 }
261
262 module_init(ar91xx_flash_init);
263 module_exit(ar91xx_flash_exit);
264
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);