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