1/* $NetBSD: psm.c,v 1.11 2016/07/07 06:55:38 msaitoh Exp $ */ 2/* 3 * Copyright (c) 2006 Itronix Inc. 4 * All rights reserved. 5 * 6 * Ported from Tadpole Solaris sources by Garrett D'Amore for Itronix Inc. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions 10 * are met: 11 * 1. Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * 2. Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in the 15 * documentation and/or other materials provided with the distribution. 16 * 3. The name of Itronix Inc. may not be used to endorse 17 * or promote products derived from this software without specific 18 * prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY ITRONIX INC. ``AS IS'' AND 21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ITRONIX INC. BE LIABLE FOR ANY 24 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 27 * ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32/* 33 * Tadpole-RDI Ultrabook IIi (huxley) power management. Note that 34 * there is a lot of stuff still missing here, due in part to the confusion 35 * that exists with the NetBSD power management framework. I'm not wasting 36 * time with APM at this point, and some of sysmon seems "lacking". 37 */ 38#include <sys/cdefs.h> 39__KERNEL_RCSID(0, "$NetBSD: psm.c,v 1.11 2016/07/07 06:55:38 msaitoh Exp $"); 40 41#include <sys/param.h> 42#include <sys/systm.h> 43#include <sys/proc.h> 44#include <sys/kernel.h> 45#include <sys/kthread.h> 46#include <sys/types.h> 47#include <sys/device.h> 48#include <sys/poll.h> 49#include <sys/kauth.h> 50 51#include <machine/autoconf.h> 52#include <sys/bus.h> 53#include <machine/intr.h> 54 55#include <dev/ebus/ebusreg.h> 56#include <dev/ebus/ebusvar.h> 57 58#include <dev/sysmon/sysmonvar.h> 59 60#include <sparc64/dev/psmreg.h> 61 62struct psm_softc { 63 device_t sc_dev; 64 bus_space_tag_t sc_memt; 65 bus_space_handle_t sc_memh; 66 67 int sc_event; 68 int sc_flags; 69 struct sysmon_pswitch sc_sm_pbutton; 70 struct sysmon_pswitch sc_sm_lid; 71 struct sysmon_pswitch sc_sm_ac; 72 struct evcnt sc_intrcnt; 73 lwp_t *sc_thread; 74}; 75 76#define PUT8(sc, r, v) \ 77 bus_space_write_1(sc->sc_memt, sc->sc_memh, r, v) 78#define GET8(sc, r) \ 79 bus_space_read_1(sc->sc_memt, sc->sc_memh, r) 80 81#define WAIT_DELAY 1000 82#define WAIT_RETRIES 1000 83 84#define RESET_DELAY 200 85#define CMD_DELAY 10 86#define CMD_RETRIES 5 87 88#ifdef DEBUG 89#define STATIC 90#else 91#define STATIC static 92#endif 93 94/* flags indicating state */ 95#define PSM_FLAG_ACPWR 0x1 96#define PSM_FLAG_LIDCLOSED 0x2 97#define PSM_FLAG_DOCKED 0x4 98 99/* system events -- causes activity in the event thread */ 100#define PSM_EV_PBUTTON 0x1 101#define PSM_EV_LID 0x2 102#define PSM_EV_ACPWR 0x4 103#define PSM_EV_BATT 0x8 104#define PSM_EV_TEMP 0x10 105 106STATIC void psm_sysmon_setup(struct psm_softc *); 107STATIC void psm_event_thread(void *); 108STATIC int psm_init(struct psm_softc *); 109STATIC void psm_reset(struct psm_softc *); 110STATIC void psm_poll_acpower(struct psm_softc *); 111STATIC int psm_intr(void *); 112STATIC int psm_misc_rd(struct psm_softc *, uint8_t, uint8_t *); 113STATIC int psm_misc_wr(struct psm_softc *, uint8_t, uint8_t); 114STATIC int psm_wait(struct psm_softc *, uint8_t); 115#if 0 116STATIC int psm_ecmd_rd16(struct psm_softc *, uint16_t *, uint8_t, uint8_t, 117 uint8_t); 118#endif 119STATIC int psm_ecmd_rd8(struct psm_softc *, uint8_t *, uint8_t, uint8_t, 120 uint8_t); 121STATIC int psm_ecmd_wr8(struct psm_softc *, uint8_t, uint8_t, uint8_t, 122 uint8_t); 123STATIC int psm_match(device_t, cfdata_t, void *); 124STATIC void psm_attach(device_t, device_t, void *); 125 126CFATTACH_DECL_NEW(psm, sizeof(struct psm_softc), 127 psm_match, psm_attach, NULL, NULL); 128 129 130int 131psm_match(device_t parent, cfdata_t cf, void *aux) 132{ 133 struct ebus_attach_args *ea = aux; 134 135 if (strcmp(ea->ea_name, "psm") != 0) 136 return (0); 137 return (1); 138} 139 140void 141psm_attach(device_t parent, device_t self, void *aux) 142{ 143 struct psm_softc *sc = device_private(self); 144 struct ebus_attach_args *ea = aux; 145 bus_addr_t devaddr; 146 const char *xname; 147 148 149 sc->sc_dev = self; 150 sc->sc_memt = ea->ea_bustag; 151 devaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]); 152 153 xname = device_xname(sc->sc_dev); 154 if (bus_space_map(sc->sc_memt, devaddr, ea->ea_reg[0].size, 155 0, &sc->sc_memh) != 0) { 156 printf(": unable to map device registers\n"); 157 return; 158 } 159 if (psm_init(sc) != 0) { 160 printf(": unable to initialize device\n"); 161 return; 162 } 163 164 printf(": UltraBook IIi power control\n"); 165 166 psm_sysmon_setup(sc); 167 168 if (kthread_create(PRI_NONE, 0, NULL, psm_event_thread, sc, 169 &sc->sc_thread, "%s", xname) != 0) { 170 aprint_error_dev(sc->sc_dev, "unable to create event kthread\n"); 171 } 172 173 /* 174 * Establish device interrupts 175 */ 176 (void) bus_intr_establish(sc->sc_memt, ea->ea_intr[0], IPL_HIGH, 177 psm_intr, sc); 178 evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL, 179 xname, "intr"); 180} 181 182/* 183 * Register sensors and events with sysmon. 184 */ 185void 186psm_sysmon_setup(struct psm_softc *sc) 187{ 188 const char *xname = device_xname(sc->sc_dev); 189 190 191 /* 192 * XXX: Register sysmon environment. 193 */ 194 195 /* 196 * Register sysmon events 197 */ 198 sc->sc_sm_pbutton.smpsw_name = xname; 199 sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER; 200 if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0) 201 printf("%s: unable to register power button\n", xname); 202 203 sc->sc_sm_lid.smpsw_name = xname; 204 sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID; 205 if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0) 206 printf("%s: unable to register lid switch\n", xname); 207 208 sc->sc_sm_ac.smpsw_name = xname; 209 sc->sc_sm_ac.smpsw_type = PSWITCH_TYPE_ACADAPTER; 210 if (sysmon_pswitch_register(&sc->sc_sm_ac) != 0) 211 printf("%s: unable to register AC adapter\n", xname); 212} 213 214void 215psm_event_thread(void *arg) 216{ 217 struct psm_softc *sc = arg; 218 int x; 219 int event; 220 int flags; 221 222 for (;;) { 223 x = splhigh(); 224 /* check for AC power. sets event if there is a change */ 225 psm_poll_acpower(sc); 226 227 /* read and clear events */ 228 event = sc->sc_event; 229 flags = sc->sc_flags; 230 sc->sc_event = 0; 231 splx(x); 232 233 if (event & PSM_EV_PBUTTON) { 234 sysmon_pswitch_event(&sc->sc_sm_pbutton, 235 PSWITCH_EVENT_PRESSED); 236 } 237 238 if (event & PSM_EV_LID) { 239 sysmon_pswitch_event(&sc->sc_sm_lid, 240 flags & PSM_FLAG_LIDCLOSED ? 241 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 242 } 243 244 if (event & PSM_EV_ACPWR) { 245 sysmon_pswitch_event(&sc->sc_sm_ac, 246 flags & PSM_FLAG_ACPWR ? 247 PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED); 248 } 249 250 /* XXX: handle PSM_EV_TEMP */ 251 252 /* one second interval between probes of power */ 253 tsleep(&sc, PWAIT, "psm", hz); 254 } 255} 256 257int 258psm_init(struct psm_softc *sc) 259{ 260 int x; 261 uint8_t batt = 0xff; /* keep GCC 4.x happy */ 262 263 /* clear interrupts */ 264 x = splhigh(); 265 PUT8(sc, PSM_ICR, 0xff); 266 splx(x); 267 268 /* enable interrupts */ 269 if (psm_misc_wr(sc, PSM_MISC_IMR, PSM_IMR_ALL)) 270 return (-1); 271 272 /* make sure that UPS battery is reasonable */ 273 if (psm_misc_rd(sc, PSM_MISC_UPS, &batt) || (batt > PSM_MAX_BATTERIES)) 274 if (psm_misc_wr(sc, PSM_MISC_UPS, batt)) 275 aprint_error_dev(sc->sc_dev, "cannot set UPS battery"); 276 277 return (0); 278} 279 280void 281psm_reset(struct psm_softc *sc) 282{ 283 284 PUT8(sc, PSM_MCR, PSM_MCR_RST); 285 delay(RESET_DELAY); 286} 287 288void 289psm_poll_acpower(struct psm_softc *sc) 290{ 291 int flags = sc->sc_flags; 292 293 if (GET8(sc, PSM_STAT) & PSM_STAT_AC) { 294 sc->sc_flags |= PSM_FLAG_ACPWR; 295 } else { 296 sc->sc_flags &= ~PSM_FLAG_ACPWR; 297 } 298 if (flags != sc->sc_flags) 299 sc->sc_event |= PSM_EV_ACPWR; 300} 301 302int 303psm_misc_rd(struct psm_softc *sc, uint8_t mreg, uint8_t *data) 304{ 305 306 return (psm_ecmd_rd8(sc, data, mreg, PSM_MODE_MISC, 0)); 307} 308 309int 310psm_misc_wr(struct psm_softc *sc, uint8_t mreg, uint8_t data) 311{ 312 313 return (psm_ecmd_wr8(sc, data, mreg, PSM_MODE_MISC, 0)); 314} 315 316int 317psm_wait(struct psm_softc *sc, uint8_t flag) 318{ 319 int retr = WAIT_RETRIES; 320 321 while (GET8(sc, PSM_STAT) & flag) { 322 if (!(retr--)) { 323 return (-1); 324 } 325 delay(WAIT_DELAY); 326 } 327 return (0); 328} 329 330#if 0 331int 332psm_ecmd_rd16(struct psm_softc *sc, uint16_t *data, uint8_t iar, uint8_t mode, 333 uint8_t addr) 334{ 335 uint8_t cmr = PSM_CMR_DATA(mode, PSM_L_16, PSM_D_RD, addr); 336 int x, rc, retr = CMD_RETRIES; 337 338 x = splhigh(); 339 340 do { 341 if ((rc = psm_wait(sc, PSM_STAT_RDA)) != 0) { 342 psm_reset(sc); 343 continue; 344 } 345 346 PUT8(sc, PSM_IAR, iar); 347 PUT8(sc, PSM_CMR, cmr); 348 349 delay(CMD_DELAY); 350 351 if ((rc = psm_wait(sc, PSM_STAT_RDA)) == 0) { 352 *data = GET8(sc, PSM_PWDL) | (GET8(sc, PSM_PWDU) << 8); 353 break; 354 } 355 356 psm_reset(sc); 357 358 } while (--retr); 359 360 splx(x); 361 return (rc); 362} 363#endif 364 365int 366psm_ecmd_rd8(struct psm_softc *sc, uint8_t *data, uint8_t iar, uint8_t mode, 367 uint8_t addr) 368{ 369 uint8_t cmr = PSM_CMR_DATA(mode, PSM_L_8, PSM_D_RD, addr); 370 int x, rc, retr = CMD_RETRIES; 371 372 x = splhigh(); 373 374 do { 375 if ((rc = psm_wait(sc, PSM_STAT_RDA)) != 0) { 376 psm_reset(sc); 377 continue; 378 } 379 380 PUT8(sc, PSM_IAR, iar); 381 PUT8(sc, PSM_CMR, cmr); 382 383 delay(CMD_DELAY); 384 385 if ((rc = psm_wait(sc, PSM_STAT_RDA)) == 0) { 386 (void) GET8(sc, PSM_PWDU); 387 *data = GET8(sc, PSM_PWDL); 388 break; 389 } 390 391 psm_reset(sc); 392 393 } while (--retr); 394 395 splx(x); 396 return (rc); 397} 398 399int 400psm_ecmd_wr8(struct psm_softc *sc, uint8_t data, uint8_t iar, uint8_t mode, 401 uint8_t addr) 402{ 403 uint8_t cmr = PSM_CMR_DATA(mode, PSM_L_8, PSM_D_WR, addr); 404 int x, rc, retr = CMD_RETRIES; 405 406 x = splhigh(); 407 408 do { 409 if ((rc = psm_wait(sc, PSM_STAT_WBF)) != 0) { 410 psm_reset(sc); 411 continue; 412 } 413 414 PUT8(sc, PSM_PWDU, 0); 415 PUT8(sc, PSM_PWDL, data); 416 PUT8(sc, PSM_IAR, iar); 417 PUT8(sc, PSM_CMR, cmr); 418 419 delay(CMD_DELAY); 420 421 if ((rc = psm_wait(sc, PSM_STAT_WBF)) == 0) { 422 break; 423 } 424 425 psm_reset(sc); 426 } while (--retr); 427 428 splx(x); 429 430 return (rc); 431} 432 433int 434psm_intr(void *arg) 435{ 436 struct psm_softc *sc = arg; 437 uint8_t isr; 438 439 isr = GET8(sc, PSM_ISR); 440 if (isr & PSM_ISR_PO) { 441 PUT8(sc, PSM_ICR, PSM_ISR_PO); 442 sc->sc_event |= PSM_EV_PBUTTON; 443 } 444 if (isr & PSM_ISR_DK) { 445 PUT8(sc, PSM_ICR, PSM_ISR_DK); 446 sc->sc_flags |= PSM_FLAG_DOCKED; 447 } 448 if (isr & PSM_ISR_UDK) { 449 PUT8(sc, PSM_ICR, PSM_ISR_UDK); 450 sc->sc_flags &= ~PSM_FLAG_DOCKED; 451 } 452 if (isr & PSM_ISR_LIDC) { 453 PUT8(sc, PSM_ICR, PSM_ISR_LIDC); 454 sc->sc_flags |= PSM_FLAG_LIDCLOSED; 455 sc->sc_event |= PSM_EV_LID; 456 } 457 if (isr & PSM_ISR_LIDO) { 458 PUT8(sc, PSM_ICR, PSM_ISR_LIDO); 459 sc->sc_flags &= ~PSM_FLAG_LIDCLOSED; 460 sc->sc_event |= PSM_EV_LID; 461 } 462 if (isr & PSM_ISR_TMP) { 463 /* Over temperature */ 464 PUT8(sc, PSM_ICR, PSM_ISR_TMP); 465 sc->sc_event |= PSM_EV_TEMP; 466 } 467 if (isr & PSM_ISR_BCC) { 468 /* battery config changed */ 469 PUT8(sc, PSM_ICR, PSM_ISR_BCC); 470 sc->sc_event |= PSM_EV_BATT; 471 } 472 if (isr & PSM_ISR_RPD) { 473 /* request to power down */ 474 sc->sc_event |= PSM_EV_PBUTTON; 475 } 476 if (sc->sc_event) { 477 /* wake up the thread */ 478 wakeup(sc); 479 } 480 return (1); 481} 482