prom.c 12 KB

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