via-cuda.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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. struct resource res;
  210. int err;
  211. if (vias)
  212. return 1;
  213. vias = of_find_node_by_name(NULL, "via-cuda");
  214. if (!vias)
  215. return 0;
  216. err = of_address_to_resource(vias, 0, &res);
  217. if (err) {
  218. printk(KERN_ERR "via-cuda: Error getting \"reg\" property !\n");
  219. goto fail;
  220. }
  221. via = ioremap(res.start, 0x2000);
  222. if (via == NULL) {
  223. printk(KERN_ERR "via-cuda: Can't map address !\n");
  224. goto fail;
  225. }
  226. cuda_state = idle;
  227. sys_ctrler = SYS_CTRLER_CUDA;
  228. err = cuda_init_via();
  229. if (err) {
  230. printk(KERN_ERR "cuda_init_via() failed\n");
  231. via = NULL;
  232. return 0;
  233. }
  234. /* Clear and enable interrupts, but only on PPC. On 68K it's done */
  235. /* for us by the main VIA driver in arch/m68k/mac/via.c */
  236. out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
  237. out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
  238. /* enable autopoll */
  239. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
  240. while (!req.complete)
  241. cuda_poll();
  242. return 1;
  243. fail:
  244. of_node_put(vias);
  245. vias = NULL;
  246. return 0;
  247. }
  248. #endif /* !defined CONFIG_MAC */
  249. static int __init via_cuda_start(void)
  250. {
  251. if (via == NULL)
  252. return -ENODEV;
  253. #ifdef CONFIG_MAC
  254. cuda_irq = IRQ_MAC_ADB;
  255. #else
  256. cuda_irq = irq_of_parse_and_map(vias, 0);
  257. if (!cuda_irq) {
  258. printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
  259. vias);
  260. return -ENODEV;
  261. }
  262. #endif
  263. if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
  264. printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
  265. return -EAGAIN;
  266. }
  267. pr_info("Macintosh Cuda and Egret driver.\n");
  268. cuda_fully_inited = 1;
  269. return 0;
  270. }
  271. device_initcall(via_cuda_start);
  272. #ifdef CONFIG_ADB
  273. static int
  274. cuda_probe(void)
  275. {
  276. #ifdef CONFIG_PPC
  277. if (sys_ctrler != SYS_CTRLER_CUDA)
  278. return -ENODEV;
  279. #else
  280. if (macintosh_config->adb_type != MAC_ADB_CUDA &&
  281. macintosh_config->adb_type != MAC_ADB_EGRET)
  282. return -ENODEV;
  283. #endif
  284. if (via == NULL)
  285. return -ENODEV;
  286. return 0;
  287. }
  288. #endif /* CONFIG_ADB */
  289. static int __init sync_egret(void)
  290. {
  291. if (TREQ_asserted(in_8(&via[B]))) {
  292. /* Complete the inbound transfer */
  293. assert_TIP_and_TACK();
  294. while (1) {
  295. negate_TACK();
  296. mdelay(1);
  297. (void)in_8(&via[SR]);
  298. assert_TACK();
  299. if (!TREQ_asserted(in_8(&via[B])))
  300. break;
  301. }
  302. negate_TIP_and_TACK();
  303. } else if (in_8(&via[B]) & TIP) {
  304. /* Terminate the outbound transfer */
  305. negate_TACK();
  306. assert_TACK();
  307. mdelay(1);
  308. negate_TIP_and_TACK();
  309. }
  310. /* Clear shift register interrupt */
  311. if (in_8(&via[IFR]) & SR_INT)
  312. (void)in_8(&via[SR]);
  313. return 0;
  314. }
  315. #define WAIT_FOR(cond, what) \
  316. do { \
  317. int x; \
  318. for (x = 1000; !(cond); --x) { \
  319. if (x == 0) { \
  320. pr_err("Timeout waiting for " what "\n"); \
  321. return -ENXIO; \
  322. } \
  323. udelay(100); \
  324. } \
  325. } while (0)
  326. static int
  327. __init cuda_init_via(void)
  328. {
  329. #ifdef CONFIG_PPC
  330. out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
  331. (void)in_8(&via[IER]);
  332. #else
  333. out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
  334. #endif
  335. out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
  336. out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
  337. (void)in_8(&via[SR]); /* clear any left-over data */
  338. if (mcu_is_egret)
  339. return sync_egret();
  340. negate_TIP_and_TACK();
  341. /* delay 4ms and then clear any pending interrupt */
  342. mdelay(4);
  343. (void)in_8(&via[SR]);
  344. out_8(&via[IFR], SR_INT);
  345. /* sync with the CUDA - assert TACK without TIP */
  346. assert_TACK();
  347. /* wait for the CUDA to assert TREQ in response */
  348. WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
  349. /* wait for the interrupt and then clear it */
  350. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
  351. (void)in_8(&via[SR]);
  352. out_8(&via[IFR], SR_INT);
  353. /* finish the sync by negating TACK */
  354. negate_TACK();
  355. /* wait for the CUDA to negate TREQ and the corresponding interrupt */
  356. WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
  357. WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
  358. (void)in_8(&via[SR]);
  359. out_8(&via[IFR], SR_INT);
  360. return 0;
  361. }
  362. #ifdef CONFIG_ADB
  363. /* Send an ADB command */
  364. static int
  365. cuda_send_request(struct adb_request *req, int sync)
  366. {
  367. int i;
  368. if ((via == NULL) || !cuda_fully_inited) {
  369. req->complete = 1;
  370. return -ENXIO;
  371. }
  372. req->reply_expected = 1;
  373. i = cuda_write(req);
  374. if (i)
  375. return i;
  376. if (sync) {
  377. while (!req->complete)
  378. cuda_poll();
  379. }
  380. return 0;
  381. }
  382. /* Enable/disable autopolling */
  383. static int
  384. cuda_adb_autopoll(int devs)
  385. {
  386. struct adb_request req;
  387. if ((via == NULL) || !cuda_fully_inited)
  388. return -ENXIO;
  389. cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
  390. while (!req.complete)
  391. cuda_poll();
  392. return 0;
  393. }
  394. /* Reset adb bus - how do we do this?? */
  395. static int
  396. cuda_reset_adb_bus(void)
  397. {
  398. struct adb_request req;
  399. if ((via == NULL) || !cuda_fully_inited)
  400. return -ENXIO;
  401. cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
  402. while (!req.complete)
  403. cuda_poll();
  404. return 0;
  405. }
  406. #endif /* CONFIG_ADB */
  407. /* Construct and send a cuda request */
  408. int
  409. cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
  410. int nbytes, ...)
  411. {
  412. va_list list;
  413. int i;
  414. if (via == NULL) {
  415. req->complete = 1;
  416. return -ENXIO;
  417. }
  418. req->nbytes = nbytes;
  419. req->done = done;
  420. va_start(list, nbytes);
  421. for (i = 0; i < nbytes; ++i)
  422. req->data[i] = va_arg(list, int);
  423. va_end(list);
  424. req->reply_expected = 1;
  425. return cuda_write(req);
  426. }
  427. EXPORT_SYMBOL(cuda_request);
  428. static int
  429. cuda_write(struct adb_request *req)
  430. {
  431. unsigned long flags;
  432. if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  433. req->complete = 1;
  434. return -EINVAL;
  435. }
  436. req->next = NULL;
  437. req->sent = 0;
  438. req->complete = 0;
  439. req->reply_len = 0;
  440. spin_lock_irqsave(&cuda_lock, flags);
  441. if (current_req) {
  442. last_req->next = req;
  443. last_req = req;
  444. } else {
  445. current_req = req;
  446. last_req = req;
  447. if (cuda_state == idle)
  448. cuda_start();
  449. }
  450. spin_unlock_irqrestore(&cuda_lock, flags);
  451. return 0;
  452. }
  453. static void
  454. cuda_start(void)
  455. {
  456. /* assert cuda_state == idle */
  457. if (current_req == NULL)
  458. return;
  459. data_index = 0;
  460. if (TREQ_asserted(in_8(&via[B])))
  461. return; /* a byte is coming in from the CUDA */
  462. /* set the shift register to shift out and send a byte */
  463. out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
  464. out_8(&via[SR], current_req->data[data_index++]);
  465. if (mcu_is_egret)
  466. assert_TIP_and_TACK();
  467. else
  468. assert_TIP();
  469. cuda_state = sent_first_byte;
  470. }
  471. void
  472. cuda_poll(void)
  473. {
  474. cuda_interrupt(0, NULL);
  475. }
  476. EXPORT_SYMBOL(cuda_poll);
  477. #define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
  478. static irqreturn_t
  479. cuda_interrupt(int irq, void *arg)
  480. {
  481. unsigned long flags;
  482. u8 status;
  483. struct adb_request *req = NULL;
  484. unsigned char ibuf[16];
  485. int ibuf_len = 0;
  486. int complete = 0;
  487. bool full;
  488. spin_lock_irqsave(&cuda_lock, flags);
  489. /* On powermacs, this handler is registered for the VIA IRQ. But they use
  490. * just the shift register IRQ -- other VIA interrupt sources are disabled.
  491. * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
  492. * we are polling, the shift register IRQ flag has already been cleared.
  493. */
  494. #ifdef CONFIG_MAC
  495. if (!arg)
  496. #endif
  497. {
  498. if ((in_8(&via[IFR]) & SR_INT) == 0) {
  499. spin_unlock_irqrestore(&cuda_lock, flags);
  500. return IRQ_NONE;
  501. } else {
  502. out_8(&via[IFR], SR_INT);
  503. }
  504. }
  505. status = in_8(&via[B]) & (TIP | TACK | TREQ);
  506. switch (cuda_state) {
  507. case idle:
  508. /* System controller has unsolicited data for us */
  509. (void)in_8(&via[SR]);
  510. idle_state:
  511. assert_TIP();
  512. cuda_state = reading;
  513. reply_ptr = cuda_rbuf;
  514. reading_reply = 0;
  515. break;
  516. case awaiting_reply:
  517. /* System controller has reply data for us */
  518. (void)in_8(&via[SR]);
  519. assert_TIP();
  520. cuda_state = reading;
  521. reply_ptr = current_req->reply;
  522. reading_reply = 1;
  523. break;
  524. case sent_first_byte:
  525. if (TREQ_asserted(status)) {
  526. /* collision */
  527. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  528. (void)in_8(&via[SR]);
  529. negate_TIP_and_TACK();
  530. cuda_state = idle;
  531. /* Egret does not raise an "aborted" interrupt */
  532. if (mcu_is_egret)
  533. goto idle_state;
  534. } else {
  535. out_8(&via[SR], current_req->data[data_index++]);
  536. toggle_TACK();
  537. if (mcu_is_egret)
  538. assert_TACK();
  539. cuda_state = sending;
  540. }
  541. break;
  542. case sending:
  543. req = current_req;
  544. if (data_index >= req->nbytes) {
  545. out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
  546. (void)in_8(&via[SR]);
  547. negate_TIP_and_TACK();
  548. req->sent = 1;
  549. if (req->reply_expected) {
  550. cuda_state = awaiting_reply;
  551. } else {
  552. current_req = req->next;
  553. complete = 1;
  554. /* not sure about this */
  555. cuda_state = idle;
  556. cuda_start();
  557. }
  558. } else {
  559. out_8(&via[SR], req->data[data_index++]);
  560. toggle_TACK();
  561. if (mcu_is_egret)
  562. assert_TACK();
  563. }
  564. break;
  565. case reading:
  566. full = reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
  567. : ARRAY_FULL(cuda_rbuf, reply_ptr);
  568. if (full)
  569. (void)in_8(&via[SR]);
  570. else
  571. *reply_ptr++ = in_8(&via[SR]);
  572. if (!TREQ_asserted(status) || full) {
  573. if (mcu_is_egret)
  574. assert_TACK();
  575. /* that's all folks */
  576. negate_TIP_and_TACK();
  577. cuda_state = read_done;
  578. /* Egret does not raise a "read done" interrupt */
  579. if (mcu_is_egret)
  580. goto read_done_state;
  581. } else {
  582. toggle_TACK();
  583. if (mcu_is_egret)
  584. negate_TACK();
  585. }
  586. break;
  587. case read_done:
  588. (void)in_8(&via[SR]);
  589. read_done_state:
  590. if (reading_reply) {
  591. req = current_req;
  592. req->reply_len = reply_ptr - req->reply;
  593. if (req->data[0] == ADB_PACKET) {
  594. /* Have to adjust the reply from ADB commands */
  595. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  596. /* the 0x2 bit indicates no response */
  597. req->reply_len = 0;
  598. } else {
  599. /* leave just the command and result bytes in the reply */
  600. req->reply_len -= 2;
  601. memmove(req->reply, req->reply + 2, req->reply_len);
  602. }
  603. }
  604. current_req = req->next;
  605. complete = 1;
  606. reading_reply = 0;
  607. } else {
  608. /* This is tricky. We must break the spinlock to call
  609. * cuda_input. However, doing so means we might get
  610. * re-entered from another CPU getting an interrupt
  611. * or calling cuda_poll(). I ended up using the stack
  612. * (it's only for 16 bytes) and moving the actual
  613. * call to cuda_input to outside of the lock.
  614. */
  615. ibuf_len = reply_ptr - cuda_rbuf;
  616. memcpy(ibuf, cuda_rbuf, ibuf_len);
  617. }
  618. reply_ptr = cuda_rbuf;
  619. cuda_state = idle;
  620. cuda_start();
  621. if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
  622. assert_TIP();
  623. cuda_state = reading;
  624. }
  625. break;
  626. default:
  627. pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
  628. }
  629. spin_unlock_irqrestore(&cuda_lock, flags);
  630. if (complete && req) {
  631. void (*done)(struct adb_request *) = req->done;
  632. mb();
  633. req->complete = 1;
  634. /* Here, we assume that if the request has a done member, the
  635. * struct request will survive to setting req->complete to 1
  636. */
  637. if (done)
  638. (*done)(req);
  639. }
  640. if (ibuf_len)
  641. cuda_input(ibuf, ibuf_len);
  642. return IRQ_HANDLED;
  643. }
  644. static void
  645. cuda_input(unsigned char *buf, int nb)
  646. {
  647. switch (buf[0]) {
  648. case ADB_PACKET:
  649. #ifdef CONFIG_XMON
  650. if (nb == 5 && buf[2] == 0x2c) {
  651. extern int xmon_wants_key, xmon_adb_keycode;
  652. if (xmon_wants_key) {
  653. xmon_adb_keycode = buf[3];
  654. return;
  655. }
  656. }
  657. #endif /* CONFIG_XMON */
  658. #ifdef CONFIG_ADB
  659. adb_input(buf+2, nb-2, buf[1] & 0x40);
  660. #endif /* CONFIG_ADB */
  661. break;
  662. case TIMER_PACKET:
  663. /* Egret sends these periodically. Might be useful as a 'heartbeat'
  664. * to trigger a recovery for the VIA shift register errata.
  665. */
  666. break;
  667. default:
  668. print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
  669. buf, nb, false);
  670. }
  671. }
  672. /* Offset between Unix time (1970-based) and Mac time (1904-based) */
  673. #define RTC_OFFSET 2082844800
  674. time64_t cuda_get_time(void)
  675. {
  676. struct adb_request req;
  677. u32 now;
  678. if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
  679. return 0;
  680. while (!req.complete)
  681. cuda_poll();
  682. if (req.reply_len != 7)
  683. pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
  684. now = (req.reply[3] << 24) + (req.reply[4] << 16) +
  685. (req.reply[5] << 8) + req.reply[6];
  686. return (time64_t)now - RTC_OFFSET;
  687. }
  688. int cuda_set_rtc_time(struct rtc_time *tm)
  689. {
  690. u32 now;
  691. struct adb_request req;
  692. now = lower_32_bits(rtc_tm_to_time64(tm) + RTC_OFFSET);
  693. if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
  694. now >> 24, now >> 16, now >> 8, now) < 0)
  695. return -ENXIO;
  696. while (!req.complete)
  697. cuda_poll();
  698. if ((req.reply_len != 3) && (req.reply_len != 7))
  699. pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
  700. return 0;
  701. }