pdt.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. static struct of_pdt_ops *of_pdt_prom_ops __initdata;
  25. void __initdata (*of_pdt_build_more)(struct device_node *dp,
  26. struct device_node ***nextp);
  27. #if defined(CONFIG_SPARC)
  28. unsigned int of_pdt_unique_id __initdata;
  29. #define of_pdt_incr_unique_id(p) do { \
  30. (p)->unique_id = of_pdt_unique_id++; \
  31. } while (0)
  32. static char * __init of_pdt_build_full_name(struct device_node *dp)
  33. {
  34. int len, ourlen, plen;
  35. char *n;
  36. dp->path_component_name = build_path_component(dp);
  37. plen = strlen(dp->parent->full_name);
  38. ourlen = strlen(dp->path_component_name);
  39. len = ourlen + plen + 2;
  40. n = prom_early_alloc(len);
  41. strcpy(n, dp->parent->full_name);
  42. if (!of_node_is_root(dp->parent)) {
  43. strcpy(n + plen, "/");
  44. plen++;
  45. }
  46. strcpy(n + plen, dp->path_component_name);
  47. return n;
  48. }
  49. #else /* CONFIG_SPARC */
  50. static inline void of_pdt_incr_unique_id(void *p) { }
  51. static inline void irq_trans_init(struct device_node *dp) { }
  52. static char * __init of_pdt_build_full_name(struct device_node *dp)
  53. {
  54. static int failsafe_id = 0; /* for generating unique names on failure */
  55. char *buf;
  56. int len;
  57. if (of_pdt_prom_ops->pkg2path(dp->phandle, NULL, 0, &len))
  58. goto failsafe;
  59. buf = prom_early_alloc(len + 1);
  60. if (of_pdt_prom_ops->pkg2path(dp->phandle, buf, len, &len))
  61. goto failsafe;
  62. return buf;
  63. failsafe:
  64. buf = prom_early_alloc(strlen(dp->parent->full_name) +
  65. strlen(dp->name) + 16);
  66. sprintf(buf, "%s/%s@unknown%i",
  67. of_node_is_root(dp->parent) ? "" : dp->parent->full_name,
  68. dp->name, failsafe_id++);
  69. pr_err("%s: pkg2path failed; assigning %s\n", __func__, buf);
  70. return buf;
  71. }
  72. #endif /* !CONFIG_SPARC */
  73. static struct property * __init of_pdt_build_one_prop(phandle node, char *prev,
  74. char *special_name,
  75. void *special_val,
  76. int special_len)
  77. {
  78. static struct property *tmp = NULL;
  79. struct property *p;
  80. int err;
  81. if (tmp) {
  82. p = tmp;
  83. memset(p, 0, sizeof(*p) + 32);
  84. tmp = NULL;
  85. } else {
  86. p = prom_early_alloc(sizeof(struct property) + 32);
  87. of_pdt_incr_unique_id(p);
  88. }
  89. p->name = (char *) (p + 1);
  90. if (special_name) {
  91. strcpy(p->name, special_name);
  92. p->length = special_len;
  93. p->value = prom_early_alloc(special_len);
  94. memcpy(p->value, special_val, special_len);
  95. } else {
  96. err = of_pdt_prom_ops->nextprop(node, prev, p->name);
  97. if (err) {
  98. tmp = p;
  99. return NULL;
  100. }
  101. p->length = of_pdt_prom_ops->getproplen(node, p->name);
  102. if (p->length <= 0) {
  103. p->length = 0;
  104. } else {
  105. int len;
  106. p->value = prom_early_alloc(p->length + 1);
  107. len = of_pdt_prom_ops->getproperty(node, p->name,
  108. p->value, p->length);
  109. if (len <= 0)
  110. p->length = 0;
  111. ((unsigned char *)p->value)[p->length] = '\0';
  112. }
  113. }
  114. return p;
  115. }
  116. static struct property * __init of_pdt_build_prop_list(phandle node)
  117. {
  118. struct property *head, *tail;
  119. head = tail = of_pdt_build_one_prop(node, NULL,
  120. ".node", &node, sizeof(node));
  121. tail->next = of_pdt_build_one_prop(node, NULL, NULL, NULL, 0);
  122. tail = tail->next;
  123. while(tail) {
  124. tail->next = of_pdt_build_one_prop(node, tail->name,
  125. NULL, NULL, 0);
  126. tail = tail->next;
  127. }
  128. return head;
  129. }
  130. static char * __init of_pdt_get_one_property(phandle node, const char *name)
  131. {
  132. char *buf = "<NULL>";
  133. int len;
  134. len = of_pdt_prom_ops->getproplen(node, name);
  135. if (len > 0) {
  136. buf = prom_early_alloc(len);
  137. len = of_pdt_prom_ops->getproperty(node, name, buf, len);
  138. }
  139. return buf;
  140. }
  141. static struct device_node * __init of_pdt_create_node(phandle node,
  142. struct device_node *parent)
  143. {
  144. struct device_node *dp;
  145. if (!node)
  146. return NULL;
  147. dp = prom_early_alloc(sizeof(*dp));
  148. of_pdt_incr_unique_id(dp);
  149. dp->parent = parent;
  150. kref_init(&dp->kref);
  151. dp->name = of_pdt_get_one_property(node, "name");
  152. dp->type = of_pdt_get_one_property(node, "device_type");
  153. dp->phandle = node;
  154. dp->properties = of_pdt_build_prop_list(node);
  155. irq_trans_init(dp);
  156. return dp;
  157. }
  158. static struct device_node * __init of_pdt_build_tree(struct device_node *parent,
  159. phandle node,
  160. struct device_node ***nextp)
  161. {
  162. struct device_node *ret = NULL, *prev_sibling = NULL;
  163. struct device_node *dp;
  164. while (1) {
  165. dp = of_pdt_create_node(node, parent);
  166. if (!dp)
  167. break;
  168. if (prev_sibling)
  169. prev_sibling->sibling = dp;
  170. if (!ret)
  171. ret = dp;
  172. prev_sibling = dp;
  173. *(*nextp) = dp;
  174. *nextp = &dp->allnext;
  175. dp->full_name = of_pdt_build_full_name(dp);
  176. dp->child = of_pdt_build_tree(dp,
  177. of_pdt_prom_ops->getchild(node), nextp);
  178. if (of_pdt_build_more)
  179. of_pdt_build_more(dp, nextp);
  180. node = of_pdt_prom_ops->getsibling(node);
  181. }
  182. return ret;
  183. }
  184. static void * __init kernel_tree_alloc(u64 size, u64 align)
  185. {
  186. return prom_early_alloc(size);
  187. }
  188. void __init of_pdt_build_devicetree(phandle root_node, struct of_pdt_ops *ops)
  189. {
  190. struct device_node **nextp;
  191. BUG_ON(!ops);
  192. of_pdt_prom_ops = ops;
  193. of_allnodes = of_pdt_create_node(root_node, NULL);
  194. #if defined(CONFIG_SPARC)
  195. of_allnodes->path_component_name = "";
  196. #endif
  197. of_allnodes->full_name = "/";
  198. nextp = &of_allnodes->allnext;
  199. of_allnodes->child = of_pdt_build_tree(of_allnodes,
  200. of_pdt_prom_ops->getchild(of_allnodes->phandle), &nextp);
  201. /* Get pointer to "/chosen" and "/aliases" nodes for use everywhere */
  202. of_alias_scan(kernel_tree_alloc);
  203. }