xref: /netbsd-src/sys/arch/evbmips/malta/machdep.c (revision f36002f244a49908fef9cba8789032bdbf48d572)
1 /*	$NetBSD: machdep.c,v 1.47 2024/03/05 14:15:30 thorpej Exp $	*/
2 
3 /*
4  * Copyright 2001, 2002 Wasabi Systems, Inc.
5  * All rights reserved.
6  *
7  * Written by Jason R. Thorpe and Simon Burge for Wasabi Systems, Inc.
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  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed for the NetBSD Project by
20  *      Wasabi Systems, Inc.
21  * 4. The name of Wasabi Systems, Inc. may not be used to endorse
22  *    or promote products derived from this software without specific prior
23  *    written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY WASABI SYSTEMS, INC. ``AS IS'' AND
26  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
27  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL WASABI SYSTEMS, INC
29  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35  * POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 /*
39  * Copyright (c) 1988 University of Utah.
40  * Copyright (c) 1992, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * This code is derived from software contributed to Berkeley by
44  * the Systems Programming Group of the University of Utah Computer
45  * Science Department, The Mach Operating System project at
46  * Carnegie-Mellon University and Ralph Campbell.
47  *
48  * Redistribution and use in source and binary forms, with or without
49  * modification, are permitted provided that the following conditions
50  * are met:
51  * 1. Redistributions of source code must retain the above copyright
52  *    notice, this list of conditions and the following disclaimer.
53  * 2. Redistributions in binary form must reproduce the above copyright
54  *    notice, this list of conditions and the following disclaimer in the
55  *    documentation and/or other materials provided with the distribution.
56  * 3. Neither the name of the University nor the names of its contributors
57  *    may be used to endorse or promote products derived from this software
58  *    without specific prior written permission.
59  *
60  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70  * SUCH DAMAGE.
71  *
72  *	@(#)machdep.c   8.3 (Berkeley) 1/12/94
73  *	from: Utah Hdr: machdep.c 1.63 91/04/24
74  */
75 
76 #include <sys/cdefs.h>
77 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.47 2024/03/05 14:15:30 thorpej Exp $");
78 
79 #include "opt_ddb.h"
80 #include "opt_execfmt.h"
81 #include "opt_modular.h"
82 
83 #include <sys/param.h>
84 #include <sys/boot_flag.h>
85 #include <sys/buf.h>
86 #include <sys/cpu.h>
87 #include <sys/device.h>
88 #include <sys/kcore.h>
89 #include <sys/kernel.h>
90 #include <sys/ksyms.h>
91 #include <sys/mount.h>
92 #include <sys/reboot.h>
93 #include <sys/systm.h>
94 #include <sys/termios.h>
95 
96 #include <uvm/uvm_extern.h>
97 
98 #include <dev/cons.h>
99 
100 #include "ksyms.h"
101 
102 #if NKSYMS || defined(DDB) || defined(MODULAR)
103 #include <mips/db_machdep.h>
104 #include <ddb/db_extern.h>
105 #endif
106 
107 #include <machine/yamon.h>
108 
109 #include <mips/locore.h>
110 #include <mips/psl.h>
111 
112 #include <evbmips/malta/autoconf.h>
113 #include <evbmips/malta/maltareg.h>
114 #include <evbmips/malta/maltavar.h>
115 
116 #include "com.h"
117 #if NCOM > 0
118 #include <dev/ic/comreg.h>
119 #include <dev/ic/comvar.h>
120 
121 int	comcnrate = 38400;	/* XXX should be config option */
122 #endif /* NCOM > 0 */
123 
124 
125 #define REGVAL(x)       *((volatile u_int32_t *)(MIPS_PHYS_TO_KSEG1((x))))
126 
127 struct malta_config malta_configuration;
128 
129 /* Maps for VM objects. */
130 struct vm_map *phys_map = NULL;
131 
132 int	netboot;		/* Are we netbooting? */
133 
134 yamon_env_var *yamon_envp;
135 
136 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
137 int mem_cluster_cnt;
138 
139 void	configure(void);
140 void	mach_init(int, char **, yamon_env_var *, u_long);
141 
142 /*
143  * Do all the stuff that locore normally does before calling main().
144  */
145 void
mach_init(int argc,char ** argv,yamon_env_var * envp,u_long memsize)146 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
147 {
148 	struct malta_config *mcp = &malta_configuration;
149 	uint8_t * const brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
150 	bus_space_handle_t sh;
151 	void *kernend;
152 	int freqok;
153 
154 	extern char edata[], end[];
155 
156 	CTASSERT((intptr_t)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES) < 0);
157 
158 	*brkres = 0;	/* Disable BREAK==reset on console */
159 
160 	/* Get the propaganda in early! */
161 	led_display_str("NetBSD");
162 
163 	/*
164 	 * Clear the BSS segment.
165 	 */
166 	kernend = (void *)mips_round_page(end);
167 	memset(edata, 0, (char *)kernend - edata);
168 
169 	/* save the yamon environment pointer */
170 	yamon_envp = envp;
171 
172 	/* Use YAMON callbacks for early console I/O */
173 	cn_tab = &yamon_promcd;
174 
175 	/*
176 	 * Set up the exception vectors and CPU-specific function
177 	 * vectors early on.  We need the wbflush() vector set up
178 	 * before comcnattach() is called (or at least before the
179 	 * first printf() after that is called).
180 	 * Also clears the I+D caches.
181 	 */
182 	mips_vector_init(NULL, false);
183 
184 	uvm_md_init();
185 
186 	physmem = btoc(memsize);
187 
188 	/*
189 	 * Use YAMON's CPU frequency if available.
190 	 */
191 	freqok = yamon_setcpufreq(1);
192 
193 	gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
194 	malta_bus_io_init(&mcp->mc_iot, mcp);
195 	malta_bus_mem_init(&mcp->mc_memt, mcp);
196 	malta_dma_init(mcp);
197 
198 	/*
199 	 * Calibrate the timer if YAMON failed to tell us.
200 	 */
201 	if (!freqok) {
202 		bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
203 		malta_cal_timer(&mcp->mc_iot, sh);
204 		bus_space_unmap(&mcp->mc_iot, sh, 2);
205 	}
206 
207 #if NCOM > 0
208 	/*
209 	 * Delay to allow firmware putchars to complete.
210 	 * FIFO depth * character time.
211 	 * character time = (1000000 / (defaultrate / 10))
212 	 */
213 	delay(160000000 / comcnrate);
214 	if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
215 	    COM_FREQ, COM_TYPE_NORMAL,
216 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
217 		panic("malta: unable to initialize serial console");
218 #else
219 	panic("malta: not configured to use serial console");
220 #endif /* NCOM > 0 */
221 
222 	mem_clusters[0].start = 0;
223 	mem_clusters[0].size = ctob(physmem);
224 	mem_cluster_cnt = 1;
225 
226 	cpu_setmodel("MIPS Malta Evaluation Board");
227 
228 	/*
229 	 * XXX: check argv[0] - do something if "gdb"???
230 	 */
231 
232 	/*
233 	 * Look at arguments passed to us and compute boothowto.
234 	 */
235 	boothowto = RB_AUTOBOOT;
236 #ifndef _LP64
237 	for (int i = 1; i < argc; i++) {
238 		for (char *cp = argv[i]; *cp; cp++) {
239 			int howto;
240 			/* Ignore superfluous '-', if there is one */
241 			if (*cp == '-')
242 				continue;
243 
244 			howto = 0;
245 			BOOT_FLAG(*cp, howto);
246 			if (! howto)
247 				printf("bootflag '%c' not recognised\n", *cp);
248 			else
249 				boothowto |= howto;
250 		}
251 	}
252 #endif
253 
254 	/*
255 	 * Load the rest of the available pages into the VM system.
256 	 */
257 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
258 	    mem_clusters, mem_cluster_cnt, NULL, 0);
259 
260 	/*
261 	 * Initialize error message buffer (at end of core).
262 	 */
263 	mips_init_msgbuf();
264 
265 	pmap_bootstrap();
266 
267 	/*
268 	 * Allocate uarea page for lwp0 and set it.
269 	 */
270 	mips_init_lwp0_uarea();
271 
272 	/*
273 	 * Initialize debuggers, and break into them, if appropriate.
274 	 */
275 #if defined(DDB)
276 	if (boothowto & RB_KDB)
277 		Debugger();
278 #endif
279 
280 #if defined(MULTIPROCESSOR) && 0
281 	/*
282 	 * We can never be running on more than one processor but we can dream.
283 	 */
284 	mips_fixup_exceptions(mips_fixup_zero_relative, NULL);
285 #endif
286 }
287 
288 void
consinit(void)289 consinit(void)
290 {
291 
292 	/*
293 	 * Everything related to console initialization is done
294 	 * in mach_init().
295 	 */
296 }
297 
298 /*
299  * Allocate memory for variable-sized tables,
300  */
301 void
cpu_startup(void)302 cpu_startup(void)
303 {
304 	/*
305 	 * Do the common startup items.
306 	 */
307 	cpu_startup_common();
308 
309 	/*
310 	 * Virtual memory is bootstrapped -- notify the bus spaces
311 	 * that memory allocation is now safe.
312 	 */
313 	malta_configuration.mc_mallocsafe = 1;
314 }
315 
316 int	waittime = -1;
317 
318 void
cpu_reboot(int howto,char * bootstr)319 cpu_reboot(int howto, char *bootstr)
320 {
321 
322 	/* Take a snapshot before clobbering any registers. */
323 	savectx(curpcb);
324 
325 	if (cold) {
326 		howto |= RB_HALT;
327 		goto haltsys;
328 	}
329 
330 	/* If "always halt" was specified as a boot flag, obey. */
331 	if (boothowto & RB_HALT)
332 		howto |= RB_HALT;
333 
334 	boothowto = howto;
335 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
336 		waittime = 0;
337 		vfs_shutdown();
338 	}
339 
340 	splhigh();
341 
342 	if (howto & RB_DUMP)
343 		dumpsys();
344 
345 haltsys:
346 	doshutdownhooks();
347 
348 	pmf_system_shutdown(boothowto);
349 
350 	if (howto & RB_HALT) {
351 		printf("\n");
352 		printf("The operating system has halted.\n");
353 		printf("Please press any key to reboot.\n\n");
354 		cnpollc(1);	/* For proper keyboard command handling */
355 		cngetc();
356 		cnpollc(0);
357 	}
358 
359 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
360 	yamon_exit(boothowto);
361 	printf("Oops, back from yamon_exit()\n\nResetting...");
362 
363 	REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
364 
365 	/*
366 	 * Need a small delay here, otherwise we see the first few characters of
367 	 * the warning below.
368 	 */
369 	delay(80000);
370 
371 	printf("WARNING: reset failed!\nSpinning...");
372 
373 	for (;;)
374 		/* spin forever */ ;	/* XXX */
375 }
376