prom_common.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. #ifdef CONFIG_SPARC32
  128. if (prev == NULL) {
  129. name = prom_firstprop(node, NULL);
  130. } else {
  131. name = prom_nextprop(node, prev, NULL);
  132. }
  133. #else
  134. if (prev == NULL) {
  135. prom_firstprop(node, p->name);
  136. } else {
  137. prom_nextprop(node, prev, p->name);
  138. }
  139. name = p->name;
  140. #endif
  141. if (strlen(name) == 0) {
  142. tmp = p;
  143. return NULL;
  144. }
  145. #ifdef CONFIG_SPARC32
  146. strcpy(p->name, name);
  147. #endif
  148. p->length = prom_getproplen(node, p->name);
  149. if (p->length <= 0) {
  150. p->length = 0;
  151. } else {
  152. int len;
  153. p->value = prom_early_alloc(p->length + 1);
  154. len = prom_getproperty(node, p->name, p->value,
  155. p->length);
  156. if (len <= 0)
  157. p->length = 0;
  158. ((unsigned char *)p->value)[p->length] = '\0';
  159. }
  160. }
  161. return p;
  162. }
  163. static struct property * __init build_prop_list(phandle node)
  164. {
  165. struct property *head, *tail;
  166. head = tail = build_one_prop(node, NULL,
  167. ".node", &node, sizeof(node));
  168. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  169. tail = tail->next;
  170. while(tail) {
  171. tail->next = build_one_prop(node, tail->name,
  172. NULL, NULL, 0);
  173. tail = tail->next;
  174. }
  175. return head;
  176. }
  177. static char * __init get_one_property(phandle node, const char *name)
  178. {
  179. char *buf = "<NULL>";
  180. int len;
  181. len = prom_getproplen(node, name);
  182. if (len > 0) {
  183. buf = prom_early_alloc(len);
  184. len = prom_getproperty(node, name, buf, len);
  185. }
  186. return buf;
  187. }
  188. static struct device_node * __init prom_create_node(phandle node,
  189. struct device_node *parent)
  190. {
  191. struct device_node *dp;
  192. if (!node)
  193. return NULL;
  194. dp = prom_early_alloc(sizeof(*dp));
  195. dp->unique_id = prom_unique_id++;
  196. dp->parent = parent;
  197. kref_init(&dp->kref);
  198. dp->name = get_one_property(node, "name");
  199. dp->type = get_one_property(node, "device_type");
  200. dp->node = node;
  201. /* Build interrupts later... */
  202. dp->properties = build_prop_list(node);
  203. return dp;
  204. }
  205. static char * __init build_full_name(struct device_node *dp)
  206. {
  207. int len, ourlen, plen;
  208. char *n;
  209. plen = strlen(dp->parent->full_name);
  210. ourlen = strlen(dp->path_component_name);
  211. len = ourlen + plen + 2;
  212. n = prom_early_alloc(len);
  213. strcpy(n, dp->parent->full_name);
  214. if (!is_root_node(dp->parent)) {
  215. strcpy(n + plen, "/");
  216. plen++;
  217. }
  218. strcpy(n + plen, dp->path_component_name);
  219. return n;
  220. }
  221. static struct device_node * __init prom_build_tree(struct device_node *parent,
  222. phandle node,
  223. struct device_node ***nextp)
  224. {
  225. struct device_node *ret = NULL, *prev_sibling = NULL;
  226. struct device_node *dp;
  227. while (1) {
  228. dp = prom_create_node(node, parent);
  229. if (!dp)
  230. break;
  231. if (prev_sibling)
  232. prev_sibling->sibling = dp;
  233. if (!ret)
  234. ret = dp;
  235. prev_sibling = dp;
  236. *(*nextp) = dp;
  237. *nextp = &dp->allnext;
  238. dp->path_component_name = build_path_component(dp);
  239. dp->full_name = build_full_name(dp);
  240. dp->child = prom_build_tree(dp, prom_getchild(node), nextp);
  241. node = prom_getsibling(node);
  242. }
  243. return ret;
  244. }
  245. unsigned int prom_early_allocated __initdata;
  246. void __init prom_build_devicetree(void)
  247. {
  248. struct device_node **nextp;
  249. allnodes = prom_create_node(prom_root_node, NULL);
  250. allnodes->path_component_name = "";
  251. allnodes->full_name = "/";
  252. nextp = &allnodes->allnext;
  253. allnodes->child = prom_build_tree(allnodes,
  254. prom_getchild(allnodes->node),
  255. &nextp);
  256. of_console_init();
  257. printk("PROM: Built device tree with %u bytes of memory.\n",
  258. prom_early_allocated);
  259. of_fill_in_cpu_data();
  260. }