xref: /netbsd-src/sys/arch/macppc/dev/adb_direct.c (revision aaf4ece63a859a04e37cf3a7229b5fab0157cc06)
1 /*	$NetBSD: adb_direct.c,v 1.33 2005/12/11 12:18:03 christos 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.33 2005/12/11 12:18:03 christos Exp $");
64 
65 #include <sys/param.h>
66 #include <sys/cdefs.h>
67 #include <sys/systm.h>
68 #include <sys/callout.h>
69 #include <sys/device.h>
70 
71 #include <machine/param.h>
72 #include <machine/cpu.h>
73 #include <machine/adbsys.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 = CALLOUT_INITIALIZER;
225 struct callout adb_soft_intr_ch = CALLOUT_INITIALIZER;
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((caddr_t, 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 
995 	/* Make sure we are not interrupted while building the table. */
996 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
997 		s = splhigh();
998 
999 	ADBNumDevices = 0;	/* no devices yet */
1000 
1001 	/* Let intr routines know we are running reinit */
1002 	adbStarting = 1;
1003 
1004 	/*
1005 	 * Initialize the ADB table.  For now, we'll always use the same table
1006 	 * that is defined at the beginning of this file - no mallocs.
1007 	 */
1008 	for (i = 0; i < 16; i++)
1009 		ADBDevTable[i].devType = 0;
1010 
1011 	adb_setup_hw_type();	/* setup hardware type */
1012 
1013 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
1014 
1015 	delay(1000);
1016 
1017 	/* send an ADB reset first */
1018 	result = adb_op_sync((Ptr)0, NULL, (Ptr)0, (short)0x00);
1019 	delay(200000);
1020 
1021 #ifdef ADB_DEBUG
1022 	if (result && adb_debug) {
1023 		printf_intr("adb_reinit: failed to reset, result = %d\n",result);
1024 	}
1025 #endif
1026 
1027 	/*
1028 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
1029 	 * which device addresses are in use and which are free. For each
1030 	 * address that is in use, move the device at that address to a higher
1031 	 * free address. Continue doing this at that address until no device
1032 	 * responds at that address. Then move the last device that was moved
1033 	 * back to the original address. Do this for the remaining addresses
1034 	 * that we determined were in use.
1035 	 *
1036 	 * When finished, do this entire process over again with the updated
1037 	 * list of in use addresses. Do this until no new devices have been
1038 	 * found in 20 passes though the in use address list. (This probably
1039 	 * seems long and complicated, but it's the best way to detect multiple
1040 	 * devices at the same address - sometimes it takes a couple of tries
1041 	 * before the collision is detected.)
1042 	 */
1043 
1044 	/* initial scan through the devices */
1045 	for (i = 1; i < 16; i++) {
1046 		send_string[0] = 0;
1047 		command = ADBTALK(i, 3);
1048 		result = adb_op_sync((Ptr)send_string, NULL,
1049 		    (Ptr)0, (short)command);
1050 
1051 #ifdef ADB_DEBUG
1052 		if (result && adb_debug) {
1053 			printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
1054 					i,result,send_string[0]);
1055 		}
1056 #endif
1057 
1058 		if (send_string[0] != 0) {
1059 			/* check for valid device handler */
1060 			switch (send_string[2]) {
1061 			case 0:
1062 			case 0xfd:
1063 			case 0xfe:
1064 			case 0xff:
1065 				continue;	/* invalid, skip */
1066 			}
1067 
1068 			/* found a device */
1069 			++ADBNumDevices;
1070 			KASSERT(ADBNumDevices < 16);
1071 			ADBDevTable[ADBNumDevices].devType =
1072 				(int)send_string[2];
1073 			ADBDevTable[ADBNumDevices].origAddr = i;
1074 			ADBDevTable[ADBNumDevices].currentAddr = i;
1075 			ADBDevTable[ADBNumDevices].DataAreaAddr =
1076 			    (long)0;
1077 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
1078 			pm_check_adb_devices(i);	/* tell pm driver device
1079 							 * is here */
1080 		}
1081 	}
1082 
1083 	/* find highest unused address */
1084 	for (saveptr = 15; saveptr > 0; saveptr--)
1085 		if (-1 == get_adb_info(&data, saveptr))
1086 			break;
1087 
1088 #ifdef ADB_DEBUG
1089 	if (adb_debug & 0x80) {
1090 		printf_intr("first free is: 0x%02x\n", saveptr);
1091 		printf_intr("devices: %i\n", ADBNumDevices);
1092 	}
1093 #endif
1094 
1095 	nonewtimes = 0;		/* no loops w/o new devices */
1096 	while (saveptr > 0 && nonewtimes++ < 11) {
1097 		for (i = 1; i <= ADBNumDevices; i++) {
1098 			device = ADBDevTable[i].currentAddr;
1099 #ifdef ADB_DEBUG
1100 			if (adb_debug & 0x80)
1101 				printf_intr("moving device 0x%02x to 0x%02x "
1102 				    "(index 0x%02x)  ", device, saveptr, i);
1103 #endif
1104 
1105 			/* send TALK R3 to address */
1106 			command = ADBTALK(device, 3);
1107 			adb_op_sync((Ptr)send_string, NULL,
1108 			    (Ptr)0, (short)command);
1109 
1110 			/* move device to higher address */
1111 			command = ADBLISTEN(device, 3);
1112 			send_string[0] = 2;
1113 			send_string[1] = (u_char)(saveptr | 0x60);
1114 			send_string[2] = 0xfe;
1115 			adb_op_sync((Ptr)send_string, NULL,
1116 			    (Ptr)0, (short)command);
1117 			delay(500);
1118 
1119 			/* send TALK R3 - anything at new address? */
1120 			command = ADBTALK(saveptr, 3);
1121 			adb_op_sync((Ptr)send_string, NULL,
1122 			    (Ptr)0, (short)command);
1123 			delay(500);
1124 
1125 			if (send_string[0] == 0) {
1126 #ifdef ADB_DEBUG
1127 				if (adb_debug & 0x80)
1128 					printf_intr("failed, continuing\n");
1129 #endif
1130 				continue;
1131 			}
1132 
1133 			/* send TALK R3 - anything at old address? */
1134 			command = ADBTALK(device, 3);
1135 			result = adb_op_sync((Ptr)send_string, NULL,
1136 			    (Ptr)0, (short)command);
1137 			if (send_string[0] != 0) {
1138 				/* check for valid device handler */
1139 				switch (send_string[2]) {
1140 				case 0:
1141 				case 0xfd:
1142 				case 0xfe:
1143 				case 0xff:
1144 					continue;	/* invalid, skip */
1145 				}
1146 
1147 				/* new device found */
1148 				/* update data for previously moved device */
1149 				ADBDevTable[i].currentAddr = saveptr;
1150 #ifdef ADB_DEBUG
1151 				if (adb_debug & 0x80)
1152 					printf_intr("old device at index %i\n",i);
1153 #endif
1154 				/* add new device in table */
1155 #ifdef ADB_DEBUG
1156 				if (adb_debug & 0x80)
1157 					printf_intr("new device found\n");
1158 #endif
1159 				if (saveptr > ADBNumDevices) {
1160 					++ADBNumDevices;
1161 					KASSERT(ADBNumDevices < 16);
1162 				}
1163 				ADBDevTable[ADBNumDevices].devType =
1164 					(int)send_string[2];
1165 				ADBDevTable[ADBNumDevices].origAddr = device;
1166 				ADBDevTable[ADBNumDevices].currentAddr = device;
1167 				/* These will be set correctly in adbsys.c */
1168 				/* Until then, unsol. data will be ignored. */
1169 				ADBDevTable[ADBNumDevices].DataAreaAddr =
1170 				    (long)0;
1171 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
1172 				    (void *)0;
1173 				/* find next unused address */
1174 				for (x = saveptr; x > 0; x--) {
1175 					if (-1 == get_adb_info(&data, x)) {
1176 						saveptr = x;
1177 						break;
1178 					}
1179 				}
1180 				if (x == 0)
1181 					saveptr = 0;
1182 #ifdef ADB_DEBUG
1183 				if (adb_debug & 0x80)
1184 					printf_intr("new free is 0x%02x\n",
1185 					    saveptr);
1186 #endif
1187 				nonewtimes = 0;
1188 				/* tell pm driver device is here */
1189 				pm_check_adb_devices(device);
1190 			} else {
1191 #ifdef ADB_DEBUG
1192 				if (adb_debug & 0x80)
1193 					printf_intr("moving back...\n");
1194 #endif
1195 				/* move old device back */
1196 				command = ADBLISTEN(saveptr, 3);
1197 				send_string[0] = 2;
1198 				send_string[1] = (u_char)(device | 0x60);
1199 				send_string[2] = 0xfe;
1200 				adb_op_sync((Ptr)send_string, NULL,
1201 				    (Ptr)0, (short)command);
1202 				delay(1000);
1203 			}
1204 		}
1205 	}
1206 
1207 #ifdef ADB_DEBUG
1208 	if (adb_debug) {
1209 		for (i = 1; i <= ADBNumDevices; i++) {
1210 			x = get_ind_adb_info(&data, i);
1211 			if (x != -1)
1212 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
1213 				    i, x, data.devType);
1214 		}
1215 	}
1216 #endif
1217 
1218 #ifdef ADB_DEBUG
1219 	if (adb_debug) {
1220 		if (0 == ADBNumDevices)	/* tell user if no devices found */
1221 			printf_intr("adb: no devices found\n");
1222 	}
1223 #endif
1224 
1225 	adbStarting = 0;	/* not starting anymore */
1226 #ifdef ADB_DEBUG
1227 	if (adb_debug)
1228 		printf_intr("adb: ADBReInit complete\n");
1229 #endif
1230 
1231 	if (adbHardware == ADB_HW_CUDA)
1232 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
1233 		    (void *)adb_cuda_tickle, NULL);
1234 
1235 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
1236 		splx(s);
1237 }
1238 
1239 /*
1240  * adb_cmd_result
1241  *
1242  * This routine lets the caller know whether the specified adb command string
1243  * should expect a returned result, such as a TALK command.
1244  *
1245  * returns: 0 if a result should be expected
1246  *          1 if a result should NOT be expected
1247  */
1248 int
1249 adb_cmd_result(u_char *in)
1250 {
1251 	switch (adbHardware) {
1252 	case ADB_HW_CUDA:
1253 		/* was it an ADB talk command? */
1254 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
1255 			return 0;
1256 		/* was it an RTC/PRAM read date/time? */
1257 		if ((in[1] == 0x01) && (in[2] == 0x03))
1258 			return 0;
1259 		return 1;
1260 
1261 	case ADB_HW_PMU:
1262 		return 1;
1263 
1264 	case ADB_HW_UNKNOWN:
1265 	default:
1266 		return 1;
1267 	}
1268 }
1269 
1270 
1271 /*
1272  * adb_cmd_extra
1273  *
1274  * This routine lets the caller know whether the specified adb command string
1275  * may have extra data appended to the end of it, such as a LISTEN command.
1276  *
1277  * returns: 0 if extra data is allowed
1278  *          1 if extra data is NOT allowed
1279  */
1280 int
1281 adb_cmd_extra(u_char *in)
1282 {
1283 	switch (adbHardware) {
1284 	case ADB_HW_CUDA:
1285 		/*
1286 		 * TO DO: support needs to be added to recognize RTC and PRAM
1287 		 * commands
1288 		 */
1289 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
1290 			return 0;
1291 		/* add others later */
1292 		return 1;
1293 
1294 	case ADB_HW_PMU:
1295 		return 1;
1296 
1297 	case ADB_HW_UNKNOWN:
1298 	default:
1299 		return 1;
1300 	}
1301 }
1302 
1303 /*
1304  * adb_op_sync
1305  *
1306  * This routine does exactly what the adb_op routine does, except that after
1307  * the adb_op is called, it waits until the return value is present before
1308  * returning.
1309  *
1310  * NOTE: The user specified compRout is ignored, since this routine specifies
1311  * it's own to adb_op, which is why you really called this in the first place
1312  * anyway.
1313  */
1314 int
1315 adb_op_sync(Ptr buffer, adbComp *compRout, Ptr data, short command)
1316 {
1317 	int tmout;
1318 	int result;
1319 	volatile int flag = 0;
1320 
1321 	result = adb_op(buffer, adb_op_comprout,
1322 	    &flag, command);	/* send command */
1323 	if (result == 0) {		/* send ok? */
1324 		/*
1325 		 * Total time to wait is calculated as follows:
1326 		 *  - Tlt (stop to start time): 260 usec
1327 		 *  - start bit: 100 usec
1328 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
1329 		 *  - stop bit (with SRQ): 140 usec
1330 		 * Total: 6900 usec
1331 		 *
1332 		 * This is the total time allowed by the specification.  Any
1333 		 * device that doesn't conform to this will fail to operate
1334 		 * properly on some Apple systems.  In spite of this we
1335 		 * double the time to wait; some Cuda-based apparently
1336 		 * queues some commands and allows the main CPU to continue
1337 		 * processing (radical concept, eh?).  To be safe, allow
1338 		 * time for two complete ADB transactions to occur.
1339 		 */
1340 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
1341 			delay(10);
1342 		if (!flag && tmout > 0)
1343 			delay(tmout);
1344 
1345 		if (!flag)
1346 			result = -2;
1347 	}
1348 
1349 	return result;
1350 }
1351 
1352 /*
1353  * adb_op_comprout
1354  *
1355  * This function is used by the adb_op_sync routine so it knows when the
1356  * function is done.
1357  */
1358 void
1359 adb_op_comprout(caddr_t buffer, volatile int *compdata, int cmd)
1360 {
1361 	volatile int *p = compdata;
1362 
1363 	*p = 1;
1364 }
1365 
1366 void
1367 adb_setup_hw_type(void)
1368 {
1369 	switch (adbHardware) {
1370 	case ADB_HW_CUDA:
1371 		return;
1372 
1373 	case ADB_HW_PMU:
1374 		pm_setup_adb();
1375 		return;
1376 
1377 	default:
1378 		panic("unknown adb hardware");
1379 	}
1380 }
1381 
1382 int
1383 count_adbs(void)
1384 {
1385 	int i;
1386 	int found;
1387 
1388 	found = 0;
1389 
1390 	for (i = 1; i < 16; i++)
1391 		if (0 != ADBDevTable[i].devType)
1392 			found++;
1393 
1394 	return found;
1395 }
1396 
1397 int
1398 get_ind_adb_info(ADBDataBlock * info, int index)
1399 {
1400 	if ((index < 1) || (index > 15))	/* check range 1-15 */
1401 		return (-1);
1402 
1403 #ifdef ADB_DEBUG
1404 	if (adb_debug & 0x80)
1405 		printf_intr("index 0x%x devType is: 0x%x\n", index,
1406 		    ADBDevTable[index].devType);
1407 #endif
1408 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
1409 		return (-1);
1410 
1411 	info->devType = ADBDevTable[index].devType;
1412 	info->origADBAddr = ADBDevTable[index].origAddr;
1413 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
1414 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
1415 
1416 	return (ADBDevTable[index].currentAddr);
1417 }
1418 
1419 int
1420 get_adb_info(ADBDataBlock * info, int adbAddr)
1421 {
1422 	int i;
1423 
1424 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
1425 		return (-1);
1426 
1427 	for (i = 1; i < 15; i++)
1428 		if (ADBDevTable[i].currentAddr == adbAddr) {
1429 			info->devType = ADBDevTable[i].devType;
1430 			info->origADBAddr = ADBDevTable[i].origAddr;
1431 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
1432 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
1433 			return 0;	/* found */
1434 		}
1435 
1436 	return (-1);		/* not found */
1437 }
1438 
1439 int
1440 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
1441 {
1442 	int i;
1443 
1444 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
1445 		return (-1);
1446 
1447 	for (i = 1; i < 15; i++)
1448 		if (ADBDevTable[i].currentAddr == adbAddr) {
1449 			ADBDevTable[i].ServiceRtPtr =
1450 			    (void *)(info->siServiceRtPtr);
1451 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
1452 			return 0;	/* found */
1453 		}
1454 
1455 	return (-1);		/* not found */
1456 
1457 }
1458 
1459 #ifndef MRG_ADB
1460 
1461 /* caller should really use machine-independant version: getPramTime */
1462 /* this version does pseudo-adb access only */
1463 int
1464 adb_read_date_time(unsigned long *time)
1465 {
1466 	u_char output[ADB_MAX_MSG_LENGTH];
1467 	int result;
1468 	volatile int flag = 0;
1469 
1470 	switch (adbHardware) {
1471 	case ADB_HW_PMU:
1472 		pm_read_date_time(time);
1473 		return 0;
1474 
1475 	case ADB_HW_CUDA:
1476 		output[0] = 0x02;	/* 2 byte message */
1477 		output[1] = 0x01;	/* to pram/rtc device */
1478 		output[2] = 0x03;	/* read date/time */
1479 		result = send_adb_cuda((u_char *)output, (u_char *)output,
1480 		    adb_op_comprout, &flag, (int)0);
1481 		if (result != 0)	/* exit if not sent */
1482 			return -1;
1483 
1484 		while (0 == flag)	/* wait for result */
1485 			;
1486 
1487 		memcpy(time, output + 1, 4);
1488 		return 0;
1489 
1490 	case ADB_HW_UNKNOWN:
1491 	default:
1492 		return -1;
1493 	}
1494 }
1495 
1496 /* caller should really use machine-independant version: setPramTime */
1497 /* this version does pseudo-adb access only */
1498 int
1499 adb_set_date_time(unsigned long time)
1500 {
1501 	u_char output[ADB_MAX_MSG_LENGTH];
1502 	int result;
1503 	volatile int flag = 0;
1504 
1505 	switch (adbHardware) {
1506 
1507 	case ADB_HW_CUDA:
1508 		output[0] = 0x06;	/* 6 byte message */
1509 		output[1] = 0x01;	/* to pram/rtc device */
1510 		output[2] = 0x09;	/* set date/time */
1511 		output[3] = (u_char)(time >> 24);
1512 		output[4] = (u_char)(time >> 16);
1513 		output[5] = (u_char)(time >> 8);
1514 		output[6] = (u_char)(time);
1515 		result = send_adb_cuda((u_char *)output, (u_char *)0,
1516 		    adb_op_comprout, &flag, (int)0);
1517 		if (result != 0)	/* exit if not sent */
1518 			return -1;
1519 
1520 		while (0 == flag)	/* wait for send to finish */
1521 			;
1522 
1523 		return 0;
1524 
1525 	case ADB_HW_PMU:
1526 		pm_set_date_time(time);
1527 		return 0;
1528 
1529 	case ADB_HW_UNKNOWN:
1530 	default:
1531 		return -1;
1532 	}
1533 }
1534 
1535 
1536 int
1537 adb_poweroff(void)
1538 {
1539 	u_char output[ADB_MAX_MSG_LENGTH];
1540 	int result;
1541 
1542 	adb_polling = 1;
1543 
1544 	switch (adbHardware) {
1545 	case ADB_HW_PMU:
1546 		pm_adb_poweroff();
1547 
1548 		for (;;);		/* wait for power off */
1549 
1550 		return 0;
1551 
1552 	case ADB_HW_CUDA:
1553 		output[0] = 0x02;	/* 2 byte message */
1554 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
1555 		output[2] = 0x0a;	/* set date/time */
1556 		result = send_adb_cuda((u_char *)output, (u_char *)0,
1557 		    (void *)0, (void *)0, (int)0);
1558 		if (result != 0)	/* exit if not sent */
1559 			return -1;
1560 
1561 		for (;;);		/* wait for power off */
1562 
1563 		return 0;
1564 
1565 	case ADB_HW_UNKNOWN:
1566 	default:
1567 		return -1;
1568 	}
1569 }
1570 
1571 int
1572 CountADBs(void)
1573 {
1574 	return (count_adbs());
1575 }
1576 
1577 void
1578 ADBReInit(void)
1579 {
1580 	adb_reinit();
1581 }
1582 
1583 int
1584 GetIndADB(ADBDataBlock * info, int index)
1585 {
1586 	return (get_ind_adb_info(info, index));
1587 }
1588 
1589 int
1590 GetADBInfo(ADBDataBlock * info, int adbAddr)
1591 {
1592 	return (get_adb_info(info, adbAddr));
1593 }
1594 
1595 int
1596 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
1597 {
1598 	return (set_adb_info(info, adbAddr));
1599 }
1600 
1601 int
1602 ADBOp(Ptr buffer, adbComp *compRout, Ptr data, short commandNum)
1603 {
1604 	return (adb_op(buffer, compRout, data, commandNum));
1605 }
1606 
1607 #endif
1608 
1609 int
1610 setsoftadb()
1611 {
1612 	callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
1613 	return 0;
1614 }
1615 
1616 void
1617 adb_cuda_autopoll()
1618 {
1619 	volatile int flag = 0;
1620 	int result;
1621 	u_char output[16];
1622 
1623 	output[0] = 0x03;	/* 3-byte message */
1624 	output[1] = 0x01;	/* to pram/rtc device */
1625 	output[2] = 0x01;	/* cuda autopoll */
1626 	output[3] = 0x01;
1627 	result = send_adb_cuda(output, output, adb_op_comprout,
1628 	    &flag, 0);
1629 	if (result != 0)	/* exit if not sent */
1630 		return;
1631 
1632 	while (flag == 0);	/* wait for result */
1633 }
1634 
1635 void
1636 adb_restart(void)
1637 {
1638 	int result;
1639 	u_char output[16];
1640 
1641 	adb_polling = 1;
1642 
1643 	switch (adbHardware) {
1644 	case ADB_HW_CUDA:
1645 		output[0] = 0x02;	/* 2 byte message */
1646 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
1647 		output[2] = 0x11;	/* restart */
1648 		result = send_adb_cuda(output, NULL, NULL, NULL, 0);
1649 		if (result != 0)	/* exit if not sent */
1650 			return;
1651 		while (1);		/* not return */
1652 
1653 	case ADB_HW_PMU:
1654 		pm_adb_restart();
1655 		while (1);		/* not return */
1656 	}
1657 }
1658