pdt.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* pdt.c: OF PROM device tree support code.
  2. *
  3. * Paul Mackerras August 1996.
  4. * Copyright (C) 1996-2005 Paul Mackerras.
  5. *
  6. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  7. * {engebret|bergner}@us.ibm.com
  8. *
  9. * Adapted for sparc by David S. Miller davem@davemloft.net
  10. * Adapted for multiple architectures by Andres Salomon <dilinger@queued.net>
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/module.h>
  19. #include <linux/errno.h>
  20. #include <linux/mutex.h>
  21. #include <linux/slab.h>
  22. #include <linux/of.h>
  23. #include <linux/of_pdt.h>
  24. #include <asm/prom.h>
  25. static struct of_pdt_ops *of_pdt_prom_ops __initdata;
  26. void __initdata (*of_pdt_build_more)(struct device_node *dp,
  27. struct device_node ***nextp);
  28. #if defined(CONFIG_SPARC)
  29. unsigned int of_pdt_unique_id __initdata;
  30. #define of_pdt_incr_unique_id(p) do { \
  31. (p)->unique_id = of_pdt_unique_id++; \
  32. } while (0)
  33. static inline const char *of_pdt_node_name(struct device_node *dp)
  34. {
  35. return dp->path_component_name;
  36. }
  37. #else
  38. static inline void of_pdt_incr_unique_id(void *p) { }
  39. static inline void irq_trans_init(struct device_node *dp) { }
  40. static inline const char *of_pdt_node_name(struct device_node *dp)
  41. {
  42. return dp->name;
  43. }
  44. #endif /* !CONFIG_SPARC */
  45. static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
  46. char *special_name,
  47. void *special_val,
  48. int special_len)
  49. {
  50. static struct property *tmp = NULL;
  51. struct property *p;
  52. int err;
  53. if (tmp) {
  54. p = tmp;
  55. memset(p, 0, sizeof(*p) + 32);
  56. tmp = NULL;
  57. } else {
  58. p = prom_early_alloc(sizeof(struct property) + 32);
  59. of_pdt_incr_unique_id(p);
  60. }
  61. p->name = (char *) (p + 1);
  62. if (special_name) {
  63. strcpy(p->name, special_name);
  64. p->length = special_len;
  65. p->value = prom_early_alloc(special_len);
  66. memcpy(p->value, special_val, special_len);
  67. } else {
  68. err = of_pdt_prom_ops->nextprop(node, prev, p->name);
  69. if (err) {
  70. tmp = p;
  71. return NULL;
  72. }
  73. p->length = of_pdt_prom_ops->getproplen(node, p->name);
  74. if (p->length <= 0) {
  75. p->length = 0;
  76. } else {
  77. int len;
  78. p->value = prom_early_alloc(p->length + 1);
  79. len = of_pdt_prom_ops->getproperty(node, p->name,
  80. p->value, p->length);
  81. if (len <= 0)
  82. p->length = 0;
  83. ((unsigned char *)p->value)[p->length] = '\0';
  84. }
  85. }
  86. return p;
  87. }
  88. static struct property * __init of_pdt_build_prop_list(phandle node)
  89. {
  90. struct property *head, *tail;
  91. head = tail = of_pdt_build_one_prop(node, NULL,
  92. ".node", &node, sizeof(node));
  93. tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
  94. tail = tail->next;
  95. while(tail) {
  96. tail->next = of_pdt_build_one_prop(node, tail->name,
  97. NULL, NULL, 0);
  98. tail = tail->next;
  99. }
  100. return head;
  101. }
  102. static char * __init of_pdt_get_one_property(phandle node, const char *name)
  103. {
  104. char *buf = "<NULL>";
  105. int len;
  106. len = of_pdt_prom_ops->getproplen(node, name);
  107. if (len > 0) {
  108. buf = prom_early_alloc(len);
  109. len = of_pdt_prom_ops->getproperty(node, name, buf, len);
  110. }
  111. return buf;
  112. }
  113. static char * __init of_pdt_try_pkg2path(phandle node)
  114. {
  115. char *res, *buf = NULL;
  116. int len;
  117. if (!of_pdt_prom_ops->pkg2path)
  118. return NULL;
  119. if (of_pdt_prom_ops->pkg2path(node, buf, 0, &len))
  120. return NULL;
  121. buf = prom_early_alloc(len + 1);
  122. if (of_pdt_prom_ops->pkg2path(node, buf, len, &len)) {
  123. pr_err("%s: package-to-path failed\n", __func__);
  124. return NULL;
  125. }
  126. res = strrchr(buf, '/');
  127. if (!res) {
  128. pr_err("%s: couldn't find / in %s\n", __func__, buf);
  129. return NULL;
  130. }
  131. return res+1;
  132. }
  133. /*
  134. * When fetching the node's name, first try using package-to-path; if
  135. * that fails (either because the arch hasn't supplied a PROM callback,
  136. * or some other random failure), fall back to just looking at the node's
  137. * 'name' property.
  138. */
  139. static char * __init of_pdt_build_name(phandle node)
  140. {
  141. char *buf;
  142. buf = of_pdt_try_pkg2path(node);
  143. if (!buf)
  144. buf = of_pdt_get_one_property(node, "name");
  145. return buf;
  146. }
  147. static struct device_node * __init of_pdt_create_node(phandle node,
  148. struct device_node *parent)
  149. {
  150. struct device_node *dp;
  151. if (!node)
  152. return NULL;
  153. dp = prom_early_alloc(sizeof(*dp));
  154. of_pdt_incr_unique_id(dp);
  155. dp->parent = parent;
  156. kref_init(&dp->kref);
  157. dp->name = of_pdt_build_name(node);
  158. dp->type = of_pdt_get_one_property(node, "device_type");
  159. dp->phandle = node;
  160. dp->properties = of_pdt_build_prop_list(node);
  161. irq_trans_init(dp);
  162. return dp;
  163. }
  164. static char * __init of_pdt_build_full_name(struct device_node *dp)
  165. {
  166. int len, ourlen, plen;
  167. char *n;
  168. plen = strlen(dp->parent->full_name);
  169. ourlen = strlen(of_pdt_node_name(dp));
  170. len = ourlen + plen + 2;
  171. n = prom_early_alloc(len);
  172. strcpy(n, dp->parent->full_name);
  173. if (!of_node_is_root(dp->parent)) {
  174. strcpy(n + plen, "/");
  175. plen++;
  176. }
  177. strcpy(n + plen, of_pdt_node_name(dp));
  178. return n;
  179. }
  180. static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
  181. phandle node,
  182. struct device_node ***nextp)
  183. {
  184. struct device_node *ret = NULL, *prev_sibling = NULL;
  185. struct device_node *dp;
  186. while (1) {
  187. dp = of_pdt_create_node(node, parent);
  188. if (!dp)
  189. break;
  190. if (prev_sibling)
  191. prev_sibling->sibling = dp;
  192. if (!ret)
  193. ret = dp;
  194. prev_sibling = dp;
  195. *(*nextp) = dp;
  196. *nextp = &dp->allnext;
  197. #if defined(CONFIG_SPARC)
  198. dp->path_component_name = build_path_component(dp);
  199. #endif
  200. dp->full_name = of_pdt_build_full_name(dp);
  201. dp->child = of_pdt_build_tree(dp,
  202. of_pdt_prom_ops->getchild(node), nextp);
  203. if (of_pdt_build_more)
  204. of_pdt_build_more(dp, nextp);
  205. node = of_pdt_prom_ops->getsibling(node);
  206. }
  207. return ret;
  208. }
  209. void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
  210. {
  211. struct device_node **nextp;
  212. BUG_ON(!ops);
  213. of_pdt_prom_ops = ops;
  214. allnodes = of_pdt_create_node(root_node, NULL);
  215. #if defined(CONFIG_SPARC)
  216. allnodes->path_component_name = "";
  217. #endif
  218. allnodes->full_name = "/";
  219. nextp = &allnodes->allnext;
  220. allnodes->child = of_pdt_build_tree(allnodes,
  221. of_pdt_prom_ops->getchild(allnodes->phandle), &nextp);
  222. }