via-cuda.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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/config.h>
  13. #include <linux/types.h>
  14. #include <linux/errno.h>
  15. #include <linux/kernel.h>
  16. #include <linux/delay.h>
  17. #include <linux/sched.h>
  18. #include <linux/adb.h>
  19. #include <linux/cuda.h>
  20. #include <linux/spinlock.h>
  21. #ifdef CONFIG_PPC
  22. #include <asm/prom.h>
  23. #include <asm/machdep.h>
  24. #else
  25. #include <asm/macintosh.h>
  26. #include <asm/macints.h>
  27. #include <asm/machw.h>
  28. #include <asm/mac_via.h>
  29. #endif
  30. #include <asm/io.h>
  31. #include <asm/system.h>
  32. #include <linux/init.h>
  33. static volatile unsigned char *via;
  34. static spinlock_t cuda_lock = SPIN_LOCK_UNLOCKED;
  35. #ifdef CONFIG_MAC
  36. #define CUDA_IRQ IRQ_MAC_ADB
  37. #define __openfirmware
  38. #define eieio()
  39. #else
  40. #define CUDA_IRQ vias->intrs[0].line
  41. #endif
  42. /* VIA registers - spaced 0x200 bytes apart */
  43. #define RS 0x200 /* skip between registers */
  44. #define B 0 /* B-side data */
  45. #define A RS /* A-side data */
  46. #define DIRB (2*RS) /* B-side direction (1=output) */
  47. #define DIRA (3*RS) /* A-side direction (1=output) */
  48. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  49. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  50. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  51. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  52. #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
  53. #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
  54. #define SR (10*RS) /* Shift register */
  55. #define ACR (11*RS) /* Auxiliary control register */
  56. #define PCR (12*RS) /* Peripheral control register */
  57. #define IFR (13*RS) /* Interrupt flag register */
  58. #define IER (14*RS) /* Interrupt enable register */
  59. #define ANH (15*RS) /* A-side data, no handshake */
  60. /* Bits in B data register: all active low */
  61. #define TREQ 0x08 /* Transfer request (input) */
  62. #define TACK 0x10 /* Transfer acknowledge (output) */
  63. #define TIP 0x20 /* Transfer in progress (output) */
  64. /* Bits in ACR */
  65. #define SR_CTRL 0x1c /* Shift register control bits */
  66. #define SR_EXT 0x0c /* Shift on external clock */
  67. #define SR_OUT 0x10 /* Shift out if 1 */
  68. /* Bits in IFR and IER */
  69. #define IER_SET 0x80 /* set bits in IER */
  70. #define IER_CLR 0 /* clear bits in IER */
  71. #define SR_INT 0x04 /* Shift register full/empty */
  72. static enum cuda_state {
  73.     idle,
  74.     sent_first_byte,
  75.     sending,
  76.     reading,
  77.     read_done,
  78.     awaiting_reply
  79. } cuda_state;
  80. static struct adb_request *current_req;
  81. static struct adb_request *last_req;
  82. static unsigned char cuda_rbuf[16];
  83. static unsigned char *reply_ptr;
  84. static int reading_reply;
  85. static int data_index;
  86. #ifdef CONFIG_PPC
  87. static struct device_node *vias;
  88. #endif
  89. static int cuda_fully_inited = 0;
  90. #ifdef CONFIG_ADB
  91. static int cuda_probe(void);
  92. static int cuda_init(void);
  93. static int cuda_send_request(struct adb_request *req, int sync);
  94. static int cuda_adb_autopoll(int devs);
  95. static int cuda_reset_adb_bus(void);
  96. #endif /* CONFIG_ADB */
  97. static int cuda_init_via(void);
  98. static void cuda_start(void);
  99. static void cuda_interrupt(int irq, void *arg, struct pt_regs *regs);
  100. static void cuda_input(unsigned char *buf, int nb, struct pt_regs *regs);
  101. void cuda_poll(void);
  102. static int cuda_write(struct adb_request *req);
  103. int cuda_request(struct adb_request *req,
  104.  void (*done)(struct adb_request *), int nbytes, ...);
  105. #ifdef CONFIG_ADB
  106. struct adb_driver via_cuda_driver = {
  107. "CUDA",
  108. cuda_probe,
  109. cuda_init,
  110. cuda_send_request,
  111. cuda_adb_autopoll,
  112. cuda_poll,
  113. cuda_reset_adb_bus
  114. };
  115. #endif /* CONFIG_ADB */
  116. #ifdef CONFIG_PPC
  117. int __init
  118. find_via_cuda(void)
  119. {
  120.     int err;
  121.     struct adb_request req;
  122.     if (vias != 0)
  123. return 1;
  124.     vias = find_devices("via-cuda");
  125.     if (vias == 0)
  126. return 0;
  127.     if (vias->next != 0)
  128. printk(KERN_WARNING "Warning: only using 1st via-cudan");
  129. #if 0
  130.     { int i;
  131.     printk("find_via_cuda: node = %p, addrs =", vias->node);
  132.     for (i = 0; i < vias->n_addrs; ++i)
  133. printk(" %x(%x)", vias->addrs[i].address, vias->addrs[i].size);
  134.     printk(", intrs =");
  135.     for (i = 0; i < vias->n_intrs; ++i)
  136. printk(" %x", vias->intrs[i].line);
  137.     printk("n"); }
  138. #endif
  139.     if (vias->n_addrs != 1 || vias->n_intrs != 1) {
  140. printk(KERN_ERR "via-cuda: expecting 1 address (%d) and 1 interrupt (%d)n",
  141.        vias->n_addrs, vias->n_intrs);
  142. if (vias->n_addrs < 1 || vias->n_intrs < 1)
  143.     return 0;
  144.     }
  145.     via = (volatile unsigned char *) ioremap(vias->addrs->address, 0x2000);
  146.     cuda_state = idle;
  147.     sys_ctrler = SYS_CTRLER_CUDA;
  148.     err = cuda_init_via();
  149.     if (err) {
  150. printk(KERN_ERR "cuda_init_via() failedn");
  151. via = NULL;
  152. return 0;
  153.     }
  154.     /* Clear and enable interrupts, but only on PPC. On 68K it's done  */
  155.     /* for us by the main VIA driver in arch/m68k/mac/via.c        */
  156. #ifndef CONFIG_MAC
  157.     via[IFR] = 0x7f; eieio(); /* clear interrupts by writing 1s */
  158.     via[IER] = IER_SET|SR_INT; eieio(); /* enable interrupt from SR */
  159. #endif
  160.     /* enable autopoll */
  161.     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
  162.     while (!req.complete)
  163. cuda_poll();
  164.     return 1;
  165. }
  166. #endif /* CONFIG_PPC */
  167. int __init
  168. via_cuda_start(void)
  169. {
  170.     if (via == NULL)
  171. return -ENODEV;
  172. #ifdef CONFIG_PPC
  173.     request_OF_resource(vias, 0, NULL);
  174. #endif
  175.     if (request_irq(CUDA_IRQ, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
  176. printk(KERN_ERR "cuda_init: can't get irq %dn", CUDA_IRQ);
  177. return -EAGAIN;
  178.     }
  179.     printk("Macintosh CUDA driver v0.5 for Unified ADB.n");
  180.     cuda_fully_inited = 1;
  181.     return 0;
  182. }
  183. #ifdef CONFIG_ADB
  184. static int
  185. cuda_probe()
  186. {
  187. #ifdef CONFIG_PPC
  188.     if (sys_ctrler != SYS_CTRLER_CUDA)
  189. return -ENODEV;
  190. #else
  191.     if (macintosh_config->adb_type != MAC_ADB_CUDA)
  192. return -ENODEV;
  193.     via = via1;
  194. #endif
  195.     return 0;
  196. }
  197. static int __init
  198. cuda_init(void)
  199. {
  200. #ifdef CONFIG_PPC
  201.     if (via == NULL)
  202. return -ENODEV;
  203.     return 0;
  204. #else 
  205.     int err = cuda_init_via();
  206.     if (err) {
  207. printk(KERN_ERR "cuda_init_via() failedn");
  208. return -ENODEV;
  209.     }
  210.     return via_cuda_start();
  211. #endif
  212. }
  213. #endif /* CONFIG_ADB */
  214. #define WAIT_FOR(cond, what)
  215.     do {
  216. for (x = 1000; !(cond); --x) {
  217.     if (x == 0) {
  218. printk("Timeout waiting for " what "n");
  219. return -ENXIO;
  220.     }
  221.     udelay(100);
  222. }
  223.     } while (0)
  224. static int
  225. cuda_init_via()
  226. {
  227.     int x;
  228.     via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ; /* TACK & TIP out */
  229.     via[B] |= TACK | TIP; /* negate them */
  230.     via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT; /* SR data in */
  231.     eieio();
  232.     x = via[SR]; eieio(); /* clear any left-over data */
  233. #ifndef CONFIG_MAC
  234.     via[IER] = 0x7f; eieio(); /* disable interrupts from VIA */
  235. #endif
  236.     eieio();
  237.     /* delay 4ms and then clear any pending interrupt */
  238.     mdelay(4);
  239.     x = via[SR]; eieio();
  240.     /* sync with the CUDA - assert TACK without TIP */
  241.     via[B] &= ~TACK; eieio();
  242.     /* wait for the CUDA to assert TREQ in response */
  243.     WAIT_FOR((via[B] & TREQ) == 0, "CUDA response to sync");
  244.     /* wait for the interrupt and then clear it */
  245.     WAIT_FOR(via[IFR] & SR_INT, "CUDA response to sync (2)");
  246.     x = via[SR]; eieio();
  247.     /* finish the sync by negating TACK */
  248.     via[B] |= TACK; eieio();
  249.     /* wait for the CUDA to negate TREQ and the corresponding interrupt */
  250.     WAIT_FOR(via[B] & TREQ, "CUDA response to sync (3)");
  251.     WAIT_FOR(via[IFR] & SR_INT, "CUDA response to sync (4)");
  252.     x = via[SR]; eieio();
  253.     via[B] |= TIP; eieio(); /* should be unnecessary */
  254.     return 0;
  255. }
  256. #ifdef CONFIG_ADB
  257. /* Send an ADB command */
  258. static int
  259. cuda_send_request(struct adb_request *req, int sync)
  260. {
  261.     int i;
  262.     if ((via == NULL) || !cuda_fully_inited) {
  263. req->complete = 1;
  264. return -ENXIO;
  265.     }
  266.   
  267.     req->reply_expected = 1;
  268.     i = cuda_write(req);
  269.     if (i)
  270. return i;
  271.     if (sync) {
  272. while (!req->complete)
  273.     cuda_poll();
  274.     }
  275.     return 0;
  276. }
  277. /* Enable/disable autopolling */
  278. static int
  279. cuda_adb_autopoll(int devs)
  280. {
  281.     struct adb_request req;
  282.     if ((via == NULL) || !cuda_fully_inited)
  283. return -ENXIO;
  284.     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
  285.     while (!req.complete)
  286. cuda_poll();
  287.     return 0;
  288. }
  289. /* Reset adb bus - how do we do this?? */
  290. static int
  291. cuda_reset_adb_bus(void)
  292. {
  293.     struct adb_request req;
  294.     if ((via == NULL) || !cuda_fully_inited)
  295. return -ENXIO;
  296.     cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
  297.     while (!req.complete)
  298. cuda_poll();
  299.     return 0;
  300. }
  301. #endif /* CONFIG_ADB */
  302. /* Construct and send a cuda request */
  303. int
  304. cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
  305.      int nbytes, ...)
  306. {
  307.     va_list list;
  308.     int i;
  309.     if (via == NULL) {
  310. req->complete = 1;
  311. return -ENXIO;
  312.     }
  313.     req->nbytes = nbytes;
  314.     req->done = done;
  315.     va_start(list, nbytes);
  316.     for (i = 0; i < nbytes; ++i)
  317. req->data[i] = va_arg(list, int);
  318.     va_end(list);
  319.     req->reply_expected = 1;
  320.     return cuda_write(req);
  321. }
  322. static int
  323. cuda_write(struct adb_request *req)
  324. {
  325.     unsigned long flags;
  326.     if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  327. req->complete = 1;
  328. return -EINVAL;
  329.     }
  330.     req->next = 0;
  331.     req->sent = 0;
  332.     req->complete = 0;
  333.     req->reply_len = 0;
  334.     spin_lock_irqsave(&cuda_lock, flags);
  335.     if (current_req != 0) {
  336. last_req->next = req;
  337. last_req = req;
  338.     } else {
  339. current_req = req;
  340. last_req = req;
  341. if (cuda_state == idle)
  342.     cuda_start();
  343.     }
  344.     spin_unlock_irqrestore(&cuda_lock, flags);
  345.     return 0;
  346. }
  347. static void
  348. cuda_start()
  349. {
  350.     struct adb_request *req;
  351.     /* assert cuda_state == idle */
  352.     /* get the packet to send */
  353.     req = current_req;
  354.     if (req == 0)
  355. return;
  356.     if ((via[B] & TREQ) == 0)
  357. return; /* a byte is coming in from the CUDA */
  358.     /* set the shift register to shift out and send a byte */
  359.     via[ACR] |= SR_OUT; eieio();
  360.     via[SR] = req->data[0]; eieio();
  361.     via[B] &= ~TIP;
  362.     cuda_state = sent_first_byte;
  363. }
  364. void
  365. cuda_poll()
  366. {
  367.     if (via[IFR] & SR_INT) {
  368. unsigned long flags;
  369. /* cuda_interrupt only takes a normal lock, we disable
  370.  * interrupts here to avoid re-entering and thus deadlocking.
  371.  * An option would be to disable only the IRQ source with
  372.  * disable_irq(), would that work on m68k ? --BenH
  373.  */
  374. local_irq_save(flags);
  375. cuda_interrupt(0, 0, 0);
  376. local_irq_restore(flags);
  377.     }
  378. }
  379. static void
  380. cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
  381. {
  382.     int x, status;
  383.     struct adb_request *req = NULL;
  384.     unsigned char ibuf[16];
  385.     int ibuf_len = 0;
  386.     int complete = 0;
  387.     
  388.     if ((via[IFR] & SR_INT) == 0)
  389. return;
  390.     spin_lock(&cuda_lock);
  391.     status = (~via[B] & (TIP|TREQ)) | (via[ACR] & SR_OUT); eieio();
  392.     /* printk("cuda_interrupt: state=%d status=%xn", cuda_state, status); */
  393.     switch (cuda_state) {
  394.     case idle:
  395. /* CUDA has sent us the first byte of data - unsolicited */
  396. if (status != TREQ)
  397.     printk("cuda: state=idle, status=%xn", status);
  398. x = via[SR]; eieio();
  399. via[B] &= ~TIP; eieio();
  400. cuda_state = reading;
  401. reply_ptr = cuda_rbuf;
  402. reading_reply = 0;
  403. break;
  404.     case awaiting_reply:
  405. /* CUDA has sent us the first byte of data of a reply */
  406. if (status != TREQ)
  407.     printk("cuda: state=awaiting_reply, status=%xn", status);
  408. x = via[SR]; eieio();
  409. via[B] &= ~TIP; eieio();
  410. cuda_state = reading;
  411. reply_ptr = current_req->reply;
  412. reading_reply = 1;
  413. break;
  414.     case sent_first_byte:
  415. if (status == TREQ + TIP + SR_OUT) {
  416.     /* collision */
  417.     via[ACR] &= ~SR_OUT; eieio();
  418.     x = via[SR]; eieio();
  419.     via[B] |= TIP | TACK; eieio();
  420.     cuda_state = idle;
  421. } else {
  422.     /* assert status == TIP + SR_OUT */
  423.     if (status != TIP + SR_OUT)
  424. printk("cuda: state=sent_first_byte status=%xn", status);
  425.     via[SR] = current_req->data[1]; eieio();
  426.     via[B] ^= TACK; eieio();
  427.     data_index = 2;
  428.     cuda_state = sending;
  429. }
  430. break;
  431.     case sending:
  432. req = current_req;
  433. if (data_index >= req->nbytes) {
  434.     via[ACR] &= ~SR_OUT; eieio();
  435.     x = via[SR]; eieio();
  436.     via[B] |= TACK | TIP; eieio();
  437.     req->sent = 1;
  438.     if (req->reply_expected) {
  439. cuda_state = awaiting_reply;
  440.     } else {
  441. current_req = req->next;
  442. complete = 1;
  443. /* not sure about this */
  444. cuda_state = idle;
  445. cuda_start();
  446.     }
  447. } else {
  448.     via[SR] = req->data[data_index++]; eieio();
  449.     via[B] ^= TACK; eieio();
  450. }
  451. break;
  452.     case reading:
  453. *reply_ptr++ = via[SR]; eieio();
  454. if (status == TIP) {
  455.     /* that's all folks */
  456.     via[B] |= TACK | TIP; eieio();
  457.     cuda_state = read_done;
  458. } else {
  459.     /* assert status == TIP | TREQ */
  460.     if (status != TIP + TREQ)
  461. printk("cuda: state=reading status=%xn", status);
  462.     via[B] ^= TACK; eieio();
  463. }
  464. break;
  465.     case read_done:
  466. x = via[SR]; eieio();
  467. if (reading_reply) {
  468.     req = current_req;
  469.     req->reply_len = reply_ptr - req->reply;
  470.     if (req->data[0] == ADB_PACKET) {
  471. /* Have to adjust the reply from ADB commands */
  472. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  473.     /* the 0x2 bit indicates no response */
  474.     req->reply_len = 0;
  475. } else {
  476.     /* leave just the command and result bytes in the reply */
  477.     req->reply_len -= 2;
  478.     memmove(req->reply, req->reply + 2, req->reply_len);
  479. }
  480.     }
  481.     req->complete = 1;
  482.     current_req = req->next;
  483.     complete = 1;
  484. } else {
  485.     /* This is tricky. We must break the spinlock to call
  486.      * cuda_input. However, doing so means we might get
  487.      * re-entered from another CPU getting an interrupt
  488.      * or calling cuda_poll(). I ended up using the stack
  489.      * (it's only for 16 bytes) and moving the actual
  490.      * call to cuda_input to outside of the lock.
  491.      */
  492.     ibuf_len = reply_ptr - cuda_rbuf;
  493.     memcpy(ibuf, cuda_rbuf, ibuf_len);
  494. }
  495. if (status == TREQ) {
  496.     via[B] &= ~TIP; eieio();
  497.     cuda_state = reading;
  498.     reply_ptr = cuda_rbuf;
  499.     reading_reply = 0;
  500. } else {
  501.     cuda_state = idle;
  502.     cuda_start();
  503. }
  504. break;
  505.     default:
  506. printk("cuda_interrupt: unknown cuda_state %d?n", cuda_state);
  507.     }
  508.     spin_unlock(&cuda_lock);
  509.     if (complete && req && req->done)
  510. (*req->done)(req);
  511.     if (ibuf_len)
  512. cuda_input(ibuf, ibuf_len, regs);
  513. }
  514. static void
  515. cuda_input(unsigned char *buf, int nb, struct pt_regs *regs)
  516. {
  517.     int i;
  518.     switch (buf[0]) {
  519.     case ADB_PACKET:
  520. #ifdef CONFIG_XMON
  521. if (nb == 5 && buf[2] == 0x2c) {
  522.     extern int xmon_wants_key, xmon_adb_keycode;
  523.     if (xmon_wants_key) {
  524. xmon_adb_keycode = buf[3];
  525. return;
  526.     }
  527. }
  528. #endif /* CONFIG_XMON */
  529. #ifdef CONFIG_ADB
  530. adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
  531. #endif /* CONFIG_ADB */
  532. break;
  533.     default:
  534. printk("data from cuda (%d bytes):", nb);
  535. for (i = 0; i < nb; ++i)
  536.     printk(" %.2x", buf[i]);
  537. printk("n");
  538.     }
  539. }