1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
33 #include <machine/stdarg.h>
34
35 #include <sys/param.h>
36 #include <sys/lock.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
39 #include <sys/bus.h>
40
41 #include <dev/ppbus/ppbconf.h>
42 #include <dev/ppbus/ppb_msq.h>
43
44 #include "ppbus_if.h"
45
46 /* msq index (see PPB_MAX_XFER)
47 * These are device modes
48 */
49 #define COMPAT_MSQ 0x0
50 #define NIBBLE_MSQ 0x1
51 #define PS2_MSQ 0x2
52 #define EPP17_MSQ 0x3
53 #define EPP19_MSQ 0x4
54 #define ECP_MSQ 0x5
55
56 /*
57 * Device mode to submsq conversion
58 */
59 static struct ppb_xfer *
60 mode2xfer(device_t bus, struct ppb_device *ppbdev, int opcode)
61 {
62 int index, epp, mode;
63 struct ppb_xfer *table;
64
65 switch (opcode) {
66 case MS_OP_GET:
67 table = ppbdev->get_xfer;
68 break;
69
70 case MS_OP_PUT:
71 table = ppbdev->put_xfer;
72 break;
73
74 default:
75 panic("%s: unknown opcode (%d)", __func__, opcode);
76 }
77
78 /* retrieve the device operating mode */
79 mode = ppb_get_mode(bus);
80 switch (mode) {
81 case PPB_COMPATIBLE:
82 index = COMPAT_MSQ;
83 break;
84 case PPB_NIBBLE:
85 index = NIBBLE_MSQ;
86 break;
87 case PPB_PS2:
88 index = PS2_MSQ;
89 break;
90 case PPB_EPP:
91 switch ((epp = ppb_get_epp_protocol(bus))) {
92 case EPP_1_7:
93 index = EPP17_MSQ;
94 break;
95 case EPP_1_9:
96 index = EPP19_MSQ;
97 break;
98 default:
99 panic("%s: unknown EPP protocol (0x%x)!", __func__,
100 epp);
101 }
102 break;
103 case PPB_ECP:
104 index = ECP_MSQ;
105 break;
106 default:
107 panic("%s: unknown mode (%d)", __func__, mode);
108 }
109
110 return (&table[index]);
111 }
112
113 /*
114 * ppb_MS_init()
115 *
116 * Initialize device dependent submicrosequence of the current mode
117 *
118 */
119 int
120 ppb_MS_init(device_t bus, device_t dev, struct ppb_microseq *loop, int opcode)
121 {
122 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
123 struct ppb_xfer *xfer = mode2xfer(bus, ppbdev, opcode);
124
125 ppb_assert_locked(bus);
126 xfer->loop = loop;
127
128 return (0);
129 }
130
131 /*
132 * ppb_MS_exec()
133 *
134 * Execute any microsequence opcode - expensive
135 *
136 */
137 int
138 ppb_MS_exec(device_t bus, device_t dev, int opcode, union ppb_insarg param1,
139 union ppb_insarg param2, union ppb_insarg param3, int *ret)
140 {
141 struct ppb_microseq msq[] = {
142 { MS_UNKNOWN, { { MS_UNKNOWN }, { MS_UNKNOWN }, { MS_UNKNOWN } } },
143 MS_RET(0)
144 };
145
146 /* initialize the corresponding microseq */
147 msq[0].opcode = opcode;
148 msq[0].arg[0] = param1;
149 msq[0].arg[1] = param2;
150 msq[0].arg[2] = param3;
151
152 /* execute the microseq */
153 return (ppb_MS_microseq(bus, dev, msq, ret));
154 }
155
156 /*
157 * ppb_MS_loop()
158 *
159 * Execute a microseq loop
160 *
161 */
162 int
163 ppb_MS_loop(device_t bus, device_t dev, struct ppb_microseq *prolog,
164 struct ppb_microseq *body, struct ppb_microseq *epilog,
165 int iter, int *ret)
166 {
167 struct ppb_microseq loop_microseq[] = {
168 MS_CALL(0), /* execute prolog */
169
170 MS_SET(MS_UNKNOWN), /* set size of transfer */
171 /* loop: */
172 MS_CALL(0), /* execute body */
173 MS_DBRA(-1 /* loop: */),
174
175 MS_CALL(0), /* execute epilog */
176 MS_RET(0)
177 };
178
179 /* initialize the structure */
180 loop_microseq[0].arg[0].p = (void *)prolog;
181 loop_microseq[1].arg[0].i = iter;
182 loop_microseq[2].arg[0].p = (void *)body;
183 loop_microseq[4].arg[0].p = (void *)epilog;
184
185 /* execute the loop */
186 return (ppb_MS_microseq(bus, dev, loop_microseq, ret));
187 }
188
189 /*
190 * ppb_MS_init_msq()
191 *
192 * Initialize a microsequence - see macros in ppb_msq.h
193 *
194 */
195 int
196 ppb_MS_init_msq(struct ppb_microseq *msq, int nbparam, ...)
197 {
198 int i;
199 int param, ins, arg, type;
200 va_list p_list;
201
202 va_start(p_list, nbparam);
203
204 for (i=0; i<nbparam; i++) {
205 /* retrieve the parameter descriptor */
206 param = va_arg(p_list, int);
207
208 ins = MS_INS(param);
209 arg = MS_ARG(param);
210 type = MS_TYP(param);
211
212 /* check the instruction position */
213 if (arg >= PPB_MS_MAXARGS)
214 panic("%s: parameter out of range (0x%x)!",
215 __func__, param);
216
217 #if 0
218 printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
219 __func__, param, ins, arg, type);
220 #endif
221
222 /* properly cast the parameter */
223 switch (type) {
224 case MS_TYP_INT:
225 msq[ins].arg[arg].i = va_arg(p_list, int);
226 break;
227
228 case MS_TYP_CHA:
229 msq[ins].arg[arg].i = (int)va_arg(p_list, int);
230 break;
231
232 case MS_TYP_PTR:
233 msq[ins].arg[arg].p = va_arg(p_list, void *);
234 break;
235
236 case MS_TYP_FUN:
237 msq[ins].arg[arg].f = va_arg(p_list, void *);
238 break;
239
240 default:
241 panic("%s: unknown parameter (0x%x)!", __func__,
242 param);
243 }
244 }
245
246 va_end(p_list);
247 return (0);
248 }
249
250 /*
251 * ppb_MS_microseq()
252 *
253 * Interprete a microsequence. Some microinstructions are executed at adapter
254 * level to avoid function call overhead between ppbus and the adapter
255 */
256 int
257 ppb_MS_microseq(device_t bus, device_t dev, struct ppb_microseq *msq, int *ret)
258 {
259 struct ppb_data *ppb = (struct ppb_data *)device_get_softc(bus);
260 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
261
262 struct ppb_microseq *mi; /* current microinstruction */
263 int error;
264
265 struct ppb_xfer *xfer;
266
267 /* microsequence executed to initialize the transfer */
268 struct ppb_microseq initxfer[] = {
269 MS_PTR(MS_UNKNOWN), /* set ptr to buffer */
270 MS_SET(MS_UNKNOWN), /* set transfer size */
271 MS_RET(0)
272 };
273
274 mtx_assert(ppb->ppc_lock, MA_OWNED);
275 if (ppb->ppb_owner != dev)
276 return (EACCES);
277
278 #define INCR_PC (mi ++)
279
280 mi = msq;
281 for (;;) {
282 switch (mi->opcode) {
283 case MS_OP_PUT:
284 case MS_OP_GET:
285
286 /* attempt to choose the best mode for the device */
287 xfer = mode2xfer(bus, ppbdev, mi->opcode);
288
289 /* figure out if we should use ieee1284 code */
290 if (!xfer->loop) {
291 if (mi->opcode == MS_OP_PUT) {
292 if ((error = PPBUS_WRITE(
293 device_get_parent(bus),
294 (char *)mi->arg[0].p,
295 mi->arg[1].i, 0)))
296 goto error;
297
298 INCR_PC;
299 goto next;
300 } else
301 panic("%s: IEEE1284 read not supported", __func__);
302 }
303
304 /* XXX should use ppb_MS_init_msq() */
305 initxfer[0].arg[0].p = mi->arg[0].p;
306 initxfer[1].arg[0].i = mi->arg[1].i;
307
308 /* initialize transfer */
309 ppb_MS_microseq(bus, dev, initxfer, &error);
310
311 if (error)
312 goto error;
313
314 /* the xfer microsequence should not contain any
315 * MS_OP_PUT or MS_OP_GET!
316 */
317 ppb_MS_microseq(bus, dev, xfer->loop, &error);
318
319 if (error)
320 goto error;
321
322 INCR_PC;
323 break;
324
325 case MS_OP_RET:
326 if (ret)
327 *ret = mi->arg[0].i; /* return code */
328 return (0);
329
330 default:
331 /* executing microinstructions at ppc level is
332 * faster. This is the default if the microinstr
333 * is unknown here
334 */
335 if ((error = PPBUS_EXEC_MICROSEQ(
336 device_get_parent(bus), &mi)))
337 goto error;
338 break;
339 }
340 next:
341 continue;
342 }
343 error:
344 return (error);
345 }
Cache object: 8fab244297192826572b8f3c66813a51
|