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