ofpart.c 4.6 KB

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