xref: /netbsd/sys/arch/dreamcast/dreamcast/machdep.c (revision bf9ec67e)
1 /*	$NetBSD: machdep.c,v 1.21 2002/05/10 20:14:39 uch Exp $	*/
2 
3 /*-
4  * Copyright (c) 1996, 1997, 1998, 2002 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
9  * Simulation Facility, NASA Ames Research Center.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. All advertising materials mentioning features or use of this software
20  *    must display the following acknowledgement:
21  *	This product includes software developed by the NetBSD
22  *	Foundation, Inc. and its contributors.
23  * 4. Neither the name of The NetBSD Foundation nor the names of its
24  *    contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37  * POSSIBILITY OF SUCH DAMAGE.
38  */
39 
40 /*-
41  * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
42  * All rights reserved.
43  *
44  * This code is derived from software contributed to Berkeley by
45  * William Jolitz.
46  *
47  * Redistribution and use in source and binary forms, with or without
48  * modification, are permitted provided that the following conditions
49  * are met:
50  * 1. Redistributions of source code must retain the above copyright
51  *    notice, this list of conditions and the following disclaimer.
52  * 2. Redistributions in binary form must reproduce the above copyright
53  *    notice, this list of conditions and the following disclaimer in the
54  *    documentation and/or other materials provided with the distribution.
55  * 3. All advertising materials mentioning features or use of this software
56  *    must display the following acknowledgement:
57  *	This product includes software developed by the University of
58  *	California, Berkeley and its contributors.
59  * 4. Neither the name of the University nor the names of its contributors
60  *    may be used to endorse or promote products derived from this software
61  *    without specific prior written permission.
62  *
63  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73  * SUCH DAMAGE.
74  *
75  *	@(#)machdep.c	7.4 (Berkeley) 6/3/91
76  */
77 
78 #include "opt_ddb.h"
79 #include "opt_kgdb.h"
80 #include "opt_memsize.h"
81 #include "scif.h"
82 
83 #include <sys/param.h>
84 #include <sys/systm.h>
85 #include <sys/kernel.h>
86 #include <sys/user.h>
87 #include <sys/mount.h>
88 #include <sys/reboot.h>
89 #include <sys/sysctl.h>
90 
91 #ifdef KGDB
92 #include <sys/kgdb.h>
93 #include <sh3/dev/scifvar.h>
94 #endif
95 #ifdef DDB
96 #include <machine/db_machdep.h>
97 #include <ddb/db_extern.h>
98 #endif
99 
100 #include <sh3/cpu.h>
101 #include <sh3/exception.h>
102 #include <machine/intr.h>
103 
104 #include <dev/cons.h>
105 
106 /* the following is used externally (sysctl_hw) */
107 char machine[] = MACHINE;		/* dreamcast */
108 char machine_arch[] = MACHINE_ARCH;	/* sh3el */
109 
110 void main(void) __attribute__((__noreturn__));
111 void dreamcast_startup(void) __attribute__((__noreturn__));
112 
113 void
114 dreamcast_startup()
115 {
116 	extern char edata[], end[];
117 	paddr_t kernend;
118 
119 	/* Clear bss */
120 	memset(edata, 0, end - edata);
121 
122 	/* Initialize CPU ops. */
123 	sh_cpu_init(CPU_ARCH_SH4, CPU_PRODUCT_7750);
124 
125 	/* Console */
126 	consinit();
127 
128 	/* Load memory to UVM */
129 	physmem = atop(IOM_RAM_SIZE);
130 	kernend = atop(round_page(SH3_P1SEG_TO_PHYS(end)));
131 	uvm_page_physload(
132 		kernend, atop(IOM_RAM_BEGIN + IOM_RAM_SIZE),
133 		kernend, atop(IOM_RAM_BEGIN + IOM_RAM_SIZE),
134 		VM_FREELIST_DEFAULT);
135 
136 	/* Initialize proc0 u-area */
137 	sh_proc0_init();
138 
139 	/* Initialize pmap and start to address translation */
140 	pmap_bootstrap();
141 
142 	/* Debugger. */
143 #ifdef DDB
144 	ddb_init(0, NULL, NULL);
145 #endif
146 #if defined(KGDB) && (NSCIF > 0)
147 	if (scif_kgdb_init() == 0) {
148 		kgdb_debug_init = 1;
149 		kgdb_connect(1);
150 	}
151 #endif /* KGDB && NSCIF > 0 */
152 
153 	/* Jump to main */
154 	__asm__ __volatile__(
155 		"jmp	@%0;"
156 		"mov	%1, sp"
157 		:: "r"(main),"r"(proc0.p_md.md_pcb->pcb_sf.sf_r7_bank));
158 	/* NOTREACHED */
159 	while (1)
160 		;
161 }
162 
163 void
164 consinit()
165 {
166 	static int initted;
167 
168 	if (initted)
169 		return;
170 	initted = 1;
171 
172 	cninit();
173 }
174 
175 void
176 cpu_startup()
177 {
178 
179 	strcpy(cpu_model, "SEGA Dreamcast\n");
180 
181 	sh_startup();
182 }
183 
184 int
185 cpu_sysctl(int *name, u_int namelen, void *oldp, size_t *oldlenp,
186     void *newp, size_t newlen, struct proc *p)
187 {
188 	/* all sysctl names at this level are terminal */
189 	if (namelen != 1)
190 		return (ENOTDIR);		/* overloaded */
191 
192 	switch (name[0]) {
193 	case CPU_CONSDEV:
194 		return (sysctl_rdstruct(oldp, oldlenp, newp, &cn_tab->cn_dev,
195 		    sizeof cn_tab->cn_dev));
196 	default:
197 		break;
198 	}
199 
200 	return (EOPNOTSUPP);
201 }
202 
203 void
204 cpu_reboot(int howto, char *bootstr)
205 {
206 	static int waittime = -1;
207 
208 	if (cold) {
209 		howto |= RB_HALT;
210 		goto haltsys;
211 	}
212 
213 	boothowto = howto;
214 	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
215 		waittime = 0;
216 		vfs_shutdown();
217 		/*
218 		 * If we've been adjusting the clock, the todr
219 		 * will be out of synch; adjust it now.
220 		 */
221 		/* resettodr(); */
222 	}
223 
224 	/* Disable interrupts. */
225 	splhigh();
226 
227 	/* Do a dump if requested. */
228 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
229 		dumpsys();
230 
231 haltsys:
232 	doshutdownhooks();
233 
234 	if (howto & RB_HALT) {
235 		printf("\n");
236 		printf("The operating system has halted.\n");
237 		printf("Please press any key to reboot.\n\n");
238 		cngetc();
239 	}
240 
241 	printf("rebooting...\n");
242 	cpu_reset();
243 	for(;;)
244 		;
245 	/*NOTREACHED*/
246 }
247 
248 void
249 intc_intr(int ssr, int spc, int ssp)
250 {
251 	struct intc_intrhand *ih;
252 	int s, evtcode;
253 
254 	evtcode = _reg_read_4(SH4_INTEVT);
255 
256 	ih = EVTCODE_IH(evtcode);
257 	KDASSERT(ih->ih_func);
258 	/*
259 	 * On entry, all interrrupts are disabled, and exception is enabled.
260 	 * Enable higher level interrupt here.
261 	 */
262 	s = _cpu_intr_resume(ih->ih_level);
263 
264 	if (evtcode == SH_INTEVT_TMU0_TUNI0) {	/* hardclock */
265 		struct clockframe cf;
266 		cf.spc = spc;
267 		cf.ssr = ssr;
268 		cf.ssp = ssp;
269 		(*ih->ih_func)(&cf);
270 	} else {
271 		(*ih->ih_func)(ih->ih_arg);
272 	}
273 }
274