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