xref: /netbsd-src/sys/arch/ofppc/ofppc/machdep.c (revision bae23eb724b13bce2e7d6a249f34c7840fc89232)
1 /*	$NetBSD: machdep.c,v 1.121 2024/12/16 11:52:43 martin Exp $	*/
2 /*-
3  * Copyright (c) 2007 The NetBSD Foundation, Inc.
4  * All rights reserved.
5  *
6  * This code is derived from software contributed to The NetBSD Foundation
7  * by Tim Rightnour
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
19  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
20  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
22  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include <sys/cdefs.h>
32 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.121 2024/12/16 11:52:43 martin Exp $");
33 
34 #include "opt_ofwoea.h"
35 
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/buf.h>
39 #include <sys/boot_flag.h>
40 #include <sys/mount.h>
41 #include <sys/kernel.h>
42 #include <sys/device.h>
43 
44 #include <uvm/uvm_extern.h>
45 
46 #include <dev/ofw/openfirm.h>
47 #include <dev/cons.h>
48 
49 #include <machine/autoconf.h>
50 #include <machine/pmap.h>
51 #include <machine/powerpc.h>
52 #include <machine/trap.h>
53 #include <sys/bus.h>
54 #include <machine/isa_machdep.h>
55 
56 #include <powerpc/spr.h>
57 #include <powerpc/oea/spr.h>
58 #include <powerpc/oea/bat.h>
59 #include <powerpc/ofw_cons.h>
60 #include <powerpc/rtas.h>
61 
62 #include "com.h"
63 #if (NCOM > 0)
64 #include <sys/termios.h>
65 #include <dev/ic/comreg.h>
66 #include <dev/ic/comvar.h>
67 #endif
68 #include "rtas.h"
69 
70 extern struct pmap ofw_pmap;
71 extern char bootpath[256];
72 
73 extern u_int l2cr_config;
74 #if (NRTAS > 0)
75 extern int machine_has_rtas;
76 #endif
77 
78 struct model_data modeldata;
79 static void model_init(void);
80 
81 #ifdef OFWOEA_DEBUG
82 #define	DPRINTF printf
83 #else
84 #define	DPRINTF while (0) printf
85 #endif
86 
87 /*
88  * Scan the device tree for ranges, and return them as bitmap 0..15
89  */
90 static uint16_t
91 ranges_bitmap(int node, uint16_t bitmap)
92 {
93 	int child, mlen, acells, scells, reclen, i, j;
94 	uint32_t addr, len, map[160];
95 
96 	for (child = OF_child(node); child; child = OF_peer(child)) {
97 		mlen = OF_getprop(child, "ranges", map, sizeof(map));
98 		if (mlen == -1)
99 	  		goto noranges;
100 
101 		j = OF_getprop(child, "#address-cells", &acells,
102 		    sizeof(acells));
103 		if (j == -1)
104 			goto noranges;
105 
106 		j = OF_getprop(child, "#size-cells", &scells,
107 		    sizeof(scells));
108 		if (j == -1)
109 			goto noranges;
110 
111 		reclen = acells + modeldata.ranges_offset + scells;
112 
113 		for (i = 0; i < (mlen / 4) / reclen; i++) {
114 			addr = map[reclen * i + acells];
115 			len = map[reclen * i + reclen - 1];
116 			for (j = 0; j < len / 0x10000000; j++)
117 				bitmap |= 1 << ((addr+j*0x10000000) >> 28);
118 			bitmap |= 1 << (addr >> 28);
119 		}
120  noranges:
121 		bitmap |= ranges_bitmap(child, bitmap);
122 		continue;
123 	}
124 	return bitmap;
125 }
126 
127 void
128 initppc(u_int startkernel, u_int endkernel, char *args)
129 {
130 	int node, i;
131 	uint16_t bitmap;
132 
133 	node = OF_finddevice("/");
134 	if (node != -1) {
135 		i = OF_getprop(node, "model", model_name, sizeof(model_name));
136 		if (i == -1) {
137 			OF_getprop(node, "name", model_name,
138 			    sizeof(model_name));
139 		}
140 	}
141 	model_init();
142 
143 	if ((oeacpufeat & OEACPU_NOBAT) == 0) {
144 		node = OF_finddevice("/");
145 
146 		bitmap = ranges_bitmap(node, 0);
147 		oea_batinit(0);
148 
149 		for (i = 1; i < 0x10; i++) {
150 			/* skip the three vital SR regions */
151 			if (i == USER_SR || i == KERNEL_SR || i == KERNEL2_SR) {
152 				continue;
153 			}
154 			if (bitmap & (1 << i)) {
155 				oea_iobat_add(0x10000000 * i, BAT_BL_256M);
156 				DPRINTF("Batmapped 256M at 0x%x\n",
157 				    0x10000000 * i);
158 			}
159 		}
160 	}
161 
162 #if defined(MULTIPROCESSOR)
163 	int l;
164 	char cpupath[32];
165 
166 	extern void cpu_spinstart(u_int);
167 	extern volatile u_int cpu_spinstart_ack;
168 
169 	for (i = 1; i < CPU_MAXNUM; i++) {
170 		snprintf(cpupath, sizeof(cpupath), "/cpus/@%x", i);
171 		node = OF_finddevice(cpupath);
172 		if (node <= 0)
173 			continue;
174 		aprint_verbose("Starting up CPU %d %s\n", i, cpupath);
175 		OF_start_cpu(node, (u_int)cpu_spinstart, i);
176 		for (l = 0; l < 100000000; l++) {
177 			if (cpu_spinstart_ack == i) {
178 				aprint_verbose("CPU %d spun up.\n", i);
179 				break;
180 			}
181 			__asm volatile ("sync");
182 		}
183 	}
184 #endif /* MULTIPROCESSOR */
185 
186 	ofwoea_initppc(startkernel, endkernel, args);
187 }
188 
189 /* perform model-specific actions at initppc() */
190 static void
191 model_init(void)
192 {
193 	int qhandle, phandle, j;
194 
195 	memset(&modeldata, 0, sizeof(struct model_data));
196 	/* provide sane defaults */
197 	for (j=0; j < MAX_PCI_BUSSES; j++) {
198 		modeldata.pciiodata[j].start = 0x00008000;
199 		modeldata.pciiodata[j].limit = 0x0000ffff;
200 	}
201 	modeldata.ranges_offset = 1;
202 
203 	if (strncmp(model_name, "FirePower,", 10) == 0) {
204 		modeldata.ranges_offset = 0;
205 	}
206 	if (strcmp(model_name, "MOT,PowerStack_II_Pro4000") == 0) {
207 		modeldata.ranges_offset = 0;
208 	}
209 
210 	/* 7044-270 and 7044-170 */
211 	if (strncmp(model_name, "IBM,7044", 8) == 0) {
212 		for (j=0; j < MAX_PCI_BUSSES; j++) {
213 			modeldata.pciiodata[j].start = 0x00fff000;
214 			modeldata.pciiodata[j].limit = 0x00ffffff;
215 		}
216 	}
217 
218 	/* Pegasos1, Pegasos2 */
219 	if (strncmp(model_name, "Pegasos", 7) == 0) {
220 		static uint16_t modew[] = { 640, 800, 1024, 1280, 0 };
221 		static uint16_t modeh[] = { 480, 600, 768, 1024, 0 };
222 		uint32_t width, height, mode, fbaddr;
223 		char buf[32];
224 		int i;
225 
226 		modeldata.pciiodata[0].start = 0x00001400;
227 		modeldata.pciiodata[0].limit = 0x0000ffff;
228 
229 		/* the pegasos doesn't bother to set the L2 cache up */
230 		l2cr_config = L2CR_L2PE;
231 
232 		/* fix the device_type property of a graphics card */
233 		for (qhandle = OF_peer(0); qhandle; qhandle = phandle) {
234 			if (OF_getprop(qhandle, "name", buf, sizeof buf) > 0
235 			    && strncmp(buf, "display", 7) == 0) {
236 				OF_setprop(qhandle, "device_type", "display", 8);
237 				break;
238 			}
239 			if ((phandle = OF_child(qhandle)))
240 				continue;
241 			while (qhandle) {
242 				if ((phandle = OF_peer(qhandle)))
243 					break;
244 				qhandle = OF_parent(qhandle);
245 			}
246 		}
247 
248 		/*
249 		 * Get screen width/height and switch to framebuffer mode.
250 		 * The default dimensions are: 800 x 600
251 		 */
252 		OF_interpret("screen-width", 0, 1, &width);
253 		if (width == 0)
254 			width = 800;
255 
256 		OF_interpret("screen-height", 0, 1, &height);
257 		if (height == 0)
258 			height = 600;
259 
260 		/* find VESA mode */
261 		for (i = 0, mode = 0; modew[i] != 0; i++) {
262 			if (modew[i] == width && modeh[i] == height) {
263 				mode = 0x101 + 2 * i;
264 				break;
265 			}
266 		}
267 		if (!mode) {
268 			mode = 0x103;
269 			width = 800;
270 			height = 600;
271 		}
272 
273 		/* init frame buffer mode */
274 		snprintf(buf, sizeof(buf), "%x vesa-set-mode", mode);
275 		OF_interpret(buf, 0, 0);
276 
277 		/* set dimensions and frame buffer address in OFW */
278 		snprintf(buf, sizeof(buf), "%x to screen-width", width);
279 		OF_interpret(buf, 0, 0);
280 		snprintf(buf, sizeof(buf), "%x to screen-height", height);
281 		OF_interpret(buf, 0, 0);
282 		OF_interpret("vesa-frame-buffer-adr", 0, 1, &fbaddr);
283 		if (fbaddr != 0) {
284 			snprintf(buf, sizeof(buf), "%x to frame-buffer-adr", fbaddr);
285 			OF_interpret(buf, 0, 0);
286 		}
287 	}
288 }
289 
290 void
291 cpu_startup(void)
292 {
293 	oea_startup(model_name[0] ? model_name : NULL);
294 	bus_space_mallocok();
295 }
296 
297 
298 void
299 consinit(void)
300 {
301 	ofwoea_consinit();
302 }
303 
304 
305 void
306 dumpsys(void)
307 {
308 	aprint_normal("dumpsys: TBD\n");
309 }
310 
311 /*
312  * Halt or reboot the machine after syncing/dumping according to howto.
313  */
314 
315 void
316 cpu_reboot(int howto, char *what)
317 {
318 	static int syncing;
319 	static char str[256];
320 	char *ap = str, *ap1 = ap;
321 #if (NRTAS > 0)
322 	int junk;
323 #endif
324 
325 	boothowto = howto;
326 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
327 		syncing = 1;
328 		vfs_shutdown();         /* sync */
329 	}
330 	splhigh();
331 	if (howto & RB_HALT) {
332 		doshutdownhooks();
333 		pmf_system_shutdown(boothowto);
334 		aprint_normal("halted\n\n");
335 #if (NRTAS > 0)
336 		if ((howto & 0x800) && machine_has_rtas &&
337 		    rtas_has_func(RTAS_FUNC_POWER_OFF))
338 			rtas_call(RTAS_FUNC_POWER_OFF, 2, 1, 0, 0, &junk);
339 #endif
340 		ppc_exit();
341 	}
342 	if (!cold && (howto & RB_DUMP))
343 		oea_dumpsys();
344 	doshutdownhooks();
345 
346 	pmf_system_shutdown(boothowto);
347 	aprint_normal("rebooting\n\n");
348 
349 #if (NRTAS > 0)
350 	if (machine_has_rtas && rtas_has_func(RTAS_FUNC_SYSTEM_REBOOT)) {
351 		rtas_call(RTAS_FUNC_SYSTEM_REBOOT, 0, 1, &junk);
352 		for(;;);
353 	}
354 #endif
355 	if (what && *what) {
356 		if (strlen(what) > sizeof str - 5)
357 			aprint_normal("boot string too large, ignored\n");
358 		else {
359 			strcpy(str, what);
360 			ap1 = ap = str + strlen(str);
361 			*ap++ = ' ';
362 		}
363 	}
364 	*ap++ = '-';
365 	if (howto & RB_SINGLE)
366 		*ap++ = 's';
367 	if (howto & RB_KDB)
368 		*ap++ = 'd';
369 	*ap++ = 0;
370 	if (ap[-2] == '-')
371 		*ap1 = 0;
372 	ppc_boot(str);
373 }
374 
375 /*
376  */
377 
378 #define divrnd(n, q)	(((n)*2/(q)+1)/2)
379 
380 void
381 ofppc_init_comcons(int isa_node)
382 {
383 #if (NCOM > 0)
384 	char name[64];
385 	uint32_t reg[2], comfreq;
386 	uint8_t dll, dlm;
387 	int speed, rate, err, com_node, child;
388 	bus_space_handle_t comh;
389 
390 	/* if we have a serial cons, we have work to do */
391 	memset(name, 0, sizeof(name));
392 	OF_getprop(console_node, "device_type", name, sizeof(name));
393 	if (strcmp(name, "serial") != 0)
394 		return;
395 
396 	/* scan ISA children for serial devices to match our console */
397 	com_node = -1;
398 	for (child = OF_child(isa_node); child; child = OF_peer(child)) {
399 		memset(name, 0, sizeof(name));
400 		OF_getprop(child, "device_type", name, sizeof(name));
401 		if (strcmp(name, "serial") == 0) {
402 			/*
403 			 * Serial device even matches our console_node?
404 			 * Then we're done!
405 			 */
406 			if (child == console_node) {
407 				com_node = child;
408 				break;
409 			}
410 			/* remember first serial device found */
411 			if (com_node == -1)
412 				com_node = child;
413 		}
414 	}
415 
416 	if (com_node == -1)
417 		return;
418 
419 	if (OF_getprop(com_node, "reg", reg, sizeof(reg)) == -1)
420 		return;
421 
422 	if (OF_getprop(com_node, "clock-frequency", &comfreq, 4) == -1)
423 		comfreq = 0;
424 
425 	if (comfreq == 0)
426 		comfreq = COM_FREQ;
427 
428 	/* we need to BSM this, and then undo that before calling
429 	 * comcnattach.
430 	 */
431 
432 	if (bus_space_map(&genppc_isa_io_space_tag, reg[1], 8, 0, &comh) != 0)
433 		panic("Can't map isa serial\n");
434 
435 	bus_space_write_1(&genppc_isa_io_space_tag, comh, com_cfcr, LCR_DLAB);
436 	dll = bus_space_read_1(&genppc_isa_io_space_tag, comh, com_dlbl);
437 	dlm = bus_space_read_1(&genppc_isa_io_space_tag, comh, com_dlbh);
438 	rate = dll | (dlm << 8);
439 	bus_space_write_1(&genppc_isa_io_space_tag, comh, com_cfcr, LCR_8BITS);
440 	speed = divrnd((comfreq / 16), rate);
441 	err = speed - (speed + 150)/300 * 300;
442 	speed -= err;
443 	if (err < 0)
444 		err = -err;
445 	if (err > 50)
446 		speed = 9600;
447 
448 	bus_space_unmap(&genppc_isa_io_space_tag, comh, 8);
449 
450 	/* Now we can attach the comcons */
451 	aprint_verbose("Switching to COM console at speed %d", speed);
452 	if (comcnattach(&genppc_isa_io_space_tag, reg[1],
453 	    speed, comfreq, COM_TYPE_NORMAL,
454 	    ((TTYDEF_CFLAG & ~(CSIZE | CSTOPB | PARENB)) | CS8)))
455 		panic("Can't init serial console");
456 	aprint_verbose("\n");
457 #endif /*NCOM*/
458 }
459 
460 void
461 copy_disp_props(device_t dev, int node, prop_dictionary_t dict)
462 {
463 	uint32_t temp;
464 	char typestr[32];
465 
466 	memset(typestr, 0, sizeof(typestr));
467 	OF_getprop(console_node, "device_type", typestr, sizeof(typestr));
468 	if (strcmp(typestr, "serial") != 0) {
469 		/* this is our console, when we don't have a serial console */
470 		prop_dictionary_set_bool(dict, "is_console", 1);
471 	}
472 
473 	if (!of_to_uint32_prop(dict, node, "width", "width")) {
474 
475 		OF_interpret("screen-width", 0, 1, &temp);
476 		prop_dictionary_set_uint32(dict, "width", temp);
477 	}
478 	if (!of_to_uint32_prop(dict, node, "height", "height")) {
479 
480 		OF_interpret("screen-height", 0, 1, &temp);
481 		prop_dictionary_set_uint32(dict, "height", temp);
482 	}
483 	of_to_uint32_prop(dict, node, "linebytes", "linebytes");
484 	if (!of_to_uint32_prop(dict, node, "depth", "depth")) {
485 		/*
486 		 * XXX we should check linebytes vs. width but those
487 		 * FBs that don't have a depth property ( /chaos/control... )
488 		 * won't have linebytes either
489 		 */
490 		prop_dictionary_set_uint32(dict, "depth", 8);
491 	}
492 	if (!of_to_uint32_prop(dict, node, "address", "address")) {
493 		uint32_t fbaddr = 0;
494 
495 		OF_interpret("frame-buffer-adr", 0, 1, &fbaddr);
496 		if (fbaddr != 0)
497 			prop_dictionary_set_uint32(dict, "address", fbaddr);
498 	}
499 }
500