physmap_of.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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. #else /* MTD_PARTITIONS */
  70. #define OF_FLASH_PARTS(info) (0)
  71. #define parse_partitions(info, dev) (0)
  72. #endif /* MTD_PARTITIONS */
  73. static int of_flash_remove(struct of_device *dev)
  74. {
  75. struct of_flash *info;
  76. info = dev_get_drvdata(&dev->dev);
  77. if (!info)
  78. return 0;
  79. dev_set_drvdata(&dev->dev, NULL);
  80. if (info->mtd) {
  81. if (OF_FLASH_PARTS(info)) {
  82. del_mtd_partitions(info->mtd);
  83. kfree(OF_FLASH_PARTS(info));
  84. } else {
  85. del_mtd_device(info->mtd);
  86. }
  87. map_destroy(info->mtd);
  88. }
  89. if (info->map.virt)
  90. iounmap(info->map.virt);
  91. if (info->res) {
  92. release_resource(info->res);
  93. kfree(info->res);
  94. }
  95. return 0;
  96. }
  97. /* Helper function to handle probing of the obsolete "direct-mapped"
  98. * compatible binding, which has an extra "probe-type" property
  99. * describing the type of flash probe necessary. */
  100. static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
  101. struct map_info *map)
  102. {
  103. struct device_node *dp = dev->node;
  104. const char *of_probe;
  105. struct mtd_info *mtd;
  106. static const char *rom_probe_types[]
  107. = { "cfi_probe", "jedec_probe", "map_rom"};
  108. int i;
  109. dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
  110. "flash binding\n");
  111. of_probe = of_get_property(dp, "probe-type", NULL);
  112. if (!of_probe) {
  113. for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
  114. mtd = do_map_probe(rom_probe_types[i], map);
  115. if (mtd)
  116. return mtd;
  117. }
  118. return NULL;
  119. } else if (strcmp(of_probe, "CFI") == 0) {
  120. return do_map_probe("cfi_probe", map);
  121. } else if (strcmp(of_probe, "JEDEC") == 0) {
  122. return do_map_probe("jedec_probe", map);
  123. } else {
  124. if (strcmp(of_probe, "ROM") != 0)
  125. dev_warn(&dev->dev, "obsolete_probe: don't know probe "
  126. "type '%s', mapping as rom\n", of_probe);
  127. return do_map_probe("mtd_rom", map);
  128. }
  129. }
  130. static int __devinit of_flash_probe(struct of_device *dev,
  131. const struct of_device_id *match)
  132. {
  133. #ifdef CONFIG_MTD_PARTITIONS
  134. static const char *part_probe_types[]
  135. = { "cmdlinepart", "RedBoot", NULL };
  136. #endif
  137. struct device_node *dp = dev->node;
  138. struct resource res;
  139. struct of_flash *info;
  140. const char *probe_type = match->data;
  141. const u32 *width;
  142. int err;
  143. err = -ENXIO;
  144. if (of_address_to_resource(dp, 0, &res)) {
  145. dev_err(&dev->dev, "Can't get IO address from device tree\n");
  146. goto err_out;
  147. }
  148. dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
  149. (unsigned long long)res.start, (unsigned long long)res.end);
  150. err = -ENOMEM;
  151. info = kzalloc(sizeof(*info), GFP_KERNEL);
  152. if (!info)
  153. goto err_out;
  154. dev_set_drvdata(&dev->dev, info);
  155. err = -EBUSY;
  156. info->res = request_mem_region(res.start, res.end - res.start + 1,
  157. dev->dev.bus_id);
  158. if (!info->res)
  159. goto err_out;
  160. err = -ENXIO;
  161. width = of_get_property(dp, "bank-width", NULL);
  162. if (!width) {
  163. dev_err(&dev->dev, "Can't get bank width from device tree\n");
  164. goto err_out;
  165. }
  166. info->map.name = dev->dev.bus_id;
  167. info->map.phys = res.start;
  168. info->map.size = res.end - res.start + 1;
  169. info->map.bankwidth = *width;
  170. err = -ENOMEM;
  171. info->map.virt = ioremap(info->map.phys, info->map.size);
  172. if (!info->map.virt) {
  173. dev_err(&dev->dev, "Failed to ioremap() flash region\n");
  174. goto err_out;
  175. }
  176. simple_map_init(&info->map);
  177. if (probe_type)
  178. info->mtd = do_map_probe(probe_type, &info->map);
  179. else
  180. info->mtd = obsolete_probe(dev, &info->map);
  181. err = -ENXIO;
  182. if (!info->mtd) {
  183. dev_err(&dev->dev, "do_map_probe() failed\n");
  184. goto err_out;
  185. }
  186. info->mtd->owner = THIS_MODULE;
  187. #ifdef CONFIG_MTD_PARTITIONS
  188. /* First look for RedBoot table or partitions on the command
  189. * line, these take precedence over device tree information */
  190. err = parse_mtd_partitions(info->mtd, part_probe_types,
  191. &info->parts, 0);
  192. if (err < 0)
  193. return err;
  194. #ifdef CONFIG_MTD_OF_PARTS
  195. if (err == 0) {
  196. err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
  197. if (err < 0)
  198. return err;
  199. }
  200. #endif
  201. if (err == 0) {
  202. err = parse_obsolete_partitions(dev, info, dp);
  203. if (err < 0)
  204. return err;
  205. }
  206. if (err > 0)
  207. add_mtd_partitions(info->mtd, info->parts, err);
  208. else
  209. #endif
  210. add_mtd_device(info->mtd);
  211. return 0;
  212. err_out:
  213. of_flash_remove(dev);
  214. return err;
  215. }
  216. static struct of_device_id of_flash_match[] = {
  217. {
  218. .compatible = "cfi-flash",
  219. .data = (void *)"cfi_probe",
  220. },
  221. {
  222. /* FIXME: JEDEC chips can't be safely and reliably
  223. * probed, although the mtd code gets it right in
  224. * practice most of the time. We should use the
  225. * vendor and device ids specified by the binding to
  226. * bypass the heuristic probe code, but the mtd layer
  227. * provides, at present, no interface for doing so
  228. * :(. */
  229. .compatible = "jedec-flash",
  230. .data = (void *)"jedec_probe",
  231. },
  232. {
  233. .type = "rom",
  234. .compatible = "direct-mapped"
  235. },
  236. { },
  237. };
  238. MODULE_DEVICE_TABLE(of, of_flash_match);
  239. static struct of_platform_driver of_flash_driver = {
  240. .name = "of-flash",
  241. .match_table = of_flash_match,
  242. .probe = of_flash_probe,
  243. .remove = of_flash_remove,
  244. };
  245. static int __init of_flash_init(void)
  246. {
  247. return of_register_platform_driver(&of_flash_driver);
  248. }
  249. static void __exit of_flash_exit(void)
  250. {
  251. of_unregister_platform_driver(&of_flash_driver);
  252. }
  253. module_init(of_flash_init);
  254. module_exit(of_flash_exit);
  255. MODULE_LICENSE("GPL");
  256. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  257. MODULE_DESCRIPTION("Device tree based MTD map driver");