/* KallistiOS ##version## maple_init_shutdown.c Copyright (C) 2002 Megan Potter Copyright (C) 2026 Ruslan Rostovtsev */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* This system handles low-level communication/initialization of the maple bus. Specific devices aren't handled by this module, rather, the modules implementing specific devices can use this module to access them. Thanks to Marcus Comstedt for information on the maple bus. Thanks to the LinuxDC guys for some ideas on how to better implement this. */ static void maple_dev_reset_cb(maple_state_t *st, maple_frame_t *frame) { (void)st; /* Unlock the frame */ maple_frame_unlock(frame); /* Wake up! */ genwait_wake_all(frame); } static void maple_dev_reset(maple_device_t *dev) { assert(dev != NULL); /* Lock the frame */ maple_frame_lock(&dev->frame); /* Reset the frame */ maple_frame_init(&dev->frame); dev->frame.cmd = MAPLE_COMMAND_RESET; dev->frame.dst_port = dev->port; dev->frame.dst_unit = dev->unit; dev->frame.length = 0; dev->frame.callback = maple_dev_reset_cb; maple_queue_frame(&dev->frame); /* Wait for the device to accept it */ if(genwait_wait(&dev->frame, "dev_reset", 500) < 0) { if(dev->frame.state != MAPLE_FRAME_VACANT) { /* Something went wrong.... */ dev->frame.state = MAPLE_FRAME_VACANT; dbglog(DBG_ERROR, "dev_reset: timeout to unit %c%c\n", dev->port + 'A', dev->unit + '0'); } } dbglog(DBG_KDEBUG, "dev_reset: reset sent to unit %c%c\n", dev->port + 'A', dev->unit + '0'); return; } static void maple_dev_reset_all(void) { /* Skip controllers: a RESET re-zeroes their analog axes at the current position, so triggers/stick held at exit stay miscalibrated. */ MAPLE_FOREACH_BEGIN(MAPLE_FUNC_ANY & ~MAPLE_FUNC_CONTROLLER, void, st) maple_dev_reset(__dev); (void)st; MAPLE_FOREACH_END() } static uint8_t maple_dma_addr_prot_byte(uint32_t addr) { return ((((addr) & MEM_AREA_CACHE_MASK) >> 20) - 0x80) & 0xff; } static uint32_t maple_dma_mem_protection(void) { uint32_t ram_lo, ram_hi; ram_lo = maple_dma_addr_prot_byte(0x0c000000); ram_hi = maple_dma_addr_prot_byte(_arch_mem_top - 1); return MAPLE_DMA_PROT_MAGIC | (ram_lo << 8) | ram_hi; } /* Initialize Hardware (call after driver inits) */ static void maple_hw_init(void) { maple_driver_t *drv; dbglog(DBG_INFO, "maple: active drivers:\n"); /* Reset structures */ for(size_t p = 0; p < MAPLE_PORT_COUNT; p++) { maple_state.ports[p].port = p; for(size_t u = 0; u < MAPLE_UNIT_COUNT; u++) maple_state.ports[p].units[u] = NULL; } TAILQ_INIT(&maple_state.frame_queue); /* Enumerate drivers */ LIST_FOREACH(drv, &maple_state.driver_list, drv_list) { dbglog(DBG_INFO, " %s: %s\n", drv->name, maple_pcaps(drv->functions)); } /* Allocate the DMA send buffer */ if(__is_defined(MAPLE_DMA_DEBUG)) maple_state.dma_buffer = aligned_alloc(32, MAPLE_DMA_SIZE + 1024); else maple_state.dma_buffer = aligned_alloc(32, MAPLE_DMA_SIZE); assert_msg(maple_state.dma_buffer != NULL, "Couldn't allocate maple DMA buffer"); assert_msg(__is_aligned((uint32_t)maple_state.dma_buffer, 32), "DMA buffer was unaligned; bug in dlmalloc; please report!"); /* Force it into the P2 area */ maple_state.dma_buffer = (uint8_t *)((((uint32_t)maple_state.dma_buffer) & MEM_AREA_CACHE_MASK) | MEM_AREA_P2_BASE); if(__is_defined(MAPLE_DMA_DEBUG)) { maple_state.dma_buffer += 512; maple_sentinel_setup(maple_state.dma_buffer - 512, MAPLE_DMA_SIZE + 1024); } maple_state.dma_in_progress = 0; dbglog(DBG_SOURCE(MAPLE_DMA_DEBUG), " DMA Buffer at %08lx\n", (uint32_t)maple_state.dma_buffer); /* Initialize other misc stuff */ maple_state.vbl_cntr = maple_state.dma_cntr = 0; maple_state.detect_port_next = 0; maple_state.scan_ready_mask = 0; maple_state.port0_mie = 0; maple_state.gun_port = -1; maple_state.gun_x = maple_state.gun_y = -1; /* Reset hardware */ maple_write(MAPLE_DMA_PROT, maple_dma_mem_protection()); maple_write(MAPLE_DMA_TSEL, MAPLE_DMA_TSEL_SOFTWARE); maple_write(MAPLE_SPEED, MAPLE_SPEED_2MBPS | MAPLE_SPEED_TIMEOUT(50000)); maple_bus_enable(); /* Hook the necessary interrupts */ maple_state.vbl_handle = vblank_handler_add(maple_vbl_irq_hnd, &maple_state); asic_evt_set_handler(ASIC_EVT_MAPLE_DMA, maple_dma_irq_hnd, &maple_state); asic_evt_enable(ASIC_EVT_MAPLE_DMA, ASIC_IRQ_DEFAULT); } /* Turn off the maple bus, free mem */ /* AGGG!! Someone save me from this idiotic voodoo bug fixing crap.. */ void maple_hw_shutdown(void) { size_t cnt = 0; /* Reset all devices to leave them as we found them */ maple_dev_reset_all(); /* Unhook interrupts */ vblank_handler_remove(maple_state.vbl_handle); asic_evt_remove_handler(ASIC_EVT_MAPLE_DMA); asic_evt_disable(ASIC_EVT_MAPLE_DMA, ASIC_IRQ_DEFAULT); /* Stop any existing maple DMA and shut down the bus */ maple_dma_stop(); while(maple_dma_in_progress()) ; maple_bus_disable(); /* We must cast this back to P1 or cache issues will arise */ if(maple_state.dma_buffer != NULL) { uint32_t ptr = (uint32_t)maple_state.dma_buffer; if(__is_defined(MAPLE_DMA_DEBUG)) ptr -= 512; ptr = (ptr & MEM_AREA_CACHE_MASK) | MEM_AREA_P1_BASE; free((void *)ptr); maple_state.dma_buffer = NULL; } /* Free any attached devices */ for(size_t p = 0; p < MAPLE_PORT_COUNT; p++) { for(size_t u = 0; u < MAPLE_UNIT_COUNT; u++) { cnt += !maple_driver_detach(p, u); free(maple_state.ports[p].units[u]); maple_state.ports[p].units[u] = NULL; } } dbglog(DBG_DEBUG, "maple: final stats -- device count = %d, vbl_cntr = %d, dma_cntr = %d\n", cnt, maple_state.vbl_cntr, maple_state.dma_cntr); } static int maple_scan_done(maple_state_t *state) { return state->scan_ready_mask == 0xf; } KOS_INIT_FLAG_WEAK(mie_init_scan, __is_defined(_arch_sub_naomi)); /* Wait for the initial bus scan to complete */ void maple_wait_scan(void) { /* Wait for it to finish */ thd_poll((thd_cb_t)maple_scan_done, &maple_state, 0); KOS_INIT_FLAG_CALL(mie_init_scan); /* Enumerate everything */ dbglog(DBG_INFO, "maple: attached devices:\n"); for(size_t p = 0; p < MAPLE_PORT_COUNT; p++) { for(size_t u = 0; u < MAPLE_UNIT_COUNT; u++) { maple_device_t *dev = maple_enum_dev(p, u); if(dev) { dbglog(DBG_INFO, " %c%c: %-30.30s (%08lx: %s)\n", 'A' + p, '0' + u, dev->info.product_name, dev->info.functions, maple_pcaps(dev->info.functions)); } } } } KOS_INIT_FLAG_WEAK(cont_init, true); KOS_INIT_FLAG_WEAK(kbd_init, true); KOS_INIT_FLAG_WEAK(mouse_init, true); KOS_INIT_FLAG_WEAK(lightgun_init, true); KOS_INIT_FLAG_WEAK(vmu_init, true); KOS_INIT_FLAG_WEAK(purupuru_init, true); KOS_INIT_FLAG_WEAK(sip_init, true); KOS_INIT_FLAG_WEAK(dreameye_init, true); KOS_INIT_FLAG_WEAK(mie_init, __is_defined(_arch_sub_naomi)); /* Full init: initialize known drivers and start maple operations */ void maple_init(void) { KOS_INIT_FLAG_CALL(lightgun_init); KOS_INIT_FLAG_CALL(cont_init); KOS_INIT_FLAG_CALL(kbd_init); KOS_INIT_FLAG_CALL(mouse_init); KOS_INIT_FLAG_CALL(vmu_init); KOS_INIT_FLAG_CALL(purupuru_init); KOS_INIT_FLAG_CALL(sip_init); KOS_INIT_FLAG_CALL(dreameye_init); KOS_INIT_FLAG_CALL(mie_init); maple_hw_init(); } KOS_INIT_FLAG_WEAK(cont_shutdown, true); KOS_INIT_FLAG_WEAK(kbd_shutdown, true); KOS_INIT_FLAG_WEAK(mouse_shutdown, true); KOS_INIT_FLAG_WEAK(lightgun_shutdown, true); KOS_INIT_FLAG_WEAK(vmu_shutdown, true); KOS_INIT_FLAG_WEAK(purupuru_shutdown, true); KOS_INIT_FLAG_WEAK(sip_shutdown, true); KOS_INIT_FLAG_WEAK(dreameye_shutdown, true); KOS_INIT_FLAG_WEAK(mie_shutdown, __is_defined(_arch_sub_naomi)); /* Full shutdown: shutdown each driver, then all hardware operations. */ void maple_shutdown(void) { KOS_INIT_FLAG_CALL(mie_shutdown); KOS_INIT_FLAG_CALL(dreameye_shutdown); KOS_INIT_FLAG_CALL(sip_shutdown); KOS_INIT_FLAG_CALL(purupuru_shutdown); KOS_INIT_FLAG_CALL(vmu_shutdown); KOS_INIT_FLAG_CALL(mouse_shutdown); KOS_INIT_FLAG_CALL(kbd_shutdown); KOS_INIT_FLAG_CALL(cont_shutdown); KOS_INIT_FLAG_CALL(lightgun_shutdown); maple_hw_shutdown(); }