Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.46
      1  1.45     lukem /*	$NetBSD: esp.c,v 1.46 2003/09/27 20:16:59 cl 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.45     lukem 
     84  1.45     lukem #include <sys/cdefs.h>
     85  1.45     lukem __KERNEL_RCSID(0, "$NetBSD: esp.c,v 1.46 2003/09/27 20:16:59 cl Exp $");
     86   1.1       dbj 
     87   1.1       dbj #include <sys/types.h>
     88   1.1       dbj #include <sys/param.h>
     89   1.1       dbj #include <sys/systm.h>
     90   1.1       dbj #include <sys/kernel.h>
     91   1.1       dbj #include <sys/errno.h>
     92   1.1       dbj #include <sys/ioctl.h>
     93   1.1       dbj #include <sys/device.h>
     94   1.1       dbj #include <sys/buf.h>
     95   1.1       dbj #include <sys/proc.h>
     96   1.1       dbj #include <sys/user.h>
     97   1.1       dbj #include <sys/queue.h>
     98   1.1       dbj 
     99  1.43   thorpej #include <uvm/uvm_extern.h>
    100  1.43   thorpej 
    101   1.1       dbj #include <dev/scsipi/scsi_all.h>
    102   1.1       dbj #include <dev/scsipi/scsipi_all.h>
    103   1.1       dbj #include <dev/scsipi/scsiconf.h>
    104   1.1       dbj #include <dev/scsipi/scsi_message.h>
    105   1.1       dbj 
    106   1.1       dbj #include <machine/bus.h>
    107   1.1       dbj #include <machine/autoconf.h>
    108   1.1       dbj #include <machine/cpu.h>
    109   1.1       dbj 
    110   1.1       dbj #include <dev/ic/ncr53c9xreg.h>
    111   1.1       dbj #include <dev/ic/ncr53c9xvar.h>
    112   1.1       dbj 
    113   1.1       dbj #include <next68k/next68k/isr.h>
    114   1.1       dbj 
    115  1.38   mycroft #include <next68k/dev/intiovar.h>
    116   1.1       dbj #include <next68k/dev/nextdmareg.h>
    117   1.1       dbj #include <next68k/dev/nextdmavar.h>
    118   1.1       dbj 
    119  1.38   mycroft #include <next68k/dev/espreg.h>
    120  1.38   mycroft #include <next68k/dev/espvar.h>
    121   1.1       dbj 
    122  1.20       dbj #ifdef DEBUG
    123  1.39   mycroft #undef ESP_DEBUG
    124   1.4       dbj #endif
    125   1.4       dbj 
    126   1.4       dbj #ifdef ESP_DEBUG
    127  1.10       dbj int esp_debug = 0;
    128  1.10       dbj #define DPRINTF(x) if (esp_debug) printf x;
    129  1.38   mycroft extern char *ndtracep;
    130  1.38   mycroft extern char ndtrace[];
    131  1.38   mycroft extern int ndtraceshow;
    132  1.38   mycroft #define NDTRACEIF(x) if (10 && ndtracep < (ndtrace + 8192)) do {x;} while (0)
    133   1.4       dbj #else
    134   1.4       dbj #define DPRINTF(x)
    135  1.38   mycroft #define NDTRACEIF(x)
    136   1.4       dbj #endif
    137  1.37  christos #define PRINTF(x) printf x;
    138   1.4       dbj 
    139   1.4       dbj 
    140   1.1       dbj void	espattach_intio	__P((struct device *, struct device *, void *));
    141   1.1       dbj int	espmatch_intio	__P((struct device *, struct cfdata *, void *));
    142   1.1       dbj 
    143   1.2       dbj /* DMA callbacks */
    144   1.2       dbj bus_dmamap_t esp_dmacb_continue __P((void *arg));
    145   1.2       dbj void esp_dmacb_completed __P((bus_dmamap_t map, void *arg));
    146   1.2       dbj void esp_dmacb_shutdown __P((void *arg));
    147   1.2       dbj 
    148  1.38   mycroft static void	findchannel_defer __P((struct device *));
    149  1.38   mycroft 
    150  1.20       dbj #ifdef ESP_DEBUG
    151  1.20       dbj char esp_dma_dump[5*1024] = "";
    152  1.20       dbj struct ncr53c9x_softc *esp_debug_sc = 0;
    153  1.20       dbj void esp_dma_store __P((struct ncr53c9x_softc *sc));
    154  1.20       dbj void esp_dma_print __P((struct ncr53c9x_softc *sc));
    155  1.22       dbj int esp_dma_nest = 0;
    156  1.20       dbj #endif
    157  1.20       dbj 
    158  1.20       dbj 
    159   1.1       dbj /* Linkup to the rest of the kernel */
    160  1.42   thorpej CFATTACH_DECL(esp, sizeof(struct esp_softc),
    161  1.42   thorpej     espmatch_intio, espattach_intio, NULL, NULL);
    162   1.1       dbj 
    163  1.38   mycroft static int attached = 0;
    164  1.38   mycroft 
    165   1.1       dbj /*
    166   1.1       dbj  * Functions and the switch for the MI code.
    167   1.1       dbj  */
    168   1.1       dbj u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    169   1.1       dbj void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    170   1.1       dbj int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    171   1.1       dbj void	esp_dma_reset __P((struct ncr53c9x_softc *));
    172   1.1       dbj int	esp_dma_intr __P((struct ncr53c9x_softc *));
    173   1.1       dbj int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    174   1.1       dbj 	    size_t *, int, size_t *));
    175   1.1       dbj void	esp_dma_go __P((struct ncr53c9x_softc *));
    176   1.1       dbj void	esp_dma_stop __P((struct ncr53c9x_softc *));
    177   1.1       dbj int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    178   1.1       dbj 
    179   1.1       dbj struct ncr53c9x_glue esp_glue = {
    180   1.1       dbj 	esp_read_reg,
    181   1.1       dbj 	esp_write_reg,
    182   1.1       dbj 	esp_dma_isintr,
    183   1.1       dbj 	esp_dma_reset,
    184   1.1       dbj 	esp_dma_intr,
    185   1.1       dbj 	esp_dma_setup,
    186   1.1       dbj 	esp_dma_go,
    187   1.1       dbj 	esp_dma_stop,
    188   1.1       dbj 	esp_dma_isactive,
    189   1.1       dbj 	NULL,			/* gl_clear_latched_intr */
    190   1.1       dbj };
    191   1.1       dbj 
    192  1.11       dbj #ifdef ESP_DEBUG
    193  1.11       dbj #define XCHR(x) "0123456789abcdef"[(x) & 0xf]
    194  1.11       dbj static void
    195  1.11       dbj esp_hex_dump(unsigned char *pkt, size_t len)
    196  1.11       dbj {
    197  1.11       dbj 	size_t i, j;
    198  1.11       dbj 
    199  1.31       dbj 	printf("00000000  ");
    200  1.11       dbj 	for(i=0; i<len; i++) {
    201  1.11       dbj 		printf("%c%c ", XCHR(pkt[i]>>4), XCHR(pkt[i]));
    202  1.24       dbj 		if ((i+1) % 16 == 8) {
    203  1.24       dbj 			printf(" ");
    204  1.24       dbj 		}
    205  1.11       dbj 		if ((i+1) % 16 == 0) {
    206  1.24       dbj 			printf(" %c", '|');
    207  1.24       dbj 			for(j=0; j<16; j++) {
    208  1.11       dbj 				printf("%c", pkt[i-15+j]>=32 && pkt[i-15+j]<127?pkt[i-15+j]:'.');
    209  1.24       dbj 			}
    210  1.24       dbj 			printf("%c\n%c%c%c%c%c%c%c%c  ", '|',
    211  1.24       dbj 					XCHR((i+1)>>28),XCHR((i+1)>>24),XCHR((i+1)>>20),XCHR((i+1)>>16),
    212  1.24       dbj 					XCHR((i+1)>>12), XCHR((i+1)>>8), XCHR((i+1)>>4), XCHR(i+1));
    213  1.11       dbj 		}
    214  1.11       dbj 	}
    215  1.11       dbj 	printf("\n");
    216  1.11       dbj }
    217  1.11       dbj #endif
    218  1.11       dbj 
    219   1.1       dbj int
    220   1.1       dbj espmatch_intio(parent, cf, aux)
    221   1.1       dbj 	struct device *parent;
    222   1.1       dbj 	struct cfdata *cf;
    223   1.1       dbj 	void *aux;
    224   1.1       dbj {
    225  1.38   mycroft 	struct intio_attach_args *ia = (struct intio_attach_args *)aux;
    226  1.38   mycroft 
    227  1.38   mycroft 	if (attached)
    228  1.38   mycroft 		return (0);
    229  1.38   mycroft 
    230  1.38   mycroft 	ia->ia_addr = (void *)NEXT_P_SCSI;
    231   1.1       dbj 
    232   1.3       dbj 	return(1);
    233   1.1       dbj }
    234   1.1       dbj 
    235  1.38   mycroft static void
    236  1.38   mycroft findchannel_defer(self)
    237  1.38   mycroft 	struct device *self;
    238  1.38   mycroft {
    239  1.38   mycroft 	struct esp_softc *esc = (void *)self;
    240  1.38   mycroft 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    241  1.38   mycroft 	int error;
    242  1.38   mycroft 
    243  1.38   mycroft 	if (!esc->sc_dma) {
    244  1.38   mycroft 		printf ("%s", sc->sc_dev.dv_xname);
    245  1.38   mycroft 		esc->sc_dma = nextdma_findchannel ("scsi");
    246  1.38   mycroft 		if (!esc->sc_dma)
    247  1.44       wiz 			panic ("%s: can't find DMA channel",
    248  1.38   mycroft 			       sc->sc_dev.dv_xname);
    249  1.38   mycroft 	}
    250  1.38   mycroft 
    251  1.38   mycroft 	nextdma_setconf (esc->sc_dma, shutdown_cb, &esp_dmacb_shutdown);
    252  1.38   mycroft 	nextdma_setconf (esc->sc_dma, continue_cb, &esp_dmacb_continue);
    253  1.38   mycroft 	nextdma_setconf (esc->sc_dma, completed_cb, &esp_dmacb_completed);
    254  1.38   mycroft 	nextdma_setconf (esc->sc_dma, cb_arg, sc);
    255  1.38   mycroft 
    256  1.38   mycroft 	error = bus_dmamap_create(esc->sc_dma->sc_dmat,
    257  1.43   thorpej 				  sc->sc_maxxfer,
    258  1.43   thorpej 				  sc->sc_maxxfer/PAGE_SIZE+1, sc->sc_maxxfer,
    259  1.38   mycroft 				  0, BUS_DMA_ALLOCNOW, &esc->sc_main_dmamap);
    260  1.38   mycroft 	if (error) {
    261  1.38   mycroft 		panic("%s: can't create main i/o DMA map, error = %d",
    262  1.38   mycroft 		      sc->sc_dev.dv_xname, error);
    263  1.38   mycroft 	}
    264  1.38   mycroft 
    265  1.38   mycroft 	error = bus_dmamap_create(esc->sc_dma->sc_dmat,
    266  1.38   mycroft 				  ESP_DMA_TAILBUFSIZE, 1, ESP_DMA_TAILBUFSIZE,
    267  1.38   mycroft 				  0, BUS_DMA_ALLOCNOW, &esc->sc_tail_dmamap);
    268  1.38   mycroft 	if (error) {
    269  1.38   mycroft 		panic("%s: can't create tail i/o DMA map, error = %d",
    270  1.38   mycroft 		      sc->sc_dev.dv_xname, error);
    271  1.38   mycroft 	}
    272  1.38   mycroft 
    273  1.38   mycroft #if 0
    274  1.44       wiz 	/* Turn on target selection using the `DMA' method */
    275  1.38   mycroft 	sc->sc_features |= NCR_F_DMASELECT;
    276  1.38   mycroft #endif
    277  1.38   mycroft 
    278  1.38   mycroft 	/* Do the common parts of attachment. */
    279  1.38   mycroft 	sc->sc_adapter.adapt_minphys = minphys;
    280  1.38   mycroft 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    281  1.38   mycroft 	ncr53c9x_attach(sc);
    282  1.38   mycroft 
    283  1.38   mycroft 	/* Establish interrupt channel */
    284  1.38   mycroft 	isrlink_autovec(ncr53c9x_intr, sc, NEXT_I_IPL(NEXT_I_SCSI), 0, NULL);
    285  1.38   mycroft 	INTR_ENABLE(NEXT_I_SCSI);
    286  1.38   mycroft 
    287  1.38   mycroft 	/* register interrupt stats */
    288  1.38   mycroft 	evcnt_attach_dynamic(&sc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    289  1.38   mycroft 			     sc->sc_dev.dv_xname, "intr");
    290  1.38   mycroft 
    291  1.44       wiz 	printf ("%s: using DMA channel %s\n", sc->sc_dev.dv_xname,
    292  1.38   mycroft 		esc->sc_dma->sc_dev.dv_xname);
    293  1.38   mycroft }
    294  1.38   mycroft 
    295   1.1       dbj void
    296   1.1       dbj espattach_intio(parent, self, aux)
    297   1.1       dbj 	struct device *parent, *self;
    298   1.1       dbj 	void *aux;
    299   1.1       dbj {
    300   1.1       dbj 	struct esp_softc *esc = (void *)self;
    301   1.1       dbj 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    302  1.38   mycroft 	struct intio_attach_args *ia = (struct intio_attach_args *)aux;
    303   1.1       dbj 
    304  1.20       dbj #ifdef ESP_DEBUG
    305  1.20       dbj 	esp_debug_sc = sc;
    306  1.20       dbj #endif
    307  1.20       dbj 
    308  1.38   mycroft 	esc->sc_bst = ia->ia_bst;
    309   1.1       dbj 	if (bus_space_map(esc->sc_bst, NEXT_P_SCSI,
    310   1.1       dbj 			ESP_DEVICE_SIZE, 0, &esc->sc_bsh)) {
    311  1.38   mycroft 		panic("\n%s: can't map ncr53c90 registers",
    312  1.38   mycroft 		      sc->sc_dev.dv_xname);
    313   1.1       dbj 	}
    314   1.1       dbj 
    315   1.1       dbj 	sc->sc_id = 7;
    316   1.1       dbj 	sc->sc_freq = 20;							/* Mhz */
    317   1.1       dbj 
    318   1.1       dbj 	/*
    319   1.1       dbj 	 * Set up glue for MI code early; we use some of it here.
    320   1.1       dbj 	 */
    321   1.1       dbj 	sc->sc_glue = &esp_glue;
    322   1.1       dbj 
    323   1.1       dbj 	/*
    324   1.1       dbj 	 * XXX More of this should be in ncr53c9x_attach(), but
    325   1.1       dbj 	 * XXX should we really poke around the chip that much in
    326   1.1       dbj 	 * XXX the MI code?  Think about this more...
    327   1.1       dbj 	 */
    328   1.1       dbj 
    329   1.1       dbj 	/*
    330   1.1       dbj 	 * It is necessary to try to load the 2nd config register here,
    331   1.1       dbj 	 * to find out what rev the esp chip is, else the ncr53c9x_reset
    332   1.1       dbj 	 * will not set up the defaults correctly.
    333   1.1       dbj 	 */
    334   1.1       dbj 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    335   1.1       dbj 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_RPE;
    336   1.1       dbj 	sc->sc_cfg3 = NCRCFG3_CDB;
    337   1.1       dbj 	NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    338   1.1       dbj 
    339   1.1       dbj 	if ((NCR_READ_REG(sc, NCR_CFG2) & ~NCRCFG2_RSVD) !=
    340   1.1       dbj 	    (NCRCFG2_SCSI2 | NCRCFG2_RPE)) {
    341   1.1       dbj 		sc->sc_rev = NCR_VARIANT_ESP100;
    342   1.1       dbj 	} else {
    343   1.1       dbj 		sc->sc_cfg2 = NCRCFG2_SCSI2;
    344   1.1       dbj 		NCR_WRITE_REG(sc, NCR_CFG2, sc->sc_cfg2);
    345   1.1       dbj 		sc->sc_cfg3 = 0;
    346   1.1       dbj 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    347   1.1       dbj 		sc->sc_cfg3 = (NCRCFG3_CDB | NCRCFG3_FCLK);
    348   1.1       dbj 		NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    349   1.1       dbj 		if (NCR_READ_REG(sc, NCR_CFG3) !=
    350   1.1       dbj 		    (NCRCFG3_CDB | NCRCFG3_FCLK)) {
    351   1.1       dbj 			sc->sc_rev = NCR_VARIANT_ESP100A;
    352   1.1       dbj 		} else {
    353   1.1       dbj 			/* NCRCFG2_FE enables > 64K transfers */
    354   1.1       dbj 			sc->sc_cfg2 |= NCRCFG2_FE;
    355   1.1       dbj 			sc->sc_cfg3 = 0;
    356   1.1       dbj 			NCR_WRITE_REG(sc, NCR_CFG3, sc->sc_cfg3);
    357   1.1       dbj 			sc->sc_rev = NCR_VARIANT_ESP200;
    358   1.1       dbj 		}
    359   1.1       dbj 	}
    360   1.1       dbj 
    361   1.1       dbj 	/*
    362   1.1       dbj 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    363   1.1       dbj 	 * XXX but it appears to have some dependency on what sort
    364   1.1       dbj 	 * XXX of DMA we're hooked up to, etc.
    365   1.1       dbj 	 */
    366   1.1       dbj 
    367   1.1       dbj 	/*
    368   1.1       dbj 	 * This is the value used to start sync negotiations
    369   1.1       dbj 	 * Note that the NCR register "SYNCTP" is programmed
    370   1.1       dbj 	 * in "clocks per byte", and has a minimum value of 4.
    371   1.1       dbj 	 * The SCSI period used in negotiation is one-fourth
    372   1.1       dbj 	 * of the time (in nanoseconds) needed to transfer one byte.
    373   1.1       dbj 	 * Since the chip's clock is given in MHz, we have the following
    374   1.1       dbj 	 * formula: 4 * period = (1000 / freq) * 4
    375   1.1       dbj 	 */
    376  1.39   mycroft 	sc->sc_minsync = /* 1000 / sc->sc_freq */ 0;
    377   1.1       dbj 
    378   1.1       dbj 	/*
    379   1.1       dbj 	 * Alas, we must now modify the value a bit, because it's
    380   1.1       dbj 	 * only valid when can switch on FASTCLK and FASTSCSI bits
    381   1.1       dbj 	 * in config register 3...
    382   1.1       dbj 	 */
    383   1.1       dbj 	switch (sc->sc_rev) {
    384   1.1       dbj 	case NCR_VARIANT_ESP100:
    385   1.1       dbj 		sc->sc_maxxfer = 64 * 1024;
    386   1.1       dbj 		sc->sc_minsync = 0;	/* No synch on old chip? */
    387   1.1       dbj 		break;
    388   1.1       dbj 
    389   1.1       dbj 	case NCR_VARIANT_ESP100A:
    390   1.1       dbj 		sc->sc_maxxfer = 64 * 1024;
    391   1.1       dbj 		/* Min clocks/byte is 5 */
    392  1.39   mycroft 		sc->sc_minsync = /* ncr53c9x_cpb2stp(sc, 5) */ 0;
    393   1.1       dbj 		break;
    394   1.1       dbj 
    395   1.1       dbj 	case NCR_VARIANT_ESP200:
    396   1.1       dbj 		sc->sc_maxxfer = 16 * 1024 * 1024;
    397   1.1       dbj 		/* XXX - do actually set FAST* bits */
    398   1.1       dbj 		break;
    399   1.1       dbj 	}
    400   1.1       dbj 
    401   1.3       dbj 	/* @@@ Some ESP_DCTL bits probably need setting */
    402   1.3       dbj 	NCR_WRITE_REG(sc, ESP_DCTL,
    403  1.37  christos 			ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_RESET);
    404   1.3       dbj 	DELAY(10);
    405  1.22       dbj 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    406  1.37  christos 	NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB);
    407   1.3       dbj 	DELAY(10);
    408  1.22       dbj 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    409   1.3       dbj 
    410  1.38   mycroft 	esc->sc_dma = nextdma_findchannel ("scsi");
    411  1.38   mycroft 	if (esc->sc_dma) {
    412  1.38   mycroft 		findchannel_defer (self);
    413  1.38   mycroft 	} else {
    414  1.38   mycroft 		printf ("\n");
    415  1.38   mycroft 		config_defer (self, findchannel_defer);
    416   1.3       dbj 	}
    417   1.1       dbj 
    418  1.38   mycroft 	attached = 1;
    419   1.1       dbj }
    420   1.1       dbj 
    421   1.1       dbj /*
    422   1.1       dbj  * Glue functions.
    423   1.1       dbj  */
    424   1.1       dbj 
    425   1.1       dbj u_char
    426   1.1       dbj esp_read_reg(sc, reg)
    427   1.1       dbj 	struct ncr53c9x_softc *sc;
    428   1.1       dbj 	int reg;
    429   1.1       dbj {
    430   1.1       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    431   1.1       dbj 
    432   1.1       dbj 	return(bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg));
    433   1.1       dbj }
    434   1.1       dbj 
    435   1.1       dbj void
    436   1.1       dbj esp_write_reg(sc, reg, val)
    437   1.1       dbj 	struct ncr53c9x_softc *sc;
    438   1.1       dbj 	int reg;
    439   1.1       dbj 	u_char val;
    440   1.1       dbj {
    441   1.1       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    442   1.1       dbj 
    443   1.1       dbj 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg, val);
    444   1.1       dbj }
    445   1.1       dbj 
    446  1.37  christos volatile u_int32_t save1;
    447  1.37  christos 
    448  1.37  christos #define xADDR 0x0211a000
    449  1.37  christos int doze __P((volatile int));
    450  1.37  christos int
    451  1.37  christos doze(c)
    452  1.37  christos 	volatile int c;
    453  1.37  christos {
    454  1.37  christos /* 	static int tmp1; */
    455  1.37  christos 	u_int32_t tmp1;
    456  1.37  christos 	volatile u_int8_t tmp2;
    457  1.37  christos 	volatile u_int8_t *reg = (volatile u_int8_t *)IIOV(xADDR);
    458  1.37  christos 	if (c > 244) return (0);
    459  1.37  christos 	if (c == 0) return (0);
    460  1.37  christos /* 		((*(volatile u_long *)IIOV(NEXT_P_INTRMASK))&=(~NEXT_I_BIT(x))) */
    461  1.37  christos 	(*reg) = 0;
    462  1.37  christos 	(*reg) = 0;
    463  1.37  christos 	do {
    464  1.37  christos 		save1 = (*reg);
    465  1.37  christos 		tmp2 = *(reg + 3);
    466  1.37  christos 		tmp1 = tmp2;
    467  1.37  christos 	} while (tmp1 <= c);
    468  1.37  christos 	return (0);
    469  1.37  christos }
    470  1.37  christos 
    471   1.1       dbj int
    472   1.1       dbj esp_dma_isintr(sc)
    473   1.1       dbj 	struct ncr53c9x_softc *sc;
    474   1.1       dbj {
    475   1.4       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    476  1.37  christos 	if (INTR_OCCURRED(NEXT_I_SCSI)) {
    477  1.38   mycroft 		NDTRACEIF (*ndtracep++ = 'i');
    478  1.37  christos 		NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB | (esc->sc_datain ? ESPDCTL_DMARD : 0));
    479  1.37  christos 		return (1);
    480  1.37  christos 	} else {
    481  1.37  christos 		return (0);
    482  1.37  christos 	}
    483  1.37  christos }
    484  1.37  christos 
    485  1.38   mycroft #define nd_bsr4(reg) bus_space_read_4(nsc->sc_bst, nsc->sc_bsh, (reg))
    486  1.38   mycroft #define nd_bsw4(reg,val) bus_space_write_4(nsc->sc_bst, nsc->sc_bsh, (reg), (val))
    487  1.37  christos int
    488  1.37  christos esp_dma_intr(sc)
    489  1.37  christos 	struct ncr53c9x_softc *sc;
    490  1.37  christos {
    491  1.37  christos 	struct esp_softc *esc = (struct esp_softc *)sc;
    492  1.38   mycroft 	struct nextdma_softc *nsc = esc->sc_dma;
    493  1.38   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
    494   1.4       dbj 
    495   1.4       dbj 	int r = (INTR_OCCURRED(NEXT_I_SCSI));
    496  1.37  christos 	int flushcount;
    497  1.37  christos 	r = 1;
    498   1.4       dbj 
    499  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'I');
    500   1.4       dbj 	if (r) {
    501  1.37  christos 		/* printf ("esp_dma_isintr start\n"); */
    502  1.20       dbj 		{
    503  1.37  christos 			int s = spldma();
    504  1.38   mycroft 			void *ndmap = stat->nd_map;
    505  1.38   mycroft 			int ndidx = stat->nd_idx;
    506  1.37  christos 			splx(s);
    507  1.20       dbj 
    508  1.23       dbj 			flushcount = 0;
    509  1.23       dbj 
    510  1.22       dbj #ifdef ESP_DEBUG
    511  1.37  christos /* 			esp_dma_nest++; */
    512  1.28        tv 
    513  1.28        tv 			if (esp_debug) {
    514  1.28        tv 				char sbuf[256];
    515  1.28        tv 
    516  1.28        tv 				bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
    517  1.28        tv 						 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    518  1.28        tv 				printf("esp_dma_isintr = 0x%s\n", sbuf);
    519  1.28        tv 			}
    520  1.22       dbj #endif
    521  1.22       dbj 
    522  1.38   mycroft 			while (!nextdma_finished(nsc)) { /* esp_dma_isactive(sc)) { */
    523  1.38   mycroft 				NDTRACEIF (*ndtracep++ = 'w');
    524  1.38   mycroft 				NDTRACEIF (
    525  1.38   mycroft 					sprintf (ndtracep, "f%dm%dl%dw", NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF,
    526  1.37  christos 						 NCR_READ_REG((sc), NCR_TCM), NCR_READ_REG((sc), NCR_TCL));
    527  1.38   mycroft 					ndtracep += strlen (ndtracep);
    528  1.38   mycroft 					);
    529  1.37  christos 				if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)
    530  1.37  christos 					flushcount=5;
    531  1.37  christos 				NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD |
    532  1.37  christos 					      (esc->sc_datain ? ESPDCTL_DMARD : 0));
    533  1.37  christos 
    534  1.37  christos 				s = spldma();
    535  1.38   mycroft 				while (ndmap == stat->nd_map && ndidx == stat->nd_idx &&
    536  1.38   mycroft 				       !(nd_bsr4 (DD_CSR) & 0x08000000) &&
    537  1.37  christos 				       ++flushcount < 5) {
    538  1.37  christos 					splx(s);
    539  1.38   mycroft 					NDTRACEIF (*ndtracep++ = 'F');
    540  1.37  christos 					NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_FLUSH |
    541  1.37  christos 						      ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD |
    542  1.37  christos 						      (esc->sc_datain ? ESPDCTL_DMARD : 0));
    543  1.37  christos 					doze(0x32);
    544  1.20       dbj 					NCR_WRITE_REG(sc, ESP_DCTL,
    545  1.37  christos 						      ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD |
    546  1.37  christos 						      (esc->sc_datain ? ESPDCTL_DMARD : 0));
    547  1.37  christos 					doze(0x32);
    548  1.37  christos 					s = spldma();
    549  1.37  christos 				}
    550  1.38   mycroft 				NDTRACEIF (*ndtracep++ = '0' + flushcount);
    551  1.37  christos 				if (flushcount > 4) {
    552  1.37  christos 					int next;
    553  1.37  christos 					int onext = 0;
    554  1.37  christos 					splx(s);
    555  1.37  christos 					DPRINTF (("DMA reset\n"));
    556  1.38   mycroft 					while (((next = nd_bsr4 (DD_NEXT)) !=
    557  1.38   mycroft 						(nd_bsr4 (DD_LIMIT) & 0x7FFFFFFF)) &&
    558  1.37  christos 					       onext != next) {
    559  1.37  christos 						onext = next;
    560  1.37  christos 						DELAY(50);
    561  1.37  christos 					}
    562  1.38   mycroft 					NDTRACEIF (*ndtracep++ = 'R');
    563  1.37  christos 					NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB);
    564  1.38   mycroft 					NDTRACEIF (
    565  1.38   mycroft 						sprintf (ndtracep, "ff:%d tcm:%d tcl:%d ",
    566  1.37  christos 							 NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF,
    567  1.37  christos 							 NCR_READ_REG((sc), NCR_TCM), NCR_READ_REG((sc), NCR_TCL));
    568  1.38   mycroft 						ndtracep += strlen (ndtracep);
    569  1.38   mycroft 						);
    570  1.37  christos 					s = spldma();
    571  1.38   mycroft 					nextdma_reset (nsc);
    572  1.37  christos 					splx(s);
    573  1.37  christos 					goto out;
    574  1.20       dbj 				}
    575  1.37  christos 				splx(s);
    576  1.20       dbj 
    577  1.23       dbj #ifdef DIAGNOSTIC
    578  1.37  christos 				if (flushcount > 4) {
    579  1.38   mycroft 					NDTRACEIF (*ndtracep++ = '+');
    580  1.37  christos 					printf("%s: unexpected flushcount %d on %s\n",sc->sc_dev.dv_xname,
    581  1.37  christos 					       flushcount, esc->sc_datain ? "read" : "write");
    582  1.37  christos 				}
    583  1.23       dbj #endif
    584  1.23       dbj 
    585  1.38   mycroft 				if (!nextdma_finished(nsc)) { /* esp_dma_isactive(sc)) { */
    586  1.38   mycroft 					NDTRACEIF (*ndtracep++ = '1');
    587  1.16       dbj 				}
    588  1.37  christos 				flushcount = 0;
    589  1.37  christos 				s = spldma();
    590  1.38   mycroft 				ndmap = stat->nd_map;
    591  1.38   mycroft 				ndidx = stat->nd_idx;
    592  1.37  christos 				splx(s);
    593  1.37  christos 
    594  1.16       dbj 			}
    595  1.46        cl 		out: ;
    596  1.20       dbj 
    597  1.22       dbj #ifdef ESP_DEBUG
    598  1.37  christos /* 			esp_dma_nest--; */
    599  1.22       dbj #endif
    600  1.22       dbj 
    601  1.13       dbj 		}
    602  1.13       dbj 
    603  1.37  christos 		doze (0x32);
    604  1.37  christos 		NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB | (esc->sc_datain ? ESPDCTL_DMARD : 0));
    605  1.38   mycroft 		NDTRACEIF (*ndtracep++ = 'b');
    606  1.37  christos 
    607  1.37  christos 		while (esc->sc_datain != -1) DELAY(50);
    608  1.37  christos 
    609  1.37  christos 		if (esc->sc_dmaaddr) {
    610  1.37  christos 			bus_size_t xfer_len = 0;
    611  1.37  christos 			int resid;
    612  1.37  christos 
    613  1.37  christos 			NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB);
    614  1.38   mycroft 			if (stat->nd_exception == 0) {
    615  1.37  christos 				resid = NCR_READ_REG((sc), NCR_TCL) + (NCR_READ_REG((sc), NCR_TCM) << 8);
    616  1.37  christos 				if (resid) {
    617  1.37  christos 					resid += (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF);
    618  1.38   mycroft #ifdef ESP_DEBUG
    619  1.37  christos 					if (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)
    620  1.37  christos 						if ((NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF) != 16 || NCR_READ_REG((sc), NCR_TCL) != 240)
    621  1.38   mycroft 							ndtraceshow++;
    622  1.38   mycroft #endif
    623  1.37  christos 				}
    624  1.37  christos 				xfer_len = esc->sc_dmasize - resid;
    625  1.37  christos 			} else {
    626  1.37  christos /*static*/ void	ncr53c9x_abort(struct ncr53c9x_softc *, struct ncr53c9x_ecb *);
    627  1.37  christos #define ncr53c9x_sched_msgout(m) \
    628  1.37  christos 	do {							\
    629  1.37  christos 		NCR_MISC(("ncr53c9x_sched_msgout %x %d", m, __LINE__));	\
    630  1.37  christos 		NCRCMD(sc, NCRCMD_SETATN);			\
    631  1.37  christos 		sc->sc_flags |= NCR_ATN;			\
    632  1.37  christos 		sc->sc_msgpriq |= (m);				\
    633  1.37  christos 	} while (0)
    634  1.37  christos 				int i;
    635  1.38   mycroft 				xfer_len = 0;
    636  1.38   mycroft 				if (esc->sc_begin)
    637  1.38   mycroft 					xfer_len += esc->sc_begin_size;
    638  1.38   mycroft 				if (esc->sc_main_dmamap)
    639  1.38   mycroft 					xfer_len += esc->sc_main_dmamap->dm_xfer_len;
    640  1.38   mycroft 				if (esc->sc_tail_dmamap)
    641  1.38   mycroft 					xfer_len += esc->sc_tail_dmamap->dm_xfer_len;
    642  1.37  christos 				resid = 0;
    643  1.37  christos 				printf ("X\n");
    644  1.37  christos 				for (i = 0; i < 16; i++) {
    645  1.37  christos 					NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_FLUSH |
    646  1.37  christos 						      ESPDCTL_16MHZ | ESPDCTL_INTENB |
    647  1.37  christos 						      (esc->sc_datain ? ESPDCTL_DMARD : 0));
    648  1.37  christos 					NCR_WRITE_REG(sc, ESP_DCTL,
    649  1.37  christos 						      ESPDCTL_16MHZ | ESPDCTL_INTENB |
    650  1.37  christos 						      (esc->sc_datain ? ESPDCTL_DMARD : 0));
    651  1.37  christos 				}
    652  1.37  christos #if 0
    653  1.37  christos 				printf ("ff:%02x tcm:%d tcl:%d esp_dstat:%02x stat:%02x step: %02x intr:%02x new stat:%02X\n",
    654  1.37  christos 					NCR_READ_REG(sc, NCR_FFLAG),
    655  1.37  christos 					NCR_READ_REG((sc), NCR_TCM), NCR_READ_REG((sc), NCR_TCL),
    656  1.37  christos 					NCR_READ_REG(sc, ESP_DSTAT),
    657  1.37  christos 					sc->sc_espstat, sc->sc_espstep,
    658  1.37  christos 					sc->sc_espintr, NCR_READ_REG(sc, NCR_STAT));
    659  1.37  christos 				printf ("sc->sc_state: %x sc->sc_phase: %x sc->sc_espstep:%x sc->sc_prevphase:%x sc->sc_flags:%x\n",
    660  1.37  christos 					sc->sc_state, sc->sc_phase, sc->sc_espstep, sc->sc_prevphase, sc->sc_flags);
    661  1.37  christos #endif
    662  1.37  christos 				/* sc->sc_flags &= ~NCR_ICCS; */
    663  1.37  christos 				sc->sc_nexus->flags |= ECB_ABORT;
    664  1.37  christos 				if (sc->sc_phase == MESSAGE_IN_PHASE) {
    665  1.37  christos 					/* ncr53c9x_sched_msgout(SEND_ABORT); */
    666  1.37  christos 					ncr53c9x_abort(sc, sc->sc_nexus);
    667  1.37  christos 				} else if (sc->sc_phase != STATUS_PHASE) {
    668  1.37  christos 					printf ("ATTENTION!!!  not message/status phase: %d\n", sc->sc_phase);
    669  1.37  christos 				}
    670  1.37  christos 			}
    671  1.37  christos 
    672  1.38   mycroft 			NDTRACEIF (
    673  1.38   mycroft 				sprintf (ndtracep, "f%dm%dl%ds%dx%dr%dS", NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF, NCR_READ_REG((sc), NCR_TCM),
    674  1.37  christos 					 NCR_READ_REG((sc), NCR_TCL), esc->sc_dmasize, (int)xfer_len, resid);
    675  1.38   mycroft 				ndtracep += strlen (ndtracep);
    676  1.38   mycroft 				);
    677  1.20       dbj 
    678  1.37  christos 			*(esc->sc_dmaaddr) += xfer_len;
    679  1.37  christos 			*(esc->sc_dmalen)  -= xfer_len;
    680  1.37  christos 			esc->sc_dmaaddr = 0;
    681  1.37  christos 			esc->sc_dmalen  = 0;
    682  1.37  christos 			esc->sc_dmasize = 0;
    683  1.13       dbj 		}
    684  1.37  christos 
    685  1.38   mycroft 		NDTRACEIF (*ndtracep++ = 'B');
    686  1.37  christos 		sc->sc_espstat = NCR_READ_REG(sc, NCR_STAT) | (sc->sc_espstat & NCRSTAT_INT);
    687  1.37  christos 
    688  1.22       dbj 		DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    689  1.37  christos 		/* printf ("esp_dma_isintr DONE\n"); */
    690  1.13       dbj 
    691   1.4       dbj 	}
    692   1.4       dbj 
    693   1.4       dbj 	return (r);
    694   1.1       dbj }
    695   1.1       dbj 
    696   1.1       dbj void
    697   1.1       dbj esp_dma_reset(sc)
    698   1.1       dbj 	struct ncr53c9x_softc *sc;
    699   1.1       dbj {
    700   1.1       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    701   1.3       dbj 
    702  1.44       wiz 	DPRINTF(("esp DMA reset\n"));
    703  1.13       dbj 
    704  1.13       dbj #ifdef ESP_DEBUG
    705  1.13       dbj 	if (esp_debug) {
    706  1.28        tv 		char sbuf[256];
    707  1.28        tv 
    708  1.28        tv 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
    709  1.28        tv 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    710  1.28        tv 		printf("  *intrstat = 0x%s\n", sbuf);
    711  1.28        tv 
    712  1.28        tv 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),
    713  1.28        tv 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
    714  1.28        tv 		printf("  *intrmask = 0x%s\n", sbuf);
    715  1.13       dbj 	}
    716  1.13       dbj #endif
    717  1.13       dbj 
    718  1.38   mycroft #if 0
    719  1.13       dbj 	/* Clear the DMAMOD bit in the DCTL register: */
    720  1.18       dbj 	NCR_WRITE_REG(sc, ESP_DCTL,
    721  1.37  christos 			ESPDCTL_16MHZ | ESPDCTL_INTENB);
    722  1.22       dbj 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
    723  1.38   mycroft #endif
    724  1.13       dbj 
    725  1.38   mycroft 	nextdma_reset(esc->sc_dma);
    726  1.38   mycroft 	nextdma_init(esc->sc_dma);
    727   1.4       dbj 
    728  1.18       dbj 	esc->sc_datain = -1;
    729  1.18       dbj 	esc->sc_dmaaddr = 0;
    730  1.18       dbj 	esc->sc_dmalen  = 0;
    731  1.20       dbj 	esc->sc_dmasize = 0;
    732  1.18       dbj 
    733  1.18       dbj 	esc->sc_loaded = 0;
    734  1.18       dbj 
    735  1.18       dbj 	esc->sc_begin = 0;
    736  1.18       dbj 	esc->sc_begin_size = 0;
    737  1.13       dbj 
    738  1.18       dbj 	if (esc->sc_main_dmamap->dm_mapsize) {
    739  1.38   mycroft 		bus_dmamap_unload(esc->sc_dma->sc_dmat, esc->sc_main_dmamap);
    740  1.13       dbj 	}
    741  1.18       dbj 	esc->sc_main = 0;
    742  1.18       dbj 	esc->sc_main_size = 0;
    743  1.13       dbj 
    744  1.18       dbj 	if (esc->sc_tail_dmamap->dm_mapsize) {
    745  1.38   mycroft 		bus_dmamap_unload(esc->sc_dma->sc_dmat, esc->sc_tail_dmamap);
    746  1.18       dbj 	}
    747  1.18       dbj 	esc->sc_tail = 0;
    748  1.18       dbj 	esc->sc_tail_size = 0;
    749   1.1       dbj }
    750   1.1       dbj 
    751  1.19       dbj /* it appears that:
    752  1.19       dbj  * addr and len arguments to this need to be kept up to date
    753  1.19       dbj  * with the status of the transfter.
    754  1.19       dbj  * the dmasize of this is the actual length of the transfer
    755  1.19       dbj  * request, which is guaranteed to be less than maxxfer.
    756  1.19       dbj  * (len may be > maxxfer)
    757  1.19       dbj  */
    758  1.19       dbj 
    759   1.1       dbj int
    760   1.1       dbj esp_dma_setup(sc, addr, len, datain, dmasize)
    761   1.1       dbj 	struct ncr53c9x_softc *sc;
    762   1.1       dbj 	caddr_t *addr;
    763   1.1       dbj 	size_t *len;
    764   1.1       dbj 	int datain;
    765   1.1       dbj 	size_t *dmasize;
    766   1.1       dbj {
    767   1.1       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
    768   1.2       dbj 
    769  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'h');
    770  1.11       dbj #ifdef DIAGNOSTIC
    771  1.20       dbj #ifdef ESP_DEBUG
    772  1.11       dbj 	/* if this is a read DMA, pre-fill the buffer with 0xdeadbeef
    773  1.11       dbj 	 * to identify bogus reads
    774  1.11       dbj 	 */
    775  1.11       dbj 	if (datain) {
    776  1.14       dbj 		int *v = (int *)(*addr);
    777  1.11       dbj 		int i;
    778  1.14       dbj 		for(i=0;i<((*len)/4);i++) v[i] = 0xdeadbeef;
    779  1.18       dbj 		v = (int *)(&(esc->sc_tailbuf[0]));
    780  1.37  christos 		for(i=0;i<((sizeof(esc->sc_tailbuf)/4));i++) v[i] = 0xdeafbeef;
    781  1.23       dbj 	} else {
    782  1.23       dbj 		int *v;
    783  1.23       dbj 		int i;
    784  1.23       dbj 		v = (int *)(&(esc->sc_tailbuf[0]));
    785  1.23       dbj 		for(i=0;i<((sizeof(esc->sc_tailbuf)/4));i++) v[i] = 0xfeeb1eed;
    786  1.11       dbj 	}
    787  1.20       dbj #endif
    788  1.11       dbj #endif
    789  1.11       dbj 
    790  1.35       chs 	DPRINTF(("esp_dma_setup(%p,0x%08x,0x%08x)\n",*addr,*len,*dmasize));
    791  1.11       dbj 
    792  1.24       dbj #if 0
    793  1.12       dbj #ifdef DIAGNOSTIC /* @@@ this is ok sometimes. verify that we handle it ok
    794  1.37  christos 		   * and then remove this check
    795  1.37  christos 		   */
    796  1.14       dbj 	if (*len != *dmasize) {
    797  1.23       dbj 		panic("esp dmalen 0x%lx != size 0x%lx",*len,*dmasize);
    798  1.11       dbj 	}
    799  1.11       dbj #endif
    800  1.24       dbj #endif
    801   1.4       dbj 
    802   1.2       dbj #ifdef DIAGNOSTIC
    803   1.3       dbj 	if ((esc->sc_datain != -1) ||
    804  1.18       dbj 			(esc->sc_main_dmamap->dm_mapsize != 0) ||
    805  1.20       dbj 			(esc->sc_tail_dmamap->dm_mapsize != 0) ||
    806  1.20       dbj 			(esc->sc_dmasize != 0)) {
    807  1.40    provos 		panic("%s: map already loaded in esp_dma_setup"
    808  1.35       chs 				"\tdatain = %d\n\tmain_mapsize=%ld\n\tail_mapsize=%ld\n\tdmasize = %d",
    809  1.18       dbj 				sc->sc_dev.dv_xname, esc->sc_datain,
    810  1.20       dbj 				esc->sc_main_dmamap->dm_mapsize,
    811  1.20       dbj 				esc->sc_tail_dmamap->dm_mapsize,
    812  1.20       dbj 				esc->sc_dmasize);
    813   1.2       dbj 	}
    814   1.2       dbj #endif
    815   1.2       dbj 
    816  1.44       wiz 	/* we are sometimes asked to DMA zero  bytes, that's easy */
    817  1.24       dbj 	if (*dmasize <= 0) {
    818  1.20       dbj 		return(0);
    819  1.20       dbj 	}
    820  1.20       dbj 
    821  1.37  christos 	if (*dmasize > ESP_MAX_DMASIZE)
    822  1.37  christos 		*dmasize = ESP_MAX_DMASIZE;
    823  1.37  christos 
    824  1.14       dbj 	/* Save these in case we have to abort DMA */
    825  1.14       dbj 	esc->sc_datain   = datain;
    826  1.14       dbj 	esc->sc_dmaaddr  = addr;
    827  1.14       dbj 	esc->sc_dmalen   = len;
    828  1.14       dbj 	esc->sc_dmasize  = *dmasize;
    829  1.14       dbj 
    830  1.18       dbj 	esc->sc_loaded = 0;
    831  1.18       dbj 
    832  1.23       dbj #define DMA_SCSI_ALIGNMENT 16
    833  1.23       dbj #define DMA_SCSI_ALIGN(type, addr)	\
    834  1.23       dbj 	((type)(((unsigned)(addr)+DMA_SCSI_ALIGNMENT-1) \
    835  1.23       dbj 		&~(DMA_SCSI_ALIGNMENT-1)))
    836  1.23       dbj #define DMA_SCSI_ALIGNED(addr) \
    837  1.23       dbj 	(((unsigned)(addr)&(DMA_SCSI_ALIGNMENT-1))==0)
    838  1.23       dbj 
    839   1.2       dbj 	{
    840  1.18       dbj 		size_t slop_bgn_size; /* # bytes to be fifo'd at beginning */
    841  1.18       dbj 		size_t slop_end_size; /* # bytes to be transferred in tail buffer */
    842  1.18       dbj 
    843   1.3       dbj 		{
    844  1.13       dbj 			u_long bgn = (u_long)(*esc->sc_dmaaddr);
    845  1.13       dbj 			u_long end = (u_long)(*esc->sc_dmaaddr+esc->sc_dmasize);
    846   1.3       dbj 
    847  1.23       dbj 			slop_bgn_size = DMA_SCSI_ALIGNMENT-(bgn % DMA_SCSI_ALIGNMENT);
    848  1.23       dbj 			if (slop_bgn_size == DMA_SCSI_ALIGNMENT) slop_bgn_size = 0;
    849  1.19       dbj 			slop_end_size = (end % DMA_ENDALIGNMENT);
    850   1.3       dbj 		}
    851   1.3       dbj 
    852  1.23       dbj 		/* Force a minimum slop end size. This ensures that write
    853  1.23       dbj 		 * requests will overrun, as required to get completion interrupts.
    854  1.23       dbj 		 * In addition, since the tail buffer is guaranteed to be mapped
    855  1.44       wiz 		 * in a single DMA segment, the overrun won't accidentally
    856  1.23       dbj 		 * end up in its own segment.
    857  1.23       dbj 		 */
    858  1.23       dbj 		if (!esc->sc_datain) {
    859  1.24       dbj #if 0
    860  1.23       dbj 			slop_end_size += ESP_DMA_MAXTAIL;
    861  1.24       dbj #else
    862  1.24       dbj 			slop_end_size += 0x10;
    863  1.24       dbj #endif
    864  1.23       dbj 		}
    865  1.23       dbj 
    866  1.10       dbj 		/* Check to make sure we haven't counted extra slop
    867  1.44       wiz 		 * as would happen for a very short DMA buffer, also
    868  1.14       dbj 		 * for short buffers, just stuff the entire thing in the tail
    869  1.14       dbj 		 */
    870  1.18       dbj 		if ((slop_bgn_size+slop_end_size >= esc->sc_dmasize)
    871  1.20       dbj #if 0
    872  1.18       dbj 				|| (esc->sc_dmasize <= ESP_DMA_MAXTAIL)
    873  1.18       dbj #endif
    874  1.18       dbj 				)
    875  1.18       dbj 		{
    876  1.14       dbj  			slop_bgn_size = 0;
    877  1.14       dbj 			slop_end_size = esc->sc_dmasize;
    878  1.18       dbj 		}
    879  1.14       dbj 
    880  1.18       dbj 		/* initialize the fifo buffer */
    881  1.18       dbj 		if (slop_bgn_size) {
    882  1.18       dbj 			esc->sc_begin = *esc->sc_dmaaddr;
    883  1.18       dbj 			esc->sc_begin_size = slop_bgn_size;
    884  1.18       dbj 		} else {
    885  1.18       dbj 			esc->sc_begin = 0;
    886  1.18       dbj 			esc->sc_begin_size = 0;
    887  1.18       dbj 		}
    888  1.18       dbj 
    889  1.37  christos #if 01
    890  1.18       dbj 		/* Load the normal DMA map */
    891  1.18       dbj 		{
    892  1.18       dbj 			esc->sc_main      = *esc->sc_dmaaddr+slop_bgn_size;
    893  1.18       dbj 			esc->sc_main_size = (esc->sc_dmasize)-(slop_end_size+slop_bgn_size);
    894  1.18       dbj 
    895  1.18       dbj 			if (esc->sc_main_size) {
    896  1.18       dbj 				int error;
    897  1.37  christos 
    898  1.37  christos 				if (!esc->sc_datain || DMA_ENDALIGNED(esc->sc_main_size + slop_end_size)) {
    899  1.37  christos 					KASSERT(DMA_SCSI_ALIGNMENT == DMA_ENDALIGNMENT);
    900  1.37  christos 					KASSERT(DMA_BEGINALIGNMENT == DMA_ENDALIGNMENT);
    901  1.37  christos 					esc->sc_main_size += slop_end_size;
    902  1.37  christos 					slop_end_size = 0;
    903  1.37  christos 					if (!esc->sc_datain) {
    904  1.37  christos 						esc->sc_main_size = DMA_ENDALIGN(caddr_t,esc->sc_main+esc->sc_main_size)-esc->sc_main;
    905  1.37  christos 					}
    906  1.37  christos 				}
    907  1.37  christos 
    908  1.38   mycroft 				error = bus_dmamap_load(esc->sc_dma->sc_dmat,
    909  1.18       dbj 						esc->sc_main_dmamap,
    910  1.18       dbj 						esc->sc_main, esc->sc_main_size,
    911  1.18       dbj 						NULL, BUS_DMA_NOWAIT);
    912  1.18       dbj 				if (error) {
    913  1.34       dbj #ifdef ESP_DEBUG
    914  1.35       chs 					printf("%s: esc->sc_main_dmamap->_dm_size = %ld\n",
    915  1.34       dbj 							sc->sc_dev.dv_xname,esc->sc_main_dmamap->_dm_size);
    916  1.34       dbj 					printf("%s: esc->sc_main_dmamap->_dm_segcnt = %d\n",
    917  1.34       dbj 							sc->sc_dev.dv_xname,esc->sc_main_dmamap->_dm_segcnt);
    918  1.35       chs 					printf("%s: esc->sc_main_dmamap->_dm_maxsegsz = %ld\n",
    919  1.34       dbj 							sc->sc_dev.dv_xname,esc->sc_main_dmamap->_dm_maxsegsz);
    920  1.35       chs 					printf("%s: esc->sc_main_dmamap->_dm_boundary = %ld\n",
    921  1.34       dbj 							sc->sc_dev.dv_xname,esc->sc_main_dmamap->_dm_boundary);
    922  1.34       dbj 					esp_dma_print(sc);
    923  1.34       dbj #endif
    924  1.44       wiz 					panic("%s: can't load main DMA map. error = %d, addr=%p, size=0x%08x",
    925  1.18       dbj 							sc->sc_dev.dv_xname, error,esc->sc_main,esc->sc_main_size);
    926  1.18       dbj 				}
    927  1.44       wiz 				if (!esc->sc_datain) { /* patch the DMA map for write overrun */
    928  1.37  christos 					esc->sc_main_dmamap->dm_mapsize += ESP_DMA_OVERRUN;
    929  1.37  christos 					esc->sc_main_dmamap->dm_segs[esc->sc_main_dmamap->dm_nsegs - 1].ds_len +=
    930  1.37  christos 						ESP_DMA_OVERRUN;
    931  1.37  christos 				}
    932  1.23       dbj #if 0
    933  1.38   mycroft 				bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_main_dmamap,
    934  1.19       dbj 						0, esc->sc_main_dmamap->dm_mapsize,
    935  1.19       dbj 						(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    936  1.34       dbj 				esc->sc_main_dmamap->dm_xfer_len = 0;
    937  1.23       dbj #endif
    938  1.18       dbj 			} else {
    939  1.18       dbj 				esc->sc_main = 0;
    940  1.18       dbj 			}
    941  1.14       dbj 		}
    942   1.3       dbj 
    943  1.18       dbj 		/* Load the tail DMA map */
    944  1.18       dbj 		if (slop_end_size) {
    945  1.18       dbj 			esc->sc_tail      = DMA_ENDALIGN(caddr_t,esc->sc_tailbuf+slop_end_size)-slop_end_size;
    946  1.18       dbj 			/* If the beginning of the tail is not correctly aligned,
    947  1.18       dbj 			 * we have no choice but to align the start, which might then unalign the end.
    948  1.18       dbj 			 */
    949  1.23       dbj 			esc->sc_tail      = DMA_SCSI_ALIGN(caddr_t,esc->sc_tail);
    950  1.18       dbj 			/* So therefore, we change the tail size to be end aligned again. */
    951  1.18       dbj 			esc->sc_tail_size = DMA_ENDALIGN(caddr_t,esc->sc_tail+slop_end_size)-esc->sc_tail;
    952  1.19       dbj 
    953  1.44       wiz 			/* @@@ next DMA overrun lossage */
    954  1.20       dbj 			if (!esc->sc_datain) {
    955  1.21       dbj 				esc->sc_tail_size += ESP_DMA_OVERRUN;
    956  1.20       dbj 			}
    957  1.20       dbj 
    958  1.18       dbj 			{
    959  1.18       dbj 				int error;
    960  1.38   mycroft 				error = bus_dmamap_load(esc->sc_dma->sc_dmat,
    961  1.18       dbj 						esc->sc_tail_dmamap,
    962  1.18       dbj 						esc->sc_tail, esc->sc_tail_size,
    963  1.18       dbj 						NULL, BUS_DMA_NOWAIT);
    964  1.18       dbj 				if (error) {
    965  1.44       wiz 					panic("%s: can't load tail DMA map. error = %d, addr=%p, size=0x%08x",
    966  1.18       dbj 							sc->sc_dev.dv_xname, error,esc->sc_tail,esc->sc_tail_size);
    967  1.18       dbj 				}
    968  1.23       dbj #if 0
    969  1.38   mycroft 				bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_tail_dmamap,
    970  1.19       dbj 						0, esc->sc_tail_dmamap->dm_mapsize,
    971  1.19       dbj 						(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
    972  1.34       dbj 				esc->sc_tail_dmamap->dm_xfer_len = 0;
    973  1.23       dbj #endif
    974   1.3       dbj 			}
    975   1.3       dbj 		}
    976  1.37  christos #else
    977  1.37  christos 
    978  1.37  christos 		esc->sc_begin = *esc->sc_dmaaddr;
    979  1.37  christos 		slop_bgn_size = DMA_SCSI_ALIGNMENT-((ulong)esc->sc_begin % DMA_SCSI_ALIGNMENT);
    980  1.37  christos 		if (slop_bgn_size == DMA_SCSI_ALIGNMENT) slop_bgn_size = 0;
    981  1.37  christos 		slop_end_size = esc->sc_dmasize - slop_bgn_size;
    982  1.37  christos 
    983  1.37  christos 		if (slop_bgn_size < esc->sc_dmasize) {
    984  1.37  christos 			int error;
    985  1.37  christos 
    986  1.37  christos 			esc->sc_tail = 0;
    987  1.37  christos 			esc->sc_tail_size = 0;
    988  1.37  christos 
    989  1.37  christos 			esc->sc_begin_size = slop_bgn_size;
    990  1.37  christos 			esc->sc_main = *esc->sc_dmaaddr+slop_bgn_size;
    991  1.37  christos 			esc->sc_main_size = DMA_ENDALIGN(caddr_t,esc->sc_main+esc->sc_dmasize-slop_bgn_size)-esc->sc_main;
    992  1.37  christos 
    993  1.37  christos 			if (!esc->sc_datain) {
    994  1.37  christos 				esc->sc_main_size += ESP_DMA_OVERRUN;
    995  1.37  christos 			}
    996  1.38   mycroft 			error = bus_dmamap_load(esc->sc_dma->sc_dmat,
    997  1.37  christos 						esc->sc_main_dmamap,
    998  1.37  christos 						esc->sc_main, esc->sc_main_size,
    999  1.37  christos 						NULL, BUS_DMA_NOWAIT);
   1000  1.37  christos 			if (error) {
   1001  1.44       wiz 				panic("%s: can't load main DMA map. error = %d, addr=%p, size=0x%08x",
   1002  1.37  christos 				      sc->sc_dev.dv_xname, error,esc->sc_main,esc->sc_main_size);
   1003  1.37  christos 			}
   1004  1.37  christos 		} else {
   1005  1.37  christos 			esc->sc_begin = 0;
   1006  1.37  christos 			esc->sc_begin_size = 0;
   1007  1.37  christos 			esc->sc_main = 0;
   1008  1.37  christos 			esc->sc_main_size = 0;
   1009  1.37  christos 
   1010  1.37  christos #if 0
   1011  1.37  christos 			esc->sc_tail      = DMA_ENDALIGN(caddr_t,esc->sc_tailbuf+slop_bgn_size)-slop_bgn_size;
   1012  1.37  christos 			/* If the beginning of the tail is not correctly aligned,
   1013  1.37  christos 			 * we have no choice but to align the start, which might then unalign the end.
   1014  1.37  christos 			 */
   1015  1.37  christos #endif
   1016  1.37  christos 			esc->sc_tail      = DMA_SCSI_ALIGN(caddr_t,esc->sc_tailbuf);
   1017  1.37  christos 			/* So therefore, we change the tail size to be end aligned again. */
   1018  1.37  christos 			esc->sc_tail_size = DMA_ENDALIGN(caddr_t,esc->sc_tail+esc->sc_dmasize)-esc->sc_tail;
   1019  1.37  christos 
   1020  1.44       wiz 			/* @@@ next DMA overrun lossage */
   1021  1.37  christos 			if (!esc->sc_datain) {
   1022  1.37  christos 				esc->sc_tail_size += ESP_DMA_OVERRUN;
   1023  1.37  christos 			}
   1024  1.37  christos 
   1025  1.37  christos 			{
   1026  1.37  christos 				int error;
   1027  1.38   mycroft 				error = bus_dmamap_load(esc->sc_dma->sc_dmat,
   1028  1.37  christos 						esc->sc_tail_dmamap,
   1029  1.37  christos 						esc->sc_tail, esc->sc_tail_size,
   1030  1.37  christos 						NULL, BUS_DMA_NOWAIT);
   1031  1.37  christos 				if (error) {
   1032  1.44       wiz 					panic("%s: can't load tail DMA map. error = %d, addr=%p, size=0x%08x",
   1033  1.37  christos 							sc->sc_dev.dv_xname, error,esc->sc_tail,esc->sc_tail_size);
   1034  1.37  christos 				}
   1035  1.37  christos 			}
   1036  1.37  christos 		}
   1037  1.37  christos #endif
   1038  1.37  christos 
   1039  1.37  christos 		DPRINTF(("%s: setup: %8p %d %8p %d %8p %d %8p %d\n", sc->sc_dev.dv_xname,
   1040  1.37  christos 			 *esc->sc_dmaaddr, esc->sc_dmasize, esc->sc_begin,
   1041  1.37  christos 			 esc->sc_begin_size, esc->sc_main, esc->sc_main_size, esc->sc_tail,
   1042  1.37  christos 			 esc->sc_tail_size));
   1043   1.2       dbj 	}
   1044   1.2       dbj 
   1045   1.1       dbj 	return (0);
   1046   1.1       dbj }
   1047   1.1       dbj 
   1048  1.20       dbj #ifdef ESP_DEBUG
   1049  1.20       dbj /* For debugging */
   1050   1.1       dbj void
   1051  1.20       dbj esp_dma_store(sc)
   1052   1.1       dbj 	struct ncr53c9x_softc *sc;
   1053   1.1       dbj {
   1054   1.1       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1055  1.20       dbj 	char *p = &esp_dma_dump[0];
   1056  1.20       dbj 
   1057  1.20       dbj 	p += sprintf(p,"%s: sc_datain=%d\n",sc->sc_dev.dv_xname,esc->sc_datain);
   1058  1.20       dbj 	p += sprintf(p,"%s: sc_loaded=0x%08x\n",sc->sc_dev.dv_xname,esc->sc_loaded);
   1059   1.3       dbj 
   1060  1.20       dbj 	if (esc->sc_dmaaddr) {
   1061  1.35       chs 		p += sprintf(p,"%s: sc_dmaaddr=%p\n",sc->sc_dev.dv_xname,*esc->sc_dmaaddr);
   1062  1.20       dbj 	} else {
   1063  1.20       dbj 		p += sprintf(p,"%s: sc_dmaaddr=NULL\n",sc->sc_dev.dv_xname);
   1064  1.20       dbj 	}
   1065  1.20       dbj 	if (esc->sc_dmalen) {
   1066  1.35       chs 		p += sprintf(p,"%s: sc_dmalen=0x%08x\n",sc->sc_dev.dv_xname,*esc->sc_dmalen);
   1067  1.20       dbj 	} else {
   1068  1.20       dbj 		p += sprintf(p,"%s: sc_dmalen=NULL\n",sc->sc_dev.dv_xname);
   1069  1.20       dbj 	}
   1070  1.20       dbj 	p += sprintf(p,"%s: sc_dmasize=0x%08x\n",sc->sc_dev.dv_xname,esc->sc_dmasize);
   1071  1.19       dbj 
   1072  1.35       chs 	p += sprintf(p,"%s: sc_begin = %p, sc_begin_size = 0x%08x\n",
   1073  1.20       dbj 			sc->sc_dev.dv_xname, esc->sc_begin, esc->sc_begin_size);
   1074  1.35       chs 	p += sprintf(p,"%s: sc_main = %p, sc_main_size = 0x%08x\n",
   1075  1.20       dbj 			sc->sc_dev.dv_xname, esc->sc_main, esc->sc_main_size);
   1076  1.37  christos 	/* if (esc->sc_main) */ {
   1077  1.19       dbj 		int i;
   1078  1.19       dbj 		bus_dmamap_t map = esc->sc_main_dmamap;
   1079  1.35       chs 		p += sprintf(p,"%s: sc_main_dmamap. mapsize = 0x%08lx, nsegs = %d\n",
   1080  1.20       dbj 				sc->sc_dev.dv_xname, map->dm_mapsize, map->dm_nsegs);
   1081  1.19       dbj 		for(i=0;i<map->dm_nsegs;i++) {
   1082  1.35       chs 			p += sprintf(p,"%s: map->dm_segs[%d].ds_addr = 0x%08lx, len = 0x%08lx\n",
   1083  1.20       dbj 			sc->sc_dev.dv_xname, i, map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len);
   1084  1.19       dbj 		}
   1085  1.19       dbj 	}
   1086  1.35       chs 	p += sprintf(p,"%s: sc_tail = %p, sc_tail_size = 0x%08x\n",
   1087  1.20       dbj 			sc->sc_dev.dv_xname, esc->sc_tail, esc->sc_tail_size);
   1088  1.37  christos 	/* if (esc->sc_tail) */ {
   1089  1.19       dbj 		int i;
   1090  1.19       dbj 		bus_dmamap_t map = esc->sc_tail_dmamap;
   1091  1.35       chs 		p += sprintf(p,"%s: sc_tail_dmamap. mapsize = 0x%08lx, nsegs = %d\n",
   1092  1.20       dbj 				sc->sc_dev.dv_xname, map->dm_mapsize, map->dm_nsegs);
   1093  1.19       dbj 		for(i=0;i<map->dm_nsegs;i++) {
   1094  1.35       chs 			p += sprintf(p,"%s: map->dm_segs[%d].ds_addr = 0x%08lx, len = 0x%08lx\n",
   1095  1.20       dbj 			sc->sc_dev.dv_xname, i, map->dm_segs[i].ds_addr, map->dm_segs[i].ds_len);
   1096  1.19       dbj 		}
   1097  1.19       dbj 	}
   1098  1.20       dbj }
   1099  1.20       dbj 
   1100  1.20       dbj void
   1101  1.20       dbj esp_dma_print(sc)
   1102  1.20       dbj 	struct ncr53c9x_softc *sc;
   1103  1.20       dbj {
   1104  1.20       dbj 	esp_dma_store(sc);
   1105  1.20       dbj 	printf("%s",esp_dma_dump);
   1106  1.20       dbj }
   1107  1.20       dbj #endif
   1108  1.20       dbj 
   1109  1.20       dbj void
   1110  1.20       dbj esp_dma_go(sc)
   1111  1.20       dbj 	struct ncr53c9x_softc *sc;
   1112  1.20       dbj {
   1113  1.20       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1114  1.38   mycroft 	struct nextdma_softc *nsc = esc->sc_dma;
   1115  1.38   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
   1116  1.37  christos /* 	int s = spldma(); */
   1117  1.37  christos 
   1118  1.38   mycroft #ifdef ESP_DEBUG
   1119  1.38   mycroft 	if (ndtracep != ndtrace) {
   1120  1.38   mycroft 		if (ndtraceshow) {
   1121  1.38   mycroft 			*ndtracep = '\0';
   1122  1.38   mycroft 			printf ("esp ndtrace: %s\n", ndtrace);
   1123  1.38   mycroft 			ndtraceshow = 0;
   1124  1.37  christos 		} else {
   1125  1.37  christos 			DPRINTF (("X"));
   1126  1.37  christos 		}
   1127  1.38   mycroft 		ndtracep = ndtrace;
   1128  1.37  christos 	}
   1129  1.38   mycroft #endif
   1130  1.20       dbj 
   1131  1.20       dbj 	DPRINTF(("%s: esp_dma_go(datain = %d)\n",
   1132  1.20       dbj 			sc->sc_dev.dv_xname, esc->sc_datain));
   1133  1.20       dbj 
   1134  1.20       dbj #ifdef ESP_DEBUG
   1135  1.20       dbj 	if (esp_debug) esp_dma_print(sc);
   1136  1.20       dbj 	else esp_dma_store(sc);
   1137  1.19       dbj #endif
   1138   1.4       dbj 
   1139  1.20       dbj #ifdef ESP_DEBUG
   1140  1.11       dbj 	{
   1141  1.11       dbj 		int n = NCR_READ_REG(sc, NCR_FFLAG);
   1142  1.20       dbj 		DPRINTF(("%s: fifo size = %d, seq = 0x%x\n",
   1143  1.20       dbj 				sc->sc_dev.dv_xname,
   1144  1.20       dbj 				n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
   1145   1.4       dbj 	}
   1146  1.11       dbj #endif
   1147   1.4       dbj 
   1148  1.44       wiz 	/* zero length DMA transfers are boring */
   1149  1.20       dbj 	if (esc->sc_dmasize == 0) {
   1150  1.37  christos /* 		splx(s); */
   1151  1.20       dbj 		return;
   1152  1.20       dbj 	}
   1153  1.20       dbj 
   1154  1.18       dbj #if defined(DIAGNOSTIC)
   1155  1.18       dbj   if ((esc->sc_begin_size == 0) &&
   1156  1.18       dbj 			(esc->sc_main_dmamap->dm_mapsize == 0) &&
   1157  1.18       dbj 			(esc->sc_tail_dmamap->dm_mapsize == 0)) {
   1158  1.38   mycroft #ifdef ESP_DEBUG
   1159  1.20       dbj 		esp_dma_print(sc);
   1160  1.38   mycroft #endif
   1161  1.18       dbj 		panic("%s: No DMA requested!",sc->sc_dev.dv_xname);
   1162  1.18       dbj 	}
   1163  1.18       dbj #endif
   1164  1.18       dbj 
   1165  1.18       dbj 	/* Stuff the fifo with the begin buffer */
   1166  1.18       dbj 	if (esc->sc_datain) {
   1167   1.4       dbj 		int i;
   1168  1.23       dbj 		DPRINTF(("%s: FIFO read of %d bytes:",
   1169  1.23       dbj 				sc->sc_dev.dv_xname,esc->sc_begin_size));
   1170  1.18       dbj 		for(i=0;i<esc->sc_begin_size;i++) {
   1171  1.24       dbj 			esc->sc_begin[i]=NCR_READ_REG(sc, NCR_FIFO);
   1172  1.24       dbj 			DPRINTF((" %02x",esc->sc_begin[i]&0xff));
   1173   1.4       dbj 		}
   1174  1.23       dbj 		DPRINTF(("\n"));
   1175   1.4       dbj 	} else {
   1176   1.4       dbj 		int i;
   1177  1.23       dbj 		DPRINTF(("%s: FIFO write of %d bytes:",
   1178  1.23       dbj 				sc->sc_dev.dv_xname,esc->sc_begin_size));
   1179  1.18       dbj 		for(i=0;i<esc->sc_begin_size;i++) {
   1180  1.18       dbj 			NCR_WRITE_REG(sc, NCR_FIFO, esc->sc_begin[i]);
   1181  1.24       dbj 			DPRINTF((" %02x",esc->sc_begin[i]&0xff));
   1182   1.4       dbj 		}
   1183  1.23       dbj 		DPRINTF(("\n"));
   1184  1.11       dbj 	}
   1185   1.4       dbj 
   1186  1.23       dbj 	if (esc->sc_main_dmamap->dm_mapsize) {
   1187  1.38   mycroft 		bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_main_dmamap,
   1188  1.23       dbj 				0, esc->sc_main_dmamap->dm_mapsize,
   1189  1.23       dbj 				(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1190  1.34       dbj 		esc->sc_main_dmamap->dm_xfer_len = 0;
   1191  1.23       dbj 	}
   1192  1.23       dbj 
   1193  1.23       dbj 	if (esc->sc_tail_dmamap->dm_mapsize) {
   1194  1.44       wiz 		/* if we are a DMA write cycle, copy the end slop */
   1195  1.37  christos 		if (!esc->sc_datain) {
   1196  1.37  christos 			memcpy(esc->sc_tail, *esc->sc_dmaaddr+esc->sc_begin_size+esc->sc_main_size,
   1197  1.37  christos 			       esc->sc_dmasize-(esc->sc_begin_size+esc->sc_main_size));
   1198  1.37  christos 		}
   1199  1.38   mycroft 		bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_tail_dmamap,
   1200  1.23       dbj 				0, esc->sc_tail_dmamap->dm_mapsize,
   1201  1.23       dbj 				(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1202  1.34       dbj 		esc->sc_tail_dmamap->dm_xfer_len = 0;
   1203  1.23       dbj 	}
   1204  1.23       dbj 
   1205  1.38   mycroft 	stat->nd_exception = 0;
   1206  1.38   mycroft 	nextdma_start(nsc, (esc->sc_datain ? DMACSR_SETREAD : DMACSR_SETWRITE));
   1207  1.12       dbj 
   1208  1.14       dbj 	if (esc->sc_datain) {
   1209  1.14       dbj 		NCR_WRITE_REG(sc, ESP_DCTL,
   1210  1.37  christos 				ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD | ESPDCTL_DMARD);
   1211   1.3       dbj 	} else {
   1212  1.14       dbj 		NCR_WRITE_REG(sc, ESP_DCTL,
   1213  1.37  christos 				ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMAMOD);
   1214   1.3       dbj 	}
   1215  1.22       dbj 	DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
   1216  1.37  christos 
   1217  1.38   mycroft 	NDTRACEIF (if (esc->sc_begin_size) { *ndtracep++ = '1'; *ndtracep++ = 'A' + esc->sc_begin_size; });
   1218  1.38   mycroft 	NDTRACEIF (if (esc->sc_main_size) { *ndtracep++ = '2'; *ndtracep++ = '0' + esc->sc_main_dmamap->dm_nsegs; });
   1219  1.38   mycroft 	NDTRACEIF (if (esc->sc_tail_size) { *ndtracep++ = '3'; *ndtracep++ = 'A' + esc->sc_tail_size; });
   1220  1.37  christos 
   1221  1.37  christos /* 	splx(s); */
   1222   1.1       dbj }
   1223   1.1       dbj 
   1224   1.1       dbj void
   1225   1.1       dbj esp_dma_stop(sc)
   1226   1.1       dbj 	struct ncr53c9x_softc *sc;
   1227   1.1       dbj {
   1228  1.34       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1229  1.38   mycroft 	nextdma_print(esc->sc_dma);
   1230  1.38   mycroft #ifdef ESP_DEBUG
   1231  1.34       dbj 	esp_dma_print(sc);
   1232  1.38   mycroft #endif
   1233  1.37  christos #if 1
   1234  1.40    provos 	panic("%s: stop not yet implemented",sc->sc_dev.dv_xname);
   1235  1.37  christos #endif
   1236   1.1       dbj }
   1237   1.1       dbj 
   1238   1.1       dbj int
   1239   1.1       dbj esp_dma_isactive(sc)
   1240   1.1       dbj 	struct ncr53c9x_softc *sc;
   1241   1.1       dbj {
   1242   1.1       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1243  1.38   mycroft 	int r = (esc->sc_dmaaddr != NULL);   /* !nextdma_finished(esc->sc_dma); */
   1244  1.11       dbj 	DPRINTF(("esp_dma_isactive = %d\n",r));
   1245  1.11       dbj 	return(r);
   1246   1.2       dbj }
   1247   1.2       dbj 
   1248   1.2       dbj /****************************************************************/
   1249   1.2       dbj 
   1250  1.37  christos int esp_dma_int __P((void *));
   1251  1.37  christos int esp_dma_int(arg)
   1252  1.37  christos 	void *arg;
   1253  1.37  christos {
   1254  1.38   mycroft 	void nextdma_rotate __P((struct nextdma_softc *));
   1255  1.38   mycroft 	void nextdma_setup_curr_regs __P((struct nextdma_softc *));
   1256  1.38   mycroft 	void nextdma_setup_cont_regs __P((struct nextdma_softc *));
   1257  1.37  christos 
   1258  1.37  christos 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   1259  1.37  christos 	struct esp_softc *esc = (struct esp_softc *)sc;
   1260  1.38   mycroft 	struct nextdma_softc *nsc = esc->sc_dma;
   1261  1.38   mycroft 	struct nextdma_status *stat = &nsc->sc_stat;
   1262  1.37  christos 	unsigned int state;
   1263  1.37  christos 
   1264  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'E');
   1265  1.37  christos 
   1266  1.38   mycroft 	state = nd_bsr4 (DD_CSR);
   1267  1.37  christos 
   1268  1.37  christos #if 1
   1269  1.38   mycroft 	NDTRACEIF (
   1270  1.38   mycroft 		if (state & DMACSR_COMPLETE) *ndtracep++ = 'c';
   1271  1.38   mycroft 		if (state & DMACSR_ENABLE) *ndtracep++ = 'e';
   1272  1.38   mycroft 		if (state & DMACSR_BUSEXC) *ndtracep++ = 'b';
   1273  1.38   mycroft 		if (state & DMACSR_READ) *ndtracep++ = 'r';
   1274  1.38   mycroft 		if (state & DMACSR_SUPDATE) *ndtracep++ = 's';
   1275  1.38   mycroft 		);
   1276  1.37  christos 
   1277  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'E');
   1278  1.37  christos 
   1279  1.38   mycroft #ifdef ESP_DEBUG
   1280  1.38   mycroft 	if (0) if ((state & DMACSR_BUSEXC) && (state & DMACSR_ENABLE)) ndtraceshow++;
   1281  1.38   mycroft 	if (0) if ((state & DMACSR_SUPDATE)) ndtraceshow++;
   1282  1.38   mycroft #endif
   1283  1.37  christos #endif
   1284  1.37  christos 
   1285  1.38   mycroft 	if ((stat->nd_exception == 0) && (state & DMACSR_COMPLETE) && (state & DMACSR_ENABLE)) {
   1286  1.38   mycroft 		stat->nd_map->dm_xfer_len += stat->nd_map->dm_segs[stat->nd_idx].ds_len;
   1287  1.38   mycroft 	}
   1288  1.37  christos 
   1289  1.38   mycroft 	if ((stat->nd_idx+1) == stat->nd_map->dm_nsegs) {
   1290  1.38   mycroft 		if (nsc->sc_conf.nd_completed_cb)
   1291  1.38   mycroft 			(*nsc->sc_conf.nd_completed_cb)(stat->nd_map, nsc->sc_conf.nd_cb_arg);
   1292  1.37  christos 	}
   1293  1.38   mycroft 	nextdma_rotate(nsc);
   1294  1.37  christos 
   1295  1.37  christos 	if ((state & DMACSR_COMPLETE) && (state & DMACSR_ENABLE)) {
   1296  1.37  christos #if 0
   1297  1.38   mycroft 		int l = nd_bsr4 (DD_LIMIT) & 0x7FFFFFFF;
   1298  1.38   mycroft 		int s = nd_bsr4 (DD_STOP);
   1299  1.37  christos #endif
   1300  1.38   mycroft /* 		nextdma_setup_cont_regs(nsc); */
   1301  1.38   mycroft 		if (stat->nd_map_cont) {
   1302  1.38   mycroft 			nd_bsw4 (DD_START, stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr);
   1303  1.38   mycroft 			nd_bsw4 (DD_STOP, (stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr +
   1304  1.38   mycroft 					   stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len));
   1305  1.37  christos 		}
   1306  1.37  christos 
   1307  1.38   mycroft 		nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | (state & DMACSR_READ ? DMACSR_SETREAD : DMACSR_SETWRITE) |
   1308  1.38   mycroft 			 (stat->nd_map_cont ? DMACSR_SETSUPDATE : 0));
   1309  1.37  christos 
   1310  1.37  christos #if 0
   1311  1.38   mycroft #ifdef ESP_DEBUG
   1312  1.37  christos 		if (state & DMACSR_BUSEXC) {
   1313  1.38   mycroft 			sprintf (ndtracep, "CE/BUSEXC: %08lX %08X %08X\n",
   1314  1.38   mycroft 				 (stat->nd_map->dm_segs[stat->nd_idx].ds_addr + stat->nd_map->dm_segs[stat->nd_idx].ds_len),
   1315  1.37  christos 				 l, s);
   1316  1.38   mycroft 			ndtracep += strlen (ndtracep);
   1317  1.37  christos 		}
   1318  1.37  christos #endif
   1319  1.38   mycroft #endif
   1320  1.37  christos 	} else {
   1321  1.37  christos #if 0
   1322  1.37  christos 		if (state & DMACSR_BUSEXC) {
   1323  1.38   mycroft 			while (nd_bsr4 (DD_NEXT) !=
   1324  1.38   mycroft 			       (nd_bsr4 (DD_LIMIT) & 0x7FFFFFFF))
   1325  1.37  christos 				printf ("Y"); /* DELAY(50); */
   1326  1.38   mycroft 			state = nd_bsr4 (DD_CSR);
   1327  1.37  christos 		}
   1328  1.37  christos #endif
   1329  1.37  christos 
   1330  1.37  christos 		if (!(state & DMACSR_SUPDATE)) {
   1331  1.38   mycroft 			nextdma_rotate(nsc);
   1332  1.37  christos 		} else {
   1333  1.38   mycroft 			nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE |
   1334  1.38   mycroft 				 DMACSR_INITBUF | DMACSR_RESET |
   1335  1.38   mycroft 				 (state & DMACSR_READ ? DMACSR_SETREAD : DMACSR_SETWRITE));
   1336  1.38   mycroft 
   1337  1.38   mycroft 			nd_bsw4 (DD_NEXT, stat->nd_map->dm_segs[stat->nd_idx].ds_addr);
   1338  1.38   mycroft 			nd_bsw4 (DD_LIMIT,
   1339  1.38   mycroft 				 (stat->nd_map->dm_segs[stat->nd_idx].ds_addr +
   1340  1.38   mycroft 				  stat->nd_map->dm_segs[stat->nd_idx].ds_len) | 0/* x80000000 */);
   1341  1.38   mycroft 			if (stat->nd_map_cont) {
   1342  1.38   mycroft 				nd_bsw4 (DD_START,
   1343  1.38   mycroft 					 stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr);
   1344  1.38   mycroft 				nd_bsw4 (DD_STOP,
   1345  1.38   mycroft 					 (stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr +
   1346  1.38   mycroft 					  stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len) | 0/* x80000000 */);
   1347  1.37  christos 			}
   1348  1.38   mycroft 			nd_bsw4 (DD_CSR, DMACSR_SETENABLE |
   1349  1.38   mycroft 				 DMACSR_CLRCOMPLETE | (state & DMACSR_READ ? DMACSR_SETREAD : DMACSR_SETWRITE) |
   1350  1.38   mycroft 				 (stat->nd_map_cont ? DMACSR_SETSUPDATE : 0));
   1351  1.37  christos #if 1
   1352  1.38   mycroft #ifdef ESP_DEBUG
   1353  1.38   mycroft 				sprintf (ndtracep, "supdate ");
   1354  1.38   mycroft 				ndtracep += strlen (ndtracep);
   1355  1.38   mycroft 				sprintf (ndtracep, "%08X %08X %08X %08X ",
   1356  1.38   mycroft 					 nd_bsr4 (DD_NEXT),
   1357  1.38   mycroft 					 nd_bsr4 (DD_LIMIT) & 0x7FFFFFFF,
   1358  1.38   mycroft 					 nd_bsr4 (DD_START),
   1359  1.38   mycroft 					 nd_bsr4 (DD_STOP) & 0x7FFFFFFF);
   1360  1.38   mycroft 				ndtracep += strlen (ndtracep);
   1361  1.38   mycroft #endif
   1362  1.37  christos #endif
   1363  1.38   mycroft 			stat->nd_exception++;
   1364  1.37  christos 			return(1);
   1365  1.37  christos 			/* NCR_WRITE_REG(sc, ESP_DCTL, ctl); */
   1366  1.37  christos 			goto restart;
   1367  1.37  christos 		}
   1368  1.37  christos 
   1369  1.38   mycroft 		if (stat->nd_map) {
   1370  1.37  christos #if 1
   1371  1.38   mycroft #ifdef ESP_DEBUG
   1372  1.38   mycroft 				sprintf (ndtracep, "%08X %08X %08X %08X ",
   1373  1.38   mycroft 					 nd_bsr4 (DD_NEXT),
   1374  1.38   mycroft 					 nd_bsr4 (DD_LIMIT) & 0x7FFFFFFF,
   1375  1.38   mycroft 					 nd_bsr4 (DD_START),
   1376  1.38   mycroft 					 nd_bsr4 (DD_STOP) & 0x7FFFFFFF);
   1377  1.38   mycroft 				ndtracep += strlen (ndtracep);
   1378  1.38   mycroft #endif
   1379  1.37  christos #endif
   1380  1.37  christos 
   1381  1.37  christos #if 0
   1382  1.38   mycroft 			nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | DMACSR_RESET);
   1383  1.37  christos 
   1384  1.38   mycroft 			nd_bsw4 (DD_CSR, 0);
   1385  1.37  christos #endif
   1386  1.37  christos #if 1
   1387  1.37  christos  /* 6/2 */
   1388  1.38   mycroft 			nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE |
   1389  1.38   mycroft 				 DMACSR_INITBUF | DMACSR_RESET |
   1390  1.38   mycroft 				 (state & DMACSR_READ ? DMACSR_SETREAD : DMACSR_SETWRITE));
   1391  1.37  christos 
   1392  1.38   mycroft 			/* 			nextdma_setup_curr_regs(nsc); */
   1393  1.38   mycroft 			nd_bsw4 (DD_NEXT, stat->nd_map->dm_segs[stat->nd_idx].ds_addr);
   1394  1.38   mycroft 			nd_bsw4 (DD_LIMIT,
   1395  1.38   mycroft 				 (stat->nd_map->dm_segs[stat->nd_idx].ds_addr +
   1396  1.38   mycroft 				  stat->nd_map->dm_segs[stat->nd_idx].ds_len) | 0/* x80000000 */);
   1397  1.38   mycroft 			/* 			nextdma_setup_cont_regs(nsc); */
   1398  1.38   mycroft 			if (stat->nd_map_cont) {
   1399  1.38   mycroft 				nd_bsw4 (DD_START,
   1400  1.38   mycroft 					 stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr);
   1401  1.38   mycroft 				nd_bsw4 (DD_STOP,
   1402  1.38   mycroft 					 (stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr +
   1403  1.38   mycroft 					  stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len) | 0/* x80000000 */);
   1404  1.37  christos 			}
   1405  1.37  christos 
   1406  1.38   mycroft 			nd_bsw4 (DD_CSR,
   1407  1.38   mycroft 				 DMACSR_SETENABLE | (stat->nd_map_cont ? DMACSR_SETSUPDATE : 0) |
   1408  1.38   mycroft 				 (state & DMACSR_READ ? DMACSR_SETREAD : DMACSR_SETWRITE));
   1409  1.38   mycroft #ifdef ESP_DEBUG
   1410  1.38   mycroft 			/* ndtraceshow++; */
   1411  1.38   mycroft #endif
   1412  1.38   mycroft 			stat->nd_exception++;
   1413  1.37  christos 			return(1);
   1414  1.37  christos #endif
   1415  1.37  christos 			/* NCR_WRITE_REG(sc, ESP_DCTL, ctl); */
   1416  1.37  christos 			goto restart;
   1417  1.37  christos 		restart:
   1418  1.37  christos #if 1
   1419  1.38   mycroft #ifdef ESP_DEBUG
   1420  1.38   mycroft 			sprintf (ndtracep, "restart %08lX %08lX\n",
   1421  1.38   mycroft 				 stat->nd_map->dm_segs[stat->nd_idx].ds_addr,
   1422  1.38   mycroft 				 stat->nd_map->dm_segs[stat->nd_idx].ds_addr +
   1423  1.38   mycroft 				 stat->nd_map->dm_segs[stat->nd_idx].ds_len);
   1424  1.38   mycroft 			if (stat->nd_map_cont) {
   1425  1.38   mycroft 				sprintf (ndtracep + strlen(ndtracep) - 1, " %08lX %08lX\n",
   1426  1.38   mycroft 					 stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr,
   1427  1.38   mycroft 					 stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_addr +
   1428  1.38   mycroft 					 stat->nd_map_cont->dm_segs[stat->nd_idx_cont].ds_len);
   1429  1.37  christos 			}
   1430  1.38   mycroft 			ndtracep += strlen (ndtracep);
   1431  1.38   mycroft #endif
   1432  1.37  christos #endif
   1433  1.38   mycroft 			nextdma_print(nsc);
   1434  1.37  christos 			NCR_WRITE_REG(sc, ESP_DCTL, ESPDCTL_16MHZ | ESPDCTL_INTENB);
   1435  1.37  christos 			printf ("ff:%02x tcm:%d tcl:%d esp_dstat:%02x state:%02x step: %02x intr:%02x state:%08X\n",
   1436  1.37  christos 				NCR_READ_REG(sc, NCR_FFLAG),
   1437  1.37  christos 				NCR_READ_REG((sc), NCR_TCM), NCR_READ_REG((sc), NCR_TCL),
   1438  1.37  christos 				NCR_READ_REG(sc, ESP_DSTAT),
   1439  1.37  christos 				NCR_READ_REG(sc, NCR_STAT), NCR_READ_REG(sc, NCR_STEP),
   1440  1.37  christos 				NCR_READ_REG(sc, NCR_INTR), state);
   1441  1.38   mycroft #ifdef ESP_DEBUG
   1442  1.38   mycroft 			*ndtracep = '\0';
   1443  1.38   mycroft 			printf ("ndtrace: %s\n", ndtrace);
   1444  1.38   mycroft #endif
   1445  1.37  christos 			panic("%s: busexc/supdate occured.  Please email this output to chris (at) pin.lu.",
   1446  1.37  christos 			      sc->sc_dev.dv_xname);
   1447  1.38   mycroft #ifdef ESP_DEBUG
   1448  1.38   mycroft 			ndtraceshow++;
   1449  1.38   mycroft #endif
   1450  1.37  christos 		} else {
   1451  1.38   mycroft 			nd_bsw4 (DD_CSR, DMACSR_CLRCOMPLETE | DMACSR_RESET);
   1452  1.38   mycroft 			if (nsc->sc_conf.nd_shutdown_cb)
   1453  1.38   mycroft 				(*nsc->sc_conf.nd_shutdown_cb)(nsc->sc_conf.nd_cb_arg);
   1454  1.37  christos 		}
   1455  1.37  christos 	}
   1456  1.37  christos 	return (1);
   1457  1.37  christos }
   1458  1.37  christos 
   1459  1.44       wiz /* Internal DMA callback routines */
   1460   1.2       dbj bus_dmamap_t
   1461   1.2       dbj esp_dmacb_continue(arg)
   1462   1.2       dbj 	void *arg;
   1463   1.2       dbj {
   1464   1.2       dbj 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   1465   1.2       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1466   1.2       dbj 
   1467  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'x');
   1468  1.44       wiz 	DPRINTF(("%s: DMA continue\n",sc->sc_dev.dv_xname));
   1469   1.4       dbj 
   1470   1.2       dbj #ifdef DIAGNOSTIC
   1471   1.2       dbj 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
   1472  1.44       wiz 		panic("%s: map not loaded in DMA continue callback, datain = %d",
   1473   1.2       dbj 				sc->sc_dev.dv_xname,esc->sc_datain);
   1474   1.2       dbj 	}
   1475   1.2       dbj #endif
   1476  1.18       dbj 
   1477  1.18       dbj 	if ((!(esc->sc_loaded & ESP_LOADED_MAIN)) &&
   1478  1.18       dbj 			(esc->sc_main_dmamap->dm_mapsize)) {
   1479  1.18       dbj 			DPRINTF(("%s: Loading main map\n",sc->sc_dev.dv_xname));
   1480  1.19       dbj #if 0
   1481  1.38   mycroft 			bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_main_dmamap,
   1482  1.18       dbj 					0, esc->sc_main_dmamap->dm_mapsize,
   1483  1.14       dbj 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1484  1.34       dbj 			esc->sc_main_dmamap->dm_xfer_len = 0;
   1485  1.19       dbj #endif
   1486  1.18       dbj 			esc->sc_loaded |= ESP_LOADED_MAIN;
   1487  1.18       dbj 			return(esc->sc_main_dmamap);
   1488  1.18       dbj 	}
   1489  1.18       dbj 
   1490  1.18       dbj 	if ((!(esc->sc_loaded & ESP_LOADED_TAIL)) &&
   1491  1.18       dbj 			(esc->sc_tail_dmamap->dm_mapsize)) {
   1492  1.18       dbj 			DPRINTF(("%s: Loading tail map\n",sc->sc_dev.dv_xname));
   1493  1.19       dbj #if 0
   1494  1.38   mycroft 			bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_tail_dmamap,
   1495  1.14       dbj 					0, esc->sc_tail_dmamap->dm_mapsize,
   1496  1.14       dbj 					(esc->sc_datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE));
   1497  1.34       dbj 			esc->sc_tail_dmamap->dm_xfer_len = 0;
   1498  1.19       dbj #endif
   1499  1.18       dbj 			esc->sc_loaded |= ESP_LOADED_TAIL;
   1500  1.14       dbj 			return(esc->sc_tail_dmamap);
   1501  1.10       dbj 	}
   1502  1.18       dbj 
   1503  1.18       dbj 	DPRINTF(("%s: not loading map\n",sc->sc_dev.dv_xname));
   1504  1.18       dbj 	return(0);
   1505   1.2       dbj }
   1506   1.2       dbj 
   1507  1.14       dbj 
   1508   1.2       dbj void
   1509   1.2       dbj esp_dmacb_completed(map, arg)
   1510   1.2       dbj 	bus_dmamap_t map;
   1511   1.2       dbj 	void *arg;
   1512   1.2       dbj {
   1513   1.2       dbj 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   1514   1.2       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1515   1.2       dbj 
   1516  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'X');
   1517  1.44       wiz 	DPRINTF(("%s: DMA completed\n",sc->sc_dev.dv_xname));
   1518   1.4       dbj 
   1519   1.2       dbj #ifdef DIAGNOSTIC
   1520  1.14       dbj 	if ((esc->sc_datain < 0) || (esc->sc_datain > 1)) {
   1521  1.44       wiz 		panic("%s: invalid DMA direction in completed callback, datain = %d",
   1522  1.18       dbj 				sc->sc_dev.dv_xname,esc->sc_datain);
   1523  1.32       dbj 	}
   1524  1.32       dbj #endif
   1525  1.32       dbj 
   1526  1.34       dbj #if defined(DIAGNOSTIC) && 0
   1527  1.32       dbj 	{
   1528  1.32       dbj 		int i;
   1529  1.32       dbj 		for(i=0;i<map->dm_nsegs;i++) {
   1530  1.33       dbj 			if (map->dm_xfer_len != map->dm_mapsize) {
   1531  1.32       dbj 				printf("%s: map->dm_mapsize = %d\n", sc->sc_dev.dv_xname,map->dm_mapsize);
   1532  1.32       dbj 				printf("%s: map->dm_nsegs = %d\n", sc->sc_dev.dv_xname,map->dm_nsegs);
   1533  1.33       dbj 				printf("%s: map->dm_xfer_len = %d\n", sc->sc_dev.dv_xname,map->dm_xfer_len);
   1534  1.32       dbj 				for(i=0;i<map->dm_nsegs;i++) {
   1535  1.32       dbj 					printf("%s: map->dm_segs[%d].ds_addr = 0x%08lx\n",
   1536  1.32       dbj 							sc->sc_dev.dv_xname,i,map->dm_segs[i].ds_addr);
   1537  1.32       dbj 					printf("%s: map->dm_segs[%d].ds_len = %d\n",
   1538  1.32       dbj 							sc->sc_dev.dv_xname,i,map->dm_segs[i].ds_len);
   1539  1.32       dbj 				}
   1540  1.44       wiz 				panic("%s: incomplete DMA transfer",sc->sc_dev.dv_xname);
   1541  1.32       dbj 			}
   1542  1.32       dbj 		}
   1543   1.2       dbj 	}
   1544  1.23       dbj #endif
   1545  1.23       dbj 
   1546  1.23       dbj 	if (map == esc->sc_main_dmamap) {
   1547  1.23       dbj #ifdef DIAGNOSTIC
   1548  1.23       dbj 		if ((esc->sc_loaded & ESP_UNLOADED_MAIN) ||
   1549  1.23       dbj 				!(esc->sc_loaded & ESP_LOADED_MAIN)) {
   1550  1.40    provos 			panic("%s: unexpected completed call for main map",sc->sc_dev.dv_xname);
   1551  1.23       dbj 		}
   1552  1.23       dbj #endif
   1553  1.23       dbj 		esc->sc_loaded |= ESP_UNLOADED_MAIN;
   1554  1.23       dbj 	} else if (map == esc->sc_tail_dmamap) {
   1555  1.23       dbj #ifdef DIAGNOSTIC
   1556  1.23       dbj 		if ((esc->sc_loaded & ESP_UNLOADED_TAIL) ||
   1557  1.23       dbj 				!(esc->sc_loaded & ESP_LOADED_TAIL)) {
   1558  1.40    provos 			panic("%s: unexpected completed call for tail map",sc->sc_dev.dv_xname);
   1559  1.23       dbj 		}
   1560  1.23       dbj #endif
   1561  1.23       dbj 		esc->sc_loaded |= ESP_UNLOADED_TAIL;
   1562  1.23       dbj 	}
   1563  1.23       dbj #ifdef DIAGNOSTIC
   1564  1.23       dbj 	 else {
   1565  1.14       dbj 		panic("%s: unexpected completed map", sc->sc_dev.dv_xname);
   1566   1.2       dbj 	}
   1567   1.2       dbj #endif
   1568   1.2       dbj 
   1569  1.23       dbj #ifdef ESP_DEBUG
   1570  1.23       dbj 	if (esp_debug) {
   1571  1.23       dbj 		if (map == esc->sc_main_dmamap) {
   1572  1.23       dbj 			printf("%s: completed main map\n",sc->sc_dev.dv_xname);
   1573  1.23       dbj 		} else if (map == esc->sc_tail_dmamap) {
   1574  1.23       dbj 			printf("%s: completed tail map\n",sc->sc_dev.dv_xname);
   1575  1.23       dbj 		}
   1576  1.23       dbj 	}
   1577  1.23       dbj #endif
   1578  1.22       dbj 
   1579  1.22       dbj #if 0
   1580  1.22       dbj 	if ((map == esc->sc_tail_dmamap) ||
   1581  1.22       dbj 			((esc->sc_tail_size == 0) && (map == esc->sc_main_dmamap))) {
   1582  1.22       dbj 
   1583  1.22       dbj 		/* Clear the DMAMOD bit in the DCTL register to give control
   1584  1.22       dbj 		 * back to the scsi chip.
   1585  1.22       dbj 		 */
   1586  1.22       dbj 		if (esc->sc_datain) {
   1587  1.22       dbj 			NCR_WRITE_REG(sc, ESP_DCTL,
   1588  1.37  christos 					ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
   1589  1.22       dbj 		} else {
   1590  1.22       dbj 			NCR_WRITE_REG(sc, ESP_DCTL,
   1591  1.37  christos 					ESPDCTL_16MHZ | ESPDCTL_INTENB);
   1592  1.22       dbj 		}
   1593  1.22       dbj 		DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
   1594  1.22       dbj 	}
   1595  1.22       dbj #endif
   1596  1.22       dbj 
   1597  1.22       dbj 
   1598  1.19       dbj #if 0
   1599  1.38   mycroft 	bus_dmamap_sync(esc->sc_dma->sc_dmat, map,
   1600  1.14       dbj 			0, map->dm_mapsize,
   1601   1.2       dbj 			(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1602  1.19       dbj #endif
   1603  1.13       dbj 
   1604   1.2       dbj }
   1605   1.2       dbj 
   1606   1.2       dbj void
   1607   1.2       dbj esp_dmacb_shutdown(arg)
   1608   1.2       dbj 	void *arg;
   1609   1.2       dbj {
   1610   1.2       dbj 	struct ncr53c9x_softc *sc = (struct ncr53c9x_softc *)arg;
   1611   1.2       dbj 	struct esp_softc *esc = (struct esp_softc *)sc;
   1612   1.2       dbj 
   1613  1.38   mycroft 	NDTRACEIF (*ndtracep++ = 'S');
   1614  1.44       wiz 	DPRINTF(("%s: DMA shutdown\n",sc->sc_dev.dv_xname));
   1615   1.4       dbj 
   1616  1.37  christos 	if (esc->sc_loaded == 0)
   1617  1.37  christos 		return;
   1618  1.37  christos 
   1619  1.22       dbj #if 0
   1620  1.22       dbj 	{
   1621  1.22       dbj 		/* Clear the DMAMOD bit in the DCTL register to give control
   1622  1.22       dbj 		 * back to the scsi chip.
   1623  1.22       dbj 		 */
   1624  1.22       dbj 		if (esc->sc_datain) {
   1625  1.22       dbj 			NCR_WRITE_REG(sc, ESP_DCTL,
   1626  1.37  christos 					ESPDCTL_16MHZ | ESPDCTL_INTENB | ESPDCTL_DMARD);
   1627  1.22       dbj 		} else {
   1628  1.22       dbj 			NCR_WRITE_REG(sc, ESP_DCTL,
   1629  1.37  christos 					ESPDCTL_16MHZ | ESPDCTL_INTENB);
   1630  1.22       dbj 		}
   1631  1.22       dbj 		DPRINTF(("esp dctl is 0x%02x\n",NCR_READ_REG(sc,ESP_DCTL)));
   1632  1.22       dbj 	}
   1633  1.22       dbj #endif
   1634  1.22       dbj 
   1635  1.22       dbj 	DPRINTF(("%s: esp_dma_nest == %d\n",sc->sc_dev.dv_xname,esp_dma_nest));
   1636  1.22       dbj 
   1637  1.13       dbj 	/* Stuff the end slop into fifo */
   1638   1.3       dbj 
   1639  1.14       dbj #ifdef ESP_DEBUG
   1640  1.14       dbj 	if (esp_debug) {
   1641  1.14       dbj 
   1642  1.13       dbj 		int n = NCR_READ_REG(sc, NCR_FFLAG);
   1643  1.20       dbj 		DPRINTF(("%s: fifo size = %d, seq = 0x%x\n",
   1644  1.20       dbj 				sc->sc_dev.dv_xname,n & NCRFIFO_FF, (n & NCRFIFO_SS)>>5));
   1645  1.13       dbj 	}
   1646  1.13       dbj #endif
   1647  1.12       dbj 
   1648  1.22       dbj 	if (esc->sc_main_dmamap->dm_mapsize) {
   1649  1.44       wiz 		if (!esc->sc_datain) { /* unpatch the DMA map for write overrun */
   1650  1.37  christos 			esc->sc_main_dmamap->dm_mapsize -= ESP_DMA_OVERRUN;
   1651  1.37  christos 			esc->sc_main_dmamap->dm_segs[esc->sc_main_dmamap->dm_nsegs - 1].ds_len -=
   1652  1.37  christos 				ESP_DMA_OVERRUN;
   1653  1.37  christos 		}
   1654  1.38   mycroft 		bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_main_dmamap,
   1655  1.22       dbj 			0, esc->sc_main_dmamap->dm_mapsize,
   1656  1.22       dbj 				(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1657  1.38   mycroft 		bus_dmamap_unload(esc->sc_dma->sc_dmat, esc->sc_main_dmamap);
   1658  1.38   mycroft 		NDTRACEIF (
   1659  1.38   mycroft 			sprintf (ndtracep, "m%ld", esc->sc_main_dmamap->dm_xfer_len);
   1660  1.38   mycroft 			ndtracep += strlen (ndtracep);
   1661  1.38   mycroft 			);
   1662  1.22       dbj 	}
   1663  1.22       dbj 
   1664  1.22       dbj 	if (esc->sc_tail_dmamap->dm_mapsize) {
   1665  1.38   mycroft 		bus_dmamap_sync(esc->sc_dma->sc_dmat, esc->sc_tail_dmamap,
   1666  1.22       dbj 			0, esc->sc_tail_dmamap->dm_mapsize,
   1667  1.22       dbj 				(esc->sc_datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE));
   1668  1.38   mycroft 		bus_dmamap_unload(esc->sc_dma->sc_dmat, esc->sc_tail_dmamap);
   1669  1.44       wiz 		/* copy the tail DMA buffer data for read transfers */
   1670  1.37  christos 		if (esc->sc_datain) {
   1671  1.37  christos 			memcpy(*esc->sc_dmaaddr+esc->sc_begin_size+esc->sc_main_size,
   1672  1.37  christos 			       esc->sc_tail, esc->sc_dmasize-(esc->sc_begin_size+esc->sc_main_size));
   1673  1.37  christos 		}
   1674  1.38   mycroft 		NDTRACEIF (
   1675  1.38   mycroft 			sprintf (ndtracep, "t%ld", esc->sc_tail_dmamap->dm_xfer_len);
   1676  1.38   mycroft 			ndtracep += strlen (ndtracep);
   1677  1.38   mycroft 			);
   1678   1.4       dbj 	}
   1679  1.13       dbj 
   1680  1.18       dbj #ifdef ESP_DEBUG
   1681  1.18       dbj 	if (esp_debug) {
   1682  1.35       chs 		printf("%s: dma_shutdown: addr=%p,len=0x%08x,size=0x%08x\n",
   1683  1.18       dbj 				sc->sc_dev.dv_xname,
   1684  1.18       dbj 				*esc->sc_dmaaddr, *esc->sc_dmalen, esc->sc_dmasize);
   1685  1.24       dbj 		if (esp_debug > 10) {
   1686  1.24       dbj 			esp_hex_dump(*(esc->sc_dmaaddr),esc->sc_dmasize);
   1687  1.35       chs 			printf("%s: tail=%p,tailbuf=%p,tail_size=0x%08x\n",
   1688  1.24       dbj 					sc->sc_dev.dv_xname,
   1689  1.24       dbj 					esc->sc_tail, &(esc->sc_tailbuf[0]), esc->sc_tail_size);
   1690  1.24       dbj 			esp_hex_dump(&(esc->sc_tailbuf[0]),sizeof(esc->sc_tailbuf));
   1691  1.24       dbj 		}
   1692  1.13       dbj 	}
   1693  1.11       dbj #endif
   1694   1.3       dbj 
   1695  1.18       dbj 	esc->sc_main = 0;
   1696  1.18       dbj 	esc->sc_main_size = 0;
   1697  1.14       dbj 	esc->sc_tail = 0;
   1698  1.14       dbj 	esc->sc_tail_size = 0;
   1699  1.19       dbj 
   1700  1.19       dbj 	esc->sc_datain = -1;
   1701  1.37  christos /* 	esc->sc_dmaaddr = 0; */
   1702  1.37  christos /* 	esc->sc_dmalen  = 0; */
   1703  1.37  christos /* 	esc->sc_dmasize = 0; */
   1704  1.19       dbj 
   1705  1.19       dbj 	esc->sc_loaded = 0;
   1706  1.19       dbj 
   1707  1.19       dbj 	esc->sc_begin = 0;
   1708  1.19       dbj 	esc->sc_begin_size = 0;
   1709  1.20       dbj 
   1710  1.20       dbj #ifdef ESP_DEBUG
   1711  1.20       dbj 	if (esp_debug) {
   1712  1.28        tv 		char sbuf[256];
   1713  1.28        tv 
   1714  1.28        tv 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRSTAT)),
   1715  1.28        tv 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
   1716  1.28        tv 		printf("  *intrstat = 0x%s\n", sbuf);
   1717  1.28        tv 
   1718  1.28        tv 		bitmask_snprintf((*(volatile u_long *)IIOV(NEXT_P_INTRMASK)),
   1719  1.28        tv 				 NEXT_INTR_BITS, sbuf, sizeof(sbuf));
   1720  1.28        tv 		printf("  *intrmask = 0x%s\n", sbuf);
   1721  1.20       dbj 	}
   1722  1.20       dbj #endif
   1723   1.1       dbj }
   1724