aefa11121674973a4e84f93d76898c79736baa98
[firefly-linux-kernel-4.4.55.git] / drivers / mtd / maps / lantiq-flash.c
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
25 /*
26  * The NOR flash is connected to the same external bus unit (EBU) as PCI.
27  * To make PCI work we need to enable the endianness swapping for the address
28  * written to the EBU. This endianness swapping works for PCI correctly but
29  * fails for attached NOR devices. To workaround this we need to use a complex
30  * map. The workaround involves swapping all addresses whilst probing the chip.
31  * Once probing is complete we stop swapping the addresses but swizzle the
32  * unlock addresses to ensure that access to the NOR device works correctly.
33  */
34
35 enum {
36         LTQ_NOR_PROBING,
37         LTQ_NOR_NORMAL
38 };
39
40 struct ltq_mtd {
41         struct resource *res;
42         struct mtd_info *mtd;
43         struct map_info *map;
44 };
45
46 static const char ltq_map_name[] = "ltq_nor";
47 static const char *ltq_probe_types[] __devinitconst = {
48                                         "cmdlinepart", "ofpart", NULL };
49
50 static map_word
51 ltq_read16(struct map_info *map, unsigned long adr)
52 {
53         unsigned long flags;
54         map_word temp;
55
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;
62 }
63
64 static void
65 ltq_write16(struct map_info *map, map_word d, unsigned long adr)
66 {
67         unsigned long flags;
68
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);
74 }
75
76 /*
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.
82  */
83 static void
84 ltq_copy_from(struct map_info *map, void *to,
85         unsigned long from, ssize_t len)
86 {
87         unsigned char *f = (unsigned char *)map->virt + from;
88         unsigned char *t = (unsigned char *)to;
89         unsigned long flags;
90
91         spin_lock_irqsave(&ebu_lock, flags);
92         while (len--)
93                 *t++ = *f++;
94         spin_unlock_irqrestore(&ebu_lock, flags);
95 }
96
97 static void
98 ltq_copy_to(struct map_info *map, unsigned long to,
99         const void *from, ssize_t len)
100 {
101         unsigned char *f = (unsigned char *)from;
102         unsigned char *t = (unsigned char *)map->virt + to;
103         unsigned long flags;
104
105         spin_lock_irqsave(&ebu_lock, flags);
106         while (len--)
107                 *t++ = *f++;
108         spin_unlock_irqrestore(&ebu_lock, flags);
109 }
110
111 static int __devinit
112 ltq_mtd_probe(struct platform_device *pdev)
113 {
114         struct mtd_part_parser_data ppdata;
115         struct ltq_mtd *ltq_mtd;
116         struct cfi_private *cfi;
117         int err;
118
119         ltq_mtd = kzalloc(sizeof(struct ltq_mtd), GFP_KERNEL);
120         platform_set_drvdata(pdev, ltq_mtd);
121
122         ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
123         if (!ltq_mtd->res) {
124                 dev_err(&pdev->dev, "failed to get memory resource\n");
125                 err = -ENOENT;
126                 goto err_out;
127         }
128
129         ltq_mtd->map = kzalloc(sizeof(struct map_info), GFP_KERNEL);
130         ltq_mtd->map->phys = ltq_mtd->res->start;
131         ltq_mtd->map->size = resource_size(ltq_mtd->res);
132         ltq_mtd->map->virt = devm_request_and_ioremap(&pdev->dev, ltq_mtd->res);
133         if (!ltq_mtd->map->virt) {
134                 dev_err(&pdev->dev, "failed to remap mem resource\n");
135                 err = -EBUSY;
136                 goto err_out;
137         }
138
139         ltq_mtd->map->name = ltq_map_name;
140         ltq_mtd->map->bankwidth = 2;
141         ltq_mtd->map->read = ltq_read16;
142         ltq_mtd->map->write = ltq_write16;
143         ltq_mtd->map->copy_from = ltq_copy_from;
144         ltq_mtd->map->copy_to = ltq_copy_to;
145
146         ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
147         ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
148         ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
149
150         if (!ltq_mtd->mtd) {
151                 dev_err(&pdev->dev, "probing failed\n");
152                 err = -ENXIO;
153                 goto err_free;
154         }
155
156         ltq_mtd->mtd->owner = THIS_MODULE;
157
158         cfi = ltq_mtd->map->fldrv_priv;
159         cfi->addr_unlock1 ^= 1;
160         cfi->addr_unlock2 ^= 1;
161
162         ppdata.of_node = pdev->dev.of_node;
163         err = mtd_device_parse_register(ltq_mtd->mtd, ltq_probe_types,
164                                         &ppdata, NULL, 0);
165         if (err) {
166                 dev_err(&pdev->dev, "failed to add partitions\n");
167                 goto err_destroy;
168         }
169
170         return 0;
171
172 err_destroy:
173         map_destroy(ltq_mtd->mtd);
174 err_free:
175         kfree(ltq_mtd->map);
176 err_out:
177         kfree(ltq_mtd);
178         return err;
179 }
180
181 static int __devexit
182 ltq_mtd_remove(struct platform_device *pdev)
183 {
184         struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
185
186         if (ltq_mtd) {
187                 if (ltq_mtd->mtd) {
188                         mtd_device_unregister(ltq_mtd->mtd);
189                         map_destroy(ltq_mtd->mtd);
190                 }
191                 kfree(ltq_mtd->map);
192                 kfree(ltq_mtd);
193         }
194         return 0;
195 }
196
197 static const struct of_device_id ltq_mtd_match[] = {
198         { .compatible = "lantiq,nor" },
199         {},
200 };
201 MODULE_DEVICE_TABLE(of, ltq_mtd_match);
202
203 static struct platform_driver ltq_mtd_driver = {
204         .probe = ltq_mtd_probe,
205         .remove = __devexit_p(ltq_mtd_remove),
206         .driver = {
207                 .name = "ltq-nor",
208                 .owner = THIS_MODULE,
209                 .of_match_table = ltq_mtd_match,
210         },
211 };
212
213 module_platform_driver(ltq_mtd_driver);
214
215 MODULE_LICENSE("GPL");
216 MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
217 MODULE_DESCRIPTION("Lantiq SoC NOR");