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 | 18 | #include <linux/init.h> |
a2c2fe4b VW |
19 | #include <linux/device.h> |
20 | #include <linux/mtd/mtd.h> | |
21 | #include <linux/mtd/map.h> | |
22 | #include <linux/mtd/partitions.h> | |
c4d5e375 DG |
23 | #include <linux/of.h> |
24 | #include <linux/of_platform.h> | |
a2c2fe4b | 25 | |
c4d5e375 | 26 | struct of_flash { |
a2c2fe4b VW |
27 | struct mtd_info *mtd; |
28 | struct map_info map; | |
29 | struct resource *res; | |
30 | #ifdef CONFIG_MTD_PARTITIONS | |
a2c2fe4b VW |
31 | struct mtd_partition *parts; |
32 | #endif | |
33 | }; | |
34 | ||
a2c2fe4b | 35 | #ifdef CONFIG_MTD_PARTITIONS |
c4d5e375 | 36 | #define OF_FLASH_PARTS(info) ((info)->parts) |
a2c2fe4b | 37 | |
2099172d | 38 | static int parse_obsolete_partitions(struct of_device *dev, |
c4d5e375 | 39 | struct of_flash *info, |
2099172d | 40 | struct device_node *dp) |
a2c2fe4b | 41 | { |
2099172d DG |
42 | int i, plen, nr_parts; |
43 | const struct { | |
44 | u32 offset, len; | |
45 | } *part; | |
46 | const char *names; | |
a2c2fe4b | 47 | |
2099172d DG |
48 | part = of_get_property(dp, "partitions", &plen); |
49 | if (!part) | |
c4d5e375 | 50 | return 0; /* No partitions found */ |
a2c2fe4b | 51 | |
2099172d DG |
52 | dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n"); |
53 | ||
54 | nr_parts = plen / sizeof(part[0]); | |
55 | ||
c4d5e375 DG |
56 | info->parts = kzalloc(nr_parts * sizeof(*info->parts), GFP_KERNEL); |
57 | if (!info->parts) | |
2099172d | 58 | return -ENOMEM; |
a2c2fe4b | 59 | |
2099172d | 60 | names = of_get_property(dp, "partition-names", &plen); |
a2c2fe4b | 61 | |
2099172d DG |
62 | for (i = 0; i < nr_parts; i++) { |
63 | info->parts[i].offset = part->offset; | |
64 | info->parts[i].size = part->len & ~1; | |
65 | if (part->len & 1) /* bit 0 set signifies read only partition */ | |
66 | info->parts[i].mask_flags = MTD_WRITEABLE; | |
a2c2fe4b | 67 | |
2099172d DG |
68 | if (names && (plen > 0)) { |
69 | int len = strlen(names) + 1; | |
a2c2fe4b | 70 | |
2099172d | 71 | info->parts[i].name = (char *)names; |
a2c2fe4b | 72 | plen -= len; |
2099172d DG |
73 | names += len; |
74 | } else { | |
75 | info->parts[i].name = "unnamed"; | |
76 | } | |
77 | ||
78 | part++; | |
a2c2fe4b | 79 | } |
2099172d DG |
80 | |
81 | return nr_parts; | |
a2c2fe4b | 82 | } |
a2c2fe4b | 83 | |
c4d5e375 DG |
84 | static int __devinit parse_partitions(struct of_flash *info, |
85 | struct of_device *dev) | |
2099172d DG |
86 | { |
87 | const char *partname; | |
88 | static const char *part_probe_types[] | |
89 | = { "cmdlinepart", "RedBoot", NULL }; | |
90 | struct device_node *dp = dev->node, *pp; | |
91 | int nr_parts, i; | |
92 | ||
93 | /* First look for RedBoot table or partitions on the command | |
94 | * line, these take precedence over device tree information */ | |
95 | nr_parts = parse_mtd_partitions(info->mtd, part_probe_types, | |
96 | &info->parts, 0); | |
4edaf56e VB |
97 | if (nr_parts > 0) |
98 | return nr_parts; | |
2099172d DG |
99 | |
100 | /* First count the subnodes */ | |
101 | nr_parts = 0; | |
4edaf56e VB |
102 | for (pp = of_get_next_child(dp, NULL); pp; |
103 | pp = of_get_next_child(dp, pp)) | |
2099172d DG |
104 | nr_parts++; |
105 | ||
c4d5e375 DG |
106 | if (nr_parts == 0) |
107 | return parse_obsolete_partitions(dev, info, dp); | |
108 | ||
109 | info->parts = kzalloc(nr_parts * sizeof(*info->parts), | |
110 | GFP_KERNEL); | |
111 | if (!info->parts) | |
112 | return -ENOMEM; | |
2099172d | 113 | |
4edaf56e VB |
114 | for (pp = of_get_next_child(dp, NULL), i = 0; pp; |
115 | pp = of_get_next_child(dp, pp), i++) { | |
c4d5e375 DG |
116 | const u32 *reg; |
117 | int len; | |
118 | ||
119 | reg = of_get_property(pp, "reg", &len); | |
120 | if (!reg || (len != 2*sizeof(u32))) { | |
4edaf56e | 121 | of_node_put(pp); |
c4d5e375 DG |
122 | dev_err(&dev->dev, "Invalid 'reg' on %s\n", |
123 | dp->full_name); | |
124 | kfree(info->parts); | |
125 | info->parts = NULL; | |
126 | return -EINVAL; | |
2099172d | 127 | } |
c4d5e375 DG |
128 | info->parts[i].offset = reg[0]; |
129 | info->parts[i].size = reg[1]; | |
2099172d | 130 | |
c4d5e375 DG |
131 | partname = of_get_property(pp, "label", &len); |
132 | if (!partname) | |
133 | partname = of_get_property(pp, "name", &len); | |
134 | info->parts[i].name = (char *)partname; | |
2099172d | 135 | |
c4d5e375 DG |
136 | if (of_get_property(pp, "read-only", &len)) |
137 | info->parts[i].mask_flags = MTD_WRITEABLE; | |
138 | } | |
2099172d | 139 | |
c4d5e375 | 140 | return nr_parts; |
2099172d DG |
141 | } |
142 | #else /* MTD_PARTITIONS */ | |
c4d5e375 DG |
143 | #define OF_FLASH_PARTS(info) (0) |
144 | #define parse_partitions(info, dev) (0) | |
2099172d | 145 | #endif /* MTD_PARTITIONS */ |
a2c2fe4b | 146 | |
c4d5e375 | 147 | static int of_flash_remove(struct of_device *dev) |
a2c2fe4b | 148 | { |
c4d5e375 | 149 | struct of_flash *info; |
a2c2fe4b VW |
150 | |
151 | info = dev_get_drvdata(&dev->dev); | |
c4d5e375 | 152 | if (!info) |
a2c2fe4b VW |
153 | return 0; |
154 | dev_set_drvdata(&dev->dev, NULL); | |
155 | ||
c4d5e375 DG |
156 | if (info->mtd) { |
157 | if (OF_FLASH_PARTS(info)) { | |
a2c2fe4b | 158 | del_mtd_partitions(info->mtd); |
c4d5e375 | 159 | kfree(OF_FLASH_PARTS(info)); |
a2c2fe4b VW |
160 | } else { |
161 | del_mtd_device(info->mtd); | |
162 | } | |
a2c2fe4b VW |
163 | map_destroy(info->mtd); |
164 | } | |
165 | ||
c4d5e375 | 166 | if (info->map.virt) |
a2c2fe4b VW |
167 | iounmap(info->map.virt); |
168 | ||
c4d5e375 | 169 | if (info->res) { |
a2c2fe4b VW |
170 | release_resource(info->res); |
171 | kfree(info->res); | |
172 | } | |
173 | ||
174 | return 0; | |
175 | } | |
176 | ||
2099172d DG |
177 | /* Helper function to handle probing of the obsolete "direct-mapped" |
178 | * compatible binding, which has an extra "probe-type" property | |
179 | * describing the type of flash probe necessary. */ | |
180 | static struct mtd_info * __devinit obsolete_probe(struct of_device *dev, | |
181 | struct map_info *map) | |
a2c2fe4b VW |
182 | { |
183 | struct device_node *dp = dev->node; | |
a2c2fe4b | 184 | const char *of_probe; |
2099172d DG |
185 | struct mtd_info *mtd; |
186 | static const char *rom_probe_types[] | |
187 | = { "cfi_probe", "jedec_probe", "map_rom"}; | |
188 | int i; | |
189 | ||
190 | dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" " | |
191 | "flash binding\n"); | |
192 | ||
193 | of_probe = of_get_property(dp, "probe-type", NULL); | |
194 | if (!of_probe) { | |
195 | for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) { | |
196 | mtd = do_map_probe(rom_probe_types[i], map); | |
197 | if (mtd) | |
198 | return mtd; | |
199 | } | |
200 | return NULL; | |
201 | } else if (strcmp(of_probe, "CFI") == 0) { | |
202 | return do_map_probe("cfi_probe", map); | |
203 | } else if (strcmp(of_probe, "JEDEC") == 0) { | |
204 | return do_map_probe("jedec_probe", map); | |
205 | } else { | |
206 | if (strcmp(of_probe, "ROM") != 0) | |
c4d5e375 DG |
207 | dev_warn(&dev->dev, "obsolete_probe: don't know probe " |
208 | "type '%s', mapping as rom\n", of_probe); | |
2099172d DG |
209 | return do_map_probe("mtd_rom", map); |
210 | } | |
211 | } | |
212 | ||
c4d5e375 DG |
213 | static int __devinit of_flash_probe(struct of_device *dev, |
214 | const struct of_device_id *match) | |
a2c2fe4b VW |
215 | { |
216 | struct device_node *dp = dev->node; | |
217 | struct resource res; | |
c4d5e375 DG |
218 | struct of_flash *info; |
219 | const char *probe_type = match->data; | |
a2c2fe4b VW |
220 | const u32 *width; |
221 | int err; | |
222 | ||
c4d5e375 | 223 | err = -ENXIO; |
a2c2fe4b | 224 | if (of_address_to_resource(dp, 0, &res)) { |
c4d5e375 | 225 | dev_err(&dev->dev, "Can't get IO address from device tree\n"); |
a2c2fe4b VW |
226 | goto err_out; |
227 | } | |
228 | ||
c4d5e375 DG |
229 | dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n", |
230 | (unsigned long long)res.start, (unsigned long long)res.end); | |
a2c2fe4b | 231 | |
c4d5e375 DG |
232 | err = -ENOMEM; |
233 | info = kzalloc(sizeof(*info), GFP_KERNEL); | |
234 | if (!info) | |
a2c2fe4b | 235 | goto err_out; |
a2c2fe4b VW |
236 | |
237 | dev_set_drvdata(&dev->dev, info); | |
238 | ||
c4d5e375 | 239 | err = -EBUSY; |
a2c2fe4b | 240 | info->res = request_mem_region(res.start, res.end - res.start + 1, |
c4d5e375 DG |
241 | dev->dev.bus_id); |
242 | if (!info->res) | |
a2c2fe4b | 243 | goto err_out; |
a2c2fe4b | 244 | |
c4d5e375 | 245 | err = -ENXIO; |
40cd3a45 | 246 | width = of_get_property(dp, "bank-width", NULL); |
c4d5e375 DG |
247 | if (!width) { |
248 | dev_err(&dev->dev, "Can't get bank width from device tree\n"); | |
a2c2fe4b VW |
249 | goto err_out; |
250 | } | |
251 | ||
252 | info->map.name = dev->dev.bus_id; | |
253 | info->map.phys = res.start; | |
254 | info->map.size = res.end - res.start + 1; | |
255 | info->map.bankwidth = *width; | |
256 | ||
c4d5e375 | 257 | err = -ENOMEM; |
a2c2fe4b | 258 | info->map.virt = ioremap(info->map.phys, info->map.size); |
c4d5e375 DG |
259 | if (!info->map.virt) { |
260 | dev_err(&dev->dev, "Failed to ioremap() flash region\n"); | |
a2c2fe4b VW |
261 | goto err_out; |
262 | } | |
263 | ||
264 | simple_map_init(&info->map); | |
265 | ||
2099172d DG |
266 | if (probe_type) |
267 | info->mtd = do_map_probe(probe_type, &info->map); | |
268 | else | |
269 | info->mtd = obsolete_probe(dev, &info->map); | |
270 | ||
c4d5e375 DG |
271 | err = -ENXIO; |
272 | if (!info->mtd) { | |
273 | dev_err(&dev->dev, "do_map_probe() failed\n"); | |
a2c2fe4b VW |
274 | goto err_out; |
275 | } | |
276 | info->mtd->owner = THIS_MODULE; | |
277 | ||
c4d5e375 DG |
278 | err = parse_partitions(info, dev); |
279 | if (err < 0) | |
280 | goto err_out; | |
a2c2fe4b | 281 | |
c4d5e375 DG |
282 | if (err > 0) |
283 | add_mtd_partitions(info->mtd, OF_FLASH_PARTS(info), err); | |
284 | else | |
285 | add_mtd_device(info->mtd); | |
a2c2fe4b | 286 | |
a2c2fe4b VW |
287 | return 0; |
288 | ||
289 | err_out: | |
c4d5e375 | 290 | of_flash_remove(dev); |
a2c2fe4b | 291 | return err; |
a2c2fe4b VW |
292 | } |
293 | ||
c4d5e375 | 294 | static struct of_device_id of_flash_match[] = { |
2099172d DG |
295 | { |
296 | .compatible = "cfi-flash", | |
297 | .data = (void *)"cfi_probe", | |
298 | }, | |
299 | { | |
300 | /* FIXME: JEDEC chips can't be safely and reliably | |
301 | * probed, although the mtd code gets it right in | |
302 | * practice most of the time. We should use the | |
303 | * vendor and device ids specified by the binding to | |
304 | * bypass the heuristic probe code, but the mtd layer | |
305 | * provides, at present, no interface for doing so | |
306 | * :(. */ | |
307 | .compatible = "jedec-flash", | |
308 | .data = (void *)"jedec_probe", | |
309 | }, | |
a2c2fe4b VW |
310 | { |
311 | .type = "rom", | |
312 | .compatible = "direct-mapped" | |
313 | }, | |
314 | { }, | |
315 | }; | |
c4d5e375 | 316 | MODULE_DEVICE_TABLE(of, of_flash_match); |
a2c2fe4b | 317 | |
c4d5e375 DG |
318 | static struct of_platform_driver of_flash_driver = { |
319 | .name = "of-flash", | |
320 | .match_table = of_flash_match, | |
321 | .probe = of_flash_probe, | |
322 | .remove = of_flash_remove, | |
a2c2fe4b VW |
323 | }; |
324 | ||
c4d5e375 | 325 | static int __init of_flash_init(void) |
a2c2fe4b | 326 | { |
c4d5e375 | 327 | return of_register_platform_driver(&of_flash_driver); |
a2c2fe4b VW |
328 | } |
329 | ||
c4d5e375 | 330 | static void __exit of_flash_exit(void) |
a2c2fe4b | 331 | { |
c4d5e375 | 332 | of_unregister_platform_driver(&of_flash_driver); |
a2c2fe4b VW |
333 | } |
334 | ||
c4d5e375 DG |
335 | module_init(of_flash_init); |
336 | module_exit(of_flash_exit); | |
a2c2fe4b VW |
337 | |
338 | MODULE_LICENSE("GPL"); | |
339 | MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>"); | |
c4d5e375 | 340 | MODULE_DESCRIPTION("Device tree based MTD map driver"); |