Home | History | Annotate | Line # | Download | only in dev
adb_direct.c revision 1.36
      1 /*	$NetBSD: adb_direct.c,v 1.36 2007/01/24 13:08:12 hubertf Exp $	*/
      2 
      3 /* From: adb_direct.c 2.02 4/18/97 jpw */
      4 
      5 /*
      6  * Copyright (C) 1996, 1997 John P. Wittkoski
      7  * All rights reserved.
      8  *
      9  * Redistribution and use in source and binary forms, with or without
     10  * modification, are permitted provided that the following conditions
     11  * are met:
     12  * 1. Redistributions of source code must retain the above copyright
     13  *    notice, this list of conditions and the following disclaimer.
     14  * 2. Redistributions in binary form must reproduce the above copyright
     15  *    notice, this list of conditions and the following disclaimer in the
     16  *    documentation and/or other materials provided with the distribution.
     17  * 3. All advertising materials mentioning features or use of this software
     18  *    must display the following acknowledgement:
     19  *  This product includes software developed by John P. Wittkoski.
     20  * 4. The name of the author may not be used to endorse or promote products
     21  *    derived from this software without specific prior written permission.
     22  *
     23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     32  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * This code is rather messy, but I don't have time right now
     37  * to clean it up as much as I would like.
     38  * But it works, so I'm happy. :-) jpw
     39  */
     40 
     41 /*
     42  * TO DO:
     43  *  - We could reduce the time spent in the adb_intr_* routines
     44  *    by having them save the incoming and outgoing data directly
     45  *    in the adbInbound and adbOutbound queues, as it would reduce
     46  *    the number of times we need to copy the data around. It
     47  *    would also make the code more readable and easier to follow.
     48  *  - (Related to above) Use the header part of adbCommand to
     49  *    reduce the number of copies we have to do of the data.
     50  *  - (Related to above) Actually implement the adbOutbound queue.
     51  *    This is fairly easy once you switch all the intr routines
     52  *    over to using adbCommand structs directly.
     53  *  - There is a bug in the state machine of adb_intr_cuda
     54  *    code that causes hangs, especially on 030 machines, probably
     55  *    because of some timing issues. Because I have been unable to
     56  *    determine the exact cause of this bug, I used the timeout function
     57  *    to check for and recover from this condition. If anyone finds
     58  *    the actual cause of this bug, the calls to timeout and the
     59  *    adb_cuda_tickle routine can be removed.
     60  */
     61 
     62 #include <sys/cdefs.h>
     63 __KERNEL_RCSID(0, "$NetBSD: adb_direct.c,v 1.36 2007/01/24 13:08:12 hubertf Exp $");
     64 
     65 #include <sys/param.h>
     66 #include <sys/systm.h>
     67 #include <sys/callout.h>
     68 #include <sys/device.h>
     69 
     70 #include <machine/param.h>
     71 #include <machine/cpu.h>
     72 #include <machine/adbsys.h>
     73 
     74 #include <macppc/dev/viareg.h>
     75 #include <macppc/dev/adbvar.h>
     76 #include <macppc/dev/pm_direct.h>
     77 
     78 #define printf_intr printf
     79 
     80 #ifdef DEBUG
     81 #ifndef ADB_DEBUG
     82 #define ADB_DEBUG
     83 #endif
     84 #endif
     85 
     86 /* some misc. leftovers */
     87 #define vPB		0x0000
     88 #define vPB3		0x08
     89 #define vPB4		0x10
     90 #define vPB5		0x20
     91 #define vSR_INT		0x04
     92 #define vSR_OUT		0x10
     93 
     94 /* the type of ADB action that we are currently preforming */
     95 #define ADB_ACTION_NOTREADY	0x1	/* has not been initialized yet */
     96 #define ADB_ACTION_IDLE		0x2	/* the bus is currently idle */
     97 #define ADB_ACTION_OUT		0x3	/* sending out a command */
     98 #define ADB_ACTION_IN		0x4	/* receiving data */
     99 #define ADB_ACTION_POLLING	0x5	/* polling - II only */
    100 
    101 /*
    102  * These describe the state of the ADB bus itself, although they
    103  * don't necessarily correspond directly to ADB states.
    104  * Note: these are not really used in the IIsi code.
    105  */
    106 #define ADB_BUS_UNKNOWN		0x1	/* we don't know yet - all models */
    107 #define ADB_BUS_IDLE		0x2	/* bus is idle - all models */
    108 #define ADB_BUS_CMD		0x3	/* starting a command - II models */
    109 #define ADB_BUS_ODD		0x4	/* the "odd" state - II models */
    110 #define ADB_BUS_EVEN		0x5	/* the "even" state - II models */
    111 #define ADB_BUS_ACTIVE		0x6	/* active state - IIsi models */
    112 #define ADB_BUS_ACK		0x7	/* currently ACKing - IIsi models */
    113 
    114 /*
    115  * Shortcuts for setting or testing the VIA bit states.
    116  * Not all shortcuts are used for every type of ADB hardware.
    117  */
    118 #define ADB_SET_STATE_IDLE_CUDA()   via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
    119 #define ADB_SET_STATE_TIP()	    via_reg_and(VIA1, vBufB, ~vPB5)
    120 #define ADB_CLR_STATE_TIP() 	    via_reg_or(VIA1, vBufB, vPB5)
    121 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
    122 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
    123 #define ADB_SET_SR_INPUT()	    via_reg_and(VIA1, vACR, ~vSR_OUT)
    124 #define ADB_SET_SR_OUTPUT()	    via_reg_or(VIA1, vACR, vSR_OUT)
    125 #define ADB_SR()		    read_via_reg(VIA1, vSR)
    126 #define ADB_VIA_INTR_ENABLE()	    write_via_reg(VIA1, vIER, 0x84)
    127 #define ADB_VIA_INTR_DISABLE()	    write_via_reg(VIA1, vIER, 0x04)
    128 #define ADB_INTR_IS_OFF		   (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
    129 #define ADB_INTR_IS_ON		   (0 == (read_via_reg(VIA1, vBufB) & vPB3))
    130 #define ADB_SR_INTR_IS_OFF	   (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
    131 #define ADB_SR_INTR_IS_ON	   (vSR_INT == (read_via_reg(VIA1, \
    132 						vIFR) & vSR_INT))
    133 
    134 /*
    135  * This is the delay that is required (in uS) between certain
    136  * ADB transactions. The actual timing delay for for each uS is
    137  * calculated at boot time to account for differences in machine speed.
    138  */
    139 #define ADB_DELAY	150
    140 
    141 /*
    142  * Maximum ADB message length; includes space for data, result, and
    143  * device code - plus a little for safety.
    144  */
    145 #define ADB_MAX_MSG_LENGTH	16
    146 #define ADB_MAX_HDR_LENGTH	8
    147 
    148 #define ADB_QUEUE		32
    149 #define ADB_TICKLE_TICKS	4
    150 
    151 /*
    152  * A structure for storing information about each ADB device.
    153  */
    154 struct ADBDevEntry {
    155 	void	(*ServiceRtPtr) __P((void));
    156 	void	*DataAreaAddr;
    157 	int	devType;
    158 	int	origAddr;
    159 	int	currentAddr;
    160 };
    161 
    162 /*
    163  * Used to hold ADB commands that are waiting to be sent out.
    164  */
    165 struct adbCmdHoldEntry {
    166 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
    167 	u_char	*saveBuf;	/* buffer to know where to save result */
    168 	adbComp	*compRout;	/* completion routine pointer */
    169 	int	*data;		/* completion routine data pointer */
    170 };
    171 
    172 /*
    173  * Eventually used for two separate queues, the queue between
    174  * the upper and lower halves, and the outgoing packet queue.
    175  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
    176  */
    177 struct adbCommand {
    178 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
    179 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
    180 	u_char	*saveBuf;	/* where to save result */
    181 	adbComp *compRout;	/* completion routine pointer */
    182 	volatile int *compData;	/* completion routine data pointer */
    183 	u_int	cmd;		/* the original command for this data */
    184 	u_int	unsol;		/* 1 if packet was unsolicited */
    185 	u_int	ack_only;	/* 1 for no special processing */
    186 };
    187 
    188 /*
    189  * A few variables that we need and their initial values.
    190  */
    191 int	adbHardware = ADB_HW_UNKNOWN;
    192 int	adbActionState = ADB_ACTION_NOTREADY;
    193 int	adbWaiting = 0;		/* waiting for return data from the device */
    194 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
    195 
    196 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
    197 u_char	*adbBuffer = (long)0;	/* pointer to user data area */
    198 adbComp *adbCompRout = NULL;	/* pointer to the completion routine */
    199 volatile int *adbCompData = NULL;	/* pointer to the completion routine data */
    200 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
    201 
    202 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
    203 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
    204 
    205 int	adbSentChars = 0;	/* how many characters we have sent */
    206 
    207 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
    208 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
    209 
    210 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
    211 int	adbInCount = 0;			/* how many packets in in queue */
    212 int	adbInHead = 0;			/* head of in queue */
    213 int	adbInTail = 0;			/* tail of in queue */
    214 struct	adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
    215 int	adbOutCount = 0;		/* how many packets in out queue */
    216 int	adbOutHead = 0;			/* head of out queue */
    217 int	adbOutTail = 0;			/* tail of out queue */
    218 
    219 int	tickle_count = 0;		/* how many tickles seen for this packet? */
    220 int	tickle_serial = 0;		/* the last packet tickled */
    221 int	adb_cuda_serial = 0;		/* the current packet */
    222 
    223 struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
    224 struct callout adb_soft_intr_ch = CALLOUT_INITIALIZER;
    225 
    226 volatile u_char *Via1Base;
    227 extern int adb_polling;			/* Are we polling? */
    228 
    229 void	pm_setup_adb __P((void));
    230 void	pm_check_adb_devices __P((int));
    231 int	pm_adb_op __P((u_char *, void *, volatile void *, int));
    232 void	pm_init_adb_device __P((void));
    233 
    234 /*
    235  * The following are private routines.
    236  */
    237 #ifdef ADB_DEBUG
    238 void	print_single __P((u_char *));
    239 #endif
    240 void	adb_soft_intr __P((void));
    241 int	send_adb_cuda __P((u_char *, u_char *, adbComp *, volatile void *, int));
    242 void	adb_intr_cuda_test __P((void));
    243 void	adb_cuda_tickle __P((void));
    244 void	adb_pass_up __P((struct adbCommand *));
    245 void	adb_op_comprout __P((caddr_t, volatile int *, int));
    246 void	adb_reinit __P((void));
    247 int	count_adbs __P((void));
    248 int	get_ind_adb_info __P((ADBDataBlock *, int));
    249 int	get_adb_info __P((ADBDataBlock *, int));
    250 int	set_adb_info __P((ADBSetInfoBlock *, int));
    251 void	adb_setup_hw_type __P((void));
    252 int	adb_op (Ptr, adbComp *, volatile void *, short);
    253 int	adb_op_sync __P((Ptr, adbComp *, Ptr, short));
    254 void	adb_hw_setup __P((void));
    255 int	adb_cmd_result __P((u_char *));
    256 int	adb_cmd_extra __P((u_char *));
    257 /* we should create this and it will be the public version */
    258 int	send_adb __P((u_char *, void *, void *));
    259 
    260 int	setsoftadb __P((void));
    261 
    262 #ifdef ADB_DEBUG
    263 /*
    264  * print_single
    265  * Diagnostic display routine. Displays the hex values of the
    266  * specified elements of the u_char. The length of the "string"
    267  * is in [0].
    268  */
    269 void
    270 print_single(str)
    271 	u_char *str;
    272 {
    273 	int x;
    274 
    275 	if (str == 0) {
    276 		printf_intr("no data - null pointer\n");
    277 		return;
    278 	}
    279 	if (*str == 0) {
    280 		printf_intr("nothing returned\n");
    281 		return;
    282 	}
    283 	if (*str > 20) {
    284 		printf_intr("ADB: ACK > 20 no way!\n");
    285 		*str = 20;
    286 	}
    287 	printf_intr("(length=0x%x):", *str);
    288 	for (x = 1; x <= *str; x++)
    289 		printf_intr("  0x%02x", str[x]);
    290 	printf_intr("\n");
    291 }
    292 #endif
    293 
    294 void
    295 adb_cuda_tickle(void)
    296 {
    297 	volatile int s;
    298 
    299 	if (adbActionState == ADB_ACTION_IN) {
    300 		if (tickle_serial == adb_cuda_serial) {
    301 			if (++tickle_count > 0) {
    302 				s = splhigh();
    303 				adbActionState = ADB_ACTION_IDLE;
    304 				adbInputBuffer[0] = 0;
    305 				ADB_SET_STATE_IDLE_CUDA();
    306 				splx(s);
    307 			}
    308 		} else {
    309 			tickle_serial = adb_cuda_serial;
    310 			tickle_count = 0;
    311 		}
    312 	} else {
    313 		tickle_serial = adb_cuda_serial;
    314 		tickle_count = 0;
    315 	}
    316 
    317 	callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
    318 	    (void *)adb_cuda_tickle, NULL);
    319 }
    320 
    321 /*
    322  * called when when an adb interrupt happens
    323  *
    324  * Cuda version of adb_intr
    325  * TO DO: do we want to add some calls to intr_dispatch() here to
    326  * grab serial interrupts?
    327  */
    328 int
    329 adb_intr_cuda(void *arg)
    330 {
    331 	volatile int i, ending;
    332 	volatile unsigned int s;
    333 	struct adbCommand packet;
    334 	uint8_t reg;
    335 
    336 	s = splhigh();		/* can't be too careful - might be called */
    337 				/* from a routine, NOT an interrupt */
    338 
    339 	reg = read_via_reg(VIA1, vIFR);		/* Read the interrupts */
    340 	if ((reg & 0x80) == 0) {
    341 		splx(s);
    342 		return 0;			/* No interrupts to process */
    343 	}
    344 
    345 	write_via_reg(VIA1, vIFR, reg & 0x7f);	/* Clear 'em */
    346 
    347 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
    348 
    349 switch_start:
    350 	switch (adbActionState) {
    351 	case ADB_ACTION_IDLE:
    352 		/*
    353 		 * This is an unexpected packet, so grab the first (dummy)
    354 		 * byte, set up the proper vars, and tell the chip we are
    355 		 * starting to receive the packet by setting the TIP bit.
    356 		 */
    357 		adbInputBuffer[1] = ADB_SR();
    358 		adb_cuda_serial++;
    359 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
    360 			break;
    361 
    362 		ADB_SET_SR_INPUT();
    363 		ADB_SET_STATE_TIP();
    364 
    365 		adbInputBuffer[0] = 1;
    366 		adbActionState = ADB_ACTION_IN;
    367 #ifdef ADB_DEBUG
    368 		if (adb_debug)
    369 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
    370 #endif
    371 		break;
    372 
    373 	case ADB_ACTION_IN:
    374 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
    375 		/* intr off means this is the last byte (end of frame) */
    376 		if (ADB_INTR_IS_OFF)
    377 			ending = 1;
    378 		else
    379 			ending = 0;
    380 
    381 		if (1 == ending) {	/* end of message? */
    382 #ifdef ADB_DEBUG
    383 			if (adb_debug) {
    384 				printf_intr("in end 0x%02x ",
    385 				    adbInputBuffer[adbInputBuffer[0]]);
    386 				print_single(adbInputBuffer);
    387 			}
    388 #endif
    389 
    390 			/*
    391 			 * Are we waiting AND does this packet match what we
    392 			 * are waiting for AND is it coming from either the
    393 			 * ADB or RTC/PRAM sub-device? This section _should_
    394 			 * recognize all ADB and RTC/PRAM type commands, but
    395 			 * there may be more... NOTE: commands are always at
    396 			 * [4], even for RTC/PRAM commands.
    397 			 */
    398 			/* set up data for adb_pass_up */
    399 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
    400 
    401 			if ((adbWaiting == 1) &&
    402 			    (adbInputBuffer[4] == adbWaitingCmd) &&
    403 			    ((adbInputBuffer[2] == 0x00) ||
    404 			    (adbInputBuffer[2] == 0x01))) {
    405 				packet.saveBuf = adbBuffer;
    406 				packet.compRout = adbCompRout;
    407 				packet.compData = adbCompData;
    408 				packet.unsol = 0;
    409 				packet.ack_only = 0;
    410 				adb_pass_up(&packet);
    411 
    412 				adbWaitingCmd = 0;	/* reset "waiting" vars */
    413 				adbWaiting = 0;
    414 				adbBuffer = (long)0;
    415 				adbCompRout = (long)0;
    416 				adbCompData = (long)0;
    417 			} else {
    418 				packet.unsol = 1;
    419 				packet.ack_only = 0;
    420 				adb_pass_up(&packet);
    421 			}
    422 
    423 
    424 			/* reset vars and signal the end of this frame */
    425 			adbActionState = ADB_ACTION_IDLE;
    426 			adbInputBuffer[0] = 0;
    427 			ADB_SET_STATE_IDLE_CUDA();
    428 			/*ADB_SET_SR_INPUT();*/
    429 
    430 			/*
    431 			 * If there is something waiting to be sent out,
    432 			 * the set everything up and send the first byte.
    433 			 */
    434 			if (adbWriteDelay == 1) {
    435 				delay(ADB_DELAY);	/* required */
    436 				adbSentChars = 0;
    437 				adbActionState = ADB_ACTION_OUT;
    438 				/*
    439 				 * If the interrupt is on, we were too slow
    440 				 * and the chip has already started to send
    441 				 * something to us, so back out of the write
    442 				 * and start a read cycle.
    443 				 */
    444 				if (ADB_INTR_IS_ON) {
    445 					ADB_SET_SR_INPUT();
    446 					ADB_SET_STATE_IDLE_CUDA();
    447 					adbSentChars = 0;
    448 					adbActionState = ADB_ACTION_IDLE;
    449 					adbInputBuffer[0] = 0;
    450 					break;
    451 				}
    452 				/*
    453 				 * If we got here, it's ok to start sending
    454 				 * so load the first byte and tell the chip
    455 				 * we want to send.
    456 				 */
    457 				ADB_SET_STATE_TIP();
    458 				ADB_SET_SR_OUTPUT();
    459 				write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
    460 			}
    461 		} else {
    462 			ADB_TOGGLE_STATE_ACK_CUDA();
    463 #ifdef ADB_DEBUG
    464 			if (adb_debug)
    465 				printf_intr("in 0x%02x ",
    466 				    adbInputBuffer[adbInputBuffer[0]]);
    467 #endif
    468 		}
    469 		break;
    470 
    471 	case ADB_ACTION_OUT:
    472 		i = ADB_SR();	/* reset SR-intr in IFR */
    473 #ifdef ADB_DEBUG
    474 		if (adb_debug)
    475 			printf_intr("intr out 0x%02x ", i);
    476 #endif
    477 
    478 		adbSentChars++;
    479 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
    480 #ifdef ADB_DEBUG
    481 			if (adb_debug)
    482 				printf_intr("intr was on ");
    483 #endif
    484 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
    485 			ADB_SET_STATE_IDLE_CUDA();
    486 			adbSentChars = 0;	/* must start all over */
    487 			adbActionState = ADB_ACTION_IDLE;	/* new state */
    488 			adbInputBuffer[0] = 0;
    489 			adbWriteDelay = 1;	/* must retry when done with
    490 						 * read */
    491 			delay(ADB_DELAY);
    492 			goto switch_start;	/* process next state right
    493 						 * now */
    494 			break;
    495 		}
    496 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
    497 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
    498 									 * back? */
    499 				adbWaiting = 1;	/* signal waiting for return */
    500 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
    501 			} else {	/* no talk, so done */
    502 				/* set up stuff for adb_pass_up */
    503 				memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
    504 				packet.saveBuf = adbBuffer;
    505 				packet.compRout = adbCompRout;
    506 				packet.compData = adbCompData;
    507 				packet.cmd = adbWaitingCmd;
    508 				packet.unsol = 0;
    509 				packet.ack_only = 1;
    510 				adb_pass_up(&packet);
    511 
    512 				/* reset "waiting" vars, just in case */
    513 				adbWaitingCmd = 0;
    514 				adbBuffer = (long)0;
    515 				adbCompRout = NULL;
    516 				adbCompData = NULL;
    517 			}
    518 
    519 			adbWriteDelay = 0;	/* done writing */
    520 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
    521 			ADB_SET_SR_INPUT();
    522 			ADB_SET_STATE_IDLE_CUDA();
    523 #ifdef ADB_DEBUG
    524 			if (adb_debug)
    525 				printf_intr("write done ");
    526 #endif
    527 		} else {
    528 			write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* send next byte */
    529 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
    530 							 * shift */
    531 #ifdef ADB_DEBUG
    532 			if (adb_debug)
    533 				printf_intr("toggle ");
    534 #endif
    535 		}
    536 		break;
    537 
    538 	case ADB_ACTION_NOTREADY:
    539 #ifdef ADB_DEBUG
    540 		if (adb_debug)
    541 			printf_intr("adb: not yet initialized\n");
    542 #endif
    543 		break;
    544 
    545 	default:
    546 #ifdef ADB_DEBUG
    547 		if (adb_debug)
    548 			printf_intr("intr: unknown ADB state\n");
    549 #endif
    550 		break;
    551 	}
    552 
    553 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
    554 
    555 	splx(s);		/* restore */
    556 
    557 	return 1;
    558 }				/* end adb_intr_cuda */
    559 
    560 
    561 int
    562 send_adb_cuda(u_char * in, u_char * buffer, adbComp *compRout,
    563     volatile void *data, int command)
    564 {
    565 	int s, len;
    566 
    567 #ifdef ADB_DEBUG
    568 	if (adb_debug)
    569 		printf_intr("SEND\n");
    570 #endif
    571 
    572 	if (adbActionState == ADB_ACTION_NOTREADY)
    573 		return 1;
    574 
    575 	/* Don't interrupt while we are messing with the ADB */
    576 	s = splhigh();
    577 
    578 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
    579 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupt? */
    580 	} else
    581 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
    582 			adbWriteDelay = 1;	/* if no, then we'll "queue"
    583 						 * it up */
    584 		else {
    585 			splx(s);
    586 			return 1;	/* really busy! */
    587 		}
    588 
    589 #ifdef ADB_DEBUG
    590 	if (adb_debug)
    591 		printf_intr("QUEUE\n");
    592 #endif
    593 	if ((long)in == (long)0) {	/* need to convert? */
    594 		/*
    595 		 * Don't need to use adb_cmd_extra here because this section
    596 		 * will be called ONLY when it is an ADB command (no RTC or
    597 		 * PRAM)
    598 		 */
    599 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
    600 						 * doing a listen! */
    601 			len = buffer[0];	/* length of additional data */
    602 		else
    603 			len = 0;/* no additional data */
    604 
    605 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
    606 						 * data */
    607 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
    608 		adbOutputBuffer[2] = (u_char)command;	/* load command */
    609 
    610 		/* copy additional output data, if any */
    611 		memcpy(adbOutputBuffer + 3, buffer + 1, len);
    612 	} else
    613 		/* if data ready, just copy over */
    614 		memcpy(adbOutputBuffer, in, in[0] + 2);
    615 
    616 	adbSentChars = 0;	/* nothing sent yet */
    617 	adbBuffer = buffer;	/* save buffer to know where to save result */
    618 	adbCompRout = compRout;	/* save completion routine pointer */
    619 	adbCompData = data;	/* save completion routine data pointer */
    620 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
    621 
    622 	if (adbWriteDelay != 1) {	/* start command now? */
    623 #ifdef ADB_DEBUG
    624 		if (adb_debug)
    625 			printf_intr("out start NOW");
    626 #endif
    627 		delay(ADB_DELAY);
    628 		adbActionState = ADB_ACTION_OUT;	/* set next state */
    629 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
    630 		write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* load byte for output */
    631 		ADB_SET_STATE_ACKOFF_CUDA();
    632 		ADB_SET_STATE_TIP();	/* tell ADB that we want to send */
    633 	}
    634 	adbWriteDelay = 1;	/* something in the write "queue" */
    635 
    636 	splx(s);
    637 
    638 	if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */
    639 		/* poll until byte done */
    640 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
    641 		    || (adbWaiting == 1))
    642 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
    643 				adb_intr_cuda(NULL);	/* process it */
    644 				adb_soft_intr();
    645 			}
    646 
    647 	return 0;
    648 }				/* send_adb_cuda */
    649 
    650 int
    651 adb_intr(void *arg)
    652 {
    653 	switch (adbHardware) {
    654 	case ADB_HW_PMU:
    655 		return pm_intr(arg);
    656 		break;
    657 
    658 	case ADB_HW_CUDA:
    659 		return adb_intr_cuda(arg);
    660 		break;
    661 
    662 	case ADB_HW_UNKNOWN:
    663 		break;
    664 	}
    665 	return 0;
    666 }
    667 
    668 
    669 /*
    670  * adb_pass_up is called by the interrupt-time routines.
    671  * It takes the raw packet data that was received from the
    672  * device and puts it into the queue that the upper half
    673  * processes. It then signals for a soft ADB interrupt which
    674  * will eventually call the upper half routine (adb_soft_intr).
    675  *
    676  * If in->unsol is 0, then this is either the notification
    677  * that the packet was sent (on a LISTEN, for example), or the
    678  * response from the device (on a TALK). The completion routine
    679  * is called only if the user specified one.
    680  *
    681  * If in->unsol is 1, then this packet was unsolicited and
    682  * so we look up the device in the ADB device table to determine
    683  * what it's default service routine is.
    684  *
    685  * If in->ack_only is 1, then we really only need to call
    686  * the completion routine, so don't do any other stuff.
    687  *
    688  * Note that in->data contains the packet header AND data,
    689  * while adbInbound[]->data contains ONLY data.
    690  *
    691  * Note: Called only at interrupt time. Assumes this.
    692  */
    693 void
    694 adb_pass_up(struct adbCommand *in)
    695 {
    696 	int start = 0, len = 0, cmd = 0;
    697 	ADBDataBlock block;
    698 
    699 	/* temp for testing */
    700 	/*u_char *buffer = 0;*/
    701 	/*u_char *compdata = 0;*/
    702 	/*u_char *comprout = 0;*/
    703 
    704 	if (adbInCount >= ADB_QUEUE) {
    705 #ifdef ADB_DEBUG
    706 		if (adb_debug)
    707 			printf_intr("adb: ring buffer overflow\n");
    708 #endif
    709 		return;
    710 	}
    711 
    712 	if (in->ack_only) {
    713 		len = in->data[0];
    714 		cmd = in->cmd;
    715 		start = 0;
    716 	} else {
    717 		switch (adbHardware) {
    718 		case ADB_HW_CUDA:
    719 			/* If it's unsolicited, accept only ADB data for now */
    720 			if (in->unsol)
    721 				if (0 != in->data[2])
    722 					return;
    723 			cmd = in->data[4];
    724 			if (in->data[0] < 5)
    725 				len = 0;
    726 			else
    727 				len = in->data[0]-4;
    728 			start = 4;
    729 			break;
    730 
    731 		case ADB_HW_PMU:
    732 			cmd = in->data[1];
    733 			if (in->data[0] < 2)
    734 				len = 0;
    735 			else
    736 				len = in->data[0]-1;
    737 			start = 1;
    738 			break;
    739 
    740 		case ADB_HW_UNKNOWN:
    741 			return;
    742 		}
    743 
    744 		/* Make sure there is a valid device entry for this device */
    745 		if (in->unsol) {
    746 			/* ignore unsolicited data during adbreinit */
    747 			if (adbStarting)
    748 				return;
    749 			/* get device's comp. routine and data area */
    750 			if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
    751 				return;
    752 		}
    753 	}
    754 
    755 	/*
    756  	 * If this is an unsolicited packet, we need to fill in
    757  	 * some info so adb_soft_intr can process this packet
    758  	 * properly. If it's not unsolicited, then use what
    759  	 * the caller sent us.
    760  	 */
    761 	if (in->unsol) {
    762 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
    763 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
    764 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
    765 	} else {
    766 		adbInbound[adbInTail].compRout = in->compRout;
    767 		adbInbound[adbInTail].compData = in->compData;
    768 		adbInbound[adbInTail].saveBuf = in->saveBuf;
    769 	}
    770 
    771 #ifdef ADB_DEBUG
    772 	if (adb_debug && in->data[1] == 2)
    773 		printf_intr("adb: caught error\n");
    774 #endif
    775 
    776 	/* copy the packet data over */
    777 	/*
    778 	 * TO DO: If the *_intr routines fed their incoming data
    779 	 * directly into an adbCommand struct, which is passed to
    780 	 * this routine, then we could eliminate this copy.
    781 	 */
    782 	memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
    783 	adbInbound[adbInTail].data[0] = len;
    784 	adbInbound[adbInTail].cmd = cmd;
    785 
    786 	adbInCount++;
    787 	if (++adbInTail >= ADB_QUEUE)
    788 		adbInTail = 0;
    789 
    790 	/*
    791 	 * If the debugger is running, call upper half manually.
    792 	 * Otherwise, trigger a soft interrupt to handle the rest later.
    793 	 */
    794 	if (adb_polling)
    795 		adb_soft_intr();
    796 	else
    797 		setsoftadb();
    798 
    799 	return;
    800 }
    801 
    802 
    803 /*
    804  * Called to process the packets after they have been
    805  * placed in the incoming queue.
    806  *
    807  */
    808 void
    809 adb_soft_intr(void)
    810 {
    811 	int s;
    812 	int cmd = 0;
    813 	u_char *buffer = 0;
    814 	adbComp *comprout = NULL;
    815 	volatile int *compdata = 0;
    816 
    817 #if 0
    818 	s = splhigh();
    819 	printf_intr("sr: %x\n", (s & 0x0700));
    820 	splx(s);
    821 #endif
    822 
    823 /*delay(2*ADB_DELAY);*/
    824 
    825 	while (adbInCount) {
    826 #ifdef ADB_DEBUG
    827 		if (adb_debug & 0x80)
    828 			printf_intr("%x %x %x ",
    829 			    adbInCount, adbInHead, adbInTail);
    830 #endif
    831 		/* get the data we need from the queue */
    832 		buffer = adbInbound[adbInHead].saveBuf;
    833 		comprout = adbInbound[adbInHead].compRout;
    834 		compdata = adbInbound[adbInHead].compData;
    835 		cmd = adbInbound[adbInHead].cmd;
    836 
    837 		/* copy over data to data area if it's valid */
    838 		/*
    839 		 * Note that for unsol packets we don't want to copy the
    840 		 * data anywhere, so buffer was already set to 0.
    841 		 * For ack_only buffer was set to 0, so don't copy.
    842 		 */
    843 		if (buffer)
    844 			memcpy(buffer, adbInbound[adbInHead].data,
    845 			    adbInbound[adbInHead].data[0] + 1);
    846 
    847 #ifdef ADB_DEBUG
    848 			if (adb_debug & 0x80) {
    849 				printf_intr("%p %p %p %x ",
    850 				    buffer, comprout, compdata, (short)cmd);
    851 				printf_intr("buf: ");
    852 				print_single(adbInbound[adbInHead].data);
    853 			}
    854 #endif
    855 		/* Remove the packet from the queue before calling
    856 		 * the completion routine, so that the completion
    857 		 * routine can reentrantly process the queue.  For
    858 		 * example, this happens when polling is turned on
    859 		 * by entering the debuger by keystroke.
    860 		 */
    861 		s = splhigh();
    862 		adbInCount--;
    863 		if (++adbInHead >= ADB_QUEUE)
    864 			adbInHead = 0;
    865 		splx(s);
    866 
    867 		/* call default completion routine if it's valid */
    868 		if (comprout)
    869 			(*comprout)(buffer, compdata, cmd);
    870 	}
    871 	return;
    872 }
    873 
    874 
    875 /*
    876  * This is my version of the ADBOp routine. It mainly just calls the
    877  * hardware-specific routine.
    878  *
    879  *   data 	: pointer to data area to be used by compRout
    880  *   compRout	: completion routine
    881  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
    882  *		  byte 0 = # of bytes
    883  *		: for TALK: points to place to save return data
    884  *   command	: the adb command to send
    885  *   result	: 0 = success
    886  *		: -1 = could not complete
    887  */
    888 int
    889 adb_op(Ptr buffer, adbComp *compRout, volatile void *data, short command)
    890 {
    891 	int result;
    892 
    893 	switch (adbHardware) {
    894 	case ADB_HW_PMU:
    895 		result = pm_adb_op((u_char *)buffer, compRout,
    896 		    data, (int)command);
    897 
    898 		if (result == 0)
    899 			return 0;
    900 		else
    901 			return -1;
    902 		break;
    903 
    904 	case ADB_HW_CUDA:
    905 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
    906 		    compRout, data, (int)command);
    907 		if (result == 0)
    908 			return 0;
    909 		else
    910 			return -1;
    911 		break;
    912 
    913 	case ADB_HW_UNKNOWN:
    914 	default:
    915 		return -1;
    916 	}
    917 }
    918 
    919 
    920 /*
    921  * adb_hw_setup
    922  * This routine sets up the possible machine specific hardware
    923  * config (mainly VIA settings) for the various models.
    924  */
    925 void
    926 adb_hw_setup(void)
    927 {
    928 	volatile int i;
    929 
    930 	switch (adbHardware) {
    931 	case ADB_HW_PMU:
    932 		/*
    933 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
    934 		 * pm_direct.h?
    935 		 */
    936 		write_via_reg(VIA1, vIFR, 0x90);	/* clear interrupt */
    937 		break;
    938 
    939 	case ADB_HW_CUDA:
    940 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
    941 						 * outputs */
    942 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
    943 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
    944 							 * to IN */
    945 		write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
    946 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
    947 							 * hardware */
    948 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
    949 						 * are on */
    950 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
    951 
    952 		/* sort of a device reset */
    953 		i = ADB_SR();	/* clear interrupt */
    954 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
    955 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
    956 		delay(ADB_DELAY);
    957 		ADB_SET_STATE_TIP();	/* signal start of frame */
    958 		delay(ADB_DELAY);
    959 		ADB_TOGGLE_STATE_ACK_CUDA();
    960 		delay(ADB_DELAY);
    961 		ADB_CLR_STATE_TIP();
    962 		delay(ADB_DELAY);
    963 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
    964 		i = ADB_SR();	/* clear interrupt */
    965 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
    966 		break;
    967 
    968 	case ADB_HW_UNKNOWN:
    969 	default:
    970 		write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
    971 						 * DO: turn PB ints off? */
    972 		return;
    973 		break;
    974 	}
    975 }
    976 
    977 /*
    978  * adb_reinit sets up the adb stuff
    979  *
    980  */
    981 void
    982 adb_reinit(void)
    983 {
    984 	u_char send_string[ADB_MAX_MSG_LENGTH];
    985 	ADBDataBlock data;	/* temp. holder for getting device info */
    986 	volatile int i, x;
    987 	int s = 0;		/* XXX: gcc */
    988 	int command;
    989 	int result;
    990 	int saveptr;		/* point to next free relocation address */
    991 	int device;
    992 	int nonewtimes;		/* times thru loop w/o any new devices */
    993 
    994 	/* Make sure we are not interrupted while building the table. */
    995 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
    996 		s = splhigh();
    997 
    998 	ADBNumDevices = 0;	/* no devices yet */
    999 
   1000 	/* Let intr routines know we are running reinit */
   1001 	adbStarting = 1;
   1002 
   1003 	/*
   1004 	 * Initialize the ADB table.  For now, we'll always use the same table
   1005 	 * that is defined at the beginning of this file - no mallocs.
   1006 	 */
   1007 	for (i = 0; i < 16; i++)
   1008 		ADBDevTable[i].devType = 0;
   1009 
   1010 	adb_setup_hw_type();	/* setup hardware type */
   1011 
   1012 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
   1013 
   1014 	delay(1000);
   1015 
   1016 	/* send an ADB reset first */
   1017 	result = adb_op_sync((Ptr)0, NULL, (Ptr)0, (short)0x00);
   1018 	delay(200000);
   1019 
   1020 #ifdef ADB_DEBUG
   1021 	if (result && adb_debug) {
   1022 		printf_intr("adb_reinit: failed to reset, result = %d\n",result);
   1023 	}
   1024 #endif
   1025 
   1026 	/*
   1027 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
   1028 	 * which device addresses are in use and which are free. For each
   1029 	 * address that is in use, move the device at that address to a higher
   1030 	 * free address. Continue doing this at that address until no device
   1031 	 * responds at that address. Then move the last device that was moved
   1032 	 * back to the original address. Do this for the remaining addresses
   1033 	 * that we determined were in use.
   1034 	 *
   1035 	 * When finished, do this entire process over again with the updated
   1036 	 * list of in use addresses. Do this until no new devices have been
   1037 	 * found in 20 passes though the in use address list. (This probably
   1038 	 * seems long and complicated, but it's the best way to detect multiple
   1039 	 * devices at the same address - sometimes it takes a couple of tries
   1040 	 * before the collision is detected.)
   1041 	 */
   1042 
   1043 	/* initial scan through the devices */
   1044 	for (i = 1; i < 16; i++) {
   1045 		send_string[0] = 0;
   1046 		command = ADBTALK(i, 3);
   1047 		result = adb_op_sync((Ptr)send_string, NULL,
   1048 		    (Ptr)0, (short)command);
   1049 
   1050 #ifdef ADB_DEBUG
   1051 		if (result && adb_debug) {
   1052 			printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
   1053 					i,result,send_string[0]);
   1054 		}
   1055 #endif
   1056 
   1057 		if (send_string[0] != 0) {
   1058 			/* check for valid device handler */
   1059 			switch (send_string[2]) {
   1060 			case 0:
   1061 			case 0xfd:
   1062 			case 0xfe:
   1063 			case 0xff:
   1064 				continue;	/* invalid, skip */
   1065 			}
   1066 
   1067 			/* found a device */
   1068 			++ADBNumDevices;
   1069 			KASSERT(ADBNumDevices < 16);
   1070 			ADBDevTable[ADBNumDevices].devType =
   1071 				(int)send_string[2];
   1072 			ADBDevTable[ADBNumDevices].origAddr = i;
   1073 			ADBDevTable[ADBNumDevices].currentAddr = i;
   1074 			ADBDevTable[ADBNumDevices].DataAreaAddr =
   1075 			    (long)0;
   1076 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
   1077 			pm_check_adb_devices(i);	/* tell pm driver device
   1078 							 * is here */
   1079 		}
   1080 	}
   1081 
   1082 	/* find highest unused address */
   1083 	for (saveptr = 15; saveptr > 0; saveptr--)
   1084 		if (-1 == get_adb_info(&data, saveptr))
   1085 			break;
   1086 
   1087 #ifdef ADB_DEBUG
   1088 	if (adb_debug & 0x80) {
   1089 		printf_intr("first free is: 0x%02x\n", saveptr);
   1090 		printf_intr("devices: %i\n", ADBNumDevices);
   1091 	}
   1092 #endif
   1093 
   1094 	nonewtimes = 0;		/* no loops w/o new devices */
   1095 	while (saveptr > 0 && nonewtimes++ < 11) {
   1096 		for (i = 1; i <= ADBNumDevices; i++) {
   1097 			device = ADBDevTable[i].currentAddr;
   1098 #ifdef ADB_DEBUG
   1099 			if (adb_debug & 0x80)
   1100 				printf_intr("moving device 0x%02x to 0x%02x "
   1101 				    "(index 0x%02x)  ", device, saveptr, i);
   1102 #endif
   1103 
   1104 			/* send TALK R3 to address */
   1105 			command = ADBTALK(device, 3);
   1106 			adb_op_sync((Ptr)send_string, NULL,
   1107 			    (Ptr)0, (short)command);
   1108 
   1109 			/* move device to higher address */
   1110 			command = ADBLISTEN(device, 3);
   1111 			send_string[0] = 2;
   1112 			send_string[1] = (u_char)(saveptr | 0x60);
   1113 			send_string[2] = 0xfe;
   1114 			adb_op_sync((Ptr)send_string, NULL,
   1115 			    (Ptr)0, (short)command);
   1116 			delay(500);
   1117 
   1118 			/* send TALK R3 - anything at new address? */
   1119 			command = ADBTALK(saveptr, 3);
   1120 			adb_op_sync((Ptr)send_string, NULL,
   1121 			    (Ptr)0, (short)command);
   1122 			delay(500);
   1123 
   1124 			if (send_string[0] == 0) {
   1125 #ifdef ADB_DEBUG
   1126 				if (adb_debug & 0x80)
   1127 					printf_intr("failed, continuing\n");
   1128 #endif
   1129 				continue;
   1130 			}
   1131 
   1132 			/* send TALK R3 - anything at old address? */
   1133 			command = ADBTALK(device, 3);
   1134 			result = adb_op_sync((Ptr)send_string, NULL,
   1135 			    (Ptr)0, (short)command);
   1136 			if (send_string[0] != 0) {
   1137 				/* check for valid device handler */
   1138 				switch (send_string[2]) {
   1139 				case 0:
   1140 				case 0xfd:
   1141 				case 0xfe:
   1142 				case 0xff:
   1143 					continue;	/* invalid, skip */
   1144 				}
   1145 
   1146 				/* new device found */
   1147 				/* update data for previously moved device */
   1148 				ADBDevTable[i].currentAddr = saveptr;
   1149 #ifdef ADB_DEBUG
   1150 				if (adb_debug & 0x80)
   1151 					printf_intr("old device at index %i\n",i);
   1152 #endif
   1153 				/* add new device in table */
   1154 #ifdef ADB_DEBUG
   1155 				if (adb_debug & 0x80)
   1156 					printf_intr("new device found\n");
   1157 #endif
   1158 				if (saveptr > ADBNumDevices) {
   1159 					++ADBNumDevices;
   1160 					KASSERT(ADBNumDevices < 16);
   1161 				}
   1162 				ADBDevTable[ADBNumDevices].devType =
   1163 					(int)send_string[2];
   1164 				ADBDevTable[ADBNumDevices].origAddr = device;
   1165 				ADBDevTable[ADBNumDevices].currentAddr = device;
   1166 				/* These will be set correctly in adbsys.c */
   1167 				/* Until then, unsol. data will be ignored. */
   1168 				ADBDevTable[ADBNumDevices].DataAreaAddr =
   1169 				    (long)0;
   1170 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
   1171 				    (void *)0;
   1172 				/* find next unused address */
   1173 				for (x = saveptr; x > 0; x--) {
   1174 					if (-1 == get_adb_info(&data, x)) {
   1175 						saveptr = x;
   1176 						break;
   1177 					}
   1178 				}
   1179 				if (x == 0)
   1180 					saveptr = 0;
   1181 #ifdef ADB_DEBUG
   1182 				if (adb_debug & 0x80)
   1183 					printf_intr("new free is 0x%02x\n",
   1184 					    saveptr);
   1185 #endif
   1186 				nonewtimes = 0;
   1187 				/* tell pm driver device is here */
   1188 				pm_check_adb_devices(device);
   1189 			} else {
   1190 #ifdef ADB_DEBUG
   1191 				if (adb_debug & 0x80)
   1192 					printf_intr("moving back...\n");
   1193 #endif
   1194 				/* move old device back */
   1195 				command = ADBLISTEN(saveptr, 3);
   1196 				send_string[0] = 2;
   1197 				send_string[1] = (u_char)(device | 0x60);
   1198 				send_string[2] = 0xfe;
   1199 				adb_op_sync((Ptr)send_string, NULL,
   1200 				    (Ptr)0, (short)command);
   1201 				delay(1000);
   1202 			}
   1203 		}
   1204 	}
   1205 
   1206 #ifdef ADB_DEBUG
   1207 	if (adb_debug) {
   1208 		for (i = 1; i <= ADBNumDevices; i++) {
   1209 			x = get_ind_adb_info(&data, i);
   1210 			if (x != -1)
   1211 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
   1212 				    i, x, data.devType);
   1213 		}
   1214 	}
   1215 #endif
   1216 
   1217 #ifdef ADB_DEBUG
   1218 	if (adb_debug) {
   1219 		if (0 == ADBNumDevices)	/* tell user if no devices found */
   1220 			printf_intr("adb: no devices found\n");
   1221 	}
   1222 #endif
   1223 
   1224 	adbStarting = 0;	/* not starting anymore */
   1225 #ifdef ADB_DEBUG
   1226 	if (adb_debug)
   1227 		printf_intr("adb: ADBReInit complete\n");
   1228 #endif
   1229 
   1230 	if (adbHardware == ADB_HW_CUDA)
   1231 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
   1232 		    (void *)adb_cuda_tickle, NULL);
   1233 
   1234 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
   1235 		splx(s);
   1236 }
   1237 
   1238 /*
   1239  * adb_cmd_result
   1240  *
   1241  * This routine lets the caller know whether the specified adb command string
   1242  * should expect a returned result, such as a TALK command.
   1243  *
   1244  * returns: 0 if a result should be expected
   1245  *          1 if a result should NOT be expected
   1246  */
   1247 int
   1248 adb_cmd_result(u_char *in)
   1249 {
   1250 	switch (adbHardware) {
   1251 	case ADB_HW_CUDA:
   1252 		/* was it an ADB talk command? */
   1253 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
   1254 			return 0;
   1255 		/* was it an RTC/PRAM read date/time? */
   1256 		if ((in[1] == 0x01) && (in[2] == 0x03))
   1257 			return 0;
   1258 		return 1;
   1259 
   1260 	case ADB_HW_PMU:
   1261 		return 1;
   1262 
   1263 	case ADB_HW_UNKNOWN:
   1264 	default:
   1265 		return 1;
   1266 	}
   1267 }
   1268 
   1269 
   1270 /*
   1271  * adb_cmd_extra
   1272  *
   1273  * This routine lets the caller know whether the specified adb command string
   1274  * may have extra data appended to the end of it, such as a LISTEN command.
   1275  *
   1276  * returns: 0 if extra data is allowed
   1277  *          1 if extra data is NOT allowed
   1278  */
   1279 int
   1280 adb_cmd_extra(u_char *in)
   1281 {
   1282 	switch (adbHardware) {
   1283 	case ADB_HW_CUDA:
   1284 		/*
   1285 		 * TO DO: support needs to be added to recognize RTC and PRAM
   1286 		 * commands
   1287 		 */
   1288 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
   1289 			return 0;
   1290 		/* add others later */
   1291 		return 1;
   1292 
   1293 	case ADB_HW_PMU:
   1294 		return 1;
   1295 
   1296 	case ADB_HW_UNKNOWN:
   1297 	default:
   1298 		return 1;
   1299 	}
   1300 }
   1301 
   1302 /*
   1303  * adb_op_sync
   1304  *
   1305  * This routine does exactly what the adb_op routine does, except that after
   1306  * the adb_op is called, it waits until the return value is present before
   1307  * returning.
   1308  *
   1309  * NOTE: The user specified compRout is ignored, since this routine specifies
   1310  * it's own to adb_op, which is why you really called this in the first place
   1311  * anyway.
   1312  */
   1313 int
   1314 adb_op_sync(Ptr buffer, adbComp *compRout, Ptr data, short command)
   1315 {
   1316 	int tmout;
   1317 	int result;
   1318 	volatile int flag = 0;
   1319 
   1320 	result = adb_op(buffer, adb_op_comprout,
   1321 	    &flag, command);	/* send command */
   1322 	if (result == 0) {		/* send ok? */
   1323 		/*
   1324 		 * Total time to wait is calculated as follows:
   1325 		 *  - Tlt (stop to start time): 260 usec
   1326 		 *  - start bit: 100 usec
   1327 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
   1328 		 *  - stop bit (with SRQ): 140 usec
   1329 		 * Total: 6900 usec
   1330 		 *
   1331 		 * This is the total time allowed by the specification.  Any
   1332 		 * device that doesn't conform to this will fail to operate
   1333 		 * properly on some Apple systems.  In spite of this we
   1334 		 * double the time to wait; some Cuda-based apparently
   1335 		 * queues some commands and allows the main CPU to continue
   1336 		 * processing (radical concept, eh?).  To be safe, allow
   1337 		 * time for two complete ADB transactions to occur.
   1338 		 */
   1339 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
   1340 			delay(10);
   1341 		if (!flag && tmout > 0)
   1342 			delay(tmout);
   1343 
   1344 		if (!flag)
   1345 			result = -2;
   1346 	}
   1347 
   1348 	return result;
   1349 }
   1350 
   1351 /*
   1352  * adb_op_comprout
   1353  *
   1354  * This function is used by the adb_op_sync routine so it knows when the
   1355  * function is done.
   1356  */
   1357 void
   1358 adb_op_comprout(caddr_t buffer, volatile int *compdata, int cmd)
   1359 {
   1360 	volatile int *p = compdata;
   1361 
   1362 	*p = 1;
   1363 }
   1364 
   1365 void
   1366 adb_setup_hw_type(void)
   1367 {
   1368 	switch (adbHardware) {
   1369 	case ADB_HW_CUDA:
   1370 		return;
   1371 
   1372 	case ADB_HW_PMU:
   1373 		pm_setup_adb();
   1374 		return;
   1375 
   1376 	default:
   1377 		panic("unknown adb hardware");
   1378 	}
   1379 }
   1380 
   1381 int
   1382 count_adbs(void)
   1383 {
   1384 	int i;
   1385 	int found;
   1386 
   1387 	found = 0;
   1388 
   1389 	for (i = 1; i < 16; i++)
   1390 		if (0 != ADBDevTable[i].devType)
   1391 			found++;
   1392 
   1393 	return found;
   1394 }
   1395 
   1396 int
   1397 get_ind_adb_info(ADBDataBlock * info, int index)
   1398 {
   1399 	if ((index < 1) || (index > 15))	/* check range 1-15 */
   1400 		return (-1);
   1401 
   1402 #ifdef ADB_DEBUG
   1403 	if (adb_debug & 0x80)
   1404 		printf_intr("index 0x%x devType is: 0x%x\n", index,
   1405 		    ADBDevTable[index].devType);
   1406 #endif
   1407 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
   1408 		return (-1);
   1409 
   1410 	info->devType = ADBDevTable[index].devType;
   1411 	info->origADBAddr = ADBDevTable[index].origAddr;
   1412 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
   1413 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
   1414 
   1415 	return (ADBDevTable[index].currentAddr);
   1416 }
   1417 
   1418 int
   1419 get_adb_info(ADBDataBlock * info, int adbAddr)
   1420 {
   1421 	int i;
   1422 
   1423 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1424 		return (-1);
   1425 
   1426 	for (i = 1; i < 15; i++)
   1427 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1428 			info->devType = ADBDevTable[i].devType;
   1429 			info->origADBAddr = ADBDevTable[i].origAddr;
   1430 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
   1431 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
   1432 			return 0;	/* found */
   1433 		}
   1434 
   1435 	return (-1);		/* not found */
   1436 }
   1437 
   1438 int
   1439 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
   1440 {
   1441 	int i;
   1442 
   1443 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
   1444 		return (-1);
   1445 
   1446 	for (i = 1; i < 15; i++)
   1447 		if (ADBDevTable[i].currentAddr == adbAddr) {
   1448 			ADBDevTable[i].ServiceRtPtr =
   1449 			    (void *)(info->siServiceRtPtr);
   1450 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
   1451 			return 0;	/* found */
   1452 		}
   1453 
   1454 	return (-1);		/* not found */
   1455 
   1456 }
   1457 
   1458 #ifndef MRG_ADB
   1459 
   1460 /* caller should really use machine-independent version: getPramTime */
   1461 /* this version does pseudo-adb access only */
   1462 int
   1463 adb_read_date_time(unsigned long *t)
   1464 {
   1465 	u_char output[ADB_MAX_MSG_LENGTH];
   1466 	int result;
   1467 	volatile int flag = 0;
   1468 
   1469 	switch (adbHardware) {
   1470 	case ADB_HW_PMU:
   1471 		pm_read_date_time(t);
   1472 		return 0;
   1473 
   1474 	case ADB_HW_CUDA:
   1475 		output[0] = 0x02;	/* 2 byte message */
   1476 		output[1] = 0x01;	/* to pram/rtc device */
   1477 		output[2] = 0x03;	/* read date/time */
   1478 		result = send_adb_cuda((u_char *)output, (u_char *)output,
   1479 		    adb_op_comprout, &flag, (int)0);
   1480 		if (result != 0)	/* exit if not sent */
   1481 			return -1;
   1482 
   1483 		while (0 == flag)	/* wait for result */
   1484 			;
   1485 
   1486 		memcpy(t, output + 1, 4);
   1487 		return 0;
   1488 
   1489 	case ADB_HW_UNKNOWN:
   1490 	default:
   1491 		return -1;
   1492 	}
   1493 }
   1494 
   1495 /* caller should really use machine-independent version: setPramTime */
   1496 /* this version does pseudo-adb access only */
   1497 int
   1498 adb_set_date_time(unsigned long t)
   1499 {
   1500 	u_char output[ADB_MAX_MSG_LENGTH];
   1501 	int result;
   1502 	volatile int flag = 0;
   1503 
   1504 	switch (adbHardware) {
   1505 
   1506 	case ADB_HW_CUDA:
   1507 		output[0] = 0x06;	/* 6 byte message */
   1508 		output[1] = 0x01;	/* to pram/rtc device */
   1509 		output[2] = 0x09;	/* set date/time */
   1510 		output[3] = (u_char)(t >> 24);
   1511 		output[4] = (u_char)(t >> 16);
   1512 		output[5] = (u_char)(t >> 8);
   1513 		output[6] = (u_char)(t);
   1514 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1515 		    adb_op_comprout, &flag, (int)0);
   1516 		if (result != 0)	/* exit if not sent */
   1517 			return -1;
   1518 
   1519 		while (0 == flag)	/* wait for send to finish */
   1520 			;
   1521 
   1522 		return 0;
   1523 
   1524 	case ADB_HW_PMU:
   1525 		pm_set_date_time(t);
   1526 		return 0;
   1527 
   1528 	case ADB_HW_UNKNOWN:
   1529 	default:
   1530 		return -1;
   1531 	}
   1532 }
   1533 
   1534 
   1535 int
   1536 adb_poweroff(void)
   1537 {
   1538 	u_char output[ADB_MAX_MSG_LENGTH];
   1539 	int result;
   1540 
   1541 	adb_polling = 1;
   1542 
   1543 	switch (adbHardware) {
   1544 	case ADB_HW_PMU:
   1545 		pm_adb_poweroff();
   1546 
   1547 		for (;;);		/* wait for power off */
   1548 
   1549 		return 0;
   1550 
   1551 	case ADB_HW_CUDA:
   1552 		output[0] = 0x02;	/* 2 byte message */
   1553 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1554 		output[2] = 0x0a;	/* set date/time */
   1555 		result = send_adb_cuda((u_char *)output, (u_char *)0,
   1556 		    (void *)0, (void *)0, (int)0);
   1557 		if (result != 0)	/* exit if not sent */
   1558 			return -1;
   1559 
   1560 		for (;;);		/* wait for power off */
   1561 
   1562 		return 0;
   1563 
   1564 	case ADB_HW_UNKNOWN:
   1565 	default:
   1566 		return -1;
   1567 	}
   1568 }
   1569 
   1570 int
   1571 CountADBs(void)
   1572 {
   1573 	return (count_adbs());
   1574 }
   1575 
   1576 void
   1577 ADBReInit(void)
   1578 {
   1579 	adb_reinit();
   1580 }
   1581 
   1582 int
   1583 GetIndADB(ADBDataBlock * info, int index)
   1584 {
   1585 	return (get_ind_adb_info(info, index));
   1586 }
   1587 
   1588 int
   1589 GetADBInfo(ADBDataBlock * info, int adbAddr)
   1590 {
   1591 	return (get_adb_info(info, adbAddr));
   1592 }
   1593 
   1594 int
   1595 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
   1596 {
   1597 	return (set_adb_info(info, adbAddr));
   1598 }
   1599 
   1600 int
   1601 ADBOp(Ptr buffer, adbComp *compRout, Ptr data, short commandNum)
   1602 {
   1603 	return (adb_op(buffer, compRout, data, commandNum));
   1604 }
   1605 
   1606 #endif
   1607 
   1608 int
   1609 setsoftadb()
   1610 {
   1611 	callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
   1612 	return 0;
   1613 }
   1614 
   1615 void
   1616 adb_cuda_autopoll()
   1617 {
   1618 	volatile int flag = 0;
   1619 	int result;
   1620 	u_char output[16];
   1621 
   1622 	output[0] = 0x03;	/* 3-byte message */
   1623 	output[1] = 0x01;	/* to pram/rtc device */
   1624 	output[2] = 0x01;	/* cuda autopoll */
   1625 	output[3] = 0x01;
   1626 	result = send_adb_cuda(output, output, adb_op_comprout,
   1627 	    &flag, 0);
   1628 	if (result != 0)	/* exit if not sent */
   1629 		return;
   1630 
   1631 	while (flag == 0);	/* wait for result */
   1632 }
   1633 
   1634 void
   1635 adb_restart(void)
   1636 {
   1637 	int result;
   1638 	u_char output[16];
   1639 
   1640 	adb_polling = 1;
   1641 
   1642 	switch (adbHardware) {
   1643 	case ADB_HW_CUDA:
   1644 		output[0] = 0x02;	/* 2 byte message */
   1645 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
   1646 		output[2] = 0x11;	/* restart */
   1647 		result = send_adb_cuda(output, NULL, NULL, NULL, 0);
   1648 		if (result != 0)	/* exit if not sent */
   1649 			return;
   1650 		while (1);		/* not return */
   1651 
   1652 	case ADB_HW_PMU:
   1653 		pm_adb_restart();
   1654 		while (1);		/* not return */
   1655 	}
   1656 }
   1657