via-cuda.c 16 KB

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