1 /* $NetBSD: ppbus_msq.c,v 1.8 2008/04/15 15:02:29 cegger Exp $ */
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
28 * FreeBSD: src/sys/dev/ppbus/ppb_msq.c,v 1.9.2.1 2000/05/24 00:20:57 n_hibma Exp
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: ppbus_msq.c,v 1.8 2008/04/15 15:02:29 cegger Exp $");
35 #include <machine/stdarg.h>
37 #include <sys/param.h>
38 #include <sys/systm.h>
40 #include <dev/ppbus/ppbus_conf.h>
41 #include <dev/ppbus/ppbus_base.h>
42 #include <dev/ppbus/ppbus_device.h>
43 #include <dev/ppbus/ppbus_msq.h>
44 #include <dev/ppbus/ppbus_var.h>
51 * msq index (see PPBUS_MAX_XFER)
52 * These are device modes
54 #define COMPAT_MSQ 0x0
55 #define NIBBLE_MSQ 0x1
61 /* Function prototypes */
62 static struct ppbus_xfer
* mode2xfer(struct ppbus_softc
*,
63 struct ppbus_device_softc
*, int);
66 * Device mode to submsq conversion
68 static struct ppbus_xfer
*
69 mode2xfer(struct ppbus_softc
* bus
, struct ppbus_device_softc
* ppbdev
,
74 struct ppbus_xfer
* table
;
78 table
= ppbdev
->get_xfer
;
82 table
= ppbdev
->put_xfer
;
86 panic("%s: unknown opcode (%d)", __func__
, opcode
);
89 /* retrieve the device operating mode */
90 switch (bus
->sc_mode
) {
91 case PPBUS_COMPATIBLE
:
101 ppbus_read_ivar(bus
->sc_dev
, PPBUS_IVAR_EPP_PROTO
, &epp
);
110 panic("%s: unknown EPP protocol [%u]!", __func__
, epp
);
117 panic("%s: unknown mode (%d)", __func__
, ppbdev
->mode
);
120 return (&table
[index
]);
126 * Initialize device dependent submicrosequence of the current mode
130 ppbus_MS_init(device_t dev
, device_t ppbdev
,
131 struct ppbus_microseq
* loop
, int opcode
)
133 struct ppbus_softc
* bus
= device_private(dev
);
134 struct ppbus_xfer
*xfer
= mode2xfer(bus
, (struct ppbus_device_softc
*)
145 * Execute any microsequence opcode - expensive
149 ppbus_MS_exec(device_t ppb
, device_t dev
,
150 int opcode
, union ppbus_insarg param1
, union ppbus_insarg param2
,
151 union ppbus_insarg param3
, int * ret
)
153 struct ppbus_microseq msq
[] = {
154 { MS_UNKNOWN
, { { MS_UNKNOWN
}, { MS_UNKNOWN
},
159 /* initialize the corresponding microseq */
160 msq
[0].opcode
= opcode
;
161 msq
[0].arg
[0] = param1
;
162 msq
[0].arg
[1] = param2
;
163 msq
[0].arg
[2] = param3
;
165 /* execute the microseq */
166 return (ppbus_MS_microseq(ppb
, dev
, msq
, ret
));
172 * Execute a microseq loop
176 ppbus_MS_loop(device_t ppb
, device_t dev
,
177 struct ppbus_microseq
* prolog
, struct ppbus_microseq
* body
,
178 struct ppbus_microseq
* epilog
, int iter
, int * ret
)
180 struct ppbus_microseq loop_microseq
[] = {
181 MS_CALL(0), /* execute prolog */
182 MS_SET(MS_UNKNOWN
), /* set size of transfer */
185 MS_CALL(0), /* execute body */
186 MS_DBRA(-1 /* loop: */),
188 MS_CALL(0), /* execute epilog */
192 /* initialize the structure */
193 loop_microseq
[0].arg
[0].p
= (void *)prolog
;
194 loop_microseq
[1].arg
[0].i
= iter
;
195 loop_microseq
[2].arg
[0].p
= (void *)body
;
196 loop_microseq
[4].arg
[0].p
= (void *)epilog
;
198 /* execute the loop */
199 return (ppbus_MS_microseq(ppb
, dev
, loop_microseq
, ret
));
203 * ppbus_MS_init_msq()
205 * Initialize a microsequence - see macros in ppbus_msq.h
206 * KNF does not work here, since using '...' requires you use the
207 * standard C way of function definotion.
211 ppbus_MS_init_msq(struct ppbus_microseq
* msq
, int nbparam
, ...)
214 int param
, ins
, arg
, type
;
217 va_start(p_list
, nbparam
);
219 for(i
= 0; i
< nbparam
; i
++) {
220 /* retrieve the parameter descriptor */
221 param
= va_arg(p_list
, int);
225 type
= MS_TYP(param
);
227 /* check the instruction position */
228 if (arg
>= PPBUS_MS_MAXARGS
)
229 panic("%s: parameter out of range (0x%x)!", __func__
,
233 printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
234 __func__
, param
, ins
, arg
, type
);
238 /* properly cast the parameter */
241 msq
[ins
].arg
[arg
].i
= va_arg(p_list
, int);
246 msq[ins].arg[arg].i = (int)va_arg(p_list, char);
247 which gives warning with gcc 3.3
249 msq
[ins
].arg
[arg
].i
= (int)va_arg(p_list
, int);
253 msq
[ins
].arg
[arg
].p
= va_arg(p_list
, void *);
257 msq
[ins
].arg
[arg
].f
= va_arg(p_list
, void *);
261 panic("%s: unknown parameter (0x%x)!", __func__
, param
);
269 * ppbus_MS_microseq()
271 * Interprete a microsequence. Some microinstructions are executed at adapter
272 * level to avoid function call overhead between ppbus and the adapter
275 ppbus_MS_microseq(device_t dev
, device_t busdev
,
276 struct ppbus_microseq
* msq
, int * ret
)
278 struct ppbus_device_softc
* ppbdev
= device_private(busdev
);
279 struct ppbus_softc
* bus
= device_private(dev
);
280 struct ppbus_microseq
* mi
; /* current microinstruction */
284 struct ppbus_xfer
* xfer
;
286 /* microsequence executed to initialize the transfer */
287 struct ppbus_microseq initxfer
[] = {
288 MS_PTR(MS_UNKNOWN
), /* set ptr to buffer */
289 MS_SET(MS_UNKNOWN
), /* set transfer size */
293 if(bus
->ppbus_owner
!= busdev
) {
297 #define INCR_PC (mi ++)
302 switch (mi
->opcode
) {
306 /* attempt to choose the best mode for the device */
307 xfer
= mode2xfer(bus
, ppbdev
, mi
->opcode
);
309 /* figure out if we should use ieee1284 code */
311 if (mi
->opcode
== MS_OP_PUT
) {
312 if ((error
= ppbus_write(
314 (char *)mi
->arg
[0].p
,
315 mi
->arg
[1].i
, 0, &cnt
))) {
323 panic("%s: IEEE1284 read not supported",
328 /* XXX should use ppbus_MS_init_msq() */
329 initxfer
[0].arg
[0].p
= mi
->arg
[0].p
;
330 initxfer
[1].arg
[0].i
= mi
->arg
[1].i
;
332 /* initialize transfer */
333 ppbus_MS_microseq(dev
, busdev
, initxfer
, &error
);
338 /* the xfer microsequence should not contain any
339 * MS_OP_PUT or MS_OP_GET!
341 ppbus_MS_microseq(dev
, busdev
, xfer
->loop
, &error
);
351 *ret
= mi
->arg
[0].i
; /* return code */
356 /* executing microinstructions at ppc level is
357 * faster. This is the default if the microinstr
361 bus
->ppbus_exec_microseq(
362 bus
->sc_dev
, &mi
))) {