pdt.c 5.7 KB

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