prom_32.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 <asm/prom.h>
  23. #include <asm/oplib.h>
  24. #include <asm/leon.h>
  25. #include <asm/leon_amba.h>
  26. #include "prom.h"
  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. /* "name:vendor:device@irq,addrlo" */
  115. static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
  116. {
  117. struct amba_prom_registers *regs;
  118. unsigned int *intr, *device, *vendor, reg0;
  119. struct property *prop;
  120. int interrupt = 0;
  121. /* In order to get a unique ID in the device tree (multiple AMBA devices
  122. * may have the same name) the node number is printed
  123. */
  124. prop = of_find_property(dp, "reg", NULL);
  125. if (!prop) {
  126. reg0 = (unsigned int)dp->phandle;
  127. } else {
  128. regs = prop->value;
  129. reg0 = regs->phys_addr;
  130. }
  131. /* Not all cores have Interrupt */
  132. prop = of_find_property(dp, "interrupts", NULL);
  133. if (!prop)
  134. intr = &interrupt; /* IRQ0 does not exist */
  135. else
  136. intr = prop->value;
  137. prop = of_find_property(dp, "vendor", NULL);
  138. if (!prop)
  139. return;
  140. vendor = prop->value;
  141. prop = of_find_property(dp, "device", NULL);
  142. if (!prop)
  143. return;
  144. device = prop->value;
  145. sprintf(tmp_buf, "%s:%d:%d@%x,%x",
  146. dp->name, *vendor, *device,
  147. *intr, reg0);
  148. }
  149. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  150. {
  151. struct device_node *parent = dp->parent;
  152. if (parent != NULL) {
  153. if (!strcmp(parent->type, "pci") ||
  154. !strcmp(parent->type, "pciex"))
  155. return pci_path_component(dp, tmp_buf);
  156. if (!strcmp(parent->type, "sbus"))
  157. return sbus_path_component(dp, tmp_buf);
  158. if (!strcmp(parent->type, "ebus"))
  159. return ebus_path_component(dp, tmp_buf);
  160. if (!strcmp(parent->type, "ambapp"))
  161. return ambapp_path_component(dp, tmp_buf);
  162. /* "isa" is handled with platform naming */
  163. }
  164. /* Use platform naming convention. */
  165. return sparc32_path_component(dp, tmp_buf);
  166. }
  167. char * __init build_path_component(struct device_node *dp)
  168. {
  169. char tmp_buf[64], *n;
  170. tmp_buf[0] = '\0';
  171. __build_path_component(dp, tmp_buf);
  172. if (tmp_buf[0] == '\0')
  173. strcpy(tmp_buf, dp->name);
  174. n = prom_early_alloc(strlen(tmp_buf) + 1);
  175. strcpy(n, tmp_buf);
  176. return n;
  177. }
  178. extern void restore_current(void);
  179. void __init of_console_init(void)
  180. {
  181. char *msg = "OF stdout device is: %s\n";
  182. struct device_node *dp;
  183. unsigned long flags;
  184. const char *type;
  185. phandle node;
  186. int skip, tmp, fd;
  187. of_console_path = prom_early_alloc(256);
  188. switch (prom_vers) {
  189. case PROM_V0:
  190. skip = 0;
  191. switch (*romvec->pv_stdout) {
  192. case PROMDEV_SCREEN:
  193. type = "display";
  194. break;
  195. case PROMDEV_TTYB:
  196. skip = 1;
  197. /* FALLTHRU */
  198. case PROMDEV_TTYA:
  199. type = "serial";
  200. break;
  201. default:
  202. prom_printf("Invalid PROM_V0 stdout value %u\n",
  203. *romvec->pv_stdout);
  204. prom_halt();
  205. }
  206. tmp = skip;
  207. for_each_node_by_type(dp, type) {
  208. if (!tmp--)
  209. break;
  210. }
  211. if (!dp) {
  212. prom_printf("Cannot find PROM_V0 console node.\n");
  213. prom_halt();
  214. }
  215. of_console_device = dp;
  216. strcpy(of_console_path, dp->full_name);
  217. if (!strcmp(type, "serial")) {
  218. strcat(of_console_path,
  219. (skip ? ":b" : ":a"));
  220. }
  221. break;
  222. default:
  223. case PROM_V2:
  224. case PROM_V3:
  225. fd = *romvec->pv_v2bootargs.fd_stdout;
  226. spin_lock_irqsave(&prom_lock, flags);
  227. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  228. restore_current();
  229. spin_unlock_irqrestore(&prom_lock, flags);
  230. if (!node) {
  231. prom_printf("Cannot resolve stdout node from "
  232. "instance %08x.\n", fd);
  233. prom_halt();
  234. }
  235. dp = of_find_node_by_phandle(node);
  236. type = of_get_property(dp, "device_type", NULL);
  237. if (!type) {
  238. prom_printf("Console stdout lacks "
  239. "device_type property.\n");
  240. prom_halt();
  241. }
  242. if (strcmp(type, "display") && strcmp(type, "serial")) {
  243. prom_printf("Console device_type is neither display "
  244. "nor serial.\n");
  245. prom_halt();
  246. }
  247. of_console_device = dp;
  248. if (prom_vers == PROM_V2) {
  249. strcpy(of_console_path, dp->full_name);
  250. switch (*romvec->pv_stdout) {
  251. case PROMDEV_TTYA:
  252. strcat(of_console_path, ":a");
  253. break;
  254. case PROMDEV_TTYB:
  255. strcat(of_console_path, ":b");
  256. break;
  257. }
  258. } else {
  259. const char *path;
  260. dp = of_find_node_by_path("/");
  261. path = of_get_property(dp, "stdout-path", NULL);
  262. if (!path) {
  263. prom_printf("No stdout-path in root node.\n");
  264. prom_halt();
  265. }
  266. strcpy(of_console_path, path);
  267. }
  268. break;
  269. }
  270. of_console_options = strrchr(of_console_path, ':');
  271. if (of_console_options) {
  272. of_console_options++;
  273. if (*of_console_options == '\0')
  274. of_console_options = NULL;
  275. }
  276. printk(msg, of_console_path);
  277. }
  278. void __init of_fill_in_cpu_data(void)
  279. {
  280. }
  281. void __init irq_trans_init(struct device_node *dp)
  282. {
  283. }