1 /* 2 * Device driver for the via-cuda on Apple Powermacs. 3 * 4 * The VIA (versatile interface adapter) interfaces to the CUDA, 5 * a 6805 microprocessor core which controls the ADB (Apple Desktop 6 * Bus) which connects to the keyboard and mouse. The CUDA also 7 * controls system power and the RTC (real time clock) chip. 8 * 9 * Copyright (C) 1996 Paul Mackerras. 10 */ 11 #include <stdarg.h> 12 #include <linux/types.h> 13 #include <linux/errno.h> 14 #include <linux/kernel.h> 15 #include <linux/delay.h> 16 #include <linux/adb.h> 17 #include <linux/cuda.h> 18 #include <linux/spinlock.h> 19 #include <linux/interrupt.h> 20 #ifdef CONFIG_PPC 21 #include <asm/prom.h> 22 #include <asm/machdep.h> 23 #else 24 #include <asm/macintosh.h> 25 #include <asm/macints.h> 26 #include <asm/mac_via.h> 27 #endif 28 #include <asm/io.h> 29 #include <linux/init.h> 30 31 static volatile unsigned char __iomem *via; 32 static DEFINE_SPINLOCK(cuda_lock); 33 34 /* VIA registers - spaced 0x200 bytes apart */ 35 #define RS 0x200 /* skip between registers */ 36 #define B 0 /* B-side data */ 37 #define A RS /* A-side data */ 38 #define DIRB (2*RS) /* B-side direction (1=output) */ 39 #define DIRA (3*RS) /* A-side direction (1=output) */ 40 #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */ 41 #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */ 42 #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */ 43 #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */ 44 #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */ 45 #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */ 46 #define SR (10*RS) /* Shift register */ 47 #define ACR (11*RS) /* Auxiliary control register */ 48 #define PCR (12*RS) /* Peripheral control register */ 49 #define IFR (13*RS) /* Interrupt flag register */ 50 #define IER (14*RS) /* Interrupt enable register */ 51 #define ANH (15*RS) /* A-side data, no handshake */ 52 53 /* Bits in B data register: all active low */ 54 #define TREQ 0x08 /* Transfer request (input) */ 55 #define TACK 0x10 /* Transfer acknowledge (output) */ 56 #define TIP 0x20 /* Transfer in progress (output) */ 57 58 /* Bits in ACR */ 59 #define SR_CTRL 0x1c /* Shift register control bits */ 60 #define SR_EXT 0x0c /* Shift on external clock */ 61 #define SR_OUT 0x10 /* Shift out if 1 */ 62 63 /* Bits in IFR and IER */ 64 #define IER_SET 0x80 /* set bits in IER */ 65 #define IER_CLR 0 /* clear bits in IER */ 66 #define SR_INT 0x04 /* Shift register full/empty */ 67 68 static inline bool TREQ_asserted(u8 portb) 69 { 70 return !(portb & TREQ); 71 } 72 73 static inline void assert_TIP(void) 74 { 75 out_8(&via[B], in_8(&via[B]) & ~TIP); 76 } 77 78 static inline void assert_TACK(void) 79 { 80 out_8(&via[B], in_8(&via[B]) & ~TACK); 81 } 82 83 static inline void toggle_TACK(void) 84 { 85 out_8(&via[B], in_8(&via[B]) ^ TACK); 86 } 87 88 static inline void negate_TACK(void) 89 { 90 out_8(&via[B], in_8(&via[B]) | TACK); 91 } 92 93 static inline void negate_TIP_and_TACK(void) 94 { 95 out_8(&via[B], in_8(&via[B]) | TIP | TACK); 96 } 97 98 static enum cuda_state { 99 idle, 100 sent_first_byte, 101 sending, 102 reading, 103 read_done, 104 awaiting_reply 105 } cuda_state; 106 107 static struct adb_request *current_req; 108 static struct adb_request *last_req; 109 static unsigned char cuda_rbuf[16]; 110 static unsigned char *reply_ptr; 111 static int reading_reply; 112 static int data_index; 113 static int cuda_irq; 114 #ifdef CONFIG_PPC 115 static struct device_node *vias; 116 #endif 117 static int cuda_fully_inited; 118 119 #ifdef CONFIG_ADB 120 static int cuda_probe(void); 121 static int cuda_send_request(struct adb_request *req, int sync); 122 static int cuda_adb_autopoll(int devs); 123 static int cuda_reset_adb_bus(void); 124 #endif /* CONFIG_ADB */ 125 126 static int cuda_init_via(void); 127 static void cuda_start(void); 128 static irqreturn_t cuda_interrupt(int irq, void *arg); 129 static void cuda_input(unsigned char *buf, int nb); 130 void cuda_poll(void); 131 static int cuda_write(struct adb_request *req); 132 133 int cuda_request(struct adb_request *req, 134 void (*done)(struct adb_request *), int nbytes, ...); 135 136 #ifdef CONFIG_ADB 137 struct adb_driver via_cuda_driver = { 138 .name = "CUDA", 139 .probe = cuda_probe, 140 .send_request = cuda_send_request, 141 .autopoll = cuda_adb_autopoll, 142 .poll = cuda_poll, 143 .reset_bus = cuda_reset_adb_bus, 144 }; 145 #endif /* CONFIG_ADB */ 146 147 #ifdef CONFIG_MAC 148 int __init find_via_cuda(void) 149 { 150 struct adb_request req; 151 int err; 152 153 if (macintosh_config->adb_type != MAC_ADB_CUDA) 154 return 0; 155 156 via = via1; 157 cuda_state = idle; 158 159 err = cuda_init_via(); 160 if (err) { 161 printk(KERN_ERR "cuda_init_via() failed\n"); 162 via = NULL; 163 return 0; 164 } 165 166 /* enable autopoll */ 167 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); 168 while (!req.complete) 169 cuda_poll(); 170 171 return 1; 172 } 173 #else 174 int __init find_via_cuda(void) 175 { 176 struct adb_request req; 177 phys_addr_t taddr; 178 const u32 *reg; 179 int err; 180 181 if (vias != 0) 182 return 1; 183 vias = of_find_node_by_name(NULL, "via-cuda"); 184 if (vias == 0) 185 return 0; 186 187 reg = of_get_property(vias, "reg", NULL); 188 if (reg == NULL) { 189 printk(KERN_ERR "via-cuda: No \"reg\" property !\n"); 190 goto fail; 191 } 192 taddr = of_translate_address(vias, reg); 193 if (taddr == 0) { 194 printk(KERN_ERR "via-cuda: Can't translate address !\n"); 195 goto fail; 196 } 197 via = ioremap(taddr, 0x2000); 198 if (via == NULL) { 199 printk(KERN_ERR "via-cuda: Can't map address !\n"); 200 goto fail; 201 } 202 203 cuda_state = idle; 204 sys_ctrler = SYS_CTRLER_CUDA; 205 206 err = cuda_init_via(); 207 if (err) { 208 printk(KERN_ERR "cuda_init_via() failed\n"); 209 via = NULL; 210 return 0; 211 } 212 213 /* Clear and enable interrupts, but only on PPC. On 68K it's done */ 214 /* for us by the main VIA driver in arch/m68k/mac/via.c */ 215 216 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */ 217 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */ 218 219 /* enable autopoll */ 220 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1); 221 while (!req.complete) 222 cuda_poll(); 223 224 return 1; 225 226 fail: 227 of_node_put(vias); 228 vias = NULL; 229 return 0; 230 } 231 #endif /* !defined CONFIG_MAC */ 232 233 static int __init via_cuda_start(void) 234 { 235 if (via == NULL) 236 return -ENODEV; 237 238 #ifdef CONFIG_MAC 239 cuda_irq = IRQ_MAC_ADB; 240 #else 241 cuda_irq = irq_of_parse_and_map(vias, 0); 242 if (!cuda_irq) { 243 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n", 244 vias->full_name); 245 return -ENODEV; 246 } 247 #endif 248 249 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) { 250 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq); 251 return -EAGAIN; 252 } 253 254 pr_info("Macintosh CUDA driver v0.5 for Unified ADB.\n"); 255 256 cuda_fully_inited = 1; 257 return 0; 258 } 259 260 device_initcall(via_cuda_start); 261 262 #ifdef CONFIG_ADB 263 static int 264 cuda_probe(void) 265 { 266 #ifdef CONFIG_PPC 267 if (sys_ctrler != SYS_CTRLER_CUDA) 268 return -ENODEV; 269 #else 270 if (macintosh_config->adb_type != MAC_ADB_CUDA) 271 return -ENODEV; 272 #endif 273 if (via == NULL) 274 return -ENODEV; 275 return 0; 276 } 277 #endif /* CONFIG_ADB */ 278 279 #define WAIT_FOR(cond, what) \ 280 do { \ 281 int x; \ 282 for (x = 1000; !(cond); --x) { \ 283 if (x == 0) { \ 284 pr_err("Timeout waiting for " what "\n"); \ 285 return -ENXIO; \ 286 } \ 287 udelay(100); \ 288 } \ 289 } while (0) 290 291 static int 292 __init cuda_init_via(void) 293 { 294 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */ 295 negate_TIP_and_TACK(); 296 out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */ 297 (void)in_8(&via[SR]); /* clear any left-over data */ 298 #ifdef CONFIG_PPC 299 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */ 300 (void)in_8(&via[IER]); 301 #else 302 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */ 303 #endif 304 305 /* delay 4ms and then clear any pending interrupt */ 306 mdelay(4); 307 (void)in_8(&via[SR]); 308 out_8(&via[IFR], SR_INT); 309 310 /* sync with the CUDA - assert TACK without TIP */ 311 assert_TACK(); 312 313 /* wait for the CUDA to assert TREQ in response */ 314 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync"); 315 316 /* wait for the interrupt and then clear it */ 317 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)"); 318 (void)in_8(&via[SR]); 319 out_8(&via[IFR], SR_INT); 320 321 /* finish the sync by negating TACK */ 322 negate_TACK(); 323 324 /* wait for the CUDA to negate TREQ and the corresponding interrupt */ 325 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)"); 326 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)"); 327 (void)in_8(&via[SR]); 328 out_8(&via[IFR], SR_INT); 329 330 return 0; 331 } 332 333 #ifdef CONFIG_ADB 334 /* Send an ADB command */ 335 static int 336 cuda_send_request(struct adb_request *req, int sync) 337 { 338 int i; 339 340 if ((via == NULL) || !cuda_fully_inited) { 341 req->complete = 1; 342 return -ENXIO; 343 } 344 345 req->reply_expected = 1; 346 347 i = cuda_write(req); 348 if (i) 349 return i; 350 351 if (sync) { 352 while (!req->complete) 353 cuda_poll(); 354 } 355 return 0; 356 } 357 358 359 /* Enable/disable autopolling */ 360 static int 361 cuda_adb_autopoll(int devs) 362 { 363 struct adb_request req; 364 365 if ((via == NULL) || !cuda_fully_inited) 366 return -ENXIO; 367 368 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0)); 369 while (!req.complete) 370 cuda_poll(); 371 return 0; 372 } 373 374 /* Reset adb bus - how do we do this?? */ 375 static int 376 cuda_reset_adb_bus(void) 377 { 378 struct adb_request req; 379 380 if ((via == NULL) || !cuda_fully_inited) 381 return -ENXIO; 382 383 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */ 384 while (!req.complete) 385 cuda_poll(); 386 return 0; 387 } 388 #endif /* CONFIG_ADB */ 389 390 /* Construct and send a cuda request */ 391 int 392 cuda_request(struct adb_request *req, void (*done)(struct adb_request *), 393 int nbytes, ...) 394 { 395 va_list list; 396 int i; 397 398 if (via == NULL) { 399 req->complete = 1; 400 return -ENXIO; 401 } 402 403 req->nbytes = nbytes; 404 req->done = done; 405 va_start(list, nbytes); 406 for (i = 0; i < nbytes; ++i) 407 req->data[i] = va_arg(list, int); 408 va_end(list); 409 req->reply_expected = 1; 410 return cuda_write(req); 411 } 412 EXPORT_SYMBOL(cuda_request); 413 414 static int 415 cuda_write(struct adb_request *req) 416 { 417 unsigned long flags; 418 419 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) { 420 req->complete = 1; 421 return -EINVAL; 422 } 423 req->next = NULL; 424 req->sent = 0; 425 req->complete = 0; 426 req->reply_len = 0; 427 428 spin_lock_irqsave(&cuda_lock, flags); 429 if (current_req != 0) { 430 last_req->next = req; 431 last_req = req; 432 } else { 433 current_req = req; 434 last_req = req; 435 if (cuda_state == idle) 436 cuda_start(); 437 } 438 spin_unlock_irqrestore(&cuda_lock, flags); 439 440 return 0; 441 } 442 443 static void 444 cuda_start(void) 445 { 446 /* assert cuda_state == idle */ 447 if (current_req == NULL) 448 return; 449 if (TREQ_asserted(in_8(&via[B]))) 450 return; /* a byte is coming in from the CUDA */ 451 452 /* set the shift register to shift out and send a byte */ 453 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT); 454 out_8(&via[SR], current_req->data[0]); 455 assert_TIP(); 456 cuda_state = sent_first_byte; 457 } 458 459 void 460 cuda_poll(void) 461 { 462 /* cuda_interrupt only takes a normal lock, we disable 463 * interrupts here to avoid re-entering and thus deadlocking. 464 */ 465 if (cuda_irq) 466 disable_irq(cuda_irq); 467 cuda_interrupt(0, NULL); 468 if (cuda_irq) 469 enable_irq(cuda_irq); 470 } 471 EXPORT_SYMBOL(cuda_poll); 472 473 #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a)) 474 475 static irqreturn_t 476 cuda_interrupt(int irq, void *arg) 477 { 478 u8 status; 479 struct adb_request *req = NULL; 480 unsigned char ibuf[16]; 481 int ibuf_len = 0; 482 int complete = 0; 483 484 spin_lock(&cuda_lock); 485 486 /* On powermacs, this handler is registered for the VIA IRQ. But they use 487 * just the shift register IRQ -- other VIA interrupt sources are disabled. 488 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless 489 * we are polling, the shift register IRQ flag has already been cleared. 490 */ 491 492 #ifdef CONFIG_MAC 493 if (!arg) 494 #endif 495 { 496 if ((in_8(&via[IFR]) & SR_INT) == 0) { 497 spin_unlock(&cuda_lock); 498 return IRQ_NONE; 499 } else { 500 out_8(&via[IFR], SR_INT); 501 } 502 } 503 504 status = in_8(&via[B]) & (TIP | TACK | TREQ); 505 506 switch (cuda_state) { 507 case idle: 508 /* CUDA has sent us the first byte of data - unsolicited */ 509 (void)in_8(&via[SR]); 510 assert_TIP(); 511 cuda_state = reading; 512 reply_ptr = cuda_rbuf; 513 reading_reply = 0; 514 break; 515 516 case awaiting_reply: 517 /* CUDA has sent us the first byte of data of a reply */ 518 (void)in_8(&via[SR]); 519 assert_TIP(); 520 cuda_state = reading; 521 reply_ptr = current_req->reply; 522 reading_reply = 1; 523 break; 524 525 case sent_first_byte: 526 if (TREQ_asserted(status)) { 527 /* collision */ 528 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); 529 (void)in_8(&via[SR]); 530 negate_TIP_and_TACK(); 531 cuda_state = idle; 532 } else { 533 out_8(&via[SR], current_req->data[1]); 534 toggle_TACK(); 535 data_index = 2; 536 cuda_state = sending; 537 } 538 break; 539 540 case sending: 541 req = current_req; 542 if (data_index >= req->nbytes) { 543 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT); 544 (void)in_8(&via[SR]); 545 negate_TIP_and_TACK(); 546 req->sent = 1; 547 if (req->reply_expected) { 548 cuda_state = awaiting_reply; 549 } else { 550 current_req = req->next; 551 complete = 1; 552 /* not sure about this */ 553 cuda_state = idle; 554 cuda_start(); 555 } 556 } else { 557 out_8(&via[SR], req->data[data_index++]); 558 toggle_TACK(); 559 } 560 break; 561 562 case reading: 563 if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr) 564 : ARRAY_FULL(cuda_rbuf, reply_ptr)) 565 (void)in_8(&via[SR]); 566 else 567 *reply_ptr++ = in_8(&via[SR]); 568 if (!TREQ_asserted(status)) { 569 /* that's all folks */ 570 negate_TIP_and_TACK(); 571 cuda_state = read_done; 572 } else { 573 toggle_TACK(); 574 } 575 break; 576 577 case read_done: 578 (void)in_8(&via[SR]); 579 if (reading_reply) { 580 req = current_req; 581 req->reply_len = reply_ptr - req->reply; 582 if (req->data[0] == ADB_PACKET) { 583 /* Have to adjust the reply from ADB commands */ 584 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) { 585 /* the 0x2 bit indicates no response */ 586 req->reply_len = 0; 587 } else { 588 /* leave just the command and result bytes in the reply */ 589 req->reply_len -= 2; 590 memmove(req->reply, req->reply + 2, req->reply_len); 591 } 592 } 593 current_req = req->next; 594 complete = 1; 595 } else { 596 /* This is tricky. We must break the spinlock to call 597 * cuda_input. However, doing so means we might get 598 * re-entered from another CPU getting an interrupt 599 * or calling cuda_poll(). I ended up using the stack 600 * (it's only for 16 bytes) and moving the actual 601 * call to cuda_input to outside of the lock. 602 */ 603 ibuf_len = reply_ptr - cuda_rbuf; 604 memcpy(ibuf, cuda_rbuf, ibuf_len); 605 } 606 if (TREQ_asserted(status)) { 607 assert_TIP(); 608 cuda_state = reading; 609 reply_ptr = cuda_rbuf; 610 reading_reply = 0; 611 } else { 612 cuda_state = idle; 613 cuda_start(); 614 } 615 break; 616 617 default: 618 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state); 619 } 620 spin_unlock(&cuda_lock); 621 if (complete && req) { 622 void (*done)(struct adb_request *) = req->done; 623 mb(); 624 req->complete = 1; 625 /* Here, we assume that if the request has a done member, the 626 * struct request will survive to setting req->complete to 1 627 */ 628 if (done) 629 (*done)(req); 630 } 631 if (ibuf_len) 632 cuda_input(ibuf, ibuf_len); 633 return IRQ_HANDLED; 634 } 635 636 static void 637 cuda_input(unsigned char *buf, int nb) 638 { 639 switch (buf[0]) { 640 case ADB_PACKET: 641 #ifdef CONFIG_XMON 642 if (nb == 5 && buf[2] == 0x2c) { 643 extern int xmon_wants_key, xmon_adb_keycode; 644 if (xmon_wants_key) { 645 xmon_adb_keycode = buf[3]; 646 return; 647 } 648 } 649 #endif /* CONFIG_XMON */ 650 #ifdef CONFIG_ADB 651 adb_input(buf+2, nb-2, buf[1] & 0x40); 652 #endif /* CONFIG_ADB */ 653 break; 654 655 default: 656 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1, 657 buf, nb, false); 658 } 659 } 660