adb_direct.c revision 1.6 1 /* $NetBSD: adb_direct.c,v 1.6 1997/08/11 22:53:27 scottr 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 /* This code is rather messy, but I don't have time right now
36 * to clean it up as much as I would like.
37 * But it works, so I'm happy. :-) jpw */
38
39 #ifdef __NetBSD__
40 #include "opt_mrg_adb.h"
41
42 #include <sys/param.h>
43 #include <sys/cdefs.h>
44 #include <sys/systm.h>
45
46 #include <machine/viareg.h>
47 #include <machine/param.h>
48 #include <machine/cpu.h>
49 #include <machine/adbsys.h> /* required for adbvar.h */
50
51 #include <mac68k/mac68k/macrom.h>
52 #include <mac68k/dev/adb_direct.h>
53 #include <mac68k/dev/adbvar.h>
54 #define printf_intr printf
55 #else
56 #include "via.h" /* for macos based testing */
57 typedef unsigned char u_char;
58 #endif
59
60 /* more verbose for testing */
61 /*#define DEBUG*/
62
63 /* some misc. leftovers */
64 #define vPB 0x0000
65 #define vPB3 0x08
66 #define vPB4 0x10
67 #define vPB5 0x20
68 #define vSR_INT 0x04
69 #define vSR_OUT 0x10
70
71 /* types of adb hardware that we (will eventually) support */
72 #define ADB_HW_UNKNOWN 0x01 /* don't know */
73 #define ADB_HW_II 0x02 /* Mac II series */
74 #define ADB_HW_IISI 0x03 /* Mac IIsi series */
75 #define ADB_HW_PB 0x04 /* PowerBook series */
76 #define ADB_HW_CUDA 0x05 /* Machines with a Cuda chip */
77
78 /* the type of ADB action that we are currently preforming */
79 #define ADB_ACTION_NOTREADY 0x01 /* has not been initialized yet */
80 #define ADB_ACTION_IDLE 0x02 /* the bus is currently idle */
81 #define ADB_ACTION_OUT 0x03 /* sending out a command */
82 #define ADB_ACTION_IN 0x04 /* receiving data */
83
84 /*
85 * These describe the state of the ADB bus itself, although they
86 * don't necessarily correspond directly to ADB states.
87 * Note: these are not really used in the IIsi code.
88 */
89 #define ADB_BUS_UNKNOWN 0x01 /* we don't know yet - all models */
90 #define ADB_BUS_IDLE 0x02 /* bus is idle - all models */
91 #define ADB_BUS_CMD 0x03 /* starting a command - II models */
92 #define ADB_BUS_ODD 0x04 /* the "odd" state - II models */
93 #define ADB_BUS_EVEN 0x05 /* the "even" state - II models */
94 #define ADB_BUS_ACTIVE 0x06 /* active state - IIsi models */
95 #define ADB_BUS_ACK 0x07 /* currently ACKing - IIsi models */
96
97 /*
98 * Shortcuts for setting or testing the VIA bit states.
99 * Not all shortcuts are used for every type of ADB hardware.
100 */
101 #define ADB_SET_STATE_IDLE_II() via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
102 #define ADB_SET_STATE_IDLE_IISI() via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
103 #define ADB_SET_STATE_IDLE_CUDA() via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
104 #define ADB_SET_STATE_CMD() via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
105 #define ADB_SET_STATE_EVEN() via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
106 vBufB) | vPB4) & ~vPB5)
107 #define ADB_SET_STATE_ODD() via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
108 vBufB) | vPB5) & ~vPB4 )
109 #define ADB_SET_STATE_ACTIVE() via_reg(VIA1, vBufB) |= vPB5
110 #define ADB_SET_STATE_INACTIVE() via_reg(VIA1, vBufB) &= ~vPB5
111 #define ADB_SET_STATE_TIP() via_reg(VIA1, vBufB) &= ~vPB5
112 #define ADB_CLR_STATE_TIP() via_reg(VIA1, vBufB) |= vPB5
113 #define ADB_SET_STATE_ACKON() via_reg(VIA1, vBufB) |= vPB4
114 #define ADB_SET_STATE_ACKOFF() via_reg(VIA1, vBufB) &= ~vPB4
115 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg(VIA1, vBufB) ^= vPB4
116 #define ADB_SET_STATE_ACKON_CUDA() via_reg(VIA1, vBufB) &= ~vPB4
117 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg(VIA1, vBufB) |= vPB4
118 #define ADB_SET_SR_INPUT() via_reg(VIA1, vACR) &= ~vSR_OUT
119 #define ADB_SET_SR_OUTPUT() via_reg(VIA1, vACR) |= vSR_OUT
120 #define ADB_SR() via_reg(VIA1, vSR)
121 #define ADB_VIA_INTR_ENABLE() via_reg(VIA1, vIER) = 0x84
122 #define ADB_VIA_INTR_DISABLE() via_reg(VIA1, vIER) = 0x04
123 #define ADB_VIA_CLR_INTR() via_reg(VIA1, vIFR) = 0x04
124 #define ADB_INTR_IS_OFF (vPB3 == (via_reg(VIA1, vBufB) & vPB3))
125 #define ADB_INTR_IS_ON (0 == (via_reg(VIA1, vBufB) & vPB3))
126 #define ADB_SR_INTR_IS_OFF (0 == (via_reg(VIA1, vIFR) & vSR_INT))
127 #define ADB_SR_INTR_IS_ON (vSR_INT == (via_reg(VIA1, \
128 vIFR) & vSR_INT))
129
130 /*
131 * This is the delay that is required (in uS) between certain
132 * ADB transactions. The actual timing delay for for each uS is
133 * calculated at boot time to account for differences in machine speed.
134 */
135 #define ADB_DELAY 150
136
137 /*
138 * Maximum ADB message length; includes space for data, result, and
139 * device code - plus a little for safety.
140 */
141 #define MAX_ADB_MSG_LENGTH 20
142
143 /*
144 * A structure for storing information about each ADB device.
145 */
146 struct ADBDevEntry {
147 void (*ServiceRtPtr) __P((void));
148 void *DataAreaAddr;
149 char devType;
150 char origAddr;
151 char currentAddr;
152 };
153
154 /*
155 * Used to hold ADB commands that are waiting to be sent out.
156 */
157 struct adbCmdHoldEntry {
158 u_char outBuf[MAX_ADB_MSG_LENGTH]; /* our message */
159 u_char *saveBuf; /* buffer to know where to save result */
160 u_char *compRout; /* completion routine pointer */
161 u_char *data; /* completion routine data pointer */
162 };
163
164 /*
165 * A few variables that we need and their initial values.
166 */
167 int adbHardware = ADB_HW_UNKNOWN;
168 int adbActionState = ADB_ACTION_NOTREADY;
169 int adbBusState = ADB_BUS_UNKNOWN;
170 int adbWaiting = 0; /* waiting for return data from the device */
171 int adbWriteDelay = 0; /* working on (or waiting to do) a write */
172 int adbOutQueueHasData = 0; /* something in the queue waiting to go out */
173 int adbNextEnd = 0; /* the next incoming bute is the last (II) */
174
175 int adbWaitingCmd = 0; /* ADB command we are waiting for */
176 u_char *adbBuffer = (long) 0; /* pointer to user data area */
177 void *adbCompRout = (long) 0; /* pointer to the completion routine */
178 void *adbCompData = (long) 0; /* pointer to the completion routine data */
179 long adbFakeInts = 0; /* keeps track of fake ADB interrupts for
180 * timeouts (II) */
181 int adbStarting = 1; /* doing ADBReInit so do polling differently */
182 int adbSendTalk = 0; /* the intr routine is sending the talk, not
183 * the user (II) */
184 int adbPolling = 0; /* we are polling for service request */
185 int adbPollCmd = 0; /* the last poll command we sent */
186
187 u_char adbInputBuffer[MAX_ADB_MSG_LENGTH]; /* data input buffer */
188 u_char adbOutputBuffer[MAX_ADB_MSG_LENGTH]; /* data output buffer */
189 struct adbCmdHoldEntry adbOutQueue; /* our 1 entry output queue */
190
191 int adbSentChars = 0; /* how many characters we have sent */
192 int adbLastDevice = 0; /* last ADB dev we heard from (II ONLY) */
193 int adbLastDevIndex = 0; /* last ADB dev loc in dev table (II ONLY) */
194 int adbLastCommand = 0; /* the last ADB command we sent (II) */
195
196 struct ADBDevEntry ADBDevTable[16]; /* our ADB device table */
197 int ADBNumDevices; /* num. of ADB devices found with ADBReInit */
198
199 extern struct mac68k_machine_S mac68k_machine;
200
201 int zshard __P((int));
202
203 void pm_setup_adb __P((void));
204 void pm_check_adb_devices __P((int));
205 void pm_intr __P((void));
206 int pm_adb_op __P((u_char *, void *, void *, int));
207 void pm_init_adb_device __P((void));
208
209 /*
210 * The following are private routines.
211 */
212 void print_single __P((u_char *));
213 void adb_intr __P((void));
214 void adb_intr_II __P((void));
215 void adb_intr_IIsi __P((void));
216 void adb_intr_cuda __P((void));
217 int send_adb_II __P((u_char *, u_char *, void *, void *, int));
218 int send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
219 int send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
220 void adb_intr_cuda_test __P((void));
221 void adb_handle_unsol __P((u_char *));
222 void adb_op_comprout __P((void));
223 void adb_reinit __P((void));
224 int count_adbs __P((void));
225 int get_ind_adb_info __P((ADBDataBlock *, int));
226 int get_adb_info __P((ADBDataBlock *, int));
227 int set_adb_info __P((ADBSetInfoBlock *, int));
228 void adb_setup_hw_type __P((void));
229 int adb_op __P((Ptr, Ptr, Ptr, short));
230 void adb_handle_unsol __P((u_char *));
231 int adb_op_sync __P((Ptr, Ptr, Ptr, short));
232 void adb_read_II __P((u_char *));
233 void adb_cleanup __P((u_char *));
234 void adb_cleanup_IIsi __P((u_char *));
235 void adb_comp_exec __P((void));
236 int adb_cmd_result __P((u_char *));
237 int adb_cmd_extra __P((u_char *));
238 int adb_guess_next_device __P((void));
239 int adb_prog_switch_enable __P((void));
240 int adb_prog_switch_disable __P((void));
241 /* we should create this and it will be the public version */
242 int send_adb __P((u_char *, void *, void *));
243
244 /*
245 * print_single
246 * Diagnostic display routine. Displays the hex values of the
247 * specified elements of the u_char. The length of the "string"
248 * is in [0].
249 */
250 void
251 print_single(thestring)
252 u_char *thestring;
253 {
254 int x;
255
256 if ((int) (thestring[0]) == 0) {
257 printf_intr("nothing returned\n");
258 return;
259 }
260 if (thestring == 0) {
261 printf_intr("no data - null pointer\n");
262 return;
263 }
264 if (thestring[0] > 20) {
265 printf_intr("ADB: ACK > 20 no way!\n");
266 thestring[0] = 20;
267 }
268 printf_intr("(length=0x%x):", thestring[0]);
269 for (x = 0; x < thestring[0]; x++)
270 printf_intr(" 0x%02x", thestring[x + 1]);
271 printf_intr("\n");
272 }
273
274
275 /*
276 * called when when an adb interrupt happens
277 *
278 * Cuda version of adb_intr
279 * TO DO: do we want to add some zshard calls in here?
280 */
281 void
282 adb_intr_cuda(void)
283 {
284 int i, ending, len;
285 unsigned int s;
286
287 s = splhigh(); /* can't be too careful - might be called */
288 /* from a routine, NOT an interrupt */
289
290 ADB_VIA_CLR_INTR(); /* clear interrupt */
291
292 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
293
294 switch_start:
295 switch (adbActionState) {
296 case ADB_ACTION_IDLE:
297 /* This is an unexpected packet, so grab the first (dummy)
298 * byte, set up the proper vars, and tell the chip we are
299 * starting to receive the packet by setting the TIP bit. */
300 adbInputBuffer[1] = ADB_SR();
301 ADB_SET_STATE_TIP();
302 ADB_SET_SR_INPUT();
303 delay(ADB_DELAY); /* required delay */
304 #ifdef DEBUG
305 printf_intr("idle 0x%02x ", adbInputBuffer[1]);
306 #endif
307 adbInputBuffer[0] = 1;
308 adbActionState = ADB_ACTION_IN;
309 break;
310
311 case ADB_ACTION_IN:
312 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
313 /* intr off means this is the last byte (end of frame) */
314 if (ADB_INTR_IS_OFF)
315 ending = 1;
316 else
317 ending = 0;
318
319 /* if the second byte is 0xff, it's a "dummy" packet */
320 if (adbInputBuffer[2] == 0xff)
321 ending = 1;
322
323 if (1 == ending) { /* end of message? */
324 #ifdef DEBUG
325 printf_intr("in end 0x%02x ",
326 adbInputBuffer[adbInputBuffer[0]]);
327 print_single(adbInputBuffer);
328 #endif
329
330 /* Are we waiting AND does this packet match what we
331 * are waiting for AND is it coming from either the
332 * ADB or RTC/PRAM sub-device? This section _should_
333 * recognize all ADB and RTC/PRAM type commands, but
334 * there may be more... NOTE: commands are always at
335 * [4], even for RTC/PRAM commands. */
336 if ((adbWaiting == 1) &&
337 (adbInputBuffer[4] == adbWaitingCmd) &&
338 ((adbInputBuffer[2] == 0x00) ||
339 (adbInputBuffer[2] == 0x01))) {
340
341 if (adbBuffer != (long) 0) {
342 /* if valid return data pointer */
343 /* get return length minus extras */
344 len = adbInputBuffer[0] - 4;
345 /*
346 * If adb_op is ever made to be called
347 * from a user routine, we should use
348 * a copyout or copyin here to be sure
349 * we're in the correct context
350 */
351 for (i = 1; i <= len; i++)
352 adbBuffer[i] = adbInputBuffer[4 + i];
353 if (len < 0)
354 len = 0;
355 adbBuffer[0] = len;
356 }
357 /* call completion routine and clean up */
358 adb_comp_exec();
359 adbWaitingCmd = 0;
360 adbWaiting = 0;
361 adbBuffer = (long) 0;
362 adbCompRout = (long) 0;
363 adbCompData = (long) 0;
364 } else {
365 /*
366 * This was an unsolicited packet, so
367 * pass the data off to the handler for
368 * this device if we are NOT doing this
369 * during a ADBReInit.
370 * This section IGNORES all data that is not
371 * from the ADB sub-device. That is, not from
372 * RTC or PRAM. Maybe we should fix later,
373 * but do the other devices every send things
374 * without being asked?
375 */
376 if (adbStarting == 0)
377 if (adbInputBuffer[2] == 0x00)
378 adb_handle_unsol(adbInputBuffer);
379 }
380
381 /* reset vars and signal the end of this frame */
382 adbActionState = ADB_ACTION_IDLE;
383 adbInputBuffer[0] = 0;
384 ADB_SET_STATE_IDLE_CUDA();
385
386 /*
387 * If there is something waiting to be sent out,
388 * the set everything up and send the first byte.
389 */
390 if (adbWriteDelay == 1) {
391 delay(ADB_DELAY); /* required */
392 adbSentChars = 0;
393 adbActionState = ADB_ACTION_OUT;
394
395 /* TO DO: don't we need to set up adbWaiting vars here??? */
396
397 /*
398 * If the interrupt is on, we were too slow
399 * and the chip has already started to send
400 * something to us, so back out of the write
401 * and start a read cycle.
402 */
403 if (ADB_INTR_IS_ON) {
404 ADB_SET_STATE_IDLE_CUDA();
405 ADB_SET_SR_INPUT();
406 adbSentChars = 0;
407 adbActionState = ADB_ACTION_IDLE;
408 adbInputBuffer[0] = 0;
409 break;
410 }
411 /*
412 * If we got here, it's ok to start sending
413 * so load the first byte and tell the chip
414 * we want to send.
415 */
416 ADB_SET_SR_OUTPUT();
417 ADB_SR() = adbOutputBuffer[adbSentChars + 1];
418 ADB_SET_STATE_TIP();
419 }
420 } else {
421 ADB_TOGGLE_STATE_ACK_CUDA();
422 #ifdef DEBUG
423 printf_intr("in 0x%02x ",
424 adbInputBuffer[adbInputBuffer[0]]);
425 #endif
426 }
427 break;
428
429 case ADB_ACTION_OUT:
430 i = ADB_SR(); /* reset SR-intr in IFR */
431 #ifdef DEBUG
432 printf_intr("intr out 0x%02x ", i);
433 #endif
434 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
435
436 adbSentChars++;
437 if (ADB_INTR_IS_ON) { /* ADB intr low during write */
438 #ifdef DEBUG
439 printf_intr("intr was on ");
440 #endif
441 ADB_SET_STATE_IDLE_CUDA();
442 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
443 adbSentChars = 0; /* must start all over */
444 adbActionState = ADB_ACTION_IDLE; /* new state */
445 adbInputBuffer[0] = 0;
446 adbWriteDelay = 1; /* must retry when done with
447 * read */
448 delay(ADB_DELAY);
449 goto switch_start; /* process next state right
450 * now */
451 break;
452 }
453 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
454 if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data
455 * back? */
456 adbWaiting = 1; /* signal waiting for return */
457 adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */
458 } else {/* no talk, so done */
459 adb_comp_exec(); /* call completion
460 * routine */
461 adbWaitingCmd = 0; /* reset "waiting" vars,
462 * just in case */
463 adbBuffer = (long) 0;
464 adbCompRout = (long) 0;
465 adbCompData = (long) 0;
466 }
467
468 adbWriteDelay = 0; /* done writing */
469 adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */
470 ADB_SET_STATE_IDLE_CUDA();
471 #ifdef DEBUG
472 printf_intr("write done ");
473 #endif
474 } else {
475 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* send next byte */
476 ADB_TOGGLE_STATE_ACK_CUDA(); /* signal byte ready to
477 * shift */
478 #ifdef DEBUG
479 printf_intr("toggle ");
480 #endif
481 }
482 break;
483
484 case ADB_ACTION_NOTREADY:
485 printf_intr("adb: not yet initialized\n");
486 break;
487
488 default:
489 printf_intr("intr: unknown ADB state\n");
490 }
491
492 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
493
494 splx(s); /* restore */
495
496 return;
497 } /* end adb_intr_IIsi */
498
499
500 int
501 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
502 command)
503 {
504 int i, s, len;
505
506 #ifdef DEBUG
507 printf_intr("SEND\n");
508 #endif
509
510 if (adbActionState == ADB_ACTION_NOTREADY)
511 return 1;
512
513 s = splhigh(); /* don't interrupt while we are messing with
514 * the ADB */
515
516 if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */
517 (ADB_INTR_IS_OFF)) { /* and no incoming interrupt? */
518 } else
519 if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */
520 adbWriteDelay = 1; /* if no, then we'll "queue"
521 * it up */
522 else {
523 splx(s);
524 return 1; /* really busy! */
525 }
526
527 #ifdef DEBUG
528 printf_intr("QUEUE\n");
529 #endif
530 if ((long) in == (long) 0) { /* need to convert? */
531 /* don't need to use adb_cmd_extra here because this section
532 * will be called */
533 /* ONLY when it is an ADB command (no RTC or PRAM) */
534 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
535 * doing a listen! */
536 len = buffer[0]; /* length of additional data */
537 else
538 len = 0;/* no additional data */
539
540 adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl.
541 * data */
542 adbOutputBuffer[1] = 0x00; /* mark as an ADB command */
543 adbOutputBuffer[2] = (u_char) command; /* load command */
544
545 for (i = 1; i <= len; i++) /* copy additional output
546 * data, if any */
547 adbOutputBuffer[2 + i] = buffer[i];
548 } else
549 for (i = 0; i <= (adbOutputBuffer[0] + 1); i++)
550 adbOutputBuffer[i] = in[i];
551
552 adbSentChars = 0; /* nothing sent yet */
553 adbBuffer = buffer; /* save buffer to know where to save result */
554 adbCompRout = compRout; /* save completion routine pointer */
555 adbCompData = data; /* save completion routine data pointer */
556 adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */
557
558 if (adbWriteDelay != 1) { /* start command now? */
559 #ifdef DEBUG
560 printf_intr("out start NOW");
561 #endif
562 delay(ADB_DELAY);
563 adbActionState = ADB_ACTION_OUT; /* set next state */
564 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
565 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* load byte for output */
566 ADB_SET_STATE_ACKOFF_CUDA();
567 ADB_SET_STATE_TIP(); /* tell ADB that we want to send */
568 }
569 adbWriteDelay = 1; /* something in the write "queue" */
570
571 splx(s);
572
573 if (0x0100 <= (s & 0x0700)) /* were VIA1 interrupts blocked ? */
574 /* poll until byte done */
575 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
576 || (adbWaiting == 1))
577 if (ADB_SR_INTR_IS_ON) /* wait for "interrupt" */
578 adb_intr_cuda(); /* go process
579 * "interrupt" */
580
581 return 0;
582 } /* send_adb_cuda */
583
584
585 /* TO DO: add one or two zshard calls in here */
586 void
587 adb_intr_II(void)
588 {
589 int i, len, intr_on = 0;
590 int send = 0, do_srq = 0;
591 unsigned int s;
592
593 s = splhigh(); /* can't be too careful - might be called */
594 /* from a routine, NOT an interrupt */
595
596 ADB_VIA_CLR_INTR(); /* clear interrupt */
597
598 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
599
600 /*if (ADB_INTR_IS_ON)*/
601 /* printf_intr("INTR ON ");*/
602 if (ADB_INTR_IS_ON)
603 intr_on = 1; /* save for later */
604
605 switch (adbActionState) {
606 case ADB_ACTION_IDLE:
607 if (!intr_on) {
608 /* printf_intr("FAKE DROPPED \n"); */
609 /* printf_intr(" XX "); */
610 i = ADB_SR();
611 break;
612 }
613 adbNextEnd = 0;
614 /* printf_intr("idle "); */
615 adbInputBuffer[0] = 1;
616 adbInputBuffer[1] = ADB_SR(); /* get first byte */
617 /* printf_intr("0x%02x ", adbInputBuffer[1]); */
618 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
619 adbActionState = ADB_ACTION_IN; /* set next state */
620 ADB_SET_STATE_EVEN(); /* set bus state to even */
621 adbBusState = ADB_BUS_EVEN;
622 break;
623
624 case ADB_ACTION_IN:
625 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR(); /* get byte */
626 /* printf_intr("in 0x%02x ",
627 * adbInputBuffer[adbInputBuffer[0]]); */
628 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
629
630 /*
631 * Check for an unsolicited Service Request (SRQ).
632 * An empty SRQ packet NEVER ends, so we must manually
633 * check for the following condition.
634 */
635 if (adbInputBuffer[0] == 4 && adbInputBuffer[2] == 0xff &&
636 adbInputBuffer[3] == 0xff && adbInputBuffer[4] == 0xff &&
637 intr_on && !adbNextEnd)
638 do_srq = 1;
639
640 if (adbNextEnd == 1) { /* process last byte of packet */
641 adbNextEnd = 0;
642 /* printf_intr("done: "); */
643
644 /* If the following conditions are true (4 byte
645 * message, last 3 bytes are 0xff) then we basically
646 * got a "no response" from the ADB chip, so change
647 * the message to an empty one. We also clear intr_on
648 * to stop the SRQ send later on because these packets
649 * normally have the SRQ bit set even when there is
650 * NOT a pending SRQ. */
651 if (adbInputBuffer[0] == 4 && adbInputBuffer[2] == 0xff &&
652 adbInputBuffer[3] == 0xff && adbInputBuffer[4] == 0xff) {
653 /* printf_intr("NO RESP "); */
654 intr_on = 0;
655 adbInputBuffer[0] = 0;
656 }
657 adbLastDevice = (adbInputBuffer[1] & 0xf0) >> 4;
658
659 if ((!adbWaiting || adbPolling)
660 && (adbInputBuffer[0] != 0)) {
661 /* unsolicided - ignore if starting */
662 if (!adbStarting)
663 adb_handle_unsol(adbInputBuffer);
664 } else
665 if (!adbPolling) { /* someone asked for it */
666 /* printf_intr("SOL: "); */
667 /* print_single(adbInputBuffer); */
668 if (adbBuffer != (long) 0) { /* if valid return data
669 * pointer */
670 /* get return length minus
671 * extras */
672 len = adbInputBuffer[0] - 1;
673
674 /* if adb_op is ever made to
675 * be called from a user
676 * routine, we should use a
677 * copyout or copyin here to
678 * be sure we're in the
679 * correct context. */
680 for (i = 1; i <= len; i++)
681 adbBuffer[i] = adbInputBuffer[i + 1];
682 if (len < 0)
683 len = 0;
684 adbBuffer[0] = len;
685 }
686 adb_comp_exec();
687 }
688 adbWaiting = 0;
689 adbPolling = 0;
690 adbInputBuffer[0] = 0;
691 adbBuffer = (long) 0;
692 adbCompRout = (long) 0;
693 adbCompData = (long) 0;
694 /*
695 * Since we are done, check whether there is any data
696 * waiting to do out. If so, start the sending the data.
697 */
698 if (adbOutQueueHasData == 1) {
699 /* printf_intr("XXX: DOING OUT QUEUE\n"); */
700 /* copy over data */
701 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
702 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
703 adbBuffer = adbOutQueue.saveBuf; /* user data area */
704 adbCompRout = adbOutQueue.compRout; /* completion routine */
705 adbCompData = adbOutQueue.data; /* comp. rout. data */
706 adbOutQueueHasData = 0; /* currently processing
707 * "queue" entry */
708 adbPolling = 0;
709 send = 1;
710 /* if intr_on is true, then it's a SRQ so poll
711 * other devices. */
712 } else
713 if (intr_on) {
714 /* printf_intr("starting POLL "); */
715 do_srq = 1;
716 adbPolling = 1;
717 } else
718 if ((adbInputBuffer[1] & 0x0f) != 0x0c) {
719 /* printf_intr("xC HACK "); */
720 adbPolling = 1;
721 send = 1;
722 adbOutputBuffer[0] = 1;
723 adbOutputBuffer[1] = (adbInputBuffer[1] & 0xf0) | 0x0c;
724 } else {
725 /* printf_intr("ending "); */
726 adbBusState = ADB_BUS_IDLE;
727 adbActionState = ADB_ACTION_IDLE;
728 ADB_SET_STATE_IDLE_II();
729 break;
730 }
731 }
732 /*
733 * If do_srq is true then something above determined that
734 * the message has ended and some device is sending a
735 * service request. So we need to determine the next device
736 * and send a poll to it. (If the device we send to isn't the
737 * one that sent the SRQ, that ok as it will be caught
738 * the next time though.)
739 */
740 if (do_srq) {
741 /* printf_intr("SRQ! "); */
742 adbPolling = 1;
743 adb_guess_next_device();
744 adbOutputBuffer[0] = 1;
745 adbOutputBuffer[1] = ((adbLastDevice & 0x0f) << 4) | 0x0c;
746 send = 1;
747 }
748 /*
749 * If send is true then something above determined that
750 * the message has ended and we need to start sending out
751 * a new message immediately. This could be because there
752 * is data waiting to go out or because an SRQ was seen.
753 */
754 if (send) {
755 adbNextEnd = 0;
756 adbSentChars = 0; /* nothing sent yet */
757 adbActionState = ADB_ACTION_OUT; /* set next state */
758 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
759 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
760 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
761 ADB_SET_STATE_CMD(); /* tell ADB that we want to
762 * send */
763 break;
764 }
765 /* We only get this far if the message hasn't ended yet. */
766 if (!intr_on) /* if adb intr. on then the */
767 adbNextEnd = 1; /* NEXT byte is the last */
768
769 switch (adbBusState) { /* set to next state */
770 case ADB_BUS_EVEN:
771 ADB_SET_STATE_ODD(); /* set state to odd */
772 adbBusState = ADB_BUS_ODD;
773 break;
774
775 case ADB_BUS_ODD:
776 ADB_SET_STATE_EVEN(); /* set state to even */
777 adbBusState = ADB_BUS_EVEN;
778 break;
779 default:
780 printf_intr("strange state!!!\n"); /* huh? */
781 break;
782 }
783 break;
784
785 case ADB_ACTION_OUT:
786 adbNextEnd = 0;
787 if (!adbPolling)
788 adbWaiting = 1; /* not unsolicited */
789 i = ADB_SR(); /* clear interrupt */
790 adbSentChars++;
791 /*
792 * If the outgoing data was a TALK, we must
793 * switch to input mode to get the result.
794 */
795 if ((adbOutputBuffer[1] & 0x0c) == 0x0c) {
796 adbInputBuffer[0] = 1;
797 adbInputBuffer[1] = i;
798 adbActionState = ADB_ACTION_IN;
799 ADB_SET_SR_INPUT();
800 adbBusState = ADB_BUS_EVEN;
801 ADB_SET_STATE_EVEN();
802 /* printf_intr("talk out 0x%02x ", i); */
803 break;
804 }
805 /* If it's not a TALK, check whether all data has been sent.
806 * If so, call the completion routine and clean up. If not,
807 * advance to the next state. */
808 /* printf_intr("non-talk out 0x%0x ", i); */
809 ADB_SET_SR_OUTPUT();
810 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
811 /* printf_intr("done \n"); */
812 adb_comp_exec();
813 adbBuffer = (long) 0;
814 adbCompRout = (long) 0;
815 adbCompData = (long) 0;
816 if (adbOutQueueHasData == 1) {
817 /* copy over data */
818 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
819 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
820 adbBuffer = adbOutQueue.saveBuf; /* user data area */
821 adbCompRout = adbOutQueue.compRout; /* completion routine */
822 adbCompData = adbOutQueue.data; /* comp. rout. data */
823 adbOutQueueHasData = 0; /* currently processing
824 * "queue" entry */
825 adbPolling = 0;
826 } else {
827 adbOutputBuffer[0] = 1;
828 adbOutputBuffer[1] = (adbOutputBuffer[1] & 0xf0) | 0x0c;
829 adbPolling = 1; /* non-user poll */
830 }
831 adbNextEnd = 0;
832 adbSentChars = 0; /* nothing sent yet */
833 adbActionState = ADB_ACTION_OUT; /* set next state */
834 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
835 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
836 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
837 ADB_SET_STATE_CMD(); /* tell ADB that we want to
838 * send */
839 break;
840 }
841 ADB_SR() = adbOutputBuffer[adbSentChars + 1];
842 switch (adbBusState) { /* advance to next state */
843 case ADB_BUS_EVEN:
844 ADB_SET_STATE_ODD(); /* set state to odd */
845 adbBusState = ADB_BUS_ODD;
846 break;
847
848 case ADB_BUS_CMD:
849 case ADB_BUS_ODD:
850 ADB_SET_STATE_EVEN(); /* set state to even */
851 adbBusState = ADB_BUS_EVEN;
852 break;
853
854 default:
855 printf_intr("strange state!!! (0x%x)\n", adbBusState);
856 break;
857 }
858 break;
859
860 default:
861 printf_intr("adb: unknown ADB state (during intr)\n");
862 }
863
864 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
865
866 splx(s); /* restore */
867
868 return;
869
870 }
871
872
873 /*
874 * send_adb version for II series machines
875 */
876 int
877 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
878 {
879 int i, s, len;
880
881 if (adbActionState == ADB_ACTION_NOTREADY) /* return if ADB not
882 * available */
883 return 1;
884
885 s = splhigh(); /* don't interrupt while we are messing with
886 * the ADB */
887
888 if (0 != adbOutQueueHasData) { /* right now, "has data" means "full" */
889 splx(s); /* sorry, try again later */
890 return 1;
891 }
892 if ((long) in == (long) 0) { /* need to convert? */
893 /*
894 * Don't need to use adb_cmd_extra here because this section
895 * will be called ONLY when it is an ADB command (no RTC or
896 * PRAM), especially on II series!
897 */
898 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
899 * doing a listen! */
900 len = buffer[0]; /* length of additional data */
901 else
902 len = 0;/* no additional data */
903
904 adbOutQueue.outBuf[0] = 1 + len; /* command + addl. data */
905 adbOutQueue.outBuf[1] = (u_char) command; /* load command */
906
907 for (i = 1; i <= len; i++) /* copy additional output
908 * data, if any */
909 adbOutQueue.outBuf[1 + i] = buffer[i];
910 } else
911 /* if data ready, just copy over */
912 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
913 adbOutQueue.outBuf[i] = in[i];
914
915 adbOutQueue.saveBuf = buffer; /* save buffer to know where to save
916 * result */
917 adbOutQueue.compRout = compRout; /* save completion routine
918 * pointer */
919 adbOutQueue.data = data;/* save completion routine data pointer */
920
921 if ((adbActionState == ADB_ACTION_IDLE) && /* is ADB available? */
922 (ADB_INTR_IS_OFF) &&/* and no incoming interrupts? */
923 (adbPolling == 0)) {/* and we are not currently polling */
924 /* then start command now */
925 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++) /* copy over data */
926 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
927
928 adbBuffer = adbOutQueue.saveBuf; /* pointer to user data
929 * area */
930 adbCompRout = adbOutQueue.compRout; /* pointer to the
931 * completion routine */
932 adbCompData = adbOutQueue.data; /* pointer to the completion
933 * routine data */
934
935 adbSentChars = 0; /* nothing sent yet */
936 adbActionState = ADB_ACTION_OUT; /* set next state */
937 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
938
939 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
940
941 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* load byte for output */
942 ADB_SET_STATE_CMD(); /* tell ADB that we want to send */
943 adbOutQueueHasData = 0; /* currently processing "queue" entry */
944 } else
945 adbOutQueueHasData = 1; /* something in the write "queue" */
946
947 splx(s);
948
949 if (0x0100 <= (s & 0x0700)) /* were VIA1 interrupts blocked ? */
950 /* poll until message done */
951 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
952 || (adbWaiting == 1) || (adbPolling == 1))
953 if (ADB_SR_INTR_IS_ON) /* wait for "interrupt" */
954 adb_intr_II(); /* go process "interrupt" */
955
956 return 0;
957 }
958
959
960 /*
961 * This routine is called from the II series interrupt routine
962 * to determine what the "next" device is that should be polled.
963 */
964 int
965 adb_guess_next_device(void)
966 {
967 int last, i, dummy;
968
969 if (adbStarting) {
970 /* start polling EVERY device, since we can't be sure there is
971 * anything in the device table yet */
972 if (adbLastDevice < 1 || adbLastDevice > 15)
973 adbLastDevice = 1;
974 if (++adbLastDevice > 15) /* point to next one */
975 adbLastDevice = 1;
976 } else {
977 /* find the next device using the device table */
978 if (adbLastDevice < 1 || adbLastDevice > 15) /* let's be parinoid */
979 adbLastDevice = 2;
980 last = 1; /* default index location */
981
982 for (i = 1; i < 16; i++) /* find index entry */
983 if (ADBDevTable[i].currentAddr == adbLastDevice) { /* look for device */
984 last = i; /* found it */
985 break;
986 }
987 dummy = last; /* index to start at */
988 for (;;) { /* find next device in index */
989 if (++dummy > 15) /* wrap around if needed */
990 dummy = 1;
991 if (dummy == last) { /* didn't find any other
992 * device! This can happen if
993 * there are no devices on the
994 * bus */
995 dummy = 2;
996 break;
997 }
998 /* found the next device */
999 if (ADBDevTable[dummy].devType != 0)
1000 break;
1001 }
1002 adbLastDevice = ADBDevTable[dummy].currentAddr;
1003 }
1004 return adbLastDevice;
1005 }
1006 /*
1007 * Called when when an adb interrupt happens.
1008 * This routine simply transfers control over to the appropriate
1009 * code for the machine we are running on.
1010 */
1011 void
1012 adb_intr(void)
1013 {
1014 switch (adbHardware) {
1015 case ADB_HW_II:
1016 adb_intr_II();
1017 break;
1018
1019 case ADB_HW_IISI:
1020 adb_intr_IIsi();
1021 break;
1022
1023 case ADB_HW_PB:
1024 break;
1025
1026 case ADB_HW_CUDA:
1027 adb_intr_cuda();
1028 break;
1029
1030 case ADB_HW_UNKNOWN:
1031 break;
1032 }
1033 }
1034
1035
1036 /*
1037 * called when when an adb interrupt happens
1038 *
1039 * IIsi version of adb_intr
1040 *
1041 */
1042 void
1043 adb_intr_IIsi(void)
1044 {
1045 int i, ending, len;
1046 unsigned int s;
1047
1048 s = splhigh(); /* can't be too careful - might be called */
1049 /* from a routine, NOT an interrupt */
1050
1051 ADB_VIA_CLR_INTR(); /* clear interrupt */
1052
1053 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
1054
1055 switch_start:
1056 switch (adbActionState) {
1057 case ADB_ACTION_IDLE:
1058 delay(ADB_DELAY); /* short delay is required before the
1059 * first byte */
1060
1061 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1062 ADB_SET_STATE_ACTIVE(); /* signal start of data frame */
1063 adbInputBuffer[1] = ADB_SR(); /* get byte */
1064 adbInputBuffer[0] = 1;
1065 adbActionState = ADB_ACTION_IN; /* set next state */
1066
1067 ADB_SET_STATE_ACKON(); /* start ACK to ADB chip */
1068 delay(ADB_DELAY); /* delay */
1069 ADB_SET_STATE_ACKOFF(); /* end ACK to ADB chip */
1070 zshard(0); /* grab any serial interrupts */
1071 break;
1072
1073 case ADB_ACTION_IN:
1074 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1075 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR(); /* get byte */
1076 if (ADB_INTR_IS_OFF) /* check for end of frame */
1077 ending = 1;
1078 else
1079 ending = 0;
1080
1081 ADB_SET_STATE_ACKON(); /* start ACK to ADB chip */
1082 delay(ADB_DELAY); /* delay */
1083 ADB_SET_STATE_ACKOFF(); /* end ACK to ADB chip */
1084 zshard(0); /* grab any serial interrupts */
1085
1086 if (1 == ending) { /* end of message? */
1087 ADB_SET_STATE_INACTIVE(); /* signal end of frame */
1088 /* this section _should_ handle all ADB and RTC/PRAM
1089 * type commands, */
1090 /* but there may be more... */
1091 /* note: commands are always at [4], even for rtc/pram
1092 * commands */
1093 if ((adbWaiting == 1) && /* are we waiting AND */
1094 (adbInputBuffer[4] == adbWaitingCmd) && /* the cmd we sent AND */
1095 ((adbInputBuffer[2] == 0x00) || /* it's from the ADB
1096 * device OR */
1097 (adbInputBuffer[2] == 0x01))) { /* it's from the
1098 * PRAM/RTC device */
1099
1100 /* is this data we are waiting for? */
1101 if (adbBuffer != (long) 0) { /* if valid return data
1102 * pointer */
1103 /* get return length minus extras */
1104 len = adbInputBuffer[0] - 4;
1105 /* if adb_op is ever made to be called
1106 * from a user routine, we should use
1107 * a copyout or copyin here to be sure
1108 * we're in the correct context */
1109 for (i = 1; i <= len; i++)
1110 adbBuffer[i] = adbInputBuffer[4 + i];
1111 if (len < 0)
1112 len = 0;
1113 adbBuffer[0] = len;
1114 }
1115 adb_comp_exec(); /* call completion
1116 * routine */
1117
1118 adbWaitingCmd = 0; /* reset "waiting" vars */
1119 adbWaiting = 0;
1120 adbBuffer = (long) 0;
1121 adbCompRout = (long) 0;
1122 adbCompData = (long) 0;
1123 } else {
1124 /* pass the data off to the handler */
1125 /* This section IGNORES all data that is not
1126 * from the ADB sub-device. That is, not from
1127 * rtc or pram. Maybe we should fix later,
1128 * but do the other devices every send things
1129 * without being asked? */
1130 if (adbStarting == 0) /* ignore if during
1131 * adbreinit */
1132 if (adbInputBuffer[2] == 0x00)
1133 adb_handle_unsol(adbInputBuffer);
1134 }
1135
1136 adbActionState = ADB_ACTION_IDLE;
1137 adbInputBuffer[0] = 0; /* reset length */
1138
1139 if (adbWriteDelay == 1) { /* were we waiting to
1140 * write? */
1141 adbSentChars = 0; /* nothing sent yet */
1142 adbActionState = ADB_ACTION_OUT; /* set next state */
1143
1144 delay(ADB_DELAY); /* delay */
1145 zshard(0); /* grab any serial interrupts */
1146
1147 if (ADB_INTR_IS_ON) { /* ADB intr low during
1148 * write */
1149 ADB_SET_STATE_IDLE_IISI(); /* reset */
1150 ADB_SET_SR_INPUT(); /* make sure SR is set
1151 * to IN */
1152 adbSentChars = 0; /* must start all over */
1153 adbActionState = ADB_ACTION_IDLE; /* new state */
1154 adbInputBuffer[0] = 0;
1155 /* may be able to take this out later */
1156 delay(ADB_DELAY); /* delay */
1157 break;
1158 }
1159 ADB_SET_STATE_ACTIVE(); /* tell ADB that we want
1160 * to send */
1161 ADB_SET_STATE_ACKOFF(); /* make sure */
1162 ADB_SET_SR_OUTPUT(); /* set shift register
1163 * for OUT */
1164 ADB_SR() = adbOutputBuffer[adbSentChars + 1];
1165 ADB_SET_STATE_ACKON(); /* tell ADB byte ready
1166 * to shift */
1167 }
1168 }
1169 break;
1170
1171 case ADB_ACTION_OUT:
1172 i = ADB_SR(); /* reset SR-intr in IFR */
1173 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
1174
1175 ADB_SET_STATE_ACKOFF(); /* finish ACK */
1176 adbSentChars++;
1177 if (ADB_INTR_IS_ON) { /* ADB intr low during write */
1178 ADB_SET_STATE_IDLE_IISI(); /* reset */
1179 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1180 adbSentChars = 0; /* must start all over */
1181 adbActionState = ADB_ACTION_IDLE; /* new state */
1182 adbInputBuffer[0] = 0;
1183 adbWriteDelay = 1; /* must retry when done with
1184 * read */
1185 delay(ADB_DELAY); /* delay */
1186 zshard(0); /* grab any serial interrupts */
1187 goto switch_start; /* process next state right
1188 * now */
1189 break;
1190 }
1191 delay(ADB_DELAY); /* required delay */
1192 zshard(0); /* grab any serial interrupts */
1193
1194 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
1195 if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data
1196 * back? */
1197 adbWaiting = 1; /* signal waiting for return */
1198 adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */
1199 } else {/* no talk, so done */
1200 adb_comp_exec(); /* call completion
1201 * routine */
1202 adbWaitingCmd = 0; /* reset "waiting" vars,
1203 * just in case */
1204 adbBuffer = (long) 0;
1205 adbCompRout = (long) 0;
1206 adbCompData = (long) 0;
1207 }
1208
1209 adbWriteDelay = 0; /* done writing */
1210 adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */
1211 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1212 ADB_SET_STATE_INACTIVE(); /* end of frame */
1213 } else {
1214 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* send next byte */
1215 ADB_SET_STATE_ACKON(); /* signal byte ready to shift */
1216 }
1217 break;
1218
1219 case ADB_ACTION_NOTREADY:
1220 printf_intr("adb: not yet initialized\n");
1221 break;
1222
1223 default:
1224 printf_intr("intr: unknown ADB state\n");
1225 }
1226
1227 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
1228
1229 splx(s); /* restore */
1230
1231 return;
1232 } /* end adb_intr_IIsi */
1233
1234
1235 /*****************************************************************************
1236 * if the device is currently busy, and there is no data waiting to go out, then
1237 * the data is "queued" in the outgoing buffer. If we are already waiting, then
1238 * we return.
1239 * in: if (in==0) then the command string is built from command and buffer
1240 * if (in!=0) then in is used as the command string
1241 * buffer: additional data to be sent (used only if in==0)
1242 * this is also where return data is stored
1243 * compRout: the completion routine that is called when then return value
1244 * is received (if a return value is expected)
1245 * data: a data pointer that can be used by the completion routine
1246 * command: an ADB command to be sent (used only if in==0)
1247 *
1248 */
1249 int
1250 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
1251 command)
1252 {
1253 int i, s, len;
1254
1255 if (adbActionState == ADB_ACTION_NOTREADY)
1256 return 1;
1257
1258 s = splhigh(); /* don't interrupt while we are messing with
1259 * the ADB */
1260
1261 if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */
1262 (ADB_INTR_IS_OFF)) {/* and no incoming interrupt? */
1263
1264 } else
1265 if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */
1266 adbWriteDelay = 1; /* if no, then we'll "queue"
1267 * it up */
1268 else {
1269 splx(s);
1270 return 1; /* really busy! */
1271 }
1272
1273 if ((long) in == (long) 0) { /* need to convert? */
1274 /* don't need to use adb_cmd_extra here because this section
1275 * will be called */
1276 /* ONLY when it is an ADB command (no RTC or PRAM) */
1277 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
1278 * doing a listen! */
1279 len = buffer[0]; /* length of additional data */
1280 else
1281 len = 0;/* no additional data */
1282
1283 adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl.
1284 * data */
1285 adbOutputBuffer[1] = 0x00; /* mark as an ADB command */
1286 adbOutputBuffer[2] = (u_char) command; /* load command */
1287
1288 for (i = 1; i <= len; i++) /* copy additional output
1289 * data, if any */
1290 adbOutputBuffer[2 + i] = buffer[i];
1291 } else
1292 for (i = 0; i <= (adbOutputBuffer[0] + 1); i++)
1293 adbOutputBuffer[i] = in[i];
1294
1295 adbSentChars = 0; /* nothing sent yet */
1296 adbBuffer = buffer; /* save buffer to know where to save result */
1297 adbCompRout = compRout; /* save completion routine pointer */
1298 adbCompData = data; /* save completion routine data pointer */
1299 adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */
1300
1301 if (adbWriteDelay != 1) { /* start command now? */
1302 adbActionState = ADB_ACTION_OUT; /* set next state */
1303
1304 ADB_SET_STATE_ACTIVE(); /* tell ADB that we want to send */
1305 ADB_SET_STATE_ACKOFF(); /* make sure */
1306
1307 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
1308
1309 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* load byte for output */
1310
1311 ADB_SET_STATE_ACKON(); /* tell ADB byte ready to shift */
1312 }
1313 adbWriteDelay = 1; /* something in the write "queue" */
1314
1315 splx(s);
1316
1317 if (0x0100 <= (s & 0x0700)) /* were VIA1 interrupts blocked ? */
1318 /* poll until byte done */
1319 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1320 || (adbWaiting == 1))
1321 if (ADB_SR_INTR_IS_ON) /* wait for "interrupt" */
1322 adb_intr_IIsi(); /* go process
1323 * "interrupt" */
1324
1325 return 0;
1326 } /* send_adb_IIsi */
1327
1328
1329 /*
1330 * adb_comp_exec
1331 * This is a general routine that calls the completion routine if there is one.
1332 */
1333 void
1334 adb_comp_exec(void)
1335 {
1336 if ((long) 0 != adbCompRout) /* don't call if empty return location */
1337 #ifdef __NetBSD__
1338 asm("
1339 movml #0xffff, sp@- | save all registers
1340 movl %0, a2 | adbCompData
1341 movl %1, a1 | adbCompRout
1342 movl %2, a0 | adbBuffer
1343 movl %3, d0 | adbWaitingCmd
1344 jbsr a1@ | go call the routine
1345 movml sp@+, #0xffff | restore all registers"
1346 :
1347 :"g"(adbCompData), "g"(adbCompRout),
1348 "g"(adbBuffer), "g"(adbWaitingCmd)
1349 :"d0", "a0", "a1", "a2");
1350 #else /* for macos based testing */
1351 asm {
1352 movem.l a0/a1/a2/d0, -(a7)
1353 move.l adbCompData, a2
1354 move.l adbCompRout, a1
1355 move.l adbBuffer, a0
1356 move.w adbWaitingCmd, d0
1357 jsr(a1)
1358 movem.l(a7) +, d0/a2/a1/a0
1359 }
1360 #endif
1361 }
1362
1363
1364 /*
1365 * This routine handles what needs to be done after an unsolicited
1366 * message is read from the ADB device. 'in' points to the raw
1367 * data received from the device, including device number
1368 * (on IIsi) and result code.
1369 *
1370 * Note that the service (completion) routine for an unsolicited
1371 * message is whatever is set in the ADB device table. This is
1372 * different than for a device responding to a specific request,
1373 * where the completion routine is defined by the caller.
1374 */
1375 void
1376 adb_handle_unsol(u_char * in)
1377 {
1378 int i, cmd = 0;
1379 u_char data[MAX_ADB_MSG_LENGTH];
1380 u_char *buffer = 0;
1381 ADBDataBlock block;
1382
1383 /* make local copy so we don't destroy the real one - it may be needed
1384 * later. */
1385 for (i = 0; i <= (in[0] + 1); i++)
1386 data[i] = in[i];
1387
1388 switch (adbHardware) {
1389 case ADB_HW_II:
1390 /* adjust the "length" byte */
1391 cmd = data[1];
1392 if (data[0] < 2)
1393 data[1] = 0;
1394 else
1395 data[1] = data[0] - 1;
1396
1397 buffer = (data + 1);
1398 break;
1399
1400 case ADB_HW_IISI:
1401 case ADB_HW_CUDA:
1402 /* only handles ADB for now */
1403 if (0 != *(data + 2))
1404 return;
1405
1406 /* adjust the "length" byte */
1407 cmd = data[4];
1408 if (data[0] < 5)
1409 data[4] = 0;
1410 else
1411 data[4] = data[0] - 4;
1412
1413 buffer = (data + 4);
1414 break;
1415
1416 case ADB_HW_PB:
1417 return; /* how does PM handle "unsolicited" messages? */
1418
1419 case ADB_HW_UNKNOWN:
1420 return;
1421 }
1422
1423 if (-1 == get_adb_info(&block, ((cmd & 0xf0) >> 4)))
1424 return;
1425
1426 /* call default completion routine if it's valid */
1427 /* TO DO: This section of code is somewhat redundant with
1428 * adb_comp_exec (above). Some day we may want to generalize it and
1429 * make it a single function. */
1430 if ((long) 0 != (long) block.dbServiceRtPtr) {
1431 #ifdef __NetBSD__
1432 asm("
1433 movml #0xffff, sp@- | save all registers
1434 movl %0, a2 | block.dbDataAreaAddr
1435 movl %1, a1 | block.dbServiceRtPtr
1436 movl %2, a0 | buffer
1437 movl %3, d0 | cmd
1438 jbsr a1@ | go call the routine
1439 movml sp@+, #0xffff | restore all registers"
1440 :
1441 : "g"(block.dbDataAreaAddr),
1442 "g"(block.dbServiceRtPtr), "g"(buffer), "g"(cmd)
1443 : "d0", "a0", "a1", "a2");
1444 #else /* for macos based testing */
1445 asm
1446 {
1447 movem.l a0/a1/a2/d0, -(a7)
1448 move.l block.dbDataAreaAddr, a2
1449 move.l block.dbServiceRtPtr, a1
1450 move.l buffer, a0
1451 move.w cmd, d0
1452 jsr(a1)
1453 movem.l(a7) +, d0/a2/a1/a0
1454 }
1455 #endif
1456 }
1457 return;
1458 }
1459
1460
1461 /*
1462 * This is my version of the ADBOp routine. It mainly just calls the hardware-specific
1463 * routine.
1464 *
1465 * data : pointer to data area to be used by compRout
1466 * compRout : completion routine
1467 * buffer : for LISTEN: points to data to send - MAX 8 data bytes,
1468 * byte 0 = # of bytes
1469 * : for TALK: points to place to save return data
1470 * command : the adb command to send
1471 * result : 0 = success
1472 * : -1 = could not complete
1473 */
1474 int
1475 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1476 {
1477 int result;
1478
1479 switch (adbHardware) {
1480 case ADB_HW_II:
1481 result = send_adb_II((u_char *) 0,
1482 (u_char *) buffer, (void *) compRout,
1483 (void *) data, (int) command);
1484 if (result == 0)
1485 return 0;
1486 else
1487 return -1;
1488 break;
1489
1490 case ADB_HW_IISI:
1491 result = send_adb_IIsi((u_char *) 0,
1492 (u_char *) buffer, (void *) compRout,
1493 (void *) data, (int) command);
1494 /*
1495 * I wish I knew why this delay is needed. It usually needs to
1496 * be here when several commands are sent in close succession,
1497 * especially early in device probes when doing collision
1498 * detection. It must be some race condition. Sigh. - jpw
1499 */
1500 delay(100);
1501 if (result == 0)
1502 return 0;
1503 else
1504 return -1;
1505 break;
1506
1507 case ADB_HW_PB:
1508 result = pm_adb_op((u_char *)buffer, (void *)compRout,
1509 (void *)data, (int)command);
1510
1511 if (result == 0)
1512 return 0;
1513 else
1514 return -1;
1515 break;
1516
1517 case ADB_HW_CUDA:
1518 result = send_adb_cuda((u_char *) 0,
1519 (u_char *) buffer, (void *) compRout,
1520 (void *) data, (int) command);
1521 if (result == 0)
1522 return 0;
1523 else
1524 return -1;
1525 break;
1526
1527 case ADB_HW_UNKNOWN:
1528 default:
1529 return -1;
1530 }
1531 }
1532
1533
1534 /*
1535 * adb_cleanup
1536 * This routine simply calls the appropriate version of the adb_cleanup routine.
1537 */
1538 void
1539 adb_cleanup(u_char * in)
1540 {
1541 volatile int i;
1542
1543 switch (adbHardware) {
1544 case ADB_HW_II:
1545 ADB_VIA_CLR_INTR(); /* clear interrupt */
1546 break;
1547
1548 case ADB_HW_IISI:
1549 /* get those pesky clock ticks we missed while booting */
1550 adb_cleanup_IIsi(in);
1551 break;
1552
1553 case ADB_HW_PB:
1554 /*
1555 * XXX - really PM_VIA_CLR_INTR - should we put it in
1556 * pm_direct.h?
1557 */
1558 via_reg(VIA1, vIFR) = 0x90; /* clear interrupt */
1559 break;
1560
1561 case ADB_HW_CUDA:
1562 i = ADB_SR(); /* clear interrupt */
1563 ADB_VIA_INTR_DISABLE(); /* no interrupts while clearing */
1564 ADB_SET_STATE_IDLE_CUDA(); /* reset state to idle */
1565 delay(ADB_DELAY);
1566 ADB_SET_STATE_TIP(); /* signal start of frame */
1567 delay(ADB_DELAY);
1568 ADB_TOGGLE_STATE_ACK_CUDA();
1569 delay(ADB_DELAY);
1570 ADB_CLR_STATE_TIP();
1571 delay(ADB_DELAY);
1572 ADB_SET_STATE_IDLE_CUDA(); /* back to idle state */
1573 i = ADB_SR(); /* clear interrupt */
1574 ADB_VIA_INTR_ENABLE(); /* ints ok now */
1575 break;
1576
1577 case ADB_HW_UNKNOWN:
1578 return;
1579 }
1580 }
1581
1582
1583 /*
1584 * adb_cleanup_IIsi
1585 * This is sort of a "read" routine that forces the adb hardware through a read cycle
1586 * if there is something waiting. This helps "clean up" any commands that may have gotten
1587 * stuck or stopped during the boot process.
1588 *
1589 */
1590 void
1591 adb_cleanup_IIsi(u_char * buffer)
1592 {
1593 int i;
1594 int dummy;
1595 int s;
1596 long my_time;
1597 int endofframe;
1598
1599 delay(ADB_DELAY);
1600
1601 i = 1; /* skip over [0] */
1602 s = splhigh(); /* block ALL interrupts while we are working */
1603 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1604 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
1605 /* this is required, especially on faster machines */
1606 delay(ADB_DELAY);
1607
1608 if (ADB_INTR_IS_ON) {
1609 ADB_SET_STATE_ACTIVE(); /* signal start of data frame */
1610
1611 endofframe = 0;
1612 while (0 == endofframe) {
1613 /* poll for ADB interrupt and watch for timeout */
1614 /* if time out, keep going in hopes of not hanging the
1615 * ADB chip - I think */
1616 my_time = ADB_DELAY * 5;
1617 while ((ADB_SR_INTR_IS_OFF) && (my_time-- > 0))
1618 dummy = via_reg(VIA1, vBufB);
1619
1620 buffer[i++] = ADB_SR(); /* reset interrupt flag by
1621 * reading vSR */
1622 /* perhaps put in a check here that ignores all data
1623 * after the first MAX_ADB_MSG_LENGTH bytes ??? */
1624 if (ADB_INTR_IS_OFF) /* check for end of frame */
1625 endofframe = 1;
1626
1627 ADB_SET_STATE_ACKON(); /* send ACK to ADB chip */
1628 delay(ADB_DELAY); /* delay */
1629 ADB_SET_STATE_ACKOFF(); /* send ACK to ADB chip */
1630 }
1631 ADB_SET_STATE_INACTIVE(); /* signal end of frame and
1632 * delay */
1633
1634 /* probably don't need to delay this long */
1635 delay(ADB_DELAY);
1636 }
1637 buffer[0] = --i; /* [0] is length of message */
1638 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
1639 splx(s); /* restore interrupts */
1640
1641 return;
1642 } /* adb_cleanup_IIsi */
1643
1644
1645
1646 /*
1647 * adb_reinit sets up the adb stuff
1648 *
1649 */
1650 void
1651 adb_reinit(void)
1652 {
1653 u_char send_string[MAX_ADB_MSG_LENGTH];
1654 int s = 0;
1655 volatile int i, x;
1656 int command;
1657 int result;
1658 int saveptr; /* point to next free relocation address */
1659 int device;
1660 int nonewtimes; /* times thru loop w/o any new devices */
1661 ADBDataBlock data; /* temp. holder for getting device info */
1662
1663 (void)(&s); /* work around lame GCC bug */
1664
1665 /* Make sure we are not interrupted while building the table. */
1666 if (adbHardware != ADB_HW_PB) /* ints must be on for PB? */
1667 s = splhigh();
1668
1669 ADBNumDevices = 0; /* no devices yet */
1670
1671 /* Let intr routines know we are running reinit */
1672 adbStarting = 1;
1673
1674 /* Initialize the ADB table. For now, we'll always use the same table
1675 * that is defined at the beginning of this file - no mallocs. */
1676 for (i = 0; i < 16; i++)
1677 ADBDevTable[i].devType = 0;
1678
1679 adb_setup_hw_type(); /* setup hardware type */
1680
1681 /* Set up all the VIA bits we need to do the ADB stuff. */
1682 switch (adbHardware) {
1683 case ADB_HW_II:
1684 via_reg(VIA1, vDirB) |= 0x30; /* register B bits 4 and 5:
1685 * outputs */
1686 via_reg(VIA1, vDirB) &= 0xf7; /* register B bit 3: input */
1687 via_reg(VIA1, vACR) &= ~vSR_OUT; /* make sure SR is set
1688 * to IN (II, IIsi) */
1689 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1690 * hardware (II, IIsi) */
1691 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1692 * code only */
1693 via_reg(VIA1, vIER) = 0x84; /* make sure VIA interrupts
1694 * are on (II, IIsi) */
1695 ADB_SET_STATE_IDLE_II(); /* set ADB bus state to idle */
1696 break;
1697
1698 case ADB_HW_IISI:
1699 via_reg(VIA1, vDirB) |= 0x30; /* register B bits 4 and 5:
1700 * outputs */
1701 via_reg(VIA1, vDirB) &= 0xf7; /* register B bit 3: input */
1702 via_reg(VIA1, vACR) &= ~vSR_OUT; /* make sure SR is set
1703 * to IN (II, IIsi) */
1704 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1705 * hardware (II, IIsi) */
1706 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1707 * code only */
1708 via_reg(VIA1, vIER) = 0x84; /* make sure VIA interrupts
1709 * are on (II, IIsi) */
1710 ADB_SET_STATE_IDLE_IISI(); /* set ADB bus state to idle */
1711 break;
1712
1713 case ADB_HW_PB:
1714 break; /* there has to be more than this? */
1715
1716 case ADB_HW_CUDA:
1717 via_reg(VIA1, vDirB) |= 0x30; /* register B bits 4 and 5:
1718 * outputs */
1719 via_reg(VIA1, vDirB) &= 0xf7; /* register B bit 3: input */
1720 via_reg(VIA1, vACR) &= ~vSR_OUT; /* make sure SR is set
1721 * to IN */
1722 via_reg(VIA1, vACR) = (via_reg(VIA1, vACR) | 0x0c) & ~0x10;
1723 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1724 * hardware */
1725 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1726 * code only */
1727 via_reg(VIA1, vIER) = 0x84; /* make sure VIA interrupts
1728 * are on */
1729 ADB_SET_STATE_IDLE_CUDA(); /* set ADB bus state to idle */
1730 break;
1731
1732 case ADB_HW_UNKNOWN: /* if type unknown then skip out */
1733 default:
1734 via_reg(VIA1, vIER) = 0x04; /* turn interrupts off - TO
1735 * DO: turn PB ints off? */
1736 return;
1737 break;
1738 }
1739
1740 /*
1741 * Clear out any "leftover" commands. Remember that up until this
1742 * point, the interrupt routine will be either off or it should be
1743 * able to ignore inputs until the device table is built.
1744 */
1745 for (i = 0; i < 30; i++) {
1746 delay(ADB_DELAY);
1747 adb_cleanup(send_string);
1748 printf_intr("adb: cleanup: ");
1749 print_single(send_string);
1750 delay(ADB_DELAY);
1751 if (ADB_INTR_IS_OFF)
1752 break;
1753 }
1754
1755 /* send an ADB reset first */
1756 adb_op_sync((Ptr) 0, (Ptr) 0, (Ptr) 0, (short) 0x00);
1757
1758 /* Probe for ADB devices. Probe devices 1-15 quickly to determine
1759 * which device addresses are in use and which are free. For each
1760 * address that is in use, move the device at that address to a higher
1761 * free address. Continue doing this at that address until no device
1762 * responds at that address. Then move the last device that was moved
1763 * back to the original address. Do this for the remaining addresses
1764 * that we determined were in use.
1765 *
1766 * When finished, do this entire process over again with the updated list
1767 * of in use addresses. Do this until no new devices have been found
1768 * in 20 passes though the in use address list. (This probably seems
1769 * long and complicated, but it's the best way to detect multiple
1770 * devices at the same address - sometimes it takes a couple of tries
1771 * before the collision is detected.) */
1772
1773 /* initial scan through the devices */
1774 for (i = 1; i < 16; i++) {
1775 command = (int) (0x0f | ((int) (i & 0x000f) << 4)); /* talk R3 */
1776 result = adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
1777 if (0x00 != send_string[0]) { /* anything come back ?? */
1778 ADBDevTable[++ADBNumDevices].devType = (u_char) send_string[2];
1779 ADBDevTable[ADBNumDevices].origAddr = i;
1780 ADBDevTable[ADBNumDevices].currentAddr = i;
1781 ADBDevTable[ADBNumDevices].DataAreaAddr = (long) 0;
1782 ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *) 0;
1783 /* printf_intr("initial device found (at index %i)\n",
1784 * ADBNumDevices); */
1785 pm_check_adb_devices(i); /* tell pm driver device
1786 * is here */
1787 }
1788 }
1789
1790 /* find highest unused address */
1791 for (saveptr = 15; saveptr > 0; saveptr--)
1792 if (-1 == get_adb_info(&data, saveptr))
1793 break;
1794
1795 if (saveptr == 0) /* no free addresses??? */
1796 saveptr = 15;
1797
1798 /* printf_intr("first free is: 0x%02x\n", saveptr); */
1799 /* printf_intr("devices: %i\n", ADBNumDevices); */
1800
1801 nonewtimes = 0; /* no loops w/o new devices */
1802 while (nonewtimes++ < 11) {
1803 for (i = 1; i <= ADBNumDevices; i++) {
1804 device = ADBDevTable[i].currentAddr;
1805 /* printf_intr("moving device 0x%02x to 0x%02x (index
1806 * 0x%02x) ", device, saveptr, i); */
1807
1808 /* send TALK R3 to address */
1809 command = (int) (0x0f | ((int) (device & 0x000f) << 4));
1810 adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
1811
1812 /* move device to higher address */
1813 command = (int) (0x0b | ((int) (device & 0x000f) << 4));
1814 send_string[0] = 2;
1815 send_string[1] = (u_char) (saveptr | 0x60);
1816 send_string[2] = 0xfe;
1817 adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
1818
1819 /* send TALK R3 - anything at old address? */
1820 command = (int) (0x0f | ((int) (device & 0x000f) << 4));
1821 result = adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
1822 if (send_string[0] != 0) {
1823 /* new device found */
1824 /* update data for previously moved device */
1825 ADBDevTable[i].currentAddr = saveptr;
1826 /* printf_intr("old device at index %i\n",i); */
1827 /* add new device in table */
1828 /* printf_intr("new device found\n"); */
1829 ADBDevTable[++ADBNumDevices].devType = (u_char) send_string[2];
1830 ADBDevTable[ADBNumDevices].origAddr = device;
1831 ADBDevTable[ADBNumDevices].currentAddr = device;
1832 /* These will be set correctly in adbsys.c */
1833 /* Until then, unsol. data will be ignored. */
1834 ADBDevTable[ADBNumDevices].DataAreaAddr = (long) 0;
1835 ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *) 0;
1836 /* find next unused address */
1837 for (x = saveptr; x > 0; x--)
1838 if (-1 == get_adb_info(&data, x)) {
1839 saveptr = x;
1840 break;
1841 }
1842 /* printf_intr("new free is 0x%02x\n",
1843 * saveptr); */
1844 nonewtimes = 0;
1845 /* tell pm driver device is here */
1846 pm_check_adb_devices(device);
1847 } else {
1848 /* printf_intr("moving back...\n"); */
1849 /* move old device back */
1850 command = (int) (0x0b | ((int) (saveptr & 0x000f) << 4));
1851 send_string[0] = 2;
1852 send_string[1] = (u_char) (device | 0x60);
1853 send_string[2] = 0xfe;
1854 adb_op_sync((Ptr) send_string, (Ptr) 0, (Ptr) 0, (short) command);
1855 }
1856 }
1857 }
1858
1859 #ifdef DEBUG
1860 for (i = 1; i <= ADBNumDevices; i++) {
1861 x = get_ind_adb_info(&data, i);
1862 if (x != -1)
1863 printf_intr("index 0x%x, addr 0x%x, type 0x%x\n", i, x, data.devType);
1864
1865 }
1866 #endif
1867
1868 adb_prog_switch_enable(); /* enable the programmer's switch, if
1869 * we have one */
1870
1871 if (0 == ADBNumDevices) /* tell user if no devices found */
1872 printf_intr("adb: no devices found\n");
1873
1874 adbStarting = 0; /* not starting anymore */
1875 printf_intr("adb: ADBReInit complete\n");
1876
1877 if (adbHardware != ADB_HW_PB) /* ints must be on for PB? */
1878 splx(s);
1879 return;
1880 }
1881
1882
1883 /* adb_cmd_result
1884 * This routine lets the caller know whether the specified adb command string should
1885 * expect a returned result, such as a TALK command.
1886 * returns: 0 if a result should be expected
1887 * 1 if a result should NOT be expected
1888 */
1889 int
1890 adb_cmd_result(u_char * in)
1891 {
1892 switch (adbHardware) {
1893 case ADB_HW_II:
1894 /* was it an ADB talk command? */
1895 if ((in[1] & 0x0c) == 0x0c)
1896 return 0;
1897 else
1898 return 1;
1899 break;
1900
1901 case ADB_HW_IISI:
1902 case ADB_HW_CUDA:
1903 /* was is an ADB talk command? */
1904 if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
1905 return 0;
1906 /* was is an RTC/PRAM read date/time? */
1907 else
1908 if ((in[1] == 0x01) && (in[2] == 0x03))
1909 return 0;
1910 else
1911 return 1;
1912 break;
1913
1914 case ADB_HW_PB:
1915 return 1;
1916 break;
1917
1918 case ADB_HW_UNKNOWN:
1919 default:
1920 return 1;
1921 }
1922 }
1923
1924
1925 /* adb_cmd_extra
1926 * This routine lets the caller know whether the specified adb command string may have
1927 * extra data appended to the end of it, such as a LISTEN command.
1928 * returns: 0 if extra data is allowed
1929 * 1 if extra data is NOT allowed
1930 */
1931 int
1932 adb_cmd_extra(u_char * in)
1933 {
1934 switch (adbHardware) {
1935 case ADB_HW_II:
1936 if ((in[1] & 0x0c) == 0x08) /* was it a listen command? */
1937 return 0;
1938 else
1939 return 1;
1940 break;
1941
1942 case ADB_HW_IISI:
1943 case ADB_HW_CUDA:
1944 /* TO DO: support needs to be added to recognize RTC and PRAM
1945 * commands */
1946 if ((in[2] & 0x0c) == 0x08) /* was it a listen command? */
1947 return 0;
1948 else /* add others later */
1949 return 1;
1950 break;
1951
1952 case ADB_HW_PB:
1953 return 1;
1954 break;
1955
1956 case ADB_HW_UNKNOWN:
1957 default:
1958 return 1;
1959 }
1960 }
1961
1962
1963 /* adb_op_sync
1964 * This routine does exactly what the adb_op routine does, except that after the
1965 * adb_op is called, it waits until the return value is present before returning
1966 */
1967 int
1968 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
1969 {
1970 int result;
1971 volatile int flag = 0;
1972
1973 result = adb_op(buffer, (void *) adb_op_comprout,
1974 (void *) &flag, command); /* send command */
1975 if (result == 0) { /* send ok? */
1976 while (0 == flag); /* wait for compl. routine */
1977 return 0;
1978 } else
1979 return result;
1980 }
1981
1982
1983 /* adb_op_comprout
1984 * This function is used by the adb_op_sync routine so it knows when the function is
1985 * done.
1986 */
1987 void
1988 adb_op_comprout(void)
1989 {
1990 #ifdef __NetBSD__
1991 asm("movw #1,a2@ | update flag value");
1992 #else /* for macos based testing */
1993 asm {
1994 move.w #1,(a2) } /* update flag value */
1995 #endif
1996 }
1997
1998 void
1999 adb_setup_hw_type(void)
2000 {
2001 long response;
2002
2003 response = mac68k_machine.machineid;
2004
2005 switch (response) {
2006 case 6: /* II */
2007 case 7: /* IIx */
2008 case 8: /* IIcx */
2009 case 9: /* SE/30 */
2010 case 11: /* IIci */
2011 case 22: /* Quadra 700 */
2012 case 30: /* Centris 650 */
2013 case 35: /* Quadra 800 */
2014 case 36: /* Quadra 650 */
2015 case 52: /* Centris 610 */
2016 case 53: /* Quadra 610 */
2017 adbHardware = ADB_HW_II;
2018 printf_intr("adb: using II series hardware support\n");
2019 break;
2020 case 18: /* IIsi */
2021 case 20: /* Quadra 900 - not sure if IIsi or not */
2022 case 23: /* Classic II */
2023 case 26: /* Quadra 950 - not sure if IIsi or not */
2024 case 27: /* LC III, Performa 450 */
2025 case 37: /* LC II, Performa 400/405/430 */
2026 case 44: /* IIvi */
2027 case 45: /* Performa 600 */
2028 case 48: /* IIvx */
2029 case 49: /* Color Classic - not sure if IIsi or not */
2030 case 62: /* Performa 460/465/467 */
2031 case 83: /* Color Classic II - not sure if IIsi or not */
2032 adbHardware = ADB_HW_IISI;
2033 printf_intr("adb: using IIsi series hardware support\n");
2034 break;
2035 case 21: /* PowerBook 170 */
2036 case 25: /* PowerBook 140 */
2037 case 54: /* PowerBook 145 */
2038 case 34: /* PowerBook 160 */
2039 case 84: /* PowerBook 165 */
2040 case 50: /* PowerBook 165c */
2041 case 33: /* PowerBook 180 */
2042 case 71: /* PowerBook 180c */
2043 case 115: /* PowerBook 150 */
2044 adbHardware = ADB_HW_PB;
2045 pm_setup_adb();
2046 printf_intr("adb: using PowerBook 100-series hardware support\n");
2047 break;
2048 case 29: /* PowerBook Duo 210 */
2049 case 32: /* PowerBook Duo 230 */
2050 case 38: /* PowerBook Duo 250 */
2051 case 72: /* PowerBook 500 series */
2052 case 77: /* PowerBook Duo 270 */
2053 case 102: /* PowerBook Duo 280 */
2054 case 103: /* PowerBook Duo 280c */
2055 adbHardware = ADB_HW_PB;
2056 pm_setup_adb();
2057 printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2058 break;
2059 case 56: /* LC 520 */
2060 case 60: /* Centris 660AV */
2061 case 78: /* Quadra 840AV */
2062 case 80: /* LC 550, Performa 550 */
2063 case 89: /* LC 475, Performa 475/476 */
2064 case 92: /* LC 575, Performa 575/577/578 */
2065 case 94: /* Quadra 605 */
2066 case 98: /* LC 630, Performa 630, Quadra 630 */
2067 adbHardware = ADB_HW_CUDA;
2068 printf_intr("adb: using Cuda series hardware support\n");
2069 break;
2070 default:
2071 adbHardware = ADB_HW_UNKNOWN;
2072 printf_intr("adb: hardware type unknown for this machine\n");
2073 printf_intr("adb: ADB support is disabled\n");
2074 break;
2075 }
2076 }
2077
2078 int
2079 count_adbs(void)
2080 {
2081 int i;
2082 int found;
2083
2084 found = 0;
2085
2086 for (i = 1; i < 16; i++)
2087 if (0 != ADBDevTable[i].devType)
2088 found++;
2089
2090 return found;
2091 }
2092
2093 int
2094 get_ind_adb_info(ADBDataBlock * info, int index)
2095 {
2096 if ((index < 1) || (index > 15)) /* check range 1-15 */
2097 return (-1);
2098
2099 /* printf_intr("index 0x%x devType is: 0x%x\n", index,
2100 ADBDevTable[index].devType); */
2101 if (0 == ADBDevTable[index].devType) /* make sure it's a valid entry */
2102 return (-1);
2103
2104 info->devType = ADBDevTable[index].devType;
2105 info->origADBAddr = ADBDevTable[index].origAddr;
2106 info->dbServiceRtPtr = (Ptr) ADBDevTable[index].ServiceRtPtr;
2107 info->dbDataAreaAddr = (Ptr) ADBDevTable[index].DataAreaAddr;
2108
2109 return (ADBDevTable[index].currentAddr);
2110 }
2111
2112 int
2113 get_adb_info(ADBDataBlock * info, int adbAddr)
2114 {
2115 int i;
2116
2117 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
2118 return (-1);
2119
2120 for (i = 1; i < 15; i++)
2121 if (ADBDevTable[i].currentAddr == adbAddr) {
2122 info->devType = ADBDevTable[i].devType;
2123 info->origADBAddr = ADBDevTable[i].origAddr;
2124 info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2125 info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2126 return 0; /* found */
2127 }
2128
2129 return (-1); /* not found */
2130 }
2131
2132 int
2133 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
2134 {
2135 int i;
2136
2137 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
2138 return (-1);
2139
2140 for (i = 1; i < 15; i++)
2141 if (ADBDevTable[i].currentAddr == adbAddr) {
2142 ADBDevTable[i].ServiceRtPtr =
2143 (void *)(info->siServiceRtPtr);
2144 ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2145 return 0; /* found */
2146 }
2147
2148 return (-1); /* not found */
2149
2150 }
2151
2152 #ifndef MRG_ADB
2153 long
2154 mrg_adbintr(void)
2155 {
2156 adb_intr();
2157 return 1; /* mimic mrg_adbintr in macrom.h just in case */
2158 }
2159
2160 long
2161 mrg_pmintr(void) /* we don't do this yet */
2162 {
2163 pm_intr();
2164 return 1; /* mimic mrg_pmintr in macrom.h just in case */
2165 }
2166 #endif
2167
2168 /* caller should really use machine-independant version: getPramTime */
2169 /* this version does pseudo-adb access only */
2170 int
2171 adb_read_date_time(unsigned long *time)
2172 {
2173 u_char output[MAX_ADB_MSG_LENGTH];
2174 int result;
2175 volatile int flag = 0;
2176
2177 switch (adbHardware) {
2178 case ADB_HW_II:
2179 return -1;
2180
2181 case ADB_HW_IISI:
2182 output[0] = 0x02; /* 2 byte message */
2183 output[1] = 0x01; /* to pram/rtc device */
2184 output[2] = 0x03; /* read date/time */
2185 result = send_adb_IIsi((u_char *) output,
2186 (u_char *) output, (void *) adb_op_comprout,
2187 (int *) &flag, (int) 0);
2188 if (result != 0) /* exit if not sent */
2189 return -1;
2190
2191 while (0 == flag) /* wait for result */
2192 ;
2193
2194 *time = (long) (*(long *) (output + 1));
2195 return 0;
2196
2197 case ADB_HW_PB:
2198 return -1;
2199
2200 case ADB_HW_CUDA:
2201 output[0] = 0x02; /* 2 byte message */
2202 output[1] = 0x01; /* to pram/rtc device */
2203 output[2] = 0x03; /* read date/time */
2204 result = send_adb_cuda((u_char *) output,
2205 (u_char *) output, (void *) adb_op_comprout,
2206 (void *) &flag, (int) 0);
2207 if (result != 0) /* exit if not sent */
2208 return -1;
2209
2210 while (0 == flag) /* wait for result */
2211 ;
2212
2213 *time = (long) (*(long *) (output + 1));
2214 return 0;
2215
2216 case ADB_HW_UNKNOWN:
2217 default:
2218 return -1;
2219 }
2220 }
2221
2222 /* caller should really use machine-independant version: setPramTime */
2223 /* this version does pseudo-adb access only */
2224 int
2225 adb_set_date_time(unsigned long time)
2226 {
2227 u_char output[MAX_ADB_MSG_LENGTH];
2228 int result;
2229 volatile int flag = 0;
2230
2231 switch (adbHardware) {
2232 case ADB_HW_II:
2233 return -1;
2234
2235 case ADB_HW_IISI:
2236 output[0] = 0x06; /* 6 byte message */
2237 output[1] = 0x01; /* to pram/rtc device */
2238 output[2] = 0x09; /* set date/time */
2239 output[3] = (u_char) (time >> 24);
2240 output[4] = (u_char) (time >> 16);
2241 output[5] = (u_char) (time >> 8);
2242 output[6] = (u_char) (time);
2243 result = send_adb_IIsi((u_char *) output,
2244 (u_char *) 0, (void *) adb_op_comprout,
2245 (void *) &flag, (int) 0);
2246 if (result != 0) /* exit if not sent */
2247 return -1;
2248
2249 while (0 == flag) /* wait for send to finish */
2250 ;
2251
2252 return 0;
2253
2254 case ADB_HW_PB:
2255 return -1;
2256
2257 case ADB_HW_CUDA:
2258 output[0] = 0x06; /* 6 byte message */
2259 output[1] = 0x01; /* to pram/rtc device */
2260 output[2] = 0x09; /* set date/time */
2261 output[3] = (u_char) (time >> 24);
2262 output[4] = (u_char) (time >> 16);
2263 output[5] = (u_char) (time >> 8);
2264 output[6] = (u_char) (time);
2265 result = send_adb_cuda((u_char *) output,
2266 (u_char *) 0, (void *) adb_op_comprout,
2267 (void *) &flag, (int) 0);
2268 if (result != 0) /* exit if not sent */
2269 return -1;
2270
2271 while (0 == flag) /* wait for send to finish */
2272 ;
2273
2274 return 0;
2275
2276 case ADB_HW_UNKNOWN:
2277 default:
2278 return -1;
2279 }
2280 }
2281
2282
2283 int
2284 adb_poweroff(void)
2285 {
2286 u_char output[MAX_ADB_MSG_LENGTH];
2287 int result;
2288
2289 switch (adbHardware) {
2290 case ADB_HW_IISI:
2291 output[0] = 0x02; /* 2 byte message */
2292 output[1] = 0x01; /* to pram/rtc/soft-power device */
2293 output[2] = 0x0a; /* set date/time */
2294 result = send_adb_IIsi((u_char *) output,
2295 (u_char *) 0, (void *) 0, (void *) 0, (int) 0);
2296 if (result != 0) /* exit if not sent */
2297 return -1;
2298
2299 for (;;); /* wait for power off */
2300
2301 return 0;
2302
2303 case ADB_HW_PB:
2304 return -1;
2305
2306 /* TO DO: some cuda models claim to do soft power - check out */
2307 case ADB_HW_II: /* II models don't do soft power */
2308 case ADB_HW_CUDA: /* cuda doesn't do soft power */
2309 case ADB_HW_UNKNOWN:
2310 default:
2311 return -1;
2312 }
2313 }
2314
2315 int
2316 adb_prog_switch_enable(void)
2317 {
2318 u_char output[MAX_ADB_MSG_LENGTH];
2319 int result;
2320 volatile int flag = 0;
2321
2322 switch (adbHardware) {
2323 case ADB_HW_IISI:
2324 output[0] = 0x03; /* 3 byte message */
2325 output[1] = 0x01; /* to pram/rtc/soft-power device */
2326 output[2] = 0x1c; /* prog. switch control */
2327 output[3] = 0x01; /* enable */
2328 result = send_adb_IIsi((u_char *) output,
2329 (u_char *) 0, (void *) adb_op_comprout,
2330 (void *) &flag, (int) 0);
2331 if (result != 0) /* exit if not sent */
2332 return -1;
2333
2334 while (0 == flag) /* wait for send to finish */
2335 ;
2336
2337 return 0;
2338
2339 case ADB_HW_PB:
2340 return -1;
2341
2342 case ADB_HW_II: /* II models don't do prog. switch */
2343 case ADB_HW_CUDA: /* cuda doesn't do prog. switch TO DO: verify this */
2344 case ADB_HW_UNKNOWN:
2345 default:
2346 return -1;
2347 }
2348 }
2349
2350 int
2351 adb_prog_switch_disable(void)
2352 {
2353 u_char output[MAX_ADB_MSG_LENGTH];
2354 int result;
2355 volatile int flag = 0;
2356
2357 switch (adbHardware) {
2358 case ADB_HW_IISI:
2359 output[0] = 0x03; /* 3 byte message */
2360 output[1] = 0x01; /* to pram/rtc/soft-power device */
2361 output[2] = 0x1c; /* prog. switch control */
2362 output[3] = 0x01; /* disable */
2363 result = send_adb_IIsi((u_char *) output,
2364 (u_char *) 0, (void *) adb_op_comprout,
2365 (void *) &flag, (int) 0);
2366 if (result != 0) /* exit if not sent */
2367 return -1;
2368
2369 while (0 == flag) /* wait for send to finish */
2370 ;
2371
2372 return 0;
2373
2374 case ADB_HW_PB:
2375 return -1;
2376
2377 case ADB_HW_II: /* II models don't do prog. switch */
2378 case ADB_HW_CUDA: /* cuda doesn't do prog. switch */
2379 case ADB_HW_UNKNOWN:
2380 default:
2381 return -1;
2382 }
2383 }
2384
2385 #ifndef MRG_ADB
2386
2387 int
2388 CountADBs(void)
2389 {
2390 return (count_adbs());
2391 }
2392
2393 void
2394 ADBReInit(void)
2395 {
2396 adb_reinit();
2397 }
2398
2399 int
2400 GetIndADB(ADBDataBlock * info, int index)
2401 {
2402 return (get_ind_adb_info(info, index));
2403 }
2404
2405 int
2406 GetADBInfo(ADBDataBlock * info, int adbAddr)
2407 {
2408 return (get_adb_info(info, adbAddr));
2409 }
2410
2411 int
2412 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2413 {
2414 return (set_adb_info(info, adbAddr));
2415 }
2416
2417 int
2418 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2419 {
2420 return (adb_op(buffer, compRout, data, commandNum));
2421 }
2422
2423 #endif
2424
2425