2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1998, 1999 Nicolas Souchu
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
33 #include <machine/stdarg.h>
35 #include <sys/param.h>
37 #include <sys/mutex.h>
38 #include <sys/systm.h>
41 #include <dev/ppbus/ppbconf.h>
42 #include <dev/ppbus/ppb_msq.h>
46 /* msq index (see PPB_MAX_XFER)
47 * These are device modes
49 #define COMPAT_MSQ 0x0
50 #define NIBBLE_MSQ 0x1
57 * Device mode to submsq conversion
59 static struct ppb_xfer *
60 mode2xfer(device_t bus, struct ppb_device *ppbdev, int opcode)
63 struct ppb_xfer *table;
67 table = ppbdev->get_xfer;
71 table = ppbdev->put_xfer;
75 panic("%s: unknown opcode (%d)", __func__, opcode);
78 /* retrieve the device operating mode */
79 mode = ppb_get_mode(bus);
91 switch ((epp = ppb_get_epp_protocol(bus))) {
99 panic("%s: unknown EPP protocol (0x%x)!", __func__,
107 panic("%s: unknown mode (%d)", __func__, mode);
110 return (&table[index]);
116 * Initialize device dependent submicrosequence of the current mode
120 ppb_MS_init(device_t bus, device_t dev, struct ppb_microseq *loop, int opcode)
122 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
123 struct ppb_xfer *xfer = mode2xfer(bus, ppbdev, opcode);
125 ppb_assert_locked(bus);
134 * Execute any microsequence opcode - expensive
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)
141 struct ppb_microseq msq[] = {
142 { MS_UNKNOWN, { { MS_UNKNOWN }, { MS_UNKNOWN }, { MS_UNKNOWN } } },
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;
152 /* execute the microseq */
153 return (ppb_MS_microseq(bus, dev, msq, ret));
159 * Execute a microseq loop
163 ppb_MS_loop(device_t bus, device_t dev, struct ppb_microseq *prolog,
164 struct ppb_microseq *body, struct ppb_microseq *epilog,
167 struct ppb_microseq loop_microseq[] = {
168 MS_CALL(0), /* execute prolog */
170 MS_SET(MS_UNKNOWN), /* set size of transfer */
172 MS_CALL(0), /* execute body */
173 MS_DBRA(-1 /* loop: */),
175 MS_CALL(0), /* execute epilog */
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;
185 /* execute the loop */
186 return (ppb_MS_microseq(bus, dev, loop_microseq, ret));
192 * Initialize a microsequence - see macros in ppb_msq.h
196 ppb_MS_init_msq(struct ppb_microseq *msq, int nbparam, ...)
199 int param, ins, arg, type;
202 va_start(p_list, nbparam);
204 for (i=0; i<nbparam; i++) {
205 /* retrieve the parameter descriptor */
206 param = va_arg(p_list, int);
210 type = MS_TYP(param);
212 /* check the instruction position */
213 if (arg >= PPB_MS_MAXARGS)
214 panic("%s: parameter out of range (0x%x)!",
218 printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
219 __func__, param, ins, arg, type);
222 /* properly cast the parameter */
225 msq[ins].arg[arg].i = va_arg(p_list, int);
229 msq[ins].arg[arg].i = (int)va_arg(p_list, int);
233 msq[ins].arg[arg].p = va_arg(p_list, void *);
237 msq[ins].arg[arg].f = va_arg(p_list, void *);
241 panic("%s: unknown parameter (0x%x)!", __func__,
253 * Interprete a microsequence. Some microinstructions are executed at adapter
254 * level to avoid function call overhead between ppbus and the adapter
257 ppb_MS_microseq(device_t bus, device_t dev, struct ppb_microseq *msq, int *ret)
259 struct ppb_data *ppb = (struct ppb_data *)device_get_softc(bus);
260 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
262 struct ppb_microseq *mi; /* current microinstruction */
265 struct ppb_xfer *xfer;
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 */
274 mtx_assert(ppb->ppc_lock, MA_OWNED);
275 if (ppb->ppb_owner != dev)
278 #define INCR_PC (mi ++)
282 switch (mi->opcode) {
286 /* attempt to choose the best mode for the device */
287 xfer = mode2xfer(bus, ppbdev, mi->opcode);
289 /* figure out if we should use ieee1284 code */
291 if (mi->opcode == MS_OP_PUT) {
292 if ((error = PPBUS_WRITE(
293 device_get_parent(bus),
294 (char *)mi->arg[0].p,
301 panic("%s: IEEE1284 read not supported", __func__);
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;
308 /* initialize transfer */
309 ppb_MS_microseq(bus, dev, initxfer, &error);
314 /* the xfer microsequence should not contain any
315 * MS_OP_PUT or MS_OP_GET!
317 ppb_MS_microseq(bus, dev, xfer->loop, &error);
327 *ret = mi->arg[0].i; /* return code */
331 /* executing microinstructions at ppc level is
332 * faster. This is the default if the microinstr
335 if ((error = PPBUS_EXEC_MICROSEQ(
336 device_get_parent(bus), &mi)))