adb_direct.c revision 1.41 1 /* $NetBSD: adb_direct.c,v 1.41 2000/03/19 07:37:58 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 = ADB_CMDADDR(adbInputBuffer[1]);
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] = ADBTALK(adbLastDevice, 0);
828
829 adbSentChars = 0; /* nothing sent yet */
830 adbActionState = ADB_ACTION_POLLING; /* set next state */
831 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
832 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
833 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
834 ADB_SET_STATE_CMD(); /* tell ADB that we want to */
835 break;
836 }
837
838 /* set up data for adb_pass_up */
839 memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1);
840
841 if (!adbWaiting && (adbInputBuffer[0] != 0)) {
842 packet.unsol = 1;
843 packet.ack_only = 0;
844 adb_pass_up(&packet);
845 } else {
846 packet.saveBuf = adbBuffer;
847 packet.compRout = adbCompRout;
848 packet.compData = adbCompData;
849 packet.unsol = 0;
850 packet.ack_only = 0;
851 adb_pass_up(&packet);
852 }
853
854 adbWaiting = 0;
855 adbInputBuffer[0] = 0;
856 adbBuffer = (long)0;
857 adbCompRout = (long)0;
858 adbCompData = (long)0;
859 /*
860 * Since we are done, check whether there is any data
861 * waiting to do out. If so, start the sending the data.
862 */
863 if (adbOutQueueHasData == 1) {
864 #ifdef ADB_DEBUG
865 if (adb_debug & 0x80)
866 printf_intr("XXX: DOING OUT QUEUE\n");
867 #endif
868 /* copy over data */
869 memcpy(adbOutputBuffer, adbOutQueue.outBuf,
870 adbOutQueue.outBuf[0] + 2);
871 adbBuffer = adbOutQueue.saveBuf; /* user data area */
872 adbCompRout = adbOutQueue.compRout; /* completion routine */
873 adbCompData = adbOutQueue.data; /* comp. rout. data */
874 adbOutQueueHasData = 0; /* currently processing
875 * "queue" entry */
876 send = 1;
877 } else {
878 #ifdef ADB_DEBUG
879 if (adb_debug & 0x80)
880 printf_intr("XXending ");
881 #endif
882 adb_guess_next_device();
883 adbOutputBuffer[0] = 1;
884 adbOutputBuffer[1] = ((adbLastDevice & 0x0f) << 4) | 0x0c;
885 adbSentChars = 0; /* nothing sent yet */
886 adbActionState = ADB_ACTION_POLLING; /* set next state */
887 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
888 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
889 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
890 ADB_SET_STATE_CMD(); /* tell ADB that we want to */
891 break;
892 }
893 }
894
895 /*
896 * If send is true then something above determined that
897 * the message has ended and we need to start sending out
898 * a new message immediately. This could be because there
899 * is data waiting to go out or because an SRQ was seen.
900 */
901 if (send) {
902 adbSentChars = 0; /* nothing sent yet */
903 adbActionState = ADB_ACTION_OUT; /* set next state */
904 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
905 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
906 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
907 ADB_SET_STATE_CMD(); /* tell ADB that we want to
908 * send */
909 break;
910 }
911 /* We only get this far if the message hasn't ended yet. */
912 switch (adbBusState) { /* set to next state */
913 case ADB_BUS_EVEN:
914 ADB_SET_STATE_ODD(); /* set state to odd */
915 adbBusState = ADB_BUS_ODD;
916 break;
917
918 case ADB_BUS_ODD:
919 ADB_SET_STATE_EVEN(); /* set state to even */
920 adbBusState = ADB_BUS_EVEN;
921 break;
922 default:
923 printf_intr("strange state!!!\n"); /* huh? */
924 break;
925 }
926 break;
927
928 case ADB_ACTION_OUT:
929 i = ADB_SR(); /* clear interrupt */
930 adbSentChars++;
931 /*
932 * If the outgoing data was a TALK, we must
933 * switch to input mode to get the result.
934 */
935 if ((adbOutputBuffer[1] & 0x0c) == 0x0c) {
936 adbInputBuffer[0] = 1;
937 adbInputBuffer[1] = i;
938 adbActionState = ADB_ACTION_IN;
939 ADB_SET_SR_INPUT();
940 adbBusState = ADB_BUS_EVEN;
941 ADB_SET_STATE_EVEN();
942 #ifdef ADB_DEBUG
943 if (adb_debug & 0x80)
944 printf_intr("talk out 0x%02x ", i);
945 #endif
946 /* we want something back */
947 adbWaiting = 1;
948 break;
949 }
950 /*
951 * If it's not a TALK, check whether all data has been sent.
952 * If so, call the completion routine and clean up. If not,
953 * advance to the next state.
954 */
955 #ifdef ADB_DEBUG
956 if (adb_debug & 0x80)
957 printf_intr("non-talk out 0x%0x ", i);
958 #endif
959 ADB_SET_SR_OUTPUT();
960 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */
961 #ifdef ADB_DEBUG
962 if (adb_debug & 0x80)
963 printf_intr("done \n");
964 #endif
965 /* set up stuff for adb_pass_up */
966 memcpy(packet.data, adbOutputBuffer, adbOutputBuffer[0] + 1);
967 packet.saveBuf = adbBuffer;
968 packet.compRout = adbCompRout;
969 packet.compData = adbCompData;
970 packet.cmd = adbWaitingCmd;
971 packet.unsol = 0;
972 packet.ack_only = 1;
973 adb_pass_up(&packet);
974
975 /* reset "waiting" vars, just in case */
976 adbBuffer = (long)0;
977 adbCompRout = (long)0;
978 adbCompData = (long)0;
979 if (adbOutQueueHasData == 1) {
980 /* copy over data */
981 memcpy(adbOutputBuffer, adbOutQueue.outBuf,
982 adbOutQueue.outBuf[0] + 2);
983 adbBuffer = adbOutQueue.saveBuf; /* user data area */
984 adbCompRout = adbOutQueue.compRout; /* completion routine */
985 adbCompData = adbOutQueue.data; /* comp. rout. data */
986 adbOutQueueHasData = 0; /* currently processing
987 * "queue" entry */
988 adbSentChars = 0; /* nothing sent yet */
989 adbActionState = ADB_ACTION_OUT; /* set next state */
990 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */
991 ADB_SR() = adbOutputBuffer[1]; /* load byte for output */
992 adbBusState = ADB_BUS_CMD; /* set bus to cmd state */
993 ADB_SET_STATE_CMD(); /* tell ADB that we want to
994 * send */
995 break;
996 } else {
997 /* send talk to last device instead */
998 adbOutputBuffer[0] = 1;
999 adbOutputBuffer[1] =
1000 ADBTALK(ADB_CMDADDR(adbOutputBuffer[1]), 0);
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, ADB_CMDADDR(cmd)))
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 = ADBTALK(i, 3);
2152 result = adb_op_sync((Ptr)send_string, (Ptr)0,
2153 (Ptr)0, (short)command);
2154
2155 if (send_string[0] != 0) {
2156 /* check for valid device handler */
2157 switch (send_string[2]) {
2158 case 0:
2159 case 0xfd:
2160 case 0xfe:
2161 case 0xff:
2162 continue; /* invalid, skip */
2163 }
2164
2165 /* found a device */
2166 ++ADBNumDevices;
2167 KASSERT(ADBNumDevices < 16);
2168 ADBDevTable[ADBNumDevices].devType =
2169 (int)(send_string[2]);
2170 ADBDevTable[ADBNumDevices].origAddr = i;
2171 ADBDevTable[ADBNumDevices].currentAddr = i;
2172 ADBDevTable[ADBNumDevices].DataAreaAddr =
2173 (long)0;
2174 ADBDevTable[ADBNumDevices].ServiceRtPtr = (void *)0;
2175 pm_check_adb_devices(i); /* tell pm driver device
2176 * is here */
2177 }
2178 }
2179
2180 /* find highest unused address */
2181 for (saveptr = 15; saveptr > 0; saveptr--)
2182 if (-1 == get_adb_info(&data, saveptr))
2183 break;
2184
2185 #ifdef ADB_DEBUG
2186 if (adb_debug & 0x80) {
2187 printf_intr("first free is: 0x%02x\n", saveptr);
2188 printf_intr("devices: %i\n", ADBNumDevices);
2189 }
2190 #endif
2191
2192 nonewtimes = 0; /* no loops w/o new devices */
2193 while (saveptr > 0 && nonewtimes++ < 11) {
2194 for (i = 1; i <= ADBNumDevices; i++) {
2195 device = ADBDevTable[i].currentAddr;
2196 #ifdef ADB_DEBUG
2197 if (adb_debug & 0x80)
2198 printf_intr("moving device 0x%02x to 0x%02x "
2199 "(index 0x%02x) ", device, saveptr, i);
2200 #endif
2201
2202 /* send TALK R3 to address */
2203 command = ADBTALK(device, 3);
2204 adb_op_sync((Ptr)send_string, (Ptr)0,
2205 (Ptr)0, (short)command);
2206
2207 /* move device to higher address */
2208 command = ADBLISTEN(device, 3);
2209 send_string[0] = 2;
2210 send_string[1] = (u_char)(saveptr | 0x60);
2211 send_string[2] = 0xfe;
2212 adb_op_sync((Ptr)send_string, (Ptr)0,
2213 (Ptr)0, (short)command);
2214 delay(500);
2215
2216 /* send TALK R3 - anthing at new address? */
2217 command = ADBTALK(saveptr, 3);
2218 adb_op_sync((Ptr)send_string, (Ptr)0,
2219 (Ptr)0, (short)command);
2220 delay(500);
2221
2222 if (send_string[0] == 0) {
2223 #ifdef ADB_DEBUG
2224 if (adb_debug & 0x80)
2225 printf_intr("failed, continuing\n");
2226 #endif
2227 continue;
2228 }
2229
2230 /* send TALK R3 - anything at old address? */
2231 command = ADBTALK(device, 3);
2232 result = adb_op_sync((Ptr)send_string, (Ptr)0,
2233 (Ptr)0, (short)command);
2234 if (send_string[0] != 0) {
2235 /* check for valid device handler */
2236 switch (send_string[2]) {
2237 case 0:
2238 case 0xfd:
2239 case 0xfe:
2240 case 0xff:
2241 continue; /* invalid, skip */
2242 }
2243
2244 /* new device found */
2245 /* update data for previously moved device */
2246 ADBDevTable[i].currentAddr = saveptr;
2247 #ifdef ADB_DEBUG
2248 if (adb_debug & 0x80)
2249 printf_intr("old device at index %i\n",i);
2250 #endif
2251 /* add new device in table */
2252 #ifdef ADB_DEBUG
2253 if (adb_debug & 0x80)
2254 printf_intr("new device found\n");
2255 #endif
2256 if (saveptr > ADBNumDevices) {
2257 ++ADBNumDevices;
2258 KASSERT(ADBNumDevices < 16);
2259 }
2260 ADBDevTable[ADBNumDevices].devType =
2261 (int)(send_string[2]);
2262 ADBDevTable[ADBNumDevices].origAddr = device;
2263 ADBDevTable[ADBNumDevices].currentAddr = device;
2264 /* These will be set correctly in adbsys.c */
2265 /* Until then, unsol. data will be ignored. */
2266 ADBDevTable[ADBNumDevices].DataAreaAddr =
2267 (long)0;
2268 ADBDevTable[ADBNumDevices].ServiceRtPtr =
2269 (void *)0;
2270 /* find next unused address */
2271 for (x = saveptr; x > 0; x--) {
2272 if (-1 == get_adb_info(&data, x)) {
2273 saveptr = x;
2274 break;
2275 }
2276 }
2277 if (x == 0)
2278 saveptr = 0;
2279 #ifdef ADB_DEBUG
2280 if (adb_debug & 0x80)
2281 printf_intr("new free is 0x%02x\n",
2282 saveptr);
2283 #endif
2284 nonewtimes = 0;
2285 /* tell pm driver device is here */
2286 pm_check_adb_devices(device);
2287 } else {
2288 #ifdef ADB_DEBUG
2289 if (adb_debug & 0x80)
2290 printf_intr("moving back...\n");
2291 #endif
2292 /* move old device back */
2293 command = ADBLISTEN(saveptr, 3);
2294 send_string[0] = 2;
2295 send_string[1] = (u_char)(device | 0x60);
2296 send_string[2] = 0xfe;
2297 adb_op_sync((Ptr)send_string, (Ptr)0,
2298 (Ptr)0, (short)command);
2299 delay(1000);
2300 }
2301 }
2302 }
2303
2304 #ifdef ADB_DEBUG
2305 if (adb_debug) {
2306 for (i = 1; i <= ADBNumDevices; i++) {
2307 x = get_ind_adb_info(&data, i);
2308 if (x != -1)
2309 printf_intr("index 0x%x, addr 0x%x, type 0x%hx\n",
2310 i, x, data.devType);
2311 }
2312 }
2313 #endif
2314
2315 #ifndef MRG_ADB
2316 /* enable the programmer's switch, if we have one */
2317 adb_prog_switch_enable();
2318 #endif
2319
2320 #ifdef ADB_DEBUG
2321 if (adb_debug) {
2322 if (0 == ADBNumDevices) /* tell user if no devices found */
2323 printf_intr("adb: no devices found\n");
2324 }
2325 #endif
2326
2327 adbStarting = 0; /* not starting anymore */
2328 #ifdef ADB_DEBUG
2329 if (adb_debug)
2330 printf_intr("adb: ADBReInit complete\n");
2331 #endif
2332
2333 if (adbHardware == ADB_HW_CUDA)
2334 timeout((void *)adb_cuda_tickle, 0, ADB_TICKLE_TICKS);
2335
2336 /* ints must be on for PB & IOP (at least, for now) */
2337 if (adbHardware != ADB_HW_PB && adbHardware != ADB_HW_IOP)
2338 splx(s);
2339
2340 return;
2341 }
2342
2343
2344 /*
2345 * adb_comp_exec
2346 * This is a general routine that calls the completion routine if there is one.
2347 * NOTE: This routine is now only used by pm_direct.c
2348 * All the code in this file (adb_direct.c) uses
2349 * the adb_pass_up routine now.
2350 */
2351 void
2352 adb_comp_exec(void)
2353 {
2354 if ((long)0 != adbCompRout) /* don't call if empty return location */
2355 #ifdef __NetBSD__
2356 asm(" movml #0xffff,sp@- | save all registers
2357 movl %0,a2 | adbCompData
2358 movl %1,a1 | adbCompRout
2359 movl %2,a0 | adbBuffer
2360 movl %3,d0 | adbWaitingCmd
2361 jbsr a1@ | go call the routine
2362 movml sp@+,#0xffff | restore all registers"
2363 :
2364 : "g"(adbCompData), "g"(adbCompRout),
2365 "g"(adbBuffer), "g"(adbWaitingCmd)
2366 : "d0", "a0", "a1", "a2");
2367 #else /* for Mac OS-based testing */
2368 asm {
2369 movem.l a0/a1/a2/d0, -(a7)
2370 move.l adbCompData, a2
2371 move.l adbCompRout, a1
2372 move.l adbBuffer, a0
2373 move.w adbWaitingCmd, d0
2374 jsr(a1)
2375 movem.l(a7) +, d0/a2/a1/a0
2376 }
2377 #endif
2378 }
2379
2380
2381 /*
2382 * adb_cmd_result
2383 *
2384 * This routine lets the caller know whether the specified adb command string
2385 * should expect a returned result, such as a TALK command.
2386 *
2387 * returns: 0 if a result should be expected
2388 * 1 if a result should NOT be expected
2389 */
2390 int
2391 adb_cmd_result(u_char *in)
2392 {
2393 switch (adbHardware) {
2394 case ADB_HW_IOP:
2395 case ADB_HW_II:
2396 /* was it an ADB talk command? */
2397 if ((in[1] & 0x0c) == 0x0c)
2398 return 0;
2399 return 1;
2400
2401 case ADB_HW_IISI:
2402 case ADB_HW_CUDA:
2403 /* was it an ADB talk command? */
2404 if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c))
2405 return 0;
2406 /* was it an RTC/PRAM read date/time? */
2407 if ((in[1] == 0x01) && (in[2] == 0x03))
2408 return 0;
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_cmd_extra
2423 *
2424 * This routine lets the caller know whether the specified adb command string
2425 * may have extra data appended to the end of it, such as a LISTEN command.
2426 *
2427 * returns: 0 if extra data is allowed
2428 * 1 if extra data is NOT allowed
2429 */
2430 int
2431 adb_cmd_extra(u_char *in)
2432 {
2433 switch (adbHardware) {
2434 case ADB_HW_II:
2435 case ADB_HW_IOP:
2436 if ((in[1] & 0x0c) == 0x08) /* was it a listen command? */
2437 return 0;
2438 return 1;
2439
2440 case ADB_HW_IISI:
2441 case ADB_HW_CUDA:
2442 /*
2443 * TO DO: support needs to be added to recognize RTC and PRAM
2444 * commands
2445 */
2446 if ((in[2] & 0x0c) == 0x08) /* was it a listen command? */
2447 return 0;
2448 /* add others later */
2449 return 1;
2450
2451 case ADB_HW_PB:
2452 return 1;
2453
2454 case ADB_HW_UNKNOWN:
2455 default:
2456 return 1;
2457 }
2458 }
2459
2460
2461 /*
2462 * adb_op_sync
2463 *
2464 * This routine does exactly what the adb_op routine does, except that after
2465 * the adb_op is called, it waits until the return value is present before
2466 * returning.
2467 *
2468 * NOTE: The user specified compRout is ignored, since this routine specifies
2469 * it's own to adb_op, which is why you really called this in the first place
2470 * anyway.
2471 */
2472 int
2473 adb_op_sync(Ptr buffer, Ptr compRout, Ptr data, short command)
2474 {
2475 int result;
2476 volatile int flag = 0;
2477
2478 result = adb_op(buffer, (void *)adb_op_comprout,
2479 (void *)&flag, command); /* send command */
2480 if (result == 0) /* send ok? */
2481 while (0 == flag)
2482 /* wait for compl. routine */;
2483
2484 return result;
2485 }
2486
2487
2488 /*
2489 * adb_op_comprout
2490 *
2491 * This function is used by the adb_op_sync routine so it knows when the
2492 * function is done.
2493 */
2494 void
2495 adb_op_comprout(void)
2496 {
2497 #ifdef __NetBSD__
2498 asm("movw #1,a2@ | update flag value");
2499 #else /* for macos based testing */
2500 asm {
2501 move.w #1,(a2) } /* update flag value */
2502 #endif
2503 }
2504
2505 void
2506 adb_setup_hw_type(void)
2507 {
2508 long response;
2509
2510 response = mac68k_machine.machineid;
2511
2512 /*
2513 * Determine what type of ADB hardware we are running on.
2514 */
2515 switch (response) {
2516 case MACH_MACC610: /* Centris 610 */
2517 case MACH_MACC650: /* Centris 650 */
2518 case MACH_MACII: /* II */
2519 case MACH_MACIICI: /* IIci */
2520 case MACH_MACIICX: /* IIcx */
2521 case MACH_MACIIX: /* IIx */
2522 case MACH_MACQ610: /* Quadra 610 */
2523 case MACH_MACQ650: /* Quadra 650 */
2524 case MACH_MACQ700: /* Quadra 700 */
2525 case MACH_MACQ800: /* Quadra 800 */
2526 case MACH_MACSE30: /* SE/30 */
2527 adbHardware = ADB_HW_II;
2528 #ifdef ADB_DEBUG
2529 if (adb_debug)
2530 printf_intr("adb: using II series hardware support\n");
2531 #endif
2532 break;
2533
2534 case MACH_MACCLASSICII: /* Classic II */
2535 case MACH_MACLCII: /* LC II, Performa 400/405/430 */
2536 case MACH_MACLCIII: /* LC III, Performa 450 */
2537 case MACH_MACIISI: /* IIsi */
2538 case MACH_MACIIVI: /* IIvi */
2539 case MACH_MACIIVX: /* IIvx */
2540 case MACH_MACP460: /* Performa 460/465/467 */
2541 case MACH_MACP600: /* Performa 600 */
2542 adbHardware = ADB_HW_IISI;
2543 #ifdef ADB_DEBUG
2544 if (adb_debug)
2545 printf_intr("adb: using IIsi series hardware support\n");
2546 #endif
2547 break;
2548
2549 case MACH_MACPB140: /* PowerBook 140 */
2550 case MACH_MACPB145: /* PowerBook 145 */
2551 case MACH_MACPB150: /* PowerBook 150 */
2552 case MACH_MACPB160: /* PowerBook 160 */
2553 case MACH_MACPB165: /* PowerBook 165 */
2554 case MACH_MACPB165C: /* PowerBook 165c */
2555 case MACH_MACPB170: /* PowerBook 170 */
2556 case MACH_MACPB180: /* PowerBook 180 */
2557 case MACH_MACPB180C: /* PowerBook 180c */
2558 adbHardware = ADB_HW_PB;
2559 pm_setup_adb();
2560 #ifdef ADB_DEBUG
2561 if (adb_debug)
2562 printf_intr("adb: using PowerBook 100-series hardware support\n");
2563 #endif
2564 break;
2565
2566 case MACH_MACPB210: /* PowerBook Duo 210 */
2567 case MACH_MACPB230: /* PowerBook Duo 230 */
2568 case MACH_MACPB250: /* PowerBook Duo 250 */
2569 case MACH_MACPB270: /* PowerBook Duo 270 */
2570 case MACH_MACPB280: /* PowerBook Duo 280 */
2571 case MACH_MACPB280C: /* PowerBook Duo 280c */
2572 case MACH_MACPB500: /* PowerBook 500 series */
2573 adbHardware = ADB_HW_PB;
2574 pm_setup_adb();
2575 #ifdef ADB_DEBUG
2576 if (adb_debug)
2577 printf_intr("adb: using PowerBook Duo-series and PowerBook 500-series hardware support\n");
2578 #endif
2579 break;
2580
2581 case MACH_MACC660AV: /* Centris 660AV */
2582 case MACH_MACCCLASSIC: /* Color Classic */
2583 case MACH_MACCCLASSICII: /* Color Classic II */
2584 case MACH_MACLC475: /* LC 475, Performa 475/476 */
2585 case MACH_MACLC475_33: /* Clock-chipped 47x */
2586 case MACH_MACLC520: /* LC 520 */
2587 case MACH_MACLC575: /* LC 575, Performa 575/577/578 */
2588 case MACH_MACP550: /* LC 550, Performa 550 */
2589 case MACH_MACTV: /* Macintosh TV */
2590 case MACH_MACP580: /* Performa 580/588 */
2591 case MACH_MACQ605: /* Quadra 605 */
2592 case MACH_MACQ605_33: /* Clock-chipped Quadra 605 */
2593 case MACH_MACQ630: /* LC 630, Performa 630, Quadra 630 */
2594 case MACH_MACQ840AV: /* Quadra 840AV */
2595 adbHardware = ADB_HW_CUDA;
2596 #ifdef ADB_DEBUG
2597 if (adb_debug)
2598 printf_intr("adb: using Cuda series hardware support\n");
2599 #endif
2600 break;
2601
2602 case MACH_MACQ900: /* Quadra 900 */
2603 case MACH_MACQ950: /* Quadra 950 */
2604 case MACH_MACIIFX: /* Mac IIfx */
2605 adbHardware = ADB_HW_IOP;
2606 iop_register_listener(ISM_IOP, IOP_CHAN_ADB, adb_iop_recv, NULL);
2607 #ifdef ADB_DEBUG
2608 if (adb_debug)
2609 printf_intr("adb: using IOP-based ADB\n");
2610 #endif
2611 break;
2612
2613 default:
2614 adbHardware = ADB_HW_UNKNOWN;
2615 #ifdef ADB_DEBUG
2616 if (adb_debug) {
2617 printf_intr("adb: hardware type unknown for this machine\n");
2618 printf_intr("adb: ADB support is disabled\n");
2619 }
2620 #endif
2621 break;
2622 }
2623
2624 /*
2625 * Determine whether this machine has ADB based soft power.
2626 */
2627 switch (response) {
2628 case MACH_MACCCLASSIC: /* Color Classic */
2629 case MACH_MACCCLASSICII: /* Color Classic II */
2630 case MACH_MACIISI: /* IIsi */
2631 case MACH_MACIIVI: /* IIvi */
2632 case MACH_MACIIVX: /* IIvx */
2633 case MACH_MACLC520: /* LC 520 */
2634 case MACH_MACLC575: /* LC 575, Performa 575/577/578 */
2635 case MACH_MACP550: /* LC 550, Performa 550 */
2636 case MACH_MACTV: /* Macintosh TV */
2637 case MACH_MACP580: /* Performa 580/588 */
2638 case MACH_MACP600: /* Performa 600 */
2639 case MACH_MACQ630: /* LC 630, Performa 630, Quadra 630 */
2640 case MACH_MACQ840AV: /* Quadra 840AV */
2641 adbSoftPower = 1;
2642 break;
2643 }
2644 }
2645
2646 int
2647 count_adbs(void)
2648 {
2649 int i;
2650 int found;
2651
2652 found = 0;
2653
2654 for (i = 1; i < 16; i++)
2655 if (0 != ADBDevTable[i].devType)
2656 found++;
2657
2658 return found;
2659 }
2660
2661 int
2662 get_ind_adb_info(ADBDataBlock * info, int index)
2663 {
2664 if ((index < 1) || (index > 15)) /* check range 1-15 */
2665 return (-1);
2666
2667 #ifdef ADB_DEBUG
2668 if (adb_debug & 0x80)
2669 printf_intr("index 0x%x devType is: 0x%x\n", index,
2670 ADBDevTable[index].devType);
2671 #endif
2672 if (0 == ADBDevTable[index].devType) /* make sure it's a valid entry */
2673 return (-1);
2674
2675 info->devType = (unsigned char)(ADBDevTable[index].devType);
2676 info->origADBAddr = (unsigned char)(ADBDevTable[index].origAddr);
2677 info->dbServiceRtPtr = (Ptr)ADBDevTable[index].ServiceRtPtr;
2678 info->dbDataAreaAddr = (Ptr)ADBDevTable[index].DataAreaAddr;
2679
2680 return (ADBDevTable[index].currentAddr);
2681 }
2682
2683 int
2684 get_adb_info(ADBDataBlock * info, int adbAddr)
2685 {
2686 int i;
2687
2688 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
2689 return (-1);
2690
2691 for (i = 1; i < 15; i++)
2692 if (ADBDevTable[i].currentAddr == adbAddr) {
2693 info->devType = (unsigned char)(ADBDevTable[i].devType);
2694 info->origADBAddr = (unsigned char)(ADBDevTable[i].origAddr);
2695 info->dbServiceRtPtr = (Ptr)ADBDevTable[i].ServiceRtPtr;
2696 info->dbDataAreaAddr = ADBDevTable[i].DataAreaAddr;
2697 return 0; /* found */
2698 }
2699
2700 return (-1); /* not found */
2701 }
2702
2703 int
2704 set_adb_info(ADBSetInfoBlock * info, int adbAddr)
2705 {
2706 int i;
2707
2708 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */
2709 return (-1);
2710
2711 for (i = 1; i < 15; i++)
2712 if (ADBDevTable[i].currentAddr == adbAddr) {
2713 ADBDevTable[i].ServiceRtPtr =
2714 (void *)(info->siServiceRtPtr);
2715 ADBDevTable[i].DataAreaAddr = info->siDataAreaAddr;
2716 return 0; /* found */
2717 }
2718
2719 return (-1); /* not found */
2720
2721 }
2722
2723 #ifndef MRG_ADB
2724 long
2725 mrg_adbintr(void)
2726 {
2727 adb_intr(NULL);
2728 return 1; /* mimic mrg_adbintr in macrom.h just in case */
2729 }
2730
2731 long
2732 mrg_pmintr(void)
2733 {
2734 pm_intr(NULL);
2735 return 1; /* mimic mrg_pmintr in macrom.h just in case */
2736 }
2737
2738 /* caller should really use machine-independant version: getPramTime */
2739 /* this version does pseudo-adb access only */
2740 int
2741 adb_read_date_time(unsigned long *time)
2742 {
2743 u_char output[ADB_MAX_MSG_LENGTH];
2744 int result;
2745 volatile int flag = 0;
2746
2747 switch (adbHardware) {
2748 case ADB_HW_II:
2749 return -1;
2750
2751 case ADB_HW_IOP:
2752 return -1;
2753
2754 case ADB_HW_IISI:
2755 output[0] = 0x02; /* 2 byte message */
2756 output[1] = 0x01; /* to pram/rtc device */
2757 output[2] = 0x03; /* read date/time */
2758 result = send_adb_IIsi((u_char *)output, (u_char *)output,
2759 (void *)adb_op_comprout, (int *)&flag, (int)0);
2760 if (result != 0) /* exit if not sent */
2761 return -1;
2762
2763 while (0 == flag) /* wait for result */
2764 ;
2765
2766 *time = (long)(*(long *)(output + 1));
2767 return 0;
2768
2769 case ADB_HW_PB:
2770 return -1;
2771
2772 case ADB_HW_CUDA:
2773 output[0] = 0x02; /* 2 byte message */
2774 output[1] = 0x01; /* to pram/rtc device */
2775 output[2] = 0x03; /* read date/time */
2776 result = send_adb_cuda((u_char *)output, (u_char *)output,
2777 (void *)adb_op_comprout, (void *)&flag, (int)0);
2778 if (result != 0) /* exit if not sent */
2779 return -1;
2780
2781 while (0 == flag) /* wait for result */
2782 ;
2783
2784 *time = (long)(*(long *)(output + 1));
2785 return 0;
2786
2787 case ADB_HW_UNKNOWN:
2788 default:
2789 return -1;
2790 }
2791 }
2792
2793 /* caller should really use machine-independant version: setPramTime */
2794 /* this version does pseudo-adb access only */
2795 int
2796 adb_set_date_time(unsigned long time)
2797 {
2798 u_char output[ADB_MAX_MSG_LENGTH];
2799 int result;
2800 volatile int flag = 0;
2801
2802 switch (adbHardware) {
2803 case ADB_HW_II:
2804 return -1;
2805
2806 case ADB_HW_IOP:
2807 return -1;
2808
2809 case ADB_HW_IISI:
2810 output[0] = 0x06; /* 6 byte message */
2811 output[1] = 0x01; /* to pram/rtc device */
2812 output[2] = 0x09; /* set date/time */
2813 output[3] = (u_char)(time >> 24);
2814 output[4] = (u_char)(time >> 16);
2815 output[5] = (u_char)(time >> 8);
2816 output[6] = (u_char)(time);
2817 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2818 (void *)adb_op_comprout, (void *)&flag, (int)0);
2819 if (result != 0) /* exit if not sent */
2820 return -1;
2821
2822 while (0 == flag) /* wait for send to finish */
2823 ;
2824
2825 return 0;
2826
2827 case ADB_HW_PB:
2828 return -1;
2829
2830 case ADB_HW_CUDA:
2831 output[0] = 0x06; /* 6 byte message */
2832 output[1] = 0x01; /* to pram/rtc device */
2833 output[2] = 0x09; /* set date/time */
2834 output[3] = (u_char)(time >> 24);
2835 output[4] = (u_char)(time >> 16);
2836 output[5] = (u_char)(time >> 8);
2837 output[6] = (u_char)(time);
2838 result = send_adb_cuda((u_char *)output, (u_char *)0,
2839 (void *)adb_op_comprout, (void *)&flag, (int)0);
2840 if (result != 0) /* exit if not sent */
2841 return -1;
2842
2843 while (0 == flag) /* wait for send to finish */
2844 ;
2845
2846 return 0;
2847
2848 case ADB_HW_UNKNOWN:
2849 default:
2850 return -1;
2851 }
2852 }
2853
2854
2855 int
2856 adb_poweroff(void)
2857 {
2858 u_char output[ADB_MAX_MSG_LENGTH];
2859 int result;
2860
2861 if (!adbSoftPower)
2862 return -1;
2863
2864 adb_polling = 1;
2865
2866 switch (adbHardware) {
2867 case ADB_HW_IISI:
2868 output[0] = 0x02; /* 2 byte message */
2869 output[1] = 0x01; /* to pram/rtc/soft-power device */
2870 output[2] = 0x0a; /* set date/time */
2871 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2872 (void *)0, (void *)0, (int)0);
2873 if (result != 0) /* exit if not sent */
2874 return -1;
2875
2876 for (;;); /* wait for power off */
2877
2878 return 0;
2879
2880 case ADB_HW_PB:
2881 return -1;
2882
2883 case ADB_HW_CUDA:
2884 output[0] = 0x02; /* 2 byte message */
2885 output[1] = 0x01; /* to pram/rtc/soft-power device */
2886 output[2] = 0x0a; /* set date/time */
2887 result = send_adb_cuda((u_char *)output, (u_char *)0,
2888 (void *)0, (void *)0, (int)0);
2889 if (result != 0) /* exit if not sent */
2890 return -1;
2891
2892 for (;;); /* wait for power off */
2893
2894 return 0;
2895
2896 case ADB_HW_II: /* II models don't do ADB soft power */
2897 case ADB_HW_IOP: /* IOP models don't do ADB soft power */
2898 case ADB_HW_UNKNOWN:
2899 default:
2900 return -1;
2901 }
2902 }
2903
2904 int
2905 adb_prog_switch_enable(void)
2906 {
2907 u_char output[ADB_MAX_MSG_LENGTH];
2908 int result;
2909 volatile int flag = 0;
2910
2911 switch (adbHardware) {
2912 case ADB_HW_IISI:
2913 output[0] = 0x03; /* 3 byte message */
2914 output[1] = 0x01; /* to pram/rtc/soft-power device */
2915 output[2] = 0x1c; /* prog. switch control */
2916 output[3] = 0x01; /* enable */
2917 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2918 (void *)adb_op_comprout, (void *)&flag, (int)0);
2919 if (result != 0) /* exit if not sent */
2920 return -1;
2921
2922 while (0 == flag) /* wait for send to finish */
2923 ;
2924
2925 return 0;
2926
2927 case ADB_HW_PB:
2928 return -1;
2929
2930 case ADB_HW_II: /* II models don't do prog. switch */
2931 case ADB_HW_IOP: /* IOP models don't do prog. switch */
2932 case ADB_HW_CUDA: /* cuda doesn't do prog. switch TO DO: verify this */
2933 case ADB_HW_UNKNOWN:
2934 default:
2935 return -1;
2936 }
2937 }
2938
2939 int
2940 adb_prog_switch_disable(void)
2941 {
2942 u_char output[ADB_MAX_MSG_LENGTH];
2943 int result;
2944 volatile int flag = 0;
2945
2946 switch (adbHardware) {
2947 case ADB_HW_IISI:
2948 output[0] = 0x03; /* 3 byte message */
2949 output[1] = 0x01; /* to pram/rtc/soft-power device */
2950 output[2] = 0x1c; /* prog. switch control */
2951 output[3] = 0x01; /* disable */
2952 result = send_adb_IIsi((u_char *)output, (u_char *)0,
2953 (void *)adb_op_comprout, (void *)&flag, (int)0);
2954 if (result != 0) /* exit if not sent */
2955 return -1;
2956
2957 while (0 == flag) /* wait for send to finish */
2958 ;
2959
2960 return 0;
2961
2962 case ADB_HW_PB:
2963 return -1;
2964
2965 case ADB_HW_II: /* II models don't do prog. switch */
2966 case ADB_HW_IOP: /* IOP models don't do prog. switch */
2967 case ADB_HW_CUDA: /* cuda doesn't do prog. switch */
2968 case ADB_HW_UNKNOWN:
2969 default:
2970 return -1;
2971 }
2972 }
2973
2974 int
2975 CountADBs(void)
2976 {
2977 return (count_adbs());
2978 }
2979
2980 void
2981 ADBReInit(void)
2982 {
2983 adb_reinit();
2984 }
2985
2986 int
2987 GetIndADB(ADBDataBlock * info, int index)
2988 {
2989 return (get_ind_adb_info(info, index));
2990 }
2991
2992 int
2993 GetADBInfo(ADBDataBlock * info, int adbAddr)
2994 {
2995 return (get_adb_info(info, adbAddr));
2996 }
2997
2998 int
2999 SetADBInfo(ADBSetInfoBlock * info, int adbAddr)
3000 {
3001 return (set_adb_info(info, adbAddr));
3002 }
3003
3004 int
3005 ADBOp(Ptr buffer, Ptr compRout, Ptr data, short commandNum)
3006 {
3007 return (adb_op(buffer, compRout, data, commandNum));
3008 }
3009
3010 #endif
3011