sparc: Move core of OF device tree building code into prom_common.c
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / arch / sparc / kernel / prom_32.c
blob75eb40be5e8a6103ace059d038dedcd3f0a84178
1 /*
2 * Procedures for creating, accessing and interpreting the device tree.
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
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>
21 #include <linux/mm.h>
22 #include <linux/bootmem.h>
23 #include <linux/module.h>
25 #include <asm/prom.h>
26 #include <asm/oplib.h>
28 #include "prom.h"
30 static unsigned int prom_early_allocated;
32 void * __init prom_early_alloc(unsigned long size)
34 void *ret;
36 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
37 if (ret != NULL)
38 memset(ret, 0, size);
40 prom_early_allocated += size;
42 return ret;
45 /* The following routines deal with the black magic of fully naming a
46 * node.
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);
68 if (!rprop)
69 return;
71 regs = rprop->value;
72 sprintf(tmp_buf, "%s@%x,%x",
73 dp->name,
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);
84 if (!prop)
85 return;
87 regs = prop->value;
88 sprintf(tmp_buf, "%s@%x,%x",
89 dp->name,
90 regs->which_io,
91 regs->phys_addr);
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;
99 unsigned int devfn;
101 prop = of_find_property(dp, "reg", NULL);
102 if (!prop)
103 return;
105 regs = prop->value;
106 devfn = (regs->phys_hi >> 8) & 0xff;
107 if (devfn & 0x07) {
108 sprintf(tmp_buf, "%s@%x,%x",
109 dp->name,
110 devfn >> 3,
111 devfn & 0x07);
112 } else {
113 sprintf(tmp_buf, "%s@%x",
114 dp->name,
115 devfn >> 3);
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);
126 if (!prop)
127 return;
129 regs = prop->value;
131 sprintf(tmp_buf, "%s@%x,%x",
132 dp->name,
133 regs->which_io, regs->phys_addr);
136 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
138 struct device_node *parent = dp->parent;
140 if (parent != NULL) {
141 if (!strcmp(parent->type, "pci") ||
142 !strcmp(parent->type, "pciex"))
143 return pci_path_component(dp, tmp_buf);
144 if (!strcmp(parent->type, "sbus"))
145 return sbus_path_component(dp, tmp_buf);
146 if (!strcmp(parent->type, "ebus"))
147 return ebus_path_component(dp, tmp_buf);
149 /* "isa" is handled with platform naming */
152 /* Use platform naming convention. */
153 return sparc32_path_component(dp, tmp_buf);
156 char * __init build_path_component(struct device_node *dp)
158 char tmp_buf[64], *n;
160 tmp_buf[0] = '\0';
161 __build_path_component(dp, tmp_buf);
162 if (tmp_buf[0] == '\0')
163 strcpy(tmp_buf, dp->name);
165 n = prom_early_alloc(strlen(tmp_buf) + 1);
166 strcpy(n, tmp_buf);
168 return n;
171 struct device_node *of_console_device;
172 EXPORT_SYMBOL(of_console_device);
174 char *of_console_path;
175 EXPORT_SYMBOL(of_console_path);
177 char *of_console_options;
178 EXPORT_SYMBOL(of_console_options);
180 extern void restore_current(void);
182 static void __init of_console_init(void)
184 char *msg = "OF stdout device is: %s\n";
185 struct device_node *dp;
186 unsigned long flags;
187 const char *type;
188 phandle node;
189 int skip, tmp, fd;
191 of_console_path = prom_early_alloc(256);
193 switch (prom_vers) {
194 case PROM_V0:
195 skip = 0;
196 switch (*romvec->pv_stdout) {
197 case PROMDEV_SCREEN:
198 type = "display";
199 break;
201 case PROMDEV_TTYB:
202 skip = 1;
203 /* FALLTHRU */
205 case PROMDEV_TTYA:
206 type = "serial";
207 break;
209 default:
210 prom_printf("Invalid PROM_V0 stdout value %u\n",
211 *romvec->pv_stdout);
212 prom_halt();
215 tmp = skip;
216 for_each_node_by_type(dp, type) {
217 if (!tmp--)
218 break;
220 if (!dp) {
221 prom_printf("Cannot find PROM_V0 console node.\n");
222 prom_halt();
224 of_console_device = dp;
226 strcpy(of_console_path, dp->full_name);
227 if (!strcmp(type, "serial")) {
228 strcat(of_console_path,
229 (skip ? ":b" : ":a"));
231 break;
233 default:
234 case PROM_V2:
235 case PROM_V3:
236 fd = *romvec->pv_v2bootargs.fd_stdout;
238 spin_lock_irqsave(&prom_lock, flags);
239 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
240 restore_current();
241 spin_unlock_irqrestore(&prom_lock, flags);
243 if (!node) {
244 prom_printf("Cannot resolve stdout node from "
245 "instance %08x.\n", fd);
246 prom_halt();
248 dp = of_find_node_by_phandle(node);
249 type = of_get_property(dp, "device_type", NULL);
251 if (!type) {
252 prom_printf("Console stdout lacks "
253 "device_type property.\n");
254 prom_halt();
257 if (strcmp(type, "display") && strcmp(type, "serial")) {
258 prom_printf("Console device_type is neither display "
259 "nor serial.\n");
260 prom_halt();
263 of_console_device = dp;
265 if (prom_vers == PROM_V2) {
266 strcpy(of_console_path, dp->full_name);
267 switch (*romvec->pv_stdout) {
268 case PROMDEV_TTYA:
269 strcat(of_console_path, ":a");
270 break;
271 case PROMDEV_TTYB:
272 strcat(of_console_path, ":b");
273 break;
275 } else {
276 const char *path;
278 dp = of_find_node_by_path("/");
279 path = of_get_property(dp, "stdout-path", NULL);
280 if (!path) {
281 prom_printf("No stdout-path in root node.\n");
282 prom_halt();
284 strcpy(of_console_path, path);
286 break;
289 of_console_options = strrchr(of_console_path, ':');
290 if (of_console_options) {
291 of_console_options++;
292 if (*of_console_options == '\0')
293 of_console_options = NULL;
296 prom_printf(msg, of_console_path);
297 printk(msg, of_console_path);
300 void __init prom_build_devicetree(void)
302 struct device_node **nextp;
304 allnodes = prom_create_node(prom_root_node, NULL);
305 allnodes->path_component_name = "";
306 allnodes->full_name = "/";
308 nextp = &allnodes->allnext;
309 allnodes->child = prom_build_tree(allnodes,
310 prom_getchild(allnodes->node),
311 &nextp);
312 of_console_init();
314 printk("PROM: Built device tree with %u bytes of memory.\n",
315 prom_early_allocated);