ofpart.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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_ofpart_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 = kzalloc(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 struct mtd_part_parser ofpart_parser = {
  121. .parse_fn = parse_ofpart_partitions,
  122. .name = "ofpart",
  123. };
  124. static int parse_ofoldpart_partitions(struct mtd_info *master,
  125. const struct mtd_partition **pparts,
  126. struct mtd_part_parser_data *data)
  127. {
  128. struct mtd_partition *parts;
  129. struct device_node *dp;
  130. int i, plen, nr_parts;
  131. const struct {
  132. __be32 offset, len;
  133. } *part;
  134. const char *names;
  135. /* Pull of_node from the master device node */
  136. dp = mtd_get_of_node(master);
  137. if (!dp)
  138. return 0;
  139. part = of_get_property(dp, "partitions", &plen);
  140. if (!part)
  141. return 0; /* No partitions found */
  142. pr_warn("Device tree uses obsolete partition map binding: %pOF\n", dp);
  143. nr_parts = plen / sizeof(part[0]);
  144. parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
  145. if (!parts)
  146. return -ENOMEM;
  147. names = of_get_property(dp, "partition-names", &plen);
  148. for (i = 0; i < nr_parts; i++) {
  149. parts[i].offset = be32_to_cpu(part->offset);
  150. parts[i].size = be32_to_cpu(part->len) & ~1;
  151. /* bit 0 set signifies read only partition */
  152. if (be32_to_cpu(part->len) & 1)
  153. parts[i].mask_flags = MTD_WRITEABLE;
  154. if (names && (plen > 0)) {
  155. int len = strlen(names) + 1;
  156. parts[i].name = names;
  157. plen -= len;
  158. names += len;
  159. } else {
  160. parts[i].name = "unnamed";
  161. }
  162. part++;
  163. }
  164. *pparts = parts;
  165. return nr_parts;
  166. }
  167. static struct mtd_part_parser ofoldpart_parser = {
  168. .parse_fn = parse_ofoldpart_partitions,
  169. .name = "ofoldpart",
  170. };
  171. static int __init ofpart_parser_init(void)
  172. {
  173. register_mtd_parser(&ofpart_parser);
  174. register_mtd_parser(&ofoldpart_parser);
  175. return 0;
  176. }
  177. static void __exit ofpart_parser_exit(void)
  178. {
  179. deregister_mtd_parser(&ofpart_parser);
  180. deregister_mtd_parser(&ofoldpart_parser);
  181. }
  182. module_init(ofpart_parser_init);
  183. module_exit(ofpart_parser_exit);
  184. MODULE_LICENSE("GPL");
  185. MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
  186. MODULE_AUTHOR("Vitaly Wool, David Gibson");
  187. /*
  188. * When MTD core cannot find the requested parser, it tries to load the module
  189. * with the same name. Since we provide the ofoldpart parser, we should have
  190. * the corresponding alias.
  191. */
  192. MODULE_ALIAS("ofoldpart");