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