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