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