via-macii.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * Device driver for the via ADB on (many) Mac II-class machines
  3. *
  4. * Based on the original ADB keyboard handler Copyright (c) 1997 Alan Cox
  5. * Also derived from code Copyright (C) 1996 Paul Mackerras.
  6. *
  7. * With various updates provided over the years by Michael Schmitz,
  8. * Guideo Koerber and others.
  9. *
  10. * Rewrite for Unified ADB by Joshua M. Thompson (funaho@jurai.org)
  11. *
  12. * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
  13. * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org>
  14. * - Big overhaul, should actually work now.
  15. */
  16. #include <stdarg.h>
  17. #include <linux/types.h>
  18. #include <linux/errno.h>
  19. #include <linux/kernel.h>
  20. #include <linux/delay.h>
  21. #include <linux/adb.h>
  22. #include <linux/interrupt.h>
  23. #include <linux/init.h>
  24. #include <asm/macintosh.h>
  25. #include <asm/macints.h>
  26. #include <asm/machw.h>
  27. #include <asm/mac_via.h>
  28. #include <asm/io.h>
  29. #include <asm/system.h>
  30. static volatile unsigned char *via;
  31. /* VIA registers - spaced 0x200 bytes apart */
  32. #define RS 0x200 /* skip between registers */
  33. #define B 0 /* B-side data */
  34. #define A RS /* A-side data */
  35. #define DIRB (2*RS) /* B-side direction (1=output) */
  36. #define DIRA (3*RS) /* A-side direction (1=output) */
  37. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  38. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  39. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  40. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  41. #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
  42. #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
  43. #define SR (10*RS) /* Shift register */
  44. #define ACR (11*RS) /* Auxiliary control register */
  45. #define PCR (12*RS) /* Peripheral control register */
  46. #define IFR (13*RS) /* Interrupt flag register */
  47. #define IER (14*RS) /* Interrupt enable register */
  48. #define ANH (15*RS) /* A-side data, no handshake */
  49. /* Bits in B data register: all active low */
  50. #define TREQ 0x08 /* Transfer request (input) */
  51. #define TACK 0x10 /* Transfer acknowledge (output) */
  52. #define TIP 0x20 /* Transfer in progress (output) */
  53. #define ST_MASK 0x30 /* mask for selecting ADB state bits */
  54. /* Bits in ACR */
  55. #define SR_CTRL 0x1c /* Shift register control bits */
  56. #define SR_EXT 0x0c /* Shift on external clock */
  57. #define SR_OUT 0x10 /* Shift out if 1 */
  58. /* Bits in IFR and IER */
  59. #define IER_SET 0x80 /* set bits in IER */
  60. #define IER_CLR 0 /* clear bits in IER */
  61. #define SR_INT 0x04 /* Shift register full/empty */
  62. #define SR_DATA 0x08 /* Shift register data */
  63. #define SR_CLOCK 0x10 /* Shift register clock */
  64. /* ADB transaction states according to GMHW */
  65. #define ST_CMD 0x00 /* ADB state: command byte */
  66. #define ST_EVEN 0x10 /* ADB state: even data byte */
  67. #define ST_ODD 0x20 /* ADB state: odd data byte */
  68. #define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
  69. static int macii_init_via(void);
  70. static void macii_start(void);
  71. static irqreturn_t macii_interrupt(int irq, void *arg);
  72. static void macii_retransmit(int);
  73. static void macii_queue_poll(void);
  74. static int macii_probe(void);
  75. static int macii_init(void);
  76. static int macii_send_request(struct adb_request *req, int sync);
  77. static int macii_write(struct adb_request *req);
  78. static int macii_autopoll(int devs);
  79. static void macii_poll(void);
  80. static int macii_reset_bus(void);
  81. struct adb_driver via_macii_driver = {
  82. "Mac II",
  83. macii_probe,
  84. macii_init,
  85. macii_send_request,
  86. macii_autopoll,
  87. macii_poll,
  88. macii_reset_bus
  89. };
  90. static enum macii_state {
  91. idle,
  92. sending,
  93. reading,
  94. read_done,
  95. awaiting_reply
  96. } macii_state;
  97. static int need_poll;
  98. static int command_byte;
  99. static int last_reply;
  100. static int last_active;
  101. static struct adb_request *current_req;
  102. static struct adb_request *last_req;
  103. static struct adb_request *retry_req;
  104. static unsigned char reply_buf[16];
  105. static unsigned char *reply_ptr;
  106. static int reply_len;
  107. static int reading_reply;
  108. static int data_index;
  109. static int first_byte;
  110. static int prefix_len;
  111. static int status = ST_IDLE|TREQ;
  112. static int last_status;
  113. static int driver_running;
  114. /* debug level 10 required for ADB logging (should be && debug_adb, ideally) */
  115. /* Check for MacII style ADB */
  116. static int macii_probe(void)
  117. {
  118. if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV;
  119. via = via1;
  120. printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
  121. return 0;
  122. }
  123. /* Initialize the driver */
  124. int macii_init(void)
  125. {
  126. unsigned long flags;
  127. int err;
  128. local_irq_save(flags);
  129. err = macii_init_via();
  130. if (err) return err;
  131. err = request_irq(IRQ_MAC_ADB, macii_interrupt, IRQ_FLG_LOCK, "ADB",
  132. macii_interrupt);
  133. if (err) return err;
  134. macii_state = idle;
  135. local_irq_restore(flags);
  136. return 0;
  137. }
  138. /* initialize the hardware */
  139. static int macii_init_via(void)
  140. {
  141. unsigned char x;
  142. /* Set the lines up. We want TREQ as input TACK|TIP as output */
  143. via[DIRB] = (via[DIRB] | TACK | TIP) & ~TREQ;
  144. /* Set up state: idle */
  145. via[B] |= ST_IDLE;
  146. last_status = via[B] & (ST_MASK|TREQ);
  147. /* Shift register on input */
  148. via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
  149. /* Wipe any pending data and int */
  150. x = via[SR];
  151. return 0;
  152. }
  153. /* Send an ADB poll (Talk Register 0 command, tagged on the front of the request queue) */
  154. static void macii_queue_poll(void)
  155. {
  156. static int device = 0;
  157. static int in_poll=0;
  158. static struct adb_request req;
  159. unsigned long flags;
  160. if (in_poll) printk("macii_queue_poll: double poll!\n");
  161. in_poll++;
  162. if (++device > 15) device = 1;
  163. adb_request(&req, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
  164. ADB_READREG(device, 0));
  165. local_irq_save(flags);
  166. req.next = current_req;
  167. current_req = &req;
  168. local_irq_restore(flags);
  169. macii_start();
  170. in_poll--;
  171. }
  172. /* Send an ADB retransmit (Talk, appended to the request queue) */
  173. static void macii_retransmit(int device)
  174. {
  175. static int in_retransmit = 0;
  176. static struct adb_request rt;
  177. unsigned long flags;
  178. if (in_retransmit) printk("macii_retransmit: double retransmit!\n");
  179. in_retransmit++;
  180. adb_request(&rt, NULL, ADBREQ_REPLY|ADBREQ_NOSEND, 1,
  181. ADB_READREG(device, 0));
  182. local_irq_save(flags);
  183. if (current_req != NULL) {
  184. last_req->next = &rt;
  185. last_req = &rt;
  186. } else {
  187. current_req = &rt;
  188. last_req = &rt;
  189. }
  190. if (macii_state == idle) macii_start();
  191. local_irq_restore(flags);
  192. in_retransmit--;
  193. }
  194. /* Send an ADB request; if sync, poll out the reply 'till it's done */
  195. static int macii_send_request(struct adb_request *req, int sync)
  196. {
  197. int i;
  198. i = macii_write(req);
  199. if (i) return i;
  200. if (sync) {
  201. while (!req->complete) macii_poll();
  202. }
  203. return 0;
  204. }
  205. /* Send an ADB request */
  206. static int macii_write(struct adb_request *req)
  207. {
  208. unsigned long flags;
  209. if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
  210. req->complete = 1;
  211. return -EINVAL;
  212. }
  213. req->next = NULL;
  214. req->sent = 0;
  215. req->complete = 0;
  216. req->reply_len = 0;
  217. local_irq_save(flags);
  218. if (current_req != NULL) {
  219. last_req->next = req;
  220. last_req = req;
  221. } else {
  222. current_req = req;
  223. last_req = req;
  224. if (macii_state == idle) macii_start();
  225. }
  226. local_irq_restore(flags);
  227. return 0;
  228. }
  229. /* Start auto-polling */
  230. static int macii_autopoll(int devs)
  231. {
  232. /* Just ping a random default address */
  233. if (!(current_req || retry_req))
  234. macii_retransmit( (last_active < 16 && last_active > 0) ? last_active : 3);
  235. return 0;
  236. }
  237. /* Prod the chip without interrupts */
  238. static void macii_poll(void)
  239. {
  240. unsigned long flags;
  241. local_irq_save(flags);
  242. if (via[IFR] & SR_INT) macii_interrupt(0, NULL);
  243. local_irq_restore(flags);
  244. }
  245. /* Reset the bus */
  246. static int macii_reset_bus(void)
  247. {
  248. static struct adb_request req;
  249. /* Command = 0, Address = ignored */
  250. adb_request(&req, NULL, 0, 1, ADB_BUSRESET);
  251. return 0;
  252. }
  253. /* Start sending ADB packet */
  254. static void macii_start(void)
  255. {
  256. unsigned long flags;
  257. struct adb_request *req;
  258. req = current_req;
  259. if (!req) return;
  260. /* assert macii_state == idle */
  261. if (macii_state != idle) {
  262. printk("macii_start: called while driver busy (%p %x %x)!\n",
  263. req, macii_state, (uint) via1[B] & (ST_MASK|TREQ));
  264. return;
  265. }
  266. local_irq_save(flags);
  267. /*
  268. * IRQ signaled ?? (means ADB controller wants to send, or might
  269. * be end of packet if we were reading)
  270. */
  271. #if 0 /* FIXME: This is broke broke broke, for some reason */
  272. if ((via[B] & TREQ) == 0) {
  273. printk("macii_start: weird poll stuff. huh?\n");
  274. /*
  275. * FIXME - we need to restart this on a timer
  276. * or a collision at boot hangs us.
  277. * Never set macii_state to idle here, or macii_start
  278. * won't be called again from send_request!
  279. * (need to re-check other cases ...)
  280. */
  281. /*
  282. * if the interrupt handler set the need_poll
  283. * flag, it's hopefully a SRQ poll or re-Talk
  284. * so we try to send here anyway
  285. */
  286. if (!need_poll) {
  287. if (console_loglevel == 10)
  288. printk("macii_start: device busy - retry %p state %d status %x!\n",
  289. req, macii_state,
  290. (uint) via[B] & (ST_MASK|TREQ));
  291. retry_req = req;
  292. /* set ADB status here ? */
  293. local_irq_restore(flags);
  294. return;
  295. } else {
  296. need_poll = 0;
  297. }
  298. }
  299. #endif
  300. /*
  301. * Another retry pending? (sanity check)
  302. */
  303. if (retry_req) {
  304. retry_req = NULL;
  305. }
  306. /* Now send it. Be careful though, that first byte of the request */
  307. /* is actually ADB_PACKET; the real data begins at index 1! */
  308. /* store command byte */
  309. command_byte = req->data[1];
  310. /* Output mode */
  311. via[ACR] |= SR_OUT;
  312. /* Load data */
  313. via[SR] = req->data[1];
  314. /* set ADB state to 'command' */
  315. via[B] = (via[B] & ~ST_MASK) | ST_CMD;
  316. macii_state = sending;
  317. data_index = 2;
  318. local_irq_restore(flags);
  319. }
  320. /*
  321. * The notorious ADB interrupt handler - does all of the protocol handling,
  322. * except for starting new send operations. Relies heavily on the ADB
  323. * controller sending and receiving data, thereby generating SR interrupts
  324. * for us. This means there has to be always activity on the ADB bus, otherwise
  325. * the whole process dies and has to be re-kicked by sending TALK requests ...
  326. * CUDA-based Macs seem to solve this with the autopoll option, for MacII-type
  327. * ADB the problem isn't solved yet (retransmit of the latest active TALK seems
  328. * a good choice; either on timeout or on a timer interrupt).
  329. *
  330. * The basic ADB state machine was left unchanged from the original MacII code
  331. * by Alan Cox, which was based on the CUDA driver for PowerMac.
  332. * The syntax of the ADB status lines seems to be totally different on MacII,
  333. * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle for
  334. * sending, and Idle -> Even -> Odd -> Even ->...-> Idle for receiving. Start
  335. * and end of a receive packet are signaled by asserting /IRQ on the interrupt
  336. * line. Timeouts are signaled by a sequence of 4 0xFF, with /IRQ asserted on
  337. * every other byte. SRQ is probably signaled by 3 or more 0xFF tacked on the
  338. * end of a packet. (Thanks to Guido Koerber for eavesdropping on the ADB
  339. * protocol with a logic analyzer!!)
  340. *
  341. * Note: As of 21/10/97, the MacII ADB part works including timeout detection
  342. * and retransmit (Talk to the last active device).
  343. */
  344. static irqreturn_t macii_interrupt(int irq, void *arg)
  345. {
  346. int x, adbdir;
  347. unsigned long flags;
  348. struct adb_request *req;
  349. last_status = status;
  350. /* prevent races due to SCSI enabling ints */
  351. local_irq_save(flags);
  352. if (driver_running) {
  353. local_irq_restore(flags);
  354. return IRQ_NONE;
  355. }
  356. driver_running = 1;
  357. status = via[B] & (ST_MASK|TREQ);
  358. adbdir = via[ACR] & SR_OUT;
  359. switch (macii_state) {
  360. case idle:
  361. x = via[SR];
  362. first_byte = x;
  363. /* set ADB state = even for first data byte */
  364. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  365. reply_buf[0] = first_byte; /* was command_byte?? */
  366. reply_ptr = reply_buf + 1;
  367. reply_len = 1;
  368. prefix_len = 1;
  369. reading_reply = 0;
  370. macii_state = reading;
  371. break;
  372. case awaiting_reply:
  373. /* handshake etc. for II ?? */
  374. x = via[SR];
  375. first_byte = x;
  376. /* set ADB state = even for first data byte */
  377. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  378. current_req->reply[0] = first_byte;
  379. reply_ptr = current_req->reply + 1;
  380. reply_len = 1;
  381. prefix_len = 1;
  382. reading_reply = 1;
  383. macii_state = reading;
  384. break;
  385. case sending:
  386. req = current_req;
  387. if (data_index >= req->nbytes) {
  388. /* print an error message if a listen command has no data */
  389. if (((command_byte & 0x0C) == 0x08)
  390. /* && (console_loglevel == 10) */
  391. && (data_index == 2))
  392. printk("MacII ADB: listen command with no data: %x!\n",
  393. command_byte);
  394. /* reset to shift in */
  395. via[ACR] &= ~SR_OUT;
  396. x = via[SR];
  397. /* set ADB state idle - might get SRQ */
  398. via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
  399. req->sent = 1;
  400. if (req->reply_expected) {
  401. macii_state = awaiting_reply;
  402. } else {
  403. req->complete = 1;
  404. current_req = req->next;
  405. if (req->done) (*req->done)(req);
  406. macii_state = idle;
  407. if (current_req || retry_req)
  408. macii_start();
  409. else
  410. macii_retransmit((command_byte & 0xF0) >> 4);
  411. }
  412. } else {
  413. via[SR] = req->data[data_index++];
  414. if ( (via[B] & ST_MASK) == ST_CMD ) {
  415. /* just sent the command byte, set to EVEN */
  416. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  417. } else {
  418. /* invert state bits, toggle ODD/EVEN */
  419. via[B] ^= ST_MASK;
  420. }
  421. }
  422. break;
  423. case reading:
  424. /* timeout / SRQ handling for II hw */
  425. if( (first_byte == 0xFF && (reply_len-prefix_len)==2
  426. && memcmp(reply_ptr-2,"\xFF\xFF",2)==0) ||
  427. ((reply_len-prefix_len)==3
  428. && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0))
  429. {
  430. /*
  431. * possible timeout (in fact, most probably a
  432. * timeout, since SRQ can't be signaled without
  433. * transfer on the bus).
  434. * The last three bytes seen were FF, together
  435. * with the starting byte (in case we started
  436. * on 'idle' or 'awaiting_reply') this probably
  437. * makes four. So this is mostl likely #5!
  438. * The timeout signal is a pattern 1 0 1 0 0..
  439. * on /INT, meaning we missed it :-(
  440. */
  441. x = via[SR];
  442. if (x != 0xFF) printk("MacII ADB: mistaken timeout/SRQ!\n");
  443. if ((status & TREQ) == (last_status & TREQ)) {
  444. /* Not a timeout. Unsolicited SRQ? weird. */
  445. /* Terminate the SRQ packet and poll */
  446. need_poll = 1;
  447. }
  448. /* There's no packet to get, so reply is blank */
  449. via[B] ^= ST_MASK;
  450. reply_ptr -= (reply_len-prefix_len);
  451. reply_len = prefix_len;
  452. macii_state = read_done;
  453. break;
  454. } /* end timeout / SRQ handling for II hw. */
  455. if((reply_len-prefix_len)>3
  456. && memcmp(reply_ptr-3,"\xFF\xFF\xFF",3)==0)
  457. {
  458. /* SRQ tacked on data packet */
  459. /* Terminate the packet (SRQ never ends) */
  460. x = via[SR];
  461. macii_state = read_done;
  462. reply_len -= 3;
  463. reply_ptr -= 3;
  464. need_poll = 1;
  465. /* need to continue; next byte not seen else */
  466. } else {
  467. /* Sanity check */
  468. if (reply_len > 15) reply_len = 0;
  469. /* read byte */
  470. x = via[SR];
  471. *reply_ptr = x;
  472. reply_ptr++;
  473. reply_len++;
  474. }
  475. /* The usual handshake ... */
  476. /*
  477. * NetBSD hints that the next to last byte
  478. * is sent with IRQ !!
  479. * Guido found out it's the last one (0x0),
  480. * but IRQ should be asserted already.
  481. * Problem with timeout detection: First
  482. * transition to /IRQ might be second
  483. * byte of timeout packet!
  484. * Timeouts are signaled by 4x FF.
  485. */
  486. if (((status & TREQ) == 0) && (x == 0x00)) { /* != 0xFF */
  487. /* invert state bits, toggle ODD/EVEN */
  488. via[B] ^= ST_MASK;
  489. /* adjust packet length */
  490. reply_len--;
  491. reply_ptr--;
  492. macii_state = read_done;
  493. } else {
  494. /* not caught: ST_CMD */
  495. /* required for re-entry 'reading'! */
  496. if ((status & ST_MASK) == ST_IDLE) {
  497. /* (in)sanity check - set even */
  498. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  499. } else {
  500. /* invert state bits */
  501. via[B] ^= ST_MASK;
  502. }
  503. }
  504. break;
  505. case read_done:
  506. x = via[SR];
  507. if (reading_reply) {
  508. req = current_req;
  509. req->reply_len = reply_ptr - req->reply;
  510. req->complete = 1;
  511. current_req = req->next;
  512. if (req->done) (*req->done)(req);
  513. } else {
  514. adb_input(reply_buf, reply_ptr - reply_buf, 0);
  515. }
  516. /*
  517. * remember this device ID; it's the latest we got a
  518. * reply from!
  519. */
  520. last_reply = command_byte;
  521. last_active = (command_byte & 0xF0) >> 4;
  522. /* SRQ seen before, initiate poll now */
  523. if (need_poll) {
  524. macii_state = idle;
  525. macii_queue_poll();
  526. need_poll = 0;
  527. break;
  528. }
  529. /* set ADB state to idle */
  530. via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
  531. /* /IRQ seen, so the ADB controller has data for us */
  532. if ((via[B] & TREQ) != 0) {
  533. macii_state = reading;
  534. reply_buf[0] = command_byte;
  535. reply_ptr = reply_buf + 1;
  536. reply_len = 1;
  537. prefix_len = 1;
  538. reading_reply = 0;
  539. } else {
  540. /* no IRQ, send next packet or wait */
  541. macii_state = idle;
  542. if (current_req)
  543. macii_start();
  544. else
  545. macii_retransmit(last_active);
  546. }
  547. break;
  548. default:
  549. break;
  550. }
  551. /* reset mutex and interrupts */
  552. driver_running = 0;
  553. local_irq_restore(flags);
  554. return IRQ_HANDLED;
  555. }