prom_32.c 7.0 KB

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