prom_32.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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 *dp;
  166. dp = create_node(node, parent);
  167. if (dp) {
  168. *(*nextp) = dp;
  169. *nextp = &dp->allnext;
  170. dp->parent = parent;
  171. dp->path_component_name = build_path_component(dp);
  172. dp->full_name = build_full_name(dp);
  173. dp->child = build_tree(dp, prom_getchild(node), nextp);
  174. dp->sibling = build_tree(parent, prom_getsibling(node), nextp);
  175. }
  176. return dp;
  177. }
  178. struct device_node *of_console_device;
  179. EXPORT_SYMBOL(of_console_device);
  180. char *of_console_path;
  181. EXPORT_SYMBOL(of_console_path);
  182. char *of_console_options;
  183. EXPORT_SYMBOL(of_console_options);
  184. extern void restore_current(void);
  185. static void __init of_console_init(void)
  186. {
  187. char *msg = "OF stdout device is: %s\n";
  188. struct device_node *dp;
  189. unsigned long flags;
  190. const char *type;
  191. phandle node;
  192. int skip, tmp, fd;
  193. of_console_path = prom_early_alloc(256);
  194. switch (prom_vers) {
  195. case PROM_V0:
  196. skip = 0;
  197. switch (*romvec->pv_stdout) {
  198. case PROMDEV_SCREEN:
  199. type = "display";
  200. break;
  201. case PROMDEV_TTYB:
  202. skip = 1;
  203. /* FALLTHRU */
  204. case PROMDEV_TTYA:
  205. type = "serial";
  206. break;
  207. default:
  208. prom_printf("Invalid PROM_V0 stdout value %u\n",
  209. *romvec->pv_stdout);
  210. prom_halt();
  211. }
  212. tmp = skip;
  213. for_each_node_by_type(dp, type) {
  214. if (!tmp--)
  215. break;
  216. }
  217. if (!dp) {
  218. prom_printf("Cannot find PROM_V0 console node.\n");
  219. prom_halt();
  220. }
  221. of_console_device = dp;
  222. strcpy(of_console_path, dp->full_name);
  223. if (!strcmp(type, "serial")) {
  224. strcat(of_console_path,
  225. (skip ? ":b" : ":a"));
  226. }
  227. break;
  228. default:
  229. case PROM_V2:
  230. case PROM_V3:
  231. fd = *romvec->pv_v2bootargs.fd_stdout;
  232. spin_lock_irqsave(&prom_lock, flags);
  233. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  234. restore_current();
  235. spin_unlock_irqrestore(&prom_lock, flags);
  236. if (!node) {
  237. prom_printf("Cannot resolve stdout node from "
  238. "instance %08x.\n", fd);
  239. prom_halt();
  240. }
  241. dp = of_find_node_by_phandle(node);
  242. type = of_get_property(dp, "device_type", NULL);
  243. if (!type) {
  244. prom_printf("Console stdout lacks "
  245. "device_type property.\n");
  246. prom_halt();
  247. }
  248. if (strcmp(type, "display") && strcmp(type, "serial")) {
  249. prom_printf("Console device_type is neither display "
  250. "nor serial.\n");
  251. prom_halt();
  252. }
  253. of_console_device = dp;
  254. if (prom_vers == PROM_V2) {
  255. strcpy(of_console_path, dp->full_name);
  256. switch (*romvec->pv_stdout) {
  257. case PROMDEV_TTYA:
  258. strcat(of_console_path, ":a");
  259. break;
  260. case PROMDEV_TTYB:
  261. strcat(of_console_path, ":b");
  262. break;
  263. }
  264. } else {
  265. const char *path;
  266. dp = of_find_node_by_path("/");
  267. path = of_get_property(dp, "stdout-path", NULL);
  268. if (!path) {
  269. prom_printf("No stdout-path in root node.\n");
  270. prom_halt();
  271. }
  272. strcpy(of_console_path, path);
  273. }
  274. break;
  275. }
  276. of_console_options = strrchr(of_console_path, ':');
  277. if (of_console_options) {
  278. of_console_options++;
  279. if (*of_console_options == '\0')
  280. of_console_options = NULL;
  281. }
  282. prom_printf(msg, of_console_path);
  283. printk(msg, of_console_path);
  284. }
  285. void __init prom_build_devicetree(void)
  286. {
  287. struct device_node **nextp;
  288. allnodes = create_node(prom_root_node, NULL);
  289. allnodes->path_component_name = "";
  290. allnodes->full_name = "/";
  291. nextp = &allnodes->allnext;
  292. allnodes->child = build_tree(allnodes,
  293. prom_getchild(allnodes->node),
  294. &nextp);
  295. of_console_init();
  296. printk("PROM: Built device tree with %u bytes of memory.\n",
  297. prom_early_allocated);
  298. }