Home | History | Annotate | Line # | Download | only in dev
si_obio.c revision 1.7
      1 /*	$NetBSD: si_obio.c,v 1.7 1996/11/20 18:57:00 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  * OBIO 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 #include "am9516.h"
    110 
    111 /*
    112  * How many uS. to delay after touching the am9516 UDC.
    113  */
    114 #define UDC_WAIT_USEC 5
    115 
    116 void si_obio_dma_setup __P((struct ncr5380_softc *));
    117 void si_obio_dma_start __P((struct ncr5380_softc *));
    118 void si_obio_dma_eop __P((struct ncr5380_softc *));
    119 void si_obio_dma_stop __P((struct ncr5380_softc *));
    120 
    121 /*
    122  * New-style autoconfig attachment
    123  */
    124 
    125 static int	si_obio_match __P((struct device *, void *, void *));
    126 static void	si_obio_attach __P((struct device *, struct device *, void *));
    127 
    128 struct cfattach si_obio_ca = {
    129 	sizeof(struct si_softc), si_obio_match, si_obio_attach
    130 };
    131 
    132 /* Options.  Interesting values are: 1,3,7 */
    133 /* XXX: Using 1 for now to mask a (pmap?) bug not yet found... */
    134 int si_obio_options = 1;	/* XXX */
    135 
    136 
    137 static int
    138 si_obio_match(parent, vcf, args)
    139 	struct device	*parent;
    140 	void		*vcf, *args;
    141 {
    142 	struct cfdata	*cf = vcf;
    143 	struct confargs *ca = args;
    144 
    145 	/* Make sure there is something there... */
    146 	if (bus_peek(ca->ca_bustype, ca->ca_paddr + 1, 1) == -1)
    147 		return (0);
    148 
    149 	/* Default interrupt priority. */
    150 	if (ca->ca_intpri == -1)
    151 		ca->ca_intpri = 2;
    152 
    153 	return (1);
    154 }
    155 
    156 static void
    157 si_obio_attach(parent, self, args)
    158 	struct device	*parent, *self;
    159 	void		*args;
    160 {
    161 	struct si_softc *sc = (struct si_softc *) self;
    162 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    163 	struct cfdata *cf = self->dv_cfdata;
    164 	struct confargs *ca = args;
    165 
    166 	/* Get options from config flags... */
    167 	sc->sc_options = cf->cf_flags | si_obio_options;
    168 	printf(": options=%d\n", sc->sc_options);
    169 
    170 	sc->sc_adapter_type = ca->ca_bustype;
    171 	sc->sc_regs = (struct si_regs *)
    172 		obio_alloc(ca->ca_paddr, sizeof(struct si_regs));
    173 
    174 	/*
    175 	 * MD function pointers used by the MI code.
    176 	 */
    177 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    178 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    179 	ncr_sc->sc_dma_alloc = si_dma_alloc;
    180 	ncr_sc->sc_dma_free  = si_dma_free;
    181 	ncr_sc->sc_dma_setup = si_obio_dma_setup;
    182 	ncr_sc->sc_dma_start = si_obio_dma_start;
    183 	ncr_sc->sc_dma_poll  = si_dma_poll;
    184 	ncr_sc->sc_dma_eop   = si_obio_dma_eop;
    185 	ncr_sc->sc_dma_stop  = si_obio_dma_stop;
    186 	ncr_sc->sc_intr_on   = NULL;
    187 	ncr_sc->sc_intr_off  = NULL;
    188 
    189 	/* Need DVMA-capable memory for the UDC command block. */
    190 	sc->sc_dmacmd = dvma_malloc(sizeof (struct udc_table));
    191 
    192 	/* Attach interrupt handler. */
    193 	isr_add_autovect(si_intr, (void *)sc, ca->ca_intpri);
    194 
    195 	/* Do the common attach stuff. */
    196 	si_attach(sc);
    197 }
    198 
    199 
    200 static __inline__ void
    201 si_obio_udc_write(si, regnum, value)
    202 	volatile struct si_regs *si;
    203 	int regnum, value;
    204 {
    205 	si->udc_addr = regnum;
    206 	delay(UDC_WAIT_USEC);
    207 	si->udc_data = value;
    208 	delay(UDC_WAIT_USEC);
    209 }
    210 
    211 static __inline__ int
    212 si_obio_udc_read(si, regnum)
    213 	volatile struct si_regs *si;
    214 	int regnum;
    215 {
    216 	int value;
    217 
    218 	si->udc_addr = regnum;
    219 	delay(UDC_WAIT_USEC);
    220 	value = si->udc_data;
    221 	delay(UDC_WAIT_USEC);
    222 
    223 	return (value);
    224 }
    225 
    226 
    227 /*
    228  * This function is called during the COMMAND or MSG_IN phase
    229  * that preceeds a DATA_IN or DATA_OUT phase, in case we need
    230  * to setup the DMA engine before the bus enters a DATA phase.
    231  *
    232  * The OBIO "si" IGNORES any attempt to set the FIFO count
    233  * register after the SCSI bus goes into any DATA phase, so
    234  * this function has to setup the evil FIFO logic.
    235  */
    236 void
    237 si_obio_dma_setup(ncr_sc)
    238 	struct ncr5380_softc *ncr_sc;
    239 {
    240 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    241 	struct sci_req *sr = ncr_sc->sc_current;
    242 	struct si_dma_handle *dh = sr->sr_dma_hand;
    243 	volatile struct si_regs *si = sc->sc_regs;
    244 	struct udc_table *cmd;
    245 	long data_pa, cmd_pa;
    246 	int xlen;
    247 
    248 	/*
    249 	 * Get the DVMA mapping for this segment.
    250 	 * XXX - Should separate allocation and mapin.
    251 	 */
    252 	data_pa = dvma_kvtopa(dh->dh_dvma, sc->sc_adapter_type);
    253 	data_pa += (ncr_sc->sc_dataptr - dh->dh_addr);
    254 	if (data_pa & 1)
    255 		panic("si_dma_start: bad pa=0x%x", data_pa);
    256 	xlen = ncr_sc->sc_datalen;
    257 	sc->sc_reqlen = xlen; 	/* XXX: or less? */
    258 
    259 #ifdef	DEBUG
    260 	if (si_debug & 2) {
    261 		printf("si_dma_setup: dh=0x%x, pa=0x%x, xlen=%d\n",
    262 			   dh, data_pa, xlen);
    263 	}
    264 #endif
    265 
    266 	/* Reset the UDC. (In case not already reset?) */
    267 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
    268 
    269 	/* Reset the FIFO */
    270 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    271 	si->si_csr |= SI_CSR_FIFO_RES;
    272 
    273 	/* Set direction (send/recv) */
    274 	if (dh->dh_flags & SIDH_OUT) {
    275 		si->si_csr |= SI_CSR_SEND;
    276 	} else {
    277 		si->si_csr &= ~SI_CSR_SEND;
    278 	}
    279 
    280 	/* Set the FIFO counter. */
    281 	si->fifo_count = xlen;
    282 
    283 	/* Reset the UDC. */
    284 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
    285 
    286 	/*
    287 	 * XXX: Reset the FIFO again!  Comment from Sprite:
    288 	 * Go through reset again becuase of the bug on the 3/50
    289 	 * where bytes occasionally linger in the DMA fifo.
    290 	 */
    291 	si->si_csr &= ~SI_CSR_FIFO_RES; 	/* active low */
    292 	si->si_csr |= SI_CSR_FIFO_RES;
    293 
    294 #ifdef	DEBUG
    295 	/* Make sure the extra FIFO reset did not hit the count. */
    296 	if (si->fifo_count != xlen) {
    297 		printf("si_dma_setup: fifo_count=0x%x, xlen=0x%x\n",
    298 			   si->fifo_count, xlen);
    299 		Debugger();
    300 	}
    301 #endif
    302 
    303 	/*
    304 	 * Set up the DMA controller.  The DMA controller on
    305 	 * OBIO needs a command block in DVMA space.
    306 	 */
    307 	cmd = sc->sc_dmacmd;
    308 	cmd->addrh = ((data_pa & 0xFF0000) >> 8) | UDC_ADDR_INFO;
    309 	cmd->addrl = data_pa & 0xFFFF;
    310 	cmd->count = xlen / 2;	/* bytes -> words */
    311 	cmd->cmrh = UDC_CMR_HIGH;
    312 	if (dh->dh_flags & SIDH_OUT) {
    313 		if (xlen & 1)
    314 			cmd->count++;
    315 		cmd->cmrl = UDC_CMR_LSEND;
    316 		cmd->rsel = UDC_RSEL_SEND;
    317 	} else {
    318 		cmd->cmrl = UDC_CMR_LRECV;
    319 		cmd->rsel = UDC_RSEL_RECV;
    320 	}
    321 
    322 	/* Tell the DMA chip where the control block is. */
    323 	cmd_pa = dvma_kvtopa((long)cmd, BUS_OBIO);
    324 	si_obio_udc_write(si, UDC_ADR_CAR_HIGH,
    325 					  (cmd_pa & 0xff0000) >> 8);
    326 	si_obio_udc_write(si, UDC_ADR_CAR_LOW,
    327 					  (cmd_pa & 0xffff));
    328 
    329 	/* Tell the chip to be a DMA master. */
    330 	si_obio_udc_write(si, UDC_ADR_MODE, UDC_MODE);
    331 
    332 	/* Tell the chip to interrupt on error. */
    333 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_CIE);
    334 
    335 	/* Will do "start chain" command in _dma_start. */
    336 }
    337 
    338 
    339 void
    340 si_obio_dma_start(ncr_sc)
    341 	struct ncr5380_softc *ncr_sc;
    342 {
    343 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    344 	struct sci_req *sr = ncr_sc->sc_current;
    345 	struct si_dma_handle *dh = sr->sr_dma_hand;
    346 	volatile struct si_regs *si = sc->sc_regs;
    347 	int s;
    348 
    349 #ifdef	DEBUG
    350 	if (si_debug & 2) {
    351 		printf("si_dma_start: sr=0x%x\n", sr);
    352 	}
    353 #endif
    354 
    355 	/* This MAY be time critical (not sure). */
    356 	s = splhigh();
    357 
    358 	/* Finally, give the UDC a "start chain" command. */
    359 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_STRT_CHN);
    360 
    361 	/*
    362 	 * Acknowledge the phase change.  (After DMA setup!)
    363 	 * Put the SBIC into DMA mode, and start the transfer.
    364 	 */
    365 	if (dh->dh_flags & SIDH_OUT) {
    366 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
    367 		SCI_CLR_INTR(ncr_sc);
    368 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
    369 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    370 		*ncr_sc->sci_dma_send = 0;	/* start it */
    371 	} else {
    372 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
    373 		SCI_CLR_INTR(ncr_sc);
    374 		*ncr_sc->sci_icmd = 0;
    375 		*ncr_sc->sci_mode |= (SCI_MODE_DMA | SCI_MODE_DMA_IE);
    376 		*ncr_sc->sci_irecv = 0;	/* start it */
    377 	}
    378 
    379 	splx(s);
    380 	ncr_sc->sc_state |= NCR_DOINGDMA;
    381 
    382 #ifdef	DEBUG
    383 	if (si_debug & 2) {
    384 		printf("si_dma_start: started, flags=0x%x\n",
    385 			   ncr_sc->sc_state);
    386 	}
    387 #endif
    388 }
    389 
    390 
    391 void
    392 si_obio_dma_eop(ncr_sc)
    393 	struct ncr5380_softc *ncr_sc;
    394 {
    395 
    396 	/* Not needed - DMA was stopped prior to examining sci_csr */
    397 }
    398 
    399 
    400 void
    401 si_obio_dma_stop(ncr_sc)
    402 	struct ncr5380_softc *ncr_sc;
    403 {
    404 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    405 	struct sci_req *sr = ncr_sc->sc_current;
    406 	struct si_dma_handle *dh = sr->sr_dma_hand;
    407 	volatile struct si_regs *si = sc->sc_regs;
    408 	int resid, ntrans, tmo, udc_cnt;
    409 
    410 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
    411 #ifdef	DEBUG
    412 		printf("si_dma_stop: dma not running\n");
    413 #endif
    414 		return;
    415 	}
    416 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
    417 
    418 	NCR_TRACE("si_dma_stop: top, csr=0x%x\n", si->si_csr);
    419 
    420 	/* OK, have either phase mis-match or end of DMA. */
    421 	/* Set an impossible phase to prevent data movement? */
    422 	*ncr_sc->sci_tcmd = PHASE_INVALID;
    423 
    424 	/* Check for DMA errors. */
    425 	if (si->si_csr & (SI_CSR_DMA_CONFLICT | SI_CSR_DMA_BUS_ERR)) {
    426 		printf("si: DMA error, csr=0x%x, reset\n", si->si_csr);
    427 		sr->sr_xs->error = XS_DRIVER_STUFFUP;
    428 		ncr_sc->sc_state |= NCR_ABORTING;
    429 		si_reset_adapter(ncr_sc);
    430 		goto out;
    431 	}
    432 
    433 	/* Note that timeout may have set the error flag. */
    434 	if (ncr_sc->sc_state & NCR_ABORTING)
    435 		goto out;
    436 
    437 	/*
    438 	 * After a read, wait for the FIFO to empty.
    439 	 * Note: this only works on the OBIO version.
    440 	 */
    441 	if ((dh->dh_flags & SIDH_OUT) == 0) {
    442 		tmo = 200000;	/* X10 = 2 sec. */
    443 		for (;;) {
    444 			if (si->si_csr & SI_CSR_FIFO_EMPTY)
    445 				break;
    446 			if (--tmo <= 0) {
    447 				printf("si: dma fifo did not empty, reset\n");
    448 				ncr_sc->sc_state |= NCR_ABORTING;
    449 				/* si_reset_adapter(ncr_sc); */
    450 				goto out;
    451 			}
    452 			delay(10);
    453 		}
    454 	}
    455 
    456 	/*
    457 	 * Now try to figure out how much actually transferred
    458 	 * The fifo_count might not reflect how many bytes were
    459 	 * actually transferred.
    460 	 */
    461 	resid = si->fifo_count & 0xFFFF;
    462 	ntrans = sc->sc_reqlen - resid;
    463 
    464 #ifdef	DEBUG
    465 	if (si_debug & 2) {
    466 		printf("si_dma_stop: resid=0x%x ntrans=0x%x\n",
    467 		       resid, ntrans);
    468 	}
    469 #endif
    470 
    471 	/* XXX: Treat (ntrans==0) as a special, non-error case? */
    472 	if (ntrans < MIN_DMA_LEN) {
    473 		printf("si: fifo count: 0x%x\n", resid);
    474 		ncr_sc->sc_state |= NCR_ABORTING;
    475 		goto out;
    476 	}
    477 	if (ntrans > ncr_sc->sc_datalen)
    478 		panic("si_dma_stop: excess transfer");
    479 
    480 	/* Adjust data pointer */
    481 	ncr_sc->sc_dataptr += ntrans;
    482 	ncr_sc->sc_datalen -= ntrans;
    483 
    484 	/*
    485 	 * After a read, we may need to clean-up
    486 	 * "Left-over bytes" (yuck!)
    487 	 */
    488 	if ((dh->dh_flags & SIDH_OUT) == 0) {
    489 		/* If odd transfer count, grab last byte by hand. */
    490 		if (ntrans & 1) {
    491 			NCR_TRACE("si_dma_stop: leftover 1 at 0x%x\n",
    492 				(int) ncr_sc->sc_dataptr - 1);
    493 			ncr_sc->sc_dataptr[-1] =
    494 				(si->fifo_data & 0xff00) >> 8;
    495 			goto out;
    496 		}
    497 		/* UDC might not have transfered the last word. */
    498 		udc_cnt = si_obio_udc_read(si, UDC_ADR_COUNT);
    499 		if (((udc_cnt * 2) - resid) == 2) {
    500 			NCR_TRACE("si_dma_stop: leftover 2 at 0x%x\n",
    501 				(int) ncr_sc->sc_dataptr - 2);
    502 			ncr_sc->sc_dataptr[-2] =
    503 				(si->fifo_data & 0xff00) >> 8;
    504 			ncr_sc->sc_dataptr[-1] =
    505 				(si->fifo_data & 0x00ff);
    506 		}
    507 	}
    508 
    509 out:
    510 	/* Reset the UDC. */
    511 	si_obio_udc_write(si, UDC_ADR_COMMAND, UDC_CMD_RESET);
    512 	si->fifo_count = 0;
    513 	si->si_csr &= ~SI_CSR_SEND;
    514 
    515 	/* Reset the FIFO */
    516 	si->si_csr &= ~SI_CSR_FIFO_RES;     /* active low */
    517 	si->si_csr |= SI_CSR_FIFO_RES;
    518 
    519 	/* Put SBIC back in PIO mode. */
    520 	/* XXX: set tcmd to PHASE_INVALID? */
    521 	*ncr_sc->sci_mode &= ~(SCI_MODE_DMA | SCI_MODE_DMA_IE);
    522 	*ncr_sc->sci_icmd = 0;
    523 }
    524 
    525