pdt.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /* pdt.c: OF PROM device tree support code.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc by David S. Miller davem@davemloft.net
  11. * Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/errno.h>
  16. #include <linux/mutex.h>
  17. #include <linux/slab.h>
  18. #include <linux/of.h>
  19. #include <linux/of_pdt.h>
  20. static struct of_pdt_ops *of_pdt_prom_ops __initdata;
  21. void __initdata (*of_pdt_build_more)(struct device_node *dp);
  22. #if defined(CONFIG_SPARC)
  23. unsigned int of_pdt_unique_id __initdata;
  24. #define of_pdt_incr_unique_id(p) do { \
  25. (p)->unique_id = of_pdt_unique_id++; \
  26. } while (0)
  27. static char * __init of_pdt_build_full_name(struct device_node *dp)
  28. {
  29. int len, ourlen, plen;
  30. char *n;
  31. dp->path_component_name = build_path_component(dp);
  32. plen = strlen(dp->parent->full_name);
  33. ourlen = strlen(dp->path_component_name);
  34. len = ourlen + plen + 2;
  35. n = prom_early_alloc(len);
  36. strcpy(n, dp->parent->full_name);
  37. if (!of_node_is_root(dp->parent)) {
  38. strcpy(n + plen, "/");
  39. plen++;
  40. }
  41. strcpy(n + plen, dp->path_component_name);
  42. return n;
  43. }
  44. #else /* CONFIG_SPARC */
  45. static inline void of_pdt_incr_unique_id(void *p) { }
  46. static inline void irq_trans_init(struct device_node *dp) { }
  47. static char * __init of_pdt_build_full_name(struct device_node *dp)
  48. {
  49. static int failsafe_id = 0; /* for generating unique names on failure */
  50. char *buf;
  51. int len;
  52. if (of_pdt_prom_ops->pkg2path(dp->phandle, NULL, 0, &len))
  53. goto failsafe;
  54. buf = prom_early_alloc(len + 1);
  55. if (of_pdt_prom_ops->pkg2path(dp->phandle, buf, len, &len))
  56. goto failsafe;
  57. return buf;
  58. failsafe:
  59. buf = prom_early_alloc(strlen(dp->parent->full_name) +
  60. strlen(dp->name) + 16);
  61. sprintf(buf, "%s/%s@unknown%i",
  62. of_node_is_root(dp->parent) ? "" : dp->parent->full_name,
  63. dp->name, failsafe_id++);
  64. pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
  65. return buf;
  66. }
  67. #endif /* !CONFIG_SPARC */
  68. static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
  69. char *special_name,
  70. void *special_val,
  71. int special_len)
  72. {
  73. static struct property *tmp = NULL;
  74. struct property *p;
  75. int err;
  76. if (tmp) {
  77. p = tmp;
  78. memset(p, 0, sizeof(*p) + 32);
  79. tmp = NULL;
  80. } else {
  81. p = prom_early_alloc(sizeof(struct property) + 32);
  82. of_pdt_incr_unique_id(p);
  83. }
  84. p->name = (char *) (p + 1);
  85. if (special_name) {
  86. strcpy(p->name, special_name);
  87. p->length = special_len;
  88. p->value = prom_early_alloc(special_len);
  89. memcpy(p->value, special_val, special_len);
  90. } else {
  91. err = of_pdt_prom_ops->nextprop(node, prev, p->name);
  92. if (err) {
  93. tmp = p;
  94. return NULL;
  95. }
  96. p->length = of_pdt_prom_ops->getproplen(node, p->name);
  97. if (p->length <= 0) {
  98. p->length = 0;
  99. } else {
  100. int len;
  101. p->value = prom_early_alloc(p->length + 1);
  102. len = of_pdt_prom_ops->getproperty(node, p->name,
  103. p->value, p->length);
  104. if (len <= 0)
  105. p->length = 0;
  106. ((unsigned char *)p->value)[p->length] = '\0';
  107. }
  108. }
  109. return p;
  110. }
  111. static struct property * __init of_pdt_build_prop_list(phandle node)
  112. {
  113. struct property *head, *tail;
  114. head = tail = of_pdt_build_one_prop(node, NULL,
  115. ".node", &node, sizeof(node));
  116. tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
  117. tail = tail->next;
  118. while(tail) {
  119. tail->next = of_pdt_build_one_prop(node, tail->name,
  120. NULL, NULL, 0);
  121. tail = tail->next;
  122. }
  123. return head;
  124. }
  125. static char * __init of_pdt_get_one_property(phandle node, const char *name)
  126. {
  127. char *buf = "<NULL>";
  128. int len;
  129. len = of_pdt_prom_ops->getproplen(node, name);
  130. if (len > 0) {
  131. buf = prom_early_alloc(len);
  132. len = of_pdt_prom_ops->getproperty(node, name, buf, len);
  133. }
  134. return buf;
  135. }
  136. static struct device_node * __init of_pdt_create_node(phandle node,
  137. struct device_node *parent)
  138. {
  139. struct device_node *dp;
  140. if (!node)
  141. return NULL;
  142. dp = prom_early_alloc(sizeof(*dp));
  143. of_node_init(dp);
  144. of_pdt_incr_unique_id(dp);
  145. dp->parent = parent;
  146. dp->name = of_pdt_get_one_property(node, "name");
  147. dp->type = of_pdt_get_one_property(node, "device_type");
  148. dp->phandle = node;
  149. dp->properties = of_pdt_build_prop_list(node);
  150. irq_trans_init(dp);
  151. return dp;
  152. }
  153. static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
  154. phandle node)
  155. {
  156. struct device_node *ret = NULL, *prev_sibling = NULL;
  157. struct device_node *dp;
  158. while (1) {
  159. dp = of_pdt_create_node(node, parent);
  160. if (!dp)
  161. break;
  162. if (prev_sibling)
  163. prev_sibling->sibling = dp;
  164. if (!ret)
  165. ret = dp;
  166. prev_sibling = dp;
  167. dp->full_name = of_pdt_build_full_name(dp);
  168. dp->child = of_pdt_build_tree(dp, of_pdt_prom_ops->getchild(node));
  169. if (of_pdt_build_more)
  170. of_pdt_build_more(dp);
  171. node = of_pdt_prom_ops->getsibling(node);
  172. }
  173. return ret;
  174. }
  175. static void * __init kernel_tree_alloc(u64 size, u64 align)
  176. {
  177. return prom_early_alloc(size);
  178. }
  179. void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
  180. {
  181. BUG_ON(!ops);
  182. of_pdt_prom_ops = ops;
  183. of_root = of_pdt_create_node(root_node, NULL);
  184. #if defined(CONFIG_SPARC)
  185. of_root->path_component_name = "";
  186. #endif
  187. of_root->full_name = "/";
  188. of_root->child = of_pdt_build_tree(of_root,
  189. of_pdt_prom_ops->getchild(of_root->phandle));
  190. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  191. of_alias_scan(kernel_tree_alloc);
  192. }