ofpart.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. struct mtd_partition **pparts,
  27. struct mtd_part_parser_data *data)
  28. {
  29. struct device_node *mtd_node;
  30. struct device_node *ofpart_node;
  31. const char *partname;
  32. struct device_node *pp;
  33. int nr_parts, i, ret = 0;
  34. bool dedicated = true;
  35. if (!data)
  36. return 0;
  37. mtd_node = data->of_node;
  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 %s. Trying to parse direct subnodes as partitions.\n",
  48. master->name, mtd_node->full_name);
  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. *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  65. if (!*pparts)
  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 %s (%s) missing reg property.\n",
  78. master->name, pp->full_name,
  79. mtd_node->full_name);
  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 %s (%s) error parsing reg property.\n",
  90. master->name, pp->full_name,
  91. mtd_node->full_name);
  92. goto ofpart_fail;
  93. }
  94. (*pparts)[i].offset = of_read_number(reg, a_cells);
  95. (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
  96. partname = of_get_property(pp, "label", &len);
  97. if (!partname)
  98. partname = of_get_property(pp, "name", &len);
  99. (*pparts)[i].name = partname;
  100. if (of_get_property(pp, "read-only", &len))
  101. (*pparts)[i].mask_flags |= MTD_WRITEABLE;
  102. if (of_get_property(pp, "lock", &len))
  103. (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
  104. i++;
  105. }
  106. if (!nr_parts)
  107. goto ofpart_none;
  108. return nr_parts;
  109. ofpart_fail:
  110. pr_err("%s: error parsing ofpart partition %s (%s)\n",
  111. master->name, pp->full_name, mtd_node->full_name);
  112. ret = -EINVAL;
  113. ofpart_none:
  114. of_node_put(pp);
  115. kfree(*pparts);
  116. *pparts = NULL;
  117. return ret;
  118. }
  119. static struct mtd_part_parser ofpart_parser = {
  120. .owner = THIS_MODULE,
  121. .parse_fn = parse_ofpart_partitions,
  122. .name = "ofpart",
  123. };
  124. static int parse_ofoldpart_partitions(struct mtd_info *master,
  125. struct mtd_partition **pparts,
  126. struct mtd_part_parser_data *data)
  127. {
  128. struct device_node *dp;
  129. int i, plen, nr_parts;
  130. const struct {
  131. __be32 offset, len;
  132. } *part;
  133. const char *names;
  134. if (!data)
  135. return 0;
  136. dp = data->of_node;
  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_warning("Device tree uses obsolete partition map binding: %s\n",
  143. dp->full_name);
  144. nr_parts = plen / sizeof(part[0]);
  145. *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
  146. if (!*pparts)
  147. return -ENOMEM;
  148. names = of_get_property(dp, "partition-names", &plen);
  149. for (i = 0; i < nr_parts; i++) {
  150. (*pparts)[i].offset = be32_to_cpu(part->offset);
  151. (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
  152. /* bit 0 set signifies read only partition */
  153. if (be32_to_cpu(part->len) & 1)
  154. (*pparts)[i].mask_flags = MTD_WRITEABLE;
  155. if (names && (plen > 0)) {
  156. int len = strlen(names) + 1;
  157. (*pparts)[i].name = names;
  158. plen -= len;
  159. names += len;
  160. } else {
  161. (*pparts)[i].name = "unnamed";
  162. }
  163. part++;
  164. }
  165. return nr_parts;
  166. }
  167. static struct mtd_part_parser ofoldpart_parser = {
  168. .owner = THIS_MODULE,
  169. .parse_fn = parse_ofoldpart_partitions,
  170. .name = "ofoldpart",
  171. };
  172. static int __init ofpart_parser_init(void)
  173. {
  174. register_mtd_parser(&ofpart_parser);
  175. register_mtd_parser(&ofoldpart_parser);
  176. return 0;
  177. }
  178. static void __exit ofpart_parser_exit(void)
  179. {
  180. deregister_mtd_parser(&ofpart_parser);
  181. deregister_mtd_parser(&ofoldpart_parser);
  182. }
  183. module_init(ofpart_parser_init);
  184. module_exit(ofpart_parser_exit);
  185. MODULE_LICENSE("GPL");
  186. MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
  187. MODULE_AUTHOR("Vitaly Wool, David Gibson");
  188. /*
  189. * When MTD core cannot find the requested parser, it tries to load the module
  190. * with the same name. Since we provide the ofoldpart parser, we should have
  191. * the corresponding alias.
  192. */
  193. MODULE_ALIAS("ofoldpart");