physmap_of.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. /*
  2. * Flash mappings described by the OF (or flattened) device tree
  3. *
  4. * Copyright (C) 2006 MontaVista Software Inc.
  5. * Author: Vitaly Wool <vwool@ru.mvista.com>
  6. *
  7. * Revised to handle newer style flash binding by:
  8. * Copyright (C) 2007 David Gibson, IBM Corporation.
  9. *
  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. #include <linux/module.h>
  16. #include <linux/types.h>
  17. #include <linux/init.h>
  18. #include <linux/device.h>
  19. #include <linux/mtd/mtd.h>
  20. #include <linux/mtd/map.h>
  21. #include <linux/mtd/partitions.h>
  22. #include <linux/of.h>
  23. #include <linux/of_platform.h>
  24. struct of_flash {
  25. struct mtd_info *mtd;
  26. struct map_info map;
  27. struct resource *res;
  28. #ifdef CONFIG_MTD_PARTITIONS
  29. struct mtd_partition *parts;
  30. #endif
  31. };
  32. #ifdef CONFIG_MTD_PARTITIONS
  33. #define OF_FLASH_PARTS(info) ((info)->parts)
  34. static int parse_obsolete_partitions(struct of_device *dev,
  35. struct of_flash *info,
  36. struct device_node *dp)
  37. {
  38. int i, plen, nr_parts;
  39. const struct {
  40. u32 offset, len;
  41. } *part;
  42. const char *names;
  43. part = of_get_property(dp, "partitions", &plen);
  44. if (!part)
  45. return 0; /* No partitions found */
  46. dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n");
  47. nr_parts = plen / sizeof(part[0]);
  48. info->parts = kzalloc(nr_parts * sizeof(*info->parts), GFP_KERNEL);
  49. if (!info->parts)
  50. return -ENOMEM;
  51. names = of_get_property(dp, "partition-names", &plen);
  52. for (i = 0; i < nr_parts; i++) {
  53. info->parts[i].offset = part->offset;
  54. info->parts[i].size = part->len & ~1;
  55. if (part->len & 1) /* bit 0 set signifies read only partition */
  56. info->parts[i].mask_flags = MTD_WRITEABLE;
  57. if (names && (plen > 0)) {
  58. int len = strlen(names) + 1;
  59. info->parts[i].name = (char *)names;
  60. plen -= len;
  61. names += len;
  62. } else {
  63. info->parts[i].name = "unnamed";
  64. }
  65. part++;
  66. }
  67. return nr_parts;
  68. }
  69. static int __devinit parse_partitions(struct of_flash *info,
  70. struct of_device *dev)
  71. {
  72. const char *partname;
  73. static const char *part_probe_types[]
  74. = { "cmdlinepart", "RedBoot", NULL };
  75. struct device_node *dp = dev->node, *pp;
  76. int nr_parts, i;
  77. /* First look for RedBoot table or partitions on the command
  78. * line, these take precedence over device tree information */
  79. nr_parts = parse_mtd_partitions(info->mtd, part_probe_types,
  80. &info->parts, 0);
  81. if (nr_parts > 0) {
  82. add_mtd_partitions(info->mtd, info->parts, nr_parts);
  83. return 0;
  84. }
  85. /* First count the subnodes */
  86. nr_parts = 0;
  87. for (pp = dp->child; pp; pp = pp->sibling)
  88. nr_parts++;
  89. if (nr_parts == 0)
  90. return parse_obsolete_partitions(dev, info, dp);
  91. info->parts = kzalloc(nr_parts * sizeof(*info->parts),
  92. GFP_KERNEL);
  93. if (!info->parts)
  94. return -ENOMEM;
  95. for (pp = dp->child, i = 0; pp; pp = pp->sibling, i++) {
  96. const u32 *reg;
  97. int len;
  98. reg = of_get_property(pp, "reg", &len);
  99. if (!reg || (len != 2*sizeof(u32))) {
  100. dev_err(&dev->dev, "Invalid 'reg' on %s\n",
  101. dp->full_name);
  102. kfree(info->parts);
  103. info->parts = NULL;
  104. return -EINVAL;
  105. }
  106. info->parts[i].offset = reg[0];
  107. info->parts[i].size = reg[1];
  108. partname = of_get_property(pp, "label", &len);
  109. if (!partname)
  110. partname = of_get_property(pp, "name", &len);
  111. info->parts[i].name = (char *)partname;
  112. if (of_get_property(pp, "read-only", &len))
  113. info->parts[i].mask_flags = MTD_WRITEABLE;
  114. }
  115. return nr_parts;
  116. }
  117. #else /* MTD_PARTITIONS */
  118. #define OF_FLASH_PARTS(info) (0)
  119. #define parse_partitions(info, dev) (0)
  120. #endif /* MTD_PARTITIONS */
  121. static int of_flash_remove(struct of_device *dev)
  122. {
  123. struct of_flash *info;
  124. info = dev_get_drvdata(&dev->dev);
  125. if (!info)
  126. return 0;
  127. dev_set_drvdata(&dev->dev, NULL);
  128. if (info->mtd) {
  129. if (OF_FLASH_PARTS(info)) {
  130. del_mtd_partitions(info->mtd);
  131. kfree(OF_FLASH_PARTS(info));
  132. } else {
  133. del_mtd_device(info->mtd);
  134. }
  135. map_destroy(info->mtd);
  136. }
  137. if (info->map.virt)
  138. iounmap(info->map.virt);
  139. if (info->res) {
  140. release_resource(info->res);
  141. kfree(info->res);
  142. }
  143. return 0;
  144. }
  145. /* Helper function to handle probing of the obsolete "direct-mapped"
  146. * compatible binding, which has an extra "probe-type" property
  147. * describing the type of flash probe necessary. */
  148. static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
  149. struct map_info *map)
  150. {
  151. struct device_node *dp = dev->node;
  152. const char *of_probe;
  153. struct mtd_info *mtd;
  154. static const char *rom_probe_types[]
  155. = { "cfi_probe", "jedec_probe", "map_rom"};
  156. int i;
  157. dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
  158. "flash binding\n");
  159. of_probe = of_get_property(dp, "probe-type", NULL);
  160. if (!of_probe) {
  161. for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
  162. mtd = do_map_probe(rom_probe_types[i], map);
  163. if (mtd)
  164. return mtd;
  165. }
  166. return NULL;
  167. } else if (strcmp(of_probe, "CFI") == 0) {
  168. return do_map_probe("cfi_probe", map);
  169. } else if (strcmp(of_probe, "JEDEC") == 0) {
  170. return do_map_probe("jedec_probe", map);
  171. } else {
  172. if (strcmp(of_probe, "ROM") != 0)
  173. dev_warn(&dev->dev, "obsolete_probe: don't know probe "
  174. "type '%s', mapping as rom\n", of_probe);
  175. return do_map_probe("mtd_rom", map);
  176. }
  177. }
  178. static int __devinit of_flash_probe(struct of_device *dev,
  179. const struct of_device_id *match)
  180. {
  181. struct device_node *dp = dev->node;
  182. struct resource res;
  183. struct of_flash *info;
  184. const char *probe_type = match->data;
  185. const u32 *width;
  186. int err;
  187. err = -ENXIO;
  188. if (of_address_to_resource(dp, 0, &res)) {
  189. dev_err(&dev->dev, "Can't get IO address from device tree\n");
  190. goto err_out;
  191. }
  192. dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
  193. (unsigned long long)res.start, (unsigned long long)res.end);
  194. err = -ENOMEM;
  195. info = kzalloc(sizeof(*info), GFP_KERNEL);
  196. if (!info)
  197. goto err_out;
  198. dev_set_drvdata(&dev->dev, info);
  199. err = -EBUSY;
  200. info->res = request_mem_region(res.start, res.end - res.start + 1,
  201. dev->dev.bus_id);
  202. if (!info->res)
  203. goto err_out;
  204. err = -ENXIO;
  205. width = of_get_property(dp, "bank-width", NULL);
  206. if (!width) {
  207. dev_err(&dev->dev, "Can't get bank width from device tree\n");
  208. goto err_out;
  209. }
  210. info->map.name = dev->dev.bus_id;
  211. info->map.phys = res.start;
  212. info->map.size = res.end - res.start + 1;
  213. info->map.bankwidth = *width;
  214. err = -ENOMEM;
  215. info->map.virt = ioremap(info->map.phys, info->map.size);
  216. if (!info->map.virt) {
  217. dev_err(&dev->dev, "Failed to ioremap() flash region\n");
  218. goto err_out;
  219. }
  220. simple_map_init(&info->map);
  221. if (probe_type)
  222. info->mtd = do_map_probe(probe_type, &info->map);
  223. else
  224. info->mtd = obsolete_probe(dev, &info->map);
  225. err = -ENXIO;
  226. if (!info->mtd) {
  227. dev_err(&dev->dev, "do_map_probe() failed\n");
  228. goto err_out;
  229. }
  230. info->mtd->owner = THIS_MODULE;
  231. err = parse_partitions(info, dev);
  232. if (err < 0)
  233. goto err_out;
  234. if (err > 0)
  235. add_mtd_partitions(info->mtd, OF_FLASH_PARTS(info), err);
  236. else
  237. add_mtd_device(info->mtd);
  238. return 0;
  239. err_out:
  240. of_flash_remove(dev);
  241. return err;
  242. }
  243. static struct of_device_id of_flash_match[] = {
  244. {
  245. .compatible = "cfi-flash",
  246. .data = (void *)"cfi_probe",
  247. },
  248. {
  249. /* FIXME: JEDEC chips can't be safely and reliably
  250. * probed, although the mtd code gets it right in
  251. * practice most of the time. We should use the
  252. * vendor and device ids specified by the binding to
  253. * bypass the heuristic probe code, but the mtd layer
  254. * provides, at present, no interface for doing so
  255. * :(. */
  256. .compatible = "jedec-flash",
  257. .data = (void *)"jedec_probe",
  258. },
  259. {
  260. .type = "rom",
  261. .compatible = "direct-mapped"
  262. },
  263. { },
  264. };
  265. MODULE_DEVICE_TABLE(of, of_flash_match);
  266. static struct of_platform_driver of_flash_driver = {
  267. .name = "of-flash",
  268. .match_table = of_flash_match,
  269. .probe = of_flash_probe,
  270. .remove = of_flash_remove,
  271. };
  272. static int __init of_flash_init(void)
  273. {
  274. return of_register_platform_driver(&of_flash_driver);
  275. }
  276. static void __exit of_flash_exit(void)
  277. {
  278. of_unregister_platform_driver(&of_flash_driver);
  279. }
  280. module_init(of_flash_init);
  281. module_exit(of_flash_exit);
  282. MODULE_LICENSE("GPL");
  283. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  284. MODULE_DESCRIPTION("Device tree based MTD map driver");