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