xref: /netbsd-src/sys/arch/hp300/dev/hpib.c (revision 82d56013d7b633d116a93943de88e08335357a7c)
1 /*	$NetBSD: hpib.c,v 1.42 2021/04/24 23:36:37 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 2021/04/24 23:36:37 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	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 = kmem_alloc(sizeof(struct dmaqueue), KM_SLEEP);
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(sc->sc_dev, &ha,
197 		    CFARG_SEARCH, hpibbussearch,
198 		    CFARG_EOL);
199 	}
200 }
201 
202 static int
203 hpibbussearch(device_t parent, cfdata_t cf, const int *ldesc, void *aux)
204 {
205 	struct hpibbus_softc *sc = device_private(parent);
206 	struct hpibbus_attach_args *ha = aux;
207 
208 	/* Make sure this is in a consistent state. */
209 	ha->ha_punit = 0;
210 
211 	if (config_probe(parent, cf, ha)) {
212 		/*
213 		 * The device probe has succeeded, and filled in
214 		 * the punit information.  Make sure the configuration
215 		 * allows for this slave/punit combination.
216 		 */
217 		if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
218 		    cf->hpibbuscf_slave != ha->ha_slave)
219 			goto out;
220 		if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
221 		    cf->hpibbuscf_punit != ha->ha_punit)
222 			goto out;
223 
224 		/*
225 		 * Allocate the device's address from the bus's
226 		 * resource map.
227 		 */
228 		if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
229 			goto out;
230 
231 		/*
232 		 * This device is allowed; attach it.
233 		 */
234 		config_attach(parent, cf, ha, hpibbusprint, CFARG_EOL);
235 	}
236  out:
237 	return 0;
238 }
239 
240 static int
241 hpibbusprint(void *aux, const char *pnp)
242 {
243 	struct hpibbus_attach_args *ha = aux;
244 
245 	aprint_normal(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
246 	return UNCONF;
247 }
248 
249 int
250 hpibdevprint(void *aux, const char *pnp)
251 {
252 
253 	/* only hpibbus's can attach to hpibdev's -- easy. */
254 	if (pnp != NULL)
255 		aprint_normal("hpibbus at %s", pnp);
256 	return UNCONF;
257 }
258 
259 void
260 hpibreset(int unit)
261 {
262 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
263 
264 	(*sc->sc_ops->hpib_reset)(sc);
265 }
266 
267 int
268 hpibreq(device_t pdev, struct hpibqueue *hq)
269 {
270 	struct hpibbus_softc *sc = device_private(pdev);
271 	int s;
272 
273 	s = splhigh();	/* XXXthorpej */
274 	TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
275 	splx(s);
276 
277 	if (TAILQ_FIRST(&sc->sc_queue) == hq)
278 		return 1;
279 
280 	return 0;
281 }
282 
283 void
284 hpibfree(device_t pdev, struct hpibqueue *hq)
285 {
286 	struct hpibbus_softc *sc = device_private(pdev);
287 	int s;
288 
289 	s = splhigh();	/* XXXthorpej */
290 	TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
291 	splx(s);
292 
293 	if ((hq = TAILQ_FIRST(&sc->sc_queue)) != NULL)
294 		(*hq->hq_start)(hq->hq_softc);
295 }
296 
297 int
298 hpibid(int unit, int slave)
299 {
300 	short id;
301 	int ohpibtimeout;
302 
303 	/*
304 	 * XXX shorten timeout value so autoconfig doesn't
305 	 * take forever on slow CPUs.
306 	 */
307 	ohpibtimeout = hpibtimeout;
308 	hpibtimeout = hpibidtimeout * (cpuspeed / 8);
309 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
310 		id = 0;
311 	hpibtimeout = ohpibtimeout;
312 	return id;
313 }
314 
315 int
316 hpibsend(int unit, int slave, int sec, void *addr, int cnt)
317 {
318 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
319 
320 	return (*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt);
321 }
322 
323 int
324 hpibrecv(int unit, int slave, int sec, void *addr, int cnt)
325 {
326 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
327 
328 	return (*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt);
329 }
330 
331 int
332 hpibpptest(int unit, int slave)
333 {
334 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
335 
336 	return (*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave);
337 }
338 
339 void
340 hpibppclear(int unit)
341 {
342 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
343 
344 	sc->sc_flags &= ~HPIBF_PPOLL;
345 }
346 
347 void
348 hpibawait(int unit)
349 {
350 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
351 
352 	sc->sc_flags |= HPIBF_PPOLL;
353 	(*sc->sc_ops->hpib_ppwatch)(sc);
354 }
355 
356 int
357 hpibswait(int unit, int slave)
358 {
359 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd, unit);
360 	int timo = hpibtimeout;
361 	int mask, (*ppoll)(struct hpibbus_softc *);
362 
363 	ppoll = sc->sc_ops->hpib_ppoll;
364 	mask = 0x80 >> slave;
365 	while (((*ppoll)(sc) & mask) == 0) {
366 		if (--timo == 0) {
367 			printf("%s: swait timeout\n", device_xname(sc->sc_dev));
368 			return -1;
369 		}
370 	}
371 	return 0;
372 }
373 
374 int
375 hpibustart(int unit)
376 {
377 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
378 
379 	if (sc->sc_type == HPIBA)
380 		sc->sc_dq->dq_chan = DMA0;
381 	else
382 		sc->sc_dq->dq_chan = DMA0 | DMA1;
383 	if (dmareq(sc->sc_dq))
384 		return 1;
385 	return 0;
386 }
387 
388 static void
389 hpibstart(void *arg)
390 {
391 	struct hpibbus_softc *sc = arg;
392 	struct hpibqueue *hq;
393 
394 	hq = TAILQ_FIRST(&sc->sc_queue);
395 	(*hq->hq_go)(hq->hq_softc);
396 }
397 
398 void
399 hpibgo(int unit, int slave, int sec, void *vbuf, int count, int rw, int timo)
400 {
401 	struct hpibbus_softc *sc = device_lookup_private(&hpibbus_cd,unit);
402 
403 	(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
404 }
405 
406 static void
407 hpibdone(void *arg)
408 {
409 	struct hpibbus_softc *sc = arg;
410 
411 	(*sc->sc_ops->hpib_done)(sc);
412 }
413 
414 int
415 hpibintr(void *arg)
416 {
417 	struct hpibbus_softc *sc = arg;
418 
419 	return (sc->sc_ops->hpib_intr)(arg);
420 }
421 
422 static int
423 hpibbus_alloc(struct hpibbus_softc *sc, int slave, int punit)
424 {
425 
426 	if (slave >= HPIB_NSLAVES ||
427 	    punit >= HPIB_NPUNITS)
428 		panic("hpibbus_alloc: device address out of range");
429 
430 	if (sc->sc_rmap[slave][punit] == 0) {
431 		sc->sc_rmap[slave][punit] = 1;
432 		return 0;
433 	}
434 	return 1;
435 }
436 
437 #if 0
438 static void
439 hpibbus_free(struct hpibbus_softc *sc, int slave, int punit)
440 {
441 
442 	if (slave >= HPIB_NSLAVES ||
443 	    punit >= HPIB_NPUNITS)
444 		panic("hpibbus_free: device address out of range");
445 
446 #ifdef DIAGNOSTIC
447 	if (sc->sc_rmap[slave][punit] == 0)
448 		panic("hpibbus_free: not allocated");
449 #endif
450 
451 	sc->sc_rmap[slave][punit] = 0;
452 }
453 #endif
454