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