via-cuda.c 18 KB

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