ppb_msq.c revision 225736
1/*-
2 * Copyright (c) 1998, 1999 Nicolas Souchu
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
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.
13 *
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
24 * SUCH DAMAGE.
25 *
26 *
27 */
28
29#include <sys/cdefs.h>
30__FBSDID("$FreeBSD: stable/9/sys/dev/ppbus/ppb_msq.c 187576 2009-01-21 23:10:06Z jhb $");
31#include <machine/stdarg.h>
32
33#include <sys/param.h>
34#include <sys/lock.h>
35#include <sys/mutex.h>
36#include <sys/systm.h>
37#include <sys/bus.h>
38
39#include <dev/ppbus/ppbconf.h>
40#include <dev/ppbus/ppb_msq.h>
41
42#include "ppbus_if.h"
43
44/* msq index (see PPB_MAX_XFER)
45 * These are device modes
46 */
47#define COMPAT_MSQ	0x0
48#define NIBBLE_MSQ	0x1
49#define PS2_MSQ		0x2
50#define EPP17_MSQ	0x3
51#define EPP19_MSQ	0x4
52#define ECP_MSQ		0x5
53
54/*
55 * Device mode to submsq conversion
56 */
57static struct ppb_xfer *
58mode2xfer(device_t bus, struct ppb_device *ppbdev, int opcode)
59{
60	int index, epp, mode;
61	struct ppb_xfer *table;
62
63	switch (opcode) {
64	case MS_OP_GET:
65		table = ppbdev->get_xfer;
66		break;
67
68	case MS_OP_PUT:
69		table = ppbdev->put_xfer;
70		break;
71
72	default:
73		panic("%s: unknown opcode (%d)", __func__, opcode);
74	}
75
76	/* retrieve the device operating mode */
77	mode = ppb_get_mode(bus);
78	switch (mode) {
79	case PPB_COMPATIBLE:
80		index = COMPAT_MSQ;
81		break;
82	case PPB_NIBBLE:
83		index = NIBBLE_MSQ;
84		break;
85	case PPB_PS2:
86		index = PS2_MSQ;
87		break;
88	case PPB_EPP:
89		switch ((epp = ppb_get_epp_protocol(bus))) {
90		case EPP_1_7:
91			index = EPP17_MSQ;
92			break;
93		case EPP_1_9:
94			index = EPP19_MSQ;
95			break;
96		default:
97			panic("%s: unknown EPP protocol (0x%x)!", __func__,
98				epp);
99		}
100		break;
101	case PPB_ECP:
102		index = ECP_MSQ;
103		break;
104	default:
105		panic("%s: unknown mode (%d)", __func__, mode);
106	}
107
108	return (&table[index]);
109}
110
111/*
112 * ppb_MS_init()
113 *
114 * Initialize device dependent submicrosequence of the current mode
115 *
116 */
117int
118ppb_MS_init(device_t bus, device_t dev, struct ppb_microseq *loop, int opcode)
119{
120#ifdef INVARIANTS
121	struct ppb_data *ppb = device_get_softc(bus);
122#endif
123	struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
124	struct ppb_xfer *xfer = mode2xfer(bus, ppbdev, opcode);
125
126	mtx_assert(ppb->ppc_lock, MA_OWNED);
127	xfer->loop = loop;
128
129	return (0);
130}
131
132/*
133 * ppb_MS_exec()
134 *
135 * Execute any microsequence opcode - expensive
136 *
137 */
138int
139ppb_MS_exec(device_t bus, device_t dev, int opcode, union ppb_insarg param1,
140		union ppb_insarg param2, union ppb_insarg param3, int *ret)
141{
142	struct ppb_microseq msq[] = {
143		  { MS_UNKNOWN, { { MS_UNKNOWN }, { MS_UNKNOWN }, { MS_UNKNOWN } } },
144		  MS_RET(0)
145	};
146
147	/* initialize the corresponding microseq */
148	msq[0].opcode = opcode;
149	msq[0].arg[0] = param1;
150	msq[0].arg[1] = param2;
151	msq[0].arg[2] = param3;
152
153	/* execute the microseq */
154	return (ppb_MS_microseq(bus, dev, msq, ret));
155}
156
157/*
158 * ppb_MS_loop()
159 *
160 * Execute a microseq loop
161 *
162 */
163int
164ppb_MS_loop(device_t bus, device_t dev, struct ppb_microseq *prolog,
165		struct ppb_microseq *body, struct ppb_microseq *epilog,
166		int iter, int *ret)
167{
168	struct ppb_microseq loop_microseq[] = {
169		  MS_CALL(0),			/* execute prolog */
170
171		  MS_SET(MS_UNKNOWN),		/* set size of transfer */
172	/* loop: */
173		  MS_CALL(0),			/* execute body */
174		  MS_DBRA(-1 /* loop: */),
175
176		  MS_CALL(0),			/* execute epilog */
177		  MS_RET(0)
178	};
179
180	/* initialize the structure */
181	loop_microseq[0].arg[0].p = (void *)prolog;
182	loop_microseq[1].arg[0].i = iter;
183	loop_microseq[2].arg[0].p = (void *)body;
184	loop_microseq[4].arg[0].p = (void *)epilog;
185
186	/* execute the loop */
187	return (ppb_MS_microseq(bus, dev, loop_microseq, ret));
188}
189
190/*
191 * ppb_MS_init_msq()
192 *
193 * Initialize a microsequence - see macros in ppb_msq.h
194 *
195 */
196int
197ppb_MS_init_msq(struct ppb_microseq *msq, int nbparam, ...)
198{
199	int i;
200	int param, ins, arg, type;
201	va_list p_list;
202
203	va_start(p_list, nbparam);
204
205	for (i=0; i<nbparam; i++) {
206		/* retrieve the parameter descriptor */
207		param = va_arg(p_list, int);
208
209		ins  = MS_INS(param);
210		arg  = MS_ARG(param);
211		type = MS_TYP(param);
212
213		/* check the instruction position */
214		if (arg >= PPB_MS_MAXARGS)
215			panic("%s: parameter out of range (0x%x)!",
216				__func__, param);
217
218#if 0
219		printf("%s: param = %d, ins = %d, arg = %d, type = %d\n",
220			__func__, param, ins, arg, type);
221#endif
222
223		/* properly cast the parameter */
224		switch (type) {
225		case MS_TYP_INT:
226			msq[ins].arg[arg].i = va_arg(p_list, int);
227			break;
228
229		case MS_TYP_CHA:
230			msq[ins].arg[arg].i = (int)va_arg(p_list, int);
231			break;
232
233		case MS_TYP_PTR:
234			msq[ins].arg[arg].p = va_arg(p_list, void *);
235			break;
236
237		case MS_TYP_FUN:
238			msq[ins].arg[arg].f = va_arg(p_list, void *);
239			break;
240
241		default:
242			panic("%s: unknown parameter (0x%x)!", __func__,
243				param);
244		}
245	}
246
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 */
256int
257ppb_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	}
343error:
344	return (error);
345}
346
347