1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2006 Michael Lorenz 5 * Copyright 2008 by Nathan Whitehorn 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 22 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 24 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 25 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 27 * SUCH DAMAGE. 28 * 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include <sys/param.h> 35 #include <sys/systm.h> 36 #include <sys/module.h> 37 #include <sys/bus.h> 38 #include <sys/conf.h> 39 #include <sys/kernel.h> 40 #include <sys/kthread.h> 41 #include <sys/clock.h> 42 #include <sys/proc.h> 43 #include <sys/reboot.h> 44 #include <sys/sysctl.h> 45 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/openfirm.h> 48 #include <dev/led/led.h> 49 50 #include <machine/_inttypes.h> 51 #include <machine/bus.h> 52 #include <machine/cpu.h> 53 #include <machine/hid.h> 54 #include <machine/intr_machdep.h> 55 #include <machine/md_var.h> 56 #include <machine/pcb.h> 57 #include <machine/pio.h> 58 #include <machine/resource.h> 59 60 #include <vm/vm.h> 61 #include <vm/pmap.h> 62 63 #include <sys/rman.h> 64 65 #include <dev/adb/adb.h> 66 67 #include "clock_if.h" 68 #include "pmuvar.h" 69 #include "viareg.h" 70 #include "uninorthvar.h" /* For unin_chip_sleep()/unin_chip_wake() */ 71 72 #define PMU_DEFAULTS PMU_INT_TICK | PMU_INT_ADB | \ 73 PMU_INT_PCEJECT | PMU_INT_SNDBRT | \ 74 PMU_INT_BATTERY | PMU_INT_ENVIRONMENT 75 76 /* 77 * Bus interface 78 */ 79 static int pmu_probe(device_t); 80 static int pmu_attach(device_t); 81 static int pmu_detach(device_t); 82 83 /* 84 * Clock interface 85 */ 86 static int pmu_gettime(device_t dev, struct timespec *ts); 87 static int pmu_settime(device_t dev, struct timespec *ts); 88 89 /* 90 * ADB Interface 91 */ 92 93 static u_int pmu_adb_send(device_t dev, u_char command_byte, int len, 94 u_char *data, u_char poll); 95 static u_int pmu_adb_autopoll(device_t dev, uint16_t mask); 96 static u_int pmu_poll(device_t dev); 97 98 /* 99 * Power interface 100 */ 101 102 static void pmu_shutdown(void *xsc, int howto); 103 static void pmu_set_sleepled(void *xsc, int onoff); 104 static int pmu_server_mode(SYSCTL_HANDLER_ARGS); 105 static int pmu_acline_state(SYSCTL_HANDLER_ARGS); 106 static int pmu_query_battery(struct pmu_softc *sc, int batt, 107 struct pmu_battstate *info); 108 static int pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS); 109 static int pmu_battmon(SYSCTL_HANDLER_ARGS); 110 static void pmu_battquery_proc(void); 111 static void pmu_battery_notify(struct pmu_battstate *batt, 112 struct pmu_battstate *old); 113 114 /* 115 * List of battery-related sysctls we might ask for 116 */ 117 118 enum { 119 PMU_BATSYSCTL_PRESENT = 1 << 8, 120 PMU_BATSYSCTL_CHARGING = 2 << 8, 121 PMU_BATSYSCTL_CHARGE = 3 << 8, 122 PMU_BATSYSCTL_MAXCHARGE = 4 << 8, 123 PMU_BATSYSCTL_CURRENT = 5 << 8, 124 PMU_BATSYSCTL_VOLTAGE = 6 << 8, 125 PMU_BATSYSCTL_TIME = 7 << 8, 126 PMU_BATSYSCTL_LIFE = 8 << 8 127 }; 128 129 static device_method_t pmu_methods[] = { 130 /* Device interface */ 131 DEVMETHOD(device_probe, pmu_probe), 132 DEVMETHOD(device_attach, pmu_attach), 133 DEVMETHOD(device_detach, pmu_detach), 134 DEVMETHOD(device_shutdown, bus_generic_shutdown), 135 136 /* ADB bus interface */ 137 DEVMETHOD(adb_hb_send_raw_packet, pmu_adb_send), 138 DEVMETHOD(adb_hb_controller_poll, pmu_poll), 139 DEVMETHOD(adb_hb_set_autopoll_mask, pmu_adb_autopoll), 140 141 /* Clock interface */ 142 DEVMETHOD(clock_gettime, pmu_gettime), 143 DEVMETHOD(clock_settime, pmu_settime), 144 145 DEVMETHOD_END 146 }; 147 148 static driver_t pmu_driver = { 149 "pmu", 150 pmu_methods, 151 sizeof(struct pmu_softc), 152 }; 153 154 static devclass_t pmu_devclass; 155 156 EARLY_DRIVER_MODULE(pmu, macio, pmu_driver, pmu_devclass, 0, 0, 157 BUS_PASS_RESOURCE); 158 DRIVER_MODULE(adb, pmu, adb_driver, adb_devclass, 0, 0); 159 160 static int pmuextint_probe(device_t); 161 static int pmuextint_attach(device_t); 162 163 static device_method_t pmuextint_methods[] = { 164 /* Device interface */ 165 DEVMETHOD(device_probe, pmuextint_probe), 166 DEVMETHOD(device_attach, pmuextint_attach), 167 168 {0,0} 169 }; 170 171 static driver_t pmuextint_driver = { 172 "pmuextint", 173 pmuextint_methods, 174 0 175 }; 176 177 static devclass_t pmuextint_devclass; 178 179 EARLY_DRIVER_MODULE(pmuextint, macgpio, pmuextint_driver, pmuextint_devclass, 180 0, 0, BUS_PASS_RESOURCE); 181 182 /* Make sure uhid is loaded, as it turns off some of the ADB emulation */ 183 MODULE_DEPEND(pmu, usb, 1, 1, 1); 184 185 static void pmu_intr(void *arg); 186 static void pmu_in(struct pmu_softc *sc); 187 static void pmu_out(struct pmu_softc *sc); 188 static void pmu_ack_on(struct pmu_softc *sc); 189 static void pmu_ack_off(struct pmu_softc *sc); 190 static int pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, 191 int rlen, uint8_t *out_msg); 192 static uint8_t pmu_read_reg(struct pmu_softc *sc, u_int offset); 193 static void pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value); 194 static int pmu_intr_state(struct pmu_softc *); 195 196 /* these values shows that number of data returned after 'send' cmd is sent */ 197 static signed char pm_send_cmd_type[] = { 198 -1, -1, -1, -1, -1, -1, -1, -1, 199 -1, -1, -1, -1, -1, -1, -1, -1, 200 0x01, 0x01, -1, -1, -1, -1, -1, -1, 201 0x00, 0x00, -1, -1, -1, -1, -1, 0x00, 202 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1, 203 0x00, -1, -1, -1, -1, -1, -1, -1, 204 0x04, 0x14, -1, 0x03, -1, -1, -1, -1, 205 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1, 206 0x01, 0x01, -1, -1, -1, -1, -1, -1, 207 0x00, 0x00, -1, -1, 0x01, -1, -1, -1, 208 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01, 209 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1, 210 0x02, -1, -1, -1, -1, -1, -1, -1, 211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1, 212 0x01, 0x01, 0x01, -1, -1, -1, -1, -1, 213 0x00, 0x00, -1, -1, -1, 0x05, 0x04, 0x04, 214 0x04, -1, 0x00, -1, -1, -1, -1, -1, 215 0x00, -1, -1, -1, -1, -1, -1, -1, 216 0x01, 0x02, -1, -1, -1, -1, -1, -1, 217 0x00, 0x00, -1, -1, -1, -1, -1, -1, 218 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1, 219 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1, 220 -1, -1, -1, -1, -1, -1, -1, -1, 221 -1, -1, -1, -1, -1, -1, -1, -1, 222 -1, -1, -1, -1, -1, -1, -1, -1, 223 -1, -1, -1, -1, -1, -1, -1, -1, 224 0x00, -1, -1, -1, -1, -1, -1, -1, 225 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1, 226 -1, 0x04, 0x00, -1, -1, -1, -1, -1, 227 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00, 228 -1, -1, -1, -1, -1, -1, -1, -1, 229 -1, -1, -1, -1, -1, -1, -1, -1 230 }; 231 232 /* these values shows that number of data returned after 'receive' cmd is sent */ 233 static signed char pm_receive_cmd_type[] = { 234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 235 -1, -1, -1, -1, -1, -1, -1, -1, 236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 237 0x02, 0x02, -1, -1, -1, -1, -1, 0x00, 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 0x05, 0x15, -1, 0x02, -1, -1, -1, -1, 242 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 243 0x02, 0x02, -1, -1, -1, -1, -1, -1, 244 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 245 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1, 246 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 247 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1, 248 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 249 -1, -1, -1, -1, -1, 0x01, 0x01, 0x01, 250 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 251 0x06, -1, -1, -1, -1, -1, -1, -1, 252 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 253 0x02, 0x02, -1, -1, -1, -1, -1, -1, 254 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 255 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1, 256 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 257 -1, -1, -1, -1, -1, -1, -1, -1, 258 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 259 -1, -1, -1, -1, -1, -1, -1, -1, 260 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 261 0x02, 0x02, -1, -1, 0x02, -1, -1, -1, 262 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 263 -1, -1, 0x02, -1, -1, -1, -1, 0x00, 264 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 265 -1, -1, -1, -1, -1, -1, -1, -1, 266 }; 267 268 static int pmu_battmon_enabled = 1; 269 static struct proc *pmubattproc; 270 static struct kproc_desc pmu_batt_kp = { 271 "pmu_batt", 272 pmu_battquery_proc, 273 &pmubattproc 274 }; 275 276 /* We only have one of each device, so globals are safe */ 277 static device_t pmu = NULL; 278 static device_t pmu_extint = NULL; 279 280 static int 281 pmuextint_probe(device_t dev) 282 { 283 const char *type = ofw_bus_get_type(dev); 284 285 if (strcmp(type, "extint-gpio1") != 0) 286 return (ENXIO); 287 288 device_set_desc(dev, "Apple PMU99 External Interrupt"); 289 return (0); 290 } 291 292 static int 293 pmu_probe(device_t dev) 294 { 295 const char *type = ofw_bus_get_type(dev); 296 297 if (strcmp(type, "via-pmu") != 0) 298 return (ENXIO); 299 300 device_set_desc(dev, "Apple PMU99 Controller"); 301 return (0); 302 } 303 304 305 static int 306 setup_pmu_intr(device_t dev, device_t extint) 307 { 308 struct pmu_softc *sc; 309 sc = device_get_softc(dev); 310 311 sc->sc_irqrid = 0; 312 sc->sc_irq = bus_alloc_resource_any(extint, SYS_RES_IRQ, &sc->sc_irqrid, 313 RF_ACTIVE); 314 if (sc->sc_irq == NULL) { 315 device_printf(dev, "could not allocate interrupt\n"); 316 return (ENXIO); 317 } 318 319 if (bus_setup_intr(dev, sc->sc_irq, INTR_TYPE_MISC | INTR_MPSAFE 320 | INTR_ENTROPY, NULL, pmu_intr, dev, &sc->sc_ih) != 0) { 321 device_printf(dev, "could not setup interrupt\n"); 322 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, 323 sc->sc_irq); 324 return (ENXIO); 325 } 326 327 return (0); 328 } 329 330 static int 331 pmuextint_attach(device_t dev) 332 { 333 pmu_extint = dev; 334 if (pmu) 335 return (setup_pmu_intr(pmu,dev)); 336 337 return (0); 338 } 339 340 static int 341 pmu_attach(device_t dev) 342 { 343 struct pmu_softc *sc; 344 345 int i; 346 uint8_t reg; 347 uint8_t cmd[2] = {2, 0}; 348 uint8_t resp[16]; 349 phandle_t node,child; 350 struct sysctl_ctx_list *ctx; 351 struct sysctl_oid *tree; 352 353 sc = device_get_softc(dev); 354 sc->sc_dev = dev; 355 356 sc->sc_memrid = 0; 357 sc->sc_memr = bus_alloc_resource_any(dev, SYS_RES_MEMORY, 358 &sc->sc_memrid, RF_ACTIVE); 359 360 mtx_init(&sc->sc_mutex,"pmu",NULL,MTX_DEF | MTX_RECURSE); 361 362 if (sc->sc_memr == NULL) { 363 device_printf(dev, "Could not alloc mem resource!\n"); 364 return (ENXIO); 365 } 366 367 /* 368 * Our interrupt is attached to a GPIO pin. Depending on probe order, 369 * we may not have found it yet. If we haven't, it will find us, and 370 * attach our interrupt then. 371 */ 372 pmu = dev; 373 if (pmu_extint != NULL) { 374 if (setup_pmu_intr(dev,pmu_extint) != 0) 375 return (ENXIO); 376 } 377 378 sc->sc_autopoll = 0; 379 sc->sc_batteries = 0; 380 sc->adb_bus = NULL; 381 sc->sc_leddev = NULL; 382 383 /* Init PMU */ 384 385 pmu_write_reg(sc, vBufB, pmu_read_reg(sc, vBufB) | vPB4); 386 pmu_write_reg(sc, vDirB, (pmu_read_reg(sc, vDirB) | vPB4) & ~vPB3); 387 388 reg = PMU_DEFAULTS; 389 pmu_send(sc, PMU_SET_IMASK, 1, ®, 16, resp); 390 391 pmu_write_reg(sc, vIER, 0x94); /* make sure VIA interrupts are on */ 392 393 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp); 394 pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp); 395 396 /* Initialize child buses (ADB) */ 397 node = ofw_bus_get_node(dev); 398 399 for (child = OF_child(node); child != 0; child = OF_peer(child)) { 400 char name[32]; 401 402 memset(name, 0, sizeof(name)); 403 OF_getprop(child, "name", name, sizeof(name)); 404 405 if (bootverbose) 406 device_printf(dev, "PMU child <%s>\n",name); 407 408 if (strncmp(name, "adb", 4) == 0) { 409 sc->adb_bus = device_add_child(dev,"adb",-1); 410 } 411 412 if (strncmp(name, "power-mgt", 9) == 0) { 413 uint32_t prim_info[9]; 414 415 if (OF_getprop(child, "prim-info", prim_info, 416 sizeof(prim_info)) >= 7) 417 sc->sc_batteries = (prim_info[6] >> 16) & 0xff; 418 419 if (bootverbose && sc->sc_batteries > 0) 420 device_printf(dev, "%d batteries detected\n", 421 sc->sc_batteries); 422 } 423 } 424 425 /* 426 * Set up sysctls 427 */ 428 429 ctx = device_get_sysctl_ctx(dev); 430 tree = device_get_sysctl_tree(dev); 431 432 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 433 "server_mode", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 434 pmu_server_mode, "I", "Enable reboot after power failure"); 435 436 if (sc->sc_batteries > 0) { 437 struct sysctl_oid *oid, *battroot; 438 char battnum[2]; 439 440 /* Only start the battery monitor if we have a battery. */ 441 kproc_start(&pmu_batt_kp); 442 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 443 "monitor_batteries", CTLTYPE_INT | CTLFLAG_RW, sc, 0, 444 pmu_battmon, "I", "Post battery events to devd"); 445 446 447 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 448 "acline", CTLTYPE_INT | CTLFLAG_RD, sc, 0, 449 pmu_acline_state, "I", "AC Line Status"); 450 451 battroot = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(tree), OID_AUTO, 452 "batteries", CTLFLAG_RD, 0, "Battery Information"); 453 454 for (i = 0; i < sc->sc_batteries; i++) { 455 battnum[0] = i + '0'; 456 battnum[1] = '\0'; 457 458 oid = SYSCTL_ADD_NODE(ctx, SYSCTL_CHILDREN(battroot), 459 OID_AUTO, battnum, CTLFLAG_RD, 0, 460 "Battery Information"); 461 462 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 463 "present", CTLTYPE_INT | CTLFLAG_RD, sc, 464 PMU_BATSYSCTL_PRESENT | i, pmu_battquery_sysctl, 465 "I", "Battery present"); 466 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 467 "charging", CTLTYPE_INT | CTLFLAG_RD, sc, 468 PMU_BATSYSCTL_CHARGING | i, pmu_battquery_sysctl, 469 "I", "Battery charging"); 470 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 471 "charge", CTLTYPE_INT | CTLFLAG_RD, sc, 472 PMU_BATSYSCTL_CHARGE | i, pmu_battquery_sysctl, 473 "I", "Battery charge (mAh)"); 474 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 475 "maxcharge", CTLTYPE_INT | CTLFLAG_RD, sc, 476 PMU_BATSYSCTL_MAXCHARGE | i, pmu_battquery_sysctl, 477 "I", "Maximum battery capacity (mAh)"); 478 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 479 "rate", CTLTYPE_INT | CTLFLAG_RD, sc, 480 PMU_BATSYSCTL_CURRENT | i, pmu_battquery_sysctl, 481 "I", "Battery discharge rate (mA)"); 482 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 483 "voltage", CTLTYPE_INT | CTLFLAG_RD, sc, 484 PMU_BATSYSCTL_VOLTAGE | i, pmu_battquery_sysctl, 485 "I", "Battery voltage (mV)"); 486 487 /* Knobs for mental compatibility with ACPI */ 488 489 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 490 "time", CTLTYPE_INT | CTLFLAG_RD, sc, 491 PMU_BATSYSCTL_TIME | i, pmu_battquery_sysctl, 492 "I", "Time Remaining (minutes)"); 493 SYSCTL_ADD_PROC(ctx, SYSCTL_CHILDREN(oid), OID_AUTO, 494 "life", CTLTYPE_INT | CTLFLAG_RD, sc, 495 PMU_BATSYSCTL_LIFE | i, pmu_battquery_sysctl, 496 "I", "Capacity remaining (percent)"); 497 } 498 } 499 500 /* 501 * Set up LED interface 502 */ 503 504 sc->sc_leddev = led_create(pmu_set_sleepled, sc, "sleepled"); 505 506 /* 507 * Register RTC 508 */ 509 510 clock_register(dev, 1000); 511 512 /* 513 * Register power control handler 514 */ 515 EVENTHANDLER_REGISTER(shutdown_final, pmu_shutdown, sc, 516 SHUTDOWN_PRI_LAST); 517 518 return (bus_generic_attach(dev)); 519 } 520 521 static int 522 pmu_detach(device_t dev) 523 { 524 struct pmu_softc *sc; 525 526 sc = device_get_softc(dev); 527 528 if (sc->sc_leddev != NULL) 529 led_destroy(sc->sc_leddev); 530 531 bus_teardown_intr(dev, sc->sc_irq, sc->sc_ih); 532 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irqrid, sc->sc_irq); 533 bus_release_resource(dev, SYS_RES_MEMORY, sc->sc_memrid, sc->sc_memr); 534 mtx_destroy(&sc->sc_mutex); 535 536 return (bus_generic_detach(dev)); 537 } 538 539 static uint8_t 540 pmu_read_reg(struct pmu_softc *sc, u_int offset) 541 { 542 return (bus_read_1(sc->sc_memr, offset)); 543 } 544 545 static void 546 pmu_write_reg(struct pmu_softc *sc, u_int offset, uint8_t value) 547 { 548 bus_write_1(sc->sc_memr, offset, value); 549 } 550 551 static int 552 pmu_send_byte(struct pmu_softc *sc, uint8_t data) 553 { 554 555 pmu_out(sc); 556 pmu_write_reg(sc, vSR, data); 557 pmu_ack_off(sc); 558 /* wait for intr to come up */ 559 /* XXX should add a timeout and bail if it expires */ 560 do {} while (pmu_intr_state(sc) == 0); 561 pmu_ack_on(sc); 562 do {} while (pmu_intr_state(sc)); 563 pmu_ack_on(sc); 564 return 0; 565 } 566 567 static inline int 568 pmu_read_byte(struct pmu_softc *sc, uint8_t *data) 569 { 570 volatile uint8_t scratch; 571 pmu_in(sc); 572 scratch = pmu_read_reg(sc, vSR); 573 pmu_ack_off(sc); 574 /* wait for intr to come up */ 575 do {} while (pmu_intr_state(sc) == 0); 576 pmu_ack_on(sc); 577 do {} while (pmu_intr_state(sc)); 578 *data = pmu_read_reg(sc, vSR); 579 return 0; 580 } 581 582 static int 583 pmu_intr_state(struct pmu_softc *sc) 584 { 585 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0); 586 } 587 588 static int 589 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen, 590 uint8_t *out_msg) 591 { 592 struct pmu_softc *sc = cookie; 593 int i, rcv_len = -1; 594 uint8_t out_len, intreg; 595 596 intreg = pmu_read_reg(sc, vIER); 597 intreg &= 0x10; 598 pmu_write_reg(sc, vIER, intreg); 599 600 /* wait idle */ 601 do {} while (pmu_intr_state(sc)); 602 603 /* send command */ 604 pmu_send_byte(sc, cmd); 605 606 /* send length if necessary */ 607 if (pm_send_cmd_type[cmd] < 0) { 608 pmu_send_byte(sc, length); 609 } 610 611 for (i = 0; i < length; i++) { 612 pmu_send_byte(sc, in_msg[i]); 613 } 614 615 /* see if there's data to read */ 616 rcv_len = pm_receive_cmd_type[cmd]; 617 if (rcv_len == 0) 618 goto done; 619 620 /* read command */ 621 if (rcv_len == 1) { 622 pmu_read_byte(sc, out_msg); 623 goto done; 624 } else 625 out_msg[0] = cmd; 626 if (rcv_len < 0) { 627 pmu_read_byte(sc, &out_len); 628 rcv_len = out_len + 1; 629 } 630 for (i = 1; i < min(rcv_len, rlen); i++) 631 pmu_read_byte(sc, &out_msg[i]); 632 633 done: 634 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90); 635 636 return rcv_len; 637 } 638 639 640 static u_int 641 pmu_poll(device_t dev) 642 { 643 pmu_intr(dev); 644 return (0); 645 } 646 647 static void 648 pmu_in(struct pmu_softc *sc) 649 { 650 uint8_t reg; 651 652 reg = pmu_read_reg(sc, vACR); 653 reg &= ~vSR_OUT; 654 reg |= 0x0c; 655 pmu_write_reg(sc, vACR, reg); 656 } 657 658 static void 659 pmu_out(struct pmu_softc *sc) 660 { 661 uint8_t reg; 662 663 reg = pmu_read_reg(sc, vACR); 664 reg |= vSR_OUT; 665 reg |= 0x0c; 666 pmu_write_reg(sc, vACR, reg); 667 } 668 669 static void 670 pmu_ack_off(struct pmu_softc *sc) 671 { 672 uint8_t reg; 673 674 reg = pmu_read_reg(sc, vBufB); 675 reg &= ~vPB4; 676 pmu_write_reg(sc, vBufB, reg); 677 } 678 679 static void 680 pmu_ack_on(struct pmu_softc *sc) 681 { 682 uint8_t reg; 683 684 reg = pmu_read_reg(sc, vBufB); 685 reg |= vPB4; 686 pmu_write_reg(sc, vBufB, reg); 687 } 688 689 static void 690 pmu_intr(void *arg) 691 { 692 device_t dev; 693 struct pmu_softc *sc; 694 695 unsigned int len; 696 uint8_t resp[16]; 697 uint8_t junk[16]; 698 699 dev = (device_t)arg; 700 sc = device_get_softc(dev); 701 702 mtx_lock(&sc->sc_mutex); 703 704 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */ 705 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp); 706 707 mtx_unlock(&sc->sc_mutex); 708 709 if ((len < 1) || (resp[1] == 0)) { 710 return; 711 } 712 713 if (resp[1] & PMU_INT_ADB) { 714 /* 715 * the PMU will turn off autopolling after each command that 716 * it did not issue, so we assume any but TALK R0 is ours and 717 * re-enable autopoll here whenever we receive an ACK for a 718 * non TR0 command. 719 */ 720 mtx_lock(&sc->sc_mutex); 721 722 if ((resp[2] & 0x0f) != (ADB_COMMAND_TALK << 2)) { 723 if (sc->sc_autopoll) { 724 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, 725 (sc->sc_autopoll >> 8) & 0xff, 726 sc->sc_autopoll & 0xff}; 727 728 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, junk); 729 } 730 } 731 732 mtx_unlock(&sc->sc_mutex); 733 734 adb_receive_raw_packet(sc->adb_bus,resp[1],resp[2], 735 len - 3,&resp[3]); 736 } 737 if (resp[1] & PMU_INT_ENVIRONMENT) { 738 /* if the lid was just closed, notify devd. */ 739 if ((resp[2] & PMU_ENV_LID_CLOSED) && (!sc->lid_closed)) { 740 sc->lid_closed = 1; 741 devctl_notify("PMU", "lid", "close", NULL); 742 } 743 else if (!(resp[2] & PMU_ENV_LID_CLOSED) && (sc->lid_closed)) { 744 /* if the lid was just opened, notify devd. */ 745 sc->lid_closed = 0; 746 devctl_notify("PMU", "lid", "open", NULL); 747 } 748 if (resp[2] & PMU_ENV_POWER) 749 devctl_notify("PMU", "Button", "pressed", NULL); 750 } 751 } 752 753 static u_int 754 pmu_adb_send(device_t dev, u_char command_byte, int len, u_char *data, 755 u_char poll) 756 { 757 struct pmu_softc *sc = device_get_softc(dev); 758 int i,replen; 759 uint8_t packet[16], resp[16]; 760 761 /* construct an ADB command packet and send it */ 762 763 packet[0] = command_byte; 764 765 packet[1] = 0; 766 packet[2] = len; 767 for (i = 0; i < len; i++) 768 packet[i + 3] = data[i]; 769 770 mtx_lock(&sc->sc_mutex); 771 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp); 772 mtx_unlock(&sc->sc_mutex); 773 774 if (poll) 775 pmu_poll(dev); 776 777 return 0; 778 } 779 780 static u_int 781 pmu_adb_autopoll(device_t dev, uint16_t mask) 782 { 783 struct pmu_softc *sc = device_get_softc(dev); 784 785 /* magical incantation to re-enable autopolling */ 786 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (mask >> 8) & 0xff, mask & 0xff}; 787 uint8_t resp[16]; 788 789 mtx_lock(&sc->sc_mutex); 790 791 if (sc->sc_autopoll == mask) { 792 mtx_unlock(&sc->sc_mutex); 793 return 0; 794 } 795 796 sc->sc_autopoll = mask & 0xffff; 797 798 if (mask) 799 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp); 800 else 801 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp); 802 803 mtx_unlock(&sc->sc_mutex); 804 805 return 0; 806 } 807 808 static void 809 pmu_shutdown(void *xsc, int howto) 810 { 811 struct pmu_softc *sc = xsc; 812 uint8_t cmd[] = {'M', 'A', 'T', 'T'}; 813 814 if (howto & RB_HALT) 815 pmu_send(sc, PMU_POWER_OFF, 4, cmd, 0, NULL); 816 else 817 pmu_send(sc, PMU_RESET_CPU, 0, NULL, 0, NULL); 818 819 for (;;); 820 } 821 822 static void 823 pmu_set_sleepled(void *xsc, int onoff) 824 { 825 struct pmu_softc *sc = xsc; 826 uint8_t cmd[] = {4, 0, 0}; 827 828 cmd[2] = onoff; 829 830 mtx_lock(&sc->sc_mutex); 831 pmu_send(sc, PMU_SET_SLEEPLED, 3, cmd, 0, NULL); 832 mtx_unlock(&sc->sc_mutex); 833 } 834 835 static int 836 pmu_server_mode(SYSCTL_HANDLER_ARGS) 837 { 838 struct pmu_softc *sc = arg1; 839 840 u_int server_mode = 0; 841 uint8_t getcmd[] = {PMU_PWR_GET_POWERUP_EVENTS}; 842 uint8_t setcmd[] = {0, 0, PMU_PWR_WAKEUP_AC_INSERT}; 843 uint8_t resp[3]; 844 int error, len; 845 846 mtx_lock(&sc->sc_mutex); 847 len = pmu_send(sc, PMU_POWER_EVENTS, 1, getcmd, 3, resp); 848 mtx_unlock(&sc->sc_mutex); 849 850 if (len == 3) 851 server_mode = (resp[2] & PMU_PWR_WAKEUP_AC_INSERT) ? 1 : 0; 852 853 error = sysctl_handle_int(oidp, &server_mode, 0, req); 854 855 if (len != 3) 856 return (EINVAL); 857 858 if (error || !req->newptr) 859 return (error); 860 861 if (server_mode == 1) 862 setcmd[0] = PMU_PWR_SET_POWERUP_EVENTS; 863 else if (server_mode == 0) 864 setcmd[0] = PMU_PWR_CLR_POWERUP_EVENTS; 865 else 866 return (EINVAL); 867 868 setcmd[1] = resp[1]; 869 870 mtx_lock(&sc->sc_mutex); 871 pmu_send(sc, PMU_POWER_EVENTS, 3, setcmd, 2, resp); 872 mtx_unlock(&sc->sc_mutex); 873 874 return (0); 875 } 876 877 static int 878 pmu_query_battery(struct pmu_softc *sc, int batt, struct pmu_battstate *info) 879 { 880 uint8_t reg; 881 uint8_t resp[16]; 882 int len; 883 884 reg = batt + 1; 885 886 mtx_lock(&sc->sc_mutex); 887 len = pmu_send(sc, PMU_SMART_BATTERY_STATE, 1, ®, 16, resp); 888 mtx_unlock(&sc->sc_mutex); 889 890 if (len < 3) 891 return (-1); 892 893 /* All PMU battery info replies share a common header: 894 * Byte 1 Payload Format 895 * Byte 2 Battery Flags 896 */ 897 898 info->state = resp[2]; 899 900 switch (resp[1]) { 901 case 3: 902 case 4: 903 /* 904 * Formats 3 and 4 appear to be the same: 905 * Byte 3 Charge 906 * Byte 4 Max Charge 907 * Byte 5 Current 908 * Byte 6 Voltage 909 */ 910 911 info->charge = resp[3]; 912 info->maxcharge = resp[4]; 913 /* Current can be positive or negative */ 914 info->current = (int8_t)resp[5]; 915 info->voltage = resp[6]; 916 break; 917 case 5: 918 /* 919 * Formats 5 is a wider version of formats 3 and 4 920 * Byte 3-4 Charge 921 * Byte 5-6 Max Charge 922 * Byte 7-8 Current 923 * Byte 9-10 Voltage 924 */ 925 926 info->charge = (resp[3] << 8) | resp[4]; 927 info->maxcharge = (resp[5] << 8) | resp[6]; 928 /* Current can be positive or negative */ 929 info->current = (int16_t)((resp[7] << 8) | resp[8]); 930 info->voltage = (resp[9] << 8) | resp[10]; 931 break; 932 default: 933 device_printf(sc->sc_dev, "Unknown battery info format (%d)!\n", 934 resp[1]); 935 return (-1); 936 } 937 938 return (0); 939 } 940 941 static void 942 pmu_battery_notify(struct pmu_battstate *batt, struct pmu_battstate *old) 943 { 944 char notify_buf[16]; 945 int new_acline, old_acline; 946 947 new_acline = (batt->state & PMU_PWR_AC_PRESENT) ? 1 : 0; 948 old_acline = (old->state & PMU_PWR_AC_PRESENT) ? 1 : 0; 949 950 if (new_acline != old_acline) { 951 snprintf(notify_buf, sizeof(notify_buf), 952 "notify=0x%02x", new_acline); 953 devctl_notify("PMU", "POWER", "ACLINE", notify_buf); 954 } 955 } 956 957 static void 958 pmu_battquery_proc() 959 { 960 struct pmu_softc *sc; 961 struct pmu_battstate batt; 962 struct pmu_battstate cur_batt; 963 int error; 964 965 sc = device_get_softc(pmu); 966 967 bzero(&cur_batt, sizeof(cur_batt)); 968 while (1) { 969 kproc_suspend_check(curproc); 970 error = pmu_query_battery(sc, 0, &batt); 971 pmu_battery_notify(&batt, &cur_batt); 972 cur_batt = batt; 973 pause("pmu_batt", hz); 974 } 975 } 976 977 static int 978 pmu_battmon(SYSCTL_HANDLER_ARGS) 979 { 980 struct pmu_softc *sc; 981 int error, result; 982 983 sc = arg1; 984 result = pmu_battmon_enabled; 985 986 error = sysctl_handle_int(oidp, &result, 0, req); 987 988 if (error || !req->newptr) 989 return (error); 990 991 if (!result && pmu_battmon_enabled) 992 error = kproc_suspend(pmubattproc, hz); 993 else if (result && pmu_battmon_enabled == 0) 994 error = kproc_resume(pmubattproc); 995 pmu_battmon_enabled = (result != 0); 996 997 return (error); 998 } 999 1000 static int 1001 pmu_acline_state(SYSCTL_HANDLER_ARGS) 1002 { 1003 struct pmu_softc *sc; 1004 struct pmu_battstate batt; 1005 int error, result; 1006 1007 sc = arg1; 1008 1009 /* The PMU treats the AC line status as a property of the battery */ 1010 error = pmu_query_battery(sc, 0, &batt); 1011 1012 if (error != 0) 1013 return (error); 1014 1015 result = (batt.state & PMU_PWR_AC_PRESENT) ? 1 : 0; 1016 error = sysctl_handle_int(oidp, &result, 0, req); 1017 1018 return (error); 1019 } 1020 1021 static int 1022 pmu_battquery_sysctl(SYSCTL_HANDLER_ARGS) 1023 { 1024 struct pmu_softc *sc; 1025 struct pmu_battstate batt; 1026 int error, result; 1027 1028 sc = arg1; 1029 1030 error = pmu_query_battery(sc, arg2 & 0x00ff, &batt); 1031 1032 if (error != 0) 1033 return (error); 1034 1035 switch (arg2 & 0xff00) { 1036 case PMU_BATSYSCTL_PRESENT: 1037 result = (batt.state & PMU_PWR_BATT_PRESENT) ? 1 : 0; 1038 break; 1039 case PMU_BATSYSCTL_CHARGING: 1040 result = (batt.state & PMU_PWR_BATT_CHARGING) ? 1 : 0; 1041 break; 1042 case PMU_BATSYSCTL_CHARGE: 1043 result = batt.charge; 1044 break; 1045 case PMU_BATSYSCTL_MAXCHARGE: 1046 result = batt.maxcharge; 1047 break; 1048 case PMU_BATSYSCTL_CURRENT: 1049 result = batt.current; 1050 break; 1051 case PMU_BATSYSCTL_VOLTAGE: 1052 result = batt.voltage; 1053 break; 1054 case PMU_BATSYSCTL_TIME: 1055 /* Time remaining until full charge/discharge, in minutes */ 1056 1057 if (batt.current >= 0) 1058 result = (batt.maxcharge - batt.charge) /* mAh */ * 60 1059 / batt.current /* mA */; 1060 else 1061 result = (batt.charge /* mAh */ * 60) 1062 / (-batt.current /* mA */); 1063 break; 1064 case PMU_BATSYSCTL_LIFE: 1065 /* Battery charge fraction, in percent */ 1066 result = (batt.charge * 100) / batt.maxcharge; 1067 break; 1068 default: 1069 /* This should never happen */ 1070 result = -1; 1071 } 1072 1073 error = sysctl_handle_int(oidp, &result, 0, req); 1074 1075 return (error); 1076 } 1077 1078 #define DIFF19041970 2082844800 1079 1080 static int 1081 pmu_gettime(device_t dev, struct timespec *ts) 1082 { 1083 struct pmu_softc *sc = device_get_softc(dev); 1084 uint8_t resp[16]; 1085 uint32_t sec; 1086 1087 mtx_lock(&sc->sc_mutex); 1088 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp); 1089 mtx_unlock(&sc->sc_mutex); 1090 1091 memcpy(&sec, &resp[1], 4); 1092 ts->tv_sec = sec - DIFF19041970; 1093 ts->tv_nsec = 0; 1094 1095 return (0); 1096 } 1097 1098 static int 1099 pmu_settime(device_t dev, struct timespec *ts) 1100 { 1101 struct pmu_softc *sc = device_get_softc(dev); 1102 uint32_t sec; 1103 1104 sec = ts->tv_sec + DIFF19041970; 1105 1106 mtx_lock(&sc->sc_mutex); 1107 pmu_send(sc, PMU_SET_RTC, sizeof(sec), (uint8_t *)&sec, 0, NULL); 1108 mtx_unlock(&sc->sc_mutex); 1109 1110 return (0); 1111 } 1112 1113 int 1114 pmu_set_speed(int low_speed) 1115 { 1116 struct pmu_softc *sc; 1117 uint8_t sleepcmd[] = {'W', 'O', 'O', 'F', 0}; 1118 uint8_t resp[16]; 1119 1120 sc = device_get_softc(pmu); 1121 pmu_write_reg(sc, vIER, 0x10); 1122 spinlock_enter(); 1123 mtdec(0x7fffffff); 1124 mb(); 1125 mtdec(0x7fffffff); 1126 1127 sleepcmd[4] = low_speed; 1128 pmu_send(sc, PMU_CPU_SPEED, 5, sleepcmd, 16, resp); 1129 unin_chip_sleep(NULL, 1); 1130 platform_sleep(); 1131 unin_chip_wake(NULL); 1132 1133 mtdec(1); /* Force a decrementer exception */ 1134 spinlock_exit(); 1135 pmu_write_reg(sc, vIER, 0x90); 1136 1137 return (0); 1138 } 1139