via-maciisi.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. /*
  2. * Device driver for the IIsi-style ADB on some Mac LC and II-class machines
  3. *
  4. * Based on via-cuda.c and via-macii.c, as well as the original
  5. * adb-bus.c, which in turn is somewhat influenced by (but uses no
  6. * code from) the NetBSD HWDIRECT ADB code. Original IIsi driver work
  7. * was done by Robert Thompson and integrated into the old style
  8. * driver by Michael Schmitz.
  9. *
  10. * Original sources (c) Alan Cox, Paul Mackerras, and others.
  11. *
  12. * Rewritten for Unified ADB by David Huggins-Daines <dhd@debian.org>
  13. *
  14. * 7/13/2000- extensive changes by Andrew McPherson <andrew@macduff.dhs.org>
  15. * Works about 30% of the time now.
  16. */
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/adb.h>
  21. #include <linux/cuda.h>
  22. #include <linux/delay.h>
  23. #include <linux/interrupt.h>
  24. #include <asm/macintosh.h>
  25. #include <asm/macints.h>
  26. #include <asm/mac_via.h>
  27. static volatile unsigned char *via;
  28. /* VIA registers - spaced 0x200 bytes apart - only the ones we actually use */
  29. #define RS 0x200 /* skip between registers */
  30. #define B 0 /* B-side data */
  31. #define A RS /* A-side data */
  32. #define DIRB (2*RS) /* B-side direction (1=output) */
  33. #define DIRA (3*RS) /* A-side direction (1=output) */
  34. #define SR (10*RS) /* Shift register */
  35. #define ACR (11*RS) /* Auxiliary control register */
  36. #define IFR (13*RS) /* Interrupt flag register */
  37. #define IER (14*RS) /* Interrupt enable register */
  38. /* Bits in B data register: all active low */
  39. #define TREQ 0x08 /* Transfer request (input) */
  40. #define TACK 0x10 /* Transfer acknowledge (output) */
  41. #define TIP 0x20 /* Transfer in progress (output) */
  42. #define ST_MASK 0x30 /* mask for selecting ADB state bits */
  43. /* Bits in ACR */
  44. #define SR_CTRL 0x1c /* Shift register control bits */
  45. #define SR_EXT 0x0c /* Shift on external clock */
  46. #define SR_OUT 0x10 /* Shift out if 1 */
  47. /* Bits in IFR and IER */
  48. #define IER_SET 0x80 /* set bits in IER */
  49. #define IER_CLR 0 /* clear bits in IER */
  50. #define SR_INT 0x04 /* Shift register full/empty */
  51. #define SR_DATA 0x08 /* Shift register data */
  52. #define SR_CLOCK 0x10 /* Shift register clock */
  53. #define ADB_DELAY 150
  54. #undef DEBUG_MACIISI_ADB
  55. static struct adb_request* current_req;
  56. static struct adb_request* last_req;
  57. static unsigned char maciisi_rbuf[16];
  58. static unsigned char *reply_ptr;
  59. static int data_index;
  60. static int reading_reply;
  61. static int reply_len;
  62. static int tmp;
  63. static int need_sync;
  64. static enum maciisi_state {
  65. idle,
  66. sending,
  67. reading,
  68. } maciisi_state;
  69. static int maciisi_probe(void);
  70. static int maciisi_init(void);
  71. static int maciisi_send_request(struct adb_request* req, int sync);
  72. static void maciisi_sync(struct adb_request *req);
  73. static int maciisi_write(struct adb_request* req);
  74. static irqreturn_t maciisi_interrupt(int irq, void* arg);
  75. static void maciisi_input(unsigned char *buf, int nb);
  76. static int maciisi_init_via(void);
  77. static void maciisi_poll(void);
  78. static int maciisi_start(void);
  79. struct adb_driver via_maciisi_driver = {
  80. "Mac IIsi",
  81. maciisi_probe,
  82. maciisi_init,
  83. maciisi_send_request,
  84. NULL, /* maciisi_adb_autopoll, */
  85. maciisi_poll,
  86. NULL /* maciisi_reset_adb_bus */
  87. };
  88. static int
  89. maciisi_probe(void)
  90. {
  91. if (macintosh_config->adb_type != MAC_ADB_IISI)
  92. return -ENODEV;
  93. via = via1;
  94. return 0;
  95. }
  96. static int
  97. maciisi_init(void)
  98. {
  99. int err;
  100. if (via == NULL)
  101. return -ENODEV;
  102. if ((err = maciisi_init_via())) {
  103. printk(KERN_ERR "maciisi_init: maciisi_init_via() failed, code %d\n", err);
  104. via = NULL;
  105. return err;
  106. }
  107. if (request_irq(IRQ_MAC_ADB, maciisi_interrupt, 0, "ADB",
  108. maciisi_interrupt)) {
  109. printk(KERN_ERR "maciisi_init: can't get irq %d\n", IRQ_MAC_ADB);
  110. return -EAGAIN;
  111. }
  112. printk("adb: Mac IIsi driver v0.2 for Unified ADB.\n");
  113. return 0;
  114. }
  115. /* Flush data from the ADB controller */
  116. static void
  117. maciisi_stfu(void)
  118. {
  119. int status = via[B] & (TIP|TREQ);
  120. if (status & TREQ) {
  121. #ifdef DEBUG_MACIISI_ADB
  122. printk (KERN_DEBUG "maciisi_stfu called with TREQ high!\n");
  123. #endif
  124. return;
  125. }
  126. udelay(ADB_DELAY);
  127. via[ACR] &= ~SR_OUT;
  128. via[IER] = IER_CLR | SR_INT;
  129. udelay(ADB_DELAY);
  130. status = via[B] & (TIP|TREQ);
  131. if (!(status & TREQ))
  132. {
  133. via[B] |= TIP;
  134. while(1)
  135. {
  136. int poll_timeout = ADB_DELAY * 5;
  137. /* Poll for SR interrupt */
  138. while (!(via[IFR] & SR_INT) && poll_timeout-- > 0)
  139. status = via[B] & (TIP|TREQ);
  140. tmp = via[SR]; /* Clear shift register */
  141. #ifdef DEBUG_MACIISI_ADB
  142. printk(KERN_DEBUG "maciisi_stfu: status %x timeout %d data %x\n",
  143. status, poll_timeout, tmp);
  144. #endif
  145. if(via[B] & TREQ)
  146. break;
  147. /* ACK on-off */
  148. via[B] |= TACK;
  149. udelay(ADB_DELAY);
  150. via[B] &= ~TACK;
  151. }
  152. /* end frame */
  153. via[B] &= ~TIP;
  154. udelay(ADB_DELAY);
  155. }
  156. via[IER] = IER_SET | SR_INT;
  157. }
  158. /* All specifically VIA-related initialization goes here */
  159. static int
  160. maciisi_init_via(void)
  161. {
  162. int i;
  163. /* Set the lines up. We want TREQ as input TACK|TIP as output */
  164. via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ;
  165. /* Shift register on input */
  166. via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
  167. #ifdef DEBUG_MACIISI_ADB
  168. printk(KERN_DEBUG "maciisi_init_via: initial status %x\n", via[B] & (TIP|TREQ));
  169. #endif
  170. /* Wipe any pending data and int */
  171. tmp = via[SR];
  172. /* Enable keyboard interrupts */
  173. via[IER] = IER_SET | SR_INT;
  174. /* Set initial state: idle */
  175. via[B] &= ~(TACK|TIP);
  176. /* Clear interrupt bit */
  177. via[IFR] = SR_INT;
  178. for(i = 0; i < 60; i++) {
  179. udelay(ADB_DELAY);
  180. maciisi_stfu();
  181. udelay(ADB_DELAY);
  182. if(via[B] & TREQ)
  183. break;
  184. }
  185. if (i == 60)
  186. printk(KERN_ERR "maciisi_init_via: bus jam?\n");
  187. maciisi_state = idle;
  188. need_sync = 0;
  189. return 0;
  190. }
  191. /* Send a request, possibly waiting for a reply */
  192. static int
  193. maciisi_send_request(struct adb_request* req, int sync)
  194. {
  195. int i;
  196. #ifdef DEBUG_MACIISI_ADB
  197. static int dump_packet = 0;
  198. #endif
  199. if (via == NULL) {
  200. req->complete = 1;
  201. return -ENXIO;
  202. }
  203. #ifdef DEBUG_MACIISI_ADB
  204. if (dump_packet) {
  205. printk(KERN_DEBUG "maciisi_send_request:");
  206. for (i = 0; i < req->nbytes; i++) {
  207. printk(" %.2x", req->data[i]);
  208. }
  209. printk(" sync %d\n", sync);
  210. }
  211. #endif
  212. req->reply_expected = 1;
  213. i = maciisi_write(req);
  214. if (i)
  215. {
  216. /* Normally, if a packet requires syncing, that happens at the end of
  217. * maciisi_send_request. But if the transfer fails, it will be restarted
  218. * by maciisi_interrupt(). We use need_sync to tell maciisi_interrupt
  219. * when to sync a packet that it sends out.
  220. *
  221. * Suggestions on a better way to do this are welcome.
  222. */
  223. if(i == -EBUSY && sync)
  224. need_sync = 1;
  225. else
  226. need_sync = 0;
  227. return i;
  228. }
  229. if(sync)
  230. maciisi_sync(req);
  231. return 0;
  232. }
  233. /* Poll the ADB chip until the request completes */
  234. static void maciisi_sync(struct adb_request *req)
  235. {
  236. int count = 0;
  237. #ifdef DEBUG_MACIISI_ADB
  238. printk(KERN_DEBUG "maciisi_sync called\n");
  239. #endif
  240. /* If for some reason the ADB chip shuts up on us, we want to avoid an endless loop. */
  241. while (!req->complete && count++ < 50) {
  242. maciisi_poll();
  243. }
  244. /* This could be BAD... when the ADB controller doesn't respond
  245. * for this long, it's probably not coming back :-( */
  246. if (count > 50) /* Hopefully shouldn't happen */
  247. printk(KERN_ERR "maciisi_send_request: poll timed out!\n");
  248. }
  249. int
  250. maciisi_request(struct adb_request *req, void (*done)(struct adb_request *),
  251. int nbytes, ...)
  252. {
  253. va_list list;
  254. int i;
  255. req->nbytes = nbytes;
  256. req->done = done;
  257. req->reply_expected = 0;
  258. va_start(list, nbytes);
  259. for (i = 0; i < nbytes; i++)
  260. req->data[i++] = va_arg(list, int);
  261. va_end(list);
  262. return maciisi_send_request(req, 1);
  263. }
  264. /* Enqueue a request, and run the queue if possible */
  265. static int
  266. maciisi_write(struct adb_request* req)
  267. {
  268. unsigned long flags;
  269. int i;
  270. /* We will accept CUDA packets - the VIA sends them to us, so
  271. it figures that we should be able to send them to it */
  272. if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  273. printk(KERN_ERR "maciisi_write: packet too small or not an ADB or CUDA packet\n");
  274. req->complete = 1;
  275. return -EINVAL;
  276. }
  277. req->next = NULL;
  278. req->sent = 0;
  279. req->complete = 0;
  280. req->reply_len = 0;
  281. local_irq_save(flags);
  282. if (current_req) {
  283. last_req->next = req;
  284. last_req = req;
  285. } else {
  286. current_req = req;
  287. last_req = req;
  288. }
  289. if (maciisi_state == idle)
  290. {
  291. i = maciisi_start();
  292. if(i != 0)
  293. {
  294. local_irq_restore(flags);
  295. return i;
  296. }
  297. }
  298. else
  299. {
  300. #ifdef DEBUG_MACIISI_ADB
  301. printk(KERN_DEBUG "maciisi_write: would start, but state is %d\n", maciisi_state);
  302. #endif
  303. local_irq_restore(flags);
  304. return -EBUSY;
  305. }
  306. local_irq_restore(flags);
  307. return 0;
  308. }
  309. static int
  310. maciisi_start(void)
  311. {
  312. struct adb_request* req;
  313. int status;
  314. #ifdef DEBUG_MACIISI_ADB
  315. status = via[B] & (TIP | TREQ);
  316. printk(KERN_DEBUG "maciisi_start called, state=%d, status=%x, ifr=%x\n", maciisi_state, status, via[IFR]);
  317. #endif
  318. if (maciisi_state != idle) {
  319. /* shouldn't happen */
  320. printk(KERN_ERR "maciisi_start: maciisi_start called when driver busy!\n");
  321. return -EBUSY;
  322. }
  323. req = current_req;
  324. if (req == NULL)
  325. return -EINVAL;
  326. status = via[B] & (TIP|TREQ);
  327. if (!(status & TREQ)) {
  328. #ifdef DEBUG_MACIISI_ADB
  329. printk(KERN_DEBUG "maciisi_start: bus busy - aborting\n");
  330. #endif
  331. return -EBUSY;
  332. }
  333. /* Okay, send */
  334. #ifdef DEBUG_MACIISI_ADB
  335. printk(KERN_DEBUG "maciisi_start: sending\n");
  336. #endif
  337. /* Set state to active */
  338. via[B] |= TIP;
  339. /* ACK off */
  340. via[B] &= ~TACK;
  341. /* Delay */
  342. udelay(ADB_DELAY);
  343. /* Shift out and send */
  344. via[ACR] |= SR_OUT;
  345. via[SR] = req->data[0];
  346. data_index = 1;
  347. /* ACK on */
  348. via[B] |= TACK;
  349. maciisi_state = sending;
  350. return 0;
  351. }
  352. void
  353. maciisi_poll(void)
  354. {
  355. unsigned long flags;
  356. local_irq_save(flags);
  357. if (via[IFR] & SR_INT) {
  358. maciisi_interrupt(0, NULL);
  359. }
  360. else /* avoid calling this function too quickly in a loop */
  361. udelay(ADB_DELAY);
  362. local_irq_restore(flags);
  363. }
  364. /* Shift register interrupt - this is *supposed* to mean that the
  365. register is either full or empty. In practice, I have no idea what
  366. it means :( */
  367. static irqreturn_t
  368. maciisi_interrupt(int irq, void* arg)
  369. {
  370. int status;
  371. struct adb_request *req;
  372. #ifdef DEBUG_MACIISI_ADB
  373. static int dump_reply = 0;
  374. #endif
  375. int i;
  376. unsigned long flags;
  377. local_irq_save(flags);
  378. status = via[B] & (TIP|TREQ);
  379. #ifdef DEBUG_MACIISI_ADB
  380. printk(KERN_DEBUG "state %d status %x ifr %x\n", maciisi_state, status, via[IFR]);
  381. #endif
  382. if (!(via[IFR] & SR_INT)) {
  383. /* Shouldn't happen, we hope */
  384. printk(KERN_ERR "maciisi_interrupt: called without interrupt flag set\n");
  385. local_irq_restore(flags);
  386. return IRQ_NONE;
  387. }
  388. /* Clear the interrupt */
  389. /* via[IFR] = SR_INT; */
  390. switch_start:
  391. switch (maciisi_state) {
  392. case idle:
  393. if (status & TIP)
  394. printk(KERN_ERR "maciisi_interrupt: state is idle but TIP asserted!\n");
  395. if(!reading_reply)
  396. udelay(ADB_DELAY);
  397. /* Shift in */
  398. via[ACR] &= ~SR_OUT;
  399. /* Signal start of frame */
  400. via[B] |= TIP;
  401. /* Clear the interrupt (throw this value on the floor, it's useless) */
  402. tmp = via[SR];
  403. /* ACK adb chip, high-low */
  404. via[B] |= TACK;
  405. udelay(ADB_DELAY);
  406. via[B] &= ~TACK;
  407. reply_len = 0;
  408. maciisi_state = reading;
  409. if (reading_reply) {
  410. reply_ptr = current_req->reply;
  411. } else {
  412. reply_ptr = maciisi_rbuf;
  413. }
  414. break;
  415. case sending:
  416. /* via[SR]; */
  417. /* Set ACK off */
  418. via[B] &= ~TACK;
  419. req = current_req;
  420. if (!(status & TREQ)) {
  421. /* collision */
  422. printk(KERN_ERR "maciisi_interrupt: send collision\n");
  423. /* Set idle and input */
  424. via[ACR] &= ~SR_OUT;
  425. tmp = via[SR];
  426. via[B] &= ~TIP;
  427. /* Must re-send */
  428. reading_reply = 0;
  429. reply_len = 0;
  430. maciisi_state = idle;
  431. udelay(ADB_DELAY);
  432. /* process this now, because the IFR has been cleared */
  433. goto switch_start;
  434. }
  435. udelay(ADB_DELAY);
  436. if (data_index >= req->nbytes) {
  437. /* Sent the whole packet, put the bus back in idle state */
  438. /* Shift in, we are about to read a reply (hopefully) */
  439. via[ACR] &= ~SR_OUT;
  440. tmp = via[SR];
  441. /* End of frame */
  442. via[B] &= ~TIP;
  443. req->sent = 1;
  444. maciisi_state = idle;
  445. if (req->reply_expected) {
  446. /* Note: only set this once we've
  447. successfully sent the packet */
  448. reading_reply = 1;
  449. } else {
  450. current_req = req->next;
  451. if (req->done)
  452. (*req->done)(req);
  453. /* Do any queued requests now */
  454. i = maciisi_start();
  455. if(i == 0 && need_sync) {
  456. /* Packet needs to be synced */
  457. maciisi_sync(current_req);
  458. }
  459. if(i != -EBUSY)
  460. need_sync = 0;
  461. }
  462. } else {
  463. /* Sending more stuff */
  464. /* Shift out */
  465. via[ACR] |= SR_OUT;
  466. /* Write */
  467. via[SR] = req->data[data_index++];
  468. /* Signal 'byte ready' */
  469. via[B] |= TACK;
  470. }
  471. break;
  472. case reading:
  473. /* Shift in */
  474. /* via[ACR] &= ~SR_OUT; */ /* Not in 2.2 */
  475. if (reply_len++ > 16) {
  476. printk(KERN_ERR "maciisi_interrupt: reply too long, aborting read\n");
  477. via[B] |= TACK;
  478. udelay(ADB_DELAY);
  479. via[B] &= ~(TACK|TIP);
  480. maciisi_state = idle;
  481. i = maciisi_start();
  482. if(i == 0 && need_sync) {
  483. /* Packet needs to be synced */
  484. maciisi_sync(current_req);
  485. }
  486. if(i != -EBUSY)
  487. need_sync = 0;
  488. break;
  489. }
  490. /* Read data */
  491. *reply_ptr++ = via[SR];
  492. status = via[B] & (TIP|TREQ);
  493. /* ACK on/off */
  494. via[B] |= TACK;
  495. udelay(ADB_DELAY);
  496. via[B] &= ~TACK;
  497. if (!(status & TREQ))
  498. break; /* more stuff to deal with */
  499. /* end of frame */
  500. via[B] &= ~TIP;
  501. tmp = via[SR]; /* That's what happens in 2.2 */
  502. udelay(ADB_DELAY); /* Give controller time to recover */
  503. /* end of packet, deal with it */
  504. if (reading_reply) {
  505. req = current_req;
  506. req->reply_len = reply_ptr - req->reply;
  507. if (req->data[0] == ADB_PACKET) {
  508. /* Have to adjust the reply from ADB commands */
  509. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  510. /* the 0x2 bit indicates no response */
  511. req->reply_len = 0;
  512. } else {
  513. /* leave just the command and result bytes in the reply */
  514. req->reply_len -= 2;
  515. memmove(req->reply, req->reply + 2, req->reply_len);
  516. }
  517. }
  518. #ifdef DEBUG_MACIISI_ADB
  519. if (dump_reply) {
  520. int i;
  521. printk(KERN_DEBUG "maciisi_interrupt: reply is ");
  522. for (i = 0; i < req->reply_len; ++i)
  523. printk(" %.2x", req->reply[i]);
  524. printk("\n");
  525. }
  526. #endif
  527. req->complete = 1;
  528. current_req = req->next;
  529. if (req->done)
  530. (*req->done)(req);
  531. /* Obviously, we got it */
  532. reading_reply = 0;
  533. } else {
  534. maciisi_input(maciisi_rbuf, reply_ptr - maciisi_rbuf);
  535. }
  536. maciisi_state = idle;
  537. status = via[B] & (TIP|TREQ);
  538. if (!(status & TREQ)) {
  539. /* Timeout?! More likely, another packet coming in already */
  540. #ifdef DEBUG_MACIISI_ADB
  541. printk(KERN_DEBUG "extra data after packet: status %x ifr %x\n",
  542. status, via[IFR]);
  543. #endif
  544. #if 0
  545. udelay(ADB_DELAY);
  546. via[B] |= TIP;
  547. maciisi_state = reading;
  548. reading_reply = 0;
  549. reply_ptr = maciisi_rbuf;
  550. #else
  551. /* Process the packet now */
  552. reading_reply = 0;
  553. goto switch_start;
  554. #endif
  555. /* We used to do this... but the controller might actually have data for us */
  556. /* maciisi_stfu(); */
  557. }
  558. else {
  559. /* Do any queued requests now if possible */
  560. i = maciisi_start();
  561. if(i == 0 && need_sync) {
  562. /* Packet needs to be synced */
  563. maciisi_sync(current_req);
  564. }
  565. if(i != -EBUSY)
  566. need_sync = 0;
  567. }
  568. break;
  569. default:
  570. printk("maciisi_interrupt: unknown maciisi_state %d?\n", maciisi_state);
  571. }
  572. local_irq_restore(flags);
  573. return IRQ_HANDLED;
  574. }
  575. static void
  576. maciisi_input(unsigned char *buf, int nb)
  577. {
  578. #ifdef DEBUG_MACIISI_ADB
  579. int i;
  580. #endif
  581. switch (buf[0]) {
  582. case ADB_PACKET:
  583. adb_input(buf+2, nb-2, buf[1] & 0x40);
  584. break;
  585. default:
  586. #ifdef DEBUG_MACIISI_ADB
  587. printk(KERN_DEBUG "data from IIsi ADB (%d bytes):", nb);
  588. for (i = 0; i < nb; ++i)
  589. printk(" %.2x", buf[i]);
  590. printk("\n");
  591. #endif
  592. break;
  593. }
  594. }