via-cuda.c 16 KB

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