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