via-cuda.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767
  1. /*
  2. * Device driver for the Cuda and Egret system controllers found on PowerMacs
  3. * and 68k Macs.
  4. *
  5. * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA.
  6. * This MCU controls system power, Parameter RAM, Real Time Clock and the
  7. * Apple Desktop Bus (ADB) that connects to the keyboard and mouse.
  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. /*
  51. * When the Cuda design replaced the Egret, some signal names and
  52. * logic sense changed. They all serve the same purposes, however.
  53. *
  54. * VIA pin | Egret pin
  55. * ----------------+------------------------------------------
  56. * PB3 (input) | Transceiver session (active low)
  57. * PB4 (output) | VIA full (active high)
  58. * PB5 (output) | System session (active high)
  59. *
  60. * VIA pin | Cuda pin
  61. * ----------------+------------------------------------------
  62. * PB3 (input) | Transfer request (active low)
  63. * PB4 (output) | Byte acknowledge (active low)
  64. * PB5 (output) | Transfer in progress (active low)
  65. */
  66. /* Bits in Port B data register */
  67. #define TREQ 0x08 /* Transfer request */
  68. #define TACK 0x10 /* Transfer acknowledge */
  69. #define TIP 0x20 /* Transfer in progress */
  70. /* Bits in ACR */
  71. #define SR_CTRL 0x1c /* Shift register control bits */
  72. #define SR_EXT 0x0c /* Shift on external clock */
  73. #define SR_OUT 0x10 /* Shift out if 1 */
  74. /* Bits in IFR and IER */
  75. #define IER_SET 0x80 /* set bits in IER */
  76. #define IER_CLR 0 /* clear bits in IER */
  77. #define SR_INT 0x04 /* Shift register full/empty */
  78. /* Duration of byte acknowledgement pulse (us) */
  79. #define EGRET_TACK_ASSERTED_DELAY 300
  80. #define EGRET_TACK_NEGATED_DELAY 400
  81. /* Interval from interrupt to start of session (us) */
  82. #define EGRET_SESSION_DELAY 450
  83. #ifdef CONFIG_PPC
  84. #define mcu_is_egret false
  85. #else
  86. static bool mcu_is_egret;
  87. #endif
  88. static inline bool TREQ_asserted(u8 portb)
  89. {
  90. return !(portb & TREQ);
  91. }
  92. static inline void assert_TIP(void)
  93. {
  94. if (mcu_is_egret) {
  95. udelay(EGRET_SESSION_DELAY);
  96. out_8(&via[B], in_8(&via[B]) | TIP);
  97. } else
  98. out_8(&via[B], in_8(&via[B]) & ~TIP);
  99. }
  100. static inline void assert_TIP_and_TACK(void)
  101. {
  102. if (mcu_is_egret) {
  103. udelay(EGRET_SESSION_DELAY);
  104. out_8(&via[B], in_8(&via[B]) | TIP | TACK);
  105. } else
  106. out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
  107. }
  108. static inline void assert_TACK(void)
  109. {
  110. if (mcu_is_egret) {
  111. udelay(EGRET_TACK_NEGATED_DELAY);
  112. out_8(&via[B], in_8(&via[B]) | TACK);
  113. } else
  114. out_8(&via[B], in_8(&via[B]) & ~TACK);
  115. }
  116. static inline void toggle_TACK(void)
  117. {
  118. out_8(&via[B], in_8(&via[B]) ^ TACK);
  119. }
  120. static inline void negate_TACK(void)
  121. {
  122. if (mcu_is_egret) {
  123. udelay(EGRET_TACK_ASSERTED_DELAY);
  124. out_8(&via[B], in_8(&via[B]) & ~TACK);
  125. } else
  126. out_8(&via[B], in_8(&via[B]) | TACK);
  127. }
  128. static inline void negate_TIP_and_TACK(void)
  129. {
  130. if (mcu_is_egret) {
  131. udelay(EGRET_TACK_ASSERTED_DELAY);
  132. out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
  133. } else
  134. out_8(&via[B], in_8(&via[B]) | TIP | TACK);
  135. }
  136. static enum cuda_state {
  137. idle,
  138. sent_first_byte,
  139. sending,
  140. reading,
  141. read_done,
  142. awaiting_reply
  143. } cuda_state;
  144. static struct adb_request *current_req;
  145. static struct adb_request *last_req;
  146. static unsigned char cuda_rbuf[16];
  147. static unsigned char *reply_ptr;
  148. static int reading_reply;
  149. static int data_index;
  150. static int cuda_irq;
  151. #ifdef CONFIG_PPC
  152. static struct device_node *vias;
  153. #endif
  154. static int cuda_fully_inited;
  155. #ifdef CONFIG_ADB
  156. static int cuda_probe(void);
  157. static int cuda_send_request(struct adb_request *req, int sync);
  158. static int cuda_adb_autopoll(int devs);
  159. static int cuda_reset_adb_bus(void);
  160. #endif /* CONFIG_ADB */
  161. static int cuda_init_via(void);
  162. static void cuda_start(void);
  163. static irqreturn_t cuda_interrupt(int irq, void *arg);
  164. static void cuda_input(unsigned char *buf, int nb);
  165. void cuda_poll(void);
  166. static int cuda_write(struct adb_request *req);
  167. int cuda_request(struct adb_request *req,
  168. void (*done)(struct adb_request *), int nbytes, ...);
  169. #ifdef CONFIG_ADB
  170. struct adb_driver via_cuda_driver = {
  171. .name = "CUDA",
  172. .probe = cuda_probe,
  173. .send_request = cuda_send_request,
  174. .autopoll = cuda_adb_autopoll,
  175. .poll = cuda_poll,
  176. .reset_bus = cuda_reset_adb_bus,
  177. };
  178. #endif /* CONFIG_ADB */
  179. #ifdef CONFIG_MAC
  180. int __init find_via_cuda(void)
  181. {
  182. struct adb_request req;
  183. int err;
  184. if (macintosh_config->adb_type != MAC_ADB_CUDA &&
  185. macintosh_config->adb_type != MAC_ADB_EGRET)
  186. return 0;
  187. via = via1;
  188. cuda_state = idle;
  189. mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
  190. err = cuda_init_via();
  191. if (err) {
  192. printk(KERN_ERR "cuda_init_via() failed\n");
  193. via = NULL;
  194. return 0;
  195. }
  196. /* enable autopoll */
  197. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
  198. while (!req.complete)
  199. cuda_poll();
  200. return 1;
  201. }
  202. #else
  203. int __init find_via_cuda(void)
  204. {
  205. struct adb_request req;
  206. phys_addr_t taddr;
  207. const u32 *reg;
  208. int err;
  209. if (vias != 0)
  210. return 1;
  211. vias = of_find_node_by_name(NULL, "via-cuda");
  212. if (vias == 0)
  213. return 0;
  214. reg = of_get_property(vias, "reg", NULL);
  215. if (reg == NULL) {
  216. printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
  217. goto fail;
  218. }
  219. taddr = of_translate_address(vias, reg);
  220. if (taddr == 0) {
  221. printk(KERN_ERR "via-cuda: Can't translate address !\n");
  222. goto fail;
  223. }
  224. via = ioremap(taddr, 0x2000);
  225. if (via == NULL) {
  226. printk(KERN_ERR "via-cuda: Can't map address !\n");
  227. goto fail;
  228. }
  229. cuda_state = idle;
  230. sys_ctrler = SYS_CTRLER_CUDA;
  231. err = cuda_init_via();
  232. if (err) {
  233. printk(KERN_ERR "cuda_init_via() failed\n");
  234. via = NULL;
  235. return 0;
  236. }
  237. /* Clear and enable interrupts, but only on PPC. On 68K it's done */
  238. /* for us by the main VIA driver in arch/m68k/mac/via.c */
  239. out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
  240. out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
  241. /* enable autopoll */
  242. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
  243. while (!req.complete)
  244. cuda_poll();
  245. return 1;
  246. fail:
  247. of_node_put(vias);
  248. vias = NULL;
  249. return 0;
  250. }
  251. #endif /* !defined CONFIG_MAC */
  252. static int __init via_cuda_start(void)
  253. {
  254. if (via == NULL)
  255. return -ENODEV;
  256. #ifdef CONFIG_MAC
  257. cuda_irq = IRQ_MAC_ADB;
  258. #else
  259. cuda_irq = irq_of_parse_and_map(vias, 0);
  260. if (!cuda_irq) {
  261. printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
  262. vias);
  263. return -ENODEV;
  264. }
  265. #endif
  266. if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
  267. printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
  268. return -EAGAIN;
  269. }
  270. pr_info("Macintosh Cuda and Egret driver.\n");
  271. cuda_fully_inited = 1;
  272. return 0;
  273. }
  274. device_initcall(via_cuda_start);
  275. #ifdef CONFIG_ADB
  276. static int
  277. cuda_probe(void)
  278. {
  279. #ifdef CONFIG_PPC
  280. if (sys_ctrler != SYS_CTRLER_CUDA)
  281. return -ENODEV;
  282. #else
  283. if (macintosh_config->adb_type != MAC_ADB_CUDA &&
  284. macintosh_config->adb_type != MAC_ADB_EGRET)
  285. return -ENODEV;
  286. #endif
  287. if (via == NULL)
  288. return -ENODEV;
  289. return 0;
  290. }
  291. #endif /* CONFIG_ADB */
  292. static int __init sync_egret(void)
  293. {
  294. if (TREQ_asserted(in_8(&via[B]))) {
  295. /* Complete the inbound transfer */
  296. assert_TIP_and_TACK();
  297. while (1) {
  298. negate_TACK();
  299. mdelay(1);
  300. (void)in_8(&via[SR]);
  301. assert_TACK();
  302. if (!TREQ_asserted(in_8(&via[B])))
  303. break;
  304. }
  305. negate_TIP_and_TACK();
  306. } else if (in_8(&via[B]) & TIP) {
  307. /* Terminate the outbound transfer */
  308. negate_TACK();
  309. assert_TACK();
  310. mdelay(1);
  311. negate_TIP_and_TACK();
  312. }
  313. /* Clear shift register interrupt */
  314. if (in_8(&via[IFR]) & SR_INT)
  315. (void)in_8(&via[SR]);
  316. return 0;
  317. }
  318. #define WAIT_FOR(cond, what) \
  319. do { \
  320. int x; \
  321. for (x = 1000; !(cond); --x) { \
  322. if (x == 0) { \
  323. pr_err("Timeout waiting for " what "\n"); \
  324. return -ENXIO; \
  325. } \
  326. udelay(100); \
  327. } \
  328. } while (0)
  329. static int
  330. __init cuda_init_via(void)
  331. {
  332. #ifdef CONFIG_PPC
  333. out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
  334. (void)in_8(&via[IER]);
  335. #else
  336. out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
  337. #endif
  338. out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
  339. out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
  340. (void)in_8(&via[SR]); /* clear any left-over data */
  341. if (mcu_is_egret)
  342. return sync_egret();
  343. negate_TIP_and_TACK();
  344. /* delay 4ms and then clear any pending interrupt */
  345. mdelay(4);
  346. (void)in_8(&via[SR]);
  347. out_8(&via[IFR], SR_INT);
  348. /* sync with the CUDA - assert TACK without TIP */
  349. assert_TACK();
  350. /* wait for the CUDA to assert TREQ in response */
  351. WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
  352. /* wait for the interrupt and then clear it */
  353. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
  354. (void)in_8(&via[SR]);
  355. out_8(&via[IFR], SR_INT);
  356. /* finish the sync by negating TACK */
  357. negate_TACK();
  358. /* wait for the CUDA to negate TREQ and the corresponding interrupt */
  359. WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
  360. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
  361. (void)in_8(&via[SR]);
  362. out_8(&via[IFR], SR_INT);
  363. return 0;
  364. }
  365. #ifdef CONFIG_ADB
  366. /* Send an ADB command */
  367. static int
  368. cuda_send_request(struct adb_request *req, int sync)
  369. {
  370. int i;
  371. if ((via == NULL) || !cuda_fully_inited) {
  372. req->complete = 1;
  373. return -ENXIO;
  374. }
  375. req->reply_expected = 1;
  376. i = cuda_write(req);
  377. if (i)
  378. return i;
  379. if (sync) {
  380. while (!req->complete)
  381. cuda_poll();
  382. }
  383. return 0;
  384. }
  385. /* Enable/disable autopolling */
  386. static int
  387. cuda_adb_autopoll(int devs)
  388. {
  389. struct adb_request req;
  390. if ((via == NULL) || !cuda_fully_inited)
  391. return -ENXIO;
  392. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
  393. while (!req.complete)
  394. cuda_poll();
  395. return 0;
  396. }
  397. /* Reset adb bus - how do we do this?? */
  398. static int
  399. cuda_reset_adb_bus(void)
  400. {
  401. struct adb_request req;
  402. if ((via == NULL) || !cuda_fully_inited)
  403. return -ENXIO;
  404. cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
  405. while (!req.complete)
  406. cuda_poll();
  407. return 0;
  408. }
  409. #endif /* CONFIG_ADB */
  410. /* Construct and send a cuda request */
  411. int
  412. cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
  413. int nbytes, ...)
  414. {
  415. va_list list;
  416. int i;
  417. if (via == NULL) {
  418. req->complete = 1;
  419. return -ENXIO;
  420. }
  421. req->nbytes = nbytes;
  422. req->done = done;
  423. va_start(list, nbytes);
  424. for (i = 0; i < nbytes; ++i)
  425. req->data[i] = va_arg(list, int);
  426. va_end(list);
  427. req->reply_expected = 1;
  428. return cuda_write(req);
  429. }
  430. EXPORT_SYMBOL(cuda_request);
  431. static int
  432. cuda_write(struct adb_request *req)
  433. {
  434. unsigned long flags;
  435. if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  436. req->complete = 1;
  437. return -EINVAL;
  438. }
  439. req->next = NULL;
  440. req->sent = 0;
  441. req->complete = 0;
  442. req->reply_len = 0;
  443. spin_lock_irqsave(&cuda_lock, flags);
  444. if (current_req != 0) {
  445. last_req->next = req;
  446. last_req = req;
  447. } else {
  448. current_req = req;
  449. last_req = req;
  450. if (cuda_state == idle)
  451. cuda_start();
  452. }
  453. spin_unlock_irqrestore(&cuda_lock, flags);
  454. return 0;
  455. }
  456. static void
  457. cuda_start(void)
  458. {
  459. /* assert cuda_state == idle */
  460. if (current_req == NULL)
  461. return;
  462. data_index = 0;
  463. if (TREQ_asserted(in_8(&via[B])))
  464. return; /* a byte is coming in from the CUDA */
  465. /* set the shift register to shift out and send a byte */
  466. out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
  467. out_8(&via[SR], current_req->data[data_index++]);
  468. if (mcu_is_egret)
  469. assert_TIP_and_TACK();
  470. else
  471. assert_TIP();
  472. cuda_state = sent_first_byte;
  473. }
  474. void
  475. cuda_poll(void)
  476. {
  477. cuda_interrupt(0, NULL);
  478. }
  479. EXPORT_SYMBOL(cuda_poll);
  480. #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
  481. static irqreturn_t
  482. cuda_interrupt(int irq, void *arg)
  483. {
  484. unsigned long flags;
  485. u8 status;
  486. struct adb_request *req = NULL;
  487. unsigned char ibuf[16];
  488. int ibuf_len = 0;
  489. int complete = 0;
  490. spin_lock_irqsave(&cuda_lock, flags);
  491. /* On powermacs, this handler is registered for the VIA IRQ. But they use
  492. * just the shift register IRQ -- other VIA interrupt sources are disabled.
  493. * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
  494. * we are polling, the shift register IRQ flag has already been cleared.
  495. */
  496. #ifdef CONFIG_MAC
  497. if (!arg)
  498. #endif
  499. {
  500. if ((in_8(&via[IFR]) & SR_INT) == 0) {
  501. spin_unlock_irqrestore(&cuda_lock, flags);
  502. return IRQ_NONE;
  503. } else {
  504. out_8(&via[IFR], SR_INT);
  505. }
  506. }
  507. status = in_8(&via[B]) & (TIP | TACK | TREQ);
  508. switch (cuda_state) {
  509. case idle:
  510. /* System controller has unsolicited data for us */
  511. (void)in_8(&via[SR]);
  512. idle_state:
  513. assert_TIP();
  514. cuda_state = reading;
  515. reply_ptr = cuda_rbuf;
  516. reading_reply = 0;
  517. break;
  518. case awaiting_reply:
  519. /* System controller has reply data for us */
  520. (void)in_8(&via[SR]);
  521. assert_TIP();
  522. cuda_state = reading;
  523. reply_ptr = current_req->reply;
  524. reading_reply = 1;
  525. break;
  526. case sent_first_byte:
  527. if (TREQ_asserted(status)) {
  528. /* collision */
  529. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  530. (void)in_8(&via[SR]);
  531. negate_TIP_and_TACK();
  532. cuda_state = idle;
  533. /* Egret does not raise an "aborted" interrupt */
  534. if (mcu_is_egret)
  535. goto idle_state;
  536. } else {
  537. out_8(&via[SR], current_req->data[data_index++]);
  538. toggle_TACK();
  539. if (mcu_is_egret)
  540. assert_TACK();
  541. cuda_state = sending;
  542. }
  543. break;
  544. case sending:
  545. req = current_req;
  546. if (data_index >= req->nbytes) {
  547. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  548. (void)in_8(&via[SR]);
  549. negate_TIP_and_TACK();
  550. req->sent = 1;
  551. if (req->reply_expected) {
  552. cuda_state = awaiting_reply;
  553. } else {
  554. current_req = req->next;
  555. complete = 1;
  556. /* not sure about this */
  557. cuda_state = idle;
  558. cuda_start();
  559. }
  560. } else {
  561. out_8(&via[SR], req->data[data_index++]);
  562. toggle_TACK();
  563. if (mcu_is_egret)
  564. assert_TACK();
  565. }
  566. break;
  567. case reading:
  568. if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
  569. : ARRAY_FULL(cuda_rbuf, reply_ptr))
  570. (void)in_8(&via[SR]);
  571. else
  572. *reply_ptr++ = in_8(&via[SR]);
  573. if (!TREQ_asserted(status)) {
  574. if (mcu_is_egret)
  575. assert_TACK();
  576. /* that's all folks */
  577. negate_TIP_and_TACK();
  578. cuda_state = read_done;
  579. /* Egret does not raise a "read done" interrupt */
  580. if (mcu_is_egret)
  581. goto read_done_state;
  582. } else {
  583. toggle_TACK();
  584. if (mcu_is_egret)
  585. negate_TACK();
  586. }
  587. break;
  588. case read_done:
  589. (void)in_8(&via[SR]);
  590. read_done_state:
  591. if (reading_reply) {
  592. req = current_req;
  593. req->reply_len = reply_ptr - req->reply;
  594. if (req->data[0] == ADB_PACKET) {
  595. /* Have to adjust the reply from ADB commands */
  596. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  597. /* the 0x2 bit indicates no response */
  598. req->reply_len = 0;
  599. } else {
  600. /* leave just the command and result bytes in the reply */
  601. req->reply_len -= 2;
  602. memmove(req->reply, req->reply + 2, req->reply_len);
  603. }
  604. }
  605. current_req = req->next;
  606. complete = 1;
  607. reading_reply = 0;
  608. } else {
  609. /* This is tricky. We must break the spinlock to call
  610. * cuda_input. However, doing so means we might get
  611. * re-entered from another CPU getting an interrupt
  612. * or calling cuda_poll(). I ended up using the stack
  613. * (it's only for 16 bytes) and moving the actual
  614. * call to cuda_input to outside of the lock.
  615. */
  616. ibuf_len = reply_ptr - cuda_rbuf;
  617. memcpy(ibuf, cuda_rbuf, ibuf_len);
  618. }
  619. reply_ptr = cuda_rbuf;
  620. cuda_state = idle;
  621. cuda_start();
  622. if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
  623. assert_TIP();
  624. cuda_state = reading;
  625. }
  626. break;
  627. default:
  628. pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
  629. }
  630. spin_unlock_irqrestore(&cuda_lock, flags);
  631. if (complete && req) {
  632. void (*done)(struct adb_request *) = req->done;
  633. mb();
  634. req->complete = 1;
  635. /* Here, we assume that if the request has a done member, the
  636. * struct request will survive to setting req->complete to 1
  637. */
  638. if (done)
  639. (*done)(req);
  640. }
  641. if (ibuf_len)
  642. cuda_input(ibuf, ibuf_len);
  643. return IRQ_HANDLED;
  644. }
  645. static void
  646. cuda_input(unsigned char *buf, int nb)
  647. {
  648. switch (buf[0]) {
  649. case ADB_PACKET:
  650. #ifdef CONFIG_XMON
  651. if (nb == 5 && buf[2] == 0x2c) {
  652. extern int xmon_wants_key, xmon_adb_keycode;
  653. if (xmon_wants_key) {
  654. xmon_adb_keycode = buf[3];
  655. return;
  656. }
  657. }
  658. #endif /* CONFIG_XMON */
  659. #ifdef CONFIG_ADB
  660. adb_input(buf+2, nb-2, buf[1] & 0x40);
  661. #endif /* CONFIG_ADB */
  662. break;
  663. case TIMER_PACKET:
  664. /* Egret sends these periodically. Might be useful as a 'heartbeat'
  665. * to trigger a recovery for the VIA shift register errata.
  666. */
  667. break;
  668. default:
  669. print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
  670. buf, nb, false);
  671. }
  672. }