blob: 05fb253305831095c660bb3aab077ac837f8f16e [file] [log] [blame]
David S. Miller942a6bd2006-06-23 15:53:31 -07001/*
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>
Konrad Eiselee63829d2009-08-17 00:13:32 +000027#include <asm/leon.h>
28#include <asm/leon_amba.h>
David S. Miller942a6bd2006-06-23 15:53:31 -070029
David S. Miller657f2012008-12-04 20:12:20 -080030#include "prom.h"
David S. Millerfb7cd9d2006-06-25 23:18:36 -070031
David S. Millerefeac2f2008-12-05 00:40:43 -080032void * __init prom_early_alloc(unsigned long size)
David S. Miller942a6bd2006-06-23 15:53:31 -070033{
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
David S. Miller942a6bd2006-06-23 15:53:31 -070045/* 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
Konrad Eiselee63829d2009-08-17 00:13:32 +0000136/* "name:vendor:device@irq,addrlo" */
137static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
138{
Daniel Hellstrom9663eb22011-01-04 01:41:33 +0000139 struct amba_prom_registers *regs;
140 unsigned int *intr, *device, *vendor, reg0;
Konrad Eiselee63829d2009-08-17 00:13:32 +0000141 struct property *prop;
Daniel Hellstrom9663eb22011-01-04 01:41:33 +0000142 int interrupt = 0;
Konrad Eiselee63829d2009-08-17 00:13:32 +0000143
Daniel Hellstrom9663eb22011-01-04 01:41:33 +0000144 /* 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
146 */
Konrad Eiselee63829d2009-08-17 00:13:32 +0000147 prop = of_find_property(dp, "reg", NULL);
Daniel Hellstrom9663eb22011-01-04 01:41:33 +0000148 if (!prop) {
149 reg0 = (unsigned int)dp->phandle;
150 } else {
151 regs = prop->value;
152 reg0 = regs->phys_addr;
153 }
154
155 /* Not all cores have Interrupt */
Konrad Eiselee63829d2009-08-17 00:13:32 +0000156 prop = of_find_property(dp, "interrupts", NULL);
157 if (!prop)
Daniel Hellstrom9663eb22011-01-04 01:41:33 +0000158 intr = &interrupt; /* IRQ0 does not exist */
159 else
160 intr = prop->value;
161
Konrad Eiselee63829d2009-08-17 00:13:32 +0000162 prop = of_find_property(dp, "vendor", NULL);
163 if (!prop)
164 return;
165 vendor = prop->value;
166 prop = of_find_property(dp, "device", NULL);
167 if (!prop)
168 return;
169 device = prop->value;
170
171 sprintf(tmp_buf, "%s:%d:%d@%x,%x",
172 dp->name, *vendor, *device,
Daniel Hellstrom9663eb22011-01-04 01:41:33 +0000173 *intr, reg0);
Konrad Eiselee63829d2009-08-17 00:13:32 +0000174}
175
David S. Miller942a6bd2006-06-23 15:53:31 -0700176static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
177{
178 struct device_node *parent = dp->parent;
179
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);
Konrad Eiselee63829d2009-08-17 00:13:32 +0000188 if (!strcmp(parent->type, "ambapp"))
189 return ambapp_path_component(dp, tmp_buf);
David S. Miller942a6bd2006-06-23 15:53:31 -0700190
191 /* "isa" is handled with platform naming */
192 }
193
194 /* Use platform naming convention. */
195 return sparc32_path_component(dp, tmp_buf);
196}
197
David S. Miller65240362008-12-05 01:21:41 -0800198char * __init build_path_component(struct device_node *dp)
David S. Miller942a6bd2006-06-23 15:53:31 -0700199{
200 char tmp_buf[64], *n;
201
202 tmp_buf[0] = '\0';
203 __build_path_component(dp, tmp_buf);
204 if (tmp_buf[0] == '\0')
205 strcpy(tmp_buf, dp->name);
206
207 n = prom_early_alloc(strlen(tmp_buf) + 1);
208 strcpy(n, tmp_buf);
209
210 return n;
211}
212
David S. Millerc73fcc82007-07-20 16:59:26 -0700213extern void restore_current(void);
214
David S. Miller23dc7582008-12-05 18:16:48 -0800215void __init of_console_init(void)
David S. Millerc73fcc82007-07-20 16:59:26 -0700216{
217 char *msg = "OF stdout device is: %s\n";
218 struct device_node *dp;
219 unsigned long flags;
220 const char *type;
221 phandle node;
David S. Millerf623f382007-07-29 02:10:37 -0700222 int skip, tmp, fd;
David S. Millerc73fcc82007-07-20 16:59:26 -0700223
224 of_console_path = prom_early_alloc(256);
225
226 switch (prom_vers) {
227 case PROM_V0:
David S. Millerc73fcc82007-07-20 16:59:26 -0700228 skip = 0;
229 switch (*romvec->pv_stdout) {
230 case PROMDEV_SCREEN:
231 type = "display";
232 break;
233
234 case PROMDEV_TTYB:
235 skip = 1;
236 /* FALLTHRU */
237
238 case PROMDEV_TTYA:
239 type = "serial";
240 break;
241
242 default:
243 prom_printf("Invalid PROM_V0 stdout value %u\n",
244 *romvec->pv_stdout);
245 prom_halt();
246 }
247
David S. Millerf623f382007-07-29 02:10:37 -0700248 tmp = skip;
David S. Millerc73fcc82007-07-20 16:59:26 -0700249 for_each_node_by_type(dp, type) {
David S. Millerf623f382007-07-29 02:10:37 -0700250 if (!tmp--)
David S. Millerc73fcc82007-07-20 16:59:26 -0700251 break;
252 }
253 if (!dp) {
254 prom_printf("Cannot find PROM_V0 console node.\n");
255 prom_halt();
256 }
257 of_console_device = dp;
258
259 strcpy(of_console_path, dp->full_name);
260 if (!strcmp(type, "serial")) {
261 strcat(of_console_path,
262 (skip ? ":b" : ":a"));
263 }
264 break;
265
266 default:
267 case PROM_V2:
268 case PROM_V3:
269 fd = *romvec->pv_v2bootargs.fd_stdout;
270
271 spin_lock_irqsave(&prom_lock, flags);
272 node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
273 restore_current();
274 spin_unlock_irqrestore(&prom_lock, flags);
275
276 if (!node) {
277 prom_printf("Cannot resolve stdout node from "
278 "instance %08x.\n", fd);
279 prom_halt();
280 }
281 dp = of_find_node_by_phandle(node);
282 type = of_get_property(dp, "device_type", NULL);
283
284 if (!type) {
285 prom_printf("Console stdout lacks "
286 "device_type property.\n");
287 prom_halt();
288 }
289
290 if (strcmp(type, "display") && strcmp(type, "serial")) {
291 prom_printf("Console device_type is neither display "
292 "nor serial.\n");
293 prom_halt();
294 }
295
296 of_console_device = dp;
297
298 if (prom_vers == PROM_V2) {
299 strcpy(of_console_path, dp->full_name);
300 switch (*romvec->pv_stdout) {
301 case PROMDEV_TTYA:
302 strcat(of_console_path, ":a");
303 break;
304 case PROMDEV_TTYB:
305 strcat(of_console_path, ":b");
306 break;
307 }
308 } else {
309 const char *path;
310
311 dp = of_find_node_by_path("/");
312 path = of_get_property(dp, "stdout-path", NULL);
313 if (!path) {
314 prom_printf("No stdout-path in root node.\n");
315 prom_halt();
316 }
317 strcpy(of_console_path, path);
318 }
319 break;
320 }
321
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;
327 }
328
329 prom_printf(msg, of_console_path);
330 printk(msg, of_console_path);
331}
332
David S. Miller23dc7582008-12-05 18:16:48 -0800333void __init of_fill_in_cpu_data(void)
David S. Miller942a6bd2006-06-23 15:53:31 -0700334{
David S. Miller942a6bd2006-06-23 15:53:31 -0700335}
David S. Millerbf944c32008-12-07 00:46:33 -0800336
337void __init irq_trans_init(struct device_node *dp)
338{
339}