2 * Normal mappings of chips in physical memory for OF devices
4 * Copyright (C) 2006 MontaVista Software Inc.
5 * Author: Vitaly Wool <vwool@ru.mvista.com>
7 * Revised to handle newer style flash binding by:
8 * Copyright (C) 2007 David Gibson, IBM Corporation.
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.
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>
28 #include <asm/of_device.h>
29 #include <asm/of_platform.h>
31 struct physmap_flash_info {
35 #ifdef CONFIG_MTD_PARTITIONS
36 struct mtd_partition *parts;
40 #ifdef CONFIG_MTD_PARTITIONS
41 static int parse_obsolete_partitions(struct of_device *dev,
42 struct physmap_flash_info *info,
43 struct device_node *dp)
45 int i, plen, nr_parts;
51 part = of_get_property(dp, "partitions", &plen);
55 dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n");
57 nr_parts = plen / sizeof(part[0]);
59 info->parts = kzalloc(nr_parts * sizeof(struct mtd_partition), GFP_KERNEL);
61 printk(KERN_ERR "Can't allocate the flash partition data!\n");
65 names = of_get_property(dp, "partition-names", &plen);
67 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;
73 if (names && (plen > 0)) {
74 int len = strlen(names) + 1;
76 info->parts[i].name = (char *)names;
80 info->parts[i].name = "unnamed";
89 static int __devinit process_partitions(struct physmap_flash_info *info,
90 struct of_device *dev)
93 static const char *part_probe_types[]
94 = { "cmdlinepart", "RedBoot", NULL };
95 struct device_node *dp = dev->node, *pp;
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,
103 add_mtd_partitions(info->mtd, info->parts, nr_parts);
107 /* First count the subnodes */
109 for (pp = dp->child; pp; pp = pp->sibling)
113 info->parts = kzalloc(nr_parts * sizeof(struct mtd_partition),
116 printk(KERN_ERR "Can't allocate the flash partition data!\n");
120 for (pp = dp->child, i = 0 ; pp; pp = pp->sibling, i++) {
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",
132 info->parts[i].offset = reg[0];
133 info->parts[i].size = reg[1];
135 partname = of_get_property(pp, "label", &len);
137 partname = of_get_property(pp, "name", &len);
138 info->parts[i].name = (char *)partname;
140 if (of_get_property(pp, "read-only", &len))
141 info->parts[i].mask_flags = MTD_WRITEABLE;
144 nr_parts = parse_obsolete_partitions(dev, info, dp);
145 if (nr_parts == -ENOENT)
153 add_mtd_partitions(info->mtd, info->parts, nr_parts);
155 add_mtd_device(info->mtd);
159 #else /* MTD_PARTITIONS */
160 static int __devinit process_partitions(struct physmap_flash_info *info,
161 struct device_node *dev)
163 add_mtd_device(info->mtd);
166 #endif /* MTD_PARTITIONS */
168 static int of_physmap_remove(struct of_device *dev)
170 struct physmap_flash_info *info;
172 info = dev_get_drvdata(&dev->dev);
175 dev_set_drvdata(&dev->dev, NULL);
177 if (info->mtd != NULL) {
178 #ifdef CONFIG_MTD_PARTITIONS
180 del_mtd_partitions(info->mtd);
183 del_mtd_device(info->mtd);
186 del_mtd_device(info->mtd);
188 map_destroy(info->mtd);
191 if (info->map.virt != NULL)
192 iounmap(info->map.virt);
194 if (info->res != NULL) {
195 release_resource(info->res);
202 /* Helper function to handle probing of the obsolete "direct-mapped"
203 * compatible binding, which has an extra "probe-type" property
204 * describing the type of flash probe necessary. */
205 static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
206 struct map_info *map)
208 struct device_node *dp = dev->node;
209 const char *of_probe;
210 struct mtd_info *mtd;
211 static const char *rom_probe_types[]
212 = { "cfi_probe", "jedec_probe", "map_rom"};
215 dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
218 of_probe = of_get_property(dp, "probe-type", NULL);
220 for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
221 mtd = do_map_probe(rom_probe_types[i], map);
226 } else if (strcmp(of_probe, "CFI") == 0) {
227 return do_map_probe("cfi_probe", map);
228 } else if (strcmp(of_probe, "JEDEC") == 0) {
229 return do_map_probe("jedec_probe", map);
231 if (strcmp(of_probe, "ROM") != 0)
232 dev_dbg(&dev->dev, "obsolete_probe: don't know probe type "
233 "'%s', mapping as rom\n", of_probe);
234 return do_map_probe("mtd_rom", map);
238 static int __devinit of_physmap_probe(struct of_device *dev, const struct of_device_id *match)
240 struct device_node *dp = dev->node;
242 struct physmap_flash_info *info;
243 const char *probe_type = (const char *)match->data;
247 if (of_address_to_resource(dp, 0, &res)) {
248 dev_err(&dev->dev, "Can't get the flash mapping!\n");
253 dev_dbg(&dev->dev, "physmap flash device: %.8llx at %.8llx\n",
254 (unsigned long long)res.end - res.start + 1,
255 (unsigned long long)res.start);
257 info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
262 memset(info, 0, sizeof(*info));
264 dev_set_drvdata(&dev->dev, info);
266 info->res = request_mem_region(res.start, res.end - res.start + 1,
268 if (info->res == NULL) {
269 dev_err(&dev->dev, "Could not reserve memory region\n");
274 width = of_get_property(dp, "bank-width", NULL);
276 dev_err(&dev->dev, "Can't get the flash bank width!\n");
281 info->map.name = dev->dev.bus_id;
282 info->map.phys = res.start;
283 info->map.size = res.end - res.start + 1;
284 info->map.bankwidth = *width;
286 info->map.virt = ioremap(info->map.phys, info->map.size);
287 if (info->map.virt == NULL) {
288 dev_err(&dev->dev, "Failed to ioremap flash region\n");
293 simple_map_init(&info->map);
296 info->mtd = do_map_probe(probe_type, &info->map);
298 info->mtd = obsolete_probe(dev, &info->map);
300 if (info->mtd == NULL) {
301 dev_err(&dev->dev, "map_probe failed\n");
305 info->mtd->owner = THIS_MODULE;
307 return process_partitions(info, dev);
310 of_physmap_remove(dev);
318 static struct of_device_id of_physmap_match[] = {
320 .compatible = "cfi-flash",
321 .data = (void *)"cfi_probe",
324 /* FIXME: JEDEC chips can't be safely and reliably
325 * probed, although the mtd code gets it right in
326 * practice most of the time. We should use the
327 * vendor and device ids specified by the binding to
328 * bypass the heuristic probe code, but the mtd layer
329 * provides, at present, no interface for doing so
331 .compatible = "jedec-flash",
332 .data = (void *)"jedec_probe",
336 .compatible = "direct-mapped"
341 MODULE_DEVICE_TABLE(of, of_physmap_match);
344 static struct of_platform_driver of_physmap_flash_driver = {
345 .name = "physmap-flash",
346 .match_table = of_physmap_match,
347 .probe = of_physmap_probe,
348 .remove = of_physmap_remove,
351 static int __init of_physmap_init(void)
353 return of_register_platform_driver(&of_physmap_flash_driver);
356 static void __exit of_physmap_exit(void)
358 of_unregister_platform_driver(&of_physmap_flash_driver);
361 module_init(of_physmap_init);
362 module_exit(of_physmap_exit);
364 MODULE_LICENSE("GPL");
365 MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
366 MODULE_DESCRIPTION("Configurable MTD map driver for OF");