sparc: Move core of OF device tree building code into prom_common.c
[deliverable/linux.git] / arch / sparc / kernel / prom_32.c
CommitLineData
942a6bdd
DM
1/*
2 * Procedures for creating, accessing and interpreting the device tree.
3 *
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
9 *
10 * Adapted for sparc32 by David S. Miller davem@davemloft.net
11 *
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.
16 */
17
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>
24
25#include <asm/prom.h>
26#include <asm/oplib.h>
27
657f201d 28#include "prom.h"
fb7cd9d9 29
942a6bdd
DM
30static unsigned int prom_early_allocated;
31
efeac2f8 32void * __init prom_early_alloc(unsigned long size)
942a6bdd
DM
33{
34 void *ret;
35
36 ret = __alloc_bootmem(size, SMP_CACHE_BYTES, 0UL);
37 if (ret != NULL)
38 memset(ret, 0, size);
39
40 prom_early_allocated += size;
41
42 return ret;
43}
44
942a6bdd
DM
45/* The following routines deal with the black magic of fully naming a
46 * node.
47 *
48 * Certain well known named nodes are just the simple name string.
49 *
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.
54 *
55 * For children of the ROOT node, the naming convention is fixed and
56 * determined by whether this is a sun4u or sun4v system.
57 *
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.
61 */
62static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
63{
64 struct linux_prom_registers *regs;
65 struct property *rprop;
66
67 rprop = of_find_property(dp, "reg", NULL);
68 if (!rprop)
69 return;
70
71 regs = rprop->value;
72 sprintf(tmp_buf, "%s@%x,%x",
73 dp->name,
74 regs->which_io, regs->phys_addr);
75}
76
77/* "name@slot,offset" */
78static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
79{
80 struct linux_prom_registers *regs;
81 struct property *prop;
82
83 prop = of_find_property(dp, "reg", NULL);
84 if (!prop)
85 return;
86
87 regs = prop->value;
88 sprintf(tmp_buf, "%s@%x,%x",
89 dp->name,
90 regs->which_io,
91 regs->phys_addr);
92}
93
94/* "name@devnum[,func]" */
95static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
96{
97 struct linux_prom_pci_registers *regs;
98 struct property *prop;
99 unsigned int devfn;
100
101 prop = of_find_property(dp, "reg", NULL);
102 if (!prop)
103 return;
104
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);
116 }
117}
118
119/* "name@addrhi,addrlo" */
120static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
121{
122 struct linux_prom_registers *regs;
123 struct property *prop;
124
125 prop = of_find_property(dp, "reg", NULL);
126 if (!prop)
127 return;
128
129 regs = prop->value;
130
131 sprintf(tmp_buf, "%s@%x,%x",
132 dp->name,
133 regs->which_io, regs->phys_addr);
134}
135
136static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
137{
138 struct device_node *parent = dp->parent;
139
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);
148
149 /* "isa" is handled with platform naming */
150 }
151
152 /* Use platform naming convention. */
153 return sparc32_path_component(dp, tmp_buf);
154}
155
6524036a 156char * __init build_path_component(struct device_node *dp)
942a6bdd
DM
157{
158 char tmp_buf[64], *n;
159
160 tmp_buf[0] = '\0';
161 __build_path_component(dp, tmp_buf);
162 if (tmp_buf[0] == '\0')
163 strcpy(tmp_buf, dp->name);
164
165 n = prom_early_alloc(strlen(tmp_buf) + 1);
166 strcpy(n, tmp_buf);
167
168 return n;
169}
170
c73fcc84
DM
171struct device_node *of_console_device;
172EXPORT_SYMBOL(of_console_device);
173
174char *of_console_path;
175EXPORT_SYMBOL(of_console_path);
176
177char *of_console_options;
178EXPORT_SYMBOL(of_console_options);
179
180extern void restore_current(void);
181
182static void __init of_console_init(void)
183{
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;
f623f388 189 int skip, tmp, fd;
c73fcc84
DM
190
191 of_console_path = prom_early_alloc(256);
192
193 switch (prom_vers) {
194 case PROM_V0:
c73fcc84
DM
195 skip = 0;
196 switch (*romvec->pv_stdout) {
197 case PROMDEV_SCREEN:
198 type = "display";
199 break;
200
201 case PROMDEV_TTYB:
202 skip = 1;
203 /* FALLTHRU */
204
205 case PROMDEV_TTYA:
206 type = "serial";
207 break;
208
209 default:
210 prom_printf("Invalid PROM_V0 stdout value %u\n",
211 *romvec->pv_stdout);
212 prom_halt();
213 }
214
f623f388 215 tmp = skip;
c73fcc84 216 for_each_node_by_type(dp, type) {
f623f388 217 if (!tmp--)
c73fcc84
DM
218 break;
219 }
220 if (!dp) {
221 prom_printf("Cannot find PROM_V0 console node.\n");
222 prom_halt();
223 }
224 of_console_device = dp;
225
226 strcpy(of_console_path, dp->full_name);
227 if (!strcmp(type, "serial")) {
228 strcat(of_console_path,
229 (skip ? ":b" : ":a"));
230 }
231 break;
232
233 default:
234 case PROM_V2:
235 case PROM_V3:
236 fd = *romvec->pv_v2bootargs.fd_stdout;
237
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);
242
243 if (!node) {
244 prom_printf("Cannot resolve stdout node from "
245 "instance %08x.\n", fd);
246 prom_halt();
247 }
248 dp = of_find_node_by_phandle(node);
249 type = of_get_property(dp, "device_type", NULL);
250
251 if (!type) {
252 prom_printf("Console stdout lacks "
253 "device_type property.\n");
254 prom_halt();
255 }
256
257 if (strcmp(type, "display") && strcmp(type, "serial")) {
258 prom_printf("Console device_type is neither display "
259 "nor serial.\n");
260 prom_halt();
261 }
262
263 of_console_device = dp;
264
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;
274 }
275 } else {
276 const char *path;
277
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();
283 }
284 strcpy(of_console_path, path);
285 }
286 break;
287 }
288
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;
294 }
295
296 prom_printf(msg, of_console_path);
297 printk(msg, of_console_path);
298}
299
942a6bdd
DM
300void __init prom_build_devicetree(void)
301{
302 struct device_node **nextp;
303
6524036a 304 allnodes = prom_create_node(prom_root_node, NULL);
942a6bdd
DM
305 allnodes->path_component_name = "";
306 allnodes->full_name = "/";
307
308 nextp = &allnodes->allnext;
6524036a
DM
309 allnodes->child = prom_build_tree(allnodes,
310 prom_getchild(allnodes->node),
311 &nextp);
c73fcc84
DM
312 of_console_init();
313
942a6bdd
DM
314 printk("PROM: Built device tree with %u bytes of memory.\n",
315 prom_early_allocated);
316}
This page took 0.268761 seconds and 5 git commands to generate.