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
10 * Adapted for sparc32 by David S. Miller davem@davemloft.net
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>
23 #include <linux/module.h>
26 #include <asm/oplib.h>
28 #include <asm/leon_amba.h>
32 void * __init
prom_early_alloc(unsigned long size
)
36 ret
= __alloc_bootmem(size
, SMP_CACHE_BYTES
, 0UL);
40 prom_early_allocated
+= size
;
45 /* The following routines deal with the black magic of fully naming a
48 * Certain well known named nodes are just the simple name string.
50 * Actual devices have an address specifier appended to the base name
51 * string, like this "foo@addr". The "addr" can be in any number of
52 * formats, and the platform plus the type of the node determine the
53 * format and how it is constructed.
55 * For children of the ROOT node, the naming convention is fixed and
56 * determined by whether this is a sun4u or sun4v system.
58 * For children of other nodes, it is bus type specific. So
59 * we walk up the tree until we discover a "device_type" property
60 * we recognize and we go from there.
62 static void __init
sparc32_path_component(struct device_node
*dp
, char *tmp_buf
)
64 struct linux_prom_registers
*regs
;
65 struct property
*rprop
;
67 rprop
= of_find_property(dp
, "reg", NULL
);
72 sprintf(tmp_buf
, "%s@%x,%x",
74 regs
->which_io
, regs
->phys_addr
);
77 /* "name@slot,offset" */
78 static void __init
sbus_path_component(struct device_node
*dp
, char *tmp_buf
)
80 struct linux_prom_registers
*regs
;
81 struct property
*prop
;
83 prop
= of_find_property(dp
, "reg", NULL
);
88 sprintf(tmp_buf
, "%s@%x,%x",
94 /* "name@devnum[,func]" */
95 static void __init
pci_path_component(struct device_node
*dp
, char *tmp_buf
)
97 struct linux_prom_pci_registers
*regs
;
98 struct property
*prop
;
101 prop
= of_find_property(dp
, "reg", NULL
);
106 devfn
= (regs
->phys_hi
>> 8) & 0xff;
108 sprintf(tmp_buf
, "%s@%x,%x",
113 sprintf(tmp_buf
, "%s@%x",
119 /* "name@addrhi,addrlo" */
120 static void __init
ebus_path_component(struct device_node
*dp
, char *tmp_buf
)
122 struct linux_prom_registers
*regs
;
123 struct property
*prop
;
125 prop
= of_find_property(dp
, "reg", NULL
);
131 sprintf(tmp_buf
, "%s@%x,%x",
133 regs
->which_io
, regs
->phys_addr
);
136 /* "name:vendor:device@irq,addrlo" */
137 static void __init
ambapp_path_component(struct device_node
*dp
, char *tmp_buf
)
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 dp
->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 (!strcmp(parent
->type
, "pci") ||
182 !strcmp(parent
->type
, "pciex"))
183 return pci_path_component(dp
, tmp_buf
);
184 if (!strcmp(parent
->type
, "sbus"))
185 return sbus_path_component(dp
, tmp_buf
);
186 if (!strcmp(parent
->type
, "ebus"))
187 return ebus_path_component(dp
, tmp_buf
);
188 if (!strcmp(parent
->type
, "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 char tmp_buf
[64], *n
;
203 __build_path_component(dp
, tmp_buf
);
204 if (tmp_buf
[0] == '\0')
205 strcpy(tmp_buf
, dp
->name
);
207 n
= prom_early_alloc(strlen(tmp_buf
) + 1);
213 extern void restore_current(void);
215 void __init
of_console_init(void)
217 char *msg
= "OF stdout device is: %s\n";
218 struct device_node
*dp
;
224 of_console_path
= prom_early_alloc(256);
229 switch (*romvec
->pv_stdout
) {
243 prom_printf("Invalid PROM_V0 stdout value %u\n",
249 for_each_node_by_type(dp
, type
) {
254 prom_printf("Cannot find PROM_V0 console node.\n");
257 of_console_device
= dp
;
259 strcpy(of_console_path
, dp
->full_name
);
260 if (!strcmp(type
, "serial")) {
261 strcat(of_console_path
,
262 (skip
? ":b" : ":a"));
269 fd
= *romvec
->pv_v2bootargs
.fd_stdout
;
271 spin_lock_irqsave(&prom_lock
, flags
);
272 node
= (*romvec
->pv_v2devops
.v2_inst2pkg
)(fd
);
274 spin_unlock_irqrestore(&prom_lock
, flags
);
277 prom_printf("Cannot resolve stdout node from "
278 "instance %08x.\n", fd
);
281 dp
= of_find_node_by_phandle(node
);
282 type
= of_get_property(dp
, "device_type", NULL
);
285 prom_printf("Console stdout lacks "
286 "device_type property.\n");
290 if (strcmp(type
, "display") && strcmp(type
, "serial")) {
291 prom_printf("Console device_type is neither display "
296 of_console_device
= dp
;
298 if (prom_vers
== PROM_V2
) {
299 strcpy(of_console_path
, dp
->full_name
);
300 switch (*romvec
->pv_stdout
) {
302 strcat(of_console_path
, ":a");
305 strcat(of_console_path
, ":b");
311 dp
= of_find_node_by_path("/");
312 path
= of_get_property(dp
, "stdout-path", NULL
);
314 prom_printf("No stdout-path in root node.\n");
317 strcpy(of_console_path
, path
);
322 of_console_options
= strrchr(of_console_path
, ':');
323 if (of_console_options
) {
324 of_console_options
++;
325 if (*of_console_options
== '\0')
326 of_console_options
= NULL
;
329 printk(msg
, of_console_path
);
332 void __init
of_fill_in_cpu_data(void)
336 void __init
irq_trans_init(struct device_node
*dp
)