physmap_of.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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. }
  125. if (nr_parts < 0)
  126. return nr_parts;
  127. if (nr_parts > 0)
  128. add_mtd_partitions(info->mtd, info->parts, nr_parts);
  129. else
  130. add_mtd_device(info->mtd);
  131. return 0;
  132. }
  133. #else /* MTD_PARTITIONS */
  134. static int __devinit process_partitions(struct physmap_flash_info *info,
  135. struct device_node *dev)
  136. {
  137. add_mtd_device(info->mtd);
  138. return 0;
  139. }
  140. #endif /* MTD_PARTITIONS */
  141. static int of_physmap_remove(struct of_device *dev)
  142. {
  143. struct physmap_flash_info *info;
  144. info = dev_get_drvdata(&dev->dev);
  145. if (info == NULL)
  146. return 0;
  147. dev_set_drvdata(&dev->dev, NULL);
  148. if (info->mtd != NULL) {
  149. #ifdef CONFIG_MTD_PARTITIONS
  150. if (info->parts) {
  151. del_mtd_partitions(info->mtd);
  152. kfree(info->parts);
  153. } else {
  154. del_mtd_device(info->mtd);
  155. }
  156. #else
  157. del_mtd_device(info->mtd);
  158. #endif
  159. map_destroy(info->mtd);
  160. }
  161. if (info->map.virt != NULL)
  162. iounmap(info->map.virt);
  163. if (info->res != NULL) {
  164. release_resource(info->res);
  165. kfree(info->res);
  166. }
  167. return 0;
  168. }
  169. /* Helper function to handle probing of the obsolete "direct-mapped"
  170. * compatible binding, which has an extra "probe-type" property
  171. * describing the type of flash probe necessary. */
  172. static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
  173. struct map_info *map)
  174. {
  175. struct device_node *dp = dev->node;
  176. const char *of_probe;
  177. struct mtd_info *mtd;
  178. static const char *rom_probe_types[]
  179. = { "cfi_probe", "jedec_probe", "map_rom"};
  180. int i;
  181. dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
  182. "flash binding\n");
  183. of_probe = of_get_property(dp, "probe-type", NULL);
  184. if (!of_probe) {
  185. for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
  186. mtd = do_map_probe(rom_probe_types[i], map);
  187. if (mtd)
  188. return mtd;
  189. }
  190. return NULL;
  191. } else if (strcmp(of_probe, "CFI") == 0) {
  192. return do_map_probe("cfi_probe", map);
  193. } else if (strcmp(of_probe, "JEDEC") == 0) {
  194. return do_map_probe("jedec_probe", map);
  195. } else {
  196. if (strcmp(of_probe, "ROM") != 0)
  197. dev_dbg(&dev->dev, "obsolete_probe: don't know probe type "
  198. "'%s', mapping as rom\n", of_probe);
  199. return do_map_probe("mtd_rom", map);
  200. }
  201. }
  202. static int __devinit of_physmap_probe(struct of_device *dev, const struct of_device_id *match)
  203. {
  204. struct device_node *dp = dev->node;
  205. struct resource res;
  206. struct physmap_flash_info *info;
  207. const char *probe_type = (const char *)match->data;
  208. const u32 *width;
  209. int err;
  210. if (of_address_to_resource(dp, 0, &res)) {
  211. dev_err(&dev->dev, "Can't get the flash mapping!\n");
  212. err = -EINVAL;
  213. goto err_out;
  214. }
  215. dev_dbg(&dev->dev, "physmap flash device: %.8llx at %.8llx\n",
  216. (unsigned long long)res.end - res.start + 1,
  217. (unsigned long long)res.start);
  218. info = kzalloc(sizeof(struct physmap_flash_info), GFP_KERNEL);
  219. if (info == NULL) {
  220. err = -ENOMEM;
  221. goto err_out;
  222. }
  223. memset(info, 0, sizeof(*info));
  224. dev_set_drvdata(&dev->dev, info);
  225. info->res = request_mem_region(res.start, res.end - res.start + 1,
  226. dev->dev.bus_id);
  227. if (info->res == NULL) {
  228. dev_err(&dev->dev, "Could not reserve memory region\n");
  229. err = -ENOMEM;
  230. goto err_out;
  231. }
  232. width = of_get_property(dp, "bank-width", NULL);
  233. if (width == NULL) {
  234. dev_err(&dev->dev, "Can't get the flash bank width!\n");
  235. err = -EINVAL;
  236. goto err_out;
  237. }
  238. info->map.name = dev->dev.bus_id;
  239. info->map.phys = res.start;
  240. info->map.size = res.end - res.start + 1;
  241. info->map.bankwidth = *width;
  242. info->map.virt = ioremap(info->map.phys, info->map.size);
  243. if (info->map.virt == NULL) {
  244. dev_err(&dev->dev, "Failed to ioremap flash region\n");
  245. err = EIO;
  246. goto err_out;
  247. }
  248. simple_map_init(&info->map);
  249. if (probe_type)
  250. info->mtd = do_map_probe(probe_type, &info->map);
  251. else
  252. info->mtd = obsolete_probe(dev, &info->map);
  253. if (info->mtd == NULL) {
  254. dev_err(&dev->dev, "map_probe failed\n");
  255. err = -ENXIO;
  256. goto err_out;
  257. }
  258. info->mtd->owner = THIS_MODULE;
  259. return process_partitions(info, dev);
  260. err_out:
  261. of_physmap_remove(dev);
  262. return err;
  263. return 0;
  264. }
  265. static struct of_device_id of_physmap_match[] = {
  266. {
  267. .compatible = "cfi-flash",
  268. .data = (void *)"cfi_probe",
  269. },
  270. {
  271. /* FIXME: JEDEC chips can't be safely and reliably
  272. * probed, although the mtd code gets it right in
  273. * practice most of the time. We should use the
  274. * vendor and device ids specified by the binding to
  275. * bypass the heuristic probe code, but the mtd layer
  276. * provides, at present, no interface for doing so
  277. * :(. */
  278. .compatible = "jedec-flash",
  279. .data = (void *)"jedec_probe",
  280. },
  281. {
  282. .type = "rom",
  283. .compatible = "direct-mapped"
  284. },
  285. { },
  286. };
  287. MODULE_DEVICE_TABLE(of, of_physmap_match);
  288. static struct of_platform_driver of_physmap_flash_driver = {
  289. .name = "physmap-flash",
  290. .match_table = of_physmap_match,
  291. .probe = of_physmap_probe,
  292. .remove = of_physmap_remove,
  293. };
  294. static int __init of_physmap_init(void)
  295. {
  296. return of_register_platform_driver(&of_physmap_flash_driver);
  297. }
  298. static void __exit of_physmap_exit(void)
  299. {
  300. of_unregister_platform_driver(&of_physmap_flash_driver);
  301. }
  302. module_init(of_physmap_init);
  303. module_exit(of_physmap_exit);
  304. MODULE_LICENSE("GPL");
  305. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  306. MODULE_DESCRIPTION("Configurable MTD map driver for OF");