blob: 5fcfec034a9466819f83fb5ae00670e1c7fa976a [file] [log] [blame]
Vitaly Woola2c2fe42006-12-06 13:17:49 +03001/*
David Gibsonc4d5e372007-09-20 11:22:25 +10002 * Flash mappings described by the OF (or flattened) device tree
Vitaly Woola2c2fe42006-12-06 13:17:49 +03003 *
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>
Vitaly Woola2c2fe42006-12-06 13:17:49 +030018#include <linux/init.h>
Vitaly Woola2c2fe42006-12-06 13:17:49 +030019#include <linux/device.h>
20#include <linux/mtd/mtd.h>
21#include <linux/mtd/map.h>
22#include <linux/mtd/partitions.h>
David Gibsonc4d5e372007-09-20 11:22:25 +100023#include <linux/of.h>
24#include <linux/of_platform.h>
Vitaly Woola2c2fe42006-12-06 13:17:49 +030025
David Gibsonc4d5e372007-09-20 11:22:25 +100026struct of_flash {
Vitaly Woola2c2fe42006-12-06 13:17:49 +030027 struct mtd_info *mtd;
28 struct map_info map;
29 struct resource *res;
30#ifdef CONFIG_MTD_PARTITIONS
Vitaly Woola2c2fe42006-12-06 13:17:49 +030031 struct mtd_partition *parts;
32#endif
33};
34
Vitaly Woola2c2fe42006-12-06 13:17:49 +030035#ifdef CONFIG_MTD_PARTITIONS
David Gibsonc4d5e372007-09-20 11:22:25 +100036#define OF_FLASH_PARTS(info) ((info)->parts)
Vitaly Woola2c2fe42006-12-06 13:17:49 +030037
David Gibson20991722007-09-07 13:23:53 +100038static int parse_obsolete_partitions(struct of_device *dev,
David Gibsonc4d5e372007-09-20 11:22:25 +100039 struct of_flash *info,
David Gibson20991722007-09-07 13:23:53 +100040 struct device_node *dp)
Vitaly Woola2c2fe42006-12-06 13:17:49 +030041{
David Gibson20991722007-09-07 13:23:53 +100042 int i, plen, nr_parts;
43 const struct {
44 u32 offset, len;
45 } *part;
46 const char *names;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030047
David Gibson20991722007-09-07 13:23:53 +100048 part = of_get_property(dp, "partitions", &plen);
49 if (!part)
David Gibsonc4d5e372007-09-20 11:22:25 +100050 return 0; /* No partitions found */
Vitaly Woola2c2fe42006-12-06 13:17:49 +030051
David Gibson20991722007-09-07 13:23:53 +100052 dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n");
Vitaly Woola2c2fe42006-12-06 13:17:49 +030053
David Gibson20991722007-09-07 13:23:53 +100054 nr_parts = plen / sizeof(part[0]);
Vitaly Woola2c2fe42006-12-06 13:17:49 +030055
David Gibsonc4d5e372007-09-20 11:22:25 +100056 info->parts = kzalloc(nr_parts * sizeof(*info->parts), GFP_KERNEL);
57 if (!info->parts)
David Gibson20991722007-09-07 13:23:53 +100058 return -ENOMEM;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030059
David Gibson20991722007-09-07 13:23:53 +100060 names = of_get_property(dp, "partition-names", &plen);
Vitaly Woola2c2fe42006-12-06 13:17:49 +030061
David Gibson20991722007-09-07 13:23:53 +100062 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;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030067
David Gibson20991722007-09-07 13:23:53 +100068 if (names && (plen > 0)) {
69 int len = strlen(names) + 1;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030070
David Gibson20991722007-09-07 13:23:53 +100071 info->parts[i].name = (char *)names;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030072 plen -= len;
David Gibson20991722007-09-07 13:23:53 +100073 names += len;
74 } else {
75 info->parts[i].name = "unnamed";
76 }
Vitaly Woola2c2fe42006-12-06 13:17:49 +030077
David Gibson20991722007-09-07 13:23:53 +100078 part++;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030079 }
David Gibson20991722007-09-07 13:23:53 +100080
81 return nr_parts;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030082}
David Gibson20991722007-09-07 13:23:53 +100083#else /* MTD_PARTITIONS */
David Gibsonc4d5e372007-09-20 11:22:25 +100084#define OF_FLASH_PARTS(info) (0)
85#define parse_partitions(info, dev) (0)
David Gibson20991722007-09-07 13:23:53 +100086#endif /* MTD_PARTITIONS */
Vitaly Woola2c2fe42006-12-06 13:17:49 +030087
David Gibsonc4d5e372007-09-20 11:22:25 +100088static int of_flash_remove(struct of_device *dev)
Vitaly Woola2c2fe42006-12-06 13:17:49 +030089{
David Gibsonc4d5e372007-09-20 11:22:25 +100090 struct of_flash *info;
Vitaly Woola2c2fe42006-12-06 13:17:49 +030091
92 info = dev_get_drvdata(&dev->dev);
David Gibsonc4d5e372007-09-20 11:22:25 +100093 if (!info)
Vitaly Woola2c2fe42006-12-06 13:17:49 +030094 return 0;
95 dev_set_drvdata(&dev->dev, NULL);
96
David Gibsonc4d5e372007-09-20 11:22:25 +100097 if (info->mtd) {
98 if (OF_FLASH_PARTS(info)) {
Vitaly Woola2c2fe42006-12-06 13:17:49 +030099 del_mtd_partitions(info->mtd);
David Gibsonc4d5e372007-09-20 11:22:25 +1000100 kfree(OF_FLASH_PARTS(info));
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300101 } else {
102 del_mtd_device(info->mtd);
103 }
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300104 map_destroy(info->mtd);
105 }
106
David Gibsonc4d5e372007-09-20 11:22:25 +1000107 if (info->map.virt)
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300108 iounmap(info->map.virt);
109
David Gibsonc4d5e372007-09-20 11:22:25 +1000110 if (info->res) {
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300111 release_resource(info->res);
112 kfree(info->res);
113 }
114
115 return 0;
116}
117
David Gibson20991722007-09-07 13:23:53 +1000118/* Helper function to handle probing of the obsolete "direct-mapped"
119 * compatible binding, which has an extra "probe-type" property
120 * describing the type of flash probe necessary. */
121static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
122 struct map_info *map)
123{
124 struct device_node *dp = dev->node;
125 const char *of_probe;
126 struct mtd_info *mtd;
127 static const char *rom_probe_types[]
128 = { "cfi_probe", "jedec_probe", "map_rom"};
129 int i;
130
131 dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
132 "flash binding\n");
133
134 of_probe = of_get_property(dp, "probe-type", NULL);
135 if (!of_probe) {
136 for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
137 mtd = do_map_probe(rom_probe_types[i], map);
138 if (mtd)
139 return mtd;
140 }
141 return NULL;
142 } else if (strcmp(of_probe, "CFI") == 0) {
143 return do_map_probe("cfi_probe", map);
144 } else if (strcmp(of_probe, "JEDEC") == 0) {
145 return do_map_probe("jedec_probe", map);
146 } else {
147 if (strcmp(of_probe, "ROM") != 0)
David Gibsonc4d5e372007-09-20 11:22:25 +1000148 dev_warn(&dev->dev, "obsolete_probe: don't know probe "
149 "type '%s', mapping as rom\n", of_probe);
David Gibson20991722007-09-07 13:23:53 +1000150 return do_map_probe("mtd_rom", map);
151 }
152}
153
David Gibsonc4d5e372007-09-20 11:22:25 +1000154static int __devinit of_flash_probe(struct of_device *dev,
155 const struct of_device_id *match)
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300156{
Scott Wood9a310d22008-01-15 17:54:43 -0600157#ifdef CONFIG_MTD_PARTITIONS
158 static const char *part_probe_types[]
159 = { "cmdlinepart", "RedBoot", NULL };
160#endif
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300161 struct device_node *dp = dev->node;
162 struct resource res;
David Gibsonc4d5e372007-09-20 11:22:25 +1000163 struct of_flash *info;
164 const char *probe_type = match->data;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300165 const u32 *width;
166 int err;
167
David Gibsonc4d5e372007-09-20 11:22:25 +1000168 err = -ENXIO;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300169 if (of_address_to_resource(dp, 0, &res)) {
David Gibsonc4d5e372007-09-20 11:22:25 +1000170 dev_err(&dev->dev, "Can't get IO address from device tree\n");
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300171 goto err_out;
172 }
173
David Gibsonc4d5e372007-09-20 11:22:25 +1000174 dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
175 (unsigned long long)res.start, (unsigned long long)res.end);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300176
David Gibsonc4d5e372007-09-20 11:22:25 +1000177 err = -ENOMEM;
178 info = kzalloc(sizeof(*info), GFP_KERNEL);
179 if (!info)
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300180 goto err_out;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300181
182 dev_set_drvdata(&dev->dev, info);
183
David Gibsonc4d5e372007-09-20 11:22:25 +1000184 err = -EBUSY;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300185 info->res = request_mem_region(res.start, res.end - res.start + 1,
David Gibsonc4d5e372007-09-20 11:22:25 +1000186 dev->dev.bus_id);
187 if (!info->res)
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300188 goto err_out;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300189
David Gibsonc4d5e372007-09-20 11:22:25 +1000190 err = -ENXIO;
Stephen Rothwell40cd3a42007-05-01 13:54:02 +1000191 width = of_get_property(dp, "bank-width", NULL);
David Gibsonc4d5e372007-09-20 11:22:25 +1000192 if (!width) {
193 dev_err(&dev->dev, "Can't get bank width from device tree\n");
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300194 goto err_out;
195 }
196
197 info->map.name = dev->dev.bus_id;
198 info->map.phys = res.start;
199 info->map.size = res.end - res.start + 1;
200 info->map.bankwidth = *width;
201
David Gibsonc4d5e372007-09-20 11:22:25 +1000202 err = -ENOMEM;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300203 info->map.virt = ioremap(info->map.phys, info->map.size);
David Gibsonc4d5e372007-09-20 11:22:25 +1000204 if (!info->map.virt) {
205 dev_err(&dev->dev, "Failed to ioremap() flash region\n");
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300206 goto err_out;
207 }
208
209 simple_map_init(&info->map);
210
David Gibson20991722007-09-07 13:23:53 +1000211 if (probe_type)
212 info->mtd = do_map_probe(probe_type, &info->map);
213 else
214 info->mtd = obsolete_probe(dev, &info->map);
215
David Gibsonc4d5e372007-09-20 11:22:25 +1000216 err = -ENXIO;
217 if (!info->mtd) {
218 dev_err(&dev->dev, "do_map_probe() failed\n");
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300219 goto err_out;
220 }
221 info->mtd->owner = THIS_MODULE;
222
Scott Wood9a310d22008-01-15 17:54:43 -0600223#ifdef CONFIG_MTD_PARTITIONS
224 /* First look for RedBoot table or partitions on the command
225 * line, these take precedence over device tree information */
226 err = parse_mtd_partitions(info->mtd, part_probe_types,
227 &info->parts, 0);
David Gibsonc4d5e372007-09-20 11:22:25 +1000228 if (err < 0)
Scott Wood9a310d22008-01-15 17:54:43 -0600229 return err;
230
231#ifdef CONFIG_MTD_OF_PARTS
232 if (err == 0) {
Sebastian Andrzej Siewior69fd3a82008-10-12 16:18:36 +0200233 err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
Scott Wood9a310d22008-01-15 17:54:43 -0600234 if (err < 0)
235 return err;
236 }
237#endif
238
239 if (err == 0) {
240 err = parse_obsolete_partitions(dev, info, dp);
241 if (err < 0)
242 return err;
243 }
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300244
David Gibsonc4d5e372007-09-20 11:22:25 +1000245 if (err > 0)
Scott Wood9a310d22008-01-15 17:54:43 -0600246 add_mtd_partitions(info->mtd, info->parts, err);
David Gibsonc4d5e372007-09-20 11:22:25 +1000247 else
Scott Wood9a310d22008-01-15 17:54:43 -0600248#endif
David Gibsonc4d5e372007-09-20 11:22:25 +1000249 add_mtd_device(info->mtd);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300250
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300251 return 0;
252
253err_out:
David Gibsonc4d5e372007-09-20 11:22:25 +1000254 of_flash_remove(dev);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300255 return err;
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300256}
257
David Gibsonc4d5e372007-09-20 11:22:25 +1000258static struct of_device_id of_flash_match[] = {
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300259 {
David Gibson20991722007-09-07 13:23:53 +1000260 .compatible = "cfi-flash",
261 .data = (void *)"cfi_probe",
262 },
263 {
264 /* FIXME: JEDEC chips can't be safely and reliably
265 * probed, although the mtd code gets it right in
266 * practice most of the time. We should use the
267 * vendor and device ids specified by the binding to
268 * bypass the heuristic probe code, but the mtd layer
269 * provides, at present, no interface for doing so
270 * :(. */
271 .compatible = "jedec-flash",
272 .data = (void *)"jedec_probe",
273 },
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300274 {
275 .type = "rom",
276 .compatible = "direct-mapped"
277 },
278 { },
279};
David Gibsonc4d5e372007-09-20 11:22:25 +1000280MODULE_DEVICE_TABLE(of, of_flash_match);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300281
David Gibsonc4d5e372007-09-20 11:22:25 +1000282static struct of_platform_driver of_flash_driver = {
283 .name = "of-flash",
284 .match_table = of_flash_match,
285 .probe = of_flash_probe,
286 .remove = of_flash_remove,
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300287};
288
David Gibsonc4d5e372007-09-20 11:22:25 +1000289static int __init of_flash_init(void)
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300290{
David Gibsonc4d5e372007-09-20 11:22:25 +1000291 return of_register_platform_driver(&of_flash_driver);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300292}
293
David Gibsonc4d5e372007-09-20 11:22:25 +1000294static void __exit of_flash_exit(void)
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300295{
David Gibsonc4d5e372007-09-20 11:22:25 +1000296 of_unregister_platform_driver(&of_flash_driver);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300297}
298
David Gibsonc4d5e372007-09-20 11:22:25 +1000299module_init(of_flash_init);
300module_exit(of_flash_exit);
Vitaly Woola2c2fe42006-12-06 13:17:49 +0300301
302MODULE_LICENSE("GPL");
303MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
David Gibsonc4d5e372007-09-20 11:22:25 +1000304MODULE_DESCRIPTION("Device tree based MTD map driver");