prom.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  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_n_addr_cells(struct device_node *np)
  158. {
  159. int* ip;
  160. do {
  161. if (np->parent)
  162. np = np->parent;
  163. ip = of_get_property(np, "#address-cells", NULL);
  164. if (ip != NULL)
  165. return *ip;
  166. } while (np->parent);
  167. /* No #address-cells property for the root node, default to 2 */
  168. return 2;
  169. }
  170. EXPORT_SYMBOL(of_n_addr_cells);
  171. int of_n_size_cells(struct device_node *np)
  172. {
  173. int* ip;
  174. do {
  175. if (np->parent)
  176. np = np->parent;
  177. ip = of_get_property(np, "#size-cells", NULL);
  178. if (ip != NULL)
  179. return *ip;
  180. } while (np->parent);
  181. /* No #size-cells property for the root node, default to 1 */
  182. return 1;
  183. }
  184. EXPORT_SYMBOL(of_n_size_cells);
  185. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  186. {
  187. struct property **prevp;
  188. void *new_val;
  189. int err;
  190. new_val = kmalloc(len, GFP_KERNEL);
  191. if (!new_val)
  192. return -ENOMEM;
  193. memcpy(new_val, val, len);
  194. err = -ENODEV;
  195. write_lock(&devtree_lock);
  196. prevp = &dp->properties;
  197. while (*prevp) {
  198. struct property *prop = *prevp;
  199. if (!strcmp(prop->name, name)) {
  200. void *old_val = prop->value;
  201. int ret;
  202. ret = prom_setprop(dp->node, name, val, len);
  203. err = -EINVAL;
  204. if (ret >= 0) {
  205. prop->value = new_val;
  206. prop->length = len;
  207. if (OF_IS_DYNAMIC(prop))
  208. kfree(old_val);
  209. OF_MARK_DYNAMIC(prop);
  210. err = 0;
  211. }
  212. break;
  213. }
  214. prevp = &(*prevp)->next;
  215. }
  216. write_unlock(&devtree_lock);
  217. /* XXX Upate procfs if necessary... */
  218. return err;
  219. }
  220. EXPORT_SYMBOL(of_set_property);
  221. static unsigned int prom_early_allocated;
  222. static void * __init prom_early_alloc(unsigned long size)
  223. {
  224. void *ret;
  225. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  226. if (ret != NULL)
  227. memset(ret, 0, size);
  228. prom_early_allocated += size;
  229. return ret;
  230. }
  231. static int is_root_node(const struct device_node *dp)
  232. {
  233. if (!dp)
  234. return 0;
  235. return (dp->parent == NULL);
  236. }
  237. /* The following routines deal with the black magic of fully naming a
  238. * node.
  239. *
  240. * Certain well known named nodes are just the simple name string.
  241. *
  242. * Actual devices have an address specifier appended to the base name
  243. * string, like this "foo@addr". The "addr" can be in any number of
  244. * formats, and the platform plus the type of the node determine the
  245. * format and how it is constructed.
  246. *
  247. * For children of the ROOT node, the naming convention is fixed and
  248. * determined by whether this is a sun4u or sun4v system.
  249. *
  250. * For children of other nodes, it is bus type specific. So
  251. * we walk up the tree until we discover a "device_type" property
  252. * we recognize and we go from there.
  253. *
  254. * As an example, the boot device on my workstation has a full path:
  255. *
  256. * /pci@1e,600000/ide@d/disk@0,0:c
  257. */
  258. static void __init sun4v_path_component(struct device_node *dp, char *tmp_buf)
  259. {
  260. struct linux_prom64_registers *regs;
  261. struct property *rprop;
  262. u32 high_bits, low_bits, type;
  263. rprop = of_find_property(dp, "reg", NULL);
  264. if (!rprop)
  265. return;
  266. regs = rprop->value;
  267. if (!is_root_node(dp->parent)) {
  268. sprintf(tmp_buf, "%s@%x,%x",
  269. dp->name,
  270. (unsigned int) (regs->phys_addr >> 32UL),
  271. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  272. return;
  273. }
  274. type = regs->phys_addr >> 60UL;
  275. high_bits = (regs->phys_addr >> 32UL) & 0x0fffffffUL;
  276. low_bits = (regs->phys_addr & 0xffffffffUL);
  277. if (type == 0 || type == 8) {
  278. const char *prefix = (type == 0) ? "m" : "i";
  279. if (low_bits)
  280. sprintf(tmp_buf, "%s@%s%x,%x",
  281. dp->name, prefix,
  282. high_bits, low_bits);
  283. else
  284. sprintf(tmp_buf, "%s@%s%x",
  285. dp->name,
  286. prefix,
  287. high_bits);
  288. } else if (type == 12) {
  289. sprintf(tmp_buf, "%s@%x",
  290. dp->name, high_bits);
  291. }
  292. }
  293. static void __init sun4u_path_component(struct device_node *dp, char *tmp_buf)
  294. {
  295. struct linux_prom64_registers *regs;
  296. struct property *prop;
  297. prop = of_find_property(dp, "reg", NULL);
  298. if (!prop)
  299. return;
  300. regs = prop->value;
  301. if (!is_root_node(dp->parent)) {
  302. sprintf(tmp_buf, "%s@%x,%x",
  303. dp->name,
  304. (unsigned int) (regs->phys_addr >> 32UL),
  305. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  306. return;
  307. }
  308. prop = of_find_property(dp, "upa-portid", NULL);
  309. if (!prop)
  310. prop = of_find_property(dp, "portid", NULL);
  311. if (prop) {
  312. unsigned long mask = 0xffffffffUL;
  313. if (tlb_type >= cheetah)
  314. mask = 0x7fffff;
  315. sprintf(tmp_buf, "%s@%x,%x",
  316. dp->name,
  317. *(u32 *)prop->value,
  318. (unsigned int) (regs->phys_addr & mask));
  319. }
  320. }
  321. /* "name@slot,offset" */
  322. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  323. {
  324. struct linux_prom_registers *regs;
  325. struct property *prop;
  326. prop = of_find_property(dp, "reg", NULL);
  327. if (!prop)
  328. return;
  329. regs = prop->value;
  330. sprintf(tmp_buf, "%s@%x,%x",
  331. dp->name,
  332. regs->which_io,
  333. regs->phys_addr);
  334. }
  335. /* "name@devnum[,func]" */
  336. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  337. {
  338. struct linux_prom_pci_registers *regs;
  339. struct property *prop;
  340. unsigned int devfn;
  341. prop = of_find_property(dp, "reg", NULL);
  342. if (!prop)
  343. return;
  344. regs = prop->value;
  345. devfn = (regs->phys_hi >> 8) & 0xff;
  346. if (devfn & 0x07) {
  347. sprintf(tmp_buf, "%s@%x,%x",
  348. dp->name,
  349. devfn >> 3,
  350. devfn & 0x07);
  351. } else {
  352. sprintf(tmp_buf, "%s@%x",
  353. dp->name,
  354. devfn >> 3);
  355. }
  356. }
  357. /* "name@UPA_PORTID,offset" */
  358. static void __init upa_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. prop = of_find_property(dp, "upa-portid", NULL);
  367. if (!prop)
  368. return;
  369. sprintf(tmp_buf, "%s@%x,%x",
  370. dp->name,
  371. *(u32 *) prop->value,
  372. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  373. }
  374. /* "name@reg" */
  375. static void __init vdev_path_component(struct device_node *dp, char *tmp_buf)
  376. {
  377. struct property *prop;
  378. u32 *regs;
  379. prop = of_find_property(dp, "reg", NULL);
  380. if (!prop)
  381. return;
  382. regs = prop->value;
  383. sprintf(tmp_buf, "%s@%x", dp->name, *regs);
  384. }
  385. /* "name@addrhi,addrlo" */
  386. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  387. {
  388. struct linux_prom64_registers *regs;
  389. struct property *prop;
  390. prop = of_find_property(dp, "reg", NULL);
  391. if (!prop)
  392. return;
  393. regs = prop->value;
  394. sprintf(tmp_buf, "%s@%x,%x",
  395. dp->name,
  396. (unsigned int) (regs->phys_addr >> 32UL),
  397. (unsigned int) (regs->phys_addr & 0xffffffffUL));
  398. }
  399. /* "name@bus,addr" */
  400. static void __init i2c_path_component(struct device_node *dp, char *tmp_buf)
  401. {
  402. struct property *prop;
  403. u32 *regs;
  404. prop = of_find_property(dp, "reg", NULL);
  405. if (!prop)
  406. return;
  407. regs = prop->value;
  408. /* This actually isn't right... should look at the #address-cells
  409. * property of the i2c bus node etc. etc.
  410. */
  411. sprintf(tmp_buf, "%s@%x,%x",
  412. dp->name, regs[0], regs[1]);
  413. }
  414. /* "name@reg0[,reg1]" */
  415. static void __init usb_path_component(struct device_node *dp, char *tmp_buf)
  416. {
  417. struct property *prop;
  418. u32 *regs;
  419. prop = of_find_property(dp, "reg", NULL);
  420. if (!prop)
  421. return;
  422. regs = prop->value;
  423. if (prop->length == sizeof(u32) || regs[1] == 1) {
  424. sprintf(tmp_buf, "%s@%x",
  425. dp->name, regs[0]);
  426. } else {
  427. sprintf(tmp_buf, "%s@%x,%x",
  428. dp->name, regs[0], regs[1]);
  429. }
  430. }
  431. /* "name@reg0reg1[,reg2reg3]" */
  432. static void __init ieee1394_path_component(struct device_node *dp, char *tmp_buf)
  433. {
  434. struct property *prop;
  435. u32 *regs;
  436. prop = of_find_property(dp, "reg", NULL);
  437. if (!prop)
  438. return;
  439. regs = prop->value;
  440. if (regs[2] || regs[3]) {
  441. sprintf(tmp_buf, "%s@%08x%08x,%04x%08x",
  442. dp->name, regs[0], regs[1], regs[2], regs[3]);
  443. } else {
  444. sprintf(tmp_buf, "%s@%08x%08x",
  445. dp->name, regs[0], regs[1]);
  446. }
  447. }
  448. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  449. {
  450. struct device_node *parent = dp->parent;
  451. if (parent != NULL) {
  452. if (!strcmp(parent->type, "pci") ||
  453. !strcmp(parent->type, "pciex"))
  454. return pci_path_component(dp, tmp_buf);
  455. if (!strcmp(parent->type, "sbus"))
  456. return sbus_path_component(dp, tmp_buf);
  457. if (!strcmp(parent->type, "upa"))
  458. return upa_path_component(dp, tmp_buf);
  459. if (!strcmp(parent->type, "ebus"))
  460. return ebus_path_component(dp, tmp_buf);
  461. if (!strcmp(parent->name, "usb") ||
  462. !strcmp(parent->name, "hub"))
  463. return usb_path_component(dp, tmp_buf);
  464. if (!strcmp(parent->type, "i2c"))
  465. return i2c_path_component(dp, tmp_buf);
  466. if (!strcmp(parent->type, "firewire"))
  467. return ieee1394_path_component(dp, tmp_buf);
  468. if (!strcmp(parent->type, "virtual-devices"))
  469. return vdev_path_component(dp, tmp_buf);
  470. /* "isa" is handled with platform naming */
  471. }
  472. /* Use platform naming convention. */
  473. if (tlb_type == hypervisor)
  474. return sun4v_path_component(dp, tmp_buf);
  475. else
  476. return sun4u_path_component(dp, tmp_buf);
  477. }
  478. static char * __init build_path_component(struct device_node *dp)
  479. {
  480. char tmp_buf[64], *n;
  481. tmp_buf[0] = '\0';
  482. __build_path_component(dp, tmp_buf);
  483. if (tmp_buf[0] == '\0')
  484. strcpy(tmp_buf, dp->name);
  485. n = prom_early_alloc(strlen(tmp_buf) + 1);
  486. strcpy(n, tmp_buf);
  487. return n;
  488. }
  489. static char * __init build_full_name(struct device_node *dp)
  490. {
  491. int len, ourlen, plen;
  492. char *n;
  493. plen = strlen(dp->parent->full_name);
  494. ourlen = strlen(dp->path_component_name);
  495. len = ourlen + plen + 2;
  496. n = prom_early_alloc(len);
  497. strcpy(n, dp->parent->full_name);
  498. if (!is_root_node(dp->parent)) {
  499. strcpy(n + plen, "/");
  500. plen++;
  501. }
  502. strcpy(n + plen, dp->path_component_name);
  503. return n;
  504. }
  505. static unsigned int unique_id;
  506. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  507. {
  508. static struct property *tmp = NULL;
  509. struct property *p;
  510. if (tmp) {
  511. p = tmp;
  512. memset(p, 0, sizeof(*p) + 32);
  513. tmp = NULL;
  514. } else {
  515. p = prom_early_alloc(sizeof(struct property) + 32);
  516. p->unique_id = unique_id++;
  517. }
  518. p->name = (char *) (p + 1);
  519. if (special_name) {
  520. strcpy(p->name, special_name);
  521. p->length = special_len;
  522. p->value = prom_early_alloc(special_len);
  523. memcpy(p->value, special_val, special_len);
  524. } else {
  525. if (prev == NULL) {
  526. prom_firstprop(node, p->name);
  527. } else {
  528. prom_nextprop(node, prev, p->name);
  529. }
  530. if (strlen(p->name) == 0) {
  531. tmp = p;
  532. return NULL;
  533. }
  534. p->length = prom_getproplen(node, p->name);
  535. if (p->length <= 0) {
  536. p->length = 0;
  537. } else {
  538. p->value = prom_early_alloc(p->length + 1);
  539. prom_getproperty(node, p->name, p->value, p->length);
  540. ((unsigned char *)p->value)[p->length] = '\0';
  541. }
  542. }
  543. return p;
  544. }
  545. static struct property * __init build_prop_list(phandle node)
  546. {
  547. struct property *head, *tail;
  548. head = tail = build_one_prop(node, NULL,
  549. ".node", &node, sizeof(node));
  550. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  551. tail = tail->next;
  552. while(tail) {
  553. tail->next = build_one_prop(node, tail->name,
  554. NULL, NULL, 0);
  555. tail = tail->next;
  556. }
  557. return head;
  558. }
  559. static char * __init get_one_property(phandle node, const char *name)
  560. {
  561. char *buf = "<NULL>";
  562. int len;
  563. len = prom_getproplen(node, name);
  564. if (len > 0) {
  565. buf = prom_early_alloc(len);
  566. prom_getproperty(node, name, buf, len);
  567. }
  568. return buf;
  569. }
  570. static struct device_node * __init create_node(phandle node)
  571. {
  572. struct device_node *dp;
  573. if (!node)
  574. return NULL;
  575. dp = prom_early_alloc(sizeof(*dp));
  576. dp->unique_id = unique_id++;
  577. kref_init(&dp->kref);
  578. dp->name = get_one_property(node, "name");
  579. dp->type = get_one_property(node, "device_type");
  580. dp->node = node;
  581. /* Build interrupts later... */
  582. dp->properties = build_prop_list(node);
  583. return dp;
  584. }
  585. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  586. {
  587. struct device_node *dp;
  588. dp = create_node(node);
  589. if (dp) {
  590. *(*nextp) = dp;
  591. *nextp = &dp->allnext;
  592. dp->parent = parent;
  593. dp->path_component_name = build_path_component(dp);
  594. dp->full_name = build_full_name(dp);
  595. dp->child = build_tree(dp, prom_getchild(node), nextp);
  596. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  597. }
  598. return dp;
  599. }
  600. void __init prom_build_devicetree(void)
  601. {
  602. struct device_node **nextp;
  603. allnodes = create_node(prom_root_node);
  604. allnodes->path_component_name = "";
  605. allnodes->full_name = "/";
  606. nextp = &allnodes->allnext;
  607. allnodes->child = build_tree(allnodes,
  608. prom_getchild(allnodes->node),
  609. &nextp);
  610. printk("PROM: Built device tree with %u bytes of memory.\n",
  611. prom_early_allocated);
  612. }