physmap_of.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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/device.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/mtd/map.h>
  20. #include <linux/mtd/partitions.h>
  21. #include <linux/mtd/concat.h>
  22. #include <linux/of.h>
  23. #include <linux/of_address.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. int list_size; /* number of elements in of_flash_list */
  34. struct of_flash_list list[0];
  35. };
  36. static int of_flash_remove(struct platform_device *dev)
  37. {
  38. struct of_flash *info;
  39. int i;
  40. info = dev_get_drvdata(&dev->dev);
  41. if (!info)
  42. return 0;
  43. dev_set_drvdata(&dev->dev, NULL);
  44. if (info->cmtd) {
  45. mtd_device_unregister(info->cmtd);
  46. if (info->cmtd != info->list[0].mtd)
  47. mtd_concat_destroy(info->cmtd);
  48. }
  49. for (i = 0; i < info->list_size; i++) {
  50. if (info->list[i].mtd)
  51. map_destroy(info->list[i].mtd);
  52. if (info->list[i].map.virt)
  53. iounmap(info->list[i].map.virt);
  54. if (info->list[i].res) {
  55. release_resource(info->list[i].res);
  56. kfree(info->list[i].res);
  57. }
  58. }
  59. return 0;
  60. }
  61. static const char * const rom_probe_types[] = {
  62. "cfi_probe", "jedec_probe", "map_rom" };
  63. /* Helper function to handle probing of the obsolete "direct-mapped"
  64. * compatible binding, which has an extra "probe-type" property
  65. * describing the type of flash probe necessary. */
  66. static struct mtd_info *obsolete_probe(struct platform_device *dev,
  67. struct map_info *map)
  68. {
  69. struct device_node *dp = dev->dev.of_node;
  70. const char *of_probe;
  71. struct mtd_info *mtd;
  72. int i;
  73. dev_warn(&dev->dev, "Device tree uses obsolete \"direct-mapped\" "
  74. "flash binding\n");
  75. of_probe = of_get_property(dp, "probe-type", NULL);
  76. if (!of_probe) {
  77. for (i = 0; i < ARRAY_SIZE(rom_probe_types); i++) {
  78. mtd = do_map_probe(rom_probe_types[i], map);
  79. if (mtd)
  80. return mtd;
  81. }
  82. return NULL;
  83. } else if (strcmp(of_probe, "CFI") == 0) {
  84. return do_map_probe("cfi_probe", map);
  85. } else if (strcmp(of_probe, "JEDEC") == 0) {
  86. return do_map_probe("jedec_probe", map);
  87. } else {
  88. if (strcmp(of_probe, "ROM") != 0)
  89. dev_warn(&dev->dev, "obsolete_probe: don't know probe "
  90. "type '%s', mapping as rom\n", of_probe);
  91. return do_map_probe("map_rom", map);
  92. }
  93. }
  94. /* When partitions are set we look for a linux,part-probe property which
  95. specifies the list of partition probers to use. If none is given then the
  96. default is use. These take precedence over other device tree
  97. information. */
  98. static const char * const part_probe_types_def[] = {
  99. "cmdlinepart", "RedBoot", "ofpart", "ofoldpart", NULL };
  100. static const char * const *of_get_probes(struct device_node *dp)
  101. {
  102. const char *cp;
  103. int cplen;
  104. unsigned int l;
  105. unsigned int count;
  106. const char **res;
  107. cp = of_get_property(dp, "linux,part-probe", &cplen);
  108. if (cp == NULL)
  109. return part_probe_types_def;
  110. count = 0;
  111. for (l = 0; l != cplen; l++)
  112. if (cp[l] == 0)
  113. count++;
  114. res = kzalloc((count + 1)*sizeof(*res), GFP_KERNEL);
  115. if (!res)
  116. return NULL;
  117. count = 0;
  118. while (cplen > 0) {
  119. res[count] = cp;
  120. l = strlen(cp) + 1;
  121. cp += l;
  122. cplen -= l;
  123. count++;
  124. }
  125. return res;
  126. }
  127. static void of_free_probes(const char * const *probes)
  128. {
  129. if (probes != part_probe_types_def)
  130. kfree(probes);
  131. }
  132. static const struct of_device_id of_flash_match[];
  133. static int of_flash_probe(struct platform_device *dev)
  134. {
  135. const char * const *part_probe_types;
  136. const struct of_device_id *match;
  137. struct device_node *dp = dev->dev.of_node;
  138. struct resource res;
  139. struct of_flash *info;
  140. const char *probe_type;
  141. const __be32 *width;
  142. int err;
  143. int i;
  144. int count;
  145. const __be32 *p;
  146. int reg_tuple_size;
  147. struct mtd_info **mtd_list = NULL;
  148. resource_size_t res_size;
  149. struct mtd_part_parser_data ppdata;
  150. bool map_indirect;
  151. const char *mtd_name = NULL;
  152. match = of_match_device(of_flash_match, &dev->dev);
  153. if (!match)
  154. return -EINVAL;
  155. probe_type = match->data;
  156. reg_tuple_size = (of_n_addr_cells(dp) + of_n_size_cells(dp)) * sizeof(u32);
  157. of_property_read_string(dp, "linux,mtd-name", &mtd_name);
  158. /*
  159. * Get number of "reg" tuples. Scan for MTD devices on area's
  160. * described by each "reg" region. This makes it possible (including
  161. * the concat support) to support the Intel P30 48F4400 chips which
  162. * consists internally of 2 non-identical NOR chips on one die.
  163. */
  164. p = of_get_property(dp, "reg", &count);
  165. if (count % reg_tuple_size != 0) {
  166. dev_err(&dev->dev, "Malformed reg property on %s\n",
  167. dev->dev.of_node->full_name);
  168. err = -EINVAL;
  169. goto err_flash_remove;
  170. }
  171. count /= reg_tuple_size;
  172. map_indirect = of_property_read_bool(dp, "no-unaligned-direct-access");
  173. err = -ENOMEM;
  174. info = devm_kzalloc(&dev->dev,
  175. sizeof(struct of_flash) +
  176. sizeof(struct of_flash_list) * count, GFP_KERNEL);
  177. if (!info)
  178. goto err_flash_remove;
  179. dev_set_drvdata(&dev->dev, info);
  180. mtd_list = kzalloc(sizeof(*mtd_list) * count, GFP_KERNEL);
  181. if (!mtd_list)
  182. goto err_flash_remove;
  183. for (i = 0; i < count; i++) {
  184. err = -ENXIO;
  185. if (of_address_to_resource(dp, i, &res)) {
  186. /*
  187. * Continue with next register tuple if this
  188. * one is not mappable
  189. */
  190. continue;
  191. }
  192. dev_dbg(&dev->dev, "of_flash device: %pR\n", &res);
  193. err = -EBUSY;
  194. res_size = resource_size(&res);
  195. info->list[i].res = request_mem_region(res.start, res_size,
  196. dev_name(&dev->dev));
  197. if (!info->list[i].res)
  198. goto err_out;
  199. err = -ENXIO;
  200. width = of_get_property(dp, "bank-width", NULL);
  201. if (!width) {
  202. dev_err(&dev->dev, "Can't get bank width from device"
  203. " tree\n");
  204. goto err_out;
  205. }
  206. info->list[i].map.name = mtd_name ?: dev_name(&dev->dev);
  207. info->list[i].map.phys = res.start;
  208. info->list[i].map.size = res_size;
  209. info->list[i].map.bankwidth = be32_to_cpup(width);
  210. info->list[i].map.device_node = dp;
  211. err = -ENOMEM;
  212. info->list[i].map.virt = ioremap(info->list[i].map.phys,
  213. info->list[i].map.size);
  214. if (!info->list[i].map.virt) {
  215. dev_err(&dev->dev, "Failed to ioremap() flash"
  216. " region\n");
  217. goto err_out;
  218. }
  219. simple_map_init(&info->list[i].map);
  220. /*
  221. * On some platforms (e.g. MPC5200) a direct 1:1 mapping
  222. * may cause problems with JFFS2 usage, as the local bus (LPB)
  223. * doesn't support unaligned accesses as implemented in the
  224. * JFFS2 code via memcpy(). By setting NO_XIP, the
  225. * flash will not be exposed directly to the MTD users
  226. * (e.g. JFFS2) any more.
  227. */
  228. if (map_indirect)
  229. info->list[i].map.phys = NO_XIP;
  230. if (probe_type) {
  231. info->list[i].mtd = do_map_probe(probe_type,
  232. &info->list[i].map);
  233. } else {
  234. info->list[i].mtd = obsolete_probe(dev,
  235. &info->list[i].map);
  236. }
  237. /* Fall back to mapping region as ROM */
  238. if (!info->list[i].mtd) {
  239. dev_warn(&dev->dev,
  240. "do_map_probe() failed for type %s\n",
  241. probe_type);
  242. info->list[i].mtd = do_map_probe("map_rom",
  243. &info->list[i].map);
  244. }
  245. mtd_list[i] = info->list[i].mtd;
  246. err = -ENXIO;
  247. if (!info->list[i].mtd) {
  248. dev_err(&dev->dev, "do_map_probe() failed\n");
  249. goto err_out;
  250. } else {
  251. info->list_size++;
  252. }
  253. info->list[i].mtd->dev.parent = &dev->dev;
  254. }
  255. err = 0;
  256. info->cmtd = NULL;
  257. if (info->list_size == 1) {
  258. info->cmtd = info->list[0].mtd;
  259. } else if (info->list_size > 1) {
  260. /*
  261. * We detected multiple devices. Concatenate them together.
  262. */
  263. info->cmtd = mtd_concat_create(mtd_list, info->list_size,
  264. dev_name(&dev->dev));
  265. }
  266. if (info->cmtd == NULL)
  267. err = -ENXIO;
  268. if (err)
  269. goto err_out;
  270. ppdata.of_node = dp;
  271. part_probe_types = of_get_probes(dp);
  272. if (!part_probe_types) {
  273. err = -ENOMEM;
  274. goto err_out;
  275. }
  276. mtd_device_parse_register(info->cmtd, part_probe_types, &ppdata,
  277. NULL, 0);
  278. of_free_probes(part_probe_types);
  279. kfree(mtd_list);
  280. return 0;
  281. err_out:
  282. kfree(mtd_list);
  283. err_flash_remove:
  284. of_flash_remove(dev);
  285. return err;
  286. }
  287. static const struct of_device_id of_flash_match[] = {
  288. {
  289. .compatible = "cfi-flash",
  290. .data = (void *)"cfi_probe",
  291. },
  292. {
  293. /* FIXME: JEDEC chips can't be safely and reliably
  294. * probed, although the mtd code gets it right in
  295. * practice most of the time. We should use the
  296. * vendor and device ids specified by the binding to
  297. * bypass the heuristic probe code, but the mtd layer
  298. * provides, at present, no interface for doing so
  299. * :(. */
  300. .compatible = "jedec-flash",
  301. .data = (void *)"jedec_probe",
  302. },
  303. {
  304. .compatible = "mtd-ram",
  305. .data = (void *)"map_ram",
  306. },
  307. {
  308. .compatible = "mtd-rom",
  309. .data = (void *)"map_rom",
  310. },
  311. {
  312. .type = "rom",
  313. .compatible = "direct-mapped"
  314. },
  315. { },
  316. };
  317. MODULE_DEVICE_TABLE(of, of_flash_match);
  318. static struct platform_driver of_flash_driver = {
  319. .driver = {
  320. .name = "of-flash",
  321. .of_match_table = of_flash_match,
  322. },
  323. .probe = of_flash_probe,
  324. .remove = of_flash_remove,
  325. };
  326. module_platform_driver(of_flash_driver);
  327. MODULE_LICENSE("GPL");
  328. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  329. MODULE_DESCRIPTION("Device tree based MTD map driver");