pdt.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 struct device_node * __init of_pdt_create_node(phandle node,
  114. struct device_node *parent)
  115. {
  116. struct device_node *dp;
  117. if (!node)
  118. return NULL;
  119. dp = prom_early_alloc(sizeof(*dp));
  120. of_pdt_incr_unique_id(dp);
  121. dp->parent = parent;
  122. kref_init(&dp->kref);
  123. dp->name = of_pdt_get_one_property(node, "name");
  124. dp->type = of_pdt_get_one_property(node, "device_type");
  125. dp->phandle = node;
  126. dp->properties = of_pdt_build_prop_list(node);
  127. irq_trans_init(dp);
  128. return dp;
  129. }
  130. static char * __init of_pdt_build_full_name(struct device_node *dp)
  131. {
  132. int len, ourlen, plen;
  133. char *n;
  134. plen = strlen(dp->parent->full_name);
  135. ourlen = strlen(of_pdt_node_name(dp));
  136. len = ourlen + plen + 2;
  137. n = prom_early_alloc(len);
  138. strcpy(n, dp->parent->full_name);
  139. if (!of_node_is_root(dp->parent)) {
  140. strcpy(n + plen, "/");
  141. plen++;
  142. }
  143. strcpy(n + plen, of_pdt_node_name(dp));
  144. return n;
  145. }
  146. static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
  147. phandle node,
  148. struct device_node ***nextp)
  149. {
  150. struct device_node *ret = NULL, *prev_sibling = NULL;
  151. struct device_node *dp;
  152. while (1) {
  153. dp = of_pdt_create_node(node, parent);
  154. if (!dp)
  155. break;
  156. if (prev_sibling)
  157. prev_sibling->sibling = dp;
  158. if (!ret)
  159. ret = dp;
  160. prev_sibling = dp;
  161. *(*nextp) = dp;
  162. *nextp = &dp->allnext;
  163. #if defined(CONFIG_SPARC)
  164. dp->path_component_name = build_path_component(dp);
  165. #endif
  166. dp->full_name = of_pdt_build_full_name(dp);
  167. dp->child = of_pdt_build_tree(dp,
  168. of_pdt_prom_ops->getchild(node), nextp);
  169. if (of_pdt_build_more)
  170. of_pdt_build_more(dp, nextp);
  171. node = of_pdt_prom_ops->getsibling(node);
  172. }
  173. return ret;
  174. }
  175. void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
  176. {
  177. struct device_node **nextp;
  178. BUG_ON(!ops);
  179. of_pdt_prom_ops = ops;
  180. allnodes = of_pdt_create_node(root_node, NULL);
  181. #if defined(CONFIG_SPARC)
  182. allnodes->path_component_name = "";
  183. #endif
  184. allnodes->full_name = "/";
  185. nextp = &allnodes->allnext;
  186. allnodes->child = of_pdt_build_tree(allnodes,
  187. of_pdt_prom_ops->getchild(allnodes->phandle), &nextp);
  188. }