powsw.c revision 1.3 1 1.3 isaki /* $NetBSD: powsw.c,v 1.3 2022/07/16 04:49:07 isaki Exp $ */
2 1.1 isaki
3 1.1 isaki /*
4 1.1 isaki * Copyright (c) 2011 Tetsuya Isaki. All rights reserved.
5 1.1 isaki *
6 1.1 isaki * Redistribution and use in source and binary forms, with or without
7 1.1 isaki * modification, are permitted provided that the following conditions
8 1.1 isaki * are met:
9 1.1 isaki * 1. Redistributions of source code must retain the above copyright
10 1.1 isaki * notice, this list of conditions and the following disclaimer.
11 1.1 isaki * 2. Redistributions in binary form must reproduce the above copyright
12 1.1 isaki * notice, this list of conditions and the following disclaimer in the
13 1.1 isaki * documentation and/or other materials provided with the distribution.
14 1.1 isaki *
15 1.1 isaki * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16 1.1 isaki * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17 1.1 isaki * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18 1.1 isaki * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19 1.1 isaki * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20 1.1 isaki * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 1.1 isaki * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22 1.1 isaki * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23 1.1 isaki * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.1 isaki * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.1 isaki * SUCH DAMAGE.
26 1.1 isaki */
27 1.1 isaki
28 1.1 isaki /*
29 1.1 isaki * Power switch monitor
30 1.1 isaki */
31 1.1 isaki
32 1.1 isaki #include <sys/cdefs.h>
33 1.3 isaki __KERNEL_RCSID(0, "$NetBSD: powsw.c,v 1.3 2022/07/16 04:49:07 isaki Exp $");
34 1.1 isaki
35 1.1 isaki #include <sys/param.h>
36 1.1 isaki #include <sys/systm.h>
37 1.1 isaki #include <sys/conf.h>
38 1.1 isaki #include <sys/device.h>
39 1.1 isaki #include <sys/intr.h>
40 1.1 isaki #include <sys/callout.h>
41 1.1 isaki
42 1.1 isaki #include <machine/bus.h>
43 1.1 isaki #include <machine/cpu.h>
44 1.1 isaki
45 1.1 isaki #include <arch/x68k/dev/intiovar.h>
46 1.1 isaki #include <arch/x68k/dev/mfp.h>
47 1.1 isaki
48 1.1 isaki #include <dev/sysmon/sysmonvar.h>
49 1.1 isaki #include <dev/sysmon/sysmon_taskq.h>
50 1.1 isaki
51 1.2 tsutsui #include "ioconf.h"
52 1.2 tsutsui
53 1.1 isaki extern int power_switch_is_off; /* XXX should be in .h */
54 1.1 isaki
55 1.1 isaki //#define POWSW_DEBUG
56 1.1 isaki
57 1.1 isaki #if defined(POWSW_DEBUG)
58 1.3 isaki #define DPRINTF(fmt...) printf(fmt)
59 1.1 isaki #define DEBUG_LOG_ADD(c) sc->sc_log[sc->sc_loglen++] = (c)
60 1.1 isaki #define DEBUG_LOG_PRINT() do { \
61 1.1 isaki sc->sc_log[sc->sc_loglen] = '\0'; \
62 1.1 isaki printf("%s", sc->sc_log); \
63 1.1 isaki } while (0)
64 1.1 isaki #else
65 1.1 isaki #define DPRINTF(fmt...)
66 1.1 isaki #define DEBUG_LOG_ADD(c)
67 1.1 isaki #define DEBUG_LOG_PRINT()
68 1.1 isaki #endif
69 1.1 isaki
70 1.1 isaki /* mask */
71 1.1 isaki #define POWSW_ALARM (0x01)
72 1.3 isaki #define POWSW_EXTERNAL (0x02)
73 1.1 isaki #define POWSW_FRONT (0x04)
74 1.1 isaki
75 1.1 isaki /* parameter */
76 1.3 isaki #define POWSW_MAX_TICK (30)
77 1.3 isaki #define POWSW_THRESHOLD (10)
78 1.1 isaki
79 1.1 isaki struct powsw_softc {
80 1.1 isaki device_t sc_dev;
81 1.1 isaki struct sysmon_pswitch sc_smpsw;
82 1.1 isaki callout_t sc_callout;
83 1.1 isaki int sc_mask;
84 1.1 isaki int sc_prev;
85 1.1 isaki int sc_last_sw;
86 1.1 isaki int sc_tick;
87 1.1 isaki int sc_count;
88 1.1 isaki #if defined(POWSW_DEBUG)
89 1.1 isaki char sc_log[100];
90 1.1 isaki int sc_loglen;
91 1.1 isaki #endif
92 1.1 isaki };
93 1.1 isaki
94 1.1 isaki static int powsw_match(device_t, cfdata_t, void *);
95 1.1 isaki static void powsw_attach(device_t, device_t, void *);
96 1.1 isaki static int powsw_intr(void *);
97 1.1 isaki static void powsw_softintr(void *);
98 1.1 isaki static void powsw_pswitch_event(void *);
99 1.1 isaki static void powsw_shutdown_check(void *);
100 1.1 isaki static void powsw_reset_counter(struct powsw_softc *);
101 1.1 isaki static void powsw_set_aer(struct powsw_softc *, int);
102 1.1 isaki
103 1.1 isaki CFATTACH_DECL_NEW(powsw, sizeof(struct powsw_softc),
104 1.1 isaki powsw_match, powsw_attach, NULL, NULL);
105 1.1 isaki
106 1.1 isaki
107 1.1 isaki typedef const struct {
108 1.1 isaki int vector; /* interrupt vector */
109 1.1 isaki int mask; /* mask bit for MFP GPIP */
110 1.1 isaki const char *name;
111 1.1 isaki } powsw_desc_t;
112 1.1 isaki
113 1.1 isaki static powsw_desc_t powsw_desc[2] = {
114 1.3 isaki { 66, POWSW_FRONT, "Front Switch", },
115 1.1 isaki { 65, POWSW_EXTERNAL, "External Power Switch", },
116 1.1 isaki /* XXX I'm not sure about alarm bit */
117 1.1 isaki };
118 1.1 isaki
119 1.1 isaki
120 1.1 isaki static int
121 1.1 isaki powsw_match(device_t parent, cfdata_t cf, void *aux)
122 1.1 isaki {
123 1.3 isaki
124 1.1 isaki return 1;
125 1.1 isaki }
126 1.1 isaki
127 1.1 isaki static void
128 1.1 isaki powsw_attach(device_t parent, device_t self, void *aux)
129 1.1 isaki {
130 1.1 isaki struct powsw_softc *sc = device_private(self);
131 1.1 isaki powsw_desc_t *desc;
132 1.1 isaki const char *xname;
133 1.1 isaki int unit;
134 1.1 isaki int sw;
135 1.1 isaki
136 1.1 isaki unit = device_unit(self);
137 1.1 isaki xname = device_xname(self);
138 1.1 isaki desc = &powsw_desc[unit];
139 1.1 isaki
140 1.1 isaki memset(sc, 0, sizeof(*sc));
141 1.1 isaki sc->sc_dev = self;
142 1.1 isaki sc->sc_mask = desc->mask;
143 1.1 isaki sc->sc_prev = -1;
144 1.1 isaki powsw_reset_counter(sc);
145 1.1 isaki
146 1.1 isaki sysmon_task_queue_init();
147 1.1 isaki sc->sc_smpsw.smpsw_name = xname;
148 1.1 isaki sc->sc_smpsw.smpsw_type = PSWITCH_TYPE_POWER;
149 1.1 isaki if (sysmon_pswitch_register(&sc->sc_smpsw) != 0)
150 1.1 isaki panic("can't register with sysmon");
151 1.1 isaki
152 1.1 isaki callout_init(&sc->sc_callout, 0);
153 1.1 isaki callout_setfunc(&sc->sc_callout, powsw_softintr, sc);
154 1.1 isaki
155 1.1 isaki if (shutdownhook_establish(powsw_shutdown_check, sc) == NULL)
156 1.1 isaki panic("%s: can't establish shutdown hook", xname);
157 1.1 isaki
158 1.1 isaki if (intio_intr_establish(desc->vector, xname, powsw_intr, sc) < 0)
159 1.1 isaki panic("%s: can't establish interrupt", xname);
160 1.1 isaki
161 1.1 isaki /* Set AER and enable interrupt */
162 1.1 isaki sw = (mfp_get_gpip() & sc->sc_mask);
163 1.1 isaki powsw_set_aer(sc, sw ? 0 : 1);
164 1.1 isaki mfp_bit_set_ierb(sc->sc_mask);
165 1.1 isaki
166 1.1 isaki aprint_normal(": %s\n", desc->name);
167 1.1 isaki }
168 1.1 isaki
169 1.1 isaki static int
170 1.1 isaki powsw_intr(void *arg)
171 1.1 isaki {
172 1.1 isaki struct powsw_softc *sc = arg;
173 1.1 isaki
174 1.1 isaki if (sc->sc_tick == 0) {
175 1.1 isaki mfp_bit_clear_ierb(sc->sc_mask);
176 1.1 isaki sc->sc_tick++;
177 1.1 isaki DEBUG_LOG_ADD('i');
178 1.1 isaki /*
179 1.1 isaki * The button state seems unstable for few ticks,
180 1.1 isaki * so wait a bit to settle.
181 1.1 isaki */
182 1.1 isaki callout_schedule(&sc->sc_callout, 1);
183 1.1 isaki } else {
184 1.1 isaki DEBUG_LOG_ADD('x');
185 1.1 isaki }
186 1.1 isaki return 0;
187 1.1 isaki }
188 1.1 isaki
189 1.1 isaki void
190 1.1 isaki powsw_softintr(void *arg)
191 1.1 isaki {
192 1.1 isaki struct powsw_softc *sc = arg;
193 1.1 isaki int sw;
194 1.1 isaki int s;
195 1.1 isaki
196 1.1 isaki s = spl6();
197 1.1 isaki
198 1.1 isaki if (sc->sc_tick++ >= POWSW_MAX_TICK) {
199 1.1 isaki /* tick is over, broken switch? */
200 1.1 isaki printf("%s: unstable power switch?, ignored\n",
201 1.1 isaki device_xname(sc->sc_dev));
202 1.1 isaki powsw_reset_counter(sc);
203 1.1 isaki
204 1.1 isaki mfp_bit_set_ierb(sc->sc_mask);
205 1.1 isaki splx(s);
206 1.1 isaki return;
207 1.1 isaki }
208 1.1 isaki
209 1.1 isaki sw = (mfp_get_gpip() & sc->sc_mask) ? 1 : 0;
210 1.1 isaki DEBUG_LOG_ADD('0' + sw);
211 1.1 isaki
212 1.1 isaki if (sw == sc->sc_last_sw) {
213 1.1 isaki sc->sc_count++;
214 1.1 isaki } else {
215 1.1 isaki sc->sc_last_sw = sw;
216 1.1 isaki sc->sc_count = 1;
217 1.1 isaki }
218 1.1 isaki
219 1.1 isaki if (sc->sc_count < POWSW_THRESHOLD) {
220 1.1 isaki callout_schedule(&sc->sc_callout, 1);
221 1.1 isaki } else {
222 1.1 isaki /* switch seems stable */
223 1.1 isaki DEBUG_LOG_PRINT();
224 1.1 isaki
225 1.1 isaki if (sc->sc_last_sw == sc->sc_prev) {
226 1.1 isaki /* switch state is not changed, it was a noise */
227 1.3 isaki DPRINTF(" ignore(sw=%d,prev=%d)\n",
228 1.3 isaki sc->sc_last_sw, sc->sc_prev);
229 1.1 isaki } else {
230 1.1 isaki /* switch state has been changed */
231 1.1 isaki sc->sc_prev = sc->sc_last_sw;
232 1.1 isaki powsw_set_aer(sc, 1 - sc->sc_prev);
233 1.1 isaki sysmon_task_queue_sched(0, powsw_pswitch_event, sc);
234 1.1 isaki }
235 1.1 isaki powsw_reset_counter(sc);
236 1.3 isaki /* enable interrupt */
237 1.3 isaki mfp_bit_set_ierb(sc->sc_mask);
238 1.1 isaki }
239 1.1 isaki
240 1.1 isaki splx(s);
241 1.1 isaki }
242 1.1 isaki
243 1.1 isaki static void
244 1.1 isaki powsw_pswitch_event(void *arg)
245 1.1 isaki {
246 1.1 isaki struct powsw_softc *sc = arg;
247 1.1 isaki int poweroff;
248 1.1 isaki
249 1.1 isaki poweroff = sc->sc_prev;
250 1.1 isaki
251 1.1 isaki DPRINTF(" %s is %s\n", device_xname(sc->sc_dev),
252 1.1 isaki poweroff ? "off(PRESS)" : "on(RELEASE)");
253 1.1 isaki
254 1.1 isaki sysmon_pswitch_event(&sc->sc_smpsw,
255 1.3 isaki poweroff ? PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
256 1.1 isaki }
257 1.1 isaki
258 1.1 isaki static void
259 1.1 isaki powsw_shutdown_check(void *arg)
260 1.1 isaki {
261 1.1 isaki struct powsw_softc *sc = arg;
262 1.1 isaki int poweroff;
263 1.1 isaki
264 1.1 isaki poweroff = sc->sc_prev;
265 1.1 isaki if (poweroff)
266 1.1 isaki power_switch_is_off = 1;
267 1.1 isaki DPRINTF("powsw_shutdown_check %s = %d\n",
268 1.3 isaki device_xname(sc->sc_dev), power_switch_is_off);
269 1.1 isaki }
270 1.1 isaki
271 1.1 isaki static void
272 1.1 isaki powsw_reset_counter(struct powsw_softc *sc)
273 1.1 isaki {
274 1.3 isaki
275 1.1 isaki sc->sc_last_sw = -1;
276 1.1 isaki sc->sc_tick = 0;
277 1.1 isaki sc->sc_count = 0;
278 1.1 isaki #if defined(POWSW_DEBUG)
279 1.1 isaki sc->sc_loglen = 0;
280 1.1 isaki #endif
281 1.1 isaki }
282 1.1 isaki
283 1.1 isaki static void
284 1.1 isaki powsw_set_aer(struct powsw_softc *sc, int aer)
285 1.1 isaki {
286 1.3 isaki
287 1.1 isaki KASSERT(aer == 0 || aer == 1);
288 1.1 isaki
289 1.1 isaki if (aer == 0) {
290 1.1 isaki mfp_bit_clear_aer(sc->sc_mask);
291 1.1 isaki } else {
292 1.1 isaki mfp_bit_set_aer(sc->sc_mask);
293 1.1 isaki }
294 1.1 isaki DPRINTF(" SetAER=%d", aer);
295 1.1 isaki }
296