Home | History | Annotate | Line # | Download | only in dev
nhpib.c revision 1.24
      1  1.24  gmcgarry /*	$NetBSD: nhpib.c,v 1.24 2002/03/15 05:55:36 gmcgarry Exp $	*/
      2  1.18   thorpej 
      3  1.18   thorpej /*-
      4  1.18   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  1.18   thorpej  * All rights reserved.
      6  1.18   thorpej  *
      7  1.18   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.18   thorpej  * by Jason R. Thorpe.
      9  1.18   thorpej  *
     10  1.18   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.18   thorpej  * modification, are permitted provided that the following conditions
     12  1.18   thorpej  * are met:
     13  1.18   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.18   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.18   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.18   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.18   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.18   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.18   thorpej  *    must display the following acknowledgement:
     20  1.18   thorpej  *	This product includes software developed by the NetBSD
     21  1.18   thorpej  *	Foundation, Inc. and its contributors.
     22  1.18   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.18   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.18   thorpej  *    from this software without specific prior written permission.
     25  1.18   thorpej  *
     26  1.18   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.18   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.18   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.18   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.18   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.18   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.18   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.18   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.18   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.18   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.18   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.18   thorpej  */
     38   1.5       cgd 
     39   1.1       cgd /*
     40   1.4   mycroft  * Copyright (c) 1982, 1990, 1993
     41   1.4   mycroft  *	The Regents of the University of California.  All rights reserved.
     42   1.1       cgd  *
     43   1.1       cgd  * Redistribution and use in source and binary forms, with or without
     44   1.1       cgd  * modification, are permitted provided that the following conditions
     45   1.1       cgd  * are met:
     46   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     47   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     48   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     49   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     50   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     51   1.1       cgd  * 3. All advertising materials mentioning features or use of this software
     52   1.1       cgd  *    must display the following acknowledgement:
     53   1.1       cgd  *	This product includes software developed by the University of
     54   1.1       cgd  *	California, Berkeley and its contributors.
     55   1.1       cgd  * 4. Neither the name of the University nor the names of its contributors
     56   1.1       cgd  *    may be used to endorse or promote products derived from this software
     57   1.1       cgd  *    without specific prior written permission.
     58   1.1       cgd  *
     59   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     60   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     61   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     62   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     63   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     64   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     65   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     66   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     67   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     68   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     69   1.1       cgd  * SUCH DAMAGE.
     70   1.1       cgd  *
     71   1.5       cgd  *	@(#)nhpib.c	8.2 (Berkeley) 1/12/94
     72   1.1       cgd  */
     73   1.1       cgd 
     74   1.1       cgd /*
     75   1.1       cgd  * Internal/98624 HPIB driver
     76   1.1       cgd  */
     77  1.24  gmcgarry 
     78  1.24  gmcgarry #include <sys/cdefs.h>
     79  1.24  gmcgarry __KERNEL_RCSID(0, "$NetBSD: nhpib.c,v 1.24 2002/03/15 05:55:36 gmcgarry Exp $");
     80   1.1       cgd 
     81   1.4   mycroft #include <sys/param.h>
     82   1.4   mycroft #include <sys/systm.h>
     83  1.22   thorpej #include <sys/callout.h>
     84   1.6   mycroft #include <sys/kernel.h>
     85   1.4   mycroft #include <sys/buf.h>
     86  1.14   thorpej #include <sys/device.h>
     87  1.14   thorpej 
     88  1.14   thorpej #include <machine/autoconf.h>
     89  1.16   thorpej #include <machine/intr.h>
     90  1.14   thorpej 
     91  1.14   thorpej #include <hp300/dev/dioreg.h>
     92  1.14   thorpej #include <hp300/dev/diovar.h>
     93  1.14   thorpej #include <hp300/dev/diodevs.h>
     94  1.14   thorpej 
     95  1.14   thorpej #include <hp300/dev/dmavar.h>
     96   1.4   mycroft 
     97   1.4   mycroft #include <hp300/dev/nhpibreg.h>
     98   1.4   mycroft #include <hp300/dev/hpibvar.h>
     99   1.1       cgd 
    100   1.6   mycroft /*
    101   1.6   mycroft  * ODD parity table for listen and talk addresses and secondary commands.
    102   1.6   mycroft  * The TI9914A doesn't produce the parity bit.
    103   1.6   mycroft  */
    104   1.6   mycroft static u_char listnr_par[] = {
    105   1.6   mycroft 	0040,0241,0242,0043,0244,0045,0046,0247,
    106   1.6   mycroft 	0250,0051,0052,0253,0054,0255,0256,0057,
    107   1.6   mycroft 	0260,0061,0062,0263,0064,0265,0266,0067,
    108   1.6   mycroft 	0070,0271,0272,0073,0274,0075,0076,0277,
    109   1.6   mycroft };
    110   1.6   mycroft static u_char talker_par[] = {
    111   1.6   mycroft 	0100,0301,0302,0103,0304,0105,0106,0307,
    112   1.6   mycroft 	0310,0111,0112,0313,0114,0315,0316,0117,
    113   1.6   mycroft 	0320,0121,0122,0323,0124,0325,0326,0127,
    114   1.6   mycroft 	0130,0331,0332,0133,0334,0135,0136,0337,
    115   1.6   mycroft };
    116   1.6   mycroft static u_char sec_par[] = {
    117   1.6   mycroft 	0340,0141,0142,0343,0144,0345,0346,0147,
    118   1.6   mycroft 	0150,0351,0352,0153,0354,0155,0156,0357,
    119   1.6   mycroft 	0160,0361,0362,0163,0364,0165,0166,0367,
    120   1.6   mycroft 	0370,0171,0172,0373,0174,0375,0376,0177
    121   1.6   mycroft };
    122   1.6   mycroft 
    123  1.14   thorpej void	nhpibifc __P((struct nhpibdevice *));
    124  1.14   thorpej void	nhpibreadtimo __P((void *));
    125  1.14   thorpej int	nhpibwait __P((struct nhpibdevice *, int));
    126  1.14   thorpej 
    127  1.14   thorpej void	nhpibreset __P((struct hpibbus_softc *));
    128  1.14   thorpej int	nhpibsend __P((struct hpibbus_softc *, int, int, void *, int));
    129  1.14   thorpej int	nhpibrecv __P((struct hpibbus_softc *, int, int, void *, int));
    130  1.14   thorpej int	nhpibppoll __P((struct hpibbus_softc *));
    131   1.7   thorpej void	nhpibppwatch __P((void *));
    132  1.14   thorpej void	nhpibgo __P((struct hpibbus_softc *, int, int, void *, int, int, int));
    133  1.14   thorpej void	nhpibdone __P((struct hpibbus_softc *));
    134   1.9   thorpej int	nhpibintr __P((void *));
    135   1.7   thorpej 
    136   1.7   thorpej /*
    137   1.7   thorpej  * Our controller ops structure.
    138   1.7   thorpej  */
    139   1.7   thorpej struct	hpib_controller nhpib_controller = {
    140   1.7   thorpej 	nhpibreset,
    141   1.7   thorpej 	nhpibsend,
    142   1.7   thorpej 	nhpibrecv,
    143   1.7   thorpej 	nhpibppoll,
    144   1.7   thorpej 	nhpibppwatch,
    145   1.7   thorpej 	nhpibgo,
    146   1.7   thorpej 	nhpibdone,
    147   1.7   thorpej 	nhpibintr
    148   1.7   thorpej };
    149   1.7   thorpej 
    150  1.14   thorpej struct nhpib_softc {
    151  1.14   thorpej 	struct device sc_dev;		/* generic device glue */
    152  1.14   thorpej 	struct nhpibdevice *sc_regs;	/* device registers */
    153  1.14   thorpej 	struct hpibbus_softc *sc_hpibbus; /* XXX */
    154  1.22   thorpej 	struct callout sc_read_ch;
    155  1.22   thorpej 	struct callout sc_ppwatch_ch;
    156  1.14   thorpej };
    157  1.14   thorpej 
    158  1.14   thorpej int	nhpibmatch __P((struct device *, struct cfdata *, void *));
    159  1.14   thorpej void	nhpibattach __P((struct device *, struct device *, void *));
    160  1.14   thorpej 
    161  1.14   thorpej struct cfattach nhpib_ca = {
    162  1.14   thorpej 	sizeof(struct nhpib_softc), nhpibmatch, nhpibattach
    163  1.14   thorpej };
    164  1.14   thorpej 
    165   1.7   thorpej int
    166  1.14   thorpej nhpibmatch(parent, match, aux)
    167  1.14   thorpej 	struct device *parent;
    168  1.14   thorpej 	struct cfdata *match;
    169  1.14   thorpej 	void *aux;
    170   1.1       cgd {
    171  1.14   thorpej 	struct dio_attach_args *da = aux;
    172   1.1       cgd 
    173  1.20   thorpej 	if (da->da_id == DIO_DEVICE_ID_NHPIB ||
    174  1.20   thorpej 	    da->da_id == DIO_DEVICE_ID_IHPIB)
    175   1.8   thorpej 		return (1);
    176   1.8   thorpej 
    177  1.14   thorpej 	return (0);
    178   1.8   thorpej }
    179   1.8   thorpej 
    180   1.8   thorpej void
    181  1.14   thorpej nhpibattach(parent, self, aux)
    182  1.14   thorpej 	struct device *parent, *self;
    183  1.14   thorpej 	void *aux;
    184  1.14   thorpej {
    185  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)self;
    186  1.14   thorpej 	struct dio_attach_args *da = aux;
    187  1.14   thorpej 	struct hpibdev_attach_args ha;
    188  1.14   thorpej 	const char *desc;
    189  1.14   thorpej 	int ipl, type = HPIBA;
    190  1.14   thorpej 
    191  1.14   thorpej 	sc->sc_regs = (struct nhpibdevice *)iomap(dio_scodetopa(da->da_scode),
    192  1.14   thorpej 	    da->da_size);
    193  1.14   thorpej 	if (sc->sc_regs == NULL) {
    194  1.14   thorpej 		printf("\n%s: can't map registers\n", self->dv_xname);
    195  1.14   thorpej 		return;
    196  1.14   thorpej 	}
    197   1.8   thorpej 
    198  1.14   thorpej 	ipl = DIO_IPL(sc->sc_regs);
    199   1.8   thorpej 
    200  1.14   thorpej 	if (da->da_scode == 7 && internalhpib)
    201  1.14   thorpej 		desc = DIO_DEVICE_DESC_IHPIB;
    202  1.14   thorpej 	else if (da->da_id == DIO_DEVICE_ID_NHPIB) {
    203  1.14   thorpej 		type = HPIBB;
    204  1.14   thorpej 		desc = DIO_DEVICE_DESC_NHPIB;
    205  1.14   thorpej 	} else
    206  1.14   thorpej 		desc = "unknown HP-IB!";
    207  1.14   thorpej 
    208  1.14   thorpej 	printf(" ipl %d: %s\n", ipl, desc);
    209  1.14   thorpej 
    210  1.14   thorpej 	/* Establish the interrupt handler. */
    211  1.17   thorpej 	(void) dio_intr_establish(nhpibintr, sc, ipl, IPL_BIO);
    212  1.14   thorpej 
    213  1.22   thorpej 	callout_init(&sc->sc_read_ch);
    214  1.22   thorpej 	callout_init(&sc->sc_ppwatch_ch);
    215  1.22   thorpej 
    216  1.14   thorpej 	ha.ha_ops = &nhpib_controller;
    217  1.14   thorpej 	ha.ha_type = type;			/* XXX */
    218  1.14   thorpej 	ha.ha_ba = (type == HPIBA) ? HPIBA_BA :
    219  1.14   thorpej 	    (sc->sc_regs->hpib_csa & CSA_BA);
    220  1.14   thorpej 	ha.ha_softcpp = &sc->sc_hpibbus;	/* XXX */
    221  1.14   thorpej 	(void)config_found(self, &ha, hpibdevprint);
    222   1.1       cgd }
    223   1.1       cgd 
    224   1.7   thorpej void
    225  1.14   thorpej nhpibreset(hs)
    226  1.14   thorpej 	struct hpibbus_softc *hs;
    227   1.1       cgd {
    228  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    229  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    230   1.1       cgd 
    231   1.1       cgd 	hd->hpib_acr = AUX_SSWRST;
    232   1.1       cgd 	hd->hpib_ar = hs->sc_ba;
    233   1.1       cgd 	hd->hpib_lim = LIS_ERR;
    234   1.1       cgd 	hd->hpib_mim = 0;
    235   1.1       cgd 	hd->hpib_acr = AUX_CDAI;
    236   1.1       cgd 	hd->hpib_acr = AUX_CSHDW;
    237   1.1       cgd 	hd->hpib_acr = AUX_SSTD1;
    238   1.1       cgd 	hd->hpib_acr = AUX_SVSTD1;
    239   1.1       cgd 	hd->hpib_acr = AUX_CPP;
    240   1.1       cgd 	hd->hpib_acr = AUX_CHDFA;
    241   1.1       cgd 	hd->hpib_acr = AUX_CHDFE;
    242   1.1       cgd 	hd->hpib_acr = AUX_RHDF;
    243   1.1       cgd 	hd->hpib_acr = AUX_CSWRST;
    244   1.1       cgd 	nhpibifc(hd);
    245   1.1       cgd 	hd->hpib_ie = IDS_IE;
    246   1.6   mycroft 	hd->hpib_data = C_DCL_P;
    247   1.1       cgd 	DELAY(100000);
    248   1.1       cgd }
    249   1.1       cgd 
    250  1.14   thorpej void
    251   1.1       cgd nhpibifc(hd)
    252  1.15    scottr 	struct nhpibdevice *hd;
    253   1.1       cgd {
    254   1.1       cgd 	hd->hpib_acr = AUX_TCA;
    255   1.1       cgd 	hd->hpib_acr = AUX_CSRE;
    256   1.1       cgd 	hd->hpib_acr = AUX_SSIC;
    257   1.1       cgd 	DELAY(100);
    258   1.1       cgd 	hd->hpib_acr = AUX_CSIC;
    259   1.1       cgd 	hd->hpib_acr = AUX_SSRE;
    260   1.1       cgd }
    261   1.1       cgd 
    262   1.7   thorpej int
    263  1.14   thorpej nhpibsend(hs, slave, sec, ptr, origcnt)
    264  1.14   thorpej 	struct hpibbus_softc *hs;
    265  1.14   thorpej 	int slave, sec, origcnt;
    266   1.7   thorpej 	void *ptr;
    267   1.1       cgd {
    268  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    269  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    270  1.14   thorpej 	int cnt = origcnt;
    271   1.7   thorpej 	char *addr = ptr;
    272   1.1       cgd 
    273   1.1       cgd 	hd->hpib_acr = AUX_TCA;
    274   1.6   mycroft 	hd->hpib_data = C_UNL_P;
    275   1.1       cgd 	if (nhpibwait(hd, MIS_BO))
    276   1.1       cgd 		goto senderror;
    277   1.6   mycroft 	hd->hpib_data = talker_par[hs->sc_ba];
    278   1.1       cgd 	hd->hpib_acr = AUX_STON;
    279   1.1       cgd 	if (nhpibwait(hd, MIS_BO))
    280   1.1       cgd 		goto senderror;
    281   1.6   mycroft 	hd->hpib_data = listnr_par[slave];
    282   1.1       cgd 	if (nhpibwait(hd, MIS_BO))
    283   1.1       cgd 		goto senderror;
    284   1.6   mycroft 	if (sec >= 0 || sec == -2) {
    285   1.6   mycroft 		if (sec == -2)		/* selected device clear KLUDGE */
    286   1.6   mycroft 			hd->hpib_data = C_SDC_P;
    287   1.6   mycroft 		else
    288   1.6   mycroft 			hd->hpib_data = sec_par[sec];
    289   1.1       cgd 		if (nhpibwait(hd, MIS_BO))
    290   1.1       cgd 			goto senderror;
    291   1.1       cgd 	}
    292   1.1       cgd 	hd->hpib_acr = AUX_GTS;
    293   1.1       cgd 	if (cnt) {
    294   1.1       cgd 		while (--cnt > 0) {
    295   1.1       cgd 			hd->hpib_data = *addr++;
    296   1.1       cgd 			if (nhpibwait(hd, MIS_BO))
    297   1.1       cgd 				goto senderror;
    298   1.1       cgd 		}
    299   1.1       cgd 		hd->hpib_acr = AUX_EOI;
    300   1.1       cgd 		hd->hpib_data = *addr;
    301   1.1       cgd 		if (nhpibwait(hd, MIS_BO))
    302   1.1       cgd 			goto senderror;
    303   1.1       cgd 		hd->hpib_acr = AUX_TCA;
    304   1.1       cgd #if 0
    305   1.1       cgd 		/*
    306   1.1       cgd 		 * May be causing 345 disks to hang due to interference
    307   1.1       cgd 		 * with PPOLL mechanism.
    308   1.1       cgd 		 */
    309   1.6   mycroft 		hd->hpib_data = C_UNL_P;
    310   1.1       cgd 		(void) nhpibwait(hd, MIS_BO);
    311   1.1       cgd #endif
    312   1.1       cgd 	}
    313   1.1       cgd 	return(origcnt);
    314   1.6   mycroft 
    315   1.1       cgd senderror:
    316   1.1       cgd 	nhpibifc(hd);
    317   1.1       cgd 	return(origcnt - cnt - 1);
    318   1.1       cgd }
    319   1.1       cgd 
    320   1.7   thorpej int
    321  1.14   thorpej nhpibrecv(hs, slave, sec, ptr, origcnt)
    322  1.14   thorpej 	struct hpibbus_softc *hs;
    323  1.14   thorpej 	int slave, sec, origcnt;
    324   1.7   thorpej 	void *ptr;
    325   1.1       cgd {
    326  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    327  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    328  1.14   thorpej 	int cnt = origcnt;
    329   1.7   thorpej 	char *addr = ptr;
    330   1.1       cgd 
    331   1.6   mycroft 	/*
    332   1.6   mycroft 	 * Slave < 0 implies continuation of a previous receive
    333   1.6   mycroft 	 * that probably timed out.
    334   1.6   mycroft 	 */
    335   1.6   mycroft 	if (slave >= 0) {
    336   1.6   mycroft 		hd->hpib_acr = AUX_TCA;
    337   1.6   mycroft 		hd->hpib_data = C_UNL_P;
    338   1.6   mycroft 		if (nhpibwait(hd, MIS_BO))
    339   1.6   mycroft 			goto recverror;
    340   1.6   mycroft 		hd->hpib_data = listnr_par[hs->sc_ba];
    341   1.6   mycroft 		hd->hpib_acr = AUX_SLON;
    342   1.6   mycroft 		if (nhpibwait(hd, MIS_BO))
    343   1.6   mycroft 			goto recverror;
    344   1.6   mycroft 		hd->hpib_data = talker_par[slave];
    345   1.1       cgd 		if (nhpibwait(hd, MIS_BO))
    346   1.1       cgd 			goto recverror;
    347   1.6   mycroft 		if (sec >= 0) {
    348   1.6   mycroft 			hd->hpib_data = sec_par[sec];
    349   1.6   mycroft 			if (nhpibwait(hd, MIS_BO))
    350   1.6   mycroft 				goto recverror;
    351   1.6   mycroft 		}
    352   1.6   mycroft 		hd->hpib_acr = AUX_RHDF;
    353   1.6   mycroft 		hd->hpib_acr = AUX_GTS;
    354   1.1       cgd 	}
    355   1.1       cgd 	if (cnt) {
    356   1.1       cgd 		while (--cnt >= 0) {
    357   1.1       cgd 			if (nhpibwait(hd, MIS_BI))
    358   1.1       cgd 				goto recvbyteserror;
    359   1.1       cgd 			*addr++ = hd->hpib_data;
    360   1.1       cgd 		}
    361   1.1       cgd 		hd->hpib_acr = AUX_TCA;
    362   1.6   mycroft 		hd->hpib_data = (slave == 31) ? C_UNA_P : C_UNT_P;
    363   1.1       cgd 		(void) nhpibwait(hd, MIS_BO);
    364   1.1       cgd 	}
    365   1.1       cgd 	return(origcnt);
    366   1.6   mycroft 
    367   1.1       cgd recverror:
    368   1.1       cgd 	nhpibifc(hd);
    369   1.1       cgd recvbyteserror:
    370   1.1       cgd 	return(origcnt - cnt - 1);
    371   1.1       cgd }
    372   1.1       cgd 
    373   1.7   thorpej void
    374  1.14   thorpej nhpibgo(hs, slave, sec, ptr, count, rw, timo)
    375  1.14   thorpej 	struct hpibbus_softc *hs;
    376  1.14   thorpej 	int slave, sec, count, rw, timo;
    377   1.7   thorpej 	void *ptr;
    378   1.1       cgd {
    379  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    380  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    381   1.7   thorpej 	char *addr = ptr;
    382   1.1       cgd 
    383   1.1       cgd 	hs->sc_flags |= HPIBF_IO;
    384   1.6   mycroft 	if (timo)
    385   1.6   mycroft 		hs->sc_flags |= HPIBF_TIMO;
    386   1.1       cgd 	if (rw == B_READ)
    387   1.1       cgd 		hs->sc_flags |= HPIBF_READ;
    388   1.1       cgd #ifdef DEBUG
    389   1.1       cgd 	else if (hs->sc_flags & HPIBF_READ) {
    390  1.13  christos 		printf("nhpibgo: HPIBF_READ still set\n");
    391   1.1       cgd 		hs->sc_flags &= ~HPIBF_READ;
    392   1.1       cgd 	}
    393   1.1       cgd #endif
    394   1.1       cgd 	hs->sc_count = count;
    395   1.1       cgd 	hs->sc_addr = addr;
    396   1.1       cgd 	if (hs->sc_flags & HPIBF_READ) {
    397   1.1       cgd 		hs->sc_curcnt = count;
    398  1.14   thorpej 		dmago(hs->sc_dq->dq_chan, addr, count, DMAGO_BYTE|DMAGO_READ);
    399  1.14   thorpej 		nhpibrecv(hs, slave, sec, 0, 0);
    400   1.1       cgd 		hd->hpib_mim = MIS_END;
    401   1.1       cgd 	} else {
    402   1.1       cgd 		hd->hpib_mim = 0;
    403   1.1       cgd 		if (count < hpibdmathresh) {
    404   1.1       cgd 			hs->sc_curcnt = count;
    405  1.14   thorpej 			nhpibsend(hs, slave, sec, addr, count);
    406  1.14   thorpej 			nhpibdone(hs);
    407   1.1       cgd 			return;
    408   1.1       cgd 		}
    409   1.1       cgd 		hs->sc_curcnt = --count;
    410  1.14   thorpej 		dmago(hs->sc_dq->dq_chan, addr, count, DMAGO_BYTE);
    411  1.14   thorpej 		nhpibsend(hs, slave, sec, 0, 0);
    412   1.1       cgd 	}
    413  1.14   thorpej 	hd->hpib_ie = IDS_IE | IDS_DMA(hs->sc_dq->dq_chan);
    414   1.1       cgd }
    415   1.1       cgd 
    416   1.6   mycroft /*
    417   1.6   mycroft  * This timeout can only happen if a DMA read finishes DMAing with the read
    418   1.6   mycroft  * still pending (more data in read transaction than the driver was prepared
    419   1.6   mycroft  * to accept).  At the moment, variable-record tape drives are the only things
    420   1.6   mycroft  * capabale of doing this.  We repeat the necessary code from nhpibintr() -
    421   1.6   mycroft  * easier and quicker than calling nhpibintr() for this special case.
    422   1.6   mycroft  */
    423   1.6   mycroft void
    424   1.6   mycroft nhpibreadtimo(arg)
    425   1.6   mycroft 	void *arg;
    426   1.6   mycroft {
    427  1.14   thorpej 	struct hpibbus_softc *hs = arg;
    428  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    429   1.6   mycroft 	int s = splbio();
    430   1.6   mycroft 
    431   1.6   mycroft 	if (hs->sc_flags & HPIBF_IO) {
    432  1.15    scottr 		struct nhpibdevice *hd = sc->sc_regs;
    433  1.15    scottr 		struct hpibqueue *hq;
    434   1.6   mycroft 
    435   1.6   mycroft 		hd->hpib_mim = 0;
    436   1.6   mycroft 		hd->hpib_acr = AUX_TCA;
    437   1.6   mycroft 		hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
    438  1.14   thorpej 		dmafree(hs->sc_dq);
    439  1.14   thorpej 
    440  1.14   thorpej 		hq = hs->sc_queue.tqh_first;
    441  1.14   thorpej 		(hq->hq_intr)(hq->hq_softc);
    442   1.6   mycroft 	}
    443  1.14   thorpej 	splx(s);
    444   1.6   mycroft }
    445   1.6   mycroft 
    446   1.7   thorpej void
    447  1.14   thorpej nhpibdone(hs)
    448  1.14   thorpej 	struct hpibbus_softc *hs;
    449   1.1       cgd {
    450  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    451  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    452  1.14   thorpej 	int cnt;
    453   1.1       cgd 
    454   1.1       cgd 	cnt = hs->sc_curcnt;
    455   1.1       cgd 	hs->sc_addr += cnt;
    456   1.1       cgd 	hs->sc_count -= cnt;
    457   1.1       cgd 	hs->sc_flags |= HPIBF_DONE;
    458   1.1       cgd 	hd->hpib_ie = IDS_IE;
    459   1.6   mycroft 	if (hs->sc_flags & HPIBF_READ) {
    460   1.6   mycroft 		if ((hs->sc_flags & HPIBF_TIMO) &&
    461   1.6   mycroft 		    (hd->hpib_ids & IDS_IR) == 0)
    462  1.23   frueauf 			callout_reset(&sc->sc_read_ch, hz >> 2,
    463  1.22   thorpej 			    nhpibreadtimo, hs);
    464   1.6   mycroft 	} else {
    465   1.1       cgd 		if (hs->sc_count == 1) {
    466   1.1       cgd 			(void) nhpibwait(hd, MIS_BO);
    467   1.1       cgd 			hd->hpib_acr = AUX_EOI;
    468   1.1       cgd 			hd->hpib_data = *hs->sc_addr;
    469   1.1       cgd 			hd->hpib_mim = MIS_BO;
    470   1.1       cgd 		}
    471   1.1       cgd #ifdef DEBUG
    472   1.1       cgd 		else if (hs->sc_count)
    473   1.1       cgd 			panic("nhpibdone");
    474   1.1       cgd #endif
    475   1.1       cgd 	}
    476   1.1       cgd }
    477   1.1       cgd 
    478   1.7   thorpej int
    479   1.9   thorpej nhpibintr(arg)
    480   1.9   thorpej 	void *arg;
    481   1.1       cgd {
    482  1.14   thorpej 	struct nhpib_softc *sc = arg;
    483  1.14   thorpej 	struct hpibbus_softc *hs = sc->sc_hpibbus;
    484  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    485  1.14   thorpej 	struct hpibqueue *hq;
    486  1.14   thorpej 	int stat0;
    487  1.14   thorpej 	int stat1;
    488   1.1       cgd 
    489   1.1       cgd #ifdef lint
    490   1.1       cgd 	if (stat1 = unit) return(1);
    491   1.1       cgd #endif
    492   1.1       cgd 	if ((hd->hpib_ids & IDS_IR) == 0)
    493   1.1       cgd 		return(0);
    494   1.1       cgd 	stat0 = hd->hpib_mis;
    495   1.1       cgd 	stat1 = hd->hpib_lis;
    496  1.14   thorpej 
    497  1.14   thorpej 	hq = hs->sc_queue.tqh_first;
    498  1.14   thorpej 
    499   1.1       cgd 	if (hs->sc_flags & HPIBF_IO) {
    500   1.1       cgd 		hd->hpib_mim = 0;
    501   1.6   mycroft 		if ((hs->sc_flags & HPIBF_DONE) == 0) {
    502   1.6   mycroft 			hs->sc_flags &= ~HPIBF_TIMO;
    503  1.14   thorpej 			dmastop(hs->sc_dq->dq_chan);
    504   1.6   mycroft 		} else if (hs->sc_flags & HPIBF_TIMO)
    505  1.22   thorpej 			callout_stop(&sc->sc_read_ch);
    506   1.1       cgd 		hd->hpib_acr = AUX_TCA;
    507   1.6   mycroft 		hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
    508  1.14   thorpej 
    509  1.14   thorpej 		dmafree(hs->sc_dq);
    510  1.14   thorpej 		(hq->hq_intr)(hq->hq_softc);
    511   1.1       cgd 	} else if (hs->sc_flags & HPIBF_PPOLL) {
    512   1.1       cgd 		hd->hpib_mim = 0;
    513  1.14   thorpej 		stat0 = nhpibppoll(hs);
    514  1.14   thorpej 		if (stat0 & (0x80 >> hq->hq_slave)) {
    515   1.1       cgd 			hs->sc_flags &= ~HPIBF_PPOLL;
    516  1.14   thorpej 			(hq->hq_intr)(hq->hq_softc);
    517   1.1       cgd 		}
    518   1.1       cgd #ifdef DEBUG
    519   1.1       cgd 		else
    520  1.13  christos 			printf("%s: PPOLL intr bad status %x\n",
    521  1.14   thorpej 			       hs->sc_dev.dv_xname, stat0);
    522   1.1       cgd #endif
    523   1.1       cgd 	}
    524   1.1       cgd 	return(1);
    525   1.1       cgd }
    526   1.1       cgd 
    527   1.7   thorpej int
    528  1.14   thorpej nhpibppoll(hs)
    529  1.14   thorpej 	struct hpibbus_softc *hs;
    530   1.1       cgd {
    531  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    532  1.14   thorpej 	struct nhpibdevice *hd = sc->sc_regs;
    533  1.14   thorpej 	int ppoll;
    534   1.1       cgd 
    535   1.1       cgd 	hd->hpib_acr = AUX_SPP;
    536   1.1       cgd 	DELAY(25);
    537   1.1       cgd 	ppoll = hd->hpib_cpt;
    538   1.1       cgd 	hd->hpib_acr = AUX_CPP;
    539   1.1       cgd 	return(ppoll);
    540   1.1       cgd }
    541   1.1       cgd 
    542   1.6   mycroft #ifdef DEBUG
    543   1.6   mycroft int nhpibreporttimo = 0;
    544   1.6   mycroft #endif
    545   1.6   mycroft 
    546   1.7   thorpej int
    547   1.1       cgd nhpibwait(hd, x)
    548  1.15    scottr 	struct nhpibdevice *hd;
    549   1.4   mycroft 	int x;
    550   1.1       cgd {
    551  1.15    scottr 	int timo = hpibtimeout;
    552   1.1       cgd 
    553   1.1       cgd 	while ((hd->hpib_mis & x) == 0 && --timo)
    554  1.11   thorpej 		DELAY(1);
    555   1.6   mycroft 	if (timo == 0) {
    556   1.6   mycroft #ifdef DEBUG
    557   1.6   mycroft 		if (nhpibreporttimo)
    558  1.13  christos 			printf("hpib0: %s timo\n", x==MIS_BO?"OUT":"IN");
    559   1.6   mycroft #endif
    560   1.1       cgd 		return(-1);
    561   1.6   mycroft 	}
    562   1.1       cgd 	return(0);
    563   1.1       cgd }
    564   1.1       cgd 
    565   1.4   mycroft void
    566   1.3   mycroft nhpibppwatch(arg)
    567   1.3   mycroft 	void *arg;
    568   1.1       cgd {
    569  1.14   thorpej 	struct hpibbus_softc *hs = arg;
    570  1.14   thorpej 	struct nhpib_softc *sc = (struct nhpib_softc *)hs->sc_dev.dv_parent;
    571   1.1       cgd 
    572   1.1       cgd 	if ((hs->sc_flags & HPIBF_PPOLL) == 0)
    573   1.1       cgd 		return;
    574   1.4   mycroft again:
    575  1.14   thorpej 	if (nhpibppoll(hs) & (0x80 >> hs->sc_queue.tqh_first->hq_slave))
    576  1.14   thorpej        		sc->sc_regs->hpib_mim = MIS_BO;
    577   1.4   mycroft 	else if (cold)
    578   1.4   mycroft 		/* timeouts not working yet */
    579   1.4   mycroft 		goto again;
    580   1.1       cgd 	else
    581  1.22   thorpej 		callout_reset(&sc->sc_ppwatch_ch, 1, nhpibppwatch, hs);
    582   1.1       cgd }
    583