cuda.c revision 1.29.2.3 1 /* $NetBSD: cuda.c,v 1.29.2.3 2021/09/11 13:13:59 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: cuda.c,v 1.29.2.3 2021/09/11 13:13:59 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/mutex.h>
38
39 #include <sys/bus.h>
40 #include <machine/autoconf.h>
41 #include <machine/pio.h>
42 #include <dev/clock_subr.h>
43 #include <dev/i2c/i2cvar.h>
44
45 #include <macppc/dev/viareg.h>
46 #include <macppc/dev/cudavar.h>
47
48 #include <dev/ofw/openfirm.h>
49 #include <dev/adb/adbvar.h>
50 #include "opt_cuda.h"
51
52 #ifdef CUDA_DEBUG
53 #define DPRINTF printf
54 #else
55 #define DPRINTF while (0) printf
56 #endif
57
58 #define CUDA_NOTREADY 0x1 /* has not been initialized yet */
59 #define CUDA_IDLE 0x2 /* the bus is currently idle */
60 #define CUDA_OUT 0x3 /* sending out a command */
61 #define CUDA_IN 0x4 /* receiving data */
62 #define CUDA_POLLING 0x5 /* polling - II only */
63
64 static void cuda_attach(device_t, device_t, void *);
65 static int cuda_match(device_t, struct cfdata *, void *);
66 static void cuda_autopoll(void *, int);
67
68 static int cuda_intr(void *);
69
70 typedef struct _cuda_handler {
71 int (*handler)(void *, int, uint8_t *);
72 void *cookie;
73 } CudaHandler;
74
75 #define CUDA_MAX_I2C_DEVICES 2
76
77 struct cuda_softc {
78 device_t sc_dev;
79 void *sc_ih;
80 CudaHandler sc_handlers[16];
81 struct todr_chip_handle sc_todr;
82 struct adb_bus_accessops sc_adbops;
83 struct i2c_controller sc_i2c;
84 bus_space_tag_t sc_memt;
85 bus_space_handle_t sc_memh;
86
87 /*
88 * We provide our own i2c device enumeration method, so we
89 * need to provide our own devhandle_impl.
90 */
91 struct devhandle_impl sc_devhandle_impl;
92
93 struct i2c_deventry sc_i2c_devices[CUDA_MAX_I2C_DEVICES];
94 unsigned int sc_ni2c_devices;
95
96 int sc_node;
97 int sc_state;
98 int sc_waiting;
99 int sc_polling;
100 int sc_sent;
101 int sc_out_length;
102 int sc_received;
103 int sc_iic_done;
104 int sc_error;
105 /* time */
106 uint32_t sc_tod;
107 uint32_t sc_autopoll;
108 uint32_t sc_todev;
109 /* ADB */
110 void (*sc_adb_handler)(void *, int, uint8_t *);
111 void *sc_adb_cookie;
112 uint32_t sc_i2c_read_len;
113 /* internal buffers */
114 uint8_t sc_in[256];
115 uint8_t sc_out[256];
116 };
117
118 CFATTACH_DECL_NEW(cuda, sizeof(struct cuda_softc),
119 cuda_match, cuda_attach, NULL, NULL);
120
121 static inline void cuda_write_reg(struct cuda_softc *, int, uint8_t);
122 static inline uint8_t cuda_read_reg(struct cuda_softc *, int);
123 static void cuda_idle(struct cuda_softc *);
124 static void cuda_tip(struct cuda_softc *);
125 static void cuda_clear_tip(struct cuda_softc *);
126 static void cuda_in(struct cuda_softc *);
127 static void cuda_out(struct cuda_softc *);
128 static void cuda_toggle_ack(struct cuda_softc *);
129 static void cuda_ack_off(struct cuda_softc *);
130 static int cuda_intr_state(struct cuda_softc *);
131
132 static void cuda_init(struct cuda_softc *);
133
134 /*
135 * send a message to Cuda.
136 */
137 /* cookie, flags, length, data */
138 static int cuda_send(void *, int, int, uint8_t *);
139 static void cuda_poll(void *);
140 static void cuda_adb_poll(void *);
141 static int cuda_set_handler(void *, int, int (*)(void *, int, uint8_t *), void *);
142
143 static int cuda_error_handler(void *, int, uint8_t *);
144
145 static int cuda_todr_handler(void *, int, uint8_t *);
146 static int cuda_todr_set(todr_chip_handle_t, struct timeval *);
147 static int cuda_todr_get(todr_chip_handle_t, struct timeval *);
148
149 static int cuda_adb_handler(void *, int, uint8_t *);
150 static void cuda_final(device_t);
151
152 static struct cuda_attach_args *cuda0 = NULL;
153
154 /* ADB bus attachment stuff */
155 static int cuda_adb_send(void *, int, int, int, uint8_t *);
156 static int cuda_adb_set_handler(void *, void (*)(void *, int, uint8_t *), void *);
157
158 /* i2c stuff */
159 static int cuda_i2c_exec(void *, i2c_op_t, i2c_addr_t, const void *, size_t,
160 void *, size_t, int);
161
162 static void
163 cuda_add_i2c_device(struct cuda_softc *sc, const char *name,
164 const char *compatible, i2c_addr_t addr)
165 {
166 KASSERT(sc->sc_ni2c_devices < CUDA_MAX_I2C_DEVICES);
167 sc->sc_i2c_devices[sc->sc_ni2c_devices].name = name;
168 sc->sc_i2c_devices[sc->sc_ni2c_devices].compat = compatible;
169 sc->sc_i2c_devices[sc->sc_ni2c_devices].addr = addr;
170 sc->sc_ni2c_devices++;
171 }
172
173 static int
174 cuda_i2c_enumerate_devices(device_t dev, devhandle_t call_handle, void *v)
175 {
176 struct i2c_enumerate_devices_args *args = v;
177
178 /* dev is the "iicbus" instance. Cuda softc is in args. */
179 struct cuda_softc *sc = args->ia->ia_tag->ic_cookie;
180
181 return i2c_enumerate_deventries(dev, call_handle, args,
182 sc->sc_i2c_devices, sc->sc_ni2c_devices);
183 }
184
185 static device_call_t
186 cuda_devhandle_lookup_device_call(devhandle_t handle, const char *name,
187 devhandle_t *call_handlep)
188 {
189 if (strcmp(name, "i2c-enumerate-devices") == 0) {
190 return cuda_i2c_enumerate_devices;
191 }
192
193 /* Defer everything else to the "super". */
194 return NULL;
195 }
196
197 static int
198 cuda_match(device_t parent, struct cfdata *cf, void *aux)
199 {
200 struct confargs *ca = aux;
201
202 if (ca->ca_nreg < 8)
203 return 0;
204
205 if (ca->ca_nintr < 4)
206 return 0;
207
208 if (strcmp(ca->ca_name, "via-cuda") == 0) {
209 return 10; /* beat adb* at obio? */
210 }
211
212 return 0;
213 }
214
215 static void
216 cuda_attach(device_t parent, device_t self, void *aux)
217 {
218 struct confargs *ca = aux;
219 struct cuda_softc *sc = device_private(self);
220 struct i2cbus_attach_args iba;
221 static struct cuda_attach_args caa;
222 int irq = ca->ca_intr[0];
223 int node, i, child;
224 char name[32];
225
226 sc->sc_dev = self;
227 node = of_getnode_byname(OF_parent(ca->ca_node), "extint-gpio1");
228 if (node)
229 OF_getprop(node, "interrupts", &irq, 4);
230
231 aprint_normal(" irq %d", irq);
232
233 sc->sc_node = ca->ca_node;
234 sc->sc_memt = ca->ca_tag;
235
236 sc->sc_sent = 0;
237 sc->sc_received = 0;
238 sc->sc_waiting = 0;
239 sc->sc_polling = 0;
240 sc->sc_state = CUDA_NOTREADY;
241 sc->sc_error = 0;
242 sc->sc_i2c_read_len = 0;
243
244 if (bus_space_map(sc->sc_memt, ca->ca_reg[0] + ca->ca_baseaddr,
245 ca->ca_reg[1], 0, &sc->sc_memh) != 0) {
246
247 aprint_normal(": unable to map registers\n");
248 return;
249 }
250 sc->sc_ih = intr_establish_xname(irq, IST_EDGE, IPL_TTY, cuda_intr, sc,
251 device_xname(self));
252 printf("\n");
253
254 for (i = 0; i < 16; i++) {
255 sc->sc_handlers[i].handler = NULL;
256 sc->sc_handlers[i].cookie = NULL;
257 }
258
259 cuda_init(sc);
260
261 /* now attach children */
262 config_interrupts(self, cuda_final);
263 cuda_set_handler(sc, CUDA_ERROR, cuda_error_handler, sc);
264 cuda_set_handler(sc, CUDA_PSEUDO, cuda_todr_handler, sc);
265
266 child = OF_child(ca->ca_node);
267 while (child != 0) {
268
269 if (OF_getprop(child, "name", name, 32) == 0)
270 continue;
271 if (strncmp(name, "adb", 4) == 0) {
272
273 cuda_set_handler(sc, CUDA_ADB, cuda_adb_handler, sc);
274 sc->sc_adbops.cookie = sc;
275 sc->sc_adbops.send = cuda_adb_send;
276 sc->sc_adbops.poll = cuda_adb_poll;
277 sc->sc_adbops.autopoll = cuda_autopoll;
278 sc->sc_adbops.set_handler = cuda_adb_set_handler;
279 config_found(self, &sc->sc_adbops, nadb_print,
280 CFARGS(.iattr = "adb_bus"));
281 } else if (strncmp(name, "rtc", 4) == 0) {
282
283 sc->sc_todr.todr_gettime = cuda_todr_get;
284 sc->sc_todr.todr_settime = cuda_todr_set;
285 sc->sc_todr.cookie = sc;
286 todr_attach(&sc->sc_todr);
287 }
288 child = OF_peer(child);
289 }
290
291 caa.cookie = sc;
292 caa.set_handler = cuda_set_handler;
293 caa.send = cuda_send;
294 caa.poll = cuda_poll;
295 #if notyet
296 config_found(self, &caa, cuda_print, CFARGS_NONE);
297 #endif
298 /* we don't have OF nodes for i2c devices so we have to make our own */
299 node = OF_finddevice("/valkyrie");
300 if (node != -1) {
301 /* XXX a real "compatible" string would be nice... */
302 cuda_add_i2c_device(sc, "videopll",
303 "aapl,valkyrie-videopll", 0x50);
304 }
305
306 node = OF_finddevice("/perch");
307 if (node != -1) {
308 cuda_add_i2c_device(sc, "sgsmix", "st,tda7433", 0x8a);
309 }
310
311 /*
312 * Normally the i2c bus instance would automatically inherit
313 * our devhandle, but we provide our own i2c device enumeration
314 * method, so we need to supply the bus instance with our own
315 * device handle implementation, using the one we got from
316 * OpenFirmware as the "super".
317 */
318 devhandle_t devhandle = devhandle_from_of(sc->sc_node);
319 devhandle_impl_inherit(&sc->sc_devhandle_impl, devhandle.impl);
320 sc->sc_devhandle_impl.lookup_device_call =
321 cuda_devhandle_lookup_device_call;
322 devhandle.impl = &sc->sc_devhandle_impl;
323
324 iic_tag_init(&sc->sc_i2c);
325 sc->sc_i2c.ic_cookie = sc;
326 sc->sc_i2c.ic_exec = cuda_i2c_exec;
327
328 memset(&iba, 0, sizeof(iba));
329 iba.iba_tag = &sc->sc_i2c;
330 config_found(self, &iba, iicbus_print,
331 CFARGS(.iattr = "i2cbus",
332 .devhandle = devhandle));
333
334 if (cuda0 == NULL)
335 cuda0 = &caa;
336 }
337
338 static void
339 cuda_init(struct cuda_softc *sc)
340 {
341 uint8_t reg;
342
343 reg = cuda_read_reg(sc, vDirB);
344 reg |= 0x30; /* register B bits 4 and 5: outputs */
345 cuda_write_reg(sc, vDirB, reg);
346
347 reg = cuda_read_reg(sc, vDirB);
348 reg &= 0xf7; /* register B bit 3: input */
349 cuda_write_reg(sc, vDirB, reg);
350
351 reg = cuda_read_reg(sc, vACR);
352 reg &= ~vSR_OUT; /* make sure SR is set to IN */
353 cuda_write_reg(sc, vACR, reg);
354
355 cuda_write_reg(sc, vACR, (cuda_read_reg(sc, vACR) | 0x0c) & ~0x10);
356
357 sc->sc_state = CUDA_IDLE; /* used by all types of hardware */
358
359 cuda_write_reg(sc, vIER, 0x84); /* make sure VIA interrupts are on */
360 cuda_idle(sc); /* set ADB bus state to idle */
361
362 /* sort of a device reset */
363 (void)cuda_read_reg(sc, vSR); /* clear interrupt */
364 cuda_write_reg(sc, vIER, 0x04); /* no interrupts while clearing */
365 cuda_idle(sc); /* reset state to idle */
366 delay(150);
367 cuda_tip(sc); /* signal start of frame */
368 delay(150);
369 cuda_toggle_ack(sc);
370 delay(150);
371 cuda_clear_tip(sc);
372 delay(150);
373 cuda_idle(sc); /* back to idle state */
374 (void)cuda_read_reg(sc, vSR); /* clear interrupt */
375 cuda_write_reg(sc, vIER, 0x84); /* ints ok now */
376 }
377
378 static void
379 cuda_final(device_t dev)
380 {
381 struct cuda_softc *sc = device_private(dev);
382
383 sc->sc_polling = 0;
384 }
385
386 static inline void
387 cuda_write_reg(struct cuda_softc *sc, int offset, uint8_t value)
388 {
389
390 bus_space_write_1(sc->sc_memt, sc->sc_memh, offset, value);
391 }
392
393 static inline uint8_t
394 cuda_read_reg(struct cuda_softc *sc, int offset)
395 {
396
397 return bus_space_read_1(sc->sc_memt, sc->sc_memh, offset);
398 }
399
400 static int
401 cuda_set_handler(void *cookie, int type,
402 int (*handler)(void *, int, uint8_t *), void *hcookie)
403 {
404 struct cuda_softc *sc = cookie;
405 CudaHandler *me;
406
407 if ((type >= 0) && (type < 16)) {
408 me = &sc->sc_handlers[type];
409 me->handler = handler;
410 me->cookie = hcookie;
411 return 0;
412 }
413 return -1;
414 }
415
416 static int
417 cuda_send(void *cookie, int poll, int length, uint8_t *msg)
418 {
419 struct cuda_softc *sc = cookie;
420 int s;
421
422 DPRINTF("cuda_send %08x\n", (uint32_t)cookie);
423 if (sc->sc_state == CUDA_NOTREADY)
424 return -1;
425
426 s = splhigh();
427
428 if (sc->sc_state == CUDA_IDLE /*&&
429 (cuda_read_reg(sc, vBufB) & vPB3) == vPB3*/) {
430 /* fine */
431 DPRINTF("chip is idle\n");
432 } else {
433 DPRINTF("cuda state is %d\n", sc->sc_state);
434 if (sc->sc_waiting == 0) {
435 sc->sc_waiting = 1;
436 } else {
437 splx(s);
438 return -1;
439 }
440 }
441
442 sc->sc_error = 0;
443 memcpy(sc->sc_out, msg, length);
444 sc->sc_out_length = length;
445 sc->sc_sent = 0;
446
447 if (sc->sc_waiting != 1) {
448
449 delay(150);
450 sc->sc_state = CUDA_OUT;
451 cuda_out(sc);
452 cuda_write_reg(sc, vSR, sc->sc_out[0]);
453 cuda_ack_off(sc);
454 cuda_tip(sc);
455 }
456 sc->sc_waiting = 1;
457
458 if (sc->sc_polling || poll || cold) {
459 cuda_poll(sc);
460 }
461
462 splx(s);
463
464 return 0;
465 }
466
467 static void
468 cuda_poll(void *cookie)
469 {
470 struct cuda_softc *sc = cookie;
471 int s;
472
473 DPRINTF("polling\n");
474 while ((sc->sc_state != CUDA_IDLE) ||
475 (cuda_intr_state(sc)) ||
476 (sc->sc_waiting == 1)) {
477 if ((cuda_read_reg(sc, vIFR) & vSR_INT) == vSR_INT) {
478 s = splhigh();
479 cuda_intr(sc);
480 splx(s);
481 }
482 }
483 }
484
485 static void
486 cuda_adb_poll(void *cookie)
487 {
488 struct cuda_softc *sc = cookie;
489 int s;
490
491 s = splhigh();
492 cuda_intr(sc);
493 splx(s);
494 }
495
496 static void
497 cuda_idle(struct cuda_softc *sc)
498 {
499 uint8_t reg;
500
501 reg = cuda_read_reg(sc, vBufB);
502 reg |= (vPB4 | vPB5);
503 cuda_write_reg(sc, vBufB, reg);
504 }
505
506 static void
507 cuda_tip(struct cuda_softc *sc)
508 {
509 uint8_t reg;
510
511 reg = cuda_read_reg(sc, vBufB);
512 reg &= ~vPB5;
513 cuda_write_reg(sc, vBufB, reg);
514 }
515
516 static void
517 cuda_clear_tip(struct cuda_softc *sc)
518 {
519 uint8_t reg;
520
521 reg = cuda_read_reg(sc, vBufB);
522 reg |= vPB5;
523 cuda_write_reg(sc, vBufB, reg);
524 }
525
526 static void
527 cuda_in(struct cuda_softc *sc)
528 {
529 uint8_t reg;
530
531 reg = cuda_read_reg(sc, vACR);
532 reg &= ~vSR_OUT;
533 cuda_write_reg(sc, vACR, reg);
534 }
535
536 static void
537 cuda_out(struct cuda_softc *sc)
538 {
539 uint8_t reg;
540
541 reg = cuda_read_reg(sc, vACR);
542 reg |= vSR_OUT;
543 cuda_write_reg(sc, vACR, reg);
544 }
545
546 static void
547 cuda_toggle_ack(struct cuda_softc *sc)
548 {
549 uint8_t reg;
550
551 reg = cuda_read_reg(sc, vBufB);
552 reg ^= vPB4;
553 cuda_write_reg(sc, vBufB, reg);
554 }
555
556 static void
557 cuda_ack_off(struct cuda_softc *sc)
558 {
559 uint8_t reg;
560
561 reg = cuda_read_reg(sc, vBufB);
562 reg |= vPB4;
563 cuda_write_reg(sc, vBufB, reg);
564 }
565
566 static int
567 cuda_intr_state(struct cuda_softc *sc)
568 {
569 return ((cuda_read_reg(sc, vBufB) & vPB3) == 0);
570 }
571
572 static int
573 cuda_intr(void *arg)
574 {
575 struct cuda_softc *sc = arg;
576 int ending, type;
577 uint8_t reg;
578
579 reg = cuda_read_reg(sc, vIFR); /* Read the interrupts */
580 DPRINTF("[");
581 if ((reg & 0x80) == 0) {
582 DPRINTF("irq %02x]", reg);
583 return 0; /* No interrupts to process */
584 }
585 DPRINTF(":");
586
587 cuda_write_reg(sc, vIFR, 0x7f); /* Clear 'em */
588
589 switch_start:
590 switch (sc->sc_state) {
591 case CUDA_IDLE:
592 /*
593 * This is an unexpected packet, so grab the first (dummy)
594 * byte, set up the proper vars, and tell the chip we are
595 * starting to receive the packet by setting the TIP bit.
596 */
597 sc->sc_in[1] = cuda_read_reg(sc, vSR);
598 DPRINTF("start: %02x", sc->sc_in[1]);
599 if (cuda_intr_state(sc) == 0) {
600 /* must have been a fake start */
601 DPRINTF(" ... fake start\n");
602 if (sc->sc_waiting) {
603 /* start over */
604 delay(150);
605 sc->sc_state = CUDA_OUT;
606 sc->sc_sent = 0;
607 cuda_out(sc);
608 cuda_write_reg(sc, vSR, sc->sc_out[1]);
609 cuda_ack_off(sc);
610 cuda_tip(sc);
611 }
612 break;
613 }
614
615 cuda_in(sc);
616 cuda_tip(sc);
617
618 sc->sc_received = 1;
619 sc->sc_state = CUDA_IN;
620 DPRINTF(" CUDA_IN");
621 break;
622
623 case CUDA_IN:
624 sc->sc_in[sc->sc_received] = cuda_read_reg(sc, vSR);
625 DPRINTF(" %02x", sc->sc_in[sc->sc_received]);
626 ending = 0;
627 if (sc->sc_received > 255) {
628 /* bitch only once */
629 if (sc->sc_received == 256) {
630 aprint_error_dev(sc->sc_dev,
631 "input overflow\n");
632 ending = 1;
633 }
634 } else
635 sc->sc_received++;
636 if (sc->sc_received > 3) {
637 if ((sc->sc_in[3] == CMD_IIC) &&
638 (sc->sc_received > (sc->sc_i2c_read_len + 4))) {
639 ending = 1;
640 }
641 }
642
643 /* intr off means this is the last byte (end of frame) */
644 if (cuda_intr_state(sc) == 0) {
645 ending = 1;
646 DPRINTF(".\n");
647 } else {
648 cuda_toggle_ack(sc);
649 }
650
651 if (ending == 1) { /* end of message? */
652
653 sc->sc_in[0] = sc->sc_received - 1;
654
655 /* reset vars and signal the end of this frame */
656 cuda_idle(sc);
657
658 /* check if we have a handler for this message */
659 type = sc->sc_in[1];
660 if ((type >= 0) && (type < 16)) {
661 CudaHandler *me = &sc->sc_handlers[type];
662
663 if (me->handler != NULL) {
664 me->handler(me->cookie,
665 sc->sc_received - 1, &sc->sc_in[1]);
666 } else {
667 aprint_error_dev(sc->sc_dev,
668 "no handler for type %02x\n", type);
669 panic("barf");
670 }
671 }
672
673 DPRINTF("CUDA_IDLE");
674 sc->sc_state = CUDA_IDLE;
675
676 sc->sc_received = 0;
677
678 /*
679 * If there is something waiting to be sent out,
680 * set everything up and send the first byte.
681 */
682 if (sc->sc_waiting == 1) {
683
684 DPRINTF("pending write\n");
685 delay(1500); /* required */
686 sc->sc_sent = 0;
687 sc->sc_state = CUDA_OUT;
688
689 /*
690 * If the interrupt is on, we were too slow
691 * and the chip has already started to send
692 * something to us, so back out of the write
693 * and start a read cycle.
694 */
695 if (cuda_intr_state(sc)) {
696 cuda_in(sc);
697 cuda_idle(sc);
698 sc->sc_sent = 0;
699 sc->sc_state = CUDA_IDLE;
700 sc->sc_received = 0;
701 delay(150);
702 DPRINTF("too slow - incoming message\n");
703 goto switch_start;
704 }
705 /*
706 * If we got here, it's ok to start sending
707 * so load the first byte and tell the chip
708 * we want to send.
709 */
710 DPRINTF("sending ");
711
712 cuda_out(sc);
713 cuda_write_reg(sc, vSR,
714 sc->sc_out[sc->sc_sent]);
715 cuda_ack_off(sc);
716 cuda_tip(sc);
717 }
718 }
719 break;
720
721 case CUDA_OUT:
722 (void)cuda_read_reg(sc, vSR); /* reset SR-intr in IFR */
723
724 sc->sc_sent++;
725 if (cuda_intr_state(sc)) { /* ADB intr low during write */
726
727 DPRINTF("incoming msg during send\n");
728 cuda_in(sc); /* make sure SR is set to IN */
729 cuda_idle(sc);
730 sc->sc_sent = 0; /* must start all over */
731 sc->sc_state = CUDA_IDLE; /* new state */
732 sc->sc_received = 0;
733 sc->sc_waiting = 1; /* must retry when done with
734 * read */
735 delay(150);
736 goto switch_start; /* process next state right
737 * now */
738 break;
739 }
740 if (sc->sc_out_length == sc->sc_sent) { /* check for done */
741
742 sc->sc_waiting = 0; /* done writing */
743 sc->sc_state = CUDA_IDLE; /* signal bus is idle */
744 cuda_in(sc);
745 cuda_idle(sc);
746 DPRINTF("done sending\n");
747 } else {
748 /* send next byte */
749 cuda_write_reg(sc, vSR, sc->sc_out[sc->sc_sent]);
750 DPRINTF("%02x", sc->sc_out[sc->sc_sent]);
751 cuda_toggle_ack(sc); /* signal byte ready to
752 * shift */
753 }
754 break;
755
756 case CUDA_NOTREADY:
757 DPRINTF("adb: not yet initialized\n");
758 break;
759
760 default:
761 DPRINTF("intr: unknown ADB state\n");
762 break;
763 }
764
765 DPRINTF("]");
766 return 1;
767 }
768
769 static int
770 cuda_error_handler(void *cookie, int len, uint8_t *data)
771 {
772 struct cuda_softc *sc = cookie;
773
774 /*
775 * something went wrong
776 * byte 3 seems to be the failed command
777 */
778 sc->sc_error = 1;
779 wakeup(&sc->sc_todev);
780 return 0;
781 }
782
783
784 /* real time clock */
785
786 static int
787 cuda_todr_handler(void *cookie, int len, uint8_t *data)
788 {
789 struct cuda_softc *sc = cookie;
790
791 #ifdef CUDA_DEBUG
792 int i;
793 printf("msg: %02x", data[0]);
794 for (i = 1; i < len; i++) {
795 printf(" %02x", data[i]);
796 }
797 printf("\n");
798 #endif
799
800 switch(data[2]) {
801 case CMD_READ_RTC:
802 memcpy(&sc->sc_tod, &data[3], 4);
803 break;
804 case CMD_WRITE_RTC:
805 sc->sc_tod = 0xffffffff;
806 break;
807 case CMD_AUTOPOLL:
808 sc->sc_autopoll = 1;
809 break;
810 case CMD_IIC:
811 sc->sc_iic_done = len;
812 break;
813 }
814 wakeup(&sc->sc_todev);
815 return 0;
816 }
817
818 #define DIFF19041970 2082844800
819
820 static int
821 cuda_todr_get(todr_chip_handle_t tch, struct timeval *tvp)
822 {
823 struct cuda_softc *sc = tch->cookie;
824 int cnt = 0;
825 uint8_t cmd[] = { CUDA_PSEUDO, CMD_READ_RTC};
826
827 sc->sc_tod = 0;
828 while (sc->sc_tod == 0) {
829 cuda_send(sc, 0, 2, cmd);
830
831 while ((sc->sc_tod == 0) && (cnt < 10)) {
832 tsleep(&sc->sc_todev, 0, "todr", 10);
833 cnt++;
834 }
835
836 if (sc->sc_tod == 0) {
837 aprint_error_dev(sc->sc_dev,
838 "unable to read a sane RTC value\n");
839 return EIO;
840 }
841 if ((sc->sc_tod > 0xf0000000UL) ||
842 (sc->sc_tod < DIFF19041970)) {
843 /* huh? try again */
844 sc->sc_tod = 0;
845 aprint_verbose_dev(sc->sc_dev,
846 "got garbage reading RTC, trying again\n");
847 }
848 }
849
850 tvp->tv_sec = sc->sc_tod - DIFF19041970;
851 DPRINTF("tod: %" PRIo64 "\n", tvp->tv_sec);
852 tvp->tv_usec = 0;
853 return 0;
854 }
855
856 static int
857 cuda_todr_set(todr_chip_handle_t tch, struct timeval *tvp)
858 {
859 struct cuda_softc *sc = tch->cookie;
860 uint32_t sec;
861 uint8_t cmd[] = {CUDA_PSEUDO, CMD_WRITE_RTC, 0, 0, 0, 0};
862
863 sec = tvp->tv_sec + DIFF19041970;
864 memcpy(&cmd[2], &sec, 4);
865 sc->sc_tod = 0;
866 if (cuda_send(sc, 0, 6, cmd) == 0) {
867 while (sc->sc_tod == 0) {
868 tsleep(&sc->sc_todev, 0, "todr", 10);
869 }
870 return 0;
871 }
872 aprint_error_dev(sc->sc_dev, "%s failed\n", __func__);
873 return -1;
874
875 }
876
877 /* poweroff and reboot */
878
879 void
880 cuda_poweroff(void)
881 {
882 struct cuda_softc *sc;
883 uint8_t cmd[] = {CUDA_PSEUDO, CMD_POWEROFF};
884
885 if (cuda0 == NULL)
886 return;
887 sc = cuda0->cookie;
888 sc->sc_polling = 1;
889 cuda0->poll(sc);
890 if (cuda0->send(sc, 1, 2, cmd) == 0)
891 while (1);
892 }
893
894 void
895 cuda_restart(void)
896 {
897 struct cuda_softc *sc;
898 uint8_t cmd[] = {CUDA_PSEUDO, CMD_RESET};
899
900 if (cuda0 == NULL)
901 return;
902 sc = cuda0->cookie;
903 sc->sc_polling = 1;
904 cuda0->poll(sc);
905 if (cuda0->send(sc, 1, 2, cmd) == 0)
906 while (1);
907 }
908
909 /* ADB message handling */
910
911 static void
912 cuda_autopoll(void *cookie, int flag)
913 {
914 struct cuda_softc *sc = cookie;
915 uint8_t cmd[] = {CUDA_PSEUDO, CMD_AUTOPOLL, (flag != 0)};
916
917 if (cmd[2] == sc->sc_autopoll)
918 return;
919
920 sc->sc_autopoll = -1;
921 cuda_send(sc, 0, 3, cmd);
922 while(sc->sc_autopoll == -1) {
923 if (sc->sc_polling || cold) {
924 cuda_poll(sc);
925 } else
926 tsleep(&sc->sc_todev, 0, "autopoll", 100);
927 }
928 }
929
930 static int
931 cuda_adb_handler(void *cookie, int len, uint8_t *data)
932 {
933 struct cuda_softc *sc = cookie;
934
935 if (sc->sc_adb_handler != NULL) {
936 sc->sc_adb_handler(sc->sc_adb_cookie, len - 1,
937 &data[1]);
938 return 0;
939 }
940 return -1;
941 }
942
943 static int
944 cuda_adb_send(void *cookie, int poll, int command, int len, uint8_t *data)
945 {
946 struct cuda_softc *sc = cookie;
947 int i, s = 0;
948 uint8_t packet[16];
949
950 /* construct an ADB command packet and send it */
951 packet[0] = CUDA_ADB;
952 packet[1] = command;
953 for (i = 0; i < len; i++)
954 packet[i + 2] = data[i];
955 if (poll || cold) {
956 s = splhigh();
957 cuda_poll(sc);
958 }
959 cuda_send(sc, poll, len + 2, packet);
960 if (poll || cold) {
961 cuda_poll(sc);
962 splx(s);
963 }
964 return 0;
965 }
966
967 static int
968 cuda_adb_set_handler(void *cookie, void (*handler)(void *, int, uint8_t *),
969 void *hcookie)
970 {
971 struct cuda_softc *sc = cookie;
972
973 /* register a callback for incoming ADB messages */
974 sc->sc_adb_handler = handler;
975 sc->sc_adb_cookie = hcookie;
976 return 0;
977 }
978
979 /* i2c message handling */
980
981 static int
982 cuda_i2c_exec(void *cookie, i2c_op_t op, i2c_addr_t addr, const void *_send,
983 size_t send_len, void *_recv, size_t recv_len, int flags)
984 {
985 struct cuda_softc *sc = cookie;
986 const uint8_t *send = _send;
987 uint8_t *recv = _recv;
988 uint8_t command[16] = {CUDA_PSEUDO, CMD_IIC};
989
990 DPRINTF("cuda_i2c_exec(%02x)\n", addr);
991 command[2] = addr;
992
993 /* Copy command and output data bytes, if any, to buffer */
994 if (send_len > 0)
995 memcpy(&command[3], send, uimin((int)send_len, 12));
996 else if (I2C_OP_READ_P(op) && (recv_len == 0)) {
997 /*
998 * If no data bytes in either direction, it's a "quick"
999 * i2c operation. We don't know how to do a quick_read
1000 * since that requires us to set the low bit of the
1001 * address byte after it has been left-shifted.
1002 */
1003 sc->sc_error = 0;
1004 return -1;
1005 }
1006
1007 sc->sc_iic_done = 0;
1008 cuda_send(sc, sc->sc_polling, send_len + 3, command);
1009
1010 while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
1011 if (sc->sc_polling || cold) {
1012 cuda_poll(sc);
1013 } else
1014 tsleep(&sc->sc_todev, 0, "i2c", 1000);
1015 }
1016
1017 if (sc->sc_error) {
1018 sc->sc_error = 0;
1019 aprint_error_dev(sc->sc_dev, "error doing I2C\n");
1020 return -1;
1021 }
1022
1023 /* see if we're supposed to do a read */
1024 if (recv_len > 0) {
1025 sc->sc_iic_done = 0;
1026 command[2] |= 1;
1027 command[3] = 0;
1028
1029 /*
1030 * XXX we need to do something to limit the size of the answer
1031 * - apparently the chip keeps sending until we tell it to stop
1032 */
1033 sc->sc_i2c_read_len = recv_len;
1034 DPRINTF("rcv_len: %d\n", recv_len);
1035 cuda_send(sc, sc->sc_polling, 3, command);
1036 while ((sc->sc_iic_done == 0) && (sc->sc_error == 0)) {
1037 if (sc->sc_polling || cold) {
1038 cuda_poll(sc);
1039 } else
1040 tsleep(&sc->sc_todev, 0, "i2c", 1000);
1041 }
1042
1043 if (sc->sc_error) {
1044 aprint_error_dev(sc->sc_dev,
1045 "error trying to read from I2C\n");
1046 sc->sc_error = 0;
1047 return -1;
1048 }
1049 }
1050
1051 DPRINTF("received: %d\n", sc->sc_iic_done);
1052 if ((sc->sc_iic_done > 3) && (recv_len > 0)) {
1053 int rlen;
1054
1055 /* we got an answer */
1056 rlen = uimin(sc->sc_iic_done - 3, recv_len);
1057 memcpy(recv, &sc->sc_in[4], rlen);
1058 #ifdef CUDA_DEBUG
1059 {
1060 int i;
1061 printf("ret:");
1062 for (i = 0; i < rlen; i++)
1063 printf(" %02x", recv[i]);
1064 printf("\n");
1065 }
1066 #endif
1067 return rlen;
1068 }
1069 return 0;
1070 }
1071