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