ppbus_msq.c revision 1.2 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 * $FreeBSD: src/sys/dev/ppbus/ppb_msq.c,v 1.9.2.1 2000/05/24 00:20:57 n_hibma Exp $
27 *
28 */
29 #include <machine/stdarg.h>
30
31 #include <sys/param.h>
32 #include <sys/systm.h>
33
34 #include <dev/ppbus/ppbus_conf.h>
35 #include <dev/ppbus/ppbus_base.h>
36 #include <dev/ppbus/ppbus_device.h>
37 #include <dev/ppbus/ppbus_msq.h>
38 #include <dev/ppbus/ppbus_var.h>
39
40 /*
41 #include "ppbus_if.h"
42 */
43
44 /*
45 * msq index (see PPBUS_MAX_XFER)
46 * These are device modes
47 */
48 #define COMPAT_MSQ 0x0
49 #define NIBBLE_MSQ 0x1
50 #define PS2_MSQ 0x2
51 #define EPP17_MSQ 0x3
52 #define EPP19_MSQ 0x4
53 #define ECP_MSQ 0x5
54
55 /* Function prototypes */
56 static struct ppbus_xfer * mode2xfer(struct ppbus_softc *,
57 struct ppbus_device_softc *, int);
58
59 /*
60 * Device mode to submsq conversion
61 */
62 static struct ppbus_xfer *
63 mode2xfer(struct ppbus_softc * bus, struct ppbus_device_softc * ppbdev,
64 int opcode)
65 {
66 int index;
67 unsigned int epp;
68 struct ppbus_xfer * table;
69
70 switch (opcode) {
71 case MS_OP_GET:
72 table = ppbdev->get_xfer;
73 break;
74
75 case MS_OP_PUT:
76 table = ppbdev->put_xfer;
77 break;
78
79 default:
80 panic("%s: unknown opcode (%d)", __func__, opcode);
81 }
82
83 /* retrieve the device operating mode */
84 switch (bus->sc_mode) {
85 case PPBUS_COMPATIBLE:
86 index = COMPAT_MSQ;
87 break;
88 case PPBUS_NIBBLE:
89 index = NIBBLE_MSQ;
90 break;
91 case PPBUS_PS2:
92 index = PS2_MSQ;
93 break;
94 case PPBUS_EPP:
95 ppbus_read_ivar(&(bus->sc_dev), PPBUS_IVAR_EPP_PROTO, &epp);
96 switch (epp) {
97 case PPBUS_EPP_1_7:
98 index = EPP17_MSQ;
99 break;
100 case PPBUS_EPP_1_9:
101 index = EPP19_MSQ;
102 break;
103 default:
104 panic("%s: unknown EPP protocol [%u]!", __func__, epp);
105 }
106 break;
107 case PPBUS_ECP:
108 index = ECP_MSQ;
109 break;
110 default:
111 panic("%s: unknown mode (%d)", __func__, ppbdev->mode);
112 }
113
114 return (&table[index]);
115 }
116
117 /*
118 * ppbus_MS_init()
119 *
120 * Initialize device dependent submicrosequence of the current mode
121 *
122 */
123 int
124 ppbus_MS_init(struct device * dev, struct device * ppbdev,
125 struct ppbus_microseq * loop, int opcode)
126 {
127 struct ppbus_softc * bus = (struct ppbus_softc *) dev;
128 struct ppbus_xfer *xfer = mode2xfer(bus, (struct ppbus_device_softc *)
129 ppbdev, opcode);
130
131 xfer->loop = loop;
132
133 return 0;
134 }
135
136 /*
137 * ppbus_MS_exec()
138 *
139 * Execute any microsequence opcode - expensive
140 *
141 */
142 int
143 ppbus_MS_exec(struct device * ppb, struct device * dev,
144 int opcode, union ppbus_insarg param1, union ppbus_insarg param2,
145 union ppbus_insarg param3, int * ret)
146 {
147 struct ppbus_microseq msq[] = {
148 { MS_UNKNOWN, { { MS_UNKNOWN }, { MS_UNKNOWN },
149 { MS_UNKNOWN } } },
150 MS_RET(0)
151 };
152
153 /* initialize the corresponding microseq */
154 msq[0].opcode = opcode;
155 msq[0].arg[0] = param1;
156 msq[0].arg[1] = param2;
157 msq[0].arg[2] = param3;
158
159 /* execute the microseq */
160 return (ppbus_MS_microseq(ppb, dev, msq, ret));
161 }
162
163 /*
164 * ppbus_MS_loop()
165 *
166 * Execute a microseq loop
167 *
168 */
169 int
170 ppbus_MS_loop(struct device * ppb, struct device * dev,
171 struct ppbus_microseq * prolog, struct ppbus_microseq * body,
172 struct ppbus_microseq * epilog, int iter, int * ret)
173 {
174 struct ppbus_microseq loop_microseq[] = {
175 MS_CALL(0), /* execute prolog */
176 MS_SET(MS_UNKNOWN), /* set size of transfer */
177
178 /* loop: */
179 MS_CALL(0), /* execute body */
180 MS_DBRA(-1 /* loop: */),
181
182 MS_CALL(0), /* execute epilog */
183 MS_RET(0)
184 };
185
186 /* initialize the structure */
187 loop_microseq[0].arg[0].p = (void *)prolog;
188 loop_microseq[1].arg[0].i = iter;
189 loop_microseq[2].arg[0].p = (void *)body;
190 loop_microseq[4].arg[0].p = (void *)epilog;
191
192 /* execute the loop */
193 return (ppbus_MS_microseq(ppb, dev, loop_microseq, ret));
194 }
195
196 /*
197 * ppbus_MS_init_msq()
198 *
199 * Initialize a microsequence - see macros in ppbus_msq.h
200 * KNF does not work here, since using '...' requires you use the
201 * standard C way of function definotion.
202 *
203 */
204 int
205 ppbus_MS_init_msq(struct ppbus_microseq * msq, int nbparam, ...)
206 {
207 int i;
208 int param, ins, arg, type;
209 va_list p_list;
210
211 va_start(p_list, nbparam);
212
213 for(i = 0; i < nbparam; i++) {
214 /* retrieve the parameter descriptor */
215 param = va_arg(p_list, int);
216
217 ins = MS_INS(param);
218 arg = MS_ARG(param);
219 type = MS_TYP(param);
220
221 /* check the instruction position */
222 if (arg >= PPBUS_MS_MAXARGS)
223 panic("%s: parameter out of range (0x%x)!", __func__,
224 param);
225
226 #if 0
227 printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
228 __func__, param, ins, arg, type);
229
230 #endif
231
232 /* properly cast the parameter */
233 switch (type) {
234 case MS_TYP_INT:
235 msq[ins].arg[arg].i = va_arg(p_list, int);
236 break;
237
238 case MS_TYP_CHA:
239 /* XXX was:
240 msq[ins].arg[arg].i = (int)va_arg(p_list, char);
241 which gives warning with gcc 3.3
242 */
243 msq[ins].arg[arg].i = (int)va_arg(p_list, int);
244 break;
245
246 case MS_TYP_PTR:
247 msq[ins].arg[arg].p = va_arg(p_list, void *);
248 break;
249
250 case MS_TYP_FUN:
251 msq[ins].arg[arg].f = va_arg(p_list, void *);
252 break;
253
254 default:
255 panic("%s: unknown parameter (0x%x)!", __func__, param);
256 }
257 }
258
259 return (0);
260 }
261
262 /*
263 * ppbus_MS_microseq()
264 *
265 * Interprete a microsequence. Some microinstructions are executed at adapter
266 * level to avoid function call overhead between ppbus and the adapter
267 */
268 int
269 ppbus_MS_microseq(struct device * dev, struct device * busdev,
270 struct ppbus_microseq * msq, int * ret)
271 {
272 struct ppbus_device_softc * ppbdev = (struct ppbus_device_softc *)
273 busdev;
274 struct ppbus_softc * bus = (struct ppbus_softc *) dev;
275 struct ppbus_microseq * mi; /* current microinstruction */
276 int error;
277 int cnt;
278
279 struct ppbus_xfer * xfer;
280
281 /* microsequence executed to initialize the transfer */
282 struct ppbus_microseq initxfer[] = {
283 MS_PTR(MS_UNKNOWN), /* set ptr to buffer */
284 MS_SET(MS_UNKNOWN), /* set transfer size */
285 MS_RET(0)
286 };
287
288 if(bus->ppbus_owner != busdev) {
289 return (EACCES);
290 }
291
292 #define INCR_PC (mi ++)
293
294 mi = msq;
295 again:
296 for (;;) {
297 switch (mi->opcode) {
298 case MS_OP_PUT:
299 case MS_OP_GET:
300
301 /* attempt to choose the best mode for the device */
302 xfer = mode2xfer(bus, ppbdev, mi->opcode);
303
304 /* figure out if we should use ieee1284 code */
305 if (!xfer->loop) {
306 if (mi->opcode == MS_OP_PUT) {
307 if ((error = ppbus_write(
308 &(bus->sc_dev),
309 (char *)mi->arg[0].p,
310 mi->arg[1].i, 0, &cnt))) {
311 goto error;
312 }
313
314 INCR_PC;
315 goto again;
316 }
317 else {
318 panic("%s: IEEE1284 read not supported",
319 __func__);
320 }
321 }
322
323 /* XXX should use ppbus_MS_init_msq() */
324 initxfer[0].arg[0].p = mi->arg[0].p;
325 initxfer[1].arg[0].i = mi->arg[1].i;
326
327 /* initialize transfer */
328 ppbus_MS_microseq(dev, busdev, initxfer, &error);
329
330 if (error)
331 goto error;
332
333 /* the xfer microsequence should not contain any
334 * MS_OP_PUT or MS_OP_GET!
335 */
336 ppbus_MS_microseq(dev, busdev, xfer->loop, &error);
337
338 if (error)
339 goto error;
340
341 INCR_PC;
342 break;
343
344 case MS_OP_RET:
345 if (ret)
346 *ret = mi->arg[0].i; /* return code */
347 return (0);
348 break;
349
350 default:
351 /* executing microinstructions at ppc level is
352 * faster. This is the default if the microinstr
353 * is unknown here
354 */
355 if((error =
356 bus->ppbus_exec_microseq(
357 (struct device *)bus, &mi))) {
358
359 goto error;
360 }
361 break;
362 }
363 }
364 error:
365 return (error);
366 }
367
368