Home | History | Annotate | Line # | Download | only in obio
esp.c revision 1.22
      1 /*	$NetBSD: esp.c,v 1.22 1999/06/09 03:41:34 briggs 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 int	esp_dualbus_intr __P((register struct ncr53c9x_softc *sc));
    143 static struct esp_softc		*esp0 = NULL, *esp1 = NULL;
    144 
    145 static __inline__ int esp_dafb_have_dreq __P((struct esp_softc *esc));
    146 static __inline__ int esp_iosb_have_dreq __P((struct esp_softc *esc));
    147 int (*esp_have_dreq) __P((struct esp_softc *esc));
    148 
    149 struct ncr53c9x_glue esp_glue = {
    150 	esp_read_reg,
    151 	esp_write_reg,
    152 	esp_dma_isintr,
    153 	esp_dma_reset,
    154 	esp_dma_intr,
    155 	esp_dma_setup,
    156 	esp_dma_go,
    157 	esp_dma_stop,
    158 	esp_dma_isactive,
    159 	NULL,			/* gl_clear_latched_intr */
    160 };
    161 
    162 int
    163 espmatch(parent, cf, aux)
    164 	struct device *parent;
    165 	struct cfdata *cf;
    166 	void *aux;
    167 {
    168 	int	found = 0;
    169 
    170 	if ((cf->cf_unit == 0) && mac68k_machine.scsi96) {
    171 		found = 1;
    172 	}
    173 	if ((cf->cf_unit == 1) && mac68k_machine.scsi96_2) {
    174 		found = 1;
    175 	}
    176 
    177 	return found;
    178 }
    179 
    180 /*
    181  * Attach this instance, and then all the sub-devices
    182  */
    183 void
    184 espattach(parent, self, aux)
    185 	struct device *parent, *self;
    186 	void *aux;
    187 {
    188 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    189 	extern vaddr_t		SCSIBase;
    190 	struct esp_softc	*esc = (void *)self;
    191 	struct ncr53c9x_softc	*sc = &esc->sc_ncr53c9x;
    192 	int			quick = 0;
    193 	unsigned long		reg_offset;
    194 
    195 	reg_offset = SCSIBase - IOBase;
    196 	esc->sc_tag = oa->oa_tag;
    197 	/*
    198 	 * For Wombat, Primus and Optimus motherboards, DREQ is
    199 	 * visible on bit 0 of the IOSB's emulated VIA2 vIFR (and
    200 	 * the scsi registers are offset 0x1000 bytes from IOBase).
    201 	 *
    202 	 * For the Q700/900/950 it's at f9800024 for bus 0 and
    203 	 * f9800028 for bus 1 (900/950).  For these machines, that is also
    204 	 * a (12-bit) configuration register for DAFB's control of the
    205 	 * pseudo-DMA timing.  The default value is 0x1d1.
    206 	 */
    207 	esp_have_dreq = esp_dafb_have_dreq;
    208 	if (sc->sc_dev.dv_unit == 0) {
    209 		if (reg_offset == 0x10000) {
    210 			quick = 1;
    211 			esp_have_dreq = esp_iosb_have_dreq;
    212 		} else if (reg_offset == 0x18000) {
    213 			quick = 0;
    214 		} else {
    215 			if (bus_space_map(esc->sc_tag, 0xf9800024,
    216 					  4, 0, &esc->sc_bsh)) {
    217 				printf("failed to map 4 at 0xf9800024.\n");
    218 			} else {
    219 				quick = 1;
    220 				bus_space_write_4(esc->sc_tag,
    221 						  esc->sc_bsh, 0, 0x1d1);
    222 			}
    223 		}
    224 	} else {
    225 		if (bus_space_map(esc->sc_tag, 0xf9800028,
    226 				  4, 0, &esc->sc_bsh)) {
    227 			printf("failed to map 4 at 0xf9800028.\n");
    228 		} else {
    229 			quick = 1;
    230 			bus_space_write_4(esc->sc_tag, esc->sc_bsh, 0, 0x1d1);
    231 		}
    232 	}
    233 	if (quick) {
    234 		esp_glue.gl_write_reg = esp_quick_write_reg;
    235 		esp_glue.gl_dma_intr = esp_quick_dma_intr;
    236 		esp_glue.gl_dma_setup = esp_quick_dma_setup;
    237 		esp_glue.gl_dma_go = esp_quick_dma_go;
    238 	}
    239 
    240 	/*
    241 	 * Set up the glue for MI code early; we use some of it here.
    242 	 */
    243 	sc->sc_glue = &esp_glue;
    244 
    245 	/*
    246 	 * Save the regs
    247 	 */
    248 	if (sc->sc_dev.dv_unit == 0) {
    249 		esp0 = esc;
    250 
    251 		esc->sc_reg = (volatile u_char *) SCSIBase;
    252 		via2_register_irq(VIA2_SCSIIRQ,
    253 		    (void (*)(void *))ncr53c9x_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,
    269 		    (void (*)(void *))esp_dualbus_intr, NULL);
    270 		esc->irq_mask = 0;
    271 		sc->sc_freq = 25000000;
    272 
    273 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    274 			printf(" (quick)");
    275 		}
    276 	}
    277 
    278 	printf(": address %p", esc->sc_reg);
    279 
    280 	sc->sc_id = 7;
    281 
    282 	/* gimme Mhz */
    283 	sc->sc_freq /= 1000000;
    284 
    285 	/*
    286 	 * It is necessary to try to load the 2nd config register here,
    287 	 * to find out what rev the esp chip is, else the esp_reset
    288 	 * will not set up the defaults correctly.
    289 	 */
    290 	sc->sc_cfg1 = sc->sc_id; /* | NCRCFG1_PARENB; */
    291 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    292 	sc->sc_cfg3 = 0;
    293 	sc->sc_rev = NCR_VARIANT_NCR53C96;
    294 
    295 	/*
    296 	 * This is the value used to start sync negotiations
    297 	 * Note that the NCR register "SYNCTP" is programmed
    298 	 * in "clocks per byte", and has a minimum value of 4.
    299 	 * The SCSI period used in negotiation is one-fourth
    300 	 * of the time (in nanoseconds) needed to transfer one byte.
    301 	 * Since the chip's clock is given in MHz, we have the following
    302 	 * formula: 4 * period = (1000 / freq) * 4
    303 	 */
    304 	sc->sc_minsync = 1000 / sc->sc_freq;
    305 
    306 	sc->sc_minsync = 0;	/* No synchronous xfers w/o DMA */
    307 	/* Really no limit, but since we want to fit into the TCR... */
    308 	sc->sc_maxxfer = 8 * 1024; /*64 * 1024; XXX */
    309 
    310 	/*
    311 	 * Now try to attach all the sub-devices
    312 	 */
    313 	sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
    314 	sc->sc_adapter.scsipi_minphys = minphys;
    315 	ncr53c9x_attach(sc, &esp_dev);
    316 
    317 	/*
    318 	 * Configure interrupts.
    319 	 */
    320 	if (esc->irq_mask) {
    321 		via2_reg(vPCR) = 0x22;
    322 		via2_reg(vIFR) = esc->irq_mask;
    323 		via2_reg(vIER) = 0x80 | esc->irq_mask;
    324 	}
    325 }
    326 
    327 /*
    328  * Glue functions.
    329  */
    330 
    331 u_char
    332 esp_read_reg(sc, reg)
    333 	struct ncr53c9x_softc *sc;
    334 	int reg;
    335 {
    336 	struct esp_softc *esc = (struct esp_softc *)sc;
    337 	u_char	v;
    338 	int	s;
    339 
    340 	s = splhigh();
    341 	v = esc->sc_reg[reg * 16];
    342 	splx(s);
    343 	return v;
    344 }
    345 
    346 void
    347 esp_write_reg(sc, reg, val)
    348 	struct ncr53c9x_softc *sc;
    349 	int reg;
    350 	u_char val;
    351 {
    352 	struct esp_softc *esc = (struct esp_softc *)sc;
    353 	u_char	v = val;
    354 	int	s;
    355 
    356 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA)) {
    357 		v = NCRCMD_TRANS;
    358 	}
    359 	s = splhigh();
    360 	esc->sc_reg[reg * 16] = v;
    361 	splx(s);
    362 }
    363 
    364 void
    365 esp_dma_stop(sc)
    366 	struct ncr53c9x_softc *sc;
    367 {
    368 }
    369 
    370 int
    371 esp_dma_isactive(sc)
    372 	struct ncr53c9x_softc *sc;
    373 {
    374 	struct esp_softc *esc = (struct esp_softc *)sc;
    375 
    376 	return esc->sc_active;
    377 }
    378 
    379 int
    380 esp_dma_isintr(sc)
    381 	struct ncr53c9x_softc *sc;
    382 {
    383 	struct esp_softc *esc = (struct esp_softc *)sc;
    384 
    385 	return esc->sc_reg[NCR_STAT * 16] & 0x80;
    386 }
    387 
    388 void
    389 esp_dma_reset(sc)
    390 	struct ncr53c9x_softc *sc;
    391 {
    392 	struct esp_softc *esc = (struct esp_softc *)sc;
    393 
    394 	esc->sc_active = 0;
    395 	esc->sc_tc = 0;
    396 }
    397 
    398 int
    399 esp_dma_intr(sc)
    400 	struct ncr53c9x_softc *sc;
    401 {
    402 	struct esp_softc *esc = (struct esp_softc *)sc;
    403 	volatile u_char *cmdreg, *intrreg, *statreg, *fiforeg;
    404 	u_char	*p;
    405 	u_int	espphase, espstat, espintr;
    406 	int	cnt, s;
    407 
    408 	if (esc->sc_active == 0) {
    409 		printf("dma_intr--inactive DMA\n");
    410 		return -1;
    411 	}
    412 
    413 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    414 		esc->sc_active = 0;
    415 		return 0;
    416 	}
    417 
    418 	cnt = *esc->sc_dmalen;
    419 	if (*esc->sc_dmalen == 0) {
    420 		printf("data interrupt, but no count left.");
    421 	}
    422 
    423 	p = *esc->sc_dmaaddr;
    424 	espphase = sc->sc_phase;
    425 	espstat = (u_int) sc->sc_espstat;
    426 	espintr = (u_int) sc->sc_espintr;
    427 	cmdreg = esc->sc_reg + NCR_CMD * 16;
    428 	fiforeg = esc->sc_reg + NCR_FIFO * 16;
    429 	statreg = esc->sc_reg + NCR_STAT * 16;
    430 	intrreg = esc->sc_reg + NCR_INTR * 16;
    431 	do {
    432 		if (esc->sc_datain) {
    433 			*p++ = *fiforeg;
    434 			cnt--;
    435 			if (espphase == DATA_IN_PHASE) {
    436 				*cmdreg = NCRCMD_TRANS;
    437 			} else {
    438 				esc->sc_active = 0;
    439 			}
    440 	 	} else {
    441 			if (   (espphase == DATA_OUT_PHASE)
    442 			    || (espphase == MESSAGE_OUT_PHASE)) {
    443 				*fiforeg = *p++;
    444 				cnt--;
    445 				*cmdreg = NCRCMD_TRANS;
    446 			} else {
    447 				esc->sc_active = 0;
    448 			}
    449 		}
    450 
    451 		if (esc->sc_active) {
    452 			while (!(*statreg & 0x80));
    453 			s = splhigh();
    454 			espstat = *statreg;
    455 			espintr = *intrreg;
    456 			espphase = (espintr & NCRINTR_DIS)
    457 				    ? /* Disconnected */ BUSFREE_PHASE
    458 				    : espstat & PHASE_MASK;
    459 			splx(s);
    460 		}
    461 	} while (esc->sc_active && (espintr & NCRINTR_BS));
    462 	sc->sc_phase = espphase;
    463 	sc->sc_espstat = (u_char) espstat;
    464 	sc->sc_espintr = (u_char) espintr;
    465 	*esc->sc_dmaaddr = p;
    466 	*esc->sc_dmalen = cnt;
    467 
    468 	if (*esc->sc_dmalen == 0) {
    469 		esc->sc_tc = NCRSTAT_TC;
    470 	}
    471 	sc->sc_espstat |= esc->sc_tc;
    472 	return 0;
    473 }
    474 
    475 int
    476 esp_dma_setup(sc, addr, len, datain, dmasize)
    477 	struct ncr53c9x_softc *sc;
    478 	caddr_t *addr;
    479 	size_t *len;
    480 	int datain;
    481 	size_t *dmasize;
    482 {
    483 	struct esp_softc *esc = (struct esp_softc *)sc;
    484 
    485 	esc->sc_dmaaddr = addr;
    486 	esc->sc_dmalen = len;
    487 	esc->sc_datain = datain;
    488 	esc->sc_dmasize = *dmasize;
    489 	esc->sc_tc = 0;
    490 
    491 	return 0;
    492 }
    493 
    494 void
    495 esp_dma_go(sc)
    496 	struct ncr53c9x_softc *sc;
    497 {
    498 	struct esp_softc *esc = (struct esp_softc *)sc;
    499 
    500 	if (esc->sc_datain == 0) {
    501 		esc->sc_reg[NCR_FIFO * 16] = **esc->sc_dmaaddr;
    502 		(*esc->sc_dmalen)--;
    503 		(*esc->sc_dmaaddr)++;
    504 	}
    505 	esc->sc_active = 1;
    506 }
    507 
    508 void
    509 esp_quick_write_reg(sc, reg, val)
    510 	struct ncr53c9x_softc *sc;
    511 	int reg;
    512 	u_char val;
    513 {
    514 	struct esp_softc *esc = (struct esp_softc *)sc;
    515 	u_char	v = val;
    516 	int	s;
    517 
    518 	s = splhigh();
    519 	esc->sc_reg[reg * 16] = v;
    520 	splx(s);
    521 }
    522 
    523 int
    524 esp_quick_dma_intr(sc)
    525 	struct ncr53c9x_softc *sc;
    526 {
    527 	struct esp_softc *esc = (struct esp_softc *)sc;
    528 	int trans=0, resid=0;
    529 
    530 	if (esc->sc_active == 0)
    531 		panic("dma_intr--inactive DMA\n");
    532 
    533 	esc->sc_active = 0;
    534 
    535 	if (esc->sc_dmasize == 0) {
    536 		int	res;
    537 
    538 		res = 65536;
    539 		res -= NCR_READ_REG(sc, NCR_TCL);
    540 		res -= NCR_READ_REG(sc, NCR_TCM) << 8;
    541 		printf("dmaintr: discarded %d b (last transfer was %d b).\n",
    542 			res, esc->sc_prevdmasize);
    543 		return 0;
    544 	}
    545 
    546 	if (esc->sc_datain &&
    547 	    (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    548 		printf("dmaintr: empty FIFO of %d\n", resid);
    549 		DELAY(1);
    550 	}
    551 
    552 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    553 		resid += NCR_READ_REG(sc, NCR_TCL);
    554 		resid += NCR_READ_REG(sc, NCR_TCM) << 8;
    555 
    556 		if (resid == 0)
    557 			resid = 65536;
    558 	}
    559 
    560 	trans = esc->sc_dmasize - resid;
    561 	if (trans < 0) {
    562 		printf("dmaintr: trans < 0????");
    563 		trans = esc->sc_dmasize;
    564 	}
    565 
    566 	NCR_DMA(("dmaintr: trans %d, resid %d.\n", trans, resid));
    567 	*esc->sc_dmaaddr += trans;
    568 	*esc->sc_dmalen -= trans;
    569 
    570 	return 0;
    571 }
    572 
    573 int
    574 esp_quick_dma_setup(sc, addr, len, datain, dmasize)
    575 	struct ncr53c9x_softc *sc;
    576 	caddr_t *addr;
    577 	size_t *len;
    578 	int datain;
    579 	size_t *dmasize;
    580 {
    581 	struct esp_softc *esc = (struct esp_softc *)sc;
    582 
    583 	esc->sc_dmaaddr = addr;
    584 	esc->sc_dmalen = len;
    585 
    586 	esc->sc_pdmaddr = (u_int16_t *) *addr;
    587 	esc->sc_pdmalen = *len;
    588 	if (esc->sc_pdmalen & 1) {
    589 		esc->sc_pdmalen--;
    590 		esc->sc_pad = 1;
    591 	} else {
    592 		esc->sc_pad = 0;
    593 	}
    594 
    595 	esc->sc_datain = datain;
    596 	esc->sc_prevdmasize = esc->sc_dmasize;
    597 	esc->sc_dmasize = *dmasize;
    598 
    599 	return 0;
    600 }
    601 
    602 static __inline__ int
    603 esp_dafb_have_dreq(esc)
    604 	struct esp_softc *esc;
    605 {
    606 	u_int32_t r;
    607 
    608 	r = bus_space_read_4(esc->sc_tag, esc->sc_bsh, 0);
    609 	return (r & 0x200);
    610 }
    611 
    612 static __inline__ int
    613 esp_iosb_have_dreq(esc)
    614 	struct esp_softc *esc;
    615 {
    616 	return (via2_reg(vIFR) & V2IF_SCSIDRQ);
    617 }
    618 
    619 static int espspl=-1;
    620 #define __splx(s) __asm __volatile ("movew %0,sr" : : "di" (s));
    621 #define __spl2()  __splx(PSL_S|PSL_IPL2)
    622 #define __spl6()  __splx(PSL_S|PSL_IPL6)
    623 
    624 void
    625 esp_quick_dma_go(sc)
    626 	struct ncr53c9x_softc *sc;
    627 {
    628 	struct esp_softc *esc = (struct esp_softc *)sc;
    629 	extern int *nofault;
    630 	label_t faultbuf;
    631 	u_int16_t volatile *pdma;
    632 	u_char volatile *statreg;
    633 
    634 	esc->sc_active = 1;
    635 
    636 	espspl = spl2();
    637 
    638 restart_dmago:
    639 	nofault = (int *) &faultbuf;
    640 	if (setjmp((label_t *) nofault)) {
    641 		int	i=0;
    642 
    643 		nofault = (int *) 0;
    644 		statreg = esc->sc_reg + NCR_STAT * 16;
    645 		for (;;) {
    646 			if (*statreg & 0x80) {
    647 				goto gotintr;
    648 			}
    649 
    650 			if (esp_have_dreq(esc)) {
    651 				break;
    652 			}
    653 
    654 			DELAY(1);
    655 			if (i++ > 10000)
    656 				panic("esp_dma_go: Argh!");
    657 		}
    658 		goto restart_dmago;
    659 	}
    660 
    661 	statreg = esc->sc_reg + NCR_STAT * 16;
    662 	pdma = (u_int16_t *) (esc->sc_reg + 0x100);
    663 
    664 #define WAIT while (!esp_have_dreq(esc)) if (*statreg & 0x80) goto gotintr
    665 
    666 	if (esc->sc_datain == 0) {
    667 		while (esc->sc_pdmalen) {
    668 			WAIT;
    669 			__spl6(); *pdma = *(esc->sc_pdmaddr)++; __spl2()
    670 			esc->sc_pdmalen -= 2;
    671 		}
    672 		if (esc->sc_pad) {
    673 			unsigned short	us;
    674 			unsigned char	*c;
    675 			c = (unsigned char *) esc->sc_pdmaddr;
    676 			us = *c;
    677 			WAIT;
    678 			__spl6(); *pdma = us; __spl2()
    679 		}
    680 	} else {
    681 		while (esc->sc_pdmalen) {
    682 			WAIT;
    683 			__spl6(); *(esc->sc_pdmaddr)++ = *pdma; __spl2()
    684 			esc->sc_pdmalen -= 2;
    685 		}
    686 		if (esc->sc_pad) {
    687 			unsigned short	us;
    688 			unsigned char	*c;
    689 			WAIT;
    690 			__spl6(); us = *pdma; __spl2()
    691 			c = (unsigned char *) esc->sc_pdmaddr;
    692 			*c = us & 0xff;
    693 		}
    694 	}
    695 #undef WAIT
    696 
    697 	nofault = (int *) 0;
    698 
    699 	if ((*statreg & 0x80) == 0) {
    700 		if (espspl != -1) splx(espspl); espspl = -1;
    701 		return;
    702 	}
    703 
    704 gotintr:
    705 	ncr53c9x_intr(sc);
    706 	if (espspl != -1) splx(espspl); espspl = -1;
    707 }
    708 
    709 int
    710 esp_dualbus_intr(sc)
    711 	register struct ncr53c9x_softc *sc;
    712 {
    713 	int	i = 0;
    714 
    715 	do {
    716 		if (esp0 && (esp0->sc_reg[NCR_STAT * 16] & 0x80)) {
    717 			ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
    718 			i++;
    719 		}
    720 
    721 		if (esp1 && (esp1->sc_reg[NCR_STAT * 16] & 0x80)) {
    722 			ncr53c9x_intr((struct ncr53c9x_softc *) esp1);
    723 			i++;
    724 		}
    725 		if (!i) {
    726 			delay(100); i++;
    727 		}
    728 	} while (!i);
    729 
    730 	return 0;
    731 }
    732