prom_common.c 6.4 KB

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