via-macii.c 14 KB

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