mtd: nand: max_retries off by one in mxc_nand
[deliverable/linux.git] / drivers / mtd / maps / physmap_of.c
CommitLineData
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 26struct 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 38static 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}
2099172d 83#else /* MTD_PARTITIONS */
c4d5e375
DG
84#define OF_FLASH_PARTS(info) (0)
85#define parse_partitions(info, dev) (0)
2099172d 86#endif /* MTD_PARTITIONS */
a2c2fe4b 87
c4d5e375 88static int of_flash_remove(struct of_device *dev)
a2c2fe4b 89{
c4d5e375 90 struct of_flash *info;
a2c2fe4b
VW
91
92 info = dev_get_drvdata(&dev->dev);
c4d5e375 93 if (!info)
a2c2fe4b
VW
94 return 0;
95 dev_set_drvdata(&dev->dev, NULL);
96
c4d5e375
DG
97 if (info->mtd) {
98 if (OF_FLASH_PARTS(info)) {
a2c2fe4b 99 del_mtd_partitions(info->mtd);
c4d5e375 100 kfree(OF_FLASH_PARTS(info));
a2c2fe4b
VW
101 } else {
102 del_mtd_device(info->mtd);
103 }
a2c2fe4b
VW
104 map_destroy(info->mtd);
105 }
106
c4d5e375 107 if (info->map.virt)
a2c2fe4b
VW
108 iounmap(info->map.virt);
109
c4d5e375 110 if (info->res) {
a2c2fe4b
VW
111 release_resource(info->res);
112 kfree(info->res);
113 }
114
115 return 0;
116}
117
2099172d
DG
118/* 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)
a2c2fe4b
VW
123{
124 struct device_node *dp = dev->node;
a2c2fe4b 125 const char *of_probe;
2099172d
DG
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)
c4d5e375
DG
148 dev_warn(&dev->dev, "obsolete_probe: don't know probe "
149 "type '%s', mapping as rom\n", of_probe);
2099172d
DG
150 return do_map_probe("mtd_rom", map);
151 }
152}
153
c4d5e375
DG
154static int __devinit of_flash_probe(struct of_device *dev,
155 const struct of_device_id *match)
a2c2fe4b 156{
9a310d21
SW
157#ifdef CONFIG_MTD_PARTITIONS
158 static const char *part_probe_types[]
159 = { "cmdlinepart", "RedBoot", NULL };
160#endif
a2c2fe4b
VW
161 struct device_node *dp = dev->node;
162 struct resource res;
c4d5e375
DG
163 struct of_flash *info;
164 const char *probe_type = match->data;
a2c2fe4b
VW
165 const u32 *width;
166 int err;
167
c4d5e375 168 err = -ENXIO;
a2c2fe4b 169 if (of_address_to_resource(dp, 0, &res)) {
c4d5e375 170 dev_err(&dev->dev, "Can't get IO address from device tree\n");
a2c2fe4b
VW
171 goto err_out;
172 }
173
c4d5e375
DG
174 dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
175 (unsigned long long)res.start, (unsigned long long)res.end);
a2c2fe4b 176
c4d5e375
DG
177 err = -ENOMEM;
178 info = kzalloc(sizeof(*info), GFP_KERNEL);
179 if (!info)
a2c2fe4b 180 goto err_out;
a2c2fe4b
VW
181
182 dev_set_drvdata(&dev->dev, info);
183
c4d5e375 184 err = -EBUSY;
a2c2fe4b 185 info->res = request_mem_region(res.start, res.end - res.start + 1,
475b44c1 186 dev_name(&dev->dev));
c4d5e375 187 if (!info->res)
a2c2fe4b 188 goto err_out;
a2c2fe4b 189
c4d5e375 190 err = -ENXIO;
40cd3a45 191 width = of_get_property(dp, "bank-width", NULL);
c4d5e375
DG
192 if (!width) {
193 dev_err(&dev->dev, "Can't get bank width from device tree\n");
a2c2fe4b
VW
194 goto err_out;
195 }
196
475b44c1 197 info->map.name = dev_name(&dev->dev);
a2c2fe4b
VW
198 info->map.phys = res.start;
199 info->map.size = res.end - res.start + 1;
200 info->map.bankwidth = *width;
201
c4d5e375 202 err = -ENOMEM;
a2c2fe4b 203 info->map.virt = ioremap(info->map.phys, info->map.size);
c4d5e375
DG
204 if (!info->map.virt) {
205 dev_err(&dev->dev, "Failed to ioremap() flash region\n");
a2c2fe4b
VW
206 goto err_out;
207 }
208
209 simple_map_init(&info->map);
210
2099172d
DG
211 if (probe_type)
212 info->mtd = do_map_probe(probe_type, &info->map);
213 else
214 info->mtd = obsolete_probe(dev, &info->map);
215
c4d5e375
DG
216 err = -ENXIO;
217 if (!info->mtd) {
218 dev_err(&dev->dev, "do_map_probe() failed\n");
a2c2fe4b
VW
219 goto err_out;
220 }
221 info->mtd->owner = THIS_MODULE;
db99a552 222 info->mtd->dev.parent = &dev->dev;
a2c2fe4b 223
9a310d21
SW
224#ifdef CONFIG_MTD_PARTITIONS
225 /* First look for RedBoot table or partitions on the command
226 * line, these take precedence over device tree information */
227 err = parse_mtd_partitions(info->mtd, part_probe_types,
228 &info->parts, 0);
c4d5e375 229 if (err < 0)
9a310d21
SW
230 return err;
231
232#ifdef CONFIG_MTD_OF_PARTS
233 if (err == 0) {
69fd3a8d 234 err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
9a310d21
SW
235 if (err < 0)
236 return err;
237 }
238#endif
239
240 if (err == 0) {
241 err = parse_obsolete_partitions(dev, info, dp);
242 if (err < 0)
243 return err;
244 }
a2c2fe4b 245
c4d5e375 246 if (err > 0)
9a310d21 247 add_mtd_partitions(info->mtd, info->parts, err);
c4d5e375 248 else
9a310d21 249#endif
c4d5e375 250 add_mtd_device(info->mtd);
a2c2fe4b 251
a2c2fe4b
VW
252 return 0;
253
254err_out:
c4d5e375 255 of_flash_remove(dev);
a2c2fe4b 256 return err;
a2c2fe4b
VW
257}
258
c4d5e375 259static struct of_device_id of_flash_match[] = {
2099172d
DG
260 {
261 .compatible = "cfi-flash",
262 .data = (void *)"cfi_probe",
263 },
264 {
265 /* FIXME: JEDEC chips can't be safely and reliably
266 * probed, although the mtd code gets it right in
267 * practice most of the time. We should use the
268 * vendor and device ids specified by the binding to
269 * bypass the heuristic probe code, but the mtd layer
270 * provides, at present, no interface for doing so
271 * :(. */
272 .compatible = "jedec-flash",
273 .data = (void *)"jedec_probe",
274 },
a2c2fe4b
VW
275 {
276 .type = "rom",
277 .compatible = "direct-mapped"
278 },
279 { },
280};
c4d5e375 281MODULE_DEVICE_TABLE(of, of_flash_match);
a2c2fe4b 282
c4d5e375
DG
283static struct of_platform_driver of_flash_driver = {
284 .name = "of-flash",
285 .match_table = of_flash_match,
286 .probe = of_flash_probe,
287 .remove = of_flash_remove,
a2c2fe4b
VW
288};
289
c4d5e375 290static int __init of_flash_init(void)
a2c2fe4b 291{
c4d5e375 292 return of_register_platform_driver(&of_flash_driver);
a2c2fe4b
VW
293}
294
c4d5e375 295static void __exit of_flash_exit(void)
a2c2fe4b 296{
c4d5e375 297 of_unregister_platform_driver(&of_flash_driver);
a2c2fe4b
VW
298}
299
c4d5e375
DG
300module_init(of_flash_init);
301module_exit(of_flash_exit);
a2c2fe4b
VW
302
303MODULE_LICENSE("GPL");
304MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
c4d5e375 305MODULE_DESCRIPTION("Device tree based MTD map driver");
This page took 0.235075 seconds and 5 git commands to generate.