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