xref: /netbsd-src/sys/arch/hp300/dev/hpib.c (revision 181254a7b1bdde6873432bffef2d2decc4b5c22f)
1 /*	$NetBSD: hpib.c,v 1.40 2019/11/10 21:16:27 chs 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.40 2019/11/10 21:16:27 chs Exp $");
69 
70 #include <sys/param.h>
71 #include <sys/systm.h>
72 #include <sys/buf.h>
73 #include <sys/malloc.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	hpibbussearch(device_t, cfdata_t, const int *, void *);
93 static int	hpibbusprint(void *, const char *);
94 
95 static int	hpibbus_alloc(struct hpibbus_softc *, int, int);
96 #if 0
97 static void	hpibbus_free(struct hpibbus_softc *, int, int);
98 #endif
99 
100 static void	hpibstart(void *);
101 static void	hpibdone(void *);
102 
103 int	hpibtimeout = 100000;	/* # of status tests before we give up */
104 int	hpibidtimeout = 10000;	/* # of status tests for hpibid() calls */
105 int	hpibdmathresh = 3;	/* byte count beyond which to attempt dma */
106 
107 /*
108  * HP-IB is essentially an IEEE 488 bus, with an HP command
109  * set (CS/80 on `newer' devices, Amigo on before-you-were-born
110  * devices) thrown on top.  Devices that respond to CS/80 (and
111  * probably Amigo, too) are tagged with a 16-bit ID.
112  *
113  * HP-IB has a 2-level addressing scheme; slave, the analog
114  * of a SCSI ID, and punit, the analog of a SCSI LUN.  Unforunately,
115  * IDs are on a per-slave basis; punits are often used for disk
116  * drives that have an accompanying tape drive on the second punit.
117  *
118  * In addition, not all HP-IB devices speak CS/80 or Amigo.
119  * Examples of such devices are HP-IB plotters, which simply
120  * take raw plotter commands over 488.  These devices do not
121  * have ID tags, and often the host cannot even tell if such
122  * a device is attached to the system!
123  *
124  * These two nasty bits mean that we have to treat HP-IB as
125  * an indirect bus.  However, since we are given some ID
126  * information, it is unreasonable to disallow cloning of
127  * CS/80 devices.
128  *
129  * To deal with all of this, we use the semi-twisted scheme
130  * in hpibbus_attach_children().  For each HP-IB slave, we loop
131  * through all of the possibly-configured children, allowing
132  * them to modify the punit parameter (but NOT the slave!).
133  *
134  * This is evil, but what can you do?
135  */
136 
137 static int
138 hpibbusmatch(device_t parent, cfdata_t cf, void *aux)
139 {
140 
141 	return 1;
142 }
143 
144 static void
145 hpibbusattach(device_t parent, device_t self, void *aux)
146 {
147 	struct hpibbus_softc *sc = device_private(self);
148 	struct hpibdev_attach_args *ha = aux;
149 
150 	sc->sc_dev = self;
151 	aprint_normal("\n");
152 
153 	/* Get the operations vector for the controller. */
154 	sc->sc_ops = ha->ha_ops;
155 	sc->sc_type = ha->ha_type;		/* XXX */
156 	sc->sc_ba = ha->ha_ba;
157 	*(ha->ha_softcpp) = sc;			/* XXX */
158 
159 	hpibreset(device_unit(self));		/* XXX souldn't be here */
160 
161 	/*
162 	 * Initialize the DMA queue entry.
163 	 */
164 	sc->sc_dq = malloc(sizeof(struct dmaqueue), M_DEVBUF, M_WAITOK);
165 	sc->sc_dq->dq_softc = sc;
166 	sc->sc_dq->dq_start = hpibstart;
167 	sc->sc_dq->dq_done = hpibdone;
168 
169 	/* Initialize the slave request queue. */
170 	TAILQ_INIT(&sc->sc_queue);
171 
172 	/* Attach any devices on the bus. */
173 	hpibbus_attach_children(sc);
174 }
175 
176 static void
177 hpibbus_attach_children(struct hpibbus_softc *sc)
178 {
179 	struct hpibbus_attach_args ha;
180 	int slave;
181 
182 	for (slave = 0; slave < 8; slave++) {
183 		/*
184 		 * Get the ID tag for the device, if any.
185 		 * Plotters won't identify themselves, and
186 		 * get the same value as non-existent devices.
187 		 */
188 		ha.ha_id = hpibid(device_unit(sc->sc_dev), slave);
189 
190 		ha.ha_slave = slave;	/* not to be modified by children */
191 		ha.ha_punit = 0;	/* children modify this */
192 
193 		/*
194 		 * Search though all configured children for this bus.
195 		 */
196 		config_search_ia(hpibbussearch, sc->sc_dev, "hpibbus", &ha);
197 	}
198 }
199 
200 static int
201 hpibbussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
202 {
203 	struct hpibbus_softc *sc = device_private(parent);
204 	struct hpibbus_attach_args *ha = aux;
205 
206 	/* Make sure this is in a consistent state. */
207 	ha->ha_punit = 0;
208 
209 	if (config_match(parent, cf, ha) > 0) {
210 		/*
211 		 * The device probe has succeeded, and filled in
212 		 * the punit information.  Make sure the configuration
213 		 * allows for this slave/punit combination.
214 		 */
215 		if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
216 		    cf->hpibbuscf_slave != ha->ha_slave)
217 			goto out;
218 		if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
219 		    cf->hpibbuscf_punit != ha->ha_punit)
220 			goto out;
221 
222 		/*
223 		 * Allocate the device's address from the bus's
224 		 * resource map.
225 		 */
226 		if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
227 			goto out;
228 
229 		/*
230 		 * This device is allowed; attach it.
231 		 */
232 		config_attach(parent, cf, ha, hpibbusprint);
233 	}
234  out:
235 	return 0;
236 }
237 
238 static int
239 hpibbusprint(void *aux, const char *pnp)
240 {
241 	struct hpibbus_attach_args *ha = aux;
242 
243 	aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
244 	return UNCONF;
245 }
246 
247 int
248 hpibdevprint(void *aux, const char *pnp)
249 {
250 
251 	/* only hpibbus's can attach to hpibdev's -- easy. */
252 	if (pnp != NULL)
253 		aprint_normal("hpibbus at %s", pnp);
254 	return UNCONF;
255 }
256 
257 void
258 hpibreset(int unit)
259 {
260 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
261 
262 	(*sc->sc_ops->hpib_reset)(sc);
263 }
264 
265 int
266 hpibreq(device_t pdev, struct hpibqueue *hq)
267 {
268 	struct hpibbus_softc *sc = device_private(pdev);
269 	int s;
270 
271 	s = splhigh();	/* XXXthorpej */
272 	TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
273 	splx(s);
274 
275 	if (TAILQ_FIRST(&sc->sc_queue) == hq)
276 		return 1;
277 
278 	return 0;
279 }
280 
281 void
282 hpibfree(device_t pdev, struct hpibqueue *hq)
283 {
284 	struct hpibbus_softc *sc = device_private(pdev);
285 	int s;
286 
287 	s = splhigh();	/* XXXthorpej */
288 	TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
289 	splx(s);
290 
291 	if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
292 		(*hq->hq_start)(hq->hq_softc);
293 }
294 
295 int
296 hpibid(int unit, int slave)
297 {
298 	short id;
299 	int ohpibtimeout;
300 
301 	/*
302 	 * XXX shorten timeout value so autoconfig doesn't
303 	 * take forever on slow CPUs.
304 	 */
305 	ohpibtimeout = hpibtimeout;
306 	hpibtimeout = hpibidtimeout * (cpuspeed / 8);
307 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
308 		id = 0;
309 	hpibtimeout = ohpibtimeout;
310 	return id;
311 }
312 
313 int
314 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
315 {
316 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
317 
318 	return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
319 }
320 
321 int
322 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
323 {
324 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
325 
326 	return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
327 }
328 
329 int
330 hpibpptest(int unit, int slave)
331 {
332 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
333 
334 	return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
335 }
336 
337 void
338 hpibppclear(int unit)
339 {
340 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
341 
342 	sc->sc_flags &= ~HPIBF_PPOLL;
343 }
344 
345 void
346 hpibawait(int unit)
347 {
348 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
349 
350 	sc->sc_flags |= HPIBF_PPOLL;
351 	(*sc->sc_ops->hpib_ppwatch)(sc);
352 }
353 
354 int
355 hpibswait(int unit, int slave)
356 {
357 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
358 	int timo = hpibtimeout;
359 	int mask, (*ppoll)(struct hpibbus_softc *);
360 
361 	ppoll = sc->sc_ops->hpib_ppoll;
362 	mask = 0x80 >> slave;
363 	while (((*ppoll)(sc) & mask) == 0) {
364 		if (--timo == 0) {
365 			printf("%s: swait timeout\n", device_xname(sc->sc_dev));
366 			return -1;
367 		}
368 	}
369 	return 0;
370 }
371 
372 int
373 hpibustart(int unit)
374 {
375 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
376 
377 	if (sc->sc_type == HPIBA)
378 		sc->sc_dq->dq_chan = DMA0;
379 	else
380 		sc->sc_dq->dq_chan = DMA0 | DMA1;
381 	if (dmareq(sc->sc_dq))
382 		return 1;
383 	return 0;
384 }
385 
386 static void
387 hpibstart(void *arg)
388 {
389 	struct hpibbus_softc *sc = arg;
390 	struct hpibqueue *hq;
391 
392 	hq = TAILQ_FIRST(&sc->sc_queue);
393 	(*hq->hq_go)(hq->hq_softc);
394 }
395 
396 void
397 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
398 {
399 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
400 
401 	(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
402 }
403 
404 static void
405 hpibdone(void *arg)
406 {
407 	struct hpibbus_softc *sc = arg;
408 
409 	(*sc->sc_ops->hpib_done)(sc);
410 }
411 
412 int
413 hpibintr(void *arg)
414 {
415 	struct hpibbus_softc *sc = arg;
416 
417 	return (sc->sc_ops->hpib_intr)(arg);
418 }
419 
420 static int
421 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit)
422 {
423 
424 	if (slave >= HPIB_NSLAVES ||
425 	    punit >= HPIB_NPUNITS)
426 		panic("hpibbus_alloc: device address out of range");
427 
428 	if (sc->sc_rmap[slave][punit] == 0) {
429 		sc->sc_rmap[slave][punit] = 1;
430 		return 0;
431 	}
432 	return 1;
433 }
434 
435 #if 0
436 static void
437 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit)
438 {
439 
440 	if (slave >= HPIB_NSLAVES ||
441 	    punit >= HPIB_NPUNITS)
442 		panic("hpibbus_free: device address out of range");
443 
444 #ifdef DIAGNOSTIC
445 	if (sc->sc_rmap[slave][punit] == 0)
446 		panic("hpibbus_free: not allocated");
447 #endif
448 
449 	sc->sc_rmap[slave][punit] = 0;
450 }
451 #endif
452