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