xref: /netbsd-src/sys/arch/evbmips/malta/machdep.c (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1 /*	$NetBSD: machdep.c,v 1.43 2014/03/24 20:06:31 christos 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.43 2014/03/24 20:06:31 christos 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
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 	/* set the VM page size */
185 	uvm_setpagesize();
186 
187 	physmem = btoc(memsize);
188 
189 	/*
190 	 * Use YAMON's CPU frequency if available.
191 	 */
192 	freqok = yamon_setcpufreq(1);
193 
194 	gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
195 	malta_bus_io_init(&mcp->mc_iot, mcp);
196 	malta_bus_mem_init(&mcp->mc_memt, mcp);
197 	malta_dma_init(mcp);
198 
199 	/*
200 	 * Calibrate the timer if YAMON failed to tell us.
201 	 */
202 	if (!freqok) {
203 		bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
204 		malta_cal_timer(&mcp->mc_iot, sh);
205 		bus_space_unmap(&mcp->mc_iot, sh, 2);
206 	}
207 
208 #if NCOM > 0
209 	/*
210 	 * Delay to allow firmware putchars to complete.
211 	 * FIFO depth * character time.
212 	 * character time = (1000000 / (defaultrate / 10))
213 	 */
214 	delay(160000000 / comcnrate);
215 	if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
216 	    COM_FREQ, COM_TYPE_NORMAL,
217 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
218 		panic("malta: unable to initialize serial console");
219 #else
220 	panic("malta: not configured to use serial console");
221 #endif /* NCOM > 0 */
222 
223 	mem_clusters[0].start = 0;
224 	mem_clusters[0].size = ctob(physmem);
225 	mem_cluster_cnt = 1;
226 
227 	cpu_setmodel("MIPS Malta Evaluation Board");
228 
229 	/*
230 	 * XXX: check argv[0] - do something if "gdb"???
231 	 */
232 
233 	/*
234 	 * Look at arguments passed to us and compute boothowto.
235 	 */
236 	boothowto = RB_AUTOBOOT;
237 #ifndef _LP64
238 	for (int i = 1; i < argc; i++) {
239 		for (char *cp = argv[i]; *cp; cp++) {
240 			int howto;
241 			/* Ignore superfluous '-', if there is one */
242 			if (*cp == '-')
243 				continue;
244 
245 			howto = 0;
246 			BOOT_FLAG(*cp, howto);
247 			if (! howto)
248 				printf("bootflag '%c' not recognised\n", *cp);
249 			else
250 				boothowto |= howto;
251 		}
252 	}
253 #endif
254 
255 	/*
256 	 * Load the rest of the available pages into the VM system.
257 	 */
258 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
259 	    mem_clusters, mem_cluster_cnt, NULL, 0);
260 
261 	/*
262 	 * Initialize error message buffer (at end of core).
263 	 */
264 	mips_init_msgbuf();
265 
266 	pmap_bootstrap();
267 
268 	/*
269 	 * Allocate uarea page for lwp0 and set it.
270 	 */
271 	mips_init_lwp0_uarea();
272 
273 	/*
274 	 * Initialize debuggers, and break into them, if appropriate.
275 	 */
276 #if defined(DDB)
277 	if (boothowto & RB_KDB)
278 		Debugger();
279 #endif
280 
281 #ifdef MULTIPROCESSOR
282 	/*
283 	 * We can never be running on more than one processor but we can dream.
284 	 */
285 	mips_fixup_exceptions(mips_fixup_zero_relative);
286 #endif
287 }
288 
289 void
290 consinit(void)
291 {
292 
293 	/*
294 	 * Everything related to console initialization is done
295 	 * in mach_init().
296 	 */
297 }
298 
299 /*
300  * Allocate memory for variable-sized tables,
301  */
302 void
303 cpu_startup(void)
304 {
305 	/*
306 	 * Do the common startup items.
307 	 */
308 	cpu_startup_common();
309 
310 	/*
311 	 * Virtual memory is bootstrapped -- notify the bus spaces
312 	 * that memory allocation is now safe.
313 	 */
314 	malta_configuration.mc_mallocsafe = 1;
315 }
316 
317 int	waittime = -1;
318 
319 void
320 cpu_reboot(int howto, char *bootstr)
321 {
322 
323 	/* Take a snapshot before clobbering any registers. */
324 	savectx(curpcb);
325 
326 	if (cold) {
327 		howto |= RB_HALT;
328 		goto haltsys;
329 	}
330 
331 	/* If "always halt" was specified as a boot flag, obey. */
332 	if (boothowto & RB_HALT)
333 		howto |= RB_HALT;
334 
335 	boothowto = howto;
336 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
337 		waittime = 0;
338 		vfs_shutdown();
339 
340 		/*
341 		 * If we've been adjusting the clock, the todr
342 		 * will be out of synch; adjust it now.
343 		 */
344 		resettodr();
345 	}
346 
347 	splhigh();
348 
349 	if (howto & RB_DUMP)
350 		dumpsys();
351 
352 haltsys:
353 	doshutdownhooks();
354 
355 	pmf_system_shutdown(boothowto);
356 
357 	if (howto & RB_HALT) {
358 		printf("\n");
359 		printf("The operating system has halted.\n");
360 		printf("Please press any key to reboot.\n\n");
361 		cnpollc(1);	/* For proper keyboard command handling */
362 		cngetc();
363 		cnpollc(0);
364 	}
365 
366 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
367 	yamon_exit(boothowto);
368 	printf("Oops, back from yamon_exit()\n\nResetting...");
369 
370 	REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
371 
372 	/*
373 	 * Need a small delay here, otherwise we see the first few characters of
374 	 * the warning below.
375 	 */
376 	delay(80000);
377 
378 	printf("WARNING: reset failed!\nSpinning...");
379 
380 	for (;;)
381 		/* spin forever */ ;	/* XXX */
382 }
383