2 * Flash mappings described by the OF (or flattened) device tree
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/init.h>
19 #include <linux/device.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/map.h>
22 #include <linux/mtd/xip.h>
23 #include <linux/mtd/partitions.h>
25 #include <linux/of_platform.h>
26 #include <linux/slab.h>
32 #ifdef CONFIG_MTD_PARTITIONS
33 struct mtd_partition *parts;
37 // Support for MPC5200 bank select on Shark board, map_priv_1 - MBAR address
38 void of_shark_mpc5200_set_bank(unsigned long map_priv_1, unsigned long addr)
43 cfg = (*(unsigned long *)(map_priv_1 + 0x0300)) & ~0xC0;
44 cfg = cfg | ( ( (addr >> 25) & 3) << 6);
46 //debug("A:%x:%x ", addr, cfg);
47 *(unsigned long *)(map_priv_1 + 0x0300) = cfg;
48 i = 5; // some small delay
51 static map_word __xipram of_shark_map_read(struct map_info *map, unsigned long ofs)
53 of_shark_mpc5200_set_bank(map->map_priv_1, ofs);
54 return inline_map_read(map, ofs);
57 static void __xipram of_shark_map_write(struct map_info *map, const map_word datum, unsigned long ofs)
59 of_shark_mpc5200_set_bank(map->map_priv_1, ofs);
60 inline_map_write(map, datum, ofs);
63 static void __xipram of_shark_map_copy_from(struct map_info *map, void *to, unsigned long from, ssize_t len)
66 memcpy(to, (char *)map->cached + from, len);
69 of_shark_mpc5200_set_bank(map->map_priv_1, from);
70 if( ((int)from < 0x2000000) && ((int)from + len > 0x2000000) )
72 int iFirstBankLength = 0x2000000 - (int)from;
74 memcpy_fromio(to, map->virt + from, iFirstBankLength);
76 of_shark_mpc5200_set_bank(map->map_priv_1, from + iFirstBankLength);
77 memcpy_fromio(to + iFirstBankLength, map->virt + from + iFirstBankLength, len - iFirstBankLength);
80 memcpy_fromio(to, map->virt + from, len);
84 static void __xipram of_shark_map_copy_to(struct map_info *map, unsigned long to, const void *from, ssize_t len)
86 of_shark_mpc5200_set_bank(map->map_priv_1, (unsigned long)from);
87 if( ((int)from < 0x2000000) && ((int)from + len > 0x2000000) )
89 int iFirstBankLength = 0x2000000 - (int)from;
90 memcpy_toio(map->virt + to, from, iFirstBankLength);
92 of_shark_mpc5200_set_bank(map->map_priv_1, (unsigned long)from + iFirstBankLength);
93 memcpy_toio(map->virt + to + iFirstBankLength, from + iFirstBankLength, len - iFirstBankLength);
96 memcpy_toio(map->virt + to, from, len);
99 #ifdef CONFIG_MTD_PARTITIONS
100 #define OF_FLASH_PARTS(info) ((info)->parts)
102 static int parse_obsolete_partitions(struct platform_device *dev,
103 struct of_flash *info,
104 struct device_node *dp)
106 int i, plen, nr_parts;
112 part = of_get_property(dp, "partitions", &plen);
114 return 0; /* No partitions found */
116 dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n");
118 nr_parts = plen / sizeof(part[0]);
120 info->parts = kzalloc(nr_parts * sizeof(*info->parts), GFP_KERNEL);
124 names = of_get_property(dp, "partition-names", &plen);
126 for (i = 0; i < nr_parts; i++) {
127 info->parts[i].offset = part->offset;
128 info->parts[i].size = part->len & ~1;
129 if (part->len & 1) /* bit 0 set signifies read only partition */
130 info->parts[i].mask_flags = MTD_WRITEABLE;
132 if (names && (plen > 0)) {
133 int len = strlen(names) + 1;
135 info->parts[i].name = (char *)names;
139 info->parts[i].name = "unnamed";
147 #else /* MTD_PARTITIONS */
148 #define OF_FLASH_PARTS(info) (0)
149 #define parse_partitions(info, dev) (0)
150 #endif /* MTD_PARTITIONS */
152 static int of_shark_flash_remove(struct platform_device *dev)
154 struct of_flash *info;
156 info = dev_get_drvdata(&dev->dev);
159 dev_set_drvdata(&dev->dev, NULL);
162 if (OF_FLASH_PARTS(info)) {
163 del_mtd_partitions(info->mtd);
164 kfree(OF_FLASH_PARTS(info));
166 del_mtd_device(info->mtd);
168 map_destroy(info->mtd);
171 if (info->map.map_priv_1)
172 iounmap((void*)info->map.map_priv_1);
175 iounmap(info->map.virt);
178 release_resource(info->res);
185 /* Helper function to handle probing of the obsolete "direct-mapped"
186 * compatible binding, which has an extra "probe-type" property
187 * describing the type of flash probe necessary. */
188 static struct mtd_info * __devinit obsolete_probe(struct platform_device *dev,
189 struct map_info *map)
191 struct device_node *dp = dev->dev.of_node;
192 const char *of_probe;
193 struct mtd_info *mtd;
194 static const char *rom_probe_types[]
195 = { "cfi_probe", "jedec_probe", "map_rom"};
198 dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
201 of_probe = of_get_property(dp, "probe-type", NULL);
203 for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
204 mtd = do_map_probe(rom_probe_types[i], map);
209 } else if (strcmp(of_probe, "CFI") == 0) {
210 return do_map_probe("cfi_probe", map);
211 } else if (strcmp(of_probe, "JEDEC") == 0) {
212 return do_map_probe("jedec_probe", map);
214 if (strcmp(of_probe, "ROM") != 0)
215 dev_warn(&dev->dev, "obsolete_probe: don't know probe "
216 "type '%s', mapping as rom\n", of_probe);
217 return do_map_probe("mtd_rom", map);
221 static int __devinit of_shark_flash_probe(struct platform_device *dev,
222 const struct of_device_id *match)
224 #ifdef CONFIG_MTD_PARTITIONS
225 static const char *part_probe_types[]
226 = { "cmdlinepart", "RedBoot", NULL };
228 struct device_node *dp = dev->dev.of_node;
229 struct device_node *np_sys;
231 struct of_flash *info;
232 const char *probe_type = match->data;
236 const struct of_device_id immr_ids[] = {
237 { .compatible = "fsl,mpc5200-immr", },
238 { .compatible = "fsl,mpc5200b-immr", },
239 { .type = "soc", .compatible = "mpc5200", }, /* lite5200 */
240 { .type = "builtin", .compatible = "mpc5200", }, /* efika */
245 if (of_address_to_resource(dp, 0, &res)) {
246 dev_err(&dev->dev, "Can't get IO address from device tree\n");
250 dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
251 (unsigned long long)res.start, (unsigned long long)res.end);
254 info = kzalloc(sizeof(*info), GFP_KERNEL);
258 dev_set_drvdata(&dev->dev, info);
261 info->res = request_mem_region(res.start, res.end - res.start + 1,
262 dev_name(&dev->dev));
267 width = of_get_property(dp, "bank-width", NULL);
269 dev_err(&dev->dev, "Can't get bank width from device tree\n");
273 info->map.name = dev_name(&dev->dev);
274 info->map.phys = res.start;
275 info->map.size = res.end - res.start + 1;
276 info->map.bankwidth = *width;
279 info->map.virt = ioremap(info->map.phys, info->map.size);
280 if (!info->map.virt) {
281 dev_err(&dev->dev, "Failed to ioremap() flash region\n");
285 info->map.read = of_shark_map_read;
286 info->map.write = of_shark_map_write;
287 info->map.copy_from = of_shark_map_copy_from;
288 info->map.copy_to = of_shark_map_copy_to;
290 /* map the whole register space */
291 np_sys = of_find_matching_node(NULL, immr_ids);
292 info->map.map_priv_1 = (unsigned long)of_iomap(np_sys, 0);
293 if (!info->map.map_priv_1) {
294 dev_err(&dev->dev, "of_shark_flash_probe: could not get MBAR address\n");
299 info->mtd = do_map_probe(probe_type, &info->map);
301 info->mtd = obsolete_probe(dev, &info->map);
305 dev_err(&dev->dev, "do_map_probe() failed\n");
308 info->mtd->owner = THIS_MODULE;
309 info->mtd->dev.parent = &dev->dev;
311 #ifdef CONFIG_MTD_PARTITIONS
312 /* First look for RedBoot table or partitions on the command
313 * line, these take precedence over device tree information */
314 err = parse_mtd_partitions(info->mtd, part_probe_types,
319 #ifdef CONFIG_MTD_OF_PARTS
321 err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
328 err = parse_obsolete_partitions(dev, info, dp);
334 add_mtd_partitions(info->mtd, info->parts, err);
337 add_mtd_device(info->mtd);
342 of_shark_flash_remove(dev);
346 static struct of_device_id of_shark_flash_match[] = {
348 .compatible = "cfi-flash",
349 .data = (void *)"cfi_probe",
352 /* FIXME: JEDEC chips can't be safely and reliably
353 * probed, although the mtd code gets it right in
354 * practice most of the time. We should use the
355 * vendor and device ids specified by the binding to
356 * bypass the heuristic probe code, but the mtd layer
357 * provides, at present, no interface for doing so
359 .compatible = "jedec-flash",
360 .data = (void *)"jedec_probe",
364 .compatible = "direct-mapped"
368 MODULE_DEVICE_TABLE(of, of_flash_match);
370 static struct of_platform_driver of_shark_flash_driver = {
373 .owner = THIS_MODULE,
374 .of_match_table = of_shark_flash_match,
377 .probe = of_shark_flash_probe,
378 .remove = of_shark_flash_remove,
381 static int __init of_shark_flash_init(void)
383 return of_register_platform_driver(&of_shark_flash_driver);
386 static void __exit of_shark_flash_exit(void)
388 of_unregister_platform_driver(&of_shark_flash_driver);
391 module_init(of_shark_flash_init);
392 module_exit(of_shark_flash_exit);
394 MODULE_LICENSE("GPL");
395 MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
396 MODULE_DESCRIPTION("Device tree based MTD map driver");