via-cuda.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628
  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 __openfirmware
  39. #define eieio()
  40. #else
  41. #define CUDA_IRQ vias->intrs[0].line
  42. #endif
  43. /* VIA registers - spaced 0x200 bytes apart */
  44. #define RS 0x200 /* skip between registers */
  45. #define B 0 /* B-side data */
  46. #define A RS /* A-side data */
  47. #define DIRB (2*RS) /* B-side direction (1=output) */
  48. #define DIRA (3*RS) /* A-side direction (1=output) */
  49. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  50. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  51. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  52. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  53. #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
  54. #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
  55. #define SR (10*RS) /* Shift register */
  56. #define ACR (11*RS) /* Auxiliary control register */
  57. #define PCR (12*RS) /* Peripheral control register */
  58. #define IFR (13*RS) /* Interrupt flag register */
  59. #define IER (14*RS) /* Interrupt enable register */
  60. #define ANH (15*RS) /* A-side data, no handshake */
  61. /* Bits in B data register: all active low */
  62. #define TREQ 0x08 /* Transfer request (input) */
  63. #define TACK 0x10 /* Transfer acknowledge (output) */
  64. #define TIP 0x20 /* Transfer in progress (output) */
  65. /* Bits in ACR */
  66. #define SR_CTRL 0x1c /* Shift register control bits */
  67. #define SR_EXT 0x0c /* Shift on external clock */
  68. #define SR_OUT 0x10 /* Shift out if 1 */
  69. /* Bits in IFR and IER */
  70. #define IER_SET 0x80 /* set bits in IER */
  71. #define IER_CLR 0 /* clear bits in IER */
  72. #define SR_INT 0x04 /* Shift register full/empty */
  73. static enum cuda_state {
  74. idle,
  75. sent_first_byte,
  76. sending,
  77. reading,
  78. read_done,
  79. awaiting_reply
  80. } cuda_state;
  81. static struct adb_request *current_req;
  82. static struct adb_request *last_req;
  83. static unsigned char cuda_rbuf[16];
  84. static unsigned char *reply_ptr;
  85. static int reading_reply;
  86. static int data_index;
  87. #ifdef CONFIG_PPC
  88. static struct device_node *vias;
  89. #endif
  90. static int cuda_fully_inited = 0;
  91. #ifdef CONFIG_ADB
  92. static int cuda_probe(void);
  93. static int cuda_init(void);
  94. static int cuda_send_request(struct adb_request *req, int sync);
  95. static int cuda_adb_autopoll(int devs);
  96. static int cuda_reset_adb_bus(void);
  97. #endif /* CONFIG_ADB */
  98. static int cuda_init_via(void);
  99. static void cuda_start(void);
  100. static irqreturn_t cuda_interrupt(int irq, void *arg, struct pt_regs *regs);
  101. static void cuda_input(unsigned char *buf, int nb, struct pt_regs *regs);
  102. void cuda_poll(void);
  103. static int cuda_write(struct adb_request *req);
  104. int cuda_request(struct adb_request *req,
  105. void (*done)(struct adb_request *), int nbytes, ...);
  106. #ifdef CONFIG_ADB
  107. struct adb_driver via_cuda_driver = {
  108. "CUDA",
  109. cuda_probe,
  110. cuda_init,
  111. cuda_send_request,
  112. cuda_adb_autopoll,
  113. cuda_poll,
  114. cuda_reset_adb_bus
  115. };
  116. #endif /* CONFIG_ADB */
  117. #ifdef CONFIG_PPC
  118. int __init
  119. find_via_cuda(void)
  120. {
  121. int err;
  122. struct adb_request req;
  123. if (vias != 0)
  124. return 1;
  125. vias = find_devices("via-cuda");
  126. if (vias == 0)
  127. return 0;
  128. if (vias->next != 0)
  129. printk(KERN_WARNING "Warning: only using 1st via-cuda\n");
  130. #if 0
  131. { int i;
  132. printk("find_via_cuda: node = %p, addrs =", vias->node);
  133. for (i = 0; i < vias->n_addrs; ++i)
  134. printk(" %x(%x)", vias->addrs[i].address, vias->addrs[i].size);
  135. printk(", intrs =");
  136. for (i = 0; i < vias->n_intrs; ++i)
  137. printk(" %x", vias->intrs[i].line);
  138. printk("\n"); }
  139. #endif
  140. if (vias->n_addrs != 1 || vias->n_intrs != 1) {
  141. printk(KERN_ERR "via-cuda: expecting 1 address (%d) and 1 interrupt (%d)\n",
  142. vias->n_addrs, vias->n_intrs);
  143. if (vias->n_addrs < 1 || vias->n_intrs < 1)
  144. return 0;
  145. }
  146. via = ioremap(vias->addrs->address, 0x2000);
  147. cuda_state = idle;
  148. sys_ctrler = SYS_CTRLER_CUDA;
  149. err = cuda_init_via();
  150. if (err) {
  151. printk(KERN_ERR "cuda_init_via() failed\n");
  152. via = NULL;
  153. return 0;
  154. }
  155. /* Clear and enable interrupts, but only on PPC. On 68K it's done */
  156. /* for us by the main VIA driver in arch/m68k/mac/via.c */
  157. #ifndef CONFIG_MAC
  158. out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
  159. out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
  160. #endif
  161. /* enable autopoll */
  162. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
  163. while (!req.complete)
  164. cuda_poll();
  165. return 1;
  166. }
  167. #endif /* CONFIG_PPC */
  168. static int __init via_cuda_start(void)
  169. {
  170. if (via == NULL)
  171. return -ENODEV;
  172. #ifdef CONFIG_PPC
  173. request_OF_resource(vias, 0, NULL);
  174. #endif
  175. if (request_irq(CUDA_IRQ, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
  176. printk(KERN_ERR "cuda_init: can't get irq %d\n", CUDA_IRQ);
  177. return -EAGAIN;
  178. }
  179. printk("Macintosh CUDA driver v0.5 for Unified ADB.\n");
  180. cuda_fully_inited = 1;
  181. return 0;
  182. }
  183. device_initcall(via_cuda_start);
  184. #ifdef CONFIG_ADB
  185. static int
  186. cuda_probe(void)
  187. {
  188. #ifdef CONFIG_PPC
  189. if (sys_ctrler != SYS_CTRLER_CUDA)
  190. return -ENODEV;
  191. #else
  192. if (macintosh_config->adb_type != MAC_ADB_CUDA)
  193. return -ENODEV;
  194. via = via1;
  195. #endif
  196. return 0;
  197. }
  198. static int __init
  199. cuda_init(void)
  200. {
  201. #ifdef CONFIG_PPC
  202. if (via == NULL)
  203. return -ENODEV;
  204. return 0;
  205. #else
  206. int err = cuda_init_via();
  207. if (err) {
  208. printk(KERN_ERR "cuda_init_via() failed\n");
  209. return -ENODEV;
  210. }
  211. return via_cuda_start();
  212. #endif
  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. #ifndef CONFIG_MAC
  234. out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
  235. (void)in_8(&via[IER]);
  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], in_8(&via[IFR]) & 0x7f);
  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], in_8(&via[IFR]) & 0x7f);
  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], in_8(&via[IFR]) & 0x7f);
  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. unsigned long flags;
  370. /* cuda_interrupt only takes a normal lock, we disable
  371. * interrupts here to avoid re-entering and thus deadlocking.
  372. * An option would be to disable only the IRQ source with
  373. * disable_irq(), would that work on m68k ? --BenH
  374. */
  375. local_irq_save(flags);
  376. cuda_interrupt(0, NULL, NULL);
  377. local_irq_restore(flags);
  378. }
  379. static irqreturn_t
  380. cuda_interrupt(int irq, void *arg, struct pt_regs *regs)
  381. {
  382. int status;
  383. struct adb_request *req = NULL;
  384. unsigned char ibuf[16];
  385. int ibuf_len = 0;
  386. int complete = 0;
  387. unsigned char virq;
  388. spin_lock(&cuda_lock);
  389. virq = in_8(&via[IFR]) & 0x7f;
  390. out_8(&via[IFR], virq);
  391. if ((virq & SR_INT) == 0) {
  392. spin_unlock(&cuda_lock);
  393. return IRQ_NONE;
  394. }
  395. status = (~in_8(&via[B]) & (TIP|TREQ)) | (in_8(&via[ACR]) & SR_OUT);
  396. /* printk("cuda_interrupt: state=%d status=%x\n", cuda_state, status); */
  397. switch (cuda_state) {
  398. case idle:
  399. /* CUDA has sent us the first byte of data - unsolicited */
  400. if (status != TREQ)
  401. printk("cuda: state=idle, status=%x\n", status);
  402. (void)in_8(&via[SR]);
  403. out_8(&via[B], in_8(&via[B]) & ~TIP);
  404. cuda_state = reading;
  405. reply_ptr = cuda_rbuf;
  406. reading_reply = 0;
  407. break;
  408. case awaiting_reply:
  409. /* CUDA has sent us the first byte of data of a reply */
  410. if (status != TREQ)
  411. printk("cuda: state=awaiting_reply, 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 = current_req->reply;
  416. reading_reply = 1;
  417. break;
  418. case sent_first_byte:
  419. if (status == TREQ + TIP + SR_OUT) {
  420. /* collision */
  421. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  422. (void)in_8(&via[SR]);
  423. out_8(&via[B], in_8(&via[B]) | TIP | TACK);
  424. cuda_state = idle;
  425. } else {
  426. /* assert status == TIP + SR_OUT */
  427. if (status != TIP + SR_OUT)
  428. printk("cuda: state=sent_first_byte status=%x\n", status);
  429. out_8(&via[SR], current_req->data[1]);
  430. out_8(&via[B], in_8(&via[B]) ^ TACK);
  431. data_index = 2;
  432. cuda_state = sending;
  433. }
  434. break;
  435. case sending:
  436. req = current_req;
  437. if (data_index >= req->nbytes) {
  438. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  439. (void)in_8(&via[SR]);
  440. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  441. req->sent = 1;
  442. if (req->reply_expected) {
  443. cuda_state = awaiting_reply;
  444. } else {
  445. current_req = req->next;
  446. complete = 1;
  447. /* not sure about this */
  448. cuda_state = idle;
  449. cuda_start();
  450. }
  451. } else {
  452. out_8(&via[SR], req->data[data_index++]);
  453. out_8(&via[B], in_8(&via[B]) ^ TACK);
  454. }
  455. break;
  456. case reading:
  457. *reply_ptr++ = in_8(&via[SR]);
  458. if (status == TIP) {
  459. /* that's all folks */
  460. out_8(&via[B], in_8(&via[B]) | TACK | TIP);
  461. cuda_state = read_done;
  462. } else {
  463. /* assert status == TIP | TREQ */
  464. if (status != TIP + TREQ)
  465. printk("cuda: state=reading status=%x\n", status);
  466. out_8(&via[B], in_8(&via[B]) ^ TACK);
  467. }
  468. break;
  469. case read_done:
  470. (void)in_8(&via[SR]);
  471. if (reading_reply) {
  472. req = current_req;
  473. req->reply_len = reply_ptr - req->reply;
  474. if (req->data[0] == ADB_PACKET) {
  475. /* Have to adjust the reply from ADB commands */
  476. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  477. /* the 0x2 bit indicates no response */
  478. req->reply_len = 0;
  479. } else {
  480. /* leave just the command and result bytes in the reply */
  481. req->reply_len -= 2;
  482. memmove(req->reply, req->reply + 2, req->reply_len);
  483. }
  484. }
  485. current_req = req->next;
  486. complete = 1;
  487. } else {
  488. /* This is tricky. We must break the spinlock to call
  489. * cuda_input. However, doing so means we might get
  490. * re-entered from another CPU getting an interrupt
  491. * or calling cuda_poll(). I ended up using the stack
  492. * (it's only for 16 bytes) and moving the actual
  493. * call to cuda_input to outside of the lock.
  494. */
  495. ibuf_len = reply_ptr - cuda_rbuf;
  496. memcpy(ibuf, cuda_rbuf, ibuf_len);
  497. }
  498. if (status == TREQ) {
  499. out_8(&via[B], in_8(&via[B]) & ~TIP);
  500. cuda_state = reading;
  501. reply_ptr = cuda_rbuf;
  502. reading_reply = 0;
  503. } else {
  504. cuda_state = idle;
  505. cuda_start();
  506. }
  507. break;
  508. default:
  509. printk("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
  510. }
  511. spin_unlock(&cuda_lock);
  512. if (complete && req) {
  513. void (*done)(struct adb_request *) = req->done;
  514. mb();
  515. req->complete = 1;
  516. /* Here, we assume that if the request has a done member, the
  517. * struct request will survive to setting req->complete to 1
  518. */
  519. if (done)
  520. (*done)(req);
  521. }
  522. if (ibuf_len)
  523. cuda_input(ibuf, ibuf_len, regs);
  524. return IRQ_HANDLED;
  525. }
  526. static void
  527. cuda_input(unsigned char *buf, int nb, struct pt_regs *regs)
  528. {
  529. int i;
  530. switch (buf[0]) {
  531. case ADB_PACKET:
  532. #ifdef CONFIG_XMON
  533. if (nb == 5 && buf[2] == 0x2c) {
  534. extern int xmon_wants_key, xmon_adb_keycode;
  535. if (xmon_wants_key) {
  536. xmon_adb_keycode = buf[3];
  537. return;
  538. }
  539. }
  540. #endif /* CONFIG_XMON */
  541. #ifdef CONFIG_ADB
  542. adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
  543. #endif /* CONFIG_ADB */
  544. break;
  545. default:
  546. printk("data from cuda (%d bytes):", nb);
  547. for (i = 0; i < nb; ++i)
  548. printk(" %.2x", buf[i]);
  549. printk("\n");
  550. }
  551. }