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