xref: /netbsd-src/sys/arch/macppc/dev/adb_direct.c (revision fd5cb0acea84d278e04e640d37ca2398f894991f)
1 /*	$NetBSD: adb_direct.c,v 1.30 2005/02/01 03:08:16 briggs 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.30 2005/02/01 03:08:16 briggs 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_II()     via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
120 #define ADB_SET_STATE_IDLE_IISI()   via_reg_and(VIA1, vBufB, ~(vPB4 | vPB5))
121 #define ADB_SET_STATE_IDLE_CUDA()   via_reg_or(VIA1, vBufB, (vPB4 | vPB5))
122 #define ADB_SET_STATE_CMD()         via_reg_and(VIA1, vBufB, ~(vPB4 | vPB5))
123 #define ADB_SET_STATE_EVEN()        write_via_reg(VIA1, vBufB, \
124                               (read_via_reg(VIA1, vBufB) | vPB4) & ~vPB5)
125 #define ADB_SET_STATE_ODD()         write_via_reg(VIA1, vBufB, \
126                               (read_via_reg(VIA1, vBufB) | vPB5) & ~vPB4 )
127 #define ADB_SET_STATE_ACTIVE() 	    via_reg_or(VIA1, vBufB, vPB5)
128 #define ADB_SET_STATE_INACTIVE()    via_reg_and(VIA1, vBufB, ~vPB5)
129 #define ADB_SET_STATE_TIP()	    via_reg_and(VIA1, vBufB, ~vPB5)
130 #define ADB_CLR_STATE_TIP() 	    via_reg_or(VIA1, vBufB, vPB5)
131 #define ADB_SET_STATE_ACKON()	    via_reg_or(VIA1, vBufB, vPB4)
132 #define ADB_SET_STATE_ACKOFF()	    via_reg_and(VIA1, vBufB, ~vPB4)
133 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4)
134 #define ADB_SET_STATE_ACKON_CUDA()  via_reg_and(VIA1, vBufB, ~vPB4)
135 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4)
136 #define ADB_SET_SR_INPUT()	    via_reg_and(VIA1, vACR, ~vSR_OUT)
137 #define ADB_SET_SR_OUTPUT()	    via_reg_or(VIA1, vACR, vSR_OUT)
138 #define ADB_SR()		    read_via_reg(VIA1, vSR)
139 #define ADB_VIA_INTR_ENABLE()	    write_via_reg(VIA1, vIER, 0x84)
140 #define ADB_VIA_INTR_DISABLE()	    write_via_reg(VIA1, vIER, 0x04)
141 #define ADB_VIA_CLR_INTR()	    write_via_reg(VIA1, vIFR, 0x04)
142 #define ADB_INTR_IS_OFF		   (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3))
143 #define ADB_INTR_IS_ON		   (0 == (read_via_reg(VIA1, vBufB) & vPB3))
144 #define ADB_SR_INTR_IS_OFF	   (0 == (read_via_reg(VIA1, vIFR) & vSR_INT))
145 #define ADB_SR_INTR_IS_ON	   (vSR_INT == (read_via_reg(VIA1, \
146 						vIFR) & vSR_INT))
147 
148 /*
149  * This is the delay that is required (in uS) between certain
150  * ADB transactions. The actual timing delay for for each uS is
151  * calculated at boot time to account for differences in machine speed.
152  */
153 #define ADB_DELAY	150
154 
155 /*
156  * Maximum ADB message length; includes space for data, result, and
157  * device code - plus a little for safety.
158  */
159 #define ADB_MAX_MSG_LENGTH	16
160 #define ADB_MAX_HDR_LENGTH	8
161 
162 #define ADB_QUEUE		32
163 #define ADB_TICKLE_TICKS	4
164 
165 /*
166  * A structure for storing information about each ADB device.
167  */
168 struct ADBDevEntry {
169 	void	(*ServiceRtPtr) __P((void));
170 	void	*DataAreaAddr;
171 	int	devType;
172 	int	origAddr;
173 	int	currentAddr;
174 };
175 
176 /*
177  * Used to hold ADB commands that are waiting to be sent out.
178  */
179 struct adbCmdHoldEntry {
180 	u_char	outBuf[ADB_MAX_MSG_LENGTH];	/* our message */
181 	u_char	*saveBuf;	/* buffer to know where to save result */
182 	u_char	*compRout;	/* completion routine pointer */
183 	u_char	*data;		/* completion routine data pointer */
184 };
185 
186 /*
187  * Eventually used for two separate queues, the queue between
188  * the upper and lower halves, and the outgoing packet queue.
189  * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
190  */
191 struct adbCommand {
192 	u_char	header[ADB_MAX_HDR_LENGTH];	/* not used yet */
193 	u_char	data[ADB_MAX_MSG_LENGTH];	/* packet data only */
194 	u_char	*saveBuf;	/* where to save result */
195 	u_char	*compRout;	/* completion routine pointer */
196 	u_char	*compData;	/* completion routine data pointer */
197 	u_int	cmd;		/* the original command for this data */
198 	u_int	unsol;		/* 1 if packet was unsolicited */
199 	u_int	ack_only;	/* 1 for no special processing */
200 };
201 
202 /*
203  * A few variables that we need and their initial values.
204  */
205 int	adbHardware = ADB_HW_UNKNOWN;
206 int	adbActionState = ADB_ACTION_NOTREADY;
207 int	adbBusState = ADB_BUS_UNKNOWN;
208 int	adbWaiting = 0;		/* waiting for return data from the device */
209 int	adbWriteDelay = 0;	/* working on (or waiting to do) a write */
210 int	adbOutQueueHasData = 0;	/* something in the queue waiting to go out */
211 int	adbNextEnd = 0;		/* the next incoming bute is the last (II) */
212 int	adbSoftPower = 0;	/* machine supports soft power */
213 
214 int	adbWaitingCmd = 0;	/* ADB command we are waiting for */
215 u_char	*adbBuffer = (long)0;	/* pointer to user data area */
216 void	*adbCompRout = (long)0;	/* pointer to the completion routine */
217 void	*adbCompData = (long)0;	/* pointer to the completion routine data */
218 long	adbFakeInts = 0;	/* keeps track of fake ADB interrupts for
219 				 * timeouts (II) */
220 int	adbStarting = 1;	/* doing ADBReInit so do polling differently */
221 int	adbSendTalk = 0;	/* the intr routine is sending the talk, not
222 				 * the user (II) */
223 int	adbPolling = 0;		/* we are polling for service request */
224 int	adbPollCmd = 0;		/* the last poll command we sent */
225 
226 u_char	adbInputBuffer[ADB_MAX_MSG_LENGTH];	/* data input buffer */
227 u_char	adbOutputBuffer[ADB_MAX_MSG_LENGTH];	/* data output buffer */
228 struct	adbCmdHoldEntry adbOutQueue;		/* our 1 entry output queue */
229 
230 int	adbSentChars = 0;	/* how many characters we have sent */
231 int	adbLastDevice = 0;	/* last ADB dev we heard from (II ONLY) */
232 int	adbLastDevIndex = 0;	/* last ADB dev loc in dev table (II ONLY) */
233 int	adbLastCommand = 0;	/* the last ADB command we sent (II) */
234 
235 struct	ADBDevEntry ADBDevTable[16];	/* our ADB device table */
236 int	ADBNumDevices;		/* num. of ADB devices found with ADBReInit */
237 
238 struct	adbCommand adbInbound[ADB_QUEUE];	/* incoming queue */
239 int	adbInCount = 0;			/* how many packets in in queue */
240 int	adbInHead = 0;			/* head of in queue */
241 int	adbInTail = 0;			/* tail of in queue */
242 struct	adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
243 int	adbOutCount = 0;		/* how many packets in out queue */
244 int	adbOutHead = 0;			/* head of out queue */
245 int	adbOutTail = 0;			/* tail of out queue */
246 
247 int	tickle_count = 0;		/* how many tickles seen for this packet? */
248 int	tickle_serial = 0;		/* the last packet tickled */
249 int	adb_cuda_serial = 0;		/* the current packet */
250 
251 struct callout adb_cuda_tickle_ch = CALLOUT_INITIALIZER;
252 struct callout adb_soft_intr_ch = CALLOUT_INITIALIZER;
253 
254 volatile u_char *Via1Base;
255 extern int adb_polling;			/* Are we polling? */
256 
257 void	pm_setup_adb __P((void));
258 void	pm_check_adb_devices __P((int));
259 int	pm_adb_op __P((u_char *, void *, void *, int));
260 void	pm_init_adb_device __P((void));
261 
262 /*
263  * The following are private routines.
264  */
265 #ifdef ADB_DEBUG
266 void	print_single __P((u_char *));
267 #endif
268 void	adb_intr_II __P((void));
269 void	adb_intr_IIsi __P((void));
270 void	adb_soft_intr __P((void));
271 int	send_adb_II __P((u_char *, u_char *, void *, void *, int));
272 int	send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
273 int	send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
274 void	adb_intr_cuda_test __P((void));
275 void	adb_cuda_tickle __P((void));
276 void	adb_pass_up __P((struct adbCommand *));
277 void	adb_op_comprout __P((caddr_t, caddr_t, int));
278 void	adb_reinit __P((void));
279 int	count_adbs __P((void));
280 int	get_ind_adb_info __P((ADBDataBlock *, int));
281 int	get_adb_info __P((ADBDataBlock *, int));
282 int	set_adb_info __P((ADBSetInfoBlock *, int));
283 void	adb_setup_hw_type __P((void));
284 int	adb_op __P((Ptr, Ptr, Ptr, short));
285 int	adb_op_sync __P((Ptr, Ptr, Ptr, short));
286 void	adb_read_II __P((u_char *));
287 void	adb_hw_setup __P((void));
288 void	adb_hw_setup_IIsi __P((u_char *));
289 int	adb_cmd_result __P((u_char *));
290 int	adb_cmd_extra __P((u_char *));
291 int	adb_guess_next_device __P((void));
292 int	adb_prog_switch_enable __P((void));
293 int	adb_prog_switch_disable __P((void));
294 /* we should create this and it will be the public version */
295 int	send_adb __P((u_char *, void *, void *));
296 
297 int	setsoftadb __P((void));
298 
299 #ifdef ADB_DEBUG
300 /*
301  * print_single
302  * Diagnostic display routine. Displays the hex values of the
303  * specified elements of the u_char. The length of the "string"
304  * is in [0].
305  */
306 void
307 print_single(str)
308 	u_char *str;
309 {
310 	int x;
311 
312 	if (str == 0) {
313 		printf_intr("no data - null pointer\n");
314 		return;
315 	}
316 	if (*str == 0) {
317 		printf_intr("nothing returned\n");
318 		return;
319 	}
320 	if (*str > 20) {
321 		printf_intr("ADB: ACK > 20 no way!\n");
322 		*str = 20;
323 	}
324 	printf_intr("(length=0x%x):", *str);
325 	for (x = 1; x <= *str; x++)
326 		printf_intr("  0x%02x", str[x]);
327 	printf_intr("\n");
328 }
329 #endif
330 
331 void
332 adb_cuda_tickle(void)
333 {
334 	volatile int s;
335 
336 	if (adbActionState == ADB_ACTION_IN) {
337 		if (tickle_serial == adb_cuda_serial) {
338 			if (++tickle_count > 0) {
339 				s = splhigh();
340 				adbActionState = ADB_ACTION_IDLE;
341 				adbInputBuffer[0] = 0;
342 				ADB_SET_STATE_IDLE_CUDA();
343 				splx(s);
344 			}
345 		} else {
346 			tickle_serial = adb_cuda_serial;
347 			tickle_count = 0;
348 		}
349 	} else {
350 		tickle_serial = adb_cuda_serial;
351 		tickle_count = 0;
352 	}
353 
354 	callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
355 	    (void *)adb_cuda_tickle, NULL);
356 }
357 
358 /*
359  * called when when an adb interrupt happens
360  *
361  * Cuda version of adb_intr
362  * TO DO: do we want to add some calls to intr_dispatch() here to
363  * grab serial interrupts?
364  */
365 int
366 adb_intr_cuda(void *arg)
367 {
368 	volatile int i, ending;
369 	volatile unsigned int s;
370 	struct adbCommand packet;
371 	uint8_t reg;
372 
373 	s = splhigh();		/* can't be too careful - might be called */
374 				/* from a routine, NOT an interrupt */
375 
376 	reg = read_via_reg(VIA1, vIFR);		/* Read the interrupts */
377 	if ((reg & 0x80) == 0) {
378 		splx(s);
379 		return 0;			/* No interrupts to process */
380 	}
381 
382 	write_via_reg(VIA1, vIFR, reg & 0x7f);	/* Clear 'em */
383 
384 	ADB_VIA_INTR_DISABLE();	/* disable ADB interrupt on IIs. */
385 
386 switch_start:
387 	switch (adbActionState) {
388 	case ADB_ACTION_IDLE:
389 		/*
390 		 * This is an unexpected packet, so grab the first (dummy)
391 		 * byte, set up the proper vars, and tell the chip we are
392 		 * starting to receive the packet by setting the TIP bit.
393 		 */
394 		adbInputBuffer[1] = ADB_SR();
395 		adb_cuda_serial++;
396 		if (ADB_INTR_IS_OFF)	/* must have been a fake start */
397 			break;
398 
399 		ADB_SET_SR_INPUT();
400 		ADB_SET_STATE_TIP();
401 
402 		adbInputBuffer[0] = 1;
403 		adbActionState = ADB_ACTION_IN;
404 #ifdef ADB_DEBUG
405 		if (adb_debug)
406 			printf_intr("idle 0x%02x ", adbInputBuffer[1]);
407 #endif
408 		break;
409 
410 	case ADB_ACTION_IN:
411 		adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
412 		/* intr off means this is the last byte (end of frame) */
413 		if (ADB_INTR_IS_OFF)
414 			ending = 1;
415 		else
416 			ending = 0;
417 
418 		if (1 == ending) {	/* end of message? */
419 #ifdef ADB_DEBUG
420 			if (adb_debug) {
421 				printf_intr("in end 0x%02x ",
422 				    adbInputBuffer[adbInputBuffer[0]]);
423 				print_single(adbInputBuffer);
424 			}
425 #endif
426 
427 			/*
428 			 * Are we waiting AND does this packet match what we
429 			 * are waiting for AND is it coming from either the
430 			 * ADB or RTC/PRAM sub-device? This section _should_
431 			 * recognize all ADB and RTC/PRAM type commands, but
432 			 * there may be more... NOTE: commands are always at
433 			 * [4], even for RTC/PRAM commands.
434 			 */
435 			/* set up data for adb_pass_up */
436 			memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
437 
438 			if ((adbWaiting == 1) &&
439 			    (adbInputBuffer[4] == adbWaitingCmd) &&
440 			    ((adbInputBuffer[2] == 0x00) ||
441 			    (adbInputBuffer[2] == 0x01))) {
442 				packet.saveBuf = adbBuffer;
443 				packet.compRout = adbCompRout;
444 				packet.compData = adbCompData;
445 				packet.unsol = 0;
446 				packet.ack_only = 0;
447 				adb_pass_up(&packet);
448 
449 				adbWaitingCmd = 0;	/* reset "waiting" vars */
450 				adbWaiting = 0;
451 				adbBuffer = (long)0;
452 				adbCompRout = (long)0;
453 				adbCompData = (long)0;
454 			} else {
455 				packet.unsol = 1;
456 				packet.ack_only = 0;
457 				adb_pass_up(&packet);
458 			}
459 
460 
461 			/* reset vars and signal the end of this frame */
462 			adbActionState = ADB_ACTION_IDLE;
463 			adbInputBuffer[0] = 0;
464 			ADB_SET_STATE_IDLE_CUDA();
465 			/*ADB_SET_SR_INPUT();*/
466 
467 			/*
468 			 * If there is something waiting to be sent out,
469 			 * the set everything up and send the first byte.
470 			 */
471 			if (adbWriteDelay == 1) {
472 				delay(ADB_DELAY);	/* required */
473 				adbSentChars = 0;
474 				adbActionState = ADB_ACTION_OUT;
475 				/*
476 				 * If the interrupt is on, we were too slow
477 				 * and the chip has already started to send
478 				 * something to us, so back out of the write
479 				 * and start a read cycle.
480 				 */
481 				if (ADB_INTR_IS_ON) {
482 					ADB_SET_SR_INPUT();
483 					ADB_SET_STATE_IDLE_CUDA();
484 					adbSentChars = 0;
485 					adbActionState = ADB_ACTION_IDLE;
486 					adbInputBuffer[0] = 0;
487 					break;
488 				}
489 				/*
490 				 * If we got here, it's ok to start sending
491 				 * so load the first byte and tell the chip
492 				 * we want to send.
493 				 */
494 				ADB_SET_STATE_TIP();
495 				ADB_SET_SR_OUTPUT();
496 				write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);
497 			}
498 		} else {
499 			ADB_TOGGLE_STATE_ACK_CUDA();
500 #ifdef ADB_DEBUG
501 			if (adb_debug)
502 				printf_intr("in 0x%02x ",
503 				    adbInputBuffer[adbInputBuffer[0]]);
504 #endif
505 		}
506 		break;
507 
508 	case ADB_ACTION_OUT:
509 		i = ADB_SR();	/* reset SR-intr in IFR */
510 #ifdef ADB_DEBUG
511 		if (adb_debug)
512 			printf_intr("intr out 0x%02x ", i);
513 #endif
514 
515 		adbSentChars++;
516 		if (ADB_INTR_IS_ON) {	/* ADB intr low during write */
517 #ifdef ADB_DEBUG
518 			if (adb_debug)
519 				printf_intr("intr was on ");
520 #endif
521 			ADB_SET_SR_INPUT();	/* make sure SR is set to IN */
522 			ADB_SET_STATE_IDLE_CUDA();
523 			adbSentChars = 0;	/* must start all over */
524 			adbActionState = ADB_ACTION_IDLE;	/* new state */
525 			adbInputBuffer[0] = 0;
526 			adbWriteDelay = 1;	/* must retry when done with
527 						 * read */
528 			delay(ADB_DELAY);
529 			goto switch_start;	/* process next state right
530 						 * now */
531 			break;
532 		}
533 		if (adbOutputBuffer[0] == adbSentChars) {	/* check for done */
534 			if (0 == adb_cmd_result(adbOutputBuffer)) {	/* do we expect data
535 									 * back? */
536 				adbWaiting = 1;	/* signal waiting for return */
537 				adbWaitingCmd = adbOutputBuffer[2];	/* save waiting command */
538 			} else {	/* no talk, so done */
539 				/* set up stuff for adb_pass_up */
540 				memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
541 				packet.saveBuf = adbBuffer;
542 				packet.compRout = adbCompRout;
543 				packet.compData = adbCompData;
544 				packet.cmd = adbWaitingCmd;
545 				packet.unsol = 0;
546 				packet.ack_only = 1;
547 				adb_pass_up(&packet);
548 
549 				/* reset "waiting" vars, just in case */
550 				adbWaitingCmd = 0;
551 				adbBuffer = (long)0;
552 				adbCompRout = (long)0;
553 				adbCompData = (long)0;
554 			}
555 
556 			adbWriteDelay = 0;	/* done writing */
557 			adbActionState = ADB_ACTION_IDLE;	/* signal bus is idle */
558 			ADB_SET_SR_INPUT();
559 			ADB_SET_STATE_IDLE_CUDA();
560 #ifdef ADB_DEBUG
561 			if (adb_debug)
562 				printf_intr("write done ");
563 #endif
564 		} else {
565 			write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* send next byte */
566 			ADB_TOGGLE_STATE_ACK_CUDA();	/* signal byte ready to
567 							 * shift */
568 #ifdef ADB_DEBUG
569 			if (adb_debug)
570 				printf_intr("toggle ");
571 #endif
572 		}
573 		break;
574 
575 	case ADB_ACTION_NOTREADY:
576 #ifdef ADB_DEBUG
577 		if (adb_debug)
578 			printf_intr("adb: not yet initialized\n");
579 #endif
580 		break;
581 
582 	default:
583 #ifdef ADB_DEBUG
584 		if (adb_debug)
585 			printf_intr("intr: unknown ADB state\n");
586 #endif
587 		break;
588 	}
589 
590 	ADB_VIA_INTR_ENABLE();	/* enable ADB interrupt on IIs. */
591 
592 	splx(s);		/* restore */
593 
594 	return 1;
595 }				/* end adb_intr_cuda */
596 
597 
598 int
599 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
600 	command)
601 {
602 	int s, len;
603 
604 #ifdef ADB_DEBUG
605 	if (adb_debug)
606 		printf_intr("SEND\n");
607 #endif
608 
609 	if (adbActionState == ADB_ACTION_NOTREADY)
610 		return 1;
611 
612 	/* Don't interrupt while we are messing with the ADB */
613 	s = splhigh();
614 
615 	if ((adbActionState == ADB_ACTION_IDLE) &&	/* ADB available? */
616 	    (ADB_INTR_IS_OFF)) {	/* and no incoming interrupt? */
617 	} else
618 		if (adbWriteDelay == 0)	/* it's busy, but is anything waiting? */
619 			adbWriteDelay = 1;	/* if no, then we'll "queue"
620 						 * it up */
621 		else {
622 			splx(s);
623 			return 1;	/* really busy! */
624 		}
625 
626 #ifdef ADB_DEBUG
627 	if (adb_debug)
628 		printf_intr("QUEUE\n");
629 #endif
630 	if ((long)in == (long)0) {	/* need to convert? */
631 		/*
632 		 * Don't need to use adb_cmd_extra here because this section
633 		 * will be called ONLY when it is an ADB command (no RTC or
634 		 * PRAM)
635 		 */
636 		if ((command & 0x0c) == 0x08)	/* copy addl data ONLY if
637 						 * doing a listen! */
638 			len = buffer[0];	/* length of additional data */
639 		else
640 			len = 0;/* no additional data */
641 
642 		adbOutputBuffer[0] = 2 + len;	/* dev. type + command + addl.
643 						 * data */
644 		adbOutputBuffer[1] = 0x00;	/* mark as an ADB command */
645 		adbOutputBuffer[2] = (u_char)command;	/* load command */
646 
647 		/* copy additional output data, if any */
648 		memcpy(adbOutputBuffer + 3, buffer + 1, len);
649 	} else
650 		/* if data ready, just copy over */
651 		memcpy(adbOutputBuffer, in, in[0] + 2);
652 
653 	adbSentChars = 0;	/* nothing sent yet */
654 	adbBuffer = buffer;	/* save buffer to know where to save result */
655 	adbCompRout = compRout;	/* save completion routine pointer */
656 	adbCompData = data;	/* save completion routine data pointer */
657 	adbWaitingCmd = adbOutputBuffer[2];	/* save wait command */
658 
659 	if (adbWriteDelay != 1) {	/* start command now? */
660 #ifdef ADB_DEBUG
661 		if (adb_debug)
662 			printf_intr("out start NOW");
663 #endif
664 		delay(ADB_DELAY);
665 		adbActionState = ADB_ACTION_OUT;	/* set next state */
666 		ADB_SET_SR_OUTPUT();	/* set shift register for OUT */
667 		write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]);	/* load byte for output */
668 		ADB_SET_STATE_ACKOFF_CUDA();
669 		ADB_SET_STATE_TIP();	/* tell ADB that we want to send */
670 	}
671 	adbWriteDelay = 1;	/* something in the write "queue" */
672 
673 	splx(s);
674 
675 	if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */
676 		/* poll until byte done */
677 		while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
678 		    || (adbWaiting == 1))
679 			if (ADB_SR_INTR_IS_ON) {	/* wait for "interrupt" */
680 				adb_intr_cuda(NULL);	/* process it */
681 				adb_soft_intr();
682 			}
683 
684 	return 0;
685 }				/* send_adb_cuda */
686 
687 
688 void
689 adb_intr_II(void)
690 {
691 	panic("adb_intr_II");
692 }
693 
694 
695 /*
696  * send_adb version for II series machines
697  */
698 int
699 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
700 {
701 	panic("send_adb_II");
702 }
703 
704 
705 /*
706  * This routine is called from the II series interrupt routine
707  * to determine what the "next" device is that should be polled.
708  */
709 int
710 adb_guess_next_device(void)
711 {
712 	int last, i, dummy;
713 
714 	if (adbStarting) {
715 		/*
716 		 * Start polling EVERY device, since we can't be sure there is
717 		 * anything in the device table yet
718 		 */
719 		if (adbLastDevice < 1 || adbLastDevice > 15)
720 			adbLastDevice = 1;
721 		if (++adbLastDevice > 15)	/* point to next one */
722 			adbLastDevice = 1;
723 	} else {
724 		/* find the next device using the device table */
725 		if (adbLastDevice < 1 || adbLastDevice > 15)	/* let's be parinoid */
726 			adbLastDevice = 2;
727 		last = 1;	/* default index location */
728 
729 		for (i = 1; i < 16; i++)	/* find index entry */
730 			if (ADBDevTable[i].currentAddr == adbLastDevice) {	/* look for device */
731 				last = i;	/* found it */
732 				break;
733 			}
734 		dummy = last;	/* index to start at */
735 		for (;;) {	/* find next device in index */
736 			if (++dummy > 15)	/* wrap around if needed */
737 				dummy = 1;
738 			if (dummy == last) {	/* didn't find any other
739 						 * device! This can happen if
740 						 * there are no devices on the
741 						 * bus */
742 				dummy = 1;
743 				break;
744 			}
745 			/* found the next device */
746 			if (ADBDevTable[dummy].devType != 0)
747 				break;
748 		}
749 		adbLastDevice = ADBDevTable[dummy].currentAddr;
750 	}
751 	return adbLastDevice;
752 }
753 
754 
755 int
756 adb_intr(void *arg)
757 {
758 	switch (adbHardware) {
759 	case ADB_HW_II:
760 		adb_intr_II();
761 		break;
762 
763 	case ADB_HW_IISI:
764 		adb_intr_IIsi();
765 		break;
766 
767 	case ADB_HW_PMU:
768 		return pm_intr(arg);
769 		break;
770 
771 	case ADB_HW_CUDA:
772 		return adb_intr_cuda(arg);
773 		break;
774 
775 	case ADB_HW_UNKNOWN:
776 		break;
777 	}
778 	return 0;
779 }
780 
781 
782 /*
783  * called when when an adb interrupt happens
784  *
785  * IIsi version of adb_intr
786  *
787  */
788 void
789 adb_intr_IIsi(void)
790 {
791 	panic("adb_intr_IIsi");
792 }
793 
794 
795 /*****************************************************************************
796  * if the device is currently busy, and there is no data waiting to go out, then
797  * the data is "queued" in the outgoing buffer. If we are already waiting, then
798  * we return.
799  * in: if (in == 0) then the command string is built from command and buffer
800  *     if (in != 0) then in is used as the command string
801  * buffer: additional data to be sent (used only if in == 0)
802  *         this is also where return data is stored
803  * compRout: the completion routine that is called when then return value
804  *	     is received (if a return value is expected)
805  * data: a data pointer that can be used by the completion routine
806  * command: an ADB command to be sent (used only if in == 0)
807  *
808  */
809 int
810 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
811 	command)
812 {
813 	panic("send_adb_IIsi");
814 }
815 
816 
817 /*
818  * adb_pass_up is called by the interrupt-time routines.
819  * It takes the raw packet data that was received from the
820  * device and puts it into the queue that the upper half
821  * processes. It then signals for a soft ADB interrupt which
822  * will eventually call the upper half routine (adb_soft_intr).
823  *
824  * If in->unsol is 0, then this is either the notification
825  * that the packet was sent (on a LISTEN, for example), or the
826  * response from the device (on a TALK). The completion routine
827  * is called only if the user specified one.
828  *
829  * If in->unsol is 1, then this packet was unsolicited and
830  * so we look up the device in the ADB device table to determine
831  * what it's default service routine is.
832  *
833  * If in->ack_only is 1, then we really only need to call
834  * the completion routine, so don't do any other stuff.
835  *
836  * Note that in->data contains the packet header AND data,
837  * while adbInbound[]->data contains ONLY data.
838  *
839  * Note: Called only at interrupt time. Assumes this.
840  */
841 void
842 adb_pass_up(struct adbCommand *in)
843 {
844 	int start = 0, len = 0, cmd = 0;
845 	ADBDataBlock block;
846 
847 	/* temp for testing */
848 	/*u_char *buffer = 0;*/
849 	/*u_char *compdata = 0;*/
850 	/*u_char *comprout = 0;*/
851 
852 	if (adbInCount >= ADB_QUEUE) {
853 #ifdef ADB_DEBUG
854 		if (adb_debug)
855 			printf_intr("adb: ring buffer overflow\n");
856 #endif
857 		return;
858 	}
859 
860 	if (in->ack_only) {
861 		len = in->data[0];
862 		cmd = in->cmd;
863 		start = 0;
864 	} else {
865 		switch (adbHardware) {
866 		case ADB_HW_II:
867 			cmd = in->data[1];
868 			if (in->data[0] < 2)
869 				len = 0;
870 			else
871 				len = in->data[0]-1;
872 			start = 1;
873 			break;
874 
875 		case ADB_HW_IISI:
876 		case ADB_HW_CUDA:
877 			/* If it's unsolicited, accept only ADB data for now */
878 			if (in->unsol)
879 				if (0 != in->data[2])
880 					return;
881 			cmd = in->data[4];
882 			if (in->data[0] < 5)
883 				len = 0;
884 			else
885 				len = in->data[0]-4;
886 			start = 4;
887 			break;
888 
889 		case ADB_HW_PMU:
890 			cmd = in->data[1];
891 			if (in->data[0] < 2)
892 				len = 0;
893 			else
894 				len = in->data[0]-1;
895 			start = 1;
896 			break;
897 
898 		case ADB_HW_UNKNOWN:
899 			return;
900 		}
901 
902 		/* Make sure there is a valid device entry for this device */
903 		if (in->unsol) {
904 			/* ignore unsolicited data during adbreinit */
905 			if (adbStarting)
906 				return;
907 			/* get device's comp. routine and data area */
908 			if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd)))
909 				return;
910 		}
911 	}
912 
913 	/*
914  	 * If this is an unsolicited packet, we need to fill in
915  	 * some info so adb_soft_intr can process this packet
916  	 * properly. If it's not unsolicited, then use what
917  	 * the caller sent us.
918  	 */
919 	if (in->unsol) {
920 		adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
921 		adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
922 		adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
923 	} else {
924 		adbInbound[adbInTail].compRout = (void *)in->compRout;
925 		adbInbound[adbInTail].compData = (void *)in->compData;
926 		adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
927 	}
928 
929 #ifdef ADB_DEBUG
930 	if (adb_debug && in->data[1] == 2)
931 		printf_intr("adb: caught error\n");
932 #endif
933 
934 	/* copy the packet data over */
935 	/*
936 	 * TO DO: If the *_intr routines fed their incoming data
937 	 * directly into an adbCommand struct, which is passed to
938 	 * this routine, then we could eliminate this copy.
939 	 */
940 	memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len);
941 	adbInbound[adbInTail].data[0] = len;
942 	adbInbound[adbInTail].cmd = cmd;
943 
944 	adbInCount++;
945 	if (++adbInTail >= ADB_QUEUE)
946 		adbInTail = 0;
947 
948 	/*
949 	 * If the debugger is running, call upper half manually.
950 	 * Otherwise, trigger a soft interrupt to handle the rest later.
951 	 */
952 	if (adb_polling)
953 		adb_soft_intr();
954 	else
955 		setsoftadb();
956 
957 	return;
958 }
959 
960 
961 /*
962  * Called to process the packets after they have been
963  * placed in the incoming queue.
964  *
965  */
966 void
967 adb_soft_intr(void)
968 {
969 	int s;
970 	int cmd = 0;
971 	u_char *buffer = 0;
972 	u_char *comprout = 0;
973 	u_char *compdata = 0;
974 
975 #if 0
976 	s = splhigh();
977 	printf_intr("sr: %x\n", (s & 0x0700));
978 	splx(s);
979 #endif
980 
981 /*delay(2*ADB_DELAY);*/
982 
983 	while (adbInCount) {
984 #ifdef ADB_DEBUG
985 		if (adb_debug & 0x80)
986 			printf_intr("%x %x %x ",
987 			    adbInCount, adbInHead, adbInTail);
988 #endif
989 		/* get the data we need from the queue */
990 		buffer = adbInbound[adbInHead].saveBuf;
991 		comprout = adbInbound[adbInHead].compRout;
992 		compdata = adbInbound[adbInHead].compData;
993 		cmd = adbInbound[adbInHead].cmd;
994 
995 		/* copy over data to data area if it's valid */
996 		/*
997 		 * Note that for unsol packets we don't want to copy the
998 		 * data anywhere, so buffer was already set to 0.
999 		 * For ack_only buffer was set to 0, so don't copy.
1000 		 */
1001 		if (buffer)
1002 			memcpy(buffer, adbInbound[adbInHead].data,
1003 			    adbInbound[adbInHead].data[0] + 1);
1004 
1005 #ifdef ADB_DEBUG
1006 			if (adb_debug & 0x80) {
1007 				printf_intr("%p %p %p %x ",
1008 				    buffer, comprout, compdata, (short)cmd);
1009 				printf_intr("buf: ");
1010 				print_single(adbInbound[adbInHead].data);
1011 			}
1012 #endif
1013 		/* Remove the packet from the queue before calling
1014 		 * the completion routine, so that the completion
1015 		 * routine can reentrantly process the queue.  For
1016 		 * example, this happens when polling is turned on
1017 		 * by entering the debuger by keystroke.
1018 		 */
1019 		s = splhigh();
1020 		adbInCount--;
1021 		if (++adbInHead >= ADB_QUEUE)
1022 			adbInHead = 0;
1023 		splx(s);
1024 
1025 		/* call default completion routine if it's valid */
1026 		if (comprout) {
1027 			void (*f)(caddr_t, caddr_t, int) =
1028 			    (void (*)(caddr_t, caddr_t, int))comprout;
1029 
1030 			(*f)(buffer, compdata, cmd);
1031 		}
1032 	}
1033 	return;
1034 }
1035 
1036 
1037 /*
1038  * This is my version of the ADBOp routine. It mainly just calls the
1039  * hardware-specific routine.
1040  *
1041  *   data 	: pointer to data area to be used by compRout
1042  *   compRout	: completion routine
1043  *   buffer	: for LISTEN: points to data to send - MAX 8 data bytes,
1044  *		  byte 0 = # of bytes
1045  *		: for TALK: points to place to save return data
1046  *   command	: the adb command to send
1047  *   result	: 0 = success
1048  *		: -1 = could not complete
1049  */
1050 int
1051 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1052 {
1053 	int result;
1054 
1055 	switch (adbHardware) {
1056 	case ADB_HW_II:
1057 		result = send_adb_II((u_char *)0, (u_char *)buffer,
1058 		    (void *)compRout, (void *)data, (int)command);
1059 		if (result == 0)
1060 			return 0;
1061 		else
1062 			return -1;
1063 		break;
1064 
1065 	case ADB_HW_IISI:
1066 		result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1067 		    (void *)compRout, (void *)data, (int)command);
1068 		/*
1069 		 * I wish I knew why this delay is needed. It usually needs to
1070 		 * be here when several commands are sent in close succession,
1071 		 * especially early in device probes when doing collision
1072 		 * detection. It must be some race condition. Sigh. - jpw
1073 		 */
1074 		delay(100);
1075 		if (result == 0)
1076 			return 0;
1077 		else
1078 			return -1;
1079 		break;
1080 
1081 	case ADB_HW_PMU:
1082 		result = pm_adb_op((u_char *)buffer, (void *)compRout,
1083 		    (void *)data, (int)command);
1084 
1085 		if (result == 0)
1086 			return 0;
1087 		else
1088 			return -1;
1089 		break;
1090 
1091 	case ADB_HW_CUDA:
1092 		result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1093 		    (void *)compRout, (void *)data, (int)command);
1094 		if (result == 0)
1095 			return 0;
1096 		else
1097 			return -1;
1098 		break;
1099 
1100 	case ADB_HW_UNKNOWN:
1101 	default:
1102 		return -1;
1103 	}
1104 }
1105 
1106 
1107 /*
1108  * adb_hw_setup
1109  * This routine sets up the possible machine specific hardware
1110  * config (mainly VIA settings) for the various models.
1111  */
1112 void
1113 adb_hw_setup(void)
1114 {
1115 	volatile int i;
1116 	u_char send_string[ADB_MAX_MSG_LENGTH];
1117 
1118 	switch (adbHardware) {
1119 	case ADB_HW_II:
1120 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
1121 						 * outputs */
1122 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
1123 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
1124 							 * to IN (II, IIsi) */
1125 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1126 							 * hardware (II, IIsi) */
1127 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1128 						 * code only */
1129 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1130 						 * are on (II, IIsi) */
1131 		ADB_SET_STATE_IDLE_II();	/* set ADB bus state to idle */
1132 
1133 		ADB_VIA_CLR_INTR();	/* clear interrupt */
1134 		break;
1135 
1136 	case ADB_HW_IISI:
1137 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
1138 						 * outputs */
1139 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
1140 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
1141 							 * to IN (II, IIsi) */
1142 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1143 							 * hardware (II, IIsi) */
1144 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1145 						 * code only */
1146 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1147 						 * are on (II, IIsi) */
1148 		ADB_SET_STATE_IDLE_IISI();	/* set ADB bus state to idle */
1149 
1150 		/* get those pesky clock ticks we missed while booting */
1151 		for (i = 0; i < 30; i++) {
1152 			delay(ADB_DELAY);
1153 			adb_hw_setup_IIsi(send_string);
1154 #ifdef ADB_DEBUG
1155 			if (adb_debug) {
1156 				printf_intr("adb: cleanup: ");
1157 				print_single(send_string);
1158 			}
1159 #endif
1160 			delay(ADB_DELAY);
1161 			if (ADB_INTR_IS_OFF)
1162 				break;
1163 		}
1164 		break;
1165 
1166 	case ADB_HW_PMU:
1167 		/*
1168 		 * XXX - really PM_VIA_CLR_INTR - should we put it in
1169 		 * pm_direct.h?
1170 		 */
1171 		write_via_reg(VIA1, vIFR, 0x90);	/* clear interrupt */
1172 		break;
1173 
1174 	case ADB_HW_CUDA:
1175 		via_reg_or(VIA1, vDirB, 0x30);	/* register B bits 4 and 5:
1176 						 * outputs */
1177 		via_reg_and(VIA1, vDirB, 0xf7);	/* register B bit 3: input */
1178 		via_reg_and(VIA1, vACR, ~vSR_OUT);	/* make sure SR is set
1179 							 * to IN */
1180 		write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10);
1181 		adbActionState = ADB_ACTION_IDLE;	/* used by all types of
1182 							 * hardware */
1183 		adbBusState = ADB_BUS_IDLE;	/* this var. used in II-series
1184 						 * code only */
1185 		write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts
1186 						 * are on */
1187 		ADB_SET_STATE_IDLE_CUDA();	/* set ADB bus state to idle */
1188 
1189 		/* sort of a device reset */
1190 		i = ADB_SR();	/* clear interrupt */
1191 		ADB_VIA_INTR_DISABLE();	/* no interrupts while clearing */
1192 		ADB_SET_STATE_IDLE_CUDA();	/* reset state to idle */
1193 		delay(ADB_DELAY);
1194 		ADB_SET_STATE_TIP();	/* signal start of frame */
1195 		delay(ADB_DELAY);
1196 		ADB_TOGGLE_STATE_ACK_CUDA();
1197 		delay(ADB_DELAY);
1198 		ADB_CLR_STATE_TIP();
1199 		delay(ADB_DELAY);
1200 		ADB_SET_STATE_IDLE_CUDA();	/* back to idle state */
1201 		i = ADB_SR();	/* clear interrupt */
1202 		ADB_VIA_INTR_ENABLE();	/* ints ok now */
1203 		break;
1204 
1205 	case ADB_HW_UNKNOWN:
1206 	default:
1207 		write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO
1208 						 * DO: turn PB ints off? */
1209 		return;
1210 		break;
1211 	}
1212 }
1213 
1214 
1215 /*
1216  * adb_hw_setup_IIsi
1217  * This is sort of a "read" routine that forces the adb hardware through a read cycle
1218  * if there is something waiting. This helps "clean up" any commands that may have gotten
1219  * stuck or stopped during the boot process.
1220  *
1221  */
1222 void
1223 adb_hw_setup_IIsi(u_char * buffer)
1224 {
1225 	panic("adb_hw_setup_IIsi");
1226 }
1227 
1228 
1229 /*
1230  * adb_reinit sets up the adb stuff
1231  *
1232  */
1233 void
1234 adb_reinit(void)
1235 {
1236 	u_char send_string[ADB_MAX_MSG_LENGTH];
1237 	ADBDataBlock data;	/* temp. holder for getting device info */
1238 	volatile int i, x;
1239 	int s = 0;		/* XXX: gcc */
1240 	int command;
1241 	int result;
1242 	int saveptr;		/* point to next free relocation address */
1243 	int device;
1244 	int nonewtimes;		/* times thru loop w/o any new devices */
1245 
1246 	/* Make sure we are not interrupted while building the table. */
1247 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
1248 		s = splhigh();
1249 
1250 	ADBNumDevices = 0;	/* no devices yet */
1251 
1252 	/* Let intr routines know we are running reinit */
1253 	adbStarting = 1;
1254 
1255 	/*
1256 	 * Initialize the ADB table.  For now, we'll always use the same table
1257 	 * that is defined at the beginning of this file - no mallocs.
1258 	 */
1259 	for (i = 0; i < 16; i++)
1260 		ADBDevTable[i].devType = 0;
1261 
1262 	adb_setup_hw_type();	/* setup hardware type */
1263 
1264 	adb_hw_setup();		/* init the VIA bits and hard reset ADB */
1265 
1266 	delay(1000);
1267 
1268 	/* send an ADB reset first */
1269 	result = adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
1270 	delay(200000);
1271 
1272 #ifdef ADB_DEBUG
1273 	if (result && adb_debug) {
1274 		printf_intr("adb_reinit: failed to reset, result = %d\n",result);
1275 	}
1276 #endif
1277 
1278 	/*
1279 	 * Probe for ADB devices. Probe devices 1-15 quickly to determine
1280 	 * which device addresses are in use and which are free. For each
1281 	 * address that is in use, move the device at that address to a higher
1282 	 * free address. Continue doing this at that address until no device
1283 	 * responds at that address. Then move the last device that was moved
1284 	 * back to the original address. Do this for the remaining addresses
1285 	 * that we determined were in use.
1286 	 *
1287 	 * When finished, do this entire process over again with the updated
1288 	 * list of in use addresses. Do this until no new devices have been
1289 	 * found in 20 passes though the in use address list. (This probably
1290 	 * seems long and complicated, but it's the best way to detect multiple
1291 	 * devices at the same address - sometimes it takes a couple of tries
1292 	 * before the collision is detected.)
1293 	 */
1294 
1295 	/* initial scan through the devices */
1296 	for (i = 1; i < 16; i++) {
1297 		send_string[0] = 0;
1298 		command = ADBTALK(i, 3);
1299 		result = adb_op_sync((Ptr)send_string, (Ptr)0,
1300 		    (Ptr)0, (short)command);
1301 
1302 #ifdef ADB_DEBUG
1303 		if (result && adb_debug) {
1304 			printf_intr("adb_reinit: scan of device %d, result = %d, str = 0x%x\n",
1305 					i,result,send_string[0]);
1306 		}
1307 #endif
1308 
1309 		if (send_string[0] != 0) {
1310 			/* check for valid device handler */
1311 			switch (send_string[2]) {
1312 			case 0:
1313 			case 0xfd:
1314 			case 0xfe:
1315 			case 0xff:
1316 				continue;	/* invalid, skip */
1317 			}
1318 
1319 			/* found a device */
1320 			++ADBNumDevices;
1321 			KASSERT(ADBNumDevices < 16);
1322 			ADBDevTable[ADBNumDevices].devType =
1323 				(int)send_string[2];
1324 			ADBDevTable[ADBNumDevices].origAddr = i;
1325 			ADBDevTable[ADBNumDevices].currentAddr = i;
1326 			ADBDevTable[ADBNumDevices].DataAreaAddr =
1327 			    (long)0;
1328 			ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
1329 			pm_check_adb_devices(i);	/* tell pm driver device
1330 							 * is here */
1331 		}
1332 	}
1333 
1334 	/* find highest unused address */
1335 	for (saveptr = 15; saveptr > 0; saveptr--)
1336 		if (-1 == get_adb_info(&data, saveptr))
1337 			break;
1338 
1339 #ifdef ADB_DEBUG
1340 	if (adb_debug & 0x80) {
1341 		printf_intr("first free is: 0x%02x\n", saveptr);
1342 		printf_intr("devices: %i\n", ADBNumDevices);
1343 	}
1344 #endif
1345 
1346 	nonewtimes = 0;		/* no loops w/o new devices */
1347 	while (saveptr > 0 && nonewtimes++ < 11) {
1348 		for (i = 1; i <= ADBNumDevices; i++) {
1349 			device = ADBDevTable[i].currentAddr;
1350 #ifdef ADB_DEBUG
1351 			if (adb_debug & 0x80)
1352 				printf_intr("moving device 0x%02x to 0x%02x "
1353 				    "(index 0x%02x)  ", device, saveptr, i);
1354 #endif
1355 
1356 			/* send TALK R3 to address */
1357 			command = ADBTALK(device, 3);
1358 			adb_op_sync((Ptr)send_string, (Ptr)0,
1359 			    (Ptr)0, (short)command);
1360 
1361 			/* move device to higher address */
1362 			command = ADBLISTEN(device, 3);
1363 			send_string[0] = 2;
1364 			send_string[1] = (u_char)(saveptr | 0x60);
1365 			send_string[2] = 0xfe;
1366 			adb_op_sync((Ptr)send_string, (Ptr)0,
1367 			    (Ptr)0, (short)command);
1368 			delay(500);
1369 
1370 			/* send TALK R3 - anything at new address? */
1371 			command = ADBTALK(saveptr, 3);
1372 			adb_op_sync((Ptr)send_string, (Ptr)0,
1373 			    (Ptr)0, (short)command);
1374 			delay(500);
1375 
1376 			if (send_string[0] == 0) {
1377 #ifdef ADB_DEBUG
1378 				if (adb_debug & 0x80)
1379 					printf_intr("failed, continuing\n");
1380 #endif
1381 				continue;
1382 			}
1383 
1384 			/* send TALK R3 - anything at old address? */
1385 			command = ADBTALK(device, 3);
1386 			result = adb_op_sync((Ptr)send_string, (Ptr)0,
1387 			    (Ptr)0, (short)command);
1388 			if (send_string[0] != 0) {
1389 				/* check for valid device handler */
1390 				switch (send_string[2]) {
1391 				case 0:
1392 				case 0xfd:
1393 				case 0xfe:
1394 				case 0xff:
1395 					continue;	/* invalid, skip */
1396 				}
1397 
1398 				/* new device found */
1399 				/* update data for previously moved device */
1400 				ADBDevTable[i].currentAddr = saveptr;
1401 #ifdef ADB_DEBUG
1402 				if (adb_debug & 0x80)
1403 					printf_intr("old device at index %i\n",i);
1404 #endif
1405 				/* add new device in table */
1406 #ifdef ADB_DEBUG
1407 				if (adb_debug & 0x80)
1408 					printf_intr("new device found\n");
1409 #endif
1410 				if (saveptr > ADBNumDevices) {
1411 					++ADBNumDevices;
1412 					KASSERT(ADBNumDevices < 16);
1413 				}
1414 				ADBDevTable[ADBNumDevices].devType =
1415 					(int)send_string[2];
1416 				ADBDevTable[ADBNumDevices].origAddr = device;
1417 				ADBDevTable[ADBNumDevices].currentAddr = device;
1418 				/* These will be set correctly in adbsys.c */
1419 				/* Until then, unsol. data will be ignored. */
1420 				ADBDevTable[ADBNumDevices].DataAreaAddr =
1421 				    (long)0;
1422 				ADBDevTable[ADBNumDevices].ServiceRtPtr =
1423 				    (void *)0;
1424 				/* find next unused address */
1425 				for (x = saveptr; x > 0; x--) {
1426 					if (-1 == get_adb_info(&data, x)) {
1427 						saveptr = x;
1428 						break;
1429 					}
1430 				}
1431 				if (x == 0)
1432 					saveptr = 0;
1433 #ifdef ADB_DEBUG
1434 				if (adb_debug & 0x80)
1435 					printf_intr("new free is 0x%02x\n",
1436 					    saveptr);
1437 #endif
1438 				nonewtimes = 0;
1439 				/* tell pm driver device is here */
1440 				pm_check_adb_devices(device);
1441 			} else {
1442 #ifdef ADB_DEBUG
1443 				if (adb_debug & 0x80)
1444 					printf_intr("moving back...\n");
1445 #endif
1446 				/* move old device back */
1447 				command = ADBLISTEN(saveptr, 3);
1448 				send_string[0] = 2;
1449 				send_string[1] = (u_char)(device | 0x60);
1450 				send_string[2] = 0xfe;
1451 				adb_op_sync((Ptr)send_string, (Ptr)0,
1452 				    (Ptr)0, (short)command);
1453 				delay(1000);
1454 			}
1455 		}
1456 	}
1457 
1458 #ifdef ADB_DEBUG
1459 	if (adb_debug) {
1460 		for (i = 1; i <= ADBNumDevices; i++) {
1461 			x = get_ind_adb_info(&data, i);
1462 			if (x != -1)
1463 				printf_intr("index 0x%x, addr 0x%x, type 0x%x\n",
1464 				    i, x, data.devType);
1465 		}
1466 	}
1467 #endif
1468 
1469 #ifndef MRG_ADB
1470 	/* enable the programmer's switch, if we have one */
1471 	adb_prog_switch_enable();
1472 #endif
1473 
1474 #ifdef ADB_DEBUG
1475 	if (adb_debug) {
1476 		if (0 == ADBNumDevices)	/* tell user if no devices found */
1477 			printf_intr("adb: no devices found\n");
1478 	}
1479 #endif
1480 
1481 	adbStarting = 0;	/* not starting anymore */
1482 #ifdef ADB_DEBUG
1483 	if (adb_debug)
1484 		printf_intr("adb: ADBReInit complete\n");
1485 #endif
1486 
1487 	if (adbHardware == ADB_HW_CUDA)
1488 		callout_reset(&adb_cuda_tickle_ch, ADB_TICKLE_TICKS,
1489 		    (void *)adb_cuda_tickle, NULL);
1490 
1491 	if (adbHardware != ADB_HW_PMU)	/* ints must be on for PMU? */
1492 		splx(s);
1493 }
1494 
1495 /*
1496  * adb_cmd_result
1497  *
1498  * This routine lets the caller know whether the specified adb command string
1499  * should expect a returned result, such as a TALK command.
1500  *
1501  * returns: 0 if a result should be expected
1502  *          1 if a result should NOT be expected
1503  */
1504 int
1505 adb_cmd_result(u_char *in)
1506 {
1507 	switch (adbHardware) {
1508 	case ADB_HW_II:
1509 		/* was it an ADB talk command? */
1510 		if ((in[1] & 0x0c) == 0x0c)
1511 			return 0;
1512 		return 1;
1513 
1514 	case ADB_HW_IISI:
1515 	case ADB_HW_CUDA:
1516 		/* was it an ADB talk command? */
1517 		if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
1518 			return 0;
1519 		/* was it an RTC/PRAM read date/time? */
1520 		if ((in[1] == 0x01) && (in[2] == 0x03))
1521 			return 0;
1522 		return 1;
1523 
1524 	case ADB_HW_PMU:
1525 		return 1;
1526 
1527 	case ADB_HW_UNKNOWN:
1528 	default:
1529 		return 1;
1530 	}
1531 }
1532 
1533 
1534 /*
1535  * adb_cmd_extra
1536  *
1537  * This routine lets the caller know whether the specified adb command string
1538  * may have extra data appended to the end of it, such as a LISTEN command.
1539  *
1540  * returns: 0 if extra data is allowed
1541  *          1 if extra data is NOT allowed
1542  */
1543 int
1544 adb_cmd_extra(u_char *in)
1545 {
1546 	switch (adbHardware) {
1547 		case ADB_HW_II:
1548 		if ((in[1] & 0x0c) == 0x08)	/* was it a listen command? */
1549 			return 0;
1550 		return 1;
1551 
1552 	case ADB_HW_IISI:
1553 	case ADB_HW_CUDA:
1554 		/*
1555 		 * TO DO: support needs to be added to recognize RTC and PRAM
1556 		 * commands
1557 		 */
1558 		if ((in[2] & 0x0c) == 0x08)	/* was it a listen command? */
1559 			return 0;
1560 		/* add others later */
1561 		return 1;
1562 
1563 	case ADB_HW_PMU:
1564 		return 1;
1565 
1566 	case ADB_HW_UNKNOWN:
1567 	default:
1568 		return 1;
1569 	}
1570 }
1571 
1572 /*
1573  * adb_op_sync
1574  *
1575  * This routine does exactly what the adb_op routine does, except that after
1576  * the adb_op is called, it waits until the return value is present before
1577  * returning.
1578  *
1579  * NOTE: The user specified compRout is ignored, since this routine specifies
1580  * it's own to adb_op, which is why you really called this in the first place
1581  * anyway.
1582  */
1583 int
1584 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
1585 {
1586 	int tmout;
1587 	int result;
1588 	volatile int flag = 0;
1589 
1590 	result = adb_op(buffer, (void *)adb_op_comprout,
1591 	    (void *)&flag, command);	/* send command */
1592 	if (result == 0) {		/* send ok? */
1593 		/*
1594 		 * Total time to wait is calculated as follows:
1595 		 *  - Tlt (stop to start time): 260 usec
1596 		 *  - start bit: 100 usec
1597 		 *  - up to 8 data bytes: 64 * 100 usec = 6400 usec
1598 		 *  - stop bit (with SRQ): 140 usec
1599 		 * Total: 6900 usec
1600 		 *
1601 		 * This is the total time allowed by the specification.  Any
1602 		 * device that doesn't conform to this will fail to operate
1603 		 * properly on some Apple systems.  In spite of this we
1604 		 * double the time to wait; some Cuda-based apparently
1605 		 * queues some commands and allows the main CPU to continue
1606 		 * processing (radical concept, eh?).  To be safe, allow
1607 		 * time for two complete ADB transactions to occur.
1608 		 */
1609 		for (tmout = 13800; !flag && tmout >= 10; tmout -= 10)
1610 			delay(10);
1611 		if (!flag && tmout > 0)
1612 			delay(tmout);
1613 
1614 		if (!flag)
1615 			result = -2;
1616 	}
1617 
1618 	return result;
1619 }
1620 
1621 /*
1622  * adb_op_comprout
1623  *
1624  * This function is used by the adb_op_sync routine so it knows when the
1625  * function is done.
1626  */
1627 void
1628 adb_op_comprout(buffer, compdata, cmd)
1629 	caddr_t buffer, compdata;
1630 	int cmd;
1631 {
1632 	short *p = (short *)compdata;
1633 
1634 	*p = 1;
1635 }
1636 
1637 void
1638 adb_setup_hw_type(void)
1639 {
1640 	switch (adbHardware) {
1641 	case ADB_HW_CUDA:
1642 		adbSoftPower = 1;
1643 		return;
1644 
1645 	case ADB_HW_PMU:
1646 		adbSoftPower = 1;
1647 		pm_setup_adb();
1648 		return;
1649 
1650 	default:
1651 		panic("unknown adb hardware");
1652 	}
1653 #if 0
1654 	response = 0; /*mac68k_machine.machineid;*/
1655 
1656 	/*
1657 	 * Determine what type of ADB hardware we are running on.
1658 	 */
1659 	switch (response) {
1660 	case MACH_MACC610:		/* Centris 610 */
1661 	case MACH_MACC650:		/* Centris 650 */
1662 	case MACH_MACII:		/* II */
1663 	case MACH_MACIICI:		/* IIci */
1664 	case MACH_MACIICX:		/* IIcx */
1665 	case MACH_MACIIX:		/* IIx */
1666 	case MACH_MACQ610:		/* Quadra 610 */
1667 	case MACH_MACQ650:		/* Quadra 650 */
1668 	case MACH_MACQ700:		/* Quadra 700 */
1669 	case MACH_MACQ800:		/* Quadra 800 */
1670 	case MACH_MACSE30:		/* SE/30 */
1671 		adbHardware = ADB_HW_II;
1672 #ifdef ADB_DEBUG
1673 		if (adb_debug)
1674 			printf_intr("adb: using II series hardware support\n");
1675 #endif
1676 		break;
1677 
1678 	case MACH_MACCLASSICII:		/* Classic II */
1679 	case MACH_MACLCII:		/* LC II, Performa 400/405/430 */
1680 	case MACH_MACLCIII:		/* LC III, Performa 450 */
1681 	case MACH_MACIISI:		/* IIsi */
1682 	case MACH_MACIIVI:		/* IIvi */
1683 	case MACH_MACIIVX:		/* IIvx */
1684 	case MACH_MACP460:		/* Performa 460/465/467 */
1685 	case MACH_MACP600:		/* Performa 600 */
1686 		adbHardware = ADB_HW_IISI;
1687 #ifdef ADB_DEBUG
1688 		if (adb_debug)
1689 			printf_intr("adb: using IIsi series hardware support\n");
1690 #endif
1691 		break;
1692 
1693 	case MACH_MACPB140:		/* PowerBook 140 */
1694 	case MACH_MACPB145:		/* PowerBook 145 */
1695 	case MACH_MACPB150:		/* PowerBook 150 */
1696 	case MACH_MACPB160:		/* PowerBook 160 */
1697 	case MACH_MACPB165:		/* PowerBook 165 */
1698 	case MACH_MACPB165C:		/* PowerBook 165c */
1699 	case MACH_MACPB170:		/* PowerBook 170 */
1700 	case MACH_MACPB180:		/* PowerBook 180 */
1701 	case MACH_MACPB180C:		/* PowerBook 180c */
1702 		adbHardware = ADB_HW_PMU;
1703 		pm_setup_adb();
1704 #ifdef ADB_DEBUG
1705 		if (adb_debug)
1706 			printf_intr("adb: using PowerBook 100-series hardware support\n");
1707 #endif
1708 		break;
1709 
1710 	case MACH_MACPB210:		/* PowerBook Duo 210 */
1711 	case MACH_MACPB230:		/* PowerBook Duo 230 */
1712 	case MACH_MACPB250:		/* PowerBook Duo 250 */
1713 	case MACH_MACPB270:		/* PowerBook Duo 270 */
1714 	case MACH_MACPB280:		/* PowerBook Duo 280 */
1715 	case MACH_MACPB280C:		/* PowerBook Duo 280c */
1716 	case MACH_MACPB500:		/* PowerBook 500 series */
1717 		adbHardware = ADB_HW_PMU;
1718 		pm_setup_adb();
1719 #ifdef ADB_DEBUG
1720 		if (adb_debug)
1721 			printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
1722 #endif
1723 		break;
1724 
1725 	case MACH_MACC660AV:		/* Centris 660AV */
1726 	case MACH_MACCCLASSIC:		/* Color Classic */
1727 	case MACH_MACCCLASSICII:	/* Color Classic II */
1728 	case MACH_MACLC475:		/* LC 475, Performa 475/476 */
1729 	case MACH_MACLC475_33:		/* Clock-chipped 47x */
1730 	case MACH_MACLC520:		/* LC 520 */
1731 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
1732 	case MACH_MACP550:		/* LC 550, Performa 550 */
1733 	case MACH_MACP580:		/* Performa 580/588 */
1734 	case MACH_MACQ605:		/* Quadra 605 */
1735 	case MACH_MACQ605_33:		/* Clock-chipped Quadra 605 */
1736 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
1737 	case MACH_MACQ840AV:		/* Quadra 840AV */
1738 		adbHardware = ADB_HW_CUDA;
1739 #ifdef ADB_DEBUG
1740 		if (adb_debug)
1741 			printf_intr("adb: using Cuda series hardware support\n");
1742 #endif
1743 		break;
1744 	default:
1745 		adbHardware = ADB_HW_UNKNOWN;
1746 #ifdef ADB_DEBUG
1747 		if (adb_debug) {
1748 			printf_intr("adb: hardware type unknown for this machine\n");
1749 			printf_intr("adb: ADB support is disabled\n");
1750 		}
1751 #endif
1752 		break;
1753 	}
1754 
1755 	/*
1756 	 * Determine whether this machine has ADB based soft power.
1757 	 */
1758 	switch (response) {
1759 	case MACH_MACCCLASSIC:		/* Color Classic */
1760 	case MACH_MACCCLASSICII:	/* Color Classic II */
1761 	case MACH_MACIISI:		/* IIsi */
1762 	case MACH_MACIIVI:		/* IIvi */
1763 	case MACH_MACIIVX:		/* IIvx */
1764 	case MACH_MACLC520:		/* LC 520 */
1765 	case MACH_MACLC575:		/* LC 575, Performa 575/577/578 */
1766 	case MACH_MACP550:		/* LC 550, Performa 550 */
1767 	case MACH_MACP600:		/* Performa 600 */
1768 	case MACH_MACQ630:		/* LC 630, Performa 630, Quadra 630 */
1769 	case MACH_MACQ840AV:		/* Quadra 840AV */
1770 		adbSoftPower = 1;
1771 		break;
1772 	}
1773 #endif
1774 }
1775 
1776 int
1777 count_adbs(void)
1778 {
1779 	int i;
1780 	int found;
1781 
1782 	found = 0;
1783 
1784 	for (i = 1; i < 16; i++)
1785 		if (0 != ADBDevTable[i].devType)
1786 			found++;
1787 
1788 	return found;
1789 }
1790 
1791 int
1792 get_ind_adb_info(ADBDataBlock * info, int index)
1793 {
1794 	if ((index < 1) || (index > 15))	/* check range 1-15 */
1795 		return (-1);
1796 
1797 #ifdef ADB_DEBUG
1798 	if (adb_debug & 0x80)
1799 		printf_intr("index 0x%x devType is: 0x%x\n", index,
1800 		    ADBDevTable[index].devType);
1801 #endif
1802 	if (0 == ADBDevTable[index].devType)	/* make sure it's a valid entry */
1803 		return (-1);
1804 
1805 	info->devType = ADBDevTable[index].devType;
1806 	info->origADBAddr = ADBDevTable[index].origAddr;
1807 	info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
1808 	info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
1809 
1810 	return (ADBDevTable[index].currentAddr);
1811 }
1812 
1813 int
1814 get_adb_info(ADBDataBlock * info, int adbAddr)
1815 {
1816 	int i;
1817 
1818 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
1819 		return (-1);
1820 
1821 	for (i = 1; i < 15; i++)
1822 		if (ADBDevTable[i].currentAddr == adbAddr) {
1823 			info->devType = ADBDevTable[i].devType;
1824 			info->origADBAddr = ADBDevTable[i].origAddr;
1825 			info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
1826 			info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
1827 			return 0;	/* found */
1828 		}
1829 
1830 	return (-1);		/* not found */
1831 }
1832 
1833 int
1834 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
1835 {
1836 	int i;
1837 
1838 	if ((adbAddr < 1) || (adbAddr > 15))	/* check range 1-15 */
1839 		return (-1);
1840 
1841 	for (i = 1; i < 15; i++)
1842 		if (ADBDevTable[i].currentAddr == adbAddr) {
1843 			ADBDevTable[i].ServiceRtPtr =
1844 			    (void *)(info->siServiceRtPtr);
1845 			ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
1846 			return 0;	/* found */
1847 		}
1848 
1849 	return (-1);		/* not found */
1850 
1851 }
1852 
1853 #ifndef MRG_ADB
1854 
1855 /* caller should really use machine-independant version: getPramTime */
1856 /* this version does pseudo-adb access only */
1857 int
1858 adb_read_date_time(unsigned long *time)
1859 {
1860 	u_char output[ADB_MAX_MSG_LENGTH];
1861 	int result;
1862 	volatile int flag = 0;
1863 
1864 	switch (adbHardware) {
1865 	case ADB_HW_II:
1866 		return -1;
1867 
1868 	case ADB_HW_IISI:
1869 		output[0] = 0x02;	/* 2 byte message */
1870 		output[1] = 0x01;	/* to pram/rtc device */
1871 		output[2] = 0x03;	/* read date/time */
1872 		result = send_adb_IIsi((u_char *)output, (u_char *)output,
1873 		    (void *)adb_op_comprout, (int *)&flag, (int)0);
1874 		if (result != 0)	/* exit if not sent */
1875 			return -1;
1876 
1877 		while (0 == flag)	/* wait for result */
1878 			;
1879 
1880 		*time = (long)(*(long *)(output + 1));
1881 		return 0;
1882 
1883 	case ADB_HW_PMU:
1884 		pm_read_date_time(time);
1885 		return 0;
1886 
1887 	case ADB_HW_CUDA:
1888 		output[0] = 0x02;	/* 2 byte message */
1889 		output[1] = 0x01;	/* to pram/rtc device */
1890 		output[2] = 0x03;	/* read date/time */
1891 		result = send_adb_cuda((u_char *)output, (u_char *)output,
1892 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
1893 		if (result != 0)	/* exit if not sent */
1894 			return -1;
1895 
1896 		while (0 == flag)	/* wait for result */
1897 			;
1898 
1899 		memcpy(time, output + 1, 4);
1900 		return 0;
1901 
1902 	case ADB_HW_UNKNOWN:
1903 	default:
1904 		return -1;
1905 	}
1906 }
1907 
1908 /* caller should really use machine-independant version: setPramTime */
1909 /* this version does pseudo-adb access only */
1910 int
1911 adb_set_date_time(unsigned long time)
1912 {
1913 	u_char output[ADB_MAX_MSG_LENGTH];
1914 	int result;
1915 	volatile int flag = 0;
1916 
1917 	switch (adbHardware) {
1918 
1919 	case ADB_HW_CUDA:
1920 		output[0] = 0x06;	/* 6 byte message */
1921 		output[1] = 0x01;	/* to pram/rtc device */
1922 		output[2] = 0x09;	/* set date/time */
1923 		output[3] = (u_char)(time >> 24);
1924 		output[4] = (u_char)(time >> 16);
1925 		output[5] = (u_char)(time >> 8);
1926 		output[6] = (u_char)(time);
1927 		result = send_adb_cuda((u_char *)output, (u_char *)0,
1928 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
1929 		if (result != 0)	/* exit if not sent */
1930 			return -1;
1931 
1932 		while (0 == flag)	/* wait for send to finish */
1933 			;
1934 
1935 		return 0;
1936 
1937 	case ADB_HW_PMU:
1938 		pm_set_date_time(time);
1939 		return 0;
1940 
1941 	case ADB_HW_II:
1942 	case ADB_HW_IISI:
1943 	case ADB_HW_UNKNOWN:
1944 	default:
1945 		return -1;
1946 	}
1947 }
1948 
1949 
1950 int
1951 adb_poweroff(void)
1952 {
1953 	u_char output[ADB_MAX_MSG_LENGTH];
1954 	int result;
1955 
1956 	if (!adbSoftPower)
1957 		return -1;
1958 
1959 	adb_polling = 1;
1960 
1961 	switch (adbHardware) {
1962 	case ADB_HW_IISI:
1963 		output[0] = 0x02;	/* 2 byte message */
1964 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
1965 		output[2] = 0x0a;	/* set date/time */
1966 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
1967 		    (void *)0, (void *)0, (int)0);
1968 		if (result != 0)	/* exit if not sent */
1969 			return -1;
1970 
1971 		for (;;);		/* wait for power off */
1972 
1973 		return 0;
1974 
1975 	case ADB_HW_PMU:
1976 		pm_adb_poweroff();
1977 
1978 		for (;;);		/* wait for power off */
1979 
1980 		return 0;
1981 
1982 	case ADB_HW_CUDA:
1983 		output[0] = 0x02;	/* 2 byte message */
1984 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
1985 		output[2] = 0x0a;	/* set date/time */
1986 		result = send_adb_cuda((u_char *)output, (u_char *)0,
1987 		    (void *)0, (void *)0, (int)0);
1988 		if (result != 0)	/* exit if not sent */
1989 			return -1;
1990 
1991 		for (;;);		/* wait for power off */
1992 
1993 		return 0;
1994 
1995 	case ADB_HW_II:			/* II models don't do ADB soft power */
1996 	case ADB_HW_UNKNOWN:
1997 	default:
1998 		return -1;
1999 	}
2000 }
2001 
2002 int
2003 adb_prog_switch_enable(void)
2004 {
2005 	u_char output[ADB_MAX_MSG_LENGTH];
2006 	int result;
2007 	volatile int flag = 0;
2008 
2009 	switch (adbHardware) {
2010 	case ADB_HW_IISI:
2011 		output[0] = 0x03;	/* 3 byte message */
2012 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2013 		output[2] = 0x1c;	/* prog. switch control */
2014 		output[3] = 0x01;	/* enable */
2015 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2016 		    (void *)adb_op_comprout, (void *)&flag, (int)0);
2017 		if (result != 0)	/* exit if not sent */
2018 			return -1;
2019 
2020 		while (0 == flag)	/* wait for send to finish */
2021 			;
2022 
2023 		return 0;
2024 
2025 	case ADB_HW_PMU:
2026 		return -1;
2027 
2028 	case ADB_HW_II:		/* II models don't do prog. switch */
2029 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch TO DO: verify this */
2030 	case ADB_HW_UNKNOWN:
2031 	default:
2032 		return -1;
2033 	}
2034 }
2035 
2036 int
2037 adb_prog_switch_disable(void)
2038 {
2039 	u_char output[ADB_MAX_MSG_LENGTH];
2040 	int result;
2041 	volatile int flag = 0;
2042 
2043 	switch (adbHardware) {
2044 	case ADB_HW_IISI:
2045 		output[0] = 0x03;	/* 3 byte message */
2046 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2047 		output[2] = 0x1c;	/* prog. switch control */
2048 		output[3] = 0x01;	/* disable */
2049 		result = send_adb_IIsi((u_char *)output, (u_char *)0,
2050 			(void *)adb_op_comprout, (void *)&flag, (int)0);
2051 		if (result != 0)	/* exit if not sent */
2052 			return -1;
2053 
2054 		while (0 == flag)	/* wait for send to finish */
2055 			;
2056 
2057 		return 0;
2058 
2059 	case ADB_HW_PMU:
2060 		return -1;
2061 
2062 	case ADB_HW_II:		/* II models don't do prog. switch */
2063 	case ADB_HW_CUDA:	/* cuda doesn't do prog. switch */
2064 	case ADB_HW_UNKNOWN:
2065 	default:
2066 		return -1;
2067 	}
2068 }
2069 
2070 int
2071 CountADBs(void)
2072 {
2073 	return (count_adbs());
2074 }
2075 
2076 void
2077 ADBReInit(void)
2078 {
2079 	adb_reinit();
2080 }
2081 
2082 int
2083 GetIndADB(ADBDataBlock * info, int index)
2084 {
2085 	return (get_ind_adb_info(info, index));
2086 }
2087 
2088 int
2089 GetADBInfo(ADBDataBlock * info, int adbAddr)
2090 {
2091 	return (get_adb_info(info, adbAddr));
2092 }
2093 
2094 int
2095 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2096 {
2097 	return (set_adb_info(info, adbAddr));
2098 }
2099 
2100 int
2101 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2102 {
2103 	return (adb_op(buffer, compRout, data, commandNum));
2104 }
2105 
2106 #endif
2107 
2108 int
2109 setsoftadb()
2110 {
2111 	callout_reset(&adb_soft_intr_ch, 1, (void *)adb_soft_intr, NULL);
2112 	return 0;
2113 }
2114 
2115 void
2116 adb_cuda_autopoll()
2117 {
2118 	volatile int flag = 0;
2119 	int result;
2120 	u_char output[16];
2121 
2122 	output[0] = 0x03;	/* 3-byte message */
2123 	output[1] = 0x01;	/* to pram/rtc device */
2124 	output[2] = 0x01;	/* cuda autopoll */
2125 	output[3] = 0x01;
2126 	result = send_adb_cuda(output, output, adb_op_comprout, (void *)&flag,
2127 			       0);
2128 	if (result != 0)	/* exit if not sent */
2129 		return;
2130 
2131 	while (flag == 0);	/* wait for result */
2132 }
2133 
2134 void
2135 adb_restart(void)
2136 {
2137 	int result;
2138 	u_char output[16];
2139 
2140 	adb_polling = 1;
2141 
2142 	switch (adbHardware) {
2143 	case ADB_HW_CUDA:
2144 		output[0] = 0x02;	/* 2 byte message */
2145 		output[1] = 0x01;	/* to pram/rtc/soft-power device */
2146 		output[2] = 0x11;	/* restart */
2147 		result = send_adb_cuda(output, NULL, NULL, NULL, 0);
2148 		if (result != 0)	/* exit if not sent */
2149 			return;
2150 		while (1);		/* not return */
2151 
2152 	case ADB_HW_PMU:
2153 		pm_adb_restart();
2154 		while (1);		/* not return */
2155 	}
2156 }
2157