Home | History | Annotate | Line # | Download | only in dev
si_vme.c revision 1.7
      1 /*	$NetBSD: si_vme.c,v 1.7 1996/11/20 18:57:01 gwr Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Adam Glass, David Jones, and Gordon W. Ross.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
     30  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * This file contains only the machine-dependent parts of the
     41  * Sun3 SCSI driver.  (Autoconfig stuff and DMA functions.)
     42  * The machine-independent parts are in ncr5380sbc.c
     43  *
     44  * Supported hardware includes:
     45  * Sun SCSI-3 on OBIO (Sun3/50,Sun3/60)
     46  * Sun SCSI-3 on VME (Sun3/160,Sun3/260)
     47  *
     48  * Could be made to support the Sun3/E if someone wanted to.
     49  *
     50  * Note:  Both supported variants of the Sun SCSI-3 adapter have
     51  * some really unusual "features" for this driver to deal with,
     52  * generally related to the DMA engine.  The OBIO variant will
     53  * ignore any attempt to write the FIFO count register while the
     54  * SCSI bus is in DATA_IN or DATA_OUT phase.  This is dealt with
     55  * by setting the FIFO count early in COMMAND or MSG_IN phase.
     56  *
     57  * The VME variant has a bit to enable or disable the DMA engine,
     58  * but that bit also gates the interrupt line from the NCR5380!
     59  * Therefore, in order to get any interrupt from the 5380, (i.e.
     60  * for reselect) one must clear the DMA engine transfer count and
     61  * then enable DMA.  This has the further complication that you
     62  * CAN NOT touch the NCR5380 while the DMA enable bit is set, so
     63  * we have to turn DMA back off before we even look at the 5380.
     64  *
     65  * What wonderfully whacky hardware this is!
     66  *
     67  * Credits, history:
     68  *
     69  * David Jones wrote the initial version of this module, which
     70  * included support for the VME adapter only. (no reselection).
     71  *
     72  * Gordon Ross added support for the OBIO adapter, and re-worked
     73  * both the VME and OBIO code to support disconnect/reselect.
     74  * (Required figuring out the hardware "features" noted above.)
     75  *
     76  * The autoconfiguration boilerplate came from Adam Glass.
     77  */
     78 
     79 /*****************************************************************
     80  * VME functions for DMA
     81  ****************************************************************/
     82 
     83 #include <sys/param.h>
     84 #include <sys/systm.h>
     85 #include <sys/errno.h>
     86 #include <sys/kernel.h>
     87 #include <sys/malloc.h>
     88 #include <sys/device.h>
     89 #include <sys/buf.h>
     90 #include <sys/proc.h>
     91 #include <sys/user.h>
     92 
     93 #include <scsi/scsi_all.h>
     94 #include <scsi/scsi_debug.h>
     95 #include <scsi/scsiconf.h>
     96 
     97 #include <machine/autoconf.h>
     98 #include <machine/isr.h>
     99 #include <machine/obio.h>
    100 #include <machine/dvma.h>
    101 
    102 #define DEBUG XXX
    103 
    104 #include <dev/ic/ncr5380reg.h>
    105 #include <dev/ic/ncr5380var.h>
    106 
    107 #include "sireg.h"
    108 #include "sivar.h"
    109 
    110 void si_vme_dma_setup __P((struct ncr5380_softc *));
    111 void si_vme_dma_start __P((struct ncr5380_softc *));
    112 void si_vme_dma_eop __P((struct ncr5380_softc *));
    113 void si_vme_dma_stop __P((struct ncr5380_softc *));
    114 
    115 void si_vme_intr_on  __P((struct ncr5380_softc *));
    116 void si_vme_intr_off __P((struct ncr5380_softc *));
    117 
    118 /*
    119  * New-style autoconfig attachment
    120  */
    121 
    122 static int	si_vmes_match __P((struct device *, void *, void *));
    123 static void	si_vmes_attach __P((struct device *, struct device *, void *));
    124 
    125 struct cfattach si_vmes_ca = {
    126 	sizeof(struct si_softc), si_vmes_match, si_vmes_attach
    127 };
    128 
    129 /* Options.  Interesting values are: 1,3,7 */
    130 int si_vme_options = 3;
    131 
    132 
    133 static int
    134 si_vmes_match(parent, vcf, args)
    135 	struct device	*parent;
    136 	void		*vcf, *args;
    137 {
    138 	struct cfdata	*cf = vcf;
    139 	struct confargs *ca = args;
    140 	int probe_addr;
    141 
    142 #ifdef	DIAGNOSTIC
    143 	if (ca->ca_bustype != BUS_VME16) {
    144 		printf("si_vmes_match: bustype %d?\n", ca->ca_bustype);
    145 		return (0);
    146 	}
    147 #endif
    148 
    149 	/*
    150 	 * Other Sun3 models may have VME "si" or "sc".
    151 	 * This driver has no default address.
    152 	 */
    153 	if (ca->ca_paddr == -1)
    154 		return (0);
    155 
    156 	/* Make sure there is something there... */
    157 	probe_addr = ca->ca_paddr + 1;
    158 	if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
    159 		return (0);
    160 
    161 	/*
    162 	 * If this is a VME SCSI board, we have to determine whether
    163 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
    164 	 * be determined using the fact that the "sc" board occupies
    165 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
    166 	 */
    167 	/* Note: the "si" board should NOT respond here. */
    168 	probe_addr = ca->ca_paddr + 0x801;
    169 	if (bus_peek(ca->ca_bustype, probe_addr, 1) != -1) {
    170 		/* Something responded at 2K+1.  Maybe an "sc" board? */
    171 #ifdef	DEBUG
    172 		printf("si_vmes_match: May be an `sc' board at pa=0x%x\n",
    173 			   ca->ca_paddr);
    174 #endif
    175 		return(0);
    176 	}
    177 
    178 	/* Default interrupt priority (always splbio==2) */
    179 	if (ca->ca_intpri == -1)
    180 		ca->ca_intpri = 2;
    181 
    182 	return (1);
    183 }
    184 
    185 static void
    186 si_vmes_attach(parent, self, args)
    187 	struct device	*parent, *self;
    188 	void		*args;
    189 {
    190 	struct si_softc *sc = (struct si_softc *) self;
    191 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    192 	struct cfdata *cf = self->dv_cfdata;
    193 	struct confargs *ca = args;
    194 
    195 	/* Get options from config flags... */
    196 	sc->sc_options = cf->cf_flags | si_vme_options;
    197 	printf(": options=%d\n", sc->sc_options);
    198 
    199 	sc->sc_adapter_type = ca->ca_bustype;
    200 	sc->sc_regs = (struct si_regs *)
    201 		bus_mapin(ca->ca_bustype, ca->ca_paddr,
    202 				sizeof(struct si_regs));
    203 	sc->sc_adapter_iv_am =
    204 		VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF);
    205 
    206 	/*
    207 	 * MD function pointers used by the MI code.
    208 	 */
    209 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    210 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    211 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    212 	ncr_sc->sc_dma_free  = si_dma_free;
    213 	ncr_sc->sc_dma_setup = si_vme_dma_setup;
    214 	ncr_sc->sc_dma_start = si_vme_dma_start;
    215 	ncr_sc->sc_dma_poll  = si_dma_poll;
    216 	ncr_sc->sc_dma_eop   = si_vme_dma_eop;
    217 	ncr_sc->sc_dma_stop  = si_vme_dma_stop;
    218 	ncr_sc->sc_intr_on   = si_vme_intr_on;
    219 	ncr_sc->sc_intr_off  = si_vme_intr_off;
    220 
    221 	/* Attach interrupt handler. */
    222 	isr_add_vectored(si_intr, (void *)sc,
    223 		ca->ca_intpri, ca->ca_intvec);
    224 
    225 	/* Do the common attach stuff. */
    226 	si_attach(sc);
    227 }
    228 
    229 
    230 /*
    231  * This is called when the bus is going idle,
    232  * so we want to enable the SBC interrupts.
    233  * That is controlled by the DMA enable!
    234  * Who would have guessed!
    235  * What a NASTY trick!
    236  */
    237 void
    238 si_vme_intr_on(ncr_sc)
    239 	struct ncr5380_softc *ncr_sc;
    240 {
    241 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    242 	volatile struct si_regs *si = sc->sc_regs;
    243 
    244 	/* receive mode should be safer */
    245 	si->si_csr &= ~SI_CSR_SEND;
    246 
    247 	/* Clear the count so nothing happens. */
    248 	si->dma_counth = 0;
    249 	si->dma_countl = 0;
    250 
    251 	/* Clear the start address too. (paranoid?) */
    252 	si->dma_addrh = 0;
    253 	si->dma_addrl = 0;
    254 
    255 	/* Finally, enable the DMA engine. */
    256 	si->si_csr |= SI_CSR_DMA_EN;
    257 }
    258 
    259 /*
    260  * This is called when the bus is idle and we are
    261  * about to start playing with the SBC chip.
    262  */
    263 void
    264 si_vme_intr_off(ncr_sc)
    265 	struct ncr5380_softc *ncr_sc;
    266 {
    267 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    268 	volatile struct si_regs *si = sc->sc_regs;
    269 
    270 	si->si_csr &= ~SI_CSR_DMA_EN;
    271 }
    272 
    273 /*
    274  * This function is called during the COMMAND or MSG_IN phase
    275  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
    276  * to setup the DMA engine before the bus enters a DATA phase.
    277  *
    278  * XXX: The VME adapter appears to suppress SBC interrupts
    279  * when the FIFO is not empty or the FIFO count is non-zero!
    280  *
    281  * On the VME version, setup the start addres, but clear the
    282  * count (to make sure it stays idle) and set that later.
    283  */
    284 void
    285 si_vme_dma_setup(ncr_sc)
    286 	struct ncr5380_softc *ncr_sc;
    287 {
    288 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    289 	struct sci_req *sr = ncr_sc->sc_current;
    290 	struct si_dma_handle *dh = sr->sr_dma_hand;
    291 	volatile struct si_regs *si = sc->sc_regs;
    292 	long data_pa;
    293 	int xlen;
    294 
    295 	/*
    296 	 * Get the DVMA mapping for this segment.
    297 	 * XXX - Should separate allocation and mapin.
    298 	 */
    299 	data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
    300 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
    301 	if (data_pa & 1)
    302 		panic("si_dma_start: bad pa=0x%x", data_pa);
    303 	xlen = ncr_sc->sc_datalen;
    304 	xlen &= ~1;				/* XXX: necessary? */
    305 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
    306 
    307 #ifdef	DEBUG
    308 	if (si_debug & 2) {
    309 		printf("si_dma_setup: dh=0x%x, pa=0x%x, xlen=%d\n",
    310 			   dh, data_pa, xlen);
    311 	}
    312 #endif
    313 
    314 	/* Set direction (send/recv) */
    315 	if (dh->dh_flags & SIDH_OUT) {
    316 		si->si_csr |= SI_CSR_SEND;
    317 	} else {
    318 		si->si_csr &= ~SI_CSR_SEND;
    319 	}
    320 
    321 	/* Reset the FIFO. */
    322 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    323 	si->si_csr |= SI_CSR_FIFO_RES;
    324 
    325 	if (data_pa & 2) {
    326 		si->si_csr |= SI_CSR_BPCON;
    327 	} else {
    328 		si->si_csr &= ~SI_CSR_BPCON;
    329 	}
    330 
    331 	/* Load the start address. */
    332 	si->dma_addrh = (ushort)(data_pa >> 16);
    333 	si->dma_addrl = (ushort)(data_pa & 0xFFFF);
    334 
    335 	/*
    336 	 * Keep the count zero or it may start early!
    337 	 */
    338 	si->dma_counth = 0;
    339 	si->dma_countl = 0;
    340 
    341 #if 0
    342 	/* Clear FIFO counter. (also hits dma_count) */
    343 	si->fifo_cnt_hi = 0;
    344 	si->fifo_count = 0;
    345 #endif
    346 }
    347 
    348 
    349 void
    350 si_vme_dma_start(ncr_sc)
    351 	struct ncr5380_softc *ncr_sc;
    352 {
    353 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    354 	struct sci_req *sr = ncr_sc->sc_current;
    355 	struct si_dma_handle *dh = sr->sr_dma_hand;
    356 	volatile struct si_regs *si = sc->sc_regs;
    357 	long data_pa;
    358 	int s, xlen;
    359 
    360 	xlen = sc->sc_reqlen;
    361 
    362 	/* This MAY be time critical (not sure). */
    363 	s = splhigh();
    364 
    365 	si->dma_counth = (ushort)(xlen >> 16);
    366 	si->dma_countl = (ushort)(xlen & 0xFFFF);
    367 
    368 	/* Set it anyway, even though dma_count hits it. */
    369 	si->fifo_cnt_hi = (ushort)(xlen >> 16);
    370 	si->fifo_count  = (ushort)(xlen & 0xFFFF);
    371 
    372 	/*
    373 	 * Acknowledge the phase change.  (After DMA setup!)
    374 	 * Put the SBIC into DMA mode, and start the transfer.
    375 	 */
    376 	if (dh->dh_flags & SIDH_OUT) {
    377 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    378 		SCI_CLR_INTR(ncr_sc);
    379 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    380 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    381 		*ncr_sc->sci_dma_send = 0;	/* start it */
    382 	} else {
    383 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    384 		SCI_CLR_INTR(ncr_sc);
    385 		*ncr_sc->sci_icmd = 0;
    386 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    387 		*ncr_sc->sci_irecv = 0;	/* start it */
    388 	}
    389 
    390 	/* Let'er rip! */
    391 	si->si_csr |= SI_CSR_DMA_EN;
    392 
    393 	splx(s);
    394 	ncr_sc->sc_state |= NCR_DOINGDMA;
    395 
    396 #ifdef	DEBUG
    397 	if (si_debug & 2) {
    398 		printf("si_dma_start: started, flags=0x%x\n",
    399 			   ncr_sc->sc_state);
    400 	}
    401 #endif
    402 }
    403 
    404 
    405 void
    406 si_vme_dma_eop(ncr_sc)
    407 	struct ncr5380_softc *ncr_sc;
    408 {
    409 
    410 	/* Not needed - DMA was stopped prior to examining sci_csr */
    411 }
    412 
    413 
    414 void
    415 si_vme_dma_stop(ncr_sc)
    416 	struct ncr5380_softc *ncr_sc;
    417 {
    418 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    419 	struct sci_req *sr = ncr_sc->sc_current;
    420 	struct si_dma_handle *dh = sr->sr_dma_hand;
    421 	volatile struct si_regs *si = sc->sc_regs;
    422 	int resid, ntrans;
    423 
    424 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    425 #ifdef	DEBUG
    426 		printf("si_dma_stop: dma not running\n");
    427 #endif
    428 		return;
    429 	}
    430 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    431 
    432 	/* First, halt the DMA engine. */
    433 	si->si_csr &= ~SI_CSR_DMA_EN;	/* VME only */
    434 
    435 	/* Set an impossible phase to prevent data movement? */
    436 	*ncr_sc->sci_tcmd = PHASE_INVALID;
    437 
    438 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
    439 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
    440 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    441 		ncr_sc->sc_state |= NCR_ABORTING;
    442 		si_reset_adapter(ncr_sc);
    443 		goto out;
    444 	}
    445 
    446 	/* Note that timeout may have set the error flag. */
    447 	if (ncr_sc->sc_state & NCR_ABORTING)
    448 		goto out;
    449 
    450 	/* XXX: Wait for DMA to actually finish? */
    451 
    452 	/*
    453 	 * Now try to figure out how much actually transferred
    454 	 *
    455 	 * The fifo_count does not reflect how many bytes were
    456 	 * actually transferred for VME.
    457 	 *
    458 	 * SCSI-3 VME interface is a little funny on writes:
    459 	 * if we have a disconnect, the dma has overshot by
    460 	 * one byte and the resid needs to be incremented.
    461 	 * Only happens for partial transfers.
    462 	 * (Thanks to Matt Jacob)
    463 	 */
    464 
    465 	resid = si->fifo_count & 0xFFFF;
    466 	if (dh->dh_flags & SIDH_OUT)
    467 		if ((resid > 0) && (resid < sc->sc_reqlen))
    468 			resid++;
    469 	ntrans = sc->sc_reqlen - resid;
    470 
    471 #ifdef	DEBUG
    472 	if (si_debug & 2) {
    473 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
    474 		       resid, ntrans);
    475 	}
    476 #endif
    477 
    478 	if (ntrans < MIN_DMA_LEN) {
    479 		printf("si: fifo count: 0x%x\n", resid);
    480 		ncr_sc->sc_state |= NCR_ABORTING;
    481 		goto out;
    482 	}
    483 	if (ntrans > ncr_sc->sc_datalen)
    484 		panic("si_dma_stop: excess transfer");
    485 
    486 	/* Adjust data pointer */
    487 	ncr_sc->sc_dataptr += ntrans;
    488 	ncr_sc->sc_datalen -= ntrans;
    489 
    490 	/*
    491 	 * After a read, we may need to clean-up
    492 	 * "Left-over bytes" (yuck!)
    493 	 */
    494 	if (((dh->dh_flags & SIDH_OUT) == 0) &&
    495 		((si->si_csr & SI_CSR_LOB) != 0))
    496 	{
    497 		char *cp = ncr_sc->sc_dataptr;
    498 #ifdef DEBUG
    499 		printf("si: Got Left-over bytes!\n");
    500 #endif
    501 		if (si->si_csr & SI_CSR_BPCON) {
    502 			/* have SI_CSR_BPCON */
    503 			cp[-1] = (si->si_bprl & 0xff00) >> 8;
    504 		} else {
    505 			switch (si->si_csr & SI_CSR_LOB) {
    506 			case SI_CSR_LOB_THREE:
    507 				cp[-3] = (si->si_bprh & 0xff00) >> 8;
    508 				cp[-2] = (si->si_bprh & 0x00ff);
    509 				cp[-1] = (si->si_bprl & 0xff00) >> 8;
    510 				break;
    511 			case SI_CSR_LOB_TWO:
    512 				cp[-2] = (si->si_bprh & 0xff00) >> 8;
    513 				cp[-1] = (si->si_bprh & 0x00ff);
    514 				break;
    515 			case SI_CSR_LOB_ONE:
    516 				cp[-1] = (si->si_bprh & 0xff00) >> 8;
    517 				break;
    518 			}
    519 		}
    520 	}
    521 
    522 out:
    523 	si->dma_addrh = 0;
    524 	si->dma_addrl = 0;
    525 
    526 	si->dma_counth = 0;
    527 	si->dma_countl = 0;
    528 
    529 	si->fifo_cnt_hi = 0;
    530 	si->fifo_count  = 0;
    531 
    532 	/* Put SBIC back in PIO mode. */
    533 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    534 	*ncr_sc->sci_icmd = 0;
    535 }
    536