1 /* $OpenBSD: adb.c,v 1.43 2021/03/11 11:16:58 jsg Exp $ */ 2 /* $NetBSD: adb.c,v 1.6 1999/08/16 06:28:09 tsubai Exp $ */ 3 /* $NetBSD: adb_direct.c,v 1.14 2000/06/08 22:10:45 tsubai Exp $ */ 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 * Copyright (C) 1994 Bradley A. Grantham 37 * All rights reserved. 38 * 39 * Redistribution and use in source and binary forms, with or without 40 * modification, are permitted provided that the following conditions 41 * are met: 42 * 1. Redistributions of source code must retain the above copyright 43 * notice, this list of conditions and the following disclaimer. 44 * 2. Redistributions in binary form must reproduce the above copyright 45 * notice, this list of conditions and the following disclaimer in the 46 * documentation and/or other materials provided with the distribution. 47 * 48 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 49 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 50 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 51 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 52 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 53 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 54 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 55 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 56 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 57 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 58 */ 59 60 /* 61 * This code is rather messy, but I don't have time right now 62 * to clean it up as much as I would like. 63 * But it works, so I'm happy. :-) jpw 64 */ 65 66 /* 67 * TO DO: 68 * - We could reduce the time spent in the adb_intr_* routines 69 * by having them save the incoming and outgoing data directly 70 * in the adbInbound and adbOutbound queues, as it would reduce 71 * the number of times we need to copy the data around. It 72 * would also make the code more readable and easier to follow. 73 * - (Related to above) Use the header part of adbCommand to 74 * reduce the number of copies we have to do of the data. 75 * - (Related to above) Actually implement the adbOutbound queue. 76 * This is fairly easy once you switch all the intr routines 77 * over to using adbCommand structs directly. 78 * - There is a bug in the state machine of adb_intr_cuda 79 * code that causes hangs, especially on 030 machines, probably 80 * because of some timing issues. Because I have been unable to 81 * determine the exact cause of this bug, I used the timeout function 82 * to check for and recover from this condition. If anyone finds 83 * the actual cause of this bug, the calls to timeout and the 84 * adb_cuda_tickle routine can be removed. 85 */ 86 87 #include <sys/param.h> 88 #include <sys/device.h> 89 #include <sys/fcntl.h> 90 #include <sys/poll.h> 91 #include <sys/selinfo.h> 92 #include <sys/proc.h> 93 #include <sys/signalvar.h> 94 #include <sys/timeout.h> 95 #include <sys/systm.h> 96 97 #include <machine/autoconf.h> 98 #include <machine/cpu.h> 99 #include <dev/ofw/openfirm.h> 100 101 #include <dev/adb/adb.h> 102 #include <macppc/dev/adbvar.h> 103 #include <macppc/dev/pm_direct.h> 104 #include <macppc/dev/viareg.h> 105 106 #include "apm.h" 107 108 #define printf_intr printf 109 110 #ifdef DEBUG 111 #ifndef ADB_DEBUG 112 #define ADB_DEBUG 113 #endif 114 #endif 115 116 int adb_polling; /* Are we polling? (Debugger mode) */ 117 #ifdef ADB_DEBUG 118 int adb_debug; /* Output debugging messages */ 119 #endif /* ADB_DEBUG */ 120 121 /* some misc. leftovers */ 122 #define vPB 0x0000 123 #define vPB3 0x08 124 #define vPB4 0x10 125 #define vPB5 0x20 126 #define vSR_INT 0x04 127 #define vSR_OUT 0x10 128 129 /* the type of ADB action that we are currently performing */ 130 #define ADB_ACTION_NOTREADY 0x1 /* has not been initialized yet */ 131 #define ADB_ACTION_IDLE 0x2 /* the bus is currently idle */ 132 #define ADB_ACTION_OUT 0x3 /* sending out a command */ 133 #define ADB_ACTION_IN 0x4 /* receiving data */ 134 135 /* 136 * Shortcuts for setting or testing the VIA bit states. 137 * Not all shortcuts are used for every type of ADB hardware. 138 */ 139 #define ADB_SET_STATE_IDLE_CUDA() via_reg_or(VIA1, vBufB, (vPB4 | vPB5)) 140 #define ADB_SET_STATE_TIP() via_reg_and(VIA1, vBufB, ~vPB5) 141 #define ADB_CLR_STATE_TIP() via_reg_or(VIA1, vBufB, vPB5) 142 #define ADB_TOGGLE_STATE_ACK_CUDA() via_reg_xor(VIA1, vBufB, vPB4) 143 #define ADB_SET_STATE_ACKOFF_CUDA() via_reg_or(VIA1, vBufB, vPB4) 144 #define ADB_SET_SR_INPUT() via_reg_and(VIA1, vACR, ~vSR_OUT) 145 #define ADB_SET_SR_OUTPUT() via_reg_or(VIA1, vACR, vSR_OUT) 146 #define ADB_SR() read_via_reg(VIA1, vSR) 147 #define ADB_VIA_INTR_ENABLE() write_via_reg(VIA1, vIER, 0x84) 148 #define ADB_VIA_INTR_DISABLE() write_via_reg(VIA1, vIER, 0x04) 149 #define ADB_VIA_CLR_INTR() write_via_reg(VIA1, vIFR, 0x04) 150 #define ADB_INTR_IS_OFF (vPB3 == (read_via_reg(VIA1, vBufB) & vPB3)) 151 #define ADB_INTR_IS_ON (0 == (read_via_reg(VIA1, vBufB) & vPB3)) 152 #define ADB_SR_INTR_IS_ON (vSR_INT == (read_via_reg(VIA1, \ 153 vIFR) & vSR_INT)) 154 155 /* 156 * This is the delay that is required (in uS) between certain 157 * ADB transactions. The actual timing delay for for each uS is 158 * calculated at boot time to account for differences in machine speed. 159 */ 160 #define ADB_DELAY 150 161 162 /* 163 * Maximum ADB message length; includes space for data, result, and 164 * device code - plus a little for safety. 165 */ 166 #define ADB_MAX_MSG_LENGTH 16 167 #define ADB_MAX_HDR_LENGTH 8 168 169 #define ADB_QUEUE 32 170 #define ADB_TICKLE_TICKS 4 171 172 /* 173 * Eventually used for two separate queues, the queue between 174 * the upper and lower halves, and the outgoing packet queue. 175 */ 176 struct adbCommand { 177 u_char header[ADB_MAX_HDR_LENGTH]; /* not used yet */ 178 u_char data[ADB_MAX_MSG_LENGTH]; /* packet data only */ 179 u_char *saveBuf; /* where to save result */ 180 u_char *compRout; /* completion routine pointer */ 181 u_char *compData; /* completion routine data pointer */ 182 u_int cmd; /* the original command for this data */ 183 u_int unsol; /* 1 if packet was unsolicited */ 184 u_int ack_only; /* 1 for no special processing */ 185 }; 186 187 /* 188 * A few variables that we need and their initial values. 189 */ 190 int adbHardware = ADB_HW_UNKNOWN; 191 int adbActionState = ADB_ACTION_NOTREADY; 192 int adbWaiting; /* waiting for return data from the device */ 193 int adbWriteDelay; /* working on (or waiting to do) a write */ 194 195 int adbWaitingCmd; /* ADB command we are waiting for */ 196 u_char *adbBuffer; /* pointer to user data area */ 197 void *adbCompRout; /* pointer to the completion routine */ 198 void *adbCompData; /* pointer to the completion routine data */ 199 int adbStarting = 1; /* doing adb_reinit so do polling differently */ 200 201 u_char adbInputBuffer[ADB_MAX_MSG_LENGTH]; /* data input buffer */ 202 u_char adbOutputBuffer[ADB_MAX_MSG_LENGTH]; /* data output buffer */ 203 204 int adbSentChars; /* how many characters we have sent */ 205 206 struct adbCommand adbInbound[ADB_QUEUE]; /* incoming queue */ 207 int adbInCount; /* how many packets in in queue */ 208 int adbInHead; /* head of in queue */ 209 int adbInTail; /* tail of in queue */ 210 211 int tickle_count; /* how many tickles seen for this packet? */ 212 int tickle_serial; /* the last packet tickled */ 213 int adb_cuda_serial; /* the current packet */ 214 struct timeout adb_cuda_timeout; 215 struct timeout adb_softintr_timeout; 216 int adbempty; /* nonzero if no adb devices */ 217 218 extern struct cfdriver adb_cd; 219 220 volatile u_char *Via1Base; 221 222 /* 223 * The following are private routines. 224 */ 225 #ifdef ADB_DEBUG 226 void print_single(u_char *); 227 #endif 228 void adb_intr_cuda(void); 229 void adb_soft_intr(void); 230 int send_adb_cuda(u_char *, u_char *, void *, void *, int); 231 void adb_cuda_tickle(void); 232 void adb_pass_up(struct adbCommand *); 233 void adb_op_comprout(caddr_t, caddr_t, int); 234 void adb_reinit(struct adb_softc *); 235 int count_adbs(struct adb_softc *); 236 int get_ind_adb_info(struct adb_softc *, ADBDataBlock *, int); 237 int get_adb_info(ADBDataBlock *, int); 238 int adb_op(Ptr, Ptr, Ptr, short); 239 void adb_hw_setup(void); 240 int adb_cmd_result(u_char *); 241 void setsoftadb(void); 242 243 int adb_intr(void *arg); 244 void adb_cuda_autopoll(void); 245 void adb_cuda_fileserver_mode(void); 246 247 #ifdef ADB_DEBUG 248 /* 249 * print_single 250 * Diagnostic display routine. Displays the hex values of the 251 * specified elements of the u_char. The length of the "string" 252 * is in [0]. 253 */ 254 void 255 print_single(u_char *str) 256 { 257 int x; 258 259 if (str == NULL) { 260 printf_intr("no data - null pointer\n"); 261 return; 262 } 263 if (*str == '\0') { 264 printf_intr("nothing returned\n"); 265 return; 266 } 267 if (*str > 20) { 268 printf_intr("ADB: ACK > 20 no way!\n"); 269 *str = 20; 270 } 271 printf_intr("(length=0x%x):", *str); 272 for (x = 1; x <= *str; x++) 273 printf_intr(" 0x%02x", str[x]); 274 printf_intr("\n"); 275 } 276 #endif 277 278 void 279 adb_cuda_tickle(void) 280 { 281 volatile int s; 282 283 if (adbActionState == ADB_ACTION_IN) { 284 if (tickle_serial == adb_cuda_serial) { 285 if (++tickle_count > 0) { 286 s = splhigh(); 287 adbActionState = ADB_ACTION_IDLE; 288 adbInputBuffer[0] = 0; 289 ADB_SET_STATE_IDLE_CUDA(); 290 splx(s); 291 } 292 } else { 293 tickle_serial = adb_cuda_serial; 294 tickle_count = 0; 295 } 296 } else { 297 tickle_serial = adb_cuda_serial; 298 tickle_count = 0; 299 } 300 301 timeout_add(&adb_cuda_timeout, ADB_TICKLE_TICKS); 302 } 303 304 /* 305 * called when when an adb interrupt happens 306 * 307 * Cuda version of adb_intr 308 * TO DO: do we want to add some calls to intr_dispatch() here to 309 * grab serial interrupts? 310 */ 311 void 312 adb_intr_cuda(void) 313 { 314 volatile int i, ending; 315 volatile unsigned int s; 316 struct adbCommand packet; 317 318 s = splhigh(); /* can't be too careful - might be called */ 319 /* from a routine, NOT an interrupt */ 320 321 ADB_VIA_CLR_INTR(); /* clear interrupt */ 322 ADB_VIA_INTR_DISABLE(); /* disable ADB interrupt on IIs. */ 323 324 switch_start: 325 switch (adbActionState) { 326 case ADB_ACTION_IDLE: 327 /* 328 * This is an unexpected packet, so grab the first (dummy) 329 * byte, set up the proper vars, and tell the chip we are 330 * starting to receive the packet by setting the TIP bit. 331 */ 332 adbInputBuffer[1] = ADB_SR(); 333 adb_cuda_serial++; 334 if (ADB_INTR_IS_OFF) /* must have been a fake start */ 335 break; 336 337 ADB_SET_SR_INPUT(); 338 ADB_SET_STATE_TIP(); 339 340 adbInputBuffer[0] = 1; 341 adbActionState = ADB_ACTION_IN; 342 #ifdef ADB_DEBUG 343 if (adb_debug) 344 printf_intr("idle 0x%02x ", adbInputBuffer[1]); 345 #endif 346 break; 347 348 case ADB_ACTION_IN: 349 adbInputBuffer[++adbInputBuffer[0]] = ADB_SR(); 350 /* intr off means this is the last byte (end of frame) */ 351 if (ADB_INTR_IS_OFF) 352 ending = 1; 353 else 354 ending = 0; 355 356 if (1 == ending) { /* end of message? */ 357 #ifdef ADB_DEBUG 358 if (adb_debug) { 359 printf_intr("in end 0x%02x ", 360 adbInputBuffer[adbInputBuffer[0]]); 361 print_single(adbInputBuffer); 362 } 363 #endif 364 365 /* 366 * Are we waiting AND does this packet match what we 367 * are waiting for AND is it coming from either the 368 * ADB or RTC/PRAM sub-device? This section _should_ 369 * recognize all ADB and RTC/PRAM type commands, but 370 * there may be more... NOTE: commands are always at 371 * [4], even for RTC/PRAM commands. 372 */ 373 /* set up data for adb_pass_up */ 374 memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1); 375 376 if ((adbWaiting == 1) && 377 (adbInputBuffer[4] == adbWaitingCmd) && 378 ((adbInputBuffer[2] == 0x00) || 379 (adbInputBuffer[2] == 0x01))) { 380 packet.saveBuf = adbBuffer; 381 packet.compRout = adbCompRout; 382 packet.compData = adbCompData; 383 packet.unsol = 0; 384 packet.ack_only = 0; 385 adb_pass_up(&packet); 386 387 adbWaitingCmd = 0; /* reset "waiting" vars */ 388 adbWaiting = 0; 389 adbBuffer = NULL; 390 adbCompRout = NULL; 391 adbCompData = NULL; 392 } else { 393 packet.unsol = 1; 394 packet.ack_only = 0; 395 adb_pass_up(&packet); 396 } 397 398 399 /* reset vars and signal the end of this frame */ 400 adbActionState = ADB_ACTION_IDLE; 401 adbInputBuffer[0] = 0; 402 ADB_SET_STATE_IDLE_CUDA(); 403 /*ADB_SET_SR_INPUT();*/ 404 405 /* 406 * If there is something waiting to be sent out, 407 * the set everything up and send the first byte. 408 */ 409 if (adbWriteDelay == 1) { 410 delay(ADB_DELAY); /* required */ 411 adbSentChars = 0; 412 adbActionState = ADB_ACTION_OUT; 413 /* 414 * If the interrupt is on, we were too slow 415 * and the chip has already started to send 416 * something to us, so back out of the write 417 * and start a read cycle. 418 */ 419 if (ADB_INTR_IS_ON) { 420 ADB_SET_SR_INPUT(); 421 ADB_SET_STATE_IDLE_CUDA(); 422 adbSentChars = 0; 423 adbActionState = ADB_ACTION_IDLE; 424 adbInputBuffer[0] = 0; 425 break; 426 } 427 /* 428 * If we got here, it's ok to start sending 429 * so load the first byte and tell the chip 430 * we want to send. 431 */ 432 ADB_SET_STATE_TIP(); 433 ADB_SET_SR_OUTPUT(); 434 write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); 435 } 436 } else { 437 ADB_TOGGLE_STATE_ACK_CUDA(); 438 #ifdef ADB_DEBUG 439 if (adb_debug) 440 printf_intr("in 0x%02x ", 441 adbInputBuffer[adbInputBuffer[0]]); 442 #endif 443 } 444 break; 445 446 case ADB_ACTION_OUT: 447 i = ADB_SR(); /* reset SR-intr in IFR */ 448 #ifdef ADB_DEBUG 449 if (adb_debug) 450 printf_intr("intr out 0x%02x ", i); 451 #endif 452 453 adbSentChars++; 454 if (ADB_INTR_IS_ON) { /* ADB intr low during write */ 455 #ifdef ADB_DEBUG 456 if (adb_debug) 457 printf_intr("intr was on "); 458 #endif 459 ADB_SET_SR_INPUT(); /* make sure SR is set to IN */ 460 ADB_SET_STATE_IDLE_CUDA(); 461 adbSentChars = 0; /* must start all over */ 462 adbActionState = ADB_ACTION_IDLE; /* new state */ 463 adbInputBuffer[0] = 0; 464 adbWriteDelay = 1; /* must retry when done with 465 * read */ 466 delay(ADB_DELAY); 467 goto switch_start; /* process next state right 468 * now */ 469 break; 470 } 471 if (adbOutputBuffer[0] == adbSentChars) { /* check for done */ 472 if (0 == adb_cmd_result(adbOutputBuffer)) { /* do we expect data 473 * back? */ 474 adbWaiting = 1; /* signal waiting for return */ 475 adbWaitingCmd = adbOutputBuffer[2]; /* save waiting command */ 476 } else { /* no talk, so done */ 477 /* set up stuff for adb_pass_up */ 478 memcpy(packet.data, adbInputBuffer, adbInputBuffer[0] + 1); 479 packet.saveBuf = adbBuffer; 480 packet.compRout = adbCompRout; 481 packet.compData = adbCompData; 482 packet.cmd = adbWaitingCmd; 483 packet.unsol = 0; 484 packet.ack_only = 1; 485 adb_pass_up(&packet); 486 487 /* reset "waiting" vars, just in case */ 488 adbWaitingCmd = 0; 489 adbBuffer = NULL; 490 adbCompRout = NULL; 491 adbCompData = NULL; 492 } 493 494 adbWriteDelay = 0; /* done writing */ 495 adbActionState = ADB_ACTION_IDLE; /* signal bus is idle */ 496 ADB_SET_SR_INPUT(); 497 ADB_SET_STATE_IDLE_CUDA(); 498 #ifdef ADB_DEBUG 499 if (adb_debug) 500 printf_intr("write done "); 501 #endif 502 } else { 503 write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); /* send next byte */ 504 ADB_TOGGLE_STATE_ACK_CUDA(); /* signal byte ready to 505 * shift */ 506 #ifdef ADB_DEBUG 507 if (adb_debug) 508 printf_intr("toggle "); 509 #endif 510 } 511 break; 512 513 case ADB_ACTION_NOTREADY: 514 #ifdef ADB_DEBUG 515 if (adb_debug) 516 printf_intr("adb: not yet initialized\n"); 517 #endif 518 break; 519 520 default: 521 ; 522 #ifdef ADB_DEBUG 523 if (adb_debug) 524 printf_intr("intr: unknown ADB state\n"); 525 #endif 526 } 527 528 ADB_VIA_INTR_ENABLE(); /* enable ADB interrupt on IIs. */ 529 530 splx(s); /* restore */ 531 } 532 533 534 int 535 send_adb_cuda(u_char * in, u_char * buffer, void *compRout, void *data, 536 int command) 537 { 538 int s, len; 539 540 #ifdef ADB_DEBUG 541 if (adb_debug) 542 printf_intr("SEND\n"); 543 #endif 544 545 if (adbActionState == ADB_ACTION_NOTREADY) 546 return 1; 547 548 /* Don't interrupt while we are messing with the ADB */ 549 s = splhigh(); 550 551 if ((adbActionState == ADB_ACTION_IDLE) && /* ADB available? */ 552 (ADB_INTR_IS_OFF)) { /* and no incoming interrupt? */ 553 } else 554 if (adbWriteDelay == 0) /* it's busy, but is anything waiting? */ 555 adbWriteDelay = 1; /* if no, then we'll "queue" 556 * it up */ 557 else { 558 splx(s); 559 return 1; /* really busy! */ 560 } 561 562 #ifdef ADB_DEBUG 563 if (adb_debug) 564 printf_intr("QUEUE\n"); 565 #endif 566 if ((long)in == (long)0) { /* need to convert? */ 567 if ((command & 0x0c) == 0x08) /* copy addl data ONLY if 568 * doing a listen! */ 569 len = buffer[0]; /* length of additional data */ 570 else 571 len = 0;/* no additional data */ 572 573 adbOutputBuffer[0] = 2 + len; /* dev. type + command + addl. 574 * data */ 575 adbOutputBuffer[1] = 0x00; /* mark as an ADB command */ 576 adbOutputBuffer[2] = (u_char)command; /* load command */ 577 578 /* copy additional output data, if any */ 579 memcpy(adbOutputBuffer + 3, buffer + 1, len); 580 } else 581 /* if data ready, just copy over */ 582 memcpy(adbOutputBuffer, in, in[0] + 2); 583 584 adbSentChars = 0; /* nothing sent yet */ 585 adbBuffer = buffer; /* save buffer to know where to save result */ 586 adbCompRout = compRout; /* save completion routine pointer */ 587 adbCompData = data; /* save completion routine data pointer */ 588 adbWaitingCmd = adbOutputBuffer[2]; /* save wait command */ 589 590 if (adbWriteDelay != 1) { /* start command now? */ 591 #ifdef ADB_DEBUG 592 if (adb_debug) 593 printf_intr("out start NOW"); 594 #endif 595 delay(ADB_DELAY); 596 adbActionState = ADB_ACTION_OUT; /* set next state */ 597 ADB_SET_SR_OUTPUT(); /* set shift register for OUT */ 598 write_via_reg(VIA1, vSR, adbOutputBuffer[adbSentChars + 1]); /* load byte for output */ 599 ADB_SET_STATE_ACKOFF_CUDA(); 600 ADB_SET_STATE_TIP(); /* tell ADB that we want to send */ 601 } 602 adbWriteDelay = 1; /* something in the write "queue" */ 603 604 splx(s); 605 606 if ((s & (1 << 18)) || adb_polling) /* XXX were VIA1 interrupts blocked ? */ 607 /* poll until byte done */ 608 while ((adbActionState != ADB_ACTION_IDLE) || (ADB_INTR_IS_ON) 609 || (adbWaiting == 1)) 610 if (ADB_SR_INTR_IS_ON) { /* wait for "interrupt" */ 611 adb_intr_cuda(); /* process it */ 612 if (cold) 613 delay(ADB_DELAY); 614 adb_soft_intr(); 615 } 616 617 return 0; 618 } 619 620 /* 621 * Called when when an adb interrupt happens. 622 * This routine simply transfers control over to the appropriate 623 * code for the machine we are running on. 624 */ 625 int 626 adb_intr(void *arg) 627 { 628 switch (adbHardware) { 629 case ADB_HW_PMU: 630 pm_intr(); 631 break; 632 633 case ADB_HW_CUDA: 634 adb_intr_cuda(); 635 break; 636 } 637 return 1; 638 } 639 640 641 /* 642 * adb_pass_up is called by the interrupt-time routines. 643 * It takes the raw packet data that was received from the 644 * device and puts it into the queue that the upper half 645 * processes. It then signals for a soft ADB interrupt which 646 * will eventually call the upper half routine (adb_soft_intr). 647 * 648 * If in->unsol is 0, then this is either the notification 649 * that the packet was sent (on a LISTEN, for example), or the 650 * response from the device (on a TALK). The completion routine 651 * is called only if the user specified one. 652 * 653 * If in->unsol is 1, then this packet was unsolicited and 654 * so we look up the device in the ADB device table to determine 655 * what its default service routine is. 656 * 657 * If in->ack_only is 1, then we really only need to call 658 * the completion routine, so don't do any other stuff. 659 * 660 * Note that in->data contains the packet header AND data, 661 * while adbInbound[]->data contains ONLY data. 662 * 663 * Note: Called only at interrupt time. Assumes this. 664 */ 665 void 666 adb_pass_up(struct adbCommand *in) 667 { 668 int start = 0, len = 0, cmd = 0; 669 ADBDataBlock block; 670 671 if (adbInCount >= ADB_QUEUE) { 672 #ifdef ADB_DEBUG 673 if (adb_debug) 674 printf_intr("adb: ring buffer overflow\n"); 675 #endif 676 return; 677 } 678 679 if (in->ack_only) { 680 len = in->data[0]; 681 cmd = in->cmd; 682 start = 0; 683 } else { 684 switch (adbHardware) { 685 case ADB_HW_CUDA: 686 /* If it's unsolicited, accept only ADB data for now */ 687 if (in->unsol) 688 if (0 != in->data[2]) 689 return; 690 cmd = in->data[4]; 691 if (in->data[0] < 5) 692 len = 0; 693 else 694 len = in->data[0]-4; 695 start = 4; 696 break; 697 698 case ADB_HW_PMU: 699 cmd = in->data[1]; 700 if (in->data[0] < 2) 701 len = 0; 702 else 703 len = in->data[0]-1; 704 start = 1; 705 break; 706 707 case ADB_HW_UNKNOWN: 708 return; 709 } 710 711 /* Make sure there is a valid device entry for this device */ 712 if (in->unsol) { 713 /* ignore unsolicited data during adbreinit */ 714 if (adbStarting) 715 return; 716 /* get device's comp. routine and data area */ 717 if (-1 == get_adb_info(&block, ADB_CMDADDR(cmd))) 718 return; 719 } 720 } 721 722 /* 723 * If this is an unsolicited packet, we need to fill in 724 * some info so adb_soft_intr can process this packet 725 * properly. If it's not unsolicited, then use what 726 * the caller sent us. 727 */ 728 if (in->unsol) { 729 adbInbound[adbInTail].compRout = (void *)block.dbServiceRtPtr; 730 adbInbound[adbInTail].compData = (void *)block.dbDataAreaAddr; 731 adbInbound[adbInTail].saveBuf = (void *)adbInbound[adbInTail].data; 732 } else { 733 adbInbound[adbInTail].compRout = (void *)in->compRout; 734 adbInbound[adbInTail].compData = (void *)in->compData; 735 adbInbound[adbInTail].saveBuf = (void *)in->saveBuf; 736 } 737 738 #ifdef ADB_DEBUG 739 if (adb_debug && in->data[1] == 2) 740 printf_intr("adb: caught error\n"); 741 #endif 742 743 /* copy the packet data over */ 744 /* 745 * TO DO: If the *_intr routines fed their incoming data 746 * directly into an adbCommand struct, which is passed to 747 * this routine, then we could eliminate this copy. 748 */ 749 memcpy(adbInbound[adbInTail].data + 1, in->data + start + 1, len); 750 adbInbound[adbInTail].data[0] = len; 751 adbInbound[adbInTail].cmd = cmd; 752 753 adbInCount++; 754 if (++adbInTail >= ADB_QUEUE) 755 adbInTail = 0; 756 757 /* 758 * If the debugger is running, call upper half manually. 759 * Otherwise, trigger a soft interrupt to handle the rest later. 760 */ 761 if (adb_polling) 762 adb_soft_intr(); 763 else 764 setsoftadb(); 765 } 766 767 768 /* 769 * Called to process the packets after they have been 770 * placed in the incoming queue. 771 * 772 */ 773 void 774 adb_soft_intr(void) 775 { 776 int s; 777 int cmd = 0; 778 u_char *buffer; 779 u_char *comprout; 780 u_char *compdata; 781 782 /*delay(2*ADB_DELAY);*/ 783 784 while (adbInCount) { 785 #ifdef ADB_DEBUG 786 if (adb_debug & 0x80) 787 printf_intr("%x %x %x ", 788 adbInCount, adbInHead, adbInTail); 789 #endif 790 /* get the data we need from the queue */ 791 buffer = adbInbound[adbInHead].saveBuf; 792 comprout = adbInbound[adbInHead].compRout; 793 compdata = adbInbound[adbInHead].compData; 794 cmd = adbInbound[adbInHead].cmd; 795 796 /* copy over data to data area if it's valid */ 797 /* 798 * Note that for unsol packets we don't want to copy the 799 * data anywhere, so buffer was already set to 0. 800 * For ack_only buffer was set to 0, so don't copy. 801 */ 802 if (buffer) 803 memcpy(buffer, adbInbound[adbInHead].data, 804 adbInbound[adbInHead].data[0] + 1); 805 806 #ifdef ADB_DEBUG 807 if (adb_debug & 0x80) { 808 printf_intr("%p %p %p %x ", 809 buffer, comprout, compdata, (short)cmd); 810 printf_intr("buf: "); 811 print_single(adbInbound[adbInHead].data); 812 } 813 #endif 814 /* 815 * Remove the packet from the queue before calling 816 * the completion routine, so that the completion 817 * routine can reentrantly process the queue. For 818 * example, this happens when polling is turned on 819 * by entering the debugger by keystroke. 820 */ 821 s = splhigh(); 822 adbInCount--; 823 if (++adbInHead >= ADB_QUEUE) 824 adbInHead = 0; 825 splx(s); 826 827 /* call default completion routine if it's valid */ 828 if (comprout) { 829 ((int (*)(u_char *, u_char *, int)) comprout) 830 (buffer, compdata, cmd); 831 } 832 833 } 834 } 835 836 837 /* 838 * This is my version of the ADBOp routine. It mainly just calls the 839 * hardware-specific routine. 840 * 841 * data : pointer to data area to be used by compRout 842 * compRout : completion routine 843 * buffer : for LISTEN: points to data to send - MAX 8 data bytes, 844 * byte 0 = # of bytes 845 * : for TALK: points to place to save return data 846 * command : the adb command to send 847 * result : 0 = success 848 * : -1 = could not complete 849 */ 850 int 851 adb_op(Ptr buffer, Ptr compRout, Ptr data, short command) 852 { 853 int result; 854 855 switch (adbHardware) { 856 case ADB_HW_PMU: 857 result = pm_adb_op((u_char *)buffer, (void *)compRout, 858 (void *)data, (int)command); 859 860 if (result == 0) 861 return 0; 862 else 863 return -1; 864 break; 865 866 case ADB_HW_CUDA: 867 result = send_adb_cuda(NULL, (u_char *)buffer, 868 (void *)compRout, (void *)data, (int)command); 869 if (result == 0) 870 return 0; 871 else 872 return -1; 873 break; 874 875 default: 876 return -1; 877 } 878 } 879 880 881 /* 882 * adb_hw_setup 883 * This routine sets up the possible machine specific hardware 884 * config (mainly VIA settings) for the various models. 885 */ 886 void 887 adb_hw_setup(void) 888 { 889 volatile int i; 890 891 switch (adbHardware) { 892 893 case ADB_HW_PMU: 894 /* 895 * XXX - really PM_VIA_CLR_INTR - should we put it in 896 * pm_direct.h? 897 */ 898 write_via_reg(VIA1, vIFR, 0x90); /* clear interrupt */ 899 break; 900 901 case ADB_HW_CUDA: 902 via_reg_or(VIA1, vDirB, 0x30); /* register B bits 4 and 5: 903 * outputs */ 904 via_reg_and(VIA1, vDirB, 0xf7); /* register B bit 3: input */ 905 via_reg_and(VIA1, vACR, ~vSR_OUT); /* make sure SR is set 906 * to IN */ 907 write_via_reg(VIA1, vACR, (read_via_reg(VIA1, vACR) | 0x0c) & ~0x10); 908 adbActionState = ADB_ACTION_IDLE; /* used by all types of 909 * hardware */ 910 write_via_reg(VIA1, vIER, 0x84);/* make sure VIA interrupts 911 * are on */ 912 ADB_SET_STATE_IDLE_CUDA(); /* set ADB bus state to idle */ 913 914 /* sort of a device reset */ 915 i = ADB_SR(); /* clear interrupt */ 916 ADB_VIA_INTR_DISABLE(); /* no interrupts while clearing */ 917 ADB_SET_STATE_IDLE_CUDA(); /* reset state to idle */ 918 delay(ADB_DELAY); 919 ADB_SET_STATE_TIP(); /* signal start of frame */ 920 delay(ADB_DELAY); 921 ADB_TOGGLE_STATE_ACK_CUDA(); 922 delay(ADB_DELAY); 923 ADB_CLR_STATE_TIP(); 924 delay(ADB_DELAY); 925 ADB_SET_STATE_IDLE_CUDA(); /* back to idle state */ 926 i = ADB_SR(); /* clear interrupt */ 927 ADB_VIA_INTR_ENABLE(); /* ints ok now */ 928 break; 929 930 case ADB_HW_UNKNOWN: 931 default: 932 write_via_reg(VIA1, vIER, 0x04);/* turn interrupts off - TO 933 * DO: turn PB ints off? */ 934 break; 935 } 936 } 937 938 /* 939 * adb_reinit sets up the adb stuff 940 * 941 */ 942 void 943 adb_reinit(struct adb_softc *sc) 944 { 945 u_char send_string[ADB_MAX_MSG_LENGTH]; 946 ADBDataBlock data; /* temp. holder for getting device info */ 947 volatile int i, x; 948 int s; 949 int command; 950 int result; 951 int saveptr; /* point to next free relocation address */ 952 int device; 953 int nonewtimes; /* times thru loop w/o any new devices */ 954 int ADBNumDevices = 0; 955 956 /* Make sure we are not interrupted while building the table. */ 957 if (adbHardware != ADB_HW_PMU) /* ints must be on for PB? */ 958 s = splhigh(); 959 960 /* Let intr routines know we are running reinit */ 961 adbStarting = 1; 962 963 /* 964 * Initialize the ADB table. For now, we'll always use the same table 965 * that is defined at the beginning of this file - no mallocs. 966 */ 967 for (i = 0; i < 16; i++) 968 sc->sc_devtable[i].handler_id = 0; 969 970 adb_hw_setup(); /* init the VIA bits and hard reset ADB */ 971 972 delay(1000); 973 974 /* send an ADB reset first */ 975 adb_op_sync((Ptr)0, (short)0x00); 976 delay(200000); 977 978 /* 979 * Probe for ADB devices. Probe devices 1-15 quickly to determine 980 * which device addresses are in use and which are free. For each 981 * address that is in use, move the device at that address to a higher 982 * free address. Continue doing this at that address until no device 983 * responds at that address. Then move the last device that was moved 984 * back to the original address. Do this for the remaining addresses 985 * that we determined were in use. 986 * 987 * When finished, do this entire process over again with the updated 988 * list of in use addresses. Do this until no new devices have been 989 * found in 20 passes though the in use address list. (This probably 990 * seems long and complicated, but it's the best way to detect multiple 991 * devices at the same address - sometimes it takes a couple of tries 992 * before the collision is detected.) 993 */ 994 995 /* initial scan through the devices */ 996 for (i = 1; i < 16; i++) { 997 send_string[0] = 0; 998 command = ADBTALK(i, 3); 999 result = adb_op_sync((Ptr)send_string, (short)command); 1000 1001 if (send_string[0] != 0) { 1002 /* check for valid device handler */ 1003 switch (send_string[2]) { 1004 case 0: 1005 case 0xfd: 1006 case 0xfe: 1007 case 0xff: 1008 continue; /* invalid, skip */ 1009 } 1010 1011 /* found a device */ 1012 ++ADBNumDevices; 1013 KASSERT(ADBNumDevices < 16); 1014 sc->sc_devtable[ADBNumDevices].handler_id = 1015 (int)send_string[2]; 1016 sc->sc_devtable[ADBNumDevices].orig_addr = i; 1017 sc->sc_devtable[ADBNumDevices].curr_addr = i; 1018 sc->sc_devtable[ADBNumDevices].data = NULL; 1019 sc->sc_devtable[ADBNumDevices].handler = NULL; 1020 } 1021 } 1022 1023 /* find highest unused address */ 1024 for (saveptr = 15; saveptr > 0; saveptr--) 1025 if (-1 == get_adb_info(&data, saveptr)) 1026 break; 1027 1028 #ifdef ADB_DEBUG 1029 if (adb_debug & 0x80) { 1030 printf_intr("first free is: 0x%02x\n", saveptr); 1031 printf_intr("devices: %i\n", ADBNumDevices); 1032 } 1033 #endif 1034 1035 nonewtimes = 0; /* no loops w/o new devices */ 1036 while (saveptr > 0 && nonewtimes++ < 11) { 1037 for (i = 1; i <= ADBNumDevices; i++) { 1038 device = sc->sc_devtable[i].curr_addr; 1039 #ifdef ADB_DEBUG 1040 if (adb_debug & 0x80) 1041 printf_intr("moving device 0x%02x to 0x%02x " 1042 "(index 0x%02x) ", device, saveptr, i); 1043 #endif 1044 1045 /* send TALK R3 to address */ 1046 command = ADBTALK(device, 3); 1047 adb_op_sync((Ptr)send_string, (short)command); 1048 1049 /* move device to higher address */ 1050 command = ADBLISTEN(device, 3); 1051 send_string[0] = 2; 1052 send_string[1] = (u_char)(saveptr | 0x60); 1053 send_string[2] = 0xfe; 1054 adb_op_sync((Ptr)send_string, (short)command); 1055 delay(500); 1056 1057 /* send TALK R3 - anything at new address? */ 1058 command = ADBTALK(saveptr, 3); 1059 adb_op_sync((Ptr)send_string, (short)command); 1060 delay(500); 1061 1062 if (send_string[0] == 0) { 1063 #ifdef ADB_DEBUG 1064 if (adb_debug & 0x80) 1065 printf_intr("failed, continuing\n"); 1066 #endif 1067 continue; 1068 } 1069 1070 /* send TALK R3 - anything at old address? */ 1071 command = ADBTALK(device, 3); 1072 result = adb_op_sync((Ptr)send_string, (short)command); 1073 if (send_string[0] != 0) { 1074 /* check for valid device handler */ 1075 switch (send_string[2]) { 1076 case 0: 1077 case 0xfd: 1078 case 0xfe: 1079 case 0xff: 1080 continue; /* invalid, skip */ 1081 } 1082 1083 /* new device found */ 1084 /* update data for previously moved device */ 1085 sc->sc_devtable[i].curr_addr = saveptr; 1086 #ifdef ADB_DEBUG 1087 if (adb_debug & 0x80) 1088 printf_intr("old device at index %i\n",i); 1089 #endif 1090 /* add new device in table */ 1091 #ifdef ADB_DEBUG 1092 if (adb_debug & 0x80) 1093 printf_intr("new device found\n"); 1094 #endif 1095 if (saveptr > ADBNumDevices) { 1096 ++ADBNumDevices; 1097 KASSERT(ADBNumDevices < 16); 1098 } 1099 sc->sc_devtable[ADBNumDevices].handler_id = 1100 (int)send_string[2]; 1101 sc->sc_devtable[ADBNumDevices].orig_addr = device; 1102 sc->sc_devtable[ADBNumDevices].curr_addr = device; 1103 /* These will be set correctly in adbsys.c */ 1104 /* Until then, unsol. data will be ignored. */ 1105 sc->sc_devtable[ADBNumDevices].data = NULL; 1106 sc->sc_devtable[ADBNumDevices].handler = NULL; 1107 /* find next unused address */ 1108 for (x = saveptr; x > 0; x--) { 1109 if (-1 == get_adb_info(&data, x)) { 1110 saveptr = x; 1111 break; 1112 } 1113 } 1114 if (x == 0) 1115 saveptr = 0; 1116 #ifdef ADB_DEBUG 1117 if (adb_debug & 0x80) 1118 printf_intr("new free is 0x%02x\n", 1119 saveptr); 1120 #endif 1121 nonewtimes = 0; 1122 } else { 1123 #ifdef ADB_DEBUG 1124 if (adb_debug & 0x80) 1125 printf_intr("moving back...\n"); 1126 #endif 1127 /* move old device back */ 1128 command = ADBLISTEN(saveptr, 3); 1129 send_string[0] = 2; 1130 send_string[1] = (u_char)(device | 0x60); 1131 send_string[2] = 0xfe; 1132 adb_op_sync((Ptr)send_string, (short)command); 1133 delay(1000); 1134 } 1135 } 1136 } 1137 1138 #ifdef ADB_DEBUG 1139 if (adb_debug) { 1140 for (i = 1; i <= ADBNumDevices; i++) { 1141 x = get_ind_adb_info(sc, &data, i); 1142 if (x != -1) 1143 printf_intr("index 0x%x, addr 0x%x, type 0x%x\n", 1144 i, x, data.devType); 1145 } 1146 } 1147 #endif 1148 1149 #ifdef ADB_DEBUG 1150 if (adb_debug) { 1151 if (0 == ADBNumDevices) /* tell user if no devices found */ 1152 printf_intr("adb: no devices found\n"); 1153 } 1154 #endif 1155 1156 adbStarting = 0; /* not starting anymore */ 1157 #ifdef ADB_DEBUG 1158 if (adb_debug) 1159 printf_intr("adb: adb_reinit complete\n"); 1160 #endif 1161 1162 if (adbHardware == ADB_HW_CUDA) { 1163 timeout_set(&adb_cuda_timeout, (void *)adb_cuda_tickle, NULL); 1164 timeout_add(&adb_cuda_timeout, ADB_TICKLE_TICKS); 1165 } 1166 1167 if (adbHardware != ADB_HW_PMU) /* ints must be on for PB? */ 1168 splx(s); 1169 } 1170 1171 1172 /* 1173 * adb_cmd_result 1174 * 1175 * This routine lets the caller know whether the specified adb command string 1176 * should expect a returned result, such as a TALK command. 1177 * 1178 * returns: 0 if a result should be expected 1179 * 1 if a result should NOT be expected 1180 */ 1181 int 1182 adb_cmd_result(u_char *in) 1183 { 1184 switch (adbHardware) { 1185 case ADB_HW_CUDA: 1186 /* was it an ADB talk command? */ 1187 if ((in[1] == 0x00) && ((in[2] & 0x0c) == 0x0c)) 1188 return 0; 1189 /* was it an RTC/PRAM read date/time? */ 1190 if ((in[1] == 0x01) && (in[2] == 0x03)) 1191 return 0; 1192 return 1; 1193 1194 case ADB_HW_PMU: 1195 return 1; 1196 1197 default: 1198 return 1; 1199 } 1200 } 1201 1202 1203 /* 1204 * adb_op_sync 1205 * 1206 * This routine does exactly what the adb_op routine does, except that after 1207 * the adb_op is called, it waits until the return value is present before 1208 * returning. 1209 */ 1210 int 1211 adb_op_sync(Ptr buffer, short command) 1212 { 1213 int tmout; 1214 int result; 1215 volatile int flag = 0; 1216 1217 result = adb_op(buffer, (void *)adb_op_comprout, 1218 (void *)&flag, command); /* send command */ 1219 if (result == 0) { /* send ok? */ 1220 /* 1221 * Total time to wait is calculated as follows: 1222 * - Tlt (stop to start time): 260 usec 1223 * - start bit: 100 usec 1224 * - up to 8 data bytes: 64 * 100 usec = 6400 usec 1225 * - stop bit (with SRQ): 140 usec 1226 * Total: 6900 usec 1227 * 1228 * This is the total time allowed by the specification. Any 1229 * device that doesn't conform to this will fail to operate 1230 * properly on some Apple systems. In spite of this we 1231 * double the time to wait; some Cuda-based apparently 1232 * queues some commands and allows the main CPU to continue 1233 * processing (radical concept, eh?). To be safe, allow 1234 * time for two complete ADB transactions to occur. 1235 */ 1236 for (tmout = 13800; !flag && tmout >= 10; tmout -= 10) 1237 delay(10); 1238 if (!flag && tmout > 0) 1239 delay(tmout); 1240 1241 if (!flag) 1242 result = -2; 1243 } 1244 1245 return result; 1246 } 1247 1248 1249 /* 1250 * adb_op_comprout 1251 * 1252 * This function is used by the adb_op_sync routine so it knows when the 1253 * function is done. 1254 */ 1255 void 1256 adb_op_comprout(caddr_t buffer, caddr_t compdata, int cmd) 1257 { 1258 *(int *)compdata = 0x01; /* update flag value */ 1259 } 1260 1261 int 1262 count_adbs(struct adb_softc *sc) 1263 { 1264 int i; 1265 int found; 1266 1267 found = 0; 1268 1269 for (i = 1; i < 16; i++) 1270 if (0 != sc->sc_devtable[i].handler_id) 1271 found++; 1272 1273 return found; 1274 } 1275 1276 int 1277 get_ind_adb_info(struct adb_softc *sc, ADBDataBlock * info, int index) 1278 { 1279 if ((index < 1) || (index > 15)) /* check range 1-15 */ 1280 return (-1); 1281 1282 #ifdef ADB_DEBUG 1283 if (adb_debug & 0x80) 1284 printf_intr("index 0x%x handler id 0x%x\n", index, 1285 sc->sc_devtable[index].handler_id); 1286 #endif 1287 if (0 == sc->sc_devtable[index].handler_id) /* make sure it's a valid entry */ 1288 return (-1); 1289 1290 info->devType = sc->sc_devtable[index].handler_id; 1291 info->origADBAddr = sc->sc_devtable[index].orig_addr; 1292 info->dbServiceRtPtr = (Ptr)sc->sc_devtable[index].handler; 1293 info->dbDataAreaAddr = (Ptr)sc->sc_devtable[index].data; 1294 1295 return (sc->sc_devtable[index].curr_addr); 1296 } 1297 1298 int 1299 get_adb_info(ADBDataBlock * info, int adbAddr) 1300 { 1301 struct adb_softc *sc = adb_cd.cd_devs[0]; 1302 int i; 1303 1304 if (sc == NULL) 1305 return (-1); 1306 1307 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */ 1308 return (-1); 1309 1310 for (i = 1; i < 15; i++) 1311 if (sc->sc_devtable[i].curr_addr == adbAddr) { 1312 info->devType = sc->sc_devtable[i].handler_id; 1313 info->origADBAddr = sc->sc_devtable[i].orig_addr; 1314 info->dbServiceRtPtr = (Ptr)sc->sc_devtable[i].handler; 1315 info->dbDataAreaAddr = sc->sc_devtable[i].data; 1316 return 0; /* found */ 1317 } 1318 1319 return (-1); /* not found */ 1320 } 1321 1322 int 1323 set_adb_info(ADBSetInfoBlock * info, int adbAddr) 1324 { 1325 struct adb_softc *sc = adb_cd.cd_devs[0]; 1326 int i; 1327 1328 if (sc == NULL) 1329 return (-1); 1330 1331 if ((adbAddr < 1) || (adbAddr > 15)) /* check range 1-15 */ 1332 return (-1); 1333 1334 for (i = 1; i < 15; i++) 1335 if (sc->sc_devtable[i].curr_addr == adbAddr) { 1336 sc->sc_devtable[i].handler = 1337 (void *)(info->siServiceRtPtr); 1338 sc->sc_devtable[i].data = info->siDataAreaAddr; 1339 return 0; /* found */ 1340 } 1341 1342 return (-1); /* not found */ 1343 1344 } 1345 1346 /* caller should really use machine-independent version: getPramTime */ 1347 /* this version does pseudo-adb access only */ 1348 int 1349 adb_read_date_time(time_t *time) 1350 { 1351 u_char output[ADB_MAX_MSG_LENGTH]; 1352 int result; 1353 int retcode; 1354 volatile int flag = 0; 1355 u_int32_t t; 1356 1357 switch (adbHardware) { 1358 case ADB_HW_PMU: 1359 pm_read_date_time(time); 1360 retcode = 0; 1361 break; 1362 1363 case ADB_HW_CUDA: 1364 output[0] = 0x02; /* 2 byte message */ 1365 output[1] = 0x01; /* to pram/rtc device */ 1366 output[2] = 0x03; /* read date/time */ 1367 result = send_adb_cuda((u_char *)output, (u_char *)output, 1368 (void *)adb_op_comprout, (void *)&flag, (int)0); 1369 if (result != 0) { /* exit if not sent */ 1370 retcode = -1; 1371 break; 1372 } 1373 1374 while (0 == flag) /* wait for result */ 1375 ; 1376 1377 delay(20); /* completion occurs too soon? */ 1378 memcpy(&t, output + 1, sizeof(t)); 1379 *time = (time_t)t; 1380 retcode = 0; 1381 break; 1382 1383 case ADB_HW_UNKNOWN: 1384 default: 1385 retcode = -1; 1386 break; 1387 } 1388 if (retcode == 0) { 1389 #define DIFF19041970 2082844800 1390 *time -= DIFF19041970; 1391 1392 } else { 1393 *time = 0; 1394 } 1395 return retcode; 1396 } 1397 1398 /* caller should really use machine-independent version: setPramTime */ 1399 /* this version does pseudo-adb access only */ 1400 int 1401 adb_set_date_time(time_t time) 1402 { 1403 u_char output[ADB_MAX_MSG_LENGTH]; 1404 int result; 1405 volatile int flag = 0; 1406 u_int32_t t; 1407 1408 time += DIFF19041970; 1409 switch (adbHardware) { 1410 1411 case ADB_HW_CUDA: 1412 t = time; /* XXX eventually truncates */ 1413 1414 output[0] = 0x06; /* 6 byte message */ 1415 output[1] = 0x01; /* to pram/rtc device */ 1416 output[2] = 0x09; /* set date/time */ 1417 output[3] = (u_char)(t >> 24); 1418 output[4] = (u_char)(t >> 16); 1419 output[5] = (u_char)(t >> 8); 1420 output[6] = (u_char)(t); 1421 result = send_adb_cuda((u_char *)output, NULL, 1422 (void *)adb_op_comprout, (void *)&flag, (int)0); 1423 if (result != 0) /* exit if not sent */ 1424 return -1; 1425 1426 while (0 == flag) /* wait for send to finish */ 1427 ; 1428 1429 return 0; 1430 1431 case ADB_HW_PMU: 1432 pm_set_date_time(time); 1433 return 0; 1434 1435 default: 1436 return -1; 1437 } 1438 } 1439 1440 1441 int 1442 adb_poweroff(void) 1443 { 1444 u_char output[ADB_MAX_MSG_LENGTH]; 1445 int result; 1446 1447 adb_polling = 1; 1448 1449 switch (adbHardware) { 1450 case ADB_HW_PMU: 1451 /* Clear the wake on AC loss event */ 1452 pmu_fileserver_mode(0); 1453 pm_adb_poweroff(); 1454 1455 for (;;); /* wait for power off */ 1456 1457 return 0; 1458 1459 case ADB_HW_CUDA: 1460 output[0] = 0x02; /* 2 byte message */ 1461 output[1] = 0x01; /* to pram/rtc/soft-power device */ 1462 output[2] = 0x0a; /* set poweroff */ 1463 result = send_adb_cuda((u_char *)output, NULL, 1464 NULL, NULL, (int)0); 1465 if (result != 0) /* exit if not sent */ 1466 return -1; 1467 1468 for (;;); /* wait for power off */ 1469 1470 return 0; 1471 1472 default: 1473 return -1; 1474 } 1475 } 1476 1477 void 1478 setsoftadb(void) 1479 { 1480 if (!timeout_initialized(&adb_softintr_timeout)) 1481 timeout_set(&adb_softintr_timeout, (void *)adb_soft_intr, NULL); 1482 timeout_add(&adb_softintr_timeout, 1); 1483 } 1484 1485 void 1486 adb_cuda_autopoll(void) 1487 { 1488 volatile int flag = 0; 1489 int result; 1490 u_char output[16]; 1491 1492 output[0] = 0x03; /* 3-byte message */ 1493 output[1] = 0x01; /* to pram/rtc/soft-power device */ 1494 output[2] = 0x01; /* cuda autopoll */ 1495 output[3] = 0x01; 1496 result = send_adb_cuda(output, output, adb_op_comprout, 1497 (void *)&flag, 0); 1498 if (result != 0) /* exit if not sent */ 1499 return; 1500 1501 while (flag == 0); /* wait for result */ 1502 } 1503 1504 void 1505 adb_cuda_fileserver_mode(void) 1506 { 1507 volatile int flag = 0; 1508 int result; 1509 u_char output[16]; 1510 1511 output[0] = 0x03; /* 3-byte message */ 1512 output[1] = 0x01; /* to pram/rtc device/soft-power device */ 1513 output[2] = 0x13; /* cuda file server mode */ 1514 output[3] = 0x01; /* True - Turn on after AC loss */ 1515 1516 result = send_adb_cuda(output, output, adb_op_comprout, 1517 (void *)&flag, 0); 1518 if (result != 0) 1519 return; 1520 1521 while (flag == 0); 1522 } 1523 1524 void 1525 adb_restart(void) 1526 { 1527 int result; 1528 u_char output[16]; 1529 1530 adb_polling = 1; 1531 1532 switch (adbHardware) { 1533 case ADB_HW_CUDA: 1534 output[0] = 0x02; /* 2 byte message */ 1535 output[1] = 0x01; /* to pram/rtc/soft-power device */ 1536 output[2] = 0x11; /* restart */ 1537 result = send_adb_cuda((u_char *)output, NULL, 1538 NULL, NULL, (int)0); 1539 if (result != 0) /* exit if not sent */ 1540 return; 1541 while (1); /* not return */ 1542 1543 case ADB_HW_PMU: 1544 pm_adb_restart(); 1545 while (1); /* not return */ 1546 } 1547 } 1548 1549 /* 1550 * Driver definition. 1551 */ 1552 1553 int adbmatch(struct device *, void *, void *); 1554 void adbattach(struct device *, struct device *, void *); 1555 1556 struct cfattach adb_ca = { 1557 sizeof(struct adb_softc), adbmatch, adbattach 1558 }; 1559 1560 int 1561 adbmatch(struct device *parent, void *cf, void *aux) 1562 { 1563 struct confargs *ca = aux; 1564 1565 if (ca->ca_nreg < 8) 1566 return 0; 1567 1568 if (ca->ca_nintr < 4) 1569 return 0; 1570 1571 if (strcmp(ca->ca_name, "via-cuda") == 0) 1572 return 1; 1573 1574 if (strcmp(ca->ca_name, "via-pmu") == 0) 1575 return 1; 1576 1577 return 0; 1578 } 1579 1580 void 1581 adbattach(struct device *parent, struct device *self, void *aux) 1582 { 1583 struct adb_softc *sc = (struct adb_softc *)self; 1584 struct confargs *ca = aux; 1585 struct confargs nca; 1586 char name[32]; 1587 int node; 1588 ADBDataBlock adbdata; 1589 struct adb_attach_args aa_args; 1590 int totaladbs; 1591 int adbindex, adbaddr; 1592 1593 ca->ca_reg[0] += ca->ca_baseaddr; 1594 1595 sc->sc_regbase = mapiodev(ca->ca_reg[0], ca->ca_reg[1]); 1596 Via1Base = sc->sc_regbase; 1597 1598 if (strcmp(ca->ca_name, "via-cuda") == 0) 1599 adbHardware = ADB_HW_CUDA; 1600 else if (strcmp(ca->ca_name, "via-pmu") == 0) { 1601 adbHardware = ADB_HW_PMU; 1602 1603 /* 1604 * Bus reset can take a long time if no adb devices are 1605 * connected, e.g. on a Mac Mini; so check for an adb 1606 * child in the OF tree to speed up pm_adb_op(). 1607 */ 1608 adbempty = 1; 1609 for (node = OF_child(ca->ca_node); node; node = OF_peer(node)) { 1610 if (OF_getprop(node, "name", name, sizeof name) <= 0) 1611 continue; 1612 if (strcmp(name, "adb") == 0) { 1613 adbempty = 0; 1614 break; 1615 } 1616 } 1617 } 1618 1619 adb_polling = 1; 1620 if (!adbempty) { 1621 adb_reinit(sc); 1622 totaladbs = count_adbs(sc); 1623 printf(": irq %d, %s, %d target%s", ca->ca_intr[0], ca->ca_name, 1624 totaladbs, (totaladbs == 1) ? "" : "s"); 1625 } 1626 printf("\n"); 1627 1628 mac_intr_establish(parent, ca->ca_intr[0], IST_LEVEL, IPL_TTY, 1629 adb_intr, sc, sc->sc_dev.dv_xname); 1630 1631 /* init powerpc globals which control RTC functionality */ 1632 time_read = adb_read_date_time; 1633 time_write = adb_set_date_time; 1634 1635 #if NAPM > 0 1636 if (adbHardware == ADB_HW_PMU) { 1637 /* Magic for signalling the apm driver to match. */ 1638 nca.ca_name = "apm"; 1639 nca.ca_node = node; 1640 config_found(self, &nca, NULL); 1641 } 1642 #endif 1643 1644 /* Attach I2C controller. */ 1645 for (node = OF_child(ca->ca_node); node; node = OF_peer(node)) { 1646 if (OF_getprop(node, "name", name, sizeof name) <= 0) 1647 continue; 1648 if (strcmp(name, "pmu-i2c") == 0) { 1649 nca.ca_name = "piic"; 1650 nca.ca_node = node; 1651 config_found(self, &nca, NULL); 1652 } 1653 } 1654 1655 if (adbHardware == ADB_HW_CUDA) 1656 adb_cuda_fileserver_mode(); 1657 if (adbHardware == ADB_HW_PMU) 1658 pmu_fileserver_mode(1); 1659 1660 /* 1661 * XXX If the machine doesn't have an ADB bus (PowerBook5,6+) 1662 * yes it sounds stupid to attach adb(4), but don't try to send 1663 * ADB commands otherwise the PMU may shutdown the machine... 1664 */ 1665 if (adbempty) 1666 return; 1667 1668 /* for each ADB device */ 1669 for (adbindex = 1; adbindex <= totaladbs; adbindex++) { 1670 /* Get the ADB information */ 1671 adbaddr = get_ind_adb_info(sc, &adbdata, adbindex); 1672 1673 aa_args.name = adb_device_name; 1674 aa_args.origaddr = adbdata.origADBAddr; 1675 aa_args.adbaddr = adbaddr; 1676 aa_args.handler_id = adbdata.devType; 1677 1678 (void)config_found(self, &aa_args, adbprint); 1679 } 1680 1681 if (adbHardware == ADB_HW_CUDA) 1682 adb_cuda_autopoll(); 1683 adb_polling = 0; 1684 } 1685