prom.c 12 KB

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