via-macii.c 17 KB

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