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 <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. __init 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. EXPORT_SYMBOL(cuda_request);
  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. EXPORT_SYMBOL(cuda_poll);
  382. static irqreturn_t
  383. cuda_interrupt(int irq, void *arg)
  384. {
  385. int status;
  386. struct adb_request *req = NULL;
  387. unsigned char ibuf[16];
  388. int ibuf_len = 0;
  389. int complete = 0;
  390. spin_lock(&cuda_lock);
  391. /* On powermacs, this handler is registered for the VIA IRQ. But they use
  392. * just the shift register IRQ -- other VIA interrupt sources are disabled.
  393. * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
  394. * we are polling, the shift register IRQ flag has already been cleared.
  395. */
  396. #ifdef CONFIG_MAC
  397. if (!arg)
  398. #endif
  399. {
  400. if ((in_8(&via[IFR]) & SR_INT) == 0) {
  401. spin_unlock(&cuda_lock);
  402. return IRQ_NONE;
  403. } else {
  404. out_8(&via[IFR], SR_INT);
  405. }
  406. }
  407. status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
  408. /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
  409. switch (cuda_state) {
  410. case idle:
  411. /* CUDA has sent us the first byte of data - unsolicited */
  412. if (status != TREQ)
  413. printk("cuda: state=idle, status=%x\n", status);
  414. (void)in_8(&via[SR]);
  415. out_8(&via[B], in_8(&via[B]) & ~TIP);
  416. cuda_state = reading;
  417. reply_ptr = cuda_rbuf;
  418. reading_reply = 0;
  419. break;
  420. case awaiting_reply:
  421. /* CUDA has sent us the first byte of data of a reply */
  422. if (status != TREQ)
  423. printk("cuda: state=awaiting_reply, status=%x\n", status);
  424. (void)in_8(&via[SR]);
  425. out_8(&via[B], in_8(&via[B]) & ~TIP);
  426. cuda_state = reading;
  427. reply_ptr = current_req->reply;
  428. reading_reply = 1;
  429. break;
  430. case sent_first_byte:
  431. if (status == TREQ + TIP + SR_OUT) {
  432. /* collision */
  433. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  434. (void)in_8(&via[SR]);
  435. out_8(&via[B], in_8(&via[B]) | TIP | TACK);
  436. cuda_state = idle;
  437. } else {
  438. /* assert status == TIP + SR_OUT */
  439. if (status != TIP + SR_OUT)
  440. printk("cuda: state=sent_first_byte status=%x\n", status);
  441. out_8(&via[SR], current_req->data[1]);
  442. out_8(&via[B], in_8(&via[B]) ^ TACK);
  443. data_index = 2;
  444. cuda_state = sending;
  445. }
  446. break;
  447. case sending:
  448. req = current_req;
  449. if (data_index >= req->nbytes) {
  450. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  451. (void)in_8(&via[SR]);
  452. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  453. req->sent = 1;
  454. if (req->reply_expected) {
  455. cuda_state = awaiting_reply;
  456. } else {
  457. current_req = req->next;
  458. complete = 1;
  459. /* not sure about this */
  460. cuda_state = idle;
  461. cuda_start();
  462. }
  463. } else {
  464. out_8(&via[SR], req->data[data_index++]);
  465. out_8(&via[B], in_8(&via[B]) ^ TACK);
  466. }
  467. break;
  468. case reading:
  469. *reply_ptr++ = in_8(&via[SR]);
  470. if (status == TIP) {
  471. /* that's all folks */
  472. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  473. cuda_state = read_done;
  474. } else {
  475. /* assert status == TIP | TREQ */
  476. if (status != TIP + TREQ)
  477. printk("cuda: state=reading status=%x\n", status);
  478. out_8(&via[B], in_8(&via[B]) ^ TACK);
  479. }
  480. break;
  481. case read_done:
  482. (void)in_8(&via[SR]);
  483. if (reading_reply) {
  484. req = current_req;
  485. req->reply_len = reply_ptr - req->reply;
  486. if (req->data[0] == ADB_PACKET) {
  487. /* Have to adjust the reply from ADB commands */
  488. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  489. /* the 0x2 bit indicates no response */
  490. req->reply_len = 0;
  491. } else {
  492. /* leave just the command and result bytes in the reply */
  493. req->reply_len -= 2;
  494. memmove(req->reply, req->reply + 2, req->reply_len);
  495. }
  496. }
  497. current_req = req->next;
  498. complete = 1;
  499. } else {
  500. /* This is tricky. We must break the spinlock to call
  501. * cuda_input. However, doing so means we might get
  502. * re-entered from another CPU getting an interrupt
  503. * or calling cuda_poll(). I ended up using the stack
  504. * (it's only for 16 bytes) and moving the actual
  505. * call to cuda_input to outside of the lock.
  506. */
  507. ibuf_len = reply_ptr - cuda_rbuf;
  508. memcpy(ibuf, cuda_rbuf, ibuf_len);
  509. }
  510. if (status == TREQ) {
  511. out_8(&via[B], in_8(&via[B]) & ~TIP);
  512. cuda_state = reading;
  513. reply_ptr = cuda_rbuf;
  514. reading_reply = 0;
  515. } else {
  516. cuda_state = idle;
  517. cuda_start();
  518. }
  519. break;
  520. default:
  521. printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
  522. }
  523. spin_unlock(&cuda_lock);
  524. if (complete && req) {
  525. void (*done)(struct adb_request *) = req->done;
  526. mb();
  527. req->complete = 1;
  528. /* Here, we assume that if the request has a done member, the
  529. * struct request will survive to setting req->complete to 1
  530. */
  531. if (done)
  532. (*done)(req);
  533. }
  534. if (ibuf_len)
  535. cuda_input(ibuf, ibuf_len);
  536. return IRQ_HANDLED;
  537. }
  538. static void
  539. cuda_input(unsigned char *buf, int nb)
  540. {
  541. int i;
  542. switch (buf[0]) {
  543. case ADB_PACKET:
  544. #ifdef CONFIG_XMON
  545. if (nb == 5 && buf[2] == 0x2c) {
  546. extern int xmon_wants_key, xmon_adb_keycode;
  547. if (xmon_wants_key) {
  548. xmon_adb_keycode = buf[3];
  549. return;
  550. }
  551. }
  552. #endif /* CONFIG_XMON */
  553. #ifdef CONFIG_ADB
  554. adb_input(buf+2, nb-2, buf[1] & 0x40);
  555. #endif /* CONFIG_ADB */
  556. break;
  557. default:
  558. printk("data from cuda (%d bytes):", nb);
  559. for (i = 0; i < nb; ++i)
  560. printk(" %.2x", buf[i]);
  561. printk("\n");
  562. }
  563. }