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