physmap_of.c 9.4 KB

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