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