via-macii.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Device driver for the via ADB on (many) Mac II-class machines
  4. *
  5. * Based on the original ADB keyboard handler Copyright (c) 1997 Alan Cox
  6. * Also derived from code Copyright (C) 1996 Paul Mackerras.
  7. *
  8. * With various updates provided over the years by Michael Schmitz,
  9. * Guideo Koerber and others.
  10. *
  11. * Rewrite for Unified ADB by Joshua M. Thompson (funaho@jurai.org)
  12. *
  13. * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
  14. * 2000-03-29 Tony Mantler <tonym@mac.linux-m68k.org>
  15. * - Big overhaul, should actually work now.
  16. * 2006-12-31 Finn Thain - Another overhaul.
  17. *
  18. * Suggested reading:
  19. * Inside Macintosh, ch. 5 ADB Manager
  20. * Guide to the Macinstosh Family Hardware, ch. 8 Apple Desktop Bus
  21. * Rockwell R6522 VIA datasheet
  22. *
  23. * Apple's "ADB Analyzer" bus sniffer is invaluable:
  24. * ftp://ftp.apple.com/developer/Tool_Chest/Devices_-_Hardware/Apple_Desktop_Bus/
  25. */
  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. /* ADB command byte structure */
  72. #define ADDR_MASK 0xF0
  73. #define CMD_MASK 0x0F
  74. #define OP_MASK 0x0C
  75. #define TALK 0x0C
  76. static int macii_init_via(void);
  77. static void macii_start(void);
  78. static irqreturn_t macii_interrupt(int irq, void *arg);
  79. static void macii_queue_poll(void);
  80. static int macii_probe(void);
  81. static int macii_init(void);
  82. static int macii_send_request(struct adb_request *req, int sync);
  83. static int macii_write(struct adb_request *req);
  84. static int macii_autopoll(int devs);
  85. static void macii_poll(void);
  86. static int macii_reset_bus(void);
  87. struct adb_driver via_macii_driver = {
  88. .name = "Mac II",
  89. .probe = macii_probe,
  90. .init = macii_init,
  91. .send_request = macii_send_request,
  92. .autopoll = macii_autopoll,
  93. .poll = macii_poll,
  94. .reset_bus = macii_reset_bus,
  95. };
  96. static enum macii_state {
  97. idle,
  98. sending,
  99. reading,
  100. } macii_state;
  101. static struct adb_request *current_req; /* first request struct in the queue */
  102. static struct adb_request *last_req; /* last request struct in the queue */
  103. static unsigned char reply_buf[16]; /* storage for autopolled replies */
  104. static unsigned char *reply_ptr; /* next byte in reply_buf or req->reply */
  105. static bool reading_reply; /* store reply in reply_buf else req->reply */
  106. static int data_index; /* index of the next byte to send from req->data */
  107. static int reply_len; /* number of bytes received in reply_buf or req->reply */
  108. static int status; /* VIA's ADB status bits captured upon interrupt */
  109. static bool bus_timeout; /* no data was sent by the device */
  110. static bool srq_asserted; /* have to poll for the device that asserted it */
  111. static u8 last_cmd; /* the most recent command byte transmitted */
  112. static u8 last_talk_cmd; /* the most recent Talk command byte transmitted */
  113. static u8 last_poll_cmd; /* the most recent Talk R0 command byte transmitted */
  114. static unsigned int autopoll_devs; /* bits set are device addresses to poll */
  115. /* Check for MacII style ADB */
  116. static int macii_probe(void)
  117. {
  118. if (macintosh_config->adb_type != MAC_ADB_II)
  119. return -ENODEV;
  120. via = via1;
  121. pr_info("adb: Mac II ADB Driver v1.0 for Unified ADB\n");
  122. return 0;
  123. }
  124. /* Initialize the driver */
  125. static int macii_init(void)
  126. {
  127. int err;
  128. err = macii_init_via();
  129. if (err)
  130. return err;
  131. err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB",
  132. macii_interrupt);
  133. if (err)
  134. return err;
  135. macii_state = idle;
  136. return 0;
  137. }
  138. /* initialize the hardware */
  139. static int macii_init_via(void)
  140. {
  141. unsigned char x;
  142. /* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */
  143. via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
  144. /* Set up state: idle */
  145. via[B] |= ST_IDLE;
  146. /* Shift register on input */
  147. via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
  148. /* Wipe any pending data and int */
  149. x = via[SR];
  150. return 0;
  151. }
  152. /* Send an ADB poll (Talk Register 0 command prepended to the request queue) */
  153. static void macii_queue_poll(void)
  154. {
  155. static struct adb_request req;
  156. unsigned char poll_command;
  157. unsigned int poll_addr;
  158. /* This only polls devices in the autopoll list, which assumes that
  159. * unprobed devices never assert SRQ. That could happen if a device was
  160. * plugged in after the adb bus scan. Unplugging it again will resolve
  161. * the problem. This behaviour is similar to MacOS.
  162. */
  163. if (!autopoll_devs)
  164. return;
  165. /* The device most recently polled may not be the best device to poll
  166. * right now. Some other device(s) may have signalled SRQ (the active
  167. * device won't do that). Or the autopoll list may have been changed.
  168. * Try polling the next higher address.
  169. */
  170. poll_addr = (last_poll_cmd & ADDR_MASK) >> 4;
  171. if ((srq_asserted && last_cmd == last_poll_cmd) ||
  172. !(autopoll_devs & (1 << poll_addr))) {
  173. unsigned int higher_devs;
  174. higher_devs = autopoll_devs & -(1 << (poll_addr + 1));
  175. poll_addr = ffs(higher_devs ? higher_devs : autopoll_devs) - 1;
  176. }
  177. /* Send a Talk Register 0 command */
  178. poll_command = ADB_READREG(poll_addr, 0);
  179. /* No need to repeat this Talk command. The transceiver will do that
  180. * as long as it is idle.
  181. */
  182. if (poll_command == last_cmd)
  183. return;
  184. adb_request(&req, NULL, ADBREQ_NOSEND, 1, poll_command);
  185. req.sent = 0;
  186. req.complete = 0;
  187. req.reply_len = 0;
  188. req.next = current_req;
  189. if (WARN_ON(current_req)) {
  190. current_req = &req;
  191. } else {
  192. current_req = &req;
  193. last_req = &req;
  194. }
  195. }
  196. /* Send an ADB request; if sync, poll out the reply 'till it's done */
  197. static int macii_send_request(struct adb_request *req, int sync)
  198. {
  199. int err;
  200. err = macii_write(req);
  201. if (err)
  202. return err;
  203. if (sync)
  204. while (!req->complete)
  205. macii_poll();
  206. return 0;
  207. }
  208. /* Send an ADB request (append to request queue) */
  209. static int macii_write(struct adb_request *req)
  210. {
  211. unsigned long flags;
  212. if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
  213. req->complete = 1;
  214. return -EINVAL;
  215. }
  216. req->next = NULL;
  217. req->sent = 0;
  218. req->complete = 0;
  219. req->reply_len = 0;
  220. local_irq_save(flags);
  221. if (current_req != NULL) {
  222. last_req->next = req;
  223. last_req = req;
  224. } else {
  225. current_req = req;
  226. last_req = req;
  227. if (macii_state == idle)
  228. macii_start();
  229. }
  230. local_irq_restore(flags);
  231. return 0;
  232. }
  233. /* Start auto-polling */
  234. static int macii_autopoll(int devs)
  235. {
  236. unsigned long flags;
  237. local_irq_save(flags);
  238. /* bit 1 == device 1, and so on. */
  239. autopoll_devs = (unsigned int)devs & 0xFFFE;
  240. if (!current_req) {
  241. macii_queue_poll();
  242. if (current_req && macii_state == idle)
  243. macii_start();
  244. }
  245. local_irq_restore(flags);
  246. return 0;
  247. }
  248. /* Prod the chip without interrupts */
  249. static void macii_poll(void)
  250. {
  251. macii_interrupt(0, NULL);
  252. }
  253. /* Reset the bus */
  254. static int macii_reset_bus(void)
  255. {
  256. struct adb_request req;
  257. /* Command = 0, Address = ignored */
  258. adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
  259. macii_send_request(&req, 1);
  260. /* Don't want any more requests during the Global Reset low time. */
  261. udelay(3000);
  262. return 0;
  263. }
  264. /* Start sending ADB packet */
  265. static void macii_start(void)
  266. {
  267. struct adb_request *req;
  268. req = current_req;
  269. /* Now send it. Be careful though, that first byte of the request
  270. * is actually ADB_PACKET; the real data begins at index 1!
  271. * And req->nbytes is the number of bytes of real data plus one.
  272. */
  273. /* Output mode */
  274. via[ACR] |= SR_OUT;
  275. /* Load data */
  276. via[SR] = req->data[1];
  277. /* set ADB state to 'command' */
  278. via[B] = (via[B] & ~ST_MASK) | ST_CMD;
  279. macii_state = sending;
  280. data_index = 2;
  281. bus_timeout = false;
  282. srq_asserted = false;
  283. }
  284. /*
  285. * The notorious ADB interrupt handler - does all of the protocol handling.
  286. * Relies on the ADB controller sending and receiving data, thereby
  287. * generating shift register interrupts (SR_INT) for us. This means there has
  288. * to be activity on the ADB bus. The chip will poll to achieve this.
  289. *
  290. * The VIA Port B output signalling works as follows. After the ADB transceiver
  291. * sees a transition on the PB4 and PB5 lines it will crank over the VIA shift
  292. * register which eventually raises the SR_INT interrupt. The PB4/PB5 outputs
  293. * are toggled with each byte as the ADB transaction progresses.
  294. *
  295. * Request with no reply expected (and empty transceiver buffer):
  296. * CMD -> IDLE
  297. * Request with expected reply packet (or with buffered autopoll packet):
  298. * CMD -> EVEN -> ODD -> EVEN -> ... -> IDLE
  299. * Unsolicited packet:
  300. * IDLE -> EVEN -> ODD -> EVEN -> ... -> IDLE
  301. */
  302. static irqreturn_t macii_interrupt(int irq, void *arg)
  303. {
  304. int x;
  305. struct adb_request *req;
  306. unsigned long flags;
  307. local_irq_save(flags);
  308. if (!arg) {
  309. /* Clear the SR IRQ flag when polling. */
  310. if (via[IFR] & SR_INT)
  311. via[IFR] = SR_INT;
  312. else {
  313. local_irq_restore(flags);
  314. return IRQ_NONE;
  315. }
  316. }
  317. status = via[B] & (ST_MASK | CTLR_IRQ);
  318. switch (macii_state) {
  319. case idle:
  320. WARN_ON((status & ST_MASK) != ST_IDLE);
  321. reply_ptr = reply_buf;
  322. reading_reply = false;
  323. bus_timeout = false;
  324. srq_asserted = false;
  325. x = via[SR];
  326. if (!(status & CTLR_IRQ)) {
  327. /* /CTLR_IRQ asserted in idle state means we must
  328. * read an autopoll reply from the transceiver buffer.
  329. */
  330. macii_state = reading;
  331. *reply_ptr = x;
  332. reply_len = 1;
  333. } else {
  334. /* bus timeout */
  335. reply_len = 0;
  336. break;
  337. }
  338. /* set ADB state = even for first data byte */
  339. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  340. break;
  341. case sending:
  342. req = current_req;
  343. if (status == (ST_CMD | CTLR_IRQ)) {
  344. /* /CTLR_IRQ de-asserted after the command byte means
  345. * the host can continue with the transaction.
  346. */
  347. /* Store command byte */
  348. last_cmd = req->data[1];
  349. if ((last_cmd & OP_MASK) == TALK) {
  350. last_talk_cmd = last_cmd;
  351. if ((last_cmd & CMD_MASK) == ADB_READREG(0, 0))
  352. last_poll_cmd = last_cmd;
  353. }
  354. }
  355. if (status == ST_CMD) {
  356. /* /CTLR_IRQ asserted after the command byte means we
  357. * must read an autopoll reply. The first byte was
  358. * lost because the shift register was an output.
  359. */
  360. macii_state = reading;
  361. reading_reply = false;
  362. reply_ptr = reply_buf;
  363. *reply_ptr = last_talk_cmd;
  364. reply_len = 1;
  365. /* reset to shift in */
  366. via[ACR] &= ~SR_OUT;
  367. x = via[SR];
  368. } else if (data_index >= req->nbytes) {
  369. req->sent = 1;
  370. if (req->reply_expected) {
  371. macii_state = reading;
  372. reading_reply = true;
  373. reply_ptr = req->reply;
  374. *reply_ptr = req->data[1];
  375. reply_len = 1;
  376. via[ACR] &= ~SR_OUT;
  377. x = via[SR];
  378. } else if ((req->data[1] & OP_MASK) == TALK) {
  379. macii_state = reading;
  380. reading_reply = false;
  381. reply_ptr = reply_buf;
  382. *reply_ptr = req->data[1];
  383. reply_len = 1;
  384. via[ACR] &= ~SR_OUT;
  385. x = via[SR];
  386. req->complete = 1;
  387. current_req = req->next;
  388. if (req->done)
  389. (*req->done)(req);
  390. } else {
  391. macii_state = idle;
  392. req->complete = 1;
  393. current_req = req->next;
  394. if (req->done)
  395. (*req->done)(req);
  396. break;
  397. }
  398. } else {
  399. via[SR] = req->data[data_index++];
  400. }
  401. if ((via[B] & ST_MASK) == ST_CMD) {
  402. /* just sent the command byte, set to EVEN */
  403. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  404. } else {
  405. /* invert state bits, toggle ODD/EVEN */
  406. via[B] ^= ST_MASK;
  407. }
  408. break;
  409. case reading:
  410. x = via[SR];
  411. WARN_ON((status & ST_MASK) == ST_CMD ||
  412. (status & ST_MASK) == ST_IDLE);
  413. if (!(status & CTLR_IRQ)) {
  414. if (status == ST_EVEN && reply_len == 1) {
  415. bus_timeout = true;
  416. } else if (status == ST_ODD && reply_len == 2) {
  417. srq_asserted = true;
  418. } else {
  419. macii_state = idle;
  420. if (bus_timeout)
  421. reply_len = 0;
  422. if (reading_reply) {
  423. struct adb_request *req = current_req;
  424. req->reply_len = reply_len;
  425. req->complete = 1;
  426. current_req = req->next;
  427. if (req->done)
  428. (*req->done)(req);
  429. } else if (reply_len && autopoll_devs &&
  430. reply_buf[0] == last_poll_cmd) {
  431. adb_input(reply_buf, reply_len, 1);
  432. }
  433. break;
  434. }
  435. }
  436. if (reply_len < ARRAY_SIZE(reply_buf)) {
  437. reply_ptr++;
  438. *reply_ptr = x;
  439. reply_len++;
  440. }
  441. /* invert state bits, toggle ODD/EVEN */
  442. via[B] ^= ST_MASK;
  443. break;
  444. default:
  445. break;
  446. }
  447. if (macii_state == idle) {
  448. if (!current_req)
  449. macii_queue_poll();
  450. if (current_req)
  451. macii_start();
  452. if (macii_state == idle) {
  453. via[ACR] &= ~SR_OUT;
  454. x = via[SR];
  455. via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
  456. }
  457. }
  458. local_irq_restore(flags);
  459. return IRQ_HANDLED;
  460. }