xref: /freebsd/sys/dev/ppbus/ppb_msq.c (revision f05cddf9)
1 /*-
2  * Copyright (c) 1998, 1999 Nicolas Souchu
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  *
26  *
27  */
28 
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <machine/stdarg.h>
32 
33 #include <sys/param.h>
34 #include <sys/lock.h>
35 #include <sys/mutex.h>
36 #include <sys/systm.h>
37 #include <sys/bus.h>
38 
39 #include <dev/ppbus/ppbconf.h>
40 #include <dev/ppbus/ppb_msq.h>
41 
42 #include "ppbus_if.h"
43 
44 /* msq index (see PPB_MAX_XFER)
45  * These are device modes
46  */
47 #define COMPAT_MSQ	0x0
48 #define NIBBLE_MSQ	0x1
49 #define PS2_MSQ		0x2
50 #define EPP17_MSQ	0x3
51 #define EPP19_MSQ	0x4
52 #define ECP_MSQ		0x5
53 
54 /*
55  * Device mode to submsq conversion
56  */
57 static struct ppb_xfer *
58 mode2xfer(device_t bus, struct ppb_device *ppbdev, int opcode)
59 {
60 	int index, epp, mode;
61 	struct ppb_xfer *table;
62 
63 	switch (opcode) {
64 	case MS_OP_GET:
65 		table = ppbdev->get_xfer;
66 		break;
67 
68 	case MS_OP_PUT:
69 		table = ppbdev->put_xfer;
70 		break;
71 
72 	default:
73 		panic("%s: unknown opcode (%d)", __func__, opcode);
74 	}
75 
76 	/* retrieve the device operating mode */
77 	mode = ppb_get_mode(bus);
78 	switch (mode) {
79 	case PPB_COMPATIBLE:
80 		index = COMPAT_MSQ;
81 		break;
82 	case PPB_NIBBLE:
83 		index = NIBBLE_MSQ;
84 		break;
85 	case PPB_PS2:
86 		index = PS2_MSQ;
87 		break;
88 	case PPB_EPP:
89 		switch ((epp = ppb_get_epp_protocol(bus))) {
90 		case EPP_1_7:
91 			index = EPP17_MSQ;
92 			break;
93 		case EPP_1_9:
94 			index = EPP19_MSQ;
95 			break;
96 		default:
97 			panic("%s: unknown EPP protocol (0x%x)!", __func__,
98 				epp);
99 		}
100 		break;
101 	case PPB_ECP:
102 		index = ECP_MSQ;
103 		break;
104 	default:
105 		panic("%s: unknown mode (%d)", __func__, mode);
106 	}
107 
108 	return (&table[index]);
109 }
110 
111 /*
112  * ppb_MS_init()
113  *
114  * Initialize device dependent submicrosequence of the current mode
115  *
116  */
117 int
118 ppb_MS_init(device_t bus, device_t dev, struct ppb_microseq *loop, int opcode)
119 {
120 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
121 	struct ppb_xfer *xfer = mode2xfer(bus, ppbdev, opcode);
122 
123 	ppb_assert_locked(bus);
124 	xfer->loop = loop;
125 
126 	return (0);
127 }
128 
129 /*
130  * ppb_MS_exec()
131  *
132  * Execute any microsequence opcode - expensive
133  *
134  */
135 int
136 ppb_MS_exec(device_t bus, device_t dev, int opcode, union ppb_insarg param1,
137 		union ppb_insarg param2, union ppb_insarg param3, int *ret)
138 {
139 	struct ppb_microseq msq[] = {
140 		  { MS_UNKNOWN, { { MS_UNKNOWN }, { MS_UNKNOWN }, { MS_UNKNOWN } } },
141 		  MS_RET(0)
142 	};
143 
144 	/* initialize the corresponding microseq */
145 	msq[0].opcode = opcode;
146 	msq[0].arg[0] = param1;
147 	msq[0].arg[1] = param2;
148 	msq[0].arg[2] = param3;
149 
150 	/* execute the microseq */
151 	return (ppb_MS_microseq(bus, dev, msq, ret));
152 }
153 
154 /*
155  * ppb_MS_loop()
156  *
157  * Execute a microseq loop
158  *
159  */
160 int
161 ppb_MS_loop(device_t bus, device_t dev, struct ppb_microseq *prolog,
162 		struct ppb_microseq *body, struct ppb_microseq *epilog,
163 		int iter, int *ret)
164 {
165 	struct ppb_microseq loop_microseq[] = {
166 		  MS_CALL(0),			/* execute prolog */
167 
168 		  MS_SET(MS_UNKNOWN),		/* set size of transfer */
169 	/* loop: */
170 		  MS_CALL(0),			/* execute body */
171 		  MS_DBRA(-1 /* loop: */),
172 
173 		  MS_CALL(0),			/* execute epilog */
174 		  MS_RET(0)
175 	};
176 
177 	/* initialize the structure */
178 	loop_microseq[0].arg[0].p = (void *)prolog;
179 	loop_microseq[1].arg[0].i = iter;
180 	loop_microseq[2].arg[0].p = (void *)body;
181 	loop_microseq[4].arg[0].p = (void *)epilog;
182 
183 	/* execute the loop */
184 	return (ppb_MS_microseq(bus, dev, loop_microseq, ret));
185 }
186 
187 /*
188  * ppb_MS_init_msq()
189  *
190  * Initialize a microsequence - see macros in ppb_msq.h
191  *
192  */
193 int
194 ppb_MS_init_msq(struct ppb_microseq *msq, int nbparam, ...)
195 {
196 	int i;
197 	int param, ins, arg, type;
198 	va_list p_list;
199 
200 	va_start(p_list, nbparam);
201 
202 	for (i=0; i<nbparam; i++) {
203 		/* retrieve the parameter descriptor */
204 		param = va_arg(p_list, int);
205 
206 		ins  = MS_INS(param);
207 		arg  = MS_ARG(param);
208 		type = MS_TYP(param);
209 
210 		/* check the instruction position */
211 		if (arg >= PPB_MS_MAXARGS)
212 			panic("%s: parameter out of range (0x%x)!",
213 				__func__, param);
214 
215 #if 0
216 		printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
217 			__func__, param, ins, arg, type);
218 #endif
219 
220 		/* properly cast the parameter */
221 		switch (type) {
222 		case MS_TYP_INT:
223 			msq[ins].arg[arg].i = va_arg(p_list, int);
224 			break;
225 
226 		case MS_TYP_CHA:
227 			msq[ins].arg[arg].i = (int)va_arg(p_list, int);
228 			break;
229 
230 		case MS_TYP_PTR:
231 			msq[ins].arg[arg].p = va_arg(p_list, void *);
232 			break;
233 
234 		case MS_TYP_FUN:
235 			msq[ins].arg[arg].f = va_arg(p_list, void *);
236 			break;
237 
238 		default:
239 			panic("%s: unknown parameter (0x%x)!", __func__,
240 				param);
241 		}
242 	}
243 
244 	va_end(p_list);
245 	return (0);
246 }
247 
248 /*
249  * ppb_MS_microseq()
250  *
251  * Interprete a microsequence. Some microinstructions are executed at adapter
252  * level to avoid function call overhead between ppbus and the adapter
253  */
254 int
255 ppb_MS_microseq(device_t bus, device_t dev, struct ppb_microseq *msq, int *ret)
256 {
257 	struct ppb_data *ppb = (struct ppb_data *)device_get_softc(bus);
258 	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
259 
260 	struct ppb_microseq *mi;		/* current microinstruction */
261 	int error;
262 
263 	struct ppb_xfer *xfer;
264 
265 	/* microsequence executed to initialize the transfer */
266 	struct ppb_microseq initxfer[] = {
267 		MS_PTR(MS_UNKNOWN), 	/* set ptr to buffer */
268 		MS_SET(MS_UNKNOWN),	/* set transfer size */
269 		MS_RET(0)
270 	};
271 
272 	mtx_assert(ppb->ppc_lock, MA_OWNED);
273 	if (ppb->ppb_owner != dev)
274 		return (EACCES);
275 
276 #define INCR_PC (mi ++)
277 
278 	mi = msq;
279 	for (;;) {
280 		switch (mi->opcode) {
281 		case MS_OP_PUT:
282 		case MS_OP_GET:
283 
284 			/* attempt to choose the best mode for the device */
285 			xfer = mode2xfer(bus, ppbdev, mi->opcode);
286 
287 			/* figure out if we should use ieee1284 code */
288 			if (!xfer->loop) {
289 				if (mi->opcode == MS_OP_PUT) {
290 					if ((error = PPBUS_WRITE(
291 						device_get_parent(bus),
292 						(char *)mi->arg[0].p,
293 						mi->arg[1].i, 0)))
294 							goto error;
295 
296 					INCR_PC;
297 					goto next;
298 				} else
299 					panic("%s: IEEE1284 read not supported", __func__);
300 			}
301 
302 			/* XXX should use ppb_MS_init_msq() */
303 			initxfer[0].arg[0].p = mi->arg[0].p;
304 			initxfer[1].arg[0].i = mi->arg[1].i;
305 
306 			/* initialize transfer */
307 			ppb_MS_microseq(bus, dev, initxfer, &error);
308 
309 			if (error)
310 				goto error;
311 
312 			/* the xfer microsequence should not contain any
313 			 * MS_OP_PUT or MS_OP_GET!
314 			 */
315 			ppb_MS_microseq(bus, dev, xfer->loop, &error);
316 
317 			if (error)
318 				goto error;
319 
320 			INCR_PC;
321 			break;
322 
323 		case MS_OP_RET:
324 			if (ret)
325 				*ret = mi->arg[0].i;	/* return code */
326 			return (0);
327 
328 		default:
329 			/* executing microinstructions at ppc level is
330 			 * faster. This is the default if the microinstr
331 			 * is unknown here
332 			 */
333 			if ((error = PPBUS_EXEC_MICROSEQ(
334 						device_get_parent(bus), &mi)))
335 				goto error;
336 			break;
337 		}
338 	next:
339 		continue;
340 	}
341 error:
342 	return (error);
343 }
344 
345