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