prom.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577
  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. /* 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. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  255. {
  256. struct linux_prom_registers *regs;
  257. struct property *rprop;
  258. rprop = of_find_property(dp, "reg", NULL);
  259. if (!rprop)
  260. return;
  261. regs = rprop->value;
  262. sprintf(tmp_buf, "%s@%x,%x",
  263. dp->name,
  264. regs->which_io, regs->phys_addr);
  265. }
  266. /* "name@slot,offset" */
  267. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  268. {
  269. struct linux_prom_registers *regs;
  270. struct property *prop;
  271. prop = of_find_property(dp, "reg", NULL);
  272. if (!prop)
  273. return;
  274. regs = prop->value;
  275. sprintf(tmp_buf, "%s@%x,%x",
  276. dp->name,
  277. regs->which_io,
  278. regs->phys_addr);
  279. }
  280. /* "name@devnum[,func]" */
  281. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  282. {
  283. struct linux_prom_pci_registers *regs;
  284. struct property *prop;
  285. unsigned int devfn;
  286. prop = of_find_property(dp, "reg", NULL);
  287. if (!prop)
  288. return;
  289. regs = prop->value;
  290. devfn = (regs->phys_hi >> 8) & 0xff;
  291. if (devfn & 0x07) {
  292. sprintf(tmp_buf, "%s@%x,%x",
  293. dp->name,
  294. devfn >> 3,
  295. devfn & 0x07);
  296. } else {
  297. sprintf(tmp_buf, "%s@%x",
  298. dp->name,
  299. devfn >> 3);
  300. }
  301. }
  302. /* "name@addrhi,addrlo" */
  303. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  304. {
  305. struct linux_prom_registers *regs;
  306. struct property *prop;
  307. prop = of_find_property(dp, "reg", NULL);
  308. if (!prop)
  309. return;
  310. regs = prop->value;
  311. sprintf(tmp_buf, "%s@%x,%x",
  312. dp->name,
  313. regs->which_io, regs->phys_addr);
  314. }
  315. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  316. {
  317. struct device_node *parent = dp->parent;
  318. if (parent != NULL) {
  319. if (!strcmp(parent->type, "pci") ||
  320. !strcmp(parent->type, "pciex"))
  321. return pci_path_component(dp, tmp_buf);
  322. if (!strcmp(parent->type, "sbus"))
  323. return sbus_path_component(dp, tmp_buf);
  324. if (!strcmp(parent->type, "ebus"))
  325. return ebus_path_component(dp, tmp_buf);
  326. /* "isa" is handled with platform naming */
  327. }
  328. /* Use platform naming convention. */
  329. return sparc32_path_component(dp, tmp_buf);
  330. }
  331. static char * __init build_path_component(struct device_node *dp)
  332. {
  333. char tmp_buf[64], *n;
  334. tmp_buf[0] = '\0';
  335. __build_path_component(dp, tmp_buf);
  336. if (tmp_buf[0] == '\0')
  337. strcpy(tmp_buf, dp->name);
  338. n = prom_early_alloc(strlen(tmp_buf) + 1);
  339. strcpy(n, tmp_buf);
  340. return n;
  341. }
  342. static char * __init build_full_name(struct device_node *dp)
  343. {
  344. int len, ourlen, plen;
  345. char *n;
  346. plen = strlen(dp->parent->full_name);
  347. ourlen = strlen(dp->path_component_name);
  348. len = ourlen + plen + 2;
  349. n = prom_early_alloc(len);
  350. strcpy(n, dp->parent->full_name);
  351. if (!is_root_node(dp->parent)) {
  352. strcpy(n + plen, "/");
  353. plen++;
  354. }
  355. strcpy(n + plen, dp->path_component_name);
  356. return n;
  357. }
  358. static unsigned int unique_id;
  359. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  360. {
  361. static struct property *tmp = NULL;
  362. struct property *p;
  363. int len;
  364. const char *name;
  365. if (tmp) {
  366. p = tmp;
  367. memset(p, 0, sizeof(*p) + 32);
  368. tmp = NULL;
  369. } else {
  370. p = prom_early_alloc(sizeof(struct property) + 32);
  371. p->unique_id = unique_id++;
  372. }
  373. p->name = (char *) (p + 1);
  374. if (special_name) {
  375. strcpy(p->name, special_name);
  376. p->length = special_len;
  377. p->value = prom_early_alloc(special_len);
  378. memcpy(p->value, special_val, special_len);
  379. } else {
  380. if (prev == NULL) {
  381. name = prom_firstprop(node, NULL);
  382. } else {
  383. name = prom_nextprop(node, prev, NULL);
  384. }
  385. if (strlen(name) == 0) {
  386. tmp = p;
  387. return NULL;
  388. }
  389. strcpy(p->name, name);
  390. p->length = prom_getproplen(node, p->name);
  391. if (p->length <= 0) {
  392. p->length = 0;
  393. } else {
  394. p->value = prom_early_alloc(p->length + 1);
  395. prom_getproperty(node, p->name, p->value, p->length);
  396. ((unsigned char *)p->value)[p->length] = '\0';
  397. }
  398. }
  399. return p;
  400. }
  401. static struct property * __init build_prop_list(phandle node)
  402. {
  403. struct property *head, *tail;
  404. head = tail = build_one_prop(node, NULL,
  405. ".node", &node, sizeof(node));
  406. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  407. tail = tail->next;
  408. while(tail) {
  409. tail->next = build_one_prop(node, tail->name,
  410. NULL, NULL, 0);
  411. tail = tail->next;
  412. }
  413. return head;
  414. }
  415. static char * __init get_one_property(phandle node, char *name)
  416. {
  417. char *buf = "<NULL>";
  418. int len;
  419. len = prom_getproplen(node, name);
  420. if (len > 0) {
  421. buf = prom_early_alloc(len);
  422. len = prom_getproperty(node, name, buf, len);
  423. }
  424. return buf;
  425. }
  426. static struct device_node * __init create_node(phandle node)
  427. {
  428. struct device_node *dp;
  429. if (!node)
  430. return NULL;
  431. dp = prom_early_alloc(sizeof(*dp));
  432. dp->unique_id = unique_id++;
  433. kref_init(&dp->kref);
  434. dp->name = get_one_property(node, "name");
  435. dp->type = get_one_property(node, "device_type");
  436. dp->node = node;
  437. /* Build interrupts later... */
  438. dp->properties = build_prop_list(node);
  439. return dp;
  440. }
  441. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  442. {
  443. struct device_node *dp;
  444. dp = create_node(node);
  445. if (dp) {
  446. *(*nextp) = dp;
  447. *nextp = &dp->allnext;
  448. dp->parent = parent;
  449. dp->path_component_name = build_path_component(dp);
  450. dp->full_name = build_full_name(dp);
  451. dp->child = build_tree(dp, prom_getchild(node), nextp);
  452. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  453. }
  454. return dp;
  455. }
  456. void __init prom_build_devicetree(void)
  457. {
  458. struct device_node **nextp;
  459. allnodes = create_node(prom_root_node);
  460. allnodes->path_component_name = "";
  461. allnodes->full_name = "/";
  462. nextp = &allnodes->allnext;
  463. allnodes->child = build_tree(allnodes,
  464. prom_getchild(allnodes->node),
  465. &nextp);
  466. printk("PROM: Built device tree with %u bytes of memory.\n",
  467. prom_early_allocated);
  468. }