Home | History | Annotate | Line # | Download | only in dev
fhpib.c revision 1.21
      1  1.21   thorpej /*	$NetBSD: fhpib.c,v 1.21 2000/03/23 06:37:23 thorpej Exp $	*/
      2  1.19   thorpej 
      3  1.19   thorpej /*-
      4  1.19   thorpej  * Copyright (c) 1996, 1997 The NetBSD Foundation, Inc.
      5  1.19   thorpej  * All rights reserved.
      6  1.19   thorpej  *
      7  1.19   thorpej  * This code is derived from software contributed to The NetBSD Foundation
      8  1.19   thorpej  * by Jason R. Thorpe.
      9  1.19   thorpej  *
     10  1.19   thorpej  * Redistribution and use in source and binary forms, with or without
     11  1.19   thorpej  * modification, are permitted provided that the following conditions
     12  1.19   thorpej  * are met:
     13  1.19   thorpej  * 1. Redistributions of source code must retain the above copyright
     14  1.19   thorpej  *    notice, this list of conditions and the following disclaimer.
     15  1.19   thorpej  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.19   thorpej  *    notice, this list of conditions and the following disclaimer in the
     17  1.19   thorpej  *    documentation and/or other materials provided with the distribution.
     18  1.19   thorpej  * 3. All advertising materials mentioning features or use of this software
     19  1.19   thorpej  *    must display the following acknowledgement:
     20  1.19   thorpej  *	This product includes software developed by the NetBSD
     21  1.19   thorpej  *	Foundation, Inc. and its contributors.
     22  1.19   thorpej  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.19   thorpej  *    contributors may be used to endorse or promote products derived
     24  1.19   thorpej  *    from this software without specific prior written permission.
     25  1.19   thorpej  *
     26  1.19   thorpej  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.19   thorpej  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.19   thorpej  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.19   thorpej  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.19   thorpej  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.19   thorpej  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.19   thorpej  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.19   thorpej  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.19   thorpej  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.19   thorpej  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.19   thorpej  * POSSIBILITY OF SUCH DAMAGE.
     37  1.19   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  *	@(#)fhpib.c	8.2 (Berkeley) 1/12/94
     72   1.1       cgd  */
     73   1.1       cgd 
     74   1.1       cgd /*
     75   1.1       cgd  * 98625A/B HPIB driver
     76   1.1       cgd  */
     77   1.1       cgd 
     78   1.4   mycroft #include <sys/param.h>
     79   1.4   mycroft #include <sys/systm.h>
     80  1.21   thorpej #include <sys/callout.h>
     81   1.6   mycroft #include <sys/kernel.h>
     82   1.4   mycroft #include <sys/buf.h>
     83  1.14   thorpej #include <sys/device.h>
     84  1.14   thorpej 
     85  1.14   thorpej #include <machine/autoconf.h>
     86  1.17   thorpej #include <machine/intr.h>
     87  1.14   thorpej 
     88  1.14   thorpej #include <hp300/dev/dioreg.h>
     89  1.14   thorpej #include <hp300/dev/diovar.h>
     90  1.14   thorpej #include <hp300/dev/diodevs.h>
     91  1.14   thorpej 
     92  1.14   thorpej #include <hp300/dev/dmavar.h>
     93   1.4   mycroft 
     94   1.4   mycroft #include <hp300/dev/fhpibreg.h>
     95   1.4   mycroft #include <hp300/dev/hpibvar.h>
     96   1.1       cgd 
     97   1.1       cgd /*
     98   1.1       cgd  * Inline version of fhpibwait to be used in places where
     99   1.1       cgd  * we don't worry about getting hung.
    100   1.1       cgd  */
    101  1.11   thorpej #define	FHPIBWAIT(hd, m)	while (((hd)->hpib_intr & (m)) == 0) DELAY(1)
    102   1.1       cgd 
    103   1.1       cgd #ifdef DEBUG
    104   1.1       cgd int	fhpibdebugunit = -1;
    105   1.1       cgd int	fhpibdebug = 0;
    106   1.1       cgd #define FDB_FAIL	0x01
    107   1.1       cgd #define FDB_DMA		0x02
    108   1.1       cgd #define FDB_WAIT	0x04
    109   1.1       cgd #define FDB_PPOLL	0x08
    110   1.1       cgd 
    111   1.1       cgd int	dopriodma = 0;	/* use high priority DMA */
    112   1.1       cgd int	doworddma = 1;	/* non-zero if we should attempt word dma */
    113   1.1       cgd int	doppollint = 1;	/* use ppoll interrupts instead of watchdog */
    114   1.4   mycroft int	fhpibppolldelay = 50;
    115   1.1       cgd #endif
    116   1.1       cgd 
    117  1.14   thorpej void	fhpibifc __P((struct fhpibdevice *));
    118  1.14   thorpej void	fhpibdmadone __P((void *));
    119  1.14   thorpej int	fhpibwait __P((struct fhpibdevice *, int));
    120  1.14   thorpej 
    121  1.14   thorpej void	fhpibreset __P((struct hpibbus_softc *));
    122  1.14   thorpej int	fhpibsend __P((struct hpibbus_softc *, int, int, void *, int));
    123  1.14   thorpej int	fhpibrecv __P((struct hpibbus_softc *, int, int, void *, int));
    124  1.14   thorpej int	fhpibppoll __P((struct hpibbus_softc *));
    125   1.7   thorpej void	fhpibppwatch __P((void *));
    126  1.14   thorpej void	fhpibgo __P((struct hpibbus_softc *, int, int, void *, int, int, int));
    127  1.14   thorpej void	fhpibdone __P((struct hpibbus_softc *));
    128   1.9   thorpej int	fhpibintr __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 fhpib_controller = {
    134   1.7   thorpej 	fhpibreset,
    135   1.7   thorpej 	fhpibsend,
    136   1.7   thorpej 	fhpibrecv,
    137   1.7   thorpej 	fhpibppoll,
    138   1.7   thorpej 	fhpibppwatch,
    139   1.7   thorpej 	fhpibgo,
    140   1.7   thorpej 	fhpibdone,
    141   1.7   thorpej 	fhpibintr
    142   1.7   thorpej };
    143   1.7   thorpej 
    144  1.14   thorpej struct fhpib_softc {
    145  1.14   thorpej 	struct device sc_dev;		/* generic device glue */
    146  1.14   thorpej 	struct fhpibdevice *sc_regs;	/* device registers */
    147  1.14   thorpej 	int	sc_cmd;
    148  1.14   thorpej 	struct hpibbus_softc *sc_hpibbus; /* XXX */
    149  1.21   thorpej 	struct callout sc_dmadone_ch;
    150  1.21   thorpej 	struct callout sc_ppwatch_ch;
    151  1.14   thorpej };
    152  1.14   thorpej 
    153  1.14   thorpej int	fhpibmatch __P((struct device *, struct cfdata *, void *));
    154  1.14   thorpej void	fhpibattach __P((struct device *, struct device *, void *));
    155  1.14   thorpej 
    156  1.14   thorpej struct cfattach fhpib_ca = {
    157  1.14   thorpej 	sizeof(struct fhpib_softc), fhpibmatch, fhpibattach
    158  1.14   thorpej };
    159  1.14   thorpej 
    160   1.7   thorpej int
    161  1.14   thorpej fhpibmatch(parent, match, aux)
    162  1.14   thorpej 	struct device *parent;
    163  1.14   thorpej 	struct cfdata *match;
    164  1.14   thorpej 	void *aux;
    165   1.1       cgd {
    166  1.14   thorpej 	struct dio_attach_args *da = aux;
    167   1.7   thorpej 
    168  1.14   thorpej 	if (da->da_id == DIO_DEVICE_ID_FHPIB)
    169  1.14   thorpej 		return (1);
    170   1.7   thorpej 
    171  1.14   thorpej 	return (0);
    172   1.8   thorpej }
    173   1.8   thorpej 
    174   1.8   thorpej void
    175  1.14   thorpej fhpibattach(parent, self, aux)
    176  1.14   thorpej 	struct device *parent, *self;
    177  1.14   thorpej 	void *aux;
    178  1.14   thorpej {
    179  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)self;
    180  1.14   thorpej 	struct dio_attach_args *da = aux;
    181  1.14   thorpej 	struct hpibdev_attach_args ha;
    182  1.14   thorpej 	int ipl;
    183  1.14   thorpej 
    184  1.14   thorpej 	sc->sc_regs = (struct fhpibdevice *)iomap(dio_scodetopa(da->da_scode),
    185  1.14   thorpej 	    da->da_size);
    186  1.14   thorpej 	if (sc->sc_regs == NULL) {
    187  1.14   thorpej 		printf("\n%s: can't map registers\n", self->dv_xname);
    188  1.14   thorpej 		return;
    189  1.14   thorpej 	}
    190  1.14   thorpej 
    191  1.14   thorpej 	ipl = DIO_IPL(sc->sc_regs);
    192  1.14   thorpej 	printf(" ipl %d: %s\n", ipl, DIO_DEVICE_DESC_FHPIB);
    193   1.8   thorpej 
    194  1.14   thorpej 	/* Establish the interrupt handler. */
    195  1.18   thorpej 	(void) dio_intr_establish(fhpibintr, sc, ipl, IPL_BIO);
    196  1.14   thorpej 
    197  1.21   thorpej 	callout_init(&sc->sc_dmadone_ch);
    198  1.21   thorpej 	callout_init(&sc->sc_ppwatch_ch);
    199  1.21   thorpej 
    200  1.14   thorpej 	ha.ha_ops = &fhpib_controller;
    201  1.14   thorpej 	ha.ha_type = HPIBC;			/* XXX */
    202  1.14   thorpej 	ha.ha_ba = HPIBC_BA;
    203  1.14   thorpej 	ha.ha_softcpp = &sc->sc_hpibbus;	/* XXX */
    204  1.14   thorpej 	(void)config_found(self, &ha, hpibdevprint);
    205   1.1       cgd }
    206   1.1       cgd 
    207   1.7   thorpej void
    208  1.14   thorpej fhpibreset(hs)
    209  1.14   thorpej 	struct hpibbus_softc *hs;
    210   1.1       cgd {
    211  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    212  1.14   thorpej 	struct fhpibdevice *hd = sc->sc_regs;
    213   1.1       cgd 
    214   1.1       cgd 	hd->hpib_cid = 0xFF;
    215   1.1       cgd 	DELAY(100);
    216   1.1       cgd 	hd->hpib_cmd = CT_8BIT;
    217   1.1       cgd 	hd->hpib_ar = AR_ARONC;
    218   1.1       cgd 	fhpibifc(hd);
    219   1.1       cgd 	hd->hpib_ie = IDS_IE;
    220   1.1       cgd 	hd->hpib_data = C_DCL;
    221   1.1       cgd 	DELAY(100000);
    222   1.1       cgd 	/*
    223   1.1       cgd 	 * See if we can do word dma.
    224   1.1       cgd 	 * If so, we should be able to write and read back the appropos bit.
    225   1.1       cgd 	 */
    226   1.1       cgd 	hd->hpib_ie |= IDS_WDMA;
    227   1.1       cgd 	if (hd->hpib_ie & IDS_WDMA) {
    228   1.1       cgd 		hd->hpib_ie &= ~IDS_WDMA;
    229   1.1       cgd 		hs->sc_flags |= HPIBF_DMA16;
    230   1.1       cgd #ifdef DEBUG
    231   1.1       cgd 		if (fhpibdebug & FDB_DMA)
    232  1.14   thorpej 			printf("fhpibtype: %s has word dma\n",
    233  1.14   thorpej 			    sc->sc_dev.dv_xname);
    234   1.1       cgd 
    235   1.1       cgd #endif
    236   1.1       cgd 	}
    237   1.1       cgd }
    238   1.1       cgd 
    239  1.14   thorpej void
    240   1.1       cgd fhpibifc(hd)
    241  1.15    scottr 	struct fhpibdevice *hd;
    242   1.1       cgd {
    243   1.1       cgd 	hd->hpib_cmd |= CT_IFC;
    244   1.1       cgd 	hd->hpib_cmd |= CT_INITFIFO;
    245   1.1       cgd 	DELAY(100);
    246   1.1       cgd 	hd->hpib_cmd &= ~CT_IFC;
    247   1.1       cgd 	hd->hpib_cmd |= CT_REN;
    248   1.1       cgd 	hd->hpib_stat = ST_ATN;
    249   1.1       cgd }
    250   1.1       cgd 
    251   1.7   thorpej int
    252  1.14   thorpej fhpibsend(hs, slave, sec, ptr, origcnt)
    253  1.14   thorpej 	struct hpibbus_softc *hs;
    254  1.14   thorpej 	int slave, sec, origcnt;
    255   1.7   thorpej 	void *ptr;
    256   1.1       cgd {
    257  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    258  1.14   thorpej 	struct fhpibdevice *hd = sc->sc_regs;
    259  1.15    scottr 	int cnt = origcnt;
    260  1.15    scottr 	int timo;
    261   1.7   thorpej 	char *addr = ptr;
    262   1.1       cgd 
    263   1.1       cgd 	hd->hpib_stat = 0;
    264   1.1       cgd 	hd->hpib_imask = IM_IDLE | IM_ROOM;
    265   1.1       cgd 	if (fhpibwait(hd, IM_IDLE) < 0)
    266   1.1       cgd 		goto senderr;
    267   1.1       cgd 	hd->hpib_stat = ST_ATN;
    268   1.1       cgd 	hd->hpib_data = C_UNL;
    269   1.1       cgd 	hd->hpib_data = C_TAG + hs->sc_ba;
    270   1.1       cgd 	hd->hpib_data = C_LAG + slave;
    271   1.6   mycroft 	if (sec < 0) {
    272   1.6   mycroft 		if (sec == -2)		/* selected device clear KLUDGE */
    273   1.6   mycroft 			hd->hpib_data = C_SDC;
    274   1.6   mycroft 	} else
    275   1.1       cgd 		hd->hpib_data = C_SCG + sec;
    276   1.1       cgd 	if (fhpibwait(hd, IM_IDLE) < 0)
    277   1.1       cgd 		goto senderr;
    278   1.1       cgd 	if (cnt) {
    279   1.1       cgd 		hd->hpib_stat = ST_WRITE;
    280   1.1       cgd 		while (--cnt) {
    281   1.1       cgd 			hd->hpib_data = *addr++;
    282   1.1       cgd 			timo = hpibtimeout;
    283   1.1       cgd 			while ((hd->hpib_intr & IM_ROOM) == 0) {
    284   1.1       cgd 				if (--timo <= 0)
    285   1.1       cgd 					goto senderr;
    286  1.11   thorpej 				DELAY(1);
    287   1.1       cgd 			}
    288   1.1       cgd 		}
    289   1.1       cgd 		hd->hpib_stat = ST_EOI;
    290   1.1       cgd 		hd->hpib_data = *addr;
    291   1.1       cgd 		FHPIBWAIT(hd, IM_ROOM);
    292   1.1       cgd 		hd->hpib_stat = ST_ATN;
    293   1.1       cgd 		/* XXX: HP-UX claims bug with CS80 transparent messages */
    294   1.1       cgd 		if (sec == 0x12)
    295   1.1       cgd 			DELAY(150);
    296   1.1       cgd 		hd->hpib_data = C_UNL;
    297   1.1       cgd 		(void) fhpibwait(hd, IM_IDLE);
    298   1.1       cgd 	}
    299   1.1       cgd 	hd->hpib_imask = 0;
    300   1.1       cgd 	return (origcnt);
    301   1.6   mycroft 
    302   1.1       cgd senderr:
    303   1.1       cgd 	hd->hpib_imask = 0;
    304   1.1       cgd 	fhpibifc(hd);
    305   1.1       cgd #ifdef DEBUG
    306   1.1       cgd 	if (fhpibdebug & FDB_FAIL) {
    307  1.13  christos 		printf("%s: fhpibsend failed: slave %d, sec %x, ",
    308  1.14   thorpej 		    sc->sc_dev.dv_xname, slave, sec);
    309  1.13  christos 		printf("sent %d of %d bytes\n", origcnt-cnt-1, origcnt);
    310   1.1       cgd 	}
    311   1.1       cgd #endif
    312   1.6   mycroft 	return (origcnt - cnt - 1);
    313   1.1       cgd }
    314   1.1       cgd 
    315   1.7   thorpej int
    316  1.14   thorpej fhpibrecv(hs, slave, sec, ptr, origcnt)
    317  1.14   thorpej 	struct hpibbus_softc *hs;
    318  1.14   thorpej 	int slave, sec, origcnt;
    319   1.7   thorpej 	void *ptr;
    320   1.1       cgd {
    321  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    322  1.14   thorpej 	struct fhpibdevice *hd = sc->sc_regs;
    323  1.15    scottr 	int cnt = origcnt;
    324  1.15    scottr 	int timo;
    325   1.7   thorpej 	char *addr = ptr;
    326   1.1       cgd 
    327   1.6   mycroft 	/*
    328   1.6   mycroft 	 * Slave < 0 implies continuation of a previous receive
    329   1.6   mycroft 	 * that probably timed out.
    330   1.6   mycroft 	 */
    331   1.6   mycroft 	if (slave >= 0) {
    332   1.6   mycroft 		hd->hpib_stat = 0;
    333   1.6   mycroft 		hd->hpib_imask = IM_IDLE | IM_ROOM | IM_BYTE;
    334   1.6   mycroft 		if (fhpibwait(hd, IM_IDLE) < 0)
    335   1.6   mycroft 			goto recverror;
    336   1.6   mycroft 		hd->hpib_stat = ST_ATN;
    337   1.6   mycroft 		hd->hpib_data = C_UNL;
    338   1.6   mycroft 		hd->hpib_data = C_LAG + hs->sc_ba;
    339   1.6   mycroft 		hd->hpib_data = C_TAG + slave;
    340   1.6   mycroft 		if (sec != -1)
    341   1.6   mycroft 			hd->hpib_data = C_SCG + sec;
    342   1.6   mycroft 		if (fhpibwait(hd, IM_IDLE) < 0)
    343   1.6   mycroft 			goto recverror;
    344   1.6   mycroft 		hd->hpib_stat = ST_READ0;
    345   1.6   mycroft 		hd->hpib_data = 0;
    346   1.6   mycroft 	}
    347   1.1       cgd 	if (cnt) {
    348   1.1       cgd 		while (--cnt >= 0) {
    349   1.1       cgd 			timo = hpibtimeout;
    350   1.1       cgd 			while ((hd->hpib_intr & IM_BYTE) == 0) {
    351   1.1       cgd 				if (--timo == 0)
    352   1.1       cgd 					goto recvbyteserror;
    353  1.11   thorpej 				DELAY(1);
    354   1.1       cgd 			}
    355   1.1       cgd 			*addr++ = hd->hpib_data;
    356   1.1       cgd 		}
    357   1.1       cgd 		FHPIBWAIT(hd, IM_ROOM);
    358   1.1       cgd 		hd->hpib_stat = ST_ATN;
    359   1.1       cgd 		hd->hpib_data = (slave == 31) ? C_UNA : C_UNT;
    360   1.1       cgd 		(void) fhpibwait(hd, IM_IDLE);
    361   1.1       cgd 	}
    362   1.1       cgd 	hd->hpib_imask = 0;
    363   1.1       cgd 	return (origcnt);
    364   1.1       cgd 
    365   1.1       cgd recverror:
    366   1.1       cgd 	fhpibifc(hd);
    367   1.1       cgd recvbyteserror:
    368   1.1       cgd 	hd->hpib_imask = 0;
    369   1.1       cgd #ifdef DEBUG
    370   1.1       cgd 	if (fhpibdebug & FDB_FAIL) {
    371  1.13  christos 		printf("%s: fhpibrecv failed: slave %d, sec %x, ",
    372  1.14   thorpej 		    sc->sc_dev.dv_xname, slave, sec);
    373  1.13  christos 		printf("got %d of %d bytes\n", origcnt-cnt-1, origcnt);
    374   1.1       cgd 	}
    375   1.1       cgd #endif
    376   1.6   mycroft 	return (origcnt - cnt - 1);
    377   1.1       cgd }
    378   1.1       cgd 
    379   1.7   thorpej void
    380  1.14   thorpej fhpibgo(hs, slave, sec, ptr, count, rw, timo)
    381  1.14   thorpej 	struct hpibbus_softc *hs;
    382  1.14   thorpej 	int slave, sec, count, rw, timo;
    383   1.7   thorpej 	void *ptr;
    384   1.1       cgd {
    385  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    386  1.15    scottr 	struct fhpibdevice *hd = sc->sc_regs;
    387  1.15    scottr 	int i;
    388   1.7   thorpej 	char *addr = ptr;
    389   1.1       cgd 	int flags = 0;
    390   1.1       cgd 
    391   1.1       cgd 	hs->sc_flags |= HPIBF_IO;
    392   1.6   mycroft 	if (timo)
    393   1.6   mycroft 		hs->sc_flags |= HPIBF_TIMO;
    394   1.1       cgd 	if (rw == B_READ)
    395   1.1       cgd 		hs->sc_flags |= HPIBF_READ;
    396   1.1       cgd #ifdef DEBUG
    397   1.1       cgd 	else if (hs->sc_flags & HPIBF_READ) {
    398  1.13  christos 		printf("fhpibgo: HPIBF_READ still set\n");
    399   1.1       cgd 		hs->sc_flags &= ~HPIBF_READ;
    400   1.1       cgd 	}
    401   1.1       cgd #endif
    402   1.1       cgd 	hs->sc_count = count;
    403   1.1       cgd 	hs->sc_addr = addr;
    404   1.1       cgd #ifdef DEBUG
    405  1.14   thorpej 	/* fhpibtransfer[unit]++;			XXX */
    406   1.1       cgd #endif
    407   1.1       cgd 	if ((hs->sc_flags & HPIBF_DMA16) &&
    408   1.1       cgd 	    ((int)addr & 1) == 0 && count && (count & 1) == 0
    409   1.1       cgd #ifdef DEBUG
    410   1.1       cgd 	    && doworddma
    411   1.1       cgd #endif
    412   1.1       cgd 	    ) {
    413   1.1       cgd #ifdef DEBUG
    414  1.14   thorpej 		/* fhpibworddma[unit]++;		XXX */
    415   1.1       cgd #endif
    416   1.1       cgd 		flags |= DMAGO_WORD;
    417   1.1       cgd 		hd->hpib_latch = 0;
    418   1.1       cgd 	}
    419   1.1       cgd #ifdef DEBUG
    420   1.1       cgd 	if (dopriodma)
    421   1.1       cgd 		flags |= DMAGO_PRI;
    422   1.1       cgd #endif
    423   1.1       cgd 	if (hs->sc_flags & HPIBF_READ) {
    424  1.14   thorpej 		sc->sc_cmd = CT_REN | CT_8BIT;
    425   1.1       cgd 		hs->sc_curcnt = count;
    426  1.14   thorpej 		dmago(hs->sc_dq->dq_chan, addr, count, flags|DMAGO_READ);
    427  1.14   thorpej 		if (fhpibrecv(hs, slave, sec, 0, 0) < 0) {
    428   1.1       cgd #ifdef DEBUG
    429  1.13  christos 			printf("fhpibgo: recv failed, retrying...\n");
    430   1.1       cgd #endif
    431  1.14   thorpej 			(void) fhpibrecv(hs, slave, sec, 0, 0);
    432   1.1       cgd 		}
    433   1.1       cgd 		i = hd->hpib_cmd;
    434  1.14   thorpej 		hd->hpib_cmd = sc->sc_cmd;
    435  1.14   thorpej 		hd->hpib_ie = IDS_DMA(hs->sc_dq->dq_chan) |
    436   1.1       cgd 			((flags & DMAGO_WORD) ? IDS_WDMA : 0);
    437   1.1       cgd 		return;
    438   1.1       cgd 	}
    439  1.14   thorpej 	sc->sc_cmd = CT_REN | CT_8BIT | CT_FIFOSEL;
    440   1.1       cgd 	if (count < hpibdmathresh) {
    441   1.1       cgd #ifdef DEBUG
    442  1.14   thorpej 		/* fhpibnondma[unit]++;			XXX */
    443   1.1       cgd 		if (flags & DMAGO_WORD)
    444  1.14   thorpej 			/* fhpibworddma[unit]--;	XXX */ ;
    445   1.1       cgd #endif
    446   1.1       cgd 		hs->sc_curcnt = count;
    447  1.14   thorpej 		(void) fhpibsend(hs, slave, sec, addr, count);
    448  1.14   thorpej 		fhpibdone(hs);
    449   1.1       cgd 		return;
    450   1.1       cgd 	}
    451   1.1       cgd 	count -= (flags & DMAGO_WORD) ? 2 : 1;
    452   1.1       cgd 	hs->sc_curcnt = count;
    453  1.14   thorpej 	dmago(hs->sc_dq->dq_chan, addr, count, flags);
    454  1.14   thorpej 	if (fhpibsend(hs, slave, sec, 0, 0) < 0) {
    455   1.1       cgd #ifdef DEBUG
    456  1.13  christos 		printf("fhpibgo: send failed, retrying...\n");
    457   1.1       cgd #endif
    458  1.14   thorpej 		(void) fhpibsend(hs, slave, sec, 0, 0);
    459   1.1       cgd 	}
    460   1.1       cgd 	i = hd->hpib_cmd;
    461  1.14   thorpej 	hd->hpib_cmd = sc->sc_cmd;
    462  1.14   thorpej 	hd->hpib_ie = IDS_DMA(hs->sc_dq->dq_chan) | IDS_WRITE |
    463   1.1       cgd 		((flags & DMAGO_WORD) ? IDS_WDMA : 0);
    464   1.1       cgd }
    465   1.1       cgd 
    466   1.6   mycroft /*
    467   1.6   mycroft  * A DMA read can finish but the device can still be waiting (MAG-tape
    468   1.6   mycroft  * with more data than we're waiting for).  This timeout routine
    469   1.6   mycroft  * takes care of that.  Somehow, the thing gets hosed.  For now, since
    470   1.6   mycroft  * this should be a very rare occurence, we RESET it.
    471   1.6   mycroft  */
    472   1.6   mycroft void
    473   1.6   mycroft fhpibdmadone(arg)
    474   1.6   mycroft 	void *arg;
    475   1.6   mycroft {
    476  1.15    scottr 	struct hpibbus_softc *hs = arg;
    477  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    478   1.6   mycroft 	int s = splbio();
    479   1.6   mycroft 
    480   1.6   mycroft 	if (hs->sc_flags & HPIBF_IO) {
    481  1.15    scottr 		struct fhpibdevice *hd = sc->sc_regs;
    482  1.15    scottr 		struct hpibqueue *hq;
    483   1.6   mycroft 
    484   1.6   mycroft 		hd->hpib_imask = 0;
    485   1.6   mycroft 		hd->hpib_cid = 0xFF;
    486   1.6   mycroft 		DELAY(100);
    487   1.6   mycroft 		hd->hpib_cmd = CT_8BIT;
    488   1.6   mycroft 		hd->hpib_ar = AR_ARONC;
    489   1.6   mycroft 		fhpibifc(hd);
    490   1.6   mycroft 		hd->hpib_ie = IDS_IE;
    491   1.6   mycroft 		hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
    492  1.14   thorpej 		dmafree(hs->sc_dq);
    493  1.14   thorpej 
    494  1.14   thorpej 		hq = hs->sc_queue.tqh_first;
    495  1.14   thorpej 		(hq->hq_intr)(hq->hq_softc);
    496   1.6   mycroft 	}
    497  1.14   thorpej 	splx(s);
    498   1.6   mycroft }
    499   1.6   mycroft 
    500   1.7   thorpej void
    501  1.14   thorpej fhpibdone(hs)
    502  1.14   thorpej 	struct hpibbus_softc *hs;
    503   1.1       cgd {
    504  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    505  1.15    scottr 	struct fhpibdevice *hd = sc->sc_regs;
    506  1.15    scottr 	char *addr;
    507  1.15    scottr 	int cnt;
    508   1.1       cgd 
    509   1.1       cgd 	cnt = hs->sc_curcnt;
    510   1.1       cgd 	hs->sc_addr += cnt;
    511   1.1       cgd 	hs->sc_count -= cnt;
    512   1.1       cgd #ifdef DEBUG
    513  1.16    scottr 	if ((fhpibdebug & FDB_DMA) && fhpibdebugunit == sc->sc_dev.dv_unit)
    514  1.16    scottr 		printf("fhpibdone: addr %p cnt %d\n",
    515   1.1       cgd 		       hs->sc_addr, hs->sc_count);
    516   1.1       cgd #endif
    517   1.6   mycroft 	if (hs->sc_flags & HPIBF_READ) {
    518   1.1       cgd 		hd->hpib_imask = IM_IDLE | IM_BYTE;
    519   1.6   mycroft 		if (hs->sc_flags & HPIBF_TIMO)
    520  1.21   thorpej 			callout_reset(&sc->sc_dmadone_ch, hz >> 2,
    521  1.21   thorpej 			    fhpibdmadone, hs);
    522   1.6   mycroft 	} else {
    523   1.1       cgd 		cnt = hs->sc_count;
    524   1.1       cgd 		if (cnt) {
    525   1.1       cgd 			addr = hs->sc_addr;
    526   1.1       cgd 			hd->hpib_imask = IM_IDLE | IM_ROOM;
    527   1.1       cgd 			FHPIBWAIT(hd, IM_IDLE);
    528   1.1       cgd 			hd->hpib_stat = ST_WRITE;
    529   1.1       cgd 			while (--cnt) {
    530   1.1       cgd 				hd->hpib_data = *addr++;
    531   1.1       cgd 				FHPIBWAIT(hd, IM_ROOM);
    532   1.1       cgd 			}
    533   1.1       cgd 			hd->hpib_stat = ST_EOI;
    534   1.1       cgd 			hd->hpib_data = *addr;
    535   1.1       cgd 		}
    536   1.1       cgd 		hd->hpib_imask = IM_IDLE;
    537   1.1       cgd 	}
    538   1.1       cgd 	hs->sc_flags |= HPIBF_DONE;
    539   1.1       cgd 	hd->hpib_stat = ST_IENAB;
    540   1.1       cgd 	hd->hpib_ie = IDS_IE;
    541   1.1       cgd }
    542   1.1       cgd 
    543   1.7   thorpej int
    544   1.9   thorpej fhpibintr(arg)
    545   1.9   thorpej 	void *arg;
    546   1.1       cgd {
    547  1.14   thorpej 	struct fhpib_softc *sc = arg;
    548  1.15    scottr 	struct hpibbus_softc *hs = sc->sc_hpibbus;
    549  1.15    scottr 	struct fhpibdevice *hd = sc->sc_regs;
    550  1.15    scottr 	struct hpibqueue *hq;
    551  1.15    scottr 	int stat0;
    552   1.1       cgd 
    553   1.1       cgd 	stat0 = hd->hpib_ids;
    554   1.1       cgd 	if ((stat0 & (IDS_IE|IDS_IR)) != (IDS_IE|IDS_IR)) {
    555   1.1       cgd #ifdef DEBUG
    556   1.1       cgd 		if ((fhpibdebug & FDB_FAIL) && (stat0 & IDS_IR) &&
    557   1.1       cgd 		    (hs->sc_flags & (HPIBF_IO|HPIBF_DONE)) != HPIBF_IO)
    558  1.13  christos 			printf("%s: fhpibintr: bad status %x\n",
    559  1.14   thorpej 			sc->sc_dev.dv_xname, stat0);
    560  1.14   thorpej 		/* fhpibbadint[0]++;			XXX */
    561   1.1       cgd #endif
    562   1.1       cgd 		return(0);
    563   1.1       cgd 	}
    564   1.1       cgd 	if ((hs->sc_flags & (HPIBF_IO|HPIBF_DONE)) == HPIBF_IO) {
    565   1.1       cgd #ifdef DEBUG
    566  1.14   thorpej 		/* fhpibbadint[1]++;			XXX */
    567   1.1       cgd #endif
    568   1.1       cgd 		return(0);
    569   1.1       cgd 	}
    570   1.1       cgd #ifdef DEBUG
    571  1.16    scottr 	if ((fhpibdebug & FDB_DMA) && fhpibdebugunit == sc->sc_dev.dv_unit)
    572  1.13  christos 		printf("fhpibintr: flags %x\n", hs->sc_flags);
    573   1.1       cgd #endif
    574  1.14   thorpej 	hq = hs->sc_queue.tqh_first;
    575   1.1       cgd 	if (hs->sc_flags & HPIBF_IO) {
    576   1.6   mycroft 		if (hs->sc_flags & HPIBF_TIMO)
    577  1.21   thorpej 			callout_stop(&sc->sc_dmadone_ch);
    578   1.1       cgd 		stat0 = hd->hpib_cmd;
    579  1.14   thorpej 		hd->hpib_cmd = sc->sc_cmd & ~CT_8BIT;
    580   1.1       cgd 		hd->hpib_stat = 0;
    581   1.1       cgd 		hd->hpib_cmd = CT_REN | CT_8BIT;
    582   1.1       cgd 		stat0 = hd->hpib_intr;
    583   1.1       cgd 		hd->hpib_imask = 0;
    584   1.6   mycroft 		hs->sc_flags &= ~(HPIBF_DONE|HPIBF_IO|HPIBF_READ|HPIBF_TIMO);
    585  1.14   thorpej 		dmafree(hs->sc_dq);
    586  1.14   thorpej 		(hq->hq_intr)(hq->hq_softc);
    587   1.1       cgd 	} else if (hs->sc_flags & HPIBF_PPOLL) {
    588   1.1       cgd 		stat0 = hd->hpib_intr;
    589   1.1       cgd #ifdef DEBUG
    590   1.1       cgd 		if ((fhpibdebug & FDB_FAIL) &&
    591   1.1       cgd 		    doppollint && (stat0 & IM_PPRESP) == 0)
    592  1.13  christos 			printf("%s: fhpibintr: bad intr reg %x\n",
    593  1.14   thorpej 			    sc->sc_dev.dv_xname, stat0);
    594   1.1       cgd #endif
    595   1.1       cgd 		hd->hpib_stat = 0;
    596   1.1       cgd 		hd->hpib_imask = 0;
    597   1.1       cgd #ifdef DEBUG
    598  1.14   thorpej 		stat0 = fhpibppoll(hs);
    599  1.16    scottr 		if ((fhpibdebug & FDB_PPOLL) &&
    600  1.16    scottr 		    fhpibdebugunit == sc->sc_dev.dv_unit)
    601  1.13  christos 			printf("fhpibintr: got PPOLL status %x\n", stat0);
    602  1.14   thorpej 		if ((stat0 & (0x80 >> hq->hq_slave)) == 0) {
    603   1.4   mycroft 			/*
    604   1.4   mycroft 			 * XXX give it another shot (68040)
    605   1.4   mycroft 			 */
    606  1.14   thorpej 			/* fhpibppollfail[unit]++;	XXX */
    607   1.4   mycroft 			DELAY(fhpibppolldelay);
    608  1.14   thorpej 			stat0 = fhpibppoll(hs);
    609  1.14   thorpej 			if ((stat0 & (0x80 >> hq->hq_slave)) == 0 &&
    610  1.16    scottr 			    (fhpibdebug & FDB_PPOLL) &&
    611  1.16    scottr 			    fhpibdebugunit == sc->sc_dev.dv_unit)
    612  1.13  christos 				printf("fhpibintr: PPOLL: unit %d slave %d stat %x\n",
    613  1.16    scottr 				       sc->sc_dev.dv_unit, hq->hq_slave, stat0);
    614   1.1       cgd 		}
    615   1.1       cgd #endif
    616   1.1       cgd 		hs->sc_flags &= ~HPIBF_PPOLL;
    617  1.14   thorpej 		(hq->hq_intr)(hq->hq_softc);
    618   1.1       cgd 	}
    619   1.1       cgd 	return(1);
    620   1.1       cgd }
    621   1.1       cgd 
    622   1.7   thorpej int
    623  1.14   thorpej fhpibppoll(hs)
    624  1.14   thorpej 	struct hpibbus_softc *hs;
    625   1.1       cgd {
    626  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    627  1.15    scottr 	struct fhpibdevice *hd = sc->sc_regs;
    628  1.15    scottr 	int ppoll;
    629   1.1       cgd 
    630   1.1       cgd 	hd->hpib_stat = 0;
    631   1.1       cgd 	hd->hpib_psense = 0;
    632   1.1       cgd 	hd->hpib_pmask = 0xFF;
    633   1.1       cgd 	hd->hpib_imask = IM_PPRESP | IM_PABORT;
    634   1.1       cgd 	DELAY(25);
    635   1.1       cgd 	hd->hpib_intr = IM_PABORT;
    636   1.1       cgd 	ppoll = hd->hpib_data;
    637   1.1       cgd 	if (hd->hpib_intr & IM_PABORT)
    638   1.1       cgd 		ppoll = 0;
    639   1.1       cgd 	hd->hpib_imask = 0;
    640   1.1       cgd 	hd->hpib_pmask = 0;
    641   1.1       cgd 	hd->hpib_stat = ST_IENAB;
    642   1.1       cgd 	return(ppoll);
    643   1.1       cgd }
    644   1.1       cgd 
    645   1.7   thorpej int
    646   1.1       cgd fhpibwait(hd, x)
    647  1.15    scottr 	struct fhpibdevice *hd;
    648   1.4   mycroft 	int x;
    649   1.1       cgd {
    650  1.15    scottr 	int timo = hpibtimeout;
    651   1.1       cgd 
    652   1.1       cgd 	while ((hd->hpib_intr & x) == 0 && --timo)
    653  1.11   thorpej 		DELAY(1);
    654   1.1       cgd 	if (timo == 0) {
    655   1.1       cgd #ifdef DEBUG
    656   1.1       cgd 		if (fhpibdebug & FDB_FAIL)
    657  1.16    scottr 			printf("fhpibwait(%p, %x) timeout\n", hd, x);
    658   1.1       cgd #endif
    659   1.1       cgd 		return(-1);
    660   1.1       cgd 	}
    661   1.1       cgd 	return(0);
    662   1.1       cgd }
    663   1.1       cgd 
    664   1.1       cgd /*
    665   1.4   mycroft  * XXX: this will have to change if we ever allow more than one
    666   1.1       cgd  * pending operation per HP-IB.
    667   1.1       cgd  */
    668   1.4   mycroft void
    669   1.3   mycroft fhpibppwatch(arg)
    670   1.3   mycroft 	void *arg;
    671   1.1       cgd {
    672  1.15    scottr 	struct hpibbus_softc *hs = arg;
    673  1.14   thorpej 	struct fhpib_softc *sc = (struct fhpib_softc *)hs->sc_dev.dv_parent;
    674  1.15    scottr 	struct fhpibdevice *hd = sc->sc_regs;
    675  1.15    scottr 	int slave;
    676   1.1       cgd 
    677   1.1       cgd 	if ((hs->sc_flags & HPIBF_PPOLL) == 0)
    678   1.1       cgd 		return;
    679  1.14   thorpej 	slave = (0x80 >> hs->sc_queue.tqh_first->hq_slave);
    680   1.1       cgd #ifdef DEBUG
    681   1.1       cgd 	if (!doppollint) {
    682  1.14   thorpej 		if (fhpibppoll(hs) & slave) {
    683   1.1       cgd 			hd->hpib_stat = ST_IENAB;
    684   1.1       cgd 			hd->hpib_imask = IM_IDLE | IM_ROOM;
    685   1.1       cgd 		} else
    686  1.21   thorpej 			callout_reset(&sc->sc_ppwatch_ch, 1, fhpibppwatch, sc);
    687   1.1       cgd 		return;
    688   1.1       cgd 	}
    689  1.14   thorpej 	if ((fhpibdebug & FDB_PPOLL) && sc->sc_dev.dv_unit == fhpibdebugunit)
    690  1.14   thorpej 		printf("fhpibppwatch: sense request on %s\n",
    691  1.14   thorpej 		    sc->sc_dev.dv_xname);
    692   1.1       cgd #endif
    693   1.1       cgd 	hd->hpib_psense = ~slave;
    694   1.1       cgd 	hd->hpib_pmask = slave;
    695   1.1       cgd 	hd->hpib_stat = ST_IENAB;
    696   1.1       cgd 	hd->hpib_imask = IM_PPRESP | IM_PABORT;
    697   1.1       cgd 	hd->hpib_ie = IDS_IE;
    698   1.1       cgd }
    699