Home | History | Annotate | Line # | Download | only in dev
psm.c revision 1.7
      1 /* $NetBSD: psm.c,v 1.7 2008/04/05 13:40:05 cegger 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.7 2008/04/05 13:40:05 cegger 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 <machine/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 
     62 struct psm_softc {
     63 	struct device		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 
    106 STATIC void psm_sysmon_setup(struct psm_softc *);
    107 STATIC void psm_event_thread(void *);
    108 STATIC int psm_init(struct psm_softc *);
    109 STATIC void psm_reset(struct psm_softc *);
    110 STATIC void psm_poll_acpower(struct psm_softc *);
    111 STATIC int psm_intr(void *);
    112 STATIC int psm_misc_rd(struct psm_softc *, uint8_t, uint8_t *);
    113 STATIC int psm_misc_wr(struct psm_softc *, uint8_t, uint8_t);
    114 STATIC int psm_wait(struct psm_softc *, uint8_t);
    115 #if 0
    116 STATIC int psm_ecmd_rd16(struct psm_softc *, uint16_t *, uint8_t, uint8_t,
    117     uint8_t);
    118 #endif
    119 STATIC int psm_ecmd_rd8(struct psm_softc *, uint8_t *, uint8_t, uint8_t,
    120     uint8_t);
    121 STATIC int psm_ecmd_wr8(struct psm_softc *, uint8_t, uint8_t, uint8_t,
    122     uint8_t);
    123 STATIC int psm_match(struct device *, struct cfdata *, void *);
    124 STATIC void psm_attach(struct device *, struct device *, void *);
    125 
    126 CFATTACH_DECL(psm, sizeof(struct psm_softc),
    127     psm_match, psm_attach, NULL, NULL);
    128 
    129 
    130 int
    131 psm_match(struct device *parent, struct cfdata *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 
    140 void
    141 psm_attach(struct device *parent, struct device *self, void *aux)
    142 {
    143 	struct psm_softc	*sc = (struct psm_softc *)self;
    144 	struct ebus_attach_args	*ea = aux;
    145 	bus_addr_t		devaddr;
    146 	const char		*xname;
    147 
    148 	xname = device_xname(&sc->sc_dev);
    149 
    150 	sc->sc_memt = ea->ea_bustag;
    151 	devaddr = EBUS_ADDR_FROM_REG(&ea->ea_reg[0]);
    152 
    153 	if (bus_space_map(sc->sc_memt, devaddr, ea->ea_reg[0].size,
    154 		0, &sc->sc_memh) != 0) {
    155 		printf(": unable to map device registers\n");
    156 		return;
    157 	}
    158 	if (psm_init(sc) != 0) {
    159 		printf(": unable to initialize device\n");
    160 		return;
    161 	}
    162 
    163 	printf(": UltraBook IIi power control\n");
    164 
    165 	psm_sysmon_setup(sc);
    166 
    167 	if (kthread_create(PRI_NONE, 0, NULL, psm_event_thread, sc,
    168 	    &sc->sc_thread, "%s", device_xname(&sc->sc_dev)) != 0) {
    169 		aprint_error_dev(&sc->sc_dev, "unable to create event kthread\n");
    170 	}
    171 
    172 	/*
    173 	 * Establish device interrupts
    174 	 */
    175 	(void) bus_intr_establish(sc->sc_memt, ea->ea_intr[0], IPL_HIGH,
    176 	    psm_intr, sc);
    177 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    178 	    device_xname(&sc->sc_dev), "intr");
    179 }
    180 
    181 /*
    182  * Register sensors and events with sysmon.
    183  */
    184 void
    185 psm_sysmon_setup(struct psm_softc *sc)
    186 {
    187 	const char	*xname	= device_xname(&sc->sc_dev);
    188 
    189 
    190 	/*
    191 	 * XXX: Register sysmon environment.
    192 	 */
    193 
    194 	/*
    195 	 * Register sysmon events
    196 	 */
    197 	sc->sc_sm_pbutton.smpsw_name = xname;
    198 	sc->sc_sm_pbutton.smpsw_type = PSWITCH_TYPE_POWER;
    199 	if (sysmon_pswitch_register(&sc->sc_sm_pbutton) != 0)
    200 		printf("%s: unable to register power button\n", xname);
    201 
    202 	sc->sc_sm_lid.smpsw_name = xname;
    203 	sc->sc_sm_lid.smpsw_type = PSWITCH_TYPE_LID;
    204 	if (sysmon_pswitch_register(&sc->sc_sm_lid) != 0)
    205 		printf("%s: unable to register lid switch\n", xname);
    206 
    207 	sc->sc_sm_ac.smpsw_name = xname;
    208 	sc->sc_sm_ac.smpsw_type = PSWITCH_TYPE_ACADAPTER;
    209 	if (sysmon_pswitch_register(&sc->sc_sm_ac) != 0)
    210 		printf("%s: unable to register AC adapter\n", xname);
    211 }
    212 
    213 void
    214 psm_event_thread(void *arg)
    215 {
    216 	struct psm_softc *sc = arg;
    217 	int	x;
    218 	int	event;
    219 	int	flags;
    220 
    221 	for (;;) {
    222 		x = splhigh();
    223 		/* check for AC power.  sets event if there is a change */
    224 		psm_poll_acpower(sc);
    225 
    226 		/* read and clear events */
    227 		event = sc->sc_event;
    228 		flags = sc->sc_flags;
    229 		sc->sc_event = 0;
    230 		splx(x);
    231 
    232 		if (event & PSM_EV_PBUTTON) {
    233 			sysmon_pswitch_event(&sc->sc_sm_pbutton,
    234 			    PSWITCH_EVENT_PRESSED);
    235 		}
    236 
    237 		if (event & PSM_EV_LID) {
    238 			sysmon_pswitch_event(&sc->sc_sm_lid,
    239 			    flags & PSM_FLAG_LIDCLOSED ?
    240 			    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    241 		}
    242 
    243 		if (event & PSM_EV_ACPWR) {
    244 			sysmon_pswitch_event(&sc->sc_sm_ac,
    245 			    flags & PSM_FLAG_ACPWR ?
    246 			    PSWITCH_EVENT_PRESSED : PSWITCH_EVENT_RELEASED);
    247 		}
    248 
    249 		/* XXX: handle PSM_EV_TEMP */
    250 
    251 		/* one second interval between probes of power */
    252 		tsleep(&sc, PWAIT, "psm", hz);
    253 	}
    254 }
    255 
    256 int
    257 psm_init(struct psm_softc *sc)
    258 {
    259 	int	x;
    260 	uint8_t	batt = 0xff;	/* keep GCC 4.x happy */
    261 
    262 	/* clear interrupts */
    263 	x = splhigh();
    264 	PUT8(sc, PSM_ICR, 0xff);
    265 	splx(x);
    266 
    267 	/* enable interrupts */
    268 	if (psm_misc_wr(sc, PSM_MISC_IMR, PSM_IMR_ALL))
    269 		return (-1);
    270 
    271 	/* make sure that UPS battery is reasonable */
    272 	if (psm_misc_rd(sc, PSM_MISC_UPS, &batt) || (batt > PSM_MAX_BATTERIES))
    273 		if (psm_misc_wr(sc, PSM_MISC_UPS, batt))
    274 			aprint_error_dev(&sc->sc_dev, "cannot set UPS battery");
    275 
    276 	return (0);
    277 }
    278 
    279 void
    280 psm_reset(struct psm_softc *sc)
    281 {
    282 
    283 	PUT8(sc, PSM_MCR, PSM_MCR_RST);
    284 	delay(RESET_DELAY);
    285 }
    286 
    287 void
    288 psm_poll_acpower(struct psm_softc *sc)
    289 {
    290 	int	flags = sc->sc_flags;
    291 
    292 	if (GET8(sc, PSM_STAT) & PSM_STAT_AC) {
    293 		sc->sc_flags |= PSM_FLAG_ACPWR;
    294 	} else {
    295 		sc->sc_flags &= ~PSM_FLAG_ACPWR;
    296 	}
    297 	if (flags != sc->sc_flags)
    298 		sc->sc_event |= PSM_EV_ACPWR;
    299 }
    300 
    301 int
    302 psm_misc_rd(struct psm_softc *sc, uint8_t mreg, uint8_t *data)
    303 {
    304 
    305 	return (psm_ecmd_rd8(sc, data, mreg, PSM_MODE_MISC, 0));
    306 }
    307 
    308 int
    309 psm_misc_wr(struct psm_softc *sc, uint8_t mreg, uint8_t data)
    310 {
    311 
    312 	return (psm_ecmd_wr8(sc, data, mreg, PSM_MODE_MISC, 0));
    313 }
    314 
    315 int
    316 psm_wait(struct psm_softc *sc, uint8_t flag)
    317 {
    318     int retr = WAIT_RETRIES;
    319 
    320     while (GET8(sc, PSM_STAT) & flag) {
    321 	    if (!(retr--)) {
    322 		    return (-1);
    323 	    }
    324 	    delay(WAIT_DELAY);
    325     }
    326     return (0);
    327 }
    328 
    329 #if 0
    330 int
    331 psm_ecmd_rd16(struct psm_softc *sc, uint16_t *data, uint8_t iar, uint8_t mode,
    332     uint8_t addr)
    333 {
    334 	uint8_t	cmr = PSM_CMR_DATA(mode, PSM_L_16, PSM_D_RD, addr);
    335 	int	x, rc, retr = CMD_RETRIES;
    336 
    337 	x = splhigh();
    338 
    339 	do {
    340 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) != 0) {
    341 			psm_reset(sc);
    342 			continue;
    343 		}
    344 
    345 		PUT8(sc, PSM_IAR, iar);
    346 		PUT8(sc, PSM_CMR, cmr);
    347 
    348 		delay(CMD_DELAY);
    349 
    350 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) == 0) {
    351 			*data = GET8(sc, PSM_PWDL) | (GET8(sc, PSM_PWDU) << 8);
    352 			break;
    353 		}
    354 
    355 		psm_reset(sc);
    356 
    357 	} while (--retr);
    358 
    359 	splx(x);
    360 	return (rc);
    361 }
    362 #endif
    363 
    364 int
    365 psm_ecmd_rd8(struct psm_softc *sc, uint8_t *data, uint8_t iar, uint8_t mode,
    366     uint8_t addr)
    367 {
    368 	uint8_t	cmr = PSM_CMR_DATA(mode, PSM_L_8, PSM_D_RD, addr);
    369 	int	x, rc, retr = CMD_RETRIES;
    370 
    371 	x = splhigh();
    372 
    373 	do {
    374 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) != 0) {
    375 			psm_reset(sc);
    376 			continue;
    377 		}
    378 
    379 		PUT8(sc, PSM_IAR, iar);
    380 		PUT8(sc, PSM_CMR, cmr);
    381 
    382 		delay(CMD_DELAY);
    383 
    384 		if ((rc = psm_wait(sc, PSM_STAT_RDA)) == 0) {
    385 			(void) GET8(sc, PSM_PWDU);
    386 			*data = GET8(sc, PSM_PWDL);
    387 			break;
    388 		}
    389 
    390 		psm_reset(sc);
    391 
    392 	} while (--retr);
    393 
    394 	splx(x);
    395 	return (rc);
    396 }
    397 
    398 int
    399 psm_ecmd_wr8(struct psm_softc *sc, uint8_t data, uint8_t iar, uint8_t mode,
    400     uint8_t addr)
    401 {
    402 	uint8_t	cmr = PSM_CMR_DATA(mode, PSM_L_8, PSM_D_WR, addr);
    403 	int	x, rc, retr = CMD_RETRIES;
    404 
    405 	x = splhigh();
    406 
    407 	do {
    408 		if ((rc = psm_wait(sc, PSM_STAT_WBF)) != 0) {
    409 			psm_reset(sc);
    410 			continue;
    411 		}
    412 
    413 		PUT8(sc, PSM_PWDU, 0);
    414 		PUT8(sc, PSM_PWDL, data);
    415 		PUT8(sc, PSM_IAR, iar);
    416 		PUT8(sc, PSM_CMR, cmr);
    417 
    418 		delay(CMD_DELAY);
    419 
    420 		if ((rc = psm_wait(sc, PSM_STAT_WBF)) == 0) {
    421 			break;
    422 		}
    423 
    424 		psm_reset(sc);
    425 	} while (--retr);
    426 
    427 	splx(x);
    428 
    429 	return (rc);
    430 }
    431 
    432 int
    433 psm_intr(void *arg)
    434 {
    435 	struct psm_softc *sc = arg;
    436 	uint8_t	isr;
    437 
    438 	isr = GET8(sc, PSM_ISR);
    439 	if (isr & PSM_ISR_PO) {
    440 		PUT8(sc, PSM_ICR, PSM_ISR_PO);
    441 		sc->sc_event |= PSM_EV_PBUTTON;
    442 	}
    443 	if (isr & PSM_ISR_DK) {
    444 		PUT8(sc, PSM_ICR, PSM_ISR_DK);
    445 		sc->sc_flags |= PSM_FLAG_DOCKED;
    446 	}
    447 	if (isr & PSM_ISR_UDK) {
    448 		PUT8(sc, PSM_ICR, PSM_ISR_UDK);
    449 		sc->sc_flags &= ~PSM_FLAG_DOCKED;
    450 	}
    451 	if (isr & PSM_ISR_LIDC) {
    452 		PUT8(sc, PSM_ICR, PSM_ISR_LIDC);
    453 		sc->sc_flags |= PSM_FLAG_LIDCLOSED;
    454 		sc->sc_event |= PSM_EV_LID;
    455 	}
    456 	if (isr & PSM_ISR_LIDO) {
    457 		PUT8(sc, PSM_ICR, PSM_ISR_LIDO);
    458 		sc->sc_flags &= ~PSM_FLAG_LIDCLOSED;
    459 		sc->sc_event |= PSM_EV_LID;
    460 	}
    461 	if (isr & PSM_ISR_TMP) {
    462 		/* Over temperature */
    463 		PUT8(sc, PSM_ICR, PSM_ISR_TMP);
    464 		sc->sc_event |= PSM_EV_TEMP;
    465 	}
    466 	if (isr & PSM_ISR_BCC) {
    467 		/* battery config changed */
    468 		PUT8(sc, PSM_ICR, PSM_ISR_BCC);
    469 		sc->sc_event |= PSM_EV_BATT;
    470 	}
    471 	if (isr & PSM_ISR_RPD) {
    472 		/* request to power down */
    473 		sc->sc_event |= PSM_EV_PBUTTON;
    474 	}
    475 	if (sc->sc_event) {
    476 		/* wake up the thread */
    477 		wakeup(sc);
    478 	}
    479 	return (1);
    480 }
    481