hpib.c revision 1.5 1 1.5 mycroft /* $NetBSD: hpib.c,v 1.5 1995/01/07 10:30:12 mycroft Exp $ */
2 1.4 cgd
3 1.1 cgd /*
4 1.3 mycroft * Copyright (c) 1982, 1990, 1993
5 1.3 mycroft * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd *
35 1.4 cgd * @(#)hpib.c 8.2 (Berkeley) 1/12/94
36 1.1 cgd */
37 1.1 cgd
38 1.1 cgd /*
39 1.1 cgd * HPIB driver
40 1.1 cgd */
41 1.1 cgd #include "hpib.h"
42 1.1 cgd #if NHPIB > 0
43 1.1 cgd
44 1.3 mycroft #include <sys/param.h>
45 1.3 mycroft #include <sys/systm.h>
46 1.3 mycroft #include <sys/buf.h>
47 1.3 mycroft
48 1.3 mycroft #include <hp300/dev/device.h>
49 1.3 mycroft #include <hp300/dev/hpibvar.h>
50 1.3 mycroft #include <hp300/dev/dmavar.h>
51 1.1 cgd
52 1.3 mycroft #include <machine/cpu.h>
53 1.3 mycroft #include <hp300/hp300/isr.h>
54 1.1 cgd
55 1.1 cgd int hpibinit(), hpibstart(), hpibgo(), hpibintr(), hpibdone();
56 1.1 cgd struct driver hpibdriver = {
57 1.1 cgd hpibinit, "hpib", hpibstart, hpibgo, hpibintr, hpibdone,
58 1.1 cgd };
59 1.1 cgd
60 1.1 cgd struct hpib_softc hpib_softc[NHPIB];
61 1.1 cgd struct isr hpib_isr[NHPIB];
62 1.1 cgd int nhpibppoll(), fhpibppoll();
63 1.1 cgd
64 1.1 cgd int hpibtimeout = 100000; /* # of status tests before we give up */
65 1.3 mycroft int hpibidtimeout = 10000; /* # of status tests for hpibid() calls */
66 1.1 cgd int hpibdmathresh = 3; /* byte count beyond which to attempt dma */
67 1.1 cgd
68 1.1 cgd hpibinit(hc)
69 1.1 cgd register struct hp_ctlr *hc;
70 1.1 cgd {
71 1.1 cgd register struct hpib_softc *hs = &hpib_softc[hc->hp_unit];
72 1.1 cgd
73 1.1 cgd if (!nhpibtype(hc) && !fhpibtype(hc))
74 1.1 cgd return(0);
75 1.1 cgd hs->sc_hc = hc;
76 1.1 cgd hs->sc_dq.dq_unit = hc->hp_unit;
77 1.1 cgd hs->sc_dq.dq_driver = &hpibdriver;
78 1.1 cgd hs->sc_sq.dq_forw = hs->sc_sq.dq_back = &hs->sc_sq;
79 1.1 cgd hpib_isr[hc->hp_unit].isr_intr = hpibintr;
80 1.1 cgd hpib_isr[hc->hp_unit].isr_ipl = hc->hp_ipl;
81 1.1 cgd hpib_isr[hc->hp_unit].isr_arg = hc->hp_unit;
82 1.1 cgd isrlink(&hpib_isr[hc->hp_unit]);
83 1.1 cgd hpibreset(hc->hp_unit);
84 1.1 cgd return(1);
85 1.1 cgd }
86 1.1 cgd
87 1.1 cgd hpibreset(unit)
88 1.1 cgd register int unit;
89 1.1 cgd {
90 1.1 cgd if (hpib_softc[unit].sc_type == HPIBC)
91 1.1 cgd fhpibreset(unit);
92 1.1 cgd else
93 1.1 cgd nhpibreset(unit);
94 1.1 cgd }
95 1.1 cgd
96 1.1 cgd hpibreq(dq)
97 1.1 cgd register struct devqueue *dq;
98 1.1 cgd {
99 1.1 cgd register struct devqueue *hq;
100 1.1 cgd
101 1.1 cgd hq = &hpib_softc[dq->dq_ctlr].sc_sq;
102 1.1 cgd insque(dq, hq->dq_back);
103 1.1 cgd if (dq->dq_back == hq)
104 1.1 cgd return(1);
105 1.1 cgd return(0);
106 1.1 cgd }
107 1.1 cgd
108 1.1 cgd hpibfree(dq)
109 1.1 cgd register struct devqueue *dq;
110 1.1 cgd {
111 1.1 cgd register struct devqueue *hq;
112 1.1 cgd
113 1.1 cgd hq = &hpib_softc[dq->dq_ctlr].sc_sq;
114 1.1 cgd remque(dq);
115 1.1 cgd if ((dq = hq->dq_forw) != hq)
116 1.1 cgd (dq->dq_driver->d_start)(dq->dq_unit);
117 1.1 cgd }
118 1.1 cgd
119 1.1 cgd hpibid(unit, slave)
120 1.3 mycroft int unit, slave;
121 1.1 cgd {
122 1.1 cgd short id;
123 1.1 cgd int ohpibtimeout;
124 1.1 cgd
125 1.1 cgd /*
126 1.3 mycroft * XXX shorten timeout value so autoconfig doesn't
127 1.3 mycroft * take forever on slow CPUs.
128 1.1 cgd */
129 1.1 cgd ohpibtimeout = hpibtimeout;
130 1.3 mycroft hpibtimeout = hpibidtimeout * cpuspeed;
131 1.1 cgd if (hpibrecv(unit, 31, slave, &id, 2) != 2)
132 1.1 cgd id = 0;
133 1.1 cgd hpibtimeout = ohpibtimeout;
134 1.1 cgd return(id);
135 1.1 cgd }
136 1.1 cgd
137 1.1 cgd hpibsend(unit, slave, sec, addr, cnt)
138 1.1 cgd register int unit;
139 1.3 mycroft int slave, sec, addr, cnt;
140 1.1 cgd {
141 1.1 cgd if (hpib_softc[unit].sc_type == HPIBC)
142 1.1 cgd return(fhpibsend(unit, slave, sec, addr, cnt));
143 1.1 cgd else
144 1.1 cgd return(nhpibsend(unit, slave, sec, addr, cnt));
145 1.1 cgd }
146 1.1 cgd
147 1.1 cgd hpibrecv(unit, slave, sec, addr, cnt)
148 1.1 cgd register int unit;
149 1.3 mycroft int slave, sec, addr, cnt;
150 1.1 cgd {
151 1.1 cgd if (hpib_softc[unit].sc_type == HPIBC)
152 1.1 cgd return(fhpibrecv(unit, slave, sec, addr, cnt));
153 1.1 cgd else
154 1.1 cgd return(nhpibrecv(unit, slave, sec, addr, cnt));
155 1.1 cgd }
156 1.1 cgd
157 1.1 cgd hpibpptest(unit, slave)
158 1.1 cgd register int unit;
159 1.3 mycroft int slave;
160 1.1 cgd {
161 1.1 cgd int (*ppoll)();
162 1.1 cgd
163 1.1 cgd ppoll = (hpib_softc[unit].sc_type == HPIBC) ? fhpibppoll : nhpibppoll;
164 1.1 cgd return((*ppoll)(unit) & (0x80 >> slave));
165 1.1 cgd }
166 1.1 cgd
167 1.5 mycroft hpibppclear(unit)
168 1.5 mycroft int unit;
169 1.5 mycroft {
170 1.5 mycroft hpib_softc[unit].sc_flags &= ~HPIBF_PPOLL;
171 1.5 mycroft }
172 1.5 mycroft
173 1.1 cgd hpibawait(unit)
174 1.3 mycroft int unit;
175 1.1 cgd {
176 1.1 cgd register struct hpib_softc *hs = &hpib_softc[unit];
177 1.1 cgd
178 1.1 cgd hs->sc_flags |= HPIBF_PPOLL;
179 1.1 cgd if (hs->sc_type == HPIBC)
180 1.3 mycroft fhpibppwatch((void *)unit);
181 1.1 cgd else
182 1.3 mycroft nhpibppwatch((void *)unit);
183 1.1 cgd }
184 1.1 cgd
185 1.1 cgd hpibswait(unit, slave)
186 1.1 cgd register int unit;
187 1.3 mycroft int slave;
188 1.1 cgd {
189 1.1 cgd register int timo = hpibtimeout;
190 1.1 cgd register int mask, (*ppoll)();
191 1.1 cgd
192 1.1 cgd ppoll = (hpib_softc[unit].sc_type == HPIBC) ? fhpibppoll : nhpibppoll;
193 1.1 cgd mask = 0x80 >> slave;
194 1.1 cgd while (((ppoll)(unit) & mask) == 0)
195 1.1 cgd if (--timo == 0) {
196 1.1 cgd printf("hpib%d: swait timeout\n", unit);
197 1.1 cgd return(-1);
198 1.1 cgd }
199 1.1 cgd return(0);
200 1.1 cgd }
201 1.1 cgd
202 1.1 cgd hpibustart(unit)
203 1.3 mycroft int unit;
204 1.1 cgd {
205 1.1 cgd register struct hpib_softc *hs = &hpib_softc[unit];
206 1.1 cgd
207 1.1 cgd if (hs->sc_type == HPIBA)
208 1.1 cgd hs->sc_dq.dq_ctlr = DMA0;
209 1.1 cgd else
210 1.1 cgd hs->sc_dq.dq_ctlr = DMA0 | DMA1;
211 1.1 cgd if (dmareq(&hs->sc_dq))
212 1.1 cgd return(1);
213 1.1 cgd return(0);
214 1.1 cgd }
215 1.1 cgd
216 1.1 cgd hpibstart(unit)
217 1.3 mycroft int unit;
218 1.1 cgd {
219 1.1 cgd register struct devqueue *dq;
220 1.1 cgd
221 1.1 cgd dq = hpib_softc[unit].sc_sq.dq_forw;
222 1.1 cgd (dq->dq_driver->d_go)(dq->dq_unit);
223 1.1 cgd }
224 1.1 cgd
225 1.5 mycroft hpibgo(unit, slave, sec, addr, count, rw, timo)
226 1.1 cgd register int unit;
227 1.3 mycroft int slave, sec, addr, count, rw;
228 1.1 cgd {
229 1.1 cgd if (hpib_softc[unit].sc_type == HPIBC)
230 1.5 mycroft fhpibgo(unit, slave, sec, addr, count, rw, timo);
231 1.1 cgd else
232 1.5 mycroft nhpibgo(unit, slave, sec, addr, count, rw, timo);
233 1.1 cgd }
234 1.1 cgd
235 1.1 cgd hpibdone(unit)
236 1.1 cgd register int unit;
237 1.1 cgd {
238 1.1 cgd if (hpib_softc[unit].sc_type == HPIBC)
239 1.1 cgd fhpibdone(unit);
240 1.1 cgd else
241 1.1 cgd nhpibdone(unit);
242 1.1 cgd }
243 1.1 cgd
244 1.1 cgd hpibintr(unit)
245 1.1 cgd register int unit;
246 1.1 cgd {
247 1.1 cgd int found;
248 1.1 cgd
249 1.1 cgd if (hpib_softc[unit].sc_type == HPIBC)
250 1.1 cgd found = fhpibintr(unit);
251 1.1 cgd else
252 1.1 cgd found = nhpibintr(unit);
253 1.1 cgd return(found);
254 1.1 cgd }
255 1.1 cgd #endif
256