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