xref: /openbmc/linux/arch/sparc/kernel/prom_32.c (revision 95777591)
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/memblock.h>
23 
24 #include <asm/prom.h>
25 #include <asm/oplib.h>
26 #include <asm/leon.h>
27 #include <asm/leon_amba.h>
28 
29 #include "prom.h"
30 
31 void * __init prom_early_alloc(unsigned long size)
32 {
33 	void *ret;
34 
35 	ret = memblock_alloc_from(size, SMP_CACHE_BYTES, 0UL);
36 	if (ret != NULL)
37 		memset(ret, 0, size);
38 
39 	prom_early_allocated += size;
40 
41 	return ret;
42 }
43 
44 /* The following routines deal with the black magic of fully naming a
45  * node.
46  *
47  * Certain well known named nodes are just the simple name string.
48  *
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.
53  *
54  * For children of the ROOT node, the naming convention is fixed and
55  * determined by whether this is a sun4u or sun4v system.
56  *
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.
60  */
61 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
62 {
63 	const char *name = of_get_property(dp, "name", NULL);
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 		name,
74 		regs->which_io, regs->phys_addr);
75 }
76 
77 /* "name@slot,offset"  */
78 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
79 {
80 	const char *name = of_get_property(dp, "name", NULL);
81 	struct linux_prom_registers *regs;
82 	struct property *prop;
83 
84 	prop = of_find_property(dp, "reg", NULL);
85 	if (!prop)
86 		return;
87 
88 	regs = prop->value;
89 	sprintf(tmp_buf, "%s@%x,%x",
90 		name,
91 		regs->which_io,
92 		regs->phys_addr);
93 }
94 
95 /* "name@devnum[,func]" */
96 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
97 {
98 	const char *name = of_get_property(dp, "name", NULL);
99 	struct linux_prom_pci_registers *regs;
100 	struct property *prop;
101 	unsigned int devfn;
102 
103 	prop = of_find_property(dp, "reg", NULL);
104 	if (!prop)
105 		return;
106 
107 	regs = prop->value;
108 	devfn = (regs->phys_hi >> 8) & 0xff;
109 	if (devfn & 0x07) {
110 		sprintf(tmp_buf, "%s@%x,%x",
111 			name,
112 			devfn >> 3,
113 			devfn & 0x07);
114 	} else {
115 		sprintf(tmp_buf, "%s@%x",
116 			name,
117 			devfn >> 3);
118 	}
119 }
120 
121 /* "name@addrhi,addrlo" */
122 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
123 {
124 	const char *name = of_get_property(dp, "name", NULL);
125 	struct linux_prom_registers *regs;
126 	struct property *prop;
127 
128 	prop = of_find_property(dp, "reg", NULL);
129 	if (!prop)
130 		return;
131 
132 	regs = prop->value;
133 
134 	sprintf(tmp_buf, "%s@%x,%x",
135 		name,
136 		regs->which_io, regs->phys_addr);
137 }
138 
139 /* "name:vendor:device@irq,addrlo" */
140 static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
141 {
142 	const char *name = of_get_property(dp, "name", NULL);
143 	struct amba_prom_registers *regs;
144 	unsigned int *intr, *device, *vendor, reg0;
145 	struct property *prop;
146 	int interrupt = 0;
147 
148 	/* In order to get a unique ID in the device tree (multiple AMBA devices
149 	 * may have the same name) the node number is printed
150 	 */
151 	prop = of_find_property(dp, "reg", NULL);
152 	if (!prop) {
153 		reg0 = (unsigned int)dp->phandle;
154 	} else {
155 		regs = prop->value;
156 		reg0 = regs->phys_addr;
157 	}
158 
159 	/* Not all cores have Interrupt */
160 	prop = of_find_property(dp, "interrupts", NULL);
161 	if (!prop)
162 		intr = &interrupt; /* IRQ0 does not exist */
163 	else
164 		intr = prop->value;
165 
166 	prop = of_find_property(dp, "vendor", NULL);
167 	if (!prop)
168 		return;
169 	vendor = prop->value;
170 	prop = of_find_property(dp, "device", NULL);
171 	if (!prop)
172 		return;
173 	device = prop->value;
174 
175 	sprintf(tmp_buf, "%s:%d:%d@%x,%x",
176 		name, *vendor, *device,
177 		*intr, reg0);
178 }
179 
180 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
181 {
182 	struct device_node *parent = dp->parent;
183 
184 	if (parent != NULL) {
185 		if (of_node_is_type(parent, "pci") ||
186 		    of_node_is_type(parent, "pciex"))
187 			return pci_path_component(dp, tmp_buf);
188 		if (of_node_is_type(parent, "sbus"))
189 			return sbus_path_component(dp, tmp_buf);
190 		if (of_node_is_type(parent, "ebus"))
191 			return ebus_path_component(dp, tmp_buf);
192 		if (of_node_is_type(parent, "ambapp"))
193 			return ambapp_path_component(dp, tmp_buf);
194 
195 		/* "isa" is handled with platform naming */
196 	}
197 
198 	/* Use platform naming convention.  */
199 	return sparc32_path_component(dp, tmp_buf);
200 }
201 
202 char * __init build_path_component(struct device_node *dp)
203 {
204 	const char *name = of_get_property(dp, "name", NULL);
205 	char tmp_buf[64], *n;
206 
207 	tmp_buf[0] = '\0';
208 	__build_path_component(dp, tmp_buf);
209 	if (tmp_buf[0] == '\0')
210 		strcpy(tmp_buf, name);
211 
212 	n = prom_early_alloc(strlen(tmp_buf) + 1);
213 	strcpy(n, tmp_buf);
214 
215 	return n;
216 }
217 
218 extern void restore_current(void);
219 
220 void __init of_console_init(void)
221 {
222 	char *msg = "OF stdout device is: %s\n";
223 	struct device_node *dp;
224 	unsigned long flags;
225 	const char *type;
226 	phandle node;
227 	int skip, tmp, fd;
228 
229 	of_console_path = prom_early_alloc(256);
230 
231 	switch (prom_vers) {
232 	case PROM_V0:
233 		skip = 0;
234 		switch (*romvec->pv_stdout) {
235 		case PROMDEV_SCREEN:
236 			type = "display";
237 			break;
238 
239 		case PROMDEV_TTYB:
240 			skip = 1;
241 			/* FALLTHRU */
242 
243 		case PROMDEV_TTYA:
244 			type = "serial";
245 			break;
246 
247 		default:
248 			prom_printf("Invalid PROM_V0 stdout value %u\n",
249 				    *romvec->pv_stdout);
250 			prom_halt();
251 		}
252 
253 		tmp = skip;
254 		for_each_node_by_type(dp, type) {
255 			if (!tmp--)
256 				break;
257 		}
258 		if (!dp) {
259 			prom_printf("Cannot find PROM_V0 console node.\n");
260 			prom_halt();
261 		}
262 		of_console_device = dp;
263 
264 		sprintf(of_console_path, "%pOF", dp);
265 		if (!strcmp(type, "serial")) {
266 			strcat(of_console_path,
267 			       (skip ? ":b" : ":a"));
268 		}
269 		break;
270 
271 	default:
272 	case PROM_V2:
273 	case PROM_V3:
274 		fd = *romvec->pv_v2bootargs.fd_stdout;
275 
276 		spin_lock_irqsave(&prom_lock, flags);
277 		node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
278 		restore_current();
279 		spin_unlock_irqrestore(&prom_lock, flags);
280 
281 		if (!node) {
282 			prom_printf("Cannot resolve stdout node from "
283 				    "instance %08x.\n", fd);
284 			prom_halt();
285 		}
286 		dp = of_find_node_by_phandle(node);
287 
288 		if (!of_node_is_type(dp, "display") &&
289 		    !of_node_is_type(dp, "serial")) {
290 			prom_printf("Console device_type is neither display "
291 				    "nor serial.\n");
292 			prom_halt();
293 		}
294 
295 		of_console_device = dp;
296 
297 		if (prom_vers == PROM_V2) {
298 			sprintf(of_console_path, "%pOF", dp);
299 			switch (*romvec->pv_stdout) {
300 			case PROMDEV_TTYA:
301 				strcat(of_console_path, ":a");
302 				break;
303 			case PROMDEV_TTYB:
304 				strcat(of_console_path, ":b");
305 				break;
306 			}
307 		} else {
308 			const char *path;
309 
310 			dp = of_find_node_by_path("/");
311 			path = of_get_property(dp, "stdout-path", NULL);
312 			if (!path) {
313 				prom_printf("No stdout-path in root node.\n");
314 				prom_halt();
315 			}
316 			strcpy(of_console_path, path);
317 		}
318 		break;
319 	}
320 
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;
326 	}
327 
328 	printk(msg, of_console_path);
329 }
330 
331 void __init of_fill_in_cpu_data(void)
332 {
333 }
334 
335 void __init irq_trans_init(struct device_node *dp)
336 {
337 }
338