Home | History | Annotate | Line # | Download | only in wii
machdep.c revision 1.4.2.2
      1 /* $NetBSD: machdep.c,v 1.4.2.2 2024/02/03 11:47:07 martin 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.4.2.2 2024/02/03 11:47:07 martin 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 
     75 #include <sys/param.h>
     76 #include <sys/buf.h>
     77 #include <sys/bus.h>
     78 #include <sys/conf.h>
     79 #include <sys/device.h>
     80 #include <sys/exec.h>
     81 #include <sys/extent.h>
     82 #include <sys/intr.h>
     83 #include <sys/kernel.h>
     84 #include <sys/kgdb.h>
     85 #include <sys/ksyms.h>
     86 #include <sys/mbuf.h>
     87 #include <sys/mount.h>
     88 #include <sys/msgbuf.h>
     89 #include <sys/proc.h>
     90 #include <sys/reboot.h>
     91 #include <sys/syscallargs.h>
     92 #include <sys/sysctl.h>
     93 #include <sys/syslog.h>
     94 #include <sys/systm.h>
     95 
     96 #include <uvm/uvm_extern.h>
     97 
     98 #include <machine/powerpc.h>
     99 #include <machine/wii.h>
    100 
    101 #include <powerpc/bus_funcs.h>
    102 #include <powerpc/db_machdep.h>
    103 #include <powerpc/pio.h>
    104 #include <powerpc/pmap.h>
    105 #include <powerpc/spr.h>
    106 #include <powerpc/trap.h>
    107 
    108 #include <powerpc/oea/bat.h>
    109 #include <powerpc/oea/spr.h>
    110 #include <powerpc/pic/picvar.h>
    111 
    112 #include <ddb/db_extern.h>
    113 
    114 #include <dev/wscons/wsconsio.h>
    115 #include <dev/wscons/wsdisplayvar.h>
    116 #include <dev/rasops/rasops.h>
    117 #include <dev/wsfont/wsfont.h>
    118 #include <dev/wscons/wsdisplay_vconsvar.h>
    119 
    120 #include <dev/usb/ukbdvar.h>
    121 
    122 #include "ksyms.h"
    123 #include "ukbd.h"
    124 
    125 #define IBM750CL_SPR_HID4	1011
    126 #define	 L2_CCFI		0x00100000	/* L2 complete castout prior
    127 						 * to L2 flash invalidate.
    128 						 */
    129 
    130 extern u_int l2cr_config;
    131 
    132 struct powerpc_bus_space wii_mem_tag = {
    133 	.pbs_flags = _BUS_SPACE_BIG_ENDIAN |
    134 		     _BUS_SPACE_MEM_TYPE,
    135 	.pbs_offset = 0,
    136 	.pbs_base = 0x0c000000,
    137 	.pbs_limit = 0x0dffffff,
    138 	.pbs_extent = NULL,
    139 };
    140 
    141 static char ex_storage[1][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)]
    142     __attribute__((aligned(8)));
    143 
    144 static bus_addr_t
    145 wii_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t addr)
    146 {
    147 	return addr;
    148 }
    149 
    150 static bus_addr_t
    151 wii_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t addr)
    152 {
    153 	return addr;
    154 }
    155 
    156 static int
    157 wii_mem2_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment,
    158     bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs,
    159     int flags)
    160 {
    161 	struct mem_region *mem, *avail;
    162 
    163 	/* Restrict memory used for DMA to ranges in MEM2 */
    164 	mem_regions(&mem, &avail);
    165 	if (mem[1].size == 0) {
    166 		return ENOMEM;
    167 	}
    168 
    169 	return _bus_dmamem_alloc_range(t, size, alignment, boundary, segs,
    170 	    nsegs, rsegs, flags, mem[1].start,
    171 	    mem[1].start + mem[1].size - PAGE_SIZE - 1);
    172 }
    173 
    174 struct powerpc_bus_dma_tag wii_bus_dma_tag = {
    175 	0,				/* _bounce_thresh */
    176 	_bus_dmamap_create,
    177 	_bus_dmamap_destroy,
    178 	_bus_dmamap_load,
    179 	_bus_dmamap_load_mbuf,
    180 	_bus_dmamap_load_uio,
    181 	_bus_dmamap_load_raw,
    182 	_bus_dmamap_unload,
    183 	_bus_dmamap_sync,
    184 	_bus_dmamem_alloc,
    185 	_bus_dmamem_free,
    186 	_bus_dmamem_map,
    187 	_bus_dmamem_unmap,
    188 	_bus_dmamem_mmap,
    189 	wii_dma_phys_to_bus_mem,
    190 	wii_dma_bus_mem_to_phys,
    191 };
    192 
    193 struct powerpc_bus_dma_tag wii_mem2_bus_dma_tag = {
    194 	0,				/* _bounce_thresh */
    195 	_bus_dmamap_create,
    196 	_bus_dmamap_destroy,
    197 	_bus_dmamap_load,
    198 	_bus_dmamap_load_mbuf,
    199 	_bus_dmamap_load_uio,
    200 	_bus_dmamap_load_raw,
    201 	_bus_dmamap_unload,
    202 	_bus_dmamap_sync,
    203 	wii_mem2_dmamem_alloc,
    204 	_bus_dmamem_free,
    205 	_bus_dmamem_map,
    206 	_bus_dmamem_unmap,
    207 	_bus_dmamem_mmap,
    208 	wii_dma_phys_to_bus_mem,
    209 	wii_dma_bus_mem_to_phys,
    210 };
    211 
    212 
    213 /*
    214  * Global variables used here and there
    215  */
    216 struct mem_region physmemr[3], availmemr[3];
    217 char wii_cmdline[1024];
    218 
    219 void initppc(u_int, u_int, u_int, void *); /* Called from locore */
    220 void wii_dolphin_elf_loader_id(void);
    221 
    222 static void wii_setup(void);
    223 static void init_decrementer(void);
    224 
    225 void
    226 initppc(u_int startkernel, u_int endkernel, u_int args, void *btinfo)
    227 {
    228 	extern u_long ticks_per_sec;
    229 	extern unsigned char edata[], end[];
    230 	extern struct wii_argv wii_argv;
    231 	uint32_t mem2_size;
    232 	register_t scratch;
    233 
    234 	memset(&edata, 0, end - edata); /* clear BSS */
    235 
    236 	if (wii_argv.magic == WII_ARGV_MAGIC) {
    237 		void *ptr = (void *)(uintptr_t)(wii_argv.cmdline & ~0x80000000);
    238 		if (ptr != NULL) {
    239 			memcpy(wii_cmdline, ptr, wii_argv.length);
    240 		}
    241 	}
    242 
    243 	mem2_size = in32(GLOBAL_MEM2_SIZE);
    244 
    245 	/* MEM1 24MB 1T-SRAM */
    246 	physmemr[0].start = WII_MEM1_BASE;
    247 	physmemr[0].size = WII_MEM1_SIZE;
    248 
    249 	/* MEM2 64MB GDDR3 */
    250 	physmemr[1].start = WII_MEM2_BASE;
    251 	physmemr[1].size = mem2_size;
    252 
    253 	physmemr[2].size = 0;
    254 
    255 	/* MEM1 available memory */
    256 	availmemr[0].start = ((endkernel & ~0x80000000) + PGOFSET) & ~PGOFSET;
    257 	availmemr[0].size = physmemr[0].size - availmemr[0].start;
    258 	/* External framebuffer is at the end of MEM1 */
    259 	availmemr[0].size -= XFB_SIZE;
    260 
    261 	/* MEM2 available memory */
    262 	availmemr[1].start = physmemr[1].start;
    263 	availmemr[1].size = physmemr[1].size;
    264 	if (mem2_size != 0) {
    265 		/* DSP uses 16KB at the start of MEM2 */
    266 		availmemr[1].start += DSP_MEM_SIZE;
    267 		availmemr[1].size -= DSP_MEM_SIZE;
    268 		/* IPC and Starlet use memory at the end of MEM2 */
    269 		availmemr[1].size -= IPC_SIZE;
    270 		availmemr[1].size -= ARM_SIZE;
    271 	}
    272 
    273 	availmemr[2].size = 0;
    274 
    275 #ifdef BOOTHOWTO
    276 	/*
    277 	 * boothowto
    278 	 */
    279 	boothowto = BOOTHOWTO;
    280 #endif
    281 
    282 	/* HID4[L2_CCFI] must be set to 1 for correct operation of L2 cache */
    283 	mtspr(IBM750CL_SPR_HID4, mfspr(IBM750CL_SPR_HID4) | L2_CCFI);
    284 
    285 	/* Configure L2 cache */
    286 	l2cr_config = L2CR_L2E | L2CR_L2PE;
    287 
    288 	if (bus_space_init(&wii_mem_tag, "iomem",
    289 			   ex_storage[0], sizeof(ex_storage[0]))) {
    290 		panic("bus_space_init failed");
    291 	}
    292 
    293 	/*
    294 	 * Initialize the BAT registers
    295 	 */
    296 	oea_batinit(
    297 	    WII_IOMEM_BASE, BAT_BL_32M,
    298 	    0);
    299 
    300 	/*
    301 	 * Set up trap vectors
    302 	 */
    303 	oea_init(NULL);
    304 
    305 	/*
    306 	 * Get CPU clock
    307 	 */
    308 	ticks_per_sec = TIMEBASE_FREQ_HZ;
    309 	cpu_timebase = ticks_per_sec;
    310 
    311 	wii_setup();
    312 
    313 	uvm_md_init();
    314 
    315 	/*
    316 	 * Initialize pmap module.
    317 	 */
    318 	pmap_bootstrap(startkernel, endkernel);
    319 
    320 	/* Now enable translation (and machine checks/recoverable interrupts) */
    321 	__asm __volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync"
    322 			  : "=r"(scratch)
    323 			  : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI));
    324 
    325 	/*
    326 	 * Setup decrementer
    327 	 */
    328 	init_decrementer();
    329 }
    330 
    331 void
    332 mem_regions(struct mem_region **mem, struct mem_region **avail)
    333 {
    334 	*mem = physmemr;
    335 	*avail = availmemr;
    336 }
    337 
    338 /*
    339  * Machine dependent startup code.
    340  */
    341 void
    342 cpu_startup(void)
    343 {
    344 	extern void pi_init_intr(void);
    345 
    346 	oea_startup(NULL);
    347 
    348 	/*
    349 	 * Now that we have VM, malloc()s are OK in bus_space.
    350 	 */
    351 	bus_space_mallocok();
    352 
    353 	/* Set up interrupt controller */
    354 	pic_init();
    355 	pi_init_intr();
    356 	oea_install_extint(pic_ext_intr);
    357 }
    358 
    359 /*
    360  * No early console support.
    361  */
    362 void
    363 consinit(void)
    364 {
    365 #if NUKBD > 0
    366 	ukbd_cnattach();
    367 #endif
    368 }
    369 
    370 /*
    371  * Halt or reboot the machine after syncing/dumping according to howto.
    372  */
    373 void
    374 cpu_reboot(int howto, char *what)
    375 {
    376 	static int syncing;
    377 	extern void disable_intr(void);
    378 
    379 	boothowto = howto;
    380 	if (!cold && !(howto & RB_NOSYNC) && !syncing) {
    381 		syncing = 1;
    382 		vfs_shutdown();		/* sync */
    383 		resettodr();		/* set wall clock */
    384 	}
    385 	splhigh();
    386 	if (!cold && (howto & RB_DUMP)) {
    387 		oea_dumpsys();
    388 	}
    389 	pmf_system_shutdown(boothowto);
    390 	doshutdownhooks();
    391 
    392 	disable_intr();
    393 
    394 	/* Force halt on panic to capture the cause on screen. */
    395 	if (panicstr != NULL) {
    396 		howto |= RB_HALT;
    397 	}
    398 	if ((howto & RB_POWERDOWN) == RB_POWERDOWN) {
    399 		printf("power off\n\n");
    400 		out32(HW_GPIOB_OUT, in32(HW_GPIOB_OUT) | __BIT(GPIO_SHUTDOWN));
    401 		delay(100000);
    402 		printf("power off failed!\n\n");
    403 	}
    404 	if (howto & RB_HALT) {
    405 		printf("halted\n\n");
    406 		while (1);
    407 	}
    408 
    409 	printf("rebooting\n\n");
    410 	out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB);
    411 	while (1);
    412 }
    413 
    414 static void
    415 wii_setup(void)
    416 {
    417 	/* Turn on the drive slot LED. */
    418 	out32(HW_GPIOB_OUT, in32(HW_GPIOB_OUT) | __BIT(GPIO_SLOT_LED));
    419 
    420 	/* Enable PPC access to SHUTDOWN GPIO. */
    421 	out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_SHUTDOWN));
    422 }
    423 
    424 static void
    425 init_decrementer(void)
    426 {
    427 	extern uint32_t ns_per_tick;
    428 	extern uint32_t ticks_per_intr;
    429 	extern u_long ticks_per_sec;
    430 	int scratch, msr;
    431 
    432 	KASSERT(ticks_per_sec != 0);
    433 
    434 	__asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1"
    435 			: "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE));
    436 	ns_per_tick = 1000000000 / ticks_per_sec;
    437 	ticks_per_intr = ticks_per_sec / hz;
    438 	cpu_timebase = ticks_per_sec;
    439 
    440 	curcpu()->ci_lasttb = mftbl();
    441 
    442 	mtspr(SPR_DEC, ticks_per_intr);
    443 	mtmsr(msr);
    444 }
    445