Root/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/ar91xx_flash.h>
26
27#define DRV_NAME "ar91xx-flash"
28
29struct 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
38static 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
52static 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
63static map_word ar91xx_flash_read_lock(struct map_info *map, unsigned long ofs)
64{
65    map_word ret;
66
67    ar71xx_flash_acquire();
68    ret = ar91xx_flash_read(map, ofs);
69    ar71xx_flash_release();
70
71    return ret;
72}
73
74static void ar91xx_flash_write_lock(struct map_info *map, map_word d,
75                   unsigned long ofs)
76{
77    ar71xx_flash_acquire();
78    ar91xx_flash_write(map, d, ofs);
79    ar71xx_flash_release();
80}
81
82static void ar91xx_flash_copy_from_lock(struct map_info *map, void *to,
83                    unsigned long from, ssize_t len)
84{
85    ar71xx_flash_acquire();
86    inline_map_copy_from(map, to, from, len);
87    ar71xx_flash_release();
88}
89
90static void ar91xx_flash_copy_to_lock(struct map_info *map, unsigned long to,
91                      const void *from, ssize_t len)
92{
93    ar71xx_flash_acquire();
94    inline_map_copy_to(map, to, from, len);
95    ar71xx_flash_release();
96}
97
98static int ar91xx_flash_remove(struct platform_device *pdev)
99{
100    struct ar91xx_flash_platform_data *pdata;
101    struct ar91xx_flash_info *info;
102
103    info = platform_get_drvdata(pdev);
104    if (info == NULL)
105        return 0;
106
107    platform_set_drvdata(pdev, NULL);
108
109    if (info->mtd == NULL)
110        return 0;
111
112    pdata = pdev->dev.platform_data;
113#ifdef CONFIG_MTD_PARTITIONS
114    if (info->nr_parts) {
115        del_mtd_partitions(info->mtd);
116        kfree(info->parts);
117    } else if (pdata->nr_parts) {
118        del_mtd_partitions(info->mtd);
119    } else {
120        del_mtd_device(info->mtd);
121    }
122#else
123    del_mtd_device(info->mtd);
124#endif
125    map_destroy(info->mtd);
126
127    return 0;
128}
129
130static const char *rom_probe_types[] = { "cfi_probe", "jedec_probe", NULL };
131#ifdef CONFIG_MTD_PARTITIONS
132static const char *part_probe_types[] = { "cmdlinepart", "RedBoot", NULL };
133#endif
134
135static int ar91xx_flash_probe(struct platform_device *pdev)
136{
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;
142    int err = 0;
143
144    pdata = pdev->dev.platform_data;
145    if (pdata == NULL)
146        return -EINVAL;
147
148    info = devm_kzalloc(&pdev->dev, sizeof(struct ar91xx_flash_info),
149                GFP_KERNEL);
150    if (info == NULL) {
151        err = -ENOMEM;
152        goto err_out;
153    }
154
155    platform_set_drvdata(pdev, info);
156
157    res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
158    if (res == NULL) {
159        err = -ENOENT;
160        goto err_out;
161    }
162
163    dev_info(&pdev->dev, "%.8llx at %.8llx\n",
164         (unsigned long long)(res->end - res->start + 1),
165         (unsigned long long)res->start);
166
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");
172        err = -ENOMEM;
173        goto err_out;
174    }
175
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;
180
181    info->map.virt = devm_ioremap(&pdev->dev, info->map.phys,
182                      info->map.size);
183    if (info->map.virt == NULL) {
184        dev_err(&pdev->dev, "failed to ioremap flash region\n");
185        err = -EIO;
186        goto err_out;
187    }
188
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;
195    } else {
196        info->map.read = ar91xx_flash_read;
197        info->map.write = ar91xx_flash_write;
198    }
199
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);
203
204    if (info->mtd == NULL) {
205        dev_err(&pdev->dev, "map_probe failed\n");
206        err = -ENXIO;
207        goto err_out;
208    }
209
210    info->mtd->owner = THIS_MODULE;
211
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);
216        return 0;
217    }
218
219    err = parse_mtd_partitions(info->mtd, part_probe_types,
220                   &info->parts, 0);
221    if (err > 0) {
222        add_mtd_partitions(info->mtd, info->parts, err);
223        return 0;
224    }
225#endif
226
227    add_mtd_device(info->mtd);
228    return 0;
229
230err_out:
231    ar91xx_flash_remove(pdev);
232    return err;
233}
234
235#ifdef CONFIG_PM
236static int ar91xx_flash_suspend(struct platform_device *dev, pm_message_t state)
237{
238    struct ar91xx_flash_info *info = platform_get_drvdata(dev);
239    int ret = 0;
240
241    if (info->mtd->suspend)
242        ret = info->mtd->suspend(info->mtd);
243
244    if (ret)
245        goto fail;
246
247    return 0;
248
249fail:
250    if (info->mtd->suspend) {
251        BUG_ON(!info->mtd->resume);
252        info->mtd->resume(info->mtd);
253    }
254
255    return ret;
256}
257
258static int ar91xx_flash_resume(struct platform_device *pdev)
259{
260    struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
261
262    if (info->mtd->resume)
263        info->mtd->resume(info->mtd);
264
265    return 0;
266}
267
268static void ar91xx_flash_shutdown(struct platform_device *pdev)
269{
270    struct ar91xx_flash_info *info = platform_get_drvdata(pdev);
271
272    if (info->mtd->suspend && info->mtd->resume)
273        if (info->mtd->suspend(info->mtd) == 0)
274            info->mtd->resume(info->mtd);
275}
276#else
277#define ar91xx_flash_suspend NULL
278#define ar91xx_flash_resume NULL
279#define ar91xx_flash_shutdown NULL
280#endif
281
282static 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,
288    .driver = {
289        .name = DRV_NAME,
290        .owner = THIS_MODULE,
291    },
292};
293
294static int __init ar91xx_flash_init(void)
295{
296    return platform_driver_register(&ar91xx_flash_driver);
297}
298
299static void __exit ar91xx_flash_exit(void)
300{
301    platform_driver_unregister(&ar91xx_flash_driver);
302}
303
304module_init(ar91xx_flash_init);
305module_exit(ar91xx_flash_exit);
306
307MODULE_LICENSE("GPL v2");
308MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
309MODULE_DESCRIPTION("Parallel flash driver for the Atheros AR91xx SoC");
310MODULE_ALIAS("platform:" DRV_NAME);
311

Archive Download this file



interactive