adb_direct.c revision 1.24 1 /* $NetBSD: adb_direct.c,v 1.24 1999/06/28 01:56:56 briggs Exp $ */
2
3 /* From: adb_direct.c 2.02 4/18/97 jpw */
4
5 /*
6 * Copyright (C) 1996, 1997 John P. Wittkoski
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
11 * are met:
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. All advertising materials mentioning features or use of this software
18 * must display the following acknowledgement:
19 * This product includes software developed by John P. Wittkoski.
20 * 4. The name of the author may not be used to endorse or promote products
21 * derived from this software without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 */
34
35 /*
36 * This code is rather messy, but I don't have time right now
37 * to clean it up as much as I would like.
38 * But it works, so I'm happy. :-) jpw
39 */
40
41 /*
42 * TO DO:
43 * - We could reduce the time spent in the adb_intr_* routines
44 * by having them save the incoming and outgoing data directly
45 * in the adbInbound and adbOutbound queues, as it would reduce
46 * the number of times we need to copy the data around. It
47 * would also make the code more readable and easier to follow.
48 * - (Related to above) Use the header part of adbCommand to
49 * reduce the number of copies we have to do of the data.
50 * - (Related to above) Actually implement the adbOutbound queue.
51 * This is fairly easy once you switch all the intr routines
52 * over to using adbCommand structs directly.
53 * - There is a bug in the state machine of adb_intr_cuda
54 * code that causes hangs, especially on 030 machines, probably
55 * because of some timing issues. Because I have been unable to
56 * determine the exact cause of this bug, I used the timeout function
57 * to check for and recover from this condition. If anyone finds
58 * the actual cause of this bug, the calls to timeout and the
59 * adb_cuda_tickle routine can be removed.
60 */
61
62 #ifdef __NetBSD__
63 #include "opt_adb.h"
64
65 #include <sys/param.h>
66 #include <sys/cdefs.h>
67 #include <sys/systm.h>
68
69 #include <machine/viareg.h>
70 #include <machine/param.h>
71 #include <machine/cpu.h>
72 #include <machine/adbsys.h> /* required for adbvar.h */
73 #include <machine/iopreg.h> /* required for IOP support */
74
75 #include <mac68k/mac68k/macrom.h>
76 #include <mac68k/dev/adbvar.h>
77 #define printf_intr printf
78 #else /* !__NetBSD__, i.e. Mac OS */
79 #include "via.h" /* for macos based testing */
80 /* #define ADB_DEBUG */ /* more verbose for testing */
81
82 /* Types of ADB hardware that we support */
83 #define ADB_HW_UNKNOWN 0x0 /* don't know */
84 #define ADB_HW_II 0x1 /* Mac II series */
85 #define ADB_HW_IISI 0x2 /* Mac IIsi series */
86 #define ADB_HW_PB 0x3 /* PowerBook series */
87 #define ADB_HW_CUDA 0x4 /* Machines with a Cuda chip */
88 #endif /* __NetBSD__ */
89
90 /* some misc. leftovers */
91 #define vPB 0x0000
92 #define vPB3 0x08
93 #define vPB4 0x10
94 #define vPB5 0x20
95 #define vSR_INT 0x04
96 #define vSR_OUT 0x10
97
98 /* the type of ADB action that we are currently preforming */
99 #define ADB_ACTION_NOTREADY 0x1 /* has not been initialized yet */
100 #define ADB_ACTION_IDLE 0x2 /* the bus is currently idle */
101 #define ADB_ACTION_OUT 0x3 /* sending out a command */
102 #define ADB_ACTION_IN 0x4 /* receiving data */
103 #define ADB_ACTION_POLLING 0x5 /* polling - II only */
104 #define ADB_ACTION_RUNNING 0x6 /* running - IOP only */
105
106 /*
107 * These describe the state of the ADB bus itself, although they
108 * don't necessarily correspond directly to ADB states.
109 * Note: these are not really used in the IIsi code.
110 */
111 #define ADB_BUS_UNKNOWN 0x1 /* we don't know yet - all models */
112 #define ADB_BUS_IDLE 0x2 /* bus is idle - all models */
113 #define ADB_BUS_CMD 0x3 /* starting a command - II models */
114 #define ADB_BUS_ODD 0x4 /* the "odd" state - II models */
115 #define ADB_BUS_EVEN 0x5 /* the "even" state - II models */
116 #define ADB_BUS_ACTIVE 0x6 /* active state - IIsi models */
117 #define ADB_BUS_ACK 0x7 /* currently ACKing - IIsi models */
118
119 /*
120 * Shortcuts for setting or testing the VIA bit states.
121 * Not all shortcuts are used for every type of ADB hardware.
122 */
123 #define ADB_SET_STATE_IDLE_II() via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
124 #define ADB_SET_STATE_IDLE_IISI() via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
125 #define ADB_SET_STATE_IDLE_CUDA() via_reg(VIA1, vBufB) |= (vPB4 | vPB5)
126 #define ADB_SET_STATE_CMD() via_reg(VIA1, vBufB) &= ~(vPB4 | vPB5)
127 #define ADB_SET_STATE_EVEN() via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
128 vBufB) | vPB4) & ~vPB5)
129 #define ADB_SET_STATE_ODD() via_reg(VIA1, vBufB) = ((via_reg(VIA1, \
130 vBufB) | vPB5) & ~vPB4)
131 #define ADB_SET_STATE_ACTIVE() via_reg(VIA1, vBufB) |= vPB5
132 #define ADB_SET_STATE_INACTIVE() via_reg(VIA1, vBufB) &= ~vPB5
133 #define ADB_SET_STATE_TIP() via_reg(VIA1, vBufB) &= ~vPB5
134 #define ADB_CLR_STATE_TIP() via_reg(VIA1, vBufB) |= vPB5
135 #define ADB_SET_STATE_ACKON() via_reg(VIA1, vBufB) |= vPB4
136 #define ADB_SET_STATE_ACKOFF() via_reg(VIA1, vBufB) &= ~vPB4
137 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg(VIA1, vBufB) ^= vPB4
138 #define ADB_SET_STATE_ACKON_CUDA() via_reg(VIA1, vBufB) &= ~vPB4
139 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg(VIA1, vBufB) |= vPB4
140 #define ADB_SET_SR_INPUT() via_reg(VIA1, vACR) &= ~vSR_OUT
141 #define ADB_SET_SR_OUTPUT() via_reg(VIA1, vACR) |= vSR_OUT
142 #define ADB_SR() via_reg(VIA1, vSR)
143 #define ADB_VIA_INTR_ENABLE() via_reg(VIA1, vIER) = 0x84
144 #define ADB_VIA_INTR_DISABLE() via_reg(VIA1, vIER) = 0x04
145 #define ADB_VIA_CLR_INTR() via_reg(VIA1, vIFR) = 0x04
146 #define ADB_INTR_IS_OFF (vPB3 == (via_reg(VIA1, vBufB) & vPB3))
147 #define ADB_INTR_IS_ON (0 == (via_reg(VIA1, vBufB) & vPB3))
148 #define ADB_SR_INTR_IS_OFF (0 == (via_reg(VIA1, vIFR) & vSR_INT))
149 #define ADB_SR_INTR_IS_ON (vSR_INT == (via_reg(VIA1, \
150 vIFR) & vSR_INT))
151
152 /*
153 * This is the delay that is required (in uS) between certain
154 * ADB transactions. The actual timing delay for for each uS is
155 * calculated at boot time to account for differences in machine speed.
156 */
157 #define ADB_DELAY 150
158
159 /*
160 * Maximum ADB message length; includes space for data, result, and
161 * device code - plus a little for safety.
162 */
163 #define ADB_MAX_MSG_LENGTH 16
164 #define ADB_MAX_HDR_LENGTH 8
165
166 #define ADB_QUEUE 32
167 #define ADB_TICKLE_TICKS 4
168
169 /*
170 * A structure for storing information about each ADB device.
171 */
172 struct ADBDevEntry {
173 void (*ServiceRtPtr) __P((void));
174 void *DataAreaAddr;
175 int devType;
176 int origAddr;
177 int currentAddr;
178 };
179
180 /*
181 * Used to hold ADB commands that are waiting to be sent out.
182 */
183 struct adbCmdHoldEntry {
184 u_char outBuf[ADB_MAX_MSG_LENGTH]; /* our message */
185 u_char *saveBuf; /* buffer to know where to save result */
186 u_char *compRout; /* completion routine pointer */
187 u_char *data; /* completion routine data pointer */
188 };
189
190 /*
191 * Eventually used for two separate queues, the queue between
192 * the upper and lower halves, and the outgoing packet queue.
193 * TO DO: adbCommand can replace all of adbCmdHoldEntry eventually
194 */
195 struct adbCommand {
196 u_char header[ADB_MAX_HDR_LENGTH]; /* not used yet */
197 u_char data[ADB_MAX_MSG_LENGTH]; /* packet data only */
198 u_char *saveBuf; /* where to save result */
199 u_char *compRout; /* completion routine pointer */
200 u_char *compData; /* completion routine data pointer */
201 u_int cmd; /* the original command for this data */
202 u_int unsol; /* 1 if packet was unsolicited */
203 u_int ack_only; /* 1 for no special processing */
204 };
205
206 /*
207 * Text representations of each hardware class
208 */
209 char *adbHardwareDescr[MAX_ADB_HW + 1] = {
210 "unknown",
211 "II series",
212 "IIsi series",
213 "PowerBook",
214 "Cuda",
215 "IOP",
216 };
217
218 /*
219 * A few variables that we need and their initial values.
220 */
221 int adbHardware = ADB_HW_UNKNOWN;
222 int adbActionState = ADB_ACTION_NOTREADY;
223 int adbBusState = ADB_BUS_UNKNOWN;
224 int adbWaiting = 0; /* waiting for return data from the device */
225 int adbWriteDelay = 0; /* working on (or waiting to do) a write */
226 int adbOutQueueHasData = 0; /* something in the queue waiting to go out */
227 int adbNextEnd = 0; /* the next incoming bute is the last (II) */
228 int adbSoftPower = 0; /* machine supports soft power */
229
230 int adbWaitingCmd = 0; /* ADB command we are waiting for */
231 u_char *adbBuffer = (long)0; /* pointer to user data area */
232 void *adbCompRout = (long)0; /* pointer to the completion routine */
233 void *adbCompData = (long)0; /* pointer to the completion routine data */
234 long adbFakeInts = 0; /* keeps track of fake ADB interrupts for
235 * timeouts (II) */
236 int adbStarting = 1; /* doing ADBReInit so do polling differently */
237 int adbSendTalk = 0; /* the intr routine is sending the talk, not
238 * the user (II) */
239 int adbPolling = 0; /* we are polling for service request */
240 int adbPollCmd = 0; /* the last poll command we sent */
241
242 u_char adbInputBuffer[ADB_MAX_MSG_LENGTH]; /* data input buffer */
243 u_char adbOutputBuffer[ADB_MAX_MSG_LENGTH]; /* data output buffer */
244 struct adbCmdHoldEntry adbOutQueue; /* our 1 entry output queue */
245
246 int adbSentChars = 0; /* how many characters we have sent */
247 int adbLastDevice = 0; /* last ADB dev we heard from (II ONLY) */
248 int adbLastDevIndex = 0; /* last ADB dev loc in dev table (II ONLY) */
249 int adbLastCommand = 0; /* the last ADB command we sent (II) */
250
251 struct ADBDevEntry ADBDevTable[16]; /* our ADB device table */
252 int ADBNumDevices; /* num. of ADB devices found with ADBReInit */
253
254 struct adbCommand adbInbound[ADB_QUEUE]; /* incoming queue */
255 int adbInCount = 0; /* how many packets in in queue */
256 int adbInHead = 0; /* head of in queue */
257 int adbInTail = 0; /* tail of in queue */
258 struct adbCommand adbOutbound[ADB_QUEUE]; /* outgoing queue - not used yet */
259 int adbOutCount = 0; /* how many packets in out queue */
260 int adbOutHead = 0; /* head of out queue */
261 int adbOutTail = 0; /* tail of out queue */
262
263 int tickle_count = 0; /* how many tickles seen for this packet? */
264 int tickle_serial = 0; /* the last packet tickled */
265 int adb_cuda_serial = 0; /* the current packet */
266
267 extern struct mac68k_machine_S mac68k_machine;
268 extern int ite_polling; /* Are we polling? (Debugger mode) */
269
270 void pm_setup_adb __P((void));
271 void pm_hw_setup __P((void));
272 void pm_check_adb_devices __P((int));
273 void pm_intr __P((void *));
274 int pm_adb_op __P((u_char *, void *, void *, int));
275 void pm_init_adb_device __P((void));
276
277 /*
278 * The following are private routines.
279 */
280 #ifdef ADB_DEBUG
281 void print_single __P((u_char *));
282 #endif
283 void adb_intr __P((void *));
284 void adb_intr_II __P((void *));
285 void adb_intr_IIsi __P((void *));
286 void adb_intr_cuda __P((void *));
287 void adb_soft_intr __P((void));
288 int send_adb_II __P((u_char *, u_char *, void *, void *, int));
289 int send_adb_IIsi __P((u_char *, u_char *, void *, void *, int));
290 int send_adb_cuda __P((u_char *, u_char *, void *, void *, int));
291 void adb_intr_cuda_test __P((void));
292 void adb_cuda_tickle __P((void));
293 void adb_pass_up __P((struct adbCommand *));
294 void adb_op_comprout __P((void));
295 void adb_reinit __P((void));
296 int count_adbs __P((void));
297 int get_ind_adb_info __P((ADBDataBlock *, int));
298 int get_adb_info __P((ADBDataBlock *, int));
299 int set_adb_info __P((ADBSetInfoBlock *, int));
300 void adb_setup_hw_type __P((void));
301 int adb_op __P((Ptr, Ptr, Ptr, short));
302 int adb_op_sync __P((Ptr, Ptr, Ptr, short));
303 void adb_read_II __P((u_char *));
304 void adb_hw_setup __P((void));
305 void adb_hw_setup_IIsi __P((u_char *));
306 void adb_comp_exec __P((void));
307 int adb_cmd_result __P((u_char *));
308 int adb_cmd_extra __P((u_char *));
309 int adb_guess_next_device __P((void));
310 int adb_prog_switch_enable __P((void));
311 int adb_prog_switch_disable __P((void));
312 /* we should create this and it will be the public version */
313 int send_adb __P((u_char *, void *, void *));
314 void adb_iop_recv __P((IOP *, struct iop_msg *));
315 int send_adb_iop __P((int, u_char *, void *, void *));
316
317 #ifdef ADB_DEBUG
318 /*
319 * print_single
320 * Diagnostic display routine. Displays the hex values of the
321 * specified elements of the u_char. The length of the "string"
322 * is in [0].
323 */
324 void
325 print_single(str)
326 u_char *str;
327 {
328 int x;
329
330 if (str == 0) {
331 printf_intr("no data - null pointer\n");
332 return;
333 }
334 if (*str == 0) {
335 printf_intr("nothing returned\n");
336 return;
337 }
338 if (*str > 20) {
339 printf_intr("ADB: ACK > 20 no way!\n");
340 *str = (u_char)20;
341 }
342 printf_intr("(length=0x%x):", (u_int)*str);
343 for (x = 1; x <= *str; x++)
344 printf_intr(" 0x%02x", (u_int)*(str + x));
345 printf_intr("\n");
346 }
347 #endif
348
349 void
350 adb_cuda_tickle(void)
351 {
352 volatile int s;
353
354 if (adbActionState == ADB_ACTION_IN) {
355 if (tickle_serial == adb_cuda_serial) {
356 if (++tickle_count > 0) {
357 s = splhigh();
358 adbActionState = ADB_ACTION_IDLE;
359 adbInputBuffer[0] = 0;
360 ADB_SET_STATE_IDLE_CUDA();
361 splx(s);
362 }
363 } else {
364 tickle_serial = adb_cuda_serial;
365 tickle_count = 0;
366 }
367 } else {
368 tickle_serial = adb_cuda_serial;
369 tickle_count = 0;
370 }
371
372 timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
373 }
374
375 /*
376 * called when when an adb interrupt happens
377 *
378 * Cuda version of adb_intr
379 * TO DO: do we want to add some calls to intr_dispatch() here to
380 * grab serial interrupts?
381 */
382 void
383 adb_intr_cuda(void *arg)
384 {
385 volatile int i, ending;
386 volatile unsigned int s;
387 struct adbCommand packet;
388
389 s = splhigh(); /* can't be too careful - might be called */
390 /* from a routine, NOT an interrupt */
391
392 ADB_VIA_CLR_INTR(); /* clear interrupt */
393 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
394
395 switch_start:
396 switch (adbActionState) {
397 case ADB_ACTION_IDLE:
398 /*
399 * This is an unexpected packet, so grab the first (dummy)
400 * byte, set up the proper vars, and tell the chip we are
401 * starting to receive the packet by setting the TIP bit.
402 */
403 adbInputBuffer[1] = ADB_SR();
404 adb_cuda_serial++;
405 if (ADB_INTR_IS_OFF) /* must have been a fake start */
406 break;
407
408 ADB_SET_SR_INPUT();
409 ADB_SET_STATE_TIP();
410
411 adbInputBuffer[0] = 1;
412 adbActionState = ADB_ACTION_IN;
413 #ifdef ADB_DEBUG
414 if (adb_debug)
415 printf_intr("idle 0x%02x ", adbInputBuffer[1]);
416 #endif
417 break;
418
419 case ADB_ACTION_IN:
420 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR();
421 /* intr off means this is the last byte (end of frame) */
422 if (ADB_INTR_IS_OFF)
423 ending = 1;
424 else
425 ending = 0;
426
427 if (1 == ending) { /* end of message? */
428 #ifdef ADB_DEBUG
429 if (adb_debug) {
430 printf_intr("in end 0x%02x ",
431 adbInputBuffer[adbInputBuffer[0]]);
432 print_single(adbInputBuffer);
433 }
434 #endif
435
436 /*
437 * Are we waiting AND does this packet match what we
438 * are waiting for AND is it coming from either the
439 * ADB or RTC/PRAM sub-device? This section _should_
440 * recognize all ADB and RTC/PRAM type commands, but
441 * there may be more... NOTE: commands are always at
442 * [4], even for RTC/PRAM commands.
443 */
444 /* set up data for adb_pass_up */
445 for (i = 0; i <= adbInputBuffer[0]; i++)
446 packet.data[i] = adbInputBuffer[i];
447
448 if ((adbWaiting == 1) &&
449 (adbInputBuffer[4] == adbWaitingCmd) &&
450 ((adbInputBuffer[2] == 0x00) ||
451 (adbInputBuffer[2] == 0x01))) {
452 packet.saveBuf = adbBuffer;
453 packet.compRout = adbCompRout;
454 packet.compData = adbCompData;
455 packet.unsol = 0;
456 packet.ack_only = 0;
457 adb_pass_up(&packet);
458
459 adbWaitingCmd = 0; /* reset "waiting" vars */
460 adbWaiting = 0;
461 adbBuffer = (long)0;
462 adbCompRout = (long)0;
463 adbCompData = (long)0;
464 } else {
465 packet.unsol = 1;
466 packet.ack_only = 0;
467 adb_pass_up(&packet);
468 }
469
470
471 /* reset vars and signal the end of this frame */
472 adbActionState = ADB_ACTION_IDLE;
473 adbInputBuffer[0] = 0;
474 ADB_SET_STATE_IDLE_CUDA();
475 /*ADB_SET_SR_INPUT();*/
476
477 /*
478 * If there is something waiting to be sent out,
479 * the set everything up and send the first byte.
480 */
481 if (adbWriteDelay == 1) {
482 delay(ADB_DELAY); /* required */
483 adbSentChars = 0;
484 adbActionState = ADB_ACTION_OUT;
485 /*
486 * If the interrupt is on, we were too slow
487 * and the chip has already started to send
488 * something to us, so back out of the write
489 * and start a read cycle.
490 */
491 if (ADB_INTR_IS_ON) {
492 ADB_SET_SR_INPUT();
493 ADB_SET_STATE_IDLE_CUDA();
494 adbSentChars = 0;
495 adbActionState = ADB_ACTION_IDLE;
496 adbInputBuffer[0] = 0;
497 break;
498 }
499 /*
500 * If we got here, it's ok to start sending
501 * so load the first byte and tell the chip
502 * we want to send.
503 */
504 ADB_SET_STATE_TIP();
505 ADB_SET_SR_OUTPUT();
506 ADB_SR() = adbOutputBuffer[adbSentChars + 1];
507 }
508 } else {
509 ADB_TOGGLE_STATE_ACK_CUDA();
510 #ifdef ADB_DEBUG
511 if (adb_debug)
512 printf_intr("in 0x%02x ",
513 adbInputBuffer[adbInputBuffer[0]]);
514 #endif
515 }
516 break;
517
518 case ADB_ACTION_OUT:
519 i = ADB_SR(); /* reset SR-intr in IFR */
520 #ifdef ADB_DEBUG
521 if (adb_debug)
522 printf_intr("intr out 0x%02x ", i);
523 #endif
524
525 adbSentChars++;
526 if (ADB_INTR_IS_ON) { /* ADB intr low during write */
527 #ifdef ADB_DEBUG
528 if (adb_debug)
529 printf_intr("intr was on ");
530 #endif
531 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
532 ADB_SET_STATE_IDLE_CUDA();
533 adbSentChars = 0; /* must start all over */
534 adbActionState = ADB_ACTION_IDLE; /* new state */
535 adbInputBuffer[0] = 0;
536 adbWriteDelay = 1; /* must retry when done with
537 * read */
538 delay(ADB_DELAY);
539 goto switch_start; /* process next state right
540 * now */
541 break;
542 }
543 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
544 if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data
545 * back? */
546 adbWaiting = 1; /* signal waiting for return */
547 adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */
548 } else { /* no talk, so done */
549 /* set up stuff for adb_pass_up */
550 for (i = 0; i <= adbInputBuffer[0]; i++)
551 packet.data[i] = adbInputBuffer[i];
552 packet.saveBuf = adbBuffer;
553 packet.compRout = adbCompRout;
554 packet.compData = adbCompData;
555 packet.cmd = adbWaitingCmd;
556 packet.unsol = 0;
557 packet.ack_only = 1;
558 adb_pass_up(&packet);
559
560 /* reset "waiting" vars, just in case */
561 adbWaitingCmd = 0;
562 adbBuffer = (long)0;
563 adbCompRout = (long)0;
564 adbCompData = (long)0;
565 }
566
567 adbWriteDelay = 0; /* done writing */
568 adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */
569 ADB_SET_SR_INPUT();
570 ADB_SET_STATE_IDLE_CUDA();
571 #ifdef ADB_DEBUG
572 if (adb_debug)
573 printf_intr("write done ");
574 #endif
575 } else {
576 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* send next byte */
577 ADB_TOGGLE_STATE_ACK_CUDA(); /* signal byte ready to
578 * shift */
579 #ifdef ADB_DEBUG
580 if (adb_debug)
581 printf_intr("toggle ");
582 #endif
583 }
584 break;
585
586 case ADB_ACTION_NOTREADY:
587 #ifdef ADB_DEBUG
588 if (adb_debug)
589 printf_intr("adb: not yet initialized\n");
590 #endif
591 break;
592
593 default:
594 #ifdef ADB_DEBUG
595 if (adb_debug)
596 printf_intr("intr: unknown ADB state\n");
597 #endif
598 }
599
600 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
601
602 splx(s); /* restore */
603
604 return;
605 } /* end adb_intr_cuda */
606
607
608 int
609 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, int
610 command)
611 {
612 int i, s, len;
613
614 #ifdef ADB_DEBUG
615 if (adb_debug)
616 printf_intr("SEND\n");
617 #endif
618
619 if (adbActionState == ADB_ACTION_NOTREADY)
620 return 1;
621
622 /* Don't interrupt while we are messing with the ADB */
623 s = splhigh();
624
625 if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */
626 (ADB_INTR_IS_OFF)) { /* and no incoming interrupt? */
627 } else
628 if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */
629 adbWriteDelay = 1; /* if no, then we'll "queue"
630 * it up */
631 else {
632 splx(s);
633 return 1; /* really busy! */
634 }
635
636 #ifdef ADB_DEBUG
637 if (adb_debug)
638 printf_intr("QUEUE\n");
639 #endif
640 if ((long)in == (long)0) { /* need to convert? */
641 /*
642 * Don't need to use adb_cmd_extra here because this section
643 * will be called ONLY when it is an ADB command (no RTC or
644 * PRAM)
645 */
646 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
647 * doing a listen! */
648 len = buffer[0]; /* length of additional data */
649 else
650 len = 0;/* no additional data */
651
652 adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl.
653 * data */
654 adbOutputBuffer[1] = 0x00; /* mark as an ADB command */
655 adbOutputBuffer[2] = (u_char)command; /* load command */
656
657 for (i = 1; i <= len; i++) /* copy additional output
658 * data, if any */
659 adbOutputBuffer[2 + i] = buffer[i];
660 } else
661 for (i = 0; i <= (in[0] + 1); i++)
662 adbOutputBuffer[i] = in[i];
663
664 adbSentChars = 0; /* nothing sent yet */
665 adbBuffer = buffer; /* save buffer to know where to save result */
666 adbCompRout = compRout; /* save completion routine pointer */
667 adbCompData = data; /* save completion routine data pointer */
668 adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */
669
670 if (adbWriteDelay != 1) { /* start command now? */
671 #ifdef ADB_DEBUG
672 if (adb_debug)
673 printf_intr("out start NOW");
674 #endif
675 delay(ADB_DELAY);
676 adbActionState = ADB_ACTION_OUT; /* set next state */
677 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
678 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* load byte for output */
679 ADB_SET_STATE_ACKOFF_CUDA();
680 ADB_SET_STATE_TIP(); /* tell ADB that we want to send */
681 }
682 adbWriteDelay = 1; /* something in the write "queue" */
683
684 splx(s);
685
686 if (0x0100 <= (s & 0x0700)) /* were VIA1 interrupts blocked ? */
687 /* poll until byte done */
688 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
689 || (adbWaiting == 1))
690 if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
691 adb_intr_cuda(NULL); /* process it */
692 adb_soft_intr();
693 }
694
695 return 0;
696 } /* send_adb_cuda */
697
698
699 void
700 adb_intr_II(void *arg)
701 {
702 struct adbCommand packet;
703 int i, intr_on = 0;
704 int send = 0;
705 unsigned int s;
706
707 s = splhigh(); /* can't be too careful - might be called */
708 /* from a routine, NOT an interrupt */
709
710 ADB_VIA_CLR_INTR(); /* clear interrupt */
711
712 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
713
714 delay(ADB_DELAY); /* yuck (don't remove) */
715
716 if (!mac68k_machine.aux_interrupts)
717 (void)intr_dispatch(0x70); /* grab any serial interrupts */
718
719 if (ADB_INTR_IS_ON)
720 intr_on = 1; /* save for later */
721
722 switch_start:
723 switch (adbActionState) {
724 case ADB_ACTION_POLLING:
725 if (!intr_on) {
726 if (adbOutQueueHasData) {
727 #ifdef ADB_DEBUG
728 if (adb_debug & 0x80)
729 printf_intr("POLL-doing-out-queue. ");
730 #endif
731 /* copy over data */
732 ADB_SET_STATE_IDLE_II();
733 delay(ADB_DELAY);
734 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
735 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
736 adbBuffer = adbOutQueue.saveBuf; /* user data area */
737 adbCompRout = adbOutQueue.compRout; /* completion routine */
738 adbCompData = adbOutQueue.data; /* comp. rout. data */
739 adbOutQueueHasData = 0; /* currently processing
740 * "queue" entry */
741 adbSentChars = 0; /* nothing sent yet */
742 adbActionState = ADB_ACTION_OUT; /* set next state */
743 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
744 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
745 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
746 ADB_SET_STATE_CMD(); /* tell ADB that we want to
747 * send */
748 break;
749 } else {
750 #ifdef ADB_DEBUG
751 if (adb_debug)
752 printf_intr("pIDLE ");
753 #endif
754 adbActionState = ADB_ACTION_IDLE;
755 }
756 } else {
757 #ifdef ADB_DEBUG
758 if (adb_debug & 0x80)
759 printf_intr("pIN ");
760 #endif
761 adbActionState = ADB_ACTION_IN;
762 }
763 delay(ADB_DELAY);
764 if (!mac68k_machine.aux_interrupts)
765 (void)intr_dispatch(0x70); /* grab any serial interrupts */
766 goto switch_start;
767 break;
768 case ADB_ACTION_IDLE:
769 if (!intr_on) {
770 i = ADB_SR();
771 adbBusState = ADB_BUS_IDLE;
772 adbActionState = ADB_ACTION_IDLE;
773 ADB_SET_STATE_IDLE_II();
774 break;
775 }
776 adbInputBuffer[0] = 1;
777 adbInputBuffer[1] = ADB_SR(); /* get first byte */
778 #ifdef ADB_DEBUG
779 if (adb_debug & 0x80)
780 printf_intr("idle 0x%02x ", adbInputBuffer[1]);
781 #endif
782 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
783 adbActionState = ADB_ACTION_IN; /* set next state */
784 ADB_SET_STATE_EVEN(); /* set bus state to even */
785 adbBusState = ADB_BUS_EVEN;
786 break;
787
788 case ADB_ACTION_IN:
789 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR(); /* get byte */
790 #ifdef ADB_DEBUG
791 if (adb_debug & 0x80)
792 printf_intr("in 0x%02x ",
793 adbInputBuffer[adbInputBuffer[0]]);
794 #endif
795 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
796
797 if (intr_on) { /* process last byte of packet */
798 adbInputBuffer[0]--; /* minus one */
799 /*
800 * If intr_on was true, and it's the second byte, then
801 * the byte we just discarded is really valid, so
802 * adjust the count
803 */
804 if (adbInputBuffer[0] == 2) {
805 adbInputBuffer[0]++;
806 }
807
808 #ifdef ADB_DEBUG
809 if (adb_debug & 0x80) {
810 printf_intr("done: ");
811 print_single(adbInputBuffer);
812 }
813 #endif
814
815 adbLastDevice = (adbInputBuffer[1] & 0xf0) >> 4;
816
817 if (adbInputBuffer[0] == 1 && !adbWaiting) { /* SRQ!!!*/
818 #ifdef ADB_DEBUG
819 if (adb_debug & 0x80)
820 printf_intr(" xSRQ! ");
821 #endif
822 adb_guess_next_device();
823 #ifdef ADB_DEBUG
824 if (adb_debug & 0x80)
825 printf_intr("try 0x%0x ",
826 adbLastDevice);
827 #endif
828 adbOutputBuffer[0] = 1;
829 adbOutputBuffer[1] =
830 ((adbLastDevice & 0x0f) << 4) | 0x0c;
831
832 adbSentChars = 0; /* nothing sent yet */
833 adbActionState = ADB_ACTION_POLLING; /* 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 break;
839 }
840
841 /* set up data for adb_pass_up */
842 for (i = 0; i <= adbInputBuffer[0]; i++)
843 packet.data[i] = adbInputBuffer[i];
844
845 if (!adbWaiting && (adbInputBuffer[0] != 0)) {
846 packet.unsol = 1;
847 packet.ack_only = 0;
848 adb_pass_up(&packet);
849 } else {
850 packet.saveBuf = adbBuffer;
851 packet.compRout = adbCompRout;
852 packet.compData = adbCompData;
853 packet.unsol = 0;
854 packet.ack_only = 0;
855 adb_pass_up(&packet);
856 }
857
858 adbWaiting = 0;
859 adbInputBuffer[0] = 0;
860 adbBuffer = (long)0;
861 adbCompRout = (long)0;
862 adbCompData = (long)0;
863 /*
864 * Since we are done, check whether there is any data
865 * waiting to do out. If so, start the sending the data.
866 */
867 if (adbOutQueueHasData == 1) {
868 #ifdef ADB_DEBUG
869 if (adb_debug & 0x80)
870 printf_intr("XXX: DOING OUT QUEUE\n");
871 #endif
872 /* copy over data */
873 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
874 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
875 adbBuffer = adbOutQueue.saveBuf; /* user data area */
876 adbCompRout = adbOutQueue.compRout; /* completion routine */
877 adbCompData = adbOutQueue.data; /* comp. rout. data */
878 adbOutQueueHasData = 0; /* currently processing
879 * "queue" entry */
880 send = 1;
881 } else {
882 #ifdef ADB_DEBUG
883 if (adb_debug & 0x80)
884 printf_intr("XXending ");
885 #endif
886 adb_guess_next_device();
887 adbOutputBuffer[0] = 1;
888 adbOutputBuffer[1] = ((adbLastDevice & 0x0f) << 4) | 0x0c;
889 adbSentChars = 0; /* nothing sent yet */
890 adbActionState = ADB_ACTION_POLLING; /* set next state */
891 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
892 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
893 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
894 ADB_SET_STATE_CMD(); /* tell ADB that we want to */
895 break;
896 }
897 }
898
899 /*
900 * If send is true then something above determined that
901 * the message has ended and we need to start sending out
902 * a new message immediately. This could be because there
903 * is data waiting to go out or because an SRQ was seen.
904 */
905 if (send) {
906 adbSentChars = 0; /* nothing sent yet */
907 adbActionState = ADB_ACTION_OUT; /* set next state */
908 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
909 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
910 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
911 ADB_SET_STATE_CMD(); /* tell ADB that we want to
912 * send */
913 break;
914 }
915 /* We only get this far if the message hasn't ended yet. */
916 switch (adbBusState) { /* set to next state */
917 case ADB_BUS_EVEN:
918 ADB_SET_STATE_ODD(); /* set state to odd */
919 adbBusState = ADB_BUS_ODD;
920 break;
921
922 case ADB_BUS_ODD:
923 ADB_SET_STATE_EVEN(); /* set state to even */
924 adbBusState = ADB_BUS_EVEN;
925 break;
926 default:
927 printf_intr("strange state!!!\n"); /* huh? */
928 break;
929 }
930 break;
931
932 case ADB_ACTION_OUT:
933 i = ADB_SR(); /* clear interrupt */
934 adbSentChars++;
935 /*
936 * If the outgoing data was a TALK, we must
937 * switch to input mode to get the result.
938 */
939 if ((adbOutputBuffer[1] & 0x0c) == 0x0c) {
940 adbInputBuffer[0] = 1;
941 adbInputBuffer[1] = i;
942 adbActionState = ADB_ACTION_IN;
943 ADB_SET_SR_INPUT();
944 adbBusState = ADB_BUS_EVEN;
945 ADB_SET_STATE_EVEN();
946 #ifdef ADB_DEBUG
947 if (adb_debug & 0x80)
948 printf_intr("talk out 0x%02x ", i);
949 #endif
950 /* we want something back */
951 adbWaiting = 1;
952 break;
953 }
954 /*
955 * If it's not a TALK, check whether all data has been sent.
956 * If so, call the completion routine and clean up. If not,
957 * advance to the next state.
958 */
959 #ifdef ADB_DEBUG
960 if (adb_debug & 0x80)
961 printf_intr("non-talk out 0x%0x ", i);
962 #endif
963 ADB_SET_SR_OUTPUT();
964 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
965 #ifdef ADB_DEBUG
966 if (adb_debug & 0x80)
967 printf_intr("done \n");
968 #endif
969 /* set up stuff for adb_pass_up */
970 for (i = 0; i <= adbOutputBuffer[0]; i++)
971 packet.data[i] = adbOutputBuffer[i];
972 packet.saveBuf = adbBuffer;
973 packet.compRout = adbCompRout;
974 packet.compData = adbCompData;
975 packet.cmd = adbWaitingCmd;
976 packet.unsol = 0;
977 packet.ack_only = 1;
978 adb_pass_up(&packet);
979
980 /* reset "waiting" vars, just in case */
981 adbBuffer = (long)0;
982 adbCompRout = (long)0;
983 adbCompData = (long)0;
984 if (adbOutQueueHasData == 1) {
985 /* copy over data */
986 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++)
987 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
988 adbBuffer = adbOutQueue.saveBuf; /* user data area */
989 adbCompRout = adbOutQueue.compRout; /* completion routine */
990 adbCompData = adbOutQueue.data; /* comp. rout. data */
991 adbOutQueueHasData = 0; /* currently processing
992 * "queue" entry */
993 adbSentChars = 0; /* nothing sent yet */
994 adbActionState = ADB_ACTION_OUT; /* set next state */
995 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
996 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
997 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
998 ADB_SET_STATE_CMD(); /* tell ADB that we want to
999 * send */
1000 break;
1001 } else {
1002 /* send talk to last device instead */
1003 adbOutputBuffer[0] = 1;
1004 adbOutputBuffer[1] = (adbOutputBuffer[1] & 0xf0) | 0x0c;
1005
1006 adbSentChars = 0; /* nothing sent yet */
1007 adbActionState = ADB_ACTION_IDLE; /* set next state */
1008 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
1009 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
1010 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
1011 ADB_SET_STATE_CMD(); /* tell ADB that we want to */
1012 break;
1013 }
1014 }
1015 ADB_SR() = adbOutputBuffer[adbSentChars + 1];
1016 switch (adbBusState) { /* advance to next state */
1017 case ADB_BUS_EVEN:
1018 ADB_SET_STATE_ODD(); /* set state to odd */
1019 adbBusState = ADB_BUS_ODD;
1020 break;
1021
1022 case ADB_BUS_CMD:
1023 case ADB_BUS_ODD:
1024 ADB_SET_STATE_EVEN(); /* set state to even */
1025 adbBusState = ADB_BUS_EVEN;
1026 break;
1027
1028 default:
1029 #ifdef ADB_DEBUG
1030 if (adb_debug) {
1031 printf_intr("strange state!!! (0x%x)\n",
1032 adbBusState);
1033 }
1034 #endif
1035 break;
1036 }
1037 break;
1038
1039 default:
1040 #ifdef ADB_DEBUG
1041 if (adb_debug)
1042 printf_intr("adb: unknown ADB state (during intr)\n");
1043 #endif
1044 }
1045
1046 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
1047
1048 splx(s); /* restore */
1049
1050 return;
1051
1052 }
1053
1054
1055 /*
1056 * send_adb version for II series machines
1057 */
1058 int
1059 send_adb_II(u_char * in, u_char * buffer, void *compRout, void *data, int command)
1060 {
1061 int i, s, len;
1062
1063 if (adbActionState == ADB_ACTION_NOTREADY) /* return if ADB not
1064 * available */
1065 return 1;
1066
1067 /* Don't interrupt while we are messing with the ADB */
1068 s = splhigh();
1069
1070 if (0 != adbOutQueueHasData) { /* right now, "has data" means "full" */
1071 splx(s); /* sorry, try again later */
1072 return 1;
1073 }
1074 if ((long)in == (long)0) { /* need to convert? */
1075 /*
1076 * Don't need to use adb_cmd_extra here because this section
1077 * will be called ONLY when it is an ADB command (no RTC or
1078 * PRAM), especially on II series!
1079 */
1080 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
1081 * doing a listen! */
1082 len = buffer[0]; /* length of additional data */
1083 else
1084 len = 0;/* no additional data */
1085
1086 adbOutQueue.outBuf[0] = 1 + len; /* command + addl. data */
1087 adbOutQueue.outBuf[1] = (u_char)command; /* load command */
1088
1089 for (i = 1; i <= len; i++) /* copy additional output
1090 * data, if any */
1091 adbOutQueue.outBuf[1 + i] = buffer[i];
1092 } else
1093 /* if data ready, just copy over */
1094 for (i = 0; i <= (in[0] + 1); i++)
1095 adbOutQueue.outBuf[i] = in[i];
1096
1097 adbOutQueue.saveBuf = buffer; /* save buffer to know where to save
1098 * result */
1099 adbOutQueue.compRout = compRout; /* save completion routine
1100 * pointer */
1101 adbOutQueue.data = data;/* save completion routine data pointer */
1102
1103 if ((adbActionState == ADB_ACTION_IDLE) && /* is ADB available? */
1104 (ADB_INTR_IS_OFF)) { /* and no incoming interrupts? */
1105 /* then start command now */
1106 for (i = 0; i <= (adbOutQueue.outBuf[0] + 1); i++) /* copy over data */
1107 adbOutputBuffer[i] = adbOutQueue.outBuf[i];
1108
1109 adbBuffer = adbOutQueue.saveBuf; /* pointer to user data
1110 * area */
1111 adbCompRout = adbOutQueue.compRout; /* pointer to the
1112 * completion routine */
1113 adbCompData = adbOutQueue.data; /* pointer to the completion
1114 * routine data */
1115
1116 adbSentChars = 0; /* nothing sent yet */
1117 adbActionState = ADB_ACTION_OUT; /* set next state */
1118 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
1119
1120 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
1121
1122 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* load byte for output */
1123 ADB_SET_STATE_CMD(); /* tell ADB that we want to send */
1124 adbOutQueueHasData = 0; /* currently processing "queue" entry */
1125 } else
1126 adbOutQueueHasData = 1; /* something in the write "queue" */
1127
1128 splx(s);
1129
1130 if (0x0100 <= (s & 0x0700)) /* were VIA1 interrupts blocked? */
1131 /* poll until message done */
1132 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1133 || (adbWaiting == 1))
1134 if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
1135 adb_intr_II(NULL); /* go process "interrupt" */
1136 adb_soft_intr();
1137 }
1138
1139 return 0;
1140 }
1141
1142
1143 /*
1144 * This routine is called from the II series interrupt routine
1145 * to determine what the "next" device is that should be polled.
1146 */
1147 int
1148 adb_guess_next_device(void)
1149 {
1150 int last, i, dummy;
1151
1152 if (adbStarting) {
1153 /*
1154 * Start polling EVERY device, since we can't be sure there is
1155 * anything in the device table yet
1156 */
1157 if (adbLastDevice < 1 || adbLastDevice > 15)
1158 adbLastDevice = 1;
1159 if (++adbLastDevice > 15) /* point to next one */
1160 adbLastDevice = 1;
1161 } else {
1162 /* find the next device using the device table */
1163 if (adbLastDevice < 1 || adbLastDevice > 15) /* let's be parinoid */
1164 adbLastDevice = 2;
1165 last = 1; /* default index location */
1166
1167 for (i = 1; i < 16; i++) /* find index entry */
1168 if (ADBDevTable[i].currentAddr == adbLastDevice) { /* look for device */
1169 last = i; /* found it */
1170 break;
1171 }
1172 dummy = last; /* index to start at */
1173 for (;;) { /* find next device in index */
1174 if (++dummy > 15) /* wrap around if needed */
1175 dummy = 1;
1176 if (dummy == last) { /* didn't find any other
1177 * device! This can happen if
1178 * there are no devices on the
1179 * bus */
1180 dummy = 2;
1181 break;
1182 }
1183 /* found the next device */
1184 if (ADBDevTable[dummy].devType != 0)
1185 break;
1186 }
1187 adbLastDevice = ADBDevTable[dummy].currentAddr;
1188 }
1189 return adbLastDevice;
1190 }
1191
1192
1193 /*
1194 * Called when when an adb interrupt happens.
1195 * This routine simply transfers control over to the appropriate
1196 * code for the machine we are running on.
1197 */
1198 void
1199 adb_intr(void *arg)
1200 {
1201 switch (adbHardware) {
1202 case ADB_HW_II:
1203 adb_intr_II(arg);
1204 break;
1205
1206 case ADB_HW_IISI:
1207 adb_intr_IIsi(arg);
1208 break;
1209
1210 case ADB_HW_PB: /* Should not come through here. */
1211 break;
1212
1213 case ADB_HW_CUDA:
1214 adb_intr_cuda(arg);
1215 break;
1216
1217 case ADB_HW_IOP: /* Should not come through here. */
1218 break;
1219
1220 case ADB_HW_UNKNOWN:
1221 break;
1222 }
1223 }
1224
1225
1226 /*
1227 * called when when an adb interrupt happens
1228 *
1229 * IIsi version of adb_intr
1230 *
1231 */
1232 void
1233 adb_intr_IIsi(void *arg)
1234 {
1235 struct adbCommand packet;
1236 int i, ending;
1237 unsigned int s;
1238
1239 s = splhigh(); /* can't be too careful - might be called */
1240 /* from a routine, NOT an interrupt */
1241
1242 ADB_VIA_CLR_INTR(); /* clear interrupt */
1243
1244 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
1245
1246 switch_start:
1247 switch (adbActionState) {
1248 case ADB_ACTION_IDLE:
1249 delay(ADB_DELAY); /* short delay is required before the
1250 * first byte */
1251
1252 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1253 ADB_SET_STATE_ACTIVE(); /* signal start of data frame */
1254 adbInputBuffer[1] = ADB_SR(); /* get byte */
1255 adbInputBuffer[0] = 1;
1256 adbActionState = ADB_ACTION_IN; /* set next state */
1257
1258 ADB_SET_STATE_ACKON(); /* start ACK to ADB chip */
1259 delay(ADB_DELAY); /* delay */
1260 ADB_SET_STATE_ACKOFF(); /* end ACK to ADB chip */
1261 (void)intr_dispatch(0x70); /* grab any serial interrupts */
1262 break;
1263
1264 case ADB_ACTION_IN:
1265 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1266 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR(); /* get byte */
1267 if (ADB_INTR_IS_OFF) /* check for end of frame */
1268 ending = 1;
1269 else
1270 ending = 0;
1271
1272 ADB_SET_STATE_ACKON(); /* start ACK to ADB chip */
1273 delay(ADB_DELAY); /* delay */
1274 ADB_SET_STATE_ACKOFF(); /* end ACK to ADB chip */
1275 (void)intr_dispatch(0x70); /* grab any serial interrupts */
1276
1277 if (1 == ending) { /* end of message? */
1278 ADB_SET_STATE_INACTIVE(); /* signal end of frame */
1279 /*
1280 * This section _should_ handle all ADB and RTC/PRAM
1281 * type commands, but there may be more... Note:
1282 * commands are always at [4], even for rtc/pram
1283 * commands
1284 */
1285 /* set up data for adb_pass_up */
1286 for (i = 0; i <= adbInputBuffer[0]; i++)
1287 packet.data[i] = adbInputBuffer[i];
1288
1289 if ((adbWaiting == 1) && /* are we waiting AND */
1290 (adbInputBuffer[4] == adbWaitingCmd) && /* the cmd we sent AND */
1291 ((adbInputBuffer[2] == 0x00) || /* it's from the ADB
1292 * device OR */
1293 (adbInputBuffer[2] == 0x01))) { /* it's from the
1294 * PRAM/RTC device */
1295
1296 packet.saveBuf = adbBuffer;
1297 packet.compRout = adbCompRout;
1298 packet.compData = adbCompData;
1299 packet.unsol = 0;
1300 packet.ack_only = 0;
1301 adb_pass_up(&packet);
1302
1303 adbWaitingCmd = 0; /* reset "waiting" vars */
1304 adbWaiting = 0;
1305 adbBuffer = (long)0;
1306 adbCompRout = (long)0;
1307 adbCompData = (long)0;
1308 } else {
1309 packet.unsol = 1;
1310 packet.ack_only = 0;
1311 adb_pass_up(&packet);
1312 }
1313
1314 adbActionState = ADB_ACTION_IDLE;
1315 adbInputBuffer[0] = 0; /* reset length */
1316
1317 if (adbWriteDelay == 1) { /* were we waiting to
1318 * write? */
1319 adbSentChars = 0; /* nothing sent yet */
1320 adbActionState = ADB_ACTION_OUT; /* set next state */
1321
1322 delay(ADB_DELAY); /* delay */
1323 (void)intr_dispatch(0x70); /* grab any serial interrupts */
1324
1325 if (ADB_INTR_IS_ON) { /* ADB intr low during
1326 * write */
1327 ADB_SET_STATE_IDLE_IISI(); /* reset */
1328 ADB_SET_SR_INPUT(); /* make sure SR is set
1329 * to IN */
1330 adbSentChars = 0; /* must start all over */
1331 adbActionState = ADB_ACTION_IDLE; /* new state */
1332 adbInputBuffer[0] = 0;
1333 /* may be able to take this out later */
1334 delay(ADB_DELAY); /* delay */
1335 break;
1336 }
1337 ADB_SET_STATE_ACTIVE(); /* tell ADB that we want
1338 * to send */
1339 ADB_SET_STATE_ACKOFF(); /* make sure */
1340 ADB_SET_SR_OUTPUT(); /* set shift register
1341 * for OUT */
1342 ADB_SR() = adbOutputBuffer[adbSentChars + 1];
1343 ADB_SET_STATE_ACKON(); /* tell ADB byte ready
1344 * to shift */
1345 }
1346 }
1347 break;
1348
1349 case ADB_ACTION_OUT:
1350 i = ADB_SR(); /* reset SR-intr in IFR */
1351 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
1352
1353 ADB_SET_STATE_ACKOFF(); /* finish ACK */
1354 adbSentChars++;
1355 if (ADB_INTR_IS_ON) { /* ADB intr low during write */
1356 ADB_SET_STATE_IDLE_IISI(); /* reset */
1357 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1358 adbSentChars = 0; /* must start all over */
1359 adbActionState = ADB_ACTION_IDLE; /* new state */
1360 adbInputBuffer[0] = 0;
1361 adbWriteDelay = 1; /* must retry when done with
1362 * read */
1363 delay(ADB_DELAY); /* delay */
1364 (void)intr_dispatch(0x70); /* grab any serial interrupts */
1365 goto switch_start; /* process next state right
1366 * now */
1367 break;
1368 }
1369 delay(ADB_DELAY); /* required delay */
1370 (void)intr_dispatch(0x70); /* grab any serial interrupts */
1371
1372 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
1373 if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data
1374 * back? */
1375 adbWaiting = 1; /* signal waiting for return */
1376 adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */
1377 } else {/* no talk, so done */
1378 /* set up stuff for adb_pass_up */
1379 for (i = 0; i <= adbInputBuffer[0]; i++)
1380 packet.data[i] = adbInputBuffer[i];
1381 packet.saveBuf = adbBuffer;
1382 packet.compRout = adbCompRout;
1383 packet.compData = adbCompData;
1384 packet.cmd = adbWaitingCmd;
1385 packet.unsol = 0;
1386 packet.ack_only = 1;
1387 adb_pass_up(&packet);
1388
1389 /* reset "waiting" vars, just in case */
1390 adbWaitingCmd = 0;
1391 adbBuffer = (long)0;
1392 adbCompRout = (long)0;
1393 adbCompData = (long)0;
1394 }
1395
1396 adbWriteDelay = 0; /* done writing */
1397 adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */
1398 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
1399 ADB_SET_STATE_INACTIVE(); /* end of frame */
1400 } else {
1401 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* send next byte */
1402 ADB_SET_STATE_ACKON(); /* signal byte ready to shift */
1403 }
1404 break;
1405
1406 case ADB_ACTION_NOTREADY:
1407 #ifdef ADB_DEBUG
1408 if (adb_debug)
1409 printf_intr("adb: not yet initialized\n");
1410 #endif
1411 break;
1412
1413 default:
1414 #ifdef ADB_DEBUG
1415 if (adb_debug)
1416 printf_intr("intr: unknown ADB state\n");
1417 #endif
1418 }
1419
1420 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
1421
1422 splx(s); /* restore */
1423
1424 return;
1425 } /* end adb_intr_IIsi */
1426
1427
1428 /*****************************************************************************
1429 * if the device is currently busy, and there is no data waiting to go out, then
1430 * the data is "queued" in the outgoing buffer. If we are already waiting, then
1431 * we return.
1432 * in: if (in == 0) then the command string is built from command and buffer
1433 * if (in != 0) then in is used as the command string
1434 * buffer: additional data to be sent (used only if in == 0)
1435 * this is also where return data is stored
1436 * compRout: the completion routine that is called when then return value
1437 * is received (if a return value is expected)
1438 * data: a data pointer that can be used by the completion routine
1439 * command: an ADB command to be sent (used only if in == 0)
1440 *
1441 */
1442 int
1443 send_adb_IIsi(u_char * in, u_char * buffer, void *compRout, void *data, int
1444 command)
1445 {
1446 int i, s, len;
1447
1448 if (adbActionState == ADB_ACTION_NOTREADY)
1449 return 1;
1450
1451 /* Don't interrupt while we are messing with the ADB */
1452 s = splhigh();
1453
1454 if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */
1455 (ADB_INTR_IS_OFF)) {/* and no incoming interrupt? */
1456
1457 } else
1458 if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */
1459 adbWriteDelay = 1; /* if no, then we'll "queue"
1460 * it up */
1461 else {
1462 splx(s);
1463 return 1; /* really busy! */
1464 }
1465
1466 if ((long)in == (long)0) { /* need to convert? */
1467 /*
1468 * Don't need to use adb_cmd_extra here because this section
1469 * will be called ONLY when it is an ADB command (no RTC or
1470 * PRAM)
1471 */
1472 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if
1473 * doing a listen! */
1474 len = buffer[0]; /* length of additional data */
1475 else
1476 len = 0;/* no additional data */
1477
1478 adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl.
1479 * data */
1480 adbOutputBuffer[1] = 0x00; /* mark as an ADB command */
1481 adbOutputBuffer[2] = (u_char)command; /* load command */
1482
1483 for (i = 1; i <= len; i++) /* copy additional output
1484 * data, if any */
1485 adbOutputBuffer[2 + i] = buffer[i];
1486 } else
1487 for (i = 0; i <= (in[0] + 1); i++)
1488 adbOutputBuffer[i] = in[i];
1489
1490 adbSentChars = 0; /* nothing sent yet */
1491 adbBuffer = buffer; /* save buffer to know where to save result */
1492 adbCompRout = compRout; /* save completion routine pointer */
1493 adbCompData = data; /* save completion routine data pointer */
1494 adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */
1495
1496 if (adbWriteDelay != 1) { /* start command now? */
1497 adbActionState = ADB_ACTION_OUT; /* set next state */
1498
1499 ADB_SET_STATE_ACTIVE(); /* tell ADB that we want to send */
1500 ADB_SET_STATE_ACKOFF(); /* make sure */
1501
1502 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
1503
1504 ADB_SR() = adbOutputBuffer[adbSentChars + 1]; /* load byte for output */
1505
1506 ADB_SET_STATE_ACKON(); /* tell ADB byte ready to shift */
1507 }
1508 adbWriteDelay = 1; /* something in the write "queue" */
1509
1510 splx(s);
1511
1512 if (0x0100 <= (s & 0x0700)) /* were VIA1 interrupts blocked ? */
1513 /* poll until byte done */
1514 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON)
1515 || (adbWaiting == 1))
1516 if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */
1517 adb_intr_IIsi(NULL); /* process it */
1518 adb_soft_intr();
1519 }
1520
1521 return 0;
1522 } /* send_adb_IIsi */
1523
1524 void
1525 adb_iop_recv(IOP *iop, struct iop_msg *msg)
1526 {
1527 struct adbCommand pkt;
1528 unsigned flags;
1529
1530 if (adbActionState != ADB_ACTION_RUNNING)
1531 return;
1532
1533 switch (msg->status) {
1534 case IOP_MSGSTAT_SENT:
1535 if (0 == adb_cmd_result(msg->msg + 1)) {
1536 adbWaiting = 1;
1537 adbWaitingCmd = msg->msg[2];
1538 }
1539 break;
1540 case IOP_MSGSTAT_RECEIVED:
1541 case IOP_MSGSTAT_UNEXPECTED:
1542 flags = msg->msg[0];
1543 if (flags != 0) {
1544 printf("ADB FLAGS 0x%x", flags);
1545 break;
1546 }
1547 if (adbWaiting &&
1548 (msg->msg[2] == adbWaitingCmd)) {
1549 pkt.saveBuf = msg->msg + 1;
1550 pkt.compRout = adbCompRout;
1551 pkt.compData = adbCompData;
1552 pkt.unsol = 0;
1553 pkt.ack_only = 0;
1554 adb_pass_up(&pkt);
1555
1556 adbWaitingCmd = 0;
1557 adbWaiting = 0;
1558 } else {
1559 pkt.unsol = 1;
1560 pkt.ack_only = 0;
1561 adb_pass_up(&pkt);
1562 }
1563 break;
1564 default:
1565 return;
1566 }
1567 }
1568
1569 int
1570 send_adb_iop(int cmd, u_char * buffer, void *compRout, void *data)
1571 {
1572 u_char buff[32];
1573 int i, cnt;
1574
1575 if (adbActionState != ADB_ACTION_RUNNING)
1576 return -1;
1577
1578 buff[0] = IOP_ADB_FL_EXPLICIT;
1579 buff[1] = buffer[0];
1580 buff[2] = cmd;
1581 cnt = (int) buff[1];
1582 for (i=0; i<cnt ; i++) {
1583 buff[i+3] = buffer[i+1];
1584 }
1585 return iop_send_msg(ISM_IOP, IOP_CHAN_ADB, buff, cnt+3,
1586 adb_iop_recv, NULL);
1587 }
1588
1589 /*
1590 * adb_pass_up is called by the interrupt-time routines.
1591 * It takes the raw packet data that was received from the
1592 * device and puts it into the queue that the upper half
1593 * processes. It then signals for a soft ADB interrupt which
1594 * will eventually call the upper half routine (adb_soft_intr).
1595 *
1596 * If in->unsol is 0, then this is either the notification
1597 * that the packet was sent (on a LISTEN, for example), or the
1598 * response from the device (on a TALK). The completion routine
1599 * is called only if the user specified one.
1600 *
1601 * If in->unsol is 1, then this packet was unsolicited and
1602 * so we look up the device in the ADB device table to determine
1603 * what it's default service routine is.
1604 *
1605 * If in->ack_only is 1, then we really only need to call
1606 * the completion routine, so don't do any other stuff.
1607 *
1608 * Note that in->data contains the packet header AND data,
1609 * while adbInbound[]->data contains ONLY data.
1610 *
1611 * Note: Called only at interrupt time. Assumes this.
1612 */
1613 void
1614 adb_pass_up(struct adbCommand *in)
1615 {
1616 int i, start = 0, len = 0, cmd = 0;
1617 ADBDataBlock block;
1618
1619 /* temp for testing */
1620 /*u_char *buffer = 0;*/
1621 /*u_char *compdata = 0;*/
1622 /*u_char *comprout = 0;*/
1623
1624 if (adbInCount >= ADB_QUEUE) {
1625 #ifdef ADB_DEBUG
1626 if (adb_debug)
1627 printf_intr("adb: ring buffer overflow\n");
1628 #endif
1629 return;
1630 }
1631
1632 if (in->ack_only) {
1633 len = in->data[0];
1634 cmd = in->cmd;
1635 start = 0;
1636 } else {
1637 switch (adbHardware) {
1638 case ADB_HW_IOP:
1639 case ADB_HW_II:
1640 cmd = in->data[1];
1641 if (in->data[0] < 2)
1642 len = 0;
1643 else
1644 len = in->data[0]-1;
1645 start = 1;
1646 break;
1647
1648 case ADB_HW_IISI:
1649 case ADB_HW_CUDA:
1650 /* If it's unsolicited, accept only ADB data for now */
1651 if (in->unsol)
1652 if (0 != in->data[2])
1653 return;
1654 cmd = in->data[4];
1655 if (in->data[0] < 5)
1656 len = 0;
1657 else
1658 len = in->data[0]-4;
1659 start = 4;
1660 break;
1661
1662 case ADB_HW_PB:
1663 cmd = in->data[1];
1664 if (in->data[0] < 2)
1665 len = 0;
1666 else
1667 len = in->data[0]-1;
1668 start = 1;
1669 break;
1670
1671 case ADB_HW_UNKNOWN:
1672 return;
1673 }
1674
1675 /* Make sure there is a valid device entry for this device */
1676 if (in->unsol) {
1677 /* ignore unsolicited data during adbreinit */
1678 if (adbStarting)
1679 return;
1680 /* get device's comp. routine and data area */
1681 if (-1 == get_adb_info(&block, ((cmd & 0xf0) >> 4)))
1682 return;
1683 }
1684 }
1685
1686 /*
1687 * If this is an unsolicited packet, we need to fill in
1688 * some info so adb_soft_intr can process this packet
1689 * properly. If it's not unsolicited, then use what
1690 * the caller sent us.
1691 */
1692 if (in->unsol) {
1693 adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr;
1694 adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr;
1695 adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data;
1696 } else {
1697 adbInbound[adbInTail].compRout = (void *)in->compRout;
1698 adbInbound[adbInTail].compData = (void *)in->compData;
1699 adbInbound[adbInTail].saveBuf = (void *)in->saveBuf;
1700 }
1701
1702 #ifdef ADB_DEBUG
1703 if (adb_debug && in->data[1] == 2)
1704 printf_intr("adb: caught error\n");
1705 #endif
1706
1707 /* copy the packet data over */
1708 /*
1709 * TO DO: If the *_intr routines fed their incoming data
1710 * directly into an adbCommand struct, which is passed to
1711 * this routine, then we could eliminate this copy.
1712 */
1713 for (i = 1; i <= len; i++)
1714 adbInbound[adbInTail].data[i] = in->data[start+i];
1715
1716 adbInbound[adbInTail].data[0] = len;
1717 adbInbound[adbInTail].cmd = cmd;
1718
1719 adbInCount++;
1720 if (++adbInTail >= ADB_QUEUE)
1721 adbInTail = 0;
1722
1723 /*
1724 * If the debugger is running, call upper half manually.
1725 * Otherwise, trigger a soft interrupt to handle the rest later.
1726 */
1727 if (ite_polling)
1728 adb_soft_intr();
1729 else
1730 setsoftadb();
1731
1732 return;
1733 }
1734
1735
1736 /*
1737 * Called to process the packets after they have been
1738 * placed in the incoming queue.
1739 *
1740 */
1741 void
1742 adb_soft_intr(void)
1743 {
1744 int s, i;
1745 int cmd = 0;
1746 u_char *buffer = 0;
1747 u_char *comprout = 0;
1748 u_char *compdata = 0;
1749
1750 #if 0
1751 s = splhigh();
1752 printf_intr("sr: %x\n", (s & 0x0700));
1753 splx(s);
1754 #endif
1755
1756 /*delay(2*ADB_DELAY);*/
1757
1758 while (adbInCount) {
1759 #ifdef ADB_DEBUG
1760 if (adb_debug & 0x80)
1761 printf_intr("%x %x %x ",
1762 adbInCount, adbInHead, adbInTail);
1763 #endif
1764 /* get the data we need from the queue */
1765 buffer = adbInbound[adbInHead].saveBuf;
1766 comprout = adbInbound[adbInHead].compRout;
1767 compdata = adbInbound[adbInHead].compData;
1768 cmd = adbInbound[adbInHead].cmd;
1769
1770 /* copy over data to data area if it's valid */
1771 /*
1772 * Note that for unsol packets we don't want to copy the
1773 * data anywhere, so buffer was already set to 0.
1774 * For ack_only buffer was set to 0, so don't copy.
1775 */
1776 if (buffer)
1777 for (i = 0; i <= adbInbound[adbInHead].data[0]; i++)
1778 *(buffer+i) = adbInbound[adbInHead].data[i];
1779
1780 #ifdef ADB_DEBUG
1781 if (adb_debug & 0x80) {
1782 printf_intr("%p %p %p %x ",
1783 buffer, comprout, compdata, (short)cmd);
1784 printf_intr("buf: ");
1785 print_single(adbInbound[adbInHead].data);
1786 }
1787 #endif
1788
1789 /* call default completion routine if it's valid */
1790 if (comprout) {
1791 #ifdef __NetBSD__
1792 asm(" movml #0xffff,sp@- | save all registers
1793 movl %0,a2 | compdata
1794 movl %1,a1 | comprout
1795 movl %2,a0 | buffer
1796 movl %3,d0 | cmd
1797 jbsr a1@ | go call the routine
1798 movml sp@+,#0xffff | restore all registers"
1799 :
1800 : "g"(compdata), "g"(comprout),
1801 "g"(buffer), "g"(cmd)
1802 : "d0", "a0", "a1", "a2");
1803 #else /* for macos based testing */
1804 asm
1805 {
1806 movem.l a0/a1/a2/d0, -(a7)
1807 move.l compdata, a2
1808 move.l comprout, a1
1809 move.l buffer, a0
1810 move.w cmd, d0
1811 jsr(a1)
1812 movem.l(a7)+, d0/a2/a1/a0
1813 }
1814 #endif
1815 }
1816
1817 s = splhigh();
1818 adbInCount--;
1819 if (++adbInHead >= ADB_QUEUE)
1820 adbInHead = 0;
1821 splx(s);
1822
1823 }
1824 return;
1825 }
1826
1827
1828 /*
1829 * This is my version of the ADBOp routine. It mainly just calls the
1830 * hardware-specific routine.
1831 *
1832 * data : pointer to data area to be used by compRout
1833 * compRout : completion routine
1834 * buffer : for LISTEN: points to data to send - MAX 8 data bytes,
1835 * byte 0 = # of bytes
1836 * : for TALK: points to place to save return data
1837 * command : the adb command to send
1838 * result : 0 = success
1839 * : -1 = could not complete
1840 */
1841 int
1842 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command)
1843 {
1844 int result;
1845
1846 switch (adbHardware) {
1847 case ADB_HW_II:
1848 result = send_adb_II((u_char *)0, (u_char *)buffer,
1849 (void *)compRout, (void *)data, (int)command);
1850 if (result == 0)
1851 return 0;
1852 else
1853 return -1;
1854 break;
1855
1856 case ADB_HW_IOP:
1857 #if 0
1858 result = send_adb_iop((int)command, (u_char *)buffer,
1859 (void *)compRout, (void *)data);
1860 if (result == 0)
1861 return 0;
1862 else
1863 #endif
1864 return -1;
1865 break;
1866
1867 case ADB_HW_IISI:
1868 result = send_adb_IIsi((u_char *)0, (u_char *)buffer,
1869 (void *)compRout, (void *)data, (int)command);
1870 /*
1871 * I wish I knew why this delay is needed. It usually needs to
1872 * be here when several commands are sent in close succession,
1873 * especially early in device probes when doing collision
1874 * detection. It must be some race condition. Sigh. - jpw
1875 */
1876 delay(100);
1877 if (result == 0)
1878 return 0;
1879 else
1880 return -1;
1881 break;
1882
1883 case ADB_HW_PB:
1884 result = pm_adb_op((u_char *)buffer, (void *)compRout,
1885 (void *)data, (int)command);
1886
1887 if (result == 0)
1888 return 0;
1889 else
1890 return -1;
1891 break;
1892
1893 case ADB_HW_CUDA:
1894 result = send_adb_cuda((u_char *)0, (u_char *)buffer,
1895 (void *)compRout, (void *)data, (int)command);
1896 if (result == 0)
1897 return 0;
1898 else
1899 return -1;
1900 break;
1901
1902 case ADB_HW_UNKNOWN:
1903 default:
1904 return -1;
1905 }
1906 }
1907
1908
1909 /*
1910 * adb_hw_setup
1911 * This routine sets up the possible machine specific hardware
1912 * config (mainly VIA settings) for the various models.
1913 */
1914 void
1915 adb_hw_setup(void)
1916 {
1917 volatile int i;
1918 u_char send_string[ADB_MAX_MSG_LENGTH];
1919
1920 switch (adbHardware) {
1921 case ADB_HW_II:
1922 via1_register_irq(2, adb_intr_II, NULL);
1923
1924 via_reg(VIA1, vDirB) |= 0x30; /* register B bits 4 and 5:
1925 * outputs */
1926 via_reg(VIA1, vDirB) &= 0xf7; /* register B bit 3: input */
1927 via_reg(VIA1, vACR) &= ~vSR_OUT; /* make sure SR is set
1928 * to IN (II, IIsi) */
1929 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1930 * hardware (II, IIsi) */
1931 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1932 * code only */
1933 via_reg(VIA1, vIER) = 0x84; /* make sure VIA interrupts
1934 * are on (II, IIsi) */
1935 ADB_SET_STATE_IDLE_II(); /* set ADB bus state to idle */
1936
1937 ADB_VIA_CLR_INTR(); /* clear interrupt */
1938 break;
1939
1940 case ADB_HW_IOP:
1941 /* adbActionState = ADB_ACTION_RUNNING; */
1942 via_reg(VIA1, vIER) = 0x84;
1943 via_reg(VIA1, vIFR) = 0x04;
1944 break;
1945
1946 case ADB_HW_IISI:
1947 via1_register_irq(2, adb_intr_IIsi, NULL);
1948 via_reg(VIA1, vDirB) |= 0x30; /* register B bits 4 and 5:
1949 * outputs */
1950 via_reg(VIA1, vDirB) &= 0xf7; /* register B bit 3: input */
1951 via_reg(VIA1, vACR) &= ~vSR_OUT; /* make sure SR is set
1952 * to IN (II, IIsi) */
1953 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1954 * hardware (II, IIsi) */
1955 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1956 * code only */
1957 via_reg(VIA1, vIER) = 0x84; /* make sure VIA interrupts
1958 * are on (II, IIsi) */
1959 ADB_SET_STATE_IDLE_IISI(); /* set ADB bus state to idle */
1960
1961 /* get those pesky clock ticks we missed while booting */
1962 for (i = 0; i < 30; i++) {
1963 delay(ADB_DELAY);
1964 adb_hw_setup_IIsi(send_string);
1965 #ifdef ADB_DEBUG
1966 if (adb_debug) {
1967 printf_intr("adb: cleanup: ");
1968 print_single(send_string);
1969 }
1970 #endif
1971 delay(ADB_DELAY);
1972 if (ADB_INTR_IS_OFF)
1973 break;
1974 }
1975 break;
1976
1977 case ADB_HW_PB:
1978 /*
1979 * XXX - really PM_VIA_CLR_INTR - should we put it in
1980 * pm_direct.h?
1981 */
1982 pm_hw_setup();
1983 break;
1984
1985 case ADB_HW_CUDA:
1986 via1_register_irq(2, adb_intr_cuda, NULL);
1987 via_reg(VIA1, vDirB) |= 0x30; /* register B bits 4 and 5:
1988 * outputs */
1989 via_reg(VIA1, vDirB) &= 0xf7; /* register B bit 3: input */
1990 via_reg(VIA1, vACR) &= ~vSR_OUT; /* make sure SR is set
1991 * to IN */
1992 via_reg(VIA1, vACR) = (via_reg(VIA1, vACR) | 0x0c) & ~0x10;
1993 adbActionState = ADB_ACTION_IDLE; /* used by all types of
1994 * hardware */
1995 adbBusState = ADB_BUS_IDLE; /* this var. used in II-series
1996 * code only */
1997 via_reg(VIA1, vIER) = 0x84; /* make sure VIA interrupts
1998 * are on */
1999 ADB_SET_STATE_IDLE_CUDA(); /* set ADB bus state to idle */
2000
2001 /* sort of a device reset */
2002 i = ADB_SR(); /* clear interrupt */
2003 ADB_VIA_INTR_DISABLE(); /* no interrupts while clearing */
2004 ADB_SET_STATE_IDLE_CUDA(); /* reset state to idle */
2005 delay(ADB_DELAY);
2006 ADB_SET_STATE_TIP(); /* signal start of frame */
2007 delay(ADB_DELAY);
2008 ADB_TOGGLE_STATE_ACK_CUDA();
2009 delay(ADB_DELAY);
2010 ADB_CLR_STATE_TIP();
2011 delay(ADB_DELAY);
2012 ADB_SET_STATE_IDLE_CUDA(); /* back to idle state */
2013 i = ADB_SR(); /* clear interrupt */
2014 ADB_VIA_INTR_ENABLE(); /* ints ok now */
2015 break;
2016
2017 case ADB_HW_UNKNOWN:
2018 default:
2019 via_reg(VIA1, vIER) = 0x04; /* turn interrupts off - TO
2020 * DO: turn PB ints off? */
2021 return;
2022 break;
2023 }
2024 }
2025
2026
2027 /*
2028 * adb_hw_setup_IIsi
2029 * This is sort of a "read" routine that forces the adb hardware through a read cycle
2030 * if there is something waiting. This helps "clean up" any commands that may have gotten
2031 * stuck or stopped during the boot process.
2032 *
2033 */
2034 void
2035 adb_hw_setup_IIsi(u_char * buffer)
2036 {
2037 int i;
2038 int dummy;
2039 int s;
2040 long my_time;
2041 int endofframe;
2042
2043 delay(ADB_DELAY);
2044
2045 i = 1; /* skip over [0] */
2046 s = splhigh(); /* block ALL interrupts while we are working */
2047 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */
2048 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */
2049 /* this is required, especially on faster machines */
2050 delay(ADB_DELAY);
2051
2052 if (ADB_INTR_IS_ON) {
2053 ADB_SET_STATE_ACTIVE(); /* signal start of data frame */
2054
2055 endofframe = 0;
2056 while (0 == endofframe) {
2057 /*
2058 * Poll for ADB interrupt and watch for timeout.
2059 * If time out, keep going in hopes of not hanging
2060 * the ADB chip - I think
2061 */
2062 my_time = ADB_DELAY * 5;
2063 while ((ADB_SR_INTR_IS_OFF) && (my_time-- > 0))
2064 dummy = via_reg(VIA1, vBufB);
2065
2066 buffer[i++] = ADB_SR(); /* reset interrupt flag by
2067 * reading vSR */
2068 /*
2069 * Perhaps put in a check here that ignores all data
2070 * after the first ADB_MAX_MSG_LENGTH bytes ???
2071 */
2072 if (ADB_INTR_IS_OFF) /* check for end of frame */
2073 endofframe = 1;
2074
2075 ADB_SET_STATE_ACKON(); /* send ACK to ADB chip */
2076 delay(ADB_DELAY); /* delay */
2077 ADB_SET_STATE_ACKOFF(); /* send ACK to ADB chip */
2078 }
2079 ADB_SET_STATE_INACTIVE(); /* signal end of frame and
2080 * delay */
2081
2082 /* probably don't need to delay this long */
2083 delay(ADB_DELAY);
2084 }
2085 buffer[0] = --i; /* [0] is length of message */
2086 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */
2087 splx(s); /* restore interrupts */
2088
2089 return;
2090 } /* adb_hw_setup_IIsi */
2091
2092
2093
2094 /*
2095 * adb_reinit sets up the adb stuff
2096 *
2097 */
2098 void
2099 adb_reinit(void)
2100 {
2101 u_char send_string[ADB_MAX_MSG_LENGTH];
2102 int s = 0;
2103 volatile int i, x;
2104 int command;
2105 int result;
2106 int saveptr; /* point to next free relocation address */
2107 int device;
2108 int nonewtimes; /* times thru loop w/o any new devices */
2109 ADBDataBlock data; /* temp. holder for getting device info */
2110
2111 (void)(&s); /* work around lame GCC bug */
2112
2113 /* Make sure we are not interrupted while building the table. */
2114 /* ints must be on for PB & IOP (at least, for now) */
2115 if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2116 s = splhigh();
2117
2118 ADBNumDevices = 0; /* no devices yet */
2119
2120 /* Let intr routines know we are running reinit */
2121 adbStarting = 1;
2122
2123 /*
2124 * Initialize the ADB table. For now, we'll always use the same table
2125 * that is defined at the beginning of this file - no mallocs.
2126 */
2127 for (i = 0; i < 16; i++)
2128 ADBDevTable[i].devType = 0;
2129
2130 adb_setup_hw_type(); /* setup hardware type */
2131
2132 adb_hw_setup(); /* init the VIA bits and hard reset ADB */
2133
2134 /* send an ADB reset first */
2135 adb_op_sync((Ptr)0, (Ptr)0, (Ptr)0, (short)0x00);
2136 delay(4000);
2137
2138 /*
2139 * Probe for ADB devices. Probe devices 1-15 quickly to determine
2140 * which device addresses are in use and which are free. For each
2141 * address that is in use, move the device at that address to a higher
2142 * free address. Continue doing this at that address until no device
2143 * responds at that address. Then move the last device that was moved
2144 * back to the original address. Do this for the remaining addresses
2145 * that we determined were in use.
2146 *
2147 * When finished, do this entire process over again with the updated
2148 * list of in use addresses. Do this until no new devices have been
2149 * found in 20 passes though the in use address list. (This probably
2150 * seems long and complicated, but it's the best way to detect multiple
2151 * devices at the same address - sometimes it takes a couple of tries
2152 * before the collision is detected.)
2153 */
2154
2155 /* initial scan through the devices */
2156 for (i = 1; i < 16; i++) {
2157 command = (int)(0x0f | ((int)(i & 0x000f) << 4)); /* talk R3 */
2158 result = adb_op_sync((Ptr)send_string, (Ptr)0,
2159 (Ptr)0, (short)command);
2160 if (0x00 != send_string[0]) { /* anything come back ?? */
2161 ADBDevTable[++ADBNumDevices].devType =
2162 (int)(send_string[2]);
2163 ADBDevTable[ADBNumDevices].origAddr = i;
2164 ADBDevTable[ADBNumDevices].currentAddr = i;
2165 ADBDevTable[ADBNumDevices].DataAreaAddr =
2166 (long)0;
2167 ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
2168 pm_check_adb_devices(i); /* tell pm driver device
2169 * is here */
2170 }
2171 }
2172
2173 /* find highest unused address */
2174 for (saveptr = 15; saveptr > 0; saveptr--)
2175 if (-1 == get_adb_info(&data, saveptr))
2176 break;
2177
2178 if (saveptr == 0) /* no free addresses??? */
2179 saveptr = 15;
2180
2181 #ifdef ADB_DEBUG
2182 if (adb_debug & 0x80) {
2183 printf_intr("first free is: 0x%02x\n", saveptr);
2184 printf_intr("devices: %i\n", ADBNumDevices);
2185 }
2186 #endif
2187
2188 nonewtimes = 0; /* no loops w/o new devices */
2189 while (nonewtimes++ < 11) {
2190 for (i = 1; i <= ADBNumDevices; i++) {
2191 device = ADBDevTable[i].currentAddr;
2192 #ifdef ADB_DEBUG
2193 if (adb_debug & 0x80)
2194 printf_intr("moving device 0x%02x to 0x%02x "
2195 "(index 0x%02x) ", device, saveptr, i);
2196 #endif
2197
2198 /* send TALK R3 to address */
2199 command = (int)(0x0f | ((int)(device & 0x000f) << 4));
2200 adb_op_sync((Ptr)send_string, (Ptr)0,
2201 (Ptr)0, (short)command);
2202
2203 /* move device to higher address */
2204 command = (int)(0x0b | ((int)(device & 0x000f) << 4));
2205 send_string[0] = 2;
2206 send_string[1] = (u_char)(saveptr | 0x60);
2207 send_string[2] = 0xfe;
2208 adb_op_sync((Ptr)send_string, (Ptr)0,
2209 (Ptr)0, (short)command);
2210
2211 /* send TALK R3 - anything at old address? */
2212 command = (int)(0x0f | ((int)(device & 0x000f) << 4));
2213 result = adb_op_sync((Ptr)send_string, (Ptr)0,
2214 (Ptr)0, (short)command);
2215 if (send_string[0] != 0) {
2216 /* new device found */
2217 /* update data for previously moved device */
2218 ADBDevTable[i].currentAddr = saveptr;
2219 #ifdef ADB_DEBUG
2220 if (adb_debug & 0x80)
2221 printf_intr("old device at index %i\n",i);
2222 #endif
2223 /* add new device in table */
2224 #ifdef ADB_DEBUG
2225 if (adb_debug & 0x80)
2226 printf_intr("new device found\n");
2227 #endif
2228 ADBDevTable[++ADBNumDevices].devType =
2229 (int)(send_string[2]);
2230 ADBDevTable[ADBNumDevices].origAddr = device;
2231 ADBDevTable[ADBNumDevices].currentAddr = device;
2232 /* These will be set correctly in adbsys.c */
2233 /* Until then, unsol. data will be ignored. */
2234 ADBDevTable[ADBNumDevices].DataAreaAddr =
2235 (long)0;
2236 ADBDevTable[ADBNumDevices].ServiceRtPtr =
2237 (void *)0;
2238 /* find next unused address */
2239 for (x = saveptr; x > 0; x--)
2240 if (-1 == get_adb_info(&data, x)) {
2241 saveptr = x;
2242 break;
2243 }
2244 #ifdef ADB_DEBUG
2245 if (adb_debug & 0x80)
2246 printf_intr("new free is 0x%02x\n",
2247 saveptr);
2248 #endif
2249 nonewtimes = 0;
2250 /* tell pm driver device is here */
2251 pm_check_adb_devices(device);
2252 } else {
2253 #ifdef ADB_DEBUG
2254 if (adb_debug & 0x80)
2255 printf_intr("moving back...\n");
2256 #endif
2257 /* move old device back */
2258 command = (int)(0x0b | ((int)(saveptr & 0x000f) << 4));
2259 send_string[0] = 2;
2260 send_string[1] = (u_char)(device | 0x60);
2261 send_string[2] = 0xfe;
2262 adb_op_sync((Ptr)send_string, (Ptr)0,
2263 (Ptr)0, (short)command);
2264 }
2265 }
2266 }
2267
2268 #ifdef ADB_DEBUG
2269 if (adb_debug) {
2270 for (i = 1; i <= ADBNumDevices; i++) {
2271 x = get_ind_adb_info(&data, i);
2272 if (x != -1)
2273 printf_intr("index 0x%x, addr 0x%x, type 0x%hx\n",
2274 i, x, data.devType);
2275 }
2276 }
2277 #endif
2278
2279 #ifndef MRG_ADB
2280 /* enable the programmer's switch, if we have one */
2281 adb_prog_switch_enable();
2282 #endif
2283
2284 #ifdef ADB_DEBUG
2285 if (adb_debug) {
2286 if (0 == ADBNumDevices) /* tell user if no devices found */
2287 printf_intr("adb: no devices found\n");
2288 }
2289 #endif
2290
2291 adbStarting = 0; /* not starting anymore */
2292 #ifdef ADB_DEBUG
2293 if (adb_debug)
2294 printf_intr("adb: ADBReInit complete\n");
2295 #endif
2296
2297 if (adbHardware == ADB_HW_CUDA)
2298 timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
2299
2300 /* ints must be on for PB & IOP (at least, for now) */
2301 if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2302 splx(s);
2303
2304 return;
2305 }
2306
2307
2308 /*
2309 * adb_comp_exec
2310 * This is a general routine that calls the completion routine if there is one.
2311 * NOTE: This routine is now only used by pm_direct.c
2312 * All the code in this file (adb_direct.c) uses
2313 * the adb_pass_up routine now.
2314 */
2315 void
2316 adb_comp_exec(void)
2317 {
2318 if ((long)0 != adbCompRout) /* don't call if empty return location */
2319 #ifdef __NetBSD__
2320 asm(" movml #0xffff,sp@- | save all registers
2321 movl %0,a2 | adbCompData
2322 movl %1,a1 | adbCompRout
2323 movl %2,a0 | adbBuffer
2324 movl %3,d0 | adbWaitingCmd
2325 jbsr a1@ | go call the routine
2326 movml sp@+,#0xffff | restore all registers"
2327 :
2328 : "g"(adbCompData), "g"(adbCompRout),
2329 "g"(adbBuffer), "g"(adbWaitingCmd)
2330 : "d0", "a0", "a1", "a2");
2331 #else /* for Mac OS-based testing */
2332 asm {
2333 movem.l a0/a1/a2/d0, -(a7)
2334 move.l adbCompData, a2
2335 move.l adbCompRout, a1
2336 move.l adbBuffer, a0
2337 move.w adbWaitingCmd, d0
2338 jsr(a1)
2339 movem.l(a7) +, d0/a2/a1/a0
2340 }
2341 #endif
2342 }
2343
2344
2345 /*
2346 * adb_cmd_result
2347 *
2348 * This routine lets the caller know whether the specified adb command string
2349 * should expect a returned result, such as a TALK command.
2350 *
2351 * returns: 0 if a result should be expected
2352 * 1 if a result should NOT be expected
2353 */
2354 int
2355 adb_cmd_result(u_char *in)
2356 {
2357 switch (adbHardware) {
2358 case ADB_HW_IOP:
2359 case ADB_HW_II:
2360 /* was it an ADB talk command? */
2361 if ((in[1] & 0x0c) == 0x0c)
2362 return 0;
2363 return 1;
2364
2365 case ADB_HW_IISI:
2366 case ADB_HW_CUDA:
2367 /* was it an ADB talk command? */
2368 if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
2369 return 0;
2370 /* was it an RTC/PRAM read date/time? */
2371 if ((in[1] == 0x01) && (in[2] == 0x03))
2372 return 0;
2373 return 1;
2374
2375 case ADB_HW_PB:
2376 return 1;
2377
2378 case ADB_HW_UNKNOWN:
2379 default:
2380 return 1;
2381 }
2382 }
2383
2384
2385 /*
2386 * adb_cmd_extra
2387 *
2388 * This routine lets the caller know whether the specified adb command string
2389 * may have extra data appended to the end of it, such as a LISTEN command.
2390 *
2391 * returns: 0 if extra data is allowed
2392 * 1 if extra data is NOT allowed
2393 */
2394 int
2395 adb_cmd_extra(u_char *in)
2396 {
2397 switch (adbHardware) {
2398 case ADB_HW_II:
2399 case ADB_HW_IOP:
2400 if ((in[1] & 0x0c) == 0x08) /* was it a listen command? */
2401 return 0;
2402 return 1;
2403
2404 case ADB_HW_IISI:
2405 case ADB_HW_CUDA:
2406 /*
2407 * TO DO: support needs to be added to recognize RTC and PRAM
2408 * commands
2409 */
2410 if ((in[2] & 0x0c) == 0x08) /* was it a listen command? */
2411 return 0;
2412 /* add others later */
2413 return 1;
2414
2415 case ADB_HW_PB:
2416 return 1;
2417
2418 case ADB_HW_UNKNOWN:
2419 default:
2420 return 1;
2421 }
2422 }
2423
2424
2425 /*
2426 * adb_op_sync
2427 *
2428 * This routine does exactly what the adb_op routine does, except that after
2429 * the adb_op is called, it waits until the return value is present before
2430 * returning.
2431 *
2432 * NOTE: The user specified compRout is ignored, since this routine specifies
2433 * it's own to adb_op, which is why you really called this in the first place
2434 * anyway.
2435 */
2436 int
2437 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
2438 {
2439 int result;
2440 volatile int flag = 0;
2441
2442 result = adb_op(buffer, (void *)adb_op_comprout,
2443 (void *)&flag, command); /* send command */
2444 if (result == 0) /* send ok? */
2445 while (0 == flag)
2446 /* wait for compl. routine */;
2447
2448 return result;
2449 }
2450
2451
2452 /*
2453 * adb_op_comprout
2454 *
2455 * This function is used by the adb_op_sync routine so it knows when the
2456 * function is done.
2457 */
2458 void
2459 adb_op_comprout(void)
2460 {
2461 #ifdef __NetBSD__
2462 asm("movw #1,a2@ | update flag value");
2463 #else /* for macos based testing */
2464 asm {
2465 move.w #1,(a2) } /* update flag value */
2466 #endif
2467 }
2468
2469 void
2470 adb_setup_hw_type(void)
2471 {
2472 long response;
2473
2474 response = mac68k_machine.machineid;
2475
2476 /*
2477 * Determine what type of ADB hardware we are running on.
2478 */
2479 switch (response) {
2480 case MACH_MACC610: /* Centris 610 */
2481 case MACH_MACC650: /* Centris 650 */
2482 case MACH_MACII: /* II */
2483 case MACH_MACIICI: /* IIci */
2484 case MACH_MACIICX: /* IIcx */
2485 case MACH_MACIIX: /* IIx */
2486 case MACH_MACQ610: /* Quadra 610 */
2487 case MACH_MACQ650: /* Quadra 650 */
2488 case MACH_MACQ700: /* Quadra 700 */
2489 case MACH_MACQ800: /* Quadra 800 */
2490 case MACH_MACSE30: /* SE/30 */
2491 adbHardware = ADB_HW_II;
2492 #ifdef ADB_DEBUG
2493 if (adb_debug)
2494 printf_intr("adb: using II series hardware support\n");
2495 #endif
2496 break;
2497
2498 case MACH_MACCLASSICII: /* Classic II */
2499 case MACH_MACLCII: /* LC II, Performa 400/405/430 */
2500 case MACH_MACLCIII: /* LC III, Performa 450 */
2501 case MACH_MACIISI: /* IIsi */
2502 case MACH_MACIIVI: /* IIvi */
2503 case MACH_MACIIVX: /* IIvx */
2504 case MACH_MACP460: /* Performa 460/465/467 */
2505 case MACH_MACP600: /* Performa 600 */
2506 adbHardware = ADB_HW_IISI;
2507 #ifdef ADB_DEBUG
2508 if (adb_debug)
2509 printf_intr("adb: using IIsi series hardware support\n");
2510 #endif
2511 break;
2512
2513 case MACH_MACPB140: /* PowerBook 140 */
2514 case MACH_MACPB145: /* PowerBook 145 */
2515 case MACH_MACPB150: /* PowerBook 150 */
2516 case MACH_MACPB160: /* PowerBook 160 */
2517 case MACH_MACPB165: /* PowerBook 165 */
2518 case MACH_MACPB165C: /* PowerBook 165c */
2519 case MACH_MACPB170: /* PowerBook 170 */
2520 case MACH_MACPB180: /* PowerBook 180 */
2521 case MACH_MACPB180C: /* PowerBook 180c */
2522 adbHardware = ADB_HW_PB;
2523 pm_setup_adb();
2524 #ifdef ADB_DEBUG
2525 if (adb_debug)
2526 printf_intr("adb: using PowerBook 100-series hardware support\n");
2527 #endif
2528 break;
2529
2530 case MACH_MACPB210: /* PowerBook Duo 210 */
2531 case MACH_MACPB230: /* PowerBook Duo 230 */
2532 case MACH_MACPB250: /* PowerBook Duo 250 */
2533 case MACH_MACPB270: /* PowerBook Duo 270 */
2534 case MACH_MACPB280: /* PowerBook Duo 280 */
2535 case MACH_MACPB280C: /* PowerBook Duo 280c */
2536 case MACH_MACPB500: /* PowerBook 500 series */
2537 adbHardware = ADB_HW_PB;
2538 pm_setup_adb();
2539 #ifdef ADB_DEBUG
2540 if (adb_debug)
2541 printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2542 #endif
2543 break;
2544
2545 case MACH_MACC660AV: /* Centris 660AV */
2546 case MACH_MACCCLASSIC: /* Color Classic */
2547 case MACH_MACCCLASSICII: /* Color Classic II */
2548 case MACH_MACLC475: /* LC 475, Performa 475/476 */
2549 case MACH_MACLC475_33: /* Clock-chipped 47x */
2550 case MACH_MACLC520: /* LC 520 */
2551 case MACH_MACLC575: /* LC 575, Performa 575/577/578 */
2552 case MACH_MACP550: /* LC 550, Performa 550 */
2553 case MACH_MACP580: /* Performa 580/588 */
2554 case MACH_MACQ605: /* Quadra 605 */
2555 case MACH_MACQ605_33: /* Clock-chipped Quadra 605 */
2556 case MACH_MACQ630: /* LC 630, Performa 630, Quadra 630 */
2557 case MACH_MACQ840AV: /* Quadra 840AV */
2558 adbHardware = ADB_HW_CUDA;
2559 #ifdef ADB_DEBUG
2560 if (adb_debug)
2561 printf_intr("adb: using Cuda series hardware support\n");
2562 #endif
2563 break;
2564
2565 case MACH_MACQ900: /* Quadra 900 */
2566 case MACH_MACQ950: /* Quadra 950 */
2567 case MACH_MACIIFX: /* Mac IIfx */
2568 adbHardware = ADB_HW_IOP;
2569 iop_register_listener(ISM_IOP, IOP_CHAN_ADB, adb_iop_recv, NULL);
2570 #ifdef ADB_DEBUG
2571 if (adb_debug)
2572 printf_intr("adb: using IOP-based ADB\n");
2573 #endif
2574 break;
2575
2576 default:
2577 adbHardware = ADB_HW_UNKNOWN;
2578 #ifdef ADB_DEBUG
2579 if (adb_debug) {
2580 printf_intr("adb: hardware type unknown for this machine\n");
2581 printf_intr("adb: ADB support is disabled\n");
2582 }
2583 #endif
2584 break;
2585 }
2586
2587 /*
2588 * Determine whether this machine has ADB based soft power.
2589 */
2590 switch (response) {
2591 case MACH_MACCCLASSIC: /* Color Classic */
2592 case MACH_MACCCLASSICII: /* Color Classic II */
2593 case MACH_MACIISI: /* IIsi */
2594 case MACH_MACIIVI: /* IIvi */
2595 case MACH_MACIIVX: /* IIvx */
2596 case MACH_MACLC520: /* LC 520 */
2597 case MACH_MACLC575: /* LC 575, Performa 575/577/578 */
2598 case MACH_MACP550: /* LC 550, Performa 550 */
2599 case MACH_MACP580: /* Performa 580/588 */
2600 case MACH_MACP600: /* Performa 600 */
2601 case MACH_MACQ630: /* LC 630, Performa 630, Quadra 630 */
2602 case MACH_MACQ840AV: /* Quadra 840AV */
2603 adbSoftPower = 1;
2604 break;
2605 }
2606 }
2607
2608 int
2609 count_adbs(void)
2610 {
2611 int i;
2612 int found;
2613
2614 found = 0;
2615
2616 for (i = 1; i < 16; i++)
2617 if (0 != ADBDevTable[i].devType)
2618 found++;
2619
2620 return found;
2621 }
2622
2623 int
2624 get_ind_adb_info(ADBDataBlock * info, int index)
2625 {
2626 if ((index < 1) || (index > 15)) /* check range 1-15 */
2627 return (-1);
2628
2629 #ifdef ADB_DEBUG
2630 if (adb_debug & 0x80)
2631 printf_intr("index 0x%x devType is: 0x%x\n", index,
2632 ADBDevTable[index].devType);
2633 #endif
2634 if (0 == ADBDevTable[index].devType) /* make sure it's a valid entry */
2635 return (-1);
2636
2637 info->devType = (unsigned char)(ADBDevTable[index].devType);
2638 info->origADBAddr = (unsigned char)(ADBDevTable[index].origAddr);
2639 info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
2640 info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
2641
2642 return (ADBDevTable[index].currentAddr);
2643 }
2644
2645 int
2646 get_adb_info(ADBDataBlock * info, int adbAddr)
2647 {
2648 int i;
2649
2650 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
2651 return (-1);
2652
2653 for (i = 1; i < 15; i++)
2654 if (ADBDevTable[i].currentAddr == adbAddr) {
2655 info->devType = (unsigned char)(ADBDevTable[i].devType);
2656 info->origADBAddr = (unsigned char)(ADBDevTable[i].origAddr);
2657 info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2658 info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2659 return 0; /* found */
2660 }
2661
2662 return (-1); /* not found */
2663 }
2664
2665 int
2666 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
2667 {
2668 int i;
2669
2670 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
2671 return (-1);
2672
2673 for (i = 1; i < 15; i++)
2674 if (ADBDevTable[i].currentAddr == adbAddr) {
2675 ADBDevTable[i].ServiceRtPtr =
2676 (void *)(info->siServiceRtPtr);
2677 ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2678 return 0; /* found */
2679 }
2680
2681 return (-1); /* not found */
2682
2683 }
2684
2685 #ifndef MRG_ADB
2686 long
2687 mrg_adbintr(void)
2688 {
2689 adb_intr(NULL);
2690 return 1; /* mimic mrg_adbintr in macrom.h just in case */
2691 }
2692
2693 long
2694 mrg_pmintr(void)
2695 {
2696 pm_intr(NULL);
2697 return 1; /* mimic mrg_pmintr in macrom.h just in case */
2698 }
2699
2700 /* caller should really use machine-independant version: getPramTime */
2701 /* this version does pseudo-adb access only */
2702 int
2703 adb_read_date_time(unsigned long *time)
2704 {
2705 u_char output[ADB_MAX_MSG_LENGTH];
2706 int result;
2707 volatile int flag = 0;
2708
2709 switch (adbHardware) {
2710 case ADB_HW_II:
2711 return -1;
2712
2713 case ADB_HW_IOP:
2714 return -1;
2715
2716 case ADB_HW_IISI:
2717 output[0] = 0x02; /* 2 byte message */
2718 output[1] = 0x01; /* to pram/rtc device */
2719 output[2] = 0x03; /* read date/time */
2720 result = send_adb_IIsi((u_char *)output, (u_char *)output,
2721 (void *)adb_op_comprout, (int *)&flag, (int)0);
2722 if (result != 0) /* exit if not sent */
2723 return -1;
2724
2725 while (0 == flag) /* wait for result */
2726 ;
2727
2728 *time = (long)(*(long *)(output + 1));
2729 return 0;
2730
2731 case ADB_HW_PB:
2732 return -1;
2733
2734 case ADB_HW_CUDA:
2735 output[0] = 0x02; /* 2 byte message */
2736 output[1] = 0x01; /* to pram/rtc device */
2737 output[2] = 0x03; /* read date/time */
2738 result = send_adb_cuda((u_char *)output, (u_char *)output,
2739 (void *)adb_op_comprout, (void *)&flag, (int)0);
2740 if (result != 0) /* exit if not sent */
2741 return -1;
2742
2743 while (0 == flag) /* wait for result */
2744 ;
2745
2746 *time = (long)(*(long *)(output + 1));
2747 return 0;
2748
2749 case ADB_HW_UNKNOWN:
2750 default:
2751 return -1;
2752 }
2753 }
2754
2755 /* caller should really use machine-independant version: setPramTime */
2756 /* this version does pseudo-adb access only */
2757 int
2758 adb_set_date_time(unsigned long time)
2759 {
2760 u_char output[ADB_MAX_MSG_LENGTH];
2761 int result;
2762 volatile int flag = 0;
2763
2764 switch (adbHardware) {
2765 case ADB_HW_II:
2766 return -1;
2767
2768 case ADB_HW_IOP:
2769 return -1;
2770
2771 case ADB_HW_IISI:
2772 output[0] = 0x06; /* 6 byte message */
2773 output[1] = 0x01; /* to pram/rtc device */
2774 output[2] = 0x09; /* set date/time */
2775 output[3] = (u_char)(time >> 24);
2776 output[4] = (u_char)(time >> 16);
2777 output[5] = (u_char)(time >> 8);
2778 output[6] = (u_char)(time);
2779 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2780 (void *)adb_op_comprout, (void *)&flag, (int)0);
2781 if (result != 0) /* exit if not sent */
2782 return -1;
2783
2784 while (0 == flag) /* wait for send to finish */
2785 ;
2786
2787 return 0;
2788
2789 case ADB_HW_PB:
2790 return -1;
2791
2792 case ADB_HW_CUDA:
2793 output[0] = 0x06; /* 6 byte message */
2794 output[1] = 0x01; /* to pram/rtc device */
2795 output[2] = 0x09; /* set date/time */
2796 output[3] = (u_char)(time >> 24);
2797 output[4] = (u_char)(time >> 16);
2798 output[5] = (u_char)(time >> 8);
2799 output[6] = (u_char)(time);
2800 result = send_adb_cuda((u_char *)output, (u_char *)0,
2801 (void *)adb_op_comprout, (void *)&flag, (int)0);
2802 if (result != 0) /* exit if not sent */
2803 return -1;
2804
2805 while (0 == flag) /* wait for send to finish */
2806 ;
2807
2808 return 0;
2809
2810 case ADB_HW_UNKNOWN:
2811 default:
2812 return -1;
2813 }
2814 }
2815
2816
2817 int
2818 adb_poweroff(void)
2819 {
2820 u_char output[ADB_MAX_MSG_LENGTH];
2821 int result;
2822
2823 if (!adbSoftPower)
2824 return -1;
2825
2826 switch (adbHardware) {
2827 case ADB_HW_IISI:
2828 output[0] = 0x02; /* 2 byte message */
2829 output[1] = 0x01; /* to pram/rtc/soft-power device */
2830 output[2] = 0x0a; /* set date/time */
2831 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2832 (void *)0, (void *)0, (int)0);
2833 if (result != 0) /* exit if not sent */
2834 return -1;
2835
2836 for (;;); /* wait for power off */
2837
2838 return 0;
2839
2840 case ADB_HW_PB:
2841 return -1;
2842
2843 case ADB_HW_CUDA:
2844 output[0] = 0x02; /* 2 byte message */
2845 output[1] = 0x01; /* to pram/rtc/soft-power device */
2846 output[2] = 0x0a; /* set date/time */
2847 result = send_adb_cuda((u_char *)output, (u_char *)0,
2848 (void *)0, (void *)0, (int)0);
2849 if (result != 0) /* exit if not sent */
2850 return -1;
2851
2852 for (;;); /* wait for power off */
2853
2854 return 0;
2855
2856 case ADB_HW_II: /* II models don't do ADB soft power */
2857 case ADB_HW_IOP: /* IOP models don't do ADB soft power */
2858 case ADB_HW_UNKNOWN:
2859 default:
2860 return -1;
2861 }
2862 }
2863
2864 int
2865 adb_prog_switch_enable(void)
2866 {
2867 u_char output[ADB_MAX_MSG_LENGTH];
2868 int result;
2869 volatile int flag = 0;
2870
2871 switch (adbHardware) {
2872 case ADB_HW_IISI:
2873 output[0] = 0x03; /* 3 byte message */
2874 output[1] = 0x01; /* to pram/rtc/soft-power device */
2875 output[2] = 0x1c; /* prog. switch control */
2876 output[3] = 0x01; /* enable */
2877 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2878 (void *)adb_op_comprout, (void *)&flag, (int)0);
2879 if (result != 0) /* exit if not sent */
2880 return -1;
2881
2882 while (0 == flag) /* wait for send to finish */
2883 ;
2884
2885 return 0;
2886
2887 case ADB_HW_PB:
2888 return -1;
2889
2890 case ADB_HW_II: /* II models don't do prog. switch */
2891 case ADB_HW_IOP: /* IOP models don't do prog. switch */
2892 case ADB_HW_CUDA: /* cuda doesn't do prog. switch TO DO: verify this */
2893 case ADB_HW_UNKNOWN:
2894 default:
2895 return -1;
2896 }
2897 }
2898
2899 int
2900 adb_prog_switch_disable(void)
2901 {
2902 u_char output[ADB_MAX_MSG_LENGTH];
2903 int result;
2904 volatile int flag = 0;
2905
2906 switch (adbHardware) {
2907 case ADB_HW_IISI:
2908 output[0] = 0x03; /* 3 byte message */
2909 output[1] = 0x01; /* to pram/rtc/soft-power device */
2910 output[2] = 0x1c; /* prog. switch control */
2911 output[3] = 0x01; /* disable */
2912 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2913 (void *)adb_op_comprout, (void *)&flag, (int)0);
2914 if (result != 0) /* exit if not sent */
2915 return -1;
2916
2917 while (0 == flag) /* wait for send to finish */
2918 ;
2919
2920 return 0;
2921
2922 case ADB_HW_PB:
2923 return -1;
2924
2925 case ADB_HW_II: /* II models don't do prog. switch */
2926 case ADB_HW_IOP: /* IOP models don't do prog. switch */
2927 case ADB_HW_CUDA: /* cuda doesn't do prog. switch */
2928 case ADB_HW_UNKNOWN:
2929 default:
2930 return -1;
2931 }
2932 }
2933
2934 int
2935 CountADBs(void)
2936 {
2937 return (count_adbs());
2938 }
2939
2940 void
2941 ADBReInit(void)
2942 {
2943 adb_reinit();
2944 }
2945
2946 int
2947 GetIndADB(ADBDataBlock * info, int index)
2948 {
2949 return (get_ind_adb_info(info, index));
2950 }
2951
2952 int
2953 GetADBInfo(ADBDataBlock * info, int adbAddr)
2954 {
2955 return (get_adb_info(info, adbAddr));
2956 }
2957
2958 int
2959 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
2960 {
2961 return (set_adb_info(info, adbAddr));
2962 }
2963
2964 int
2965 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
2966 {
2967 return (adb_op(buffer, compRout, data, commandNum));
2968 }
2969
2970 #endif
2971