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