• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Driver for the ADB controller in the Mac I/O (Hydra) chip.
4  */
5 #include <stdarg.h>
6 #include <linux/types.h>
7 #include <linux/errno.h>
8 #include <linux/kernel.h>
9 #include <linux/delay.h>
10 #include <linux/spinlock.h>
11 #include <linux/interrupt.h>
12 #include <linux/pgtable.h>
13 #include <asm/prom.h>
14 #include <linux/adb.h>
15 #include <asm/io.h>
16 #include <asm/hydra.h>
17 #include <asm/irq.h>
18 #include <linux/init.h>
19 #include <linux/ioport.h>
20 
21 struct preg {
22 	unsigned char r;
23 	char pad[15];
24 };
25 
26 struct adb_regs {
27 	struct preg intr;
28 	struct preg data[9];
29 	struct preg intr_enb;
30 	struct preg dcount;
31 	struct preg error;
32 	struct preg ctrl;
33 	struct preg autopoll;
34 	struct preg active_hi;
35 	struct preg active_lo;
36 	struct preg test;
37 };
38 
39 /* Bits in intr and intr_enb registers */
40 #define DFB	1		/* data from bus */
41 #define TAG	2		/* transfer access grant */
42 
43 /* Bits in dcount register */
44 #define HMB	0x0f		/* how many bytes */
45 #define APD	0x10		/* auto-poll data */
46 
47 /* Bits in error register */
48 #define NRE	1		/* no response error */
49 #define DLE	2		/* data lost error */
50 
51 /* Bits in ctrl register */
52 #define TAR	1		/* transfer access request */
53 #define DTB	2		/* data to bus */
54 #define CRE	4		/* command response expected */
55 #define ADB_RST	8		/* ADB reset */
56 
57 /* Bits in autopoll register */
58 #define APE	1		/* autopoll enable */
59 
60 static volatile struct adb_regs __iomem *adb;
61 static struct adb_request *current_req, *last_req;
62 static DEFINE_SPINLOCK(macio_lock);
63 
64 static int macio_probe(void);
65 static int macio_init(void);
66 static irqreturn_t macio_adb_interrupt(int irq, void *arg);
67 static int macio_send_request(struct adb_request *req, int sync);
68 static int macio_adb_autopoll(int devs);
69 static void macio_adb_poll(void);
70 static int macio_adb_reset_bus(void);
71 
72 struct adb_driver macio_adb_driver = {
73 	.name         = "MACIO",
74 	.probe        = macio_probe,
75 	.init         = macio_init,
76 	.send_request = macio_send_request,
77 	.autopoll     = macio_adb_autopoll,
78 	.poll         = macio_adb_poll,
79 	.reset_bus    = macio_adb_reset_bus,
80 };
81 
macio_probe(void)82 int macio_probe(void)
83 {
84 	struct device_node *np;
85 
86 	np = of_find_compatible_node(NULL, "adb", "chrp,adb0");
87 	if (np) {
88 		of_node_put(np);
89 		return 0;
90 	}
91 	return -ENODEV;
92 }
93 
macio_init(void)94 int macio_init(void)
95 {
96 	struct device_node *adbs;
97 	struct resource r;
98 	unsigned int irq;
99 
100 	adbs = of_find_compatible_node(NULL, "adb", "chrp,adb0");
101 	if (adbs == 0)
102 		return -ENXIO;
103 
104 	if (of_address_to_resource(adbs, 0, &r)) {
105 		of_node_put(adbs);
106 		return -ENXIO;
107 	}
108 	adb = ioremap(r.start, sizeof(struct adb_regs));
109 	if (!adb) {
110 		of_node_put(adbs);
111 		return -ENOMEM;
112 	}
113 
114 	out_8(&adb->ctrl.r, 0);
115 	out_8(&adb->intr.r, 0);
116 	out_8(&adb->error.r, 0);
117 	out_8(&adb->active_hi.r, 0xff); /* for now, set all devices active */
118 	out_8(&adb->active_lo.r, 0xff);
119 	out_8(&adb->autopoll.r, APE);
120 
121 	irq = irq_of_parse_and_map(adbs, 0);
122 	of_node_put(adbs);
123 	if (request_irq(irq, macio_adb_interrupt, 0, "ADB", (void *)0)) {
124 		printk(KERN_ERR "ADB: can't get irq %d\n", irq);
125 		return -EAGAIN;
126 	}
127 	out_8(&adb->intr_enb.r, DFB | TAG);
128 
129 	printk("adb: mac-io driver 1.0 for unified ADB\n");
130 
131 	return 0;
132 }
133 
macio_adb_autopoll(int devs)134 static int macio_adb_autopoll(int devs)
135 {
136 	unsigned long flags;
137 
138 	spin_lock_irqsave(&macio_lock, flags);
139 	out_8(&adb->active_hi.r, devs >> 8);
140 	out_8(&adb->active_lo.r, devs);
141 	out_8(&adb->autopoll.r, devs? APE: 0);
142 	spin_unlock_irqrestore(&macio_lock, flags);
143 	return 0;
144 }
145 
macio_adb_reset_bus(void)146 static int macio_adb_reset_bus(void)
147 {
148 	unsigned long flags;
149 	int timeout = 1000000;
150 
151 	/* Hrm... we may want to not lock interrupts for so
152 	 * long ... oh well, who uses that chip anyway ? :)
153 	 * That function will be seldom used during boot
154 	 * on rare machines, so...
155 	 */
156 	spin_lock_irqsave(&macio_lock, flags);
157 	out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | ADB_RST);
158 	while ((in_8(&adb->ctrl.r) & ADB_RST) != 0) {
159 		if (--timeout == 0) {
160 			out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) & ~ADB_RST);
161 			spin_unlock_irqrestore(&macio_lock, flags);
162 			return -1;
163 		}
164 	}
165 	spin_unlock_irqrestore(&macio_lock, flags);
166 	return 0;
167 }
168 
169 /* Send an ADB command */
macio_send_request(struct adb_request * req,int sync)170 static int macio_send_request(struct adb_request *req, int sync)
171 {
172 	unsigned long flags;
173 	int i;
174 
175 	if (req->data[0] != ADB_PACKET)
176 		return -EINVAL;
177 
178 	for (i = 0; i < req->nbytes - 1; ++i)
179 		req->data[i] = req->data[i+1];
180 	--req->nbytes;
181 
182 	req->next = NULL;
183 	req->sent = 0;
184 	req->complete = 0;
185 	req->reply_len = 0;
186 
187 	spin_lock_irqsave(&macio_lock, flags);
188 	if (current_req != 0) {
189 		last_req->next = req;
190 		last_req = req;
191 	} else {
192 		current_req = last_req = req;
193 		out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
194 	}
195 	spin_unlock_irqrestore(&macio_lock, flags);
196 
197 	if (sync) {
198 		while (!req->complete)
199 			macio_adb_poll();
200 	}
201 
202 	return 0;
203 }
204 
macio_adb_interrupt(int irq,void * arg)205 static irqreturn_t macio_adb_interrupt(int irq, void *arg)
206 {
207 	int i, n, err;
208 	struct adb_request *req = NULL;
209 	unsigned char ibuf[16];
210 	int ibuf_len = 0;
211 	int complete = 0;
212 	int autopoll = 0;
213 	int handled = 0;
214 
215 	spin_lock(&macio_lock);
216 	if (in_8(&adb->intr.r) & TAG) {
217 		handled = 1;
218 		if ((req = current_req) != 0) {
219 			/* put the current request in */
220 			for (i = 0; i < req->nbytes; ++i)
221 				out_8(&adb->data[i].r, req->data[i]);
222 			out_8(&adb->dcount.r, req->nbytes & HMB);
223 			req->sent = 1;
224 			if (req->reply_expected) {
225 				out_8(&adb->ctrl.r, DTB + CRE);
226 			} else {
227 				out_8(&adb->ctrl.r, DTB);
228 				current_req = req->next;
229 				complete = 1;
230 				if (current_req)
231 					out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
232 			}
233 		}
234 		out_8(&adb->intr.r, 0);
235 	}
236 
237 	if (in_8(&adb->intr.r) & DFB) {
238 		handled = 1;
239 		err = in_8(&adb->error.r);
240 		if (current_req && current_req->sent) {
241 			/* this is the response to a command */
242 			req = current_req;
243 			if (err == 0) {
244 				req->reply_len = in_8(&adb->dcount.r) & HMB;
245 				for (i = 0; i < req->reply_len; ++i)
246 					req->reply[i] = in_8(&adb->data[i].r);
247 			}
248 			current_req = req->next;
249 			complete = 1;
250 			if (current_req)
251 				out_8(&adb->ctrl.r, in_8(&adb->ctrl.r) | TAR);
252 		} else if (err == 0) {
253 			/* autopoll data */
254 			n = in_8(&adb->dcount.r) & HMB;
255 			for (i = 0; i < n; ++i)
256 				ibuf[i] = in_8(&adb->data[i].r);
257 			ibuf_len = n;
258 			autopoll = (in_8(&adb->dcount.r) & APD) != 0;
259 		}
260 		out_8(&adb->error.r, 0);
261 		out_8(&adb->intr.r, 0);
262 	}
263 	spin_unlock(&macio_lock);
264 	if (complete && req) {
265 	    void (*done)(struct adb_request *) = req->done;
266 	    mb();
267 	    req->complete = 1;
268 	    /* Here, we assume that if the request has a done member, the
269     	     * struct request will survive to setting req->complete to 1
270 	     */
271 	    if (done)
272 		(*done)(req);
273 	}
274 	if (ibuf_len)
275 		adb_input(ibuf, ibuf_len, autopoll);
276 
277 	return IRQ_RETVAL(handled);
278 }
279 
macio_adb_poll(void)280 static void macio_adb_poll(void)
281 {
282 	unsigned long flags;
283 
284 	local_irq_save(flags);
285 	if (in_8(&adb->intr.r) != 0)
286 		macio_adb_interrupt(0, NULL);
287 	local_irq_restore(flags);
288 }
289