Home | History | Annotate | Line # | Download | only in nintendo
      1 /* $NetBSD: machdep.c,v 1.6 2026/02/24 21:41:59 jmcneill Exp $ */
      2 
      3 /*
      4  * Copyright (c) 2002, 2024 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at Sandburst Corp.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Copyright (C) 1995, 1996 Wolfgang Solfrank.
     34  * Copyright (C) 1995, 1996 TooLs GmbH.
     35  * All rights reserved.
     36  *
     37  * Redistribution and use in source and binary forms, with or without
     38  * modification, are permitted provided that the following conditions
     39  * are met:
     40  * 1. Redistributions of source code must retain the above copyright
     41  *    notice, this list of conditions and the following disclaimer.
     42  * 2. Redistributions in binary form must reproduce the above copyright
     43  *    notice, this list of conditions and the following disclaimer in the
     44  *    documentation and/or other materials provided with the distribution.
     45  * 3. All advertising materials mentioning features or use of this software
     46  *    must display the following acknowledgement:
     47  *	This product includes software developed by TooLs GmbH.
     48  * 4. The name of TooLs GmbH may not be used to endorse or promote products
     49  *    derived from this software without specific prior written permission.
     50  *
     51  * THIS SOFTWARE IS PROVIDED BY TOOLS GMBH ``AS IS'' AND ANY EXPRESS OR
     52  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     53  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     54  * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     55  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
     56  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
     57  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
     58  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
     59  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
     60  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     61  */
     62 
     63 #define _POWERPC_BUS_DMA_PRIVATE
     64 
     65 #include <sys/cdefs.h>
     66 __KERNEL_RCSID(0, "$NetBSD: machdep.c,v 1.6 2026/02/24 21:41:59 jmcneill Exp $");
     67 
     68 #include "opt_compat_netbsd.h"
     69 #include "opt_ddb.h"
     70 #include "opt_ddbparam.h"
     71 #include "opt_inet.h"
     72 #include "opt_ns.h"
     73 #include "opt_oea.h"
     74 #include "opt_multiprocessor.h"
     75 
     76 #include <sys/param.h>
     77 #include <sys/buf.h>
     78 #include <sys/bus.h>
     79 #include <sys/conf.h>
     80 #include <sys/device.h>
     81 #include <sys/exec.h>
     82 #include <sys/extent.h>
     83 #include <sys/intr.h>
     84 #include <sys/kernel.h>
     85 #include <sys/kgdb.h>
     86 #include <sys/ksyms.h>
     87 #include <sys/mbuf.h>
     88 #include <sys/mount.h>
     89 #include <sys/msgbuf.h>
     90 #include <sys/proc.h>
     91 #include <sys/reboot.h>
     92 #include <sys/syscallargs.h>
     93 #include <sys/sysctl.h>
     94 #include <sys/syslog.h>
     95 #include <sys/systm.h>
     96 #include <sys/cpu.h>
     97 
     98 #include <uvm/uvm_extern.h>
     99 
    100 #include <machine/powerpc.h>
    101 #include <machine/wii.h>
    102 #include <machine/wiiu.h>
    103 #include <arch/evbppc/nintendo/dev/gecko.h>
    104 #include <arch/evbppc/nintendo/pic_pi.h>
    105 #include <arch/evbppc/nintendo/ipi_latte.h>
    106 
    107 #include <powerpc/bus_funcs.h>
    108 #include <powerpc/db_machdep.h>
    109 #include <powerpc/pio.h>
    110 #include <powerpc/pmap.h>
    111 #include <powerpc/spr.h>
    112 #include <powerpc/trap.h>
    113 
    114 #include <powerpc/oea/bat.h>
    115 #include <powerpc/oea/spr.h>
    116 #include <powerpc/oea/hid.h>
    117 #include <powerpc/pic/picvar.h>
    118 
    119 #include <ddb/db_extern.h>
    120 #include <ddb/db_active.h>
    121 
    122 #include <dev/wscons/wsconsio.h>
    123 #include <dev/wscons/wsdisplayvar.h>
    124 #include <dev/rasops/rasops.h>
    125 #include <dev/wsfont/wsfont.h>
    126 #include <dev/wscons/wsdisplay_vconsvar.h>
    127 
    128 #include <dev/usb/ukbdvar.h>
    129 
    130 #include "ksyms.h"
    131 #include "ukbd.h"
    132 #include "genfb.h"
    133 #include "gecko.h"
    134 
    135 #ifndef WII_DEFAULT_CMDLINE
    136 #define WII_DEFAULT_CMDLINE "root=ld0a"
    137 #endif
    138 
    139 #define IBM750CL_SPR_HID4	1011
    140 
    141 #define MINI_MEM2_START		0x13f00000	/* Start of reserved MEM2 for MINI */
    142 
    143 #define PI_INTERRUPT_CAUSE	0x0c003000
    144 #define  RESET_SWITCH_STATE	__BIT(16)
    145 
    146 extern u_int l2cr_config;
    147 
    148 struct powerpc_bus_space wii_mem_tag = {
    149 	.pbs_flags = _BUS_SPACE_BIG_ENDIAN |
    150 		     _BUS_SPACE_MEM_TYPE,
    151 };
    152 
    153 static char ex_storage[1][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    154     __attribute__((aligned(8)));
    155 
    156 static bus_addr_t
    157 wii_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t addr)
    158 {
    159 	return addr;
    160 }
    161 
    162 static bus_addr_t
    163 wii_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t addr)
    164 {
    165 	return addr;
    166 }
    167 
    168 static int
    169 wii_mem2_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    170     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    171     int flags)
    172 {
    173 	struct mem_region *mem, *avail;
    174 	const int mem2_index = wiiu_native ? 2 : 1;
    175 
    176 	/* Restrict memory used for DMA to ranges in MEM2 */
    177 	mem_regions(&mem, &avail);
    178 	if (mem[mem2_index].size == 0) {
    179 		return ENOMEM;
    180 	}
    181 
    182 	return _bus_dmamem_alloc_range(t, size, alignment, boundary, segs,
    183 	    nsegs, rsegs, flags, mem[mem2_index].start,
    184 	    mem[mem2_index].start + mem[mem2_index].size - PAGE_SIZE - 1);
    185 }
    186 
    187 struct powerpc_bus_dma_tag wii_bus_dma_tag = {
    188 	0, 0,				/* _bounce_thresh_min/max */
    189 	_bus_dmamap_create,
    190 	_bus_dmamap_destroy,
    191 	_bus_dmamap_load,
    192 	_bus_dmamap_load_mbuf,
    193 	_bus_dmamap_load_uio,
    194 	_bus_dmamap_load_raw,
    195 	_bus_dmamap_unload,
    196 	_bus_dmamap_sync,
    197 	_bus_dmamem_alloc,
    198 	_bus_dmamem_free,
    199 	_bus_dmamem_map,
    200 	_bus_dmamem_unmap,
    201 	_bus_dmamem_mmap,
    202 	wii_dma_phys_to_bus_mem,
    203 	wii_dma_bus_mem_to_phys,
    204 };
    205 
    206 struct powerpc_bus_dma_tag wii_mem2_bus_dma_tag = {
    207 	WII_MEM2_BASE,				/* _bounce_thresh_min */
    208 	WII_MEM2_BASE + WII_MEM2_SIZE - 1,	/* _bounce_thresh_max */
    209 	_bus_dmamap_create,
    210 	_bus_dmamap_destroy,
    211 	_bus_dmamap_load,
    212 	_bus_dmamap_load_mbuf,
    213 	_bus_dmamap_load_uio,
    214 	_bus_dmamap_load_raw,
    215 	_bus_dmamap_unload,
    216 	_bus_dmamap_sync,
    217 	wii_mem2_dmamem_alloc,
    218 	_bus_dmamem_free,
    219 	_bus_dmamem_map,
    220 	_bus_dmamem_unmap,
    221 	_bus_dmamem_mmap,
    222 	wii_dma_phys_to_bus_mem,
    223 	wii_dma_bus_mem_to_phys,
    224 };
    225 
    226 
    227 /*
    228  * Global variables used here and there
    229  */
    230 struct mem_region physmemr[4], availmemr[4];
    231 char wii_cmdline[1024];
    232 bool wiiu_plat;		/* Running on a Wii U */
    233 bool wiiu_native;	/* Native Wii U mode (not vWii) */
    234 
    235 void initppc(u_int, u_int, u_int, void *); /* Called from locore */
    236 void wii_dolphin_elf_loader_id(void);
    237 
    238 static void wii_setup(void);
    239 static void wii_poweroff(void);
    240 static void wii_reset(void);
    241 static void wii_halt(void);
    242 static void init_decrementer(void);
    243 
    244 static void
    245 system_detect(void)
    246 {
    247 	uint32_t val;
    248 
    249 	val = in32(LT_CHIPREVID);
    250 	if (__SHIFTOUT(val, LT_CHIPREVID_MAGIC) == LT_CHIPREVID_MAGIC_CAFE) {
    251 		wiiu_plat = true;
    252 		wiiu_native = (in32(LT_PIMCOMPAT) & PPC_COMPAT) == 0;
    253 	}
    254 }
    255 
    256 static void
    257 wii_init_cmdline(void)
    258 {
    259 	extern struct wii_argv wii_argv;
    260 
    261 	if (wii_argv.magic == WII_ARGV_MAGIC) {
    262 		void *ptr = (void *)(uintptr_t)(wii_argv.cmdline & ~0x80000000);
    263 		if (ptr != NULL) {
    264 			memcpy(wii_cmdline, ptr, wii_argv.length);
    265 		}
    266 	} else {
    267 		snprintf(wii_cmdline, sizeof(wii_cmdline), WII_DEFAULT_CMDLINE);
    268 	}
    269 }
    270 
    271 static void
    272 wii_init_memmap(u_int endkernel)
    273 {
    274 	uint32_t mem2_start, mem2_end;
    275 
    276 	mem2_start = in32(GLOBAL_MEM2_AVAIL_START) & ~0x80000000;
    277 	mem2_end = in32(GLOBAL_MEM2_AVAIL_END) & ~0x80000000;
    278 	if (mem2_start < WII_MEM2_BASE) {
    279 		/* Must have been booted from MINI. */
    280 		mem2_start = WII_MEM2_BASE + DSP_MEM_SIZE;
    281 		mem2_end = MINI_MEM2_START;
    282 	}
    283 	/*
    284 	 * Clear GLOBAL_MEM2_AVAIL_{START,END} so we can detect the correct
    285 	 * memory size when soft resetting from IOS to MINI.
    286 	 */
    287 	out32(GLOBAL_MEM2_AVAIL_START, 0);
    288 	out32(GLOBAL_MEM2_AVAIL_END, 0);
    289 
    290 	/* MEM1 24MB 1T-SRAM */
    291 	physmemr[0].start = WII_MEM1_BASE;
    292 	physmemr[0].size = WII_MEM1_SIZE;
    293 
    294 	/* MEM2 64MB GDDR3 */
    295 	physmemr[1].start = WII_MEM2_BASE;
    296 	physmemr[1].size = WII_MEM2_SIZE;
    297 
    298 	physmemr[2].size = 0;
    299 
    300 	/* MEM1 available memory */
    301 	availmemr[0].start = ((endkernel & ~0x80000000) + PGOFSET) & ~PGOFSET;
    302 	availmemr[0].size = physmemr[0].size - availmemr[0].start;
    303 	/* External framebuffer is at the end of MEM1 */
    304 	availmemr[0].size -= XFB_SIZE;
    305 
    306 	/* MEM2 available memory */
    307 	availmemr[1].start = mem2_start;
    308 	availmemr[1].size = mem2_end - mem2_start;
    309 
    310 	availmemr[2].size = 0;
    311 }
    312 
    313 static void
    314 wiiu_init_cmdline(void)
    315 {
    316 	struct wiiu_argv *argv = (struct wiiu_argv *)WIIU_LOADER_DATA_ADDR;
    317 
    318 	if (argv->magic == WIIU_LOADER_MAGIC) {
    319 		char *p;
    320 
    321 		/*
    322 		 * Split the command line string into tokens to match
    323 		 * the format used by HBC.
    324 		 */
    325 		memcpy(wii_cmdline, argv->cmdline, sizeof(argv->cmdline));
    326 		wii_cmdline[sizeof(argv->cmdline) - 1] = '\0';
    327 		p = wii_cmdline;
    328 		for (;;) {
    329 			p = strchr(p, ' ');
    330 			if (*p == '\0') {
    331 				break;
    332 			}
    333 			*p++ = '\0';
    334 		}
    335 		p++;
    336 		*p = '\0';
    337 	} else {
    338 		snprintf(wii_cmdline, sizeof(wii_cmdline), WII_DEFAULT_CMDLINE);
    339 	}
    340 }
    341 
    342 static void
    343 wiiu_init_memmap(u_int endkernel)
    344 {
    345 	/* MEM1 32MB */
    346 	physmemr[0].start  = 0x00000000;
    347 	physmemr[0].size   = 0x2000000;
    348 
    349 	/* MEM0 3MB */
    350 	physmemr[1].start  = 0x08000000;
    351 	physmemr[1].size   = 0x300000;
    352 
    353 	/* MEM2 2GB */
    354 	physmemr[2].start  = 0x10000000;
    355 	physmemr[2].size   = 0x80000000;
    356 
    357 	physmemr[3].size   = 0;
    358 
    359 	/* MEM1 available memory */
    360 	availmemr[0].start = (endkernel + PGOFSET) & ~PGOFSET;
    361 	availmemr[0].size  = physmemr[0].size - availmemr[0].start;
    362 
    363 	/*
    364 	 * MEM2 available memory. Reserved regions:
    365 	 *   USB DMA			0x10000000 - 0x103fffff
    366 	 *   Sound DMA			0x10400000 - 0x1041ffff
    367 	 *   Framebuffer		0x17500000 - 0x17a7ffff
    368 	 */
    369 	availmemr[1].start = 0x10420000;
    370 	availmemr[1].size  = 0x17500000 - availmemr[1].start;
    371 	availmemr[2].start = 0x17a80000;
    372 	availmemr[2].size  = physmemr[2].start + physmemr[2].size -
    373 			     availmemr[2].start;
    374 
    375 	availmemr[3].size  = 0;
    376 }
    377 
    378 void
    379 initppc(u_int startkernel, u_int endkernel, u_int args, void *btinfo)
    380 {
    381 	extern uint32_t ticks_per_sec;
    382 	extern uint32_t ticks_per_msec;
    383 	extern unsigned char edata[], end[];
    384 	register_t scratch, spr;
    385 
    386 	memset(&edata, 0, end - edata); /* clear BSS */
    387 	wii_cmdline[0] = '\0';
    388 
    389 	system_detect();
    390 
    391 	if (wiiu_native) {
    392 		wiiu_init_cmdline();
    393 		wiiu_init_memmap(endkernel);
    394 	} else {
    395 		wii_init_cmdline();
    396 		wii_init_memmap(endkernel);
    397 	}
    398 
    399 #ifdef BOOTHOWTO
    400 	/*
    401 	 * boothowto
    402 	 */
    403 	boothowto = BOOTHOWTO;
    404 #endif
    405 
    406 	cpu_model_init();
    407 
    408 	if (!wiiu_native) {
    409 		spr = mfspr(IBM750CL_SPR_HID4);
    410 		spr |= HID4_L2_CCFI;
    411 		spr |= HID4_DBP;
    412 		mtspr(IBM750CL_SPR_HID4, spr);
    413 		asm volatile ("isync");
    414 	}
    415 
    416 	if (wiiu_native) {
    417 		spr = mfspr(SPR_IBMESPRESSO_HID5);
    418 		mtspr(SPR_IBMESPRESSO_HID5, spr | HID5_H5A | HID5_PIRE);
    419 
    420 		spr = mfspr(SPR_SCR);
    421 		mtspr(SPR_SCR, (spr & ~0x40000000) | 0x80000000);
    422 
    423 		spr = mfspr(SPR_CAR);
    424 		mtspr(SPR_CAR, spr | 0xfc100000);
    425 
    426 		mtspr(SPR_BCR, 0x08000000);
    427 
    428 		asm volatile ("isync");
    429 	}
    430 
    431 	/* Configure L2 cache */
    432 	l2cr_config = L2CR_L2E;
    433 
    434 	if (bus_space_init(&wii_mem_tag, NULL,
    435 			   ex_storage[0], sizeof(ex_storage[0]))) {
    436 		panic("bus_space_init failed");
    437 	}
    438 
    439 	/*
    440 	 * Initialize the BAT registers
    441 	 */
    442 	if (wiiu_native) {
    443 		oea_batinit(
    444 		    WII_IOMEM_BASE, BAT_BL_32M,
    445 		    0);
    446 	} else {
    447 		oea_batinit(
    448 		    EFB_BASE, BAT_BL_128M,
    449 		    0);
    450 	}
    451 
    452 	/*
    453 	 * Set up trap vectors
    454 	 */
    455 	oea_init(NULL);
    456 
    457 	/*
    458 	 * Get CPU clock
    459 	 */
    460 	ticks_per_sec = wiiu_native ? WIIU_TIMEBASE_FREQ_HZ :
    461 				      WII_TIMEBASE_FREQ_HZ;
    462 	ticks_per_msec = ticks_per_sec / 1000;
    463 	cpu_timebase = ticks_per_sec;
    464 
    465 	wii_setup();
    466 
    467 	uvm_md_init();
    468 
    469 	/*
    470 	 * Initialize pmap module.
    471 	 */
    472 	pmap_bootstrap(startkernel, endkernel);
    473 
    474 	/* Now enable translation (and machine checks/recoverable interrupts) */
    475 	asm volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    476 		      : "=r"(scratch)
    477 		      : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    478 
    479 	/*
    480 	 * Setup decrementer
    481 	 */
    482 	init_decrementer();
    483 }
    484 
    485 void
    486 mem_regions(struct mem_region **mem, struct mem_region **avail)
    487 {
    488 	*mem = physmemr;
    489 	*avail = availmemr;
    490 }
    491 
    492 /*
    493  * Machine dependent startup code.
    494  */
    495 void
    496 cpu_startup(void)
    497 {
    498 	oea_startup(NULL);
    499 
    500 	/*
    501 	 * Now that we have VM, malloc()s are OK in bus_space.
    502 	 */
    503 	bus_space_mallocok();
    504 
    505 	/* Set up interrupt controller */
    506 	pic_init();
    507 	pi_init_intr();
    508 	oea_install_extint(pic_ext_intr);
    509 
    510 #ifdef MULTIPROCESSOR
    511 	if (wiiu_native) {
    512 		ipi_latte_init();
    513 		oea_install_extint_vec(pic_ext_intr, EXC_IPI);
    514 	}
    515 #endif
    516 }
    517 
    518 /*
    519  * No early console support.
    520  */
    521 void
    522 consinit(void)
    523 {
    524 #if NGENFB > 0
    525 	if (wiiu_native) {
    526 		extern void wiiufb_consinit(void);
    527 		wiiufb_consinit();
    528 	}
    529 #endif
    530 #if NGECKO > 0
    531 	if (!wiiu_native) {
    532 		usbgecko_consinit();
    533 	}
    534 #endif
    535 #if NUKBD > 0
    536 	ukbd_cnattach();
    537 #endif
    538 }
    539 
    540 /*
    541  * Halt or reboot the machine after syncing/dumping according to howto.
    542  */
    543 void
    544 cpu_reboot(int howto, char *what)
    545 {
    546 	static int syncdone = false;
    547 
    548 	if (cold) {
    549 		howto |= RB_HALT;
    550 		goto haltsys;
    551 	}
    552 
    553 	boothowto = howto;
    554 
    555 	if ((howto & RB_NOSYNC) == 0 && panicstr == NULL) {
    556 		if (!syncdone) {
    557 			syncdone = true;
    558 			/* XXX used to force unmount as well, here */
    559 			vfs_sync_all(curlwp);
    560 		}
    561 
    562 		while (vfs_unmountall1(curlwp, false, false) ||
    563 		       config_detach_all(boothowto) ||
    564 		       vfs_unmount_forceone(curlwp)) {
    565 			/* do nothing */
    566 		}
    567 	} else {
    568 		if (!db_active) {
    569 			suspendsched();
    570 		}
    571 	}
    572 
    573 	pmf_system_shutdown(boothowto);
    574 
    575 	/* Disable interrupts */
    576 	splhigh();
    577 
    578 	if ((howto & (RB_DUMP | RB_HALT)) == RB_DUMP) {
    579 		oea_dumpsys();
    580 	}
    581 
    582 haltsys:
    583 	doshutdownhooks();
    584 
    585 #ifdef MULTIPROCESSOR
    586 	cpu_halt_others();
    587 	delay(100000);
    588 #endif
    589 
    590 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    591 		printf("power off\n\n");
    592 		wii_poweroff();
    593 		delay(100000);
    594 		printf("power off failed!\n\n");
    595 	}
    596 
    597 	if (howto & RB_HALT) {
    598 		printf("The operating system has halted.\n");
    599 		wii_halt();
    600 	}
    601 
    602 	printf("rebooting...\n\n");
    603 	wii_reset();
    604 	while (1);
    605 }
    606 
    607 static void
    608 wii_setup(void)
    609 {
    610 	/* Enable PPC access to EXI bus. */
    611 	out32(HW_AIPPROT, in32(HW_AIPPROT) | ENAHBIOPI);
    612 
    613 	if (!wiiu_plat) {
    614 		/* Turn on the drive slot LED. */
    615 		wii_slot_led(true);
    616 
    617 		/* Enable PPC access to SHUTDOWN GPIO. */
    618 		out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_SHUTDOWN));
    619 
    620 		/* Enable PPC access to DI_SPIN GPIO. */
    621 		out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_DI_SPIN));
    622 
    623 		/* Enable DVD video support. */
    624 		out32(HW_COMPAT, in32(HW_COMPAT) & ~DVDVIDEO);
    625 	}
    626 }
    627 
    628 static void
    629 wiiu_wood_ipc(uint32_t msg)
    630 {
    631 	KASSERT(wiiu_native);
    632 
    633 	out32(HW_IPCPPCMSG, msg);
    634 	out32(HW_IPCPPCCTRL, HW_IPCPPCCTRL_X1);
    635 	for (;;) {
    636 		if ((in32(HW_IPCPPCCTRL) & HW_IPCPPCCTRL_X1) == 0) {
    637 			break;
    638 		}
    639 	}
    640 }
    641 
    642 static void
    643 wii_poweroff(void)
    644 {
    645 	if (wiiu_native) {
    646 		wiiu_wood_ipc(CMD_POWEROFF);
    647 	} else {
    648 		out32(HW_GPIOB_OUT, in32(HW_GPIOB_OUT) | __BIT(GPIO_SHUTDOWN));
    649 	}
    650 }
    651 
    652 static void
    653 wii_reset(void)
    654 {
    655 	if (wiiu_native) {
    656 		wiiu_wood_ipc(CMD_REBOOT);
    657 	} else {
    658 		out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB);
    659 	}
    660 }
    661 
    662 static void
    663 wii_halt(void)
    664 {
    665 	printf("Please press the %s button to reboot.\n",
    666 	    wiiu_native ? "POWER" : "RESET");
    667 	if (wiiu_native) {
    668 		bool pressed = false;
    669 		while (1) {
    670 			if ((in32(HW_GPIOB_IN) & __BIT(WIIU_GPIO_POWER)) != 0) {
    671 				/* Button was pressed */
    672 				pressed = true;
    673 			} else if (pressed) {
    674 				/* Button was released */
    675 				break;
    676 			}
    677 		}
    678 	} else {
    679 		while (1) {
    680 			if ((in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0) {
    681 				break;
    682 			}
    683 		}
    684 	}
    685 }
    686 
    687 static void
    688 init_decrementer(void)
    689 {
    690 	extern uint32_t ns_per_tick;
    691 	extern uint32_t ticks_per_intr;
    692 	extern uint32_t ticks_per_sec;
    693 	int scratch, msr;
    694 
    695 	KASSERT(ticks_per_sec != 0);
    696 
    697 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    698 			: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    699 	ns_per_tick = 1000000000 / ticks_per_sec;
    700 	ticks_per_intr = ticks_per_sec / hz;
    701 	cpu_timebase = ticks_per_sec;
    702 
    703 	curcpu()->ci_lasttb = mftbl();
    704 
    705 	mtspr(SPR_DEC, ticks_per_intr);
    706 	mtmsr(msr);
    707 }
    708