1 /* $NetBSD: pmu.c,v 1.34 2020/07/14 08:58:03 martin 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.34 2020/07/14 08:58:03 martin 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(irq, type, IPL_TTY, pmu_intr, sc); 317 318 pmu_init(sc); 319 320 sc->sc_pmu_ops.cookie = sc; 321 sc->sc_pmu_ops.do_command = pmu_send; 322 sc->sc_pmu_ops.register_callback = pmu_register_callback; 323 324 if (pmu0 == NULL) 325 pmu0 = sc; 326 327 pmu_send(sc, PMU_SYSTEM_READY, 1, cmd, 16, resp); 328 329 /* check what kind of PMU we're talking to */ 330 if (pmu_send(sc, PMU_GET_VERSION, 0, cmd, 16, resp) > 1) 331 aprint_normal(" rev. %d", resp[1]); 332 aprint_normal("\n"); 333 334 node = OF_child(sc->sc_node); 335 336 while (node != 0) { 337 338 if (OF_getprop(node, "name", name, 256) <= 0) 339 goto next; 340 341 if (strncmp(name, "pmu-i2c", 8) == 0) { 342 int devs; 343 uint32_t addr; 344 char compat[256]; 345 prop_array_t cfg; 346 prop_dictionary_t dev; 347 prop_data_t data; 348 349 aprint_normal_dev(self, "initializing IIC bus\n"); 350 351 cfg = prop_array_create(); 352 prop_dictionary_set(dict, "i2c-child-devices", cfg); 353 prop_object_release(cfg); 354 355 /* look for i2c devices */ 356 devs = OF_child(node); 357 while (devs != 0) { 358 if (OF_getprop(devs, "name", name, 256) <= 0) 359 goto skip; 360 if (OF_getprop(devs, "compatible", 361 compat, 256) <= 0) 362 goto skip; 363 if (OF_getprop(devs, "reg", &addr, 4) <= 0) 364 goto skip; 365 addr = (addr & 0xff) >> 1; 366 DPRINTF("-> %s@%x\n", name, addr); 367 dev = prop_dictionary_create(); 368 prop_dictionary_set_string(dev, "name", name); 369 data = prop_data_create_copy(compat, strlen(compat)+1); 370 prop_dictionary_set(dev, "compatible", data); 371 prop_object_release(data); 372 prop_dictionary_set_uint32(dev, "addr", addr); 373 prop_dictionary_set_uint64(dev, "cookie", devs); 374 prop_array_add(cfg, dev); 375 prop_object_release(dev); 376 skip: 377 devs = OF_peer(devs); 378 } 379 memset(&iba, 0, sizeof(iba)); 380 iba.iba_tag = &sc->sc_i2c; 381 iic_tag_init(&sc->sc_i2c); 382 sc->sc_i2c.ic_cookie = sc; 383 sc->sc_i2c.ic_exec = pmu_i2c_exec; 384 config_found_ia(sc->sc_dev, "i2cbus", &iba, 385 iicbus_print); 386 goto next; 387 } 388 if (strncmp(name, "adb", 4) == 0) { 389 aprint_normal_dev(self, "initializing ADB\n"); 390 sc->sc_adbops.cookie = sc; 391 sc->sc_adbops.send = pmu_adb_send; 392 sc->sc_adbops.poll = pmu_adb_poll; 393 sc->sc_adbops.autopoll = pmu_autopoll; 394 sc->sc_adbops.set_handler = pmu_adb_set_handler; 395 #if NNADB > 0 396 config_found_ia(self, "adb_bus", &sc->sc_adbops, 397 nadb_print); 398 #endif 399 goto next; 400 } 401 if (strncmp(name, "rtc", 4) == 0) { 402 403 aprint_normal_dev(self, "initializing RTC\n"); 404 sc->sc_todr.todr_gettime = pmu_todr_get; 405 sc->sc_todr.todr_settime = pmu_todr_set; 406 sc->sc_todr.cookie = sc; 407 todr_attach(&sc->sc_todr); 408 goto next; 409 } 410 if (strncmp(name, "battery", 8) == 0) 411 goto next; 412 413 aprint_normal_dev(self, "%s not configured\n", name); 414 next: 415 node = OF_peer(node); 416 } 417 418 if (OF_finddevice("/bandit/ohare") != -1) { 419 aprint_normal_dev(self, "enabling ohare backlight control\n"); 420 sc->sc_flags |= PMU_HAS_BACKLIGHT_CONTROL; 421 cmd[0] = 0; 422 cmd[1] = 0; 423 memset(resp, 0, 6); 424 if (pmu_send(sc, PMU_READ_BRIGHTNESS, 1, cmd, 16, resp) > 1) { 425 sc->sc_brightness_wanted = resp[1]; 426 pmu_update_brightness(sc); 427 } 428 } 429 430 /* attach batteries */ 431 if (of_compatible(root_node, has_legacy_battery) != -1) { 432 433 pmu_attach_legacy_battery(sc); 434 } else if (of_compatible(root_node, has_two_smart_batteries) != -1) { 435 436 pmu_attach_smart_battery(sc, 0); 437 pmu_attach_smart_battery(sc, 1); 438 } else { 439 440 /* check how many batteries we have */ 441 pmnode = of_getnode_byname(ca->ca_node, "power-mgt"); 442 if (pmnode == -1) 443 goto bat_done; 444 if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24) 445 goto bat_done; 446 nbat = regs[6] >> 16; 447 for (i = 0; i < nbat; i++) 448 pmu_attach_smart_battery(sc, i); 449 } 450 bat_done: 451 452 if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread, 453 "%s", "pmu") != 0) { 454 aprint_error_dev(self, "unable to create event kthread\n"); 455 } 456 457 sc->sc_lidswitch.smpsw_name = "Lid switch"; 458 sc->sc_lidswitch.smpsw_type = PSWITCH_TYPE_LID; 459 if (sysmon_pswitch_register(&sc->sc_lidswitch) != 0) 460 aprint_error_dev(self, 461 "unable to register lid switch with sysmon\n"); 462 463 sc->sc_powerbutton.smpsw_name = "Power button"; 464 sc->sc_powerbutton.smpsw_type = PSWITCH_TYPE_POWER; 465 if (sysmon_pswitch_register(&sc->sc_powerbutton) != 0) 466 aprint_error_dev(self, 467 "unable to register power button with sysmon\n"); 468 } 469 470 static void 471 pmu_register_callback(void *pmu_cookie, void (*cb)(void *), void *cookie) 472 { 473 struct pmu_softc *sc = pmu_cookie; 474 475 sc->sc_callback = cb; 476 sc->sc_cb_cookie = cookie; 477 } 478 479 static void 480 pmu_init(struct pmu_softc *sc) 481 { 482 uint8_t pmu_imask, resp[16]; 483 484 pmu_imask = 485 PMU_INT_PCEJECT | PMU_INT_SNDBRT | PMU_INT_ADB/* | PMU_INT_TICK*/; 486 pmu_imask |= PMU_INT_BATTERY; 487 pmu_imask |= PMU_INT_ENVIRONMENT; 488 pmu_send(sc, PMU_SET_IMASK, 1, &pmu_imask, 16, resp); 489 490 pmu_write_reg(sc, vIER, 0x90); /* enable VIA interrupts */ 491 } 492 493 static inline void 494 pmu_write_reg(struct pmu_softc *sc, int offset, uint8_t value) 495 { 496 497 bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value); 498 } 499 500 static inline uint8_t 501 pmu_read_reg(struct pmu_softc *sc, int offset) 502 { 503 504 return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset); 505 } 506 507 static inline int 508 pmu_send_byte(struct pmu_softc *sc, uint8_t data) 509 { 510 511 pmu_out(sc); 512 pmu_write_reg(sc, vSR, data); 513 pmu_ack_off(sc); 514 /* wait for intr to come up */ 515 /* XXX should add a timeout and bail if it expires */ 516 do {} while (pmu_intr_state(sc) == 0); 517 pmu_ack_on(sc); 518 do {} while (pmu_intr_state(sc)); 519 pmu_ack_on(sc); 520 DPRINTF(" %02x>", data); 521 return 0; 522 } 523 524 static inline int 525 pmu_read_byte(struct pmu_softc *sc, uint8_t *data) 526 { 527 pmu_in(sc); 528 (void)pmu_read_reg(sc, vSR); 529 pmu_ack_off(sc); 530 /* wait for intr to come up */ 531 do {} while (pmu_intr_state(sc) == 0); 532 pmu_ack_on(sc); 533 do {} while (pmu_intr_state(sc)); 534 *data = pmu_read_reg(sc, vSR); 535 DPRINTF(" <%02x", *data); 536 return 0; 537 } 538 539 static int 540 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen, 541 uint8_t *out_msg) 542 { 543 struct pmu_softc *sc = cookie; 544 int i, rcv_len = -1, s; 545 uint8_t out_len, intreg; 546 547 DPRINTF("pmu_send: "); 548 549 s = splhigh(); 550 intreg = pmu_read_reg(sc, vIER); 551 intreg &= 0x10; 552 pmu_write_reg(sc, vIER, intreg); 553 554 /* wait idle */ 555 do {} while (pmu_intr_state(sc)); 556 sc->sc_error = 0; 557 558 /* send command */ 559 pmu_send_byte(sc, cmd); 560 561 /* send length if necessary */ 562 if (pm_send_cmd_type[cmd] < 0) { 563 pmu_send_byte(sc, length); 564 } 565 566 for (i = 0; i < length; i++) { 567 pmu_send_byte(sc, in_msg[i]); 568 DPRINTF(" next "); 569 } 570 DPRINTF("done sending\n"); 571 572 /* see if there's data to read */ 573 rcv_len = pm_receive_cmd_type[cmd]; 574 if (rcv_len == 0) 575 goto done; 576 577 /* read command */ 578 if (rcv_len == 1) { 579 pmu_read_byte(sc, out_msg); 580 goto done; 581 } else 582 out_msg[0] = cmd; 583 if (rcv_len < 0) { 584 pmu_read_byte(sc, &out_len); 585 rcv_len = out_len + 1; 586 } 587 for (i = 1; i < uimin(rcv_len, rlen); i++) 588 pmu_read_byte(sc, &out_msg[i]); 589 590 done: 591 DPRINTF("\n"); 592 pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90); 593 splx(s); 594 595 return rcv_len; 596 } 597 598 static void 599 pmu_adb_poll(void *cookie) 600 { 601 struct pmu_softc *sc = cookie; 602 int s; 603 604 s = spltty(); 605 pmu_intr(sc); 606 splx(s); 607 } 608 609 static void 610 pmu_in(struct pmu_softc *sc) 611 { 612 uint8_t reg; 613 614 reg = pmu_read_reg(sc, vACR); 615 reg &= ~vSR_OUT; 616 reg |= 0x0c; 617 pmu_write_reg(sc, vACR, reg); 618 } 619 620 static void 621 pmu_out(struct pmu_softc *sc) 622 { 623 uint8_t reg; 624 625 reg = pmu_read_reg(sc, vACR); 626 reg |= vSR_OUT; 627 reg |= 0x0c; 628 pmu_write_reg(sc, vACR, reg); 629 } 630 631 static void 632 pmu_ack_off(struct pmu_softc *sc) 633 { 634 uint8_t reg; 635 636 reg = pmu_read_reg(sc, vBufB); 637 reg &= ~vPB4; 638 pmu_write_reg(sc, vBufB, reg); 639 } 640 641 static void 642 pmu_ack_on(struct pmu_softc *sc) 643 { 644 uint8_t reg; 645 646 reg = pmu_read_reg(sc, vBufB); 647 reg |= vPB4; 648 pmu_write_reg(sc, vBufB, reg); 649 } 650 651 static int 652 pmu_intr_state(struct pmu_softc *sc) 653 { 654 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0); 655 } 656 657 static int 658 pmu_intr(void *arg) 659 { 660 struct pmu_softc *sc = arg; 661 unsigned int len, i; 662 uint8_t resp[16]; 663 664 DPRINTF(":"); 665 666 pmu_write_reg(sc, vIFR, 0x90); /* Clear 'em */ 667 len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp); 668 if ((len < 1) || (resp[1] == 0)) 669 goto done; 670 #ifdef PMU_DEBUG 671 { 672 DPRINTF("intr: %02x", resp[0]); 673 for (i = 1; i < len; i++) 674 DPRINTF(" %02x", resp[i]); 675 DPRINTF("\n"); 676 } 677 #endif 678 if (resp[1] & PMU_INT_ADB) { 679 pmu_adb_handler(sc, len - 1, &resp[1]); 680 goto done; 681 } 682 if (resp[1] & PMU_INT_SNDBRT) { 683 /* deal with the brightness / volume control buttons */ 684 DPRINTF("brightness: %d volume %d\n", resp[2], resp[3]); 685 sc->sc_brightness_wanted = resp[2]; 686 sc->sc_volume_wanted = resp[3]; 687 wakeup(&sc->sc_event); 688 goto done; 689 } 690 if (resp[1] & PMU_INT_PCEJECT) { 691 /* deal with PCMCIA eject buttons */ 692 DPRINTF("card eject %d\n", resp[3]); 693 atomic_or_32(&sc->sc_pending, (resp[3] & 3)); 694 wakeup(&sc->sc_event); 695 goto done; 696 } 697 if (resp[1] & PMU_INT_BATTERY) { 698 /* deal with battery messages */ 699 printf("battery:"); 700 for (i = 2; i < len; i++) 701 printf(" %02x", resp[i]); 702 printf("\n"); 703 goto done; 704 } 705 if (resp[1] & PMU_INT_ENVIRONMENT) { 706 uint8_t diff; 707 #ifdef PMU_VERBOSE 708 /* deal with environment messages */ 709 printf("environment:"); 710 for (i = 2; i < len; i++) 711 printf(" %02x", resp[i]); 712 printf("\n"); 713 #endif 714 diff = (resp[2] ^ sc->sc_env_old ) & sc->sc_env_mask; 715 if (diff == 0) goto done; 716 sc->sc_env_old = resp[2]; 717 if (diff & PMU_ENV_LID_CLOSED) { 718 sc->sc_lid_closed = (resp[2] & PMU_ENV_LID_CLOSED) != 0; 719 atomic_or_32(&sc->sc_pending, PMU_EV_LID); 720 wakeup(&sc->sc_event); 721 } 722 if (diff & PMU_ENV_POWER_BUTTON) { 723 sc->sc_button = (resp[2] & PMU_ENV_POWER_BUTTON) != 0; 724 atomic_or_32(&sc->sc_pending, PMU_EV_BUTTON); 725 wakeup(&sc->sc_event); 726 } 727 goto done; 728 } 729 if (resp[1] & PMU_INT_TICK) { 730 /* don't bother */ 731 goto done; 732 } 733 734 /* unknown interrupt code?! */ 735 #ifdef PMU_DEBUG 736 printf("pmu intr: %02x:", resp[1]); 737 for (i = 2; i < len; i++) 738 printf(" %02x", resp[i]); 739 printf("\n"); 740 #endif 741 done: 742 return 1; 743 } 744 745 #if 0 746 static int 747 pmu_error_handler(void *cookie, int len, uint8_t *data) 748 { 749 struct pmu_softc *sc = cookie; 750 751 /* 752 * something went wrong 753 * byte 3 seems to be the failed command 754 */ 755 sc->sc_error = 1; 756 wakeup(&sc->sc_todev); 757 return 0; 758 } 759 #endif 760 #define DIFF19041970 2082844800 761 762 static int 763 pmu_todr_get(todr_chip_handle_t tch, struct timeval *tvp) 764 { 765 struct pmu_softc *sc = tch->cookie; 766 uint32_t sec; 767 int count = 10; 768 int ok = FALSE; 769 uint8_t resp[16]; 770 771 DPRINTF("pmu_todr_get\n"); 772 while ((count > 0) && (!ok)) { 773 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp); 774 775 memcpy(&sec, &resp[1], 4); 776 tvp->tv_sec = sec - DIFF19041970; 777 ok = (sec > DIFF19041970) && (sec < 0xf0000000); 778 if (!ok) aprint_error_dev(sc->sc_dev, 779 "got garbage from rtc (%08x)\n", sec); 780 count--; 781 } 782 if (count == 0) { 783 aprint_error_dev(sc->sc_dev, 784 "unable to get a sane time value\n"); 785 tvp->tv_sec = 0; 786 } 787 DPRINTF("tod: %" PRIo64 "\n", tvp->tv_sec); 788 tvp->tv_usec = 0; 789 return 0; 790 } 791 792 static int 793 pmu_todr_set(todr_chip_handle_t tch, struct timeval *tvp) 794 { 795 struct pmu_softc *sc = tch->cookie; 796 uint32_t sec; 797 uint8_t resp[16]; 798 799 sec = tvp->tv_sec + DIFF19041970; 800 if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0) 801 return 0; 802 return -1; 803 } 804 805 void 806 pmu_poweroff(void) 807 { 808 struct pmu_softc *sc; 809 uint8_t cmd[] = {'M', 'A', 'T', 'T'}; 810 uint8_t resp[16]; 811 812 if (pmu0 == NULL) 813 return; 814 sc = pmu0; 815 if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0) 816 while (1); 817 } 818 819 void 820 pmu_restart(void) 821 { 822 struct pmu_softc *sc; 823 uint8_t resp[16]; 824 825 if (pmu0 == NULL) 826 return; 827 sc = pmu0; 828 if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0) 829 while (1); 830 } 831 832 void 833 pmu_modem(int on) 834 { 835 struct pmu_softc *sc; 836 uint8_t resp[16], cmd[2] = {0, 0}; 837 838 if (pmu0 == NULL) 839 return; 840 841 sc = pmu0; 842 cmd[0] = PMU_POW0_MODEM | (on ? PMU_POW0_ON : 0); 843 pmu_send(sc, PMU_POWER_CTRL0, 1, cmd, 16, resp); 844 } 845 846 static void 847 pmu_autopoll(void *cookie, int flag) 848 { 849 struct pmu_softc *sc = cookie; 850 /* magical incantation to re-enable autopolling */ 851 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff}; 852 uint8_t resp[16]; 853 854 if (sc->sc_autopoll == flag) 855 return; 856 857 if (flag) { 858 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp); 859 } else { 860 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp); 861 } 862 sc->sc_autopoll = flag & 0xffff; 863 } 864 865 static int 866 pmu_adb_handler(void *cookie, int len, uint8_t *data) 867 { 868 struct pmu_softc *sc = cookie; 869 uint8_t resp[16]; 870 871 if (sc->sc_adb_handler != NULL) { 872 sc->sc_adb_handler(sc->sc_adb_cookie, len, data); 873 /* 874 * the PMU will turn off autopolling after each LISTEN so we 875 * need to re-enable it here whenever we receive an ACK for a 876 * LISTEN command 877 */ 878 if ((data[1] & 0x0c) == 0x08) { 879 uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff, 880 sc->sc_autopoll & 0xff}; 881 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp); 882 } 883 return 0; 884 } 885 return -1; 886 } 887 888 static int 889 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data) 890 { 891 struct pmu_softc *sc = cookie; 892 int i; 893 uint8_t packet[16], resp[16]; 894 895 /* construct an ADB command packet and send it */ 896 packet[0] = command; 897 packet[1] = 0; 898 packet[2] = len; 899 for (i = 0; i < len; i++) 900 packet[i + 3] = data[i]; 901 (void)pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp); 902 903 return 0; 904 } 905 906 static int 907 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *), 908 void *hcookie) 909 { 910 struct pmu_softc *sc = cookie; 911 912 /* register a callback for incoming ADB messages */ 913 sc->sc_adb_handler = handler; 914 sc->sc_adb_cookie = hcookie; 915 return 0; 916 } 917 918 static int 919 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send, 920 size_t send_len, void *_recv, size_t recv_len, int flags) 921 { 922 struct pmu_softc *sc = cookie; 923 const uint8_t *send = _send; 924 uint8_t command[32] = {1, /* bus number */ 925 PMU_I2C_MODE_SIMPLE, 926 0, /* bus2 */ 927 addr, 928 0, /* sub address */ 929 0, /* comb address */ 930 0, /* count */ 931 0 /* data */ 932 }; 933 uint8_t resp[16]; 934 int len, rw; 935 936 rw = addr << 1; 937 command[3] = rw; 938 if (send_len > 0) { 939 command[6] = send_len; 940 memcpy(&command[7], send, send_len); 941 len = send_len + 7; 942 DPRINTF("pmu_i2c_exec(%02x, %d)\n", addr, send_len); 943 944 len = pmu_send(sc, PMU_I2C_CMD, len, command, 16, resp); 945 DPRINTF("resp(%d): %2x %2x\n", len, resp[0], resp[1]); 946 947 if (resp[1] != PMU_I2C_STATUS_OK) { 948 DPRINTF("%s: iic error %d\n", __func__, resp[1]); 949 return -1; 950 } 951 } 952 /* see if we're supposed to read */ 953 if (I2C_OP_READ_P(op)) { 954 rw |= 1; 955 command[3] = rw; 956 command[6] = recv_len; 957 len = pmu_send(sc, PMU_I2C_CMD, 7, command, 16, resp); 958 DPRINTF("resp2(%d): %2x %2x\n", len, resp[0], resp[1]); 959 960 command[0] = 0; 961 len = pmu_send(sc, PMU_I2C_CMD, 1, command, 16, resp); 962 DPRINTF("resp3(%d): %2x %2x %2x\n", len, resp[0], resp[1], 963 resp[2]); 964 if ((len - 2) != recv_len) { 965 DPRINTF("%s: %s(%d) - got %d\n", 966 device_xname(sc->sc_dev), 967 __func__, recv_len, len - 2); 968 return -1; 969 } 970 memcpy(_recv, &resp[2], len - 2); 971 return 0; 972 }; 973 return 0; 974 } 975 976 static void 977 pmu_eject_card(struct pmu_softc *sc, int socket) 978 { 979 uint8_t buf[] = {socket | 4}; 980 uint8_t res[4]; 981 982 atomic_and_32(&sc->sc_pending, ~socket); 983 pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res); 984 } 985 986 static void 987 pmu_update_brightness(struct pmu_softc *sc) 988 { 989 int val; 990 uint8_t cmd[2], resp[16]; 991 992 if (sc->sc_brightness == sc->sc_brightness_wanted) 993 return; 994 995 if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) { 996 997 aprint_normal_dev(sc->sc_dev, 998 "this PMU doesn't support backlight control\n"); 999 sc->sc_brightness = sc->sc_brightness_wanted; 1000 return; 1001 } 1002 1003 if (sc->sc_brightness_wanted == 0) { 1004 1005 /* turn backlight off completely */ 1006 cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT; 1007 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp); 1008 sc->sc_brightness = sc->sc_brightness_wanted; 1009 1010 /* don't bother with brightness */ 1011 return; 1012 } 1013 1014 /* turn backlight on if needed */ 1015 if (sc->sc_brightness == 0) { 1016 cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT; 1017 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp); 1018 } 1019 1020 DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness, 1021 sc->sc_brightness_wanted); 1022 1023 val = 0x7f - (sc->sc_brightness_wanted >> 1); 1024 if (val < 0x08) 1025 val = 0x08; 1026 if (val > 0x78) 1027 val = 0x78; 1028 cmd[0] = val; 1029 pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp); 1030 1031 sc->sc_brightness = sc->sc_brightness_wanted; 1032 } 1033 1034 static void 1035 pmu_thread(void *cookie) 1036 { 1037 struct pmu_softc *sc = cookie; 1038 //time_t time_bat = time_second; 1039 int ticks = hz, i; 1040 1041 while (1) { 1042 tsleep(&sc->sc_event, PWAIT, "wait", ticks); 1043 if ((sc->sc_pending & 3) != 0) { 1044 DPRINTF("eject %d\n", sc->sc_pending & 3); 1045 for (i = 1; i < 3; i++) { 1046 if (i & sc->sc_pending) 1047 pmu_eject_card(sc, i); 1048 } 1049 } 1050 1051 /* see if we need to update brightness */ 1052 if (sc->sc_brightness_wanted != sc->sc_brightness) { 1053 pmu_update_brightness(sc); 1054 } 1055 1056 /* see if we need to update audio volume */ 1057 if (sc->sc_volume_wanted != sc->sc_volume) { 1058 #if 0 1059 set_volume(sc->sc_volume_wanted); 1060 #endif 1061 sc->sc_volume = sc->sc_volume_wanted; 1062 } 1063 1064 if (sc->sc_pending & PMU_EV_LID) { 1065 atomic_and_32(&sc->sc_pending, ~PMU_EV_LID); 1066 sysmon_pswitch_event(&sc->sc_lidswitch, 1067 sc->sc_lid_closed ? PSWITCH_EVENT_PRESSED : 1068 PSWITCH_EVENT_RELEASED); 1069 } 1070 1071 if (sc->sc_pending & PMU_EV_BUTTON) { 1072 atomic_and_32(&sc->sc_pending, ~PMU_EV_BUTTON); 1073 sysmon_pswitch_event(&sc->sc_powerbutton, 1074 sc->sc_button ? PSWITCH_EVENT_PRESSED : 1075 PSWITCH_EVENT_RELEASED); 1076 } 1077 1078 if (sc->sc_callback != NULL) 1079 sc->sc_callback(sc->sc_cb_cookie); 1080 } 1081 } 1082 1083 static int 1084 pmu_print(void *aux, const char *what) 1085 { 1086 1087 return 0; 1088 } 1089 1090 static void 1091 pmu_attach_legacy_battery(struct pmu_softc *sc) 1092 { 1093 struct battery_attach_args baa; 1094 1095 baa.baa_type = BATTERY_TYPE_LEGACY; 1096 baa.baa_pmu_ops = &sc->sc_pmu_ops; 1097 config_found_ia(sc->sc_dev, "pmu_bus", &baa, pmu_print); 1098 } 1099 1100 static void 1101 pmu_attach_smart_battery(struct pmu_softc *sc, int num) 1102 { 1103 struct battery_attach_args baa; 1104 1105 baa.baa_type = BATTERY_TYPE_SMART; 1106 baa.baa_pmu_ops = &sc->sc_pmu_ops; 1107 baa.baa_num = num; 1108 config_found_ia(sc->sc_dev, "pmu_bus", &baa, pmu_print); 1109 } 1110