via-maciisi.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:15k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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 <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 = NULL;
  57. static struct adb_request* last_req = NULL;
  58. static unsigned char maciisi_rbuf[16];
  59. static unsigned char *reply_ptr = NULL;
  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 void maciisi_interrupt(int irq, void* arg, struct pt_regs* regs);
  76. static void maciisi_input(unsigned char *buf, int nb, struct pt_regs *regs);
  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 %dn", 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 %dn", 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 %xn",
  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 %xn", 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 %dn", 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 calledn");
  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. /* Enqueue a request, and run the queue if possible */
  251. static int
  252. maciisi_write(struct adb_request* req)
  253. {
  254. unsigned long flags;
  255. int i;
  256. /* We will accept CUDA packets - the VIA sends them to us, so
  257.            it figures that we should be able to send them to it */
  258. if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
  259. printk(KERN_ERR "maciisi_write: packet too small or not an ADB or CUDA packetn");
  260. req->complete = 1;
  261. return -EINVAL;
  262. }
  263. req->next = 0;
  264. req->sent = 0;
  265. req->complete = 0;
  266. req->reply_len = 0;
  267. save_flags(flags);
  268. cli();
  269. if (current_req) {
  270. last_req->next = req;
  271. last_req = req;
  272. } else {
  273. current_req = req;
  274. last_req = req;
  275. }
  276. if (maciisi_state == idle)
  277. {
  278. i = maciisi_start();
  279. if(i != 0)
  280. {
  281. restore_flags(flags);
  282. return i;
  283. }
  284. }
  285. else
  286. {
  287. #ifdef DEBUG_MACIISI_ADB
  288. printk(KERN_DEBUG "maciisi_write: would start, but state is %dn", maciisi_state);
  289. #endif
  290. restore_flags(flags);
  291. return -EBUSY;
  292. }
  293. restore_flags(flags);
  294. return 0;
  295. }
  296. static int
  297. maciisi_start(void)
  298. {
  299. struct adb_request* req;
  300. int status;
  301. #ifdef DEBUG_MACIISI_ADB
  302. status = via[B] & (TIP | TREQ);
  303. printk(KERN_DEBUG "maciisi_start called, state=%d, status=%x, ifr=%xn", maciisi_state, status, via[IFR]);
  304. #endif
  305. if (maciisi_state != idle) {
  306. /* shouldn't happen */
  307. printk(KERN_ERR "maciisi_start: maciisi_start called when driver busy!n");
  308. return -EBUSY;
  309. }
  310. req = current_req;
  311. if (req == NULL)
  312. return -EINVAL;
  313. status = via[B] & (TIP|TREQ);
  314. if (!(status & TREQ)) {
  315. #ifdef DEBUG_MACIISI_ADB
  316. printk(KERN_DEBUG "maciisi_start: bus busy - abortingn");
  317. #endif
  318. return -EBUSY;
  319. }
  320. /* Okay, send */
  321. #ifdef DEBUG_MACIISI_ADB
  322. printk(KERN_DEBUG "maciisi_start: sendingn");
  323. #endif
  324. /* Set state to active */
  325. via[B] |= TIP;
  326. /* ACK off */
  327. via[B] &= ~TACK;
  328. /* Delay */
  329. udelay(ADB_DELAY);
  330. /* Shift out and send */
  331. via[ACR] |= SR_OUT;
  332. via[SR] = req->data[0];
  333. data_index = 1;
  334. /* ACK on */
  335. via[B] |= TACK;
  336. maciisi_state = sending;
  337. return 0;
  338. }
  339. void
  340. maciisi_poll(void)
  341. {
  342. unsigned long flags;
  343. save_flags(flags);
  344. cli();
  345. if (via[IFR] & SR_INT) {
  346. maciisi_interrupt(0, 0, 0);
  347. }
  348. else /* avoid calling this function too quickly in a loop */
  349. udelay(ADB_DELAY);
  350. restore_flags(flags);
  351. }
  352. /* Shift register interrupt - this is *supposed* to mean that the
  353.    register is either full or empty. In practice, I have no idea what
  354.    it means :( */
  355. static void
  356. maciisi_interrupt(int irq, void* arg, struct pt_regs* regs)
  357. {
  358. int status;
  359. struct adb_request *req;
  360. #ifdef DEBUG_MACIISI_ADB
  361. static int dump_reply = 0;
  362. #endif
  363. int i;
  364. unsigned long flags;
  365. save_flags(flags);
  366. cli();
  367. status = via[B] & (TIP|TREQ);
  368. #ifdef DEBUG_MACIISI_ADB
  369. printk(KERN_DEBUG "state %d status %x ifr %xn", maciisi_state, status, via[IFR]);
  370. #endif
  371. if (!(via[IFR] & SR_INT)) {
  372. /* Shouldn't happen, we hope */
  373. printk(KERN_ERR "maciisi_interrupt: called without interrupt flag setn");
  374. restore_flags(flags);
  375. return;
  376. }
  377. /* Clear the interrupt */
  378. /* via[IFR] = SR_INT; */
  379.  switch_start:
  380. switch (maciisi_state) {
  381. case idle:
  382. if (status & TIP)
  383. printk(KERN_ERR "maciisi_interrupt: state is idle but TIP asserted!n");
  384. if(!reading_reply)
  385. udelay(ADB_DELAY);
  386. /* Shift in */
  387. via[ACR] &= ~SR_OUT;
  388.   /* Signal start of frame */
  389. via[B] |= TIP;
  390. /* Clear the interrupt (throw this value on the floor, it's useless) */
  391. tmp = via[SR];
  392. /* ACK adb chip, high-low */
  393. via[B] |= TACK;
  394. udelay(ADB_DELAY);
  395. via[B] &= ~TACK;
  396. reply_len = 0;
  397. maciisi_state = reading;
  398. if (reading_reply) {
  399. reply_ptr = current_req->reply;
  400. } else {
  401. reply_ptr = maciisi_rbuf;
  402. }
  403. break;
  404. case sending:
  405. /* via[SR]; */
  406. /* Set ACK off */
  407. via[B] &= ~TACK;
  408. req = current_req;
  409. if (!(status & TREQ)) {
  410. /* collision */
  411. printk(KERN_ERR "maciisi_interrupt: send collisionn");
  412. /* Set idle and input */
  413. via[ACR] &= ~SR_OUT;
  414. tmp = via[SR];
  415. via[B] &= ~TIP;
  416. /* Must re-send */
  417. reading_reply = 0;
  418. reply_len = 0;
  419. maciisi_state = idle;
  420. udelay(ADB_DELAY);
  421. /* process this now, because the IFR has been cleared */
  422. goto switch_start;
  423. }
  424. udelay(ADB_DELAY);
  425. if (data_index >= req->nbytes) {
  426. /* Sent the whole packet, put the bus back in idle state */
  427. /* Shift in, we are about to read a reply (hopefully) */
  428. via[ACR] &= ~SR_OUT;
  429. tmp = via[SR];
  430. /* End of frame */
  431. via[B] &= ~TIP;
  432. req->sent = 1;
  433. maciisi_state = idle;
  434. if (req->reply_expected) {
  435. /* Note: only set this once we've
  436.                                    successfully sent the packet */
  437. reading_reply = 1;
  438. } else {
  439. current_req = req->next;
  440. if (req->done)
  441. (*req->done)(req);
  442. /* Do any queued requests now */
  443. i = maciisi_start();
  444. if(i == 0 && need_sync) {
  445. /* Packet needs to be synced */
  446. maciisi_sync(current_req);
  447. }
  448. if(i != -EBUSY)
  449. need_sync = 0;
  450. }
  451. } else {
  452. /* Sending more stuff */
  453. /* Shift out */
  454. via[ACR] |= SR_OUT;
  455. /* Write */
  456. via[SR] = req->data[data_index++];
  457. /* Signal 'byte ready' */
  458. via[B] |= TACK;
  459. }
  460. break;
  461. case reading:
  462. /* Shift in */
  463. /* via[ACR] &= ~SR_OUT; */ /* Not in 2.2 */
  464. if (reply_len++ > 16) {
  465. printk(KERN_ERR "maciisi_interrupt: reply too long, aborting readn");
  466. via[B] |= TACK;
  467. udelay(ADB_DELAY);
  468. via[B] &= ~(TACK|TIP);
  469. maciisi_state = idle;
  470. i = maciisi_start();
  471. if(i == 0 && need_sync) {
  472. /* Packet needs to be synced */
  473. maciisi_sync(current_req);
  474. }
  475. if(i != -EBUSY)
  476. need_sync = 0;
  477. break;
  478. }
  479. /* Read data */
  480. *reply_ptr++ = via[SR];
  481. status = via[B] & (TIP|TREQ);
  482. /* ACK on/off */
  483. via[B] |= TACK;
  484. udelay(ADB_DELAY);
  485. via[B] &= ~TACK;
  486. if (!(status & TREQ))
  487. break; /* more stuff to deal with */
  488. /* end of frame */
  489. via[B] &= ~TIP;
  490. tmp = via[SR]; /* That's what happens in 2.2 */
  491. udelay(ADB_DELAY); /* Give controller time to recover */
  492. /* end of packet, deal with it */
  493. if (reading_reply) {
  494. req = current_req;
  495. req->reply_len = reply_ptr - req->reply;
  496. if (req->data[0] == ADB_PACKET) {
  497. /* Have to adjust the reply from ADB commands */
  498. if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
  499. /* the 0x2 bit indicates no response */
  500. req->reply_len = 0;
  501. } else {
  502. /* leave just the command and result bytes in the reply */
  503. req->reply_len -= 2;
  504. memmove(req->reply, req->reply + 2, req->reply_len);
  505. }
  506. }
  507. #ifdef DEBUG_MACIISI_ADB
  508. if (dump_reply) {
  509. int i;
  510. printk(KERN_DEBUG "maciisi_interrupt: reply is ");
  511. for (i = 0; i < req->reply_len; ++i)
  512. printk(" %.2x", req->reply[i]);
  513. printk("n");
  514. }
  515. #endif
  516. req->complete = 1;
  517. current_req = req->next;
  518. if (req->done)
  519. (*req->done)(req);
  520. /* Obviously, we got it */
  521. reading_reply = 0;
  522. } else {
  523. maciisi_input(maciisi_rbuf, reply_ptr - maciisi_rbuf, regs);
  524. }
  525. maciisi_state = idle;
  526. status = via[B] & (TIP|TREQ);
  527. if (!(status & TREQ)) {
  528. /* Timeout?! More likely, another packet coming in already */
  529. #ifdef DEBUG_MACIISI_ADB
  530. printk(KERN_DEBUG "extra data after packet: status %x ifr %xn",
  531.        status, via[IFR]);
  532. #endif
  533. #if 0
  534. udelay(ADB_DELAY);
  535. via[B] |= TIP;
  536. maciisi_state = reading;
  537. reading_reply = 0;
  538. reply_ptr = maciisi_rbuf;
  539. #else
  540. /* Process the packet now */
  541. reading_reply = 0;
  542. goto switch_start;
  543. #endif
  544. /* We used to do this... but the controller might actually have data for us */
  545. /* maciisi_stfu(); */
  546. }
  547. else {
  548. /* Do any queued requests now if possible */
  549. i = maciisi_start();
  550. if(i == 0 && need_sync) {
  551. /* Packet needs to be synced */
  552. maciisi_sync(current_req);
  553. }
  554. if(i != -EBUSY)
  555. need_sync = 0;
  556. }
  557. break;
  558. default:
  559. printk("maciisi_interrupt: unknown maciisi_state %d?n", maciisi_state);
  560. }
  561. restore_flags(flags);
  562. }
  563. static void
  564. maciisi_input(unsigned char *buf, int nb, struct pt_regs *regs)
  565. {
  566. #ifdef DEBUG_MACIISI_ADB
  567.     int i;
  568. #endif
  569.     switch (buf[0]) {
  570.     case ADB_PACKET:
  571.     adb_input(buf+2, nb-2, regs, buf[1] & 0x40);
  572.     break;
  573.     default:
  574. #ifdef DEBUG_MACIISI_ADB
  575.     printk(KERN_DEBUG "data from IIsi ADB (%d bytes):", nb);
  576.     for (i = 0; i < nb; ++i)
  577.     printk(" %.2x", buf[i]);
  578.     printk("n");
  579. #endif
  580.     break;
  581.     }
  582. }