Commit | Line | Data |
---|---|---|
a2c2fe4b | 1 | /* |
c4d5e375 | 2 | * Flash mappings described by the OF (or flattened) device tree |
a2c2fe4b VW |
3 | * |
4 | * Copyright (C) 2006 MontaVista Software Inc. | |
5 | * Author: Vitaly Wool <vwool@ru.mvista.com> | |
6 | * | |
2099172d DG |
7 | * Revised to handle newer style flash binding by: |
8 | * Copyright (C) 2007 David Gibson, IBM Corporation. | |
9 | * | |
a2c2fe4b VW |
10 | * This program is free software; you can redistribute it and/or modify it |
11 | * under the terms of the GNU General Public License as published by the | |
12 | * Free Software Foundation; either version 2 of the License, or (at your | |
13 | * option) any later version. | |
14 | */ | |
15 | ||
16 | #include <linux/module.h> | |
17 | #include <linux/types.h> | |
a2c2fe4b VW |
18 | #include <linux/device.h> |
19 | #include <linux/mtd/mtd.h> | |
20 | #include <linux/mtd/map.h> | |
21 | #include <linux/mtd/partitions.h> | |
143070e7 | 22 | #include <linux/mtd/concat.h> |
7d5cba59 | 23 | #include <linux/mtd/cfi_endian.h> |
c4d5e375 | 24 | #include <linux/of.h> |
7a50d06e | 25 | #include <linux/of_address.h> |
c4d5e375 | 26 | #include <linux/of_platform.h> |
5a0e3ad6 | 27 | #include <linux/slab.h> |
56ff337e | 28 | #include "physmap_of_gemini.h" |
b0afd44b | 29 | #include "physmap_of_versatile.h" |
a2c2fe4b | 30 | |
143070e7 SR |
31 | struct of_flash_list { |
32 | struct mtd_info *mtd; | |
33 | struct map_info map; | |
34 | struct resource *res; | |
35 | }; | |
36 | ||
c4d5e375 | 37 | struct of_flash { |
143070e7 | 38 | struct mtd_info *cmtd; |
143070e7 SR |
39 | int list_size; /* number of elements in of_flash_list */ |
40 | struct of_flash_list list[0]; | |
a2c2fe4b VW |
41 | }; |
42 | ||
2dc11581 | 43 | static int of_flash_remove(struct platform_device *dev) |
a2c2fe4b | 44 | { |
c4d5e375 | 45 | struct of_flash *info; |
143070e7 | 46 | int i; |
a2c2fe4b VW |
47 | |
48 | info = dev_get_drvdata(&dev->dev); | |
c4d5e375 | 49 | if (!info) |
a2c2fe4b VW |
50 | return 0; |
51 | dev_set_drvdata(&dev->dev, NULL); | |
52 | ||
92b633a8 | 53 | if (info->cmtd) { |
984e6d8e | 54 | mtd_device_unregister(info->cmtd); |
92b633a8 AB |
55 | if (info->cmtd != info->list[0].mtd) |
56 | mtd_concat_destroy(info->cmtd); | |
143070e7 | 57 | } |
143070e7 | 58 | |
143070e7 SR |
59 | for (i = 0; i < info->list_size; i++) { |
60 | if (info->list[i].mtd) | |
61 | map_destroy(info->list[i].mtd); | |
a2c2fe4b | 62 | |
143070e7 SR |
63 | if (info->list[i].map.virt) |
64 | iounmap(info->list[i].map.virt); | |
65 | ||
66 | if (info->list[i].res) { | |
67 | release_resource(info->list[i].res); | |
68 | kfree(info->list[i].res); | |
69 | } | |
a2c2fe4b | 70 | } |
a2c2fe4b VW |
71 | return 0; |
72 | } | |
73 | ||
cce2a026 AB |
74 | static const char * const rom_probe_types[] = { |
75 | "cfi_probe", "jedec_probe", "map_rom" }; | |
76 | ||
2099172d DG |
77 | /* Helper function to handle probing of the obsolete "direct-mapped" |
78 | * compatible binding, which has an extra "probe-type" property | |
79 | * describing the type of flash probe necessary. */ | |
06f25510 | 80 | static struct mtd_info *obsolete_probe(struct platform_device *dev, |
d8929942 | 81 | struct map_info *map) |
a2c2fe4b | 82 | { |
61c7a080 | 83 | struct device_node *dp = dev->dev.of_node; |
a2c2fe4b | 84 | const char *of_probe; |
2099172d | 85 | struct mtd_info *mtd; |
2099172d DG |
86 | int i; |
87 | ||
88 | dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" " | |
89 | "flash binding\n"); | |
90 | ||
91 | of_probe = of_get_property(dp, "probe-type", NULL); | |
92 | if (!of_probe) { | |
93 | for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) { | |
94 | mtd = do_map_probe(rom_probe_types[i], map); | |
95 | if (mtd) | |
96 | return mtd; | |
97 | } | |
98 | return NULL; | |
99 | } else if (strcmp(of_probe, "CFI") == 0) { | |
100 | return do_map_probe("cfi_probe", map); | |
101 | } else if (strcmp(of_probe, "JEDEC") == 0) { | |
102 | return do_map_probe("jedec_probe", map); | |
103 | } else { | |
104 | if (strcmp(of_probe, "ROM") != 0) | |
c4d5e375 DG |
105 | dev_warn(&dev->dev, "obsolete_probe: don't know probe " |
106 | "type '%s', mapping as rom\n", of_probe); | |
9b07a8d1 | 107 | return do_map_probe("map_rom", map); |
2099172d DG |
108 | } |
109 | } | |
110 | ||
9d5da3a9 JG |
111 | /* When partitions are set we look for a linux,part-probe property which |
112 | specifies the list of partition probers to use. If none is given then the | |
113 | default is use. These take precedence over other device tree | |
114 | information. */ | |
cce2a026 AB |
115 | static const char * const part_probe_types_def[] = { |
116 | "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL }; | |
117 | ||
118 | static const char * const *of_get_probes(struct device_node *dp) | |
9d5da3a9 | 119 | { |
9d5da3a9 | 120 | const char **res; |
da4b1caa | 121 | int count; |
9d5da3a9 | 122 | |
da4b1caa RM |
123 | count = of_property_count_strings(dp, "linux,part-probe"); |
124 | if (count < 0) | |
9d5da3a9 JG |
125 | return part_probe_types_def; |
126 | ||
6396bb22 | 127 | res = kcalloc(count + 1, sizeof(*res), GFP_KERNEL); |
7e0c19c9 CIK |
128 | if (!res) |
129 | return NULL; | |
da4b1caa RM |
130 | |
131 | count = of_property_read_string_array(dp, "linux,part-probe", res, | |
132 | count); | |
133 | if (count < 0) | |
134 | return NULL; | |
135 | ||
9d5da3a9 JG |
136 | return res; |
137 | } | |
138 | ||
cce2a026 | 139 | static void of_free_probes(const char * const *probes) |
9d5da3a9 JG |
140 | { |
141 | if (probes != part_probe_types_def) | |
142 | kfree(probes); | |
143 | } | |
9d5da3a9 | 144 | |
66610443 | 145 | static const struct of_device_id of_flash_match[]; |
06f25510 | 146 | static int of_flash_probe(struct platform_device *dev) |
a2c2fe4b | 147 | { |
cce2a026 | 148 | const char * const *part_probe_types; |
b1608d69 | 149 | const struct of_device_id *match; |
61c7a080 | 150 | struct device_node *dp = dev->dev.of_node; |
a2c2fe4b | 151 | struct resource res; |
c4d5e375 | 152 | struct of_flash *info; |
1c48a5c9 | 153 | const char *probe_type; |
766f271a | 154 | const __be32 *width; |
a2c2fe4b | 155 | int err; |
143070e7 SR |
156 | int i; |
157 | int count; | |
766f271a | 158 | const __be32 *p; |
143070e7 SR |
159 | int reg_tuple_size; |
160 | struct mtd_info **mtd_list = NULL; | |
2763c508 | 161 | resource_size_t res_size; |
d0788ce4 | 162 | bool map_indirect; |
7dfe4be3 | 163 | const char *mtd_name = NULL; |
143070e7 | 164 | |
b1608d69 GL |
165 | match = of_match_device(of_flash_match, &dev->dev); |
166 | if (!match) | |
1c48a5c9 | 167 | return -EINVAL; |
b1608d69 | 168 | probe_type = match->data; |
1c48a5c9 | 169 | |
143070e7 SR |
170 | reg_tuple_size = (of_n_addr_cells(dp) + of_n_size_cells(dp)) * sizeof(u32); |
171 | ||
d68cbdd4 JCPV |
172 | of_property_read_string(dp, "linux,mtd-name", &mtd_name); |
173 | ||
143070e7 SR |
174 | /* |
175 | * Get number of "reg" tuples. Scan for MTD devices on area's | |
176 | * described by each "reg" region. This makes it possible (including | |
177 | * the concat support) to support the Intel P30 48F4400 chips which | |
178 | * consists internally of 2 non-identical NOR chips on one die. | |
179 | */ | |
180 | p = of_get_property(dp, "reg", &count); | |
b137aab4 | 181 | if (!p || count % reg_tuple_size != 0) { |
1d706077 RH |
182 | dev_err(&dev->dev, "Malformed reg property on %pOF\n", |
183 | dev->dev.of_node); | |
143070e7 | 184 | err = -EINVAL; |
ad4fbc79 | 185 | goto err_flash_remove; |
a2c2fe4b | 186 | } |
143070e7 | 187 | count /= reg_tuple_size; |
a2c2fe4b | 188 | |
d0788ce4 SR |
189 | map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access"); |
190 | ||
c4d5e375 | 191 | err = -ENOMEM; |
eb82038f EG |
192 | info = devm_kzalloc(&dev->dev, |
193 | sizeof(struct of_flash) + | |
194 | sizeof(struct of_flash_list) * count, GFP_KERNEL); | |
143070e7 | 195 | if (!info) |
ad4fbc79 | 196 | goto err_flash_remove; |
a2c2fe4b VW |
197 | |
198 | dev_set_drvdata(&dev->dev, info); | |
199 | ||
6396bb22 | 200 | mtd_list = kcalloc(count, sizeof(*mtd_list), GFP_KERNEL); |
ad4fbc79 | 201 | if (!mtd_list) |
202 | goto err_flash_remove; | |
203 | ||
143070e7 SR |
204 | for (i = 0; i < count; i++) { |
205 | err = -ENXIO; | |
206 | if (of_address_to_resource(dp, i, &res)) { | |
940fe282 SR |
207 | /* |
208 | * Continue with next register tuple if this | |
209 | * one is not mappable | |
210 | */ | |
211 | continue; | |
143070e7 | 212 | } |
a2c2fe4b | 213 | |
f9a5279c | 214 | dev_dbg(&dev->dev, "of_flash device: %pR\n", &res); |
143070e7 SR |
215 | |
216 | err = -EBUSY; | |
2763c508 WS |
217 | res_size = resource_size(&res); |
218 | info->list[i].res = request_mem_region(res.start, res_size, | |
143070e7 SR |
219 | dev_name(&dev->dev)); |
220 | if (!info->list[i].res) | |
221 | goto err_out; | |
222 | ||
223 | err = -ENXIO; | |
224 | width = of_get_property(dp, "bank-width", NULL); | |
225 | if (!width) { | |
226 | dev_err(&dev->dev, "Can't get bank width from device" | |
227 | " tree\n"); | |
228 | goto err_out; | |
229 | } | |
a2c2fe4b | 230 | |
d68cbdd4 | 231 | info->list[i].map.name = mtd_name ?: dev_name(&dev->dev); |
143070e7 | 232 | info->list[i].map.phys = res.start; |
2763c508 | 233 | info->list[i].map.size = res_size; |
766f271a | 234 | info->list[i].map.bankwidth = be32_to_cpup(width); |
1648eaaa | 235 | info->list[i].map.device_node = dp; |
56ff337e | 236 | |
7d5cba59 PK |
237 | if (of_property_read_bool(dp, "big-endian")) |
238 | info->list[i].map.swap = CFI_BIG_ENDIAN; | |
239 | else if (of_property_read_bool(dp, "little-endian")) | |
240 | info->list[i].map.swap = CFI_LITTLE_ENDIAN; | |
241 | ||
56ff337e LW |
242 | err = of_flash_probe_gemini(dev, dp, &info->list[i].map); |
243 | if (err) | |
871e7c01 | 244 | goto err_out; |
b0afd44b | 245 | err = of_flash_probe_versatile(dev, dp, &info->list[i].map); |
56ff337e | 246 | if (err) |
871e7c01 | 247 | goto err_out; |
143070e7 SR |
248 | |
249 | err = -ENOMEM; | |
250 | info->list[i].map.virt = ioremap(info->list[i].map.phys, | |
251 | info->list[i].map.size); | |
252 | if (!info->list[i].map.virt) { | |
253 | dev_err(&dev->dev, "Failed to ioremap() flash" | |
254 | " region\n"); | |
255 | goto err_out; | |
256 | } | |
a2c2fe4b | 257 | |
143070e7 | 258 | simple_map_init(&info->list[i].map); |
a2c2fe4b | 259 | |
d0788ce4 SR |
260 | /* |
261 | * On some platforms (e.g. MPC5200) a direct 1:1 mapping | |
262 | * may cause problems with JFFS2 usage, as the local bus (LPB) | |
263 | * doesn't support unaligned accesses as implemented in the | |
264 | * JFFS2 code via memcpy(). By setting NO_XIP, the | |
265 | * flash will not be exposed directly to the MTD users | |
266 | * (e.g. JFFS2) any more. | |
267 | */ | |
268 | if (map_indirect) | |
269 | info->list[i].map.phys = NO_XIP; | |
270 | ||
143070e7 SR |
271 | if (probe_type) { |
272 | info->list[i].mtd = do_map_probe(probe_type, | |
273 | &info->list[i].map); | |
274 | } else { | |
275 | info->list[i].mtd = obsolete_probe(dev, | |
276 | &info->list[i].map); | |
277 | } | |
3fc1cf5f JS |
278 | |
279 | /* Fall back to mapping region as ROM */ | |
280 | if (!info->list[i].mtd) { | |
281 | dev_warn(&dev->dev, | |
282 | "do_map_probe() failed for type %s\n", | |
283 | probe_type); | |
284 | ||
285 | info->list[i].mtd = do_map_probe("map_rom", | |
286 | &info->list[i].map); | |
287 | } | |
143070e7 | 288 | mtd_list[i] = info->list[i].mtd; |
a2c2fe4b | 289 | |
143070e7 SR |
290 | err = -ENXIO; |
291 | if (!info->list[i].mtd) { | |
292 | dev_err(&dev->dev, "do_map_probe() failed\n"); | |
293 | goto err_out; | |
294 | } else { | |
295 | info->list_size++; | |
296 | } | |
143070e7 SR |
297 | info->list[i].mtd->dev.parent = &dev->dev; |
298 | } | |
2099172d | 299 | |
143070e7 | 300 | err = 0; |
e58a66d8 | 301 | info->cmtd = NULL; |
143070e7 SR |
302 | if (info->list_size == 1) { |
303 | info->cmtd = info->list[0].mtd; | |
304 | } else if (info->list_size > 1) { | |
305 | /* | |
306 | * We detected multiple devices. Concatenate them together. | |
307 | */ | |
143070e7 SR |
308 | info->cmtd = mtd_concat_create(mtd_list, info->list_size, |
309 | dev_name(&dev->dev)); | |
a2c2fe4b | 310 | } |
e58a66d8 AP |
311 | if (info->cmtd == NULL) |
312 | err = -ENXIO; | |
313 | ||
143070e7 SR |
314 | if (err) |
315 | goto err_out; | |
a2c2fe4b | 316 | |
8361a9b8 | 317 | info->cmtd->dev.parent = &dev->dev; |
004b5e60 | 318 | mtd_set_of_node(info->cmtd, dp); |
9d5da3a9 | 319 | part_probe_types = of_get_probes(dp); |
7e0c19c9 CIK |
320 | if (!part_probe_types) { |
321 | err = -ENOMEM; | |
322 | goto err_out; | |
323 | } | |
004b5e60 | 324 | mtd_device_parse_register(info->cmtd, part_probe_types, NULL, |
f44dcbd0 | 325 | NULL, 0); |
9d5da3a9 | 326 | of_free_probes(part_probe_types); |
9a310d21 | 327 | |
143070e7 | 328 | kfree(mtd_list); |
a2c2fe4b | 329 | |
a2c2fe4b VW |
330 | return 0; |
331 | ||
332 | err_out: | |
143070e7 | 333 | kfree(mtd_list); |
ad4fbc79 | 334 | err_flash_remove: |
c4d5e375 | 335 | of_flash_remove(dev); |
143070e7 | 336 | |
a2c2fe4b | 337 | return err; |
a2c2fe4b VW |
338 | } |
339 | ||
66610443 | 340 | static const struct of_device_id of_flash_match[] = { |
2099172d DG |
341 | { |
342 | .compatible = "cfi-flash", | |
343 | .data = (void *)"cfi_probe", | |
344 | }, | |
345 | { | |
346 | /* FIXME: JEDEC chips can't be safely and reliably | |
347 | * probed, although the mtd code gets it right in | |
348 | * practice most of the time. We should use the | |
349 | * vendor and device ids specified by the binding to | |
350 | * bypass the heuristic probe code, but the mtd layer | |
351 | * provides, at present, no interface for doing so | |
352 | * :(. */ | |
353 | .compatible = "jedec-flash", | |
354 | .data = (void *)"jedec_probe", | |
355 | }, | |
fc28c39f WS |
356 | { |
357 | .compatible = "mtd-ram", | |
358 | .data = (void *)"map_ram", | |
359 | }, | |
e5bffb59 AS |
360 | { |
361 | .compatible = "mtd-rom", | |
362 | .data = (void *)"map_rom", | |
363 | }, | |
a2c2fe4b VW |
364 | { |
365 | .type = "rom", | |
366 | .compatible = "direct-mapped" | |
367 | }, | |
368 | { }, | |
369 | }; | |
c4d5e375 | 370 | MODULE_DEVICE_TABLE(of, of_flash_match); |
a2c2fe4b | 371 | |
1c48a5c9 | 372 | static struct platform_driver of_flash_driver = { |
4018294b GL |
373 | .driver = { |
374 | .name = "of-flash", | |
4018294b GL |
375 | .of_match_table = of_flash_match, |
376 | }, | |
c4d5e375 DG |
377 | .probe = of_flash_probe, |
378 | .remove = of_flash_remove, | |
a2c2fe4b VW |
379 | }; |
380 | ||
f99640de | 381 | module_platform_driver(of_flash_driver); |
a2c2fe4b VW |
382 | |
383 | MODULE_LICENSE("GPL"); | |
384 | MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>"); | |
c4d5e375 | 385 | MODULE_DESCRIPTION("Device tree based MTD map driver"); |