prom.c 12 KB

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