prom_common.c 6.5 KB

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