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