Home | History | Annotate | Line # | Download | only in vsa
ncr.c revision 1.26
      1 /*	$NetBSD: ncr.c,v 1.26 2000/03/25 15:27:57 tsutsui 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, Gordon W. Ross, and Jens A. Nilsson.
      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 the machine-dependent parts of the NCR-5380
     41  * controller. The machine-independent parts are in ncr5380sbc.c.
     42  *
     43  * Note: Only PIO transfers for now which implicates very bad
     44  * performance. DMA support will come soon.
     45  *
     46  * Jens A. Nilsson.
     47  *
     48  * Credits:
     49  *
     50  * This code is based on arch/sun3/dev/si*
     51  * Written by David Jones, Gordon Ross, and Adam Glass.
     52  */
     53 
     54 #include <sys/param.h>
     55 #include <sys/systm.h>
     56 #include <sys/errno.h>
     57 #include <sys/kernel.h>
     58 #include <sys/malloc.h>
     59 #include <sys/device.h>
     60 #include <sys/buf.h>
     61 #include <sys/proc.h>
     62 #include <sys/user.h>
     63 
     64 #include <vm/vm.h>
     65 #include <vm/vm_kern.h>
     66 
     67 #include <dev/scsipi/scsi_all.h>
     68 #include <dev/scsipi/scsipi_all.h>
     69 #include <dev/scsipi/scsipi_debug.h>
     70 #include <dev/scsipi/scsiconf.h>
     71 
     72 #include <dev/ic/ncr5380reg.h>
     73 #include <dev/ic/ncr5380var.h>
     74 
     75 #include <machine/cpu.h>
     76 #include <machine/vsbus.h>
     77 #include <machine/bus.h>
     78 #include <machine/sid.h>
     79 #include <machine/scb.h>
     80 
     81 #include "ioconf.h"
     82 
     83 #define MIN_DMA_LEN 128
     84 
     85 struct si_dma_handle {
     86 	int	dh_flags;
     87 #define SIDH_BUSY	1
     88 #define SIDH_OUT	2
     89 	caddr_t dh_addr;
     90 	int	dh_len;
     91 	struct	proc *dh_proc;
     92 };
     93 
     94 struct si_softc {
     95 	struct	ncr5380_softc	ncr_sc;
     96 	caddr_t ncr_addr;
     97 	int	ncr_off;
     98 	int	ncr_dmaaddr;
     99 	int	ncr_dmacount;
    100 	int	ncr_dmadir;
    101 	struct	si_dma_handle ncr_dma[SCI_OPENINGS];
    102 };
    103 
    104 static	int si_match(struct device *, struct cfdata *, void *);
    105 static	void si_attach(struct device *, struct device *, void *);
    106 static	void si_minphys(struct buf *);
    107 
    108 static	void si_dma_alloc __P((struct ncr5380_softc *));
    109 static	void si_dma_free __P((struct ncr5380_softc *));
    110 static	void si_dma_setup __P((struct ncr5380_softc *));
    111 static	void si_dma_start __P((struct ncr5380_softc *));
    112 static	void si_dma_poll __P((struct ncr5380_softc *));
    113 static	void si_dma_eop __P((struct ncr5380_softc *));
    114 static	void si_dma_stop __P((struct ncr5380_softc *));
    115 
    116 
    117 struct cfattach ncr_ca = {
    118 	sizeof(struct si_softc), si_match, si_attach
    119 };
    120 
    121 static int
    122 si_match(parent, cf, aux)
    123 	struct device *parent;
    124 	struct cfdata *cf;
    125 	void *aux;
    126 {
    127 	struct vsbus_attach_args *va = aux;
    128 	volatile char *si_csr = (char *) va->va_addr;
    129 
    130 	if (vax_boardtype == VAX_BTYP_49)
    131 		return 0;
    132 	/* This is the way Linux autoprobes the interrupt MK-990321 */
    133 	si_csr[12] = 0;
    134 	si_csr[16] = 0x80;
    135 	si_csr[0] = 0x80;
    136 	si_csr[4] = 5; /* 0xcf */
    137 	DELAY(100000);
    138 	return 1;
    139 }
    140 
    141 static void
    142 si_attach(parent, self, aux)
    143 	struct device	*parent, *self;
    144 	void		*aux;
    145 {
    146 	struct vsbus_attach_args *va = aux;
    147 	struct si_softc *sc = (struct si_softc *) self;
    148 	struct ncr5380_softc *ncr_sc = &sc->ncr_sc;
    149 
    150 	printf("\n");
    151 
    152 	scb_vecalloc(va->va_cvec, (void (*)(void *)) ncr5380_intr, sc, SCB_ISTACK);
    153 
    154 	/*
    155 	 * DMA area mapin.
    156 	 * On VS3100, split the 128K block between the two devices.
    157 	 * On VS2000, don't care for now.
    158 	 */
    159 #define DMASIZE (64*1024)
    160 	if (vax_boardtype != VAX_BTYP_410) {
    161 		if (va->va_paddr & 0x100) /* Magic */
    162 			sc->ncr_off = DMASIZE;
    163 		sc->ncr_addr = (caddr_t)uvm_km_valloc(kernel_map, DMASIZE);
    164 
    165 		ioaccess((vaddr_t)sc->ncr_addr,
    166 		    0x202d0000 + sc->ncr_off, DMASIZE/VAX_NBPG);
    167 
    168 		/*
    169 		 * MD function pointers used by the MI code.
    170 		 */
    171 		ncr_sc->sc_dma_alloc = si_dma_alloc;
    172 		ncr_sc->sc_dma_free  = si_dma_free;
    173 		ncr_sc->sc_dma_setup = si_dma_setup;
    174 		ncr_sc->sc_dma_start = si_dma_start;
    175 		ncr_sc->sc_dma_poll  = si_dma_poll;
    176 		ncr_sc->sc_dma_eop   = si_dma_eop;
    177 		ncr_sc->sc_dma_stop  = si_dma_stop;
    178 
    179 		/* DMA control register offsets */
    180 		sc->ncr_dmaaddr = 32;	/* DMA address in buffer, longword */
    181 		sc->ncr_dmacount = 64;	/* DMA count register */
    182 		sc->ncr_dmadir = 68;	/* Direction of DMA transfer */
    183 	}
    184 	ncr_sc->sc_pio_out = ncr5380_pio_out;
    185 	ncr_sc->sc_pio_in =  ncr5380_pio_in;
    186 
    187 	ncr_sc->sc_min_dma_len = MIN_DMA_LEN;
    188 
    189 	/*
    190 	 * Initialize fields used by the MI code.
    191 	 */
    192 /*	ncr_sc->sc_regt =  Unused on VAX */
    193 	ncr_sc->sc_regh = vax_map_physmem(va->va_paddr, 1);
    194 
    195 	/* Register offsets */
    196 	ncr_sc->sci_r0 = 0;
    197 	ncr_sc->sci_r1 = 4;
    198 	ncr_sc->sci_r2 = 8;
    199 	ncr_sc->sci_r3 = 12;
    200 	ncr_sc->sci_r4 = 16;
    201 	ncr_sc->sci_r5 = 20;
    202 	ncr_sc->sci_r6 = 24;
    203 	ncr_sc->sci_r7 = 28;
    204 
    205 	ncr_sc->sc_rev = NCR_VARIANT_NCR5380;
    206 
    207 	ncr_sc->sc_no_disconnect = 0xff;
    208 
    209 	ncr_sc->sc_link.scsipi_scsi.adapter_target = 7;
    210 	ncr_sc->sc_adapter.scsipi_minphys = si_minphys;
    211 
    212 	/*
    213 	 * Initialize si board itself.
    214 	 */
    215 	ncr5380_attach(ncr_sc);
    216 }
    217 
    218 /*
    219  * Adjust the max transfer size. The DMA buffer is only 16k on VS2000.
    220  */
    221 static void
    222 si_minphys(struct buf *bp)
    223 {
    224 	if ((vax_boardtype == VAX_BTYP_410) && (bp->b_bcount > (16*1024)))
    225 		bp->b_bcount = (16*1024);
    226 	else if (bp->b_bcount > MAXPHYS)
    227 		bp->b_bcount = MAXPHYS;
    228 }
    229 
    230 void
    231 si_dma_alloc(ncr_sc)
    232 	struct ncr5380_softc *ncr_sc;
    233 {
    234 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    235 	struct sci_req *sr = ncr_sc->sc_current;
    236 	struct scsipi_xfer *xs = sr->sr_xs;
    237 	struct si_dma_handle *dh;
    238 	int xlen, i;
    239 
    240 #ifdef DIAGNOSTIC
    241 	if (sr->sr_dma_hand != NULL)
    242 		panic("si_dma_alloc: already have DMA handle");
    243 #endif
    244 
    245 	/* Polled transfers shouldn't allocate a DMA handle. */
    246 	if (sr->sr_flags & SR_IMMED)
    247 		return;
    248 
    249 	xlen = ncr_sc->sc_datalen;
    250 
    251 	/* Make sure our caller checked sc_min_dma_len. */
    252 	if (xlen < MIN_DMA_LEN)
    253 		panic("si_dma_alloc: len=0x%x\n", xlen);
    254 
    255 	/*
    256 	 * Find free PDMA handle.  Guaranteed to find one since we
    257 	 * have as many PDMA handles as the driver has processes.
    258 	 * (instances?)
    259 	 */
    260 	 for (i = 0; i < SCI_OPENINGS; i++) {
    261 		if ((sc->ncr_dma[i].dh_flags & SIDH_BUSY) == 0)
    262 			goto found;
    263 	}
    264 	panic("sbc: no free PDMA handles");
    265 found:
    266 	dh = &sc->ncr_dma[i];
    267 	dh->dh_flags = SIDH_BUSY;
    268 	dh->dh_addr = ncr_sc->sc_dataptr;
    269 	dh->dh_len = xlen;
    270 	dh->dh_proc = xs->bp->b_proc;
    271 
    272 	/* Remember dest buffer parameters */
    273 	if (xs->xs_control & XS_CTL_DATA_OUT)
    274 		dh->dh_flags |= SIDH_OUT;
    275 
    276 	sr->sr_dma_hand = dh;
    277 }
    278 
    279 void
    280 si_dma_free(ncr_sc)
    281 	struct ncr5380_softc *ncr_sc;
    282 {
    283 	struct sci_req *sr = ncr_sc->sc_current;
    284 	struct si_dma_handle *dh = sr->sr_dma_hand;
    285 
    286 	if (dh->dh_flags & SIDH_BUSY)
    287 		dh->dh_flags = 0;
    288 	else
    289 		printf("si_dma_free: free'ing unused buffer\n");
    290 
    291 	sr->sr_dma_hand = NULL;
    292 }
    293 
    294 void
    295 si_dma_setup(ncr_sc)
    296 	struct ncr5380_softc *ncr_sc;
    297 {
    298 	/* Do nothing here */
    299 }
    300 
    301 void
    302 si_dma_start(ncr_sc)
    303 	struct ncr5380_softc *ncr_sc;
    304 {
    305 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    306 	struct sci_req *sr = ncr_sc->sc_current;
    307 	struct si_dma_handle *dh = sr->sr_dma_hand;
    308 
    309 	/*
    310 	 * Set the VAX-DMA-specific registers, and copy the data if
    311 	 * it is directed "outbound".
    312 	 */
    313 	if (dh->dh_flags & SIDH_OUT) {
    314 		if ((vaddr_t)dh->dh_addr & KERNBASE)
    315 			bcopy(dh->dh_addr, sc->ncr_addr, dh->dh_len);
    316 		else
    317 			vsbus_copyfromproc(dh->dh_proc, dh->dh_addr,
    318 			    sc->ncr_addr, dh->dh_len);
    319 		bus_space_write_1(ncr_sc->sc_regt, ncr_sc->sc_regh,
    320 		    sc->ncr_dmadir, 0);
    321 	} else {
    322 		bus_space_write_1(ncr_sc->sc_regt, ncr_sc->sc_regh,
    323 		    sc->ncr_dmadir, 1);
    324 	}
    325 	bus_space_write_4(ncr_sc->sc_regt, ncr_sc->sc_regh,
    326 	    sc->ncr_dmacount, -dh->dh_len);
    327 	bus_space_write_4(ncr_sc->sc_regt, ncr_sc->sc_regh,
    328 	    sc->ncr_dmaaddr, sc->ncr_off);
    329 	/*
    330 	 * Now from the 5380-internal DMA registers.
    331 	 */
    332 	if (dh->dh_flags & SIDH_OUT) {
    333 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_OUT);
    334 		NCR5380_WRITE(ncr_sc, sci_icmd, SCI_ICMD_DATA);
    335 		NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode)
    336 		    | SCI_MODE_DMA | SCI_MODE_DMA_IE);
    337 		NCR5380_WRITE(ncr_sc, sci_dma_send, 0);
    338 	} else {
    339 		NCR5380_WRITE(ncr_sc, sci_tcmd, PHASE_DATA_IN);
    340 		NCR5380_WRITE(ncr_sc, sci_icmd, 0);
    341 		NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode)
    342 		    | SCI_MODE_DMA | SCI_MODE_DMA_IE);
    343 		NCR5380_WRITE(ncr_sc, sci_irecv, 0);
    344 	}
    345 	ncr_sc->sc_state |= NCR_DOINGDMA;
    346 }
    347 
    348 /*
    349  * When?
    350  */
    351 void
    352 si_dma_poll(ncr_sc)
    353 	struct ncr5380_softc *ncr_sc;
    354 {
    355 	printf("si_dma_poll\n");
    356 }
    357 
    358 /*
    359  * When?
    360  */
    361 void
    362 si_dma_eop(ncr_sc)
    363 	struct ncr5380_softc *ncr_sc;
    364 {
    365 	printf("si_dma_eop\n");
    366 }
    367 
    368 void
    369 si_dma_stop(ncr_sc)
    370 	struct ncr5380_softc *ncr_sc;
    371 {
    372 	struct si_softc *sc = (struct si_softc *)ncr_sc;
    373 	struct sci_req *sr = ncr_sc->sc_current;
    374 	struct si_dma_handle *dh = sr->sr_dma_hand;
    375 	int count, i;
    376 
    377 	if (ncr_sc->sc_state & NCR_DOINGDMA)
    378 		ncr_sc->sc_state &= ~NCR_DOINGDMA;
    379 
    380 	/*
    381 	 * Sometimes the FIFO buffer isn't drained when the
    382 	 * interrupt is posted. Just loop here and hope that
    383 	 * it will drain soon.
    384 	 */
    385 	for (i = 0; i < 20000; i++) {
    386 		count = bus_space_read_4(ncr_sc->sc_regt,
    387 		    ncr_sc->sc_regh, sc->ncr_dmacount);
    388 		if (count == 0)
    389 			break;
    390 		DELAY(100);
    391 	}
    392 	if (count == 0) {
    393 		if (((dh->dh_flags & SIDH_OUT) == 0)) {
    394 			if ((vaddr_t)dh->dh_addr & KERNBASE)
    395 				bcopy(sc->ncr_addr, dh->dh_addr, dh->dh_len);
    396 			else
    397 				vsbus_copytoproc(dh->dh_proc, sc->ncr_addr,
    398 				    dh->dh_addr, dh->dh_len);
    399 
    400 		}
    401 		ncr_sc->sc_dataptr += dh->dh_len;
    402 		ncr_sc->sc_datalen -= dh->dh_len;
    403 	}
    404 
    405 	NCR5380_WRITE(ncr_sc, sci_mode, NCR5380_READ(ncr_sc, sci_mode) &
    406 	    ~(SCI_MODE_DMA | SCI_MODE_DMA_IE));
    407 	NCR5380_WRITE(ncr_sc, sci_icmd, 0);
    408 }
    409