prom.c 11 KB

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