2 * Procedures for creating, accessing and interpreting the device tree.
4 * Paul Mackerras August 1996.
5 * Copyright (C) 1996-2005 Paul Mackerras.
7 * Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
8 * {engebret|bergner}@us.ibm.com
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.
18 #include <linux/kernel.h>
19 #include <linux/types.h>
20 #include <linux/string.h>
22 #include <linux/bootmem.h>
25 #include <asm/oplib.h>
27 #include <asm/leon_amba.h>
31 void * __init prom_early_alloc(unsigned long size)
35 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
39 prom_early_allocated += size;
44 /* The following routines deal with the black magic of fully naming a
47 * Certain well known named nodes are just the simple name string.
49 * Actual devices have an address specifier appended to the base name
50 * string, like this "foo@addr". The "addr" can be in any number of
51 * formats, and the platform plus the type of the node determine the
52 * format and how it is constructed.
54 * For children of the ROOT node, the naming convention is fixed and
55 * determined by whether this is a sun4u or sun4v system.
57 * For children of other nodes, it is bus type specific. So
58 * we walk up the tree until we discover a "device_type" property
59 * we recognize and we go from there.
61 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
63 struct linux_prom_registers *regs;
64 struct property *rprop;
66 rprop = of_find_property(dp, "reg", NULL);
71 sprintf(tmp_buf, "%s@%x,%x",
73 regs->which_io, regs->phys_addr);
76 /* "name@slot,offset" */
77 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
79 struct linux_prom_registers *regs;
80 struct property *prop;
82 prop = of_find_property(dp, "reg", NULL);
87 sprintf(tmp_buf, "%s@%x,%x",
93 /* "name@devnum[,func]" */
94 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
96 struct linux_prom_pci_registers *regs;
97 struct property *prop;
100 prop = of_find_property(dp, "reg", NULL);
105 devfn = (regs->phys_hi >> 8) & 0xff;
107 sprintf(tmp_buf, "%s@%x,%x",
112 sprintf(tmp_buf, "%s@%x",
118 /* "name@addrhi,addrlo" */
119 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
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 struct amba_prom_registers *regs;
139 unsigned int *intr, *device, *vendor, reg0;
140 struct property *prop;
143 /* In order to get a unique ID in the device tree (multiple AMBA devices
144 * may have the same name) the node number is printed
146 prop = of_find_property(dp, "reg", NULL);
148 reg0 = (unsigned int)dp->phandle;
151 reg0 = regs->phys_addr;
154 /* Not all cores have Interrupt */
155 prop = of_find_property(dp, "interrupts", NULL);
157 intr = &interrupt; /* IRQ0 does not exist */
161 prop = of_find_property(dp, "vendor", NULL);
164 vendor = prop->value;
165 prop = of_find_property(dp, "device", NULL);
168 device = prop->value;
170 sprintf(tmp_buf, "%s:%d:%d@%x,%x",
171 dp->name, *vendor, *device,
175 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
177 struct device_node *parent = dp->parent;
179 if (parent != NULL) {
180 if (!strcmp(parent->type, "pci") ||
181 !strcmp(parent->type, "pciex"))
182 return pci_path_component(dp, tmp_buf);
183 if (!strcmp(parent->type, "sbus"))
184 return sbus_path_component(dp, tmp_buf);
185 if (!strcmp(parent->type, "ebus"))
186 return ebus_path_component(dp, tmp_buf);
187 if (!strcmp(parent->type, "ambapp"))
188 return ambapp_path_component(dp, tmp_buf);
190 /* "isa" is handled with platform naming */
193 /* Use platform naming convention. */
194 return sparc32_path_component(dp, tmp_buf);
197 char * __init build_path_component(struct device_node *dp)
199 char tmp_buf[64], *n;
202 __build_path_component(dp, tmp_buf);
203 if (tmp_buf[0] == '\0')
204 strcpy(tmp_buf, dp->name);
206 n = prom_early_alloc(strlen(tmp_buf) + 1);
212 extern void restore_current(void);
214 void __init of_console_init(void)
216 char *msg = "OF stdout device is: %s\n";
217 struct device_node *dp;
223 of_console_path = prom_early_alloc(256);
228 switch (*romvec->pv_stdout) {
242 prom_printf("Invalid PROM_V0 stdout value %u\n",
248 for_each_node_by_type(dp, type) {
253 prom_printf("Cannot find PROM_V0 console node.\n");
256 of_console_device = dp;
258 strcpy(of_console_path, dp->full_name);
259 if (!strcmp(type, "serial")) {
260 strcat(of_console_path,
261 (skip ? ":b" : ":a"));
268 fd = *romvec->pv_v2bootargs.fd_stdout;
270 spin_lock_irqsave(&prom_lock, flags);
271 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
273 spin_unlock_irqrestore(&prom_lock, flags);
276 prom_printf("Cannot resolve stdout node from "
277 "instance %08x.\n", fd);
280 dp = of_find_node_by_phandle(node);
281 type = of_get_property(dp, "device_type", NULL);
284 prom_printf("Console stdout lacks "
285 "device_type property.\n");
289 if (strcmp(type, "display") && strcmp(type, "serial")) {
290 prom_printf("Console device_type is neither display "
295 of_console_device = dp;
297 if (prom_vers == PROM_V2) {
298 strcpy(of_console_path, dp->full_name);
299 switch (*romvec->pv_stdout) {
301 strcat(of_console_path, ":a");
304 strcat(of_console_path, ":b");
310 dp = of_find_node_by_path("/");
311 path = of_get_property(dp, "stdout-path", NULL);
313 prom_printf("No stdout-path in root node.\n");
316 strcpy(of_console_path, path);
321 of_console_options = strrchr(of_console_path, ':');
322 if (of_console_options) {
323 of_console_options++;
324 if (*of_console_options == '\0')
325 of_console_options = NULL;
328 printk(msg, of_console_path);
331 void __init of_fill_in_cpu_data(void)
335 void __init irq_trans_init(struct device_node *dp)