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