1*a6ce3504Sthorpej /* $NetBSD: pwrsw_obio.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $ */
21fd097bfSuwe
31fd097bfSuwe /*-
42388feefSnonaka * Copyright (C) 2005 NONAKA Kimihiro <nonaka@netbsd.org>
51fd097bfSuwe * All rights reserved.
61fd097bfSuwe *
71fd097bfSuwe * Redistribution and use in source and binary forms, with or without
81fd097bfSuwe * modification, are permitted provided that the following conditions
91fd097bfSuwe * are met:
101fd097bfSuwe * 1. Redistributions of source code must retain the above copyright
111fd097bfSuwe * notice, this list of conditions and the following disclaimer.
121fd097bfSuwe * 2. Redistributions in binary form must reproduce the above copyright
131fd097bfSuwe * notice, this list of conditions and the following disclaimer in the
141fd097bfSuwe * documentation and/or other materials provided with the distribution.
151fd097bfSuwe *
162388feefSnonaka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
172388feefSnonaka * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
182388feefSnonaka * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
192388feefSnonaka * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
202388feefSnonaka * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
212388feefSnonaka * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
222388feefSnonaka * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
232388feefSnonaka * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
242388feefSnonaka * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
252388feefSnonaka * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
261fd097bfSuwe */
271fd097bfSuwe
281fd097bfSuwe #include "btn_obio.h"
291fd097bfSuwe
301fd097bfSuwe #include <sys/cdefs.h>
31*a6ce3504Sthorpej __KERNEL_RCSID(0, "$NetBSD: pwrsw_obio.c,v 1.4 2023/12/20 15:00:08 thorpej Exp $");
321fd097bfSuwe
331fd097bfSuwe #include <sys/types.h>
341fd097bfSuwe #include <sys/param.h>
351fd097bfSuwe #include <sys/systm.h>
361fd097bfSuwe #include <sys/device.h>
371fd097bfSuwe #include <sys/conf.h>
381fd097bfSuwe #include <sys/ioctl.h>
391fd097bfSuwe
401fd097bfSuwe #include <dev/sysmon/sysmonvar.h>
411fd097bfSuwe #include <dev/sysmon/sysmon_taskq.h>
421fd097bfSuwe
431fd097bfSuwe #include <sh3/devreg.h>
441fd097bfSuwe
451fd097bfSuwe #include <landisk/landisk/landiskreg.h>
461fd097bfSuwe #include <landisk/dev/obiovar.h>
471fd097bfSuwe
481fd097bfSuwe struct pwrsw_obio_softc {
49e65c61e2Suwe device_t sc_dev;
501fd097bfSuwe void *sc_ih;
511fd097bfSuwe
521fd097bfSuwe struct sysmon_pswitch sc_smpsw; /* our sysmon glue */
531fd097bfSuwe
541fd097bfSuwe int sc_flags;
551fd097bfSuwe #define SYSMON_ATTACHED 1
561fd097bfSuwe };
571fd097bfSuwe
58e65c61e2Suwe static int pwrsw_obio_probe(device_t, cfdata_t, void *);
59e65c61e2Suwe static void pwrsw_obio_attach(device_t, device_t, void *);
601fd097bfSuwe
611fd097bfSuwe static int pwrsw_intr(void *aux);
621fd097bfSuwe static void pwrsw_pressed_event(void *arg);
631fd097bfSuwe
64e65c61e2Suwe CFATTACH_DECL_NEW(pwrsw_obio, sizeof(struct pwrsw_obio_softc),
651fd097bfSuwe pwrsw_obio_probe, pwrsw_obio_attach, NULL, NULL);
661fd097bfSuwe
671fd097bfSuwe static struct pwrsw_obio_softc *pwrsw_softc;
681fd097bfSuwe
691fd097bfSuwe static int
pwrsw_obio_probe(device_t parent,cfdata_t cfp,void * aux)70e65c61e2Suwe pwrsw_obio_probe(device_t parent, cfdata_t cfp, void *aux)
711fd097bfSuwe {
721fd097bfSuwe struct obio_attach_args *oa = aux;
731fd097bfSuwe
741fd097bfSuwe if (pwrsw_softc)
751fd097bfSuwe return (0);
761fd097bfSuwe
771fd097bfSuwe oa->oa_nio = 0;
781fd097bfSuwe oa->oa_niomem = 0;
791fd097bfSuwe oa->oa_nirq = 1;
801fd097bfSuwe oa->oa_irq[0].or_irq = LANDISK_INTR_PWRSW;
811fd097bfSuwe
821fd097bfSuwe return (1);
831fd097bfSuwe }
841fd097bfSuwe
851fd097bfSuwe static void
pwrsw_obio_attach(device_t parent,device_t self,void * aux)86e65c61e2Suwe pwrsw_obio_attach(device_t parent, device_t self, void *aux)
871fd097bfSuwe {
88e65c61e2Suwe struct pwrsw_obio_softc *sc;
891fd097bfSuwe
90e65c61e2Suwe aprint_naive("\n");
91e65c61e2Suwe aprint_normal(": Power Switch\n");
92e65c61e2Suwe
93e65c61e2Suwe sc = device_private(self);
94e65c61e2Suwe sc->sc_dev = self;
951fd097bfSuwe
961fd097bfSuwe pwrsw_softc = sc;
971fd097bfSuwe
98e65c61e2Suwe sc->sc_smpsw.smpsw_name = device_xname(self);
991fd097bfSuwe sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
1001fd097bfSuwe
1011fd097bfSuwe sc->sc_ih = extintr_establish(LANDISK_INTR_PWRSW, IPL_TTY,
1021fd097bfSuwe pwrsw_intr, sc);
1031fd097bfSuwe if (sc->sc_ih == NULL) {
104e65c61e2Suwe aprint_error_dev(self, "unable to establish interrupt");
1051fd097bfSuwe panic("extintr_establish");
1061fd097bfSuwe }
1071fd097bfSuwe
1081fd097bfSuwe if (sysmon_pswitch_register(&sc->sc_smpsw) != 0) {
109e65c61e2Suwe aprint_error_dev(self, "unable to register with sysmon\n");
1101fd097bfSuwe return;
1111fd097bfSuwe }
1121fd097bfSuwe sc->sc_flags |= SYSMON_ATTACHED;
1131fd097bfSuwe }
1141fd097bfSuwe
1151fd097bfSuwe static int
pwrsw_intr(void * arg)1161fd097bfSuwe pwrsw_intr(void *arg)
1171fd097bfSuwe {
118e65c61e2Suwe struct pwrsw_obio_softc *sc = arg;
1191fd097bfSuwe int status;
1201fd097bfSuwe
1211fd097bfSuwe status = (int8_t)_reg_read_1(LANDISK_BTNSTAT);
1221fd097bfSuwe if (status == -1) {
1231fd097bfSuwe return (0);
1241fd097bfSuwe }
1251fd097bfSuwe
1261fd097bfSuwe status = ~status;
1271fd097bfSuwe if (status & BTN_POWER_BIT) {
1281fd097bfSuwe if (sc->sc_flags & SYSMON_ATTACHED) {
1291fd097bfSuwe sysmon_task_queue_sched(0, pwrsw_pressed_event, sc);
1301fd097bfSuwe extintr_disable(sc->sc_ih);
1311fd097bfSuwe #if NBTN_OBIO > 0
1321fd097bfSuwe extintr_disable_by_num(LANDISK_INTR_BTN);
1331fd097bfSuwe #endif
1341fd097bfSuwe } else {
135e65c61e2Suwe aprint_normal_dev(sc->sc_dev, "pressed\n");
1361fd097bfSuwe }
1371fd097bfSuwe _reg_write_1(LANDISK_PWRSW_INTCLR, 1);
1381fd097bfSuwe return (1);
1391fd097bfSuwe }
1401fd097bfSuwe return (0);
1411fd097bfSuwe }
1421fd097bfSuwe
1431fd097bfSuwe static void
pwrsw_pressed_event(void * arg)1441fd097bfSuwe pwrsw_pressed_event(void *arg)
1451fd097bfSuwe {
146e65c61e2Suwe struct pwrsw_obio_softc *sc = arg;
1471fd097bfSuwe
1481fd097bfSuwe sysmon_pswitch_event(&sc->sc_smpsw, PSWITCH_EVENT_PRESSED);
1491fd097bfSuwe }
150