Home | History | Annotate | Line # | Download | only in vme
si.c revision 1.2
      1  1.2  thorpej /*	$NetBSD: si.c,v 1.2 2000/07/04 01:29:14 thorpej Exp $	*/
      2  1.1       pk 
      3  1.1       pk /*-
      4  1.1       pk  * Copyright (c) 1996,2000 The NetBSD Foundation, Inc.
      5  1.1       pk  * All rights reserved.
      6  1.1       pk  *
      7  1.1       pk  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1       pk  * by Adam Glass, David Jones, Gordon W. Ross, Jason R. Thorpe and
      9  1.1       pk  * Paul Kranenburg.
     10  1.1       pk  *
     11  1.1       pk  * Redistribution and use in source and binary forms, with or without
     12  1.1       pk  * modification, are permitted provided that the following conditions
     13  1.1       pk  * are met:
     14  1.1       pk  * 1. Redistributions of source code must retain the above copyright
     15  1.1       pk  *    notice, this list of conditions and the following disclaimer.
     16  1.1       pk  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1       pk  *    notice, this list of conditions and the following disclaimer in the
     18  1.1       pk  *    documentation and/or other materials provided with the distribution.
     19  1.1       pk  * 3. All advertising materials mentioning features or use of this software
     20  1.1       pk  *    must display the following acknowledgement:
     21  1.1       pk  *        This product includes software developed by the NetBSD
     22  1.1       pk  *        Foundation, Inc. and its contributors.
     23  1.1       pk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1       pk  *    contributors may be used to endorse or promote products derived
     25  1.1       pk  *    from this software without specific prior written permission.
     26  1.1       pk  *
     27  1.1       pk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1       pk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1       pk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1       pk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1       pk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1       pk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1       pk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1       pk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1       pk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1       pk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1       pk  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1       pk  */
     39  1.1       pk 
     40  1.1       pk /*
     41  1.1       pk  * This file contains VME bus-dependent of the `si' SCSI adapter.
     42  1.1       pk  * This hardware is frequently found on Sun 3 and Sun 4 machines.
     43  1.1       pk  *
     44  1.1       pk  * The SCSI machinery on this adapter is implemented by an NCR5380,
     45  1.1       pk  * which is taken care of by the chipset driver in /sys/dev/ic/ncr5380sbc.c
     46  1.1       pk  *
     47  1.1       pk  * The logic has a bit to enable or disable the DMA engine,
     48  1.1       pk  * but that bit also gates the interrupt line from the NCR5380!
     49  1.1       pk  * Therefore, in order to get any interrupt from the 5380, (i.e.
     50  1.1       pk  * for reselect) one must clear the DMA engine transfer count and
     51  1.1       pk  * then enable DMA.  This has the further complication that you
     52  1.1       pk  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     53  1.1       pk  * we have to turn DMA back off before we even look at the 5380.
     54  1.1       pk  *
     55  1.1       pk  * What wonderfully whacky hardware this is!
     56  1.1       pk  *
     57  1.1       pk  */
     58  1.1       pk 
     59  1.1       pk /*
     60  1.1       pk  * This driver originated as an MD implementation for the sun3 and sun4
     61  1.1       pk  * ports. The notes pertaining to that history are included below.
     62  1.1       pk  *
     63  1.1       pk  * David Jones wrote the initial version of this module for NetBSD/sun3,
     64  1.1       pk  * which included support for the VME adapter only. (no reselection).
     65  1.1       pk  *
     66  1.1       pk  * Gordon Ross added support for the Sun 3 OBIO adapter, and re-worked
     67  1.1       pk  * both the VME and OBIO code to support disconnect/reselect.
     68  1.1       pk  * (Required figuring out the hardware "features" noted above.)
     69  1.1       pk  *
     70  1.1       pk  * The autoconfiguration boilerplate came from Adam Glass.
     71  1.1       pk  *
     72  1.1       pk  * Jason R. Thorpe ported the autoconfiguration and VME portions to
     73  1.1       pk  * NetBSD/sparc, and added initial support for the 4/100 "SCSI Weird",
     74  1.1       pk  * a wacky OBIO variant of the VME SCSI-3.  Many thanks to Chuck Cranor
     75  1.1       pk  * for lots of helpful tips and suggestions.  Thanks also to Paul Kranenburg
     76  1.1       pk  * and Chris Torek for bits of insight needed along the way.  Thanks to
     77  1.1       pk  * David Gilbert and Andrew Gillham who risked filesystem life-and-limb
     78  1.1       pk  * for the sake of testing.  Andrew Gillham helped work out the bugs
     79  1.1       pk  * the 4/100 DMA code.
     80  1.1       pk  */
     81  1.1       pk 
     82  1.1       pk #include "opt_ddb.h"
     83  1.1       pk 
     84  1.1       pk #include <sys/types.h>
     85  1.1       pk #include <sys/param.h>
     86  1.1       pk #include <sys/systm.h>
     87  1.1       pk #include <sys/kernel.h>
     88  1.1       pk #include <sys/malloc.h>
     89  1.1       pk #include <sys/errno.h>
     90  1.1       pk #include <sys/device.h>
     91  1.1       pk #include <sys/buf.h>
     92  1.1       pk 
     93  1.1       pk #include <machine/bus.h>
     94  1.1       pk #include <machine/intr.h>
     95  1.1       pk 
     96  1.1       pk #include <dev/vme/vmereg.h>
     97  1.1       pk #include <dev/vme/vmevar.h>
     98  1.1       pk 
     99  1.1       pk #include <dev/scsipi/scsi_all.h>
    100  1.1       pk #include <dev/scsipi/scsipi_all.h>
    101  1.1       pk #include <dev/scsipi/scsipi_debug.h>
    102  1.1       pk #include <dev/scsipi/scsiconf.h>
    103  1.1       pk 
    104  1.1       pk #ifndef DDB
    105  1.1       pk #define	Debugger()
    106  1.1       pk #endif
    107  1.1       pk 
    108  1.1       pk #ifndef DEBUG
    109  1.1       pk #define DEBUG XXX
    110  1.1       pk #endif
    111  1.1       pk 
    112  1.1       pk #include <dev/ic/ncr5380reg.h>
    113  1.1       pk #include <dev/ic/ncr5380var.h>
    114  1.1       pk 
    115  1.2  thorpej #include <dev/vme/sireg.h>
    116  1.1       pk 
    117  1.1       pk /*
    118  1.1       pk  * Transfers smaller than this are done using PIO
    119  1.1       pk  * (on assumption they're not worth DMA overhead)
    120  1.1       pk  */
    121  1.1       pk #define	MIN_DMA_LEN 128
    122  1.1       pk 
    123  1.1       pk #ifdef	DEBUG
    124  1.1       pk int si_debug = 0;
    125  1.1       pk static int si_link_flags = 0 /* | SDEV_DB2 */ ;
    126  1.1       pk #endif
    127  1.1       pk 
    128  1.1       pk /*
    129  1.1       pk  * This structure is used to keep track of mapped DMA requests.
    130  1.1       pk  */
    131  1.1       pk struct si_dma_handle {
    132  1.1       pk 	int 		dh_flags;
    133  1.1       pk #define	SIDH_BUSY	0x01		/* This DH is in use */
    134  1.1       pk #define	SIDH_OUT	0x02		/* DMA does data out (write) */
    135  1.1       pk 	int 		dh_maplen;	/* Original data length */
    136  1.1       pk 	bus_dmamap_t	dh_dmamap;
    137  1.1       pk #define dh_dvma	dh_dmamap->dm_segs[0].ds_addr /* VA of buffer in DVMA space */
    138  1.1       pk };
    139  1.1       pk 
    140  1.1       pk /*
    141  1.1       pk  * The first structure member has to be the ncr5380_softc
    142  1.1       pk  * so we can just cast to go back and fourth between them.
    143  1.1       pk  */
    144  1.1       pk struct si_softc {
    145  1.1       pk 	struct ncr5380_softc	ncr_sc;
    146  1.1       pk 	bus_space_tag_t		sc_bustag;	/* bus tags */
    147  1.1       pk 	bus_dma_tag_t		sc_dmatag;
    148  1.1       pk 	vme_chipset_tag_t	sc_vctag;
    149  1.1       pk 
    150  1.1       pk 	int		sc_adapter_iv_am; /* int. vec + address modifier */
    151  1.1       pk 	struct si_dma_handle *sc_dma;
    152  1.1       pk 	int		sc_xlen;	/* length of current DMA segment. */
    153  1.1       pk 	int		sc_options;	/* options for this instance. */
    154  1.1       pk };
    155  1.1       pk 
    156  1.1       pk /*
    157  1.1       pk  * Options.  By default, DMA is enabled and DMA completion interrupts
    158  1.1       pk  * and reselect are disabled.  You may enable additional features
    159  1.1       pk  * the `flags' directive in your kernel's configuration file.
    160  1.1       pk  *
    161  1.1       pk  * Alternatively, you can patch your kernel with DDB or some other
    162  1.1       pk  * mechanism.  The sc_options member of the softc is OR'd with
    163  1.1       pk  * the value in si_options.
    164  1.1       pk  *
    165  1.1       pk  * Note, there's a separate sw_options to make life easier.
    166  1.1       pk  */
    167  1.1       pk #define	SI_ENABLE_DMA	0x01	/* Use DMA (maybe polled) */
    168  1.1       pk #define	SI_DMA_INTR	0x02	/* DMA completion interrupts */
    169  1.1       pk #define	SI_DO_RESELECT	0x04	/* Allow disconnect/reselect */
    170  1.1       pk #define	SI_OPTIONS_MASK	(SI_ENABLE_DMA|SI_DMA_INTR|SI_DO_RESELECT)
    171  1.1       pk #define SI_OPTIONS_BITS	"\10\3RESELECT\2DMA_INTR\1DMA"
    172  1.1       pk int si_options = SI_ENABLE_DMA|SI_DMA_INTR|SI_DO_RESELECT;
    173  1.1       pk 
    174  1.1       pk static int	si_match __P((struct device *, struct cfdata *, void *));
    175  1.1       pk static void	si_attach __P((struct device *, struct device *, void *));
    176  1.1       pk static int	si_intr __P((void *));
    177  1.1       pk static void	si_reset_adapter __P((struct ncr5380_softc *));
    178  1.1       pk 
    179  1.1       pk void	si_dma_alloc __P((struct ncr5380_softc *));
    180  1.1       pk void	si_dma_free __P((struct ncr5380_softc *));
    181  1.1       pk void	si_dma_poll __P((struct ncr5380_softc *));
    182  1.1       pk 
    183  1.1       pk void	si_dma_setup __P((struct ncr5380_softc *));
    184  1.1       pk void	si_dma_start __P((struct ncr5380_softc *));
    185  1.1       pk void	si_dma_eop __P((struct ncr5380_softc *));
    186  1.1       pk void	si_dma_stop __P((struct ncr5380_softc *));
    187  1.1       pk 
    188  1.1       pk void	si_intr_on  __P((struct ncr5380_softc *));
    189  1.1       pk void	si_intr_off __P((struct ncr5380_softc *));
    190  1.1       pk 
    191  1.1       pk /*
    192  1.1       pk  * Shorthand bus space access
    193  1.1       pk  * XXX - must look into endian issues here.
    194  1.1       pk  */
    195  1.1       pk #define SIREG_READ(sc, index) \
    196  1.1       pk 	bus_space_read_2((sc)->sc_regt, (sc)->sc_regh, index)
    197  1.1       pk #define SIREG_WRITE(sc, index, v) \
    198  1.1       pk 	bus_space_write_2((sc)->sc_regt, (sc)->sc_regh, index, v)
    199  1.1       pk 
    200  1.1       pk 
    201  1.1       pk /* Auto-configuration glue. */
    202  1.1       pk struct cfattach si_ca = {
    203  1.1       pk 	sizeof(struct si_softc), si_match, si_attach
    204  1.1       pk };
    205  1.1       pk 
    206  1.1       pk static int
    207  1.1       pk si_match(parent, cf, aux)
    208  1.1       pk 	struct device	*parent;
    209  1.1       pk 	struct cfdata *cf;
    210  1.1       pk 	void *aux;
    211  1.1       pk {
    212  1.1       pk 	struct vme_attach_args	*va = aux;
    213  1.1       pk 	vme_chipset_tag_t	ct = va->va_vct;
    214  1.1       pk         vme_am_t		mod;
    215  1.1       pk         vme_addr_t		vme_addr;
    216  1.1       pk 
    217  1.1       pk 	/* Make sure there is something there... */
    218  1.1       pk 	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
    219  1.1       pk 	vme_addr = va->r[0].offset;
    220  1.1       pk 
    221  1.1       pk 	if (vme_probe(ct, vme_addr, 1, mod, VME_D8, NULL, 0) != 0)
    222  1.1       pk 		return (0);
    223  1.1       pk 
    224  1.1       pk 	/*
    225  1.1       pk 	 * If this is a VME SCSI board, we have to determine whether
    226  1.1       pk 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
    227  1.1       pk 	 * be determined using the fact that the "sc" board occupies
    228  1.1       pk 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
    229  1.1       pk 	 */
    230  1.1       pk 	return (vme_probe(ct, vme_addr + 0x801, 1, mod, VME_D8, NULL, 0) != 0);
    231  1.1       pk }
    232  1.1       pk 
    233  1.1       pk static void
    234  1.1       pk si_attach(parent, self, aux)
    235  1.1       pk 	struct device	*parent, *self;
    236  1.1       pk 	void		*aux;
    237  1.1       pk {
    238  1.1       pk 	struct si_softc		*sc = (struct si_softc *) self;
    239  1.1       pk 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    240  1.1       pk 	struct vme_attach_args	*va = aux;
    241  1.1       pk 	vme_chipset_tag_t	ct = va->va_vct;
    242  1.1       pk 	bus_space_tag_t		bt;
    243  1.1       pk 	bus_space_handle_t	bh;
    244  1.1       pk 	vme_mapresc_t resc;
    245  1.1       pk 	vme_intr_handle_t	ih;
    246  1.1       pk 	vme_am_t		mod;
    247  1.1       pk 	char bits[64];
    248  1.1       pk 	int i;
    249  1.1       pk 
    250  1.1       pk 	sc->sc_dmatag = va->va_bdt;
    251  1.1       pk 	sc->sc_vctag = ct;
    252  1.1       pk 
    253  1.1       pk 	mod = VME_AM_A24 | VME_AM_MBO | VME_AM_SUPER | VME_AM_DATA;
    254  1.1       pk 
    255  1.1       pk 	if (vme_space_map(ct, va->r[0].offset, SIREG_BANK_SZ,
    256  1.1       pk 			  mod, VME_D8, 0, &bt, &bh, &resc) != 0)
    257  1.1       pk 		panic("%s: vme_space_map", ncr_sc->sc_dev.dv_xname);
    258  1.1       pk 
    259  1.1       pk 	ncr_sc->sc_regt = bt;
    260  1.1       pk 	ncr_sc->sc_regh = bh;
    261  1.1       pk 
    262  1.1       pk 	sc->sc_options = si_options;
    263  1.1       pk 
    264  1.1       pk 	ncr_sc->sc_dma_setup = si_dma_setup;
    265  1.1       pk 	ncr_sc->sc_dma_start = si_dma_start;
    266  1.1       pk 	ncr_sc->sc_dma_eop   = si_dma_stop;
    267  1.1       pk 	ncr_sc->sc_dma_stop  = si_dma_stop;
    268  1.1       pk 
    269  1.1       pk 	vme_intr_map(ct, va->ilevel, va->ivector, &ih);
    270  1.1       pk 	vme_intr_establish(ct, ih, IPL_BIO, si_intr, sc);
    271  1.1       pk 
    272  1.1       pk 	printf("\n");
    273  1.1       pk 
    274  1.1       pk 	sc->sc_adapter_iv_am = (mod << 8) | (va->ivector & 0xFF);
    275  1.1       pk 
    276  1.1       pk 	/*
    277  1.1       pk 	 * Pull in the options flags.  Allow the user to completely
    278  1.1       pk 	 * override the default values.
    279  1.1       pk 	 */
    280  1.1       pk 	if ((ncr_sc->sc_dev.dv_cfdata->cf_flags & SI_OPTIONS_MASK) != 0)
    281  1.1       pk 		sc->sc_options =
    282  1.1       pk 		    (ncr_sc->sc_dev.dv_cfdata->cf_flags & SI_OPTIONS_MASK);
    283  1.1       pk 
    284  1.1       pk 	/*
    285  1.1       pk 	 * Initialize fields used by the MI code
    286  1.1       pk 	 */
    287  1.1       pk 
    288  1.1       pk 	/* NCR5380 register bank offsets */
    289  1.1       pk 	ncr_sc->sci_r0 = 0;
    290  1.1       pk 	ncr_sc->sci_r1 = 1;
    291  1.1       pk 	ncr_sc->sci_r2 = 2;
    292  1.1       pk 	ncr_sc->sci_r3 = 3;
    293  1.1       pk 	ncr_sc->sci_r4 = 4;
    294  1.1       pk 	ncr_sc->sci_r5 = 5;
    295  1.1       pk 	ncr_sc->sci_r6 = 6;
    296  1.1       pk 	ncr_sc->sci_r7 = 7;
    297  1.1       pk 
    298  1.1       pk 	ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
    299  1.1       pk 
    300  1.1       pk 	/*
    301  1.1       pk 	 * MD function pointers used by the MI code.
    302  1.1       pk 	 */
    303  1.1       pk 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    304  1.1       pk 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    305  1.1       pk 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    306  1.1       pk 	ncr_sc->sc_dma_free  = si_dma_free;
    307  1.1       pk 	ncr_sc->sc_dma_poll  = si_dma_poll;
    308  1.1       pk 
    309  1.1       pk 	ncr_sc->sc_flags = 0;
    310  1.1       pk 	if ((sc->sc_options & SI_DO_RESELECT) == 0)
    311  1.1       pk 		ncr_sc->sc_no_disconnect = 0xFF;
    312  1.1       pk 	if ((sc->sc_options & SI_DMA_INTR) == 0)
    313  1.1       pk 		ncr_sc->sc_flags |= NCR5380_FORCE_POLLING;
    314  1.1       pk 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
    315  1.1       pk 
    316  1.1       pk 	/*
    317  1.1       pk 	 * Allocate DMA handles.
    318  1.1       pk 	 */
    319  1.1       pk 	i = SCI_OPENINGS * sizeof(struct si_dma_handle);
    320  1.1       pk 	sc->sc_dma = (struct si_dma_handle *)malloc(i, M_DEVBUF, M_NOWAIT);
    321  1.1       pk 	if (sc->sc_dma == NULL)
    322  1.1       pk 		panic("si: dma handle malloc failed\n");
    323  1.1       pk 
    324  1.1       pk 	for (i = 0; i < SCI_OPENINGS; i++) {
    325  1.1       pk 		sc->sc_dma[i].dh_flags = 0;
    326  1.1       pk 
    327  1.1       pk 		/* Allocate a DMA handle */
    328  1.1       pk 		if (vme_dmamap_create(
    329  1.1       pk 				sc->sc_vctag,	/* VME chip tag */
    330  1.1       pk 				MAXPHYS,	/* size */
    331  1.1       pk 				VME_AM_A24,	/* address modifier */
    332  1.1       pk 				VME_D16,	/* data size */
    333  1.1       pk 				0,		/* swap */
    334  1.1       pk 				1,		/* nsegments */
    335  1.1       pk 				MAXPHYS,	/* maxsegsz */
    336  1.1       pk 				0,		/* boundary */
    337  1.1       pk 				BUS_DMA_NOWAIT,
    338  1.1       pk 				&sc->sc_dma[i].dh_dmamap) != 0) {
    339  1.1       pk 
    340  1.1       pk 			printf("%s: DMA buffer map create error\n",
    341  1.1       pk 				ncr_sc->sc_dev.dv_xname);
    342  1.1       pk 			return;
    343  1.1       pk 		}
    344  1.1       pk 	}
    345  1.1       pk 
    346  1.1       pk 	if (sc->sc_options) {
    347  1.1       pk 		printf("%s: options=%s\n", ncr_sc->sc_dev.dv_xname,
    348  1.1       pk 			bitmask_snprintf(sc->sc_options, SI_OPTIONS_BITS,
    349  1.1       pk 			    bits, sizeof(bits)));
    350  1.1       pk 	}
    351  1.1       pk #ifdef	DEBUG
    352  1.1       pk 	ncr_sc->sc_link.flags |= si_link_flags;
    353  1.1       pk #endif
    354  1.1       pk 
    355  1.1       pk 	ncr_sc->sc_link.scsipi_scsi.adapter_target = 7;
    356  1.1       pk 	ncr_sc->sc_adapter.scsipi_minphys = minphys;
    357  1.1       pk 
    358  1.1       pk 	/*
    359  1.1       pk 	 *  Initialize si board itself.
    360  1.1       pk 	 */
    361  1.1       pk 	si_reset_adapter(ncr_sc);
    362  1.1       pk 	ncr5380_attach(ncr_sc);
    363  1.1       pk 
    364  1.1       pk 	if (sc->sc_options & SI_DO_RESELECT) {
    365  1.1       pk 		/*
    366  1.1       pk 		 * Need to enable interrupts (and DMA!)
    367  1.1       pk 		 * on this H/W for reselect to work.
    368  1.1       pk 		 */
    369  1.1       pk 		ncr_sc->sc_intr_on   = si_intr_on;
    370  1.1       pk 		ncr_sc->sc_intr_off  = si_intr_off;
    371  1.1       pk 	}
    372  1.1       pk }
    373  1.1       pk 
    374  1.1       pk #define CSR_WANT (SI_CSR_SBC_IP | SI_CSR_DMA_IP | \
    375  1.1       pk 	SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR )
    376  1.1       pk 
    377  1.1       pk static int
    378  1.1       pk si_intr(void *arg)
    379  1.1       pk {
    380  1.1       pk 	struct si_softc *sc = arg;
    381  1.1       pk 	struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)arg;
    382  1.1       pk 	int dma_error, claimed;
    383  1.1       pk 	u_short csr;
    384  1.1       pk 
    385  1.1       pk 	claimed = 0;
    386  1.1       pk 	dma_error = 0;
    387  1.1       pk 
    388  1.1       pk 	/* SBC interrupt? DMA interrupt? */
    389  1.1       pk 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
    390  1.1       pk 
    391  1.1       pk 	NCR_TRACE("si_intr: csr=0x%x\n", csr);
    392  1.1       pk 
    393  1.1       pk 	if (csr & SI_CSR_DMA_CONFLICT) {
    394  1.1       pk 		dma_error |= SI_CSR_DMA_CONFLICT;
    395  1.1       pk 		printf("si_intr: DMA conflict\n");
    396  1.1       pk 	}
    397  1.1       pk 	if (csr & SI_CSR_DMA_BUS_ERR) {
    398  1.1       pk 		dma_error |= SI_CSR_DMA_BUS_ERR;
    399  1.1       pk 		printf("si_intr: DMA bus error\n");
    400  1.1       pk 	}
    401  1.1       pk 	if (dma_error) {
    402  1.1       pk 		if (sc->ncr_sc.sc_state & NCR_DOINGDMA)
    403  1.1       pk 			sc->ncr_sc.sc_state |= NCR_ABORTING;
    404  1.1       pk 		/* Make sure we will call the main isr. */
    405  1.1       pk 		csr |= SI_CSR_DMA_IP;
    406  1.1       pk 	}
    407  1.1       pk 
    408  1.1       pk 	if (csr & (SI_CSR_SBC_IP | SI_CSR_DMA_IP)) {
    409  1.1       pk 		claimed = ncr5380_intr(&sc->ncr_sc);
    410  1.1       pk #ifdef DEBUG
    411  1.1       pk 		if (!claimed) {
    412  1.1       pk 			printf("si_intr: spurious from SBC\n");
    413  1.1       pk 			if (si_debug & 4) {
    414  1.1       pk 				Debugger();	/* XXX */
    415  1.1       pk 			}
    416  1.1       pk 		}
    417  1.1       pk #endif
    418  1.1       pk 	}
    419  1.1       pk 
    420  1.1       pk 	return (claimed);
    421  1.1       pk }
    422  1.1       pk 
    423  1.1       pk 
    424  1.1       pk static void
    425  1.1       pk si_reset_adapter(struct ncr5380_softc *ncr_sc)
    426  1.1       pk {
    427  1.1       pk 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    428  1.1       pk 
    429  1.1       pk #ifdef	DEBUG
    430  1.1       pk 	if (si_debug) {
    431  1.1       pk 		printf("si_reset_adapter\n");
    432  1.1       pk 	}
    433  1.1       pk #endif
    434  1.1       pk 
    435  1.1       pk 	/*
    436  1.1       pk 	 * The SCSI3 controller has an 8K FIFO to buffer data between the
    437  1.1       pk 	 * 5380 and the DMA.  Make sure it starts out empty.
    438  1.1       pk 	 *
    439  1.1       pk 	 * The reset bits in the CSR are active low.
    440  1.1       pk 	 */
    441  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, 0);
    442  1.1       pk 	delay(10);
    443  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR,
    444  1.1       pk 			SI_CSR_FIFO_RES | SI_CSR_SCSI_RES | SI_CSR_INTR_EN);
    445  1.1       pk 	delay(10);
    446  1.1       pk 
    447  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, 0);
    448  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, 0);
    449  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, 0);
    450  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
    451  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
    452  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_IV_AM, sc->sc_adapter_iv_am);
    453  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, 0);
    454  1.1       pk 
    455  1.1       pk 	SCI_CLR_INTR(ncr_sc);
    456  1.1       pk }
    457  1.1       pk 
    458  1.1       pk /*****************************************************************
    459  1.1       pk  * Common functions for DMA
    460  1.1       pk  ****************************************************************/
    461  1.1       pk 
    462  1.1       pk /*
    463  1.1       pk  * Allocate a DMA handle and put it in sc->sc_dma.  Prepare
    464  1.1       pk  * for DMA transfer.
    465  1.1       pk  */
    466  1.1       pk void
    467  1.1       pk si_dma_alloc(ncr_sc)
    468  1.1       pk 	struct ncr5380_softc *ncr_sc;
    469  1.1       pk {
    470  1.1       pk 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    471  1.1       pk 	struct sci_req *sr = ncr_sc->sc_current;
    472  1.1       pk 	struct scsipi_xfer *xs = sr->sr_xs;
    473  1.1       pk 	struct si_dma_handle *dh;
    474  1.1       pk 	int i, xlen;
    475  1.1       pk 	u_long addr;
    476  1.1       pk 
    477  1.1       pk #ifdef DIAGNOSTIC
    478  1.1       pk 	if (sr->sr_dma_hand != NULL)
    479  1.1       pk 		panic("si_dma_alloc: already have DMA handle");
    480  1.1       pk #endif
    481  1.1       pk 
    482  1.1       pk #if 1	/* XXX - Temporary */
    483  1.1       pk 	/* XXX - In case we think DMA is completely broken... */
    484  1.1       pk 	if ((sc->sc_options & SI_ENABLE_DMA) == 0)
    485  1.1       pk 		return;
    486  1.1       pk #endif
    487  1.1       pk 
    488  1.1       pk 	addr = (u_long) ncr_sc->sc_dataptr;
    489  1.1       pk 	xlen = ncr_sc->sc_datalen;
    490  1.1       pk 
    491  1.1       pk 	/* If the DMA start addr is misaligned then do PIO */
    492  1.1       pk 	if ((addr & 1) || (xlen & 1)) {
    493  1.1       pk 		printf("si_dma_alloc: misaligned.\n");
    494  1.1       pk 		return;
    495  1.1       pk 	}
    496  1.1       pk 
    497  1.1       pk 	/* Make sure our caller checked sc_min_dma_len. */
    498  1.1       pk 	if (xlen < MIN_DMA_LEN)
    499  1.1       pk 		panic("si_dma_alloc: xlen=0x%x\n", xlen);
    500  1.1       pk 
    501  1.1       pk 	/* Find free DMA handle.  Guaranteed to find one since we have
    502  1.1       pk 	   as many DMA handles as the driver has processes. */
    503  1.1       pk 	for (i = 0; i < SCI_OPENINGS; i++) {
    504  1.1       pk 		if ((sc->sc_dma[i].dh_flags & SIDH_BUSY) == 0)
    505  1.1       pk 			goto found;
    506  1.1       pk 	}
    507  1.1       pk 	panic("si: no free DMA handles.");
    508  1.1       pk 
    509  1.1       pk found:
    510  1.1       pk 	dh = &sc->sc_dma[i];
    511  1.1       pk 	dh->dh_flags = SIDH_BUSY;
    512  1.1       pk 	dh->dh_maplen  = xlen;
    513  1.1       pk 
    514  1.1       pk 	/* Copy the "write" flag for convenience. */
    515  1.1       pk 	if ((xs->xs_control & XS_CTL_DATA_OUT) != 0)
    516  1.1       pk 		dh->dh_flags |= SIDH_OUT;
    517  1.1       pk 
    518  1.1       pk 	/*
    519  1.1       pk 	 * Double-map the buffer into DVMA space.  If we can't re-map
    520  1.1       pk 	 * the buffer, we print a warning and fall back to PIO mode.
    521  1.1       pk 	 *
    522  1.1       pk 	 * NOTE: it is not safe to sleep here!
    523  1.1       pk 	 */
    524  1.1       pk 	if (bus_dmamap_load(sc->sc_dmatag, dh->dh_dmamap,
    525  1.1       pk 			    (caddr_t)addr, xlen, NULL, BUS_DMA_NOWAIT) != 0) {
    526  1.1       pk 		/* Can't remap segment */
    527  1.1       pk 		printf("si_dma_alloc: can't remap 0x%lx/0x%x, doing PIO\n",
    528  1.1       pk 			addr, dh->dh_maplen);
    529  1.1       pk 		dh->dh_flags = 0;
    530  1.1       pk 		return;
    531  1.1       pk 	}
    532  1.1       pk 	bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap, addr, xlen,
    533  1.1       pk 			(dh->dh_flags & SIDH_OUT)
    534  1.1       pk 				? BUS_DMASYNC_PREWRITE
    535  1.1       pk 				: BUS_DMASYNC_PREREAD);
    536  1.1       pk 
    537  1.1       pk 	/* success */
    538  1.1       pk 	sr->sr_dma_hand = dh;
    539  1.1       pk 
    540  1.1       pk 	return;
    541  1.1       pk }
    542  1.1       pk 
    543  1.1       pk 
    544  1.1       pk void
    545  1.1       pk si_dma_free(ncr_sc)
    546  1.1       pk 	struct ncr5380_softc *ncr_sc;
    547  1.1       pk {
    548  1.1       pk 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    549  1.1       pk 	struct sci_req *sr = ncr_sc->sc_current;
    550  1.1       pk 	struct si_dma_handle *dh = sr->sr_dma_hand;
    551  1.1       pk 
    552  1.1       pk #ifdef DIAGNOSTIC
    553  1.1       pk 	if (dh == NULL)
    554  1.1       pk 		panic("si_dma_free: no DMA handle");
    555  1.1       pk #endif
    556  1.1       pk 
    557  1.1       pk 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    558  1.1       pk 		panic("si_dma_free: free while in progress");
    559  1.1       pk 
    560  1.1       pk 	if (dh->dh_flags & SIDH_BUSY) {
    561  1.1       pk 		/* Give back the DVMA space. */
    562  1.1       pk 		bus_dmamap_sync(sc->sc_dmatag, dh->dh_dmamap,
    563  1.1       pk 				dh->dh_dvma, dh->dh_maplen,
    564  1.1       pk 				(dh->dh_flags & SIDH_OUT)
    565  1.1       pk 					? BUS_DMASYNC_POSTWRITE
    566  1.1       pk 					: BUS_DMASYNC_POSTREAD);
    567  1.1       pk 		bus_dmamap_unload(sc->sc_dmatag, dh->dh_dmamap);
    568  1.1       pk 		dh->dh_flags = 0;
    569  1.1       pk 	}
    570  1.1       pk 	sr->sr_dma_hand = NULL;
    571  1.1       pk }
    572  1.1       pk 
    573  1.1       pk 
    574  1.1       pk /*
    575  1.1       pk  * Poll (spin-wait) for DMA completion.
    576  1.1       pk  * Called right after xx_dma_start(), and
    577  1.1       pk  * xx_dma_stop() will be called next.
    578  1.1       pk  * Same for either VME or OBIO.
    579  1.1       pk  */
    580  1.1       pk void
    581  1.1       pk si_dma_poll(ncr_sc)
    582  1.1       pk 	struct ncr5380_softc *ncr_sc;
    583  1.1       pk {
    584  1.1       pk 	struct sci_req *sr = ncr_sc->sc_current;
    585  1.1       pk 	int tmo, csr_mask, csr;
    586  1.1       pk 
    587  1.1       pk 	/* Make sure DMA started successfully. */
    588  1.1       pk 	if (ncr_sc->sc_state & NCR_ABORTING)
    589  1.1       pk 		return;
    590  1.1       pk 
    591  1.1       pk 	csr_mask = SI_CSR_SBC_IP | SI_CSR_DMA_IP |
    592  1.1       pk 		SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR;
    593  1.1       pk 
    594  1.1       pk 	tmo = 50000;	/* X100 = 5 sec. */
    595  1.1       pk 	for (;;) {
    596  1.1       pk 		csr = SIREG_READ(ncr_sc, SIREG_CSR);
    597  1.1       pk 		if (csr & csr_mask)
    598  1.1       pk 			break;
    599  1.1       pk 		if (--tmo <= 0) {
    600  1.1       pk 			printf("%s: DMA timeout (while polling)\n",
    601  1.1       pk 			    ncr_sc->sc_dev.dv_xname);
    602  1.1       pk 			/* Indicate timeout as MI code would. */
    603  1.1       pk 			sr->sr_flags |= SR_OVERDUE;
    604  1.1       pk 			break;
    605  1.1       pk 		}
    606  1.1       pk 		delay(100);
    607  1.1       pk 	}
    608  1.1       pk 
    609  1.1       pk #ifdef	DEBUG
    610  1.1       pk 	if (si_debug) {
    611  1.1       pk 		printf("si_dma_poll: done, csr=0x%x\n", csr);
    612  1.1       pk 	}
    613  1.1       pk #endif
    614  1.1       pk }
    615  1.1       pk 
    616  1.1       pk 
    617  1.1       pk /*****************************************************************
    618  1.1       pk  * VME functions for DMA
    619  1.1       pk  ****************************************************************/
    620  1.1       pk 
    621  1.1       pk 
    622  1.1       pk /*
    623  1.1       pk  * This is called when the bus is going idle,
    624  1.1       pk  * so we want to enable the SBC interrupts.
    625  1.1       pk  * That is controlled by the DMA enable!
    626  1.1       pk  * Who would have guessed!
    627  1.1       pk  * What a NASTY trick!
    628  1.1       pk  */
    629  1.1       pk void
    630  1.1       pk si_intr_on(ncr_sc)
    631  1.1       pk 	struct ncr5380_softc *ncr_sc;
    632  1.1       pk {
    633  1.1       pk 	u_int16_t csr;
    634  1.1       pk 
    635  1.1       pk 	si_dma_setup(ncr_sc);
    636  1.1       pk 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
    637  1.1       pk 	csr |= SI_CSR_DMA_EN;
    638  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    639  1.1       pk }
    640  1.1       pk 
    641  1.1       pk /*
    642  1.1       pk  * This is called when the bus is idle and we are
    643  1.1       pk  * about to start playing with the SBC chip.
    644  1.1       pk  */
    645  1.1       pk void
    646  1.1       pk si_intr_off(ncr_sc)
    647  1.1       pk 	struct ncr5380_softc *ncr_sc;
    648  1.1       pk {
    649  1.1       pk 	u_int16_t csr;
    650  1.1       pk 
    651  1.1       pk 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
    652  1.1       pk 	csr &= ~SI_CSR_DMA_EN;
    653  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    654  1.1       pk }
    655  1.1       pk 
    656  1.1       pk /*
    657  1.1       pk  * This function is called during the COMMAND or MSG_IN phase
    658  1.1       pk  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
    659  1.1       pk  * to setup the DMA engine before the bus enters a DATA phase.
    660  1.1       pk  *
    661  1.1       pk  * XXX: The VME adapter appears to suppress SBC interrupts
    662  1.1       pk  * when the FIFO is not empty or the FIFO count is non-zero!
    663  1.1       pk  *
    664  1.1       pk  * On the VME version we just clear the DMA count and address
    665  1.1       pk  * here (to make sure it stays idle) and do the real setup
    666  1.1       pk  * later, in dma_start.
    667  1.1       pk  */
    668  1.1       pk void
    669  1.1       pk si_dma_setup(ncr_sc)
    670  1.1       pk 	struct ncr5380_softc *ncr_sc;
    671  1.1       pk {
    672  1.1       pk 	u_int16_t csr;
    673  1.1       pk 
    674  1.1       pk 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
    675  1.1       pk 
    676  1.1       pk 	/* Reset the FIFO */
    677  1.1       pk 	csr &= ~SI_CSR_FIFO_RES;		/* active low */
    678  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    679  1.1       pk 	csr |= SI_CSR_FIFO_RES;
    680  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    681  1.1       pk 
    682  1.1       pk 	/* Set direction (assume recv here) */
    683  1.1       pk 	csr &= ~SI_CSR_SEND;
    684  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    685  1.1       pk 	/* Assume worst alignment */
    686  1.1       pk 	csr |= SI_CSR_BPCON;
    687  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    688  1.1       pk 
    689  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, 0);
    690  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, 0);
    691  1.1       pk 
    692  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
    693  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
    694  1.1       pk 
    695  1.1       pk 	/* Clear FIFO counter. (also hits dma_count) */
    696  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, 0);
    697  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, 0);
    698  1.1       pk }
    699  1.1       pk 
    700  1.1       pk 
    701  1.1       pk void
    702  1.1       pk si_dma_start(ncr_sc)
    703  1.1       pk 	struct ncr5380_softc *ncr_sc;
    704  1.1       pk {
    705  1.1       pk 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    706  1.1       pk 	struct sci_req *sr = ncr_sc->sc_current;
    707  1.1       pk 	struct si_dma_handle *dh = sr->sr_dma_hand;
    708  1.1       pk 	u_long dva;
    709  1.1       pk 	int xlen;
    710  1.1       pk 	u_int mode;
    711  1.1       pk 	u_int16_t csr;
    712  1.1       pk 
    713  1.1       pk 	/*
    714  1.1       pk 	 * Get the DVMA mapping for this segment.
    715  1.1       pk 	 */
    716  1.1       pk 	dva = (u_long)(dh->dh_dvma);
    717  1.1       pk 	if (dva & 1)
    718  1.1       pk 		panic("si_dma_start: bad dmaaddr=0x%lx", dva);
    719  1.1       pk 	xlen = ncr_sc->sc_datalen;
    720  1.1       pk 	xlen &= ~1;
    721  1.1       pk 	sc->sc_xlen = xlen;	/* XXX: or less... */
    722  1.1       pk 
    723  1.1       pk #ifdef	DEBUG
    724  1.1       pk 	if (si_debug & 2) {
    725  1.1       pk 		printf("si_dma_start: dh=%p, dmaaddr=0x%lx, xlen=%d\n",
    726  1.1       pk 			   dh, dva, xlen);
    727  1.1       pk 	}
    728  1.1       pk #endif
    729  1.1       pk 
    730  1.1       pk 	/*
    731  1.1       pk 	 * Set up the DMA controller.
    732  1.1       pk 	 * Note that (dh->dh_len < sc_datalen)
    733  1.1       pk 	 */
    734  1.1       pk 
    735  1.1       pk 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
    736  1.1       pk 
    737  1.1       pk 	/* Disable DMA while we're setting up the transfer */
    738  1.1       pk 	csr &= ~SI_CSR_DMA_EN;
    739  1.1       pk 
    740  1.1       pk 	/* Reset FIFO (again?) */
    741  1.1       pk 	csr &= ~SI_CSR_FIFO_RES;		/* active low */
    742  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    743  1.1       pk 	csr |= SI_CSR_FIFO_RES;
    744  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    745  1.1       pk 
    746  1.1       pk 	/* Set direction (send/recv) */
    747  1.1       pk 	if (dh->dh_flags & SIDH_OUT) {
    748  1.1       pk 		csr |= SI_CSR_SEND;
    749  1.1       pk 	} else {
    750  1.1       pk 		csr &= ~SI_CSR_SEND;
    751  1.1       pk 	}
    752  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    753  1.1       pk 
    754  1.1       pk 	if (dva & 2) {
    755  1.1       pk 		csr |= SI_CSR_BPCON;
    756  1.1       pk 	} else {
    757  1.1       pk 		csr &= ~SI_CSR_BPCON;
    758  1.1       pk 	}
    759  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    760  1.1       pk 
    761  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, (u_int16_t)(dva >> 16));
    762  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, (u_int16_t)(dva & 0xFFFF));
    763  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, (u_int16_t)(xlen >> 16));
    764  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, (u_int16_t)(xlen & 0xFFFF));
    765  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, (u_int16_t)(xlen >> 16));
    766  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, (u_int16_t)(xlen & 0xFFFF));
    767  1.1       pk 
    768  1.1       pk 	/*
    769  1.1       pk 	 * Acknowledge the phase change.  (After DMA setup!)
    770  1.1       pk 	 * Put the SBIC into DMA mode, and start the transfer.
    771  1.1       pk 	 */
    772  1.1       pk 	if (dh->dh_flags & SIDH_OUT) {
    773  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
    774  1.1       pk 		SCI_CLR_INTR(ncr_sc);
    775  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
    776  1.1       pk 
    777  1.1       pk 		mode = NCR5380_READ(ncr_sc, sci_mode);
    778  1.1       pk 		mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    779  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_mode, mode);
    780  1.1       pk 
    781  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_dma_send, 0); /* start it */
    782  1.1       pk 	} else {
    783  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
    784  1.1       pk 		SCI_CLR_INTR(ncr_sc);
    785  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_icmd, 0);
    786  1.1       pk 
    787  1.1       pk 		mode = NCR5380_READ(ncr_sc, sci_mode);
    788  1.1       pk 		mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    789  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_mode, mode);
    790  1.1       pk 
    791  1.1       pk 		NCR5380_WRITE(ncr_sc, sci_irecv, 0); /* start it */
    792  1.1       pk 	}
    793  1.1       pk 
    794  1.1       pk 	/* Enable DMA engine */
    795  1.1       pk 	csr |= SI_CSR_DMA_EN;
    796  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    797  1.1       pk 
    798  1.1       pk 	ncr_sc->sc_state |= NCR_DOINGDMA;
    799  1.1       pk 
    800  1.1       pk #ifdef	DEBUG
    801  1.1       pk 	if (si_debug & 2) {
    802  1.1       pk 		printf("si_dma_start: started, flags=0x%x\n",
    803  1.1       pk 			   ncr_sc->sc_state);
    804  1.1       pk 	}
    805  1.1       pk #endif
    806  1.1       pk }
    807  1.1       pk 
    808  1.1       pk 
    809  1.1       pk void
    810  1.1       pk si_dma_eop(ncr_sc)
    811  1.1       pk 	struct ncr5380_softc *ncr_sc;
    812  1.1       pk {
    813  1.1       pk 
    814  1.1       pk 	/* Not needed - DMA was stopped prior to examining sci_csr */
    815  1.1       pk }
    816  1.1       pk 
    817  1.1       pk 
    818  1.1       pk void
    819  1.1       pk si_dma_stop(ncr_sc)
    820  1.1       pk 	struct ncr5380_softc *ncr_sc;
    821  1.1       pk {
    822  1.1       pk 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    823  1.1       pk 	struct sci_req *sr = ncr_sc->sc_current;
    824  1.1       pk 	struct si_dma_handle *dh = sr->sr_dma_hand;
    825  1.1       pk 	int resid, ntrans;
    826  1.1       pk 	u_int16_t csr;
    827  1.1       pk 	u_int mode;
    828  1.1       pk 
    829  1.1       pk 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    830  1.1       pk #ifdef	DEBUG
    831  1.1       pk 		printf("si_dma_stop: dma not running\n");
    832  1.1       pk #endif
    833  1.1       pk 		return;
    834  1.1       pk 	}
    835  1.1       pk 
    836  1.1       pk 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    837  1.1       pk 
    838  1.1       pk 	csr = SIREG_READ(ncr_sc, SIREG_CSR);
    839  1.1       pk 
    840  1.1       pk 	/* First, halt the DMA engine. */
    841  1.1       pk 	csr &= ~SI_CSR_DMA_EN;
    842  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_CSR, csr);
    843  1.1       pk 
    844  1.1       pk 	if (csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
    845  1.1       pk 		printf("si: DMA error, csr=0x%x, reset\n", csr);
    846  1.1       pk 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    847  1.1       pk 		ncr_sc->sc_state |= NCR_ABORTING;
    848  1.1       pk 		si_reset_adapter(ncr_sc);
    849  1.1       pk 	}
    850  1.1       pk 
    851  1.1       pk 	/* Note that timeout may have set the error flag. */
    852  1.1       pk 	if (ncr_sc->sc_state & NCR_ABORTING)
    853  1.1       pk 		goto out;
    854  1.1       pk 
    855  1.1       pk 	/*
    856  1.1       pk 	 * Now try to figure out how much actually transferred
    857  1.1       pk 	 *
    858  1.1       pk 	 * The fifo_count does not reflect how many bytes were
    859  1.1       pk 	 * actually transferred for VME.
    860  1.1       pk 	 *
    861  1.1       pk 	 * SCSI-3 VME interface is a little funny on writes:
    862  1.1       pk 	 * if we have a disconnect, the dma has overshot by
    863  1.1       pk 	 * one byte and the resid needs to be incremented.
    864  1.1       pk 	 * Only happens for partial transfers.
    865  1.1       pk 	 * (Thanks to Matt Jacob)
    866  1.1       pk 	 */
    867  1.1       pk 
    868  1.1       pk 	resid = SIREG_READ(ncr_sc, SIREG_FIFO_CNTH) << 16;
    869  1.1       pk 	resid |= SIREG_READ(ncr_sc, SIREG_FIFO_CNT) & 0xFFFF;
    870  1.1       pk 	if (dh->dh_flags & SIDH_OUT)
    871  1.1       pk 		if ((resid > 0) && (resid < sc->sc_xlen))
    872  1.1       pk 			resid++;
    873  1.1       pk 	ntrans = sc->sc_xlen - resid;
    874  1.1       pk 
    875  1.1       pk #ifdef	DEBUG
    876  1.1       pk 	if (si_debug & 2) {
    877  1.1       pk 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
    878  1.1       pk 		    resid, ntrans);
    879  1.1       pk 	}
    880  1.1       pk #endif
    881  1.1       pk 
    882  1.1       pk 	if (ntrans > ncr_sc->sc_datalen)
    883  1.1       pk 		panic("si_dma_stop: excess transfer");
    884  1.1       pk 
    885  1.1       pk 	/* Adjust data pointer */
    886  1.1       pk 	ncr_sc->sc_dataptr += ntrans;
    887  1.1       pk 	ncr_sc->sc_datalen -= ntrans;
    888  1.1       pk 
    889  1.1       pk #ifdef	DEBUG
    890  1.1       pk 	if (si_debug & 2) {
    891  1.1       pk 		printf("si_dma_stop: ntrans=0x%x\n", ntrans);
    892  1.1       pk 	}
    893  1.1       pk #endif
    894  1.1       pk 
    895  1.1       pk 	/*
    896  1.1       pk 	 * After a read, we may need to clean-up
    897  1.1       pk 	 * "Left-over bytes" (yuck!)
    898  1.1       pk 	 */
    899  1.1       pk 	if (((dh->dh_flags & SIDH_OUT) == 0) &&
    900  1.1       pk 		((csr & SI_CSR_LOB) != 0))
    901  1.1       pk 	{
    902  1.1       pk 		char *cp = ncr_sc->sc_dataptr;
    903  1.1       pk 		u_int16_t bprh, bprl;
    904  1.1       pk 
    905  1.1       pk 		bprh = SIREG_READ(ncr_sc, SIREG_BPRH);
    906  1.1       pk 		bprl = SIREG_READ(ncr_sc, SIREG_BPRL);
    907  1.1       pk 
    908  1.1       pk #ifdef DEBUG
    909  1.1       pk 		printf("si: got left-over bytes: bprh=%x, bprl=%x, csr=%x\n",
    910  1.1       pk 			bprh, bprl, csr);
    911  1.1       pk #endif
    912  1.1       pk 
    913  1.1       pk 		if (csr & SI_CSR_BPCON) {
    914  1.1       pk 			/* have SI_CSR_BPCON */
    915  1.1       pk 			cp[-1] = (bprl & 0xff00) >> 8;
    916  1.1       pk 		} else {
    917  1.1       pk 			switch (csr & SI_CSR_LOB) {
    918  1.1       pk 			case SI_CSR_LOB_THREE:
    919  1.1       pk 				cp[-3] = (bprh & 0xff00) >> 8;
    920  1.1       pk 				cp[-2] = (bprh & 0x00ff);
    921  1.1       pk 				cp[-1] = (bprl & 0xff00) >> 8;
    922  1.1       pk 				break;
    923  1.1       pk 			case SI_CSR_LOB_TWO:
    924  1.1       pk 				cp[-2] = (bprh & 0xff00) >> 8;
    925  1.1       pk 				cp[-1] = (bprh & 0x00ff);
    926  1.1       pk 				break;
    927  1.1       pk 			case SI_CSR_LOB_ONE:
    928  1.1       pk 				cp[-1] = (bprh & 0xff00) >> 8;
    929  1.1       pk 				break;
    930  1.1       pk 			}
    931  1.1       pk 		}
    932  1.1       pk 	}
    933  1.1       pk 
    934  1.1       pk out:
    935  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRH, 0);
    936  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_ADDRL, 0);
    937  1.1       pk 
    938  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTH, 0);
    939  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_DMA_CNTL, 0);
    940  1.1       pk 
    941  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNTH, 0);
    942  1.1       pk 	SIREG_WRITE(ncr_sc, SIREG_FIFO_CNT, 0);
    943  1.1       pk 
    944  1.1       pk 	mode = NCR5380_READ(ncr_sc, sci_mode);
    945  1.1       pk 	/* Put SBIC back in PIO mode. */
    946  1.1       pk 	mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    947  1.1       pk 	NCR5380_WRITE(ncr_sc, sci_mode, mode);
    948  1.1       pk 	NCR5380_WRITE(ncr_sc, sci_icmd, 0);
    949  1.1       pk }
    950