xref: /linux/arch/sparc/kernel/prom_32.c (revision 9c5968db9e625019a0ee5226c7eebef5519d366a)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Procedures for creating, accessing and interpreting the device tree.
4  *
5  * Paul Mackerras	August 1996.
6  * Copyright (C) 1996-2005 Paul Mackerras.
7  *
8  *  Adapted for 64bit PowerPC by Dave Engebretsen and Peter Bergner.
9  *    {engebret|bergner}@us.ibm.com
10  *
11  *  Adapted for sparc32 by David S. Miller davem@davemloft.net
12  */
13 
14 #include <linux/kernel.h>
15 #include <linux/types.h>
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <linux/memblock.h>
19 
20 #include <asm/prom.h>
21 #include <asm/oplib.h>
22 #include <asm/leon.h>
23 #include <asm/leon_amba.h>
24 
25 #include "prom.h"
26 
27 void * __init prom_early_alloc(unsigned long size)
28 {
29 	void *ret;
30 
31 	ret = memblock_alloc_or_panic(size, SMP_CACHE_BYTES);
32 
33 	prom_early_allocated += size;
34 
35 	return ret;
36 }
37 
38 /* The following routines deal with the black magic of fully naming a
39  * node.
40  *
41  * Certain well known named nodes are just the simple name string.
42  *
43  * Actual devices have an address specifier appended to the base name
44  * string, like this "foo@addr".  The "addr" can be in any number of
45  * formats, and the platform plus the type of the node determine the
46  * format and how it is constructed.
47  *
48  * For children of the ROOT node, the naming convention is fixed and
49  * determined by whether this is a sun4u or sun4v system.
50  *
51  * For children of other nodes, it is bus type specific.  So
52  * we walk up the tree until we discover a "device_type" property
53  * we recognize and we go from there.
54  */
55 static void __init sparc32_path_component(struct device_node *dp, char *tmp_buf)
56 {
57 	const char *name = of_get_property(dp, "name", NULL);
58 	struct linux_prom_registers *regs;
59 	struct property *rprop;
60 
61 	rprop = of_find_property(dp, "reg", NULL);
62 	if (!rprop)
63 		return;
64 
65 	regs = rprop->value;
66 	sprintf(tmp_buf, "%s@%x,%x",
67 		name,
68 		regs->which_io, regs->phys_addr);
69 }
70 
71 /* "name@slot,offset"  */
72 static void __init sbus_path_component(struct device_node *dp, char *tmp_buf)
73 {
74 	const char *name = of_get_property(dp, "name", NULL);
75 	struct linux_prom_registers *regs;
76 	struct property *prop;
77 
78 	prop = of_find_property(dp, "reg", NULL);
79 	if (!prop)
80 		return;
81 
82 	regs = prop->value;
83 	sprintf(tmp_buf, "%s@%x,%x",
84 		name,
85 		regs->which_io,
86 		regs->phys_addr);
87 }
88 
89 /* "name@devnum[,func]" */
90 static void __init pci_path_component(struct device_node *dp, char *tmp_buf)
91 {
92 	const char *name = of_get_property(dp, "name", NULL);
93 	struct linux_prom_pci_registers *regs;
94 	struct property *prop;
95 	unsigned int devfn;
96 
97 	prop = of_find_property(dp, "reg", NULL);
98 	if (!prop)
99 		return;
100 
101 	regs = prop->value;
102 	devfn = (regs->phys_hi >> 8) & 0xff;
103 	if (devfn & 0x07) {
104 		sprintf(tmp_buf, "%s@%x,%x",
105 			name,
106 			devfn >> 3,
107 			devfn & 0x07);
108 	} else {
109 		sprintf(tmp_buf, "%s@%x",
110 			name,
111 			devfn >> 3);
112 	}
113 }
114 
115 /* "name@addrhi,addrlo" */
116 static void __init ebus_path_component(struct device_node *dp, char *tmp_buf)
117 {
118 	const char *name = of_get_property(dp, "name", NULL);
119 	struct linux_prom_registers *regs;
120 	struct property *prop;
121 
122 	prop = of_find_property(dp, "reg", NULL);
123 	if (!prop)
124 		return;
125 
126 	regs = prop->value;
127 
128 	sprintf(tmp_buf, "%s@%x,%x",
129 		name,
130 		regs->which_io, regs->phys_addr);
131 }
132 
133 /* "name@irq,addrlo" */
134 static void __init ambapp_path_component(struct device_node *dp, char *tmp_buf)
135 {
136 	const char *name = of_get_property(dp, "name", NULL);
137 	struct amba_prom_registers *regs;
138 	unsigned int *intr;
139 	unsigned int reg0;
140 	struct property *prop;
141 	int interrupt = 0;
142 
143 	/* In order to get a unique ID in the device tree (multiple AMBA devices
144 	 * may have the same name) the node number is printed
145 	 */
146 	prop = of_find_property(dp, "reg", NULL);
147 	if (!prop) {
148 		reg0 = (unsigned int)dp->phandle;
149 	} else {
150 		regs = prop->value;
151 		reg0 = regs->phys_addr;
152 	}
153 
154 	/* Not all cores have Interrupt */
155 	prop = of_find_property(dp, "interrupts", NULL);
156 	if (!prop)
157 		intr = &interrupt; /* IRQ0 does not exist */
158 	else
159 		intr = prop->value;
160 
161 	sprintf(tmp_buf, "%s@%x,%x", name, *intr, reg0);
162 }
163 
164 static void __init __build_path_component(struct device_node *dp, char *tmp_buf)
165 {
166 	struct device_node *parent = dp->parent;
167 
168 	if (parent != NULL) {
169 		if (of_node_is_type(parent, "pci") ||
170 		    of_node_is_type(parent, "pciex"))
171 			return pci_path_component(dp, tmp_buf);
172 		if (of_node_is_type(parent, "sbus"))
173 			return sbus_path_component(dp, tmp_buf);
174 		if (of_node_is_type(parent, "ebus"))
175 			return ebus_path_component(dp, tmp_buf);
176 		if (of_node_is_type(parent, "ambapp"))
177 			return ambapp_path_component(dp, tmp_buf);
178 
179 		/* "isa" is handled with platform naming */
180 	}
181 
182 	/* Use platform naming convention.  */
183 	return sparc32_path_component(dp, tmp_buf);
184 }
185 
186 char * __init build_path_component(struct device_node *dp)
187 {
188 	const char *name = of_get_property(dp, "name", NULL);
189 	char tmp_buf[64], *n;
190 
191 	tmp_buf[0] = '\0';
192 	__build_path_component(dp, tmp_buf);
193 	if (tmp_buf[0] == '\0')
194 		strcpy(tmp_buf, name);
195 
196 	n = prom_early_alloc(strlen(tmp_buf) + 1);
197 	strcpy(n, tmp_buf);
198 
199 	return n;
200 }
201 
202 extern void restore_current(void);
203 
204 void __init of_console_init(void)
205 {
206 	char *msg = "OF stdout device is: %s\n";
207 	struct device_node *dp;
208 	unsigned long flags;
209 	const char *type;
210 	phandle node;
211 	int skip, tmp, fd;
212 
213 	of_console_path = prom_early_alloc(256);
214 
215 	switch (prom_vers) {
216 	case PROM_V0:
217 		skip = 0;
218 		switch (*romvec->pv_stdout) {
219 		case PROMDEV_SCREEN:
220 			type = "display";
221 			break;
222 
223 		case PROMDEV_TTYB:
224 			skip = 1;
225 			fallthrough;
226 
227 		case PROMDEV_TTYA:
228 			type = "serial";
229 			break;
230 
231 		default:
232 			prom_printf("Invalid PROM_V0 stdout value %u\n",
233 				    *romvec->pv_stdout);
234 			prom_halt();
235 		}
236 
237 		tmp = skip;
238 		for_each_node_by_type(dp, type) {
239 			if (!tmp--)
240 				break;
241 		}
242 		if (!dp) {
243 			prom_printf("Cannot find PROM_V0 console node.\n");
244 			prom_halt();
245 		}
246 		of_console_device = dp;
247 
248 		sprintf(of_console_path, "%pOF", dp);
249 		if (!strcmp(type, "serial")) {
250 			strcat(of_console_path,
251 			       (skip ? ":b" : ":a"));
252 		}
253 		break;
254 
255 	default:
256 	case PROM_V2:
257 	case PROM_V3:
258 		fd = *romvec->pv_v2bootargs.fd_stdout;
259 
260 		spin_lock_irqsave(&prom_lock, flags);
261 		node = (*romvec->pv_v2devops.v2_inst2pkg)(fd);
262 		restore_current();
263 		spin_unlock_irqrestore(&prom_lock, flags);
264 
265 		if (!node) {
266 			prom_printf("Cannot resolve stdout node from "
267 				    "instance %08x.\n", fd);
268 			prom_halt();
269 		}
270 		dp = of_find_node_by_phandle(node);
271 
272 		if (!of_node_is_type(dp, "display") &&
273 		    !of_node_is_type(dp, "serial")) {
274 			prom_printf("Console device_type is neither display "
275 				    "nor serial.\n");
276 			prom_halt();
277 		}
278 
279 		of_console_device = dp;
280 
281 		if (prom_vers == PROM_V2) {
282 			sprintf(of_console_path, "%pOF", dp);
283 			switch (*romvec->pv_stdout) {
284 			case PROMDEV_TTYA:
285 				strcat(of_console_path, ":a");
286 				break;
287 			case PROMDEV_TTYB:
288 				strcat(of_console_path, ":b");
289 				break;
290 			}
291 		} else {
292 			const char *path;
293 
294 			dp = of_find_node_by_path("/");
295 			path = of_get_property(dp, "stdout-path", NULL);
296 			if (!path) {
297 				prom_printf("No stdout-path in root node.\n");
298 				prom_halt();
299 			}
300 			strcpy(of_console_path, path);
301 		}
302 		break;
303 	}
304 
305 	of_console_options = strrchr(of_console_path, ':');
306 	if (of_console_options) {
307 		of_console_options++;
308 		if (*of_console_options == '\0')
309 			of_console_options = NULL;
310 	}
311 
312 	printk(msg, of_console_path);
313 }
314 
315 void __init of_fill_in_cpu_data(void)
316 {
317 }
318 
319 void __init irq_trans_init(struct device_node *dp)
320 {
321 }
322