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