xref: /freebsd/sys/powerpc/powermac/pmu.c (revision d0b2dbfa)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (c) 2006 Michael Lorenz
5  * Copyright 2008 by Nathan Whitehorn
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
22  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  */
30 
31 #include <sys/cdefs.h>
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
35 #include <sys/bus.h>
36 #include <sys/conf.h>
37 #include <sys/eventhandler.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/lock.h>
41 #include <sys/mutex.h>
42 #include <sys/clock.h>
43 #include <sys/proc.h>
44 #include <sys/reboot.h>
45 #include <sys/sysctl.h>
46 
47 #include <dev/ofw/ofw_bus.h>
48 #include <dev/ofw/openfirm.h>
49 #include <dev/led/led.h>
50 
51 #include <machine/_inttypes.h>
52 #include <machine/bus.h>
53 #include <machine/cpu.h>
54 #include <machine/hid.h>
55 #include <machine/intr_machdep.h>
56 #include <machine/md_var.h>
57 #include <machine/pcb.h>
58 #include <machine/pio.h>
59 #include <machine/resource.h>
60 
61 #include <vm/vm.h>
62 #include <vm/pmap.h>
63 
64 #include <sys/rman.h>
65 
66 #include <dev/adb/adb.h>
67 
68 #include "clock_if.h"
69 #include "pmuvar.h"
70 #include "viareg.h"
71 #include "uninorthvar.h"	/* For unin_chip_sleep()/unin_chip_wake() */
72 
73 #define PMU_DEFAULTS	PMU_INT_TICK | PMU_INT_ADB | \
74 	PMU_INT_PCEJECT | PMU_INT_SNDBRT | \
75 	PMU_INT_BATTERY | PMU_INT_ENVIRONMENT
76 
77 /*
78  * Bus interface
79  */
80 static int	pmu_probe(device_t);
81 static int	pmu_attach(device_t);
82 static int	pmu_detach(device_t);
83 
84 /*
85  * Clock interface
86  */
87 static int	pmu_gettime(device_t dev, struct timespec *ts);
88 static int	pmu_settime(device_t dev, struct timespec *ts);
89 
90 /*
91  * ADB Interface
92  */
93 
94 static u_int	pmu_adb_send(device_t dev, u_char command_byte, int len,
95 		    u_char *data, u_char poll);
96 static u_int	pmu_adb_autopoll(device_t dev, uint16_t mask);
97 static u_int	pmu_poll(device_t dev);
98 
99 /*
100  * Power interface
101  */
102 
103 static void	pmu_shutdown(void *xsc, int howto);
104 static void	pmu_set_sleepled(void *xsc, int onoff);
105 static int	pmu_server_mode(SYSCTL_HANDLER_ARGS);
106 static int	pmu_acline_state(SYSCTL_HANDLER_ARGS);
107 static int	pmu_query_battery(struct pmu_softc *sc, int batt,
108 		    struct pmu_battstate *info);
109 static int	pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
110 static int	pmu_battmon(SYSCTL_HANDLER_ARGS);
111 static void	pmu_battquery_proc(void);
112 static void	pmu_battery_notify(struct pmu_battstate *batt,
113 		    struct pmu_battstate *old);
114 
115 /*
116  * List of battery-related sysctls we might ask for
117  */
118 
119 enum {
120 	PMU_BATSYSCTL_PRESENT	= 1 << 8,
121 	PMU_BATSYSCTL_CHARGING	= 2 << 8,
122 	PMU_BATSYSCTL_CHARGE	= 3 << 8,
123 	PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
124 	PMU_BATSYSCTL_CURRENT	= 5 << 8,
125 	PMU_BATSYSCTL_VOLTAGE	= 6 << 8,
126 	PMU_BATSYSCTL_TIME	= 7 << 8,
127 	PMU_BATSYSCTL_LIFE	= 8 << 8
128 };
129 
130 static device_method_t  pmu_methods[] = {
131 	/* Device interface */
132 	DEVMETHOD(device_probe,		pmu_probe),
133 	DEVMETHOD(device_attach,	pmu_attach),
134         DEVMETHOD(device_detach,        pmu_detach),
135         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
136 
137 	/* ADB bus interface */
138 	DEVMETHOD(adb_hb_send_raw_packet,   pmu_adb_send),
139 	DEVMETHOD(adb_hb_controller_poll,   pmu_poll),
140 	DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
141 
142 	/* Clock interface */
143 	DEVMETHOD(clock_gettime,	pmu_gettime),
144 	DEVMETHOD(clock_settime,	pmu_settime),
145 
146 	DEVMETHOD_END
147 };
148 
149 static driver_t pmu_driver = {
150 	"pmu",
151 	pmu_methods,
152 	sizeof(struct pmu_softc),
153 };
154 
155 EARLY_DRIVER_MODULE(pmu, macio, pmu_driver, 0, 0, BUS_PASS_RESOURCE);
156 DRIVER_MODULE(adb, pmu, adb_driver, 0, 0);
157 
158 static int	pmuextint_probe(device_t);
159 static int	pmuextint_attach(device_t);
160 
161 static device_method_t  pmuextint_methods[] = {
162 	/* Device interface */
163 	DEVMETHOD(device_probe,		pmuextint_probe),
164 	DEVMETHOD(device_attach,	pmuextint_attach),
165 	{0,0}
166 };
167 
168 static driver_t pmuextint_driver = {
169 	"pmuextint",
170 	pmuextint_methods,
171 	0
172 };
173 
174 EARLY_DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, 0, 0,
175     BUS_PASS_RESOURCE);
176 
177 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
178 MODULE_DEPEND(pmu, usb, 1, 1, 1);
179 
180 static void pmu_intr(void *arg);
181 static void pmu_in(struct pmu_softc *sc);
182 static void pmu_out(struct pmu_softc *sc);
183 static void pmu_ack_on(struct pmu_softc *sc);
184 static void pmu_ack_off(struct pmu_softc *sc);
185 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
186 	int rlen, uint8_t *out_msg);
187 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
188 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
189 static int pmu_intr_state(struct pmu_softc *);
190 
191 /* these values shows that number of data returned after 'send' cmd is sent */
192 static signed char pm_send_cmd_type[] = {
193 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
194 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
195 	0x01, 0x01,   -1,   -1,   -1,   -1,   -1,   -1,
196 	0x00, 0x00,   -1,   -1,   -1,   -1,   -1, 0x00,
197 	  -1, 0x00, 0x02, 0x01, 0x01,   -1,   -1,   -1,
198 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
199 	0x04, 0x14,   -1, 0x03,   -1,   -1,   -1,   -1,
200 	0x00, 0x00, 0x02, 0x02,   -1,   -1,   -1,   -1,
201 	0x01, 0x01,   -1,   -1,   -1,   -1,   -1,   -1,
202 	0x00, 0x00,   -1,   -1, 0x01,   -1,   -1,   -1,
203 	0x01, 0x00, 0x02, 0x02,   -1, 0x01, 0x03, 0x01,
204 	0x00, 0x01, 0x00, 0x00, 0x00,   -1,   -1,   -1,
205 	0x02,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
206 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   -1,   -1,
207 	0x01, 0x01, 0x01,   -1,   -1,   -1,   -1,   -1,
208 	0x00, 0x00,   -1,   -1,   -1, 0x05, 0x04, 0x04,
209 	0x04,   -1, 0x00,   -1,   -1,   -1,   -1,   -1,
210 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
211 	0x01, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
212 	0x00, 0x00,   -1,   -1,   -1,   -1,   -1,   -1,
213 	0x02, 0x02, 0x02, 0x04,   -1, 0x00,   -1,   -1,
214 	0x01, 0x01, 0x03, 0x02,   -1,   -1,   -1,   -1,
215 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
216 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
217 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
218 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
219 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
220 	0x01, 0x01,   -1,   -1, 0x00, 0x00,   -1,   -1,
221 	  -1, 0x04, 0x00,   -1,   -1,   -1,   -1,   -1,
222 	0x03,   -1, 0x00,   -1, 0x00,   -1,   -1, 0x00,
223 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
224 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1
225 };
226 
227 /* these values shows that number of data returned after 'receive' cmd is sent */
228 static signed char pm_receive_cmd_type[] = {
229 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
231 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
232 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1, 0x00,
233 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
235 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 	0x05, 0x15,   -1, 0x02,   -1,   -1,   -1,   -1,
237 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
239 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240 	0x02, 0x00, 0x03, 0x03,   -1,   -1,   -1,   -1,
241 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 	0x04, 0x04, 0x03, 0x09,   -1,   -1,   -1,   -1,
243 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244 	  -1,   -1,   -1,   -1,   -1, 0x01, 0x01, 0x01,
245 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 	0x06,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
247 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
249 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
250 	0x02, 0x00, 0x00, 0x00,   -1,   -1,   -1,   -1,
251 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
253 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
254 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
255 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256 	0x02, 0x02,   -1,   -1, 0x02,   -1,   -1,   -1,
257 	0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
258 	  -1,   -1, 0x02,   -1,   -1,   -1,   -1, 0x00,
259 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
260 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
261 };
262 
263 static int pmu_battmon_enabled = 1;
264 static struct proc *pmubattproc;
265 static struct kproc_desc pmu_batt_kp = {
266 	"pmu_batt",
267 	pmu_battquery_proc,
268 	&pmubattproc
269 };
270 
271 /* We only have one of each device, so globals are safe */
272 static device_t pmu = NULL;
273 static device_t pmu_extint = NULL;
274 
275 static int
276 pmuextint_probe(device_t dev)
277 {
278 	const char *type = ofw_bus_get_type(dev);
279 
280 	if (strcmp(type, "extint-gpio1") != 0)
281                 return (ENXIO);
282 
283 	device_set_desc(dev, "Apple PMU99 External Interrupt");
284 	return (0);
285 }
286 
287 static int
288 pmu_probe(device_t dev)
289 {
290 	const char *type = ofw_bus_get_type(dev);
291 
292 	if (strcmp(type, "via-pmu") != 0)
293                 return (ENXIO);
294 
295 	device_set_desc(dev, "Apple PMU99 Controller");
296 	return (0);
297 }
298 
299 static int
300 setup_pmu_intr(device_t dev, device_t extint)
301 {
302 	struct pmu_softc *sc;
303 	sc = device_get_softc(dev);
304 
305 	sc->sc_irqrid = 0;
306 	sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
307            	RF_ACTIVE);
308         if (sc->sc_irq == NULL) {
309                 device_printf(dev, "could not allocate interrupt\n");
310                 return (ENXIO);
311         }
312 
313 	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
314 	    | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
315                 device_printf(dev, "could not setup interrupt\n");
316                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
317                     sc->sc_irq);
318                 return (ENXIO);
319         }
320 
321 	return (0);
322 }
323 
324 static int
325 pmuextint_attach(device_t dev)
326 {
327 	pmu_extint = dev;
328 	if (pmu)
329 		return (setup_pmu_intr(pmu,dev));
330 
331 	return (0);
332 }
333 
334 static int
335 pmu_attach(device_t dev)
336 {
337 	struct pmu_softc *sc;
338 
339 	int i;
340 	uint8_t reg;
341 	uint8_t cmd[2] = {2, 0};
342 	uint8_t resp[16];
343 	phandle_t node,child;
344 	struct sysctl_ctx_list *ctx;
345 	struct sysctl_oid *tree;
346 
347 	sc = device_get_softc(dev);
348 	sc->sc_dev = dev;
349 
350 	sc->sc_memrid = 0;
351 	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
352 		          &sc->sc_memrid, RF_ACTIVE);
353 
354 	mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
355 
356 	if (sc->sc_memr == NULL) {
357 		device_printf(dev, "Could not alloc mem resource!\n");
358 		return (ENXIO);
359 	}
360 
361 	/*
362 	 * Our interrupt is attached to a GPIO pin. Depending on probe order,
363 	 * we may not have found it yet. If we haven't, it will find us, and
364 	 * attach our interrupt then.
365 	 */
366 	pmu = dev;
367 	if (pmu_extint != NULL) {
368 		if (setup_pmu_intr(dev,pmu_extint) != 0)
369 			return (ENXIO);
370 	}
371 
372 	sc->sc_autopoll = 0;
373 	sc->sc_batteries = 0;
374 	sc->adb_bus = NULL;
375 	sc->sc_leddev = NULL;
376 
377 	/* Init PMU */
378 
379 	pmu_write_reg(sc, vBufB, pmu_read_reg(sc, vBufB) | vPB4);
380 	pmu_write_reg(sc, vDirB, (pmu_read_reg(sc, vDirB) | vPB4) & ~vPB3);
381 
382 	reg = PMU_DEFAULTS;
383 	pmu_send(sc, PMU_SET_IMASK, 1, &reg, 16, resp);
384 
385 	pmu_write_reg(sc, vIER, 0x94); /* make sure VIA interrupts are on */
386 
387 	pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
388 	pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp);
389 
390 	/* Initialize child buses (ADB) */
391 	node = ofw_bus_get_node(dev);
392 
393 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
394 		char name[32];
395 
396 		memset(name, 0, sizeof(name));
397 		OF_getprop(child, "name", name, sizeof(name));
398 
399 		if (bootverbose)
400 			device_printf(dev, "PMU child <%s>\n",name);
401 
402 		if (strncmp(name, "adb", 4) == 0) {
403 			sc->adb_bus = device_add_child(dev,"adb",-1);
404 		}
405 
406 		if (strncmp(name, "power-mgt", 9) == 0) {
407 			uint32_t prim_info[9];
408 
409 			if (OF_getprop(child, "prim-info", prim_info,
410 			    sizeof(prim_info)) >= 7)
411 				sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
412 
413 			if (bootverbose && sc->sc_batteries > 0)
414 				device_printf(dev, "%d batteries detected\n",
415 				    sc->sc_batteries);
416 		}
417 	}
418 
419 	/*
420 	 * Set up sysctls
421 	 */
422 
423 	ctx = device_get_sysctl_ctx(dev);
424 	tree = device_get_sysctl_tree(dev);
425 
426 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
427 	    "server_mode", CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_MPSAFE, sc, 0,
428 	    pmu_server_mode, "I", "Enable reboot after power failure");
429 
430 	if (sc->sc_batteries > 0) {
431 		struct sysctl_oid *oid, *battroot;
432 		char battnum[2];
433 
434 		/* Only start the battery monitor if we have a battery. */
435 		kproc_start(&pmu_batt_kp);
436 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
437 		    "monitor_batteries",
438 		    CTLTYPE_INT | CTLFLAG_RW | CTLFLAG_NEEDGIANT, sc, 0,
439 		    pmu_battmon, "I", "Post battery events to devd");
440 
441 		SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
442 		    "acline", CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
443 		    0, pmu_acline_state, "I", "AC Line Status");
444 
445 		battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
446 		    "batteries", CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
447 		    "Battery Information");
448 
449 		for (i = 0; i < sc->sc_batteries; i++) {
450 			battnum[0] = i + '0';
451 			battnum[1] = '\0';
452 
453 			oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
454 			    OID_AUTO, battnum, CTLFLAG_RD | CTLFLAG_MPSAFE, 0,
455 			    "Battery Information");
456 
457 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
458 			    "present",
459 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
460 			    PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
461 			    "I", "Battery present");
462 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
463 			    "charging",
464 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
465 			    PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
466 			    "I", "Battery charging");
467 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
468 			    "charge",
469 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
470 			    PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
471 			    "I", "Battery charge (mAh)");
472 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
473 			    "maxcharge",
474 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
475 			    PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
476 			    "I", "Maximum battery capacity (mAh)");
477 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
478 			    "rate",
479 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
480 			    PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
481 			    "I", "Battery discharge rate (mA)");
482 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
483 			    "voltage",
484 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
485 			    PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
486 			    "I", "Battery voltage (mV)");
487 
488 			/* Knobs for mental compatibility with ACPI */
489 
490 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
491 			    "time",
492 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
493 			    PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
494 			    "I", "Time Remaining (minutes)");
495 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
496 			    "life",
497 			    CTLTYPE_INT | CTLFLAG_RD | CTLFLAG_MPSAFE, sc,
498 			    PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
499 			    "I", "Capacity remaining (percent)");
500 		}
501 	}
502 
503 	/*
504 	 * Set up LED interface
505 	 */
506 
507 	sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
508 
509 	/*
510 	 * Register RTC
511 	 */
512 
513 	clock_register(dev, 1000);
514 
515 	/*
516 	 * Register power control handler
517 	 */
518 	EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc,
519 	    SHUTDOWN_PRI_LAST);
520 
521 	return (bus_generic_attach(dev));
522 }
523 
524 static int
525 pmu_detach(device_t dev)
526 {
527 	struct pmu_softc *sc;
528 
529 	sc = device_get_softc(dev);
530 
531 	if (sc->sc_leddev != NULL)
532 		led_destroy(sc->sc_leddev);
533 
534 	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
535 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
536 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
537 	mtx_destroy(&sc->sc_mutex);
538 
539 	return (bus_generic_detach(dev));
540 }
541 
542 static uint8_t
543 pmu_read_reg(struct pmu_softc *sc, u_int offset)
544 {
545 	return (bus_read_1(sc->sc_memr, offset));
546 }
547 
548 static void
549 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
550 {
551 	bus_write_1(sc->sc_memr, offset, value);
552 }
553 
554 static int
555 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
556 {
557 
558 	pmu_out(sc);
559 	pmu_write_reg(sc, vSR, data);
560 	pmu_ack_off(sc);
561 	/* wait for intr to come up */
562 	/* XXX should add a timeout and bail if it expires */
563 	do {} while (pmu_intr_state(sc) == 0);
564 	pmu_ack_on(sc);
565 	do {} while (pmu_intr_state(sc));
566 	pmu_ack_on(sc);
567 	return 0;
568 }
569 
570 static inline int
571 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
572 {
573 	pmu_in(sc);
574 	(void)pmu_read_reg(sc, vSR);
575 	pmu_ack_off(sc);
576 	/* wait for intr to come up */
577 	do {} while (pmu_intr_state(sc) == 0);
578 	pmu_ack_on(sc);
579 	do {} while (pmu_intr_state(sc));
580 	*data = pmu_read_reg(sc, vSR);
581 	return 0;
582 }
583 
584 static int
585 pmu_intr_state(struct pmu_softc *sc)
586 {
587 	return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
588 }
589 
590 static int
591 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
592     uint8_t *out_msg)
593 {
594 	struct pmu_softc *sc = cookie;
595 	int i, rcv_len = -1;
596 	uint8_t out_len, intreg;
597 
598 	intreg = pmu_read_reg(sc, vIER);
599 	intreg &= 0x10;
600 	pmu_write_reg(sc, vIER, intreg);
601 
602 	/* wait idle */
603 	do {} while (pmu_intr_state(sc));
604 
605 	/* send command */
606 	pmu_send_byte(sc, cmd);
607 
608 	/* send length if necessary */
609 	if (pm_send_cmd_type[cmd] < 0) {
610 		pmu_send_byte(sc, length);
611 	}
612 
613 	for (i = 0; i < length; i++) {
614 		pmu_send_byte(sc, in_msg[i]);
615 	}
616 
617 	/* see if there's data to read */
618 	rcv_len = pm_receive_cmd_type[cmd];
619 	if (rcv_len == 0)
620 		goto done;
621 
622 	/* read command */
623 	if (rcv_len == 1) {
624 		pmu_read_byte(sc, out_msg);
625 		goto done;
626 	} else
627 		out_msg[0] = cmd;
628 	if (rcv_len < 0) {
629 		pmu_read_byte(sc, &out_len);
630 		rcv_len = out_len + 1;
631 	}
632 	for (i = 1; i < min(rcv_len, rlen); i++)
633 		pmu_read_byte(sc, &out_msg[i]);
634 
635 done:
636 	pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
637 
638 	return rcv_len;
639 }
640 
641 static u_int
642 pmu_poll(device_t dev)
643 {
644 	pmu_intr(dev);
645 	return (0);
646 }
647 
648 static void
649 pmu_in(struct pmu_softc *sc)
650 {
651 	uint8_t reg;
652 
653 	reg = pmu_read_reg(sc, vACR);
654 	reg &= ~vSR_OUT;
655 	reg |= 0x0c;
656 	pmu_write_reg(sc, vACR, reg);
657 }
658 
659 static void
660 pmu_out(struct pmu_softc *sc)
661 {
662 	uint8_t reg;
663 
664 	reg = pmu_read_reg(sc, vACR);
665 	reg |= vSR_OUT;
666 	reg |= 0x0c;
667 	pmu_write_reg(sc, vACR, reg);
668 }
669 
670 static void
671 pmu_ack_off(struct pmu_softc *sc)
672 {
673 	uint8_t reg;
674 
675 	reg = pmu_read_reg(sc, vBufB);
676 	reg &= ~vPB4;
677 	pmu_write_reg(sc, vBufB, reg);
678 }
679 
680 static void
681 pmu_ack_on(struct pmu_softc *sc)
682 {
683 	uint8_t reg;
684 
685 	reg = pmu_read_reg(sc, vBufB);
686 	reg |= vPB4;
687 	pmu_write_reg(sc, vBufB, reg);
688 }
689 
690 static void
691 pmu_intr(void *arg)
692 {
693 	device_t        dev;
694 	struct pmu_softc *sc;
695 
696 	unsigned int len;
697 	uint8_t resp[16];
698 	uint8_t junk[16];
699 
700         dev = (device_t)arg;
701 	sc = device_get_softc(dev);
702 
703 	mtx_lock(&sc->sc_mutex);
704 
705 	pmu_write_reg(sc, vIFR, 0x90);	/* Clear 'em */
706 	len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
707 
708 	mtx_unlock(&sc->sc_mutex);
709 
710 	if ((len < 1) || (resp[1] == 0)) {
711 		return;
712 	}
713 
714 	if (resp[1] & PMU_INT_ADB) {
715 		/*
716 		 * the PMU will turn off autopolling after each command that
717 		 * it did not issue, so we assume any but TALK R0 is ours and
718 		 * re-enable autopoll here whenever we receive an ACK for a
719 		 * non TR0 command.
720 		 */
721 		mtx_lock(&sc->sc_mutex);
722 
723 		if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
724 			if (sc->sc_autopoll) {
725 				uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
726 				    (sc->sc_autopoll >> 8) & 0xff,
727 				    sc->sc_autopoll & 0xff};
728 
729 				pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
730 			}
731 		}
732 
733 		mtx_unlock(&sc->sc_mutex);
734 
735 		adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
736 			len - 3,&resp[3]);
737 	}
738 	if (resp[1] & PMU_INT_ENVIRONMENT) {
739 		/* if the lid was just closed, notify devd. */
740 		if ((resp[2] & PMU_ENV_LID_CLOSED) && (!sc->lid_closed)) {
741 			sc->lid_closed = 1;
742 			devctl_notify("PMU", "lid", "close", NULL);
743 		}
744 		else if (!(resp[2] & PMU_ENV_LID_CLOSED) && (sc->lid_closed)) {
745 			/* if the lid was just opened, notify devd. */
746 			sc->lid_closed = 0;
747 			devctl_notify("PMU", "lid", "open", NULL);
748 		}
749 		if (resp[2] & PMU_ENV_POWER)
750 			devctl_notify("PMU", "Button", "pressed", NULL);
751 	}
752 }
753 
754 static u_int
755 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
756     u_char poll)
757 {
758 	struct pmu_softc *sc = device_get_softc(dev);
759 	int i;
760 	uint8_t packet[16], resp[16];
761 
762 	/* construct an ADB command packet and send it */
763 
764 	packet[0] = command_byte;
765 
766 	packet[1] = 0;
767 	packet[2] = len;
768 	for (i = 0; i < len; i++)
769 		packet[i + 3] = data[i];
770 
771 	mtx_lock(&sc->sc_mutex);
772 	pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
773 	mtx_unlock(&sc->sc_mutex);
774 
775 	if (poll)
776 		pmu_poll(dev);
777 
778 	return 0;
779 }
780 
781 static u_int
782 pmu_adb_autopoll(device_t dev, uint16_t mask)
783 {
784 	struct pmu_softc *sc = device_get_softc(dev);
785 
786 	/* magical incantation to re-enable autopolling */
787 	uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
788 	uint8_t resp[16];
789 
790 	mtx_lock(&sc->sc_mutex);
791 
792 	if (sc->sc_autopoll == mask) {
793 		mtx_unlock(&sc->sc_mutex);
794 		return 0;
795 	}
796 
797 	sc->sc_autopoll = mask & 0xffff;
798 
799 	if (mask)
800 		pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
801 	else
802 		pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
803 
804 	mtx_unlock(&sc->sc_mutex);
805 
806 	return 0;
807 }
808 
809 static void
810 pmu_shutdown(void *xsc, int howto)
811 {
812 	struct pmu_softc *sc = xsc;
813 	uint8_t cmd[] = {'M', 'A', 'T', 'T'};
814 
815 	if (howto & RB_HALT)
816 		pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL);
817 	else
818 		pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL);
819 
820 	for (;;);
821 }
822 
823 static void
824 pmu_set_sleepled(void *xsc, int onoff)
825 {
826 	struct pmu_softc *sc = xsc;
827 	uint8_t cmd[] = {4, 0, 0};
828 
829 	cmd[2] = onoff;
830 
831 	mtx_lock(&sc->sc_mutex);
832 	pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
833 	mtx_unlock(&sc->sc_mutex);
834 }
835 
836 static int
837 pmu_server_mode(SYSCTL_HANDLER_ARGS)
838 {
839 	struct pmu_softc *sc = arg1;
840 
841 	u_int server_mode = 0;
842 	uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
843 	uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
844 	uint8_t resp[3];
845 	int error, len;
846 
847 	mtx_lock(&sc->sc_mutex);
848 	len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
849 	mtx_unlock(&sc->sc_mutex);
850 
851 	if (len == 3)
852 		server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
853 
854 	error = sysctl_handle_int(oidp, &server_mode, 0, req);
855 
856 	if (len != 3)
857 		return (EINVAL);
858 
859 	if (error || !req->newptr)
860 		return (error);
861 
862 	if (server_mode == 1)
863 		setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
864 	else if (server_mode == 0)
865 		setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
866 	else
867 		return (EINVAL);
868 
869 	setcmd[1] = resp[1];
870 
871 	mtx_lock(&sc->sc_mutex);
872 	pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
873 	mtx_unlock(&sc->sc_mutex);
874 
875 	return (0);
876 }
877 
878 static int
879 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
880 {
881 	uint8_t reg;
882 	uint8_t resp[16];
883 	int len;
884 
885 	reg = batt + 1;
886 
887 	mtx_lock(&sc->sc_mutex);
888 	len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, &reg, 16, resp);
889 	mtx_unlock(&sc->sc_mutex);
890 
891 	if (len < 3)
892 		return (-1);
893 
894 	/* All PMU battery info replies share a common header:
895 	 * Byte 1	Payload Format
896 	 * Byte 2	Battery Flags
897 	 */
898 
899 	info->state = resp[2];
900 
901 	switch (resp[1]) {
902 	case 3:
903 	case 4:
904 		/*
905 		 * Formats 3 and 4 appear to be the same:
906 		 * Byte 3	Charge
907 		 * Byte 4	Max Charge
908 		 * Byte 5	Current
909 		 * Byte 6	Voltage
910 		 */
911 
912 		info->charge = resp[3];
913 		info->maxcharge = resp[4];
914 		/* Current can be positive or negative */
915 		info->current = (int8_t)resp[5];
916 		info->voltage = resp[6];
917 		break;
918 	case 5:
919 		/*
920 		 * Formats 5 is a wider version of formats 3 and 4
921 		 * Byte 3-4	Charge
922 		 * Byte 5-6	Max Charge
923 		 * Byte 7-8	Current
924 		 * Byte 9-10	Voltage
925 		 */
926 
927 		info->charge = (resp[3] << 8) | resp[4];
928 		info->maxcharge = (resp[5] << 8) | resp[6];
929 		/* Current can be positive or negative */
930 		info->current = (int16_t)((resp[7] << 8) | resp[8]);
931 		info->voltage = (resp[9] << 8) | resp[10];
932 		break;
933 	default:
934 		device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
935 		    resp[1]);
936 		return (-1);
937 	}
938 
939 	return (0);
940 }
941 
942 static void
943 pmu_battery_notify(struct pmu_battstate *batt, struct pmu_battstate *old)
944 {
945 	char notify_buf[16];
946 	int new_acline, old_acline;
947 
948 	new_acline = (batt->state & PMU_PWR_AC_PRESENT) ? 1 : 0;
949 	old_acline = (old->state & PMU_PWR_AC_PRESENT) ? 1 : 0;
950 
951 	if (new_acline != old_acline) {
952 		snprintf(notify_buf, sizeof(notify_buf),
953 		    "notify=0x%02x", new_acline);
954 		devctl_notify("PMU", "POWER", "ACLINE", notify_buf);
955 	}
956 }
957 
958 static void
959 pmu_battquery_proc(void)
960 {
961 	struct pmu_softc *sc;
962 	struct pmu_battstate batt;
963 	struct pmu_battstate cur_batt;
964 	int error;
965 
966 	sc = device_get_softc(pmu);
967 
968 	bzero(&cur_batt, sizeof(cur_batt));
969 	while (1) {
970 		kproc_suspend_check(curproc);
971 		error = pmu_query_battery(sc, 0, &batt);
972 		if (error == 0) {
973 			pmu_battery_notify(&batt, &cur_batt);
974 			cur_batt = batt;
975 		}
976 		pause("pmu_batt", hz);
977 	}
978 }
979 
980 static int
981 pmu_battmon(SYSCTL_HANDLER_ARGS)
982 {
983 	int error, result;
984 
985 	result = pmu_battmon_enabled;
986 
987 	error = sysctl_handle_int(oidp, &result, 0, req);
988 
989 	if (error || !req->newptr)
990 		return (error);
991 
992 	if (!result && pmu_battmon_enabled)
993 		error = kproc_suspend(pmubattproc, hz);
994 	else if (result && pmu_battmon_enabled == 0)
995 		error = kproc_resume(pmubattproc);
996 	pmu_battmon_enabled = (result != 0);
997 
998 	return (error);
999 }
1000 
1001 static int
1002 pmu_acline_state(SYSCTL_HANDLER_ARGS)
1003 {
1004 	struct pmu_softc *sc;
1005 	struct pmu_battstate batt;
1006 	int error, result;
1007 
1008 	sc = arg1;
1009 
1010 	/* The PMU treats the AC line status as a property of the battery */
1011 	error = pmu_query_battery(sc, 0, &batt);
1012 
1013 	if (error != 0)
1014 		return (error);
1015 
1016 	result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
1017 	error = sysctl_handle_int(oidp, &result, 0, req);
1018 
1019 	return (error);
1020 }
1021 
1022 static int
1023 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
1024 {
1025 	struct pmu_softc *sc;
1026 	struct pmu_battstate batt;
1027 	int error, result;
1028 
1029 	sc = arg1;
1030 
1031 	error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
1032 
1033 	if (error != 0)
1034 		return (error);
1035 
1036 	switch (arg2 & 0xff00) {
1037 	case PMU_BATSYSCTL_PRESENT:
1038 		result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
1039 		break;
1040 	case PMU_BATSYSCTL_CHARGING:
1041 		result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
1042 		break;
1043 	case PMU_BATSYSCTL_CHARGE:
1044 		result = batt.charge;
1045 		break;
1046 	case PMU_BATSYSCTL_MAXCHARGE:
1047 		result = batt.maxcharge;
1048 		break;
1049 	case PMU_BATSYSCTL_CURRENT:
1050 		result = batt.current;
1051 		break;
1052 	case PMU_BATSYSCTL_VOLTAGE:
1053 		result = batt.voltage;
1054 		break;
1055 	case PMU_BATSYSCTL_TIME:
1056 		/* Time remaining until full charge/discharge, in minutes */
1057 
1058 		if (batt.current >= 0)
1059 			result = (batt.maxcharge - batt.charge) /* mAh */ * 60
1060 			    / batt.current /* mA */;
1061 		else
1062 			result = (batt.charge /* mAh */ * 60)
1063 			    / (-batt.current /* mA */);
1064 		break;
1065 	case PMU_BATSYSCTL_LIFE:
1066 		/* Battery charge fraction, in percent */
1067 		result = (batt.charge * 100) / batt.maxcharge;
1068 		break;
1069 	default:
1070 		/* This should never happen */
1071 		result = -1;
1072 	}
1073 
1074 	error = sysctl_handle_int(oidp, &result, 0, req);
1075 
1076 	return (error);
1077 }
1078 
1079 #define DIFF19041970	2082844800
1080 
1081 static int
1082 pmu_gettime(device_t dev, struct timespec *ts)
1083 {
1084 	struct pmu_softc *sc = device_get_softc(dev);
1085 	uint8_t resp[16];
1086 	uint32_t sec;
1087 
1088 	mtx_lock(&sc->sc_mutex);
1089 	pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
1090 	mtx_unlock(&sc->sc_mutex);
1091 
1092 	memcpy(&sec, &resp[1], 4);
1093 	ts->tv_sec = sec - DIFF19041970;
1094 	ts->tv_nsec = 0;
1095 
1096 	return (0);
1097 }
1098 
1099 static int
1100 pmu_settime(device_t dev, struct timespec *ts)
1101 {
1102 	struct pmu_softc *sc = device_get_softc(dev);
1103 	uint32_t sec;
1104 
1105 	sec = ts->tv_sec + DIFF19041970;
1106 
1107 	mtx_lock(&sc->sc_mutex);
1108 	pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL);
1109 	mtx_unlock(&sc->sc_mutex);
1110 
1111 	return (0);
1112 }
1113 
1114 int
1115 pmu_set_speed(int low_speed)
1116 {
1117 	struct pmu_softc *sc;
1118 	uint8_t sleepcmd[] = {'W', 'O', 'O', 'F', 0};
1119 	uint8_t resp[16];
1120 
1121 	sc = device_get_softc(pmu);
1122 	pmu_write_reg(sc, vIER, 0x10);
1123 	spinlock_enter();
1124 	mtdec(0x7fffffff);
1125 	mb();
1126 	mtdec(0x7fffffff);
1127 
1128 	sleepcmd[4] = low_speed;
1129 	pmu_send(sc, PMU_CPU_SPEED, 5, sleepcmd, 16, resp);
1130 	unin_chip_sleep(NULL, 1);
1131 	platform_sleep();
1132 	unin_chip_wake(NULL);
1133 
1134 	mtdec(1);	/* Force a decrementer exception */
1135 	spinlock_exit();
1136 	pmu_write_reg(sc, vIER, 0x90);
1137 
1138 	return (0);
1139 }
1140