physmap_of.c 11 KB

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