xref: /original-bsd/sys/hp300/dev/hpib.c (revision e58d00f4)
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.5 (Berkeley) 07/07/92
8  */
9 
10 /*
11  * HPIB driver
12  */
13 #include "hpib.h"
14 #if NHPIB > 0
15 
16 #include "sys/param.h"
17 #include "sys/systm.h"
18 #include "sys/buf.h"
19 #include "hp/dev/device.h"
20 #include "hpibvar.h"
21 #include "dmavar.h"
22 
23 #include "../include/cpu.h"
24 #include "../hp300/isr.h"
25 
26 int	hpibinit(), hpibstart(), hpibgo(), hpibintr(), hpibdone();
27 struct	driver hpibdriver = {
28 	hpibinit, "hpib", hpibstart, hpibgo, hpibintr, hpibdone,
29 };
30 
31 struct	hpib_softc hpib_softc[NHPIB];
32 struct	isr hpib_isr[NHPIB];
33 int	nhpibppoll(), fhpibppoll();
34 
35 int	hpibtimeout = 100000;	/* # of status tests before we give up */
36 int	hpibidtimeout = 20000;	/* # of status tests for hpibid() calls */
37 int	hpibdmathresh = 3;	/* byte count beyond which to attempt dma */
38 
39 hpibinit(hc)
40 	register struct hp_ctlr *hc;
41 {
42 	register struct hpib_softc *hs = &hpib_softc[hc->hp_unit];
43 
44 	if (!nhpibtype(hc) && !fhpibtype(hc))
45 		return(0);
46 	hs->sc_hc = hc;
47 	hs->sc_dq.dq_unit = hc->hp_unit;
48 	hs->sc_dq.dq_driver = &hpibdriver;
49 	hs->sc_sq.dq_forw = hs->sc_sq.dq_back = &hs->sc_sq;
50 	hpib_isr[hc->hp_unit].isr_intr = hpibintr;
51 	hpib_isr[hc->hp_unit].isr_ipl = hc->hp_ipl;
52 	hpib_isr[hc->hp_unit].isr_arg = hc->hp_unit;
53 	isrlink(&hpib_isr[hc->hp_unit]);
54 	hpibreset(hc->hp_unit);
55 	return(1);
56 }
57 
58 hpibreset(unit)
59 	register int unit;
60 {
61 	if (hpib_softc[unit].sc_type == HPIBC)
62 		fhpibreset(unit);
63 	else
64 		nhpibreset(unit);
65 }
66 
67 hpibreq(dq)
68 	register struct devqueue *dq;
69 {
70 	register struct devqueue *hq;
71 
72 	hq = &hpib_softc[dq->dq_ctlr].sc_sq;
73 	insque(dq, hq->dq_back);
74 	if (dq->dq_back == hq)
75 		return(1);
76 	return(0);
77 }
78 
79 hpibfree(dq)
80 	register struct devqueue *dq;
81 {
82 	register struct devqueue *hq;
83 
84 	hq = &hpib_softc[dq->dq_ctlr].sc_sq;
85 	remque(dq);
86 	if ((dq = hq->dq_forw) != hq)
87 		(dq->dq_driver->d_start)(dq->dq_unit);
88 }
89 
90 hpibid(unit, slave)
91 {
92 	short id;
93 	int ohpibtimeout;
94 
95 	/*
96 	 * XXX: shorten timeout value (so autoconfig doesn't take forever)
97 	 */
98 	ohpibtimeout = hpibtimeout;
99 	hpibtimeout = hpibidtimeout;
100 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
101 		id = 0;
102 	hpibtimeout = ohpibtimeout;
103 	return(id);
104 }
105 
106 hpibsend(unit, slave, sec, addr, cnt)
107 	register int unit;
108 {
109 	if (hpib_softc[unit].sc_type == HPIBC)
110 		return(fhpibsend(unit, slave, sec, addr, cnt));
111 	else
112 		return(nhpibsend(unit, slave, sec, addr, cnt));
113 }
114 
115 hpibrecv(unit, slave, sec, addr, cnt)
116 	register int unit;
117 {
118 	if (hpib_softc[unit].sc_type == HPIBC)
119 		return(fhpibrecv(unit, slave, sec, addr, cnt));
120 	else
121 		return(nhpibrecv(unit, slave, sec, addr, cnt));
122 }
123 
124 hpibpptest(unit, slave)
125 	register int unit;
126 {
127 	int (*ppoll)();
128 
129 	ppoll = (hpib_softc[unit].sc_type == HPIBC) ? fhpibppoll : nhpibppoll;
130 	return((*ppoll)(unit) & (0x80 >> slave));
131 }
132 
133 hpibawait(unit)
134 	int 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((void *)unit);
141 	else
142 		nhpibppwatch((void *)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