pdt.c 5.9 KB

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