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