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