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