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