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