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