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