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