prom.c 10 KB

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