i2c-eg20t: change timeout value 50msec to 1000msec
[zen-stable.git] / drivers / mtd / maps / lantiq-flash.c
blobcf7a3cddad35dbecd3107ba0e3475706f5da4b64
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.
6 * Copyright (C) 2004 Liu Peng Infineon IFAP DC COM CPE
7 * Copyright (C) 2010 John Crispin <blogic@openwrt.org>
8 */
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>
23 #include <lantiq_soc.h>
24 #include <lantiq_platform.h>
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.
36 enum {
37 LTQ_NOR_PROBING,
38 LTQ_NOR_NORMAL
41 struct ltq_mtd {
42 struct resource *res;
43 struct mtd_info *mtd;
44 struct map_info *map;
47 static char ltq_map_name[] = "ltq_nor";
48 static const char *ltq_probe_types[] __devinitconst = { "cmdlinepart", NULL };
50 static map_word
51 ltq_read16(struct map_info *map, unsigned long adr)
53 unsigned long flags;
54 map_word temp;
56 if (map->map_priv_1 == LTQ_NOR_PROBING)
57 adr ^= 2;
58 spin_lock_irqsave(&ebu_lock, flags);
59 temp.x[0] = *(u16 *)(map->virt + adr);
60 spin_unlock_irqrestore(&ebu_lock, flags);
61 return temp;
64 static void
65 ltq_write16(struct map_info *map, map_word d, unsigned long adr)
67 unsigned long flags;
69 if (map->map_priv_1 == LTQ_NOR_PROBING)
70 adr ^= 2;
71 spin_lock_irqsave(&ebu_lock, flags);
72 *(u16 *)(map->virt + adr) = d.x[0];
73 spin_unlock_irqrestore(&ebu_lock, flags);
77 * The following 2 functions copy data between iomem and a cached memory
78 * section. As memcpy() makes use of pre-fetching we cannot use it here.
79 * The normal alternative of using memcpy_{to,from}io also makes use of
80 * memcpy() on MIPS so it is not applicable either. We are therefore stuck
81 * with having to use our own loop.
83 static void
84 ltq_copy_from(struct map_info *map, void *to,
85 unsigned long from, ssize_t len)
87 unsigned char *f = (unsigned char *)map->virt + from;
88 unsigned char *t = (unsigned char *)to;
89 unsigned long flags;
91 spin_lock_irqsave(&ebu_lock, flags);
92 while (len--)
93 *t++ = *f++;
94 spin_unlock_irqrestore(&ebu_lock, flags);
97 static void
98 ltq_copy_to(struct map_info *map, unsigned long to,
99 const void *from, ssize_t len)
101 unsigned char *f = (unsigned char *)from;
102 unsigned char *t = (unsigned char *)map->virt + to;
103 unsigned long flags;
105 spin_lock_irqsave(&ebu_lock, flags);
106 while (len--)
107 *t++ = *f++;
108 spin_unlock_irqrestore(&ebu_lock, flags);
111 static int __init
112 ltq_mtd_probe(struct platform_device *pdev)
114 struct physmap_flash_data *ltq_mtd_data = dev_get_platdata(&pdev->dev);
115 struct ltq_mtd *ltq_mtd;
116 struct resource *res;
117 struct cfi_private *cfi;
118 int err;
120 ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
121 platform_set_drvdata(pdev, ltq_mtd);
123 ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
124 if (!ltq_mtd->res) {
125 dev_err(&pdev->dev, "failed to get memory resource");
126 err = -ENOENT;
127 goto err_out;
130 res = devm_request_mem_region(&pdev->dev, ltq_mtd->res->start,
131 resource_size(ltq_mtd->res), dev_name(&pdev->dev));
132 if (!ltq_mtd->res) {
133 dev_err(&pdev->dev, "failed to request mem resource");
134 err = -EBUSY;
135 goto err_out;
138 ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
139 ltq_mtd->map->phys = res->start;
140 ltq_mtd->map->size = resource_size(res);
141 ltq_mtd->map->virt = devm_ioremap_nocache(&pdev->dev,
142 ltq_mtd->map->phys, ltq_mtd->map->size);
143 if (!ltq_mtd->map->virt) {
144 dev_err(&pdev->dev, "failed to ioremap!\n");
145 err = -ENOMEM;
146 goto err_free;
149 ltq_mtd->map->name = ltq_map_name;
150 ltq_mtd->map->bankwidth = 2;
151 ltq_mtd->map->read = ltq_read16;
152 ltq_mtd->map->write = ltq_write16;
153 ltq_mtd->map->copy_from = ltq_copy_from;
154 ltq_mtd->map->copy_to = ltq_copy_to;
156 ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
157 ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
158 ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
160 if (!ltq_mtd->mtd) {
161 dev_err(&pdev->dev, "probing failed\n");
162 err = -ENXIO;
163 goto err_free;
166 ltq_mtd->mtd->owner = THIS_MODULE;
168 cfi = ltq_mtd->map->fldrv_priv;
169 cfi->addr_unlock1 ^= 1;
170 cfi->addr_unlock2 ^= 1;
172 err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types, 0,
173 ltq_mtd_data->parts, ltq_mtd_data->nr_parts);
174 if (err) {
175 dev_err(&pdev->dev, "failed to add partitions\n");
176 goto err_destroy;
179 return 0;
181 err_destroy:
182 map_destroy(ltq_mtd->mtd);
183 err_free:
184 kfree(ltq_mtd->map);
185 err_out:
186 kfree(ltq_mtd);
187 return err;
190 static int __devexit
191 ltq_mtd_remove(struct platform_device *pdev)
193 struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
195 if (ltq_mtd) {
196 if (ltq_mtd->mtd) {
197 mtd_device_unregister(ltq_mtd->mtd);
198 map_destroy(ltq_mtd->mtd);
200 kfree(ltq_mtd->map);
201 kfree(ltq_mtd);
203 return 0;
206 static struct platform_driver ltq_mtd_driver = {
207 .remove = __devexit_p(ltq_mtd_remove),
208 .driver = {
209 .name = "ltq_nor",
210 .owner = THIS_MODULE,
214 static int __init
215 init_ltq_mtd(void)
217 int ret = platform_driver_probe(&ltq_mtd_driver, ltq_mtd_probe);
219 if (ret)
220 pr_err("ltq_nor: error registering platform driver");
221 return ret;
224 static void __exit
225 exit_ltq_mtd(void)
227 platform_driver_unregister(&ltq_mtd_driver);
230 module_init(init_ltq_mtd);
231 module_exit(exit_ltq_mtd);
233 MODULE_LICENSE("GPL");
234 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
235 MODULE_DESCRIPTION("Lantiq SoC NOR");