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