via-cuda.c 16 KB

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