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