Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.11
      1 /*	$NetBSD: esp.c,v 1.11 1998/12/26 06:17:44 dbj Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9  * Simulation Facility, NASA Ames Research Center.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * Copyright (c) 1994 Peter Galbavy
     42  * All rights reserved.
     43  *
     44  * Redistribution and use in source and binary forms, with or without
     45  * modification, are permitted provided that the following conditions
     46  * are met:
     47  * 1. Redistributions of source code must retain the above copyright
     48  *    notice, this list of conditions and the following disclaimer.
     49  * 2. Redistributions in binary form must reproduce the above copyright
     50  *    notice, this list of conditions and the following disclaimer in the
     51  *    documentation and/or other materials provided with the distribution.
     52  * 3. All advertising materials mentioning features or use of this software
     53  *    must display the following acknowledgement:
     54  *	This product includes software developed by Peter Galbavy
     55  * 4. The name of the author may not be used to endorse or promote products
     56  *    derived from this software without specific prior written permission.
     57  *
     58  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     60  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     61  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     62  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     63  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     64  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     66  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     67  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68  * POSSIBILITY OF SUCH DAMAGE.
     69  */
     70 
     71 /*
     72  * Based on aic6360 by Jarle Greipsland
     73  *
     74  * Acknowledgements: Many of the algorithms used in this driver are
     75  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     76  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     77  */
     78 
     79 /*
     80  * Grabbed from the sparc port at revision 1.73 for the NeXT.
     81  * Darrin B. Jewell <dbj (at) netbsd.org>  Sat Jul  4 15:41:32 1998
     82  */
     83 
     84 #include <sys/types.h>
     85 #include <sys/param.h>
     86 #include <sys/systm.h>
     87 #include <sys/kernel.h>
     88 #include <sys/errno.h>
     89 #include <sys/ioctl.h>
     90 #include <sys/device.h>
     91 #include <sys/buf.h>
     92 #include <sys/proc.h>
     93 #include <sys/user.h>
     94 #include <sys/queue.h>
     95 
     96 #include <dev/scsipi/scsi_all.h>
     97 #include <dev/scsipi/scsipi_all.h>
     98 #include <dev/scsipi/scsiconf.h>
     99 #include <dev/scsipi/scsi_message.h>
    100 
    101 #include <machine/bus.h>
    102 #include <machine/autoconf.h>
    103 #include <machine/cpu.h>
    104 
    105 #include <dev/ic/ncr53c9xreg.h>
    106 #include <dev/ic/ncr53c9xvar.h>
    107 
    108 #include <next68k/next68k/isr.h>
    109 
    110 #include <next68k/dev/nextdmareg.h>
    111 #include <next68k/dev/nextdmavar.h>
    112 
    113 #include "espreg.h"
    114 #include "espvar.h"
    115 
    116 #if 1
    117 #define ESP_DEBUG
    118 #endif
    119 
    120 #ifdef ESP_DEBUG
    121 int esp_debug = 0;
    122 #define DPRINTF(x) if (esp_debug) printf x;
    123 #else
    124 #define DPRINTF(x)
    125 #endif
    126 
    127 
    128 void	espattach_intio	__P((struct device *, struct device *, void *));
    129 int	espmatch_intio	__P((struct device *, struct cfdata *, void *));
    130 
    131 /* DMA callbacks */
    132 bus_dmamap_t esp_dmacb_continue __P((void *arg));
    133 void esp_dmacb_completed __P((bus_dmamap_t map, void *arg));
    134 void esp_dmacb_shutdown __P((void *arg));
    135 
    136 /* Linkup to the rest of the kernel */
    137 struct cfattach esp_ca = {
    138 	sizeof(struct esp_softc), espmatch_intio, espattach_intio
    139 };
    140 
    141 struct scsipi_device esp_dev = {
    142 	NULL,			/* Use default error handler */
    143 	NULL,			/* have a queue, served by this */
    144 	NULL,			/* have no async handler */
    145 	NULL,			/* Use default 'done' routine */
    146 };
    147 
    148 /*
    149  * Functions and the switch for the MI code.
    150  */
    151 u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    152 void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    153 int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    154 void	esp_dma_reset __P((struct ncr53c9x_softc *));
    155 int	esp_dma_intr __P((struct ncr53c9x_softc *));
    156 int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    157 	    size_t *, int, size_t *));
    158 void	esp_dma_go __P((struct ncr53c9x_softc *));
    159 void	esp_dma_stop __P((struct ncr53c9x_softc *));
    160 int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    161 
    162 struct ncr53c9x_glue esp_glue = {
    163 	esp_read_reg,
    164 	esp_write_reg,
    165 	esp_dma_isintr,
    166 	esp_dma_reset,
    167 	esp_dma_intr,
    168 	esp_dma_setup,
    169 	esp_dma_go,
    170 	esp_dma_stop,
    171 	esp_dma_isactive,
    172 	NULL,			/* gl_clear_latched_intr */
    173 };
    174 
    175 #ifdef ESP_DEBUG
    176 #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    177 static void
    178 esp_hex_dump(unsigned char *pkt, size_t len)
    179 {
    180 	size_t i, j;
    181 
    182 	printf("0000: ");
    183 	for(i=0; i<len; i++) {
    184 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    185 		if ((i+1) % 16 == 0) {
    186 			printf("  %c", '"');
    187 			for(j=0; j<16; j++)
    188 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    189 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
    190 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    191 		}
    192 	}
    193 	printf("\n");
    194 }
    195 #endif
    196 
    197 int
    198 espmatch_intio(parent, cf, aux)
    199 	struct device *parent;
    200 	struct cfdata *cf;
    201 	void *aux;
    202 {
    203   /* should probably probe here */
    204   /* Should also probably set up data from config */
    205 
    206 #if 1
    207 /* this code isn't working yet, don't match on it */
    208 	return(0);
    209 #else
    210 	return(1);
    211 #endif
    212 }
    213 
    214 void
    215 espattach_intio(parent, self, aux)
    216 	struct device *parent, *self;
    217 	void *aux;
    218 {
    219 	struct esp_softc *esc = (void *)self;
    220 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    221 
    222 	esc->sc_bst = NEXT68K_INTIO_BUS_SPACE;
    223 	if (bus_space_map(esc->sc_bst, NEXT_P_SCSI,
    224 			ESP_DEVICE_SIZE, 0, &esc->sc_bsh)) {
    225     panic("\n%s: can't map ncr53c90 registers",
    226 				sc->sc_dev.dv_xname);
    227 	}
    228 
    229 	sc->sc_id = 7;
    230 	sc->sc_freq = 20;							/* Mhz */
    231 
    232 	/*
    233 	 * Set up glue for MI code early; we use some of it here.
    234 	 */
    235 	sc->sc_glue = &esp_glue;
    236 
    237 	/*
    238 	 * XXX More of this should be in ncr53c9x_attach(), but
    239 	 * XXX should we really poke around the chip that much in
    240 	 * XXX the MI code?  Think about this more...
    241 	 */
    242 
    243 	/*
    244 	 * It is necessary to try to load the 2nd config register here,
    245 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    246 	 * will not set up the defaults correctly.
    247 	 */
    248 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    249 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    250 	sc->sc_cfg3 = NCRCFG3_CDB;
    251 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    252 
    253 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    254 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    255 		sc->sc_rev = NCR_VARIANT_ESP100;
    256 	} else {
    257 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    258 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    259 		sc->sc_cfg3 = 0;
    260 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    261 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    262 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    263 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    264 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    265 			sc->sc_rev = NCR_VARIANT_ESP100A;
    266 		} else {
    267 			/* NCRCFG2_FE enables > 64K transfers */
    268 			sc->sc_cfg2 |= NCRCFG2_FE;
    269 			sc->sc_cfg3 = 0;
    270 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    271 			sc->sc_rev = NCR_VARIANT_ESP200;
    272 		}
    273 	}
    274 
    275 	/*
    276 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    277 	 * XXX but it appears to have some dependency on what sort
    278 	 * XXX of DMA we're hooked up to, etc.
    279 	 */
    280 
    281 	/*
    282 	 * This is the value used to start sync negotiations
    283 	 * Note that the NCR register "SYNCTP" is programmed
    284 	 * in "clocks per byte", and has a minimum value of 4.
    285 	 * The SCSI period used in negotiation is one-fourth
    286 	 * of the time (in nanoseconds) needed to transfer one byte.
    287 	 * Since the chip's clock is given in MHz, we have the following
    288 	 * formula: 4 * period = (1000 / freq) * 4
    289 	 */
    290 	sc->sc_minsync = 1000 / sc->sc_freq;
    291 
    292 	/*
    293 	 * Alas, we must now modify the value a bit, because it's
    294 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    295 	 * in config register 3...
    296 	 */
    297 	switch (sc->sc_rev) {
    298 	case NCR_VARIANT_ESP100:
    299 		sc->sc_maxxfer = 64 * 1024;
    300 		sc->sc_minsync = 0;	/* No synch on old chip? */
    301 		break;
    302 
    303 	case NCR_VARIANT_ESP100A:
    304 		sc->sc_maxxfer = 64 * 1024;
    305 		/* Min clocks/byte is 5 */
    306 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    307 		break;
    308 
    309 	case NCR_VARIANT_ESP200:
    310 		sc->sc_maxxfer = 16 * 1024 * 1024;
    311 		/* XXX - do actually set FAST* bits */
    312 		break;
    313 	}
    314 
    315 	/* @@@ Some ESP_DCTL bits probably need setting */
    316 	NCR_WRITE_REG(sc, ESP_DCTL,
    317 			ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_RESET);
    318 	DELAY(10);
    319 	NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_20MHZ | ESPDCTL_INTENB);
    320 	DELAY(10);
    321 
    322 	/* Set up SCSI DMA */
    323 	{
    324 		esc->sc_scsi_dma.nd_bst = NEXT68K_INTIO_BUS_SPACE;
    325 
    326 		if (bus_space_map(esc->sc_scsi_dma.nd_bst, NEXT_P_SCSI_CSR,
    327 				sizeof(struct dma_dev),0, &esc->sc_scsi_dma.nd_bsh)) {
    328 			panic("\n%s: can't map scsi DMA registers",
    329 					sc->sc_dev.dv_xname);
    330 		}
    331 
    332 		esc->sc_scsi_dma.nd_intr = NEXT_I_SCSI_DMA;
    333 		esc->sc_scsi_dma.nd_shutdown_cb  = &esp_dmacb_shutdown;
    334 		esc->sc_scsi_dma.nd_continue_cb  = &esp_dmacb_continue;
    335 		esc->sc_scsi_dma.nd_completed_cb = &esp_dmacb_completed;
    336 		esc->sc_scsi_dma.nd_cb_arg       = sc;
    337 		nextdma_config(&esc->sc_scsi_dma);
    338 		nextdma_init(&esc->sc_scsi_dma);
    339 
    340 		{
    341 			int error;
    342 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    343 					sc->sc_maxxfer, 1, sc->sc_maxxfer,
    344 					0, BUS_DMA_ALLOCNOW, &esc->sc_dmamap)) != 0) {
    345 				panic("%s: can't create i/o DMA map, error = %d",
    346 						sc->sc_dev.dv_xname,error);
    347 			}
    348 		}
    349 	}
    350 
    351 #if 0
    352 	/* Turn on target selection using the `dma' method */
    353 	ncr53c9x_dmaselect = 1;
    354 #else
    355 	ncr53c9x_dmaselect = 0;
    356 #endif
    357 
    358 	esc->sc_slop_bgn_addr = 0;
    359 	esc->sc_slop_bgn_size = 0;
    360 	esc->sc_slop_end_addr = 0;
    361 	esc->sc_slop_end_size = 0;
    362 	esc->sc_datain = -1;
    363 	esc->sc_dmamap_loaded = 0;
    364 
    365 	/* Establish interrupt channel */
    366 	isrlink_autovec((int(*)__P((void*)))ncr53c9x_intr, sc,
    367 			NEXT_I_IPL(NEXT_I_SCSI), 0);
    368 	INTR_ENABLE(NEXT_I_SCSI);
    369 
    370 	/* register interrupt stats */
    371 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    372 
    373 	/* Do the common parts of attachment. */
    374 	sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
    375 	sc->sc_adapter.scsipi_minphys = minphys;
    376 	ncr53c9x_attach(sc, &esp_dev);
    377 }
    378 
    379 /*
    380  * Glue functions.
    381  */
    382 
    383 u_char
    384 esp_read_reg(sc, reg)
    385 	struct ncr53c9x_softc *sc;
    386 	int reg;
    387 {
    388 	struct esp_softc *esc = (struct esp_softc *)sc;
    389 
    390 	return(bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg));
    391 }
    392 
    393 void
    394 esp_write_reg(sc, reg, val)
    395 	struct ncr53c9x_softc *sc;
    396 	int reg;
    397 	u_char val;
    398 {
    399 	struct esp_softc *esc = (struct esp_softc *)sc;
    400 
    401 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg, val);
    402 }
    403 
    404 int
    405 esp_dma_isintr(sc)
    406 	struct ncr53c9x_softc *sc;
    407 {
    408 	struct esp_softc *esc = (struct esp_softc *)sc;
    409 
    410 	int r = (INTR_OCCURRED(NEXT_I_SCSI));
    411 
    412 	if (r) {
    413 		DPRINTF(("esp_dma_isintr = 0x%b\n",r,NEXT_INTR_BITS));
    414 	}
    415 
    416 	return (r);
    417 }
    418 
    419 void
    420 esp_dma_reset(sc)
    421 	struct ncr53c9x_softc *sc;
    422 {
    423 	struct esp_softc *esc = (struct esp_softc *)sc;
    424 
    425 	nextdma_reset(&esc->sc_scsi_dma);
    426 
    427 	if (esc->sc_dmamap->dm_mapsize != 0) {
    428 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap);
    429 	}
    430 
    431 	esc->sc_slop_bgn_addr = 0;
    432 	esc->sc_slop_bgn_size = 0;
    433 	esc->sc_slop_end_addr = 0;
    434 	esc->sc_slop_end_size = 0;
    435 	esc->sc_datain = -1;
    436 	esc->sc_dmamap_loaded = 0;
    437 
    438 	/* Clear the DMAMOD bit in the DCTL register: */
    439 	if (esc->sc_datain) {
    440 		NCR_WRITE_REG(sc, ESP_DCTL,
    441 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    442 	} else {
    443 		NCR_WRITE_REG(sc, ESP_DCTL,
    444 				ESPDCTL_20MHZ | ESPDCTL_INTENB);
    445 	}
    446 }
    447 
    448 int
    449 esp_dma_intr(sc)
    450 	struct ncr53c9x_softc *sc;
    451 {
    452 	int trans;
    453 	int resid;
    454 	int datain;
    455 	struct esp_softc *esc = (struct esp_softc *)sc;
    456 
    457 	datain = esc->sc_datain;
    458 
    459 	DPRINTF(("esp_dma_intr resetting dma\n"));
    460 
    461 	/* If the dma hasn't finished when we are in a scsi
    462 	 * interrupt. Then, "Houston, we have a problem."
    463 	 * Stop DMA and figure out how many bytes were transferred
    464 	 */
    465 	esp_dma_reset(sc);
    466 
    467 	resid = 0;
    468 
    469 	/*
    470 	 * If a transfer onto the SCSI bus gets interrupted by the device
    471 	 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
    472 	 * as residual since the ESP counter registers get decremented as
    473 	 * bytes are clocked into the FIFO.
    474 	 */
    475 
    476 	if (! datain) {
    477 		resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
    478 		if (resid) {
    479 			NCR_DMA(("dmaintr: empty esp FIFO of %d ", resid));
    480 			NCRCMD(sc, NCRCMD_FLUSH);
    481 			DELAY(1);
    482 		}
    483 	}
    484 
    485 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    486 		/*
    487 		 * `Terminal count' is off, so read the residue
    488 		 * out of the ESP counter registers.
    489 		 */
    490 		resid += (NCR_READ_REG(sc, NCR_TCL) |
    491 			  (NCR_READ_REG(sc, NCR_TCM) << 8) |
    492 			   ((sc->sc_cfg2 & NCRCFG2_FE)
    493 				? (NCR_READ_REG(sc, NCR_TCH) << 16)
    494 				: 0));
    495 
    496 		if (resid == 0 && esc->sc_dmasize == 65536 &&
    497 		    (sc->sc_cfg2 & NCRCFG2_FE) == 0)
    498 			/* A transfer of 64K is encoded as `TCL=TCM=0' */
    499 			resid = 65536;
    500 	}
    501 
    502 	trans = esc->sc_dmasize - resid;
    503 	if (trans < 0) {			/* transferred < 0 ? */
    504 #if 0
    505 		/*
    506 		 * This situation can happen in perfectly normal operation
    507 		 * if the ESP is reselected while using DMA to select
    508 		 * another target.  As such, don't print the warning.
    509 		 */
    510 		printf("%s: xfer (%d) > req (%d)\n",
    511 		    esc->sc_dev.dv_xname, trans, esc->sc_dmasize);
    512 #endif
    513 		trans = esc->sc_dmasize;
    514 	}
    515 
    516 	NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
    517 		NCR_READ_REG(sc, NCR_TCL),
    518 		NCR_READ_REG(sc, NCR_TCM),
    519 		(sc->sc_cfg2 & NCRCFG2_FE)
    520 			? NCR_READ_REG(sc, NCR_TCH) : 0,
    521 		trans, resid));
    522 
    523 #ifdef ESP_DEBUG
    524 	if (esp_debug) esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
    525 #endif
    526 
    527 	*esc->sc_dmalen -= trans;
    528 	*esc->sc_dmaaddr += trans;
    529 
    530 	return 0;
    531 }
    532 
    533 int
    534 esp_dma_setup(sc, addr, len, datain, dmasize)
    535 	struct ncr53c9x_softc *sc;
    536 	caddr_t *addr;
    537 	size_t *len;
    538 	int datain;
    539 	size_t *dmasize;
    540 {
    541 	struct esp_softc *esc = (struct esp_softc *)sc;
    542 
    543 	/* Save these in case we have to abort DMA */
    544 	esc->sc_dmaaddr = addr;
    545 	esc->sc_dmalen = len;
    546 	esc->sc_dmasize = *dmasize;
    547 
    548 #ifdef DIAGNOSTIC
    549 	/* if this is a read DMA, pre-fill the buffer with 0xdeadbeef
    550 	 * to identify bogus reads
    551 	 */
    552 	if (datain) {
    553 		int *v = (int *)(*esc->sc_dmaaddr);
    554 		int i;
    555 		for(i=0;i<((*len)/4);i++) v[i] = 0xdeadbeef;
    556 	}
    557 #endif
    558 
    559 	DPRINTF(("esp_dma_setup(0x%08lx,0x%08lx,0x%08lx)\n",*addr,*len,*dmasize));
    560 
    561 #ifdef DIAGNOSTIC								/* @@@ this is ok sometimes, verify.  */
    562 	if (*(esc->sc_dmalen) != esc->sc_dmasize) {
    563 		panic("esp dmalen != size");
    564 	}
    565 #endif
    566 
    567 #ifdef DIAGNOSTIC
    568 	if ((esc->sc_datain != -1) ||
    569 			(esc->sc_dmamap->dm_mapsize != 0) ||
    570 			(esc->sc_dmamap_loaded != 0)) {
    571 		panic("%s: map already loaded in esp_dma_setup\n"
    572 				"\tdatain = %d\n\tmapsize=%d\n\tloaed = %d",
    573 				sc->sc_dev.dv_xname,esc->sc_datain,esc->sc_dmamap->dm_mapsize,
    574 				esc->sc_dmamap_loaded);
    575 	}
    576 #endif
    577 
    578 	/* Deal with DMA alignment issues, by stuffing the FIFO.
    579 	 * This assumes that if bus_dmamap_load is given an aligned
    580 	 * buffer, then it will generate aligned hardware addresses
    581 	 * to give to the device.  Perhaps that is not a good assumption,
    582 	 * but it is probably true. [dbj (at) netbsd.org:19980719.0135EDT]
    583 	 */
    584 	{
    585 		int slop_bgn_size; /* # bytes to be fifo'd at beginning */
    586 		int slop_end_size; /* # bytes to be fifo'd at end */
    587 
    588 		{
    589 			u_long bgn = (u_long)(*addr);
    590 			u_long end = (u_long)(*addr+*dmasize);
    591 
    592 			slop_bgn_size = DMA_BEGINALIGNMENT-(bgn % DMA_BEGINALIGNMENT);
    593 			if (slop_bgn_size == DMA_BEGINALIGNMENT) slop_bgn_size = 0;
    594 			slop_end_size = end % DMA_ENDALIGNMENT;
    595 		}
    596 
    597 		/* Check to make sure we haven't counted extra slop
    598 		 * as would happen for a very short dma buffer */
    599 		if (slop_bgn_size+slop_end_size >= *dmasize) {
    600 			slop_bgn_size = *dmasize;
    601 			slop_end_size = 0;
    602 
    603 		} else {
    604 			int error;
    605 			error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    606 					esc->sc_dmamap,
    607 					*addr+slop_bgn_size,
    608 					*dmasize-(slop_bgn_size+slop_end_size),
    609 					NULL, BUS_DMA_NOWAIT);
    610 			if (error) {
    611 				panic("%s: can't load dma map. error = %d",
    612 						sc->sc_dev.dv_xname, error);
    613 			}
    614 
    615 		}
    616 
    617 		esc->sc_slop_bgn_addr = *addr;
    618 		esc->sc_slop_bgn_size = slop_bgn_size;
    619 		esc->sc_slop_end_addr = (*addr+*dmasize)-slop_end_size;
    620 		esc->sc_slop_end_size = slop_end_size;
    621 	}
    622 
    623 	esc->sc_datain = datain;
    624 
    625 	return (0);
    626 }
    627 
    628 void
    629 esp_dma_go(sc)
    630 	struct ncr53c9x_softc *sc;
    631 {
    632 	struct esp_softc *esc = (struct esp_softc *)sc;
    633 
    634 	DPRINTF(("esp_dma_go(datain = %d)\n",esc->sc_datain));
    635 
    636 	DPRINTF(("\tbgn slop = %d\n\tend slop = %d\n\tmapsize = %d\n",
    637 			esc->sc_slop_bgn_size,esc->sc_slop_end_size,
    638 			esc->sc_dmamap->dm_mapsize));
    639 
    640 #ifdef DIAGNOSTIC
    641 	{
    642 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    643 		DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    644 	}
    645 #endif
    646 
    647 	if (esc->sc_datain) {
    648 		int i;
    649 		for(i=0;i<esc->sc_slop_bgn_size;i++) {
    650 			esc->sc_slop_bgn_addr[i]=NCR_READ_REG(sc, NCR_FIFO);
    651 		}
    652 	} else {
    653 		int i;
    654 		for(i=0;i<esc->sc_slop_bgn_size;i++) {
    655 			NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_slop_bgn_addr[i]);
    656 		}
    657 	}
    658 
    659 #ifdef DIAGNOSTIC
    660 	{
    661 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    662 		DPRINTF(("esp fifo size = %d, seq = 0x%x\n",n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    663 	}
    664 #endif
    665 
    666 	if (esc->sc_dmamap->dm_mapsize != 0) {
    667 		if (esc->sc_datain) {
    668 			NCR_WRITE_REG(sc, ESP_DCTL,
    669 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    670 		} else {
    671 			NCR_WRITE_REG(sc, ESP_DCTL,
    672 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    673 		}
    674 
    675 		nextdma_start(&esc->sc_scsi_dma,
    676 				(esc->sc_datain ? DMACSR_READ : DMACSR_WRITE));
    677 	} else {
    678 #if defined(DIAGNOSTIC)
    679 		/* verify that end slop is 0, since the shutdown
    680 		 * callback will not be called.
    681 		 */
    682 		if (esc->sc_slop_end_size != 0) {
    683 			panic("%s: Unexpected end slop with no DMA, slop = %d",
    684 					sc->sc_dev.dv_xname, esc->sc_slop_end_size);
    685 		}
    686 #endif
    687 
    688 		esc->sc_datain = -1;
    689 		esc->sc_slop_bgn_addr = 0;
    690 		esc->sc_slop_bgn_size = 0;
    691 		esc->sc_slop_end_addr = 0;
    692 		esc->sc_slop_end_size = 0;
    693 
    694 		DPRINTF(("esp fifo size = %d\n",
    695 				(NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)));
    696 	}
    697 }
    698 
    699 void
    700 esp_dma_stop(sc)
    701 	struct ncr53c9x_softc *sc;
    702 {
    703 	panic("Not yet implemented");
    704 }
    705 
    706 int
    707 esp_dma_isactive(sc)
    708 	struct ncr53c9x_softc *sc;
    709 {
    710 	struct esp_softc *esc = (struct esp_softc *)sc;
    711 	int r = !nextdma_finished(&esc->sc_scsi_dma);
    712 	DPRINTF(("esp_dma_isactive = %d\n",r));
    713 	return(r);
    714 }
    715 
    716 /****************************************************************/
    717 
    718 /* Internal dma callback routines */
    719 bus_dmamap_t
    720 esp_dmacb_continue(arg)
    721 	void *arg;
    722 {
    723 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    724 	struct esp_softc *esc = (struct esp_softc *)sc;
    725 
    726 	DPRINTF(("esp dma continue\n"));
    727 
    728   bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap,
    729 			0, esc->sc_dmamap->dm_mapsize,
    730 			(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    731 
    732 #ifdef DIAGNOSTIC
    733 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
    734 		panic("%s: map not loaded in dma continue callback, datain = %d",
    735 				sc->sc_dev.dv_xname,esc->sc_datain);
    736 	}
    737 #endif
    738 
    739 	if (esc->sc_dmamap_loaded == 0) {
    740 		esc->sc_dmamap_loaded++;
    741 		return(esc->sc_dmamap);
    742 	} else {
    743 #ifdef DIAGNOSTIC
    744 		if (esc->sc_dmamap_loaded != 1) {
    745 			panic("%s: Unexpected sc_dmamap_loaded (%d) != 1 in continue_cb",
    746 					sc->sc_dev.dv_xname,esc->sc_dmamap_loaded);
    747 		}
    748 #endif
    749 		return(0);
    750 	}
    751 }
    752 
    753 void
    754 esp_dmacb_completed(map, arg)
    755 	bus_dmamap_t map;
    756 	void *arg;
    757 {
    758 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    759 	struct esp_softc *esc = (struct esp_softc *)sc;
    760 
    761 	DPRINTF(("esp dma completed\n"));
    762 
    763 #ifdef DIAGNOSTIC
    764 	if ((esc->sc_datain < 0) ||
    765 			(esc->sc_datain > 1) ||
    766 			(esc->sc_dmamap_loaded != 1)) {
    767 		panic("%s: map not loaded in dma completed callback, datain = %d, loaded = %d",
    768 				sc->sc_dev.dv_xname,esc->sc_datain,esc->sc_dmamap_loaded);
    769 	}
    770 	if (map != esc->sc_dmamap) {
    771 		panic("%s: unexpected tx completed map", sc->sc_dev.dv_xname);
    772 	}
    773 #endif
    774 
    775 	/* @@@ Flush the fifo? */
    776 
    777   bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap,
    778 			0, esc->sc_dmamap->dm_mapsize,
    779 			(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
    780 }
    781 
    782 void
    783 esp_dmacb_shutdown(arg)
    784 	void *arg;
    785 {
    786 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    787 	struct esp_softc *esc = (struct esp_softc *)sc;
    788 
    789 	DPRINTF(("esp dma shutdown\n"));
    790 
    791 #ifdef DIAGNOSTIC
    792 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
    793 		panic("%s: map not loaded in dma shutdown callback, datain = %d",
    794 				sc->sc_dev.dv_xname,esc->sc_datain);
    795 	}
    796 #endif
    797 
    798 	bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_dmamap);
    799 
    800 	if (esc->sc_datain) {
    801 		NCR_WRITE_REG(sc, ESP_DCTL,
    802 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    803 	} else {
    804 		NCR_WRITE_REG(sc, ESP_DCTL,
    805 				ESPDCTL_20MHZ | ESPDCTL_INTENB);
    806 	}
    807 
    808 	/* Stuff the end slop into fifo */
    809 
    810 	{
    811 
    812 		if (esc->sc_datain) {
    813 			int i;
    814 			int r = NCR_READ_REG(sc, NCR_FFLAG);
    815 			int n = r & NCRFIFO_FF;
    816 #ifdef DIAGNOSTIC
    817 #if 0 /*
    818 			 * This condition is ok.
    819 			 * For example, scsi sense requests as much as might be available.
    820 			 */
    821 			int s = (r & NCRFIFO_SS) >> 5;
    822 			if (n != esc->sc_slop_end_size) {
    823 				panic("%s: Unexpected data in fifo n = %d, expecting %d at end. (seq = 0x%x)\n",
    824 						sc->sc_dev.dv_xname, n , esc->sc_slop_end_size, s);
    825 			}
    826 #endif
    827 #endif
    828 			for(i=0;i<n;i++) {
    829 				esc->sc_slop_end_addr[i]=NCR_READ_REG(sc, NCR_FIFO);
    830 			}
    831 
    832 		} else {
    833 			int i;
    834 			for(i=0;i<esc->sc_slop_end_size;i++) {
    835 				NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_slop_end_addr[i]);
    836 			}
    837 		}
    838 	}
    839 
    840 #ifdef ESP_DEBUG
    841 	if (esp_debug) esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
    842 #endif
    843 
    844 	esc->sc_datain = -1;
    845 	esc->sc_slop_bgn_addr = 0;
    846 	esc->sc_slop_bgn_size = 0;
    847 	esc->sc_slop_end_addr = 0;
    848 	esc->sc_slop_end_size = 0;
    849 	esc->sc_dmamap_loaded--;
    850 #ifdef DIAGNOSTIC
    851 	if (esc->sc_dmamap_loaded != 0) {
    852 		panic("%s: Unexpected sc_dmamap_loaded (%d) != 0 in shutdown_cb",
    853 				sc->sc_dev.dv_xname,esc->sc_dmamap_loaded);
    854 	}
    855 #endif
    856 }
    857