Home | History | Annotate | Line # | Download | only in dev
hpib.c revision 1.8
      1  1.8  thorpej /*	$NetBSD: hpib.c,v 1.8 1996/02/14 02:44:28 thorpej Exp $	*/
      2  1.4      cgd 
      3  1.1      cgd /*
      4  1.3  mycroft  * Copyright (c) 1982, 1990, 1993
      5  1.3  mycroft  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * Redistribution and use in source and binary forms, with or without
      8  1.1      cgd  * modification, are permitted provided that the following conditions
      9  1.1      cgd  * are met:
     10  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     11  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     12  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     14  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     15  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     16  1.1      cgd  *    must display the following acknowledgement:
     17  1.1      cgd  *	This product includes software developed by the University of
     18  1.1      cgd  *	California, Berkeley and its contributors.
     19  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     20  1.1      cgd  *    may be used to endorse or promote products derived from this software
     21  1.1      cgd  *    without specific prior written permission.
     22  1.1      cgd  *
     23  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     24  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     25  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     26  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     27  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     28  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     29  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     30  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     31  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     32  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     33  1.1      cgd  * SUCH DAMAGE.
     34  1.1      cgd  *
     35  1.4      cgd  *	@(#)hpib.c	8.2 (Berkeley) 1/12/94
     36  1.1      cgd  */
     37  1.1      cgd 
     38  1.1      cgd /*
     39  1.1      cgd  * HPIB driver
     40  1.1      cgd  */
     41  1.1      cgd #include "hpib.h"
     42  1.1      cgd #if NHPIB > 0
     43  1.1      cgd 
     44  1.3  mycroft #include <sys/param.h>
     45  1.3  mycroft #include <sys/systm.h>
     46  1.3  mycroft #include <sys/buf.h>
     47  1.3  mycroft 
     48  1.3  mycroft #include <hp300/dev/device.h>
     49  1.3  mycroft #include <hp300/dev/hpibvar.h>
     50  1.3  mycroft #include <hp300/dev/dmavar.h>
     51  1.1      cgd 
     52  1.3  mycroft #include <machine/cpu.h>
     53  1.3  mycroft #include <hp300/hp300/isr.h>
     54  1.1      cgd 
     55  1.7  thorpej int	hpibmatch __P((struct hp_ctlr *));
     56  1.7  thorpej void	hpibattach __P((struct hp_ctlr *));
     57  1.6  thorpej void	hpibstart __P((int));
     58  1.6  thorpej void	hpibgo __P((int, int, int, void *, int, int, int));
     59  1.6  thorpej void	hpibdone __P((int));
     60  1.8  thorpej int	hpibintr __P((void *));
     61  1.6  thorpej 
     62  1.1      cgd struct	driver hpibdriver = {
     63  1.7  thorpej 	hpibmatch,
     64  1.7  thorpej 	hpibattach,
     65  1.6  thorpej 	"hpib",
     66  1.6  thorpej 	(int(*)())hpibstart,			/* XXX */
     67  1.6  thorpej 	(int(*)())hpibgo,			/* XXX */
     68  1.6  thorpej 	hpibintr,
     69  1.6  thorpej 	(int(*)())hpibdone,			/* XXX */
     70  1.1      cgd };
     71  1.1      cgd 
     72  1.1      cgd struct	hpib_softc hpib_softc[NHPIB];
     73  1.6  thorpej 
     74  1.6  thorpej extern	int nhpibtype __P((struct hp_ctlr *));	/* XXX */
     75  1.6  thorpej extern	int fhpibtype __P((struct hp_ctlr *));	/* XXX */
     76  1.7  thorpej extern	void nhpibattach __P((struct hp_ctlr *));	/* XXX */
     77  1.7  thorpej extern	void fhpibattach __P((struct hp_ctlr *));	/* XXX */
     78  1.1      cgd 
     79  1.1      cgd int	hpibtimeout = 100000;	/* # of status tests before we give up */
     80  1.3  mycroft int	hpibidtimeout = 10000;	/* # of status tests for hpibid() calls */
     81  1.1      cgd int	hpibdmathresh = 3;	/* byte count beyond which to attempt dma */
     82  1.1      cgd 
     83  1.6  thorpej int
     84  1.7  thorpej hpibmatch(hc)
     85  1.1      cgd 	register struct hp_ctlr *hc;
     86  1.1      cgd {
     87  1.7  thorpej 	struct hp_hw *hw = hc->hp_args;
     88  1.7  thorpej 	extern caddr_t internalhpib;
     89  1.7  thorpej 
     90  1.7  thorpej 	/* Special case for internal HP-IB. */
     91  1.7  thorpej 	if ((hw->hw_sc == 7) && internalhpib)
     92  1.7  thorpej 		goto hwid_ok;
     93  1.7  thorpej 
     94  1.7  thorpej 	switch (hw->hw_id) {
     95  1.7  thorpej 	case 8:			/* 98625B */
     96  1.7  thorpej 	case 128:		/* 98624A */
     97  1.7  thorpej  hwid_ok:
     98  1.7  thorpej 		if (nhpibtype(hc) || fhpibtype(hc))
     99  1.7  thorpej 			return (1);
    100  1.7  thorpej 	}
    101  1.7  thorpej 
    102  1.7  thorpej 	return (0);
    103  1.7  thorpej }
    104  1.7  thorpej 
    105  1.7  thorpej void
    106  1.7  thorpej hpibattach(hc)
    107  1.7  thorpej 	struct hp_ctlr *hc;
    108  1.7  thorpej {
    109  1.7  thorpej 	struct hpib_softc *hs = &hpib_softc[hc->hp_unit];
    110  1.7  thorpej 
    111  1.7  thorpej 	/*
    112  1.7  thorpej 	 * Call the appropriate "attach" routine for this controller.
    113  1.7  thorpej 	 * The type is set in the "type" routine.
    114  1.8  thorpej 	 *
    115  1.8  thorpej 	 * XXX This is, by the way, exactly backwards.
    116  1.7  thorpej 	 */
    117  1.7  thorpej 	switch (hs->sc_type) {
    118  1.7  thorpej 	case HPIBA:
    119  1.7  thorpej 	case HPIBB:
    120  1.7  thorpej 		nhpibattach(hc);
    121  1.7  thorpej 		break;
    122  1.7  thorpej 
    123  1.7  thorpej 	case HPIBC:
    124  1.7  thorpej 		fhpibattach(hc);
    125  1.7  thorpej 		break;
    126  1.7  thorpej 
    127  1.7  thorpej 	default:
    128  1.7  thorpej 		panic("hpibattach: unknown type 0x%x", hs->sc_type);
    129  1.7  thorpej 		/* NOTREACHED */
    130  1.7  thorpej 	}
    131  1.6  thorpej 
    132  1.1      cgd 	hs->sc_hc = hc;
    133  1.1      cgd 	hs->sc_dq.dq_unit = hc->hp_unit;
    134  1.1      cgd 	hs->sc_dq.dq_driver = &hpibdriver;
    135  1.1      cgd 	hs->sc_sq.dq_forw = hs->sc_sq.dq_back = &hs->sc_sq;
    136  1.6  thorpej 
    137  1.6  thorpej 	/* Establish the interrupt handler. */
    138  1.8  thorpej 	isrlink(hpibintr, hs, hc->hp_ipl, ISRPRI_BIO);
    139  1.6  thorpej 
    140  1.6  thorpej 	/* Reset the controller, display what we've seen, and we're done. */
    141  1.1      cgd 	hpibreset(hc->hp_unit);
    142  1.7  thorpej 	printf(": %s\n", hs->sc_descrip);
    143  1.1      cgd }
    144  1.1      cgd 
    145  1.6  thorpej void
    146  1.1      cgd hpibreset(unit)
    147  1.1      cgd 	register int unit;
    148  1.1      cgd {
    149  1.6  thorpej 
    150  1.6  thorpej 	(hpib_softc[unit].sc_controller->hpib_reset)(unit);
    151  1.1      cgd }
    152  1.1      cgd 
    153  1.6  thorpej int
    154  1.1      cgd hpibreq(dq)
    155  1.1      cgd 	register struct devqueue *dq;
    156  1.1      cgd {
    157  1.1      cgd 	register struct devqueue *hq;
    158  1.1      cgd 
    159  1.1      cgd 	hq = &hpib_softc[dq->dq_ctlr].sc_sq;
    160  1.1      cgd 	insque(dq, hq->dq_back);
    161  1.1      cgd 	if (dq->dq_back == hq)
    162  1.1      cgd 		return(1);
    163  1.1      cgd 	return(0);
    164  1.1      cgd }
    165  1.1      cgd 
    166  1.6  thorpej void
    167  1.1      cgd hpibfree(dq)
    168  1.1      cgd 	register struct devqueue *dq;
    169  1.1      cgd {
    170  1.1      cgd 	register struct devqueue *hq;
    171  1.1      cgd 
    172  1.1      cgd 	hq = &hpib_softc[dq->dq_ctlr].sc_sq;
    173  1.1      cgd 	remque(dq);
    174  1.1      cgd 	if ((dq = hq->dq_forw) != hq)
    175  1.1      cgd 		(dq->dq_driver->d_start)(dq->dq_unit);
    176  1.1      cgd }
    177  1.1      cgd 
    178  1.6  thorpej int
    179  1.1      cgd hpibid(unit, slave)
    180  1.3  mycroft 	int unit, slave;
    181  1.1      cgd {
    182  1.1      cgd 	short id;
    183  1.1      cgd 	int ohpibtimeout;
    184  1.1      cgd 
    185  1.1      cgd 	/*
    186  1.3  mycroft 	 * XXX shorten timeout value so autoconfig doesn't
    187  1.3  mycroft 	 * take forever on slow CPUs.
    188  1.1      cgd 	 */
    189  1.1      cgd 	ohpibtimeout = hpibtimeout;
    190  1.3  mycroft 	hpibtimeout = hpibidtimeout * cpuspeed;
    191  1.1      cgd 	if (hpibrecv(unit, 31, slave, &id, 2) != 2)
    192  1.1      cgd 		id = 0;
    193  1.1      cgd 	hpibtimeout = ohpibtimeout;
    194  1.1      cgd 	return(id);
    195  1.1      cgd }
    196  1.1      cgd 
    197  1.6  thorpej int
    198  1.1      cgd hpibsend(unit, slave, sec, addr, cnt)
    199  1.6  thorpej 	int unit, slave, sec, cnt;
    200  1.6  thorpej 	void *addr;
    201  1.1      cgd {
    202  1.6  thorpej 
    203  1.6  thorpej 	return ((hpib_softc[unit].sc_controller->hpib_send)(unit, slave,
    204  1.6  thorpej 	    sec, addr, cnt));
    205  1.1      cgd }
    206  1.1      cgd 
    207  1.6  thorpej int
    208  1.1      cgd hpibrecv(unit, slave, sec, addr, cnt)
    209  1.6  thorpej 	int unit, slave, sec, cnt;
    210  1.6  thorpej 	void *addr;
    211  1.1      cgd {
    212  1.6  thorpej 
    213  1.6  thorpej 	return ((hpib_softc[unit].sc_controller->hpib_recv)(unit, slave,
    214  1.6  thorpej 	    sec, addr, cnt));
    215  1.1      cgd }
    216  1.1      cgd 
    217  1.6  thorpej int
    218  1.1      cgd hpibpptest(unit, slave)
    219  1.1      cgd 	register int unit;
    220  1.3  mycroft 	int slave;
    221  1.1      cgd {
    222  1.1      cgd 
    223  1.6  thorpej 	return ((hpib_softc[unit].sc_controller->hpib_ppoll)(unit) &
    224  1.6  thorpej 	    (0x80 >> slave));
    225  1.1      cgd }
    226  1.1      cgd 
    227  1.6  thorpej void
    228  1.5  mycroft hpibppclear(unit)
    229  1.5  mycroft 	int unit;
    230  1.5  mycroft {
    231  1.5  mycroft 	hpib_softc[unit].sc_flags &= ~HPIBF_PPOLL;
    232  1.5  mycroft }
    233  1.5  mycroft 
    234  1.1      cgd hpibawait(unit)
    235  1.3  mycroft 	int unit;
    236  1.1      cgd {
    237  1.1      cgd 	register struct hpib_softc *hs = &hpib_softc[unit];
    238  1.1      cgd 
    239  1.1      cgd 	hs->sc_flags |= HPIBF_PPOLL;
    240  1.6  thorpej 	(hs->sc_controller->hpib_ppwatch)((void *)unit);
    241  1.1      cgd }
    242  1.1      cgd 
    243  1.6  thorpej int
    244  1.1      cgd hpibswait(unit, slave)
    245  1.1      cgd 	register int unit;
    246  1.3  mycroft 	int slave;
    247  1.1      cgd {
    248  1.1      cgd 	register int timo = hpibtimeout;
    249  1.6  thorpej 	register int mask, (*ppoll) __P((int));
    250  1.1      cgd 
    251  1.6  thorpej 	ppoll = hpib_softc[unit].sc_controller->hpib_ppoll;
    252  1.1      cgd 	mask = 0x80 >> slave;
    253  1.1      cgd 	while (((ppoll)(unit) & mask) == 0)
    254  1.1      cgd 		if (--timo == 0) {
    255  1.7  thorpej 			printf("%s: swait timeout\n",
    256  1.7  thorpej 			    hpib_softc[unit].sc_hc->hp_xname);
    257  1.1      cgd 			return(-1);
    258  1.1      cgd 		}
    259  1.1      cgd 	return(0);
    260  1.1      cgd }
    261  1.1      cgd 
    262  1.6  thorpej int
    263  1.1      cgd hpibustart(unit)
    264  1.3  mycroft 	int unit;
    265  1.1      cgd {
    266  1.1      cgd 	register struct hpib_softc *hs = &hpib_softc[unit];
    267  1.1      cgd 
    268  1.1      cgd 	if (hs->sc_type == HPIBA)
    269  1.1      cgd 		hs->sc_dq.dq_ctlr = DMA0;
    270  1.1      cgd 	else
    271  1.1      cgd 		hs->sc_dq.dq_ctlr = DMA0 | DMA1;
    272  1.1      cgd 	if (dmareq(&hs->sc_dq))
    273  1.1      cgd 		return(1);
    274  1.1      cgd 	return(0);
    275  1.1      cgd }
    276  1.1      cgd 
    277  1.6  thorpej void
    278  1.1      cgd hpibstart(unit)
    279  1.3  mycroft 	int unit;
    280  1.1      cgd {
    281  1.1      cgd 	register struct devqueue *dq;
    282  1.1      cgd 
    283  1.1      cgd 	dq = hpib_softc[unit].sc_sq.dq_forw;
    284  1.1      cgd 	(dq->dq_driver->d_go)(dq->dq_unit);
    285  1.1      cgd }
    286  1.1      cgd 
    287  1.6  thorpej void
    288  1.5  mycroft hpibgo(unit, slave, sec, addr, count, rw, timo)
    289  1.6  thorpej 	int unit, slave, sec, count, rw, timo;
    290  1.6  thorpej 	void *addr;
    291  1.1      cgd {
    292  1.6  thorpej 
    293  1.6  thorpej 	(hpib_softc[unit].sc_controller->hpib_go)(unit, slave, sec,
    294  1.6  thorpej 	    addr, count, rw, timo);
    295  1.1      cgd }
    296  1.1      cgd 
    297  1.6  thorpej void
    298  1.1      cgd hpibdone(unit)
    299  1.1      cgd 	register int unit;
    300  1.1      cgd {
    301  1.6  thorpej 
    302  1.6  thorpej 	(hpib_softc[unit].sc_controller->hpib_done)(unit);
    303  1.1      cgd }
    304  1.1      cgd 
    305  1.6  thorpej int
    306  1.8  thorpej hpibintr(arg)
    307  1.8  thorpej 	void *arg;
    308  1.1      cgd {
    309  1.8  thorpej 	struct hpib_softc *hs = arg;
    310  1.1      cgd 
    311  1.8  thorpej 	return ((hs->sc_controller->hpib_intr)(arg));
    312  1.1      cgd }
    313  1.6  thorpej #endif /* NHPIB > 0 */
    314