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