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