ofpart.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. pr_warn("%s: 'partitions' subnode not found on %s. Trying to parse direct subnodes as partitions.\n",
  43. master->name, mtd_node->full_name);
  44. ofpart_node = mtd_node;
  45. dedicated = false;
  46. }
  47. /* First count the subnodes */
  48. nr_parts = 0;
  49. for_each_child_of_node(ofpart_node, pp) {
  50. if (!dedicated && node_has_compatible(pp))
  51. continue;
  52. nr_parts++;
  53. }
  54. if (nr_parts == 0)
  55. return 0;
  56. *pparts = kzalloc(nr_parts * sizeof(**pparts), GFP_KERNEL);
  57. if (!*pparts)
  58. return -ENOMEM;
  59. i = 0;
  60. for_each_child_of_node(ofpart_node, pp) {
  61. const __be32 *reg;
  62. int len;
  63. int a_cells, s_cells;
  64. if (!dedicated && node_has_compatible(pp))
  65. continue;
  66. reg = of_get_property(pp, "reg", &len);
  67. if (!reg) {
  68. if (dedicated) {
  69. pr_debug("%s: ofpart partition %s (%s) missing reg property.\n",
  70. master->name, pp->full_name,
  71. mtd_node->full_name);
  72. goto ofpart_fail;
  73. } else {
  74. nr_parts--;
  75. continue;
  76. }
  77. }
  78. a_cells = of_n_addr_cells(pp);
  79. s_cells = of_n_size_cells(pp);
  80. if (len / 4 != a_cells + s_cells) {
  81. pr_debug("%s: ofpart partition %s (%s) error parsing reg property.\n",
  82. master->name, pp->full_name,
  83. mtd_node->full_name);
  84. goto ofpart_fail;
  85. }
  86. (*pparts)[i].offset = of_read_number(reg, a_cells);
  87. (*pparts)[i].size = of_read_number(reg + a_cells, s_cells);
  88. partname = of_get_property(pp, "label", &len);
  89. if (!partname)
  90. partname = of_get_property(pp, "name", &len);
  91. (*pparts)[i].name = partname;
  92. if (of_get_property(pp, "read-only", &len))
  93. (*pparts)[i].mask_flags |= MTD_WRITEABLE;
  94. if (of_get_property(pp, "lock", &len))
  95. (*pparts)[i].mask_flags |= MTD_POWERUP_LOCK;
  96. i++;
  97. }
  98. if (!nr_parts)
  99. goto ofpart_none;
  100. return nr_parts;
  101. ofpart_fail:
  102. pr_err("%s: error parsing ofpart partition %s (%s)\n",
  103. master->name, pp->full_name, mtd_node->full_name);
  104. ret = -EINVAL;
  105. ofpart_none:
  106. of_node_put(pp);
  107. kfree(*pparts);
  108. *pparts = NULL;
  109. return ret;
  110. }
  111. static struct mtd_part_parser ofpart_parser = {
  112. .owner = THIS_MODULE,
  113. .parse_fn = parse_ofpart_partitions,
  114. .name = "ofpart",
  115. };
  116. static int parse_ofoldpart_partitions(struct mtd_info *master,
  117. struct mtd_partition **pparts,
  118. struct mtd_part_parser_data *data)
  119. {
  120. struct device_node *dp;
  121. int i, plen, nr_parts;
  122. const struct {
  123. __be32 offset, len;
  124. } *part;
  125. const char *names;
  126. if (!data)
  127. return 0;
  128. dp = data->of_node;
  129. if (!dp)
  130. return 0;
  131. part = of_get_property(dp, "partitions", &plen);
  132. if (!part)
  133. return 0; /* No partitions found */
  134. pr_warning("Device tree uses obsolete partition map binding: %s\n",
  135. dp->full_name);
  136. nr_parts = plen / sizeof(part[0]);
  137. *pparts = kzalloc(nr_parts * sizeof(*(*pparts)), GFP_KERNEL);
  138. if (!*pparts)
  139. return -ENOMEM;
  140. names = of_get_property(dp, "partition-names", &plen);
  141. for (i = 0; i < nr_parts; i++) {
  142. (*pparts)[i].offset = be32_to_cpu(part->offset);
  143. (*pparts)[i].size = be32_to_cpu(part->len) & ~1;
  144. /* bit 0 set signifies read only partition */
  145. if (be32_to_cpu(part->len) & 1)
  146. (*pparts)[i].mask_flags = MTD_WRITEABLE;
  147. if (names && (plen > 0)) {
  148. int len = strlen(names) + 1;
  149. (*pparts)[i].name = names;
  150. plen -= len;
  151. names += len;
  152. } else {
  153. (*pparts)[i].name = "unnamed";
  154. }
  155. part++;
  156. }
  157. return nr_parts;
  158. }
  159. static struct mtd_part_parser ofoldpart_parser = {
  160. .owner = THIS_MODULE,
  161. .parse_fn = parse_ofoldpart_partitions,
  162. .name = "ofoldpart",
  163. };
  164. static int __init ofpart_parser_init(void)
  165. {
  166. register_mtd_parser(&ofpart_parser);
  167. register_mtd_parser(&ofoldpart_parser);
  168. return 0;
  169. }
  170. static void __exit ofpart_parser_exit(void)
  171. {
  172. deregister_mtd_parser(&ofpart_parser);
  173. deregister_mtd_parser(&ofoldpart_parser);
  174. }
  175. module_init(ofpart_parser_init);
  176. module_exit(ofpart_parser_exit);
  177. MODULE_LICENSE("GPL");
  178. MODULE_DESCRIPTION("Parser for MTD partitioning information in device tree");
  179. MODULE_AUTHOR("Vitaly Wool, David Gibson");
  180. /*
  181. * When MTD core cannot find the requested parser, it tries to load the module
  182. * with the same name. Since we provide the ofoldpart parser, we should have
  183. * the corresponding alias.
  184. */
  185. MODULE_ALIAS("ofoldpart");