1 /* $NetBSD: pmu.c,v 1.47 2025/09/21 19:13:48 thorpej Exp $ */ 2 3 /*- 4 * Copyright (c) 2006 Michael Lorenz 5 * All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 17 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 18 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 19 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 20 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 26 * POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29 #include <sys/cdefs.h> 30 __KERNEL_RCSID(0, "$NetBSD: pmu.c,v 1.47 2025/09/21 19:13:48 thorpej Exp $"); 31 32 #include <sys/param.h> 33 #include <sys/systm.h> 34 #include <sys/kernel.h> 35 #include <sys/device.h> 36 #include <sys/proc.h> 37 #include <sys/kthread.h> 38 #include <sys/atomic.h> 39 #include <sys/condvar.h> 40 #include <sys/mutex.h> 41 42 #include <sys/bus.h> 43 #include <machine/pio.h> 44 #include <machine/autoconf.h> 45 #include <dev/clock_subr.h> 46 #include <dev/i2c/i2cvar.h> 47 48 #include <dev/sysmon/sysmonvar.h> 49 50 #include <macppc/dev/viareg.h> 51 #include <macppc/dev/pmuvar.h> 52 #include <macppc/dev/batteryvar.h> 53 54 #include <dev/ofw/openfirm.h> 55 #include <dev/adb/adbvar.h> 56 #include "opt_pmu.h" 57 #include "nadb.h" 58 59 #ifdef PMU_DEBUG 60 #define DPRINTF printf 61 #else 62 #define DPRINTF while (0) printf 63 #endif 64 65 #define PMU_NOTREADY 0x1 /* has not been initialized yet */ 66 #define PMU_IDLE 0x2 /* the bus is currently idle */ 67 #define PMU_OUT 0x3 /* sending out a command */ 68 #define PMU_IN 0x4 /* receiving data */ 69 70 static void pmu_attach(device_t, device_t, void *); 71 static int pmu_match(device_t, cfdata_t, void *); 72 static void pmu_autopoll(void *, int); 73 74 static int pmu_intr(void *); 75 76 77 /* bits for sc_pending, as signals to the event thread */ 78 #define PMU_EV_CARD0 1 79 #define PMU_EV_CARD1 2 80 #define PMU_EV_BUTTON 4 81 #define PMU_EV_LID 8 82 83 struct pmu_softc { 84 device_t sc_dev; 85 void *sc_ih; 86 struct todr_chip_handle sc_todr; 87 struct adb_bus_accessops sc_adbops; 88 struct i2c_controller sc_i2c; 89 struct pmu_ops sc_pmu_ops; 90 struct sysmon_pswitch sc_lidswitch; 91 struct sysmon_pswitch sc_powerbutton; 92 bus_space_tag_t sc_memt; 93 bus_space_handle_t sc_memh; 94 uint32_t sc_flags; 95 #define PMU_HAS_BACKLIGHT_CONTROL 1 96 int sc_node; 97 int sc_error; 98 int sc_autopoll; 99 int sc_brightness, sc_brightness_wanted; 100 int sc_volume, sc_volume_wanted; 101 int sc_lid_closed; 102 int sc_is_rackmac; 103 int sc_button; 104 uint8_t sc_env_old; 105 uint8_t sc_env_mask; 106 /* deferred processing */ 107 lwp_t *sc_thread; 108 int sc_pending; 109 /* signalling the event thread */ 110 kcondvar_t sc_event; 111 kmutex_t sc_evmtx; 112 /* ADB */ 113 void (*sc_adb_handler)(void *, int, uint8_t *); 114 void *sc_adb_cookie; 115 void (*sc_callback)(void *); 116 void *sc_cb_cookie; 117 }; 118 119 CFATTACH_DECL_NEW(pmu, sizeof(struct pmu_softc), 120 pmu_match, pmu_attach, NULL, NULL); 121 122 static inline void pmu_write_reg(struct pmu_softc *, int, uint8_t); 123 static inline uint8_t pmu_read_reg(struct pmu_softc *, int); 124 static void pmu_in(struct pmu_softc *); 125 static void pmu_out(struct pmu_softc *); 126 static void pmu_ack_off(struct pmu_softc *); 127 static void pmu_ack_on(struct pmu_softc *); 128 static int pmu_intr_state(struct pmu_softc *); 129 130 static void pmu_init(struct pmu_softc *); 131 static void pmu_thread(void *); 132 static void pmu_eject_card(struct pmu_softc *, int); 133 static void pmu_update_brightness(struct pmu_softc *); 134 static void pmu_register_callback(void *, void (*)(void *), void *); 135 /* 136 * send a message to the PMU. 137 */ 138 static int pmu_send(void *, int, int, uint8_t *, int, uint8_t *); 139 static void pmu_adb_poll(void *); 140 static int pmu_todr_set(todr_chip_handle_t, struct timeval *); 141 static int pmu_todr_get(todr_chip_handle_t, struct timeval *); 142 143 static int pmu_adb_handler(void *, int, uint8_t *); 144 145 static struct pmu_softc *pmu0 = NULL; 146 147 /* ADB bus attachment stuff */ 148 static int pmu_adb_send(void *, int, int, int, uint8_t *); 149 static int pmu_adb_set_handler(void *, void (*)(void *, int, uint8_t *), void *); 150 151 /* i2c stuff */ 152 static int pmu_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t, 153 void *, size_t, int); 154 155 static void pmu_attach_legacy_battery(struct pmu_softc *); 156 static void pmu_attach_smart_battery(struct pmu_softc *, int); 157 static int pmu_print(void *, const char *); 158 159 /* these values shows that number of data returned after 'send' cmd is sent */ 160 static signed char pm_send_cmd_type[] = { 161 -1, -1, -1, -1, -1, -1, -1, -1, 162 -1, -1, -1, -1, -1, -1, -1, -1, 163 0x01, 0x01, -1, -1, -1, -1, -1, -1, 164 0x00, 0x00, -1, -1, -1, -1, -1, 0x00, 165 -1, 0x00, 0x02, 0x01, 0x01, -1, -1, -1, 166 0x00, -1, -1, -1, -1, -1, -1, -1, 167 0x04, 0x14, -1, 0x03, -1, -1, -1, -1, 168 0x00, 0x00, 0x02, 0x02, -1, -1, -1, -1, 169 0x01, 0x01, -1, -1, -1, -1, -1, -1, 170 0x00, 0x00, -1, -1, 0x01, -1, -1, -1, 171 0x01, 0x00, 0x02, 0x02, -1, 0x01, 0x03, 0x01, 172 0x00, 0x01, 0x00, 0x00, 0x00, -1, -1, -1, 173 0x02, -1, -1, -1, -1, -1, -1, -1, 174 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, -1, -1, 175 0x01, 0x01, 0x01, -1, -1, -1, -1, -1, 176 0x00, 0x00, -1, -1, -1, -1, 0x04, 0x04, 177 0x04, -1, 0x00, -1, -1, -1, -1, -1, 178 0x00, -1, -1, -1, -1, -1, -1, -1, 179 0x01, 0x02, -1, -1, -1, -1, -1, -1, 180 0x00, 0x00, -1, -1, -1, -1, -1, -1, 181 0x02, 0x02, 0x02, 0x04, -1, 0x00, -1, -1, 182 0x01, 0x01, 0x03, 0x02, -1, -1, -1, -1, 183 -1, -1, -1, -1, -1, -1, -1, -1, 184 -1, -1, -1, -1, -1, -1, -1, -1, 185 -1, -1, -1, -1, -1, -1, -1, -1, 186 -1, -1, -1, -1, -1, -1, -1, -1, 187 0x00, -1, -1, -1, -1, -1, -1, -1, 188 0x01, 0x01, -1, -1, 0x00, 0x00, -1, -1, 189 -1, 0x04, 0x00, -1, -1, -1, -1, -1, 190 0x03, -1, 0x00, -1, 0x00, -1, -1, 0x00, 191 -1, -1, -1, -1, -1, -1, -1, -1, 192 -1, -1, -1, -1, -1, -1, -1, -1 193 }; 194 195 /* these values shows that number of data returned after 'receive' cmd is sent */ 196 static signed char pm_receive_cmd_type[] = { 197 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 198 -1, -1, -1, -1, -1, -1, -1, -1, 199 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 200 0x02, 0x02, -1, -1, -1, -1, -1, 0x00, 201 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 202 -1, -1, -1, -1, -1, -1, -1, -1, 203 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 204 0x05, 0x15, -1, 0x02, -1, -1, -1, -1, 205 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 206 0x02, 0x02, -1, -1, -1, -1, -1, -1, 207 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 208 0x02, 0x00, 0x03, 0x03, -1, -1, -1, -1, 209 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 210 0x04, 0x04, 0x03, 0x09, -1, -1, -1, -1, 211 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 212 -1, -1, -1, -1, -1, -1, 0x01, 0x01, 213 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 214 0x06, -1, -1, -1, -1, -1, -1, -1, 215 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 216 0x02, 0x02, -1, -1, -1, -1, -1, -1, 217 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 218 0x02, 0x00, 0x00, 0x00, -1, -1, -1, -1, 219 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 220 -1, -1, -1, -1, -1, -1, -1, -1, 221 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 222 -1, -1, -1, -1, -1, -1, -1, -1, 223 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 224 0x02, 0x02, -1, -1, 0x02, -1, -1, -1, 225 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 226 -1, -1, 0x02, -1, -1, -1, -1, 0x00, 227 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 228 -1, -1, -1, -1, -1, -1, -1, -1, 229 }; 230 231 static const char *has_legacy_battery[] = { 232 "AAPL,3500", 233 "AAPL,3400/2400", 234 NULL }; 235 236 static const char *has_two_smart_batteries[] = { 237 "AAPL,PowerBook1998", 238 "PowerBook1,1", 239 NULL }; 240 241 static int 242 pmu_match(device_t parent, cfdata_t cf, void *aux) 243 { 244 struct confargs *ca = aux; 245 246 if (ca->ca_nreg < 8) 247 return 0; 248 249 if (ca->ca_nintr < 4) 250 return 0; 251 252 if (strcmp(ca->ca_name, "via-pmu") == 0) { 253 return 10; 254 } 255 256 return 0; 257 } 258 259 static void 260 pmu_attach(device_t parent, device_t self, void *aux) 261 { 262 struct confargs *ca = aux; 263 struct pmu_softc *sc = device_private(self); 264 uint32_t regs[16]; 265 int irq = ca->ca_intr[0]; 266 int node, extint_node, root_node; 267 int nbat = 1, i, pmnode; 268 int type = IST_EDGE; 269 uint8_t cmd[2] = {2, 0}; 270 uint8_t resp[16]; 271 char name[256], model[32]; 272 273 extint_node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1"); 274 if (extint_node) { 275 276 OF_getprop(extint_node, "interrupts", &irq, 4); 277 type = IST_LEVEL; 278 } 279 280 aprint_normal(" irq %d: ", irq); 281 282 sc->sc_dev = self; 283 sc->sc_node = ca->ca_node; 284 sc->sc_memt = ca->ca_tag; 285 286 root_node = OF_finddevice("/"); 287 288 sc->sc_error = 0; 289 sc->sc_autopoll = 0; 290 sc->sc_pending = 0; 291 sc->sc_env_old = 0; 292 sc->sc_brightness = sc->sc_brightness_wanted = 0x80; 293 sc->sc_volume = sc->sc_volume_wanted = 0x80; 294 sc->sc_flags = 0; 295 sc->sc_callback = NULL; 296 sc->sc_lid_closed = 0; 297 sc->sc_button = 0; 298 sc->sc_env_mask = 0xff; 299 sc->sc_is_rackmac = 0; 300 301 cv_init(&sc->sc_event, "pmu_event"); 302 mutex_init(&sc->sc_evmtx, MUTEX_DEFAULT, IPL_NONE); 303 304 /* 305 * core99 PowerMacs like to send environment messages with the lid 306 * switch bit set - since that doesn't make any sense here and it 307 * probably means something else anyway we mask it out 308 */ 309 310 if (OF_getprop(root_node, "model", model, 32) != 0) { 311 if (strncmp(model, "PowerMac", 8) == 0) { 312 sc->sc_env_mask = PMU_ENV_POWER_BUTTON; 313 } 314 if (strncmp(model, "RackMac1", 8) == 0) { 315 sc->sc_is_rackmac = 1; 316 } 317 } 318 319 if (bus_space_map(sc->sc_memt, ca->ca_reg[0] + ca->ca_baseaddr, 320 ca->ca_reg[1], 0, &sc->sc_memh) != 0) { 321 aprint_error_dev(self, "unable to map registers\n"); 322 return; 323 } 324 sc->sc_ih = intr_establish_xname(irq, type, IPL_TTY, pmu_intr, sc, 325 device_xname(self)); 326 327 pmu_init(sc); 328 329 sc->sc_pmu_ops.cookie = sc; 330 sc->sc_pmu_ops.do_command = pmu_send; 331 sc->sc_pmu_ops.register_callback = pmu_register_callback; 332 333 if (pmu0 == NULL) 334 pmu0 = sc; 335 336 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp); 337 338 /* check what kind of PMU we're talking to */ 339 if (pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp) > 1) 340 aprint_normal(" rev. %d", resp[1]); 341 aprint_normal("\n"); 342 343 node = OF_child(sc->sc_node); 344 345 for (node = OF_child(sc->sc_node); node != 0; node = OF_peer(node)) { 346 if (OF_getprop(node, "name", name, 256) <= 0) { 347 continue; 348 } 349 if (strncmp(name, "pmu-i2c", 8) == 0) { 350 aprint_normal_dev(self, "initializing IIC bus\n"); 351 iic_tag_init(&sc->sc_i2c); 352 sc->sc_i2c.ic_cookie = sc; 353 sc->sc_i2c.ic_exec = pmu_i2c_exec; 354 iicbus_attach_with_devhandle(self, &sc->sc_i2c, 355 devhandle_from_of(device_handle(self), node)); 356 continue; 357 } 358 if (strncmp(name, "adb", 4) == 0) { 359 aprint_normal_dev(self, "initializing ADB\n"); 360 sc->sc_adbops.cookie = sc; 361 sc->sc_adbops.send = pmu_adb_send; 362 sc->sc_adbops.poll = pmu_adb_poll; 363 sc->sc_adbops.autopoll = pmu_autopoll; 364 sc->sc_adbops.set_handler = pmu_adb_set_handler; 365 #if NNADB > 0 366 config_found(self, &sc->sc_adbops, nadb_print, 367 CFARGS(.iattr = "adb_bus")); 368 #endif 369 continue; 370 } 371 if (strncmp(name, "rtc", 4) == 0) { 372 aprint_normal_dev(self, "initializing RTC\n"); 373 sc->sc_todr.todr_gettime = pmu_todr_get; 374 sc->sc_todr.todr_settime = pmu_todr_set; 375 sc->sc_todr.todr_dev = self; 376 todr_attach(&sc->sc_todr); 377 continue; 378 } 379 if (strncmp(name, "battery", 8) == 0) { 380 continue; 381 } 382 aprint_normal_dev(self, "%s not configured\n", name); 383 } 384 385 if (OF_finddevice("/bandit/ohare") != -1) { 386 aprint_normal_dev(self, "enabling ohare backlight control\n"); 387 sc->sc_flags |= PMU_HAS_BACKLIGHT_CONTROL; 388 cmd[0] = 0; 389 cmd[1] = 0; 390 memset(resp, 0, 6); 391 if (pmu_send(sc, PMU_READ_BRIGHTNESS, 1, cmd, 16, resp) > 1) { 392 sc->sc_brightness_wanted = resp[1]; 393 pmu_update_brightness(sc); 394 } 395 } 396 397 /* attach batteries */ 398 if (of_compatible(root_node, has_legacy_battery)) { 399 400 pmu_attach_legacy_battery(sc); 401 } else if (of_compatible(root_node, has_two_smart_batteries)) { 402 403 pmu_attach_smart_battery(sc, 0); 404 pmu_attach_smart_battery(sc, 1); 405 } else { 406 407 /* check how many batteries we have */ 408 pmnode = of_getnode_byname(ca->ca_node, "power-mgt"); 409 if (pmnode == -1) 410 goto bat_done; 411 if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24) 412 goto bat_done; 413 nbat = regs[6] >> 16; 414 for (i = 0; i < nbat; i++) 415 pmu_attach_smart_battery(sc, i); 416 } 417 bat_done: 418 419 if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread, 420 "%s", "pmu") != 0) { 421 aprint_error_dev(self, "unable to create event kthread\n"); 422 } 423 424 sc->sc_lidswitch.smpsw_name = "Lid switch"; 425 sc->sc_lidswitch.smpsw_type = PSWITCH_TYPE_LID; 426 if (sysmon_pswitch_register(&sc->sc_lidswitch) != 0) 427 aprint_error_dev(self, 428 "unable to register lid switch with sysmon\n"); 429 430 sc->sc_powerbutton.smpsw_name = "Power button"; 431 sc->sc_powerbutton.smpsw_type = PSWITCH_TYPE_POWER; 432 if (sysmon_pswitch_register(&sc->sc_powerbutton) != 0) 433 aprint_error_dev(self, 434 "unable to register power button with sysmon\n"); 435 } 436 437 static void 438 pmu_register_callback(void *pmu_cookie, void (*cb)(void *), void *cookie) 439 { 440 struct pmu_softc *sc = pmu_cookie; 441 442 sc->sc_callback = cb; 443 sc->sc_cb_cookie = cookie; 444 } 445 446 static void 447 pmu_init(struct pmu_softc *sc) 448 { 449 uint8_t pmu_imask, resp[16]; 450 451 pmu_imask = 452 PMU_INT_PCEJECT | PMU_INT_SNDBRT | PMU_INT_ADB/* | PMU_INT_TICK*/; 453 pmu_imask |= PMU_INT_BATTERY; 454 pmu_imask |= PMU_INT_ENVIRONMENT; 455 pmu_send(sc, PMU_SET_IMASK, 1, &pmu_imask, 16, resp); 456 457 pmu_write_reg(sc, vIER, 0x90); /* enable VIA interrupts */ 458 } 459 460 static inline void 461 pmu_write_reg(struct pmu_softc *sc, int offset, uint8_t value) 462 { 463 464 bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value); 465 } 466 467 static inline uint8_t 468 pmu_read_reg(struct pmu_softc *sc, int offset) 469 { 470 471 return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset); 472 } 473 474 static inline int 475 pmu_send_byte(struct pmu_softc *sc, uint8_t data) 476 { 477 478 pmu_out(sc); 479 pmu_write_reg(sc, vSR, data); 480 pmu_ack_off(sc); 481 /* wait for intr to come up */ 482 /* XXX should add a timeout and bail if it expires */ 483 do {} while (pmu_intr_state(sc) == 0); 484 pmu_ack_on(sc); 485 do {} while (pmu_intr_state(sc)); 486 pmu_ack_on(sc); 487 DPRINTF(" %02x>", data); 488 return 0; 489 } 490 491 static inline int 492 pmu_read_byte(struct pmu_softc *sc, uint8_t *data) 493 { 494 volatile uint8_t junk; 495 pmu_in(sc); 496 junk = pmu_read_reg(sc, vSR); 497 __USE(junk); 498 pmu_ack_off(sc); 499 /* wait for intr to come up */ 500 do {} while (pmu_intr_state(sc) == 0); 501 pmu_ack_on(sc); 502 do {} while (pmu_intr_state(sc)); 503 *data = pmu_read_reg(sc, vSR); 504 DPRINTF(" <%02x", *data); 505 return 0; 506 } 507 508 static int 509 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen, 510 uint8_t *out_msg) 511 { 512 struct pmu_softc *sc = cookie; 513 int i, rcv_len = -1, s; 514 uint8_t out_len, intreg; 515 516 DPRINTF("pmu_send: "); 517 518 s = splhigh(); 519 intreg = pmu_read_reg(sc, vIER); 520 intreg &= 0x10; 521 pmu_write_reg(sc, vIER, intreg); 522 523 /* wait idle */ 524 do {} while (pmu_intr_state(sc)); 525 sc->sc_error = 0; 526 527 /* send command */ 528 pmu_send_byte(sc, cmd); 529 530 /* send length if necessary */ 531 if (pm_send_cmd_type[cmd] < 0) { 532 pmu_send_byte(sc, length); 533 } 534 535 for (i = 0; i < length; i++) { 536 pmu_send_byte(sc, in_msg[i]); 537 DPRINTF(" next "); 538 } 539 DPRINTF("done sending\n"); 540 541 /* see if there's data to read */ 542 rcv_len = pm_receive_cmd_type[cmd]; 543 if (rcv_len == 0) 544 goto done; 545 546 /* read command */ 547 if (rcv_len == 1) { 548 pmu_read_byte(sc, out_msg); 549 goto done; 550 } else 551 out_msg[0] = cmd; 552 if (rcv_len < 0) { 553 pmu_read_byte(sc, &out_len); 554 rcv_len = out_len + 1; 555 } 556 for (i = 1; i < uimin(rcv_len, rlen); i++) 557 pmu_read_byte(sc, &out_msg[i]); 558 559 done: 560 DPRINTF("\n"); 561 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90); 562 splx(s); 563 564 return rcv_len; 565 } 566 567 static void 568 pmu_adb_poll(void *cookie) 569 { 570 struct pmu_softc *sc = cookie; 571 int s; 572 573 s = spltty(); 574 pmu_intr(sc); 575 splx(s); 576 } 577 578 static void 579 pmu_in(struct pmu_softc *sc) 580 { 581 volatile uint8_t reg; 582 583 reg = pmu_read_reg(sc, vACR); 584 reg &= ~vSR_OUT; 585 reg |= 0x0c; 586 pmu_write_reg(sc, vACR, reg); 587 } 588 589 static void 590 pmu_out(struct pmu_softc *sc) 591 { 592 volatile uint8_t reg; 593 594 reg = pmu_read_reg(sc, vACR); 595 reg |= vSR_OUT; 596 reg |= 0x0c; 597 pmu_write_reg(sc, vACR, reg); 598 } 599 600 static void 601 pmu_ack_off(struct pmu_softc *sc) 602 { 603 volatile uint8_t reg; 604 605 reg = pmu_read_reg(sc, vBufB); 606 reg &= ~vPB4; 607 pmu_write_reg(sc, vBufB, reg); 608 } 609 610 static void 611 pmu_ack_on(struct pmu_softc *sc) 612 { 613 volatile uint8_t reg; 614 615 reg = pmu_read_reg(sc, vBufB); 616 reg |= vPB4; 617 pmu_write_reg(sc, vBufB, reg); 618 } 619 620 static int 621 pmu_intr_state(struct pmu_softc *sc) 622 { 623 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0); 624 } 625 626 static int 627 pmu_intr(void *arg) 628 { 629 struct pmu_softc *sc = arg; 630 unsigned int len, i; 631 uint8_t resp[16]; 632 633 DPRINTF(":"); 634 635 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */ 636 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp); 637 if ((len < 1) || (resp[1] == 0)) 638 goto done; 639 #ifdef PMU_DEBUG 640 { 641 DPRINTF("intr: %02x", resp[0]); 642 for (i = 1; i < len; i++) 643 DPRINTF(" %02x", resp[i]); 644 DPRINTF("\n"); 645 } 646 #endif 647 if (resp[1] & PMU_INT_ADB) { 648 pmu_adb_handler(sc, len - 1, &resp[1]); 649 goto done; 650 } 651 if (resp[1] & PMU_INT_SNDBRT) { 652 /* deal with the brightness / volume control buttons */ 653 DPRINTF("brightness: %d volume %d\n", resp[2], resp[3]); 654 sc->sc_brightness_wanted = resp[2]; 655 sc->sc_volume_wanted = resp[3]; 656 cv_signal(&sc->sc_event); 657 goto done; 658 } 659 if (resp[1] & PMU_INT_PCEJECT) { 660 /* deal with PCMCIA eject buttons */ 661 DPRINTF("card eject %d\n", resp[3]); 662 atomic_or_32(&sc->sc_pending, (resp[3] & 3)); 663 cv_signal(&sc->sc_event); 664 goto done; 665 } 666 if (resp[1] & PMU_INT_BATTERY) { 667 /* deal with battery messages */ 668 printf("battery:"); 669 for (i = 2; i < len; i++) 670 printf(" %02x", resp[i]); 671 printf("\n"); 672 goto done; 673 } 674 if (resp[1] & PMU_INT_ENVIRONMENT) { 675 uint8_t diff; 676 #ifdef PMU_DEBUG 677 /* deal with environment messages */ 678 printf("environment:"); 679 for (i = 2; i < len; i++) 680 printf(" %02x", resp[i]); 681 printf("\n"); 682 #endif 683 diff = (resp[2] ^ sc->sc_env_old ) & sc->sc_env_mask; 684 if (diff == 0) goto done; 685 sc->sc_env_old = resp[2]; 686 if (diff & PMU_ENV_LID_CLOSED) { 687 sc->sc_lid_closed = (resp[2] & PMU_ENV_LID_CLOSED) != 0; 688 atomic_or_32(&sc->sc_pending, PMU_EV_LID); 689 cv_signal(&sc->sc_event); 690 } 691 if (diff & PMU_ENV_POWER_BUTTON) { 692 sc->sc_button = (resp[2] & PMU_ENV_POWER_BUTTON) != 0; 693 atomic_or_32(&sc->sc_pending, PMU_EV_BUTTON); 694 cv_signal(&sc->sc_event); 695 } 696 goto done; 697 } 698 if (resp[1] & PMU_INT_TICK) { 699 /* don't bother */ 700 goto done; 701 } 702 703 /* unknown interrupt code?! */ 704 #ifdef PMU_DEBUG 705 printf("pmu intr: %02x:", resp[1]); 706 for (i = 2; i < len; i++) 707 printf(" %02x", resp[i]); 708 printf("\n"); 709 #endif 710 done: 711 return 1; 712 } 713 714 #define DIFF19041970 2082844800 715 716 static int 717 pmu_todr_get(todr_chip_handle_t tch, struct timeval *tvp) 718 { 719 struct pmu_softc *sc = device_private(tch->todr_dev); 720 uint32_t sec; 721 int count = 10; 722 int ok = FALSE; 723 uint8_t resp[16]; 724 725 DPRINTF("pmu_todr_get\n"); 726 while ((count > 0) && (!ok)) { 727 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp); 728 729 memcpy(&sec, &resp[1], 4); 730 tvp->tv_sec = sec - DIFF19041970; 731 ok = (sec > DIFF19041970) && (sec < 0xf0000000); 732 if (!ok) aprint_error_dev(sc->sc_dev, 733 "got garbage from rtc (%08x)\n", sec); 734 count--; 735 } 736 if (count == 0) { 737 aprint_error_dev(sc->sc_dev, 738 "unable to get a sane time value\n"); 739 tvp->tv_sec = 0; 740 } 741 DPRINTF("tod: %" PRIo64 "\n", tvp->tv_sec); 742 tvp->tv_usec = 0; 743 return 0; 744 } 745 746 static int 747 pmu_todr_set(todr_chip_handle_t tch, struct timeval *tvp) 748 { 749 struct pmu_softc *sc = device_private(tch->todr_dev); 750 uint32_t sec; 751 uint8_t resp[16]; 752 753 sec = tvp->tv_sec + DIFF19041970; 754 if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0) 755 return 0; 756 return -1; 757 } 758 759 void 760 pmu_poweroff(void) 761 { 762 struct pmu_softc *sc; 763 uint8_t cmd[] = {'M', 'A', 'T', 'T'}; 764 uint8_t resp[16]; 765 766 if (pmu0 == NULL) 767 return; 768 sc = pmu0; 769 if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0) 770 while (1); 771 } 772 773 void 774 pmu_restart(void) 775 { 776 struct pmu_softc *sc; 777 uint8_t resp[16]; 778 779 if (pmu0 == NULL) 780 return; 781 sc = pmu0; 782 if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0) 783 while (1); 784 } 785 786 void 787 pmu_modem(int on) 788 { 789 struct pmu_softc *sc; 790 uint8_t resp[16], cmd[2] = {0, 0}; 791 792 if (pmu0 == NULL) 793 return; 794 795 sc = pmu0; 796 cmd[0] = PMU_POW0_MODEM | (on ? PMU_POW0_ON : 0); 797 pmu_send(sc, PMU_POWER_CTRL0, 1, cmd, 16, resp); 798 } 799 800 static void 801 pmu_autopoll(void *cookie, int flag) 802 { 803 struct pmu_softc *sc = cookie; 804 /* magical incantation to re-enable autopolling */ 805 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff}; 806 uint8_t resp[16]; 807 808 if (sc->sc_autopoll == flag) 809 return; 810 811 if (flag) { 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 sc->sc_autopoll = flag & 0xffff; 817 } 818 819 static int 820 pmu_adb_handler(void *cookie, int len, uint8_t *data) 821 { 822 struct pmu_softc *sc = cookie; 823 uint8_t resp[16]; 824 825 if (sc->sc_adb_handler != NULL) { 826 sc->sc_adb_handler(sc->sc_adb_cookie, len, data); 827 /* 828 * the PMU will turn off autopolling after each LISTEN so we 829 * need to re-enable it here whenever we receive an ACK for a 830 * LISTEN command 831 */ 832 if ((data[1] & 0x0c) == 0x08) { 833 uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff, 834 sc->sc_autopoll & 0xff}; 835 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp); 836 } 837 return 0; 838 } 839 return -1; 840 } 841 842 static int 843 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data) 844 { 845 struct pmu_softc *sc = cookie; 846 int i; 847 uint8_t packet[16], resp[16]; 848 849 /* construct an ADB command packet and send it */ 850 packet[0] = command; 851 packet[1] = 0; 852 packet[2] = len; 853 for (i = 0; i < len; i++) 854 packet[i + 3] = data[i]; 855 (void)pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp); 856 857 return 0; 858 } 859 860 static int 861 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *), 862 void *hcookie) 863 { 864 struct pmu_softc *sc = cookie; 865 866 /* register a callback for incoming ADB messages */ 867 sc->sc_adb_handler = handler; 868 sc->sc_adb_cookie = hcookie; 869 return 0; 870 } 871 872 static int 873 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send, 874 size_t send_len, void *_recv, size_t recv_len, int flags) 875 { 876 struct pmu_softc *sc = cookie; 877 const uint8_t *send = _send; 878 const uint8_t *recv = _recv; 879 uint8_t command[64] = {1, /* bus number */ 880 PMU_I2C_MODE_SIMPLE, 881 0, /* bus2 */ 882 addr, 883 0, /* sub address */ 884 0, /* comb address */ 885 0, /* count */ 886 0 /* data */ 887 }; 888 uint8_t resp[16]; 889 int len, rw; 890 891 rw = (addr & 0x7f) << 1; 892 command[3] = rw; 893 /* see if we're supposed to read */ 894 if (I2C_OP_READ_P(op)) { 895 if (send_len == 1) { 896 command[1] = PMU_I2C_MODE_COMBINED; 897 command[4] = *send; 898 command[5] = rw | 1; 899 command[6] = recv_len; 900 if (sc->sc_is_rackmac) delay(20000); 901 len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp); 902 DPRINTF("resp1(%d): %2x %2x ", len, resp[0], resp[1]); 903 command[0] = 0; 904 int ok = 0; 905 int bail = 10; 906 while ((!ok) && (bail > 0)) { 907 len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp); 908 if (resp[1] != 0xfe) { 909 ok = 1; 910 } else delay(10000); 911 bail--; 912 } 913 #ifdef PMU_DEBUG 914 if (bail < 9) printf("bail %d\n", bail); 915 #endif 916 } else { 917 command[6] = send_len; 918 memcpy(&command[7], send, send_len); 919 /* 920 * Xserve G4's PMU needs these, if we send i2c commands back to 921 * back we get busy responses. 922 * No such problem on Minis, which have temperature sensors 923 * here. 924 */ 925 if (sc->sc_is_rackmac) delay(20000); 926 len = pmu_send(sc, PMU_I2C_CMD, send_len + 7, command, 16, resp); 927 DPRINTF("resp1(%d): %2x %2x ", len, resp[0], resp[1]); 928 rw |= 1; 929 command[3] = rw; 930 command[6] = recv_len; 931 if (sc->sc_is_rackmac) delay(20000); 932 len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp); 933 DPRINTF("resp2(%d): %2x %2x ", len, resp[0], resp[1]); 934 command[0] = 0; 935 int ok = 0; 936 int bail = 10; 937 while ((!ok) && (bail > 0)) { 938 len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp); 939 if (resp[1] != 0xfe) { 940 ok = 1; 941 } else delay(10000); 942 bail--; 943 } 944 #ifdef PMU_DEBUG 945 if (bail < 9) printf("bail %d\n", bail); 946 #endif 947 } 948 DPRINTF("resp3(%d): %2x %2x %2x ", len, resp[0], resp[1], 949 resp[2]); 950 if ((len - 2) != recv_len) { 951 DPRINTF("%s: %s(%d) - got %d\n", 952 device_xname(sc->sc_dev), 953 __func__, recv_len, len - 2); 954 return -1; 955 } 956 memcpy(_recv, &resp[2], len - 2); 957 return 0; 958 } else { 959 int clen = send_len + recv_len; 960 uint8_t *dptr = &command[7]; 961 if (clen > 57) return -1; 962 command[6] = clen; 963 memcpy(dptr, send, send_len); 964 if (recv_len > 0) { 965 dptr += send_len; 966 memcpy(dptr, recv, recv_len); 967 } 968 len = clen + 7; 969 DPRINTF("pmu_i2c_exec(%02x, %d)\n", addr, send_len); 970 971 if (sc->sc_is_rackmac) delay(20000); 972 len = pmu_send(sc, PMU_I2C_CMD, len, command, 16, resp); 973 DPRINTF("resp(%d): %2x %2x\n", len, resp[0], resp[1]); 974 975 if (resp[1] != PMU_I2C_STATUS_OK) { 976 DPRINTF("%s: iic error %d\n", __func__, resp[1]); 977 return -1; 978 } 979 } 980 return 0; 981 } 982 983 static void 984 pmu_eject_card(struct pmu_softc *sc, int socket) 985 { 986 uint8_t buf[] = {socket | 4}; 987 uint8_t res[4]; 988 989 atomic_and_32(&sc->sc_pending, ~socket); 990 pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res); 991 } 992 993 static void 994 pmu_update_brightness(struct pmu_softc *sc) 995 { 996 int val; 997 uint8_t cmd[2], resp[16]; 998 999 if (sc->sc_brightness == sc->sc_brightness_wanted) 1000 return; 1001 1002 if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) { 1003 1004 aprint_normal_dev(sc->sc_dev, 1005 "this PMU doesn't support backlight control\n"); 1006 sc->sc_brightness = sc->sc_brightness_wanted; 1007 return; 1008 } 1009 1010 if (sc->sc_brightness_wanted == 0) { 1011 1012 /* turn backlight off completely */ 1013 cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT; 1014 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp); 1015 sc->sc_brightness = sc->sc_brightness_wanted; 1016 1017 /* don't bother with brightness */ 1018 return; 1019 } 1020 1021 /* turn backlight on if needed */ 1022 if (sc->sc_brightness == 0) { 1023 cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT; 1024 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp); 1025 } 1026 1027 DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness, 1028 sc->sc_brightness_wanted); 1029 1030 val = 0x7f - (sc->sc_brightness_wanted >> 1); 1031 if (val < 0x08) 1032 val = 0x08; 1033 if (val > 0x78) 1034 val = 0x78; 1035 cmd[0] = val; 1036 pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp); 1037 1038 sc->sc_brightness = sc->sc_brightness_wanted; 1039 } 1040 1041 static void 1042 pmu_thread(void *cookie) 1043 { 1044 struct pmu_softc *sc = cookie; 1045 //time_t time_bat = time_second; 1046 int ticks = hz, i; 1047 1048 while (1) { 1049 mutex_enter(&sc->sc_evmtx); 1050 cv_timedwait(&sc->sc_event, &sc->sc_evmtx, ticks); 1051 if ((sc->sc_pending & 3) != 0) { 1052 DPRINTF("eject %d\n", sc->sc_pending & 3); 1053 for (i = 1; i < 3; i++) { 1054 if (i & sc->sc_pending) 1055 pmu_eject_card(sc, i); 1056 } 1057 } 1058 mutex_exit(&sc->sc_evmtx); 1059 /* see if we need to update brightness */ 1060 if (sc->sc_brightness_wanted != sc->sc_brightness) { 1061 pmu_update_brightness(sc); 1062 } 1063 1064 /* see if we need to update audio volume */ 1065 if (sc->sc_volume_wanted != sc->sc_volume) { 1066 #if 0 1067 set_volume(sc->sc_volume_wanted); 1068 #endif 1069 sc->sc_volume = sc->sc_volume_wanted; 1070 } 1071 1072 if (sc->sc_pending & PMU_EV_LID) { 1073 atomic_and_32(&sc->sc_pending, ~PMU_EV_LID); 1074 sysmon_pswitch_event(&sc->sc_lidswitch, 1075 sc->sc_lid_closed ? PSWITCH_EVENT_PRESSED : 1076 PSWITCH_EVENT_RELEASED); 1077 } 1078 1079 if (sc->sc_pending & PMU_EV_BUTTON) { 1080 atomic_and_32(&sc->sc_pending, ~PMU_EV_BUTTON); 1081 sysmon_pswitch_event(&sc->sc_powerbutton, 1082 sc->sc_button ? PSWITCH_EVENT_PRESSED : 1083 PSWITCH_EVENT_RELEASED); 1084 } 1085 1086 if (sc->sc_callback != NULL) 1087 sc->sc_callback(sc->sc_cb_cookie); 1088 } 1089 } 1090 1091 static int 1092 pmu_print(void *aux, const char *what) 1093 { 1094 1095 return 0; 1096 } 1097 1098 static void 1099 pmu_attach_legacy_battery(struct pmu_softc *sc) 1100 { 1101 struct battery_attach_args baa; 1102 1103 baa.baa_type = BATTERY_TYPE_LEGACY; 1104 baa.baa_pmu_ops = &sc->sc_pmu_ops; 1105 config_found(sc->sc_dev, &baa, pmu_print, 1106 CFARGS(.iattr = "pmu_bus")); 1107 } 1108 1109 static void 1110 pmu_attach_smart_battery(struct pmu_softc *sc, int num) 1111 { 1112 struct battery_attach_args baa; 1113 1114 baa.baa_type = BATTERY_TYPE_SMART; 1115 baa.baa_pmu_ops = &sc->sc_pmu_ops; 1116 baa.baa_num = num; 1117 config_found(sc->sc_dev, &baa, pmu_print, 1118 CFARGS(.iattr = "pmu_bus")); 1119 } 1120