btn_obio.c revision 1.5
1/*	$NetBSD: btn_obio.c,v 1.5 2023/12/20 13:55:17 thorpej Exp $	*/
2
3/*-
4 * Copyright (C) 2005, 2006 NONAKA Kimihiro <nonaka@netbsd.org>
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28#include <sys/cdefs.h>
29__KERNEL_RCSID(0, "$NetBSD: btn_obio.c,v 1.5 2023/12/20 13:55:17 thorpej Exp $");
30
31#include <sys/types.h>
32#include <sys/param.h>
33#include <sys/systm.h>
34#include <sys/device.h>
35#include <sys/kernel.h>
36#include <sys/conf.h>
37#include <sys/ioctl.h>
38#include <sys/callout.h>
39
40#include <arm/xscale/i80321var.h>
41
42#include <sys/bus.h>
43#include <machine/intr.h>
44
45#include <dev/sysmon/sysmonvar.h>
46#include <dev/sysmon/sysmon_taskq.h>
47
48#include <evbarm/hdl_g/hdlgreg.h>
49#include <evbarm/hdl_g/obiovar.h>
50
51struct btn_obio_softc {
52	device_t		sc_dev;
53	bus_space_tag_t		sc_iot;
54	bus_space_handle_t	sc_ioh;
55	void			*sc_ih;
56
57	struct sysmon_pswitch	sc_smpsw[2];
58
59	int			sc_mask;
60};
61
62static int btn_obio_match(device_t, cfdata_t, void *);
63static void btn_obio_attach(device_t, device_t, void *);
64
65static int btn_intr(void *aux);
66static void btn_sysmon_pressed_event(void *arg);
67
68CFATTACH_DECL_NEW(btn_obio, sizeof(struct btn_obio_softc),
69    btn_obio_match, btn_obio_attach, NULL, NULL);
70
71static int
72btn_obio_match(device_t parent, cfdata_t cf, void *aux)
73{
74
75	/* We take it on faith that the device is there. */
76	return 1;
77}
78
79static void
80btn_obio_attach(device_t parent, device_t self, void *aux)
81{
82	struct obio_attach_args *oba = aux;
83	struct btn_obio_softc *sc = device_private(self);
84	int error;
85
86	sc->sc_dev = self;
87	sc->sc_iot = oba->oba_st;
88	error = bus_space_map(sc->sc_iot, oba->oba_addr, 1, 0, &sc->sc_ioh);
89	if (error) {
90		aprint_error(": failed to map registers: %d\n", error);
91		return;
92	}
93
94	sysmon_power_settype("landisk");
95	sysmon_task_queue_init();
96
97	/* power switch */
98	sc->sc_smpsw[0].smpsw_name = device_xname(self);
99	sc->sc_smpsw[0].smpsw_type = PSWITCH_TYPE_POWER;
100	if (sysmon_pswitch_register(&sc->sc_smpsw[0]) != 0) {
101		aprint_error(": unable to register power button with sysmon\n");
102		return;
103	}
104	sc->sc_mask |= BTNSTAT_POWER;
105	hdlg_enable_pldintr(INTEN_PWRSW);
106
107	/* reset button */
108	sc->sc_smpsw[1].smpsw_name = device_xname(self);
109	sc->sc_smpsw[1].smpsw_type = PSWITCH_TYPE_RESET;
110	if (sysmon_pswitch_register(&sc->sc_smpsw[1]) != 0) {
111		aprint_error(": unable to register reset button with sysmon\n");
112		return;
113	}
114	sc->sc_mask |= BTNSTAT_RESET;
115
116	sc->sc_ih = i80321_intr_establish(oba->oba_irq, IPL_TTY, btn_intr, sc);
117	if (sc->sc_ih == NULL) {
118		aprint_error(": couldn't establish intr handler");
119		return;
120	}
121	hdlg_enable_pldintr(INTEN_BUTTON);
122
123	aprint_normal("\n");
124}
125
126static int
127btn_intr(void *arg)
128{
129	struct btn_obio_softc *sc = (void *)arg;
130	int status;
131	int rv;
132
133	status = (int8_t)bus_space_read_1(sc->sc_iot, sc->sc_ioh, 0);
134	if (status == -1) {
135		return 0;
136	}
137
138	rv = 0;
139	status = ~status;
140	if (status & BTNSTAT_POWER) {
141		if (sc->sc_mask & BTNSTAT_POWER) {
142			hdlg_disable_pldintr(INTEN_PWRSW|INTEN_BUTTON);
143			i80321_intr_disestablish(sc->sc_ih);
144			sc->sc_ih = NULL;
145			sysmon_task_queue_sched(0, btn_sysmon_pressed_event,
146			    &sc->sc_smpsw[0]);
147		} else {
148			aprint_error("%s: power button pressed\n",
149			    device_xname(sc->sc_dev));
150		}
151		rv = 1;
152	} else if (status & BTNSTAT_RESET) {
153		if (sc->sc_mask & BTNSTAT_RESET) {
154			hdlg_disable_pldintr(INTEN_PWRSW|INTEN_BUTTON);
155			i80321_intr_disestablish(sc->sc_ih);
156			sc->sc_ih = NULL;
157			sysmon_task_queue_sched(0, btn_sysmon_pressed_event,
158			    &sc->sc_smpsw[1]);
159		} else {
160			aprint_error("%s: reset button pressed\n",
161			    device_xname(sc->sc_dev));
162		}
163		rv = 1;
164	}
165
166	return rv;
167}
168
169static void
170btn_sysmon_pressed_event(void *arg)
171{
172	struct sysmon_pswitch *smpsw = (void *)arg;
173
174	sysmon_pswitch_event(smpsw, PSWITCH_EVENT_PRESSED);
175}
176