prom.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  1. /*
  2. * Procedures for creating, accessing and interpreting the device tree.
  3. *
  4. * Paul Mackerras August 1996.
  5. * Copyright (C) 1996-2005 Paul Mackerras.
  6. *
  7. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  8. * {engebret|bergner}@us.ibm.com
  9. *
  10. * Adapted for sparc32 by David S. Miller davem@davemloft.net
  11. *
  12. * This program is free software; you can redistribute it and/or
  13. * modify it under the terms of the GNU General Public License
  14. * as published by the Free Software Foundation; either version
  15. * 2 of the License, or (at your option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/types.h>
  19. #include <linux/string.h>
  20. #include <linux/mm.h>
  21. #include <linux/bootmem.h>
  22. #include <linux/module.h>
  23. #include <asm/prom.h>
  24. #include <asm/oplib.h>
  25. static struct device_node *allnodes;
  26. extern rwlock_t devtree_lock; /* temporary while merging */
  27. struct device_node *of_find_node_by_path(const char *path)
  28. {
  29. struct device_node *np = allnodes;
  30. for (; np != 0; np = np->allnext) {
  31. if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
  32. break;
  33. }
  34. return np;
  35. }
  36. EXPORT_SYMBOL(of_find_node_by_path);
  37. struct device_node *of_find_node_by_phandle(phandle handle)
  38. {
  39. struct device_node *np;
  40. for (np = allnodes; np != 0; np = np->allnext)
  41. if (np->node == handle)
  42. break;
  43. return np;
  44. }
  45. EXPORT_SYMBOL(of_find_node_by_phandle);
  46. struct device_node *of_find_node_by_name(struct device_node *from,
  47. const char *name)
  48. {
  49. struct device_node *np;
  50. np = from ? from->allnext : allnodes;
  51. for (; np != NULL; np = np->allnext)
  52. if (np->name != NULL && strcmp(np->name, name) == 0)
  53. break;
  54. return np;
  55. }
  56. EXPORT_SYMBOL(of_find_node_by_name);
  57. struct device_node *of_find_node_by_type(struct device_node *from,
  58. const char *type)
  59. {
  60. struct device_node *np;
  61. np = from ? from->allnext : allnodes;
  62. for (; np != 0; np = np->allnext)
  63. if (np->type != 0 && strcmp(np->type, type) == 0)
  64. break;
  65. return np;
  66. }
  67. EXPORT_SYMBOL(of_find_node_by_type);
  68. struct device_node *of_find_compatible_node(struct device_node *from,
  69. const char *type, const char *compatible)
  70. {
  71. struct device_node *np;
  72. np = from ? from->allnext : allnodes;
  73. for (; np != 0; np = np->allnext) {
  74. if (type != NULL
  75. && !(np->type != 0 && strcmp(np->type, type) == 0))
  76. continue;
  77. if (of_device_is_compatible(np, compatible))
  78. break;
  79. }
  80. return np;
  81. }
  82. EXPORT_SYMBOL(of_find_compatible_node);
  83. int of_getintprop_default(struct device_node *np, const char *name, int def)
  84. {
  85. struct property *prop;
  86. int len;
  87. prop = of_find_property(np, name, &len);
  88. if (!prop || len != 4)
  89. return def;
  90. return *(int *) prop->value;
  91. }
  92. EXPORT_SYMBOL(of_getintprop_default);
  93. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  94. {
  95. struct property **prevp;
  96. void *new_val;
  97. int err;
  98. new_val = kmalloc(len, GFP_KERNEL);
  99. if (!new_val)
  100. return -ENOMEM;
  101. memcpy(new_val, val, len);
  102. err = -ENODEV;
  103. write_lock(&devtree_lock);
  104. prevp = &dp->properties;
  105. while (*prevp) {
  106. struct property *prop = *prevp;
  107. if (!strcasecmp(prop->name, name)) {
  108. void *old_val = prop->value;
  109. int ret;
  110. ret = prom_setprop(dp->node, (char *) name, val, len);
  111. err = -EINVAL;
  112. if (ret >= 0) {
  113. prop->value = new_val;
  114. prop->length = len;
  115. if (OF_IS_DYNAMIC(prop))
  116. kfree(old_val);
  117. OF_MARK_DYNAMIC(prop);
  118. err = 0;
  119. }
  120. break;
  121. }
  122. prevp = &(*prevp)->next;
  123. }
  124. write_unlock(&devtree_lock);
  125. /* XXX Upate procfs if necessary... */
  126. return err;
  127. }
  128. EXPORT_SYMBOL(of_set_property);
  129. static unsigned int prom_early_allocated;
  130. static void * __init prom_early_alloc(unsigned long size)
  131. {
  132. void *ret;
  133. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  134. if (ret != NULL)
  135. memset(ret, 0, size);
  136. prom_early_allocated += size;
  137. return ret;
  138. }
  139. static int is_root_node(const struct device_node *dp)
  140. {
  141. if (!dp)
  142. return 0;
  143. return (dp->parent == NULL);
  144. }
  145. /* The following routines deal with the black magic of fully naming a
  146. * node.
  147. *
  148. * Certain well known named nodes are just the simple name string.
  149. *
  150. * Actual devices have an address specifier appended to the base name
  151. * string, like this "foo@addr". The "addr" can be in any number of
  152. * formats, and the platform plus the type of the node determine the
  153. * format and how it is constructed.
  154. *
  155. * For children of the ROOT node, the naming convention is fixed and
  156. * determined by whether this is a sun4u or sun4v system.
  157. *
  158. * For children of other nodes, it is bus type specific. So
  159. * we walk up the tree until we discover a "device_type" property
  160. * we recognize and we go from there.
  161. */
  162. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  163. {
  164. struct linux_prom_registers *regs;
  165. struct property *rprop;
  166. rprop = of_find_property(dp, "reg", NULL);
  167. if (!rprop)
  168. return;
  169. regs = rprop->value;
  170. sprintf(tmp_buf, "%s@%x,%x",
  171. dp->name,
  172. regs->which_io, regs->phys_addr);
  173. }
  174. /* "name@slot,offset" */
  175. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  176. {
  177. struct linux_prom_registers *regs;
  178. struct property *prop;
  179. prop = of_find_property(dp, "reg", NULL);
  180. if (!prop)
  181. return;
  182. regs = prop->value;
  183. sprintf(tmp_buf, "%s@%x,%x",
  184. dp->name,
  185. regs->which_io,
  186. regs->phys_addr);
  187. }
  188. /* "name@devnum[,func]" */
  189. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  190. {
  191. struct linux_prom_pci_registers *regs;
  192. struct property *prop;
  193. unsigned int devfn;
  194. prop = of_find_property(dp, "reg", NULL);
  195. if (!prop)
  196. return;
  197. regs = prop->value;
  198. devfn = (regs->phys_hi >> 8) & 0xff;
  199. if (devfn & 0x07) {
  200. sprintf(tmp_buf, "%s@%x,%x",
  201. dp->name,
  202. devfn >> 3,
  203. devfn & 0x07);
  204. } else {
  205. sprintf(tmp_buf, "%s@%x",
  206. dp->name,
  207. devfn >> 3);
  208. }
  209. }
  210. /* "name@addrhi,addrlo" */
  211. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  212. {
  213. struct linux_prom_registers *regs;
  214. struct property *prop;
  215. prop = of_find_property(dp, "reg", NULL);
  216. if (!prop)
  217. return;
  218. regs = prop->value;
  219. sprintf(tmp_buf, "%s@%x,%x",
  220. dp->name,
  221. regs->which_io, regs->phys_addr);
  222. }
  223. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  224. {
  225. struct device_node *parent = dp->parent;
  226. if (parent != NULL) {
  227. if (!strcmp(parent->type, "pci") ||
  228. !strcmp(parent->type, "pciex"))
  229. return pci_path_component(dp, tmp_buf);
  230. if (!strcmp(parent->type, "sbus"))
  231. return sbus_path_component(dp, tmp_buf);
  232. if (!strcmp(parent->type, "ebus"))
  233. return ebus_path_component(dp, tmp_buf);
  234. /* "isa" is handled with platform naming */
  235. }
  236. /* Use platform naming convention. */
  237. return sparc32_path_component(dp, tmp_buf);
  238. }
  239. static char * __init build_path_component(struct device_node *dp)
  240. {
  241. char tmp_buf[64], *n;
  242. tmp_buf[0] = '\0';
  243. __build_path_component(dp, tmp_buf);
  244. if (tmp_buf[0] == '\0')
  245. strcpy(tmp_buf, dp->name);
  246. n = prom_early_alloc(strlen(tmp_buf) + 1);
  247. strcpy(n, tmp_buf);
  248. return n;
  249. }
  250. static char * __init build_full_name(struct device_node *dp)
  251. {
  252. int len, ourlen, plen;
  253. char *n;
  254. plen = strlen(dp->parent->full_name);
  255. ourlen = strlen(dp->path_component_name);
  256. len = ourlen + plen + 2;
  257. n = prom_early_alloc(len);
  258. strcpy(n, dp->parent->full_name);
  259. if (!is_root_node(dp->parent)) {
  260. strcpy(n + plen, "/");
  261. plen++;
  262. }
  263. strcpy(n + plen, dp->path_component_name);
  264. return n;
  265. }
  266. static unsigned int unique_id;
  267. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  268. {
  269. static struct property *tmp = NULL;
  270. struct property *p;
  271. int len;
  272. const char *name;
  273. if (tmp) {
  274. p = tmp;
  275. memset(p, 0, sizeof(*p) + 32);
  276. tmp = NULL;
  277. } else {
  278. p = prom_early_alloc(sizeof(struct property) + 32);
  279. p->unique_id = unique_id++;
  280. }
  281. p->name = (char *) (p + 1);
  282. if (special_name) {
  283. strcpy(p->name, special_name);
  284. p->length = special_len;
  285. p->value = prom_early_alloc(special_len);
  286. memcpy(p->value, special_val, special_len);
  287. } else {
  288. if (prev == NULL) {
  289. name = prom_firstprop(node, NULL);
  290. } else {
  291. name = prom_nextprop(node, prev, NULL);
  292. }
  293. if (strlen(name) == 0) {
  294. tmp = p;
  295. return NULL;
  296. }
  297. strcpy(p->name, name);
  298. p->length = prom_getproplen(node, p->name);
  299. if (p->length <= 0) {
  300. p->length = 0;
  301. } else {
  302. p->value = prom_early_alloc(p->length + 1);
  303. len = prom_getproperty(node, p->name, p->value,
  304. p->length);
  305. if (len <= 0)
  306. p->length = 0;
  307. ((unsigned char *)p->value)[p->length] = '\0';
  308. }
  309. }
  310. return p;
  311. }
  312. static struct property * __init build_prop_list(phandle node)
  313. {
  314. struct property *head, *tail;
  315. head = tail = build_one_prop(node, NULL,
  316. ".node", &node, sizeof(node));
  317. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  318. tail = tail->next;
  319. while(tail) {
  320. tail->next = build_one_prop(node, tail->name,
  321. NULL, NULL, 0);
  322. tail = tail->next;
  323. }
  324. return head;
  325. }
  326. static char * __init get_one_property(phandle node, char *name)
  327. {
  328. char *buf = "<NULL>";
  329. int len;
  330. len = prom_getproplen(node, name);
  331. if (len > 0) {
  332. buf = prom_early_alloc(len);
  333. len = prom_getproperty(node, name, buf, len);
  334. }
  335. return buf;
  336. }
  337. static struct device_node * __init create_node(phandle node)
  338. {
  339. struct device_node *dp;
  340. if (!node)
  341. return NULL;
  342. dp = prom_early_alloc(sizeof(*dp));
  343. dp->unique_id = unique_id++;
  344. kref_init(&dp->kref);
  345. dp->name = get_one_property(node, "name");
  346. dp->type = get_one_property(node, "device_type");
  347. dp->node = node;
  348. /* Build interrupts later... */
  349. dp->properties = build_prop_list(node);
  350. return dp;
  351. }
  352. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  353. {
  354. struct device_node *dp;
  355. dp = create_node(node);
  356. if (dp) {
  357. *(*nextp) = dp;
  358. *nextp = &dp->allnext;
  359. dp->parent = parent;
  360. dp->path_component_name = build_path_component(dp);
  361. dp->full_name = build_full_name(dp);
  362. dp->child = build_tree(dp, prom_getchild(node), nextp);
  363. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  364. }
  365. return dp;
  366. }
  367. void __init prom_build_devicetree(void)
  368. {
  369. struct device_node **nextp;
  370. allnodes = create_node(prom_root_node);
  371. allnodes->path_component_name = "";
  372. allnodes->full_name = "/";
  373. nextp = &allnodes->allnext;
  374. allnodes->child = build_tree(allnodes,
  375. prom_getchild(allnodes->node),
  376. &nextp);
  377. printk("PROM: Built device tree with %u bytes of memory.\n",
  378. prom_early_allocated);
  379. }