1*c0681579Stsutsui /* $NetBSD: nhpib.c,v 1.6 2006/06/25 17:37:43 tsutsui Exp $ */
2a0864b3eSthorpej
3a0864b3eSthorpej /*
4a0864b3eSthorpej * Copyright (c) 1982, 1990, 1993
5a0864b3eSthorpej * The Regents of the University of California. All rights reserved.
6a0864b3eSthorpej *
7a0864b3eSthorpej * Redistribution and use in source and binary forms, with or without
8a0864b3eSthorpej * modification, are permitted provided that the following conditions
9a0864b3eSthorpej * are met:
10a0864b3eSthorpej * 1. Redistributions of source code must retain the above copyright
11a0864b3eSthorpej * notice, this list of conditions and the following disclaimer.
12a0864b3eSthorpej * 2. Redistributions in binary form must reproduce the above copyright
13a0864b3eSthorpej * notice, this list of conditions and the following disclaimer in the
14a0864b3eSthorpej * documentation and/or other materials provided with the distribution.
15aad01611Sagc * 3. Neither the name of the University nor the names of its contributors
16a0864b3eSthorpej * may be used to endorse or promote products derived from this software
17a0864b3eSthorpej * without specific prior written permission.
18a0864b3eSthorpej *
19a0864b3eSthorpej * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20a0864b3eSthorpej * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21a0864b3eSthorpej * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22a0864b3eSthorpej * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23a0864b3eSthorpej * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24a0864b3eSthorpej * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25a0864b3eSthorpej * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26a0864b3eSthorpej * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27a0864b3eSthorpej * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28a0864b3eSthorpej * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29a0864b3eSthorpej * SUCH DAMAGE.
30a0864b3eSthorpej *
31a0864b3eSthorpej * @(#)nhpib.c 8.1 (Berkeley) 6/10/93
32a0864b3eSthorpej */
33a0864b3eSthorpej
34a0864b3eSthorpej /*
35a0864b3eSthorpej * Internal/98624 HPIB driver
36a0864b3eSthorpej */
37a0864b3eSthorpej
38a0864b3eSthorpej #include <sys/param.h>
39a0864b3eSthorpej
40a0864b3eSthorpej #include <hp300/dev/nhpibreg.h>
41a0864b3eSthorpej
42a0864b3eSthorpej #include <hp300/stand/common/hpibvar.h>
43a0864b3eSthorpej #include <hp300/stand/common/samachdep.h>
44a0864b3eSthorpej
45212f884fStsutsui static int nhpibiwait(struct nhpibdevice *);
46212f884fStsutsui static int nhpibowait(struct nhpibdevice *);
47212f884fStsutsui
48212f884fStsutsui int
nhpibinit(int unit)49d3dc0553Stsutsui nhpibinit(int unit)
50a0864b3eSthorpej {
51212f884fStsutsui struct hpib_softc *hs = &hpib_softc[unit];
52212f884fStsutsui struct nhpibdevice *hd = (void *)hs->sc_addr;
53a0864b3eSthorpej
54a0864b3eSthorpej if ((int)hd == internalhpib) {
55a0864b3eSthorpej hs->sc_type = HPIBA;
56a0864b3eSthorpej hs->sc_ba = HPIBA_BA;
57a0864b3eSthorpej }
58a0864b3eSthorpej else if (hd->hpib_cid == HPIBB) {
59a0864b3eSthorpej hs->sc_type = HPIBB;
60a0864b3eSthorpej hs->sc_ba = hd->hpib_csa & CSA_BA;
61a0864b3eSthorpej }
62a0864b3eSthorpej else
63212f884fStsutsui return 0;
64a0864b3eSthorpej nhpibreset(unit);
65212f884fStsutsui return 1;
66a0864b3eSthorpej }
67a0864b3eSthorpej
68212f884fStsutsui void
nhpibreset(int unit)69d3dc0553Stsutsui nhpibreset(int unit)
70a0864b3eSthorpej {
71212f884fStsutsui struct hpib_softc *hs = &hpib_softc[unit];
72212f884fStsutsui struct nhpibdevice *hd;
73a0864b3eSthorpej
74212f884fStsutsui hd = (void *)hs->sc_addr;
75a0864b3eSthorpej hd->hpib_acr = AUX_SSWRST;
76a0864b3eSthorpej hd->hpib_ar = hs->sc_ba;
77a0864b3eSthorpej hd->hpib_lim = 0;
78a0864b3eSthorpej hd->hpib_mim = 0;
79a0864b3eSthorpej hd->hpib_acr = AUX_CDAI;
80a0864b3eSthorpej hd->hpib_acr = AUX_CSHDW;
81a0864b3eSthorpej hd->hpib_acr = AUX_SSTD1;
82a0864b3eSthorpej hd->hpib_acr = AUX_SVSTD1;
83a0864b3eSthorpej hd->hpib_acr = AUX_CPP;
84a0864b3eSthorpej hd->hpib_acr = AUX_CHDFA;
85a0864b3eSthorpej hd->hpib_acr = AUX_CHDFE;
86a0864b3eSthorpej hd->hpib_acr = AUX_RHDF;
87a0864b3eSthorpej hd->hpib_acr = AUX_CSWRST;
88a0864b3eSthorpej hd->hpib_acr = AUX_TCA;
89a0864b3eSthorpej hd->hpib_acr = AUX_CSRE;
90a0864b3eSthorpej hd->hpib_acr = AUX_SSIC;
91a0864b3eSthorpej DELAY(100);
92a0864b3eSthorpej hd->hpib_acr = AUX_CSIC;
93a0864b3eSthorpej hd->hpib_acr = AUX_SSRE;
94a0864b3eSthorpej hd->hpib_data = C_DCL;
95a0864b3eSthorpej DELAY(100000);
96a0864b3eSthorpej }
97a0864b3eSthorpej
98212f884fStsutsui int
nhpibsend(int unit,int slave,int sec,uint8_t * buf,int cnt)99*c0681579Stsutsui nhpibsend(int unit, int slave, int sec, uint8_t *buf, int cnt)
100a0864b3eSthorpej {
101212f884fStsutsui struct hpib_softc *hs = &hpib_softc[unit];
102212f884fStsutsui struct nhpibdevice *hd;
103212f884fStsutsui int origcnt = cnt;
104a0864b3eSthorpej
105212f884fStsutsui hd = (void *)hs->sc_addr;
106a0864b3eSthorpej hd->hpib_acr = AUX_TCA;
107a0864b3eSthorpej hd->hpib_data = C_UNL;
108a0864b3eSthorpej nhpibowait(hd);
109a0864b3eSthorpej hd->hpib_data = C_TAG + hs->sc_ba;
110a0864b3eSthorpej hd->hpib_acr = AUX_STON;
111a0864b3eSthorpej nhpibowait(hd);
112a0864b3eSthorpej hd->hpib_data = C_LAG + slave;
113a0864b3eSthorpej nhpibowait(hd);
114a0864b3eSthorpej if (sec != -1) {
115a0864b3eSthorpej hd->hpib_data = C_SCG + sec;
116a0864b3eSthorpej nhpibowait(hd);
117a0864b3eSthorpej }
118a0864b3eSthorpej hd->hpib_acr = AUX_GTS;
119a0864b3eSthorpej if (cnt) {
120a0864b3eSthorpej while (--cnt) {
121a0864b3eSthorpej hd->hpib_data = *buf++;
122a0864b3eSthorpej if (nhpibowait(hd) < 0)
123a0864b3eSthorpej break;
124a0864b3eSthorpej }
125a0864b3eSthorpej hd->hpib_acr = AUX_EOI;
126a0864b3eSthorpej hd->hpib_data = *buf;
127a0864b3eSthorpej if (nhpibowait(hd) < 0)
128a0864b3eSthorpej cnt++;
129a0864b3eSthorpej hd->hpib_acr = AUX_TCA;
130a0864b3eSthorpej }
131212f884fStsutsui return origcnt - cnt;
132a0864b3eSthorpej }
133a0864b3eSthorpej
134212f884fStsutsui int
nhpibrecv(int unit,int slave,int sec,uint8_t * buf,int cnt)135*c0681579Stsutsui nhpibrecv(int unit, int slave, int sec, uint8_t *buf, int cnt)
136a0864b3eSthorpej {
137212f884fStsutsui struct hpib_softc *hs = &hpib_softc[unit];
138212f884fStsutsui struct nhpibdevice *hd;
139212f884fStsutsui int origcnt = cnt;
140a0864b3eSthorpej
141212f884fStsutsui hd = (void *)hs->sc_addr;
142a0864b3eSthorpej hd->hpib_acr = AUX_TCA;
143a0864b3eSthorpej hd->hpib_data = C_UNL;
144a0864b3eSthorpej nhpibowait(hd);
145a0864b3eSthorpej hd->hpib_data = C_LAG + hs->sc_ba;
146a0864b3eSthorpej hd->hpib_acr = AUX_SLON;
147a0864b3eSthorpej nhpibowait(hd);
148a0864b3eSthorpej hd->hpib_data = C_TAG + slave;
149a0864b3eSthorpej nhpibowait(hd);
150a0864b3eSthorpej if (sec != -1) {
151a0864b3eSthorpej hd->hpib_data = C_SCG + sec;
152a0864b3eSthorpej nhpibowait(hd);
153a0864b3eSthorpej }
154a0864b3eSthorpej hd->hpib_acr = AUX_RHDF;
155a0864b3eSthorpej hd->hpib_acr = AUX_GTS;
156a0864b3eSthorpej if (cnt) {
157a0864b3eSthorpej while (--cnt >= 0) {
158a0864b3eSthorpej if (nhpibiwait(hd) < 0)
159a0864b3eSthorpej break;
160a0864b3eSthorpej *buf++ = hd->hpib_data;
161a0864b3eSthorpej }
162a0864b3eSthorpej cnt++;
163a0864b3eSthorpej hd->hpib_acr = AUX_TCA;
164a0864b3eSthorpej }
165212f884fStsutsui return origcnt - cnt;
166a0864b3eSthorpej }
167a0864b3eSthorpej
168212f884fStsutsui int
nhpibppoll(int unit)169d3dc0553Stsutsui nhpibppoll(int unit)
170a0864b3eSthorpej {
171212f884fStsutsui struct hpib_softc *hs = &hpib_softc[unit];
172212f884fStsutsui struct nhpibdevice *hd;
173212f884fStsutsui int ppoll;
174a0864b3eSthorpej
175212f884fStsutsui hd = (void *)hs->sc_addr;
176a0864b3eSthorpej hd->hpib_acr = AUX_SPP;
177a0864b3eSthorpej DELAY(25);
178a0864b3eSthorpej ppoll = hd->hpib_cpt;
179a0864b3eSthorpej hd->hpib_acr = AUX_CPP;
180212f884fStsutsui return ppoll;
181a0864b3eSthorpej }
182a0864b3eSthorpej
183212f884fStsutsui static int
nhpibowait(struct nhpibdevice * hd)184d3dc0553Stsutsui nhpibowait(struct nhpibdevice *hd)
185a0864b3eSthorpej {
186212f884fStsutsui int timo = 100000;
187a0864b3eSthorpej
188a0864b3eSthorpej while ((hd->hpib_mis & MIS_BO) == 0 && --timo)
189a0864b3eSthorpej ;
190a0864b3eSthorpej if (timo == 0)
191212f884fStsutsui return -1;
192212f884fStsutsui return 0;
193a0864b3eSthorpej }
194a0864b3eSthorpej
195212f884fStsutsui static int
nhpibiwait(struct nhpibdevice * hd)196d3dc0553Stsutsui nhpibiwait(struct nhpibdevice *hd)
197a0864b3eSthorpej {
198212f884fStsutsui int timo = 100000;
199a0864b3eSthorpej
200a0864b3eSthorpej while ((hd->hpib_mis & MIS_BI) == 0 && --timo)
201a0864b3eSthorpej ;
202a0864b3eSthorpej if (timo == 0)
203212f884fStsutsui return -1;
204212f884fStsutsui return 0;
205a0864b3eSthorpej }
206