physmap_of.c 8.4 KB

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