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