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