Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.20
      1  1.20      dbj /*	$NetBSD: esp.c,v 1.20 1999/02/14 10:19:51 dbj Exp $	*/
      2   1.1      dbj 
      3   1.1      dbj /*-
      4   1.5  mycroft  * Copyright (c) 1997, 1998 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.6  mycroft  * by Charles M. Hannum and by Jason R. Thorpe of the Numerical Aerospace
      9   1.6  mycroft  * Simulation Facility, 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) 1994 Peter Galbavy
     42   1.1      dbj  * All rights reserved.
     43   1.1      dbj  *
     44   1.1      dbj  * Redistribution and use in source and binary forms, with or without
     45   1.1      dbj  * modification, are permitted provided that the following conditions
     46   1.1      dbj  * are met:
     47   1.1      dbj  * 1. Redistributions of source code must retain the above copyright
     48   1.1      dbj  *    notice, this list of conditions and the following disclaimer.
     49   1.1      dbj  * 2. Redistributions in binary form must reproduce the above copyright
     50   1.1      dbj  *    notice, this list of conditions and the following disclaimer in the
     51   1.1      dbj  *    documentation and/or other materials provided with the distribution.
     52   1.1      dbj  * 3. All advertising materials mentioning features or use of this software
     53   1.1      dbj  *    must display the following acknowledgement:
     54   1.1      dbj  *	This product includes software developed by Peter Galbavy
     55   1.1      dbj  * 4. The name of the author may not be used to endorse or promote products
     56   1.1      dbj  *    derived from this software without specific prior written permission.
     57   1.1      dbj  *
     58   1.1      dbj  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     59   1.1      dbj  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     60   1.1      dbj  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     61   1.1      dbj  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     62   1.1      dbj  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     63   1.1      dbj  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     64   1.1      dbj  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     65   1.1      dbj  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     66   1.1      dbj  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     67   1.1      dbj  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     68   1.1      dbj  * POSSIBILITY OF SUCH DAMAGE.
     69   1.1      dbj  */
     70   1.1      dbj 
     71   1.1      dbj /*
     72   1.1      dbj  * Based on aic6360 by Jarle Greipsland
     73   1.1      dbj  *
     74   1.1      dbj  * Acknowledgements: Many of the algorithms used in this driver are
     75   1.1      dbj  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     76   1.1      dbj  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     77   1.1      dbj  */
     78   1.1      dbj 
     79   1.1      dbj /*
     80   1.1      dbj  * Grabbed from the sparc port at revision 1.73 for the NeXT.
     81   1.1      dbj  * Darrin B. Jewell <dbj (at) netbsd.org>  Sat Jul  4 15:41:32 1998
     82   1.1      dbj  */
     83   1.1      dbj 
     84   1.1      dbj #include <sys/types.h>
     85   1.1      dbj #include <sys/param.h>
     86   1.1      dbj #include <sys/systm.h>
     87   1.1      dbj #include <sys/kernel.h>
     88   1.1      dbj #include <sys/errno.h>
     89   1.1      dbj #include <sys/ioctl.h>
     90   1.1      dbj #include <sys/device.h>
     91   1.1      dbj #include <sys/buf.h>
     92   1.1      dbj #include <sys/proc.h>
     93   1.1      dbj #include <sys/user.h>
     94   1.1      dbj #include <sys/queue.h>
     95   1.1      dbj 
     96   1.1      dbj #include <dev/scsipi/scsi_all.h>
     97   1.1      dbj #include <dev/scsipi/scsipi_all.h>
     98   1.1      dbj #include <dev/scsipi/scsiconf.h>
     99   1.1      dbj #include <dev/scsipi/scsi_message.h>
    100   1.1      dbj 
    101   1.1      dbj #include <machine/bus.h>
    102   1.1      dbj #include <machine/autoconf.h>
    103   1.1      dbj #include <machine/cpu.h>
    104   1.1      dbj 
    105   1.1      dbj #include <dev/ic/ncr53c9xreg.h>
    106   1.1      dbj #include <dev/ic/ncr53c9xvar.h>
    107   1.1      dbj 
    108   1.1      dbj #include <next68k/next68k/isr.h>
    109   1.1      dbj 
    110   1.1      dbj #include <next68k/dev/nextdmareg.h>
    111   1.1      dbj #include <next68k/dev/nextdmavar.h>
    112   1.1      dbj 
    113   1.1      dbj #include "espreg.h"
    114   1.1      dbj #include "espvar.h"
    115   1.1      dbj 
    116  1.20      dbj #ifdef DEBUG
    117   1.4      dbj #define ESP_DEBUG
    118   1.4      dbj #endif
    119   1.4      dbj 
    120   1.4      dbj #ifdef ESP_DEBUG
    121  1.10      dbj int esp_debug = 0;
    122  1.10      dbj #define DPRINTF(x) if (esp_debug) printf x;
    123   1.4      dbj #else
    124   1.4      dbj #define DPRINTF(x)
    125   1.4      dbj #endif
    126   1.4      dbj 
    127   1.4      dbj 
    128   1.1      dbj void	espattach_intio	__P((struct device *, struct device *, void *));
    129   1.1      dbj int	espmatch_intio	__P((struct device *, struct cfdata *, void *));
    130   1.1      dbj 
    131   1.2      dbj /* DMA callbacks */
    132   1.2      dbj bus_dmamap_t esp_dmacb_continue __P((void *arg));
    133   1.2      dbj void esp_dmacb_completed __P((bus_dmamap_t map, void *arg));
    134   1.2      dbj void esp_dmacb_shutdown __P((void *arg));
    135   1.2      dbj 
    136  1.20      dbj #ifdef ESP_DEBUG
    137  1.20      dbj char esp_dma_dump[5*1024] = "";
    138  1.20      dbj struct ncr53c9x_softc *esp_debug_sc = 0;
    139  1.20      dbj void esp_dma_store __P((struct ncr53c9x_softc *sc));
    140  1.20      dbj void esp_dma_print __P((struct ncr53c9x_softc *sc));
    141  1.20      dbj #endif
    142  1.20      dbj 
    143  1.20      dbj 
    144   1.1      dbj /* Linkup to the rest of the kernel */
    145   1.1      dbj struct cfattach esp_ca = {
    146   1.1      dbj 	sizeof(struct esp_softc), espmatch_intio, espattach_intio
    147   1.1      dbj };
    148   1.1      dbj 
    149   1.1      dbj struct scsipi_device esp_dev = {
    150   1.1      dbj 	NULL,			/* Use default error handler */
    151   1.1      dbj 	NULL,			/* have a queue, served by this */
    152   1.1      dbj 	NULL,			/* have no async handler */
    153   1.1      dbj 	NULL,			/* Use default 'done' routine */
    154   1.1      dbj };
    155   1.1      dbj 
    156   1.1      dbj /*
    157   1.1      dbj  * Functions and the switch for the MI code.
    158   1.1      dbj  */
    159   1.1      dbj u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    160   1.1      dbj void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    161   1.1      dbj int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    162   1.1      dbj void	esp_dma_reset __P((struct ncr53c9x_softc *));
    163   1.1      dbj int	esp_dma_intr __P((struct ncr53c9x_softc *));
    164   1.1      dbj int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    165   1.1      dbj 	    size_t *, int, size_t *));
    166   1.1      dbj void	esp_dma_go __P((struct ncr53c9x_softc *));
    167   1.1      dbj void	esp_dma_stop __P((struct ncr53c9x_softc *));
    168   1.1      dbj int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    169   1.1      dbj 
    170   1.1      dbj struct ncr53c9x_glue esp_glue = {
    171   1.1      dbj 	esp_read_reg,
    172   1.1      dbj 	esp_write_reg,
    173   1.1      dbj 	esp_dma_isintr,
    174   1.1      dbj 	esp_dma_reset,
    175   1.1      dbj 	esp_dma_intr,
    176   1.1      dbj 	esp_dma_setup,
    177   1.1      dbj 	esp_dma_go,
    178   1.1      dbj 	esp_dma_stop,
    179   1.1      dbj 	esp_dma_isactive,
    180   1.1      dbj 	NULL,			/* gl_clear_latched_intr */
    181   1.1      dbj };
    182   1.1      dbj 
    183  1.11      dbj #ifdef ESP_DEBUG
    184  1.11      dbj #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    185  1.11      dbj static void
    186  1.11      dbj esp_hex_dump(unsigned char *pkt, size_t len)
    187  1.11      dbj {
    188  1.11      dbj 	size_t i, j;
    189  1.11      dbj 
    190  1.11      dbj 	printf("0000: ");
    191  1.11      dbj 	for(i=0; i<len; i++) {
    192  1.11      dbj 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    193  1.11      dbj 		if ((i+1) % 16 == 0) {
    194  1.11      dbj 			printf("  %c", '"');
    195  1.11      dbj 			for(j=0; j<16; j++)
    196  1.11      dbj 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    197  1.11      dbj 			printf("%c\n%c%c%c%c: ", '"', XCHR((i+1)>>12),
    198  1.11      dbj 				XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    199  1.11      dbj 		}
    200  1.11      dbj 	}
    201  1.11      dbj 	printf("\n");
    202  1.11      dbj }
    203  1.11      dbj #endif
    204  1.11      dbj 
    205   1.1      dbj int
    206   1.1      dbj espmatch_intio(parent, cf, aux)
    207   1.1      dbj 	struct device *parent;
    208   1.1      dbj 	struct cfdata *cf;
    209   1.1      dbj 	void *aux;
    210   1.1      dbj {
    211   1.1      dbj   /* should probably probe here */
    212   1.1      dbj   /* Should also probably set up data from config */
    213   1.1      dbj 
    214   1.3      dbj 	return(1);
    215   1.1      dbj }
    216   1.1      dbj 
    217   1.1      dbj void
    218   1.1      dbj espattach_intio(parent, self, aux)
    219   1.1      dbj 	struct device *parent, *self;
    220   1.1      dbj 	void *aux;
    221   1.1      dbj {
    222   1.1      dbj 	struct esp_softc *esc = (void *)self;
    223   1.1      dbj 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    224   1.1      dbj 
    225  1.20      dbj #ifdef ESP_DEBUG
    226  1.20      dbj 	esp_debug_sc = sc;
    227  1.20      dbj #endif
    228  1.20      dbj 
    229   1.1      dbj 	esc->sc_bst = NEXT68K_INTIO_BUS_SPACE;
    230   1.1      dbj 	if (bus_space_map(esc->sc_bst, NEXT_P_SCSI,
    231   1.1      dbj 			ESP_DEVICE_SIZE, 0, &esc->sc_bsh)) {
    232   1.3      dbj     panic("\n%s: can't map ncr53c90 registers",
    233   1.1      dbj 				sc->sc_dev.dv_xname);
    234   1.1      dbj 	}
    235   1.1      dbj 
    236   1.1      dbj 	sc->sc_id = 7;
    237   1.1      dbj 	sc->sc_freq = 20;							/* Mhz */
    238   1.1      dbj 
    239   1.1      dbj 	/*
    240   1.1      dbj 	 * Set up glue for MI code early; we use some of it here.
    241   1.1      dbj 	 */
    242   1.1      dbj 	sc->sc_glue = &esp_glue;
    243   1.1      dbj 
    244   1.1      dbj 	/*
    245   1.1      dbj 	 * XXX More of this should be in ncr53c9x_attach(), but
    246   1.1      dbj 	 * XXX should we really poke around the chip that much in
    247   1.1      dbj 	 * XXX the MI code?  Think about this more...
    248   1.1      dbj 	 */
    249   1.1      dbj 
    250   1.1      dbj 	/*
    251   1.1      dbj 	 * It is necessary to try to load the 2nd config register here,
    252   1.1      dbj 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    253   1.1      dbj 	 * will not set up the defaults correctly.
    254   1.1      dbj 	 */
    255   1.1      dbj 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    256   1.1      dbj 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    257   1.1      dbj 	sc->sc_cfg3 = NCRCFG3_CDB;
    258   1.1      dbj 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    259   1.1      dbj 
    260   1.1      dbj 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    261   1.1      dbj 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    262   1.1      dbj 		sc->sc_rev = NCR_VARIANT_ESP100;
    263   1.1      dbj 	} else {
    264   1.1      dbj 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    265   1.1      dbj 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    266   1.1      dbj 		sc->sc_cfg3 = 0;
    267   1.1      dbj 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    268   1.1      dbj 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    269   1.1      dbj 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    270   1.1      dbj 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    271   1.1      dbj 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    272   1.1      dbj 			sc->sc_rev = NCR_VARIANT_ESP100A;
    273   1.1      dbj 		} else {
    274   1.1      dbj 			/* NCRCFG2_FE enables > 64K transfers */
    275   1.1      dbj 			sc->sc_cfg2 |= NCRCFG2_FE;
    276   1.1      dbj 			sc->sc_cfg3 = 0;
    277   1.1      dbj 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    278   1.1      dbj 			sc->sc_rev = NCR_VARIANT_ESP200;
    279   1.1      dbj 		}
    280   1.1      dbj 	}
    281   1.1      dbj 
    282   1.1      dbj 	/*
    283   1.1      dbj 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    284   1.1      dbj 	 * XXX but it appears to have some dependency on what sort
    285   1.1      dbj 	 * XXX of DMA we're hooked up to, etc.
    286   1.1      dbj 	 */
    287   1.1      dbj 
    288   1.1      dbj 	/*
    289   1.1      dbj 	 * This is the value used to start sync negotiations
    290   1.1      dbj 	 * Note that the NCR register "SYNCTP" is programmed
    291   1.1      dbj 	 * in "clocks per byte", and has a minimum value of 4.
    292   1.1      dbj 	 * The SCSI period used in negotiation is one-fourth
    293   1.1      dbj 	 * of the time (in nanoseconds) needed to transfer one byte.
    294   1.1      dbj 	 * Since the chip's clock is given in MHz, we have the following
    295   1.1      dbj 	 * formula: 4 * period = (1000 / freq) * 4
    296   1.1      dbj 	 */
    297   1.1      dbj 	sc->sc_minsync = 1000 / sc->sc_freq;
    298   1.1      dbj 
    299   1.1      dbj 	/*
    300   1.1      dbj 	 * Alas, we must now modify the value a bit, because it's
    301   1.1      dbj 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    302   1.1      dbj 	 * in config register 3...
    303   1.1      dbj 	 */
    304   1.1      dbj 	switch (sc->sc_rev) {
    305   1.1      dbj 	case NCR_VARIANT_ESP100:
    306   1.1      dbj 		sc->sc_maxxfer = 64 * 1024;
    307   1.1      dbj 		sc->sc_minsync = 0;	/* No synch on old chip? */
    308   1.1      dbj 		break;
    309   1.1      dbj 
    310   1.1      dbj 	case NCR_VARIANT_ESP100A:
    311   1.1      dbj 		sc->sc_maxxfer = 64 * 1024;
    312   1.1      dbj 		/* Min clocks/byte is 5 */
    313   1.1      dbj 		sc->sc_minsync = ncr53c9x_cpb2stp(sc, 5);
    314   1.1      dbj 		break;
    315   1.1      dbj 
    316   1.1      dbj 	case NCR_VARIANT_ESP200:
    317   1.1      dbj 		sc->sc_maxxfer = 16 * 1024 * 1024;
    318   1.1      dbj 		/* XXX - do actually set FAST* bits */
    319   1.1      dbj 		break;
    320   1.1      dbj 	}
    321   1.1      dbj 
    322   1.3      dbj 	/* @@@ Some ESP_DCTL bits probably need setting */
    323   1.3      dbj 	NCR_WRITE_REG(sc, ESP_DCTL,
    324   1.3      dbj 			ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_RESET);
    325   1.3      dbj 	DELAY(10);
    326   1.3      dbj 	NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_20MHZ | ESPDCTL_INTENB);
    327   1.3      dbj 	DELAY(10);
    328   1.3      dbj 
    329   1.3      dbj 	/* Set up SCSI DMA */
    330   1.3      dbj 	{
    331   1.3      dbj 		esc->sc_scsi_dma.nd_bst = NEXT68K_INTIO_BUS_SPACE;
    332   1.3      dbj 
    333   1.3      dbj 		if (bus_space_map(esc->sc_scsi_dma.nd_bst, NEXT_P_SCSI_CSR,
    334   1.3      dbj 				sizeof(struct dma_dev),0, &esc->sc_scsi_dma.nd_bsh)) {
    335   1.3      dbj 			panic("\n%s: can't map scsi DMA registers",
    336   1.3      dbj 					sc->sc_dev.dv_xname);
    337   1.3      dbj 		}
    338   1.3      dbj 
    339   1.3      dbj 		esc->sc_scsi_dma.nd_intr = NEXT_I_SCSI_DMA;
    340   1.3      dbj 		esc->sc_scsi_dma.nd_shutdown_cb  = &esp_dmacb_shutdown;
    341   1.3      dbj 		esc->sc_scsi_dma.nd_continue_cb  = &esp_dmacb_continue;
    342   1.3      dbj 		esc->sc_scsi_dma.nd_completed_cb = &esp_dmacb_completed;
    343   1.3      dbj 		esc->sc_scsi_dma.nd_cb_arg       = sc;
    344   1.3      dbj 		nextdma_config(&esc->sc_scsi_dma);
    345   1.3      dbj 		nextdma_init(&esc->sc_scsi_dma);
    346   1.3      dbj 
    347  1.18      dbj #if 0
    348  1.18      dbj 		/* Turn on target selection using the `dma' method */
    349  1.18      dbj 		ncr53c9x_dmaselect = 1;
    350  1.18      dbj #else
    351  1.18      dbj 		ncr53c9x_dmaselect = 0;
    352  1.18      dbj #endif
    353  1.18      dbj 
    354  1.18      dbj 		esc->sc_datain = -1;
    355  1.18      dbj 		esc->sc_dmaaddr = 0;
    356  1.18      dbj 		esc->sc_dmalen  = 0;
    357  1.20      dbj 		esc->sc_dmasize = 0;
    358  1.18      dbj 
    359  1.18      dbj 		esc->sc_loaded = 0;
    360  1.18      dbj 
    361  1.18      dbj 		esc->sc_begin = 0;
    362  1.18      dbj 		esc->sc_begin_size = 0;
    363  1.18      dbj 
    364   1.3      dbj 		{
    365   1.3      dbj 			int error;
    366   1.3      dbj 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    367  1.19      dbj 					sc->sc_maxxfer, sc->sc_maxxfer/NBPG, sc->sc_maxxfer,
    368  1.18      dbj 					0, BUS_DMA_ALLOCNOW, &esc->sc_main_dmamap)) != 0) {
    369  1.18      dbj 				panic("%s: can't create main i/o DMA map, error = %d",
    370   1.3      dbj 						sc->sc_dev.dv_xname,error);
    371   1.3      dbj 			}
    372   1.3      dbj 		}
    373  1.18      dbj 		esc->sc_main = 0;
    374  1.18      dbj 		esc->sc_main_size = 0;
    375  1.14      dbj 
    376  1.14      dbj 		{
    377  1.14      dbj 			int error;
    378  1.14      dbj 			if ((error = bus_dmamap_create(esc->sc_scsi_dma.nd_dmat,
    379  1.19      dbj 					ESP_DMA_TAILBUFSIZE,
    380  1.19      dbj 					1, ESP_DMA_TAILBUFSIZE,
    381  1.14      dbj 					0, BUS_DMA_ALLOCNOW, &esc->sc_tail_dmamap)) != 0) {
    382  1.14      dbj 				panic("%s: can't create tail i/o DMA map, error = %d",
    383  1.14      dbj 						sc->sc_dev.dv_xname,error);
    384  1.14      dbj 			}
    385  1.14      dbj 		}
    386  1.18      dbj 		esc->sc_tail = 0;
    387  1.18      dbj 		esc->sc_tail_size = 0;
    388  1.18      dbj 
    389   1.3      dbj 	}
    390   1.1      dbj 
    391   1.3      dbj 	/* Establish interrupt channel */
    392   1.3      dbj 	isrlink_autovec((int(*)__P((void*)))ncr53c9x_intr, sc,
    393  1.20      dbj 			NEXT_I_IPL(NEXT_I_SCSI), 0);
    394   1.3      dbj 	INTR_ENABLE(NEXT_I_SCSI);
    395   1.4      dbj 
    396   1.4      dbj 	/* register interrupt stats */
    397   1.4      dbj 	evcnt_attach(&sc->sc_dev, "intr", &sc->sc_intrcnt);
    398   1.4      dbj 
    399   1.4      dbj 	/* Do the common parts of attachment. */
    400   1.9  thorpej 	sc->sc_adapter.scsipi_cmd = ncr53c9x_scsi_cmd;
    401   1.9  thorpej 	sc->sc_adapter.scsipi_minphys = minphys;
    402   1.9  thorpej 	ncr53c9x_attach(sc, &esp_dev);
    403   1.1      dbj }
    404   1.1      dbj 
    405   1.1      dbj /*
    406   1.1      dbj  * Glue functions.
    407   1.1      dbj  */
    408   1.1      dbj 
    409   1.1      dbj u_char
    410   1.1      dbj esp_read_reg(sc, reg)
    411   1.1      dbj 	struct ncr53c9x_softc *sc;
    412   1.1      dbj 	int reg;
    413   1.1      dbj {
    414   1.1      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    415   1.1      dbj 
    416   1.1      dbj 	return(bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg));
    417   1.1      dbj }
    418   1.1      dbj 
    419   1.1      dbj void
    420   1.1      dbj esp_write_reg(sc, reg, val)
    421   1.1      dbj 	struct ncr53c9x_softc *sc;
    422   1.1      dbj 	int reg;
    423   1.1      dbj 	u_char val;
    424   1.1      dbj {
    425   1.1      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    426   1.1      dbj 
    427   1.1      dbj 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg, val);
    428   1.1      dbj }
    429   1.1      dbj 
    430   1.1      dbj int
    431   1.1      dbj esp_dma_isintr(sc)
    432   1.1      dbj 	struct ncr53c9x_softc *sc;
    433   1.1      dbj {
    434   1.4      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    435   1.4      dbj 
    436   1.4      dbj 	int r = (INTR_OCCURRED(NEXT_I_SCSI));
    437   1.4      dbj 
    438   1.4      dbj 	if (r) {
    439  1.13      dbj 
    440  1.20      dbj 		{
    441  1.20      dbj 			int s;
    442  1.20      dbj 			s = spldma();
    443  1.20      dbj 
    444  1.20      dbj 			DPRINTF(("esp_dma_isintr = 0x%b\n",
    445  1.20      dbj 					(*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS));
    446  1.20      dbj 
    447  1.20      dbj 			while (esp_dma_isactive(sc)) {
    448  1.17      dbj 
    449  1.17      dbj #ifdef DIAGNOSTIC
    450  1.20      dbj 				r = (INTR_OCCURRED(NEXT_I_SCSI));
    451  1.20      dbj 				if (!r) panic("esp intr enabled but dma failed to flush");
    452  1.17      dbj #endif
    453  1.17      dbj 
    454  1.20      dbj 				if (esc->sc_datain) {
    455  1.20      dbj 					NCR_WRITE_REG(sc, ESP_DCTL,
    456  1.20      dbj 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD | ESPDCTL_FLUSH);
    457  1.20      dbj 					NCR_WRITE_REG(sc, ESP_DCTL,
    458  1.20      dbj 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    459  1.20      dbj 				} else {
    460  1.20      dbj 					NCR_WRITE_REG(sc, ESP_DCTL,
    461  1.20      dbj 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_FLUSH);
    462  1.20      dbj 					NCR_WRITE_REG(sc, ESP_DCTL,
    463  1.20      dbj 							ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    464  1.20      dbj 				}
    465  1.20      dbj 
    466  1.20      dbj 				/* Really this code should only be used in polled mode */
    467  1.20      dbj 				{
    468  1.20      dbj 					int nr;
    469  1.20      dbj 					nr = nextdma_intr(&esc->sc_scsi_dma);
    470  1.20      dbj 					if (nr) {
    471  1.20      dbj 						DPRINTF(("nextma_intr = %d\n",nr));
    472  1.20      dbj 					}
    473  1.16      dbj 				}
    474  1.16      dbj 			}
    475  1.20      dbj 
    476  1.20      dbj 			splx(s);
    477  1.13      dbj 		}
    478  1.13      dbj 
    479  1.20      dbj #ifdef DIAGNOSTIC
    480  1.20      dbj 		r = (INTR_OCCURRED(NEXT_I_SCSI));
    481  1.20      dbj 		if (!r) panic("esp intr not enabled after dma flush");
    482  1.20      dbj #endif
    483  1.20      dbj 
    484  1.13      dbj 		/* Clear the DMAMOD bit in the DCTL register, since if this
    485  1.13      dbj 		 * routine returns true, then the ncr53c9x_intr handler will
    486  1.13      dbj 		 * be called and needs access to the scsi registers.
    487  1.13      dbj 		 */
    488  1.13      dbj 		if (esc->sc_datain) {
    489  1.13      dbj 			NCR_WRITE_REG(sc, ESP_DCTL,
    490  1.13      dbj 					ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
    491  1.13      dbj 		} else {
    492  1.13      dbj 			NCR_WRITE_REG(sc, ESP_DCTL,
    493  1.13      dbj 					ESPDCTL_20MHZ | ESPDCTL_INTENB);
    494  1.13      dbj 		}
    495  1.13      dbj 
    496   1.4      dbj 	}
    497   1.4      dbj 
    498   1.4      dbj 	return (r);
    499   1.1      dbj }
    500   1.1      dbj 
    501   1.1      dbj void
    502   1.1      dbj esp_dma_reset(sc)
    503   1.1      dbj 	struct ncr53c9x_softc *sc;
    504   1.1      dbj {
    505   1.1      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    506   1.3      dbj 
    507  1.13      dbj 	DPRINTF(("esp dma reset\n"));
    508  1.13      dbj 
    509  1.13      dbj #ifdef ESP_DEBUG
    510  1.13      dbj 	if (esp_debug) {
    511  1.13      dbj 		printf("  *intrstat = 0x%b\n",
    512  1.13      dbj 				(*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS);
    513  1.13      dbj 		printf("  *intrmask = 0x%b\n",
    514  1.13      dbj 				(*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),NEXT_INTR_BITS);
    515  1.13      dbj 	}
    516  1.13      dbj #endif
    517  1.13      dbj 
    518  1.13      dbj 	/* Clear the DMAMOD bit in the DCTL register: */
    519  1.18      dbj 	NCR_WRITE_REG(sc, ESP_DCTL,
    520  1.18      dbj 			ESPDCTL_20MHZ | ESPDCTL_INTENB);
    521  1.13      dbj 
    522   1.4      dbj 	nextdma_reset(&esc->sc_scsi_dma);
    523   1.4      dbj 
    524  1.18      dbj 	esc->sc_datain = -1;
    525  1.18      dbj 	esc->sc_dmaaddr = 0;
    526  1.18      dbj 	esc->sc_dmalen  = 0;
    527  1.20      dbj 	esc->sc_dmasize = 0;
    528  1.18      dbj 
    529  1.18      dbj 	esc->sc_loaded = 0;
    530  1.18      dbj 
    531  1.18      dbj 	esc->sc_begin = 0;
    532  1.18      dbj 	esc->sc_begin_size = 0;
    533  1.13      dbj 
    534  1.18      dbj 	if (esc->sc_main_dmamap->dm_mapsize) {
    535  1.18      dbj 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap);
    536  1.13      dbj 	}
    537  1.18      dbj 	esc->sc_main = 0;
    538  1.18      dbj 	esc->sc_main_size = 0;
    539  1.13      dbj 
    540  1.18      dbj 	if (esc->sc_tail_dmamap->dm_mapsize) {
    541  1.18      dbj 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap);
    542  1.18      dbj 	}
    543  1.18      dbj 	esc->sc_tail = 0;
    544  1.18      dbj 	esc->sc_tail_size = 0;
    545   1.1      dbj }
    546   1.1      dbj 
    547   1.1      dbj int
    548   1.1      dbj esp_dma_intr(sc)
    549   1.1      dbj 	struct ncr53c9x_softc *sc;
    550   1.1      dbj {
    551  1.18      dbj #ifdef DIAGNOSTIC
    552  1.18      dbj 	panic("%s: esp_dma_intr shouldn't be invoked.\n", sc->sc_dev.dv_xname);
    553  1.11      dbj #endif
    554  1.11      dbj 
    555  1.18      dbj 	return -1;
    556   1.1      dbj }
    557   1.1      dbj 
    558  1.19      dbj /* it appears that:
    559  1.19      dbj  * addr and len arguments to this need to be kept up to date
    560  1.19      dbj  * with the status of the transfter.
    561  1.19      dbj  * the dmasize of this is the actual length of the transfer
    562  1.19      dbj  * request, which is guaranteed to be less than maxxfer.
    563  1.19      dbj  * (len may be > maxxfer)
    564  1.19      dbj  */
    565  1.19      dbj 
    566   1.1      dbj int
    567   1.1      dbj esp_dma_setup(sc, addr, len, datain, dmasize)
    568   1.1      dbj 	struct ncr53c9x_softc *sc;
    569   1.1      dbj 	caddr_t *addr;
    570   1.1      dbj 	size_t *len;
    571   1.1      dbj 	int datain;
    572   1.1      dbj 	size_t *dmasize;
    573   1.1      dbj {
    574   1.1      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    575   1.2      dbj 
    576  1.11      dbj #ifdef DIAGNOSTIC
    577  1.20      dbj #ifdef ESP_DEBUG
    578  1.11      dbj 	/* if this is a read DMA, pre-fill the buffer with 0xdeadbeef
    579  1.11      dbj 	 * to identify bogus reads
    580  1.11      dbj 	 */
    581  1.11      dbj 	if (datain) {
    582  1.14      dbj 		int *v = (int *)(*addr);
    583  1.11      dbj 		int i;
    584  1.14      dbj 		for(i=0;i<((*len)/4);i++) v[i] = 0xdeadbeef;
    585  1.18      dbj 		v = (int *)(&(esc->sc_tailbuf[0]));
    586  1.18      dbj 		for(i=0;i<((sizeof(esc->sc_tailbuf)/4));i++) v[i] = 0xdeaffeed;
    587  1.11      dbj 	}
    588  1.20      dbj #endif
    589  1.11      dbj #endif
    590  1.11      dbj 
    591  1.14      dbj 	DPRINTF(("esp_dma_setup(0x%08lx,0x%08lx,0x%08lx)\n",*addr,*len,*dmasize));
    592  1.11      dbj 
    593  1.12      dbj #ifdef DIAGNOSTIC /* @@@ this is ok sometimes. verify that we handle it ok
    594  1.12      dbj 									 * and then remove this check
    595  1.12      dbj 									 */
    596  1.14      dbj 	if (*len != *dmasize) {
    597  1.11      dbj 		panic("esp dmalen != size");
    598  1.11      dbj 	}
    599  1.11      dbj #endif
    600   1.4      dbj 
    601   1.2      dbj #ifdef DIAGNOSTIC
    602   1.3      dbj 	if ((esc->sc_datain != -1) ||
    603  1.18      dbj 			(esc->sc_main_dmamap->dm_mapsize != 0) ||
    604  1.20      dbj 			(esc->sc_tail_dmamap->dm_mapsize != 0) ||
    605  1.20      dbj 			(esc->sc_dmasize != 0)) {
    606   1.3      dbj 		panic("%s: map already loaded in esp_dma_setup\n"
    607  1.20      dbj 				"\tdatain = %d\n\tmain_mapsize=%d\n\tail_mapsize=%d\n\tdmasize = %d",
    608  1.18      dbj 				sc->sc_dev.dv_xname, esc->sc_datain,
    609  1.20      dbj 				esc->sc_main_dmamap->dm_mapsize,
    610  1.20      dbj 				esc->sc_tail_dmamap->dm_mapsize,
    611  1.20      dbj 				esc->sc_dmasize);
    612   1.2      dbj 	}
    613   1.2      dbj #endif
    614   1.2      dbj 
    615  1.20      dbj 	/* we are sometimes asked to dma zero  bytes, that's easy */
    616  1.20      dbj 	if (*len <= 0) {
    617  1.20      dbj 		return(0);
    618  1.20      dbj 	}
    619  1.20      dbj 
    620  1.14      dbj 	/* Save these in case we have to abort DMA */
    621  1.14      dbj 	esc->sc_datain   = datain;
    622  1.14      dbj 	esc->sc_dmaaddr  = addr;
    623  1.14      dbj 	esc->sc_dmalen   = len;
    624  1.14      dbj 	esc->sc_dmasize  = *dmasize;
    625  1.14      dbj 
    626  1.18      dbj 	esc->sc_loaded = 0;
    627  1.18      dbj 
    628   1.2      dbj 	{
    629  1.18      dbj 		size_t slop_bgn_size; /* # bytes to be fifo'd at beginning */
    630  1.18      dbj 		size_t slop_end_size; /* # bytes to be transferred in tail buffer */
    631  1.18      dbj 
    632   1.3      dbj 		{
    633  1.13      dbj 			u_long bgn = (u_long)(*esc->sc_dmaaddr);
    634  1.13      dbj 			u_long end = (u_long)(*esc->sc_dmaaddr+esc->sc_dmasize);
    635   1.3      dbj 
    636   1.3      dbj 			slop_bgn_size = DMA_BEGINALIGNMENT-(bgn % DMA_BEGINALIGNMENT);
    637   1.4      dbj 			if (slop_bgn_size == DMA_BEGINALIGNMENT) slop_bgn_size = 0;
    638  1.19      dbj 			slop_end_size = (end % DMA_ENDALIGNMENT);
    639   1.3      dbj 		}
    640   1.3      dbj 
    641  1.10      dbj 		/* Check to make sure we haven't counted extra slop
    642  1.14      dbj 		 * as would happen for a very short dma buffer, also
    643  1.14      dbj 		 * for short buffers, just stuff the entire thing in the tail
    644  1.14      dbj 		 */
    645  1.18      dbj 		if ((slop_bgn_size+slop_end_size >= esc->sc_dmasize)
    646  1.20      dbj #if 0
    647  1.18      dbj 				|| (esc->sc_dmasize <= ESP_DMA_MAXTAIL)
    648  1.18      dbj #endif
    649  1.18      dbj 				)
    650  1.18      dbj 		{
    651  1.14      dbj  			slop_bgn_size = 0;
    652  1.14      dbj 			slop_end_size = esc->sc_dmasize;
    653  1.18      dbj 		}
    654  1.14      dbj 
    655  1.18      dbj 		/* initialize the fifo buffer */
    656  1.18      dbj 		if (slop_bgn_size) {
    657  1.18      dbj 			esc->sc_begin = *esc->sc_dmaaddr;
    658  1.18      dbj 			esc->sc_begin_size = slop_bgn_size;
    659  1.18      dbj 		} else {
    660  1.18      dbj 			esc->sc_begin = 0;
    661  1.18      dbj 			esc->sc_begin_size = 0;
    662  1.18      dbj 		}
    663  1.18      dbj 
    664  1.18      dbj 		/* Load the normal DMA map */
    665  1.18      dbj 		{
    666  1.18      dbj 			esc->sc_main      = *esc->sc_dmaaddr+slop_bgn_size;
    667  1.18      dbj 			esc->sc_main_size = (esc->sc_dmasize)-(slop_end_size+slop_bgn_size);
    668  1.18      dbj 
    669  1.18      dbj 			if (esc->sc_main_size) {
    670  1.18      dbj 				int error;
    671  1.18      dbj 				error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    672  1.18      dbj 						esc->sc_main_dmamap,
    673  1.18      dbj 						esc->sc_main, esc->sc_main_size,
    674  1.18      dbj 						NULL, BUS_DMA_NOWAIT);
    675  1.18      dbj 				if (error) {
    676  1.18      dbj 					panic("%s: can't load main dma map. error = %d, addr=0x%08x, size=0x%08x",
    677  1.18      dbj 							sc->sc_dev.dv_xname, error,esc->sc_main,esc->sc_main_size);
    678  1.18      dbj 				}
    679  1.19      dbj 				bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
    680  1.19      dbj 						0, esc->sc_main_dmamap->dm_mapsize,
    681  1.19      dbj 						(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    682  1.18      dbj 			} else {
    683  1.18      dbj 				esc->sc_main = 0;
    684  1.18      dbj 			}
    685  1.14      dbj 		}
    686   1.3      dbj 
    687  1.18      dbj 		/* Load the tail DMA map */
    688  1.18      dbj 		if (slop_end_size) {
    689  1.18      dbj 			esc->sc_tail      = DMA_ENDALIGN(caddr_t,esc->sc_tailbuf+slop_end_size)-slop_end_size;
    690  1.18      dbj 			/* If the beginning of the tail is not correctly aligned,
    691  1.18      dbj 			 * we have no choice but to align the start, which might then unalign the end.
    692  1.18      dbj 			 */
    693  1.18      dbj 			esc->sc_tail      = DMA_ALIGN(caddr_t,esc->sc_tail);
    694  1.18      dbj 			/* So therefore, we change the tail size to be end aligned again. */
    695  1.18      dbj 			esc->sc_tail_size = DMA_ENDALIGN(caddr_t,esc->sc_tail+slop_end_size)-esc->sc_tail;
    696  1.19      dbj 
    697  1.19      dbj 			/* @@@ next dma overrun lossage */
    698  1.20      dbj 			if (!esc->sc_datain) {
    699  1.20      dbj 				esc->sc_tail_size += 2*ESP_DMA_OVERRUN;
    700  1.20      dbj 			}
    701  1.20      dbj 
    702  1.18      dbj 			{
    703  1.18      dbj 				int error;
    704  1.18      dbj 				error = bus_dmamap_load(esc->sc_scsi_dma.nd_dmat,
    705  1.18      dbj 						esc->sc_tail_dmamap,
    706  1.18      dbj 						esc->sc_tail, esc->sc_tail_size,
    707  1.18      dbj 						NULL, BUS_DMA_NOWAIT);
    708  1.18      dbj 				if (error) {
    709  1.18      dbj 					panic("%s: can't load tail dma map. error = %d, addr=0x%08x, size=0x%08x",
    710  1.18      dbj 							sc->sc_dev.dv_xname, error,esc->sc_tail,esc->sc_tail_size);
    711  1.18      dbj 				}
    712  1.19      dbj 				bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
    713  1.19      dbj 						0, esc->sc_tail_dmamap->dm_mapsize,
    714  1.19      dbj 						(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    715   1.3      dbj 			}
    716   1.3      dbj 		}
    717   1.2      dbj 	}
    718   1.2      dbj 
    719   1.1      dbj 	return (0);
    720   1.1      dbj }
    721   1.1      dbj 
    722  1.20      dbj #ifdef ESP_DEBUG
    723  1.20      dbj /* For debugging */
    724   1.1      dbj void
    725  1.20      dbj esp_dma_store(sc)
    726   1.1      dbj 	struct ncr53c9x_softc *sc;
    727   1.1      dbj {
    728   1.1      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    729  1.20      dbj 	char *p = &esp_dma_dump[0];
    730  1.20      dbj 
    731  1.20      dbj 	p += sprintf(p,"%s: sc_datain=%d\n",sc->sc_dev.dv_xname,esc->sc_datain);
    732  1.20      dbj 	p += sprintf(p,"%s: sc_loaded=0x%08x\n",sc->sc_dev.dv_xname,esc->sc_loaded);
    733   1.3      dbj 
    734  1.20      dbj 	if (esc->sc_dmaaddr) {
    735  1.20      dbj 		p += sprintf(p,"%s: sc_dmaaddr=0x%08lx\n",sc->sc_dev.dv_xname,*esc->sc_dmaaddr);
    736  1.20      dbj 	} else {
    737  1.20      dbj 		p += sprintf(p,"%s: sc_dmaaddr=NULL\n",sc->sc_dev.dv_xname);
    738  1.20      dbj 	}
    739  1.20      dbj 	if (esc->sc_dmalen) {
    740  1.20      dbj 		p += sprintf(p,"%s: sc_dmalen=0x%08lx\n",sc->sc_dev.dv_xname,*esc->sc_dmalen);
    741  1.20      dbj 	} else {
    742  1.20      dbj 		p += sprintf(p,"%s: sc_dmalen=NULL\n",sc->sc_dev.dv_xname);
    743  1.20      dbj 	}
    744  1.20      dbj 	p += sprintf(p,"%s: sc_dmasize=0x%08x\n",sc->sc_dev.dv_xname,esc->sc_dmasize);
    745  1.19      dbj 
    746  1.20      dbj 	p += sprintf(p,"%s: sc_begin = 0x%08x, sc_begin_size = 0x%08x\n",
    747  1.20      dbj 			sc->sc_dev.dv_xname, esc->sc_begin, esc->sc_begin_size);
    748  1.20      dbj 	p += sprintf(p,"%s: sc_main = 0x%08x, sc_main_size = 0x%08x\n",
    749  1.20      dbj 			sc->sc_dev.dv_xname, esc->sc_main, esc->sc_main_size);
    750  1.19      dbj 	{
    751  1.19      dbj 		int i;
    752  1.19      dbj 		bus_dmamap_t map = esc->sc_main_dmamap;
    753  1.20      dbj 		p += sprintf(p,"%s: sc_main_dmamap. mapsize = 0x%08x, nsegs = %d\n",
    754  1.20      dbj 				sc->sc_dev.dv_xname, map->dm_mapsize, map->dm_nsegs);
    755  1.19      dbj 		for(i=0;i<map->dm_nsegs;i++) {
    756  1.20      dbj 			p += sprintf(p,"%s: map->dm_segs[%d]->ds_addr = 0x%08x, len = 0x%08x\n",
    757  1.20      dbj 			sc->sc_dev.dv_xname, i, map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len);
    758  1.19      dbj 		}
    759  1.19      dbj 	}
    760  1.20      dbj 	p += sprintf(p,"%s: sc_tail = 0x%08x, sc_tail_size = 0x%08x\n",
    761  1.20      dbj 			sc->sc_dev.dv_xname, esc->sc_tail, esc->sc_tail_size);
    762  1.19      dbj 	{
    763  1.19      dbj 		int i;
    764  1.19      dbj 		bus_dmamap_t map = esc->sc_tail_dmamap;
    765  1.20      dbj 		p += sprintf(p,"%s: sc_tail_dmamap. mapsize = 0x%08x, nsegs = %d\n",
    766  1.20      dbj 				sc->sc_dev.dv_xname, map->dm_mapsize, map->dm_nsegs);
    767  1.19      dbj 		for(i=0;i<map->dm_nsegs;i++) {
    768  1.20      dbj 			p += sprintf(p,"%s: map->dm_segs[%d]->ds_addr = 0x%08x, len = 0x%08x\n",
    769  1.20      dbj 			sc->sc_dev.dv_xname, i, map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len);
    770  1.19      dbj 		}
    771  1.19      dbj 	}
    772  1.20      dbj }
    773  1.20      dbj 
    774  1.20      dbj void
    775  1.20      dbj esp_dma_print(sc)
    776  1.20      dbj 	struct ncr53c9x_softc *sc;
    777  1.20      dbj {
    778  1.20      dbj 	esp_dma_store(sc);
    779  1.20      dbj 	printf("%s",esp_dma_dump);
    780  1.20      dbj }
    781  1.20      dbj #endif
    782  1.20      dbj 
    783  1.20      dbj void
    784  1.20      dbj esp_dma_go(sc)
    785  1.20      dbj 	struct ncr53c9x_softc *sc;
    786  1.20      dbj {
    787  1.20      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    788  1.20      dbj 
    789  1.20      dbj 	DPRINTF(("%s: esp_dma_go(datain = %d)\n",
    790  1.20      dbj 			sc->sc_dev.dv_xname, esc->sc_datain));
    791  1.20      dbj 
    792  1.20      dbj #ifdef ESP_DEBUG
    793  1.20      dbj 	if (esp_debug) esp_dma_print(sc);
    794  1.20      dbj 	else esp_dma_store(sc);
    795  1.19      dbj #endif
    796   1.4      dbj 
    797  1.20      dbj #ifdef ESP_DEBUG
    798  1.11      dbj 	{
    799  1.11      dbj 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    800  1.20      dbj 		DPRINTF(("%s: fifo size = %d, seq = 0x%x\n",
    801  1.20      dbj 				sc->sc_dev.dv_xname,
    802  1.20      dbj 				n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    803   1.4      dbj 	}
    804  1.11      dbj #endif
    805   1.4      dbj 
    806  1.20      dbj 	/* zero length dma transfers are boring, dmasize is probably -1 in those cases
    807  1.20      dbj 	 * because it was never set up by esp_dma_setup
    808  1.20      dbj 	 */
    809  1.20      dbj 	if (esc->sc_dmasize == 0) {
    810  1.20      dbj 		return;
    811  1.20      dbj 	}
    812  1.20      dbj 
    813  1.18      dbj #if defined(DIAGNOSTIC)
    814  1.18      dbj   if ((esc->sc_begin_size == 0) &&
    815  1.18      dbj 			(esc->sc_main_dmamap->dm_mapsize == 0) &&
    816  1.18      dbj 			(esc->sc_tail_dmamap->dm_mapsize == 0)) {
    817  1.20      dbj 		esp_dma_print(sc);
    818  1.18      dbj 		panic("%s: No DMA requested!",sc->sc_dev.dv_xname);
    819  1.18      dbj 	}
    820  1.18      dbj #endif
    821  1.18      dbj 
    822  1.18      dbj 	/* Stuff the fifo with the begin buffer */
    823  1.18      dbj 	if (esc->sc_datain) {
    824   1.4      dbj 		int i;
    825  1.18      dbj 		for(i=0;i<esc->sc_begin_size;i++) {
    826  1.18      dbj 			esc->sc_begin[i]=NCR_READ_REG(sc, NCR_FIFO);
    827   1.4      dbj 		}
    828   1.4      dbj 	} else {
    829   1.4      dbj 		int i;
    830  1.18      dbj 		for(i=0;i<esc->sc_begin_size;i++) {
    831  1.18      dbj 			NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_begin[i]);
    832   1.4      dbj 		}
    833  1.11      dbj 	}
    834   1.4      dbj 
    835  1.14      dbj 	/* if we are a dma write cycle, copy the end slop */
    836  1.14      dbj 	if (esc->sc_datain == 0) {
    837  1.18      dbj 		memcpy(esc->sc_tail,
    838  1.18      dbj 				(*esc->sc_dmaaddr+esc->sc_begin_size+esc->sc_main_size),
    839  1.18      dbj 				(esc->sc_dmasize-(esc->sc_begin_size+esc->sc_main_size)));
    840  1.14      dbj 	}
    841  1.17      dbj 
    842  1.14      dbj 	nextdma_start(&esc->sc_scsi_dma,
    843  1.14      dbj 			(esc->sc_datain ? DMACSR_READ : DMACSR_WRITE));
    844  1.12      dbj 
    845  1.14      dbj 	if (esc->sc_datain) {
    846  1.14      dbj 		NCR_WRITE_REG(sc, ESP_DCTL,
    847  1.14      dbj 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
    848   1.3      dbj 	} else {
    849  1.14      dbj 		NCR_WRITE_REG(sc, ESP_DCTL,
    850  1.14      dbj 				ESPDCTL_20MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
    851   1.3      dbj 	}
    852  1.18      dbj 
    853   1.1      dbj }
    854   1.1      dbj 
    855   1.1      dbj void
    856   1.1      dbj esp_dma_stop(sc)
    857   1.1      dbj 	struct ncr53c9x_softc *sc;
    858   1.1      dbj {
    859   1.1      dbj 	panic("Not yet implemented");
    860   1.1      dbj }
    861   1.1      dbj 
    862   1.1      dbj int
    863   1.1      dbj esp_dma_isactive(sc)
    864   1.1      dbj 	struct ncr53c9x_softc *sc;
    865   1.1      dbj {
    866   1.1      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    867  1.11      dbj 	int r = !nextdma_finished(&esc->sc_scsi_dma);
    868  1.11      dbj 	DPRINTF(("esp_dma_isactive = %d\n",r));
    869  1.11      dbj 	return(r);
    870   1.2      dbj }
    871   1.2      dbj 
    872   1.2      dbj /****************************************************************/
    873   1.2      dbj 
    874   1.2      dbj /* Internal dma callback routines */
    875   1.2      dbj bus_dmamap_t
    876   1.2      dbj esp_dmacb_continue(arg)
    877   1.2      dbj 	void *arg;
    878   1.2      dbj {
    879   1.2      dbj 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    880   1.2      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    881   1.2      dbj 
    882  1.18      dbj 	DPRINTF(("%s: dma continue\n",sc->sc_dev.dv_xname));
    883   1.4      dbj 
    884   1.2      dbj #ifdef DIAGNOSTIC
    885   1.2      dbj 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
    886   1.2      dbj 		panic("%s: map not loaded in dma continue callback, datain = %d",
    887   1.2      dbj 				sc->sc_dev.dv_xname,esc->sc_datain);
    888   1.2      dbj 	}
    889   1.2      dbj #endif
    890  1.18      dbj 
    891  1.18      dbj 	if ((!(esc->sc_loaded & ESP_LOADED_MAIN)) &&
    892  1.18      dbj 			(esc->sc_main_dmamap->dm_mapsize)) {
    893  1.18      dbj 			DPRINTF(("%s: Loading main map\n",sc->sc_dev.dv_xname));
    894  1.19      dbj #if 0
    895  1.18      dbj 			bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
    896  1.18      dbj 					0, esc->sc_main_dmamap->dm_mapsize,
    897  1.14      dbj 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    898  1.19      dbj #endif
    899  1.18      dbj 			esc->sc_loaded |= ESP_LOADED_MAIN;
    900  1.18      dbj 			return(esc->sc_main_dmamap);
    901  1.18      dbj 	}
    902  1.18      dbj 
    903  1.18      dbj 	if ((!(esc->sc_loaded & ESP_LOADED_TAIL)) &&
    904  1.18      dbj 			(esc->sc_tail_dmamap->dm_mapsize)) {
    905  1.18      dbj 			DPRINTF(("%s: Loading tail map\n",sc->sc_dev.dv_xname));
    906  1.19      dbj #if 0
    907  1.14      dbj 			bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
    908  1.14      dbj 					0, esc->sc_tail_dmamap->dm_mapsize,
    909  1.14      dbj 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    910  1.19      dbj #endif
    911  1.18      dbj 			esc->sc_loaded |= ESP_LOADED_TAIL;
    912  1.14      dbj 			return(esc->sc_tail_dmamap);
    913  1.10      dbj 	}
    914  1.18      dbj 
    915  1.18      dbj 	DPRINTF(("%s: not loading map\n",sc->sc_dev.dv_xname));
    916  1.18      dbj 	return(0);
    917   1.2      dbj }
    918   1.2      dbj 
    919  1.14      dbj 
    920   1.2      dbj void
    921   1.2      dbj esp_dmacb_completed(map, arg)
    922   1.2      dbj 	bus_dmamap_t map;
    923   1.2      dbj 	void *arg;
    924   1.2      dbj {
    925   1.2      dbj 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    926   1.2      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    927   1.2      dbj 
    928  1.20      dbj 	DPRINTF(("%s: dma completed\n",sc->sc_dev.dv_xname));
    929   1.4      dbj 
    930   1.2      dbj #ifdef DIAGNOSTIC
    931  1.14      dbj 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
    932  1.18      dbj 		panic("%s: invalid dma direction in completed callback, datain = %d",
    933  1.18      dbj 				sc->sc_dev.dv_xname,esc->sc_datain);
    934   1.2      dbj 	}
    935  1.18      dbj 	if ((map != esc->sc_main_dmamap) && (map != esc->sc_tail_dmamap)) {
    936  1.14      dbj 		panic("%s: unexpected completed map", sc->sc_dev.dv_xname);
    937   1.2      dbj 	}
    938   1.2      dbj #endif
    939   1.2      dbj 
    940  1.19      dbj #if 0
    941  1.14      dbj 	bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, map,
    942  1.14      dbj 			0, map->dm_mapsize,
    943   1.2      dbj 			(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
    944  1.19      dbj #endif
    945  1.13      dbj 
    946   1.2      dbj }
    947   1.2      dbj 
    948   1.2      dbj void
    949   1.2      dbj esp_dmacb_shutdown(arg)
    950   1.2      dbj 	void *arg;
    951   1.2      dbj {
    952   1.2      dbj 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
    953   1.2      dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    954   1.2      dbj 
    955  1.20      dbj 	DPRINTF(("%s: dma shutdown\n",sc->sc_dev.dv_xname));
    956   1.4      dbj 
    957  1.13      dbj 	/* Stuff the end slop into fifo */
    958   1.3      dbj 
    959  1.14      dbj #ifdef ESP_DEBUG
    960  1.14      dbj 	if (esp_debug) {
    961  1.14      dbj 
    962  1.13      dbj 		int n = NCR_READ_REG(sc, NCR_FFLAG);
    963  1.20      dbj 		DPRINTF(("%s: fifo size = %d, seq = 0x%x\n",
    964  1.20      dbj 				sc->sc_dev.dv_xname,n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
    965  1.12      dbj 
    966  1.20      dbj 		NCR_DMA(("%s:dmaintr: tcl=%d, tcm=%d, tch=%d\n",
    967  1.20      dbj 				sc->sc_dev.dv_xname,
    968  1.13      dbj 				NCR_READ_REG(sc, NCR_TCL),
    969  1.13      dbj 				NCR_READ_REG(sc, NCR_TCM),
    970  1.13      dbj 				(sc->sc_cfg2 & NCRCFG2_FE)
    971  1.13      dbj 				? NCR_READ_REG(sc, NCR_TCH) : 0));
    972  1.13      dbj 	}
    973  1.13      dbj #endif
    974  1.12      dbj 
    975  1.18      dbj 	/* First copy the tail dma buffer data for read transfers */
    976  1.18      dbj 	if (esc->sc_datain == 1) {
    977  1.18      dbj 		memcpy((*esc->sc_dmaaddr+esc->sc_begin_size+esc->sc_main_size),
    978  1.18      dbj 				esc->sc_tail,
    979  1.18      dbj 				(esc->sc_dmasize-(esc->sc_begin_size+esc->sc_main_size)));
    980   1.4      dbj 	}
    981  1.13      dbj 
    982  1.19      dbj 	*(esc->sc_dmaaddr) += esc->sc_dmasize;
    983  1.19      dbj 	*(esc->sc_dmalen)  -= esc->sc_dmasize;
    984  1.19      dbj 
    985  1.18      dbj #ifdef ESP_DEBUG
    986  1.18      dbj 	if (esp_debug) {
    987  1.18      dbj 		printf("%s: dma_shutdown: addr=0x%08lx,len=0x%08lx,size=0x%08lx\n",
    988  1.18      dbj 				sc->sc_dev.dv_xname,
    989  1.18      dbj 				*esc->sc_dmaaddr, *esc->sc_dmalen, esc->sc_dmasize);
    990  1.18      dbj 		esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
    991  1.18      dbj 		printf("%s: tail=0x%08lx,tailbuf=0x%08lx,tail_size=0x%08lx\n",
    992  1.18      dbj 				sc->sc_dev.dv_xname,
    993  1.18      dbj 				esc->sc_tail, &(esc->sc_tailbuf[0]), esc->sc_tail_size);
    994  1.18      dbj 		esp_hex_dump(&(esc->sc_tailbuf[0]),sizeof(esc->sc_tailbuf));
    995  1.13      dbj 	}
    996  1.11      dbj #endif
    997   1.3      dbj 
    998  1.18      dbj 	if (esc->sc_main_dmamap->dm_mapsize) {
    999  1.19      dbj 		bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap,
   1000  1.19      dbj 			0, esc->sc_main_dmamap->dm_mapsize,
   1001  1.19      dbj 				(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1002  1.18      dbj 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_main_dmamap);
   1003  1.18      dbj 	}
   1004  1.18      dbj 	esc->sc_main = 0;
   1005  1.18      dbj 	esc->sc_main_size = 0;
   1006  1.18      dbj 
   1007  1.18      dbj 	if (esc->sc_tail_dmamap->dm_mapsize) {
   1008  1.19      dbj 		bus_dmamap_sync(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap,
   1009  1.19      dbj 			0, esc->sc_tail_dmamap->dm_mapsize,
   1010  1.19      dbj 				(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1011  1.18      dbj 		bus_dmamap_unload(esc->sc_scsi_dma.nd_dmat, esc->sc_tail_dmamap);
   1012  1.18      dbj 	}
   1013  1.14      dbj 	esc->sc_tail = 0;
   1014  1.14      dbj 	esc->sc_tail_size = 0;
   1015  1.19      dbj 
   1016  1.19      dbj 	esc->sc_datain = -1;
   1017  1.19      dbj 	esc->sc_dmaaddr = 0;
   1018  1.19      dbj 	esc->sc_dmalen  = 0;
   1019  1.20      dbj 	esc->sc_dmasize = 0;
   1020  1.19      dbj 
   1021  1.19      dbj 	esc->sc_loaded = 0;
   1022  1.19      dbj 
   1023  1.19      dbj 	esc->sc_begin = 0;
   1024  1.19      dbj 	esc->sc_begin_size = 0;
   1025  1.20      dbj 
   1026  1.20      dbj #ifdef ESP_DEBUG
   1027  1.20      dbj 	if (esp_debug) {
   1028  1.20      dbj 		printf("  *intrstat = 0x%b\n",
   1029  1.20      dbj 				(*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),NEXT_INTR_BITS);
   1030  1.20      dbj 		printf("  *intrmask = 0x%b\n",
   1031  1.20      dbj 				(*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),NEXT_INTR_BITS);
   1032  1.20      dbj 	}
   1033  1.20      dbj #endif
   1034  1.18      dbj 
   1035   1.1      dbj }
   1036