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