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