via-macii.c 14 KB

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