blob: 1a742bd9f612f16641a21ac5bbfd71bc88e97688 [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
208 if (macintosh_config->adb_type != MAC_ADB_CUDA)
209 return 0;
210
211 via = via1;
212 cuda_state = idle;
Finn Thaind23eee82016-12-31 19:56:26 -0500213 mcu_is_egret = false;
Finn Thain18814ee2009-11-17 20:03:05 +1100214
215 err = cuda_init_via();
216 if (err) {
217 printk(KERN_ERR "cuda_init_via() failed\n");
218 via = NULL;
219 return 0;
220 }
221
222 /* enable autopoll */
223 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
224 while (!req.complete)
225 cuda_poll();
226
227 return 1;
228}
229#else
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100230int __init find_via_cuda(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 struct adb_request req;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100233 phys_addr_t taddr;
Jeremy Kerr018a3d12006-07-12 15:40:29 +1000234 const u32 *reg;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100235 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
237 if (vias != 0)
238 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100239 vias = of_find_node_by_name(NULL, "via-cuda");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 if (vias == 0)
241 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
Stephen Rothwell01b27262007-04-27 13:41:15 +1000243 reg = of_get_property(vias, "reg", NULL);
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100244 if (reg == NULL) {
245 printk(KERN_ERR "via-cuda: No \"reg\" property !\n");
246 goto fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100248 taddr = of_translate_address(vias, reg);
249 if (taddr == 0) {
250 printk(KERN_ERR "via-cuda: Can't translate address !\n");
251 goto fail;
252 }
253 via = ioremap(taddr, 0x2000);
254 if (via == NULL) {
255 printk(KERN_ERR "via-cuda: Can't map address !\n");
256 goto fail;
257 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 cuda_state = idle;
260 sys_ctrler = SYS_CTRLER_CUDA;
261
262 err = cuda_init_via();
263 if (err) {
264 printk(KERN_ERR "cuda_init_via() failed\n");
265 via = NULL;
266 return 0;
267 }
268
269 /* Clear and enable interrupts, but only on PPC. On 68K it's done */
270 /* for us by the main VIA driver in arch/m68k/mac/via.c */
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272 out_8(&via[IFR], 0x7f); /* clear interrupts by writing 1s */
273 out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274
275 /* enable autopoll */
276 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
277 while (!req.complete)
278 cuda_poll();
279
280 return 1;
Benjamin Herrenschmidt51d30822005-11-23 17:57:25 +1100281
282 fail:
283 of_node_put(vias);
284 vias = NULL;
285 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
Finn Thain18814ee2009-11-17 20:03:05 +1100287#endif /* !defined CONFIG_MAC */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288
289static int __init via_cuda_start(void)
290{
291 if (via == NULL)
292 return -ENODEV;
293
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000294#ifdef CONFIG_MAC
Finn Thain0251c382007-05-01 22:33:00 +0200295 cuda_irq = IRQ_MAC_ADB;
Finn Thain18814ee2009-11-17 20:03:05 +1100296#else
Finn Thain0251c382007-05-01 22:33:00 +0200297 cuda_irq = irq_of_parse_and_map(vias, 0);
Michael Ellermanef24ba72016-09-06 21:53:24 +1000298 if (!cuda_irq) {
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000299 printk(KERN_ERR "via-cuda: can't map interrupts for %s\n",
300 vias->full_name);
301 return -ENODEV;
302 }
Finn Thain18814ee2009-11-17 20:03:05 +1100303#endif
Benjamin Herrenschmidt0ebfff12006-07-03 21:36:01 +1000304
Finn Thain0251c382007-05-01 22:33:00 +0200305 if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
306 printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 return -EAGAIN;
308 }
309
Finn Thaind23eee82016-12-31 19:56:26 -0500310 pr_info("Macintosh Cuda and Egret driver.\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 cuda_fully_inited = 1;
313 return 0;
314}
315
316device_initcall(via_cuda_start);
317
318#ifdef CONFIG_ADB
319static int
320cuda_probe(void)
321{
322#ifdef CONFIG_PPC
323 if (sys_ctrler != SYS_CTRLER_CUDA)
324 return -ENODEV;
325#else
326 if (macintosh_config->adb_type != MAC_ADB_CUDA)
327 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 if (via == NULL)
330 return -ENODEV;
331 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333#endif /* CONFIG_ADB */
334
Finn Thaind23eee82016-12-31 19:56:26 -0500335static int __init sync_egret(void)
336{
337 if (TREQ_asserted(in_8(&via[B]))) {
338 /* Complete the inbound transfer */
339 assert_TIP_and_TACK();
340 while (1) {
341 negate_TACK();
342 mdelay(1);
343 (void)in_8(&via[SR]);
344 assert_TACK();
345 if (!TREQ_asserted(in_8(&via[B])))
346 break;
347 }
348 negate_TIP_and_TACK();
349 } else if (in_8(&via[B]) & TIP) {
350 /* Terminate the outbound transfer */
351 negate_TACK();
352 assert_TACK();
353 mdelay(1);
354 negate_TIP_and_TACK();
355 }
356 /* Clear shift register interrupt */
357 if (in_8(&via[IFR]) & SR_INT)
358 (void)in_8(&via[SR]);
359 return 0;
360}
361
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362#define WAIT_FOR(cond, what) \
363 do { \
364 int x; \
365 for (x = 1000; !(cond); --x) { \
366 if (x == 0) { \
Finn Thain523717d2016-12-31 19:56:26 -0500367 pr_err("Timeout waiting for " what "\n"); \
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 return -ENXIO; \
369 } \
370 udelay(100); \
371 } \
372 } while (0)
373
374static int
Geert Uytterhoeven330dae12013-06-30 12:03:23 +0200375__init cuda_init_via(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376{
Finn Thain0251c382007-05-01 22:33:00 +0200377#ifdef CONFIG_PPC
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 out_8(&via[IER], 0x7f); /* disable interrupts from VIA */
379 (void)in_8(&via[IER]);
Finn Thain0251c382007-05-01 22:33:00 +0200380#else
381 out_8(&via[IER], SR_INT); /* disable SR interrupt from VIA */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382#endif
383
Finn Thaind23eee82016-12-31 19:56:26 -0500384 out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ); /* TACK & TIP out */
385 out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT); /* SR data in */
386 (void)in_8(&via[SR]); /* clear any left-over data */
387
388 if (mcu_is_egret)
389 return sync_egret();
390
391 negate_TIP_and_TACK();
392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 /* delay 4ms and then clear any pending interrupt */
394 mdelay(4);
395 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200396 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /* sync with the CUDA - assert TACK without TIP */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500399 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
401 /* wait for the CUDA to assert TREQ in response */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500402 WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 /* wait for the interrupt and then clear it */
405 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
406 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200407 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408
409 /* finish the sync by negating TACK */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500410 negate_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
412 /* wait for the CUDA to negate TREQ and the corresponding interrupt */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500413 WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
415 (void)in_8(&via[SR]);
Finn Thain0251c382007-05-01 22:33:00 +0200416 out_8(&via[IFR], SR_INT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
418 return 0;
419}
420
421#ifdef CONFIG_ADB
422/* Send an ADB command */
423static int
424cuda_send_request(struct adb_request *req, int sync)
425{
426 int i;
427
428 if ((via == NULL) || !cuda_fully_inited) {
429 req->complete = 1;
430 return -ENXIO;
431 }
432
433 req->reply_expected = 1;
434
435 i = cuda_write(req);
436 if (i)
437 return i;
438
439 if (sync) {
440 while (!req->complete)
441 cuda_poll();
442 }
443 return 0;
444}
445
446
447/* Enable/disable autopolling */
448static int
449cuda_adb_autopoll(int devs)
450{
451 struct adb_request req;
452
453 if ((via == NULL) || !cuda_fully_inited)
454 return -ENXIO;
455
456 cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
457 while (!req.complete)
458 cuda_poll();
459 return 0;
460}
461
462/* Reset adb bus - how do we do this?? */
463static int
464cuda_reset_adb_bus(void)
465{
466 struct adb_request req;
467
468 if ((via == NULL) || !cuda_fully_inited)
469 return -ENXIO;
470
471 cuda_request(&req, NULL, 2, ADB_PACKET, 0); /* maybe? */
472 while (!req.complete)
473 cuda_poll();
474 return 0;
475}
476#endif /* CONFIG_ADB */
Finn Thain523717d2016-12-31 19:56:26 -0500477
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478/* Construct and send a cuda request */
479int
480cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
481 int nbytes, ...)
482{
483 va_list list;
484 int i;
485
486 if (via == NULL) {
487 req->complete = 1;
488 return -ENXIO;
489 }
490
491 req->nbytes = nbytes;
492 req->done = done;
493 va_start(list, nbytes);
494 for (i = 0; i < nbytes; ++i)
495 req->data[i] = va_arg(list, int);
496 va_end(list);
497 req->reply_expected = 1;
498 return cuda_write(req);
499}
Anton Blanchard4a1b08e2014-08-20 08:00:01 +1000500EXPORT_SYMBOL(cuda_request);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502static int
503cuda_write(struct adb_request *req)
504{
505 unsigned long flags;
506
507 if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
508 req->complete = 1;
509 return -EINVAL;
510 }
511 req->next = NULL;
512 req->sent = 0;
513 req->complete = 0;
514 req->reply_len = 0;
515
516 spin_lock_irqsave(&cuda_lock, flags);
517 if (current_req != 0) {
518 last_req->next = req;
519 last_req = req;
520 } else {
521 current_req = req;
522 last_req = req;
523 if (cuda_state == idle)
524 cuda_start();
525 }
526 spin_unlock_irqrestore(&cuda_lock, flags);
527
528 return 0;
529}
530
531static void
532cuda_start(void)
533{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /* assert cuda_state == idle */
Finn Thain06d7e992016-12-31 19:56:26 -0500535 if (current_req == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 return;
Finn Thain97ced1a2016-12-31 19:56:26 -0500537 data_index = 0;
Finn Thainfd7a65a2016-12-31 19:56:26 -0500538 if (TREQ_asserted(in_8(&via[B])))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return; /* a byte is coming in from the CUDA */
540
541 /* set the shift register to shift out and send a byte */
542 out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
Finn Thain97ced1a2016-12-31 19:56:26 -0500543 out_8(&via[SR], current_req->data[data_index++]);
Finn Thaind23eee82016-12-31 19:56:26 -0500544 if (mcu_is_egret)
545 assert_TIP_and_TACK();
546 else
547 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 cuda_state = sent_first_byte;
549}
550
551void
552cuda_poll(void)
553{
Finn Thainac394522016-12-31 19:56:26 -0500554 cuda_interrupt(0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555}
Anton Blanchard4a1b08e2014-08-20 08:00:01 +1000556EXPORT_SYMBOL(cuda_poll);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Finn Thainfe73b582016-12-31 19:56:26 -0500558#define ARRAY_FULL(a, p) ((p) - (a) == ARRAY_SIZE(a))
559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560static irqreturn_t
David Howells7d12e782006-10-05 14:55:46 +0100561cuda_interrupt(int irq, void *arg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562{
Finn Thainac394522016-12-31 19:56:26 -0500563 unsigned long flags;
Finn Thainfd7a65a2016-12-31 19:56:26 -0500564 u8 status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 struct adb_request *req = NULL;
566 unsigned char ibuf[16];
567 int ibuf_len = 0;
568 int complete = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569
Finn Thainac394522016-12-31 19:56:26 -0500570 spin_lock_irqsave(&cuda_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Finn Thain18814ee2009-11-17 20:03:05 +1100572 /* On powermacs, this handler is registered for the VIA IRQ. But they use
Finn Thain0251c382007-05-01 22:33:00 +0200573 * just the shift register IRQ -- other VIA interrupt sources are disabled.
574 * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
575 * we are polling, the shift register IRQ flag has already been cleared.
576 */
577
578#ifdef CONFIG_MAC
579 if (!arg)
580#endif
581 {
582 if ((in_8(&via[IFR]) & SR_INT) == 0) {
Finn Thainac394522016-12-31 19:56:26 -0500583 spin_unlock_irqrestore(&cuda_lock, flags);
Finn Thain0251c382007-05-01 22:33:00 +0200584 return IRQ_NONE;
585 } else {
586 out_8(&via[IFR], SR_INT);
587 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Finn Thainfd7a65a2016-12-31 19:56:26 -0500589
590 status = in_8(&via[B]) & (TIP | TACK | TREQ);
591
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 switch (cuda_state) {
593 case idle:
Finn Thaind23eee82016-12-31 19:56:26 -0500594 /* System controller has unsolicited data for us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 (void)in_8(&via[SR]);
Finn Thaind23eee82016-12-31 19:56:26 -0500596idle_state:
Finn Thainfd7a65a2016-12-31 19:56:26 -0500597 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598 cuda_state = reading;
599 reply_ptr = cuda_rbuf;
600 reading_reply = 0;
601 break;
602
603 case awaiting_reply:
Finn Thaind23eee82016-12-31 19:56:26 -0500604 /* System controller has reply data for us */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 (void)in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500606 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 cuda_state = reading;
608 reply_ptr = current_req->reply;
609 reading_reply = 1;
610 break;
611
612 case sent_first_byte:
Finn Thainfd7a65a2016-12-31 19:56:26 -0500613 if (TREQ_asserted(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* collision */
615 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
616 (void)in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500617 negate_TIP_and_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 cuda_state = idle;
Finn Thaind23eee82016-12-31 19:56:26 -0500619 /* Egret does not raise an "aborted" interrupt */
620 if (mcu_is_egret)
621 goto idle_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 } else {
Finn Thain97ced1a2016-12-31 19:56:26 -0500623 out_8(&via[SR], current_req->data[data_index++]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500624 toggle_TACK();
Finn Thaind23eee82016-12-31 19:56:26 -0500625 if (mcu_is_egret)
626 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 cuda_state = sending;
628 }
629 break;
630
631 case sending:
632 req = current_req;
633 if (data_index >= req->nbytes) {
634 out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
635 (void)in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500636 negate_TIP_and_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 req->sent = 1;
638 if (req->reply_expected) {
639 cuda_state = awaiting_reply;
640 } else {
641 current_req = req->next;
642 complete = 1;
643 /* not sure about this */
644 cuda_state = idle;
645 cuda_start();
646 }
647 } else {
648 out_8(&via[SR], req->data[data_index++]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500649 toggle_TACK();
Finn Thaind23eee82016-12-31 19:56:26 -0500650 if (mcu_is_egret)
651 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653 break;
654
655 case reading:
Finn Thainfe73b582016-12-31 19:56:26 -0500656 if (reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
657 : ARRAY_FULL(cuda_rbuf, reply_ptr))
658 (void)in_8(&via[SR]);
659 else
660 *reply_ptr++ = in_8(&via[SR]);
Finn Thainfd7a65a2016-12-31 19:56:26 -0500661 if (!TREQ_asserted(status)) {
Finn Thaind23eee82016-12-31 19:56:26 -0500662 if (mcu_is_egret)
663 assert_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 /* that's all folks */
Finn Thainfd7a65a2016-12-31 19:56:26 -0500665 negate_TIP_and_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 cuda_state = read_done;
Finn Thaind23eee82016-12-31 19:56:26 -0500667 /* Egret does not raise a "read done" interrupt */
668 if (mcu_is_egret)
669 goto read_done_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 } else {
Finn Thainfd7a65a2016-12-31 19:56:26 -0500671 toggle_TACK();
Finn Thaind23eee82016-12-31 19:56:26 -0500672 if (mcu_is_egret)
673 negate_TACK();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 }
675 break;
676
677 case read_done:
678 (void)in_8(&via[SR]);
Finn Thaind23eee82016-12-31 19:56:26 -0500679read_done_state:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (reading_reply) {
681 req = current_req;
682 req->reply_len = reply_ptr - req->reply;
683 if (req->data[0] == ADB_PACKET) {
684 /* Have to adjust the reply from ADB commands */
685 if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
686 /* the 0x2 bit indicates no response */
687 req->reply_len = 0;
688 } else {
689 /* leave just the command and result bytes in the reply */
690 req->reply_len -= 2;
691 memmove(req->reply, req->reply + 2, req->reply_len);
692 }
693 }
694 current_req = req->next;
695 complete = 1;
Finn Thaincfbf9982016-12-31 19:56:26 -0500696 reading_reply = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 } else {
698 /* This is tricky. We must break the spinlock to call
699 * cuda_input. However, doing so means we might get
700 * re-entered from another CPU getting an interrupt
701 * or calling cuda_poll(). I ended up using the stack
702 * (it's only for 16 bytes) and moving the actual
703 * call to cuda_input to outside of the lock.
704 */
705 ibuf_len = reply_ptr - cuda_rbuf;
706 memcpy(ibuf, cuda_rbuf, ibuf_len);
707 }
Finn Thaincfbf9982016-12-31 19:56:26 -0500708 reply_ptr = cuda_rbuf;
Finn Thaina6466242016-12-31 19:56:26 -0500709 cuda_state = idle;
710 cuda_start();
711 if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
Finn Thainfd7a65a2016-12-31 19:56:26 -0500712 assert_TIP();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 cuda_state = reading;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715 break;
716
717 default:
Finn Thain523717d2016-12-31 19:56:26 -0500718 pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719 }
Finn Thainac394522016-12-31 19:56:26 -0500720 spin_unlock_irqrestore(&cuda_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (complete && req) {
722 void (*done)(struct adb_request *) = req->done;
723 mb();
724 req->complete = 1;
725 /* Here, we assume that if the request has a done member, the
726 * struct request will survive to setting req->complete to 1
727 */
728 if (done)
729 (*done)(req);
730 }
731 if (ibuf_len)
David Howells7d12e782006-10-05 14:55:46 +0100732 cuda_input(ibuf, ibuf_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 return IRQ_HANDLED;
734}
735
736static void
David Howells7d12e782006-10-05 14:55:46 +0100737cuda_input(unsigned char *buf, int nb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 switch (buf[0]) {
740 case ADB_PACKET:
741#ifdef CONFIG_XMON
742 if (nb == 5 && buf[2] == 0x2c) {
743 extern int xmon_wants_key, xmon_adb_keycode;
744 if (xmon_wants_key) {
745 xmon_adb_keycode = buf[3];
746 return;
747 }
748 }
749#endif /* CONFIG_XMON */
750#ifdef CONFIG_ADB
David Howells7d12e782006-10-05 14:55:46 +0100751 adb_input(buf+2, nb-2, buf[1] & 0x40);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752#endif /* CONFIG_ADB */
753 break;
754
Finn Thaind23eee82016-12-31 19:56:26 -0500755 case TIMER_PACKET:
756 /* Egret sends these periodically. Might be useful as a 'heartbeat'
757 * to trigger a recovery for the VIA shift register errata.
758 */
759 break;
760
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 default:
Finn Thain523717d2016-12-31 19:56:26 -0500762 print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
763 buf, nb, false);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 }
765}