Home | History | Annotate | Line # | Download | only in dev
pmu.c revision 1.8
      1 /*	$NetBSD: pmu.c,v 1.8 2007/08/08 22:19:55 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.8 2007/08/08 22:19:55 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 #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 		cmd[0] = 0;
    355 		cmd[1] = 0;
    356 		memset(resp, 0, 6);
    357 		if (pmu_send(sc, PMU_READ_BRIGHTNESS, 1, cmd, 16, resp) > 1) {
    358 			sc->sc_brightness_wanted = resp[1];
    359 			pmu_update_brightness(sc);
    360 		}
    361 	}
    362 
    363 	/* attach batteries */
    364 	if (of_compatible(root_node, has_legacy_battery) != -1) {
    365 
    366 		pmu_attach_legacy_battery(sc);
    367 	} else if (of_compatible(root_node, has_two_smart_batteries) != -1) {
    368 
    369 		pmu_attach_smart_battery(sc, 0);
    370 		pmu_attach_smart_battery(sc, 1);
    371 	} else {
    372 
    373 		/* check how many batteries we have */
    374 		pmnode = getnodebyname(ca->ca_node, "power-mgt");
    375 		if (pmnode == -1)
    376 			goto bat_done;
    377 		if (OF_getprop(pmnode, "prim-info", regs, sizeof(regs)) < 24)
    378 			goto bat_done;
    379 		nbat = regs[6] >> 16;
    380 		for (i = 0; i < nbat; i++)
    381 			pmu_attach_smart_battery(sc, i);
    382 	}
    383 bat_done:
    384 
    385 #if notyet
    386 	iba.iba_tag = &sc->sc_i2c;
    387 	sc->sc_i2c.ic_cookie = sc;
    388 	sc->sc_i2c.ic_acquire_bus = pmu_i2c_acquire_bus;
    389 	sc->sc_i2c.ic_release_bus = pmu_i2c_release_bus;
    390 	sc->sc_i2c.ic_send_start = NULL;
    391 	sc->sc_i2c.ic_send_stop = NULL;
    392 	sc->sc_i2c.ic_initiate_xfer = NULL;
    393 	sc->sc_i2c.ic_read_byte = NULL;
    394 	sc->sc_i2c.ic_write_byte = NULL;
    395 	sc->sc_i2c.ic_exec = pmu_i2c_exec;
    396 	config_found_ia(&sc->sc_dev, "i2cbus", &iba, iicbus_print);
    397 #endif
    398 
    399 	if (kthread_create(PRI_NONE, 0, NULL, pmu_thread, sc, &sc->sc_thread,
    400 	    "%s", "pmu") != 0) {
    401 		printf("pmu: unable to create event kthread");
    402 	}
    403 }
    404 
    405 static void
    406 pmu_register_callback(void *pmu_cookie, void (*cb)(void *), void *cookie)
    407 {
    408 	struct pmu_softc *sc = pmu_cookie;
    409 
    410 	sc->sc_callback = cb;
    411 	sc->sc_cb_cookie = cookie;
    412 }
    413 
    414 static void
    415 pmu_init(struct pmu_softc *sc)
    416 {
    417 	uint8_t pmu_imask, resp[16];
    418 
    419 	pmu_imask =
    420 	    PMU_INT_PCEJECT | PMU_INT_SNDBRT | PMU_INT_ADB/* | PMU_INT_TICK*/;
    421 	pmu_imask |= PMU_INT_BATTERY;
    422 	pmu_imask |= PMU_INT_ENVIRONMENT;
    423 	pmu_send(sc, PMU_SET_IMASK, 1, &pmu_imask, 16, resp);
    424 
    425 	pmu_write_reg(sc, vIER, 0x90);	/* enable VIA interrupts */
    426 }
    427 
    428 static inline void
    429 pmu_write_reg(struct pmu_softc *sc, int offset, uint8_t value)
    430 {
    431 
    432 	bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value);
    433 }
    434 
    435 static inline uint8_t
    436 pmu_read_reg(struct pmu_softc *sc, int offset)
    437 {
    438 
    439 	return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset);
    440 }
    441 
    442 static inline int
    443 pmu_send_byte(struct pmu_softc *sc, uint8_t data)
    444 {
    445 
    446 	pmu_out(sc);
    447 	pmu_write_reg(sc, vSR, data);
    448 	pmu_ack_off(sc);
    449 	/* wait for intr to come up */
    450 	/* XXX should add a timeout and bail if it expires */
    451 	do {} while (pmu_intr_state(sc) == 0);
    452 	pmu_ack_on(sc);
    453 	do {} while (pmu_intr_state(sc));
    454 	pmu_ack_on(sc);
    455 	DPRINTF(" %02x>", data);
    456 	return 0;
    457 }
    458 
    459 static inline int
    460 pmu_read_byte(struct pmu_softc *sc, uint8_t *data)
    461 {
    462 	volatile uint8_t scratch;
    463 	pmu_in(sc);
    464 	scratch = pmu_read_reg(sc, vSR);
    465 	pmu_ack_off(sc);
    466 	/* wait for intr to come up */
    467 	do {} while (pmu_intr_state(sc) == 0);
    468 	pmu_ack_on(sc);
    469 	do {} while (pmu_intr_state(sc));
    470 	*data = pmu_read_reg(sc, vSR);
    471 	DPRINTF(" <%02x", *data);
    472 	return 0;
    473 }
    474 
    475 static int
    476 pmu_send(void *cookie, int cmd, int length, uint8_t *in_msg, int rlen,
    477     uint8_t *out_msg)
    478 {
    479 	struct pmu_softc *sc = cookie;
    480 	int i, rcv_len = -1, s;
    481 	uint8_t out_len, intreg;
    482 
    483 	DPRINTF("pmu_send: ");
    484 
    485 	s = splhigh();
    486 	intreg = pmu_read_reg(sc, vIER);
    487 	intreg &= 0x10;
    488 	pmu_write_reg(sc, vIER, intreg);
    489 
    490 	/* wait idle */
    491 	do {} while (pmu_intr_state(sc));
    492 	sc->sc_error = 0;
    493 
    494 	/* send command */
    495 	pmu_send_byte(sc, cmd);
    496 
    497 	/* send length if necessary */
    498 	if (pm_send_cmd_type[cmd] < 0) {
    499 		pmu_send_byte(sc, length);
    500 	}
    501 
    502 	for (i = 0; i < length; i++) {
    503 		pmu_send_byte(sc, in_msg[i]);
    504 		DPRINTF(" next ");
    505 	}
    506 	DPRINTF("done sending\n");
    507 
    508 	/* see if there's data to read */
    509 	rcv_len = pm_receive_cmd_type[cmd];
    510 	if (rcv_len == 0)
    511 		goto done;
    512 
    513 	/* read command */
    514 	if (rcv_len == 1) {
    515 		pmu_read_byte(sc, out_msg);
    516 		goto done;
    517 	} else
    518 		out_msg[0] = cmd;
    519 	if (rcv_len < 0) {
    520 		pmu_read_byte(sc, &out_len);
    521 		rcv_len = out_len + 1;
    522 	}
    523 	for (i = 1; i < min(rcv_len, rlen); i++)
    524 		pmu_read_byte(sc, &out_msg[i]);
    525 
    526 done:
    527 	DPRINTF("\n");
    528 	pmu_write_reg(sc, vIER, (intreg == 0) ? 0 : 0x90);
    529 	splx(s);
    530 
    531 	return rcv_len;
    532 }
    533 
    534 static void
    535 pmu_adb_poll(void *cookie)
    536 {
    537 	struct pmu_softc *sc = cookie;
    538 
    539 	pmu_intr(sc);
    540 }
    541 
    542 static void
    543 pmu_in(struct pmu_softc *sc)
    544 {
    545 	uint8_t reg;
    546 
    547 	reg = pmu_read_reg(sc, vACR);
    548 	reg &= ~vSR_OUT;
    549 	reg |= 0x0c;
    550 	pmu_write_reg(sc, vACR, reg);
    551 }
    552 
    553 static void
    554 pmu_out(struct pmu_softc *sc)
    555 {
    556 	uint8_t reg;
    557 
    558 	reg = pmu_read_reg(sc, vACR);
    559 	reg |= vSR_OUT;
    560 	reg |= 0x0c;
    561 	pmu_write_reg(sc, vACR, reg);
    562 }
    563 
    564 static void
    565 pmu_ack_off(struct pmu_softc *sc)
    566 {
    567 	uint8_t reg;
    568 
    569 	reg = pmu_read_reg(sc, vBufB);
    570 	reg &= ~vPB4;
    571 	pmu_write_reg(sc, vBufB, reg);
    572 }
    573 
    574 static void
    575 pmu_ack_on(struct pmu_softc *sc)
    576 {
    577 	uint8_t reg;
    578 
    579 	reg = pmu_read_reg(sc, vBufB);
    580 	reg |= vPB4;
    581 	pmu_write_reg(sc, vBufB, reg);
    582 }
    583 
    584 static int
    585 pmu_intr_state(struct pmu_softc *sc)
    586 {
    587 	return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
    588 }
    589 
    590 static int
    591 pmu_intr(void *arg)
    592 {
    593 	struct pmu_softc *sc = arg;
    594 	unsigned int s, len, i;
    595 	uint8_t resp[16];
    596 
    597 	s = splhigh();		/* can't be too careful - might be called */
    598 				/* from a routine, NOT an interrupt */
    599 
    600 	DPRINTF(":");
    601 
    602 	pmu_write_reg(sc, vIFR, 0x90);	/* Clear 'em */
    603 	len = pmu_send(sc, PMU_INT_ACK, 0, NULL, 16, resp);
    604 	if ((len < 1) || (resp[1] == 0))
    605 		goto done;
    606 #ifdef PMU_DEBUG
    607 	{
    608 		DPRINTF("intr: %02x", resp[0]);
    609 		for (i = 1; i < len; i++)
    610 			DPRINTF(" %02x", resp[i]);
    611 		DPRINTF("\n");
    612 	}
    613 #endif
    614 	if (resp[1] & PMU_INT_ADB) {
    615 		pmu_adb_handler(sc, len - 1, &resp[1]);
    616 		goto done;
    617 	}
    618 	if (resp[1] & PMU_INT_SNDBRT) {
    619 		/* deal with the brightness / volume control buttons */
    620 		DPRINTF("brightness: %d volume %d\n", resp[2], resp[3]);
    621 		sc->sc_brightness_wanted = resp[2];
    622 		sc->sc_volume_wanted = resp[3];
    623 		wakeup(&sc->sc_event);
    624 		goto done;
    625 	}
    626 	if (resp[1] & PMU_INT_PCEJECT) {
    627 		/* deal with PCMCIA eject buttons */
    628 		DPRINTF("card eject %d\n", resp[3]);
    629 		sc->sc_pending_eject |= (resp[3] & 3);
    630 		wakeup(&sc->sc_event);
    631 		goto done;
    632 	}
    633 	if (resp[1] & PMU_INT_BATTERY) {
    634 		/* deal with battery messages */
    635 		printf("battery:");
    636 		for (i = 2; i < len; i++)
    637 			printf(" %02x", resp[i]);
    638 		printf("\n");
    639 		goto done;
    640 	}
    641 	if (resp[1] & PMU_INT_ENVIRONMENT) {
    642 #ifdef PMU_VERBOSE
    643 		/* deal with environment messages */
    644 		printf("environment:");
    645 		for (i = 2; i < len; i++)
    646 			printf(" %02x", resp[i]);
    647 		printf("\n");
    648 #endif
    649 		goto done;
    650 	}
    651 	if (resp[1] & PMU_INT_TICK) {
    652 		/* don't bother */
    653 		goto done;
    654 	}
    655 
    656 	/* unknown interrupt code?! */
    657 #ifdef PMU_DEBUG
    658 	printf("pmu intr: %02x:", resp[1]);
    659 	for (i = 2; i < len; i++)
    660 		printf(" %02x", resp[i]);
    661 	printf("\n");
    662 #endif
    663 done:
    664 	splx(s);
    665 	return 1;
    666 }
    667 
    668 #if 0
    669 static int
    670 pmu_error_handler(void *cookie, int len, uint8_t *data)
    671 {
    672 	struct pmu_softc *sc = cookie;
    673 
    674 	/*
    675 	 * something went wrong
    676 	 * byte 3 seems to be the failed command
    677 	 */
    678 	sc->sc_error = 1;
    679 	wakeup(&sc->sc_todev);
    680 	return 0;
    681 }
    682 #endif
    683 #define DIFF19041970 2082844800
    684 
    685 static int
    686 pmu_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
    687 {
    688 	struct pmu_softc *sc = tch->cookie;
    689 	uint32_t sec;
    690 	uint8_t resp[16];
    691 
    692 	DPRINTF("pmu_todr_get\n");
    693 	pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
    694 
    695 	memcpy(&sec, &resp[1], 4);
    696 	tvp->tv_sec = sec - DIFF19041970;
    697 	DPRINTF("tod: %ld\n", tvp->tv_sec);
    698 	tvp->tv_usec = 0;
    699 	return 0;
    700 }
    701 
    702 static int
    703 pmu_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
    704 {
    705 	struct pmu_softc *sc = tch->cookie;
    706 	uint32_t sec;
    707 	uint8_t resp[16];
    708 
    709 	sec = tvp->tv_sec + DIFF19041970;
    710 	if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0)
    711 		return 0;
    712 	return -1;
    713 }
    714 
    715 void
    716 pmu_poweroff()
    717 {
    718 	struct pmu_softc *sc;
    719 	uint8_t cmd[] = {'M', 'A', 'T', 'T'};
    720 	uint8_t resp[16];
    721 
    722 	if (pmu0 == NULL)
    723 		return;
    724 	sc = pmu0;
    725 	if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0)
    726 		while (1);
    727 }
    728 
    729 void
    730 pmu_restart()
    731 {
    732 	struct pmu_softc *sc;
    733 	uint8_t resp[16];
    734 
    735 	if (pmu0 == NULL)
    736 		return;
    737 	sc = pmu0;
    738 	if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0)
    739 		while (1);
    740 }
    741 
    742 static void
    743 pmu_autopoll(void *cookie, int flag)
    744 {
    745 	struct pmu_softc *sc = cookie;
    746 	/* magical incantation to re-enable autopolling */
    747 	uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff};
    748 	uint8_t resp[16];
    749 
    750 	if (sc->sc_autopoll == flag)
    751 		return;
    752 
    753 	if (flag) {
    754 		pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
    755 	} else {
    756 		pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
    757 	}
    758 	sc->sc_autopoll = flag & 0xffff;
    759 }
    760 
    761 static int
    762 pmu_adb_handler(void *cookie, int len, uint8_t *data)
    763 {
    764 	struct pmu_softc *sc = cookie;
    765 	uint8_t resp[16];
    766 
    767 	if (sc->sc_adb_handler != NULL) {
    768 		sc->sc_adb_handler(sc->sc_adb_cookie, len, data);
    769 		/*
    770 		 * the PMU will turn off autopolling after each LISTEN so we
    771 		 * need to re-enable it here whenever we receive an ACK for a
    772 		 * LISTEN command
    773 		 */
    774 		if ((data[1] & 0x0c) == 0x08) {
    775 			uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff,
    776 			    sc->sc_autopoll & 0xff};
    777 			pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
    778 		}
    779 		return 0;
    780 	}
    781 	return -1;
    782 }
    783 
    784 static int
    785 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
    786 {
    787 	struct pmu_softc *sc = cookie;
    788 	int i, replen;
    789 	uint8_t packet[16], resp[16];
    790 
    791 	/* construct an ADB command packet and send it */
    792 	packet[0] = command;
    793 	packet[1] = 0;
    794 	packet[2] = len;
    795 	for (i = 0; i < len; i++)
    796 		packet[i + 3] = data[i];
    797 	replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
    798 
    799 	return 0;
    800 }
    801 
    802 static int
    803 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
    804     void *hcookie)
    805 {
    806 	struct pmu_softc *sc = cookie;
    807 
    808 	/* register a callback for incoming ADB messages */
    809 	sc->sc_adb_handler = handler;
    810 	sc->sc_adb_cookie = hcookie;
    811 	return 0;
    812 }
    813 #if 0
    814 static int
    815 pmu_i2c_acquire_bus(void *cookie, int flags)
    816 {
    817 	/* nothing yet */
    818 	return 0;
    819 }
    820 
    821 static void
    822 pmu_i2c_release_bus(void *cookie, int flags)
    823 {
    824 	/* nothing here either */
    825 }
    826 
    827 static int
    828 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
    829     size_t send_len, void *_recv, size_t recv_len, int flags)
    830 {
    831 #if 0
    832 	struct pmu_softc *sc = cookie;
    833 	const uint8_t *send = _send;
    834 	uint8_t *recv = _recv;
    835 	uint8_t command[16] = {PMU_POWERMGR, PMGR_IIC};
    836 
    837 	DPRINTF("pmu_i2c_exec(%02x)\n", addr);
    838 	command[2] = addr;
    839 
    840 	memcpy(&command[3], send, min((int)send_len, 12));
    841 
    842 	sc->sc_iic_done = 0;
    843 	pmu_send(sc, sc->sc_polling, send_len + 3, command);
    844 
    845 	while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
    846 		if (sc->sc_polling) {
    847 			pmu_poll(sc);
    848 		} else
    849 			tsleep(&sc->sc_todev, 0, "i2c", 1000);
    850 	}
    851 
    852 	if (sc->sc_error) {
    853 		sc->sc_error = 0;
    854 		return -1;
    855 	}
    856 
    857 	/* see if we're supposed to do a read */
    858 	if (recv_len > 0) {
    859 		sc->sc_iic_done = 0;
    860 		command[2] |= 1;
    861 		command[3] = 0;
    862 
    863 		/*
    864 		 * XXX we need to do something to limit the size of the answer
    865 		 * - apparently the chip keeps sending until we tell it to stop
    866 		 */
    867 		pmu_send(sc, sc->sc_polling, 3, command);
    868 		while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
    869 			if (sc->sc_polling) {
    870 				pmu_poll(sc);
    871 			} else
    872 				tsleep(&sc->sc_todev, 0, "i2c", 1000);
    873 		}
    874 
    875 		if (sc->sc_error) {
    876 			printf("error trying to read\n");
    877 			sc->sc_error = 0;
    878 			return -1;
    879 		}
    880 	}
    881 
    882 	if ((sc->sc_iic_done > 3) && (recv_len > 0)) {
    883 		/* we got an answer */
    884 		recv[0] = sc->sc_iic_val;
    885 		printf("ret: %02x\n", sc->sc_iic_val);
    886 		return 1;
    887 	}
    888 #endif
    889 	return 0;
    890 }
    891 #endif
    892 
    893 static void
    894 pmu_eject_card(struct pmu_softc *sc, int socket)
    895 {
    896 	int s;
    897 	uint8_t buf[] = {socket | 4};
    898 	uint8_t res[4];
    899 
    900 	s = splhigh();
    901 	sc->sc_pending_eject &= ~socket;
    902 	splx(s);
    903 	pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res);
    904 }
    905 
    906 static void
    907 pmu_update_brightness(struct pmu_softc *sc)
    908 {
    909 	int val;
    910 	uint8_t cmd[2], resp[16];
    911 
    912 	if (sc->sc_brightness == sc->sc_brightness_wanted)
    913 		return;
    914 
    915 	if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) {
    916 
    917 		printf("%s: this PMU doesn't support backlight control\n",
    918 			sc->sc_dev.dv_xname);
    919 		sc->sc_brightness = sc->sc_brightness_wanted;
    920 		return;
    921 	}
    922 
    923 	if (sc->sc_brightness_wanted == 0) {
    924 
    925 		/* turn backlight off completely */
    926 		cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT;
    927 		pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
    928 		sc->sc_brightness = sc->sc_brightness_wanted;
    929 
    930 		/* don't bother with brightness */
    931 		return;
    932 	}
    933 
    934 	/* turn backlight on if needed */
    935 	if (sc->sc_brightness == 0) {
    936 		cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT;
    937 		pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
    938 	}
    939 
    940 	DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness,
    941 	    sc->sc_brightness_wanted);
    942 
    943 	val = 0x7f - (sc->sc_brightness_wanted >> 1);
    944 	if (val < 0x08)
    945 		val = 0x08;
    946 	if (val > 0x78)
    947 		val = 0x78;
    948 	cmd[0] = val;
    949 	pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp);
    950 
    951 	sc->sc_brightness = sc->sc_brightness_wanted;
    952 }
    953 
    954 static void
    955 pmu_thread(void *cookie)
    956 {
    957 	struct pmu_softc *sc = cookie;
    958 	//time_t time_bat = time_second;
    959 	int ticks = hz, i;
    960 
    961 	while (1) {
    962 		tsleep(&sc->sc_event, PWAIT, "wait", ticks);
    963 		if (sc->sc_pending_eject != 0) {
    964 			DPRINTF("eject %d\n", sc->sc_pending_eject);
    965 			for (i = 1; i < 3; i++) {
    966 				if (i & sc->sc_pending_eject)
    967 					pmu_eject_card(sc, i);
    968 			}
    969 		}
    970 
    971 		/* see if we need to update brightness */
    972 		if (sc->sc_brightness_wanted != sc->sc_brightness) {
    973 			pmu_update_brightness(sc);
    974 		}
    975 
    976 		/* see if we need to update audio volume */
    977 		if (sc->sc_volume_wanted != sc->sc_volume) {
    978 #if 0
    979 			set_volume(sc->sc_volume_wanted);
    980 #endif
    981 			sc->sc_volume = sc->sc_volume_wanted;
    982 		}
    983 
    984 		if (sc->sc_callback != NULL)
    985 			sc->sc_callback(sc->sc_cb_cookie);
    986 	}
    987 }
    988 
    989 static int
    990 pmu_print(void *aux, const char *what)
    991 {
    992 
    993 	return 0;
    994 }
    995 
    996 static void
    997 pmu_attach_legacy_battery(struct pmu_softc *sc)
    998 {
    999 	struct battery_attach_args baa;
   1000 
   1001 	baa.baa_type = BATTERY_TYPE_LEGACY;
   1002 	baa.baa_pmu_ops = &sc->sc_pmu_ops;
   1003 	config_found_ia(&sc->sc_dev, "pmu_bus", &baa, pmu_print);
   1004 }
   1005 
   1006 static void
   1007 pmu_attach_smart_battery(struct pmu_softc *sc, int num)
   1008 {
   1009 	struct battery_attach_args baa;
   1010 
   1011 	baa.baa_type = BATTERY_TYPE_SMART;
   1012 	baa.baa_pmu_ops = &sc->sc_pmu_ops;
   1013 	baa.baa_num = num;
   1014 	config_found_ia(&sc->sc_dev, "pmu_bus", &baa, pmu_print);
   1015 }
   1016