xref: /netbsd-src/sys/arch/hp300/dev/hpib.c (revision 3b01aba77a7a698587faaae455bbfe740923c1f5)
1 /*	$NetBSD: hpib.c,v 1.19 1998/01/12 18:31:00 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  * 3. All advertising materials mentioning features or use of this software
19  *    must display the following acknowledgement:
20  *	This product includes software developed by the NetBSD
21  *	Foundation, Inc. and its contributors.
22  * 4. Neither the name of The NetBSD Foundation nor the names of its
23  *    contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36  * POSSIBILITY OF SUCH DAMAGE.
37  */
38 
39 /*
40  * Copyright (c) 1982, 1990, 1993
41  *	The Regents of the University of California.  All rights reserved.
42  *
43  * Redistribution and use in source and binary forms, with or without
44  * modification, are permitted provided that the following conditions
45  * are met:
46  * 1. Redistributions of source code must retain the above copyright
47  *    notice, this list of conditions and the following disclaimer.
48  * 2. Redistributions in binary form must reproduce the above copyright
49  *    notice, this list of conditions and the following disclaimer in the
50  *    documentation and/or other materials provided with the distribution.
51  * 3. All advertising materials mentioning features or use of this software
52  *    must display the following acknowledgement:
53  *	This product includes software developed by the University of
54  *	California, Berkeley and its contributors.
55  * 4. Neither the name of the University nor the names of its contributors
56  *    may be used to endorse or promote products derived from this software
57  *    without specific prior written permission.
58  *
59  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
60  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
61  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
62  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
63  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
64  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
65  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
66  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
67  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
68  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
69  * SUCH DAMAGE.
70  *
71  *	@(#)hpib.c	8.2 (Berkeley) 1/12/94
72  */
73 
74 /*
75  * HP-IB bus driver
76  */
77 
78 #include <sys/param.h>
79 #include <sys/systm.h>
80 #include <sys/buf.h>
81 #include <sys/malloc.h>
82 #include <sys/device.h>
83 
84 #include <hp300/dev/dmavar.h>
85 
86 #include <hp300/dev/hpibvar.h>
87 
88 #include <machine/cpu.h>
89 #include <machine/hp300spu.h>
90 
91 int	hpibbusmatch __P((struct device *, struct cfdata *, void *));
92 void	hpibbusattach __P((struct device *, struct device *, void *));
93 
94 struct cfattach hpibbus_ca = {
95 	sizeof(struct hpibbus_softc), hpibbusmatch, hpibbusattach
96 };
97 
98 extern struct cfdriver hpibbus_cd;
99 
100 void	hpibbus_attach_children __P((struct hpibbus_softc *));
101 int	hpibbussearch __P((struct device *, struct cfdata *, void *));
102 int	hpibbusprint __P((void *, const char *));
103 
104 int	hpibbus_alloc __P((struct hpibbus_softc *, int, int));
105 void	hpibbus_free __P((struct hpibbus_softc *, int, int));
106 
107 void	hpibstart __P((void *));
108 void	hpibdone __P((void *));
109 
110 int	hpibtimeout = 100000;	/* # of status tests before we give up */
111 int	hpibidtimeout = 10000;	/* # of status tests for hpibid() calls */
112 int	hpibdmathresh = 3;	/* byte count beyond which to attempt dma */
113 
114 /*
115  * HP-IB is essentially an IEEE 488 bus, with an HP command
116  * set (CS/80 on `newer' devices, Amigo on before-you-were-born
117  * devices) thrown on top.  Devices that respond to CS/80 (and
118  * probably Amigo, too) are tagged with a 16-bit ID.
119  *
120  * HP-IB has a 2-level addressing scheme; slave, the analog
121  * of a SCSI ID, and punit, the analog of a SCSI LUN.  Unforunately,
122  * IDs are on a per-slave basis; punits are often used for disk
123  * drives that have an accompanying tape drive on the second punit.
124  *
125  * In addition, not all HP-IB devices speak CS/80 or Amigo.
126  * Examples of such devices are HP-IB plotters, which simply
127  * take raw plotter commands over 488.  These devices do not
128  * have ID tags, and often the host cannot even tell if such
129  * a device is attached to the system!
130  *
131  * These two nasty bits mean that we have to treat HP-IB as
132  * an indirect bus.  However, since we are given some ID
133  * information, it is unreasonable to disallow cloning of
134  * CS/80 devices.
135  *
136  * To deal with all of this, we use the semi-twisted scheme
137  * in hpibbus_attach_children().  For each HP-IB slave, we loop
138  * through all of the possibly-configured children, allowing
139  * them to modify the punit parameter (but NOT the slave!).
140  *
141  * This is evil, but what can you do?
142  */
143 
144 int
145 hpibbusmatch(parent, match, aux)
146 	struct device *parent;
147 	struct cfdata *match;
148 	void *aux;
149 {
150 
151 	return (1);
152 }
153 
154 void
155 hpibbusattach(parent, self, aux)
156 	struct device *parent, *self;
157 	void *aux;
158 {
159 	struct hpibbus_softc *sc = (struct hpibbus_softc *)self;
160 	struct hpibdev_attach_args *ha = aux;
161 
162 	printf("\n");
163 
164 	/* Get the operations vector for the controller. */
165 	sc->sc_ops = ha->ha_ops;
166 	sc->sc_type = ha->ha_type;		/* XXX */
167 	sc->sc_ba = ha->ha_ba;
168 	*(ha->ha_softcpp) = sc;			/* XXX */
169 
170 	hpibreset(self->dv_unit);		/* XXX souldn't be here */
171 
172 	/*
173 	 * Initialize the DMA queue entry.
174 	 */
175 	sc->sc_dq = (struct dmaqueue *)malloc(sizeof(struct dmaqueue),
176 	    M_DEVBUF, M_NOWAIT);
177 	if (sc->sc_dq == NULL) {
178 		printf("%s: can't allocate DMA queue entry\n", self->dv_xname);
179 		return;
180 	}
181 	sc->sc_dq->dq_softc = sc;
182 	sc->sc_dq->dq_start = hpibstart;
183 	sc->sc_dq->dq_done = hpibdone;
184 
185 	/* Initialize the slave request queue. */
186 	TAILQ_INIT(&sc->sc_queue);
187 
188 	/* Attach any devices on the bus. */
189 	hpibbus_attach_children(sc);
190 }
191 
192 void
193 hpibbus_attach_children(sc)
194 	struct hpibbus_softc *sc;
195 {
196 	struct hpibbus_attach_args ha;
197 	int slave;
198 
199 	for (slave = 0; slave < 8; slave++) {
200 		/*
201 		 * Get the ID tag for the device, if any.
202 		 * Plotters won't identify themselves, and
203 		 * get the same value as non-existent devices.
204 		 */
205 		ha.ha_id = hpibid(sc->sc_dev.dv_unit, slave);
206 
207 		ha.ha_slave = slave;	/* not to be modified by children */
208 		ha.ha_punit = 0;	/* children modify this */
209 
210 		/*
211 		 * Search though all configured children for this bus.
212 		 */
213 		(void)config_search(hpibbussearch, &sc->sc_dev, &ha);
214 	}
215 }
216 
217 int
218 hpibbussearch(parent, cf, aux)
219 	struct device *parent;
220 	struct cfdata *cf;
221 	void *aux;
222 {
223 	struct hpibbus_softc *sc = (struct hpibbus_softc *)parent;
224 	struct hpibbus_attach_args *ha = aux;
225 
226 	/* Make sure this is in a consistent state. */
227 	ha->ha_punit = 0;
228 
229 	if ((*cf->cf_attach->ca_match)(parent, cf, ha) > 0) {
230 		/*
231 		 * The device probe has succeeded, and filled in
232 		 * the punit information.  Make sure the configuration
233 		 * allows for this slave/punit combination.
234 		 */
235 		if (cf->hpibbuscf_slave != HPIBBUSCF_SLAVE_DEFAULT &&
236 		    cf->hpibbuscf_slave != ha->ha_slave)
237 			goto out;
238 		if (cf->hpibbuscf_punit != HPIBBUSCF_PUNIT_DEFAULT &&
239 		    cf->hpibbuscf_punit != ha->ha_punit)
240 			goto out;
241 
242 		/*
243 		 * Allocate the device's address from the bus's
244 		 * resource map.
245 		 */
246 		if (hpibbus_alloc(sc, ha->ha_slave, ha->ha_punit))
247 			goto out;
248 
249 		/*
250 		 * This device is allowed; attach it.
251 		 */
252 		config_attach(parent, cf, ha, hpibbusprint);
253 	}
254  out:
255 	return (0);
256 }
257 
258 int
259 hpibbusprint(aux, pnp)
260 	void *aux;
261 	const char *pnp;
262 {
263 	struct hpibbus_attach_args *ha = aux;
264 
265 	printf(" slave %d punit %d", ha->ha_slave, ha->ha_punit);
266 	return (UNCONF);
267 }
268 
269 int
270 hpibdevprint(aux, pnp)
271 	void *aux;
272 	const char *pnp;
273 {
274 
275 	/* only hpibbus's can attach to hpibdev's -- easy. */
276 	if (pnp != NULL)
277 		printf("hpibbus at %s", pnp);
278 	return (UNCONF);
279 }
280 
281 void
282 hpibreset(unit)
283 	int unit;
284 {
285 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
286 
287 	(*sc->sc_ops->hpib_reset)(sc);
288 }
289 
290 int
291 hpibreq(pdev, hq)
292 	struct device *pdev;
293 	struct hpibqueue *hq;
294 {
295 	struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev;
296 	int s;
297 
298 	s = splhigh();	/* XXXthorpej */
299 	TAILQ_INSERT_TAIL(&sc->sc_queue, hq, hq_list);
300 	splx(s);
301 
302 	if (sc->sc_queue.tqh_first == hq)
303 		return (1);
304 
305 	return (0);
306 }
307 
308 void
309 hpibfree(pdev, hq)
310 	struct device *pdev;
311 	struct hpibqueue *hq;
312 {
313 	struct hpibbus_softc *sc = (struct hpibbus_softc *)pdev;
314 	int s;
315 
316 	s = splhigh();	/* XXXthorpej */
317 	TAILQ_REMOVE(&sc->sc_queue, hq, hq_list);
318 	splx(s);
319 
320 	if ((hq = sc->sc_queue.tqh_first) != NULL)
321 		(*hq->hq_start)(hq->hq_softc);
322 }
323 
324 int
325 hpibid(unit, slave)
326 	int unit, slave;
327 {
328 	short id;
329 	int ohpibtimeout;
330 
331 	/*
332 	 * XXX shorten timeout value so autoconfig doesn't
333 	 * take forever on slow CPUs.
334 	 */
335 	ohpibtimeout = hpibtimeout;
336 	hpibtimeout = hpibidtimeout * (cpuspeed / 8);
337 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
338 		id = 0;
339 	hpibtimeout = ohpibtimeout;
340 	return(id);
341 }
342 
343 int
344 hpibsend(unit, slave, sec, addr, cnt)
345 	int unit, slave, sec, cnt;
346 	void *addr;
347 {
348 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
349 
350 	return ((*sc->sc_ops->hpib_send)(sc, slave, sec, addr, cnt));
351 }
352 
353 int
354 hpibrecv(unit, slave, sec, addr, cnt)
355 	int unit, slave, sec, cnt;
356 	void *addr;
357 {
358 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
359 
360 	return ((*sc->sc_ops->hpib_recv)(sc, slave, sec, addr, cnt));
361 }
362 
363 int
364 hpibpptest(unit, slave)
365 	int unit;
366 	int slave;
367 {
368 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
369 
370 	return ((*sc->sc_ops->hpib_ppoll)(sc) & (0x80 >> slave));
371 }
372 
373 void
374 hpibppclear(unit)
375 	int unit;
376 {
377 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
378 
379 	sc->sc_flags &= ~HPIBF_PPOLL;
380 }
381 
382 void
383 hpibawait(unit)
384 	int unit;
385 {
386 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
387 
388 	sc->sc_flags |= HPIBF_PPOLL;
389 	(*sc->sc_ops->hpib_ppwatch)(sc);
390 }
391 
392 int
393 hpibswait(unit, slave)
394 	int unit;
395 	int slave;
396 {
397 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
398 	int timo = hpibtimeout;
399 	int mask, (*ppoll) __P((struct hpibbus_softc *));
400 
401 	ppoll = sc->sc_ops->hpib_ppoll;
402 	mask = 0x80 >> slave;
403 	while (((*ppoll)(sc) & mask) == 0) {
404 		if (--timo == 0) {
405 			printf("%s: swait timeout\n", sc->sc_dev.dv_xname);
406 			return(-1);
407 		}
408 	}
409 	return(0);
410 }
411 
412 int
413 hpibustart(unit)
414 	int unit;
415 {
416 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
417 
418 	if (sc->sc_type == HPIBA)
419 		sc->sc_dq->dq_chan = DMA0;
420 	else
421 		sc->sc_dq->dq_chan = DMA0 | DMA1;
422 	if (dmareq(sc->sc_dq))
423 		return(1);
424 	return(0);
425 }
426 
427 void
428 hpibstart(arg)
429 	void *arg;
430 {
431 	struct hpibbus_softc *sc = arg;
432 	struct hpibqueue *hq;
433 
434 	hq = sc->sc_queue.tqh_first;
435 	(*hq->hq_go)(hq->hq_softc);
436 }
437 
438 void
439 hpibgo(unit, slave, sec, vbuf, count, rw, timo)
440 	int unit, slave, sec;
441 	void *vbuf;
442 	int count, rw, timo;
443 {
444 	struct hpibbus_softc *sc = hpibbus_cd.cd_devs[unit];
445 
446 	(*sc->sc_ops->hpib_go)(sc, slave, sec, vbuf, count, rw, timo);
447 }
448 
449 void
450 hpibdone(arg)
451 	void *arg;
452 {
453 	struct hpibbus_softc *sc = arg;
454 
455 	(*sc->sc_ops->hpib_done)(sc);
456 }
457 
458 int
459 hpibintr(arg)
460 	void *arg;
461 {
462 	struct hpibbus_softc *sc = arg;
463 
464 	return ((sc->sc_ops->hpib_intr)(arg));
465 }
466 
467 int
468 hpibbus_alloc(sc, slave, punit)
469 	struct hpibbus_softc *sc;
470 	int slave, punit;
471 {
472 
473 	if (slave >= HPIB_NSLAVES ||
474 	    punit >= HPIB_NPUNITS)
475 		panic("hpibbus_alloc: device address out of range");
476 
477 	if (sc->sc_rmap[slave][punit] == 0) {
478 		sc->sc_rmap[slave][punit] = 1;
479 		return (0);
480 	}
481 	return (1);
482 }
483 
484 void
485 hpibbus_free(sc, slave, punit)
486 	struct hpibbus_softc *sc;
487 	int slave, punit;
488 {
489 
490 	if (slave >= HPIB_NSLAVES ||
491 	    punit >= HPIB_NPUNITS)
492 		panic("hpibbus_free: device address out of range");
493 
494 #ifdef DIAGNOSTIC
495 	if (sc->sc_rmap[slave][punit] == 0)
496 		panic("hpibbus_free: not allocated");
497 #endif
498 
499 	sc->sc_rmap[slave][punit] = 0;
500 }
501