via-macii.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. * 2006-12-31 Finn Thain <fthain@telegraphics.com.au> - Another overhaul.
  16. *
  17. * Suggested reading:
  18. * Inside Macintosh, ch. 5 ADB Manager
  19. * Guide to the Macinstosh Family Hardware, ch. 8 Apple Desktop Bus
  20. * Rockwell R6522 VIA datasheet
  21. *
  22. * Apple's "ADB Analyzer" bus sniffer is invaluable:
  23. * ftp://ftp.apple.com/developer/Tool_Chest/Devices_-_Hardware/Apple_Desktop_Bus/
  24. */
  25. #include <stdarg.h>
  26. #include <linux/types.h>
  27. #include <linux/errno.h>
  28. #include <linux/kernel.h>
  29. #include <linux/delay.h>
  30. #include <linux/adb.h>
  31. #include <linux/interrupt.h>
  32. #include <linux/init.h>
  33. #include <asm/macintosh.h>
  34. #include <asm/macints.h>
  35. #include <asm/mac_via.h>
  36. static volatile unsigned char *via;
  37. /* VIA registers - spaced 0x200 bytes apart */
  38. #define RS 0x200 /* skip between registers */
  39. #define B 0 /* B-side data */
  40. #define A RS /* A-side data */
  41. #define DIRB (2*RS) /* B-side direction (1=output) */
  42. #define DIRA (3*RS) /* A-side direction (1=output) */
  43. #define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
  44. #define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
  45. #define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
  46. #define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
  47. #define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
  48. #define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
  49. #define SR (10*RS) /* Shift register */
  50. #define ACR (11*RS) /* Auxiliary control register */
  51. #define PCR (12*RS) /* Peripheral control register */
  52. #define IFR (13*RS) /* Interrupt flag register */
  53. #define IER (14*RS) /* Interrupt enable register */
  54. #define ANH (15*RS) /* A-side data, no handshake */
  55. /* Bits in B data register: all active low */
  56. #define CTLR_IRQ 0x08 /* Controller rcv status (input) */
  57. #define ST_MASK 0x30 /* mask for selecting ADB state bits */
  58. /* Bits in ACR */
  59. #define SR_CTRL 0x1c /* Shift register control bits */
  60. #define SR_EXT 0x0c /* Shift on external clock */
  61. #define SR_OUT 0x10 /* Shift out if 1 */
  62. /* Bits in IFR and IER */
  63. #define IER_SET 0x80 /* set bits in IER */
  64. #define IER_CLR 0 /* clear bits in IER */
  65. #define SR_INT 0x04 /* Shift register full/empty */
  66. /* ADB transaction states according to GMHW */
  67. #define ST_CMD 0x00 /* ADB state: command byte */
  68. #define ST_EVEN 0x10 /* ADB state: even data byte */
  69. #define ST_ODD 0x20 /* ADB state: odd data byte */
  70. #define ST_IDLE 0x30 /* ADB state: idle, nothing to send */
  71. static int macii_init_via(void);
  72. static void macii_start(void);
  73. static irqreturn_t macii_interrupt(int irq, void *arg);
  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. } macii_state;
  97. static struct adb_request *current_req; /* first request struct in the queue */
  98. static struct adb_request *last_req; /* last request struct in the queue */
  99. static unsigned char reply_buf[16]; /* storage for autopolled replies */
  100. static unsigned char *reply_ptr; /* next byte in reply_buf or req->reply */
  101. static int reading_reply; /* store reply in reply_buf else req->reply */
  102. static int data_index; /* index of the next byte to send from req->data */
  103. static int reply_len; /* number of bytes received in reply_buf or req->reply */
  104. static int status; /* VIA's ADB status bits captured upon interrupt */
  105. static int last_status; /* status bits as at previous interrupt */
  106. static int srq_asserted; /* have to poll for the device that asserted it */
  107. static int command_byte; /* the most recent command byte transmitted */
  108. static int autopoll_devs; /* bits set are device addresses to be polled */
  109. /* Sanity check for request queue. Doesn't check for cycles. */
  110. static int request_is_queued(struct adb_request *req) {
  111. struct adb_request *cur;
  112. unsigned long flags;
  113. local_irq_save(flags);
  114. cur = current_req;
  115. while (cur) {
  116. if (cur == req) {
  117. local_irq_restore(flags);
  118. return 1;
  119. }
  120. cur = cur->next;
  121. }
  122. local_irq_restore(flags);
  123. return 0;
  124. }
  125. /* Check for MacII style ADB */
  126. static int macii_probe(void)
  127. {
  128. if (macintosh_config->adb_type != MAC_ADB_II) return -ENODEV;
  129. via = via1;
  130. printk("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
  131. return 0;
  132. }
  133. /* Initialize the driver */
  134. int macii_init(void)
  135. {
  136. unsigned long flags;
  137. int err;
  138. local_irq_save(flags);
  139. err = macii_init_via();
  140. if (err) goto out;
  141. err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB",
  142. macii_interrupt);
  143. if (err) goto out;
  144. macii_state = idle;
  145. out:
  146. local_irq_restore(flags);
  147. return err;
  148. }
  149. /* initialize the hardware */
  150. static int macii_init_via(void)
  151. {
  152. unsigned char x;
  153. /* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */
  154. via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
  155. /* Set up state: idle */
  156. via[B] |= ST_IDLE;
  157. last_status = via[B] & (ST_MASK|CTLR_IRQ);
  158. /* Shift register on input */
  159. via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
  160. /* Wipe any pending data and int */
  161. x = via[SR];
  162. return 0;
  163. }
  164. /* Send an ADB poll (Talk Register 0 command prepended to the request queue) */
  165. static void macii_queue_poll(void)
  166. {
  167. /* No point polling the active device as it will never assert SRQ, so
  168. * poll the next device in the autopoll list. This could leave us
  169. * stuck in a polling loop if an unprobed device is asserting SRQ.
  170. * In theory, that could only happen if a device was plugged in after
  171. * probing started. Unplugging it again will break the cycle.
  172. * (Simply polling the next higher device often ends up polling almost
  173. * every device (after wrapping around), which takes too long.)
  174. */
  175. int device_mask;
  176. int next_device;
  177. static struct adb_request req;
  178. if (!autopoll_devs) return;
  179. device_mask = (1 << (((command_byte & 0xF0) >> 4) + 1)) - 1;
  180. if (autopoll_devs & ~device_mask)
  181. next_device = ffs(autopoll_devs & ~device_mask) - 1;
  182. else
  183. next_device = ffs(autopoll_devs) - 1;
  184. BUG_ON(request_is_queued(&req));
  185. adb_request(&req, NULL, ADBREQ_NOSEND, 1,
  186. ADB_READREG(next_device, 0));
  187. req.sent = 0;
  188. req.complete = 0;
  189. req.reply_len = 0;
  190. req.next = current_req;
  191. if (current_req != NULL) {
  192. current_req = &req;
  193. } else {
  194. current_req = &req;
  195. last_req = &req;
  196. }
  197. }
  198. /* Send an ADB request; if sync, poll out the reply 'till it's done */
  199. static int macii_send_request(struct adb_request *req, int sync)
  200. {
  201. int err;
  202. unsigned long flags;
  203. BUG_ON(request_is_queued(req));
  204. local_irq_save(flags);
  205. err = macii_write(req);
  206. local_irq_restore(flags);
  207. if (!err && sync) {
  208. while (!req->complete) {
  209. macii_poll();
  210. }
  211. BUG_ON(request_is_queued(req));
  212. }
  213. return err;
  214. }
  215. /* Send an ADB request (append to request queue) */
  216. static int macii_write(struct adb_request *req)
  217. {
  218. if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
  219. req->complete = 1;
  220. return -EINVAL;
  221. }
  222. req->next = NULL;
  223. req->sent = 0;
  224. req->complete = 0;
  225. req->reply_len = 0;
  226. if (current_req != NULL) {
  227. last_req->next = req;
  228. last_req = req;
  229. } else {
  230. current_req = req;
  231. last_req = req;
  232. if (macii_state == idle) macii_start();
  233. }
  234. return 0;
  235. }
  236. /* Start auto-polling */
  237. static int macii_autopoll(int devs)
  238. {
  239. static struct adb_request req;
  240. unsigned long flags;
  241. int err = 0;
  242. /* bit 1 == device 1, and so on. */
  243. autopoll_devs = devs & 0xFFFE;
  244. if (!autopoll_devs) return 0;
  245. local_irq_save(flags);
  246. if (current_req == NULL) {
  247. /* Send a Talk Reg 0. The controller will repeatedly transmit
  248. * this as long as it is idle.
  249. */
  250. adb_request(&req, NULL, ADBREQ_NOSEND, 1,
  251. ADB_READREG(ffs(autopoll_devs) - 1, 0));
  252. err = macii_write(&req);
  253. }
  254. local_irq_restore(flags);
  255. return err;
  256. }
  257. static inline int need_autopoll(void) {
  258. /* Was the last command Talk Reg 0
  259. * and is the target on the autopoll list?
  260. */
  261. if ((command_byte & 0x0F) == 0x0C &&
  262. ((1 << ((command_byte & 0xF0) >> 4)) & autopoll_devs))
  263. return 0;
  264. return 1;
  265. }
  266. /* Prod the chip without interrupts */
  267. static void macii_poll(void)
  268. {
  269. disable_irq(IRQ_MAC_ADB);
  270. macii_interrupt(0, NULL);
  271. enable_irq(IRQ_MAC_ADB);
  272. }
  273. /* Reset the bus */
  274. static int macii_reset_bus(void)
  275. {
  276. static struct adb_request req;
  277. if (request_is_queued(&req))
  278. return 0;
  279. /* Command = 0, Address = ignored */
  280. adb_request(&req, NULL, 0, 1, ADB_BUSRESET);
  281. /* Don't want any more requests during the Global Reset low time. */
  282. udelay(3000);
  283. return 0;
  284. }
  285. /* Start sending ADB packet */
  286. static void macii_start(void)
  287. {
  288. struct adb_request *req;
  289. req = current_req;
  290. BUG_ON(req == NULL);
  291. BUG_ON(macii_state != idle);
  292. /* Now send it. Be careful though, that first byte of the request
  293. * is actually ADB_PACKET; the real data begins at index 1!
  294. * And req->nbytes is the number of bytes of real data plus one.
  295. */
  296. /* store command byte */
  297. command_byte = req->data[1];
  298. /* Output mode */
  299. via[ACR] |= SR_OUT;
  300. /* Load data */
  301. via[SR] = req->data[1];
  302. /* set ADB state to 'command' */
  303. via[B] = (via[B] & ~ST_MASK) | ST_CMD;
  304. macii_state = sending;
  305. data_index = 2;
  306. }
  307. /*
  308. * The notorious ADB interrupt handler - does all of the protocol handling.
  309. * Relies on the ADB controller sending and receiving data, thereby
  310. * generating shift register interrupts (SR_INT) for us. This means there has
  311. * to be activity on the ADB bus. The chip will poll to achieve this.
  312. *
  313. * The basic ADB state machine was left unchanged from the original MacII code
  314. * by Alan Cox, which was based on the CUDA driver for PowerMac.
  315. * The syntax of the ADB status lines is totally different on MacII,
  316. * though. MacII uses the states Command -> Even -> Odd -> Even ->...-> Idle
  317. * for sending and Idle -> Even -> Odd -> Even ->...-> Idle for receiving.
  318. * Start and end of a receive packet are signalled by asserting /IRQ on the
  319. * interrupt line (/IRQ means the CTLR_IRQ bit in port B; not to be confused
  320. * with the VIA shift register interrupt. /IRQ never actually interrupts the
  321. * processor, it's just an ordinary input.)
  322. */
  323. static irqreturn_t macii_interrupt(int irq, void *arg)
  324. {
  325. int x;
  326. static int entered;
  327. struct adb_request *req;
  328. if (!arg) {
  329. /* Clear the SR IRQ flag when polling. */
  330. if (via[IFR] & SR_INT)
  331. via[IFR] = SR_INT;
  332. else
  333. return IRQ_NONE;
  334. }
  335. BUG_ON(entered++);
  336. last_status = status;
  337. status = via[B] & (ST_MASK|CTLR_IRQ);
  338. switch (macii_state) {
  339. case idle:
  340. if (reading_reply) {
  341. reply_ptr = current_req->reply;
  342. } else {
  343. BUG_ON(current_req != NULL);
  344. reply_ptr = reply_buf;
  345. }
  346. x = via[SR];
  347. if ((status & CTLR_IRQ) && (x == 0xFF)) {
  348. /* Bus timeout without SRQ sequence:
  349. * data is "FF" while CTLR_IRQ is "H"
  350. */
  351. reply_len = 0;
  352. srq_asserted = 0;
  353. macii_state = read_done;
  354. } else {
  355. macii_state = reading;
  356. *reply_ptr = x;
  357. reply_len = 1;
  358. }
  359. /* set ADB state = even for first data byte */
  360. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  361. break;
  362. case sending:
  363. req = current_req;
  364. if (data_index >= req->nbytes) {
  365. req->sent = 1;
  366. macii_state = idle;
  367. if (req->reply_expected) {
  368. reading_reply = 1;
  369. } else {
  370. req->complete = 1;
  371. current_req = req->next;
  372. if (req->done) (*req->done)(req);
  373. if (current_req)
  374. macii_start();
  375. else
  376. if (need_autopoll())
  377. macii_autopoll(autopoll_devs);
  378. }
  379. if (macii_state == idle) {
  380. /* reset to shift in */
  381. via[ACR] &= ~SR_OUT;
  382. x = via[SR];
  383. /* set ADB state idle - might get SRQ */
  384. via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
  385. }
  386. } else {
  387. via[SR] = req->data[data_index++];
  388. if ( (via[B] & ST_MASK) == ST_CMD ) {
  389. /* just sent the command byte, set to EVEN */
  390. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  391. } else {
  392. /* invert state bits, toggle ODD/EVEN */
  393. via[B] ^= ST_MASK;
  394. }
  395. }
  396. break;
  397. case reading:
  398. x = via[SR];
  399. BUG_ON((status & ST_MASK) == ST_CMD ||
  400. (status & ST_MASK) == ST_IDLE);
  401. /* Bus timeout with SRQ sequence:
  402. * data is "XX FF" while CTLR_IRQ is "L L"
  403. * End of packet without SRQ sequence:
  404. * data is "XX...YY 00" while CTLR_IRQ is "L...H L"
  405. * End of packet SRQ sequence:
  406. * data is "XX...YY 00" while CTLR_IRQ is "L...L L"
  407. * (where XX is the first response byte and
  408. * YY is the last byte of valid response data.)
  409. */
  410. srq_asserted = 0;
  411. if (!(status & CTLR_IRQ)) {
  412. if (x == 0xFF) {
  413. if (!(last_status & CTLR_IRQ)) {
  414. macii_state = read_done;
  415. reply_len = 0;
  416. srq_asserted = 1;
  417. }
  418. } else if (x == 0x00) {
  419. macii_state = read_done;
  420. if (!(last_status & CTLR_IRQ))
  421. srq_asserted = 1;
  422. }
  423. }
  424. if (macii_state == reading) {
  425. BUG_ON(reply_len > 15);
  426. reply_ptr++;
  427. *reply_ptr = x;
  428. reply_len++;
  429. }
  430. /* invert state bits, toggle ODD/EVEN */
  431. via[B] ^= ST_MASK;
  432. break;
  433. case read_done:
  434. x = via[SR];
  435. if (reading_reply) {
  436. reading_reply = 0;
  437. req = current_req;
  438. req->reply_len = reply_len;
  439. req->complete = 1;
  440. current_req = req->next;
  441. if (req->done) (*req->done)(req);
  442. } else if (reply_len && autopoll_devs)
  443. adb_input(reply_buf, reply_len, 0);
  444. macii_state = idle;
  445. /* SRQ seen before, initiate poll now */
  446. if (srq_asserted)
  447. macii_queue_poll();
  448. if (current_req)
  449. macii_start();
  450. else
  451. if (need_autopoll())
  452. macii_autopoll(autopoll_devs);
  453. if (macii_state == idle)
  454. via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
  455. break;
  456. default:
  457. break;
  458. }
  459. entered--;
  460. return IRQ_HANDLED;
  461. }