via-maciisi.c 15 KB

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