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
6 This patch adds the driver/map for NOR devices attached to the SoC via the
7 External Bus Unit (EBU).
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>
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
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.
32 + tristate "Lantiq SoC NOR support"
34 + select MTD_PARTITIONS
36 + Support for NOR flash attached to the Lantiq SoC's External Bus Unit.
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
49 +++ b/drivers/mtd/maps/lantiq-flash.c
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.
56 + * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
57 + * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
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>
73 +#include <lantiq_soc.h>
74 +#include <lantiq_platform.h>
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.
92 + struct resource *res;
93 + struct mtd_info *mtd;
94 + struct map_info *map;
97 +static char ltq_map_name[] = "ltq_nor";
100 +ltq_read16(struct map_info *map, unsigned long adr)
102 + unsigned long flags;
105 + if (map->map_priv_1 == LTQ_NOR_PROBING)
107 + spin_lock_irqsave(&ebu_lock, flags);
108 + temp.x[0] = *(u16 *)(map->virt + adr);
109 + spin_unlock_irqrestore(&ebu_lock, flags);
114 +ltq_write16(struct map_info *map, map_word d, unsigned long adr)
116 + unsigned long flags;
118 + if (map->map_priv_1 == LTQ_NOR_PROBING)
120 + spin_lock_irqsave(&ebu_lock, flags);
121 + *(u16 *)(map->virt + adr) = d.x[0];
122 + spin_unlock_irqrestore(&ebu_lock, flags);
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.
133 +ltq_copy_from(struct map_info *map, void *to,
134 + unsigned long from, ssize_t len)
136 + unsigned char *f = (unsigned char *)map->virt + from;
137 + unsigned char *t = (unsigned char *)to;
138 + unsigned long flags;
140 + spin_lock_irqsave(&ebu_lock, flags);
143 + spin_unlock_irqrestore(&ebu_lock, flags);
147 +ltq_copy_to(struct map_info *map, unsigned long to,
148 + const void *from, ssize_t len)
150 + unsigned char *f = (unsigned char *)from;
151 + unsigned char *t = (unsigned char *)map->virt + to;
152 + unsigned long flags;
154 + spin_lock_irqsave(&ebu_lock, flags);
157 + spin_unlock_irqrestore(&ebu_lock, flags);
160 +static const char const *part_probe_types[] = { "cmdlinepart", NULL };
163 +ltq_mtd_probe(struct platform_device *pdev)
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;
170 + struct cfi_private *cfi;
173 + ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
174 + platform_set_drvdata(pdev, ltq_mtd);
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");
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");
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");
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;
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;
213 + if (!ltq_mtd->mtd) {
214 + dev_err(&pdev->dev, "probing failed\n");
219 + ltq_mtd->mtd->owner = THIS_MODULE;
221 + cfi = ltq_mtd->map->fldrv_priv;
222 + cfi->addr_unlock1 ^= 1;
223 + cfi->addr_unlock2 ^= 1;
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);
231 + nr_parts = ltq_mtd_data->nr_parts;
232 + parts = ltq_mtd_data->parts;
235 + err = add_mtd_partitions(ltq_mtd->mtd, parts, nr_parts);
237 + dev_err(&pdev->dev, "failed to add partitions\n");
244 + map_destroy(ltq_mtd->mtd);
246 + iounmap(ltq_mtd->map->virt);
248 + kfree(ltq_mtd->map);
254 +static int __devexit
255 +ltq_mtd_remove(struct platform_device *pdev)
257 + struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
260 + if (ltq_mtd->mtd) {
261 + del_mtd_partitions(ltq_mtd->mtd);
262 + map_destroy(ltq_mtd->mtd);
264 + if (ltq_mtd->map->virt)
265 + iounmap(ltq_mtd->map->virt);
266 + kfree(ltq_mtd->map);
272 +static struct platform_driver ltq_mtd_driver = {
273 + .remove = __devexit_p(ltq_mtd_remove),
276 + .owner = THIS_MODULE,
283 + int ret = platform_driver_probe(<q_mtd_driver, ltq_mtd_probe);
286 + pr_err("ltq_nor: error registering platform driver");
293 + platform_driver_unregister(<q_mtd_driver);
296 +module_init(init_ltq_mtd);
297 +module_exit(exit_ltq_mtd);
299 +MODULE_LICENSE("GPL");
300 +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
301 +MODULE_DESCRIPTION("Lantiq SoC NOR");