]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/ieee488/upd7210.c
This commit was generated by cvs2svn to compensate for changes in r155094,
[FreeBSD/FreeBSD.git] / sys / dev / ieee488 / upd7210.c
1 /*-
2  * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
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  * High-level driver for µPD7210 based GPIB cards.
27  *
28  */
29
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
32
33 #  define       GPIB_DEBUG
34 #  undef        GPIB_DEBUG
35
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/conf.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/module.h>
43 #include <sys/rman.h>
44 #include <sys/bus.h>
45 #include <sys/lock.h>
46 #include <sys/mutex.h>
47 #include <sys/uio.h>
48 #include <sys/time.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <isa/isavar.h>
52
53 #define UPD7210_HW_DRIVER
54 #define UPD7210_SW_DRIVER
55 #include <dev/ieee488/upd7210.h>
56
57 static MALLOC_DEFINE(M_GPIB, "GPIB", "GPIB");
58
59 /* upd7210 generic stuff */
60
61 void
62 upd7210_print_isr(u_int isr1, u_int isr2)
63 {
64         printf("isr1=0x%b isr2=0x%b",
65             isr1, "\20\10CPT\7APT\6DET\5ENDRX\4DEC\3ERR\2DO\1DI",
66             isr2, "\20\10INT\7SRQI\6LOK\5REM\4CO\3LOKC\2REMC\1ADSC");
67 }
68
69 u_int
70 upd7210_rd(struct upd7210 *u, enum upd7210_rreg reg)
71 {
72         u_int r;
73
74         r = bus_read_1(u->reg_res[reg], u->reg_offset[reg]);
75         u->rreg[reg] = r;
76         return (r);
77 }
78
79 void
80 upd7210_wr(struct upd7210 *u, enum upd7210_wreg reg, u_int val)
81 {
82
83         bus_write_1(u->reg_res[reg], u->reg_offset[reg], val);
84         u->wreg[reg] = val;
85         if (reg == AUXMR)
86                 u->wreg[8 + (val >> 5)] = val & 0x1f;
87 }
88
89 void
90 upd7210intr(void *arg)
91 {
92         u_int isr1, isr2;
93         struct upd7210 *u;
94
95         u = arg;
96         mtx_lock(&u->mutex);
97         isr1 = upd7210_rd(u, ISR1);
98         isr2 = upd7210_rd(u, ISR2);
99         if (u->busy == 0 || u->irq == NULL || !u->irq(u, 1)) {
100                 printf("upd7210intr [%02x %02x %02x",
101                     upd7210_rd(u, DIR), isr1, isr2);
102                 printf(" %02x %02x %02x %02x %02x] ",
103                     upd7210_rd(u, SPSR),
104                     upd7210_rd(u, ADSR),
105                     upd7210_rd(u, CPTR),
106                     upd7210_rd(u, ADR0),
107                     upd7210_rd(u, ADR1));
108                 upd7210_print_isr(isr1, isr2);
109                 printf("\n");
110         }
111         mtx_unlock(&u->mutex);
112 }
113
114 int
115 upd7210_take_ctrl_async(struct upd7210 *u)
116 {
117         int i;
118
119         upd7210_wr(u, AUXMR, AUXMR_TCA);
120
121         if (!(upd7210_rd(u, ADSR) & ADSR_ATN))
122                 return (0);
123         for (i = 0; i < 20; i++) {
124                 DELAY(1);
125                 if (!(upd7210_rd(u, ADSR) & ADSR_ATN))
126                         return (0);
127         }
128         return (1);
129 }
130
131 int
132 upd7210_goto_standby(struct upd7210 *u)
133 {
134         int i;
135
136         upd7210_wr(u, AUXMR, AUXMR_GTS);
137
138         if (upd7210_rd(u, ADSR) & ADSR_ATN)
139                 return (0);
140         for (i = 0; i < 20; i++) {
141                 DELAY(1);
142                 if (upd7210_rd(u, ADSR) & ADSR_ATN)
143                         return (0);
144         }
145         return (1);
146 }
147
148 /* Unaddressed Listen Only mode */
149
150 static int
151 gpib_l_irq(struct upd7210 *u, int intr __unused)
152 {
153         int i;
154
155         if (u->rreg[ISR1] & 1) {
156                 i = upd7210_rd(u, DIR);
157                 u->buf[u->buf_wp++] = i;
158                 u->buf_wp &= (u->bufsize - 1);
159                 i = (u->buf_rp + u->bufsize - u->buf_wp) & (u->bufsize - 1);
160                 if (i < 8)
161                         upd7210_wr(u, IMR1, 0);
162                 wakeup(u->buf);
163                 return (1);
164         }
165         return (0);
166 }
167
168 static int
169 gpib_l_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
170 {
171         struct upd7210 *u;
172
173         u = dev->si_drv1;
174
175         mtx_lock(&u->mutex);
176         if (u->busy) {
177                 mtx_unlock(&u->mutex);
178                 return (EBUSY);
179         }
180         u->busy = 1;
181         u->irq = gpib_l_irq;
182         mtx_unlock(&u->mutex);
183
184         u->buf = malloc(PAGE_SIZE, M_GPIB, M_WAITOK);
185         u->bufsize = PAGE_SIZE;
186         u->buf_wp = 0;
187         u->buf_rp = 0;
188
189         upd7210_wr(u, AUXMR, AUXMR_CRST);
190         DELAY(10000);
191         upd7210_wr(u, AUXMR, C_ICR | 8);
192         DELAY(1000);
193         upd7210_wr(u, ADR, 0x60);
194         upd7210_wr(u, ADR, 0xe0);
195         upd7210_wr(u, ADMR, 0x70);
196         upd7210_wr(u, AUXMR, AUXMR_PON);
197         upd7210_wr(u, IMR1, 0x01);
198         return (0);
199 }
200
201 static int
202 gpib_l_close(struct cdev *dev, int oflags, int devtype, struct thread *td)
203 {
204         struct upd7210 *u;
205
206         u = dev->si_drv1;
207
208         mtx_lock(&u->mutex);
209         u->busy = 0;
210         upd7210_wr(u, AUXMR, AUXMR_CRST);
211         DELAY(10000);
212         upd7210_wr(u, IMR1, 0x00);
213         upd7210_wr(u, IMR2, 0x00);
214         free(u->buf, M_GPIB);
215         u->buf = NULL;
216         mtx_unlock(&u->mutex);
217         return (0);
218 }
219
220 static int
221 gpib_l_read(struct cdev *dev, struct uio *uio, int ioflag)
222 {
223         struct upd7210 *u;
224         int error;
225         size_t z;
226
227         u = dev->si_drv1;
228         error = 0;
229
230         mtx_lock(&u->mutex);
231         while (u->buf_wp == u->buf_rp) {
232                 error = msleep(u->buf, &u->mutex, PZERO | PCATCH,
233                     "gpibrd", hz);
234                 if (error && error != EWOULDBLOCK) {
235                         mtx_unlock(&u->mutex);
236                         return (error);
237                 }
238         }
239         while (uio->uio_resid > 0 && u->buf_wp != u->buf_rp) {
240                 if (u->buf_wp < u->buf_rp)
241                         z = u->bufsize - u->buf_rp;
242                 else
243                         z = u->buf_wp - u->buf_rp;
244                 if (z > uio->uio_resid)
245                         z = uio->uio_resid;
246                 mtx_unlock(&u->mutex);
247                 error = uiomove(u->buf + u->buf_rp, z, uio);
248                 mtx_lock(&u->mutex);
249                 if (error)
250                         break;
251                 u->buf_rp += z;
252                 u->buf_rp &= (u->bufsize - 1);
253         }
254         if (u->wreg[IMR1] == 0)
255                 upd7210_wr(u, IMR1, 0x01);
256         mtx_unlock(&u->mutex);
257         return (error);
258 }
259
260 static struct cdevsw gpib_l_cdevsw = {
261         .d_version =    D_VERSION,
262         .d_name =       "gpib_l",
263         .d_open =       gpib_l_open,
264         .d_close =      gpib_l_close,
265         .d_read =       gpib_l_read,
266 };
267
268 /* Housekeeping */
269
270 static struct unrhdr *units;
271
272 void
273 upd7210attach(struct upd7210 *u)
274 {
275         struct cdev *dev;
276
277         if (units == NULL)
278                 units = new_unrhdr(0, minor2unit(MAXMINOR), NULL);
279         u->unit = alloc_unr(units);
280         mtx_init(&u->mutex, "gpib", NULL, MTX_DEF);
281         u->cdev = make_dev(&gpib_l_cdevsw, u->unit,
282             UID_ROOT, GID_WHEEL, 0444,
283             "gpib%ul", u->unit);
284         u->cdev->si_drv1 = u;
285
286         dev = make_dev(&gpib_ib_cdevsw, u->unit,
287             UID_ROOT, GID_WHEEL, 0444,
288             "gpib%uib", u->unit);
289         dev->si_drv1 = u;
290         dev_depends(u->cdev, dev);
291 }
292
293 void
294 upd7210detach(struct upd7210 *u)
295 {
296
297         destroy_dev(u->cdev);
298         mtx_destroy(&u->mutex);
299         free_unr(units, u->unit);
300 }