xref: /netbsd-src/sys/arch/evbmips/malta/machdep.c (revision 48fb7bfab72acd4281a53bbee5ccf3f809019e75)
1 /*	$NetBSD: machdep.c,v 1.42 2012/03/02 16:20:55 matt 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.42 2012/03/02 16:20:55 matt 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 /* For sysctl_hw. */
130 extern char cpu_model[];
131 
132 /* Maps for VM objects. */
133 struct vm_map *phys_map = NULL;
134 
135 int	netboot;		/* Are we netbooting? */
136 
137 yamon_env_var *yamon_envp;
138 
139 phys_ram_seg_t mem_clusters[VM_PHYSSEG_MAX];
140 int mem_cluster_cnt;
141 
142 void	configure(void);
143 void	mach_init(int, char **, yamon_env_var *, u_long);
144 
145 /*
146  * Do all the stuff that locore normally does before calling main().
147  */
148 void
149 mach_init(int argc, char **argv, yamon_env_var *envp, u_long memsize)
150 {
151 	struct malta_config *mcp = &malta_configuration;
152 	uint8_t * const brkres = (uint8_t *)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES);
153 	bus_space_handle_t sh;
154 	void *kernend;
155 	int freqok;
156 
157 	extern char edata[], end[];
158 
159 	CTASSERT((intptr_t)MIPS_PHYS_TO_KSEG1(MALTA_BRKRES) < 0);
160 
161 	*brkres = 0;	/* Disable BREAK==reset on console */
162 
163 	/* Get the propaganda in early! */
164 	led_display_str("NetBSD");
165 
166 	/*
167 	 * Clear the BSS segment.
168 	 */
169 	kernend = (void *)mips_round_page(end);
170 	memset(edata, 0, (char *)kernend - edata);
171 
172 	/* save the yamon environment pointer */
173 	yamon_envp = envp;
174 
175 	/* Use YAMON callbacks for early console I/O */
176 	cn_tab = &yamon_promcd;
177 
178 	/*
179 	 * Set up the exception vectors and CPU-specific function
180 	 * vectors early on.  We need the wbflush() vector set up
181 	 * before comcnattach() is called (or at least before the
182 	 * first printf() after that is called).
183 	 * Also clears the I+D caches.
184 	 */
185 	mips_vector_init(NULL, false);
186 
187 	/* set the VM page size */
188 	uvm_setpagesize();
189 
190 	physmem = btoc(memsize);
191 
192 	/*
193 	 * Use YAMON's CPU frequency if available.
194 	 */
195 	freqok = yamon_setcpufreq(1);
196 
197 	gt_pci_init(&mcp->mc_pc, &mcp->mc_gt);
198 	malta_bus_io_init(&mcp->mc_iot, mcp);
199 	malta_bus_mem_init(&mcp->mc_memt, mcp);
200 	malta_dma_init(mcp);
201 
202 	/*
203 	 * Calibrate the timer if YAMON failed to tell us.
204 	 */
205 	if (!freqok) {
206 		bus_space_map(&mcp->mc_iot, MALTA_RTCADR, 2, 0, &sh);
207 		malta_cal_timer(&mcp->mc_iot, sh);
208 		bus_space_unmap(&mcp->mc_iot, sh, 2);
209 	}
210 
211 #if NCOM > 0
212 	/*
213 	 * Delay to allow firmware putchars to complete.
214 	 * FIFO depth * character time.
215 	 * character time = (1000000 / (defaultrate / 10))
216 	 */
217 	delay(160000000 / comcnrate);
218 	if (comcnattach(&mcp->mc_iot, MALTA_UART0ADR, comcnrate,
219 	    COM_FREQ, COM_TYPE_NORMAL,
220 	    (TTYDEF_CFLAG & ~(CSIZE | PARENB)) | CS8) != 0)
221 		panic("malta: unable to initialize serial console");
222 #else
223 	panic("malta: not configured to use serial console");
224 #endif /* NCOM > 0 */
225 
226 	mem_clusters[0].start = 0;
227 	mem_clusters[0].size = ctob(physmem);
228 	mem_cluster_cnt = 1;
229 
230 	strcpy(cpu_model, "MIPS Malta Evaluation Board");
231 
232 	/*
233 	 * XXX: check argv[0] - do something if "gdb"???
234 	 */
235 
236 	/*
237 	 * Look at arguments passed to us and compute boothowto.
238 	 */
239 	boothowto = RB_AUTOBOOT;
240 #ifndef _LP64
241 	for (int i = 1; i < argc; i++) {
242 		for (char *cp = argv[i]; *cp; cp++) {
243 			int howto;
244 			/* Ignore superfluous '-', if there is one */
245 			if (*cp == '-')
246 				continue;
247 
248 			howto = 0;
249 			BOOT_FLAG(*cp, howto);
250 			if (! howto)
251 				printf("bootflag '%c' not recognised\n", *cp);
252 			else
253 				boothowto |= howto;
254 		}
255 	}
256 #endif
257 
258 	/*
259 	 * Load the rest of the available pages into the VM system.
260 	 */
261 	mips_page_physload(MIPS_KSEG0_START, (vaddr_t)kernend,
262 	    mem_clusters, mem_cluster_cnt, NULL, 0);
263 
264 	/*
265 	 * Initialize error message buffer (at end of core).
266 	 */
267 	mips_init_msgbuf();
268 
269 	pmap_bootstrap();
270 
271 	/*
272 	 * Allocate uarea page for lwp0 and set it.
273 	 */
274 	mips_init_lwp0_uarea();
275 
276 	/*
277 	 * Initialize debuggers, and break into them, if appropriate.
278 	 */
279 #if defined(DDB)
280 	if (boothowto & RB_KDB)
281 		Debugger();
282 #endif
283 
284 #ifdef MULTIPROCESSOR
285 	/*
286 	 * We can never be running on more than one processor but we can dream.
287 	 */
288 	mips_fixup_exceptions(mips_fixup_zero_relative);
289 #endif
290 }
291 
292 void
293 consinit(void)
294 {
295 
296 	/*
297 	 * Everything related to console initialization is done
298 	 * in mach_init().
299 	 */
300 }
301 
302 /*
303  * Allocate memory for variable-sized tables,
304  */
305 void
306 cpu_startup(void)
307 {
308 	/*
309 	 * Do the common startup items.
310 	 */
311 	cpu_startup_common();
312 
313 	/*
314 	 * Virtual memory is bootstrapped -- notify the bus spaces
315 	 * that memory allocation is now safe.
316 	 */
317 	malta_configuration.mc_mallocsafe = 1;
318 }
319 
320 int	waittime = -1;
321 
322 void
323 cpu_reboot(int howto, char *bootstr)
324 {
325 
326 	/* Take a snapshot before clobbering any registers. */
327 	savectx(curpcb);
328 
329 	if (cold) {
330 		howto |= RB_HALT;
331 		goto haltsys;
332 	}
333 
334 	/* If "always halt" was specified as a boot flag, obey. */
335 	if (boothowto & RB_HALT)
336 		howto |= RB_HALT;
337 
338 	boothowto = howto;
339 	if ((howto & RB_NOSYNC) == 0 && (waittime < 0)) {
340 		waittime = 0;
341 		vfs_shutdown();
342 
343 		/*
344 		 * If we've been adjusting the clock, the todr
345 		 * will be out of synch; adjust it now.
346 		 */
347 		resettodr();
348 	}
349 
350 	splhigh();
351 
352 	if (howto & RB_DUMP)
353 		dumpsys();
354 
355 haltsys:
356 	doshutdownhooks();
357 
358 	pmf_system_shutdown(boothowto);
359 
360 	if (howto & RB_HALT) {
361 		printf("\n");
362 		printf("The operating system has halted.\n");
363 		printf("Please press any key to reboot.\n\n");
364 		cnpollc(1);	/* For proper keyboard command handling */
365 		cngetc();
366 		cnpollc(0);
367 	}
368 
369 	printf("%s\n\n", ((howto & RB_HALT) != 0) ? "halted." : "rebooting...");
370 	yamon_exit(boothowto);
371 	printf("Oops, back from yamon_exit()\n\nResetting...");
372 
373 	REGVAL(MALTA_SOFTRES) = MALTA_GORESET;
374 
375 	/*
376 	 * Need a small delay here, otherwise we see the first few characters of
377 	 * the warning below.
378 	 */
379 	delay(80000);
380 
381 	printf("WARNING: reset failed!\nSpinning...");
382 
383 	for (;;)
384 		/* spin forever */ ;	/* XXX */
385 }
386