ofpart.c 4.4 KB

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