physmap_of.c 8.8 KB

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