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