prom_32.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Procedures for creating, accessing and interpreting the device tree.
  4. *
  5. * Paul Mackerras August 1996.
  6. * Copyright (C) 1996-2005 Paul Mackerras.
  7. *
  8. * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
  9. * {engebret|bergner}@us.ibm.com
  10. *
  11. * Adapted for sparc32 by David S. Miller [email protected]
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/types.h>
  15. #include <linux/string.h>
  16. #include <linux/mm.h>
  17. #include <linux/memblock.h>
  18. #include <asm/prom.h>
  19. #include <asm/oplib.h>
  20. #include <asm/leon.h>
  21. #include <asm/leon_amba.h>
  22. #include "prom.h"
  23. void * __init prom_early_alloc(unsigned long size)
  24. {
  25. void *ret;
  26. ret = memblock_alloc(size, SMP_CACHE_BYTES);
  27. if (!ret)
  28. panic("%s: Failed to allocate %lu bytes\n", __func__, size);
  29. prom_early_allocated += size;
  30. return ret;
  31. }
  32. /* The following routines deal with the black magic of fully naming a
  33. * node.
  34. *
  35. * Certain well known named nodes are just the simple name string.
  36. *
  37. * Actual devices have an address specifier appended to the base name
  38. * string, like this "foo@addr". The "addr" can be in any number of
  39. * formats, and the platform plus the type of the node determine the
  40. * format and how it is constructed.
  41. *
  42. * For children of the ROOT node, the naming convention is fixed and
  43. * determined by whether this is a sun4u or sun4v system.
  44. *
  45. * For children of other nodes, it is bus type specific. So
  46. * we walk up the tree until we discover a "device_type" property
  47. * we recognize and we go from there.
  48. */
  49. static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
  50. {
  51. const char *name = of_get_property(dp, "name", NULL);
  52. struct linux_prom_registers *regs;
  53. struct property *rprop;
  54. rprop = of_find_property(dp, "reg", NULL);
  55. if (!rprop)
  56. return;
  57. regs = rprop->value;
  58. sprintf(tmp_buf, "%s@%x,%x",
  59. name,
  60. regs->which_io, regs->phys_addr);
  61. }
  62. /* "name@slot,offset" */
  63. static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
  64. {
  65. const char *name = of_get_property(dp, "name", NULL);
  66. struct linux_prom_registers *regs;
  67. struct property *prop;
  68. prop = of_find_property(dp, "reg", NULL);
  69. if (!prop)
  70. return;
  71. regs = prop->value;
  72. sprintf(tmp_buf, "%s@%x,%x",
  73. name,
  74. regs->which_io,
  75. regs->phys_addr);
  76. }
  77. /* "name@devnum[,func]" */
  78. static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
  79. {
  80. const char *name = of_get_property(dp, "name", NULL);
  81. struct linux_prom_pci_registers *regs;
  82. struct property *prop;
  83. unsigned int devfn;
  84. prop = of_find_property(dp, "reg", NULL);
  85. if (!prop)
  86. return;
  87. regs = prop->value;
  88. devfn = (regs->phys_hi >> 8) & 0xff;
  89. if (devfn & 0x07) {
  90. sprintf(tmp_buf, "%s@%x,%x",
  91. name,
  92. devfn >> 3,
  93. devfn & 0x07);
  94. } else {
  95. sprintf(tmp_buf, "%s@%x",
  96. name,
  97. devfn >> 3);
  98. }
  99. }
  100. /* "name@addrhi,addrlo" */
  101. static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
  102. {
  103. const char *name = of_get_property(dp, "name", NULL);
  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. name,
  112. regs->which_io, regs->phys_addr);
  113. }
  114. /* "name@irq,addrlo" */
  115. static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
  116. {
  117. const char *name = of_get_property(dp, "name", NULL);
  118. struct amba_prom_registers *regs;
  119. unsigned int *intr;
  120. unsigned int reg0;
  121. struct property *prop;
  122. int interrupt = 0;
  123. /* In order to get a unique ID in the device tree (multiple AMBA devices
  124. * may have the same name) the node number is printed
  125. */
  126. prop = of_find_property(dp, "reg", NULL);
  127. if (!prop) {
  128. reg0 = (unsigned int)dp->phandle;
  129. } else {
  130. regs = prop->value;
  131. reg0 = regs->phys_addr;
  132. }
  133. /* Not all cores have Interrupt */
  134. prop = of_find_property(dp, "interrupts", NULL);
  135. if (!prop)
  136. intr = &interrupt; /* IRQ0 does not exist */
  137. else
  138. intr = prop->value;
  139. sprintf(tmp_buf, "%s@%x,%x", name, *intr, reg0);
  140. }
  141. static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
  142. {
  143. struct device_node *parent = dp->parent;
  144. if (parent != NULL) {
  145. if (of_node_is_type(parent, "pci") ||
  146. of_node_is_type(parent, "pciex"))
  147. return pci_path_component(dp, tmp_buf);
  148. if (of_node_is_type(parent, "sbus"))
  149. return sbus_path_component(dp, tmp_buf);
  150. if (of_node_is_type(parent, "ebus"))
  151. return ebus_path_component(dp, tmp_buf);
  152. if (of_node_is_type(parent, "ambapp"))
  153. return ambapp_path_component(dp, tmp_buf);
  154. /* "isa" is handled with platform naming */
  155. }
  156. /* Use platform naming convention. */
  157. return sparc32_path_component(dp, tmp_buf);
  158. }
  159. char * __init build_path_component(struct device_node *dp)
  160. {
  161. const char *name = of_get_property(dp, "name", NULL);
  162. char tmp_buf[64], *n;
  163. tmp_buf[0] = '\0';
  164. __build_path_component(dp, tmp_buf);
  165. if (tmp_buf[0] == '\0')
  166. strcpy(tmp_buf, name);
  167. n = prom_early_alloc(strlen(tmp_buf) + 1);
  168. strcpy(n, tmp_buf);
  169. return n;
  170. }
  171. extern void restore_current(void);
  172. void __init of_console_init(void)
  173. {
  174. char *msg = "OF stdout device is: %s\n";
  175. struct device_node *dp;
  176. unsigned long flags;
  177. const char *type;
  178. phandle node;
  179. int skip, tmp, fd;
  180. of_console_path = prom_early_alloc(256);
  181. switch (prom_vers) {
  182. case PROM_V0:
  183. skip = 0;
  184. switch (*romvec->pv_stdout) {
  185. case PROMDEV_SCREEN:
  186. type = "display";
  187. break;
  188. case PROMDEV_TTYB:
  189. skip = 1;
  190. fallthrough;
  191. case PROMDEV_TTYA:
  192. type = "serial";
  193. break;
  194. default:
  195. prom_printf("Invalid PROM_V0 stdout value %u\n",
  196. *romvec->pv_stdout);
  197. prom_halt();
  198. }
  199. tmp = skip;
  200. for_each_node_by_type(dp, type) {
  201. if (!tmp--)
  202. break;
  203. }
  204. if (!dp) {
  205. prom_printf("Cannot find PROM_V0 console node.\n");
  206. prom_halt();
  207. }
  208. of_console_device = dp;
  209. sprintf(of_console_path, "%pOF", dp);
  210. if (!strcmp(type, "serial")) {
  211. strcat(of_console_path,
  212. (skip ? ":b" : ":a"));
  213. }
  214. break;
  215. default:
  216. case PROM_V2:
  217. case PROM_V3:
  218. fd = *romvec->pv_v2bootargs.fd_stdout;
  219. spin_lock_irqsave(&prom_lock, flags);
  220. node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
  221. restore_current();
  222. spin_unlock_irqrestore(&prom_lock, flags);
  223. if (!node) {
  224. prom_printf("Cannot resolve stdout node from "
  225. "instance %08x.\n", fd);
  226. prom_halt();
  227. }
  228. dp = of_find_node_by_phandle(node);
  229. if (!of_node_is_type(dp, "display") &&
  230. !of_node_is_type(dp, "serial")) {
  231. prom_printf("Console device_type is neither display "
  232. "nor serial.\n");
  233. prom_halt();
  234. }
  235. of_console_device = dp;
  236. if (prom_vers == PROM_V2) {
  237. sprintf(of_console_path, "%pOF", dp);
  238. switch (*romvec->pv_stdout) {
  239. case PROMDEV_TTYA:
  240. strcat(of_console_path, ":a");
  241. break;
  242. case PROMDEV_TTYB:
  243. strcat(of_console_path, ":b");
  244. break;
  245. }
  246. } else {
  247. const char *path;
  248. dp = of_find_node_by_path("/");
  249. path = of_get_property(dp, "stdout-path", NULL);
  250. if (!path) {
  251. prom_printf("No stdout-path in root node.\n");
  252. prom_halt();
  253. }
  254. strcpy(of_console_path, path);
  255. }
  256. break;
  257. }
  258. of_console_options = strrchr(of_console_path, ':');
  259. if (of_console_options) {
  260. of_console_options++;
  261. if (*of_console_options == '\0')
  262. of_console_options = NULL;
  263. }
  264. printk(msg, of_console_path);
  265. }
  266. void __init of_fill_in_cpu_data(void)
  267. {
  268. }
  269. void __init irq_trans_init(struct device_node *dp)
  270. {
  271. }