Home | History | Annotate | Line # | Download | only in dev
si_vme.c revision 1.9
      1 /*	$NetBSD: si_vme.c,v 1.9 1997/01/27 19:40:55 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 FOUNDATION OR CONTRIBUTORS
     30  * BE 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/dvma.h>
     99 
    100 #define DEBUG XXX
    101 
    102 #include <dev/ic/ncr5380reg.h>
    103 #include <dev/ic/ncr5380var.h>
    104 
    105 #include "sireg.h"
    106 #include "sivar.h"
    107 
    108 void si_vme_dma_setup __P((struct ncr5380_softc *));
    109 void si_vme_dma_start __P((struct ncr5380_softc *));
    110 void si_vme_dma_eop __P((struct ncr5380_softc *));
    111 void si_vme_dma_stop __P((struct ncr5380_softc *));
    112 
    113 void si_vme_intr_on  __P((struct ncr5380_softc *));
    114 void si_vme_intr_off __P((struct ncr5380_softc *));
    115 
    116 /*
    117  * New-style autoconfig attachment
    118  */
    119 
    120 static int	si_vmes_match __P((struct device *, struct cfdata *, void *));
    121 static void	si_vmes_attach __P((struct device *, struct device *, void *));
    122 
    123 struct cfattach si_vmes_ca = {
    124 	sizeof(struct si_softc), si_vmes_match, si_vmes_attach
    125 };
    126 
    127 /* Options.  Interesting values are: 1,3,7 */
    128 int si_vme_options = 3;
    129 
    130 
    131 static int
    132 si_vmes_match(parent, cf, args)
    133 	struct device	*parent;
    134 	struct cfdata *cf;
    135 	void *args;
    136 {
    137 	struct confargs *ca = args;
    138 	int probe_addr;
    139 
    140 #ifdef	DIAGNOSTIC
    141 	if (ca->ca_bustype != BUS_VME16) {
    142 		printf("si_vmes_match: bustype %d?\n", ca->ca_bustype);
    143 		return (0);
    144 	}
    145 #endif
    146 
    147 	/*
    148 	 * Other Sun3 models may have VME "si" or "sc".
    149 	 * This driver has no default address.
    150 	 */
    151 	if (ca->ca_paddr == -1)
    152 		return (0);
    153 
    154 	/* Make sure there is something there... */
    155 	probe_addr = ca->ca_paddr + 1;
    156 	if (bus_peek(ca->ca_bustype, probe_addr, 1) == -1)
    157 		return (0);
    158 
    159 	/*
    160 	 * If this is a VME SCSI board, we have to determine whether
    161 	 * it is an "sc" (Sun2) or "si" (Sun3) SCSI board.  This can
    162 	 * be determined using the fact that the "sc" board occupies
    163 	 * 4K bytes in VME space but the "si" board occupies 2K bytes.
    164 	 */
    165 	/* Note: the "si" board should NOT respond here. */
    166 	probe_addr = ca->ca_paddr + 0x801;
    167 	if (bus_peek(ca->ca_bustype, probe_addr, 1) != -1) {
    168 		/* Something responded at 2K+1.  Maybe an "sc" board? */
    169 #ifdef	DEBUG
    170 		printf("si_vmes_match: May be an `sc' board at pa=0x%x\n",
    171 			   ca->ca_paddr);
    172 #endif
    173 		return(0);
    174 	}
    175 
    176 	/* Default interrupt priority (always splbio==2) */
    177 	if (ca->ca_intpri == -1)
    178 		ca->ca_intpri = 2;
    179 
    180 	return (1);
    181 }
    182 
    183 static void
    184 si_vmes_attach(parent, self, args)
    185 	struct device	*parent, *self;
    186 	void		*args;
    187 {
    188 	struct si_softc *sc = (struct si_softc *) self;
    189 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    190 	struct cfdata *cf = self->dv_cfdata;
    191 	struct confargs *ca = args;
    192 
    193 	/* Get options from config flags... */
    194 	sc->sc_options = cf->cf_flags | si_vme_options;
    195 	printf(": options=%d\n", sc->sc_options);
    196 
    197 	sc->sc_adapter_type = ca->ca_bustype;
    198 	sc->sc_regs = (struct si_regs *)
    199 		bus_mapin(ca->ca_bustype, ca->ca_paddr,
    200 				sizeof(struct si_regs));
    201 	sc->sc_adapter_iv_am =
    202 		VME_SUPV_DATA_24 | (ca->ca_intvec & 0xFF);
    203 
    204 	/*
    205 	 * MD function pointers used by the MI code.
    206 	 */
    207 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    208 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    209 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    210 	ncr_sc->sc_dma_free  = si_dma_free;
    211 	ncr_sc->sc_dma_setup = si_vme_dma_setup;
    212 	ncr_sc->sc_dma_start = si_vme_dma_start;
    213 	ncr_sc->sc_dma_poll  = si_dma_poll;
    214 	ncr_sc->sc_dma_eop   = si_vme_dma_eop;
    215 	ncr_sc->sc_dma_stop  = si_vme_dma_stop;
    216 	ncr_sc->sc_intr_on   = si_vme_intr_on;
    217 	ncr_sc->sc_intr_off  = si_vme_intr_off;
    218 
    219 	/* Attach interrupt handler. */
    220 	isr_add_vectored(si_intr, (void *)sc,
    221 		ca->ca_intpri, ca->ca_intvec);
    222 
    223 	/* Do the common attach stuff. */
    224 	si_attach(sc);
    225 }
    226 
    227 
    228 /*
    229  * This is called when the bus is going idle,
    230  * so we want to enable the SBC interrupts.
    231  * That is controlled by the DMA enable!
    232  * Who would have guessed!
    233  * What a NASTY trick!
    234  */
    235 void
    236 si_vme_intr_on(ncr_sc)
    237 	struct ncr5380_softc *ncr_sc;
    238 {
    239 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    240 	volatile struct si_regs *si = sc->sc_regs;
    241 
    242 	/* receive mode should be safer */
    243 	si->si_csr &= ~SI_CSR_SEND;
    244 
    245 	/* Clear the count so nothing happens. */
    246 	si->dma_counth = 0;
    247 	si->dma_countl = 0;
    248 
    249 	/* Clear the start address too. (paranoid?) */
    250 	si->dma_addrh = 0;
    251 	si->dma_addrl = 0;
    252 
    253 	/* Finally, enable the DMA engine. */
    254 	si->si_csr |= SI_CSR_DMA_EN;
    255 }
    256 
    257 /*
    258  * This is called when the bus is idle and we are
    259  * about to start playing with the SBC chip.
    260  */
    261 void
    262 si_vme_intr_off(ncr_sc)
    263 	struct ncr5380_softc *ncr_sc;
    264 {
    265 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    266 	volatile struct si_regs *si = sc->sc_regs;
    267 
    268 	si->si_csr &= ~SI_CSR_DMA_EN;
    269 }
    270 
    271 /*
    272  * This function is called during the COMMAND or MSG_IN phase
    273  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
    274  * to setup the DMA engine before the bus enters a DATA phase.
    275  *
    276  * XXX: The VME adapter appears to suppress SBC interrupts
    277  * when the FIFO is not empty or the FIFO count is non-zero!
    278  *
    279  * On the VME version, setup the start addres, but clear the
    280  * count (to make sure it stays idle) and set that later.
    281  */
    282 void
    283 si_vme_dma_setup(ncr_sc)
    284 	struct ncr5380_softc *ncr_sc;
    285 {
    286 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    287 	struct sci_req *sr = ncr_sc->sc_current;
    288 	struct si_dma_handle *dh = sr->sr_dma_hand;
    289 	volatile struct si_regs *si = sc->sc_regs;
    290 	long data_pa;
    291 	int xlen;
    292 
    293 	/*
    294 	 * Get the DVMA mapping for this segment.
    295 	 * XXX - Should separate allocation and mapin.
    296 	 */
    297 	data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
    298 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
    299 	if (data_pa & 1)
    300 		panic("si_dma_start: bad pa=0x%x", data_pa);
    301 	xlen = ncr_sc->sc_datalen;
    302 	xlen &= ~1;				/* XXX: necessary? */
    303 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
    304 
    305 #ifdef	DEBUG
    306 	if (si_debug & 2) {
    307 		printf("si_dma_setup: dh=%p, pa=0x%x, xlen=0x%x\n",
    308 			   dh, data_pa, xlen);
    309 	}
    310 #endif
    311 
    312 	/* Set direction (send/recv) */
    313 	if (dh->dh_flags & SIDH_OUT) {
    314 		si->si_csr |= SI_CSR_SEND;
    315 	} else {
    316 		si->si_csr &= ~SI_CSR_SEND;
    317 	}
    318 
    319 	/* Reset the FIFO. */
    320 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    321 	si->si_csr |= SI_CSR_FIFO_RES;
    322 
    323 	if (data_pa & 2) {
    324 		si->si_csr |= SI_CSR_BPCON;
    325 	} else {
    326 		si->si_csr &= ~SI_CSR_BPCON;
    327 	}
    328 
    329 	/* Load the start address. */
    330 	si->dma_addrh = (ushort)(data_pa >> 16);
    331 	si->dma_addrl = (ushort)(data_pa & 0xFFFF);
    332 
    333 	/*
    334 	 * Keep the count zero or it may start early!
    335 	 */
    336 	si->dma_counth = 0;
    337 	si->dma_countl = 0;
    338 
    339 #if 0
    340 	/* Clear FIFO counter. (also hits dma_count) */
    341 	si->fifo_cnt_hi = 0;
    342 	si->fifo_count = 0;
    343 #endif
    344 }
    345 
    346 
    347 void
    348 si_vme_dma_start(ncr_sc)
    349 	struct ncr5380_softc *ncr_sc;
    350 {
    351 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    352 	struct sci_req *sr = ncr_sc->sc_current;
    353 	struct si_dma_handle *dh = sr->sr_dma_hand;
    354 	volatile struct si_regs *si = sc->sc_regs;
    355 	int s, xlen;
    356 
    357 	xlen = sc->sc_reqlen;
    358 
    359 	/* This MAY be time critical (not sure). */
    360 	s = splhigh();
    361 
    362 	si->dma_counth = (ushort)(xlen >> 16);
    363 	si->dma_countl = (ushort)(xlen & 0xFFFF);
    364 
    365 	/* Set it anyway, even though dma_count hits it. */
    366 	si->fifo_cnt_hi = (ushort)(xlen >> 16);
    367 	si->fifo_count  = (ushort)(xlen & 0xFFFF);
    368 
    369 	/*
    370 	 * Acknowledge the phase change.  (After DMA setup!)
    371 	 * Put the SBIC into DMA mode, and start the transfer.
    372 	 */
    373 	if (dh->dh_flags & SIDH_OUT) {
    374 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    375 		SCI_CLR_INTR(ncr_sc);
    376 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    377 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    378 		*ncr_sc->sci_dma_send = 0;	/* start it */
    379 	} else {
    380 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    381 		SCI_CLR_INTR(ncr_sc);
    382 		*ncr_sc->sci_icmd = 0;
    383 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    384 		*ncr_sc->sci_irecv = 0;	/* start it */
    385 	}
    386 
    387 	/* Let'er rip! */
    388 	si->si_csr |= SI_CSR_DMA_EN;
    389 
    390 	splx(s);
    391 	ncr_sc->sc_state |= NCR_DOINGDMA;
    392 
    393 #ifdef	DEBUG
    394 	if (si_debug & 2) {
    395 		printf("si_dma_start: started, flags=0x%x\n",
    396 			   ncr_sc->sc_state);
    397 	}
    398 #endif
    399 }
    400 
    401 
    402 void
    403 si_vme_dma_eop(ncr_sc)
    404 	struct ncr5380_softc *ncr_sc;
    405 {
    406 
    407 	/* Not needed - DMA was stopped prior to examining sci_csr */
    408 }
    409 
    410 
    411 void
    412 si_vme_dma_stop(ncr_sc)
    413 	struct ncr5380_softc *ncr_sc;
    414 {
    415 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    416 	struct sci_req *sr = ncr_sc->sc_current;
    417 	struct si_dma_handle *dh = sr->sr_dma_hand;
    418 	volatile struct si_regs *si = sc->sc_regs;
    419 	int resid, ntrans;
    420 
    421 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    422 #ifdef	DEBUG
    423 		printf("si_dma_stop: dma not running\n");
    424 #endif
    425 		return;
    426 	}
    427 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    428 
    429 	/* First, halt the DMA engine. */
    430 	si->si_csr &= ~SI_CSR_DMA_EN;	/* VME only */
    431 
    432 	/* Set an impossible phase to prevent data movement? */
    433 	*ncr_sc->sci_tcmd = PHASE_INVALID;
    434 
    435 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
    436 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
    437 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    438 		ncr_sc->sc_state |= NCR_ABORTING;
    439 		si_reset_adapter(ncr_sc);
    440 		goto out;
    441 	}
    442 
    443 	/* Note that timeout may have set the error flag. */
    444 	if (ncr_sc->sc_state & NCR_ABORTING)
    445 		goto out;
    446 
    447 	/* XXX: Wait for DMA to actually finish? */
    448 
    449 	/*
    450 	 * Now try to figure out how much actually transferred
    451 	 *
    452 	 * The fifo_count does not reflect how many bytes were
    453 	 * actually transferred for VME.
    454 	 *
    455 	 * SCSI-3 VME interface is a little funny on writes:
    456 	 * if we have a disconnect, the dma has overshot by
    457 	 * one byte and the resid needs to be incremented.
    458 	 * Only happens for partial transfers.
    459 	 * (Thanks to Matt Jacob)
    460 	 */
    461 
    462 	resid = si->fifo_count & 0xFFFF;
    463 	if (dh->dh_flags & SIDH_OUT)
    464 		if ((resid > 0) && (resid < sc->sc_reqlen))
    465 			resid++;
    466 	ntrans = sc->sc_reqlen - resid;
    467 
    468 #ifdef	DEBUG
    469 	if (si_debug & 2) {
    470 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
    471 		       resid, ntrans);
    472 	}
    473 #endif
    474 
    475 	if (ntrans < MIN_DMA_LEN) {
    476 		printf("si: fifo count: 0x%x\n", resid);
    477 		ncr_sc->sc_state |= NCR_ABORTING;
    478 		goto out;
    479 	}
    480 	if (ntrans > ncr_sc->sc_datalen)
    481 		panic("si_dma_stop: excess transfer");
    482 
    483 	/* Adjust data pointer */
    484 	ncr_sc->sc_dataptr += ntrans;
    485 	ncr_sc->sc_datalen -= ntrans;
    486 
    487 	/*
    488 	 * After a read, we may need to clean-up
    489 	 * "Left-over bytes" (yuck!)
    490 	 */
    491 	if (((dh->dh_flags & SIDH_OUT) == 0) &&
    492 		((si->si_csr & SI_CSR_LOB) != 0))
    493 	{
    494 		char *cp = ncr_sc->sc_dataptr;
    495 #ifdef DEBUG
    496 		printf("si: Got Left-over bytes!\n");
    497 #endif
    498 		if (si->si_csr & SI_CSR_BPCON) {
    499 			/* have SI_CSR_BPCON */
    500 			cp[-1] = (si->si_bprl & 0xff00) >> 8;
    501 		} else {
    502 			switch (si->si_csr & SI_CSR_LOB) {
    503 			case SI_CSR_LOB_THREE:
    504 				cp[-3] = (si->si_bprh & 0xff00) >> 8;
    505 				cp[-2] = (si->si_bprh & 0x00ff);
    506 				cp[-1] = (si->si_bprl & 0xff00) >> 8;
    507 				break;
    508 			case SI_CSR_LOB_TWO:
    509 				cp[-2] = (si->si_bprh & 0xff00) >> 8;
    510 				cp[-1] = (si->si_bprh & 0x00ff);
    511 				break;
    512 			case SI_CSR_LOB_ONE:
    513 				cp[-1] = (si->si_bprh & 0xff00) >> 8;
    514 				break;
    515 			}
    516 		}
    517 	}
    518 
    519 out:
    520 	si->dma_addrh = 0;
    521 	si->dma_addrl = 0;
    522 
    523 	si->dma_counth = 0;
    524 	si->dma_countl = 0;
    525 
    526 	si->fifo_cnt_hi = 0;
    527 	si->fifo_count  = 0;
    528 
    529 	/* Put SBIC back in PIO mode. */
    530 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    531 	*ncr_sc->sci_icmd = 0;
    532 }
    533