xref: /openbmc/linux/drivers/macintosh/via-cuda.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
1b2441318SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
21da177e4SLinus Torvalds /*
3d23eee88SFinn Thain  * Device driver for the Cuda and Egret system controllers found on PowerMacs
4d23eee88SFinn Thain  * and 68k Macs.
51da177e4SLinus Torvalds  *
6d23eee88SFinn Thain  * The Cuda or Egret is a 6805 microcontroller interfaced to the 6522 VIA.
7d23eee88SFinn Thain  * This MCU controls system power, Parameter RAM, Real Time Clock and the
8d23eee88SFinn Thain  * Apple Desktop Bus (ADB) that connects to the keyboard and mouse.
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * Copyright (C) 1996 Paul Mackerras.
111da177e4SLinus Torvalds  */
12c0891ac1SAlexey Dobriyan #include <linux/stdarg.h>
131da177e4SLinus Torvalds #include <linux/types.h>
141da177e4SLinus Torvalds #include <linux/errno.h>
151da177e4SLinus Torvalds #include <linux/kernel.h>
161da177e4SLinus Torvalds #include <linux/delay.h>
171da177e4SLinus Torvalds #include <linux/adb.h>
181da177e4SLinus Torvalds #include <linux/cuda.h>
191da177e4SLinus Torvalds #include <linux/spinlock.h>
201da177e4SLinus Torvalds #include <linux/interrupt.h>
21a486e512SChristophe Leroy #include <linux/of_address.h>
22a486e512SChristophe Leroy #include <linux/of_irq.h>
23a486e512SChristophe Leroy 
241da177e4SLinus Torvalds #ifdef CONFIG_PPC
251da177e4SLinus Torvalds #include <asm/machdep.h>
26e6d03ac1SChristophe Leroy #include <asm/pmac_feature.h>
271da177e4SLinus Torvalds #else
281da177e4SLinus Torvalds #include <asm/macintosh.h>
291da177e4SLinus Torvalds #include <asm/macints.h>
301da177e4SLinus Torvalds #include <asm/mac_via.h>
311da177e4SLinus Torvalds #endif
321da177e4SLinus Torvalds #include <asm/io.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds static volatile unsigned char __iomem *via;
361da177e4SLinus Torvalds static DEFINE_SPINLOCK(cuda_lock);
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds /* VIA registers - spaced 0x200 bytes apart */
391da177e4SLinus Torvalds #define RS		0x200		/* skip between registers */
401da177e4SLinus Torvalds #define B		0		/* B-side data */
411da177e4SLinus Torvalds #define A		RS		/* A-side data */
421da177e4SLinus Torvalds #define DIRB		(2*RS)		/* B-side direction (1=output) */
431da177e4SLinus Torvalds #define DIRA		(3*RS)		/* A-side direction (1=output) */
441da177e4SLinus Torvalds #define T1CL		(4*RS)		/* Timer 1 ctr/latch (low 8 bits) */
451da177e4SLinus Torvalds #define T1CH		(5*RS)		/* Timer 1 counter (high 8 bits) */
461da177e4SLinus Torvalds #define T1LL		(6*RS)		/* Timer 1 latch (low 8 bits) */
471da177e4SLinus Torvalds #define T1LH		(7*RS)		/* Timer 1 latch (high 8 bits) */
481da177e4SLinus Torvalds #define T2CL		(8*RS)		/* Timer 2 ctr/latch (low 8 bits) */
491da177e4SLinus Torvalds #define T2CH		(9*RS)		/* Timer 2 counter (high 8 bits) */
501da177e4SLinus Torvalds #define SR		(10*RS)		/* Shift register */
511da177e4SLinus Torvalds #define ACR		(11*RS)		/* Auxiliary control register */
521da177e4SLinus Torvalds #define PCR		(12*RS)		/* Peripheral control register */
531da177e4SLinus Torvalds #define IFR		(13*RS)		/* Interrupt flag register */
541da177e4SLinus Torvalds #define IER		(14*RS)		/* Interrupt enable register */
551da177e4SLinus Torvalds #define ANH		(15*RS)		/* A-side data, no handshake */
561da177e4SLinus Torvalds 
57d23eee88SFinn Thain /*
58d23eee88SFinn Thain  * When the Cuda design replaced the Egret, some signal names and
59d23eee88SFinn Thain  * logic sense changed. They all serve the same purposes, however.
60d23eee88SFinn Thain  *
61d23eee88SFinn Thain  *   VIA pin       |  Egret pin
62d23eee88SFinn Thain  * ----------------+------------------------------------------
63d23eee88SFinn Thain  *   PB3 (input)   |  Transceiver session   (active low)
64d23eee88SFinn Thain  *   PB4 (output)  |  VIA full              (active high)
65d23eee88SFinn Thain  *   PB5 (output)  |  System session        (active high)
66d23eee88SFinn Thain  *
67d23eee88SFinn Thain  *   VIA pin       |  Cuda pin
68d23eee88SFinn Thain  * ----------------+------------------------------------------
69d23eee88SFinn Thain  *   PB3 (input)   |  Transfer request      (active low)
70d23eee88SFinn Thain  *   PB4 (output)  |  Byte acknowledge      (active low)
71d23eee88SFinn Thain  *   PB5 (output)  |  Transfer in progress  (active low)
72d23eee88SFinn Thain  */
73d23eee88SFinn Thain 
74d23eee88SFinn Thain /* Bits in Port B data register */
75d23eee88SFinn Thain #define TREQ		0x08		/* Transfer request */
76d23eee88SFinn Thain #define TACK		0x10		/* Transfer acknowledge */
77d23eee88SFinn Thain #define TIP		0x20		/* Transfer in progress */
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds /* Bits in ACR */
801da177e4SLinus Torvalds #define SR_CTRL		0x1c		/* Shift register control bits */
811da177e4SLinus Torvalds #define SR_EXT		0x0c		/* Shift on external clock */
821da177e4SLinus Torvalds #define SR_OUT		0x10		/* Shift out if 1 */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds /* Bits in IFR and IER */
851da177e4SLinus Torvalds #define IER_SET		0x80		/* set bits in IER */
861da177e4SLinus Torvalds #define IER_CLR		0		/* clear bits in IER */
871da177e4SLinus Torvalds #define SR_INT		0x04		/* Shift register full/empty */
881da177e4SLinus Torvalds 
89d23eee88SFinn Thain /* Duration of byte acknowledgement pulse (us) */
90d23eee88SFinn Thain #define EGRET_TACK_ASSERTED_DELAY	300
91d23eee88SFinn Thain #define EGRET_TACK_NEGATED_DELAY	400
92d23eee88SFinn Thain 
93d23eee88SFinn Thain /* Interval from interrupt to start of session (us) */
94d23eee88SFinn Thain #define EGRET_SESSION_DELAY		450
95d23eee88SFinn Thain 
96d23eee88SFinn Thain #ifdef CONFIG_PPC
97d23eee88SFinn Thain #define mcu_is_egret	false
98d23eee88SFinn Thain #else
99d23eee88SFinn Thain static bool mcu_is_egret;
100d23eee88SFinn Thain #endif
101d23eee88SFinn Thain 
TREQ_asserted(u8 portb)102fd7a65a2SFinn Thain static inline bool TREQ_asserted(u8 portb)
103fd7a65a2SFinn Thain {
104fd7a65a2SFinn Thain 	return !(portb & TREQ);
105fd7a65a2SFinn Thain }
106fd7a65a2SFinn Thain 
assert_TIP(void)107fd7a65a2SFinn Thain static inline void assert_TIP(void)
108fd7a65a2SFinn Thain {
109d23eee88SFinn Thain 	if (mcu_is_egret) {
110d23eee88SFinn Thain 		udelay(EGRET_SESSION_DELAY);
111d23eee88SFinn Thain 		out_8(&via[B], in_8(&via[B]) | TIP);
112d23eee88SFinn Thain 	} else
113fd7a65a2SFinn Thain 		out_8(&via[B], in_8(&via[B]) & ~TIP);
114fd7a65a2SFinn Thain }
115fd7a65a2SFinn Thain 
assert_TIP_and_TACK(void)116d23eee88SFinn Thain static inline void assert_TIP_and_TACK(void)
117d23eee88SFinn Thain {
118d23eee88SFinn Thain 	if (mcu_is_egret) {
119d23eee88SFinn Thain 		udelay(EGRET_SESSION_DELAY);
120d23eee88SFinn Thain 		out_8(&via[B], in_8(&via[B]) | TIP | TACK);
121d23eee88SFinn Thain 	} else
122d23eee88SFinn Thain 		out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
123d23eee88SFinn Thain }
124d23eee88SFinn Thain 
assert_TACK(void)125fd7a65a2SFinn Thain static inline void assert_TACK(void)
126fd7a65a2SFinn Thain {
127d23eee88SFinn Thain 	if (mcu_is_egret) {
128d23eee88SFinn Thain 		udelay(EGRET_TACK_NEGATED_DELAY);
129d23eee88SFinn Thain 		out_8(&via[B], in_8(&via[B]) | TACK);
130d23eee88SFinn Thain 	} else
131fd7a65a2SFinn Thain 		out_8(&via[B], in_8(&via[B]) & ~TACK);
132fd7a65a2SFinn Thain }
133fd7a65a2SFinn Thain 
toggle_TACK(void)134fd7a65a2SFinn Thain static inline void toggle_TACK(void)
135fd7a65a2SFinn Thain {
136fd7a65a2SFinn Thain 	out_8(&via[B], in_8(&via[B]) ^ TACK);
137fd7a65a2SFinn Thain }
138fd7a65a2SFinn Thain 
negate_TACK(void)139fd7a65a2SFinn Thain static inline void negate_TACK(void)
140fd7a65a2SFinn Thain {
141d23eee88SFinn Thain 	if (mcu_is_egret) {
142d23eee88SFinn Thain 		udelay(EGRET_TACK_ASSERTED_DELAY);
143d23eee88SFinn Thain 		out_8(&via[B], in_8(&via[B]) & ~TACK);
144d23eee88SFinn Thain 	} else
145fd7a65a2SFinn Thain 		out_8(&via[B], in_8(&via[B]) | TACK);
146fd7a65a2SFinn Thain }
147fd7a65a2SFinn Thain 
negate_TIP_and_TACK(void)148fd7a65a2SFinn Thain static inline void negate_TIP_and_TACK(void)
149fd7a65a2SFinn Thain {
150d23eee88SFinn Thain 	if (mcu_is_egret) {
151d23eee88SFinn Thain 		udelay(EGRET_TACK_ASSERTED_DELAY);
152d23eee88SFinn Thain 		out_8(&via[B], in_8(&via[B]) & ~(TIP | TACK));
153d23eee88SFinn Thain 	} else
154fd7a65a2SFinn Thain 		out_8(&via[B], in_8(&via[B]) | TIP | TACK);
155fd7a65a2SFinn Thain }
156fd7a65a2SFinn Thain 
1571da177e4SLinus Torvalds static enum cuda_state {
1581da177e4SLinus Torvalds     idle,
1591da177e4SLinus Torvalds     sent_first_byte,
1601da177e4SLinus Torvalds     sending,
1611da177e4SLinus Torvalds     reading,
1621da177e4SLinus Torvalds     read_done,
1631da177e4SLinus Torvalds     awaiting_reply
1641da177e4SLinus Torvalds } cuda_state;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds static struct adb_request *current_req;
1671da177e4SLinus Torvalds static struct adb_request *last_req;
1681da177e4SLinus Torvalds static unsigned char cuda_rbuf[16];
1691da177e4SLinus Torvalds static unsigned char *reply_ptr;
1701da177e4SLinus Torvalds static int reading_reply;
1711da177e4SLinus Torvalds static int data_index;
1720251c38cSFinn Thain static int cuda_irq;
1731da177e4SLinus Torvalds #ifdef CONFIG_PPC
1741da177e4SLinus Torvalds static struct device_node *vias;
1751da177e4SLinus Torvalds #endif
17687275856SOlaf Hering static int cuda_fully_inited;
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds #ifdef CONFIG_ADB
1791da177e4SLinus Torvalds static int cuda_probe(void);
1801da177e4SLinus Torvalds static int cuda_send_request(struct adb_request *req, int sync);
1811da177e4SLinus Torvalds static int cuda_adb_autopoll(int devs);
1821da177e4SLinus Torvalds static int cuda_reset_adb_bus(void);
1831da177e4SLinus Torvalds #endif /* CONFIG_ADB */
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds static int cuda_init_via(void);
1861da177e4SLinus Torvalds static void cuda_start(void);
1877d12e780SDavid Howells static irqreturn_t cuda_interrupt(int irq, void *arg);
1887d12e780SDavid Howells static void cuda_input(unsigned char *buf, int nb);
1891da177e4SLinus Torvalds void cuda_poll(void);
1901da177e4SLinus Torvalds static int cuda_write(struct adb_request *req);
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds int cuda_request(struct adb_request *req,
1931da177e4SLinus Torvalds 		 void (*done)(struct adb_request *), int nbytes, ...);
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds #ifdef CONFIG_ADB
1961da177e4SLinus Torvalds struct adb_driver via_cuda_driver = {
19718814ee8SFinn Thain 	.name         = "CUDA",
19818814ee8SFinn Thain 	.probe        = cuda_probe,
19918814ee8SFinn Thain 	.send_request = cuda_send_request,
20018814ee8SFinn Thain 	.autopoll     = cuda_adb_autopoll,
20118814ee8SFinn Thain 	.poll         = cuda_poll,
20218814ee8SFinn Thain 	.reset_bus    = cuda_reset_adb_bus,
2031da177e4SLinus Torvalds };
2041da177e4SLinus Torvalds #endif /* CONFIG_ADB */
2051da177e4SLinus Torvalds 
20618814ee8SFinn Thain #ifdef CONFIG_MAC
find_via_cuda(void)20718814ee8SFinn Thain int __init find_via_cuda(void)
20818814ee8SFinn Thain {
20918814ee8SFinn Thain     struct adb_request req;
21018814ee8SFinn Thain     int err;
21118814ee8SFinn Thain 
212f74faec6SFinn Thain     if (macintosh_config->adb_type != MAC_ADB_CUDA &&
213f74faec6SFinn Thain         macintosh_config->adb_type != MAC_ADB_EGRET)
21418814ee8SFinn Thain 	return 0;
21518814ee8SFinn Thain 
21618814ee8SFinn Thain     via = via1;
21718814ee8SFinn Thain     cuda_state = idle;
218f74faec6SFinn Thain     mcu_is_egret = macintosh_config->adb_type == MAC_ADB_EGRET;
21918814ee8SFinn Thain 
22018814ee8SFinn Thain     err = cuda_init_via();
22118814ee8SFinn Thain     if (err) {
22218814ee8SFinn Thain 	printk(KERN_ERR "cuda_init_via() failed\n");
22318814ee8SFinn Thain 	via = NULL;
22418814ee8SFinn Thain 	return 0;
22518814ee8SFinn Thain     }
22618814ee8SFinn Thain 
22718814ee8SFinn Thain     /* enable autopoll */
22818814ee8SFinn Thain     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
22918814ee8SFinn Thain     while (!req.complete)
23018814ee8SFinn Thain 	cuda_poll();
23118814ee8SFinn Thain 
23218814ee8SFinn Thain     return 1;
23318814ee8SFinn Thain }
23418814ee8SFinn Thain #else
find_via_cuda(void)23551d3082fSBenjamin Herrenschmidt int __init find_via_cuda(void)
2361da177e4SLinus Torvalds {
2371da177e4SLinus Torvalds     struct adb_request req;
238*93cfa6fbSRob Herring     struct resource res;
23951d3082fSBenjamin Herrenschmidt     int err;
2401da177e4SLinus Torvalds 
241928b3964SYang Li     if (vias)
2421da177e4SLinus Torvalds 	return 1;
24351d3082fSBenjamin Herrenschmidt     vias = of_find_node_by_name(NULL, "via-cuda");
244928b3964SYang Li     if (!vias)
2451da177e4SLinus Torvalds 	return 0;
2461da177e4SLinus Torvalds 
247*93cfa6fbSRob Herring     err = of_address_to_resource(vias, 0, &res);
248*93cfa6fbSRob Herring     if (err) {
249*93cfa6fbSRob Herring 	    printk(KERN_ERR "via-cuda: Error getting \"reg\" property !\n");
25051d3082fSBenjamin Herrenschmidt 	    goto fail;
2511da177e4SLinus Torvalds     }
252*93cfa6fbSRob Herring     via = ioremap(res.start, 0x2000);
25351d3082fSBenjamin Herrenschmidt     if (via == NULL) {
25451d3082fSBenjamin Herrenschmidt 	    printk(KERN_ERR "via-cuda: Can't map address !\n");
25551d3082fSBenjamin Herrenschmidt 	    goto fail;
25651d3082fSBenjamin Herrenschmidt     }
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds     cuda_state = idle;
2591da177e4SLinus Torvalds     sys_ctrler = SYS_CTRLER_CUDA;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds     err = cuda_init_via();
2621da177e4SLinus Torvalds     if (err) {
2631da177e4SLinus Torvalds 	printk(KERN_ERR "cuda_init_via() failed\n");
2641da177e4SLinus Torvalds 	via = NULL;
2651da177e4SLinus Torvalds 	return 0;
2661da177e4SLinus Torvalds     }
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds     /* Clear and enable interrupts, but only on PPC. On 68K it's done  */
2691da177e4SLinus Torvalds     /* for us by the main VIA driver in arch/m68k/mac/via.c        */
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds     out_8(&via[IFR], 0x7f);	/* clear interrupts by writing 1s */
2721da177e4SLinus Torvalds     out_8(&via[IER], IER_SET|SR_INT); /* enable interrupt from SR */
2731da177e4SLinus Torvalds 
2741da177e4SLinus Torvalds     /* enable autopoll */
2751da177e4SLinus Torvalds     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, 1);
2761da177e4SLinus Torvalds     while (!req.complete)
2771da177e4SLinus Torvalds 	cuda_poll();
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds     return 1;
28051d3082fSBenjamin Herrenschmidt 
28151d3082fSBenjamin Herrenschmidt  fail:
28251d3082fSBenjamin Herrenschmidt     of_node_put(vias);
28351d3082fSBenjamin Herrenschmidt     vias = NULL;
28451d3082fSBenjamin Herrenschmidt     return 0;
2851da177e4SLinus Torvalds }
28618814ee8SFinn Thain #endif /* !defined CONFIG_MAC */
2871da177e4SLinus Torvalds 
via_cuda_start(void)2881da177e4SLinus Torvalds static int __init via_cuda_start(void)
2891da177e4SLinus Torvalds {
2901da177e4SLinus Torvalds     if (via == NULL)
2911da177e4SLinus Torvalds 	return -ENODEV;
2921da177e4SLinus Torvalds 
2930ebfff14SBenjamin Herrenschmidt #ifdef CONFIG_MAC
2940251c38cSFinn Thain     cuda_irq = IRQ_MAC_ADB;
29518814ee8SFinn Thain #else
2960251c38cSFinn Thain     cuda_irq = irq_of_parse_and_map(vias, 0);
297ef24ba70SMichael Ellerman     if (!cuda_irq) {
298b6a945aeSRob Herring 	printk(KERN_ERR "via-cuda: can't map interrupts for %pOF\n",
299b6a945aeSRob Herring 	       vias);
3000ebfff14SBenjamin Herrenschmidt 	return -ENODEV;
3010ebfff14SBenjamin Herrenschmidt     }
30218814ee8SFinn Thain #endif
3030ebfff14SBenjamin Herrenschmidt 
3040251c38cSFinn Thain     if (request_irq(cuda_irq, cuda_interrupt, 0, "ADB", cuda_interrupt)) {
3050251c38cSFinn Thain 	printk(KERN_ERR "via-cuda: can't request irq %d\n", cuda_irq);
3061da177e4SLinus Torvalds 	return -EAGAIN;
3071da177e4SLinus Torvalds     }
3081da177e4SLinus Torvalds 
309d23eee88SFinn Thain     pr_info("Macintosh Cuda and Egret driver.\n");
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds     cuda_fully_inited = 1;
3121da177e4SLinus Torvalds     return 0;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds device_initcall(via_cuda_start);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds #ifdef CONFIG_ADB
3181da177e4SLinus Torvalds static int
cuda_probe(void)3191da177e4SLinus Torvalds cuda_probe(void)
3201da177e4SLinus Torvalds {
3211da177e4SLinus Torvalds #ifdef CONFIG_PPC
3221da177e4SLinus Torvalds     if (sys_ctrler != SYS_CTRLER_CUDA)
3231da177e4SLinus Torvalds 	return -ENODEV;
3241da177e4SLinus Torvalds #else
325f74faec6SFinn Thain     if (macintosh_config->adb_type != MAC_ADB_CUDA &&
326f74faec6SFinn Thain         macintosh_config->adb_type != MAC_ADB_EGRET)
3271da177e4SLinus Torvalds 	return -ENODEV;
3281da177e4SLinus Torvalds #endif
3291da177e4SLinus Torvalds     if (via == NULL)
3301da177e4SLinus Torvalds 	return -ENODEV;
3311da177e4SLinus Torvalds     return 0;
3321da177e4SLinus Torvalds }
3331da177e4SLinus Torvalds #endif /* CONFIG_ADB */
3341da177e4SLinus Torvalds 
sync_egret(void)335d23eee88SFinn Thain static int __init sync_egret(void)
336d23eee88SFinn Thain {
337d23eee88SFinn Thain 	if (TREQ_asserted(in_8(&via[B]))) {
338d23eee88SFinn Thain 		/* Complete the inbound transfer */
339d23eee88SFinn Thain 		assert_TIP_and_TACK();
340d23eee88SFinn Thain 		while (1) {
341d23eee88SFinn Thain 			negate_TACK();
342d23eee88SFinn Thain 			mdelay(1);
343d23eee88SFinn Thain 			(void)in_8(&via[SR]);
344d23eee88SFinn Thain 			assert_TACK();
345d23eee88SFinn Thain 			if (!TREQ_asserted(in_8(&via[B])))
346d23eee88SFinn Thain 				break;
347d23eee88SFinn Thain 		}
348d23eee88SFinn Thain 		negate_TIP_and_TACK();
349d23eee88SFinn Thain 	} else if (in_8(&via[B]) & TIP) {
350d23eee88SFinn Thain 		/* Terminate the outbound transfer */
351d23eee88SFinn Thain 		negate_TACK();
352d23eee88SFinn Thain 		assert_TACK();
353d23eee88SFinn Thain 		mdelay(1);
354d23eee88SFinn Thain 		negate_TIP_and_TACK();
355d23eee88SFinn Thain 	}
356d23eee88SFinn Thain 	/* Clear shift register interrupt */
357d23eee88SFinn Thain 	if (in_8(&via[IFR]) & SR_INT)
358d23eee88SFinn Thain 		(void)in_8(&via[SR]);
359d23eee88SFinn Thain 	return 0;
360d23eee88SFinn Thain }
361d23eee88SFinn Thain 
3621da177e4SLinus Torvalds #define WAIT_FOR(cond, what)					\
3631da177e4SLinus Torvalds     do {                                                        \
3641da177e4SLinus Torvalds     	int x;							\
3651da177e4SLinus Torvalds 	for (x = 1000; !(cond); --x) {				\
3661da177e4SLinus Torvalds 	    if (x == 0) {					\
367523717d1SFinn Thain 		pr_err("Timeout waiting for " what "\n");	\
3681da177e4SLinus Torvalds 		return -ENXIO;					\
3691da177e4SLinus Torvalds 	    }							\
3701da177e4SLinus Torvalds 	    udelay(100);					\
3711da177e4SLinus Torvalds 	}							\
3721da177e4SLinus Torvalds     } while (0)
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds static int
cuda_init_via(void)375330dae19SGeert Uytterhoeven __init cuda_init_via(void)
3761da177e4SLinus Torvalds {
3770251c38cSFinn Thain #ifdef CONFIG_PPC
3781da177e4SLinus Torvalds     out_8(&via[IER], 0x7f);					/* disable interrupts from VIA */
3791da177e4SLinus Torvalds     (void)in_8(&via[IER]);
3800251c38cSFinn Thain #else
3810251c38cSFinn Thain     out_8(&via[IER], SR_INT);					/* disable SR interrupt from VIA */
3821da177e4SLinus Torvalds #endif
3831da177e4SLinus Torvalds 
384d23eee88SFinn Thain     out_8(&via[DIRB], (in_8(&via[DIRB]) | TACK | TIP) & ~TREQ);	/* TACK & TIP out */
385d23eee88SFinn Thain     out_8(&via[ACR], (in_8(&via[ACR]) & ~SR_CTRL) | SR_EXT);	/* SR data in */
386d23eee88SFinn Thain     (void)in_8(&via[SR]);					/* clear any left-over data */
387d23eee88SFinn Thain 
388d23eee88SFinn Thain     if (mcu_is_egret)
389d23eee88SFinn Thain 	return sync_egret();
390d23eee88SFinn Thain 
391d23eee88SFinn Thain     negate_TIP_and_TACK();
392d23eee88SFinn Thain 
3931da177e4SLinus Torvalds     /* delay 4ms and then clear any pending interrupt */
3941da177e4SLinus Torvalds     mdelay(4);
3951da177e4SLinus Torvalds     (void)in_8(&via[SR]);
3960251c38cSFinn Thain     out_8(&via[IFR], SR_INT);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds     /* sync with the CUDA - assert TACK without TIP */
399fd7a65a2SFinn Thain     assert_TACK();
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds     /* wait for the CUDA to assert TREQ in response */
402fd7a65a2SFinn Thain     WAIT_FOR(TREQ_asserted(in_8(&via[B])), "CUDA response to sync");
4031da177e4SLinus Torvalds 
4041da177e4SLinus Torvalds     /* wait for the interrupt and then clear it */
4051da177e4SLinus Torvalds     WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (2)");
4061da177e4SLinus Torvalds     (void)in_8(&via[SR]);
4070251c38cSFinn Thain     out_8(&via[IFR], SR_INT);
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds     /* finish the sync by negating TACK */
410fd7a65a2SFinn Thain     negate_TACK();
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds     /* wait for the CUDA to negate TREQ and the corresponding interrupt */
413fd7a65a2SFinn Thain     WAIT_FOR(!TREQ_asserted(in_8(&via[B])), "CUDA response to sync (3)");
4141da177e4SLinus Torvalds     WAIT_FOR(in_8(&via[IFR]) & SR_INT, "CUDA response to sync (4)");
4151da177e4SLinus Torvalds     (void)in_8(&via[SR]);
4160251c38cSFinn Thain     out_8(&via[IFR], SR_INT);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds     return 0;
4191da177e4SLinus Torvalds }
4201da177e4SLinus Torvalds 
4211da177e4SLinus Torvalds #ifdef CONFIG_ADB
4221da177e4SLinus Torvalds /* Send an ADB command */
4231da177e4SLinus Torvalds static int
cuda_send_request(struct adb_request * req,int sync)4241da177e4SLinus Torvalds cuda_send_request(struct adb_request *req, int sync)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds     int i;
4271da177e4SLinus Torvalds 
4281da177e4SLinus Torvalds     if ((via == NULL) || !cuda_fully_inited) {
4291da177e4SLinus Torvalds 	req->complete = 1;
4301da177e4SLinus Torvalds 	return -ENXIO;
4311da177e4SLinus Torvalds     }
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds     req->reply_expected = 1;
4341da177e4SLinus Torvalds 
4351da177e4SLinus Torvalds     i = cuda_write(req);
4361da177e4SLinus Torvalds     if (i)
4371da177e4SLinus Torvalds 	return i;
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds     if (sync) {
4401da177e4SLinus Torvalds 	while (!req->complete)
4411da177e4SLinus Torvalds 	    cuda_poll();
4421da177e4SLinus Torvalds     }
4431da177e4SLinus Torvalds     return 0;
4441da177e4SLinus Torvalds }
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds /* Enable/disable autopolling */
4481da177e4SLinus Torvalds static int
cuda_adb_autopoll(int devs)4491da177e4SLinus Torvalds cuda_adb_autopoll(int devs)
4501da177e4SLinus Torvalds {
4511da177e4SLinus Torvalds     struct adb_request req;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds     if ((via == NULL) || !cuda_fully_inited)
4541da177e4SLinus Torvalds 	return -ENXIO;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds     cuda_request(&req, NULL, 3, CUDA_PACKET, CUDA_AUTOPOLL, (devs? 1: 0));
4571da177e4SLinus Torvalds     while (!req.complete)
4581da177e4SLinus Torvalds 	cuda_poll();
4591da177e4SLinus Torvalds     return 0;
4601da177e4SLinus Torvalds }
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds /* Reset adb bus - how do we do this?? */
4631da177e4SLinus Torvalds static int
cuda_reset_adb_bus(void)4641da177e4SLinus Torvalds cuda_reset_adb_bus(void)
4651da177e4SLinus Torvalds {
4661da177e4SLinus Torvalds     struct adb_request req;
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds     if ((via == NULL) || !cuda_fully_inited)
4691da177e4SLinus Torvalds 	return -ENXIO;
4701da177e4SLinus Torvalds 
4711da177e4SLinus Torvalds     cuda_request(&req, NULL, 2, ADB_PACKET, 0);		/* maybe? */
4721da177e4SLinus Torvalds     while (!req.complete)
4731da177e4SLinus Torvalds 	cuda_poll();
4741da177e4SLinus Torvalds     return 0;
4751da177e4SLinus Torvalds }
4761da177e4SLinus Torvalds #endif /* CONFIG_ADB */
477523717d1SFinn Thain 
4781da177e4SLinus Torvalds /* Construct and send a cuda request */
4791da177e4SLinus Torvalds int
cuda_request(struct adb_request * req,void (* done)(struct adb_request *),int nbytes,...)4801da177e4SLinus Torvalds cuda_request(struct adb_request *req, void (*done)(struct adb_request *),
4811da177e4SLinus Torvalds 	     int nbytes, ...)
4821da177e4SLinus Torvalds {
4831da177e4SLinus Torvalds     va_list list;
4841da177e4SLinus Torvalds     int i;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds     if (via == NULL) {
4871da177e4SLinus Torvalds 	req->complete = 1;
4881da177e4SLinus Torvalds 	return -ENXIO;
4891da177e4SLinus Torvalds     }
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds     req->nbytes = nbytes;
4921da177e4SLinus Torvalds     req->done = done;
4931da177e4SLinus Torvalds     va_start(list, nbytes);
4941da177e4SLinus Torvalds     for (i = 0; i < nbytes; ++i)
4951da177e4SLinus Torvalds 	req->data[i] = va_arg(list, int);
4961da177e4SLinus Torvalds     va_end(list);
4971da177e4SLinus Torvalds     req->reply_expected = 1;
4981da177e4SLinus Torvalds     return cuda_write(req);
4991da177e4SLinus Torvalds }
5004a1b08e8SAnton Blanchard EXPORT_SYMBOL(cuda_request);
5011da177e4SLinus Torvalds 
5021da177e4SLinus Torvalds static int
cuda_write(struct adb_request * req)5031da177e4SLinus Torvalds cuda_write(struct adb_request *req)
5041da177e4SLinus Torvalds {
5051da177e4SLinus Torvalds     unsigned long flags;
5061da177e4SLinus Torvalds 
5071da177e4SLinus Torvalds     if (req->nbytes < 2 || req->data[0] > CUDA_PACKET) {
5081da177e4SLinus Torvalds 	req->complete = 1;
5091da177e4SLinus Torvalds 	return -EINVAL;
5101da177e4SLinus Torvalds     }
5111da177e4SLinus Torvalds     req->next = NULL;
5121da177e4SLinus Torvalds     req->sent = 0;
5131da177e4SLinus Torvalds     req->complete = 0;
5141da177e4SLinus Torvalds     req->reply_len = 0;
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds     spin_lock_irqsave(&cuda_lock, flags);
517928b3964SYang Li     if (current_req) {
5181da177e4SLinus Torvalds 	last_req->next = req;
5191da177e4SLinus Torvalds 	last_req = req;
5201da177e4SLinus Torvalds     } else {
5211da177e4SLinus Torvalds 	current_req = req;
5221da177e4SLinus Torvalds 	last_req = req;
5231da177e4SLinus Torvalds 	if (cuda_state == idle)
5241da177e4SLinus Torvalds 	    cuda_start();
5251da177e4SLinus Torvalds     }
5261da177e4SLinus Torvalds     spin_unlock_irqrestore(&cuda_lock, flags);
5271da177e4SLinus Torvalds 
5281da177e4SLinus Torvalds     return 0;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds static void
cuda_start(void)5321da177e4SLinus Torvalds cuda_start(void)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds     /* assert cuda_state == idle */
53506d7e994SFinn Thain     if (current_req == NULL)
5361da177e4SLinus Torvalds 	return;
53797ced1aaSFinn Thain     data_index = 0;
538fd7a65a2SFinn Thain     if (TREQ_asserted(in_8(&via[B])))
5391da177e4SLinus Torvalds 	return;			/* a byte is coming in from the CUDA */
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds     /* set the shift register to shift out and send a byte */
5421da177e4SLinus Torvalds     out_8(&via[ACR], in_8(&via[ACR]) | SR_OUT);
54397ced1aaSFinn Thain     out_8(&via[SR], current_req->data[data_index++]);
544d23eee88SFinn Thain     if (mcu_is_egret)
545d23eee88SFinn Thain 	assert_TIP_and_TACK();
546d23eee88SFinn Thain     else
547fd7a65a2SFinn Thain 	assert_TIP();
5481da177e4SLinus Torvalds     cuda_state = sent_first_byte;
5491da177e4SLinus Torvalds }
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds void
cuda_poll(void)5521da177e4SLinus Torvalds cuda_poll(void)
5531da177e4SLinus Torvalds {
55449f19ce4SOlof Johansson 	cuda_interrupt(0, NULL);
5551da177e4SLinus Torvalds }
5564a1b08e8SAnton Blanchard EXPORT_SYMBOL(cuda_poll);
5571da177e4SLinus Torvalds 
558fe73b582SFinn Thain #define ARRAY_FULL(a, p)	((p) - (a) == ARRAY_SIZE(a))
559fe73b582SFinn Thain 
5601da177e4SLinus Torvalds static irqreturn_t
cuda_interrupt(int irq,void * arg)5617d12e780SDavid Howells cuda_interrupt(int irq, void *arg)
5621da177e4SLinus Torvalds {
563ac39452eSFinn Thain     unsigned long flags;
564fd7a65a2SFinn Thain     u8 status;
5651da177e4SLinus Torvalds     struct adb_request *req = NULL;
5661da177e4SLinus Torvalds     unsigned char ibuf[16];
5671da177e4SLinus Torvalds     int ibuf_len = 0;
5681da177e4SLinus Torvalds     int complete = 0;
569458c77f3SFinn Thain     bool full;
5701da177e4SLinus Torvalds 
571ac39452eSFinn Thain     spin_lock_irqsave(&cuda_lock, flags);
5721da177e4SLinus Torvalds 
57318814ee8SFinn Thain     /* On powermacs, this handler is registered for the VIA IRQ. But they use
5740251c38cSFinn Thain      * just the shift register IRQ -- other VIA interrupt sources are disabled.
5750251c38cSFinn Thain      * On m68k macs, the VIA IRQ sources are dispatched individually. Unless
5760251c38cSFinn Thain      * we are polling, the shift register IRQ flag has already been cleared.
5770251c38cSFinn Thain      */
5780251c38cSFinn Thain 
5790251c38cSFinn Thain #ifdef CONFIG_MAC
5800251c38cSFinn Thain     if (!arg)
5810251c38cSFinn Thain #endif
5820251c38cSFinn Thain     {
5830251c38cSFinn Thain         if ((in_8(&via[IFR]) & SR_INT) == 0) {
584ac39452eSFinn Thain             spin_unlock_irqrestore(&cuda_lock, flags);
5851da177e4SLinus Torvalds             return IRQ_NONE;
5860251c38cSFinn Thain         } else {
5870251c38cSFinn Thain             out_8(&via[IFR], SR_INT);
5880251c38cSFinn Thain         }
5891da177e4SLinus Torvalds     }
5901da177e4SLinus Torvalds 
591fd7a65a2SFinn Thain     status = in_8(&via[B]) & (TIP | TACK | TREQ);
592fd7a65a2SFinn Thain 
5931da177e4SLinus Torvalds     switch (cuda_state) {
5941da177e4SLinus Torvalds     case idle:
595d23eee88SFinn Thain 	/* System controller has unsolicited data for us */
5961da177e4SLinus Torvalds 	(void)in_8(&via[SR]);
597d23eee88SFinn Thain idle_state:
598fd7a65a2SFinn Thain 	assert_TIP();
5991da177e4SLinus Torvalds 	cuda_state = reading;
6001da177e4SLinus Torvalds 	reply_ptr = cuda_rbuf;
6011da177e4SLinus Torvalds 	reading_reply = 0;
6021da177e4SLinus Torvalds 	break;
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds     case awaiting_reply:
605d23eee88SFinn Thain 	/* System controller has reply data for us */
6061da177e4SLinus Torvalds 	(void)in_8(&via[SR]);
607fd7a65a2SFinn Thain 	assert_TIP();
6081da177e4SLinus Torvalds 	cuda_state = reading;
6091da177e4SLinus Torvalds 	reply_ptr = current_req->reply;
6101da177e4SLinus Torvalds 	reading_reply = 1;
6111da177e4SLinus Torvalds 	break;
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds     case sent_first_byte:
614fd7a65a2SFinn Thain 	if (TREQ_asserted(status)) {
6151da177e4SLinus Torvalds 	    /* collision */
6161da177e4SLinus Torvalds 	    out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
6171da177e4SLinus Torvalds 	    (void)in_8(&via[SR]);
618fd7a65a2SFinn Thain 	    negate_TIP_and_TACK();
6191da177e4SLinus Torvalds 	    cuda_state = idle;
620d23eee88SFinn Thain 	    /* Egret does not raise an "aborted" interrupt */
621d23eee88SFinn Thain 	    if (mcu_is_egret)
622d23eee88SFinn Thain 		goto idle_state;
6231da177e4SLinus Torvalds 	} else {
62497ced1aaSFinn Thain 	    out_8(&via[SR], current_req->data[data_index++]);
625fd7a65a2SFinn Thain 	    toggle_TACK();
626d23eee88SFinn Thain 	    if (mcu_is_egret)
627d23eee88SFinn Thain 		assert_TACK();
6281da177e4SLinus Torvalds 	    cuda_state = sending;
6291da177e4SLinus Torvalds 	}
6301da177e4SLinus Torvalds 	break;
6311da177e4SLinus Torvalds 
6321da177e4SLinus Torvalds     case sending:
6331da177e4SLinus Torvalds 	req = current_req;
6341da177e4SLinus Torvalds 	if (data_index >= req->nbytes) {
6351da177e4SLinus Torvalds 	    out_8(&via[ACR], in_8(&via[ACR]) & ~SR_OUT);
6361da177e4SLinus Torvalds 	    (void)in_8(&via[SR]);
637fd7a65a2SFinn Thain 	    negate_TIP_and_TACK();
6381da177e4SLinus Torvalds 	    req->sent = 1;
6391da177e4SLinus Torvalds 	    if (req->reply_expected) {
6401da177e4SLinus Torvalds 		cuda_state = awaiting_reply;
6411da177e4SLinus Torvalds 	    } else {
6421da177e4SLinus Torvalds 		current_req = req->next;
6431da177e4SLinus Torvalds 		complete = 1;
6441da177e4SLinus Torvalds 		/* not sure about this */
6451da177e4SLinus Torvalds 		cuda_state = idle;
6461da177e4SLinus Torvalds 		cuda_start();
6471da177e4SLinus Torvalds 	    }
6481da177e4SLinus Torvalds 	} else {
6491da177e4SLinus Torvalds 	    out_8(&via[SR], req->data[data_index++]);
650fd7a65a2SFinn Thain 	    toggle_TACK();
651d23eee88SFinn Thain 	    if (mcu_is_egret)
652d23eee88SFinn Thain 		assert_TACK();
6531da177e4SLinus Torvalds 	}
6541da177e4SLinus Torvalds 	break;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds     case reading:
657458c77f3SFinn Thain 	full = reading_reply ? ARRAY_FULL(current_req->reply, reply_ptr)
658458c77f3SFinn Thain 	                     : ARRAY_FULL(cuda_rbuf, reply_ptr);
659458c77f3SFinn Thain 	if (full)
660fe73b582SFinn Thain 	    (void)in_8(&via[SR]);
661fe73b582SFinn Thain 	else
6621da177e4SLinus Torvalds 	    *reply_ptr++ = in_8(&via[SR]);
663458c77f3SFinn Thain 	if (!TREQ_asserted(status) || full) {
664d23eee88SFinn Thain 	    if (mcu_is_egret)
665d23eee88SFinn Thain 		assert_TACK();
6661da177e4SLinus Torvalds 	    /* that's all folks */
667fd7a65a2SFinn Thain 	    negate_TIP_and_TACK();
6681da177e4SLinus Torvalds 	    cuda_state = read_done;
669d23eee88SFinn Thain 	    /* Egret does not raise a "read done" interrupt */
670d23eee88SFinn Thain 	    if (mcu_is_egret)
671d23eee88SFinn Thain 		goto read_done_state;
6721da177e4SLinus Torvalds 	} else {
673fd7a65a2SFinn Thain 	    toggle_TACK();
674d23eee88SFinn Thain 	    if (mcu_is_egret)
675d23eee88SFinn Thain 		negate_TACK();
6761da177e4SLinus Torvalds 	}
6771da177e4SLinus Torvalds 	break;
6781da177e4SLinus Torvalds 
6791da177e4SLinus Torvalds     case read_done:
6801da177e4SLinus Torvalds 	(void)in_8(&via[SR]);
681d23eee88SFinn Thain read_done_state:
6821da177e4SLinus Torvalds 	if (reading_reply) {
6831da177e4SLinus Torvalds 	    req = current_req;
6841da177e4SLinus Torvalds 	    req->reply_len = reply_ptr - req->reply;
6851da177e4SLinus Torvalds 	    if (req->data[0] == ADB_PACKET) {
6861da177e4SLinus Torvalds 		/* Have to adjust the reply from ADB commands */
6871da177e4SLinus Torvalds 		if (req->reply_len <= 2 || (req->reply[1] & 2) != 0) {
6881da177e4SLinus Torvalds 		    /* the 0x2 bit indicates no response */
6891da177e4SLinus Torvalds 		    req->reply_len = 0;
6901da177e4SLinus Torvalds 		} else {
6911da177e4SLinus Torvalds 		    /* leave just the command and result bytes in the reply */
6921da177e4SLinus Torvalds 		    req->reply_len -= 2;
6931da177e4SLinus Torvalds 		    memmove(req->reply, req->reply + 2, req->reply_len);
6941da177e4SLinus Torvalds 		}
6951da177e4SLinus Torvalds 	    }
6961da177e4SLinus Torvalds 	    current_req = req->next;
6971da177e4SLinus Torvalds 	    complete = 1;
698cfbf9980SFinn Thain 	    reading_reply = 0;
6991da177e4SLinus Torvalds 	} else {
7001da177e4SLinus Torvalds 	    /* This is tricky. We must break the spinlock to call
7011da177e4SLinus Torvalds 	     * cuda_input. However, doing so means we might get
7021da177e4SLinus Torvalds 	     * re-entered from another CPU getting an interrupt
7031da177e4SLinus Torvalds 	     * or calling cuda_poll(). I ended up using the stack
7041da177e4SLinus Torvalds 	     * (it's only for 16 bytes) and moving the actual
7051da177e4SLinus Torvalds 	     * call to cuda_input to outside of the lock.
7061da177e4SLinus Torvalds 	     */
7071da177e4SLinus Torvalds 	    ibuf_len = reply_ptr - cuda_rbuf;
7081da177e4SLinus Torvalds 	    memcpy(ibuf, cuda_rbuf, ibuf_len);
7091da177e4SLinus Torvalds 	}
710cfbf9980SFinn Thain 	reply_ptr = cuda_rbuf;
7111da177e4SLinus Torvalds 	cuda_state = idle;
7121da177e4SLinus Torvalds 	cuda_start();
713a6466243SFinn Thain 	if (cuda_state == idle && TREQ_asserted(in_8(&via[B]))) {
714a6466243SFinn Thain 	    assert_TIP();
715a6466243SFinn Thain 	    cuda_state = reading;
7161da177e4SLinus Torvalds 	}
7171da177e4SLinus Torvalds 	break;
7181da177e4SLinus Torvalds 
7191da177e4SLinus Torvalds     default:
720523717d1SFinn Thain 	pr_err("cuda_interrupt: unknown cuda_state %d?\n", cuda_state);
7211da177e4SLinus Torvalds     }
722ac39452eSFinn Thain     spin_unlock_irqrestore(&cuda_lock, flags);
7231da177e4SLinus Torvalds     if (complete && req) {
7241da177e4SLinus Torvalds     	void (*done)(struct adb_request *) = req->done;
7251da177e4SLinus Torvalds     	mb();
7261da177e4SLinus Torvalds     	req->complete = 1;
7271da177e4SLinus Torvalds     	/* Here, we assume that if the request has a done member, the
7281da177e4SLinus Torvalds     	 * struct request will survive to setting req->complete to 1
7291da177e4SLinus Torvalds     	 */
7301da177e4SLinus Torvalds     	if (done)
7311da177e4SLinus Torvalds 		(*done)(req);
7321da177e4SLinus Torvalds     }
7331da177e4SLinus Torvalds     if (ibuf_len)
7347d12e780SDavid Howells 	cuda_input(ibuf, ibuf_len);
7351da177e4SLinus Torvalds     return IRQ_HANDLED;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds static void
cuda_input(unsigned char * buf,int nb)7397d12e780SDavid Howells cuda_input(unsigned char *buf, int nb)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds     switch (buf[0]) {
7421da177e4SLinus Torvalds     case ADB_PACKET:
7431da177e4SLinus Torvalds #ifdef CONFIG_XMON
7441da177e4SLinus Torvalds 	if (nb == 5 && buf[2] == 0x2c) {
7451da177e4SLinus Torvalds 	    extern int xmon_wants_key, xmon_adb_keycode;
7461da177e4SLinus Torvalds 	    if (xmon_wants_key) {
7471da177e4SLinus Torvalds 		xmon_adb_keycode = buf[3];
7481da177e4SLinus Torvalds 		return;
7491da177e4SLinus Torvalds 	    }
7501da177e4SLinus Torvalds 	}
7511da177e4SLinus Torvalds #endif /* CONFIG_XMON */
7521da177e4SLinus Torvalds #ifdef CONFIG_ADB
7537d12e780SDavid Howells 	adb_input(buf+2, nb-2, buf[1] & 0x40);
7541da177e4SLinus Torvalds #endif /* CONFIG_ADB */
7551da177e4SLinus Torvalds 	break;
7561da177e4SLinus Torvalds 
757d23eee88SFinn Thain     case TIMER_PACKET:
758d23eee88SFinn Thain 	/* Egret sends these periodically. Might be useful as a 'heartbeat'
759d23eee88SFinn Thain 	 * to trigger a recovery for the VIA shift register errata.
760d23eee88SFinn Thain 	 */
761d23eee88SFinn Thain 	break;
762d23eee88SFinn Thain 
7631da177e4SLinus Torvalds     default:
764523717d1SFinn Thain 	print_hex_dump(KERN_INFO, "cuda_input: ", DUMP_PREFIX_NONE, 32, 1,
765523717d1SFinn Thain 	               buf, nb, false);
7661da177e4SLinus Torvalds     }
7671da177e4SLinus Torvalds }
7680792a2c8SFinn Thain 
7690792a2c8SFinn Thain /* Offset between Unix time (1970-based) and Mac time (1904-based) */
7700792a2c8SFinn Thain #define RTC_OFFSET	2082844800
7710792a2c8SFinn Thain 
cuda_get_time(void)7720792a2c8SFinn Thain time64_t cuda_get_time(void)
7730792a2c8SFinn Thain {
7740792a2c8SFinn Thain 	struct adb_request req;
7750792a2c8SFinn Thain 	u32 now;
7760792a2c8SFinn Thain 
7770792a2c8SFinn Thain 	if (cuda_request(&req, NULL, 2, CUDA_PACKET, CUDA_GET_TIME) < 0)
7780792a2c8SFinn Thain 		return 0;
7790792a2c8SFinn Thain 	while (!req.complete)
7800792a2c8SFinn Thain 		cuda_poll();
7810792a2c8SFinn Thain 	if (req.reply_len != 7)
7820792a2c8SFinn Thain 		pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
7830792a2c8SFinn Thain 	now = (req.reply[3] << 24) + (req.reply[4] << 16) +
7840792a2c8SFinn Thain 	      (req.reply[5] << 8) + req.reply[6];
7850792a2c8SFinn Thain 	return (time64_t)now - RTC_OFFSET;
7860792a2c8SFinn Thain }
7870792a2c8SFinn Thain 
cuda_set_rtc_time(struct rtc_time * tm)7880792a2c8SFinn Thain int cuda_set_rtc_time(struct rtc_time *tm)
7890792a2c8SFinn Thain {
7900792a2c8SFinn Thain 	u32 now;
7910792a2c8SFinn Thain 	struct adb_request req;
7920792a2c8SFinn Thain 
7930792a2c8SFinn Thain 	now = lower_32_bits(rtc_tm_to_time64(tm) + RTC_OFFSET);
7940792a2c8SFinn Thain 	if (cuda_request(&req, NULL, 6, CUDA_PACKET, CUDA_SET_TIME,
7950792a2c8SFinn Thain 	                 now >> 24, now >> 16, now >> 8, now) < 0)
7960792a2c8SFinn Thain 		return -ENXIO;
7970792a2c8SFinn Thain 	while (!req.complete)
7980792a2c8SFinn Thain 		cuda_poll();
7990792a2c8SFinn Thain 	if ((req.reply_len != 3) && (req.reply_len != 7))
8000792a2c8SFinn Thain 		pr_err("%s: got %d byte reply\n", __func__, req.reply_len);
8010792a2c8SFinn Thain 	return 0;
8020792a2c8SFinn Thain }
803