2 * Copyright (c) 1998, 1999 Nicolas Souchu
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
31 #include <machine/stdarg.h>
33 #include <sys/param.h>
34 #include <sys/systm.h>
37 #include <dev/ppbus/ppbconf.h>
38 #include <dev/ppbus/ppb_msq.h>
42 /* msq index (see PPB_MAX_XFER)
43 * These are device modes
45 #define COMPAT_MSQ 0x0
46 #define NIBBLE_MSQ 0x1
53 * Device mode to submsq conversion
55 static struct ppb_xfer *
56 mode2xfer(device_t bus, struct ppb_device *ppbdev, int opcode)
59 struct ppb_xfer *table;
63 table = ppbdev->get_xfer;
67 table = ppbdev->put_xfer;
71 panic("%s: unknown opcode (%d)", __func__, opcode);
74 /* retrieve the device operating mode */
75 switch (ppb_get_mode(bus)) {
86 switch ((epp = ppb_get_epp_protocol(bus))) {
94 panic("%s: unknown EPP protocol (0x%x)!", __func__,
102 panic("%s: unknown mode (%d)", __func__, ppbdev->mode);
105 return (&table[index]);
111 * Initialize device dependent submicrosequence of the current mode
115 ppb_MS_init(device_t bus, device_t dev, struct ppb_microseq *loop, int opcode)
117 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
118 struct ppb_xfer *xfer = mode2xfer(bus, ppbdev, opcode);
128 * Execute any microsequence opcode - expensive
132 ppb_MS_exec(device_t bus, device_t dev, int opcode, union ppb_insarg param1,
133 union ppb_insarg param2, union ppb_insarg param3, int *ret)
135 struct ppb_microseq msq[] = {
136 { MS_UNKNOWN, { { MS_UNKNOWN }, { MS_UNKNOWN }, { MS_UNKNOWN } } },
140 /* initialize the corresponding microseq */
141 msq[0].opcode = opcode;
142 msq[0].arg[0] = param1;
143 msq[0].arg[1] = param2;
144 msq[0].arg[2] = param3;
146 /* execute the microseq */
147 return (ppb_MS_microseq(bus, dev, msq, ret));
153 * Execute a microseq loop
157 ppb_MS_loop(device_t bus, device_t dev, struct ppb_microseq *prolog,
158 struct ppb_microseq *body, struct ppb_microseq *epilog,
161 struct ppb_microseq loop_microseq[] = {
162 MS_CALL(0), /* execute prolog */
164 MS_SET(MS_UNKNOWN), /* set size of transfer */
166 MS_CALL(0), /* execute body */
167 MS_DBRA(-1 /* loop: */),
169 MS_CALL(0), /* execute epilog */
173 /* initialize the structure */
174 loop_microseq[0].arg[0].p = (void *)prolog;
175 loop_microseq[1].arg[0].i = iter;
176 loop_microseq[2].arg[0].p = (void *)body;
177 loop_microseq[4].arg[0].p = (void *)epilog;
179 /* execute the loop */
180 return (ppb_MS_microseq(bus, dev, loop_microseq, ret));
186 * Initialize a microsequence - see macros in ppb_msq.h
190 ppb_MS_init_msq(struct ppb_microseq *msq, int nbparam, ...)
193 int param, ins, arg, type;
196 va_start(p_list, nbparam);
198 for (i=0; i<nbparam; i++) {
199 /* retrieve the parameter descriptor */
200 param = va_arg(p_list, int);
204 type = MS_TYP(param);
206 /* check the instruction position */
207 if (arg >= PPB_MS_MAXARGS)
208 panic("%s: parameter out of range (0x%x)!",
212 printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
213 __func__, param, ins, arg, type);
216 /* properly cast the parameter */
219 msq[ins].arg[arg].i = va_arg(p_list, int);
223 msq[ins].arg[arg].i = (int)va_arg(p_list, int);
227 msq[ins].arg[arg].p = va_arg(p_list, void *);
231 msq[ins].arg[arg].f = va_arg(p_list, void *);
235 panic("%s: unknown parameter (0x%x)!", __func__,
246 * Interprete a microsequence. Some microinstructions are executed at adapter
247 * level to avoid function call overhead between ppbus and the adapter
250 ppb_MS_microseq(device_t bus, device_t dev, struct ppb_microseq *msq, int *ret)
252 struct ppb_data *ppb = (struct ppb_data *)device_get_softc(bus);
253 struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
255 struct ppb_microseq *mi; /* current microinstruction */
258 struct ppb_xfer *xfer;
260 /* microsequence executed to initialize the transfer */
261 struct ppb_microseq initxfer[] = {
262 MS_PTR(MS_UNKNOWN), /* set ptr to buffer */
263 MS_SET(MS_UNKNOWN), /* set transfer size */
267 if (ppb->ppb_owner != dev)
270 #define INCR_PC (mi ++)
274 switch (mi->opcode) {
278 /* attempt to choose the best mode for the device */
279 xfer = mode2xfer(bus, ppbdev, mi->opcode);
281 /* figure out if we should use ieee1284 code */
283 if (mi->opcode == MS_OP_PUT) {
284 if ((error = PPBUS_WRITE(
285 device_get_parent(bus),
286 (char *)mi->arg[0].p,
293 panic("%s: IEEE1284 read not supported", __func__);
296 /* XXX should use ppb_MS_init_msq() */
297 initxfer[0].arg[0].p = mi->arg[0].p;
298 initxfer[1].arg[0].i = mi->arg[1].i;
300 /* initialize transfer */
301 ppb_MS_microseq(bus, dev, initxfer, &error);
306 /* the xfer microsequence should not contain any
307 * MS_OP_PUT or MS_OP_GET!
309 ppb_MS_microseq(bus, dev, xfer->loop, &error);
319 *ret = mi->arg[0].i; /* return code */
324 /* executing microinstructions at ppc level is
325 * faster. This is the default if the microinstr
328 if ((error = PPBUS_EXEC_MICROSEQ(
329 device_get_parent(bus), &mi)))