via-cuda.c 19 KB

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