via-cuda.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  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. #include <linux/interrupt.h>
  22. #ifdef CONFIG_PPC
  23. #include <asm/prom.h>
  24. #include <asm/machdep.h>
  25. #else
  26. #include <asm/macintosh.h>
  27. #include <asm/macints.h>
  28. #include <asm/machw.h>
  29. #include <asm/mac_via.h>
  30. #endif
  31. #include <asm/io.h>
  32. #include <asm/system.h>
  33. #include <linux/init.h>
  34. static volatile unsigned char __iomem *via;
  35. static DEFINE_SPINLOCK(cuda_lock);
  36. #ifdef CONFIG_MAC
  37. #define CUDA_IRQ IRQ_MAC_ADB
  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 irqreturn_t 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-cuda\n");
  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 = 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() failed\n");
  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. out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
  158. out_8(&via[IER], IER_SET|SR_INT); /* 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. static int __init via_cuda_start(void)
  168. {
  169. if (via == NULL)
  170. return -ENODEV;
  171. #ifdef CONFIG_PPC
  172. request_OF_resource(vias, 0, NULL);
  173. #endif
  174. if (request_irq(CUDA_IRQ, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
  175. printk(KERN_ERR "cuda_init: can't get irq %d\n", CUDA_IRQ);
  176. return -EAGAIN;
  177. }
  178. printk("Macintosh CUDA driver v0.5 for Unified ADB.\n");
  179. cuda_fully_inited = 1;
  180. return 0;
  181. }
  182. device_initcall(via_cuda_start);
  183. #ifdef CONFIG_ADB
  184. static int
  185. cuda_probe(void)
  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() failed\n");
  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. int x; \
  217. for (x = 1000; !(cond); --x) { \
  218. if (x == 0) { \
  219. printk("Timeout waiting for " what "\n"); \
  220. return -ENXIO; \
  221. } \
  222. udelay(100); \
  223. } \
  224. } while (0)
  225. static int
  226. cuda_init_via(void)
  227. {
  228. out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
  229. out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */
  230. out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
  231. (void)in_8(&via[SR]); /* clear any left-over data */
  232. #ifndef CONFIG_MAC
  233. out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
  234. (void)in_8(&via[IER]);
  235. #endif
  236. /* delay 4ms and then clear any pending interrupt */
  237. mdelay(4);
  238. (void)in_8(&via[SR]);
  239. out_8(&via[IFR], in_8(&via[IFR]) & 0x7f);
  240. /* sync with the CUDA - assert TACK without TIP */
  241. out_8(&via[B], in_8(&via[B]) & ~TACK);
  242. /* wait for the CUDA to assert TREQ in response */
  243. WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync");
  244. /* wait for the interrupt and then clear it */
  245. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
  246. (void)in_8(&via[SR]);
  247. out_8(&via[IFR], in_8(&via[IFR]) & 0x7f);
  248. /* finish the sync by negating TACK */
  249. out_8(&via[B], in_8(&via[B]) | TACK);
  250. /* wait for the CUDA to negate TREQ and the corresponding interrupt */
  251. WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)");
  252. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
  253. (void)in_8(&via[SR]);
  254. out_8(&via[IFR], in_8(&via[IFR]) & 0x7f);
  255. out_8(&via[B], in_8(&via[B]) | TIP); /* should be unnecessary */
  256. return 0;
  257. }
  258. #ifdef CONFIG_ADB
  259. /* Send an ADB command */
  260. static int
  261. cuda_send_request(struct adb_request *req, int sync)
  262. {
  263. int i;
  264. if ((via == NULL) || !cuda_fully_inited) {
  265. req->complete = 1;
  266. return -ENXIO;
  267. }
  268. req->reply_expected = 1;
  269. i = cuda_write(req);
  270. if (i)
  271. return i;
  272. if (sync) {
  273. while (!req->complete)
  274. cuda_poll();
  275. }
  276. return 0;
  277. }
  278. /* Enable/disable autopolling */
  279. static int
  280. cuda_adb_autopoll(int devs)
  281. {
  282. struct adb_request req;
  283. if ((via == NULL) || !cuda_fully_inited)
  284. return -ENXIO;
  285. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
  286. while (!req.complete)
  287. cuda_poll();
  288. return 0;
  289. }
  290. /* Reset adb bus - how do we do this?? */
  291. static int
  292. cuda_reset_adb_bus(void)
  293. {
  294. struct adb_request req;
  295. if ((via == NULL) || !cuda_fully_inited)
  296. return -ENXIO;
  297. cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
  298. while (!req.complete)
  299. cuda_poll();
  300. return 0;
  301. }
  302. #endif /* CONFIG_ADB */
  303. /* Construct and send a cuda request */
  304. int
  305. cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
  306. int nbytes, ...)
  307. {
  308. va_list list;
  309. int i;
  310. if (via == NULL) {
  311. req->complete = 1;
  312. return -ENXIO;
  313. }
  314. req->nbytes = nbytes;
  315. req->done = done;
  316. va_start(list, nbytes);
  317. for (i = 0; i < nbytes; ++i)
  318. req->data[i] = va_arg(list, int);
  319. va_end(list);
  320. req->reply_expected = 1;
  321. return cuda_write(req);
  322. }
  323. static int
  324. cuda_write(struct adb_request *req)
  325. {
  326. unsigned long flags;
  327. if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  328. req->complete = 1;
  329. return -EINVAL;
  330. }
  331. req->next = NULL;
  332. req->sent = 0;
  333. req->complete = 0;
  334. req->reply_len = 0;
  335. spin_lock_irqsave(&cuda_lock, flags);
  336. if (current_req != 0) {
  337. last_req->next = req;
  338. last_req = req;
  339. } else {
  340. current_req = req;
  341. last_req = req;
  342. if (cuda_state == idle)
  343. cuda_start();
  344. }
  345. spin_unlock_irqrestore(&cuda_lock, flags);
  346. return 0;
  347. }
  348. static void
  349. cuda_start(void)
  350. {
  351. struct adb_request *req;
  352. /* assert cuda_state == idle */
  353. /* get the packet to send */
  354. req = current_req;
  355. if (req == 0)
  356. return;
  357. if ((in_8(&via[B]) & TREQ) == 0)
  358. return; /* a byte is coming in from the CUDA */
  359. /* set the shift register to shift out and send a byte */
  360. out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
  361. out_8(&via[SR], req->data[0]);
  362. out_8(&via[B], in_8(&via[B]) & ~TIP);
  363. cuda_state = sent_first_byte;
  364. }
  365. void
  366. cuda_poll(void)
  367. {
  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, NULL, NULL);
  376. local_irq_restore(flags);
  377. }
  378. static irqreturn_t
  379. cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
  380. {
  381. int status;
  382. struct adb_request *req = NULL;
  383. unsigned char ibuf[16];
  384. int ibuf_len = 0;
  385. int complete = 0;
  386. unsigned char virq;
  387. spin_lock(&cuda_lock);
  388. virq = in_8(&via[IFR]) & 0x7f;
  389. out_8(&via[IFR], virq);
  390. if ((virq & SR_INT) == 0) {
  391. spin_unlock(&cuda_lock);
  392. return IRQ_NONE;
  393. }
  394. status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
  395. /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
  396. switch (cuda_state) {
  397. case idle:
  398. /* CUDA has sent us the first byte of data - unsolicited */
  399. if (status != TREQ)
  400. printk("cuda: state=idle, status=%x\n", status);
  401. (void)in_8(&via[SR]);
  402. out_8(&via[B], in_8(&via[B]) & ~TIP);
  403. cuda_state = reading;
  404. reply_ptr = cuda_rbuf;
  405. reading_reply = 0;
  406. break;
  407. case awaiting_reply:
  408. /* CUDA has sent us the first byte of data of a reply */
  409. if (status != TREQ)
  410. printk("cuda: state=awaiting_reply, status=%x\n", status);
  411. (void)in_8(&via[SR]);
  412. out_8(&via[B], in_8(&via[B]) & ~TIP);
  413. cuda_state = reading;
  414. reply_ptr = current_req->reply;
  415. reading_reply = 1;
  416. break;
  417. case sent_first_byte:
  418. if (status == TREQ + TIP + SR_OUT) {
  419. /* collision */
  420. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  421. (void)in_8(&via[SR]);
  422. out_8(&via[B], in_8(&via[B]) | TIP | TACK);
  423. cuda_state = idle;
  424. } else {
  425. /* assert status == TIP + SR_OUT */
  426. if (status != TIP + SR_OUT)
  427. printk("cuda: state=sent_first_byte status=%x\n", status);
  428. out_8(&via[SR], current_req->data[1]);
  429. out_8(&via[B], in_8(&via[B]) ^ TACK);
  430. data_index = 2;
  431. cuda_state = sending;
  432. }
  433. break;
  434. case sending:
  435. req = current_req;
  436. if (data_index >= req->nbytes) {
  437. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  438. (void)in_8(&via[SR]);
  439. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  440. req->sent = 1;
  441. if (req->reply_expected) {
  442. cuda_state = awaiting_reply;
  443. } else {
  444. current_req = req->next;
  445. complete = 1;
  446. /* not sure about this */
  447. cuda_state = idle;
  448. cuda_start();
  449. }
  450. } else {
  451. out_8(&via[SR], req->data[data_index++]);
  452. out_8(&via[B], in_8(&via[B]) ^ TACK);
  453. }
  454. break;
  455. case reading:
  456. *reply_ptr++ = in_8(&via[SR]);
  457. if (status == TIP) {
  458. /* that's all folks */
  459. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  460. cuda_state = read_done;
  461. } else {
  462. /* assert status == TIP | TREQ */
  463. if (status != TIP + TREQ)
  464. printk("cuda: state=reading status=%x\n", status);
  465. out_8(&via[B], in_8(&via[B]) ^ TACK);
  466. }
  467. break;
  468. case read_done:
  469. (void)in_8(&via[SR]);
  470. if (reading_reply) {
  471. req = current_req;
  472. req->reply_len = reply_ptr - req->reply;
  473. if (req->data[0] == ADB_PACKET) {
  474. /* Have to adjust the reply from ADB commands */
  475. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  476. /* the 0x2 bit indicates no response */
  477. req->reply_len = 0;
  478. } else {
  479. /* leave just the command and result bytes in the reply */
  480. req->reply_len -= 2;
  481. memmove(req->reply, req->reply + 2, req->reply_len);
  482. }
  483. }
  484. current_req = req->next;
  485. complete = 1;
  486. } else {
  487. /* This is tricky. We must break the spinlock to call
  488. * cuda_input. However, doing so means we might get
  489. * re-entered from another CPU getting an interrupt
  490. * or calling cuda_poll(). I ended up using the stack
  491. * (it's only for 16 bytes) and moving the actual
  492. * call to cuda_input to outside of the lock.
  493. */
  494. ibuf_len = reply_ptr - cuda_rbuf;
  495. memcpy(ibuf, cuda_rbuf, ibuf_len);
  496. }
  497. if (status == TREQ) {
  498. out_8(&via[B], in_8(&via[B]) & ~TIP);
  499. cuda_state = reading;
  500. reply_ptr = cuda_rbuf;
  501. reading_reply = 0;
  502. } else {
  503. cuda_state = idle;
  504. cuda_start();
  505. }
  506. break;
  507. default:
  508. printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
  509. }
  510. spin_unlock(&cuda_lock);
  511. if (complete && req) {
  512. void (*done)(struct adb_request *) = req->done;
  513. mb();
  514. req->complete = 1;
  515. /* Here, we assume that if the request has a done member, the
  516. * struct request will survive to setting req->complete to 1
  517. */
  518. if (done)
  519. (*done)(req);
  520. }
  521. if (ibuf_len)
  522. cuda_input(ibuf, ibuf_len, regs);
  523. return IRQ_HANDLED;
  524. }
  525. static void
  526. cuda_input(unsigned char *buf, int nb, struct pt_regs *regs)
  527. {
  528. int i;
  529. switch (buf[0]) {
  530. case ADB_PACKET:
  531. #ifdef CONFIG_XMON
  532. if (nb == 5 && buf[2] == 0x2c) {
  533. extern int xmon_wants_key, xmon_adb_keycode;
  534. if (xmon_wants_key) {
  535. xmon_adb_keycode = buf[3];
  536. return;
  537. }
  538. }
  539. #endif /* CONFIG_XMON */
  540. #ifdef CONFIG_ADB
  541. adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
  542. #endif /* CONFIG_ADB */
  543. break;
  544. default:
  545. printk("data from cuda (%d bytes):", nb);
  546. for (i = 0; i < nb; ++i)
  547. printk(" %.2x", buf[i]);
  548. printk("\n");
  549. }
  550. }