prom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 sparc64 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. struct device_node *of_get_parent(const struct device_node *node)
  27. {
  28. struct device_node *np;
  29. if (!node)
  30. return NULL;
  31. np = node->parent;
  32. return np;
  33. }
  34. struct device_node *of_get_next_child(const struct device_node *node,
  35. struct device_node *prev)
  36. {
  37. struct device_node *next;
  38. next = prev ? prev->sibling : node->child;
  39. for (; next != 0; next = next->sibling) {
  40. break;
  41. }
  42. return next;
  43. }
  44. struct device_node *of_find_node_by_path(const char *path)
  45. {
  46. struct device_node *np = allnodes;
  47. for (; np != 0; np = np->allnext) {
  48. if (np->full_name != 0 && strcmp(np->full_name, path) == 0)
  49. break;
  50. }
  51. return np;
  52. }
  53. EXPORT_SYMBOL(of_find_node_by_path);
  54. struct device_node *of_find_node_by_phandle(phandle handle)
  55. {
  56. struct device_node *np;
  57. for (np = allnodes; np != 0; np = np->allnext)
  58. if (np->node == handle)
  59. break;
  60. return np;
  61. }
  62. struct device_node *of_find_node_by_name(struct device_node *from,
  63. const char *name)
  64. {
  65. struct device_node *np;
  66. np = from ? from->allnext : allnodes;
  67. for (; np != NULL; np = np->allnext)
  68. if (np->name != NULL && strcmp(np->name, name) == 0)
  69. break;
  70. return np;
  71. }
  72. struct device_node *of_find_node_by_type(struct device_node *from,
  73. const char *type)
  74. {
  75. struct device_node *np;
  76. np = from ? from->allnext : allnodes;
  77. for (; np != 0; np = np->allnext)
  78. if (np->type != 0 && strcmp(np->type, type) == 0)
  79. break;
  80. return np;
  81. }
  82. struct property *of_find_property(struct device_node *np, const char *name,
  83. int *lenp)
  84. {
  85. struct property *pp;
  86. for (pp = np->properties; pp != 0; pp = pp->next) {
  87. if (strcmp(pp->name, name) == 0) {
  88. if (lenp != 0)
  89. *lenp = pp->length;
  90. break;
  91. }
  92. }
  93. return pp;
  94. }
  95. EXPORT_SYMBOL(of_find_property);
  96. /*
  97. * Find a property with a given name for a given node
  98. * and return the value.
  99. */
  100. void *of_get_property(struct device_node *np, const char *name, int *lenp)
  101. {
  102. struct property *pp = of_find_property(np,name,lenp);
  103. return pp ? pp->value : NULL;
  104. }
  105. EXPORT_SYMBOL(of_get_property);
  106. int of_getintprop_default(struct device_node *np, const char *name, int def)
  107. {
  108. struct property *prop;
  109. int len;
  110. prop = of_find_property(np, name, &len);
  111. if (!prop || len != 4)
  112. return def;
  113. return *(int *) prop->value;
  114. }
  115. EXPORT_SYMBOL(of_getintprop_default);
  116. static unsigned int prom_early_allocated;
  117. static void * __init prom_early_alloc(unsigned long size)
  118. {
  119. void *ret;
  120. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  121. if (ret != NULL)
  122. memset(ret, 0, size);
  123. prom_early_allocated += size;
  124. return ret;
  125. }
  126. static int is_root_node(const struct device_node *dp)
  127. {
  128. if (!dp)
  129. return 0;
  130. return (dp->parent == NULL);
  131. }
  132. /* The following routines deal with the black magic of fully naming a
  133. * node.
  134. *
  135. * Certain well known named nodes are just the simple name string.
  136. *
  137. * Actual devices have an address specifier appended to the base name
  138. * string, like this "foo@addr". The "addr" can be in any number of
  139. * formats, and the platform plus the type of the node determine the
  140. * format and how it is constructed.
  141. *
  142. * For children of the ROOT node, the naming convention is fixed and
  143. * determined by whether this is a sun4u or sun4v system.
  144. *
  145. * For children of other nodes, it is bus type specific. So
  146. * we walk up the tree until we discover a "device_type" property
  147. * we recognize and we go from there.
  148. *
  149. * As an example, the boot device on my workstation has a full path:
  150. *
  151. * /pci@1e,600000/ide@d/disk@0,0:c
  152. */
  153. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  154. {
  155. struct linux_prom64_registers *regs;
  156. struct property *rprop;
  157. u32 high_bits, low_bits, type;
  158. rprop = of_find_property(dp, "reg", NULL);
  159. if (!rprop)
  160. return;
  161. regs = rprop->value;
  162. if (!is_root_node(dp->parent)) {
  163. sprintf(tmp_buf, "%s@%x,%x",
  164. dp->name,
  165. (unsigned int) (regs->phys_addr >> 32UL),
  166. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  167. return;
  168. }
  169. type = regs->phys_addr >> 60UL;
  170. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  171. low_bits = (regs->phys_addr & 0xffffffffUL);
  172. if (type == 0 || type == 8) {
  173. const char *prefix = (type == 0) ? "m" : "i";
  174. if (low_bits)
  175. sprintf(tmp_buf, "%s@%s%x,%x",
  176. dp->name, prefix,
  177. high_bits, low_bits);
  178. else
  179. sprintf(tmp_buf, "%s@%s%x",
  180. dp->name,
  181. prefix,
  182. high_bits);
  183. } else if (type == 12) {
  184. sprintf(tmp_buf, "%s@%x",
  185. dp->name, high_bits);
  186. }
  187. }
  188. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  189. {
  190. struct linux_prom64_registers *regs;
  191. struct property *prop;
  192. prop = of_find_property(dp, "reg", NULL);
  193. if (!prop)
  194. return;
  195. regs = prop->value;
  196. if (!is_root_node(dp->parent)) {
  197. sprintf(tmp_buf, "%s@%x,%x",
  198. dp->name,
  199. (unsigned int) (regs->phys_addr >> 32UL),
  200. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  201. return;
  202. }
  203. prop = of_find_property(dp, "upa-portid", NULL);
  204. if (!prop)
  205. prop = of_find_property(dp, "portid", NULL);
  206. if (prop) {
  207. unsigned long mask = 0xffffffffUL;
  208. if (tlb_type >= cheetah)
  209. mask = 0x7fffff;
  210. sprintf(tmp_buf, "%s@%x,%x",
  211. dp->name,
  212. *(u32 *)prop->value,
  213. (unsigned int) (regs->phys_addr & mask));
  214. }
  215. }
  216. /* "name@slot,offset" */
  217. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  218. {
  219. struct linux_prom_registers *regs;
  220. struct property *prop;
  221. prop = of_find_property(dp, "reg", NULL);
  222. if (!prop)
  223. return;
  224. regs = prop->value;
  225. sprintf(tmp_buf, "%s@%x,%x",
  226. dp->name,
  227. regs->which_io,
  228. regs->phys_addr);
  229. }
  230. /* "name@devnum[,func]" */
  231. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  232. {
  233. struct linux_prom_pci_registers *regs;
  234. struct property *prop;
  235. unsigned int devfn;
  236. prop = of_find_property(dp, "reg", NULL);
  237. if (!prop)
  238. return;
  239. regs = prop->value;
  240. devfn = (regs->phys_hi >> 8) & 0xff;
  241. if (devfn & 0x07) {
  242. sprintf(tmp_buf, "%s@%x,%x",
  243. dp->name,
  244. devfn >> 3,
  245. devfn & 0x07);
  246. } else {
  247. sprintf(tmp_buf, "%s@%x",
  248. dp->name,
  249. devfn >> 3);
  250. }
  251. }
  252. /* "name@UPA_PORTID,offset" */
  253. static void __init upa_path_component(struct device_node *dp, char *tmp_buf)
  254. {
  255. struct linux_prom64_registers *regs;
  256. struct property *prop;
  257. prop = of_find_property(dp, "reg", NULL);
  258. if (!prop)
  259. return;
  260. regs = prop->value;
  261. prop = of_find_property(dp, "upa-portid", NULL);
  262. if (!prop)
  263. return;
  264. sprintf(tmp_buf, "%s@%x,%x",
  265. dp->name,
  266. *(u32 *) prop->value,
  267. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  268. }
  269. /* "name@reg" */
  270. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  271. {
  272. struct property *prop;
  273. u32 *regs;
  274. prop = of_find_property(dp, "reg", NULL);
  275. if (!prop)
  276. return;
  277. regs = prop->value;
  278. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  279. }
  280. /* "name@addrhi,addrlo" */
  281. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  282. {
  283. struct linux_prom64_registers *regs;
  284. struct property *prop;
  285. prop = of_find_property(dp, "reg", NULL);
  286. if (!prop)
  287. return;
  288. regs = prop->value;
  289. sprintf(tmp_buf, "%s@%x,%x",
  290. dp->name,
  291. (unsigned int) (regs->phys_addr >> 32UL),
  292. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  293. }
  294. /* "name@bus,addr" */
  295. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  296. {
  297. struct property *prop;
  298. u32 *regs;
  299. prop = of_find_property(dp, "reg", NULL);
  300. if (!prop)
  301. return;
  302. regs = prop->value;
  303. /* This actually isn't right... should look at the #address-cells
  304. * property of the i2c bus node etc. etc.
  305. */
  306. sprintf(tmp_buf, "%s@%x,%x",
  307. dp->name, regs[0], regs[1]);
  308. }
  309. /* "name@reg0[,reg1]" */
  310. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  311. {
  312. struct property *prop;
  313. u32 *regs;
  314. prop = of_find_property(dp, "reg", NULL);
  315. if (!prop)
  316. return;
  317. regs = prop->value;
  318. if (prop->length == sizeof(u32) || regs[1] == 1) {
  319. sprintf(tmp_buf, "%s@%x",
  320. dp->name, regs[0]);
  321. } else {
  322. sprintf(tmp_buf, "%s@%x,%x",
  323. dp->name, regs[0], regs[1]);
  324. }
  325. }
  326. /* "name@reg0reg1[,reg2reg3]" */
  327. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  328. {
  329. struct property *prop;
  330. u32 *regs;
  331. prop = of_find_property(dp, "reg", NULL);
  332. if (!prop)
  333. return;
  334. regs = prop->value;
  335. if (regs[2] || regs[3]) {
  336. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  337. dp->name, regs[0], regs[1], regs[2], regs[3]);
  338. } else {
  339. sprintf(tmp_buf, "%s@%08x%08x",
  340. dp->name, regs[0], regs[1]);
  341. }
  342. }
  343. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  344. {
  345. struct device_node *parent = dp->parent;
  346. if (parent != NULL) {
  347. if (!strcmp(parent->type, "pci") ||
  348. !strcmp(parent->type, "pciex"))
  349. return pci_path_component(dp, tmp_buf);
  350. if (!strcmp(parent->type, "sbus"))
  351. return sbus_path_component(dp, tmp_buf);
  352. if (!strcmp(parent->type, "upa"))
  353. return upa_path_component(dp, tmp_buf);
  354. if (!strcmp(parent->type, "ebus"))
  355. return ebus_path_component(dp, tmp_buf);
  356. if (!strcmp(parent->name, "usb") ||
  357. !strcmp(parent->name, "hub"))
  358. return usb_path_component(dp, tmp_buf);
  359. if (!strcmp(parent->type, "i2c"))
  360. return i2c_path_component(dp, tmp_buf);
  361. if (!strcmp(parent->type, "firewire"))
  362. return ieee1394_path_component(dp, tmp_buf);
  363. if (!strcmp(parent->type, "virtual-devices"))
  364. return vdev_path_component(dp, tmp_buf);
  365. /* "isa" is handled with platform naming */
  366. }
  367. /* Use platform naming convention. */
  368. if (tlb_type == hypervisor)
  369. return sun4v_path_component(dp, tmp_buf);
  370. else
  371. return sun4u_path_component(dp, tmp_buf);
  372. }
  373. static char * __init build_path_component(struct device_node *dp)
  374. {
  375. char tmp_buf[64], *n;
  376. tmp_buf[0] = '\0';
  377. __build_path_component(dp, tmp_buf);
  378. if (tmp_buf[0] == '\0')
  379. strcpy(tmp_buf, dp->name);
  380. n = prom_early_alloc(strlen(tmp_buf) + 1);
  381. strcpy(n, tmp_buf);
  382. return n;
  383. }
  384. static char * __init build_full_name(struct device_node *dp)
  385. {
  386. int len, ourlen, plen;
  387. char *n;
  388. plen = strlen(dp->parent->full_name);
  389. ourlen = strlen(dp->path_component_name);
  390. len = ourlen + plen + 2;
  391. n = prom_early_alloc(len);
  392. strcpy(n, dp->parent->full_name);
  393. if (!is_root_node(dp->parent)) {
  394. strcpy(n + plen, "/");
  395. plen++;
  396. }
  397. strcpy(n + plen, dp->path_component_name);
  398. return n;
  399. }
  400. static struct property * __init build_one_prop(phandle node, char *prev)
  401. {
  402. static struct property *tmp = NULL;
  403. struct property *p;
  404. if (tmp) {
  405. p = tmp;
  406. memset(p, 0, sizeof(*p) + 32);
  407. tmp = NULL;
  408. } else
  409. p = prom_early_alloc(sizeof(struct property) + 32);
  410. p->name = (char *) (p + 1);
  411. if (prev == NULL) {
  412. prom_firstprop(node, p->name);
  413. } else {
  414. prom_nextprop(node, prev, p->name);
  415. }
  416. if (strlen(p->name) == 0) {
  417. tmp = p;
  418. return NULL;
  419. }
  420. p->length = prom_getproplen(node, p->name);
  421. if (p->length <= 0) {
  422. p->length = 0;
  423. } else {
  424. p->value = prom_early_alloc(p->length);
  425. prom_getproperty(node, p->name, p->value, p->length);
  426. }
  427. return p;
  428. }
  429. static struct property * __init build_prop_list(phandle node)
  430. {
  431. struct property *head, *tail;
  432. head = tail = build_one_prop(node, NULL);
  433. while(tail) {
  434. tail->next = build_one_prop(node, tail->name);
  435. tail = tail->next;
  436. }
  437. return head;
  438. }
  439. static char * __init get_one_property(phandle node, const char *name)
  440. {
  441. char *buf = "<NULL>";
  442. int len;
  443. len = prom_getproplen(node, name);
  444. if (len > 0) {
  445. buf = prom_early_alloc(len);
  446. prom_getproperty(node, name, buf, len);
  447. }
  448. return buf;
  449. }
  450. static struct device_node * __init create_node(phandle node)
  451. {
  452. struct device_node *dp;
  453. if (!node)
  454. return NULL;
  455. dp = prom_early_alloc(sizeof(*dp));
  456. kref_init(&dp->kref);
  457. dp->name = get_one_property(node, "name");
  458. dp->type = get_one_property(node, "device_type");
  459. dp->node = node;
  460. /* Build interrupts later... */
  461. dp->properties = build_prop_list(node);
  462. return dp;
  463. }
  464. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  465. {
  466. struct device_node *dp;
  467. dp = create_node(node);
  468. if (dp) {
  469. *(*nextp) = dp;
  470. *nextp = &dp->allnext;
  471. dp->parent = parent;
  472. dp->path_component_name = build_path_component(dp);
  473. dp->full_name = build_full_name(dp);
  474. dp->child = build_tree(dp, prom_getchild(node), nextp);
  475. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  476. }
  477. return dp;
  478. }
  479. void __init prom_build_devicetree(void)
  480. {
  481. struct device_node **nextp;
  482. allnodes = create_node(prom_root_node);
  483. allnodes->path_component_name = "";
  484. allnodes->full_name = "/";
  485. nextp = &allnodes->allnext;
  486. allnodes->child = build_tree(allnodes,
  487. prom_getchild(allnodes->node),
  488. &nextp);
  489. printk("PROM: Built device tree with %u bytes of memory.\n",
  490. prom_early_allocated);
  491. }