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