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