resetbtn.c revision 1.1
11.1Sjmcneill/* $NetBSD: resetbtn.c,v 1.1 2026/01/09 22:54:30 jmcneill Exp $ */
21.1Sjmcneill
31.1Sjmcneill/*-
41.1Sjmcneill * Copyright (c) 2024 Jared McNeill <jmcneill@invisible.ca>
51.1Sjmcneill * All rights reserved.
61.1Sjmcneill *
71.1Sjmcneill * Redistribution and use in source and binary forms, with or without
81.1Sjmcneill * modification, are permitted provided that the following conditions
91.1Sjmcneill * are met:
101.1Sjmcneill * 1. Redistributions of source code must retain the above copyright
111.1Sjmcneill *    notice, this list of conditions and the following disclaimer.
121.1Sjmcneill * 2. Redistributions in binary form must reproduce the above copyright
131.1Sjmcneill *    notice, this list of conditions and the following disclaimer in the
141.1Sjmcneill *    documentation and/or other materials provided with the distribution.
151.1Sjmcneill *
161.1Sjmcneill * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
171.1Sjmcneill * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
181.1Sjmcneill * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
191.1Sjmcneill * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
201.1Sjmcneill * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211.1Sjmcneill * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
221.1Sjmcneill * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231.1Sjmcneill * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241.1Sjmcneill * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
251.1Sjmcneill * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261.1Sjmcneill * SUCH DAMAGE.
271.1Sjmcneill */
281.1Sjmcneill
291.1Sjmcneill#include <sys/cdefs.h>
301.1Sjmcneill__KERNEL_RCSID(0, "$NetBSD: resetbtn.c,v 1.1 2026/01/09 22:54:30 jmcneill Exp $");
311.1Sjmcneill
321.1Sjmcneill#include <sys/param.h>
331.1Sjmcneill#include <sys/bus.h>
341.1Sjmcneill#include <sys/device.h>
351.1Sjmcneill#include <sys/systm.h>
361.1Sjmcneill#include <sys/reboot.h>
371.1Sjmcneill#include <powerpc/pio.h>
381.1Sjmcneill#include <machine/wii.h>
391.1Sjmcneill#include <machine/wiiu.h>
401.1Sjmcneill#include <dev/sysmon/sysmonvar.h>
411.1Sjmcneill#include <dev/sysmon/sysmon_taskq.h>
421.1Sjmcneill
431.1Sjmcneill#include "ahb.h"
441.1Sjmcneill
451.1Sjmcneill#define	PI_INTERRUPT_CAUSE	0x0c003000
461.1Sjmcneill#define	 RESET_SWITCH_STATE	__BIT(16)
471.1Sjmcneill
481.1Sjmcneillstatic int	resetbtn_match(device_t, cfdata_t, void *);
491.1Sjmcneillstatic void	resetbtn_attach(device_t, device_t, void *);
501.1Sjmcneill
511.1Sjmcneillstatic int	resetbtn_intr(void *);
521.1Sjmcneillstatic void	resetbtn_task(void *);
531.1Sjmcneillstatic void	resetbtn_critpoll(void *);
541.1Sjmcneill
551.1SjmcneillCFATTACH_DECL_NEW(resetbtn, sizeof(struct sysmon_pswitch),
561.1Sjmcneill	resetbtn_match, resetbtn_attach, NULL, NULL);
571.1Sjmcneill
581.1Sjmcneillstatic int
591.1Sjmcneillresetbtn_match(device_t parent, cfdata_t cf, void *aux)
601.1Sjmcneill{
611.1Sjmcneill	return !wiiu_plat;
621.1Sjmcneill}
631.1Sjmcneill
641.1Sjmcneillstatic void
651.1Sjmcneillresetbtn_attach(device_t parent, device_t self, void *aux)
661.1Sjmcneill{
671.1Sjmcneill	struct ahb_attach_args *aaa = aux;
681.1Sjmcneill	struct sysmon_pswitch *smpsw = device_private(self);
691.1Sjmcneill	int error;
701.1Sjmcneill
711.1Sjmcneill	KASSERT(device_unit(self) == 0);
721.1Sjmcneill
731.1Sjmcneill	aprint_naive("\n");
741.1Sjmcneill	aprint_normal(": Reset button\n");
751.1Sjmcneill
761.1Sjmcneill	critpollhook_establish(resetbtn_critpoll, NULL);
771.1Sjmcneill
781.1Sjmcneill	sysmon_task_queue_init();
791.1Sjmcneill
801.1Sjmcneill	smpsw->smpsw_name = device_xname(self);
811.1Sjmcneill	smpsw->smpsw_type = PSWITCH_TYPE_RESET;
821.1Sjmcneill	error = sysmon_pswitch_register(smpsw);
831.1Sjmcneill	if (error != 0) {
841.1Sjmcneill		aprint_error_dev(self,
851.1Sjmcneill		    "unable to register reset button with sysmon: %d\n", error);
861.1Sjmcneill		smpsw = NULL;
871.1Sjmcneill	}
881.1Sjmcneill
891.1Sjmcneill	ahb_intr_establish(aaa->aaa_irq, IPL_HIGH, resetbtn_intr, smpsw,
901.1Sjmcneill	    device_xname(self));
911.1Sjmcneill}
921.1Sjmcneill
931.1Sjmcneillstatic int
941.1Sjmcneillresetbtn_intr(void *arg)
951.1Sjmcneill{
961.1Sjmcneill	struct sysmon_pswitch *smpsw = arg;
971.1Sjmcneill
981.1Sjmcneill	sysmon_task_queue_sched(0, resetbtn_task, smpsw);
991.1Sjmcneill
1001.1Sjmcneill	return 1;
1011.1Sjmcneill}
1021.1Sjmcneill
1031.1Sjmcneillstatic void
1041.1Sjmcneillresetbtn_task(void *arg)
1051.1Sjmcneill{
1061.1Sjmcneill	struct sysmon_pswitch *smpsw = arg;
1071.1Sjmcneill	bool pressed;
1081.1Sjmcneill
1091.1Sjmcneill	pressed = (in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0;
1101.1Sjmcneill
1111.1Sjmcneill	if (smpsw != NULL) {
1121.1Sjmcneill		sysmon_pswitch_event(smpsw,
1131.1Sjmcneill		    pressed ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
1141.1Sjmcneill	} else if (!pressed) {
1151.1Sjmcneill		kern_reboot(0, NULL);
1161.1Sjmcneill	}
1171.1Sjmcneill}
1181.1Sjmcneill
1191.1Sjmcneillstatic void
1201.1Sjmcneillresetbtn_critpoll(void *arg)
1211.1Sjmcneill{
1221.1Sjmcneill	if ((in32(PI_INTERRUPT_CAUSE) & RESET_SWITCH_STATE) == 0) {
1231.1Sjmcneill		out32(HW_RESETS, in32(HW_RESETS) & ~RSTBINB);
1241.1Sjmcneill	}
1251.1Sjmcneill}
126