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