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