ofpart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Flash partitions described by the OF (or flattened) device tree
  3. *
  4. * Copyright © 2006 MontaVista Software Inc.
  5. * Author: Vitaly Wool <vwool@ru.mvista.com>
  6. *
  7. * Revised to handle newer style flash binding by:
  8. * Copyright © 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/init.h>
  17. #include <linux/of.h>
  18. #include <linux/mtd/mtd.h>
  19. #include <linux/slab.h>
  20. #include <linux/mtd/partitions.h>
  21. static bool node_has_compatible(struct device_node *pp)
  22. {
  23. return of_get_property(pp, "compatible", NULL);
  24. }
  25. static int parse_fixed_partitions(struct mtd_info *master,
  26. const struct mtd_partition **pparts,
  27. struct mtd_part_parser_data *data)
  28. {
  29. struct mtd_partition *parts;
  30. struct device_node *mtd_node;
  31. struct device_node *ofpart_node;
  32. const char *partname;
  33. struct device_node *pp;
  34. int nr_parts, i, ret = 0;
  35. bool dedicated = true;
  36. /* Pull of_node from the master device node */
  37. mtd_node = mtd_get_of_node(master);
  38. if (!mtd_node)
  39. return 0;
  40. ofpart_node = of_get_child_by_name(mtd_node, "partitions");
  41. if (!ofpart_node) {
  42. /*
  43. * We might get here even when ofpart isn't used at all (e.g.,
  44. * when using another parser), so don't be louder than
  45. * KERN_DEBUG
  46. */
  47. pr_debug("%s: 'partitions' subnode not found on %pOF. Trying to parse direct subnodes as partitions.\n",
  48. master->name, mtd_node);
  49. ofpart_node = mtd_node;
  50. dedicated = false;
  51. } else if (!of_device_is_compatible(ofpart_node, "fixed-partitions")) {
  52. /* The 'partitions' subnode might be used by another parser */
  53. return 0;
  54. }
  55. /* First count the subnodes */
  56. nr_parts = 0;
  57. for_each_child_of_node(ofpart_node, pp) {
  58. if (!dedicated && node_has_compatible(pp))
  59. continue;
  60. nr_parts++;
  61. }
  62. if (nr_parts == 0)
  63. return 0;
  64. parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
  65. if (!parts)
  66. return -ENOMEM;
  67. i = 0;
  68. for_each_child_of_node(ofpart_node, pp) {
  69. const __be32 *reg;
  70. int len;
  71. int a_cells, s_cells;
  72. if (!dedicated && node_has_compatible(pp))
  73. continue;
  74. reg = of_get_property(pp, "reg", &len);
  75. if (!reg) {
  76. if (dedicated) {
  77. pr_debug("%s: ofpart partition %pOF (%pOF) missing reg property.\n",
  78. master->name, pp,
  79. mtd_node);
  80. goto ofpart_fail;
  81. } else {
  82. nr_parts--;
  83. continue;
  84. }
  85. }
  86. a_cells = of_n_addr_cells(pp);
  87. s_cells = of_n_size_cells(pp);
  88. if (len / 4 != a_cells + s_cells) {
  89. pr_debug("%s: ofpart partition %pOF (%pOF) error parsing reg property.\n",
  90. master->name, pp,
  91. mtd_node);
  92. goto ofpart_fail;
  93. }
  94. parts[i].offset = of_read_number(reg, a_cells);
  95. parts[i].size = of_read_number(reg + a_cells, s_cells);
  96. parts[i].of_node = pp;
  97. partname = of_get_property(pp, "label", &len);
  98. if (!partname)
  99. partname = of_get_property(pp, "name", &len);
  100. parts[i].name = partname;
  101. if (of_get_property(pp, "read-only", &len))
  102. parts[i].mask_flags |= MTD_WRITEABLE;
  103. if (of_get_property(pp, "lock", &len))
  104. parts[i].mask_flags |= MTD_POWERUP_LOCK;
  105. i++;
  106. }
  107. if (!nr_parts)
  108. goto ofpart_none;
  109. *pparts = parts;
  110. return nr_parts;
  111. ofpart_fail:
  112. pr_err("%s: error parsing ofpart partition %pOF (%pOF)\n",
  113. master->name, pp, mtd_node);
  114. ret = -EINVAL;
  115. ofpart_none:
  116. of_node_put(pp);
  117. kfree(parts);
  118. return ret;
  119. }
  120. static const struct of_device_id parse_ofpart_match_table[] = {
  121. { .compatible = "fixed-partitions" },
  122. {},
  123. };
  124. MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
  125. static struct mtd_part_parser ofpart_parser = {
  126. .parse_fn = parse_fixed_partitions,
  127. .name = "fixed-partitions",
  128. .of_match_table = parse_ofpart_match_table,
  129. };
  130. static int parse_ofoldpart_partitions(struct mtd_info *master,
  131. const struct mtd_partition **pparts,
  132. struct mtd_part_parser_data *data)
  133. {
  134. struct mtd_partition *parts;
  135. struct device_node *dp;
  136. int i, plen, nr_parts;
  137. const struct {
  138. __be32 offset, len;
  139. } *part;
  140. const char *names;
  141. /* Pull of_node from the master device node */
  142. dp = mtd_get_of_node(master);
  143. if (!dp)
  144. return 0;
  145. part = of_get_property(dp, "partitions", &plen);
  146. if (!part)
  147. return 0; /* No partitions found */
  148. pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
  149. nr_parts = plen / sizeof(part[0]);
  150. parts = kcalloc(nr_parts, sizeof(*parts), GFP_KERNEL);
  151. if (!parts)
  152. return -ENOMEM;
  153. names = of_get_property(dp, "partition-names", &plen);
  154. for (i = 0; i < nr_parts; i++) {
  155. parts[i].offset = be32_to_cpu(part->offset);
  156. parts[i].size = be32_to_cpu(part->len) & ~1;
  157. /* bit 0 set signifies read only partition */
  158. if (be32_to_cpu(part->len) & 1)
  159. parts[i].mask_flags = MTD_WRITEABLE;
  160. if (names && (plen > 0)) {
  161. int len = strlen(names) + 1;
  162. parts[i].name = names;
  163. plen -= len;
  164. names += len;
  165. } else {
  166. parts[i].name = "unnamed";
  167. }
  168. part++;
  169. }
  170. *pparts = parts;
  171. return nr_parts;
  172. }
  173. static struct mtd_part_parser ofoldpart_parser = {
  174. .parse_fn = parse_ofoldpart_partitions,
  175. .name = "ofoldpart",
  176. };
  177. static int __init ofpart_parser_init(void)
  178. {
  179. register_mtd_parser(&ofpart_parser);
  180. register_mtd_parser(&ofoldpart_parser);
  181. return 0;
  182. }
  183. static void __exit ofpart_parser_exit(void)
  184. {
  185. deregister_mtd_parser(&ofpart_parser);
  186. deregister_mtd_parser(&ofoldpart_parser);
  187. }
  188. module_init(ofpart_parser_init);
  189. module_exit(ofpart_parser_exit);
  190. MODULE_LICENSE("GPL");
  191. MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
  192. MODULE_AUTHOR("Vitaly Wool, David Gibson");
  193. /*
  194. * When MTD core cannot find the requested parser, it tries to load the module
  195. * with the same name. Since we provide the ofoldpart parser, we should have
  196. * the corresponding alias.
  197. */
  198. MODULE_ALIAS("fixed-partitions");
  199. MODULE_ALIAS("ofoldpart");