physmap_of_core.c 9.3 KB

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