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