xref: /freebsd/sys/powerpc/powermac/pmu.c (revision 39beb93c)
1 /*-
2  * Copyright (c) 2006 Michael Lorenz
3  * Copyright 2008 by Nathan Whitehorn
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
20  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
22  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
23  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 
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/kernel.h>
38 #include <sys/sysctl.h>
39 
40 #include <dev/ofw/ofw_bus.h>
41 #include <dev/ofw/openfirm.h>
42 #include <dev/led/led.h>
43 
44 #include <machine/bus.h>
45 #include <machine/intr.h>
46 #include <machine/intr_machdep.h>
47 #include <machine/md_var.h>
48 #include <machine/pio.h>
49 #include <machine/resource.h>
50 
51 #include <vm/vm.h>
52 #include <vm/pmap.h>
53 
54 #include <sys/rman.h>
55 
56 #include <dev/adb/adb.h>
57 
58 #include "pmuvar.h"
59 #include "viareg.h"
60 
61 /*
62  * MacIO interface
63  */
64 static int	pmu_probe(device_t);
65 static int	pmu_attach(device_t);
66 static int	pmu_detach(device_t);
67 
68 static u_int	pmu_adb_send(device_t dev, u_char command_byte, int len,
69 		    u_char *data, u_char poll);
70 static u_int	pmu_adb_autopoll(device_t dev, uint16_t mask);
71 static void	pmu_poll(device_t dev);
72 
73 static void	pmu_set_sleepled(void *xsc, int onoff);
74 static int	pmu_server_mode(SYSCTL_HANDLER_ARGS);
75 static int	pmu_query_battery(struct pmu_softc *sc, int batt,
76 		    struct pmu_battstate *info);
77 static int	pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
78 
79 /*
80  * List of battery-related sysctls we might ask for
81  */
82 
83 enum {
84 	PMU_BATSYSCTL_PRESENT	= 1 << 8,
85 	PMU_BATSYSCTL_CHARGING	= 2 << 8,
86 	PMU_BATSYSCTL_CHARGE	= 3 << 8,
87 	PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
88 	PMU_BATSYSCTL_CURRENT	= 5 << 8,
89 	PMU_BATSYSCTL_VOLTAGE	= 6 << 8,
90 	PMU_BATSYSCTL_TIME	= 7 << 8,
91 	PMU_BATSYSCTL_LIFE	= 8 << 8
92 };
93 
94 static device_method_t  pmu_methods[] = {
95 	/* Device interface */
96 	DEVMETHOD(device_probe,		pmu_probe),
97 	DEVMETHOD(device_attach,	pmu_attach),
98         DEVMETHOD(device_detach,        pmu_detach),
99         DEVMETHOD(device_shutdown,      bus_generic_shutdown),
100         DEVMETHOD(device_suspend,       bus_generic_suspend),
101         DEVMETHOD(device_resume,        bus_generic_resume),
102 
103 	/* bus interface, for ADB root */
104         DEVMETHOD(bus_print_child,      bus_generic_print_child),
105         DEVMETHOD(bus_driver_added,     bus_generic_driver_added),
106 
107 	/* ADB bus interface */
108 	DEVMETHOD(adb_hb_send_raw_packet,   pmu_adb_send),
109 	DEVMETHOD(adb_hb_controller_poll,   pmu_poll),
110 	DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
111 
112 	{ 0, 0 },
113 };
114 
115 static driver_t pmu_driver = {
116 	"pmu",
117 	pmu_methods,
118 	sizeof(struct pmu_softc),
119 };
120 
121 static devclass_t pmu_devclass;
122 
123 DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0);
124 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
125 
126 static int	pmuextint_probe(device_t);
127 static int	pmuextint_attach(device_t);
128 
129 static device_method_t  pmuextint_methods[] = {
130 	/* Device interface */
131 	DEVMETHOD(device_probe,		pmuextint_probe),
132 	DEVMETHOD(device_attach,	pmuextint_attach),
133 
134 	{0,0}
135 };
136 
137 static driver_t pmuextint_driver = {
138 	"pmuextint",
139 	pmuextint_methods,
140 	0
141 };
142 
143 static devclass_t pmuextint_devclass;
144 
145 DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass, 0, 0);
146 
147 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
148 MODULE_DEPEND(pmu, usb, 1, 1, 1);
149 
150 static void pmu_intr(void *arg);
151 static void pmu_in(struct pmu_softc *sc);
152 static void pmu_out(struct pmu_softc *sc);
153 static void pmu_ack_on(struct pmu_softc *sc);
154 static void pmu_ack_off(struct pmu_softc *sc);
155 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
156 	int rlen, uint8_t *out_msg);
157 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
158 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
159 static int pmu_intr_state(struct pmu_softc *);
160 
161 /* these values shows that number of data returned after 'send' cmd is sent */
162 static signed char pm_send_cmd_type[] = {
163 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
164 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
165 	0x01, 0x01,   -1,   -1,   -1,   -1,   -1,   -1,
166 	0x00, 0x00,   -1,   -1,   -1,   -1,   -1, 0x00,
167 	  -1, 0x00, 0x02, 0x01, 0x01,   -1,   -1,   -1,
168 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
169 	0x04, 0x14,   -1, 0x03,   -1,   -1,   -1,   -1,
170 	0x00, 0x00, 0x02, 0x02,   -1,   -1,   -1,   -1,
171 	0x01, 0x01,   -1,   -1,   -1,   -1,   -1,   -1,
172 	0x00, 0x00,   -1,   -1, 0x01,   -1,   -1,   -1,
173 	0x01, 0x00, 0x02, 0x02,   -1, 0x01, 0x03, 0x01,
174 	0x00, 0x01, 0x00, 0x00, 0x00,   -1,   -1,   -1,
175 	0x02,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
176 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   -1,   -1,
177 	0x01, 0x01, 0x01,   -1,   -1,   -1,   -1,   -1,
178 	0x00, 0x00,   -1,   -1,   -1,   -1, 0x04, 0x04,
179 	0x04,   -1, 0x00,   -1,   -1,   -1,   -1,   -1,
180 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
181 	0x01, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
182 	0x00, 0x00,   -1,   -1,   -1,   -1,   -1,   -1,
183 	0x02, 0x02, 0x02, 0x04,   -1, 0x00,   -1,   -1,
184 	0x01, 0x01, 0x03, 0x02,   -1,   -1,   -1,   -1,
185 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
186 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
187 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
188 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
189 	0x00,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
190 	0x01, 0x01,   -1,   -1, 0x00, 0x00,   -1,   -1,
191 	  -1, 0x04, 0x00,   -1,   -1,   -1,   -1,   -1,
192 	0x03,   -1, 0x00,   -1, 0x00,   -1,   -1, 0x00,
193 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
194 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1
195 };
196 
197 /* these values shows that number of data returned after 'receive' cmd is sent */
198 static signed char pm_receive_cmd_type[] = {
199 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
200 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
201 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
202 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1, 0x00,
203 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
204 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
205 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
206 	0x05, 0x15,   -1, 0x02,   -1,   -1,   -1,   -1,
207 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
208 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
209 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
210 	0x02, 0x00, 0x03, 0x03,   -1,   -1,   -1,   -1,
211 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
212 	0x04, 0x04, 0x03, 0x09,   -1,   -1,   -1,   -1,
213 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
214 	  -1,   -1,   -1,   -1,   -1,   -1, 0x01, 0x01,
215 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
216 	0x06,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
217 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
218 	0x02, 0x02,   -1,   -1,   -1,   -1,   -1,   -1,
219 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
220 	0x02, 0x00, 0x00, 0x00,   -1,   -1,   -1,   -1,
221 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
222 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
223 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
224 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
225 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 	0x02, 0x02,   -1,   -1, 0x02,   -1,   -1,   -1,
227 	0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
228 	  -1,   -1, 0x02,   -1,   -1,   -1,   -1, 0x00,
229 	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
230 	  -1,   -1,   -1,   -1,   -1,   -1,   -1,   -1,
231 };
232 
233 /* We only have one of each device, so globals are safe */
234 static device_t pmu = NULL;
235 static device_t pmu_extint = NULL;
236 
237 static int
238 pmuextint_probe(device_t dev)
239 {
240 	const char *type = ofw_bus_get_type(dev);
241 
242 	if (strcmp(type, "extint-gpio1") != 0)
243                 return (ENXIO);
244 
245 	device_set_desc(dev, "Apple PMU99 External Interrupt");
246 	return (0);
247 }
248 
249 static int
250 pmu_probe(device_t dev)
251 {
252 	const char *type = ofw_bus_get_type(dev);
253 
254 	if (strcmp(type, "via-pmu") != 0)
255                 return (ENXIO);
256 
257 	device_set_desc(dev, "Apple PMU99 Controller");
258 	return (0);
259 }
260 
261 
262 static int
263 setup_pmu_intr(device_t dev, device_t extint)
264 {
265 	struct pmu_softc *sc;
266 	sc = device_get_softc(dev);
267 
268 	sc->sc_irqrid = 0;
269 	sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
270            	RF_ACTIVE);
271         if (sc->sc_irq == NULL) {
272                 device_printf(dev, "could not allocate interrupt\n");
273                 return (ENXIO);
274         }
275 
276 	if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
277 	    | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
278                 device_printf(dev, "could not setup interrupt\n");
279                 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
280                     sc->sc_irq);
281                 return (ENXIO);
282         }
283 
284 	return (0);
285 }
286 
287 static int
288 pmuextint_attach(device_t dev)
289 {
290 	pmu_extint = dev;
291 	if (pmu)
292 		return (setup_pmu_intr(pmu,dev));
293 
294 	return (0);
295 }
296 
297 static int
298 pmu_attach(device_t dev)
299 {
300 	struct pmu_softc *sc;
301 
302 	int i;
303 	uint8_t reg;
304 	uint8_t cmd[2] = {2, 0};
305 	uint8_t resp[16];
306 	phandle_t node,child;
307 	struct sysctl_ctx_list *ctx;
308 	struct sysctl_oid *tree;
309 
310 	sc = device_get_softc(dev);
311 	sc->sc_dev = dev;
312 
313 	sc->sc_memrid = 0;
314 	sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
315 		          &sc->sc_memrid, RF_ACTIVE);
316 
317 	mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
318 
319 	if (sc->sc_memr == NULL) {
320 		device_printf(dev, "Could not alloc mem resource!\n");
321 		return (ENXIO);
322 	}
323 
324 	/*
325 	 * Our interrupt is attached to a GPIO pin. Depending on probe order,
326 	 * we may not have found it yet. If we haven't, it will find us, and
327 	 * attach our interrupt then.
328 	 */
329 	pmu = dev;
330 	if (pmu_extint != NULL) {
331 		if (setup_pmu_intr(dev,pmu_extint) != 0)
332 			return (ENXIO);
333 	}
334 
335 	sc->sc_autopoll = 0;
336 	sc->sc_batteries = 0;
337 	sc->adb_bus = NULL;
338 	sc->sc_leddev = NULL;
339 
340 	/* Init PMU */
341 
342 	reg = PMU_INT_TICK | PMU_INT_ADB | PMU_INT_PCEJECT | PMU_INT_SNDBRT;
343 	reg |= PMU_INT_BATTERY;
344 	reg |= PMU_INT_ENVIRONMENT;
345 	pmu_send(sc, PMU_SET_IMASK, 1, &reg, 16, resp);
346 
347 	pmu_write_reg(sc, vIER, 0x90); /* make sure VIA interrupts are on */
348 
349 	pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
350 	pmu_send(sc, PMU_GET_VERSION, 1, cmd, 16, resp);
351 
352 	/* Initialize child buses (ADB) */
353 	node = ofw_bus_get_node(dev);
354 
355 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
356 		char name[32];
357 
358 		memset(name, 0, sizeof(name));
359 		OF_getprop(child, "name", name, sizeof(name));
360 
361 		if (bootverbose)
362 			device_printf(dev, "PMU child <%s>\n",name);
363 
364 		if (strncmp(name, "adb", 4) == 0) {
365 			sc->adb_bus = device_add_child(dev,"adb",-1);
366 		}
367 
368 		if (strncmp(name, "power-mgt", 9) == 0) {
369 			uint32_t prim_info[9];
370 
371 			if (OF_getprop(child, "prim-info", prim_info,
372 			    sizeof(prim_info)) >= 7)
373 				sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
374 
375 			if (bootverbose && sc->sc_batteries > 0)
376 				device_printf(dev, "%d batteries detected\n",
377 				    sc->sc_batteries);
378 		}
379 	}
380 
381 	/*
382 	 * Set up sysctls
383 	 */
384 
385 	ctx = device_get_sysctl_ctx(dev);
386 	tree = device_get_sysctl_tree(dev);
387 
388 	SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
389 	    "server_mode", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
390 	    pmu_server_mode, "I", "Enable reboot after power failure");
391 
392 	if (sc->sc_batteries > 0) {
393 		struct sysctl_oid *oid, *battroot;
394 		char battnum[2];
395 
396 		battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
397 		    "batteries", CTLFLAG_RD, 0, "Battery Information");
398 
399 		for (i = 0; i < sc->sc_batteries; i++) {
400 			battnum[0] = i + '0';
401 			battnum[1] = '\0';
402 
403 			oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
404 			    OID_AUTO, battnum, CTLFLAG_RD, 0,
405 			    "Battery Information");
406 
407 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
408 			    "present", CTLTYPE_INT | CTLFLAG_RD, sc,
409 			    PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
410 			    "I", "Battery present");
411 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
412 			    "charging", CTLTYPE_INT | CTLFLAG_RD, sc,
413 			    PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
414 			    "I", "Battery charging");
415 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
416 			    "charge", CTLTYPE_INT | CTLFLAG_RD, sc,
417 			    PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
418 			    "I", "Battery charge (mAh)");
419 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
420 			    "maxcharge", CTLTYPE_INT | CTLFLAG_RD, sc,
421 			    PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
422 			    "I", "Maximum battery capacity (mAh)");
423 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
424 			    "rate", CTLTYPE_INT | CTLFLAG_RD, sc,
425 			    PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
426 			    "I", "Battery discharge rate (mA)");
427 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
428 			    "voltage", CTLTYPE_INT | CTLFLAG_RD, sc,
429 			    PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
430 			    "I", "Battery voltage (mV)");
431 
432 			/* Knobs for mental compatibility with ACPI */
433 
434 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
435 			    "time", CTLTYPE_INT | CTLFLAG_RD, sc,
436 			    PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
437 			    "I", "Time Remaining (minutes)");
438 			SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
439 			    "life", CTLTYPE_INT | CTLFLAG_RD, sc,
440 			    PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
441 			    "I", "Capacity remaining (percent)");
442 		}
443 	}
444 
445 	/*
446 	 * Set up LED interface
447 	 */
448 
449 	sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
450 
451 	return (bus_generic_attach(dev));
452 }
453 
454 static int
455 pmu_detach(device_t dev)
456 {
457 	struct pmu_softc *sc;
458 
459 	sc = device_get_softc(dev);
460 
461 	if (sc->sc_leddev != NULL)
462 		led_destroy(sc->sc_leddev);
463 
464 	bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
465 	bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
466 	bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
467 	mtx_destroy(&sc->sc_mutex);
468 
469 	return (bus_generic_detach(dev));
470 }
471 
472 static uint8_t
473 pmu_read_reg(struct pmu_softc *sc, u_int offset)
474 {
475 	return (bus_read_1(sc->sc_memr, offset));
476 }
477 
478 static void
479 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
480 {
481 	bus_write_1(sc->sc_memr, offset, value);
482 }
483 
484 static int
485 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
486 {
487 
488 	pmu_out(sc);
489 	pmu_write_reg(sc, vSR, data);
490 	pmu_ack_off(sc);
491 	/* wait for intr to come up */
492 	/* XXX should add a timeout and bail if it expires */
493 	do {} while (pmu_intr_state(sc) == 0);
494 	pmu_ack_on(sc);
495 	do {} while (pmu_intr_state(sc));
496 	pmu_ack_on(sc);
497 	return 0;
498 }
499 
500 static inline int
501 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
502 {
503 	volatile uint8_t scratch;
504 	pmu_in(sc);
505 	scratch = pmu_read_reg(sc, vSR);
506 	pmu_ack_off(sc);
507 	/* wait for intr to come up */
508 	do {} while (pmu_intr_state(sc) == 0);
509 	pmu_ack_on(sc);
510 	do {} while (pmu_intr_state(sc));
511 	*data = pmu_read_reg(sc, vSR);
512 	return 0;
513 }
514 
515 static int
516 pmu_intr_state(struct pmu_softc *sc)
517 {
518 	return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
519 }
520 
521 static int
522 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
523     uint8_t *out_msg)
524 {
525 	struct pmu_softc *sc = cookie;
526 	int i, rcv_len = -1;
527 	uint8_t out_len, intreg;
528 
529 	intreg = pmu_read_reg(sc, vIER);
530 	intreg &= 0x10;
531 	pmu_write_reg(sc, vIER, intreg);
532 
533 	/* wait idle */
534 	do {} while (pmu_intr_state(sc));
535 
536 	/* send command */
537 	pmu_send_byte(sc, cmd);
538 
539 	/* send length if necessary */
540 	if (pm_send_cmd_type[cmd] < 0) {
541 		pmu_send_byte(sc, length);
542 	}
543 
544 	for (i = 0; i < length; i++) {
545 		pmu_send_byte(sc, in_msg[i]);
546 	}
547 
548 	/* see if there's data to read */
549 	rcv_len = pm_receive_cmd_type[cmd];
550 	if (rcv_len == 0)
551 		goto done;
552 
553 	/* read command */
554 	if (rcv_len == 1) {
555 		pmu_read_byte(sc, out_msg);
556 		goto done;
557 	} else
558 		out_msg[0] = cmd;
559 	if (rcv_len < 0) {
560 		pmu_read_byte(sc, &out_len);
561 		rcv_len = out_len + 1;
562 	}
563 	for (i = 1; i < min(rcv_len, rlen); i++)
564 		pmu_read_byte(sc, &out_msg[i]);
565 
566 done:
567 	pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
568 
569 	return rcv_len;
570 }
571 
572 
573 static void
574 pmu_poll(device_t dev)
575 {
576 	pmu_intr(dev);
577 }
578 
579 static void
580 pmu_in(struct pmu_softc *sc)
581 {
582 	uint8_t reg;
583 
584 	reg = pmu_read_reg(sc, vACR);
585 	reg &= ~vSR_OUT;
586 	reg |= 0x0c;
587 	pmu_write_reg(sc, vACR, reg);
588 }
589 
590 static void
591 pmu_out(struct pmu_softc *sc)
592 {
593 	uint8_t reg;
594 
595 	reg = pmu_read_reg(sc, vACR);
596 	reg |= vSR_OUT;
597 	reg |= 0x0c;
598 	pmu_write_reg(sc, vACR, reg);
599 }
600 
601 static void
602 pmu_ack_off(struct pmu_softc *sc)
603 {
604 	uint8_t reg;
605 
606 	reg = pmu_read_reg(sc, vBufB);
607 	reg &= ~vPB4;
608 	pmu_write_reg(sc, vBufB, reg);
609 }
610 
611 static void
612 pmu_ack_on(struct pmu_softc *sc)
613 {
614 	uint8_t reg;
615 
616 	reg = pmu_read_reg(sc, vBufB);
617 	reg |= vPB4;
618 	pmu_write_reg(sc, vBufB, reg);
619 }
620 
621 static void
622 pmu_intr(void *arg)
623 {
624 	device_t        dev;
625 	struct pmu_softc *sc;
626 
627 	unsigned int len;
628 	uint8_t resp[16];
629 	uint8_t junk[16];
630 
631         dev = (device_t)arg;
632 	sc = device_get_softc(dev);
633 
634 	mtx_lock(&sc->sc_mutex);
635 
636 	pmu_write_reg(sc, vIFR, 0x90);	/* Clear 'em */
637 	len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
638 
639 	mtx_unlock(&sc->sc_mutex);
640 
641 	if ((len < 1) || (resp[1] == 0)) {
642 		return;
643 	}
644 
645 	if (resp[1] & PMU_INT_ADB) {
646 		/*
647 		 * the PMU will turn off autopolling after each command that
648 		 * it did not issue, so we assume any but TALK R0 is ours and
649 		 * re-enable autopoll here whenever we receive an ACK for a
650 		 * non TR0 command.
651 		 */
652 		mtx_lock(&sc->sc_mutex);
653 
654 		if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
655 			if (sc->sc_autopoll) {
656 				uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
657 				    (sc->sc_autopoll >> 8) & 0xff,
658 				    sc->sc_autopoll & 0xff};
659 
660 				pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
661 			}
662 		}
663 
664 		mtx_unlock(&sc->sc_mutex);
665 
666 		adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
667 			len - 3,&resp[3]);
668 	}
669 }
670 
671 static u_int
672 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
673     u_char poll)
674 {
675 	struct pmu_softc *sc = device_get_softc(dev);
676 	int i,replen;
677 	uint8_t packet[16], resp[16];
678 
679 	/* construct an ADB command packet and send it */
680 
681 	packet[0] = command_byte;
682 
683 	packet[1] = 0;
684 	packet[2] = len;
685 	for (i = 0; i < len; i++)
686 		packet[i + 3] = data[i];
687 
688 	mtx_lock(&sc->sc_mutex);
689 	replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
690 	mtx_unlock(&sc->sc_mutex);
691 
692 	if (poll)
693 		pmu_poll(dev);
694 
695 	return 0;
696 }
697 
698 static u_int
699 pmu_adb_autopoll(device_t dev, uint16_t mask)
700 {
701 	struct pmu_softc *sc = device_get_softc(dev);
702 
703 	/* magical incantation to re-enable autopolling */
704 	uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
705 	uint8_t resp[16];
706 
707 	mtx_lock(&sc->sc_mutex);
708 
709 	if (sc->sc_autopoll == mask) {
710 		mtx_unlock(&sc->sc_mutex);
711 		return 0;
712 	}
713 
714 	sc->sc_autopoll = mask & 0xffff;
715 
716 	if (mask)
717 		pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
718 	else
719 		pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
720 
721 	mtx_unlock(&sc->sc_mutex);
722 
723 	return 0;
724 }
725 
726 static void
727 pmu_set_sleepled(void *xsc, int onoff)
728 {
729 	struct pmu_softc *sc = xsc;
730 	uint8_t cmd[] = {4, 0, 0};
731 
732 	cmd[2] = onoff;
733 
734 	mtx_lock(&sc->sc_mutex);
735 	pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
736 	mtx_unlock(&sc->sc_mutex);
737 }
738 
739 static int
740 pmu_server_mode(SYSCTL_HANDLER_ARGS)
741 {
742 	struct pmu_softc *sc = arg1;
743 
744 	u_int server_mode = 0;
745 	uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
746 	uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
747 	uint8_t resp[3];
748 	int error, len;
749 
750 	mtx_lock(&sc->sc_mutex);
751 	len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
752 	mtx_unlock(&sc->sc_mutex);
753 
754 	if (len == 3)
755 		server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
756 
757 	error = sysctl_handle_int(oidp, &server_mode, 0, req);
758 
759 	if (len != 3)
760 		return (EINVAL);
761 
762 	if (error || !req->newptr)
763 		return (error);
764 
765 	if (server_mode == 1)
766 		setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
767 	else if (server_mode == 0)
768 		setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
769 	else
770 		return (EINVAL);
771 
772 	setcmd[1] = resp[1];
773 
774 	mtx_lock(&sc->sc_mutex);
775 	pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
776 	mtx_unlock(&sc->sc_mutex);
777 
778 	return (0);
779 }
780 
781 static int
782 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
783 {
784 	uint8_t reg;
785 	uint8_t resp[16];
786 	int len;
787 
788 	reg = batt + 1;
789 
790 	mtx_lock(&sc->sc_mutex);
791 	len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, &reg, 16, resp);
792 	mtx_unlock(&sc->sc_mutex);
793 
794 	if (len < 3)
795 		return (-1);
796 
797 	/* All PMU battery info replies share a common header:
798 	 * Byte 1	Payload Format
799 	 * Byte 2	Battery Flags
800 	 */
801 
802 	info->state = resp[2];
803 
804 	switch (resp[1]) {
805 	case 3:
806 	case 4:
807 		/*
808 		 * Formats 3 and 4 appear to be the same:
809 		 * Byte 3	Charge
810 		 * Byte 4	Max Charge
811 		 * Byte 5	Current
812 		 * Byte 6	Voltage
813 		 */
814 
815 		info->charge = resp[3];
816 		info->maxcharge = resp[4];
817 		/* Current can be positive or negative */
818 		info->current = (int8_t)resp[5];
819 		info->voltage = resp[6];
820 		break;
821 	case 5:
822 		/*
823 		 * Formats 5 is a wider version of formats 3 and 4
824 		 * Byte 3-4	Charge
825 		 * Byte 5-6	Max Charge
826 		 * Byte 7-8	Current
827 		 * Byte 9-10	Voltage
828 		 */
829 
830 		info->charge = (resp[3] << 8) | resp[4];
831 		info->maxcharge = (resp[5] << 8) | resp[6];
832 		/* Current can be positive or negative */
833 		info->current = (int16_t)((resp[7] << 8) | resp[8]);
834 		info->voltage = (resp[9] << 8) | resp[10];
835 		break;
836 	default:
837 		device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
838 		    resp[1]);
839 		return (-1);
840 	}
841 
842 	return (0);
843 }
844 
845 static int
846 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
847 {
848 	struct pmu_softc *sc;
849 	struct pmu_battstate batt;
850 	int error, result;
851 
852 	sc = arg1;
853 
854 	error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
855 
856 	if (error != 0)
857 		return (error);
858 
859 	switch (arg2 & 0xff00) {
860 	case PMU_BATSYSCTL_PRESENT:
861 		result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
862 		break;
863 	case PMU_BATSYSCTL_CHARGING:
864 		result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
865 		break;
866 	case PMU_BATSYSCTL_CHARGE:
867 		result = batt.charge;
868 		break;
869 	case PMU_BATSYSCTL_MAXCHARGE:
870 		result = batt.maxcharge;
871 		break;
872 	case PMU_BATSYSCTL_CURRENT:
873 		result = batt.current;
874 		break;
875 	case PMU_BATSYSCTL_VOLTAGE:
876 		result = batt.voltage;
877 		break;
878 	case PMU_BATSYSCTL_TIME:
879 		/* Time remaining until full charge/discharge, in minutes */
880 
881 		if (batt.current >= 0)
882 			result = (batt.maxcharge - batt.charge) /* mAh */ * 60
883 			    / batt.current /* mA */;
884 		else
885 			result = (batt.charge /* mAh */ * 60)
886 			    / (-batt.current /* mA */);
887 		break;
888 	case PMU_BATSYSCTL_LIFE:
889 		/* Battery charge fraction, in percent */
890 		result = (batt.charge * 100) / batt.maxcharge;
891 		break;
892 	default:
893 		/* This should never happen */
894 		result = -1;
895 	};
896 
897 	error = sysctl_handle_int(oidp, &result, 0, req);
898 
899 	return (error);
900 }
901 
902