2 * Copyright (c) 2006 Michael Lorenz
3 * Copyright 2008 by Nathan Whitehorn
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/module.h>
37 #include <sys/kernel.h>
38 #include <sys/clock.h>
40 #include <sys/reboot.h>
41 #include <sys/sysctl.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/openfirm.h>
45 #include <dev/led/led.h>
47 #include <machine/_inttypes.h>
48 #include <machine/bus.h>
49 #include <machine/cpu.h>
50 #include <machine/hid.h>
51 #include <machine/intr_machdep.h>
52 #include <machine/md_var.h>
53 #include <machine/pcb.h>
54 #include <machine/pio.h>
55 #include <machine/resource.h>
62 #include <dev/adb/adb.h>
67 #include "uninorthvar.h" /* For unin_chip_sleep()/unin_chip_wake() */
69 #define PMU_DEFAULTS PMU_INT_TICK | PMU_INT_ADB | \
70 PMU_INT_PCEJECT | PMU_INT_SNDBRT | \
71 PMU_INT_BATTERY | PMU_INT_ENVIRONMENT
76 static int pmu_probe(device_t);
77 static int pmu_attach(device_t);
78 static int pmu_detach(device_t);
83 static int pmu_gettime(device_t dev, struct timespec *ts);
84 static int pmu_settime(device_t dev, struct timespec *ts);
90 static u_int pmu_adb_send(device_t dev, u_char command_byte, int len,
91 u_char *data, u_char poll);
92 static u_int pmu_adb_autopoll(device_t dev, uint16_t mask);
93 static u_int pmu_poll(device_t dev);
99 static void pmu_shutdown(void *xsc, int howto);
100 static void pmu_set_sleepled(void *xsc, int onoff);
101 static int pmu_server_mode(SYSCTL_HANDLER_ARGS);
102 static int pmu_acline_state(SYSCTL_HANDLER_ARGS);
103 static int pmu_query_battery(struct pmu_softc *sc, int batt,
104 struct pmu_battstate *info);
105 static int pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS);
108 * List of battery-related sysctls we might ask for
112 PMU_BATSYSCTL_PRESENT = 1 << 8,
113 PMU_BATSYSCTL_CHARGING = 2 << 8,
114 PMU_BATSYSCTL_CHARGE = 3 << 8,
115 PMU_BATSYSCTL_MAXCHARGE = 4 << 8,
116 PMU_BATSYSCTL_CURRENT = 5 << 8,
117 PMU_BATSYSCTL_VOLTAGE = 6 << 8,
118 PMU_BATSYSCTL_TIME = 7 << 8,
119 PMU_BATSYSCTL_LIFE = 8 << 8
122 static device_method_t pmu_methods[] = {
123 /* Device interface */
124 DEVMETHOD(device_probe, pmu_probe),
125 DEVMETHOD(device_attach, pmu_attach),
126 DEVMETHOD(device_detach, pmu_detach),
127 DEVMETHOD(device_shutdown, bus_generic_shutdown),
129 /* ADB bus interface */
130 DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send),
131 DEVMETHOD(adb_hb_controller_poll, pmu_poll),
132 DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll),
134 /* Clock interface */
135 DEVMETHOD(clock_gettime, pmu_gettime),
136 DEVMETHOD(clock_settime, pmu_settime),
141 static driver_t pmu_driver = {
144 sizeof(struct pmu_softc),
147 static devclass_t pmu_devclass;
149 DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0);
150 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0);
152 static int pmuextint_probe(device_t);
153 static int pmuextint_attach(device_t);
155 static device_method_t pmuextint_methods[] = {
156 /* Device interface */
157 DEVMETHOD(device_probe, pmuextint_probe),
158 DEVMETHOD(device_attach, pmuextint_attach),
163 static driver_t pmuextint_driver = {
169 static devclass_t pmuextint_devclass;
171 DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass, 0, 0);
173 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */
174 MODULE_DEPEND(pmu, usb, 1, 1, 1);
176 static void pmu_intr(void *arg);
177 static void pmu_in(struct pmu_softc *sc);
178 static void pmu_out(struct pmu_softc *sc);
179 static void pmu_ack_on(struct pmu_softc *sc);
180 static void pmu_ack_off(struct pmu_softc *sc);
181 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg,
182 int rlen, uint8_t *out_msg);
183 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset);
184 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value);
185 static int pmu_intr_state(struct pmu_softc *);
187 /* these values shows that number of data returned after 'send' cmd is sent */
188 static signed char pm_send_cmd_type[] = {
189 -1, -1, -1, -1, -1, -1, -1, -1,
190 -1, -1, -1, -1, -1, -1, -1, -1,
191 0x01, 0x01, -1, -1, -1, -1, -1, -1,
192 0x00, 0x00, -1, -1, -1, -1, -1, 0x00,
193 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1,
194 0x00, -1, -1, -1, -1, -1, -1, -1,
195 0x04, 0x14, -1, 0x03, -1, -1, -1, -1,
196 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1,
197 0x01, 0x01, -1, -1, -1, -1, -1, -1,
198 0x00, 0x00, -1, -1, 0x01, -1, -1, -1,
199 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01,
200 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1,
201 0x02, -1, -1, -1, -1, -1, -1, -1,
202 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1,
203 0x01, 0x01, 0x01, -1, -1, -1, -1, -1,
204 0x00, 0x00, -1, -1, -1, 0x05, 0x04, 0x04,
205 0x04, -1, 0x00, -1, -1, -1, -1, -1,
206 0x00, -1, -1, -1, -1, -1, -1, -1,
207 0x01, 0x02, -1, -1, -1, -1, -1, -1,
208 0x00, 0x00, -1, -1, -1, -1, -1, -1,
209 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1,
210 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1,
211 -1, -1, -1, -1, -1, -1, -1, -1,
212 -1, -1, -1, -1, -1, -1, -1, -1,
213 -1, -1, -1, -1, -1, -1, -1, -1,
214 -1, -1, -1, -1, -1, -1, -1, -1,
215 0x00, -1, -1, -1, -1, -1, -1, -1,
216 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1,
217 -1, 0x04, 0x00, -1, -1, -1, -1, -1,
218 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00,
219 -1, -1, -1, -1, -1, -1, -1, -1,
220 -1, -1, -1, -1, -1, -1, -1, -1
223 /* these values shows that number of data returned after 'receive' cmd is sent */
224 static signed char pm_receive_cmd_type[] = {
225 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
226 -1, -1, -1, -1, -1, -1, -1, -1,
227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
228 0x02, 0x02, -1, -1, -1, -1, -1, 0x00,
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 0x05, 0x15, -1, 0x02, -1, -1, -1, -1,
233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
234 0x02, 0x02, -1, -1, -1, -1, -1, -1,
235 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
236 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1,
237 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
238 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1,
239 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
240 -1, -1, -1, -1, -1, 0x01, 0x01, 0x01,
241 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
242 0x06, -1, -1, -1, -1, -1, -1, -1,
243 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
244 0x02, 0x02, -1, -1, -1, -1, -1, -1,
245 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
246 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1,
247 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
248 -1, -1, -1, -1, -1, -1, -1, -1,
249 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
250 -1, -1, -1, -1, -1, -1, -1, -1,
251 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
252 0x02, 0x02, -1, -1, 0x02, -1, -1, -1,
253 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
254 -1, -1, 0x02, -1, -1, -1, -1, 0x00,
255 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
256 -1, -1, -1, -1, -1, -1, -1, -1,
259 /* We only have one of each device, so globals are safe */
260 static device_t pmu = NULL;
261 static device_t pmu_extint = NULL;
264 pmuextint_probe(device_t dev)
266 const char *type = ofw_bus_get_type(dev);
268 if (strcmp(type, "extint-gpio1") != 0)
271 device_set_desc(dev, "Apple PMU99 External Interrupt");
276 pmu_probe(device_t dev)
278 const char *type = ofw_bus_get_type(dev);
280 if (strcmp(type, "via-pmu") != 0)
283 device_set_desc(dev, "Apple PMU99 Controller");
289 setup_pmu_intr(device_t dev, device_t extint)
291 struct pmu_softc *sc;
292 sc = device_get_softc(dev);
295 sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid,
297 if (sc->sc_irq == NULL) {
298 device_printf(dev, "could not allocate interrupt\n");
302 if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE
303 | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) {
304 device_printf(dev, "could not setup interrupt\n");
305 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid,
314 pmuextint_attach(device_t dev)
318 return (setup_pmu_intr(pmu,dev));
324 pmu_attach(device_t dev)
326 struct pmu_softc *sc;
330 uint8_t cmd[2] = {2, 0};
332 phandle_t node,child;
333 struct sysctl_ctx_list *ctx;
334 struct sysctl_oid *tree;
336 sc = device_get_softc(dev);
340 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY,
341 &sc->sc_memrid, RF_ACTIVE);
343 mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE);
345 if (sc->sc_memr == NULL) {
346 device_printf(dev, "Could not alloc mem resource!\n");
351 * Our interrupt is attached to a GPIO pin. Depending on probe order,
352 * we may not have found it yet. If we haven't, it will find us, and
353 * attach our interrupt then.
356 if (pmu_extint != NULL) {
357 if (setup_pmu_intr(dev,pmu_extint) != 0)
362 sc->sc_batteries = 0;
364 sc->sc_leddev = NULL;
368 pmu_write_reg(sc, vBufB, pmu_read_reg(sc, vBufB) | vPB4);
369 pmu_write_reg(sc, vDirB, (pmu_read_reg(sc, vDirB) | vPB4) & ~vPB3);
372 pmu_send(sc, PMU_SET_IMASK, 1, ®, 16, resp);
374 pmu_write_reg(sc, vIER, 0x94); /* make sure VIA interrupts are on */
376 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp);
377 pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp);
379 /* Initialize child buses (ADB) */
380 node = ofw_bus_get_node(dev);
382 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
385 memset(name, 0, sizeof(name));
386 OF_getprop(child, "name", name, sizeof(name));
389 device_printf(dev, "PMU child <%s>\n",name);
391 if (strncmp(name, "adb", 4) == 0) {
392 sc->adb_bus = device_add_child(dev,"adb",-1);
395 if (strncmp(name, "power-mgt", 9) == 0) {
396 uint32_t prim_info[9];
398 if (OF_getprop(child, "prim-info", prim_info,
399 sizeof(prim_info)) >= 7)
400 sc->sc_batteries = (prim_info[6] >> 16) & 0xff;
402 if (bootverbose && sc->sc_batteries > 0)
403 device_printf(dev, "%d batteries detected\n",
412 ctx = device_get_sysctl_ctx(dev);
413 tree = device_get_sysctl_tree(dev);
415 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
416 "server_mode", CTLTYPE_INT | CTLFLAG_RW, sc, 0,
417 pmu_server_mode, "I", "Enable reboot after power failure");
419 if (sc->sc_batteries > 0) {
420 struct sysctl_oid *oid, *battroot;
423 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
424 "acline", CTLTYPE_INT | CTLFLAG_RD, sc, 0,
425 pmu_acline_state, "I", "AC Line Status");
427 battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO,
428 "batteries", CTLFLAG_RD, 0, "Battery Information");
430 for (i = 0; i < sc->sc_batteries; i++) {
431 battnum[0] = i + '0';
434 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot),
435 OID_AUTO, battnum, CTLFLAG_RD, 0,
436 "Battery Information");
438 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
439 "present", CTLTYPE_INT | CTLFLAG_RD, sc,
440 PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl,
441 "I", "Battery present");
442 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
443 "charging", CTLTYPE_INT | CTLFLAG_RD, sc,
444 PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl,
445 "I", "Battery charging");
446 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
447 "charge", CTLTYPE_INT | CTLFLAG_RD, sc,
448 PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl,
449 "I", "Battery charge (mAh)");
450 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
451 "maxcharge", CTLTYPE_INT | CTLFLAG_RD, sc,
452 PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl,
453 "I", "Maximum battery capacity (mAh)");
454 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
455 "rate", CTLTYPE_INT | CTLFLAG_RD, sc,
456 PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl,
457 "I", "Battery discharge rate (mA)");
458 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
459 "voltage", CTLTYPE_INT | CTLFLAG_RD, sc,
460 PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl,
461 "I", "Battery voltage (mV)");
463 /* Knobs for mental compatibility with ACPI */
465 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
466 "time", CTLTYPE_INT | CTLFLAG_RD, sc,
467 PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl,
468 "I", "Time Remaining (minutes)");
469 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO,
470 "life", CTLTYPE_INT | CTLFLAG_RD, sc,
471 PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl,
472 "I", "Capacity remaining (percent)");
477 * Set up LED interface
480 sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled");
486 clock_register(dev, 1000);
489 * Register power control handler
491 EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc,
494 return (bus_generic_attach(dev));
498 pmu_detach(device_t dev)
500 struct pmu_softc *sc;
502 sc = device_get_softc(dev);
504 if (sc->sc_leddev != NULL)
505 led_destroy(sc->sc_leddev);
507 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih);
508 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq);
509 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr);
510 mtx_destroy(&sc->sc_mutex);
512 return (bus_generic_detach(dev));
516 pmu_read_reg(struct pmu_softc *sc, u_int offset)
518 return (bus_read_1(sc->sc_memr, offset));
522 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value)
524 bus_write_1(sc->sc_memr, offset, value);
528 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
532 pmu_write_reg(sc, vSR, data);
534 /* wait for intr to come up */
535 /* XXX should add a timeout and bail if it expires */
536 do {} while (pmu_intr_state(sc) == 0);
538 do {} while (pmu_intr_state(sc));
544 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
546 volatile uint8_t scratch;
548 scratch = pmu_read_reg(sc, vSR);
550 /* wait for intr to come up */
551 do {} while (pmu_intr_state(sc) == 0);
553 do {} while (pmu_intr_state(sc));
554 *data = pmu_read_reg(sc, vSR);
559 pmu_intr_state(struct pmu_softc *sc)
561 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
565 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
568 struct pmu_softc *sc = cookie;
570 uint8_t out_len, intreg;
572 intreg = pmu_read_reg(sc, vIER);
574 pmu_write_reg(sc, vIER, intreg);
577 do {} while (pmu_intr_state(sc));
580 pmu_send_byte(sc, cmd);
582 /* send length if necessary */
583 if (pm_send_cmd_type[cmd] < 0) {
584 pmu_send_byte(sc, length);
587 for (i = 0; i < length; i++) {
588 pmu_send_byte(sc, in_msg[i]);
591 /* see if there's data to read */
592 rcv_len = pm_receive_cmd_type[cmd];
598 pmu_read_byte(sc, out_msg);
603 pmu_read_byte(sc, &out_len);
604 rcv_len = out_len + 1;
606 for (i = 1; i < min(rcv_len, rlen); i++)
607 pmu_read_byte(sc, &out_msg[i]);
610 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
617 pmu_poll(device_t dev)
624 pmu_in(struct pmu_softc *sc)
628 reg = pmu_read_reg(sc, vACR);
631 pmu_write_reg(sc, vACR, reg);
635 pmu_out(struct pmu_softc *sc)
639 reg = pmu_read_reg(sc, vACR);
642 pmu_write_reg(sc, vACR, reg);
646 pmu_ack_off(struct pmu_softc *sc)
650 reg = pmu_read_reg(sc, vBufB);
652 pmu_write_reg(sc, vBufB, reg);
656 pmu_ack_on(struct pmu_softc *sc)
660 reg = pmu_read_reg(sc, vBufB);
662 pmu_write_reg(sc, vBufB, reg);
669 struct pmu_softc *sc;
676 sc = device_get_softc(dev);
678 mtx_lock(&sc->sc_mutex);
680 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */
681 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
683 mtx_unlock(&sc->sc_mutex);
685 if ((len < 1) || (resp[1] == 0)) {
689 if (resp[1] & PMU_INT_ADB) {
691 * the PMU will turn off autopolling after each command that
692 * it did not issue, so we assume any but TALK R0 is ours and
693 * re-enable autopoll here whenever we receive an ACK for a
696 mtx_lock(&sc->sc_mutex);
698 if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) {
699 if (sc->sc_autopoll) {
700 uint8_t cmd[] = {0, PMU_SET_POLL_MASK,
701 (sc->sc_autopoll >> 8) & 0xff,
702 sc->sc_autopoll & 0xff};
704 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk);
708 mtx_unlock(&sc->sc_mutex);
710 adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2],
713 if (resp[1] & PMU_INT_ENVIRONMENT) {
714 /* if the lid was just closed, notify devd. */
715 if ((resp[2] & PMU_ENV_LID_CLOSED) && (!sc->lid_closed)) {
717 if (devctl_process_running())
718 devctl_notify("PMU", "lid", "close", NULL);
720 else if (!(resp[2] & PMU_ENV_LID_CLOSED) && (sc->lid_closed)) {
721 /* if the lid was just opened, notify devd. */
722 if (devctl_process_running())
723 devctl_notify("PMU", "lid", "open", NULL);
730 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data,
733 struct pmu_softc *sc = device_get_softc(dev);
735 uint8_t packet[16], resp[16];
737 /* construct an ADB command packet and send it */
739 packet[0] = command_byte;
743 for (i = 0; i < len; i++)
744 packet[i + 3] = data[i];
746 mtx_lock(&sc->sc_mutex);
747 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
748 mtx_unlock(&sc->sc_mutex);
757 pmu_adb_autopoll(device_t dev, uint16_t mask)
759 struct pmu_softc *sc = device_get_softc(dev);
761 /* magical incantation to re-enable autopolling */
762 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff};
765 mtx_lock(&sc->sc_mutex);
767 if (sc->sc_autopoll == mask) {
768 mtx_unlock(&sc->sc_mutex);
772 sc->sc_autopoll = mask & 0xffff;
775 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
777 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
779 mtx_unlock(&sc->sc_mutex);
785 pmu_shutdown(void *xsc, int howto)
787 struct pmu_softc *sc = xsc;
788 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
791 pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL);
793 pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL);
799 pmu_set_sleepled(void *xsc, int onoff)
801 struct pmu_softc *sc = xsc;
802 uint8_t cmd[] = {4, 0, 0};
806 mtx_lock(&sc->sc_mutex);
807 pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL);
808 mtx_unlock(&sc->sc_mutex);
812 pmu_server_mode(SYSCTL_HANDLER_ARGS)
814 struct pmu_softc *sc = arg1;
816 u_int server_mode = 0;
817 uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS};
818 uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT};
822 mtx_lock(&sc->sc_mutex);
823 len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp);
824 mtx_unlock(&sc->sc_mutex);
827 server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0;
829 error = sysctl_handle_int(oidp, &server_mode, 0, req);
834 if (error || !req->newptr)
837 if (server_mode == 1)
838 setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS;
839 else if (server_mode == 0)
840 setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS;
846 mtx_lock(&sc->sc_mutex);
847 pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp);
848 mtx_unlock(&sc->sc_mutex);
854 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info)
862 mtx_lock(&sc->sc_mutex);
863 len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, ®, 16, resp);
864 mtx_unlock(&sc->sc_mutex);
869 /* All PMU battery info replies share a common header:
870 * Byte 1 Payload Format
871 * Byte 2 Battery Flags
874 info->state = resp[2];
880 * Formats 3 and 4 appear to be the same:
887 info->charge = resp[3];
888 info->maxcharge = resp[4];
889 /* Current can be positive or negative */
890 info->current = (int8_t)resp[5];
891 info->voltage = resp[6];
895 * Formats 5 is a wider version of formats 3 and 4
897 * Byte 5-6 Max Charge
902 info->charge = (resp[3] << 8) | resp[4];
903 info->maxcharge = (resp[5] << 8) | resp[6];
904 /* Current can be positive or negative */
905 info->current = (int16_t)((resp[7] << 8) | resp[8]);
906 info->voltage = (resp[9] << 8) | resp[10];
909 device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n",
918 pmu_acline_state(SYSCTL_HANDLER_ARGS)
920 struct pmu_softc *sc;
921 struct pmu_battstate batt;
926 /* The PMU treats the AC line status as a property of the battery */
927 error = pmu_query_battery(sc, 0, &batt);
932 result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0;
933 error = sysctl_handle_int(oidp, &result, 0, req);
939 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS)
941 struct pmu_softc *sc;
942 struct pmu_battstate batt;
947 error = pmu_query_battery(sc, arg2 & 0x00ff, &batt);
952 switch (arg2 & 0xff00) {
953 case PMU_BATSYSCTL_PRESENT:
954 result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0;
956 case PMU_BATSYSCTL_CHARGING:
957 result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0;
959 case PMU_BATSYSCTL_CHARGE:
960 result = batt.charge;
962 case PMU_BATSYSCTL_MAXCHARGE:
963 result = batt.maxcharge;
965 case PMU_BATSYSCTL_CURRENT:
966 result = batt.current;
968 case PMU_BATSYSCTL_VOLTAGE:
969 result = batt.voltage;
971 case PMU_BATSYSCTL_TIME:
972 /* Time remaining until full charge/discharge, in minutes */
974 if (batt.current >= 0)
975 result = (batt.maxcharge - batt.charge) /* mAh */ * 60
976 / batt.current /* mA */;
978 result = (batt.charge /* mAh */ * 60)
979 / (-batt.current /* mA */);
981 case PMU_BATSYSCTL_LIFE:
982 /* Battery charge fraction, in percent */
983 result = (batt.charge * 100) / batt.maxcharge;
986 /* This should never happen */
990 error = sysctl_handle_int(oidp, &result, 0, req);
995 #define DIFF19041970 2082844800
998 pmu_gettime(device_t dev, struct timespec *ts)
1000 struct pmu_softc *sc = device_get_softc(dev);
1004 mtx_lock(&sc->sc_mutex);
1005 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
1006 mtx_unlock(&sc->sc_mutex);
1008 memcpy(&sec, &resp[1], 4);
1009 ts->tv_sec = sec - DIFF19041970;
1016 pmu_settime(device_t dev, struct timespec *ts)
1018 struct pmu_softc *sc = device_get_softc(dev);
1021 sec = ts->tv_sec + DIFF19041970;
1023 mtx_lock(&sc->sc_mutex);
1024 pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL);
1025 mtx_unlock(&sc->sc_mutex);
1031 pmu_set_speed(int low_speed)
1033 struct pmu_softc *sc;
1034 uint8_t sleepcmd[] = {'W', 'O', 'O', 'F', 0};
1037 sc = device_get_softc(pmu);
1038 pmu_write_reg(sc, vIER, 0x10);
1044 sleepcmd[4] = low_speed;
1045 pmu_send(sc, PMU_CPU_SPEED, 5, sleepcmd, 16, resp);
1046 unin_chip_sleep(NULL, 1);
1048 unin_chip_wake(NULL);
1050 mtdec(1); /* Force a decrementer exception */
1052 pmu_write_reg(sc, vIER, 0x90);