prom_common.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  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 "prom.h"
  25. struct device_node *of_find_node_by_phandle(phandle handle)
  26. {
  27. struct device_node *np;
  28. for (np = allnodes; np; np = np->allnext)
  29. if (np->node == handle)
  30. break;
  31. return np;
  32. }
  33. EXPORT_SYMBOL(of_find_node_by_phandle);
  34. int of_getintprop_default(struct device_node *np, const char *name, int def)
  35. {
  36. struct property *prop;
  37. int len;
  38. prop = of_find_property(np, name, &len);
  39. if (!prop || len != 4)
  40. return def;
  41. return *(int *) prop->value;
  42. }
  43. EXPORT_SYMBOL(of_getintprop_default);
  44. DEFINE_MUTEX(of_set_property_mutex);
  45. EXPORT_SYMBOL(of_set_property_mutex);
  46. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  47. {
  48. struct property **prevp;
  49. void *new_val;
  50. int err;
  51. new_val = kmalloc(len, GFP_KERNEL);
  52. if (!new_val)
  53. return -ENOMEM;
  54. memcpy(new_val, val, len);
  55. err = -ENODEV;
  56. write_lock(&devtree_lock);
  57. prevp = &dp->properties;
  58. while (*prevp) {
  59. struct property *prop = *prevp;
  60. if (!strcasecmp(prop->name, name)) {
  61. void *old_val = prop->value;
  62. int ret;
  63. mutex_lock(&of_set_property_mutex);
  64. ret = prom_setprop(dp->node, name, val, len);
  65. mutex_unlock(&of_set_property_mutex);
  66. err = -EINVAL;
  67. if (ret >= 0) {
  68. prop->value = new_val;
  69. prop->length = len;
  70. if (OF_IS_DYNAMIC(prop))
  71. kfree(old_val);
  72. OF_MARK_DYNAMIC(prop);
  73. err = 0;
  74. }
  75. break;
  76. }
  77. prevp = &(*prevp)->next;
  78. }
  79. write_unlock(&devtree_lock);
  80. /* XXX Upate procfs if necessary... */
  81. return err;
  82. }
  83. EXPORT_SYMBOL(of_set_property);
  84. int of_find_in_proplist(const char *list, const char *match, int len)
  85. {
  86. while (len > 0) {
  87. int l;
  88. if (!strcmp(list, match))
  89. return 1;
  90. l = strlen(list) + 1;
  91. list += l;
  92. len -= l;
  93. }
  94. return 0;
  95. }
  96. EXPORT_SYMBOL(of_find_in_proplist);
  97. unsigned int prom_unique_id;
  98. static struct property * __init build_one_prop(phandle node, char *prev,
  99. char *special_name,
  100. void *special_val,
  101. int special_len)
  102. {
  103. static struct property *tmp = NULL;
  104. struct property *p;
  105. const char *name;
  106. if (tmp) {
  107. p = tmp;
  108. memset(p, 0, sizeof(*p) + 32);
  109. tmp = NULL;
  110. } else {
  111. p = prom_early_alloc(sizeof(struct property) + 32);
  112. p->unique_id = prom_unique_id++;
  113. }
  114. p->name = (char *) (p + 1);
  115. if (special_name) {
  116. strcpy(p->name, special_name);
  117. p->length = special_len;
  118. p->value = prom_early_alloc(special_len);
  119. memcpy(p->value, special_val, special_len);
  120. } else {
  121. #ifdef CONFIG_SPARC32
  122. if (prev == NULL) {
  123. name = prom_firstprop(node, NULL);
  124. } else {
  125. name = prom_nextprop(node, prev, NULL);
  126. }
  127. #else
  128. if (prev == NULL) {
  129. prom_firstprop(node, p->name);
  130. } else {
  131. prom_nextprop(node, prev, p->name);
  132. }
  133. name = p->name;
  134. #endif
  135. if (strlen(name) == 0) {
  136. tmp = p;
  137. return NULL;
  138. }
  139. #ifdef CONFIG_SPARC32
  140. strcpy(p->name, name);
  141. #endif
  142. p->length = prom_getproplen(node, p->name);
  143. if (p->length <= 0) {
  144. p->length = 0;
  145. } else {
  146. int len;
  147. p->value = prom_early_alloc(p->length + 1);
  148. len = prom_getproperty(node, p->name, p->value,
  149. p->length);
  150. if (len <= 0)
  151. p->length = 0;
  152. ((unsigned char *)p->value)[p->length] = '\0';
  153. }
  154. }
  155. return p;
  156. }
  157. static struct property * __init build_prop_list(phandle node)
  158. {
  159. struct property *head, *tail;
  160. head = tail = build_one_prop(node, NULL,
  161. ".node", &node, sizeof(node));
  162. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  163. tail = tail->next;
  164. while(tail) {
  165. tail->next = build_one_prop(node, tail->name,
  166. NULL, NULL, 0);
  167. tail = tail->next;
  168. }
  169. return head;
  170. }
  171. static char * __init get_one_property(phandle node, const char *name)
  172. {
  173. char *buf = "<NULL>";
  174. int len;
  175. len = prom_getproplen(node, name);
  176. if (len > 0) {
  177. buf = prom_early_alloc(len);
  178. len = prom_getproperty(node, name, buf, len);
  179. }
  180. return buf;
  181. }
  182. static struct device_node * __init prom_create_node(phandle node,
  183. struct device_node *parent)
  184. {
  185. struct device_node *dp;
  186. if (!node)
  187. return NULL;
  188. dp = prom_early_alloc(sizeof(*dp));
  189. dp->unique_id = prom_unique_id++;
  190. dp->parent = parent;
  191. kref_init(&dp->kref);
  192. dp->name = get_one_property(node, "name");
  193. dp->type = get_one_property(node, "device_type");
  194. dp->node = node;
  195. /* Build interrupts later... */
  196. dp->properties = build_prop_list(node);
  197. return dp;
  198. }
  199. static char * __init build_full_name(struct device_node *dp)
  200. {
  201. int len, ourlen, plen;
  202. char *n;
  203. plen = strlen(dp->parent->full_name);
  204. ourlen = strlen(dp->path_component_name);
  205. len = ourlen + plen + 2;
  206. n = prom_early_alloc(len);
  207. strcpy(n, dp->parent->full_name);
  208. if (!is_root_node(dp->parent)) {
  209. strcpy(n + plen, "/");
  210. plen++;
  211. }
  212. strcpy(n + plen, dp->path_component_name);
  213. return n;
  214. }
  215. static struct device_node * __init prom_build_tree(struct device_node *parent,
  216. phandle node,
  217. struct device_node ***nextp)
  218. {
  219. struct device_node *ret = NULL, *prev_sibling = NULL;
  220. struct device_node *dp;
  221. while (1) {
  222. dp = prom_create_node(node, parent);
  223. if (!dp)
  224. break;
  225. if (prev_sibling)
  226. prev_sibling->sibling = dp;
  227. if (!ret)
  228. ret = dp;
  229. prev_sibling = dp;
  230. *(*nextp) = dp;
  231. *nextp = &dp->allnext;
  232. dp->path_component_name = build_path_component(dp);
  233. dp->full_name = build_full_name(dp);
  234. dp->child = prom_build_tree(dp, prom_getchild(node), nextp);
  235. node = prom_getsibling(node);
  236. }
  237. return ret;
  238. }
  239. unsigned int prom_early_allocated __initdata;
  240. void __init prom_build_devicetree(void)
  241. {
  242. struct device_node **nextp;
  243. allnodes = prom_create_node(prom_root_node, NULL);
  244. allnodes->path_component_name = "";
  245. allnodes->full_name = "/";
  246. nextp = &allnodes->allnext;
  247. allnodes->child = prom_build_tree(allnodes,
  248. prom_getchild(allnodes->node),
  249. &nextp);
  250. of_console_init();
  251. printk("PROM: Built device tree with %u bytes of memory.\n",
  252. prom_early_allocated);
  253. of_fill_in_cpu_data();
  254. }