prom_common.c 6.6 KB

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