212a02077052119b820a7d5622d8df390a9f4510
[15.05/openwrt.git] / target / linux / lantiq / patches-2.6.39 / 0004-MIPS-Lantiq-Add-NOR-flash-support.patch
1 From cd0d53b24ca744295d2cdf69bb2b659571091b75 Mon Sep 17 00:00:00 2001
2 From: John Crispin <blogic@openwrt.org>
3 Date: Tue, 12 Apr 2011 18:10:01 +0200
4 Subject: [PATCH 04/13] MIPS: Lantiq: Add NOR flash support
5
6 This patch adds the driver/map for NOR devices attached to the SoC via the
7 External Bus Unit (EBU).
8
9 Signed-off-by: John Crispin <blogic@openwrt.org>
10 Signed-off-by: Ralph Hempel <ralph.hempel@lantiq.com>
11 Cc: David Woodhouse <dwmw2@infradead.org>
12 Cc: Daniel Schwierzeck <daniel.schwierzeck@googlemail.com>
13 Cc: linux-mips@linux-mips.org
14 Cc: linux-mtd@lists.infradead.org
15 Acked-by: Artem Bityutskiy <dedekind1@gmail.com>
16 Patchwork: https://patchwork.linux-mips.org/patch/2285/
17 Signed-off-by: Ralf Baechle <ralf@linux-mips.org>
18 ---
19  drivers/mtd/maps/Kconfig        |    7 +
20  drivers/mtd/maps/Makefile       |    1 +
21  drivers/mtd/maps/lantiq-flash.c |  251 +++++++++++++++++++++++++++++++++++++++
22  3 files changed, 259 insertions(+), 0 deletions(-)
23  create mode 100644 drivers/mtd/maps/lantiq-flash.c
24
25 --- a/drivers/mtd/maps/Kconfig
26 +++ b/drivers/mtd/maps/Kconfig
27 @@ -260,6 +260,13 @@ config MTD_BCM963XX
28           Support for parsing CFE image tag and creating MTD partitions on
29           Broadcom BCM63xx boards.
30  
31 +config MTD_LANTIQ
32 +       tristate "Lantiq SoC NOR support"
33 +       depends on LANTIQ
34 +       select MTD_PARTITIONS
35 +       help
36 +         Support for NOR flash attached to the Lantiq SoC's External Bus Unit.
37 +
38  config MTD_DILNETPC
39         tristate "CFI Flash device mapped on DIL/Net PC"
40         depends on X86 && MTD_PARTITIONS && MTD_CFI_INTELEXT && BROKEN
41 --- a/drivers/mtd/maps/Makefile
42 +++ b/drivers/mtd/maps/Makefile
43 @@ -60,3 +60,4 @@ obj-$(CONFIG_MTD_VMU)         += vmu-flash.o
44  obj-$(CONFIG_MTD_GPIO_ADDR)    += gpio-addr-flash.o
45  obj-$(CONFIG_MTD_BCM963XX)     += bcm963xx-flash.o
46  obj-$(CONFIG_MTD_LATCH_ADDR)   += latch-addr-flash.o
47 +obj-$(CONFIG_MTD_LANTIQ)       += lantiq-flash.o
48 --- /dev/null
49 +++ b/drivers/mtd/maps/lantiq-flash.c
50 @@ -0,0 +1,251 @@
51 +/*
52 + *  This program is free software; you can redistribute it and/or modify it
53 + *  under the terms of the GNU General Public License version 2 as published
54 + *  by the Free Software Foundation.
55 + *
56 + *  Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
57 + *  Copyright (C) 2010 John Crispin <blogic@openwrt.org>
58 + */
59 +
60 +#include <linux/module.h>
61 +#include <linux/types.h>
62 +#include <linux/kernel.h>
63 +#include <linux/io.h>
64 +#include <linux/slab.h>
65 +#include <linux/init.h>
66 +#include <linux/mtd/mtd.h>
67 +#include <linux/mtd/map.h>
68 +#include <linux/mtd/partitions.h>
69 +#include <linux/mtd/cfi.h>
70 +#include <linux/platform_device.h>
71 +#include <linux/mtd/physmap.h>
72 +
73 +#include <lantiq_soc.h>
74 +#include <lantiq_platform.h>
75 +
76 +/*
77 + * The NOR flash is connected to the same external bus unit (EBU) as PCI.
78 + * To make PCI work we need to enable the endianness swapping for the address
79 + * written to the EBU. This endianness swapping works for PCI correctly but
80 + * fails for attached NOR devices. To workaround this we need to use a complex
81 + * map. The workaround involves swapping all addresses whilst probing the chip.
82 + * Once probing is complete we stop swapping the addresses but swizzle the
83 + * unlock addresses to ensure that access to the NOR device works correctly.
84 + */
85 +
86 +enum {
87 +       LTQ_NOR_PROBING,
88 +       LTQ_NOR_NORMAL
89 +};
90 +
91 +struct ltq_mtd {
92 +       struct resource *res;
93 +       struct mtd_info *mtd;
94 +       struct map_info *map;
95 +};
96 +
97 +static char ltq_map_name[] = "ltq_nor";
98 +
99 +static map_word
100 +ltq_read16(struct map_info *map, unsigned long adr)
101 +{
102 +       unsigned long flags;
103 +       map_word temp;
104 +
105 +       if (map->map_priv_1 == LTQ_NOR_PROBING)
106 +               adr ^= 2;
107 +       spin_lock_irqsave(&ebu_lock, flags);
108 +       temp.x[0] = *(u16 *)(map->virt + adr);
109 +       spin_unlock_irqrestore(&ebu_lock, flags);
110 +       return temp;
111 +}
112 +
113 +static void
114 +ltq_write16(struct map_info *map, map_word d, unsigned long adr)
115 +{
116 +       unsigned long flags;
117 +
118 +       if (map->map_priv_1 == LTQ_NOR_PROBING)
119 +               adr ^= 2;
120 +       spin_lock_irqsave(&ebu_lock, flags);
121 +       *(u16 *)(map->virt + adr) = d.x[0];
122 +       spin_unlock_irqrestore(&ebu_lock, flags);
123 +}
124 +
125 +/*
126 + * The following 2 functions copy data between iomem and a cached memory
127 + * section. As memcpy() makes use of pre-fetching we cannot use it here.
128 + * The normal alternative of using memcpy_{to,from}io also makes use of
129 + * memcpy() on MIPS so it is not applicable either. We are therefore stuck
130 + * with having to use our own loop.
131 + */
132 +static void
133 +ltq_copy_from(struct map_info *map, void *to,
134 +       unsigned long from, ssize_t len)
135 +{
136 +       unsigned char *f = (unsigned char *)map->virt + from;
137 +       unsigned char *t = (unsigned char *)to;
138 +       unsigned long flags;
139 +
140 +       spin_lock_irqsave(&ebu_lock, flags);
141 +       while (len--)
142 +               *t++ = *f++;
143 +       spin_unlock_irqrestore(&ebu_lock, flags);
144 +}
145 +
146 +static void
147 +ltq_copy_to(struct map_info *map, unsigned long to,
148 +       const void *from, ssize_t len)
149 +{
150 +       unsigned char *f = (unsigned char *)from;
151 +       unsigned char *t = (unsigned char *)map->virt + to;
152 +       unsigned long flags;
153 +
154 +       spin_lock_irqsave(&ebu_lock, flags);
155 +       while (len--)
156 +               *t++ = *f++;
157 +       spin_unlock_irqrestore(&ebu_lock, flags);
158 +}
159 +
160 +static const char const *part_probe_types[] = { "cmdlinepart", NULL };
161 +
162 +static int __init
163 +ltq_mtd_probe(struct platform_device *pdev)
164 +{
165 +       struct physmap_flash_data *ltq_mtd_data = dev_get_platdata(&pdev->dev);
166 +       struct ltq_mtd *ltq_mtd;
167 +       struct mtd_partition *parts;
168 +       struct resource *res;
169 +       int nr_parts = 0;
170 +       struct cfi_private *cfi;
171 +       int err;
172 +
173 +       ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
174 +       platform_set_drvdata(pdev, ltq_mtd);
175 +
176 +       ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
177 +       if (!ltq_mtd->res) {
178 +               dev_err(&pdev->dev, "failed to get memory resource");
179 +               err = -ENOENT;
180 +               goto err_out;
181 +       }
182 +
183 +       res = devm_request_mem_region(&pdev->dev, ltq_mtd->res->start,
184 +               resource_size(ltq_mtd->res), dev_name(&pdev->dev));
185 +       if (!ltq_mtd->res) {
186 +               dev_err(&pdev->dev, "failed to request mem resource");
187 +               err = -EBUSY;
188 +               goto err_out;
189 +       }
190 +
191 +       ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
192 +       ltq_mtd->map->phys = res->start;
193 +       ltq_mtd->map->size = resource_size(res);
194 +       ltq_mtd->map->virt = devm_ioremap_nocache(&pdev->dev,
195 +                               ltq_mtd->map->phys, ltq_mtd->map->size);
196 +       if (!ltq_mtd->map->virt) {
197 +               dev_err(&pdev->dev, "failed to ioremap!\n");
198 +               err = -ENOMEM;
199 +               goto err_free;
200 +       }
201 +
202 +       ltq_mtd->map->name = ltq_map_name;
203 +       ltq_mtd->map->bankwidth = 2;
204 +       ltq_mtd->map->read = ltq_read16;
205 +       ltq_mtd->map->write = ltq_write16;
206 +       ltq_mtd->map->copy_from = ltq_copy_from;
207 +       ltq_mtd->map->copy_to = ltq_copy_to;
208 +
209 +       ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
210 +       ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
211 +       ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
212 +
213 +       if (!ltq_mtd->mtd) {
214 +               dev_err(&pdev->dev, "probing failed\n");
215 +               err = -ENXIO;
216 +               goto err_unmap;
217 +       }
218 +
219 +       ltq_mtd->mtd->owner = THIS_MODULE;
220 +
221 +       cfi = ltq_mtd->map->fldrv_priv;
222 +       cfi->addr_unlock1 ^= 1;
223 +       cfi->addr_unlock2 ^= 1;
224 +
225 +       nr_parts = parse_mtd_partitions(ltq_mtd->mtd,
226 +                               part_probe_types, &parts, 0);
227 +       if (nr_parts > 0) {
228 +               dev_info(&pdev->dev,
229 +                       "using %d partitions from cmdline", nr_parts);
230 +       } else {
231 +               nr_parts = ltq_mtd_data->nr_parts;
232 +               parts = ltq_mtd_data->parts;
233 +       }
234 +
235 +       err = add_mtd_partitions(ltq_mtd->mtd, parts, nr_parts);
236 +       if (err) {
237 +               dev_err(&pdev->dev, "failed to add partitions\n");
238 +               goto err_destroy;
239 +       }
240 +
241 +       return 0;
242 +
243 +err_destroy:
244 +       map_destroy(ltq_mtd->mtd);
245 +err_unmap:
246 +       iounmap(ltq_mtd->map->virt);
247 +err_free:
248 +       kfree(ltq_mtd->map);
249 +err_out:
250 +       kfree(ltq_mtd);
251 +       return err;
252 +}
253 +
254 +static int __devexit
255 +ltq_mtd_remove(struct platform_device *pdev)
256 +{
257 +       struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
258 +
259 +       if (ltq_mtd) {
260 +               if (ltq_mtd->mtd) {
261 +                       del_mtd_partitions(ltq_mtd->mtd);
262 +                       map_destroy(ltq_mtd->mtd);
263 +               }
264 +               if (ltq_mtd->map->virt)
265 +                       iounmap(ltq_mtd->map->virt);
266 +               kfree(ltq_mtd->map);
267 +               kfree(ltq_mtd);
268 +       }
269 +       return 0;
270 +}
271 +
272 +static struct platform_driver ltq_mtd_driver = {
273 +       .remove = __devexit_p(ltq_mtd_remove),
274 +       .driver = {
275 +               .name = "ltq_nor",
276 +               .owner = THIS_MODULE,
277 +       },
278 +};
279 +
280 +static int __init
281 +init_ltq_mtd(void)
282 +{
283 +       int ret = platform_driver_probe(&ltq_mtd_driver, ltq_mtd_probe);
284 +
285 +       if (ret)
286 +               pr_err("ltq_nor: error registering platform driver");
287 +       return ret;
288 +}
289 +
290 +static void __exit
291 +exit_ltq_mtd(void)
292 +{
293 +       platform_driver_unregister(&ltq_mtd_driver);
294 +}
295 +
296 +module_init(init_ltq_mtd);
297 +module_exit(exit_ltq_mtd);
298 +
299 +MODULE_LICENSE("GPL");
300 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
301 +MODULE_DESCRIPTION("Lantiq SoC NOR");