lantiq: refresh patches
[openwrt/openwrt.git] / target / linux / lantiq / patches-4.4 / 0160-owrt-lantiq-multiple-flash.patch
1 --- a/drivers/mtd/maps/lantiq-flash.c
2 +++ b/drivers/mtd/maps/lantiq-flash.c
3 @@ -19,6 +19,7 @@
4 #include <linux/mtd/cfi.h>
5 #include <linux/platform_device.h>
6 #include <linux/mtd/physmap.h>
7 +#include <linux/mtd/concat.h>
8 #include <linux/of.h>
9
10 #include <lantiq_soc.h>
11 @@ -38,13 +39,16 @@ enum {
12 LTQ_NOR_NORMAL
13 };
14
15 +#define MAX_RESOURCES 4
16 +
17 struct ltq_mtd {
18 - struct resource *res;
19 - struct mtd_info *mtd;
20 - struct map_info *map;
21 + struct mtd_info *mtd[MAX_RESOURCES];
22 + struct mtd_info *cmtd;
23 + struct map_info map[MAX_RESOURCES];
24 };
25
26 static const char ltq_map_name[] = "ltq_nor";
27 +static const char * const ltq_probe_types[] = { "cmdlinepart", "ofpart", NULL };
28
29 static map_word
30 ltq_read16(struct map_info *map, unsigned long adr)
31 @@ -108,12 +112,44 @@ ltq_copy_to(struct map_info *map, unsign
32 }
33
34 static int
35 +ltq_mtd_remove(struct platform_device *pdev)
36 +{
37 + struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
38 + int i;
39 +
40 + if (ltq_mtd == NULL)
41 + return 0;
42 +
43 + if (ltq_mtd->cmtd) {
44 + mtd_device_unregister(ltq_mtd->cmtd);
45 + if (ltq_mtd->cmtd != ltq_mtd->mtd[0])
46 + mtd_concat_destroy(ltq_mtd->cmtd);
47 + }
48 +
49 + for (i = 0; i < MAX_RESOURCES; i++) {
50 + if (ltq_mtd->mtd[i] != NULL)
51 + map_destroy(ltq_mtd->mtd[i]);
52 + }
53 +
54 + kfree(ltq_mtd);
55 +
56 + return 0;
57 +}
58 +
59 +static int
60 ltq_mtd_probe(struct platform_device *pdev)
61 {
62 struct mtd_part_parser_data ppdata;
63 struct ltq_mtd *ltq_mtd;
64 struct cfi_private *cfi;
65 - int err;
66 + int err = 0;
67 + int i;
68 + int devices_found = 0;
69 +
70 + static const char *rom_probe_types[] = {
71 + "cfi_probe", "jedec_probe", NULL
72 + };
73 + const char **type;
74
75 if (of_machine_is_compatible("lantiq,falcon") &&
76 (ltq_boot_select() != BS_FLASH)) {
77 @@ -127,75 +163,90 @@ ltq_mtd_probe(struct platform_device *pd
78
79 platform_set_drvdata(pdev, ltq_mtd);
80
81 - ltq_mtd->res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
82 - if (!ltq_mtd->res) {
83 - dev_err(&pdev->dev, "failed to get memory resource\n");
84 - return -ENOENT;
85 - }
86 + for (i = 0; i < pdev->num_resources; i++) {
87 + printk(KERN_NOTICE "lantiq nor flash device: %.8llx at %.8llx\n",
88 + (unsigned long long)resource_size(&pdev->resource[i]),
89 + (unsigned long long)pdev->resource[i].start);
90 +
91 + if (!devm_request_mem_region(&pdev->dev,
92 + pdev->resource[i].start,
93 + resource_size(&pdev->resource[i]),
94 + dev_name(&pdev->dev))) {
95 + dev_err(&pdev->dev, "Could not reserve memory region\n");
96 + return -ENOMEM;
97 + }
98
99 - ltq_mtd->map = devm_kzalloc(&pdev->dev, sizeof(struct map_info),
100 - GFP_KERNEL);
101 - if (!ltq_mtd->map)
102 - return -ENOMEM;
103 + ltq_mtd->map[i].name = ltq_map_name;
104 + ltq_mtd->map[i].bankwidth = 2;
105 + ltq_mtd->map[i].read = ltq_read16;
106 + ltq_mtd->map[i].write = ltq_write16;
107 + ltq_mtd->map[i].copy_from = ltq_copy_from;
108 + ltq_mtd->map[i].copy_to = ltq_copy_to;
109
110 - if (of_find_property(pdev->dev.of_node, "lantiq,noxip", NULL))
111 - ltq_mtd->map->phys = NO_XIP;
112 - else
113 - ltq_mtd->map->phys = ltq_mtd->res->start;
114 - ltq_mtd->res->start;
115 - ltq_mtd->map->size = resource_size(ltq_mtd->res);
116 - ltq_mtd->map->virt = devm_ioremap_resource(&pdev->dev, ltq_mtd->res);
117 - if (IS_ERR(ltq_mtd->map->virt))
118 - return PTR_ERR(ltq_mtd->map->virt);
119 + if (of_find_property(pdev->dev.of_node, "lantiq,noxip", NULL))
120 + ltq_mtd->map[i].phys = NO_XIP;
121 + else
122 + ltq_mtd->map[i].phys = pdev->resource[i].start;
123 + ltq_mtd->map[i].size = resource_size(&pdev->resource[i]);
124 + ltq_mtd->map[i].virt = devm_ioremap(&pdev->dev, pdev->resource[i].start,
125 + ltq_mtd->map[i].size);
126 + if (IS_ERR(ltq_mtd->map[i].virt))
127 + return PTR_ERR(ltq_mtd->map[i].virt);
128
129 - ltq_mtd->map->name = ltq_map_name;
130 - ltq_mtd->map->bankwidth = 2;
131 - ltq_mtd->map->read = ltq_read16;
132 - ltq_mtd->map->write = ltq_write16;
133 - ltq_mtd->map->copy_from = ltq_copy_from;
134 - ltq_mtd->map->copy_to = ltq_copy_to;
135 + if (ltq_mtd->map[i].virt == NULL) {
136 + dev_err(&pdev->dev, "Failed to ioremap flash region\n");
137 + err = PTR_ERR(ltq_mtd->map[i].virt);
138 + goto err_out;
139 + }
140
141 - ltq_mtd->map->map_priv_1 = LTQ_NOR_PROBING;
142 - ltq_mtd->mtd = do_map_probe("cfi_probe", ltq_mtd->map);
143 - ltq_mtd->map->map_priv_1 = LTQ_NOR_NORMAL;
144 + ltq_mtd->map[i].map_priv_1 = LTQ_NOR_PROBING;
145 + for (type = rom_probe_types; !ltq_mtd->mtd[i] && *type; type++)
146 + ltq_mtd->mtd[i] = do_map_probe(*type, &ltq_mtd->map[i]);
147 + ltq_mtd->map[i].map_priv_1 = LTQ_NOR_NORMAL;
148
149 - if (!ltq_mtd->mtd) {
150 - dev_err(&pdev->dev, "probing failed\n");
151 - return -ENXIO;
152 + if (!ltq_mtd->mtd[i]) {
153 + dev_err(&pdev->dev, "probing failed\n");
154 + return -ENXIO;
155 + } else {
156 + devices_found++;
157 + }
158 +
159 + ltq_mtd->mtd[i]->owner = THIS_MODULE;
160 + ltq_mtd->mtd[i]->dev.parent = &pdev->dev;
161 +
162 + cfi = ltq_mtd->map[i].fldrv_priv;
163 + cfi->addr_unlock1 ^= 1;
164 + cfi->addr_unlock2 ^= 1;
165 }
166
167 - ltq_mtd->mtd->dev.parent = &pdev->dev;
168 + if (devices_found == 1) {
169 + ltq_mtd->cmtd = ltq_mtd->mtd[0];
170 + } else if (devices_found > 1) {
171 + /*
172 + * We detected multiple devices. Concatenate them together.
173 + */
174 + ltq_mtd->cmtd = mtd_concat_create(ltq_mtd->mtd, devices_found, dev_name(&pdev->dev));
175 + if (ltq_mtd->cmtd == NULL)
176 + err = -ENXIO;
177 + }
178
179 - cfi = ltq_mtd->map->fldrv_priv;
180 - cfi->addr_unlock1 ^= 1;
181 - cfi->addr_unlock2 ^= 1;
182 + ltq_mtd->cmtd->dev.parent = &pdev->dev;
183
184 ppdata.of_node = pdev->dev.of_node;
185 - err = mtd_device_parse_register(ltq_mtd->mtd, NULL, &ppdata, NULL, 0);
186 + err = mtd_device_parse_register(ltq_mtd->cmtd, ltq_probe_types,
187 + &ppdata, NULL, 0);
188 if (err) {
189 dev_err(&pdev->dev, "failed to add partitions\n");
190 - goto err_destroy;
191 + goto err_out;
192 }
193
194 return 0;
195
196 -err_destroy:
197 - map_destroy(ltq_mtd->mtd);
198 +err_out:
199 + ltq_mtd_remove(pdev);
200 return err;
201 }
202
203 -static int
204 -ltq_mtd_remove(struct platform_device *pdev)
205 -{
206 - struct ltq_mtd *ltq_mtd = platform_get_drvdata(pdev);
207 -
208 - if (ltq_mtd && ltq_mtd->mtd) {
209 - mtd_device_unregister(ltq_mtd->mtd);
210 - map_destroy(ltq_mtd->mtd);
211 - }
212 - return 0;
213 -}
214 -
215 static const struct of_device_id ltq_mtd_match[] = {
216 { .compatible = "lantiq,nor" },
217 {},