physmap_of.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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/mtd/concat.h>
  23. #include <linux/of.h>
  24. #include <linux/of_platform.h>
  25. #include <linux/slab.h>
  26. struct of_flash_list {
  27. struct mtd_info *mtd;
  28. struct map_info map;
  29. struct resource *res;
  30. };
  31. struct of_flash {
  32. struct mtd_info *cmtd;
  33. #ifdef CONFIG_MTD_PARTITIONS
  34. struct mtd_partition *parts;
  35. #endif
  36. int list_size; /* number of elements in of_flash_list */
  37. struct of_flash_list list[0];
  38. };
  39. #ifdef CONFIG_MTD_PARTITIONS
  40. #define OF_FLASH_PARTS(info) ((info)->parts)
  41. static int parse_obsolete_partitions(struct of_device *dev,
  42. struct of_flash *info,
  43. struct device_node *dp)
  44. {
  45. int i, plen, nr_parts;
  46. const struct {
  47. u32 offset, len;
  48. } *part;
  49. const char *names;
  50. part = of_get_property(dp, "partitions", &plen);
  51. if (!part)
  52. return 0; /* No partitions found */
  53. dev_warn(&dev->dev, "Device tree uses obsolete partition map binding\n");
  54. nr_parts = plen / sizeof(part[0]);
  55. info->parts = kzalloc(nr_parts * sizeof(*info->parts), GFP_KERNEL);
  56. if (!info->parts)
  57. return -ENOMEM;
  58. names = of_get_property(dp, "partition-names", &plen);
  59. for (i = 0; i < nr_parts; i++) {
  60. info->parts[i].offset = part->offset;
  61. info->parts[i].size = part->len & ~1;
  62. if (part->len & 1) /* bit 0 set signifies read only partition */
  63. info->parts[i].mask_flags = MTD_WRITEABLE;
  64. if (names && (plen > 0)) {
  65. int len = strlen(names) + 1;
  66. info->parts[i].name = (char *)names;
  67. plen -= len;
  68. names += len;
  69. } else {
  70. info->parts[i].name = "unnamed";
  71. }
  72. part++;
  73. }
  74. return nr_parts;
  75. }
  76. #else /* MTD_PARTITIONS */
  77. #define OF_FLASH_PARTS(info) (0)
  78. #define parse_partitions(info, dev) (0)
  79. #endif /* MTD_PARTITIONS */
  80. static int of_flash_remove(struct of_device *dev)
  81. {
  82. struct of_flash *info;
  83. int i;
  84. info = dev_get_drvdata(&dev->dev);
  85. if (!info)
  86. return 0;
  87. dev_set_drvdata(&dev->dev, NULL);
  88. #ifdef CONFIG_MTD_CONCAT
  89. if (info->cmtd != info->list[0].mtd) {
  90. del_mtd_device(info->cmtd);
  91. mtd_concat_destroy(info->cmtd);
  92. }
  93. #endif
  94. if (info->cmtd) {
  95. if (OF_FLASH_PARTS(info)) {
  96. del_mtd_partitions(info->cmtd);
  97. kfree(OF_FLASH_PARTS(info));
  98. } else {
  99. del_mtd_device(info->cmtd);
  100. }
  101. }
  102. for (i = 0; i < info->list_size; i++) {
  103. if (info->list[i].mtd)
  104. map_destroy(info->list[i].mtd);
  105. if (info->list[i].map.virt)
  106. iounmap(info->list[i].map.virt);
  107. if (info->list[i].res) {
  108. release_resource(info->list[i].res);
  109. kfree(info->list[i].res);
  110. }
  111. }
  112. kfree(info);
  113. return 0;
  114. }
  115. /* Helper function to handle probing of the obsolete "direct-mapped"
  116. * compatible binding, which has an extra "probe-type" property
  117. * describing the type of flash probe necessary. */
  118. static struct mtd_info * __devinit obsolete_probe(struct of_device *dev,
  119. struct map_info *map)
  120. {
  121. struct device_node *dp = dev->node;
  122. const char *of_probe;
  123. struct mtd_info *mtd;
  124. static const char *rom_probe_types[]
  125. = { "cfi_probe", "jedec_probe", "map_rom"};
  126. int i;
  127. dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
  128. "flash binding\n");
  129. of_probe = of_get_property(dp, "probe-type", NULL);
  130. if (!of_probe) {
  131. for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
  132. mtd = do_map_probe(rom_probe_types[i], map);
  133. if (mtd)
  134. return mtd;
  135. }
  136. return NULL;
  137. } else if (strcmp(of_probe, "CFI") == 0) {
  138. return do_map_probe("cfi_probe", map);
  139. } else if (strcmp(of_probe, "JEDEC") == 0) {
  140. return do_map_probe("jedec_probe", map);
  141. } else {
  142. if (strcmp(of_probe, "ROM") != 0)
  143. dev_warn(&dev->dev, "obsolete_probe: don't know probe "
  144. "type '%s', mapping as rom\n", of_probe);
  145. return do_map_probe("mtd_rom", map);
  146. }
  147. }
  148. static int __devinit of_flash_probe(struct of_device *dev,
  149. const struct of_device_id *match)
  150. {
  151. #ifdef CONFIG_MTD_PARTITIONS
  152. static const char *part_probe_types[]
  153. = { "cmdlinepart", "RedBoot", NULL };
  154. #endif
  155. struct device_node *dp = dev->node;
  156. struct resource res;
  157. struct of_flash *info;
  158. const char *probe_type = match->data;
  159. const u32 *width;
  160. int err;
  161. int i;
  162. int count;
  163. const u32 *p;
  164. int reg_tuple_size;
  165. struct mtd_info **mtd_list = NULL;
  166. resource_size_t res_size;
  167. reg_tuple_size = (of_n_addr_cells(dp) + of_n_size_cells(dp)) * sizeof(u32);
  168. /*
  169. * Get number of "reg" tuples. Scan for MTD devices on area's
  170. * described by each "reg" region. This makes it possible (including
  171. * the concat support) to support the Intel P30 48F4400 chips which
  172. * consists internally of 2 non-identical NOR chips on one die.
  173. */
  174. p = of_get_property(dp, "reg", &count);
  175. if (count % reg_tuple_size != 0) {
  176. dev_err(&dev->dev, "Malformed reg property on %s\n",
  177. dev->node->full_name);
  178. err = -EINVAL;
  179. goto err_flash_remove;
  180. }
  181. count /= reg_tuple_size;
  182. err = -ENOMEM;
  183. info = kzalloc(sizeof(struct of_flash) +
  184. sizeof(struct of_flash_list) * count, GFP_KERNEL);
  185. if (!info)
  186. goto err_flash_remove;
  187. dev_set_drvdata(&dev->dev, info);
  188. mtd_list = kzalloc(sizeof(struct mtd_info) * count, GFP_KERNEL);
  189. if (!mtd_list)
  190. goto err_flash_remove;
  191. for (i = 0; i < count; i++) {
  192. err = -ENXIO;
  193. if (of_address_to_resource(dp, i, &res)) {
  194. dev_err(&dev->dev, "Can't get IO address from device"
  195. " tree\n");
  196. goto err_out;
  197. }
  198. dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
  199. (unsigned long long)res.start,
  200. (unsigned long long)res.end);
  201. err = -EBUSY;
  202. res_size = resource_size(&res);
  203. info->list[i].res = request_mem_region(res.start, res_size,
  204. dev_name(&dev->dev));
  205. if (!info->list[i].res)
  206. goto err_out;
  207. err = -ENXIO;
  208. width = of_get_property(dp, "bank-width", NULL);
  209. if (!width) {
  210. dev_err(&dev->dev, "Can't get bank width from device"
  211. " tree\n");
  212. goto err_out;
  213. }
  214. info->list[i].map.name = dev_name(&dev->dev);
  215. info->list[i].map.phys = res.start;
  216. info->list[i].map.size = res_size;
  217. info->list[i].map.bankwidth = *width;
  218. err = -ENOMEM;
  219. info->list[i].map.virt = ioremap(info->list[i].map.phys,
  220. info->list[i].map.size);
  221. if (!info->list[i].map.virt) {
  222. dev_err(&dev->dev, "Failed to ioremap() flash"
  223. " region\n");
  224. goto err_out;
  225. }
  226. simple_map_init(&info->list[i].map);
  227. if (probe_type) {
  228. info->list[i].mtd = do_map_probe(probe_type,
  229. &info->list[i].map);
  230. } else {
  231. info->list[i].mtd = obsolete_probe(dev,
  232. &info->list[i].map);
  233. }
  234. mtd_list[i] = info->list[i].mtd;
  235. err = -ENXIO;
  236. if (!info->list[i].mtd) {
  237. dev_err(&dev->dev, "do_map_probe() failed\n");
  238. goto err_out;
  239. } else {
  240. info->list_size++;
  241. }
  242. info->list[i].mtd->owner = THIS_MODULE;
  243. info->list[i].mtd->dev.parent = &dev->dev;
  244. }
  245. err = 0;
  246. if (info->list_size == 1) {
  247. info->cmtd = info->list[0].mtd;
  248. } else if (info->list_size > 1) {
  249. /*
  250. * We detected multiple devices. Concatenate them together.
  251. */
  252. #ifdef CONFIG_MTD_CONCAT
  253. info->cmtd = mtd_concat_create(mtd_list, info->list_size,
  254. dev_name(&dev->dev));
  255. if (info->cmtd == NULL)
  256. err = -ENXIO;
  257. #else
  258. printk(KERN_ERR "physmap_of: multiple devices "
  259. "found but MTD concat support disabled.\n");
  260. err = -ENXIO;
  261. #endif
  262. }
  263. if (err)
  264. goto err_out;
  265. #ifdef CONFIG_MTD_PARTITIONS
  266. /* First look for RedBoot table or partitions on the command
  267. * line, these take precedence over device tree information */
  268. err = parse_mtd_partitions(info->cmtd, part_probe_types,
  269. &info->parts, 0);
  270. if (err < 0)
  271. return err;
  272. #ifdef CONFIG_MTD_OF_PARTS
  273. if (err == 0) {
  274. err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
  275. if (err < 0)
  276. return err;
  277. }
  278. #endif
  279. if (err == 0) {
  280. err = parse_obsolete_partitions(dev, info, dp);
  281. if (err < 0)
  282. return err;
  283. }
  284. if (err > 0)
  285. add_mtd_partitions(info->cmtd, info->parts, err);
  286. else
  287. #endif
  288. add_mtd_device(info->cmtd);
  289. kfree(mtd_list);
  290. return 0;
  291. err_out:
  292. kfree(mtd_list);
  293. err_flash_remove:
  294. of_flash_remove(dev);
  295. return err;
  296. }
  297. static struct of_device_id of_flash_match[] = {
  298. {
  299. .compatible = "cfi-flash",
  300. .data = (void *)"cfi_probe",
  301. },
  302. {
  303. /* FIXME: JEDEC chips can't be safely and reliably
  304. * probed, although the mtd code gets it right in
  305. * practice most of the time. We should use the
  306. * vendor and device ids specified by the binding to
  307. * bypass the heuristic probe code, but the mtd layer
  308. * provides, at present, no interface for doing so
  309. * :(. */
  310. .compatible = "jedec-flash",
  311. .data = (void *)"jedec_probe",
  312. },
  313. {
  314. .compatible = "mtd-ram",
  315. .data = (void *)"map_ram",
  316. },
  317. {
  318. .type = "rom",
  319. .compatible = "direct-mapped"
  320. },
  321. { },
  322. };
  323. MODULE_DEVICE_TABLE(of, of_flash_match);
  324. static struct of_platform_driver of_flash_driver = {
  325. .name = "of-flash",
  326. .match_table = of_flash_match,
  327. .probe = of_flash_probe,
  328. .remove = of_flash_remove,
  329. };
  330. static int __init of_flash_init(void)
  331. {
  332. return of_register_platform_driver(&of_flash_driver);
  333. }
  334. static void __exit of_flash_exit(void)
  335. {
  336. of_unregister_platform_driver(&of_flash_driver);
  337. }
  338. module_init(of_flash_init);
  339. module_exit(of_flash_exit);
  340. MODULE_LICENSE("GPL");
  341. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  342. MODULE_DESCRIPTION("Device tree based MTD map driver");