via-maciisi.c 16 KB

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