prom.c 16 KB

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