physmap_of.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. resource_size_t res_size;
  166. reg_tuple_size = (of_n_addr_cells(dp) + of_n_size_cells(dp)) * sizeof(u32);
  167. /*
  168. * Get number of "reg" tuples. Scan for MTD devices on area's
  169. * described by each "reg" region. This makes it possible (including
  170. * the concat support) to support the Intel P30 48F4400 chips which
  171. * consists internally of 2 non-identical NOR chips on one die.
  172. */
  173. p = of_get_property(dp, "reg", &count);
  174. if (count % reg_tuple_size != 0) {
  175. dev_err(&dev->dev, "Malformed reg property on %s\n",
  176. dev->node->full_name);
  177. err = -EINVAL;
  178. goto err_flash_remove;
  179. }
  180. count /= reg_tuple_size;
  181. err = -ENOMEM;
  182. info = kzalloc(sizeof(struct of_flash) +
  183. sizeof(struct of_flash_list) * count, GFP_KERNEL);
  184. if (!info)
  185. goto err_flash_remove;
  186. dev_set_drvdata(&dev->dev, info);
  187. mtd_list = kzalloc(sizeof(struct mtd_info) * count, GFP_KERNEL);
  188. if (!mtd_list)
  189. goto err_flash_remove;
  190. for (i = 0; i < count; i++) {
  191. err = -ENXIO;
  192. if (of_address_to_resource(dp, i, &res)) {
  193. dev_err(&dev->dev, "Can't get IO address from device"
  194. " tree\n");
  195. goto err_out;
  196. }
  197. dev_dbg(&dev->dev, "of_flash device: %.8llx-%.8llx\n",
  198. (unsigned long long)res.start,
  199. (unsigned long long)res.end);
  200. err = -EBUSY;
  201. res_size = resource_size(&res);
  202. info->list[i].res = request_mem_region(res.start, res_size,
  203. dev_name(&dev->dev));
  204. if (!info->list[i].res)
  205. goto err_out;
  206. err = -ENXIO;
  207. width = of_get_property(dp, "bank-width", NULL);
  208. if (!width) {
  209. dev_err(&dev->dev, "Can't get bank width from device"
  210. " tree\n");
  211. goto err_out;
  212. }
  213. info->list[i].map.name = dev_name(&dev->dev);
  214. info->list[i].map.phys = res.start;
  215. info->list[i].map.size = res_size;
  216. info->list[i].map.bankwidth = *width;
  217. err = -ENOMEM;
  218. info->list[i].map.virt = ioremap(info->list[i].map.phys,
  219. info->list[i].map.size);
  220. if (!info->list[i].map.virt) {
  221. dev_err(&dev->dev, "Failed to ioremap() flash"
  222. " region\n");
  223. goto err_out;
  224. }
  225. simple_map_init(&info->list[i].map);
  226. if (probe_type) {
  227. info->list[i].mtd = do_map_probe(probe_type,
  228. &info->list[i].map);
  229. } else {
  230. info->list[i].mtd = obsolete_probe(dev,
  231. &info->list[i].map);
  232. }
  233. mtd_list[i] = info->list[i].mtd;
  234. err = -ENXIO;
  235. if (!info->list[i].mtd) {
  236. dev_err(&dev->dev, "do_map_probe() failed\n");
  237. goto err_out;
  238. } else {
  239. info->list_size++;
  240. }
  241. info->list[i].mtd->owner = THIS_MODULE;
  242. info->list[i].mtd->dev.parent = &dev->dev;
  243. }
  244. err = 0;
  245. if (info->list_size == 1) {
  246. info->cmtd = info->list[0].mtd;
  247. } else if (info->list_size > 1) {
  248. /*
  249. * We detected multiple devices. Concatenate them together.
  250. */
  251. #ifdef CONFIG_MTD_CONCAT
  252. info->cmtd = mtd_concat_create(mtd_list, info->list_size,
  253. dev_name(&dev->dev));
  254. if (info->cmtd == NULL)
  255. err = -ENXIO;
  256. #else
  257. printk(KERN_ERR "physmap_of: multiple devices "
  258. "found but MTD concat support disabled.\n");
  259. err = -ENXIO;
  260. #endif
  261. }
  262. if (err)
  263. goto err_out;
  264. #ifdef CONFIG_MTD_PARTITIONS
  265. /* First look for RedBoot table or partitions on the command
  266. * line, these take precedence over device tree information */
  267. err = parse_mtd_partitions(info->cmtd, part_probe_types,
  268. &info->parts, 0);
  269. if (err < 0)
  270. return err;
  271. #ifdef CONFIG_MTD_OF_PARTS
  272. if (err == 0) {
  273. err = of_mtd_parse_partitions(&dev->dev, dp, &info->parts);
  274. if (err < 0)
  275. return err;
  276. }
  277. #endif
  278. if (err == 0) {
  279. err = parse_obsolete_partitions(dev, info, dp);
  280. if (err < 0)
  281. return err;
  282. }
  283. if (err > 0)
  284. add_mtd_partitions(info->cmtd, info->parts, err);
  285. else
  286. #endif
  287. add_mtd_device(info->cmtd);
  288. kfree(mtd_list);
  289. return 0;
  290. err_out:
  291. kfree(mtd_list);
  292. err_flash_remove:
  293. of_flash_remove(dev);
  294. return err;
  295. }
  296. static struct of_device_id of_flash_match[] = {
  297. {
  298. .compatible = "cfi-flash",
  299. .data = (void *)"cfi_probe",
  300. },
  301. {
  302. /* FIXME: JEDEC chips can't be safely and reliably
  303. * probed, although the mtd code gets it right in
  304. * practice most of the time. We should use the
  305. * vendor and device ids specified by the binding to
  306. * bypass the heuristic probe code, but the mtd layer
  307. * provides, at present, no interface for doing so
  308. * :(. */
  309. .compatible = "jedec-flash",
  310. .data = (void *)"jedec_probe",
  311. },
  312. {
  313. .compatible = "mtd-ram",
  314. .data = (void *)"map_ram",
  315. },
  316. {
  317. .type = "rom",
  318. .compatible = "direct-mapped"
  319. },
  320. { },
  321. };
  322. MODULE_DEVICE_TABLE(of, of_flash_match);
  323. static struct of_platform_driver of_flash_driver = {
  324. .name = "of-flash",
  325. .match_table = of_flash_match,
  326. .probe = of_flash_probe,
  327. .remove = of_flash_remove,
  328. };
  329. static int __init of_flash_init(void)
  330. {
  331. return of_register_platform_driver(&of_flash_driver);
  332. }
  333. static void __exit of_flash_exit(void)
  334. {
  335. of_unregister_platform_driver(&of_flash_driver);
  336. }
  337. module_init(of_flash_init);
  338. module_exit(of_flash_exit);
  339. MODULE_LICENSE("GPL");
  340. MODULE_AUTHOR("Vitaly Wool <vwool@ru.mvista.com>");
  341. MODULE_DESCRIPTION("Device tree based MTD map driver");