pmu.c revision 1.12 1 /* $NetBSD: pmu.c,v 1.12 2008/03/30 17:57:29 macallan Exp $ */
2
3 /*-
4 * Copyright (c) 2006 Michael Lorenz
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of The NetBSD Foundation nor the names of its
16 * contributors may be used to endorse or promote products derived
17 * from this software without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: pmu.c,v 1.12 2008/03/30 17:57:29 macallan Exp $");
34
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/device.h>
39 #include <sys/proc.h>
40 #include <sys/kthread.h>
41
42 #include <machine/bus.h>
43 #include <machine/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 int s;
543
544 s = spltty();
545 pmu_intr(sc);
546 splx(s);
547 }
548
549 static void
550 pmu_in(struct pmu_softc *sc)
551 {
552 uint8_t reg;
553
554 reg = pmu_read_reg(sc, vACR);
555 reg &= ~vSR_OUT;
556 reg |= 0x0c;
557 pmu_write_reg(sc, vACR, reg);
558 }
559
560 static void
561 pmu_out(struct pmu_softc *sc)
562 {
563 uint8_t reg;
564
565 reg = pmu_read_reg(sc, vACR);
566 reg |= vSR_OUT;
567 reg |= 0x0c;
568 pmu_write_reg(sc, vACR, reg);
569 }
570
571 static void
572 pmu_ack_off(struct pmu_softc *sc)
573 {
574 uint8_t reg;
575
576 reg = pmu_read_reg(sc, vBufB);
577 reg &= ~vPB4;
578 pmu_write_reg(sc, vBufB, reg);
579 }
580
581 static void
582 pmu_ack_on(struct pmu_softc *sc)
583 {
584 uint8_t reg;
585
586 reg = pmu_read_reg(sc, vBufB);
587 reg |= vPB4;
588 pmu_write_reg(sc, vBufB, reg);
589 }
590
591 static int
592 pmu_intr_state(struct pmu_softc *sc)
593 {
594 return ((pmu_read_reg(sc, vBufB) & vPB3) == 0);
595 }
596
597 static int
598 pmu_intr(void *arg)
599 {
600 struct pmu_softc *sc = arg;
601 unsigned int len, i;
602 uint8_t resp[16];
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 return 1;
669 }
670
671 #if 0
672 static int
673 pmu_error_handler(void *cookie, int len, uint8_t *data)
674 {
675 struct pmu_softc *sc = cookie;
676
677 /*
678 * something went wrong
679 * byte 3 seems to be the failed command
680 */
681 sc->sc_error = 1;
682 wakeup(&sc->sc_todev);
683 return 0;
684 }
685 #endif
686 #define DIFF19041970 2082844800
687
688 static int
689 pmu_todr_get(todr_chip_handle_t tch, volatile struct timeval *tvp)
690 {
691 struct pmu_softc *sc = tch->cookie;
692 uint32_t sec;
693 uint8_t resp[16];
694
695 DPRINTF("pmu_todr_get\n");
696 pmu_send(sc, PMU_READ_RTC, 0, NULL, 16, resp);
697
698 memcpy(&sec, &resp[1], 4);
699 tvp->tv_sec = sec - DIFF19041970;
700 DPRINTF("tod: %ld\n", tvp->tv_sec);
701 tvp->tv_usec = 0;
702 return 0;
703 }
704
705 static int
706 pmu_todr_set(todr_chip_handle_t tch, volatile struct timeval *tvp)
707 {
708 struct pmu_softc *sc = tch->cookie;
709 uint32_t sec;
710 uint8_t resp[16];
711
712 sec = tvp->tv_sec + DIFF19041970;
713 if (pmu_send(sc, PMU_SET_RTC, 4, (uint8_t *)&sec, 16, resp) >= 0)
714 return 0;
715 return -1;
716 }
717
718 void
719 pmu_poweroff()
720 {
721 struct pmu_softc *sc;
722 uint8_t cmd[] = {'M', 'A', 'T', 'T'};
723 uint8_t resp[16];
724
725 if (pmu0 == NULL)
726 return;
727 sc = pmu0;
728 if (pmu_send(sc, PMU_POWER_OFF, 4, cmd, 16, resp) >= 0)
729 while (1);
730 }
731
732 void
733 pmu_restart()
734 {
735 struct pmu_softc *sc;
736 uint8_t resp[16];
737
738 if (pmu0 == NULL)
739 return;
740 sc = pmu0;
741 if (pmu_send(sc, PMU_RESET_CPU, 0, NULL, 16, resp) >= 0)
742 while (1);
743 }
744
745 static void
746 pmu_autopoll(void *cookie, int flag)
747 {
748 struct pmu_softc *sc = cookie;
749 /* magical incantation to re-enable autopolling */
750 uint8_t cmd[] = {0, PMU_SET_POLL_MASK, (flag >> 8) & 0xff, flag & 0xff};
751 uint8_t resp[16];
752
753 if (sc->sc_autopoll == flag)
754 return;
755
756 if (flag) {
757 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
758 } else {
759 pmu_send(sc, PMU_ADB_POLL_OFF, 0, NULL, 16, resp);
760 }
761 sc->sc_autopoll = flag & 0xffff;
762 }
763
764 static int
765 pmu_adb_handler(void *cookie, int len, uint8_t *data)
766 {
767 struct pmu_softc *sc = cookie;
768 uint8_t resp[16];
769
770 if (sc->sc_adb_handler != NULL) {
771 sc->sc_adb_handler(sc->sc_adb_cookie, len, data);
772 /*
773 * the PMU will turn off autopolling after each LISTEN so we
774 * need to re-enable it here whenever we receive an ACK for a
775 * LISTEN command
776 */
777 if ((data[1] & 0x0c) == 0x08) {
778 uint8_t cmd[] = {0, 0x86, (sc->sc_autopoll >> 8) & 0xff,
779 sc->sc_autopoll & 0xff};
780 pmu_send(sc, PMU_ADB_CMD, 4, cmd, 16, resp);
781 }
782 return 0;
783 }
784 return -1;
785 }
786
787 static int
788 pmu_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
789 {
790 struct pmu_softc *sc = cookie;
791 int i, replen;
792 uint8_t packet[16], resp[16];
793
794 /* construct an ADB command packet and send it */
795 packet[0] = command;
796 packet[1] = 0;
797 packet[2] = len;
798 for (i = 0; i < len; i++)
799 packet[i + 3] = data[i];
800 replen = pmu_send(sc, PMU_ADB_CMD, len + 3, packet, 16, resp);
801
802 return 0;
803 }
804
805 static int
806 pmu_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
807 void *hcookie)
808 {
809 struct pmu_softc *sc = cookie;
810
811 /* register a callback for incoming ADB messages */
812 sc->sc_adb_handler = handler;
813 sc->sc_adb_cookie = hcookie;
814 return 0;
815 }
816 #if 0
817 static int
818 pmu_i2c_acquire_bus(void *cookie, int flags)
819 {
820 /* nothing yet */
821 return 0;
822 }
823
824 static void
825 pmu_i2c_release_bus(void *cookie, int flags)
826 {
827 /* nothing here either */
828 }
829
830 static int
831 pmu_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
832 size_t send_len, void *_recv, size_t recv_len, int flags)
833 {
834 #if 0
835 struct pmu_softc *sc = cookie;
836 const uint8_t *send = _send;
837 uint8_t *recv = _recv;
838 uint8_t command[16] = {PMU_POWERMGR, PMGR_IIC};
839
840 DPRINTF("pmu_i2c_exec(%02x)\n", addr);
841 command[2] = addr;
842
843 memcpy(&command[3], send, min((int)send_len, 12));
844
845 sc->sc_iic_done = 0;
846 pmu_send(sc, sc->sc_polling, send_len + 3, command);
847
848 while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
849 if (sc->sc_polling) {
850 pmu_poll(sc);
851 } else
852 tsleep(&sc->sc_todev, 0, "i2c", 1000);
853 }
854
855 if (sc->sc_error) {
856 sc->sc_error = 0;
857 return -1;
858 }
859
860 /* see if we're supposed to do a read */
861 if (recv_len > 0) {
862 sc->sc_iic_done = 0;
863 command[2] |= 1;
864 command[3] = 0;
865
866 /*
867 * XXX we need to do something to limit the size of the answer
868 * - apparently the chip keeps sending until we tell it to stop
869 */
870 pmu_send(sc, sc->sc_polling, 3, command);
871 while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
872 if (sc->sc_polling) {
873 pmu_poll(sc);
874 } else
875 tsleep(&sc->sc_todev, 0, "i2c", 1000);
876 }
877
878 if (sc->sc_error) {
879 printf("error trying to read\n");
880 sc->sc_error = 0;
881 return -1;
882 }
883 }
884
885 if ((sc->sc_iic_done > 3) && (recv_len > 0)) {
886 /* we got an answer */
887 recv[0] = sc->sc_iic_val;
888 printf("ret: %02x\n", sc->sc_iic_val);
889 return 1;
890 }
891 #endif
892 return 0;
893 }
894 #endif
895
896 static void
897 pmu_eject_card(struct pmu_softc *sc, int socket)
898 {
899 int s;
900 uint8_t buf[] = {socket | 4};
901 uint8_t res[4];
902
903 s = splhigh();
904 sc->sc_pending_eject &= ~socket;
905 splx(s);
906 pmu_send(sc, PMU_EJECT_PCMCIA, 1, buf, 4, res);
907 }
908
909 static void
910 pmu_update_brightness(struct pmu_softc *sc)
911 {
912 int val;
913 uint8_t cmd[2], resp[16];
914
915 if (sc->sc_brightness == sc->sc_brightness_wanted)
916 return;
917
918 if ((sc->sc_flags & PMU_HAS_BACKLIGHT_CONTROL) == 0) {
919
920 printf("%s: this PMU doesn't support backlight control\n",
921 sc->sc_dev.dv_xname);
922 sc->sc_brightness = sc->sc_brightness_wanted;
923 return;
924 }
925
926 if (sc->sc_brightness_wanted == 0) {
927
928 /* turn backlight off completely */
929 cmd[0] = PMU_POW_OFF | PMU_POW_BACKLIGHT;
930 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
931 sc->sc_brightness = sc->sc_brightness_wanted;
932
933 /* don't bother with brightness */
934 return;
935 }
936
937 /* turn backlight on if needed */
938 if (sc->sc_brightness == 0) {
939 cmd[0] = PMU_POW_ON | PMU_POW_BACKLIGHT;
940 pmu_send(sc, PMU_POWER_CTRL, 1, cmd, 16, resp);
941 }
942
943 DPRINTF("pmu_update_brightness: %d -> %d\n", sc->sc_brightness,
944 sc->sc_brightness_wanted);
945
946 val = 0x7f - (sc->sc_brightness_wanted >> 1);
947 if (val < 0x08)
948 val = 0x08;
949 if (val > 0x78)
950 val = 0x78;
951 cmd[0] = val;
952 pmu_send(sc, PMU_SET_BRIGHTNESS, 1, cmd, 16, resp);
953
954 sc->sc_brightness = sc->sc_brightness_wanted;
955 }
956
957 static void
958 pmu_thread(void *cookie)
959 {
960 struct pmu_softc *sc = cookie;
961 //time_t time_bat = time_second;
962 int ticks = hz, i;
963
964 while (1) {
965 tsleep(&sc->sc_event, PWAIT, "wait", ticks);
966 if (sc->sc_pending_eject != 0) {
967 DPRINTF("eject %d\n", sc->sc_pending_eject);
968 for (i = 1; i < 3; i++) {
969 if (i & sc->sc_pending_eject)
970 pmu_eject_card(sc, i);
971 }
972 }
973
974 /* see if we need to update brightness */
975 if (sc->sc_brightness_wanted != sc->sc_brightness) {
976 pmu_update_brightness(sc);
977 }
978
979 /* see if we need to update audio volume */
980 if (sc->sc_volume_wanted != sc->sc_volume) {
981 #if 0
982 set_volume(sc->sc_volume_wanted);
983 #endif
984 sc->sc_volume = sc->sc_volume_wanted;
985 }
986
987 if (sc->sc_callback != NULL)
988 sc->sc_callback(sc->sc_cb_cookie);
989 }
990 }
991
992 static int
993 pmu_print(void *aux, const char *what)
994 {
995
996 return 0;
997 }
998
999 static void
1000 pmu_attach_legacy_battery(struct pmu_softc *sc)
1001 {
1002 struct battery_attach_args baa;
1003
1004 baa.baa_type = BATTERY_TYPE_LEGACY;
1005 baa.baa_pmu_ops = &sc->sc_pmu_ops;
1006 config_found_ia(&sc->sc_dev, "pmu_bus", &baa, pmu_print);
1007 }
1008
1009 static void
1010 pmu_attach_smart_battery(struct pmu_softc *sc, int num)
1011 {
1012 struct battery_attach_args baa;
1013
1014 baa.baa_type = BATTERY_TYPE_SMART;
1015 baa.baa_pmu_ops = &sc->sc_pmu_ops;
1016 baa.baa_num = num;
1017 config_found_ia(&sc->sc_dev, "pmu_bus", &baa, pmu_print);
1018 }
1019