prom.c 14 KB

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