1 /* $NetBSD: machdep.c,v 1.11 2025/10/25 15:02:56 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.11 2025/10/25 15:02:56 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 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 #ifndef WII_DEFAULT_CMDLINE 126 #define WII_DEFAULT_CMDLINE "root=ld0a" 127 #endif 128 129 #define IBM750CL_SPR_HID4 1011 130 #define HID4_DBP 0x00400000 /* Data bus parking */ 131 #define HID4_L2_CCFI 0x00100000 /* L2 complete castout prior 132 * to L2 flash invalidate. 133 */ 134 135 #define MINI_MEM2_START 0x13f00000 /* Start of reserved MEM2 for MINI */ 136 137 #define PI_INTERRUPT_CAUSE 0x0c003000 138 #define RESET_SWITCH_STATE __BIT(16) 139 140 extern u_int l2cr_config; 141 142 struct powerpc_bus_space wii_mem_tag = { 143 .pbs_flags = _BUS_SPACE_BIG_ENDIAN | 144 _BUS_SPACE_MEM_TYPE, 145 }; 146 147 static char ex_storage[1][EXTENT_FIXED_STORAGE_SIZE(EXTMAP_RANGES)] 148 __attribute__((aligned(8))); 149 150 static bus_addr_t 151 wii_dma_phys_to_bus_mem(bus_dma_tag_t t, bus_addr_t addr) 152 { 153 return addr; 154 } 155 156 static bus_addr_t 157 wii_dma_bus_mem_to_phys(bus_dma_tag_t t, bus_addr_t addr) 158 { 159 return addr; 160 } 161 162 static int 163 wii_mem2_dmamem_alloc(bus_dma_tag_t t, bus_size_t size, bus_size_t alignment, 164 bus_size_t boundary, bus_dma_segment_t *segs, int nsegs, int *rsegs, 165 int flags) 166 { 167 struct mem_region *mem, *avail; 168 169 /* Restrict memory used for DMA to ranges in MEM2 */ 170 mem_regions(&mem, &avail); 171 if (mem[1].size == 0) { 172 return ENOMEM; 173 } 174 175 return _bus_dmamem_alloc_range(t, size, alignment, boundary, segs, 176 nsegs, rsegs, flags, mem[1].start, 177 mem[1].start + mem[1].size - PAGE_SIZE - 1); 178 } 179 180 struct powerpc_bus_dma_tag wii_bus_dma_tag = { 181 0, /* _bounce_thresh */ 182 _bus_dmamap_create, 183 _bus_dmamap_destroy, 184 _bus_dmamap_load, 185 _bus_dmamap_load_mbuf, 186 _bus_dmamap_load_uio, 187 _bus_dmamap_load_raw, 188 _bus_dmamap_unload, 189 _bus_dmamap_sync, 190 _bus_dmamem_alloc, 191 _bus_dmamem_free, 192 _bus_dmamem_map, 193 _bus_dmamem_unmap, 194 _bus_dmamem_mmap, 195 wii_dma_phys_to_bus_mem, 196 wii_dma_bus_mem_to_phys, 197 }; 198 199 struct powerpc_bus_dma_tag wii_mem2_bus_dma_tag = { 200 0, /* _bounce_thresh */ 201 _bus_dmamap_create, 202 _bus_dmamap_destroy, 203 _bus_dmamap_load, 204 _bus_dmamap_load_mbuf, 205 _bus_dmamap_load_uio, 206 _bus_dmamap_load_raw, 207 _bus_dmamap_unload, 208 _bus_dmamap_sync, 209 wii_mem2_dmamem_alloc, 210 _bus_dmamem_free, 211 _bus_dmamem_map, 212 _bus_dmamem_unmap, 213 _bus_dmamem_mmap, 214 wii_dma_phys_to_bus_mem, 215 wii_dma_bus_mem_to_phys, 216 }; 217 218 219 /* 220 * Global variables used here and there 221 */ 222 struct mem_region physmemr[3], availmemr[3]; 223 char wii_cmdline[1024]; 224 225 void initppc(u_int, u_int, u_int, void *); /* Called from locore */ 226 void wii_dolphin_elf_loader_id(void); 227 228 static void wii_setup(void); 229 static void init_decrementer(void); 230 231 void 232 initppc(u_int startkernel, u_int endkernel, u_int args, void *btinfo) 233 { 234 extern uint32_t ticks_per_sec; 235 extern uint32_t ticks_per_msec; 236 extern unsigned char edata[], end[]; 237 extern struct wii_argv wii_argv; 238 uint32_t mem2_start, mem2_end; 239 register_t scratch; 240 241 memset(&edata, 0, end - edata); /* clear BSS */ 242 243 wii_cmdline[0] = '\0'; 244 if (wii_argv.magic == WII_ARGV_MAGIC) { 245 void *ptr = (void *)(uintptr_t)(wii_argv.cmdline & ~0x80000000); 246 if (ptr != NULL) { 247 memcpy(wii_cmdline, ptr, wii_argv.length); 248 } 249 } else { 250 snprintf(wii_cmdline, sizeof(wii_cmdline), WII_DEFAULT_CMDLINE); 251 } 252 253 mem2_start = in32(GLOBAL_MEM2_AVAIL_START) & ~0x80000000; 254 mem2_end = in32(GLOBAL_MEM2_AVAIL_END) & ~0x80000000; 255 if (mem2_start < WII_MEM2_BASE) { 256 /* Must have been booted from MINI. */ 257 mem2_start = WII_MEM2_BASE + DSP_MEM_SIZE; 258 mem2_end = MINI_MEM2_START; 259 } 260 /* 261 * Clear GLOBAL_MEM2_AVAIL_{START,END} so we can detect the correct 262 * memory size when soft resetting from IOS to MINI. 263 */ 264 out32(GLOBAL_MEM2_AVAIL_START, 0); 265 out32(GLOBAL_MEM2_AVAIL_END, 0); 266 267 /* MEM1 24MB 1T-SRAM */ 268 physmemr[0].start = WII_MEM1_BASE; 269 physmemr[0].size = WII_MEM1_SIZE; 270 271 /* MEM2 64MB GDDR3 */ 272 physmemr[1].start = WII_MEM2_BASE; 273 physmemr[1].size = WII_MEM2_SIZE; 274 275 physmemr[2].size = 0; 276 277 /* MEM1 available memory */ 278 availmemr[0].start = ((endkernel & ~0x80000000) + PGOFSET) & ~PGOFSET; 279 availmemr[0].size = physmemr[0].size - availmemr[0].start; 280 /* External framebuffer is at the end of MEM1 */ 281 availmemr[0].size -= XFB_SIZE; 282 283 /* MEM2 available memory */ 284 availmemr[1].start = mem2_start; 285 availmemr[1].size = mem2_end - mem2_start; 286 287 availmemr[2].size = 0; 288 289 #ifdef BOOTHOWTO 290 /* 291 * boothowto 292 */ 293 boothowto = BOOTHOWTO; 294 #endif 295 296 /* 297 * HID4[L2_CCFI] must be set to 1 for correct operation of L2 cache. 298 * HID4[DBP] must be set to 1 to support multiple bus masters. 299 */ 300 mtspr(IBM750CL_SPR_HID4, 301 mfspr(IBM750CL_SPR_HID4) | HID4_L2_CCFI | HID4_DBP); 302 asm volatile ("isync"); 303 304 /* Configure L2 cache */ 305 l2cr_config = L2CR_L2E | L2CR_L2PE; 306 307 if (bus_space_init(&wii_mem_tag, NULL, 308 ex_storage[0], sizeof(ex_storage[0]))) { 309 panic("bus_space_init failed"); 310 } 311 312 /* 313 * Initialize the BAT registers 314 */ 315 oea_batinit( 316 EFB_BASE, BAT_BL_128M, 317 0); 318 319 /* 320 * Set up trap vectors 321 */ 322 oea_init(NULL); 323 324 /* 325 * Get CPU clock 326 */ 327 ticks_per_sec = TIMEBASE_FREQ_HZ; 328 ticks_per_msec = ticks_per_sec / 1000; 329 cpu_timebase = ticks_per_sec; 330 331 wii_setup(); 332 333 uvm_md_init(); 334 335 /* 336 * Initialize pmap module. 337 */ 338 pmap_bootstrap(startkernel, endkernel); 339 340 /* Now enable translation (and machine checks/recoverable interrupts) */ 341 asm volatile ("sync; mfmsr %0; ori %0,%0,%1; mtmsr %0; isync" 342 : "=r"(scratch) 343 : "K"(PSL_IR|PSL_DR|PSL_ME|PSL_RI)); 344 345 /* 346 * Setup decrementer 347 */ 348 init_decrementer(); 349 } 350 351 void 352 mem_regions(struct mem_region **mem, struct mem_region **avail) 353 { 354 *mem = physmemr; 355 *avail = availmemr; 356 } 357 358 /* 359 * Machine dependent startup code. 360 */ 361 void 362 cpu_startup(void) 363 { 364 extern void pi_init_intr(void); 365 366 oea_startup(NULL); 367 368 /* 369 * Now that we have VM, malloc()s are OK in bus_space. 370 */ 371 bus_space_mallocok(); 372 373 /* Set up interrupt controller */ 374 pic_init(); 375 pi_init_intr(); 376 oea_install_extint(pic_ext_intr); 377 } 378 379 /* 380 * No early console support. 381 */ 382 void 383 consinit(void) 384 { 385 #if NUKBD > 0 386 ukbd_cnattach(); 387 #endif 388 } 389 390 /* 391 * Halt or reboot the machine after syncing/dumping according to howto. 392 */ 393 void 394 cpu_reboot(int howto, char *what) 395 { 396 static int syncing; 397 extern void disable_intr(void); 398 399 boothowto = howto; 400 if (!cold && !(howto & RB_NOSYNC) && !syncing) { 401 syncing = 1; 402 vfs_shutdown(); /* sync */ 403 } 404 splhigh(); 405 if (!cold && (howto & RB_DUMP)) { 406 oea_dumpsys(); 407 } 408 pmf_system_shutdown(boothowto); 409 doshutdownhooks(); 410 411 disable_intr(); 412 413 /* Force halt on panic to capture the cause on screen. */ 414 if (panicstr != NULL) { 415 howto |= RB_HALT; 416 } 417 if ((howto & RB_POWERDOWN) == RB_POWERDOWN) { 418 printf("power off\n\n"); 419 out32(HW_GPIOB_OUT, in32(HW_GPIOB_OUT) | __BIT(GPIO_SHUTDOWN)); 420 delay(100000); 421 printf("power off failed!\n\n"); 422 } 423 if (howto & RB_HALT) { 424 printf("The operating system has halted.\n"); 425 printf("Please press the RESET button to reboot.\n"); 426 while (1) { 427 if ((in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0) { 428 break; 429 } 430 } 431 } 432 433 printf("rebooting...\n\n"); 434 out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB); 435 while (1); 436 } 437 438 static void 439 wii_setup(void) 440 { 441 /* Turn on the drive slot LED. */ 442 wii_slot_led(true); 443 444 /* Enable PPC access to SHUTDOWN GPIO. */ 445 out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_SHUTDOWN)); 446 447 /* Enable PPC access to DI_SPIN GPIO. */ 448 out32(HW_GPIO_OWNER, in32(HW_GPIO_OWNER) | __BIT(GPIO_DI_SPIN)); 449 450 /* Enable PPC access to EXI bus. */ 451 out32(HW_AIPPROT, in32(HW_AIPPROT) | ENAHBIOPI); 452 453 /* Enable DVD video support. */ 454 out32(HW_COMPAT, in32(HW_COMPAT) & ~DVDVIDEO); 455 } 456 457 static void 458 init_decrementer(void) 459 { 460 extern uint32_t ns_per_tick; 461 extern uint32_t ticks_per_intr; 462 extern uint32_t ticks_per_sec; 463 int scratch, msr; 464 465 KASSERT(ticks_per_sec != 0); 466 467 __asm volatile ("mfmsr %0; andi. %1,%0,%2; mtmsr %1" 468 : "=r"(msr), "=r"(scratch) : "K"((u_short)~PSL_EE)); 469 ns_per_tick = 1000000000 / ticks_per_sec; 470 ticks_per_intr = ticks_per_sec / hz; 471 cpu_timebase = ticks_per_sec; 472 473 curcpu()->ci_lasttb = mftbl(); 474 475 mtspr(SPR_DEC, ticks_per_intr); 476 mtmsr(msr); 477 } 478