]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - sys/dev/ieee488/upd7210.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.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 #if 0
101                 printf("upd7210intr [%02x %02x %02x",
102                     upd7210_rd(u, DIR), isr1, isr2);
103                 printf(" %02x %02x %02x %02x %02x] ",
104                     upd7210_rd(u, SPSR),
105                     upd7210_rd(u, ADSR),
106                     upd7210_rd(u, CPTR),
107                     upd7210_rd(u, ADR0),
108                     upd7210_rd(u, ADR1));
109                 upd7210_print_isr(isr1, isr2);
110                 printf("\n");
111 #endif
112         }
113         mtx_unlock(&u->mutex);
114 }
115
116 int
117 upd7210_take_ctrl_async(struct upd7210 *u)
118 {
119         int i;
120
121         upd7210_wr(u, AUXMR, AUXMR_TCA);
122
123         if (!(upd7210_rd(u, ADSR) & ADSR_ATN))
124                 return (0);
125         for (i = 0; i < 20; i++) {
126                 DELAY(1);
127                 if (!(upd7210_rd(u, ADSR) & ADSR_ATN))
128                         return (0);
129         }
130         return (1);
131 }
132
133 int
134 upd7210_goto_standby(struct upd7210 *u)
135 {
136         int i;
137
138         upd7210_wr(u, AUXMR, AUXMR_GTS);
139
140         if (upd7210_rd(u, ADSR) & ADSR_ATN)
141                 return (0);
142         for (i = 0; i < 20; i++) {
143                 DELAY(1);
144                 if (upd7210_rd(u, ADSR) & ADSR_ATN)
145                         return (0);
146         }
147         return (1);
148 }
149
150 /* Unaddressed Listen Only mode */
151
152 static int
153 gpib_l_irq(struct upd7210 *u, int intr __unused)
154 {
155         int i;
156
157         if (u->rreg[ISR1] & 1) {
158                 i = upd7210_rd(u, DIR);
159                 u->buf[u->buf_wp++] = i;
160                 u->buf_wp &= (u->bufsize - 1);
161                 i = (u->buf_rp + u->bufsize - u->buf_wp) & (u->bufsize - 1);
162                 if (i < 8)
163                         upd7210_wr(u, IMR1, 0);
164                 wakeup(u->buf);
165                 return (1);
166         }
167         return (0);
168 }
169
170 static int
171 gpib_l_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
172 {
173         struct upd7210 *u;
174
175         u = dev->si_drv1;
176
177         mtx_lock(&u->mutex);
178         if (u->busy) {
179                 mtx_unlock(&u->mutex);
180                 return (EBUSY);
181         }
182         u->busy = 1;
183         u->irq = gpib_l_irq;
184         mtx_unlock(&u->mutex);
185
186         u->buf = malloc(PAGE_SIZE, M_GPIB, M_WAITOK);
187         u->bufsize = PAGE_SIZE;
188         u->buf_wp = 0;
189         u->buf_rp = 0;
190
191         upd7210_wr(u, AUXMR, AUXMR_CRST);
192         DELAY(10000);
193         upd7210_wr(u, AUXMR, C_ICR | 8);
194         DELAY(1000);
195         upd7210_wr(u, ADR, 0x60);
196         upd7210_wr(u, ADR, 0xe0);
197         upd7210_wr(u, ADMR, 0x70);
198         upd7210_wr(u, AUXMR, AUXMR_PON);
199         upd7210_wr(u, IMR1, 0x01);
200         return (0);
201 }
202
203 static int
204 gpib_l_close(struct cdev *dev, int oflags, int devtype, struct thread *td)
205 {
206         struct upd7210 *u;
207
208         u = dev->si_drv1;
209
210         mtx_lock(&u->mutex);
211         u->busy = 0;
212         upd7210_wr(u, AUXMR, AUXMR_CRST);
213         DELAY(10000);
214         upd7210_wr(u, IMR1, 0x00);
215         upd7210_wr(u, IMR2, 0x00);
216         free(u->buf, M_GPIB);
217         u->buf = NULL;
218         mtx_unlock(&u->mutex);
219         return (0);
220 }
221
222 static int
223 gpib_l_read(struct cdev *dev, struct uio *uio, int ioflag)
224 {
225         struct upd7210 *u;
226         int error;
227         size_t z;
228
229         u = dev->si_drv1;
230         error = 0;
231
232         mtx_lock(&u->mutex);
233         while (u->buf_wp == u->buf_rp) {
234                 error = msleep(u->buf, &u->mutex, PZERO | PCATCH,
235                     "gpibrd", hz);
236                 if (error && error != EWOULDBLOCK) {
237                         mtx_unlock(&u->mutex);
238                         return (error);
239                 }
240         }
241         while (uio->uio_resid > 0 && u->buf_wp != u->buf_rp) {
242                 if (u->buf_wp < u->buf_rp)
243                         z = u->bufsize - u->buf_rp;
244                 else
245                         z = u->buf_wp - u->buf_rp;
246                 if (z > uio->uio_resid)
247                         z = uio->uio_resid;
248                 mtx_unlock(&u->mutex);
249                 error = uiomove(u->buf + u->buf_rp, z, uio);
250                 mtx_lock(&u->mutex);
251                 if (error)
252                         break;
253                 u->buf_rp += z;
254                 u->buf_rp &= (u->bufsize - 1);
255         }
256         if (u->wreg[IMR1] == 0)
257                 upd7210_wr(u, IMR1, 0x01);
258         mtx_unlock(&u->mutex);
259         return (error);
260 }
261
262 static struct cdevsw gpib_l_cdevsw = {
263         .d_version =    D_VERSION,
264         .d_name =       "gpib_l",
265         .d_open =       gpib_l_open,
266         .d_close =      gpib_l_close,
267         .d_read =       gpib_l_read,
268 };
269
270 /* Housekeeping */
271
272 static struct unrhdr *units;
273
274 void
275 upd7210attach(struct upd7210 *u)
276 {
277         struct cdev *dev;
278
279         if (units == NULL)
280                 units = new_unrhdr(0, minor2unit(MAXMINOR), NULL);
281         u->unit = alloc_unr(units);
282         mtx_init(&u->mutex, "gpib", NULL, MTX_DEF);
283         u->cdev = make_dev(&gpib_l_cdevsw, u->unit,
284             UID_ROOT, GID_WHEEL, 0444,
285             "gpib%ul", u->unit);
286         u->cdev->si_drv1 = u;
287
288         dev = make_dev(&gpib_ib_cdevsw, u->unit,
289             UID_ROOT, GID_WHEEL, 0444,
290             "gpib%uib", u->unit);
291         dev->si_drv1 = u;
292         dev_depends(u->cdev, dev);
293 }
294
295 void
296 upd7210detach(struct upd7210 *u)
297 {
298
299         destroy_dev(u->cdev);
300         mtx_destroy(&u->mutex);
301         free_unr(units, u->unit);
302 }