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