1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Procedures for creating, accessing and interpreting the device tree.
5 * Paul Mackerras August 1996.
6 * Copyright (C) 1996-2005 Paul Mackerras.
8 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9 * {engebret|bergner}@us.ibm.com
11 * Adapted for sparc32 by David S. Miller davem@davemloft.net
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/string.h>
18 #include <linux/memblock.h>
21 #include <asm/oplib.h>
23 #include <asm/leon_amba.h>
27 void * __init prom_early_alloc(unsigned long size)
31 ret = memblock_alloc(size, SMP_CACHE_BYTES);
33 panic("%s: Failed to allocate %lu bytes\n", __func__, size);
35 prom_early_allocated += size;
40 /* The following routines deal with the black magic of fully naming a
43 * Certain well known named nodes are just the simple name string.
45 * Actual devices have an address specifier appended to the base name
46 * string, like this "foo@addr". The "addr" can be in any number of
47 * formats, and the platform plus the type of the node determine the
48 * format and how it is constructed.
50 * For children of the ROOT node, the naming convention is fixed and
51 * determined by whether this is a sun4u or sun4v system.
53 * For children of other nodes, it is bus type specific. So
54 * we walk up the tree until we discover a "device_type" property
55 * we recognize and we go from there.
57 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
59 const char *name = of_get_property(dp, "name", NULL);
60 struct linux_prom_registers *regs;
61 struct property *rprop;
63 rprop = of_find_property(dp, "reg", NULL);
68 sprintf(tmp_buf, "%s@%x,%x",
70 regs->which_io, regs->phys_addr);
73 /* "name@slot,offset" */
74 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
76 const char *name = of_get_property(dp, "name", NULL);
77 struct linux_prom_registers *regs;
78 struct property *prop;
80 prop = of_find_property(dp, "reg", NULL);
85 sprintf(tmp_buf, "%s@%x,%x",
91 /* "name@devnum[,func]" */
92 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
94 const char *name = of_get_property(dp, "name", NULL);
95 struct linux_prom_pci_registers *regs;
96 struct property *prop;
99 prop = of_find_property(dp, "reg", NULL);
104 devfn = (regs->phys_hi >> 8) & 0xff;
106 sprintf(tmp_buf, "%s@%x,%x",
111 sprintf(tmp_buf, "%s@%x",
117 /* "name@addrhi,addrlo" */
118 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
120 const char *name = of_get_property(dp, "name", NULL);
121 struct linux_prom_registers *regs;
122 struct property *prop;
124 prop = of_find_property(dp, "reg", NULL);
130 sprintf(tmp_buf, "%s@%x,%x",
132 regs->which_io, regs->phys_addr);
135 /* "name:vendor:device@irq,addrlo" */
136 static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
138 const char *name = of_get_property(dp, "name", NULL);
139 struct amba_prom_registers *regs;
140 unsigned int *intr, *device, *vendor, reg0;
141 struct property *prop;
144 /* In order to get a unique ID in the device tree (multiple AMBA devices
145 * may have the same name) the node number is printed
147 prop = of_find_property(dp, "reg", NULL);
149 reg0 = (unsigned int)dp->phandle;
152 reg0 = regs->phys_addr;
155 /* Not all cores have Interrupt */
156 prop = of_find_property(dp, "interrupts", NULL);
158 intr = &interrupt; /* IRQ0 does not exist */
162 prop = of_find_property(dp, "vendor", NULL);
165 vendor = prop->value;
166 prop = of_find_property(dp, "device", NULL);
169 device = prop->value;
171 sprintf(tmp_buf, "%s:%d:%d@%x,%x",
172 name, *vendor, *device,
176 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
178 struct device_node *parent = dp->parent;
180 if (parent != NULL) {
181 if (of_node_is_type(parent, "pci") ||
182 of_node_is_type(parent, "pciex"))
183 return pci_path_component(dp, tmp_buf);
184 if (of_node_is_type(parent, "sbus"))
185 return sbus_path_component(dp, tmp_buf);
186 if (of_node_is_type(parent, "ebus"))
187 return ebus_path_component(dp, tmp_buf);
188 if (of_node_is_type(parent, "ambapp"))
189 return ambapp_path_component(dp, tmp_buf);
191 /* "isa" is handled with platform naming */
194 /* Use platform naming convention. */
195 return sparc32_path_component(dp, tmp_buf);
198 char * __init build_path_component(struct device_node *dp)
200 const char *name = of_get_property(dp, "name", NULL);
201 char tmp_buf[64], *n;
204 __build_path_component(dp, tmp_buf);
205 if (tmp_buf[0] == '\0')
206 strcpy(tmp_buf, name);
208 n = prom_early_alloc(strlen(tmp_buf) + 1);
214 extern void restore_current(void);
216 void __init of_console_init(void)
218 char *msg = "OF stdout device is: %s\n";
219 struct device_node *dp;
225 of_console_path = prom_early_alloc(256);
230 switch (*romvec->pv_stdout) {
244 prom_printf("Invalid PROM_V0 stdout value %u\n",
250 for_each_node_by_type(dp, type) {
255 prom_printf("Cannot find PROM_V0 console node.\n");
258 of_console_device = dp;
260 sprintf(of_console_path, "%pOF", dp);
261 if (!strcmp(type, "serial")) {
262 strcat(of_console_path,
263 (skip ? ":b" : ":a"));
270 fd = *romvec->pv_v2bootargs.fd_stdout;
272 spin_lock_irqsave(&prom_lock, flags);
273 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
275 spin_unlock_irqrestore(&prom_lock, flags);
278 prom_printf("Cannot resolve stdout node from "
279 "instance %08x.\n", fd);
282 dp = of_find_node_by_phandle(node);
284 if (!of_node_is_type(dp, "display") &&
285 !of_node_is_type(dp, "serial")) {
286 prom_printf("Console device_type is neither display "
291 of_console_device = dp;
293 if (prom_vers == PROM_V2) {
294 sprintf(of_console_path, "%pOF", dp);
295 switch (*romvec->pv_stdout) {
297 strcat(of_console_path, ":a");
300 strcat(of_console_path, ":b");
306 dp = of_find_node_by_path("/");
307 path = of_get_property(dp, "stdout-path", NULL);
309 prom_printf("No stdout-path in root node.\n");
312 strcpy(of_console_path, path);
317 of_console_options = strrchr(of_console_path, ':');
318 if (of_console_options) {
319 of_console_options++;
320 if (*of_console_options == '\0')
321 of_console_options = NULL;
324 printk(msg, of_console_path);
327 void __init of_fill_in_cpu_data(void)
331 void __init irq_trans_init(struct device_node *dp)