hpib.c revision 1.42.2.1 1 /* $NetBSD: hpib.c,v 1.42.2.1 2021/08/01 22:42:08 thorpej Exp $ */
2
3 /*-
4 * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Jason R. Thorpe.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 /*
33 * Copyright (c) 1982, 1990, 1993
34 * The Regents of the University of California. All rights reserved.
35 *
36 * Redistribution and use in source and binary forms, with or without
37 * modification, are permitted provided that the following conditions
38 * are met:
39 * 1. Redistributions of source code must retain the above copyright
40 * notice, this list of conditions and the following disclaimer.
41 * 2. Redistributions in binary form must reproduce the above copyright
42 * notice, this list of conditions and the following disclaimer in the
43 * documentation and/or other materials provided with the distribution.
44 * 3. Neither the name of the University nor the names of its contributors
45 * may be used to endorse or promote products derived from this software
46 * without specific prior written permission.
47 *
48 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
49 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
50 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
51 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
52 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
53 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
54 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
55 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
56 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
57 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
58 * SUCH DAMAGE.
59 *
60 * @(#)hpib.c 8.2 (Berkeley) 1/12/94
61 */
62
63 /*
64 * HP-IB bus driver
65 */
66
67 #include <sys/cdefs.h>
68 __KERNEL_RCSID(0, "$NetBSD: hpib.c,v 1.42.2.1 2021/08/01 22:42:08 thorpej Exp $");
69
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/buf.h>
73 #include <sys/kmem.h>
74 #include <sys/device.h>
75
76 #include <hp300/dev/dmavar.h>
77
78 #include <hp300/dev/hpibvar.h>
79
80 #include <machine/cpu.h>
81 #include <machine/hp300spu.h>
82
83 #include "ioconf.h"
84
85 static int hpibbusmatch(device_t, cfdata_t, void *);
86 static void hpibbusattach(device_t, device_t, void *);
87
88 CFATTACH_DECL_NEW(hpibbus, sizeof(struct hpibbus_softc),
89 hpibbusmatch, hpibbusattach, NULL, NULL);
90
91 static void hpibbus_attach_children(struct hpibbus_softc *);
92 static int hpibbussubmatch(device_t, cfdata_t, const int *ldesc, void *);
93 static int hpibbusprint(void *, const char *);
94
95 static void hpibstart(void *);
96 static void hpibdone(void *);
97
98 int hpibtimeout = 100000; /* # of status tests before we give up */
99 int hpibidtimeout = 10000; /* # of status tests for hpibid() calls */
100 int hpibdmathresh = 3; /* byte count beyond which to attempt dma */
101
102 /*
103 * HP-IB is essentially an IEEE 488 bus, with an HP command
104 * set (CS/80 on `newer' devices, Amigo on before-you-were-born
105 * devices) thrown on top. Devices that respond to CS/80 (and
106 * probably Amigo, too) are tagged with a 16-bit ID.
107 *
108 * HP-IB has a 2-level addressing scheme; slave, the analog
109 * of a SCSI ID, and punit, the analog of a SCSI LUN. Unforunately,
110 * IDs are on a per-slave basis; punits are often used for disk
111 * drives that have an accompanying tape drive on the second punit.
112 *
113 * In addition, not all HP-IB devices speak CS/80 or Amigo.
114 * Examples of such devices are HP-IB plotters, which simply
115 * take raw plotter commands over 488. These devices do not
116 * have ID tags, and often the host cannot even tell if such
117 * a device is attached to the system!
118 *
119 * * We nevertheless probe the whole (slave, punit) tuple space, since
120 * drivers for devices with a unique ID know exactly where to attach;
121 * and we disallow ``star'' locators for other drivers.
122 */
123
124 static int
125 hpibbusmatch(device_t parent, cfdata_t cf, void *aux)
126 {
127
128 return 1;
129 }
130
131 static void
132 hpibbusattach(device_t parent, device_t self, void *aux)
133 {
134 struct hpibbus_softc *sc = device_private(self);
135 struct hpibdev_attach_args *ha = aux;
136
137 sc->sc_dev = self;
138 aprint_normal("\n");
139
140 /* Get the operations vector for the controller. */
141 sc->sc_ops = ha->ha_ops;
142 sc->sc_type = ha->ha_type; /* XXX */
143 sc->sc_ba = ha->ha_ba;
144 *(ha->ha_softcpp) = sc; /* XXX */
145
146 hpibreset(device_unit(self)); /* XXX souldn't be here */
147
148 /*
149 * Initialize the DMA queue entry.
150 */
151 sc->sc_dq = kmem_alloc(sizeof(struct dmaqueue), KM_SLEEP);
152 sc->sc_dq->dq_softc = sc;
153 sc->sc_dq->dq_start = hpibstart;
154 sc->sc_dq->dq_done = hpibdone;
155
156 /* Initialize the slave request queue. */
157 TAILQ_INIT(&sc->sc_queue);
158
159 /* Attach any devices on the bus. */
160 hpibbus_attach_children(sc);
161 }
162
163 static void
164 hpibbus_attach_children(struct hpibbus_softc *sc)
165 {
166 struct hpibbus_attach_args ha;
167 int id, slave, punit;
168 int i;
169
170 for (slave = 0; slave < HPIB_NSLAVES; slave++) {
171 /*
172 * Get the ID tag for the device, if any.
173 * Plotters won't identify themselves, and
174 * get the same value as non-existent devices.
175 * However, aging HP-IB drives are slow to respond; try up
176 * to three times to get a valid ID.
177 */
178 for (i = 0; i < 3; i++) {
179 id = hpibid(device_unit(sc->sc_dev), slave);
180 if ((id & 0x200) != 0)
181 break;
182 delay(10000);
183 }
184
185 for (punit = 0; punit < HPIB_NPUNITS; punit++) {
186 /*
187 * Search through all configured children for this bus.
188 */
189 ha.ha_id = id;
190 ha.ha_slave = slave;
191 ha.ha_punit = punit;
192 config_found(sc->sc_dev, &ha, hpibbusprint,
193 CFARG_SUBMATCH, hpibbussubmatch,
194 CFARG_EOL);
195 }
196 }
197 }
198
199 static int
200 hpibbussubmatch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
201 {
202 struct hpibbus_attach_args *ha = aux;
203
204 if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
205 cf->hpibbuscf_slave != ha->ha_slave)
206 return 0;
207 if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
208 cf->hpibbuscf_punit != ha->ha_punit)
209 return 0;
210
211 return config_match(parent, cf, aux);
212 }
213
214 static int
215 hpibbusprint(void *aux, const char *pnp)
216 {
217 struct hpibbus_attach_args *ha = aux;
218
219 if (pnp != NULL) {
220 if (ha->ha_id == 0 || ha->ha_punit != 0 /* XXX */)
221 return QUIET;
222 printf("HP-IB device (id %04X) at %s", ha->ha_id, pnp);
223 }
224 aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
225 return UNCONF;
226 }
227
228 int
229 hpibdevprint(void *aux, const char *pnp)
230 {
231
232 /* only hpibbus's can attach to hpibdev's -- easy. */
233 if (pnp != NULL)
234 aprint_normal("hpibbus at %s", pnp);
235 return UNCONF;
236 }
237
238 void
239 hpibreset(int unit)
240 {
241 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
242
243 (*sc->sc_ops->hpib_reset)(sc);
244 }
245
246 int
247 hpibreq(device_t pdev, struct hpibqueue *hq)
248 {
249 struct hpibbus_softc *sc = device_private(pdev);
250 int s;
251
252 s = splhigh(); /* XXXthorpej */
253 TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
254 splx(s);
255
256 if (TAILQ_FIRST(&sc->sc_queue) == hq)
257 return 1;
258
259 return 0;
260 }
261
262 void
263 hpibfree(device_t pdev, struct hpibqueue *hq)
264 {
265 struct hpibbus_softc *sc = device_private(pdev);
266 int s;
267
268 s = splhigh(); /* XXXthorpej */
269 TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
270 splx(s);
271
272 if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
273 (*hq->hq_start)(hq->hq_softc);
274 }
275
276 int
277 hpibid(int unit, int slave)
278 {
279 short id;
280 int ohpibtimeout;
281
282 /*
283 * XXX shorten timeout value so autoconfig doesn't
284 * take forever on slow CPUs.
285 */
286 ohpibtimeout = hpibtimeout;
287 hpibtimeout = hpibidtimeout * (cpuspeed / 8);
288 if (hpibrecv(unit, 31, slave, &id, 2) != 2)
289 id = 0;
290 hpibtimeout = ohpibtimeout;
291 return id;
292 }
293
294 int
295 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
296 {
297 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
298
299 return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
300 }
301
302 int
303 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
304 {
305 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
306
307 return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
308 }
309
310 int
311 hpibpptest(int unit, int slave)
312 {
313 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
314
315 return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
316 }
317
318 void
319 hpibppclear(int unit)
320 {
321 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
322
323 sc->sc_flags &= ~HPIBF_PPOLL;
324 }
325
326 void
327 hpibawait(int unit)
328 {
329 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
330
331 sc->sc_flags |= HPIBF_PPOLL;
332 (*sc->sc_ops->hpib_ppwatch)(sc);
333 }
334
335 int
336 hpibswait(int unit, int slave)
337 {
338 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
339 int timo = hpibtimeout;
340 int mask, (*ppoll)(struct hpibbus_softc *);
341
342 ppoll = sc->sc_ops->hpib_ppoll;
343 mask = 0x80 >> slave;
344 while (((*ppoll)(sc) & mask) == 0) {
345 if (--timo == 0) {
346 printf("%s: swait timeout\n", device_xname(sc->sc_dev));
347 return -1;
348 }
349 }
350 return 0;
351 }
352
353 int
354 hpibustart(int unit)
355 {
356 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
357
358 if (sc->sc_type == HPIBA)
359 sc->sc_dq->dq_chan = DMA0;
360 else
361 sc->sc_dq->dq_chan = DMA0 | DMA1;
362 if (dmareq(sc->sc_dq))
363 return 1;
364 return 0;
365 }
366
367 static void
368 hpibstart(void *arg)
369 {
370 struct hpibbus_softc *sc = arg;
371 struct hpibqueue *hq;
372
373 hq = TAILQ_FIRST(&sc->sc_queue);
374 (*hq->hq_go)(hq->hq_softc);
375 }
376
377 void
378 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
379 {
380 struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
381
382 (*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
383 }
384
385 static void
386 hpibdone(void *arg)
387 {
388 struct hpibbus_softc *sc = arg;
389
390 (*sc->sc_ops->hpib_done)(sc);
391 }
392
393 int
394 hpibintr(void *arg)
395 {
396 struct hpibbus_softc *sc = arg;
397
398 return (sc->sc_ops->hpib_intr)(arg);
399 }
400