2 * Copyright (c) 2005 Poul-Henning Kamp <phk@FreeBSD.org>
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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
26 * High-level driver for µPD7210 based GPIB cards.
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
36 #include <sys/param.h>
37 #include <sys/systm.h>
39 #include <sys/malloc.h>
40 #include <sys/kernel.h>
41 #include <sys/limits.h>
42 #include <sys/module.h>
46 #include <sys/mutex.h>
49 #include <machine/bus.h>
50 #include <machine/resource.h>
51 #include <isa/isavar.h>
53 #define UPD7210_HW_DRIVER
54 #define UPD7210_SW_DRIVER
55 #include <dev/ieee488/upd7210.h>
57 static MALLOC_DEFINE(M_GPIB, "GPIB", "GPIB");
59 /* upd7210 generic stuff */
62 upd7210_print_isr(u_int isr1, u_int isr2)
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");
70 upd7210_rd(struct upd7210 *u, enum upd7210_rreg reg)
74 r = bus_read_1(u->reg_res[reg], u->reg_offset[reg]);
80 upd7210_wr(struct upd7210 *u, enum upd7210_wreg reg, u_int val)
83 bus_write_1(u->reg_res[reg], u->reg_offset[reg], val);
86 u->wreg[8 + (val >> 5)] = val & 0x1f;
90 upd7210intr(void *arg)
97 isr1 = upd7210_rd(u, ISR1);
98 isr2 = upd7210_rd(u, ISR2);
99 if (u->busy == 0 || u->irq == NULL || !u->irq(u, 1)) {
101 printf("upd7210intr [%02x %02x %02x",
102 upd7210_rd(u, DIR), isr1, isr2);
103 printf(" %02x %02x %02x %02x %02x] ",
108 upd7210_rd(u, ADR1));
109 upd7210_print_isr(isr1, isr2);
113 mtx_unlock(&u->mutex);
117 upd7210_take_ctrl_async(struct upd7210 *u)
121 upd7210_wr(u, AUXMR, AUXMR_TCA);
123 if (!(upd7210_rd(u, ADSR) & ADSR_ATN))
125 for (i = 0; i < 20; i++) {
127 if (!(upd7210_rd(u, ADSR) & ADSR_ATN))
134 upd7210_goto_standby(struct upd7210 *u)
138 upd7210_wr(u, AUXMR, AUXMR_GTS);
140 if (upd7210_rd(u, ADSR) & ADSR_ATN)
142 for (i = 0; i < 20; i++) {
144 if (upd7210_rd(u, ADSR) & ADSR_ATN)
150 /* Unaddressed Listen Only mode */
153 gpib_l_irq(struct upd7210 *u, int intr __unused)
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);
163 upd7210_wr(u, IMR1, 0);
171 gpib_l_open(struct cdev *dev, int oflags, int devtype, struct thread *td)
179 mtx_unlock(&u->mutex);
184 mtx_unlock(&u->mutex);
186 u->buf = malloc(PAGE_SIZE, M_GPIB, M_WAITOK);
187 u->bufsize = PAGE_SIZE;
191 upd7210_wr(u, AUXMR, AUXMR_CRST);
193 upd7210_wr(u, AUXMR, C_ICR | 8);
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);
204 gpib_l_close(struct cdev *dev, int oflags, int devtype, struct thread *td)
212 upd7210_wr(u, AUXMR, AUXMR_CRST);
214 upd7210_wr(u, IMR1, 0x00);
215 upd7210_wr(u, IMR2, 0x00);
216 free(u->buf, M_GPIB);
218 mtx_unlock(&u->mutex);
223 gpib_l_read(struct cdev *dev, struct uio *uio, int ioflag)
233 while (u->buf_wp == u->buf_rp) {
234 error = msleep(u->buf, &u->mutex, PZERO | PCATCH,
236 if (error && error != EWOULDBLOCK) {
237 mtx_unlock(&u->mutex);
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;
245 z = u->buf_wp - u->buf_rp;
246 if (z > uio->uio_resid)
248 mtx_unlock(&u->mutex);
249 error = uiomove(u->buf + u->buf_rp, z, uio);
254 u->buf_rp &= (u->bufsize - 1);
256 if (u->wreg[IMR1] == 0)
257 upd7210_wr(u, IMR1, 0x01);
258 mtx_unlock(&u->mutex);
262 static struct cdevsw gpib_l_cdevsw = {
263 .d_version = D_VERSION,
265 .d_open = gpib_l_open,
266 .d_close = gpib_l_close,
267 .d_read = gpib_l_read,
272 static struct unrhdr *units;
275 upd7210attach(struct upd7210 *u)
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,
286 u->cdev->si_drv1 = u;
288 dev = make_dev(&gpib_ib_cdevsw, u->unit,
289 UID_ROOT, GID_WHEEL, 0444,
290 "gpib%uib", u->unit);
292 dev_depends(u->cdev, dev);
296 upd7210detach(struct upd7210 *u)
299 destroy_dev(u->cdev);
300 mtx_destroy(&u->mutex);
301 free_unr(units, u->unit);