prom_32.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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. #include "prom.h"
  26. static unsigned int prom_early_allocated;
  27. void * __init prom_early_alloc(unsigned long size)
  28. {
  29. void *ret;
  30. ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
  31. if (ret != NULL)
  32. memset(ret, 0, size);
  33. prom_early_allocated += size;
  34. return ret;
  35. }
  36. static int is_root_node(const struct device_node *dp)
  37. {
  38. if (!dp)
  39. return 0;
  40. return (dp->parent == NULL);
  41. }
  42. /* The following routines deal with the black magic of fully naming a
  43. * node.
  44. *
  45. * Certain well known named nodes are just the simple name string.
  46. *
  47. * Actual devices have an address specifier appended to the base name
  48. * string, like this "foo@addr". The "addr" can be in any number of
  49. * formats, and the platform plus the type of the node determine the
  50. * format and how it is constructed.
  51. *
  52. * For children of the ROOT node, the naming convention is fixed and
  53. * determined by whether this is a sun4u or sun4v system.
  54. *
  55. * For children of other nodes, it is bus type specific. So
  56. * we walk up the tree until we discover a "device_type" property
  57. * we recognize and we go from there.
  58. */
  59. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  60. {
  61. struct linux_prom_registers *regs;
  62. struct property *rprop;
  63. rprop = of_find_property(dp, "reg", NULL);
  64. if (!rprop)
  65. return;
  66. regs = rprop->value;
  67. sprintf(tmp_buf, "%s@%x,%x",
  68. dp->name,
  69. regs->which_io, regs->phys_addr);
  70. }
  71. /* "name@slot,offset" */
  72. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  73. {
  74. struct linux_prom_registers *regs;
  75. struct property *prop;
  76. prop = of_find_property(dp, "reg", NULL);
  77. if (!prop)
  78. return;
  79. regs = prop->value;
  80. sprintf(tmp_buf, "%s@%x,%x",
  81. dp->name,
  82. regs->which_io,
  83. regs->phys_addr);
  84. }
  85. /* "name@devnum[,func]" */
  86. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  87. {
  88. struct linux_prom_pci_registers *regs;
  89. struct property *prop;
  90. unsigned int devfn;
  91. prop = of_find_property(dp, "reg", NULL);
  92. if (!prop)
  93. return;
  94. regs = prop->value;
  95. devfn = (regs->phys_hi >> 8) & 0xff;
  96. if (devfn & 0x07) {
  97. sprintf(tmp_buf, "%s@%x,%x",
  98. dp->name,
  99. devfn >> 3,
  100. devfn & 0x07);
  101. } else {
  102. sprintf(tmp_buf, "%s@%x",
  103. dp->name,
  104. devfn >> 3);
  105. }
  106. }
  107. /* "name@addrhi,addrlo" */
  108. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  109. {
  110. struct linux_prom_registers *regs;
  111. struct property *prop;
  112. prop = of_find_property(dp, "reg", NULL);
  113. if (!prop)
  114. return;
  115. regs = prop->value;
  116. sprintf(tmp_buf, "%s@%x,%x",
  117. dp->name,
  118. regs->which_io, regs->phys_addr);
  119. }
  120. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  121. {
  122. struct device_node *parent = dp->parent;
  123. if (parent != NULL) {
  124. if (!strcmp(parent->type, "pci") ||
  125. !strcmp(parent->type, "pciex"))
  126. return pci_path_component(dp, tmp_buf);
  127. if (!strcmp(parent->type, "sbus"))
  128. return sbus_path_component(dp, tmp_buf);
  129. if (!strcmp(parent->type, "ebus"))
  130. return ebus_path_component(dp, tmp_buf);
  131. /* "isa" is handled with platform naming */
  132. }
  133. /* Use platform naming convention. */
  134. return sparc32_path_component(dp, tmp_buf);
  135. }
  136. static char * __init build_path_component(struct device_node *dp)
  137. {
  138. char tmp_buf[64], *n;
  139. tmp_buf[0] = '\0';
  140. __build_path_component(dp, tmp_buf);
  141. if (tmp_buf[0] == '\0')
  142. strcpy(tmp_buf, dp->name);
  143. n = prom_early_alloc(strlen(tmp_buf) + 1);
  144. strcpy(n, tmp_buf);
  145. return n;
  146. }
  147. static char * __init build_full_name(struct device_node *dp)
  148. {
  149. int len, ourlen, plen;
  150. char *n;
  151. plen = strlen(dp->parent->full_name);
  152. ourlen = strlen(dp->path_component_name);
  153. len = ourlen + plen + 2;
  154. n = prom_early_alloc(len);
  155. strcpy(n, dp->parent->full_name);
  156. if (!is_root_node(dp->parent)) {
  157. strcpy(n + plen, "/");
  158. plen++;
  159. }
  160. strcpy(n + plen, dp->path_component_name);
  161. return n;
  162. }
  163. static struct device_node * __init build_tree(struct device_node *parent, phandle node, struct device_node ***nextp)
  164. {
  165. struct device_node *ret = NULL, *prev_sibling = NULL;
  166. struct device_node *dp;
  167. while (1) {
  168. dp = create_node(node, parent);
  169. if (!dp)
  170. break;
  171. if (prev_sibling)
  172. prev_sibling->sibling = dp;
  173. if (!ret)
  174. ret = dp;
  175. prev_sibling = dp;
  176. *(*nextp) = dp;
  177. *nextp = &dp->allnext;
  178. dp->path_component_name = build_path_component(dp);
  179. dp->full_name = build_full_name(dp);
  180. dp->child = build_tree(dp, prom_getchild(node), nextp);
  181. node = prom_getsibling(node);
  182. }
  183. return ret;
  184. }
  185. struct device_node *of_console_device;
  186. EXPORT_SYMBOL(of_console_device);
  187. char *of_console_path;
  188. EXPORT_SYMBOL(of_console_path);
  189. char *of_console_options;
  190. EXPORT_SYMBOL(of_console_options);
  191. extern void restore_current(void);
  192. static void __init of_console_init(void)
  193. {
  194. char *msg = "OF stdout device is: %s\n";
  195. struct device_node *dp;
  196. unsigned long flags;
  197. const char *type;
  198. phandle node;
  199. int skip, tmp, fd;
  200. of_console_path = prom_early_alloc(256);
  201. switch (prom_vers) {
  202. case PROM_V0:
  203. skip = 0;
  204. switch (*romvec->pv_stdout) {
  205. case PROMDEV_SCREEN:
  206. type = "display";
  207. break;
  208. case PROMDEV_TTYB:
  209. skip = 1;
  210. /* FALLTHRU */
  211. case PROMDEV_TTYA:
  212. type = "serial";
  213. break;
  214. default:
  215. prom_printf("Invalid PROM_V0 stdout value %u\n",
  216. *romvec->pv_stdout);
  217. prom_halt();
  218. }
  219. tmp = skip;
  220. for_each_node_by_type(dp, type) {
  221. if (!tmp--)
  222. break;
  223. }
  224. if (!dp) {
  225. prom_printf("Cannot find PROM_V0 console node.\n");
  226. prom_halt();
  227. }
  228. of_console_device = dp;
  229. strcpy(of_console_path, dp->full_name);
  230. if (!strcmp(type, "serial")) {
  231. strcat(of_console_path,
  232. (skip ? ":b" : ":a"));
  233. }
  234. break;
  235. default:
  236. case PROM_V2:
  237. case PROM_V3:
  238. fd = *romvec->pv_v2bootargs.fd_stdout;
  239. spin_lock_irqsave(&prom_lock, flags);
  240. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  241. restore_current();
  242. spin_unlock_irqrestore(&prom_lock, flags);
  243. if (!node) {
  244. prom_printf("Cannot resolve stdout node from "
  245. "instance %08x.\n", fd);
  246. prom_halt();
  247. }
  248. dp = of_find_node_by_phandle(node);
  249. type = of_get_property(dp, "device_type", NULL);
  250. if (!type) {
  251. prom_printf("Console stdout lacks "
  252. "device_type property.\n");
  253. prom_halt();
  254. }
  255. if (strcmp(type, "display") && strcmp(type, "serial")) {
  256. prom_printf("Console device_type is neither display "
  257. "nor serial.\n");
  258. prom_halt();
  259. }
  260. of_console_device = dp;
  261. if (prom_vers == PROM_V2) {
  262. strcpy(of_console_path, dp->full_name);
  263. switch (*romvec->pv_stdout) {
  264. case PROMDEV_TTYA:
  265. strcat(of_console_path, ":a");
  266. break;
  267. case PROMDEV_TTYB:
  268. strcat(of_console_path, ":b");
  269. break;
  270. }
  271. } else {
  272. const char *path;
  273. dp = of_find_node_by_path("/");
  274. path = of_get_property(dp, "stdout-path", NULL);
  275. if (!path) {
  276. prom_printf("No stdout-path in root node.\n");
  277. prom_halt();
  278. }
  279. strcpy(of_console_path, path);
  280. }
  281. break;
  282. }
  283. of_console_options = strrchr(of_console_path, ':');
  284. if (of_console_options) {
  285. of_console_options++;
  286. if (*of_console_options == '\0')
  287. of_console_options = NULL;
  288. }
  289. prom_printf(msg, of_console_path);
  290. printk(msg, of_console_path);
  291. }
  292. void __init prom_build_devicetree(void)
  293. {
  294. struct device_node **nextp;
  295. allnodes = create_node(prom_root_node, NULL);
  296. allnodes->path_component_name = "";
  297. allnodes->full_name = "/";
  298. nextp = &allnodes->allnext;
  299. allnodes->child = build_tree(allnodes,
  300. prom_getchild(allnodes->node),
  301. &nextp);
  302. of_console_init();
  303. printk("PROM: Built device tree with %u bytes of memory.\n",
  304. prom_early_allocated);
  305. }