124139Sjoerg/*	$NetBSD$	*/
224139Sjoerg
324139Sjoerg/*-
424139Sjoerg * Copyright (c) 1996, 1997, 1998, 2002 The NetBSD Foundation, Inc.
524139Sjoerg * All rights reserved.
624139Sjoerg *
724139Sjoerg * This code is derived from software contributed to The NetBSD Foundation
824139Sjoerg * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
924139Sjoerg * Simulation Facility, NASA Ames Research Center.
1024139Sjoerg *
1124139Sjoerg * Redistribution and use in source and binary forms, with or without
1224139Sjoerg * modification, are permitted provided that the following conditions
1324139Sjoerg * are met:
1424139Sjoerg * 1. Redistributions of source code must retain the above copyright
1524139Sjoerg *    notice, this list of conditions and the following disclaimer.
1624139Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
1724139Sjoerg *    notice, this list of conditions and the following disclaimer in the
1824139Sjoerg *    documentation and/or other materials provided with the distribution.
1924139Sjoerg *
2024139Sjoerg * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2124139Sjoerg * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2224139Sjoerg * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2324139Sjoerg * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2424139Sjoerg * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2524139Sjoerg * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2624139Sjoerg * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2724139Sjoerg * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2824139Sjoerg * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2924139Sjoerg * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3024139Sjoerg * POSSIBILITY OF SUCH DAMAGE.
3124139Sjoerg */
3224139Sjoerg
3324139Sjoerg/*-
3424139Sjoerg * Copyright (c) 1982, 1987, 1990 The Regents of the University of California.
3524139Sjoerg * All rights reserved.
3624139Sjoerg *
3724139Sjoerg * This code is derived from software contributed to Berkeley by
3824139Sjoerg * William Jolitz.
3924139Sjoerg *
4024139Sjoerg * Redistribution and use in source and binary forms, with or without
4124139Sjoerg * modification, are permitted provided that the following conditions
4224139Sjoerg * are met:
4324139Sjoerg * 1. Redistributions of source code must retain the above copyright
4424139Sjoerg *    notice, this list of conditions and the following disclaimer.
4524139Sjoerg * 2. Redistributions in binary form must reproduce the above copyright
4624139Sjoerg *    notice, this list of conditions and the following disclaimer in the
4724139Sjoerg *    documentation and/or other materials provided with the distribution.
4824139Sjoerg * 3. Neither the name of the University nor the names of its contributors
4924139Sjoerg *    may be used to endorse or promote products derived from this software
5024139Sjoerg *    without specific prior written permission.
5124139Sjoerg *
5224139Sjoerg * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
5324139Sjoerg * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
5424139Sjoerg * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
5524139Sjoerg * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
5624139Sjoerg * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
5724139Sjoerg * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
5824139Sjoerg * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
5924139Sjoerg * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
6024139Sjoerg * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
6124139Sjoerg * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
6224139Sjoerg * SUCH DAMAGE.
6324139Sjoerg *
6424139Sjoerg *	@(#)machdep.c	7.4 (Berkeley) 6/3/91
6524139Sjoerg */
6624139Sjoerg
6724139Sjoerg#include <sys/cdefs.h>
6824139Sjoerg__KERNEL_RCSID(0, "$NetBSD$");
6924139Sjoerg
7024139Sjoerg#include "opt_ddb.h"
7124139Sjoerg#include "opt_kgdb.h"
7224139Sjoerg#include "opt_memsize.h"
7324139Sjoerg#include "scif.h"
7424139Sjoerg#include "opt_kloader.h"
7524139Sjoerg#include "opt_modular.h"
7624139Sjoerg
7724139Sjoerg#include <sys/param.h>
7824139Sjoerg#include <sys/systm.h>
7924139Sjoerg#include <sys/kernel.h>
8024139Sjoerg#include <sys/mount.h>
8124139Sjoerg#include <sys/reboot.h>
8224139Sjoerg#include <sys/sysctl.h>
8324139Sjoerg#include <sys/ksyms.h>
8424139Sjoerg#include <sys/device.h>
8524139Sjoerg#include <sys/module.h>
8624139Sjoerg
8724139Sjoerg#ifdef KGDB
8824139Sjoerg#include <sys/kgdb.h>
8924139Sjoerg#include <sh3/dev/scifvar.h>
9024139Sjoerg#endif
9124139Sjoerg#ifdef DDB
9224139Sjoerg#include <machine/db_machdep.h>
9324139Sjoerg#include <ddb/db_extern.h>
9424139Sjoerg#endif
9524139Sjoerg
9624139Sjoerg#include <sh3/cpu.h>
9724139Sjoerg#include <sh3/exception.h>
9824139Sjoerg#include <sh3/bscreg.h>
9924139Sjoerg#include <machine/intr.h>
10024139Sjoerg#include <machine/kloader.h>
10124139Sjoerg#include <machine/pcb.h>
10224139Sjoerg
10324139Sjoerg#include <dev/cons.h>
10424139Sjoerg
10524139Sjoerg#include "ksyms.h"
10624139Sjoerg
10724139Sjoerg/* the following is used externally (sysctl_hw) */
10824139Sjoergchar machine[] = MACHINE;		/* dreamcast */
10924139Sjoergchar machine_arch[] = MACHINE_ARCH;	/* sh3el */
11024139Sjoerg
11124139Sjoergvoid main(void) __attribute__((__noreturn__));
11224139Sjoergvoid dreamcast_startup(void) __attribute__((__noreturn__));
11324139Sjoerg
11424139Sjoergvoid
11524139Sjoergdreamcast_startup(void)
11624139Sjoerg{
11724139Sjoerg	extern char edata[], end[];
11824139Sjoerg	paddr_t kernend;
11924139Sjoerg
12024139Sjoerg	/* Clear bss */
12124139Sjoerg	memset(edata, 0, end - edata);
12224139Sjoerg
12324139Sjoerg	/* Initialize CPU ops. */
12424139Sjoerg	sh_cpu_init(CPU_ARCH_SH4, CPU_PRODUCT_7750);
12524139Sjoerg
12624139Sjoerg	/* Console */
12724139Sjoerg	consinit();
12824139Sjoerg
12924139Sjoerg	/* Load memory to UVM */
13024139Sjoerg	physmem = atop(IOM_RAM_SIZE);
13124139Sjoerg	kernend = atop(round_page(SH3_P1SEG_TO_PHYS(end)));
13224139Sjoerg	uvm_page_physload(
13324139Sjoerg		kernend, atop(IOM_RAM_BEGIN + IOM_RAM_SIZE),
13424139Sjoerg		kernend, atop(IOM_RAM_BEGIN + IOM_RAM_SIZE),
13524139Sjoerg		VM_FREELIST_DEFAULT);
13624139Sjoerg
13724139Sjoerg	/* Initialize proc0 u-area */
13824139Sjoerg	sh_proc0_init();
13924139Sjoerg
14024139Sjoerg	/* Initialize pmap and start to address translation */
14124139Sjoerg	pmap_bootstrap();
14224139Sjoerg
14324139Sjoerg	/* Debugger. */
14424139Sjoerg#if defined(KGDB) && (NSCIF > 0)
14524139Sjoerg	if (scif_kgdb_init() == 0) {
14624139Sjoerg		kgdb_debug_init = 1;
14724139Sjoerg		kgdb_connect(1);
14824139Sjoerg	}
14924139Sjoerg#endif /* KGDB && NSCIF > 0 */
15024139Sjoerg
15124139Sjoerg	/* Jump to main */
15224139Sjoerg	__asm volatile(
15324139Sjoerg		"jmp	@%0;"
15424139Sjoerg		"mov	%1, sp"
15524139Sjoerg		:: "r"(main),"r"(lwp0.l_md.md_pcb->pcb_sf.sf_r7_bank));
15624139Sjoerg	/* NOTREACHED */
15724139Sjoerg	while (1)
15824139Sjoerg		;
15924139Sjoerg}
16024139Sjoerg
16124139Sjoergvoid
16224139Sjoergconsinit(void)
16324139Sjoerg{
16424139Sjoerg	static int initted;
16524139Sjoerg
16624139Sjoerg	if (initted)
16724139Sjoerg		return;
16824139Sjoerg	initted = 1;
16924139Sjoerg
17024139Sjoerg	cninit();
17124139Sjoerg}
17224139Sjoerg
17324139Sjoergvoid
17424139Sjoergcpu_startup(void)
17524139Sjoerg{
17624139Sjoerg
17724139Sjoerg	strcpy(cpu_model, "SEGA Dreamcast\n");
17824139Sjoerg
17924139Sjoerg	sh_startup();
18024139Sjoerg}
18124139Sjoerg
18224139SjoergSYSCTL_SETUP(sysctl_machdep_setup, "sysctl machdep subtree setup")
18324139Sjoerg{
18424139Sjoerg
18524139Sjoerg	sysctl_createv(clog, 0, NULL, NULL,
18624139Sjoerg		       CTLFLAG_PERMANENT,
18724139Sjoerg		       CTLTYPE_NODE, "machdep", NULL,
18824139Sjoerg		       NULL, 0, NULL, 0,
18924139Sjoerg		       CTL_MACHDEP, CTL_EOL);
19024139Sjoerg
19124139Sjoerg	sysctl_createv(clog, 0, NULL, NULL,
19224139Sjoerg		       CTLFLAG_PERMANENT,
19324139Sjoerg		       CTLTYPE_STRUCT, "console_device", NULL,
19424139Sjoerg		       sysctl_consdev, 0, NULL, sizeof(dev_t),
19524139Sjoerg		       CTL_MACHDEP, CPU_CONSDEV, CTL_EOL);
19624139Sjoerg}
19724139Sjoerg
19824139Sjoergvoid
19924139Sjoergcpu_reboot(int howto, char *bootstr)
20024139Sjoerg{
20124139Sjoerg#ifdef KLOADER
20224139Sjoerg	struct kloader_bootinfo kbi;
20324139Sjoerg#endif
20424139Sjoerg	static int waittime = -1;
20524139Sjoerg
20624139Sjoerg	if (cold) {
20724139Sjoerg		howto |= RB_HALT;
20824139Sjoerg		goto haltsys;
20924139Sjoerg	}
21024139Sjoerg
21124139Sjoerg#ifdef KLOADER
21224139Sjoerg	/* No bootinfo is required. */
21324139Sjoerg	kloader_bootinfo_set(&kbi, 0, NULL, NULL, true);
21424139Sjoerg	if ((howto & RB_HALT) == 0) {
21524139Sjoerg		if ((howto & RB_STRING) && bootstr != NULL) {
21624139Sjoerg			printf("loading a new kernel: %s\n", bootstr);
21724139Sjoerg			kloader_reboot_setup(bootstr);
21824139Sjoerg		}
21924139Sjoerg	}
22024139Sjoerg#endif
22124139Sjoerg
22224139Sjoerg	boothowto = howto;
22324139Sjoerg	if ((howto & RB_NOSYNC) == 0 && waittime < 0) {
22424139Sjoerg		waittime = 0;
22524139Sjoerg		vfs_shutdown();
22624139Sjoerg		/*
22724139Sjoerg		 * If we've been adjusting the clock, the todr
22824139Sjoerg		 * will be out of synch; adjust it now.
22924139Sjoerg		 */
23024139Sjoerg#if 0
23124139Sjoerg		resettodr();
23224139Sjoerg#endif
23324139Sjoerg	}
23424139Sjoerg
23524139Sjoerg	/* Disable interrupts. */
23624139Sjoerg	splhigh();
23724139Sjoerg
23824139Sjoerg	/* Do a dump if requested. */
23924139Sjoerg	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP)
24024139Sjoerg		dumpsys();
24124139Sjoerg
24224139Sjoerg haltsys:
24324139Sjoerg	doshutdownhooks();
24424139Sjoerg
24524139Sjoerg	pmf_system_shutdown(boothowto);
24624139Sjoerg
24724139Sjoerg	if (howto & RB_HALT) {
24824139Sjoerg		printf("\n");
24924139Sjoerg		printf("The operating system has halted.\n");
25024139Sjoerg		printf("Please press any key to reboot.\n\n");
25124139Sjoerg		cngetc();
25224139Sjoerg	}
25324139Sjoerg
25424139Sjoerg#ifdef KLOADER
25524139Sjoerg	else if ((howto & RB_STRING) && bootstr != NULL) {
25624139Sjoerg		kloader_reboot();
25724139Sjoerg		printf("\nFailed to load a new kernel.\n");
25824139Sjoerg		cngetc();
25924139Sjoerg	}
26024139Sjoerg#endif
26124139Sjoerg
26224139Sjoerg	printf("rebooting...\n");
26324139Sjoerg	cpu_reset();
26424139Sjoerg	for(;;)
26524139Sjoerg		;
26624139Sjoerg	/*NOTREACHED*/
26724139Sjoerg}
26824139Sjoerg
26924139Sjoergvoid
27024139Sjoergintc_intr(int ssr, int spc, int ssp)
27124139Sjoerg{
27224139Sjoerg	struct intc_intrhand *ih;
27324139Sjoerg	int s, evtcode;
27424139Sjoerg
27524139Sjoerg	curcpu()->ci_data.cpu_nintr++;
27624139Sjoerg
27724139Sjoerg	evtcode = _reg_read_4(SH4_INTEVT);
27824139Sjoerg
27924139Sjoerg	ih = EVTCODE_IH(evtcode);
28024139Sjoerg	KDASSERT(ih->ih_func);
28124139Sjoerg	/*
28224139Sjoerg	 * On entry, all interrrupts are disabled, and exception is enabled.
28324139Sjoerg	 * Enable higher level interrupt here.
28424139Sjoerg	 */
28524139Sjoerg	s = _cpu_intr_resume(ih->ih_level);
28624139Sjoerg
28724139Sjoerg	if (evtcode == SH_INTEVT_TMU0_TUNI0) {	/* hardclock */
28824139Sjoerg		struct clockframe cf;
28924139Sjoerg		cf.spc = spc;
29024139Sjoerg		cf.ssr = ssr;
29124139Sjoerg		cf.ssp = ssp;
29224139Sjoerg		(*ih->ih_func)(&cf);
29324139Sjoerg	} else {
29424139Sjoerg		(*ih->ih_func)(ih->ih_arg);
29524139Sjoerg	}
29624139Sjoerg}
29724139Sjoerg
29824139Sjoerg#ifdef MODULAR
29924139Sjoerg/*
30024139Sjoerg * Push any modules loaded by the bootloader etc.
30124139Sjoerg */
30224139Sjoergvoid
30324139Sjoergmodule_init_md(void)
30424139Sjoerg{
30524139Sjoerg}
30624139Sjoerg#endif
30724139Sjoerg