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