xref: /csrg-svn/sys/hp300/dev/hpib.c (revision 41480)
1 /*
2  * Copyright (c) 1982, 1990 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  *
7  *	@(#)hpib.c	7.1 (Berkeley) 05/08/90
8  */
9 
10 /*
11  * HPIB driver
12  */
13 #include "hpib.h"
14 #if NHPIB > 0
15 
16 #include "param.h"
17 #include "systm.h"
18 #include "buf.h"
19 #include "device.h"
20 #include "hpibvar.h"
21 #include "dmavar.h"
22 
23 #include "machine/cpu.h"
24 #include "machine/isr.h"
25 
26 int	internalhpib = IOV(0x478000);
27 
28 int	hpibinit(), hpibstart(), hpibgo(), hpibintr(), hpibdone();
29 struct	driver hpibdriver = {
30 	hpibinit, "hpib", hpibstart, hpibgo, hpibintr, hpibdone,
31 };
32 
33 struct	hpib_softc hpib_softc[NHPIB];
34 struct	isr hpib_isr[NHPIB];
35 int	nhpibppoll(), fhpibppoll();
36 
37 int	hpibtimeout = 100000;	/* # of status tests before we give up */
38 int	hpibidtimeout = 20000;	/* # of status tests for hpibid() calls */
39 
40 hpibinit(hc)
41 	register struct hp_ctlr *hc;
42 {
43 	register struct hpib_softc *hs = &hpib_softc[hc->hp_unit];
44 
45 	if (!nhpibtype(hc) && !fhpibtype(hc))
46 		return(0);
47 	hs->sc_hc = hc;
48 	hs->sc_dq.dq_unit = hc->hp_unit;
49 	hs->sc_dq.dq_driver = &hpibdriver;
50 	hs->sc_sq.dq_forw = hs->sc_sq.dq_back = &hs->sc_sq;
51 	hpib_isr[hc->hp_unit].isr_intr = hpibintr;
52 	hpib_isr[hc->hp_unit].isr_ipl = hc->hp_ipl;
53 	hpib_isr[hc->hp_unit].isr_arg = hc->hp_unit;
54 	isrlink(&hpib_isr[hc->hp_unit]);
55 	hpibreset(hc->hp_unit);
56 	return(1);
57 }
58 
59 hpibreset(unit)
60 	register int unit;
61 {
62 	if (hpib_softc[unit].sc_type == HPIBC)
63 		fhpibreset(unit);
64 	else
65 		nhpibreset(unit);
66 }
67 
68 hpibreq(dq)
69 	register struct devqueue *dq;
70 {
71 	register struct devqueue *hq;
72 
73 	hq = &hpib_softc[dq->dq_ctlr].sc_sq;
74 	insque(dq, hq->dq_back);
75 	if (dq->dq_back == hq)
76 		return(1);
77 	return(0);
78 }
79 
80 hpibfree(dq)
81 	register struct devqueue *dq;
82 {
83 	register struct devqueue *hq;
84 
85 	hq = &hpib_softc[dq->dq_ctlr].sc_sq;
86 	remque(dq);
87 	if ((dq = hq->dq_forw) != hq)
88 		(dq->dq_driver->d_start)(dq->dq_unit);
89 }
90 
91 hpibid(unit, slave)
92 {
93 	short id;
94 	int ohpibtimeout;
95 
96 	/*
97 	 * XXX: shorten timeout value (so autoconfig doesn't take forever)
98 	 */
99 	ohpibtimeout = hpibtimeout;
100 	hpibtimeout = hpibidtimeout;
101 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
102 		id = 0;
103 	hpibtimeout = ohpibtimeout;
104 	return(id);
105 }
106 
107 hpibsend(unit, slave, sec, addr, cnt)
108 	register int unit;
109 {
110 	if (hpib_softc[unit].sc_type == HPIBC)
111 		return(fhpibsend(unit, slave, sec, addr, cnt));
112 	else
113 		return(nhpibsend(unit, slave, sec, addr, cnt));
114 }
115 
116 hpibrecv(unit, slave, sec, addr, cnt)
117 	register int unit;
118 {
119 	if (hpib_softc[unit].sc_type == HPIBC)
120 		return(fhpibrecv(unit, slave, sec, addr, cnt));
121 	else
122 		return(nhpibrecv(unit, slave, sec, addr, cnt));
123 }
124 
125 hpibpptest(unit, slave)
126 	register int unit;
127 {
128 	int (*ppoll)();
129 
130 	ppoll = (hpib_softc[unit].sc_type == HPIBC) ? fhpibppoll : nhpibppoll;
131 	return((*ppoll)(unit) & (0x80 >> slave));
132 }
133 
134 hpibawait(unit)
135 {
136 	register struct hpib_softc *hs = &hpib_softc[unit];
137 
138 	hs->sc_flags |= HPIBF_PPOLL;
139 	if (hs->sc_type == HPIBC)
140 		fhpibppwatch(unit);
141 	else
142 		nhpibppwatch(unit);
143 }
144 
145 hpibswait(unit, slave)
146 	register int unit;
147 {
148 	register int timo = hpibtimeout;
149 	register int mask, (*ppoll)();
150 
151 	ppoll = (hpib_softc[unit].sc_type == HPIBC) ? fhpibppoll : nhpibppoll;
152 	mask = 0x80 >> slave;
153 	while (((ppoll)(unit) & mask) == 0)
154 		if (--timo == 0) {
155 			printf("hpib%d: swait timeout\n", unit);
156 			return(-1);
157 		}
158 	return(0);
159 }
160 
161 hpibustart(unit)
162 {
163 	register struct hpib_softc *hs = &hpib_softc[unit];
164 
165 	if (hs->sc_type == HPIBA)
166 		hs->sc_dq.dq_ctlr = DMA0;
167 	else
168 		hs->sc_dq.dq_ctlr = DMA0 | DMA1;
169 	if (dmareq(&hs->sc_dq))
170 		return(1);
171 	return(0);
172 }
173 
174 hpibstart(unit)
175 {
176 	register struct devqueue *dq;
177 
178 	dq = hpib_softc[unit].sc_sq.dq_forw;
179 	(dq->dq_driver->d_go)(dq->dq_unit);
180 }
181 
182 hpibgo(unit, slave, sec, addr, count, rw)
183 	register int unit;
184 {
185 	if (hpib_softc[unit].sc_type == HPIBC)
186 		fhpibgo(unit, slave, sec, addr, count, rw);
187 	else
188 		nhpibgo(unit, slave, sec, addr, count, rw);
189 }
190 
191 hpibdone(unit)
192 	register int unit;
193 {
194 	if (hpib_softc[unit].sc_type == HPIBC)
195 		fhpibdone(unit);
196 	else
197 		nhpibdone(unit);
198 }
199 
200 hpibintr(unit)
201 	register int unit;
202 {
203 	int found;
204 
205 	if (hpib_softc[unit].sc_type == HPIBC)
206 		found = fhpibintr(unit);
207 	else
208 		found = nhpibintr(unit);
209 	return(found);
210 }
211 #endif
212