Home | History | Annotate | Line # | Download | only in obio
esp.c revision 1.20.6.2
      1 /*	$NetBSD: esp.c,v 1.20.6.2 1999/07/01 23:10:04 thorpej Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1997 Jason R. Thorpe.
      5  * All rights reserved.
      6  *
      7  * Redistribution and use in source and binary forms, with or without
      8  * modification, are permitted provided that the following conditions
      9  * are met:
     10  * 1. Redistributions of source code must retain the above copyright
     11  *    notice, this list of conditions and the following disclaimer.
     12  * 2. Redistributions in binary form must reproduce the above copyright
     13  *    notice, this list of conditions and the following disclaimer in the
     14  *    documentation and/or other materials provided with the distribution.
     15  * 3. All advertising materials mentioning features or use of this software
     16  *    must display the following acknowledgement:
     17  *	This product includes software developed for the NetBSD Project
     18  *	by Jason R. Thorpe.
     19  * 4. The name of the author may not be used to endorse or promote products
     20  *    derived from this software without specific prior written permission.
     21  *
     22  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  */
     33 
     34 /*
     35  * Copyright (c) 1994 Peter Galbavy
     36  * All rights reserved.
     37  *
     38  * Redistribution and use in source and binary forms, with or without
     39  * modification, are permitted provided that the following conditions
     40  * are met:
     41  * 1. Redistributions of source code must retain the above copyright
     42  *    notice, this list of conditions and the following disclaimer.
     43  * 2. Redistributions in binary form must reproduce the above copyright
     44  *    notice, this list of conditions and the following disclaimer in the
     45  *    documentation and/or other materials provided with the distribution.
     46  * 3. All advertising materials mentioning features or use of this software
     47  *    must display the following acknowledgement:
     48  *	This product includes software developed by Peter Galbavy
     49  * 4. The name of the author may not be used to endorse or promote products
     50  *    derived from this software without specific prior written permission.
     51  *
     52  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     54  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     55  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     56  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     57  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     58  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     60  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     61  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62  * POSSIBILITY OF SUCH DAMAGE.
     63  */
     64 
     65 /*
     66  * Based on aic6360 by Jarle Greipsland
     67  *
     68  * Acknowledgements: Many of the algorithms used in this driver are
     69  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     70  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     71  */
     72 
     73 /*
     74  * Initial m68k mac support from Allen Briggs <briggs (at) macbsd.com>
     75  * (basically consisting of the match, a bit of the attach, and the
     76  *  "DMA" glue functions).
     77  */
     78 
     79 #include <sys/types.h>
     80 #include <sys/param.h>
     81 #include <sys/systm.h>
     82 #include <sys/kernel.h>
     83 #include <sys/errno.h>
     84 #include <sys/ioctl.h>
     85 #include <sys/device.h>
     86 #include <sys/buf.h>
     87 #include <sys/proc.h>
     88 #include <sys/user.h>
     89 #include <sys/queue.h>
     90 
     91 #include <dev/scsipi/scsi_all.h>
     92 #include <dev/scsipi/scsipi_all.h>
     93 #include <dev/scsipi/scsiconf.h>
     94 #include <dev/scsipi/scsi_message.h>
     95 
     96 #include <machine/cpu.h>
     97 #include <machine/bus.h>
     98 #include <machine/param.h>
     99 
    100 #include <dev/ic/ncr53c9xreg.h>
    101 #include <dev/ic/ncr53c9xvar.h>
    102 
    103 #include <machine/viareg.h>
    104 
    105 #include <mac68k/obio/espvar.h>
    106 #include <mac68k/obio/obiovar.h>
    107 
    108 void	espattach	__P((struct device *, struct device *, void *));
    109 int	espmatch	__P((struct device *, struct cfdata *, void *));
    110 
    111 /* Linkup to the rest of the kernel */
    112 struct cfattach esp_ca = {
    113 	sizeof(struct esp_softc), espmatch, espattach
    114 };
    115 
    116 struct scsipi_device esp_dev = {
    117 	NULL,			/* Use default error handler */
    118 	NULL,			/* have a queue, served by this */
    119 	NULL,			/* have no async handler */
    120 	NULL,			/* Use default 'done' routine */
    121 };
    122 
    123 /*
    124  * Functions and the switch for the MI code.
    125  */
    126 u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    127 void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    128 int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    129 void	esp_dma_reset __P((struct ncr53c9x_softc *));
    130 int	esp_dma_intr __P((struct ncr53c9x_softc *));
    131 int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    132 	    size_t *, int, size_t *));
    133 void	esp_dma_go __P((struct ncr53c9x_softc *));
    134 void	esp_dma_stop __P((struct ncr53c9x_softc *));
    135 int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    136 void	esp_quick_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    137 int	esp_quick_dma_intr __P((struct ncr53c9x_softc *));
    138 int	esp_quick_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    139 	    size_t *, int, size_t *));
    140 void	esp_quick_dma_go __P((struct ncr53c9x_softc *));
    141 
    142 void	esp_intr __P((void *sc));
    143 void	esp_dualbus_intr __P((void *sc));
    144 static struct esp_softc		*esp0 = NULL, *esp1 = NULL;
    145 
    146 static __inline__ int esp_dafb_have_dreq __P((struct esp_softc *esc));
    147 static __inline__ int esp_iosb_have_dreq __P((struct esp_softc *esc));
    148 int (*esp_have_dreq) __P((struct esp_softc *esc));
    149 
    150 struct ncr53c9x_glue esp_glue = {
    151 	esp_read_reg,
    152 	esp_write_reg,
    153 	esp_dma_isintr,
    154 	esp_dma_reset,
    155 	esp_dma_intr,
    156 	esp_dma_setup,
    157 	esp_dma_go,
    158 	esp_dma_stop,
    159 	esp_dma_isactive,
    160 	NULL,			/* gl_clear_latched_intr */
    161 };
    162 
    163 int
    164 espmatch(parent, cf, aux)
    165 	struct device *parent;
    166 	struct cfdata *cf;
    167 	void *aux;
    168 {
    169 	int	found = 0;
    170 
    171 	if ((cf->cf_unit == 0) && mac68k_machine.scsi96) {
    172 		found = 1;
    173 	}
    174 	if ((cf->cf_unit == 1) && mac68k_machine.scsi96_2) {
    175 		found = 1;
    176 	}
    177 
    178 	return found;
    179 }
    180 
    181 /*
    182  * Attach this instance, and then all the sub-devices
    183  */
    184 void
    185 espattach(parent, self, aux)
    186 	struct device *parent, *self;
    187 	void *aux;
    188 {
    189 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    190 	extern vaddr_t		SCSIBase;
    191 	struct esp_softc	*esc = (void *)self;
    192 	struct ncr53c9x_softc	*sc = &esc->sc_ncr53c9x;
    193 	int			quick = 0;
    194 	unsigned long		reg_offset;
    195 
    196 	reg_offset = SCSIBase - IOBase;
    197 	esc->sc_tag = oa->oa_tag;
    198 	/*
    199 	 * For Wombat, Primus and Optimus motherboards, DREQ is
    200 	 * visible on bit 0 of the IOSB's emulated VIA2 vIFR (and
    201 	 * the scsi registers are offset 0x1000 bytes from IOBase).
    202 	 *
    203 	 * For the Q700/900/950 it's at f9800024 for bus 0 and
    204 	 * f9800028 for bus 1 (900/950).  For these machines, that is also
    205 	 * a (12-bit) configuration register for DAFB's control of the
    206 	 * pseudo-DMA timing.  The default value is 0x1d1.
    207 	 */
    208 	esp_have_dreq = esp_dafb_have_dreq;
    209 	if (sc->sc_dev.dv_unit == 0) {
    210 		if (reg_offset == 0x10000) {
    211 			quick = 1;
    212 			esp_have_dreq = esp_iosb_have_dreq;
    213 		} else if (reg_offset == 0x18000) {
    214 			quick = 0;
    215 		} else {
    216 			if (bus_space_map(esc->sc_tag, 0xf9800024,
    217 					  4, 0, &esc->sc_bsh)) {
    218 				printf("failed to map 4 at 0xf9800024.\n");
    219 			} else {
    220 				quick = 1;
    221 				bus_space_write_4(esc->sc_tag,
    222 						  esc->sc_bsh, 0, 0x1d1);
    223 			}
    224 		}
    225 	} else {
    226 		if (bus_space_map(esc->sc_tag, 0xf9800028,
    227 				  4, 0, &esc->sc_bsh)) {
    228 			printf("failed to map 4 at 0xf9800028.\n");
    229 		} else {
    230 			quick = 1;
    231 			bus_space_write_4(esc->sc_tag, esc->sc_bsh, 0, 0x1d1);
    232 		}
    233 	}
    234 	if (quick) {
    235 		esp_glue.gl_write_reg = esp_quick_write_reg;
    236 		esp_glue.gl_dma_intr = esp_quick_dma_intr;
    237 		esp_glue.gl_dma_setup = esp_quick_dma_setup;
    238 		esp_glue.gl_dma_go = esp_quick_dma_go;
    239 	}
    240 
    241 	/*
    242 	 * Set up the glue for MI code early; we use some of it here.
    243 	 */
    244 	sc->sc_glue = &esp_glue;
    245 
    246 	/*
    247 	 * Save the regs
    248 	 */
    249 	if (sc->sc_dev.dv_unit == 0) {
    250 		esp0 = esc;
    251 
    252 		esc->sc_reg = (volatile u_char *) SCSIBase;
    253 		via2_register_irq(VIA2_SCSIIRQ, esp_intr, esc);
    254 		esc->irq_mask = V2IF_SCSIIRQ;
    255 		if (reg_offset == 0x10000) {
    256 			sc->sc_freq = 16500000;
    257 		} else {
    258 			sc->sc_freq = 25000000;
    259 		}
    260 
    261 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    262 			printf(" (quick)");
    263 		}
    264 	} else {
    265 		esp1 = esc;
    266 
    267 		esc->sc_reg = (volatile u_char *) SCSIBase + 0x402;
    268 		via2_register_irq(VIA2_SCSIIRQ, esp_dualbus_intr, NULL);
    269 		esc->irq_mask = 0;
    270 		sc->sc_freq = 25000000;
    271 
    272 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    273 			printf(" (quick)");
    274 		}
    275 	}
    276 
    277 	printf(": address %p", esc->sc_reg);
    278 
    279 	sc->sc_id = 7;
    280 
    281 	/* gimme Mhz */
    282 	sc->sc_freq /= 1000000;
    283 
    284 	/*
    285 	 * It is necessary to try to load the 2nd config register here,
    286 	 * to find out what rev the esp chip is, else the esp_reset
    287 	 * will not set up the defaults correctly.
    288 	 */
    289 	sc->sc_cfg1 = sc->sc_id; /* | NCRCFG1_PARENB; */
    290 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    291 	sc->sc_cfg3 = 0;
    292 	sc->sc_rev = NCR_VARIANT_NCR53C96;
    293 
    294 	/*
    295 	 * This is the value used to start sync negotiations
    296 	 * Note that the NCR register "SYNCTP" is programmed
    297 	 * in "clocks per byte", and has a minimum value of 4.
    298 	 * The SCSI period used in negotiation is one-fourth
    299 	 * of the time (in nanoseconds) needed to transfer one byte.
    300 	 * Since the chip's clock is given in MHz, we have the following
    301 	 * formula: 4 * period = (1000 / freq) * 4
    302 	 */
    303 	sc->sc_minsync = 1000 / sc->sc_freq;
    304 
    305 	sc->sc_minsync = 0;	/* No synchronous xfers w/o DMA */
    306 	/* Really no limit, but since we want to fit into the TCR... */
    307 	sc->sc_maxxfer = 8 * 1024; /*64 * 1024; XXX */
    308 
    309 	/*
    310 	 * Now try to attach all the sub-devices
    311 	 */
    312 	sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
    313 	sc->sc_adapter.scsipi_minphys = minphys;
    314 	ncr53c9x_attach(sc, &esp_dev);
    315 
    316 	/*
    317 	 * Configure interrupts.
    318 	 */
    319 	if (esc->irq_mask) {
    320 		via2_reg(vPCR) = 0x22;
    321 		via2_reg(vIFR) = esc->irq_mask;
    322 		via2_reg(vIER) = 0x80 | esc->irq_mask;
    323 	}
    324 }
    325 
    326 /*
    327  * Glue functions.
    328  */
    329 
    330 u_char
    331 esp_read_reg(sc, reg)
    332 	struct ncr53c9x_softc *sc;
    333 	int reg;
    334 {
    335 	struct esp_softc *esc = (struct esp_softc *)sc;
    336 
    337 	return esc->sc_reg[reg * 16];
    338 }
    339 
    340 void
    341 esp_write_reg(sc, reg, val)
    342 	struct ncr53c9x_softc *sc;
    343 	int reg;
    344 	u_char val;
    345 {
    346 	struct esp_softc *esc = (struct esp_softc *)sc;
    347 	u_char	v = val;
    348 
    349 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA)) {
    350 		v = NCRCMD_TRANS;
    351 	}
    352 	esc->sc_reg[reg * 16] = v;
    353 }
    354 
    355 void
    356 esp_dma_stop(sc)
    357 	struct ncr53c9x_softc *sc;
    358 {
    359 }
    360 
    361 int
    362 esp_dma_isactive(sc)
    363 	struct ncr53c9x_softc *sc;
    364 {
    365 	struct esp_softc *esc = (struct esp_softc *)sc;
    366 
    367 	return esc->sc_active;
    368 }
    369 
    370 int
    371 esp_dma_isintr(sc)
    372 	struct ncr53c9x_softc *sc;
    373 {
    374 	struct esp_softc *esc = (struct esp_softc *)sc;
    375 
    376 	return esc->sc_reg[NCR_STAT * 16] & 0x80;
    377 }
    378 
    379 void
    380 esp_dma_reset(sc)
    381 	struct ncr53c9x_softc *sc;
    382 {
    383 	struct esp_softc *esc = (struct esp_softc *)sc;
    384 
    385 	esc->sc_active = 0;
    386 	esc->sc_tc = 0;
    387 }
    388 
    389 int
    390 esp_dma_intr(sc)
    391 	struct ncr53c9x_softc *sc;
    392 {
    393 	struct esp_softc *esc = (struct esp_softc *)sc;
    394 	volatile u_char *cmdreg, *intrreg, *statreg, *fiforeg;
    395 	u_char	*p;
    396 	u_int	espphase, espstat, espintr;
    397 	int	cnt, s;
    398 
    399 	if (esc->sc_active == 0) {
    400 		printf("dma_intr--inactive DMA\n");
    401 		return -1;
    402 	}
    403 
    404 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    405 		esc->sc_active = 0;
    406 		return 0;
    407 	}
    408 
    409 	cnt = *esc->sc_dmalen;
    410 	if (*esc->sc_dmalen == 0) {
    411 		printf("data interrupt, but no count left.");
    412 	}
    413 
    414 	p = *esc->sc_dmaaddr;
    415 	espphase = sc->sc_phase;
    416 	espstat = (u_int) sc->sc_espstat;
    417 	espintr = (u_int) sc->sc_espintr;
    418 	cmdreg = esc->sc_reg + NCR_CMD * 16;
    419 	fiforeg = esc->sc_reg + NCR_FIFO * 16;
    420 	statreg = esc->sc_reg + NCR_STAT * 16;
    421 	intrreg = esc->sc_reg + NCR_INTR * 16;
    422 	do {
    423 		if (esc->sc_datain) {
    424 			*p++ = *fiforeg;
    425 			cnt--;
    426 			if (espphase == DATA_IN_PHASE) {
    427 				*cmdreg = NCRCMD_TRANS;
    428 			} else {
    429 				esc->sc_active = 0;
    430 			}
    431 	 	} else {
    432 			if (   (espphase == DATA_OUT_PHASE)
    433 			    || (espphase == MESSAGE_OUT_PHASE)) {
    434 				*fiforeg = *p++;
    435 				cnt--;
    436 				*cmdreg = NCRCMD_TRANS;
    437 			} else {
    438 				esc->sc_active = 0;
    439 			}
    440 		}
    441 
    442 		if (esc->sc_active) {
    443 			while (!(*statreg & 0x80));
    444 			s = splhigh();
    445 			espstat = *statreg;
    446 			espintr = *intrreg;
    447 			espphase = (espintr & NCRINTR_DIS)
    448 				    ? /* Disconnected */ BUSFREE_PHASE
    449 				    : espstat & PHASE_MASK;
    450 			splx(s);
    451 		}
    452 	} while (esc->sc_active && (espintr & NCRINTR_BS));
    453 	sc->sc_phase = espphase;
    454 	sc->sc_espstat = (u_char) espstat;
    455 	sc->sc_espintr = (u_char) espintr;
    456 	*esc->sc_dmaaddr = p;
    457 	*esc->sc_dmalen = cnt;
    458 
    459 	if (*esc->sc_dmalen == 0) {
    460 		esc->sc_tc = NCRSTAT_TC;
    461 	}
    462 	sc->sc_espstat |= esc->sc_tc;
    463 	return 0;
    464 }
    465 
    466 int
    467 esp_dma_setup(sc, addr, len, datain, dmasize)
    468 	struct ncr53c9x_softc *sc;
    469 	caddr_t *addr;
    470 	size_t *len;
    471 	int datain;
    472 	size_t *dmasize;
    473 {
    474 	struct esp_softc *esc = (struct esp_softc *)sc;
    475 
    476 	esc->sc_dmaaddr = addr;
    477 	esc->sc_dmalen = len;
    478 	esc->sc_datain = datain;
    479 	esc->sc_dmasize = *dmasize;
    480 	esc->sc_tc = 0;
    481 
    482 	return 0;
    483 }
    484 
    485 void
    486 esp_dma_go(sc)
    487 	struct ncr53c9x_softc *sc;
    488 {
    489 	struct esp_softc *esc = (struct esp_softc *)sc;
    490 
    491 	if (esc->sc_datain == 0) {
    492 		esc->sc_reg[NCR_FIFO * 16] = **esc->sc_dmaaddr;
    493 		(*esc->sc_dmalen)--;
    494 		(*esc->sc_dmaaddr)++;
    495 	}
    496 	esc->sc_active = 1;
    497 }
    498 
    499 void
    500 esp_quick_write_reg(sc, reg, val)
    501 	struct ncr53c9x_softc *sc;
    502 	int reg;
    503 	u_char val;
    504 {
    505 	struct esp_softc *esc = (struct esp_softc *)sc;
    506 
    507 	esc->sc_reg[reg * 16] = val;
    508 }
    509 
    510 int
    511 esp_quick_dma_intr(sc)
    512 	struct ncr53c9x_softc *sc;
    513 {
    514 	struct esp_softc *esc = (struct esp_softc *)sc;
    515 	int trans=0, resid=0;
    516 
    517 	if (esc->sc_active == 0)
    518 		panic("dma_intr--inactive DMA\n");
    519 
    520 	esc->sc_active = 0;
    521 
    522 	if (esc->sc_dmasize == 0) {
    523 		int	res;
    524 
    525 		res = 65536;
    526 		res -= NCR_READ_REG(sc, NCR_TCL);
    527 		res -= NCR_READ_REG(sc, NCR_TCM) << 8;
    528 		printf("dmaintr: discarded %d b (last transfer was %d b).\n",
    529 			res, esc->sc_prevdmasize);
    530 		return 0;
    531 	}
    532 
    533 	if (esc->sc_datain &&
    534 	    (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    535 		printf("dmaintr: empty FIFO of %d\n", resid);
    536 		DELAY(1);
    537 	}
    538 
    539 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    540 		resid += NCR_READ_REG(sc, NCR_TCL);
    541 		resid += NCR_READ_REG(sc, NCR_TCM) << 8;
    542 
    543 		if (resid == 0)
    544 			resid = 65536;
    545 	}
    546 
    547 	trans = esc->sc_dmasize - resid;
    548 	if (trans < 0) {
    549 		printf("dmaintr: trans < 0????");
    550 		trans = esc->sc_dmasize;
    551 	}
    552 
    553 	NCR_DMA(("dmaintr: trans %d, resid %d.\n", trans, resid));
    554 	*esc->sc_dmaaddr += trans;
    555 	*esc->sc_dmalen -= trans;
    556 
    557 	return 0;
    558 }
    559 
    560 int
    561 esp_quick_dma_setup(sc, addr, len, datain, dmasize)
    562 	struct ncr53c9x_softc *sc;
    563 	caddr_t *addr;
    564 	size_t *len;
    565 	int datain;
    566 	size_t *dmasize;
    567 {
    568 	struct esp_softc *esc = (struct esp_softc *)sc;
    569 
    570 	esc->sc_dmaaddr = addr;
    571 	esc->sc_dmalen = len;
    572 
    573 	esc->sc_pdmaddr = (u_int16_t *) *addr;
    574 	esc->sc_pdmalen = *len;
    575 	if (esc->sc_pdmalen & 1) {
    576 		esc->sc_pdmalen--;
    577 		esc->sc_pad = 1;
    578 	} else {
    579 		esc->sc_pad = 0;
    580 	}
    581 
    582 	esc->sc_datain = datain;
    583 	esc->sc_prevdmasize = esc->sc_dmasize;
    584 	esc->sc_dmasize = *dmasize;
    585 
    586 	return 0;
    587 }
    588 
    589 static __inline__ int
    590 esp_dafb_have_dreq(esc)
    591 	struct esp_softc *esc;
    592 {
    593 	u_int32_t r;
    594 
    595 	r = bus_space_read_4(esc->sc_tag, esc->sc_bsh, 0);
    596 	return (r & 0x200);
    597 }
    598 
    599 static __inline__ int
    600 esp_iosb_have_dreq(esc)
    601 	struct esp_softc *esc;
    602 {
    603 	return (via2_reg(vIFR) & V2IF_SCSIDRQ);
    604 }
    605 
    606 static int espspl=-1;
    607 #define __splx(s) __asm __volatile ("movew %0,sr" : : "di" (s));
    608 #define __spl2()  __splx(PSL_S|PSL_IPL2)
    609 #define __spl6()  __splx(PSL_S|PSL_IPL6)
    610 
    611 void
    612 esp_quick_dma_go(sc)
    613 	struct ncr53c9x_softc *sc;
    614 {
    615 	struct esp_softc *esc = (struct esp_softc *)sc;
    616 	extern int *nofault;
    617 	label_t faultbuf;
    618 	u_int16_t volatile *pdma;
    619 	u_char volatile *statreg;
    620 
    621 	esc->sc_active = 1;
    622 
    623 	espspl = spl2();
    624 
    625 restart_dmago:
    626 	nofault = (int *) &faultbuf;
    627 	if (setjmp((label_t *) nofault)) {
    628 		int	i=0;
    629 
    630 		nofault = (int *) 0;
    631 		statreg = esc->sc_reg + NCR_STAT * 16;
    632 		for (;;) {
    633 			if (*statreg & 0x80) {
    634 				goto gotintr;
    635 			}
    636 
    637 			if (esp_have_dreq(esc)) {
    638 				break;
    639 			}
    640 
    641 			DELAY(1);
    642 			if (i++ > 10000)
    643 				panic("esp_dma_go: Argh!");
    644 		}
    645 		goto restart_dmago;
    646 	}
    647 
    648 	statreg = esc->sc_reg + NCR_STAT * 16;
    649 	pdma = (u_int16_t *) (esc->sc_reg + 0x100);
    650 
    651 #define WAIT while (!esp_have_dreq(esc)) if (*statreg & 0x80) goto gotintr
    652 
    653 	if (esc->sc_datain == 0) {
    654 		while (esc->sc_pdmalen) {
    655 			WAIT;
    656 			__spl6(); *pdma = *(esc->sc_pdmaddr)++; __spl2()
    657 			esc->sc_pdmalen -= 2;
    658 		}
    659 		if (esc->sc_pad) {
    660 			unsigned short	us;
    661 			unsigned char	*c;
    662 			c = (unsigned char *) esc->sc_pdmaddr;
    663 			us = *c;
    664 			WAIT;
    665 			__spl6(); *pdma = us; __spl2()
    666 		}
    667 	} else {
    668 		while (esc->sc_pdmalen) {
    669 			WAIT;
    670 			__spl6(); *(esc->sc_pdmaddr)++ = *pdma; __spl2()
    671 			esc->sc_pdmalen -= 2;
    672 		}
    673 		if (esc->sc_pad) {
    674 			unsigned short	us;
    675 			unsigned char	*c;
    676 			WAIT;
    677 			__spl6(); us = *pdma; __spl2()
    678 			c = (unsigned char *) esc->sc_pdmaddr;
    679 			*c = us & 0xff;
    680 		}
    681 	}
    682 #undef WAIT
    683 
    684 	nofault = (int *) 0;
    685 
    686 	if ((*statreg & 0x80) == 0) {
    687 		if (espspl != -1) splx(espspl); espspl = -1;
    688 		return;
    689 	}
    690 
    691 gotintr:
    692 	ncr53c9x_intr(sc);
    693 	if (espspl != -1) splx(espspl); espspl = -1;
    694 }
    695 
    696 void
    697 esp_intr(sc)
    698 	void *sc;
    699 {
    700 	struct esp_softc *esc = (struct esp_softc *)sc;
    701 	int	i = 0;
    702 
    703 	do {
    704 		if (esc->sc_reg[NCR_STAT * 16] & 0x80) {
    705 			ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
    706 			i++;
    707 		}
    708 
    709 		if (!i) {
    710 			delay(10000);
    711 		}
    712 	} while (!i++);
    713 }
    714 
    715 void
    716 esp_dualbus_intr(sc)
    717 	void *sc;
    718 {
    719 	int	i = 0;
    720 
    721 	do {
    722 		if (esp0 && (esp0->sc_reg[NCR_STAT * 16] & 0x80)) {
    723 			ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
    724 			i++;
    725 		}
    726 
    727 		if (esp1 && (esp1->sc_reg[NCR_STAT * 16] & 0x80)) {
    728 			ncr53c9x_intr((struct ncr53c9x_softc *) esp1);
    729 			i++;
    730 		}
    731 
    732 		if (!i) {
    733 			delay(10000);
    734 		}
    735 	} while (!i++);
    736 }
    737