via-macii.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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 ([email protected])
  12. *
  13. * 1999-08-02 (jmt) - Initial rewrite for Unified ADB.
  14. * 2000-03-29 Tony Mantler <[email protected]>
  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. unsigned long flags;
  128. int err;
  129. local_irq_save(flags);
  130. err = macii_init_via();
  131. if (err)
  132. goto out;
  133. err = request_irq(IRQ_MAC_ADB, macii_interrupt, 0, "ADB",
  134. macii_interrupt);
  135. if (err)
  136. goto out;
  137. macii_state = idle;
  138. out:
  139. local_irq_restore(flags);
  140. return err;
  141. }
  142. /* initialize the hardware */
  143. static int macii_init_via(void)
  144. {
  145. unsigned char x;
  146. /* We want CTLR_IRQ as input and ST_EVEN | ST_ODD as output lines. */
  147. via[DIRB] = (via[DIRB] | ST_EVEN | ST_ODD) & ~CTLR_IRQ;
  148. /* Set up state: idle */
  149. via[B] |= ST_IDLE;
  150. /* Shift register on input */
  151. via[ACR] = (via[ACR] & ~SR_CTRL) | SR_EXT;
  152. /* Wipe any pending data and int */
  153. x = via[SR];
  154. return 0;
  155. }
  156. /* Send an ADB poll (Talk Register 0 command prepended to the request queue) */
  157. static void macii_queue_poll(void)
  158. {
  159. static struct adb_request req;
  160. unsigned char poll_command;
  161. unsigned int poll_addr;
  162. /* This only polls devices in the autopoll list, which assumes that
  163. * unprobed devices never assert SRQ. That could happen if a device was
  164. * plugged in after the adb bus scan. Unplugging it again will resolve
  165. * the problem. This behaviour is similar to MacOS.
  166. */
  167. if (!autopoll_devs)
  168. return;
  169. /* The device most recently polled may not be the best device to poll
  170. * right now. Some other device(s) may have signalled SRQ (the active
  171. * device won't do that). Or the autopoll list may have been changed.
  172. * Try polling the next higher address.
  173. */
  174. poll_addr = (last_poll_cmd & ADDR_MASK) >> 4;
  175. if ((srq_asserted && last_cmd == last_poll_cmd) ||
  176. !(autopoll_devs & (1 << poll_addr))) {
  177. unsigned int higher_devs;
  178. higher_devs = autopoll_devs & -(1 << (poll_addr + 1));
  179. poll_addr = ffs(higher_devs ? higher_devs : autopoll_devs) - 1;
  180. }
  181. /* Send a Talk Register 0 command */
  182. poll_command = ADB_READREG(poll_addr, 0);
  183. /* No need to repeat this Talk command. The transceiver will do that
  184. * as long as it is idle.
  185. */
  186. if (poll_command == last_cmd)
  187. return;
  188. adb_request(&req, NULL, ADBREQ_NOSEND, 1, poll_command);
  189. req.sent = 0;
  190. req.complete = 0;
  191. req.reply_len = 0;
  192. req.next = current_req;
  193. if (WARN_ON(current_req)) {
  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. err = macii_write(req);
  205. if (err)
  206. return err;
  207. if (sync)
  208. while (!req->complete)
  209. macii_poll();
  210. return 0;
  211. }
  212. /* Send an ADB request (append to request queue) */
  213. static int macii_write(struct adb_request *req)
  214. {
  215. unsigned long flags;
  216. if (req->nbytes < 2 || req->data[0] != ADB_PACKET || req->nbytes > 15) {
  217. req->complete = 1;
  218. return -EINVAL;
  219. }
  220. req->next = NULL;
  221. req->sent = 0;
  222. req->complete = 0;
  223. req->reply_len = 0;
  224. local_irq_save(flags);
  225. if (current_req != NULL) {
  226. last_req->next = req;
  227. last_req = req;
  228. } else {
  229. current_req = req;
  230. last_req = req;
  231. if (macii_state == idle)
  232. macii_start();
  233. }
  234. local_irq_restore(flags);
  235. return 0;
  236. }
  237. /* Start auto-polling */
  238. static int macii_autopoll(int devs)
  239. {
  240. unsigned long flags;
  241. local_irq_save(flags);
  242. /* bit 1 == device 1, and so on. */
  243. autopoll_devs = (unsigned int)devs & 0xFFFE;
  244. if (!current_req) {
  245. macii_queue_poll();
  246. if (current_req && macii_state == idle)
  247. macii_start();
  248. }
  249. local_irq_restore(flags);
  250. return 0;
  251. }
  252. /* Prod the chip without interrupts */
  253. static void macii_poll(void)
  254. {
  255. macii_interrupt(0, NULL);
  256. }
  257. /* Reset the bus */
  258. static int macii_reset_bus(void)
  259. {
  260. struct adb_request req;
  261. /* Command = 0, Address = ignored */
  262. adb_request(&req, NULL, ADBREQ_NOSEND, 1, ADB_BUSRESET);
  263. macii_send_request(&req, 1);
  264. /* Don't want any more requests during the Global Reset low time. */
  265. udelay(3000);
  266. return 0;
  267. }
  268. /* Start sending ADB packet */
  269. static void macii_start(void)
  270. {
  271. struct adb_request *req;
  272. req = current_req;
  273. /* Now send it. Be careful though, that first byte of the request
  274. * is actually ADB_PACKET; the real data begins at index 1!
  275. * And req->nbytes is the number of bytes of real data plus one.
  276. */
  277. /* Output mode */
  278. via[ACR] |= SR_OUT;
  279. /* Load data */
  280. via[SR] = req->data[1];
  281. /* set ADB state to 'command' */
  282. via[B] = (via[B] & ~ST_MASK) | ST_CMD;
  283. macii_state = sending;
  284. data_index = 2;
  285. bus_timeout = false;
  286. srq_asserted = false;
  287. }
  288. /*
  289. * The notorious ADB interrupt handler - does all of the protocol handling.
  290. * Relies on the ADB controller sending and receiving data, thereby
  291. * generating shift register interrupts (SR_INT) for us. This means there has
  292. * to be activity on the ADB bus. The chip will poll to achieve this.
  293. *
  294. * The VIA Port B output signalling works as follows. After the ADB transceiver
  295. * sees a transition on the PB4 and PB5 lines it will crank over the VIA shift
  296. * register which eventually raises the SR_INT interrupt. The PB4/PB5 outputs
  297. * are toggled with each byte as the ADB transaction progresses.
  298. *
  299. * Request with no reply expected (and empty transceiver buffer):
  300. * CMD -> IDLE
  301. * Request with expected reply packet (or with buffered autopoll packet):
  302. * CMD -> EVEN -> ODD -> EVEN -> ... -> IDLE
  303. * Unsolicited packet:
  304. * IDLE -> EVEN -> ODD -> EVEN -> ... -> IDLE
  305. */
  306. static irqreturn_t macii_interrupt(int irq, void *arg)
  307. {
  308. int x;
  309. struct adb_request *req;
  310. unsigned long flags;
  311. local_irq_save(flags);
  312. if (!arg) {
  313. /* Clear the SR IRQ flag when polling. */
  314. if (via[IFR] & SR_INT)
  315. via[IFR] = SR_INT;
  316. else {
  317. local_irq_restore(flags);
  318. return IRQ_NONE;
  319. }
  320. }
  321. status = via[B] & (ST_MASK | CTLR_IRQ);
  322. switch (macii_state) {
  323. case idle:
  324. WARN_ON((status & ST_MASK) != ST_IDLE);
  325. reply_ptr = reply_buf;
  326. reading_reply = false;
  327. bus_timeout = false;
  328. srq_asserted = false;
  329. x = via[SR];
  330. if (!(status & CTLR_IRQ)) {
  331. /* /CTLR_IRQ asserted in idle state means we must
  332. * read an autopoll reply from the transceiver buffer.
  333. */
  334. macii_state = reading;
  335. *reply_ptr = x;
  336. reply_len = 1;
  337. } else {
  338. /* bus timeout */
  339. reply_len = 0;
  340. break;
  341. }
  342. /* set ADB state = even for first data byte */
  343. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  344. break;
  345. case sending:
  346. req = current_req;
  347. if (status == (ST_CMD | CTLR_IRQ)) {
  348. /* /CTLR_IRQ de-asserted after the command byte means
  349. * the host can continue with the transaction.
  350. */
  351. /* Store command byte */
  352. last_cmd = req->data[1];
  353. if ((last_cmd & OP_MASK) == TALK) {
  354. last_talk_cmd = last_cmd;
  355. if ((last_cmd & CMD_MASK) == ADB_READREG(0, 0))
  356. last_poll_cmd = last_cmd;
  357. }
  358. }
  359. if (status == ST_CMD) {
  360. /* /CTLR_IRQ asserted after the command byte means we
  361. * must read an autopoll reply. The first byte was
  362. * lost because the shift register was an output.
  363. */
  364. macii_state = reading;
  365. reading_reply = false;
  366. reply_ptr = reply_buf;
  367. *reply_ptr = last_talk_cmd;
  368. reply_len = 1;
  369. /* reset to shift in */
  370. via[ACR] &= ~SR_OUT;
  371. x = via[SR];
  372. } else if (data_index >= req->nbytes) {
  373. req->sent = 1;
  374. if (req->reply_expected) {
  375. macii_state = reading;
  376. reading_reply = true;
  377. reply_ptr = req->reply;
  378. *reply_ptr = req->data[1];
  379. reply_len = 1;
  380. via[ACR] &= ~SR_OUT;
  381. x = via[SR];
  382. } else if ((req->data[1] & OP_MASK) == TALK) {
  383. macii_state = reading;
  384. reading_reply = false;
  385. reply_ptr = reply_buf;
  386. *reply_ptr = req->data[1];
  387. reply_len = 1;
  388. via[ACR] &= ~SR_OUT;
  389. x = via[SR];
  390. req->complete = 1;
  391. current_req = req->next;
  392. if (req->done)
  393. (*req->done)(req);
  394. } else {
  395. macii_state = idle;
  396. req->complete = 1;
  397. current_req = req->next;
  398. if (req->done)
  399. (*req->done)(req);
  400. break;
  401. }
  402. } else {
  403. via[SR] = req->data[data_index++];
  404. }
  405. if ((via[B] & ST_MASK) == ST_CMD) {
  406. /* just sent the command byte, set to EVEN */
  407. via[B] = (via[B] & ~ST_MASK) | ST_EVEN;
  408. } else {
  409. /* invert state bits, toggle ODD/EVEN */
  410. via[B] ^= ST_MASK;
  411. }
  412. break;
  413. case reading:
  414. x = via[SR];
  415. WARN_ON((status & ST_MASK) == ST_CMD ||
  416. (status & ST_MASK) == ST_IDLE);
  417. if (!(status & CTLR_IRQ)) {
  418. if (status == ST_EVEN && reply_len == 1) {
  419. bus_timeout = true;
  420. } else if (status == ST_ODD && reply_len == 2) {
  421. srq_asserted = true;
  422. } else {
  423. macii_state = idle;
  424. if (bus_timeout)
  425. reply_len = 0;
  426. if (reading_reply) {
  427. struct adb_request *req = current_req;
  428. req->reply_len = reply_len;
  429. req->complete = 1;
  430. current_req = req->next;
  431. if (req->done)
  432. (*req->done)(req);
  433. } else if (reply_len && autopoll_devs &&
  434. reply_buf[0] == last_poll_cmd) {
  435. adb_input(reply_buf, reply_len, 1);
  436. }
  437. break;
  438. }
  439. }
  440. if (reply_len < ARRAY_SIZE(reply_buf)) {
  441. reply_ptr++;
  442. *reply_ptr = x;
  443. reply_len++;
  444. }
  445. /* invert state bits, toggle ODD/EVEN */
  446. via[B] ^= ST_MASK;
  447. break;
  448. default:
  449. break;
  450. }
  451. if (macii_state == idle) {
  452. if (!current_req)
  453. macii_queue_poll();
  454. if (current_req)
  455. macii_start();
  456. if (macii_state == idle) {
  457. via[ACR] &= ~SR_OUT;
  458. x = via[SR];
  459. via[B] = (via[B] & ~ST_MASK) | ST_IDLE;
  460. }
  461. }
  462. local_irq_restore(flags);
  463. return IRQ_HANDLED;
  464. }