blob: c60415958dfee96b961d1539bb7f3935a91de936 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Finn Thaind23eee82016-12-31 19:56:26 -05002 * Device driver for the Cuda and Egret system controllers found on PowerMacs
3 * and 68k Macs.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Finn Thaind23eee82016-12-31 19:56:26 -05005 * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA.
6 * This MCU controls system power, Parameter RAM, Real Time Clock and the
7 * Apple Desktop Bus (ADB) that connects to the keyboard and mouse.
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * Copyright (C) 1996 Paul Mackerras.
10 */
11#include <stdarg.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <linux/types.h>
13#include <linux/errno.h>
14#include <linux/kernel.h>
15#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/adb.h>
17#include <linux/cuda.h>
18#include <linux/spinlock.h>
19#include <linux/interrupt.h>
20#ifdef CONFIG_PPC
21#include <asm/prom.h>
22#include <asm/machdep.h>
23#else
24#include <asm/macintosh.h>
25#include <asm/macints.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include <asm/mac_via.h>
27#endif
28#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/init.h>
30
31static volatile unsigned char __iomem *via;
32static DEFINE_SPINLOCK(cuda_lock);
33
Linus Torvalds1da177e2005-04-16 15:20:36 -070034/* VIA registers - spaced 0x200 bytes apart */
35#define RS 0x200 /* skip between registers */
36#define B 0 /* B-side data */
37#define A RS /* A-side data */
38#define DIRB (2*RS) /* B-side direction (1=output) */
39#define DIRA (3*RS) /* A-side direction (1=output) */
40#define T1CL (4*RS) /* Timer 1 ctr/latch (low 8 bits) */
41#define T1CH (5*RS) /* Timer 1 counter (high 8 bits) */
42#define T1LL (6*RS) /* Timer 1 latch (low 8 bits) */
43#define T1LH (7*RS) /* Timer 1 latch (high 8 bits) */
44#define T2CL (8*RS) /* Timer 2 ctr/latch (low 8 bits) */
45#define T2CH (9*RS) /* Timer 2 counter (high 8 bits) */
46#define SR (10*RS) /* Shift register */
47#define ACR (11*RS) /* Auxiliary control register */
48#define PCR (12*RS) /* Peripheral control register */
49#define IFR (13*RS) /* Interrupt flag register */
50#define IER (14*RS) /* Interrupt enable register */
51#define ANH (15*RS) /* A-side data, no handshake */
52
Finn Thaind23eee82016-12-31 19:56:26 -050053/*
54 * When the Cuda design replaced the Egret, some signal names and
55 * logic sense changed. They all serve the same purposes, however.
56 *
57 * VIA pin | Egret pin
58 * ----------------+------------------------------------------
59 * PB3 (input) | Transceiver session (active low)
60 * PB4 (output) | VIA full (active high)
61 * PB5 (output) | System session (active high)
62 *
63 * VIA pin | Cuda pin
64 * ----------------+------------------------------------------
65 * PB3 (input) | Transfer request (active low)
66 * PB4 (output) | Byte acknowledge (active low)
67 * PB5 (output) | Transfer in progress (active low)
68 */
69
70/* Bits in Port B data register */
71#define TREQ 0x08 /* Transfer request */
72#define TACK 0x10 /* Transfer acknowledge */
73#define TIP 0x20 /* Transfer in progress */
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75/* Bits in ACR */
76#define SR_CTRL 0x1c /* Shift register control bits */
77#define SR_EXT 0x0c /* Shift on external clock */
78#define SR_OUT 0x10 /* Shift out if 1 */
79
80/* Bits in IFR and IER */
81#define IER_SET 0x80 /* set bits in IER */
82#define IER_CLR 0 /* clear bits in IER */
83#define SR_INT 0x04 /* Shift register full/empty */
84
Finn Thaind23eee82016-12-31 19:56:26 -050085/* Duration of byte acknowledgement pulse (us) */
86#define EGRET_TACK_ASSERTED_DELAY 300
87#define EGRET_TACK_NEGATED_DELAY 400
88
89/* Interval from interrupt to start of session (us) */
90#define EGRET_SESSION_DELAY 450
91
92#ifdef CONFIG_PPC
93#define mcu_is_egret false
94#else
95static bool mcu_is_egret;
96#endif
97
Finn Thainfd7a65a2016-12-31 19:56:26 -050098static inline bool TREQ_asserted(u8 portb)
99{
100 return !(portb & TREQ);
101}
102
103static inline void assert_TIP(void)
104{
Finn Thaind23eee82016-12-31 19:56:26 -0500105 if (mcu_is_egret) {
106 udelay(EGRET_SESSION_DELAY);
107 out_8(&via[B], in_8(&via[B]) | TIP);
108 } else
109 out_8(&via[B], in_8(&via[B]) & ~TIP);
110}
111
112static inline void assert_TIP_and_TACK(void)
113{
114 if (mcu_is_egret) {
115 udelay(EGRET_SESSION_DELAY);
116 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
117 } else
118 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
Finn Thainfd7a65a2016-12-31 19:56:26 -0500119}
120
121static inline void assert_TACK(void)
122{
Finn Thaind23eee82016-12-31 19:56:26 -0500123 if (mcu_is_egret) {
124 udelay(EGRET_TACK_NEGATED_DELAY);
125 out_8(&via[B], in_8(&via[B]) | TACK);
126 } else
127 out_8(&via[B], in_8(&via[B]) & ~TACK);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500128}
129
130static inline void toggle_TACK(void)
131{
132 out_8(&via[B], in_8(&via[B]) ^ TACK);
133}
134
135static inline void negate_TACK(void)
136{
Finn Thaind23eee82016-12-31 19:56:26 -0500137 if (mcu_is_egret) {
138 udelay(EGRET_TACK_ASSERTED_DELAY);
139 out_8(&via[B], in_8(&via[B]) & ~TACK);
140 } else
141 out_8(&via[B], in_8(&via[B]) | TACK);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500142}
143
144static inline void negate_TIP_and_TACK(void)
145{
Finn Thaind23eee82016-12-31 19:56:26 -0500146 if (mcu_is_egret) {
147 udelay(EGRET_TACK_ASSERTED_DELAY);
148 out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
149 } else
150 out_8(&via[B], in_8(&via[B]) | TIP | TACK);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500151}
152
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153static enum cuda_state {
154 idle,
155 sent_first_byte,
156 sending,
157 reading,
158 read_done,
159 awaiting_reply
160} cuda_state;
161
162static struct adb_request *current_req;
163static struct adb_request *last_req;
164static unsigned char cuda_rbuf[16];
165static unsigned char *reply_ptr;
166static int reading_reply;
167static int data_index;
Finn Thain0251c382007-05-01 22:33:00 +0200168static int cuda_irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169#ifdef CONFIG_PPC
170static struct device_node *vias;
171#endif
Olaf Hering87275852007-02-10 21:35:12 +0100172static int cuda_fully_inited;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174#ifdef CONFIG_ADB
175static int cuda_probe(void);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176static int cuda_send_request(struct adb_request *req, int sync);
177static int cuda_adb_autopoll(int devs);
178static int cuda_reset_adb_bus(void);
179#endif /* CONFIG_ADB */
180
181static int cuda_init_via(void);
182static void cuda_start(void);
David Howells7d12e782006-10-05 14:55:46 +0100183static irqreturn_t cuda_interrupt(int irq, void *arg);
184static void cuda_input(unsigned char *buf, int nb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185void cuda_poll(void);
186static int cuda_write(struct adb_request *req);
187
188int cuda_request(struct adb_request *req,
189 void (*done)(struct adb_request *), int nbytes, ...);
190
191#ifdef CONFIG_ADB
192struct adb_driver via_cuda_driver = {
Finn Thain18814ee2009-11-17 20:03:05 +1100193 .name = "CUDA",
194 .probe = cuda_probe,
195 .send_request = cuda_send_request,
196 .autopoll = cuda_adb_autopoll,
197 .poll = cuda_poll,
198 .reset_bus = cuda_reset_adb_bus,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199};
200#endif /* CONFIG_ADB */
201
Finn Thain18814ee2009-11-17 20:03:05 +1100202#ifdef CONFIG_MAC
203int __init find_via_cuda(void)
204{
205 struct adb_request req;
206 int err;
207
Finn Thainf74faec2016-12-31 19:56:26 -0500208 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
209 macintosh_config->adb_type != MAC_ADB_EGRET)
Finn Thain18814ee2009-11-17 20:03:05 +1100210 return 0;
211
212 via = via1;
213 cuda_state = idle;
Finn Thainf74faec2016-12-31 19:56:26 -0500214 mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
Finn Thain18814ee2009-11-17 20:03:05 +1100215
216 err = cuda_init_via();
217 if (err) {
218 printk(KERN_ERR "cuda_init_via() failed\n");
219 via = NULL;
220 return 0;
221 }
222
223 /* enable autopoll */
224 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
225 while (!req.complete)
226 cuda_poll();
227
228 return 1;
229}
230#else
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100231int __init find_via_cuda(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 struct adb_request req;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100234 phys_addr_t taddr;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000235 const u32 *reg;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100236 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (vias != 0)
239 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100240 vias = of_find_node_by_name(NULL, "via-cuda");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 if (vias == 0)
242 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Stephen Rothwell01b27262007-04-27 13:41:15 +1000244 reg = of_get_property(vias, "reg", NULL);
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100245 if (reg == NULL) {
246 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
247 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100249 taddr = of_translate_address(vias, reg);
250 if (taddr == 0) {
251 printk(KERN_ERR "via-cuda: Can't translate address !\n");
252 goto fail;
253 }
254 via = ioremap(taddr, 0x2000);
255 if (via == NULL) {
256 printk(KERN_ERR "via-cuda: Can't map address !\n");
257 goto fail;
258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
260 cuda_state = idle;
261 sys_ctrler = SYS_CTRLER_CUDA;
262
263 err = cuda_init_via();
264 if (err) {
265 printk(KERN_ERR "cuda_init_via() failed\n");
266 via = NULL;
267 return 0;
268 }
269
270 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
271 /* for us by the main VIA driver in arch/m68k/mac/via.c */
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
274 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 /* enable autopoll */
277 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
278 while (!req.complete)
279 cuda_poll();
280
281 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100282
283 fail:
284 of_node_put(vias);
285 vias = NULL;
286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287}
Finn Thain18814ee2009-11-17 20:03:05 +1100288#endif /* !defined CONFIG_MAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289
290static int __init via_cuda_start(void)
291{
292 if (via == NULL)
293 return -ENODEV;
294
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000295#ifdef CONFIG_MAC
Finn Thain0251c382007-05-01 22:33:00 +0200296 cuda_irq = IRQ_MAC_ADB;
Finn Thain18814ee2009-11-17 20:03:05 +1100297#else
Finn Thain0251c382007-05-01 22:33:00 +0200298 cuda_irq = irq_of_parse_and_map(vias, 0);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000299 if (!cuda_irq) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000300 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
301 vias->full_name);
302 return -ENODEV;
303 }
Finn Thain18814ee2009-11-17 20:03:05 +1100304#endif
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000305
Finn Thain0251c382007-05-01 22:33:00 +0200306 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
307 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308 return -EAGAIN;
309 }
310
Finn Thaind23eee82016-12-31 19:56:26 -0500311 pr_info("Macintosh Cuda and Egret driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
313 cuda_fully_inited = 1;
314 return 0;
315}
316
317device_initcall(via_cuda_start);
318
319#ifdef CONFIG_ADB
320static int
321cuda_probe(void)
322{
323#ifdef CONFIG_PPC
324 if (sys_ctrler != SYS_CTRLER_CUDA)
325 return -ENODEV;
326#else
Finn Thainf74faec2016-12-31 19:56:26 -0500327 if (macintosh_config->adb_type != MAC_ADB_CUDA &&
328 macintosh_config->adb_type != MAC_ADB_EGRET)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 if (via == NULL)
332 return -ENODEV;
333 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334}
335#endif /* CONFIG_ADB */
336
Finn Thaind23eee82016-12-31 19:56:26 -0500337static int __init sync_egret(void)
338{
339 if (TREQ_asserted(in_8(&via[B]))) {
340 /* Complete the inbound transfer */
341 assert_TIP_and_TACK();
342 while (1) {
343 negate_TACK();
344 mdelay(1);
345 (void)in_8(&via[SR]);
346 assert_TACK();
347 if (!TREQ_asserted(in_8(&via[B])))
348 break;
349 }
350 negate_TIP_and_TACK();
351 } else if (in_8(&via[B]) & TIP) {
352 /* Terminate the outbound transfer */
353 negate_TACK();
354 assert_TACK();
355 mdelay(1);
356 negate_TIP_and_TACK();
357 }
358 /* Clear shift register interrupt */
359 if (in_8(&via[IFR]) & SR_INT)
360 (void)in_8(&via[SR]);
361 return 0;
362}
363
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364#define WAIT_FOR(cond, what) \
365 do { \
366 int x; \
367 for (x = 1000; !(cond); --x) { \
368 if (x == 0) { \
Finn Thain523717d2016-12-31 19:56:26 -0500369 pr_err("Timeout waiting for " what "\n"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 return -ENXIO; \
371 } \
372 udelay(100); \
373 } \
374 } while (0)
375
376static int
Geert Uytterhoeven330dae12013-06-30 12:03:23 +0200377__init cuda_init_via(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378{
Finn Thain0251c382007-05-01 22:33:00 +0200379#ifdef CONFIG_PPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
381 (void)in_8(&via[IER]);
Finn Thain0251c382007-05-01 22:33:00 +0200382#else
383 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384#endif
385
Finn Thaind23eee82016-12-31 19:56:26 -0500386 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
387 out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
388 (void)in_8(&via[SR]); /* clear any left-over data */
389
390 if (mcu_is_egret)
391 return sync_egret();
392
393 negate_TIP_and_TACK();
394
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 /* delay 4ms and then clear any pending interrupt */
396 mdelay(4);
397 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200398 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
400 /* sync with the CUDA - assert TACK without TIP */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500401 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 /* wait for the CUDA to assert TREQ in response */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500404 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
406 /* wait for the interrupt and then clear it */
407 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
408 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200409 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 /* finish the sync by negating TACK */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500412 negate_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
414 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500415 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
417 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200418 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
420 return 0;
421}
422
423#ifdef CONFIG_ADB
424/* Send an ADB command */
425static int
426cuda_send_request(struct adb_request *req, int sync)
427{
428 int i;
429
430 if ((via == NULL) || !cuda_fully_inited) {
431 req->complete = 1;
432 return -ENXIO;
433 }
434
435 req->reply_expected = 1;
436
437 i = cuda_write(req);
438 if (i)
439 return i;
440
441 if (sync) {
442 while (!req->complete)
443 cuda_poll();
444 }
445 return 0;
446}
447
448
449/* Enable/disable autopolling */
450static int
451cuda_adb_autopoll(int devs)
452{
453 struct adb_request req;
454
455 if ((via == NULL) || !cuda_fully_inited)
456 return -ENXIO;
457
458 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
459 while (!req.complete)
460 cuda_poll();
461 return 0;
462}
463
464/* Reset adb bus - how do we do this?? */
465static int
466cuda_reset_adb_bus(void)
467{
468 struct adb_request req;
469
470 if ((via == NULL) || !cuda_fully_inited)
471 return -ENXIO;
472
473 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
474 while (!req.complete)
475 cuda_poll();
476 return 0;
477}
478#endif /* CONFIG_ADB */
Finn Thain523717d2016-12-31 19:56:26 -0500479
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480/* Construct and send a cuda request */
481int
482cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
483 int nbytes, ...)
484{
485 va_list list;
486 int i;
487
488 if (via == NULL) {
489 req->complete = 1;
490 return -ENXIO;
491 }
492
493 req->nbytes = nbytes;
494 req->done = done;
495 va_start(list, nbytes);
496 for (i = 0; i < nbytes; ++i)
497 req->data[i] = va_arg(list, int);
498 va_end(list);
499 req->reply_expected = 1;
500 return cuda_write(req);
501}
Anton Blanchard4a1b08e2014-08-20 08:00:01 +1000502EXPORT_SYMBOL(cuda_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504static int
505cuda_write(struct adb_request *req)
506{
507 unsigned long flags;
508
509 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
510 req->complete = 1;
511 return -EINVAL;
512 }
513 req->next = NULL;
514 req->sent = 0;
515 req->complete = 0;
516 req->reply_len = 0;
517
518 spin_lock_irqsave(&cuda_lock, flags);
519 if (current_req != 0) {
520 last_req->next = req;
521 last_req = req;
522 } else {
523 current_req = req;
524 last_req = req;
525 if (cuda_state == idle)
526 cuda_start();
527 }
528 spin_unlock_irqrestore(&cuda_lock, flags);
529
530 return 0;
531}
532
533static void
534cuda_start(void)
535{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /* assert cuda_state == idle */
Finn Thain06d7e992016-12-31 19:56:26 -0500537 if (current_req == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 return;
Finn Thain97ced1a2016-12-31 19:56:26 -0500539 data_index = 0;
Finn Thainfd7a65a2016-12-31 19:56:26 -0500540 if (TREQ_asserted(in_8(&via[B])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return; /* a byte is coming in from the CUDA */
542
543 /* set the shift register to shift out and send a byte */
544 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
Finn Thain97ced1a2016-12-31 19:56:26 -0500545 out_8(&via[SR], current_req->data[data_index++]);
Finn Thaind23eee82016-12-31 19:56:26 -0500546 if (mcu_is_egret)
547 assert_TIP_and_TACK();
548 else
549 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 cuda_state = sent_first_byte;
551}
552
553void
554cuda_poll(void)
555{
Finn Thainac394522016-12-31 19:56:26 -0500556 cuda_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557}
Anton Blanchard4a1b08e2014-08-20 08:00:01 +1000558EXPORT_SYMBOL(cuda_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559
Finn Thainfe73b582016-12-31 19:56:26 -0500560#define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
561
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100563cuda_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564{
Finn Thainac394522016-12-31 19:56:26 -0500565 unsigned long flags;
Finn Thainfd7a65a2016-12-31 19:56:26 -0500566 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 struct adb_request *req = NULL;
568 unsigned char ibuf[16];
569 int ibuf_len = 0;
570 int complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Finn Thainac394522016-12-31 19:56:26 -0500572 spin_lock_irqsave(&cuda_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Finn Thain18814ee2009-11-17 20:03:05 +1100574 /* On powermacs, this handler is registered for the VIA IRQ. But they use
Finn Thain0251c382007-05-01 22:33:00 +0200575 * just the shift register IRQ -- other VIA interrupt sources are disabled.
576 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
577 * we are polling, the shift register IRQ flag has already been cleared.
578 */
579
580#ifdef CONFIG_MAC
581 if (!arg)
582#endif
583 {
584 if ((in_8(&via[IFR]) & SR_INT) == 0) {
Finn Thainac394522016-12-31 19:56:26 -0500585 spin_unlock_irqrestore(&cuda_lock, flags);
Finn Thain0251c382007-05-01 22:33:00 +0200586 return IRQ_NONE;
587 } else {
588 out_8(&via[IFR], SR_INT);
589 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 }
Finn Thainfd7a65a2016-12-31 19:56:26 -0500591
592 status = in_8(&via[B]) & (TIP | TACK | TREQ);
593
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 switch (cuda_state) {
595 case idle:
Finn Thaind23eee82016-12-31 19:56:26 -0500596 /* System controller has unsolicited data for us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 (void)in_8(&via[SR]);
Finn Thaind23eee82016-12-31 19:56:26 -0500598idle_state:
Finn Thainfd7a65a2016-12-31 19:56:26 -0500599 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 cuda_state = reading;
601 reply_ptr = cuda_rbuf;
602 reading_reply = 0;
603 break;
604
605 case awaiting_reply:
Finn Thaind23eee82016-12-31 19:56:26 -0500606 /* System controller has reply data for us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 (void)in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500608 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 cuda_state = reading;
610 reply_ptr = current_req->reply;
611 reading_reply = 1;
612 break;
613
614 case sent_first_byte:
Finn Thainfd7a65a2016-12-31 19:56:26 -0500615 if (TREQ_asserted(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* collision */
617 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
618 (void)in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500619 negate_TIP_and_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 cuda_state = idle;
Finn Thaind23eee82016-12-31 19:56:26 -0500621 /* Egret does not raise an "aborted" interrupt */
622 if (mcu_is_egret)
623 goto idle_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 } else {
Finn Thain97ced1a2016-12-31 19:56:26 -0500625 out_8(&via[SR], current_req->data[data_index++]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500626 toggle_TACK();
Finn Thaind23eee82016-12-31 19:56:26 -0500627 if (mcu_is_egret)
628 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 cuda_state = sending;
630 }
631 break;
632
633 case sending:
634 req = current_req;
635 if (data_index >= req->nbytes) {
636 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
637 (void)in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500638 negate_TIP_and_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 req->sent = 1;
640 if (req->reply_expected) {
641 cuda_state = awaiting_reply;
642 } else {
643 current_req = req->next;
644 complete = 1;
645 /* not sure about this */
646 cuda_state = idle;
647 cuda_start();
648 }
649 } else {
650 out_8(&via[SR], req->data[data_index++]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500651 toggle_TACK();
Finn Thaind23eee82016-12-31 19:56:26 -0500652 if (mcu_is_egret)
653 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654 }
655 break;
656
657 case reading:
Finn Thainfe73b582016-12-31 19:56:26 -0500658 if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
659 : ARRAY_FULL(cuda_rbuf, reply_ptr))
660 (void)in_8(&via[SR]);
661 else
662 *reply_ptr++ = in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500663 if (!TREQ_asserted(status)) {
Finn Thaind23eee82016-12-31 19:56:26 -0500664 if (mcu_is_egret)
665 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 /* that's all folks */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500667 negate_TIP_and_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 cuda_state = read_done;
Finn Thaind23eee82016-12-31 19:56:26 -0500669 /* Egret does not raise a "read done" interrupt */
670 if (mcu_is_egret)
671 goto read_done_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 } else {
Finn Thainfd7a65a2016-12-31 19:56:26 -0500673 toggle_TACK();
Finn Thaind23eee82016-12-31 19:56:26 -0500674 if (mcu_is_egret)
675 negate_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 }
677 break;
678
679 case read_done:
680 (void)in_8(&via[SR]);
Finn Thaind23eee82016-12-31 19:56:26 -0500681read_done_state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 if (reading_reply) {
683 req = current_req;
684 req->reply_len = reply_ptr - req->reply;
685 if (req->data[0] == ADB_PACKET) {
686 /* Have to adjust the reply from ADB commands */
687 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
688 /* the 0x2 bit indicates no response */
689 req->reply_len = 0;
690 } else {
691 /* leave just the command and result bytes in the reply */
692 req->reply_len -= 2;
693 memmove(req->reply, req->reply + 2, req->reply_len);
694 }
695 }
696 current_req = req->next;
697 complete = 1;
Finn Thaincfbf9982016-12-31 19:56:26 -0500698 reading_reply = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else {
700 /* This is tricky. We must break the spinlock to call
701 * cuda_input. However, doing so means we might get
702 * re-entered from another CPU getting an interrupt
703 * or calling cuda_poll(). I ended up using the stack
704 * (it's only for 16 bytes) and moving the actual
705 * call to cuda_input to outside of the lock.
706 */
707 ibuf_len = reply_ptr - cuda_rbuf;
708 memcpy(ibuf, cuda_rbuf, ibuf_len);
709 }
Finn Thaincfbf9982016-12-31 19:56:26 -0500710 reply_ptr = cuda_rbuf;
Finn Thaina6466242016-12-31 19:56:26 -0500711 cuda_state = idle;
712 cuda_start();
713 if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
Finn Thainfd7a65a2016-12-31 19:56:26 -0500714 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 cuda_state = reading;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 }
717 break;
718
719 default:
Finn Thain523717d2016-12-31 19:56:26 -0500720 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 }
Finn Thainac394522016-12-31 19:56:26 -0500722 spin_unlock_irqrestore(&cuda_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (complete && req) {
724 void (*done)(struct adb_request *) = req->done;
725 mb();
726 req->complete = 1;
727 /* Here, we assume that if the request has a done member, the
728 * struct request will survive to setting req->complete to 1
729 */
730 if (done)
731 (*done)(req);
732 }
733 if (ibuf_len)
David Howells7d12e782006-10-05 14:55:46 +0100734 cuda_input(ibuf, ibuf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 return IRQ_HANDLED;
736}
737
738static void
David Howells7d12e782006-10-05 14:55:46 +0100739cuda_input(unsigned char *buf, int nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 switch (buf[0]) {
742 case ADB_PACKET:
743#ifdef CONFIG_XMON
744 if (nb == 5 && buf[2] == 0x2c) {
745 extern int xmon_wants_key, xmon_adb_keycode;
746 if (xmon_wants_key) {
747 xmon_adb_keycode = buf[3];
748 return;
749 }
750 }
751#endif /* CONFIG_XMON */
752#ifdef CONFIG_ADB
David Howells7d12e782006-10-05 14:55:46 +0100753 adb_input(buf+2, nb-2, buf[1] & 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754#endif /* CONFIG_ADB */
755 break;
756
Finn Thaind23eee82016-12-31 19:56:26 -0500757 case TIMER_PACKET:
758 /* Egret sends these periodically. Might be useful as a 'heartbeat'
759 * to trigger a recovery for the VIA shift register errata.
760 */
761 break;
762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 default:
Finn Thain523717d2016-12-31 19:56:26 -0500764 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
765 buf, nb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 }
767}