via-cuda.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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 find_via_cuda(void)
  118. {
  119. struct adb_request req;
  120. phys_addr_t taddr;
  121. u32 *reg;
  122. int err;
  123. if (vias != 0)
  124. return 1;
  125. vias = of_find_node_by_name(NULL, "via-cuda");
  126. if (vias == 0)
  127. return 0;
  128. reg = (u32 *)get_property(vias, "reg", NULL);
  129. if (reg == NULL) {
  130. printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
  131. goto fail;
  132. }
  133. taddr = of_translate_address(vias, reg);
  134. if (taddr == 0) {
  135. printk(KERN_ERR "via-cuda: Can't translate address !\n");
  136. goto fail;
  137. }
  138. via = ioremap(taddr, 0x2000);
  139. if (via == NULL) {
  140. printk(KERN_ERR "via-cuda: Can't map address !\n");
  141. goto fail;
  142. }
  143. cuda_state = idle;
  144. sys_ctrler = SYS_CTRLER_CUDA;
  145. err = cuda_init_via();
  146. if (err) {
  147. printk(KERN_ERR "cuda_init_via() failed\n");
  148. via = NULL;
  149. return 0;
  150. }
  151. /* Clear and enable interrupts, but only on PPC. On 68K it's done */
  152. /* for us by the main VIA driver in arch/m68k/mac/via.c */
  153. #ifndef CONFIG_MAC
  154. out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
  155. out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
  156. #endif
  157. /* enable autopoll */
  158. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
  159. while (!req.complete)
  160. cuda_poll();
  161. return 1;
  162. fail:
  163. of_node_put(vias);
  164. vias = NULL;
  165. return 0;
  166. }
  167. #endif /* CONFIG_PPC */
  168. static int __init via_cuda_start(void)
  169. {
  170. if (via == NULL)
  171. return -ENODEV;
  172. if (request_irq(CUDA_IRQ, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
  173. printk(KERN_ERR "cuda_init: can't get 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. return via_cuda_start();
  209. #endif
  210. }
  211. #endif /* CONFIG_ADB */
  212. #define WAIT_FOR(cond, what) \
  213. do { \
  214. int x; \
  215. for (x = 1000; !(cond); --x) { \
  216. if (x == 0) { \
  217. printk("Timeout waiting for " what "\n"); \
  218. return -ENXIO; \
  219. } \
  220. udelay(100); \
  221. } \
  222. } while (0)
  223. static int
  224. cuda_init_via(void)
  225. {
  226. out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
  227. out_8(&via[B], in_8(&via[B]) | TACK | TIP); /* negate them */
  228. out_8(&via[ACR] ,(in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
  229. (void)in_8(&via[SR]); /* clear any left-over data */
  230. #ifndef CONFIG_MAC
  231. out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
  232. (void)in_8(&via[IER]);
  233. #endif
  234. /* delay 4ms and then clear any pending interrupt */
  235. mdelay(4);
  236. (void)in_8(&via[SR]);
  237. out_8(&via[IFR], in_8(&via[IFR]) & 0x7f);
  238. /* sync with the CUDA - assert TACK without TIP */
  239. out_8(&via[B], in_8(&via[B]) & ~TACK);
  240. /* wait for the CUDA to assert TREQ in response */
  241. WAIT_FOR((in_8(&via[B]) & TREQ) == 0, "CUDA response to sync");
  242. /* wait for the interrupt and then clear it */
  243. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
  244. (void)in_8(&via[SR]);
  245. out_8(&via[IFR], in_8(&via[IFR]) & 0x7f);
  246. /* finish the sync by negating TACK */
  247. out_8(&via[B], in_8(&via[B]) | TACK);
  248. /* wait for the CUDA to negate TREQ and the corresponding interrupt */
  249. WAIT_FOR(in_8(&via[B]) & TREQ, "CUDA response to sync (3)");
  250. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
  251. (void)in_8(&via[SR]);
  252. out_8(&via[IFR], in_8(&via[IFR]) & 0x7f);
  253. out_8(&via[B], in_8(&via[B]) | TIP); /* 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. req->reply_expected = 1;
  267. i = cuda_write(req);
  268. if (i)
  269. return i;
  270. if (sync) {
  271. while (!req->complete)
  272. cuda_poll();
  273. }
  274. return 0;
  275. }
  276. /* Enable/disable autopolling */
  277. static int
  278. cuda_adb_autopoll(int devs)
  279. {
  280. struct adb_request req;
  281. if ((via == NULL) || !cuda_fully_inited)
  282. return -ENXIO;
  283. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
  284. while (!req.complete)
  285. cuda_poll();
  286. return 0;
  287. }
  288. /* Reset adb bus - how do we do this?? */
  289. static int
  290. cuda_reset_adb_bus(void)
  291. {
  292. struct adb_request req;
  293. if ((via == NULL) || !cuda_fully_inited)
  294. return -ENXIO;
  295. cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
  296. while (!req.complete)
  297. cuda_poll();
  298. return 0;
  299. }
  300. #endif /* CONFIG_ADB */
  301. /* Construct and send a cuda request */
  302. int
  303. cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
  304. int nbytes, ...)
  305. {
  306. va_list list;
  307. int i;
  308. if (via == NULL) {
  309. req->complete = 1;
  310. return -ENXIO;
  311. }
  312. req->nbytes = nbytes;
  313. req->done = done;
  314. va_start(list, nbytes);
  315. for (i = 0; i < nbytes; ++i)
  316. req->data[i] = va_arg(list, int);
  317. va_end(list);
  318. req->reply_expected = 1;
  319. return cuda_write(req);
  320. }
  321. static int
  322. cuda_write(struct adb_request *req)
  323. {
  324. unsigned long flags;
  325. if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  326. req->complete = 1;
  327. return -EINVAL;
  328. }
  329. req->next = NULL;
  330. req->sent = 0;
  331. req->complete = 0;
  332. req->reply_len = 0;
  333. spin_lock_irqsave(&cuda_lock, flags);
  334. if (current_req != 0) {
  335. last_req->next = req;
  336. last_req = req;
  337. } else {
  338. current_req = req;
  339. last_req = req;
  340. if (cuda_state == idle)
  341. cuda_start();
  342. }
  343. spin_unlock_irqrestore(&cuda_lock, flags);
  344. return 0;
  345. }
  346. static void
  347. cuda_start(void)
  348. {
  349. struct adb_request *req;
  350. /* assert cuda_state == idle */
  351. /* get the packet to send */
  352. req = current_req;
  353. if (req == 0)
  354. return;
  355. if ((in_8(&via[B]) & TREQ) == 0)
  356. return; /* a byte is coming in from the CUDA */
  357. /* set the shift register to shift out and send a byte */
  358. out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
  359. out_8(&via[SR], req->data[0]);
  360. out_8(&via[B], in_8(&via[B]) & ~TIP);
  361. cuda_state = sent_first_byte;
  362. }
  363. void
  364. cuda_poll(void)
  365. {
  366. unsigned long flags;
  367. /* cuda_interrupt only takes a normal lock, we disable
  368. * interrupts here to avoid re-entering and thus deadlocking.
  369. * An option would be to disable only the IRQ source with
  370. * disable_irq(), would that work on m68k ? --BenH
  371. */
  372. local_irq_save(flags);
  373. cuda_interrupt(0, NULL, NULL);
  374. local_irq_restore(flags);
  375. }
  376. static irqreturn_t
  377. cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
  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. unsigned char virq;
  385. spin_lock(&cuda_lock);
  386. virq = in_8(&via[IFR]) & 0x7f;
  387. out_8(&via[IFR], virq);
  388. if ((virq & SR_INT) == 0) {
  389. spin_unlock(&cuda_lock);
  390. return IRQ_NONE;
  391. }
  392. status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
  393. /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
  394. switch (cuda_state) {
  395. case idle:
  396. /* CUDA has sent us the first byte of data - unsolicited */
  397. if (status != TREQ)
  398. printk("cuda: state=idle, status=%x\n", status);
  399. (void)in_8(&via[SR]);
  400. out_8(&via[B], in_8(&via[B]) & ~TIP);
  401. cuda_state = reading;
  402. reply_ptr = cuda_rbuf;
  403. reading_reply = 0;
  404. break;
  405. case awaiting_reply:
  406. /* CUDA has sent us the first byte of data of a reply */
  407. if (status != TREQ)
  408. printk("cuda: state=awaiting_reply, status=%x\n", status);
  409. (void)in_8(&via[SR]);
  410. out_8(&via[B], in_8(&via[B]) & ~TIP);
  411. cuda_state = reading;
  412. reply_ptr = current_req->reply;
  413. reading_reply = 1;
  414. break;
  415. case sent_first_byte:
  416. if (status == TREQ + TIP + SR_OUT) {
  417. /* collision */
  418. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  419. (void)in_8(&via[SR]);
  420. out_8(&via[B], in_8(&via[B]) | TIP | TACK);
  421. cuda_state = idle;
  422. } else {
  423. /* assert status == TIP + SR_OUT */
  424. if (status != TIP + SR_OUT)
  425. printk("cuda: state=sent_first_byte status=%x\n", status);
  426. out_8(&via[SR], current_req->data[1]);
  427. out_8(&via[B], in_8(&via[B]) ^ TACK);
  428. data_index = 2;
  429. cuda_state = sending;
  430. }
  431. break;
  432. case sending:
  433. req = current_req;
  434. if (data_index >= req->nbytes) {
  435. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  436. (void)in_8(&via[SR]);
  437. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  438. req->sent = 1;
  439. if (req->reply_expected) {
  440. cuda_state = awaiting_reply;
  441. } else {
  442. current_req = req->next;
  443. complete = 1;
  444. /* not sure about this */
  445. cuda_state = idle;
  446. cuda_start();
  447. }
  448. } else {
  449. out_8(&via[SR], req->data[data_index++]);
  450. out_8(&via[B], in_8(&via[B]) ^ TACK);
  451. }
  452. break;
  453. case reading:
  454. *reply_ptr++ = in_8(&via[SR]);
  455. if (status == TIP) {
  456. /* that's all folks */
  457. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  458. cuda_state = read_done;
  459. } else {
  460. /* assert status == TIP | TREQ */
  461. if (status != TIP + TREQ)
  462. printk("cuda: state=reading status=%x\n", status);
  463. out_8(&via[B], in_8(&via[B]) ^ TACK);
  464. }
  465. break;
  466. case read_done:
  467. (void)in_8(&via[SR]);
  468. if (reading_reply) {
  469. req = current_req;
  470. req->reply_len = reply_ptr - req->reply;
  471. if (req->data[0] == ADB_PACKET) {
  472. /* Have to adjust the reply from ADB commands */
  473. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  474. /* the 0x2 bit indicates no response */
  475. req->reply_len = 0;
  476. } else {
  477. /* leave just the command and result bytes in the reply */
  478. req->reply_len -= 2;
  479. memmove(req->reply, req->reply + 2, req->reply_len);
  480. }
  481. }
  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. out_8(&via[B], in_8(&via[B]) & ~TIP);
  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) {
  510. void (*done)(struct adb_request *) = req->done;
  511. mb();
  512. req->complete = 1;
  513. /* Here, we assume that if the request has a done member, the
  514. * struct request will survive to setting req->complete to 1
  515. */
  516. if (done)
  517. (*done)(req);
  518. }
  519. if (ibuf_len)
  520. cuda_input(ibuf, ibuf_len, regs);
  521. return IRQ_HANDLED;
  522. }
  523. static void
  524. cuda_input(unsigned char *buf, int nb, struct pt_regs *regs)
  525. {
  526. int i;
  527. switch (buf[0]) {
  528. case ADB_PACKET:
  529. #ifdef CONFIG_XMON
  530. if (nb == 5 && buf[2] == 0x2c) {
  531. extern int xmon_wants_key, xmon_adb_keycode;
  532. if (xmon_wants_key) {
  533. xmon_adb_keycode = buf[3];
  534. return;
  535. }
  536. }
  537. #endif /* CONFIG_XMON */
  538. #ifdef CONFIG_ADB
  539. adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
  540. #endif /* CONFIG_ADB */
  541. break;
  542. default:
  543. printk("data from cuda (%d bytes):", nb);
  544. for (i = 0; i < nb; ++i)
  545. printk(" %.2x", buf[i]);
  546. printk("\n");
  547. }
  548. }