blob: 3df001bfee365fe110b7d44a07c05a196f44a27f [file] [log] [blame]
Vitaly Woola2c2fe42006-12-06 13:17:49 +03001/*
2 * Normal mappings of chips in physical memory for OF devices
3 *
4 * Copyright (C) 2006 MontaVista Software Inc.
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
6 *
David Gibson20991722007-09-07 13:23:53 +10007 * Revised to handle newer style flash binding by:
8 * Copyright (C) 2007 David Gibson, IBM Corporation.
9 *
Vitaly Woola2c2fe42006-12-06 13:17:49 +030010 * 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>
18#include <linux/kernel.h>
19#include <linux/init.h>
20#include <linux/slab.h>
21#include <linux/device.h>
22#include <linux/mtd/mtd.h>
23#include <linux/mtd/map.h>
24#include <linux/mtd/partitions.h>
25#include <linux/mtd/physmap.h>
26#include <asm/io.h>
27#include <asm/prom.h>
28#include <asm/of_device.h>
29#include <asm/of_platform.h>
30
31struct physmap_flash_info {
32 struct mtd_info *mtd;
33 struct map_info map;
34 struct resource *res;
35#ifdef CONFIG_MTD_PARTITIONS
Vitaly Woola2c2fe42006-12-06 13:17:49 +030036 struct mtd_partition *parts;
37#endif
38};
39
Vitaly Woola2c2fe42006-12-06 13:17:49 +030040#ifdef CONFIG_MTD_PARTITIONS
David Gibson20991722007-09-07 13:23:53 +100041static int parse_obsolete_partitions(struct of_device *dev,
42 struct physmap_flash_info *info,
43 struct device_node *dp)
Vitaly Woola2c2fe42006-12-06 13:17:49 +030044{
David Gibson20991722007-09-07 13:23:53 +100045 int i, plen, nr_parts;
46 const struct {
47 u32 offset, len;
48 } *part;
49 const char *names;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030050
David Gibson20991722007-09-07 13:23:53 +100051 part = of_get_property(dp, "partitions", &plen);
52 if (!part)
53 return -ENOENT;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030054
David Gibson20991722007-09-07 13:23:53 +100055 dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n");
56
57 nr_parts = plen / sizeof(part[0]);
58
59 info->parts = kzalloc(nr_parts * sizeof(struct mtd_partition), GFP_KERNEL);
60 if (!info->parts) {
Vitaly Woola2c2fe42006-12-06 13:17:49 +030061 printk(KERN_ERR "Can't allocate the flash partition data!\n");
David Gibson20991722007-09-07 13:23:53 +100062 return -ENOMEM;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030063 }
64
David Gibson20991722007-09-07 13:23:53 +100065 names = of_get_property(dp, "partition-names", &plen);
Vitaly Woola2c2fe42006-12-06 13:17:49 +030066
David Gibson20991722007-09-07 13:23:53 +100067 for (i = 0; i < nr_parts; i++) {
68 info->parts[i].offset = part->offset;
69 info->parts[i].size = part->len & ~1;
70 if (part->len & 1) /* bit 0 set signifies read only partition */
71 info->parts[i].mask_flags = MTD_WRITEABLE;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030072
David Gibson20991722007-09-07 13:23:53 +100073 if (names && (plen > 0)) {
74 int len = strlen(names) + 1;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030075
David Gibson20991722007-09-07 13:23:53 +100076 info->parts[i].name = (char *)names;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030077 plen -= len;
David Gibson20991722007-09-07 13:23:53 +100078 names += len;
79 } else {
80 info->parts[i].name = "unnamed";
81 }
82
83 part++;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030084 }
David Gibson20991722007-09-07 13:23:53 +100085
86 return nr_parts;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030087}
David Gibson20991722007-09-07 13:23:53 +100088
89static int __devinit process_partitions(struct physmap_flash_info *info,
90 struct of_device *dev)
91{
92 const char *partname;
93 static const char *part_probe_types[]
94 = { "cmdlinepart", "RedBoot", NULL };
95 struct device_node *dp = dev->node, *pp;
96 int nr_parts, i;
97
98 /* First look for RedBoot table or partitions on the command
99 * line, these take precedence over device tree information */
100 nr_parts = parse_mtd_partitions(info->mtd, part_probe_types,
101 &info->parts, 0);
102 if (nr_parts > 0) {
103 add_mtd_partitions(info->mtd, info->parts, nr_parts);
104 return 0;
105 }
106
107 /* First count the subnodes */
108 nr_parts = 0;
109 for (pp = dp->child; pp; pp = pp->sibling)
110 nr_parts++;
111
112 if (nr_parts) {
113 info->parts = kzalloc(nr_parts * sizeof(struct mtd_partition),
114 GFP_KERNEL);
115 if (!info->parts) {
116 printk(KERN_ERR "Can't allocate the flash partition data!\n");
117 return -ENOMEM;
118 }
119
120 for (pp = dp->child, i = 0 ; pp; pp = pp->sibling, i++) {
121 const u32 *reg;
122 int len;
123
124 reg = of_get_property(pp, "reg", &len);
125 if (!reg || (len != 2*sizeof(u32))) {
126 dev_err(&dev->dev, "Invalid 'reg' on %s\n",
127 dp->full_name);
128 kfree(info->parts);
129 info->parts = NULL;
130 return -EINVAL;
131 }
132 info->parts[i].offset = reg[0];
133 info->parts[i].size = reg[1];
134
135 partname = of_get_property(pp, "label", &len);
136 if (!partname)
137 partname = of_get_property(pp, "name", &len);
138 info->parts[i].name = (char *)partname;
139
140 if (of_get_property(pp, "read-only", &len))
141 info->parts[i].mask_flags = MTD_WRITEABLE;
142 }
143 } else {
144 nr_parts = parse_obsolete_partitions(dev, info, dp);
145 }
146
147 if (nr_parts < 0)
148 return nr_parts;
149
150 if (nr_parts > 0)
151 add_mtd_partitions(info->mtd, info->parts, nr_parts);
152 else
153 add_mtd_device(info->mtd);
154
155 return 0;
156}
157#else /* MTD_PARTITIONS */
158static int __devinit process_partitions(struct physmap_flash_info *info,
159 struct device_node *dev)
160{
161 add_mtd_device(info->mtd);
162 return 0;
163}
164#endif /* MTD_PARTITIONS */
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300165
166static int of_physmap_remove(struct of_device *dev)
167{
168 struct physmap_flash_info *info;
169
170 info = dev_get_drvdata(&dev->dev);
171 if (info == NULL)
172 return 0;
173 dev_set_drvdata(&dev->dev, NULL);
174
175 if (info->mtd != NULL) {
176#ifdef CONFIG_MTD_PARTITIONS
David Gibson20991722007-09-07 13:23:53 +1000177 if (info->parts) {
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300178 del_mtd_partitions(info->mtd);
179 kfree(info->parts);
180 } else {
181 del_mtd_device(info->mtd);
182 }
183#else
184 del_mtd_device(info->mtd);
185#endif
186 map_destroy(info->mtd);
187 }
188
189 if (info->map.virt != NULL)
190 iounmap(info->map.virt);
191
192 if (info->res != NULL) {
193 release_resource(info->res);
194 kfree(info->res);
195 }
196
197 return 0;
198}
199
David Gibson20991722007-09-07 13:23:53 +1000200/* Helper function to handle probing of the obsolete "direct-mapped"
201 * compatible binding, which has an extra "probe-type" property
202 * describing the type of flash probe necessary. */
203static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
204 struct map_info *map)
205{
206 struct device_node *dp = dev->node;
207 const char *of_probe;
208 struct mtd_info *mtd;
209 static const char *rom_probe_types[]
210 = { "cfi_probe", "jedec_probe", "map_rom"};
211 int i;
212
213 dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
214 "flash binding\n");
215
216 of_probe = of_get_property(dp, "probe-type", NULL);
217 if (!of_probe) {
218 for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
219 mtd = do_map_probe(rom_probe_types[i], map);
220 if (mtd)
221 return mtd;
222 }
223 return NULL;
224 } else if (strcmp(of_probe, "CFI") == 0) {
225 return do_map_probe("cfi_probe", map);
226 } else if (strcmp(of_probe, "JEDEC") == 0) {
227 return do_map_probe("jedec_probe", map);
228 } else {
229 if (strcmp(of_probe, "ROM") != 0)
230 dev_dbg(&dev->dev, "obsolete_probe: don't know probe type "
231 "'%s', mapping as rom\n", of_probe);
232 return do_map_probe("mtd_rom", map);
233 }
234}
235
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300236static int __devinit of_physmap_probe(struct of_device *dev, const struct of_device_id *match)
237{
238 struct device_node *dp = dev->node;
239 struct resource res;
240 struct physmap_flash_info *info;
David Gibson20991722007-09-07 13:23:53 +1000241 const char *probe_type = (const char *)match->data;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300242 const u32 *width;
243 int err;
244
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300245 if (of_address_to_resource(dp, 0, &res)) {
246 dev_err(&dev->dev, "Can't get the flash mapping!\n");
247 err = -EINVAL;
248 goto err_out;
249 }
250
251 dev_dbg(&dev->dev, "physmap flash device: %.8llx at %.8llx\n",
252 (unsigned long long)res.end - res.start + 1,
253 (unsigned long long)res.start);
254
255 info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
256 if (info == NULL) {
257 err = -ENOMEM;
258 goto err_out;
259 }
260 memset(info, 0, sizeof(*info));
261
262 dev_set_drvdata(&dev->dev, info);
263
264 info->res = request_mem_region(res.start, res.end - res.start + 1,
265 dev->dev.bus_id);
266 if (info->res == NULL) {
267 dev_err(&dev->dev, "Could not reserve memory region\n");
268 err = -ENOMEM;
269 goto err_out;
270 }
271
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000272 width = of_get_property(dp, "bank-width", NULL);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300273 if (width == NULL) {
274 dev_err(&dev->dev, "Can't get the flash bank width!\n");
275 err = -EINVAL;
276 goto err_out;
277 }
278
279 info->map.name = dev->dev.bus_id;
280 info->map.phys = res.start;
281 info->map.size = res.end - res.start + 1;
282 info->map.bankwidth = *width;
283
284 info->map.virt = ioremap(info->map.phys, info->map.size);
285 if (info->map.virt == NULL) {
286 dev_err(&dev->dev, "Failed to ioremap flash region\n");
287 err = EIO;
288 goto err_out;
289 }
290
291 simple_map_init(&info->map);
292
David Gibson20991722007-09-07 13:23:53 +1000293 if (probe_type)
294 info->mtd = do_map_probe(probe_type, &info->map);
295 else
296 info->mtd = obsolete_probe(dev, &info->map);
297
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300298 if (info->mtd == NULL) {
299 dev_err(&dev->dev, "map_probe failed\n");
300 err = -ENXIO;
301 goto err_out;
302 }
303 info->mtd->owner = THIS_MODULE;
304
David Gibson20991722007-09-07 13:23:53 +1000305 return process_partitions(info, dev);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300306
307err_out:
308 of_physmap_remove(dev);
309 return err;
310
311 return 0;
312
313
314}
315
316static struct of_device_id of_physmap_match[] = {
317 {
David Gibson20991722007-09-07 13:23:53 +1000318 .compatible = "cfi-flash",
319 .data = (void *)"cfi_probe",
320 },
321 {
322 /* FIXME: JEDEC chips can't be safely and reliably
323 * probed, although the mtd code gets it right in
324 * practice most of the time. We should use the
325 * vendor and device ids specified by the binding to
326 * bypass the heuristic probe code, but the mtd layer
327 * provides, at present, no interface for doing so
328 * :(. */
329 .compatible = "jedec-flash",
330 .data = (void *)"jedec_probe",
331 },
332 {
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300333 .type = "rom",
334 .compatible = "direct-mapped"
335 },
336 { },
337};
338
339MODULE_DEVICE_TABLE(of, of_physmap_match);
340
341
342static struct of_platform_driver of_physmap_flash_driver = {
343 .name = "physmap-flash",
344 .match_table = of_physmap_match,
345 .probe = of_physmap_probe,
346 .remove = of_physmap_remove,
347};
348
349static int __init of_physmap_init(void)
350{
351 return of_register_platform_driver(&of_physmap_flash_driver);
352}
353
354static void __exit of_physmap_exit(void)
355{
356 of_unregister_platform_driver(&of_physmap_flash_driver);
357}
358
359module_init(of_physmap_init);
360module_exit(of_physmap_exit);
361
362MODULE_LICENSE("GPL");
363MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
364MODULE_DESCRIPTION("Configurable MTD map driver for OF");