prom.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541
  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. int of_getintprop_default(struct device_node *np, const char *name, int def)
  140. {
  141. struct property *prop;
  142. int len;
  143. prop = of_find_property(np, name, &len);
  144. if (!prop || len != 4)
  145. return def;
  146. return *(int *) prop->value;
  147. }
  148. EXPORT_SYMBOL(of_getintprop_default);
  149. int of_set_property(struct device_node *dp, const char *name, void *val, int len)
  150. {
  151. struct property **prevp;
  152. void *new_val;
  153. int err;
  154. new_val = kmalloc(len, GFP_KERNEL);
  155. if (!new_val)
  156. return -ENOMEM;
  157. memcpy(new_val, val, len);
  158. err = -ENODEV;
  159. write_lock(&devtree_lock);
  160. prevp = &dp->properties;
  161. while (*prevp) {
  162. struct property *prop = *prevp;
  163. if (!strcasecmp(prop->name, name)) {
  164. void *old_val = prop->value;
  165. int ret;
  166. ret = prom_setprop(dp->node, (char *) name, val, len);
  167. err = -EINVAL;
  168. if (ret >= 0) {
  169. prop->value = new_val;
  170. prop->length = len;
  171. if (OF_IS_DYNAMIC(prop))
  172. kfree(old_val);
  173. OF_MARK_DYNAMIC(prop);
  174. err = 0;
  175. }
  176. break;
  177. }
  178. prevp = &(*prevp)->next;
  179. }
  180. write_unlock(&devtree_lock);
  181. /* XXX Upate procfs if necessary... */
  182. return err;
  183. }
  184. EXPORT_SYMBOL(of_set_property);
  185. static unsigned int prom_early_allocated;
  186. static void * __init prom_early_alloc(unsigned long size)
  187. {
  188. void *ret;
  189. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  190. if (ret != NULL)
  191. memset(ret, 0, size);
  192. prom_early_allocated += size;
  193. return ret;
  194. }
  195. static int is_root_node(const struct device_node *dp)
  196. {
  197. if (!dp)
  198. return 0;
  199. return (dp->parent == NULL);
  200. }
  201. /* The following routines deal with the black magic of fully naming a
  202. * node.
  203. *
  204. * Certain well known named nodes are just the simple name string.
  205. *
  206. * Actual devices have an address specifier appended to the base name
  207. * string, like this "foo@addr". The "addr" can be in any number of
  208. * formats, and the platform plus the type of the node determine the
  209. * format and how it is constructed.
  210. *
  211. * For children of the ROOT node, the naming convention is fixed and
  212. * determined by whether this is a sun4u or sun4v system.
  213. *
  214. * For children of other nodes, it is bus type specific. So
  215. * we walk up the tree until we discover a "device_type" property
  216. * we recognize and we go from there.
  217. */
  218. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  219. {
  220. struct linux_prom_registers *regs;
  221. struct property *rprop;
  222. rprop = of_find_property(dp, "reg", NULL);
  223. if (!rprop)
  224. return;
  225. regs = rprop->value;
  226. sprintf(tmp_buf, "%s@%x,%x",
  227. dp->name,
  228. regs->which_io, regs->phys_addr);
  229. }
  230. /* "name@slot,offset" */
  231. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  232. {
  233. struct linux_prom_registers *regs;
  234. struct property *prop;
  235. prop = of_find_property(dp, "reg", NULL);
  236. if (!prop)
  237. return;
  238. regs = prop->value;
  239. sprintf(tmp_buf, "%s@%x,%x",
  240. dp->name,
  241. regs->which_io,
  242. regs->phys_addr);
  243. }
  244. /* "name@devnum[,func]" */
  245. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  246. {
  247. struct linux_prom_pci_registers *regs;
  248. struct property *prop;
  249. unsigned int devfn;
  250. prop = of_find_property(dp, "reg", NULL);
  251. if (!prop)
  252. return;
  253. regs = prop->value;
  254. devfn = (regs->phys_hi >> 8) & 0xff;
  255. if (devfn & 0x07) {
  256. sprintf(tmp_buf, "%s@%x,%x",
  257. dp->name,
  258. devfn >> 3,
  259. devfn & 0x07);
  260. } else {
  261. sprintf(tmp_buf, "%s@%x",
  262. dp->name,
  263. devfn >> 3);
  264. }
  265. }
  266. /* "name@addrhi,addrlo" */
  267. static void __init ebus_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, regs->phys_addr);
  278. }
  279. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  280. {
  281. struct device_node *parent = dp->parent;
  282. if (parent != NULL) {
  283. if (!strcmp(parent->type, "pci") ||
  284. !strcmp(parent->type, "pciex"))
  285. return pci_path_component(dp, tmp_buf);
  286. if (!strcmp(parent->type, "sbus"))
  287. return sbus_path_component(dp, tmp_buf);
  288. if (!strcmp(parent->type, "ebus"))
  289. return ebus_path_component(dp, tmp_buf);
  290. /* "isa" is handled with platform naming */
  291. }
  292. /* Use platform naming convention. */
  293. return sparc32_path_component(dp, tmp_buf);
  294. }
  295. static char * __init build_path_component(struct device_node *dp)
  296. {
  297. char tmp_buf[64], *n;
  298. tmp_buf[0] = '\0';
  299. __build_path_component(dp, tmp_buf);
  300. if (tmp_buf[0] == '\0')
  301. strcpy(tmp_buf, dp->name);
  302. n = prom_early_alloc(strlen(tmp_buf) + 1);
  303. strcpy(n, tmp_buf);
  304. return n;
  305. }
  306. static char * __init build_full_name(struct device_node *dp)
  307. {
  308. int len, ourlen, plen;
  309. char *n;
  310. plen = strlen(dp->parent->full_name);
  311. ourlen = strlen(dp->path_component_name);
  312. len = ourlen + plen + 2;
  313. n = prom_early_alloc(len);
  314. strcpy(n, dp->parent->full_name);
  315. if (!is_root_node(dp->parent)) {
  316. strcpy(n + plen, "/");
  317. plen++;
  318. }
  319. strcpy(n + plen, dp->path_component_name);
  320. return n;
  321. }
  322. static unsigned int unique_id;
  323. static struct property * __init build_one_prop(phandle node, char *prev, char *special_name, void *special_val, int special_len)
  324. {
  325. static struct property *tmp = NULL;
  326. struct property *p;
  327. int len;
  328. const char *name;
  329. if (tmp) {
  330. p = tmp;
  331. memset(p, 0, sizeof(*p) + 32);
  332. tmp = NULL;
  333. } else {
  334. p = prom_early_alloc(sizeof(struct property) + 32);
  335. p->unique_id = unique_id++;
  336. }
  337. p->name = (char *) (p + 1);
  338. if (special_name) {
  339. strcpy(p->name, special_name);
  340. p->length = special_len;
  341. p->value = prom_early_alloc(special_len);
  342. memcpy(p->value, special_val, special_len);
  343. } else {
  344. if (prev == NULL) {
  345. name = prom_firstprop(node, NULL);
  346. } else {
  347. name = prom_nextprop(node, prev, NULL);
  348. }
  349. if (strlen(name) == 0) {
  350. tmp = p;
  351. return NULL;
  352. }
  353. strcpy(p->name, name);
  354. p->length = prom_getproplen(node, p->name);
  355. if (p->length <= 0) {
  356. p->length = 0;
  357. } else {
  358. p->value = prom_early_alloc(p->length + 1);
  359. len = prom_getproperty(node, p->name, p->value,
  360. p->length);
  361. if (len <= 0)
  362. p->length = 0;
  363. ((unsigned char *)p->value)[p->length] = '\0';
  364. }
  365. }
  366. return p;
  367. }
  368. static struct property * __init build_prop_list(phandle node)
  369. {
  370. struct property *head, *tail;
  371. head = tail = build_one_prop(node, NULL,
  372. ".node", &node, sizeof(node));
  373. tail->next = build_one_prop(node, NULL, NULL, NULL, 0);
  374. tail = tail->next;
  375. while(tail) {
  376. tail->next = build_one_prop(node, tail->name,
  377. NULL, NULL, 0);
  378. tail = tail->next;
  379. }
  380. return head;
  381. }
  382. static char * __init get_one_property(phandle node, char *name)
  383. {
  384. char *buf = "<NULL>";
  385. int len;
  386. len = prom_getproplen(node, name);
  387. if (len > 0) {
  388. buf = prom_early_alloc(len);
  389. len = prom_getproperty(node, name, buf, len);
  390. }
  391. return buf;
  392. }
  393. static struct device_node * __init create_node(phandle node)
  394. {
  395. struct device_node *dp;
  396. if (!node)
  397. return NULL;
  398. dp = prom_early_alloc(sizeof(*dp));
  399. dp->unique_id = unique_id++;
  400. kref_init(&dp->kref);
  401. dp->name = get_one_property(node, "name");
  402. dp->type = get_one_property(node, "device_type");
  403. dp->node = node;
  404. /* Build interrupts later... */
  405. dp->properties = build_prop_list(node);
  406. return dp;
  407. }
  408. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  409. {
  410. struct device_node *dp;
  411. dp = create_node(node);
  412. if (dp) {
  413. *(*nextp) = dp;
  414. *nextp = &dp->allnext;
  415. dp->parent = parent;
  416. dp->path_component_name = build_path_component(dp);
  417. dp->full_name = build_full_name(dp);
  418. dp->child = build_tree(dp, prom_getchild(node), nextp);
  419. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  420. }
  421. return dp;
  422. }
  423. void __init prom_build_devicetree(void)
  424. {
  425. struct device_node **nextp;
  426. allnodes = create_node(prom_root_node);
  427. allnodes->path_component_name = "";
  428. allnodes->full_name = "/";
  429. nextp = &allnodes->allnext;
  430. allnodes->child = build_tree(allnodes,
  431. prom_getchild(allnodes->node),
  432. &nextp);
  433. printk("PROM: Built device tree with %u bytes of memory.\n",
  434. prom_early_allocated);
  435. }