#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: resetbtn.c,v 1.1 2026/01/09 22:54:30 jmcneill Exp $");
#include <sys/param.h>
#include <sys/bus.h>
#include <sys/device.h>
#include <sys/systm.h>
#include <sys/reboot.h>
#include <powerpc/pio.h>
#include <machine/wii.h>
#include <machine/wiiu.h>
#include <dev/sysmon/sysmonvar.h>
#include <dev/sysmon/sysmon_taskq.h>
#include "ahb.h"
#define PI_INTERRUPT_CAUSE 0x0c003000
#define RESET_SWITCH_STATE __BIT(16)
static int resetbtn_match(device_t, cfdata_t, void *);
static void resetbtn_attach(device_t, device_t, void *);
static int resetbtn_intr(void *);
static void resetbtn_task(void *);
static void resetbtn_critpoll(void *);
CFATTACH_DECL_NEW(resetbtn, sizeof(struct sysmon_pswitch),
resetbtn_match, resetbtn_attach, NULL, NULL);
static int
resetbtn_match(device_t parent, cfdata_t cf, void *aux)
{
return !wiiu_plat;
}
static void
resetbtn_attach(device_t parent, device_t self, void *aux)
{
struct ahb_attach_args *aaa = aux;
struct sysmon_pswitch *smpsw = device_private(self);
int error;
KASSERT(device_unit(self) == 0);
aprint_naive("\n");
aprint_normal(": Reset button\n");
critpollhook_establish(resetbtn_critpoll, NULL);
sysmon_task_queue_init();
smpsw->smpsw_name = device_xname(self);
smpsw->smpsw_type = PSWITCH_TYPE_RESET;
error = sysmon_pswitch_register(smpsw);
if (error != 0) {
aprint_error_dev(self,
"unable to register reset button with sysmon: %d\n", error);
smpsw = NULL;
}
ahb_intr_establish(aaa->aaa_irq, IPL_HIGH, resetbtn_intr, smpsw,
device_xname(self));
}
static int
resetbtn_intr(void *arg)
{
struct sysmon_pswitch *smpsw = arg;
sysmon_task_queue_sched(0, resetbtn_task, smpsw);
return 1;
}
static void
resetbtn_task(void *arg)
{
struct sysmon_pswitch *smpsw = arg;
bool pressed;
pressed = (in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0;
if (smpsw != NULL) {
sysmon_pswitch_event(smpsw,
pressed ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
} else if (!pressed) {
kern_reboot(0, NULL);
}
}
static void
resetbtn_critpoll(void *arg)
{
if ((in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0) {
out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB);
}
}