]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - sys/dev/ppbus/ppb_msq.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / sys / dev / ppbus / ppb_msq.c
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$");
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  */
57 static struct ppb_xfer *
58 mode2xfer(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  */
117 int
118 ppb_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  */
138 int
139 ppb_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  */
163 int
164 ppb_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  */
196 int
197 ppb_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         va_end(p_list);
248         return (0);
249 }
250
251 /*
252  * ppb_MS_microseq()
253  *
254  * Interprete a microsequence. Some microinstructions are executed at adapter
255  * level to avoid function call overhead between ppbus and the adapter
256  */
257 int
258 ppb_MS_microseq(device_t bus, device_t dev, struct ppb_microseq *msq, int *ret)
259 {
260         struct ppb_data *ppb = (struct ppb_data *)device_get_softc(bus);
261         struct ppb_device *ppbdev = (struct ppb_device *)device_get_ivars(dev);
262
263         struct ppb_microseq *mi;                /* current microinstruction */
264         int error;
265
266         struct ppb_xfer *xfer;
267
268         /* microsequence executed to initialize the transfer */
269         struct ppb_microseq initxfer[] = {
270                 MS_PTR(MS_UNKNOWN),     /* set ptr to buffer */
271                 MS_SET(MS_UNKNOWN),     /* set transfer size */
272                 MS_RET(0)
273         };
274
275         mtx_assert(ppb->ppc_lock, MA_OWNED);
276         if (ppb->ppb_owner != dev)
277                 return (EACCES);
278
279 #define INCR_PC (mi ++)
280
281         mi = msq;
282         for (;;) {
283                 switch (mi->opcode) {
284                 case MS_OP_PUT:
285                 case MS_OP_GET:
286
287                         /* attempt to choose the best mode for the device */
288                         xfer = mode2xfer(bus, ppbdev, mi->opcode);
289
290                         /* figure out if we should use ieee1284 code */
291                         if (!xfer->loop) {
292                                 if (mi->opcode == MS_OP_PUT) {
293                                         if ((error = PPBUS_WRITE(
294                                                 device_get_parent(bus),
295                                                 (char *)mi->arg[0].p,
296                                                 mi->arg[1].i, 0)))
297                                                         goto error;
298
299                                         INCR_PC;
300                                         goto next;
301                                 } else
302                                         panic("%s: IEEE1284 read not supported", __func__);
303                         }
304
305                         /* XXX should use ppb_MS_init_msq() */
306                         initxfer[0].arg[0].p = mi->arg[0].p;
307                         initxfer[1].arg[0].i = mi->arg[1].i;
308
309                         /* initialize transfer */
310                         ppb_MS_microseq(bus, dev, initxfer, &error);
311
312                         if (error)
313                                 goto error;
314
315                         /* the xfer microsequence should not contain any
316                          * MS_OP_PUT or MS_OP_GET!
317                          */
318                         ppb_MS_microseq(bus, dev, xfer->loop, &error);
319
320                         if (error)
321                                 goto error;
322
323                         INCR_PC;
324                         break;
325
326                 case MS_OP_RET:
327                         if (ret)
328                                 *ret = mi->arg[0].i;    /* return code */
329                         return (0);
330
331                 default:
332                         /* executing microinstructions at ppc level is
333                          * faster. This is the default if the microinstr
334                          * is unknown here
335                          */
336                         if ((error = PPBUS_EXEC_MICROSEQ(
337                                                 device_get_parent(bus), &mi)))
338                                 goto error;
339                         break;
340                 }
341         next:
342                 continue;
343         }
344 error:
345         return (error);
346 }
347