pdt.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /* prom_common.c: OF device tree support common 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. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License
  13. * as published by the Free Software Foundation; either version
  14. * 2 of the License, or (at your option) any later version.
  15. */
  16. #include <linux/kernel.h>
  17. #include <linux/module.h>
  18. #include <linux/errno.h>
  19. #include <linux/mutex.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <asm/prom.h>
  23. #include <asm/oplib.h>
  24. #include <asm/leon.h>
  25. void (*prom_build_more)(struct device_node *dp, struct device_node ***nextp);
  26. unsigned int prom_unique_id;
  27. static struct property * __init build_one_prop(phandle node, char *prev,
  28. char *special_name,
  29. void *special_val,
  30. int special_len)
  31. {
  32. static struct property *tmp = NULL;
  33. struct property *p;
  34. const char *name;
  35. if (tmp) {
  36. p = tmp;
  37. memset(p, 0, sizeof(*p) + 32);
  38. tmp = NULL;
  39. } else {
  40. p = prom_early_alloc(sizeof(struct property) + 32);
  41. p->unique_id = prom_unique_id++;
  42. }
  43. p->name = (char *) (p + 1);
  44. if (special_name) {
  45. strcpy(p->name, special_name);
  46. p->length = special_len;
  47. p->value = prom_early_alloc(special_len);
  48. memcpy(p->value, special_val, special_len);
  49. } else {
  50. if (prev == NULL) {
  51. name = prom_firstprop(node, p->name);
  52. } else {
  53. name = prom_nextprop(node, prev, p->name);
  54. }
  55. if (!name || strlen(name) == 0) {
  56. tmp = p;
  57. return NULL;
  58. }
  59. #ifdef CONFIG_SPARC32
  60. strcpy(p->name, name);
  61. #endif
  62. p->length = prom_getproplen(node, p->name);
  63. if (p->length <= 0) {
  64. p->length = 0;
  65. } else {
  66. int len;
  67. p->value = prom_early_alloc(p->length + 1);
  68. len = prom_getproperty(node, p->name, p->value,
  69. p->length);
  70. if (len <= 0)
  71. p->length = 0;
  72. ((unsigned char *)p->value)[p->length] = '\0';
  73. }
  74. }
  75. return p;
  76. }
  77. static struct property * __init build_prop_list(phandle node)
  78. {
  79. struct property *head, *tail;
  80. head = tail = build_one_prop(node, NULL,
  81. ".node", &node, sizeof(node));
  82. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  83. tail = tail->next;
  84. while(tail) {
  85. tail->next = build_one_prop(node, tail->name,
  86. NULL, NULL, 0);
  87. tail = tail->next;
  88. }
  89. return head;
  90. }
  91. static char * __init get_one_property(phandle node, const char *name)
  92. {
  93. char *buf = "<NULL>";
  94. int len;
  95. len = prom_getproplen(node, name);
  96. if (len > 0) {
  97. buf = prom_early_alloc(len);
  98. len = prom_getproperty(node, name, buf, len);
  99. }
  100. return buf;
  101. }
  102. static struct device_node * __init prom_create_node(phandle node,
  103. struct device_node *parent)
  104. {
  105. struct device_node *dp;
  106. if (!node)
  107. return NULL;
  108. dp = prom_early_alloc(sizeof(*dp));
  109. dp->unique_id = prom_unique_id++;
  110. dp->parent = parent;
  111. kref_init(&dp->kref);
  112. dp->name = get_one_property(node, "name");
  113. dp->type = get_one_property(node, "device_type");
  114. dp->phandle = node;
  115. dp->properties = build_prop_list(node);
  116. irq_trans_init(dp);
  117. return dp;
  118. }
  119. char * __init build_full_name(struct device_node *dp)
  120. {
  121. int len, ourlen, plen;
  122. char *n;
  123. plen = strlen(dp->parent->full_name);
  124. ourlen = strlen(dp->path_component_name);
  125. len = ourlen + plen + 2;
  126. n = prom_early_alloc(len);
  127. strcpy(n, dp->parent->full_name);
  128. if (!of_node_is_root(dp->parent)) {
  129. strcpy(n + plen, "/");
  130. plen++;
  131. }
  132. strcpy(n + plen, dp->path_component_name);
  133. return n;
  134. }
  135. static struct device_node * __init prom_build_tree(struct device_node *parent,
  136. phandle node,
  137. struct device_node ***nextp)
  138. {
  139. struct device_node *ret = NULL, *prev_sibling = NULL;
  140. struct device_node *dp;
  141. while (1) {
  142. dp = prom_create_node(node, parent);
  143. if (!dp)
  144. break;
  145. if (prev_sibling)
  146. prev_sibling->sibling = dp;
  147. if (!ret)
  148. ret = dp;
  149. prev_sibling = dp;
  150. *(*nextp) = dp;
  151. *nextp = &dp->allnext;
  152. dp->path_component_name = build_path_component(dp);
  153. dp->full_name = build_full_name(dp);
  154. dp->child = prom_build_tree(dp, prom_getchild(node), nextp);
  155. if (prom_build_more)
  156. prom_build_more(dp, nextp);
  157. node = prom_getsibling(node);
  158. }
  159. return ret;
  160. }
  161. void __init prom_build_devicetree(void)
  162. {
  163. struct device_node **nextp;
  164. allnodes = prom_create_node(prom_root_node, NULL);
  165. allnodes->path_component_name = "";
  166. allnodes->full_name = "/";
  167. nextp = &allnodes->allnext;
  168. allnodes->child = prom_build_tree(allnodes,
  169. prom_getchild(allnodes->phandle),
  170. &nextp);
  171. of_console_init();
  172. printk("PROM: Built device tree with %u bytes of memory.\n",
  173. prom_early_allocated);
  174. }