Home | History | Annotate | Line # | Download | only in obio
asc.c revision 1.4
      1  1.4  wdk /*	$NetBSD: asc.c,v 1.4 2000/08/29 08:24:06 wdk Exp $	*/
      2  1.1  wdk /*-
      3  1.1  wdk  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      4  1.1  wdk  * All rights reserved.
      5  1.1  wdk  *
      6  1.1  wdk  * This code is derived from software contributed to The NetBSD Foundation
      7  1.1  wdk  * by Wayne Knowles
      8  1.1  wdk  *
      9  1.1  wdk  * Redistribution and use in source and binary forms, with or without
     10  1.1  wdk  * modification, are permitted provided that the following conditions
     11  1.1  wdk  * are met:
     12  1.1  wdk  * 1. Redistributions of source code must retain the above copyright
     13  1.1  wdk  *    notice, this list of conditions and the following disclaimer.
     14  1.1  wdk  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.1  wdk  *    notice, this list of conditions and the following disclaimer in the
     16  1.1  wdk  *    documentation and/or other materials provided with the distribution.
     17  1.1  wdk  * 3. All advertising materials mentioning features or use of this software
     18  1.1  wdk  *    must display the following acknowledgement:
     19  1.1  wdk  *        This product includes software developed by the NetBSD
     20  1.1  wdk  *        Foundation, Inc. and its contributors.
     21  1.1  wdk  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  1.1  wdk  *    contributors may be used to endorse or promote products derived
     23  1.1  wdk  *    from this software without specific prior written permission.
     24  1.1  wdk  *
     25  1.1  wdk  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.1  wdk  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.1  wdk  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.1  wdk  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.1  wdk  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.1  wdk  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.1  wdk  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.1  wdk  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.1  wdk  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.1  wdk  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.1  wdk  * POSSIBILITY OF SUCH DAMAGE.
     36  1.1  wdk  */
     37  1.1  wdk 
     38  1.1  wdk #include <sys/types.h>
     39  1.1  wdk #include <sys/param.h>
     40  1.1  wdk #include <sys/systm.h>
     41  1.1  wdk #include <sys/kernel.h>
     42  1.1  wdk #include <sys/errno.h>
     43  1.1  wdk #include <sys/device.h>
     44  1.1  wdk #include <sys/buf.h>
     45  1.1  wdk #include <sys/malloc.h>
     46  1.1  wdk 
     47  1.1  wdk #include <dev/scsipi/scsi_all.h>
     48  1.1  wdk #include <dev/scsipi/scsipi_all.h>
     49  1.1  wdk #include <dev/scsipi/scsiconf.h>
     50  1.1  wdk #include <dev/scsipi/scsi_message.h>
     51  1.1  wdk 
     52  1.1  wdk #include <machine/cpu.h>
     53  1.1  wdk #include <machine/autoconf.h>
     54  1.1  wdk #include <machine/mainboard.h>
     55  1.1  wdk #include <machine/bus.h>
     56  1.1  wdk 
     57  1.1  wdk #include <mipsco/obio/rambo.h>
     58  1.1  wdk 
     59  1.1  wdk #include <dev/ic/ncr53c9xreg.h>
     60  1.1  wdk #include <dev/ic/ncr53c9xvar.h>
     61  1.1  wdk 
     62  1.1  wdk struct asc_softc {
     63  1.1  wdk 	struct ncr53c9x_softc	sc_ncr53c9x;	/* glue to MI code */
     64  1.1  wdk         struct evcnt		sc_intrcnt; 	/* Interrupt counter */
     65  1.1  wdk 	bus_space_tag_t		sc_bst;
     66  1.1  wdk 	bus_space_handle_t	sc_bsh;		/* NCR 53c94 registers */
     67  1.1  wdk 	bus_space_handle_t	dm_bsh;		/* RAMBO registers */
     68  1.1  wdk 	bus_dma_tag_t		sc_dmat;
     69  1.1  wdk         bus_dmamap_t		sc_dmamap;
     70  1.1  wdk         caddr_t			*sc_dmaaddr;
     71  1.1  wdk 	size_t			*sc_dmalen;
     72  1.1  wdk 	size_t			sc_dmasize;
     73  1.1  wdk         size_t			sc_blkcnt;
     74  1.1  wdk 	int			sc_flags;
     75  1.1  wdk #define DMA_IDLE	0x0
     76  1.1  wdk #define	DMA_PULLUP	0x1
     77  1.1  wdk #define	DMA_ACTIVE	0x2
     78  1.1  wdk #define	DMA_MAPLOADED	0x4
     79  1.1  wdk         u_int32_t		dm_mode;
     80  1.1  wdk         int			dm_curseg;
     81  1.1  wdk };
     82  1.1  wdk 
     83  1.1  wdk static int	ascmatch  __P((struct device *, struct cfdata *, void *));
     84  1.1  wdk static void	ascattach __P((struct device *, struct device *, void *));
     85  1.1  wdk 
     86  1.1  wdk struct cfattach asc_ca = {
     87  1.1  wdk 	sizeof(struct asc_softc), ascmatch, ascattach
     88  1.1  wdk };
     89  1.1  wdk 
     90  1.1  wdk /*
     91  1.1  wdk  * Functions and the switch for the MI code.
     92  1.1  wdk  */
     93  1.1  wdk static u_char	asc_read_reg __P((struct ncr53c9x_softc *, int));
     94  1.1  wdk static void	asc_write_reg __P((struct ncr53c9x_softc *, int, u_char));
     95  1.1  wdk static int	asc_dma_isintr __P((struct ncr53c9x_softc *));
     96  1.1  wdk static void	asc_dma_reset __P((struct ncr53c9x_softc *));
     97  1.1  wdk static int	asc_dma_intr __P((struct ncr53c9x_softc *));
     98  1.1  wdk static int	asc_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
     99  1.1  wdk 				    size_t *, int, size_t *));
    100  1.1  wdk static void	asc_dma_go __P((struct ncr53c9x_softc *));
    101  1.1  wdk static void	asc_dma_stop __P((struct ncr53c9x_softc *));
    102  1.1  wdk static int	asc_dma_isactive __P((struct ncr53c9x_softc *));
    103  1.1  wdk 
    104  1.1  wdk static struct ncr53c9x_glue asc_glue = {
    105  1.1  wdk 	asc_read_reg,
    106  1.1  wdk 	asc_write_reg,
    107  1.1  wdk 	asc_dma_isintr,
    108  1.1  wdk 	asc_dma_reset,
    109  1.1  wdk 	asc_dma_intr,
    110  1.1  wdk 	asc_dma_setup,
    111  1.1  wdk 	asc_dma_go,
    112  1.1  wdk 	asc_dma_stop,
    113  1.1  wdk 	asc_dma_isactive,
    114  1.1  wdk 	NULL,			/* gl_clear_latched_intr */
    115  1.1  wdk };
    116  1.1  wdk 
    117  1.3  wdk static int	asc_intr __P((void *));
    118  1.3  wdk 
    119  1.1  wdk #define MAX_SCSI_XFER   (64*1024)
    120  1.1  wdk #define	MAX_DMA_SZ	MAX_SCSI_XFER
    121  1.1  wdk #define	DMA_SEGS	(MAX_DMA_SZ/NBPG)
    122  1.1  wdk 
    123  1.1  wdk static int
    124  1.1  wdk ascmatch(parent, cf, aux)
    125  1.1  wdk 	struct device *parent;
    126  1.1  wdk 	struct cfdata *cf;
    127  1.1  wdk 	void *aux;
    128  1.1  wdk {
    129  1.1  wdk 	return 1;
    130  1.1  wdk }
    131  1.1  wdk 
    132  1.1  wdk static void
    133  1.1  wdk ascattach(parent, self, aux)
    134  1.1  wdk 	struct device *parent, *self;
    135  1.1  wdk 	void *aux;
    136  1.1  wdk {
    137  1.1  wdk 	struct confargs *ca = aux;
    138  1.1  wdk 	struct asc_softc *esc = (void *)self;
    139  1.1  wdk 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    140  1.1  wdk 
    141  1.1  wdk 	/*
    142  1.1  wdk 	 * Set up glue for MI code early; we use some of it here.
    143  1.1  wdk 	 */
    144  1.1  wdk 	sc->sc_glue = &asc_glue;
    145  1.1  wdk 
    146  1.1  wdk 	esc->sc_bst = ca->ca_bustag;
    147  1.1  wdk 	esc->sc_dmat = ca->ca_dmatag;
    148  1.1  wdk 
    149  1.1  wdk 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
    150  1.1  wdk 			  16*4,	/* sizeof (ncr53c9xreg) */
    151  1.1  wdk 			  BUS_SPACE_MAP_LINEAR,
    152  1.1  wdk 			  &esc->sc_bsh) != 0) {
    153  1.1  wdk 		printf(": cannot map registers\n");
    154  1.1  wdk 		return;
    155  1.1  wdk 	}
    156  1.1  wdk 
    157  1.1  wdk 	if (bus_space_map(ca->ca_bustag, RAMBO_BASE, sizeof(struct rambo_ch),
    158  1.1  wdk 			  BUS_SPACE_MAP_LINEAR,
    159  1.1  wdk 			  &esc->dm_bsh) != 0) {
    160  1.1  wdk 		printf(": cannot map dma registers\n");
    161  1.1  wdk 		return;
    162  1.1  wdk 	}
    163  1.1  wdk 
    164  1.1  wdk         if (bus_dmamap_create(esc->sc_dmat, MAX_DMA_SZ,
    165  1.1  wdk 			      DMA_SEGS, MAX_DMA_SZ, RB_BOUNDRY,
    166  1.1  wdk 			      BUS_DMA_WAITOK,
    167  1.1  wdk 			      &esc->sc_dmamap) != 0) {
    168  1.1  wdk 		printf(": failed to create dmamap\n");
    169  1.1  wdk 		return;
    170  1.1  wdk         }
    171  1.1  wdk 
    172  1.1  wdk 	evcnt_attach_dynamic(&esc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    173  1.1  wdk 			     self->dv_xname, "intr");
    174  1.1  wdk 
    175  1.1  wdk 	esc->sc_flags = DMA_IDLE;
    176  1.1  wdk 	asc_dma_reset(sc);
    177  1.1  wdk 
    178  1.1  wdk 	/* Other settings */
    179  1.1  wdk 	sc->sc_id = 7;
    180  1.1  wdk 	sc->sc_freq = 24;	/* 24 MHz clock */
    181  1.1  wdk 
    182  1.1  wdk 	/*
    183  1.1  wdk 	 * Setup for genuine NCR 53C94 SCSI Controller
    184  1.1  wdk 	 */
    185  1.1  wdk 
    186  1.1  wdk 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    187  1.1  wdk 	sc->sc_cfg2 = NCRCFG2_SCSI2; /* | NCRCFG2_FE */
    188  1.1  wdk 	sc->sc_cfg3 = 0; /* NCRCFG3_CDB; */
    189  1.1  wdk 	sc->sc_rev = NCR_VARIANT_NCR53C94;
    190  1.1  wdk 
    191  1.1  wdk 	sc->sc_minsync = (1000 / sc->sc_freq) * 5 / 4;
    192  1.1  wdk 	sc->sc_maxxfer = MAX_SCSI_XFER;
    193  1.1  wdk 
    194  1.1  wdk #ifdef OLDNCR
    195  1.1  wdk 	if (!NCR_READ_REG(sc, NCR_CFG3)) {
    196  1.1  wdk 		printf(" [old revision]");
    197  1.1  wdk 		sc->sc_cfg2 = 0;
    198  1.1  wdk 		sc->sc_cfg3 = 0;
    199  1.1  wdk 		sc->sc_minsync = 0;
    200  1.1  wdk 	}
    201  1.1  wdk #endif
    202  1.1  wdk 
    203  1.3  wdk 	ncr53c9x_dmaselect = 0;
    204  1.1  wdk 	ncr53c9x_attach(sc, NULL, NULL);
    205  1.1  wdk 
    206  1.3  wdk 	bus_intr_establish(esc->sc_bst, SYS_INTR_SCSI, 0, 0, asc_intr, esc);
    207  1.1  wdk }
    208  1.1  wdk 
    209  1.1  wdk /*
    210  1.1  wdk  * Glue functions.
    211  1.1  wdk  */
    212  1.1  wdk 
    213  1.1  wdk u_char
    214  1.1  wdk asc_read_reg(sc, reg)
    215  1.1  wdk 	struct ncr53c9x_softc *sc;
    216  1.1  wdk 	int reg;
    217  1.1  wdk {
    218  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    219  1.1  wdk 
    220  1.1  wdk 	return bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg * 4 + 3);
    221  1.1  wdk }
    222  1.1  wdk 
    223  1.1  wdk void
    224  1.1  wdk asc_write_reg(sc, reg, val)
    225  1.1  wdk 	struct ncr53c9x_softc *sc;
    226  1.1  wdk 	int reg;
    227  1.1  wdk 	u_char val;
    228  1.1  wdk {
    229  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    230  1.1  wdk 
    231  1.1  wdk 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg * 4 + 3, val);
    232  1.1  wdk }
    233  1.1  wdk 
    234  1.1  wdk void
    235  1.1  wdk dma_status(sc)
    236  1.1  wdk 	struct ncr53c9x_softc *sc;
    237  1.1  wdk {
    238  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    239  1.1  wdk 	int    count;
    240  1.1  wdk 	int    stat;
    241  1.1  wdk 	void   *addr;
    242  1.1  wdk 	u_int32_t  tc;
    243  1.1  wdk 
    244  1.1  wdk 	tc = (asc_read_reg(sc, NCR_TCM)<<8) + asc_read_reg(sc, NCR_TCL);
    245  1.1  wdk 	count = bus_space_read_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT);
    246  1.1  wdk 	stat  = bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE);
    247  1.1  wdk 	addr  = (void *)
    248  1.1  wdk 	        bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_CADDR);
    249  1.1  wdk 
    250  1.4  wdk 	printf("rambo status: cnt=%x addr=%p stat=%08x tc=%04x "
    251  1.1  wdk 		 "ncr_stat=0x%02x ncr_fifo=0x%02x\n",
    252  1.1  wdk 		 count, addr, stat, tc,
    253  1.1  wdk 		 asc_read_reg(sc, NCR_STAT),
    254  1.4  wdk 		 asc_read_reg(sc, NCR_FFLAG));
    255  1.4  wdk }
    256  1.4  wdk 
    257  1.4  wdk static __inline void
    258  1.4  wdk check_fifo(esc)
    259  1.4  wdk 	struct asc_softc *esc;
    260  1.4  wdk {
    261  1.4  wdk 	register int i=16;
    262  1.4  wdk 
    263  1.4  wdk 	while (i && !(bus_space_read_4(esc->sc_bst, esc->dm_bsh,
    264  1.4  wdk 				       RAMBO_MODE) & RB_FIFO_EMPTY)) {
    265  1.4  wdk 		 DELAY(1); i--;
    266  1.4  wdk 	}
    267  1.4  wdk 
    268  1.4  wdk 	if (!i) {
    269  1.4  wdk 		dma_status((void *)esc);
    270  1.4  wdk 		panic("fifo didn't flush");
    271  1.4  wdk 	}
    272  1.1  wdk }
    273  1.1  wdk 
    274  1.1  wdk int
    275  1.1  wdk asc_dma_isintr(sc)
    276  1.1  wdk 	struct ncr53c9x_softc *sc;
    277  1.1  wdk {
    278  1.1  wdk 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
    279  1.1  wdk }
    280  1.1  wdk 
    281  1.1  wdk void
    282  1.1  wdk asc_dma_reset(sc)
    283  1.1  wdk 	struct ncr53c9x_softc *sc;
    284  1.1  wdk {
    285  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    286  1.1  wdk 
    287  1.1  wdk  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, 0);
    288  1.1  wdk 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE,
    289  1.1  wdk 			  RB_CLRFIFO|RB_CLRERROR);
    290  1.1  wdk 	DELAY(10);
    291  1.1  wdk  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    292  1.1  wdk 
    293  1.1  wdk 	if (esc->sc_flags & DMA_MAPLOADED)
    294  1.1  wdk 		bus_dmamap_unload(esc->sc_dmat, esc->sc_dmamap);
    295  1.1  wdk 
    296  1.1  wdk 	esc->sc_flags = DMA_IDLE;
    297  1.1  wdk }
    298  1.1  wdk 
    299  1.1  wdk /*
    300  1.1  wdk  * Setup a DMA transfer
    301  1.1  wdk  */
    302  1.1  wdk 
    303  1.1  wdk static int
    304  1.1  wdk asc_dma_setup(sc, addr, len, datain, dmasize)
    305  1.1  wdk 	struct ncr53c9x_softc *sc;
    306  1.1  wdk 	caddr_t *addr;
    307  1.1  wdk 	size_t *len;
    308  1.1  wdk 	int datain;
    309  1.1  wdk 	size_t *dmasize;
    310  1.1  wdk {
    311  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    312  1.1  wdk 	paddr_t paddr;
    313  1.1  wdk         size_t count, blocks;
    314  1.1  wdk 	int prime, err;
    315  1.1  wdk 
    316  1.1  wdk #ifdef DIAGNOSTIC
    317  1.1  wdk 	if (esc->sc_flags & DMA_ACTIVE) {
    318  1.1  wdk 		dma_status(sc);
    319  1.1  wdk 		panic("DMA active");
    320  1.1  wdk 	}
    321  1.1  wdk #endif
    322  1.1  wdk 
    323  1.1  wdk 	/* Flush FIFO from previous operation */
    324  1.1  wdk 
    325  1.1  wdk 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE,
    326  1.1  wdk 			  RB_CLRFIFO|RB_CLRERROR);
    327  1.1  wdk  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    328  1.1  wdk 
    329  1.1  wdk 	esc->sc_dmaaddr = addr;
    330  1.1  wdk 	esc->sc_dmalen  = len;
    331  1.1  wdk 	esc->sc_dmasize = *dmasize;
    332  1.1  wdk 	esc->sc_flags   = datain ? DMA_PULLUP : 0;
    333  1.1  wdk 
    334  1.1  wdk 	NCR_DMA(("asc_dma_setup va=%p len=%d datain=%d count=%d\n",
    335  1.1  wdk 		 *addr, *len, datain, esc->sc_dmasize));
    336  1.1  wdk 
    337  1.1  wdk 	/* have dmamap for the transfering addresses */
    338  1.1  wdk 	if (err=bus_dmamap_load(esc->sc_dmat, esc->sc_dmamap,
    339  1.1  wdk 				*esc->sc_dmaaddr, esc->sc_dmasize,
    340  1.1  wdk 				NULL /* kernel address */,
    341  1.1  wdk 				BUS_DMA_NOWAIT))
    342  1.1  wdk 		panic("%s: bus_dmamap_load err=%d", sc->sc_dev.dv_xname, err);
    343  1.1  wdk 
    344  1.1  wdk 	esc->sc_flags |= DMA_MAPLOADED;
    345  1.1  wdk 
    346  1.1  wdk 	/* No cache flush required for R3000 processors */
    347  1.1  wdk 
    348  1.1  wdk 	paddr  = esc->sc_dmamap->dm_segs[0].ds_addr;
    349  1.1  wdk 	count  = esc->sc_dmamap->dm_segs[0].ds_len;
    350  1.1  wdk 	blocks = (((u_int32_t)*addr & 0x3f) + count + 63) >> 6;
    351  1.1  wdk 
    352  1.1  wdk 	esc->dm_mode = (datain ? RB_DMA_WR : RB_DMA_RD) | RB_DMA_ENABLE;
    353  1.1  wdk 	if (esc->sc_dmamap->dm_nsegs > 1)
    354  1.1  wdk 		esc->dm_mode |= RB_INT_ENABLE;	/* Requires DMA chaining */
    355  1.1  wdk 
    356  1.1  wdk 	/* Load DMA transfer address */
    357  1.1  wdk  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_LADDR,
    358  1.1  wdk 			  paddr & ~0x3f);
    359  1.1  wdk 
    360  1.1  wdk 	/* Set count to zero bytes as this will prevent DMA from starting */
    361  1.1  wdk  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, 0);
    362  1.1  wdk 
    363  1.1  wdk 	/* Set transfer direction and enable DMA FIFO */
    364  1.1  wdk  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, esc->dm_mode);
    365  1.1  wdk 
    366  1.1  wdk 	/* If non block-aligned transfer prime FIFO manually */
    367  1.1  wdk 	prime = (u_int32_t)*esc->sc_dmaaddr & 0x3f;
    368  1.1  wdk 	if (prime) {
    369  1.1  wdk 		if (esc->sc_flags & DMA_PULLUP) {
    370  1.1  wdk 			u_int16_t *p;
    371  1.4  wdk 
    372  1.1  wdk 			p = (u_int16_t *)((u_int32_t)*esc->sc_dmaaddr & ~0x3f);
    373  1.1  wdk 			/* Read from NCR 53c94 controller*/
    374  1.1  wdk 			while (prime > 0) {
    375  1.1  wdk 				bus_space_write_2(esc->sc_bst, esc->dm_bsh,
    376  1.1  wdk 						  RAMBO_FIFO, *p++);
    377  1.1  wdk 				prime -= 2;
    378  1.1  wdk 			}
    379  1.1  wdk 		} else {
    380  1.1  wdk 			/* Fetch the first block */
    381  1.1  wdk 			bus_space_write_2(esc->sc_bst, esc->dm_bsh,
    382  1.1  wdk 					  RAMBO_BLKCNT, 1);
    383  1.1  wdk 			while (prime > 0) {
    384  1.1  wdk 				(void)bus_space_read_2(esc->sc_bst,
    385  1.1  wdk 						       esc->dm_bsh,
    386  1.1  wdk 						       RAMBO_FIFO);
    387  1.1  wdk 				prime -= 2;
    388  1.1  wdk 			}
    389  1.2  wdk 			blocks--;	/* 1 block has been prefetched */
    390  1.1  wdk 		}
    391  1.1  wdk 	}
    392  1.1  wdk 	esc->sc_blkcnt = blocks;
    393  1.1  wdk 	esc->dm_curseg = 0;
    394  1.1  wdk 	return 0;
    395  1.1  wdk }
    396  1.1  wdk 
    397  1.1  wdk void
    398  1.1  wdk asc_dma_go(sc)
    399  1.1  wdk 	struct ncr53c9x_softc *sc;
    400  1.1  wdk {
    401  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    402  1.1  wdk 
    403  1.1  wdk 	/* Load block count to start transfer */
    404  1.1  wdk  	bus_space_write_2(esc->sc_bst, esc->dm_bsh,
    405  1.1  wdk 			  RAMBO_BLKCNT, esc->sc_blkcnt);
    406  1.1  wdk 	esc->sc_flags |= DMA_ACTIVE;
    407  1.1  wdk }
    408  1.1  wdk 
    409  1.1  wdk int
    410  1.1  wdk asc_dma_intr(sc)
    411  1.1  wdk 	struct ncr53c9x_softc *sc;
    412  1.1  wdk {
    413  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    414  1.1  wdk 
    415  1.1  wdk 	size_t      resid, len;
    416  1.1  wdk 	int         trans;
    417  1.1  wdk 	u_int32_t   status;
    418  1.1  wdk 	u_int tcl, tcm;
    419  1.1  wdk 
    420  1.1  wdk #ifdef DIAGNOSTIC
    421  1.1  wdk 	if (!(esc->sc_flags & DMA_ACTIVE)) {
    422  1.1  wdk 		dma_status(sc);
    423  1.1  wdk 		panic("DMA not active");
    424  1.1  wdk 	}
    425  1.1  wdk #endif
    426  1.1  wdk 
    427  1.1  wdk 	if ((resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    428  1.1  wdk 		NCR_DMA(("asc_intr: empty FIFO of %d ", resid));
    429  1.1  wdk 		DELAY(10);
    430  1.1  wdk 	}
    431  1.4  wdk 
    432  1.1  wdk 	resid = (tcl = NCR_READ_REG(sc, NCR_TCL)) +
    433  1.1  wdk 		((tcm = NCR_READ_REG(sc, NCR_TCM)) << 8);
    434  1.1  wdk 	trans = esc->sc_dmasize - resid;
    435  1.1  wdk 	if (trans < 0) {			/* transferred < 0 ? */
    436  1.1  wdk 		printf("asc_intr: xfer (%d) > req (%d)\n",
    437  1.1  wdk 		       trans, esc->sc_dmasize);
    438  1.1  wdk 		trans = esc->sc_dmasize;
    439  1.1  wdk 	}
    440  1.1  wdk 
    441  1.1  wdk 	NCR_DMA(("asc_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
    442  1.1  wdk 		 tcl, tcm, trans, resid));
    443  1.1  wdk 
    444  1.1  wdk 	status = bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE);
    445  1.1  wdk 
    446  1.1  wdk 	if (!(status & RB_FIFO_EMPTY)) { /* Data left in RAMBO FIFO */
    447  1.1  wdk 		if (esc->sc_flags & DMA_PULLUP) { /* SCSI Read */
    448  1.1  wdk 			paddr_t ptr;
    449  1.1  wdk 			u_int16_t *p;
    450  1.1  wdk 
    451  1.4  wdk 			resid  = status & 0x1f;
    452  1.4  wdk 
    453  1.1  wdk 			/* take the address of block to fixed up */
    454  1.1  wdk 			ptr = bus_space_read_4(esc->sc_bst, esc->dm_bsh,
    455  1.1  wdk 					       RAMBO_CADDR);
    456  1.1  wdk 			/* find the starting address of fractional data */
    457  1.4  wdk 			p = (u_int16_t *)MIPS_PHYS_TO_KSEG0(ptr+(resid<<1));
    458  1.1  wdk 
    459  1.1  wdk 			/* XXX - disable DMA xfer before flushing FIFO ? */
    460  1.1  wdk 			len = RB_BLK_CNT - resid;
    461  1.1  wdk 			while (len--) {
    462  1.1  wdk 				bus_space_write_2(esc->sc_bst, esc->dm_bsh,
    463  1.1  wdk 						  RAMBO_FIFO, *p++);
    464  1.1  wdk 			}
    465  1.4  wdk 			check_fifo(esc);
    466  1.1  wdk 		} else {		/* SCSI Write */
    467  1.1  wdk 			bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    468  1.4  wdk 					  RAMBO_MODE, 0);
    469  1.4  wdk 			bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    470  1.1  wdk 					  RAMBO_MODE, RB_CLRFIFO);
    471  1.1  wdk 		}
    472  1.1  wdk 	}
    473  1.1  wdk 
    474  1.4  wdk  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, 0);
    475  1.4  wdk 
    476  1.1  wdk 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    477  1.1  wdk 
    478  1.1  wdk 	bus_dmamap_sync(esc->sc_dmat, esc->sc_dmamap,
    479  1.1  wdk 			0, esc->sc_dmasize,
    480  1.1  wdk 			(esc->sc_flags & DMA_PULLUP)
    481  1.1  wdk 			  ? BUS_DMASYNC_POSTREAD
    482  1.1  wdk 			  : BUS_DMASYNC_POSTWRITE);
    483  1.1  wdk 	bus_dmamap_unload(esc->sc_dmat, esc->sc_dmamap);
    484  1.4  wdk 
    485  1.1  wdk 	*esc->sc_dmaaddr += trans;
    486  1.1  wdk 	*esc->sc_dmalen  -= trans;
    487  1.1  wdk 
    488  1.1  wdk 	esc->sc_flags = DMA_IDLE;
    489  1.1  wdk 
    490  1.1  wdk 	return 0;
    491  1.1  wdk }
    492  1.1  wdk 
    493  1.1  wdk 
    494  1.1  wdk void
    495  1.1  wdk asc_dma_stop(sc)
    496  1.1  wdk 	struct ncr53c9x_softc *sc;
    497  1.1  wdk {
    498  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    499  1.1  wdk 
    500  1.1  wdk 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    501  1.1  wdk 	if (esc->sc_flags & DMA_MAPLOADED)
    502  1.1  wdk 		bus_dmamap_unload(esc->sc_dmat, esc->sc_dmamap);
    503  1.1  wdk 	esc->sc_flags = DMA_IDLE;
    504  1.1  wdk }
    505  1.1  wdk 
    506  1.1  wdk int
    507  1.1  wdk asc_dma_isactive(sc)
    508  1.1  wdk 	struct ncr53c9x_softc *sc;
    509  1.1  wdk {
    510  1.1  wdk 	struct asc_softc *esc = (struct asc_softc *)sc;
    511  1.1  wdk 	return (esc->sc_flags & DMA_ACTIVE)? 1 : 0;
    512  1.1  wdk }
    513  1.1  wdk 
    514  1.1  wdk void
    515  1.1  wdk rambo_dma_chain(esc)
    516  1.1  wdk 	struct asc_softc *esc;
    517  1.1  wdk {
    518  1.1  wdk 	int seg;
    519  1.1  wdk 	size_t	count, blocks;
    520  1.1  wdk 	paddr_t paddr;
    521  1.1  wdk 
    522  1.1  wdk 	seg = ++esc->dm_curseg;
    523  1.1  wdk 
    524  1.4  wdk 	if (!(esc->sc_flags & DMA_PULLUP)) /* Wait for FIFO during write */
    525  1.4  wdk 		check_fifo(esc);
    526  1.1  wdk 
    527  1.1  wdk #ifdef DIAGNOSTIC
    528  1.1  wdk 	if (!(esc->sc_flags & DMA_ACTIVE) || seg > esc->sc_dmamap->dm_nsegs)
    529  1.1  wdk 		panic("Unexpected DMA chaining intr");
    530  1.4  wdk 
    531  1.4  wdk 	/* Interrupt can only occur at terminal count, but double check */
    532  1.4  wdk 	if (bus_space_read_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT)) {
    533  1.4  wdk 		dma_status((void *)esc);
    534  1.4  wdk 		panic("rambo blkcnt != 0");
    535  1.4  wdk 	}
    536  1.1  wdk #endif
    537  1.1  wdk 
    538  1.1  wdk 	paddr  = esc->sc_dmamap->dm_segs[seg].ds_addr;
    539  1.1  wdk 	count  = esc->sc_dmamap->dm_segs[seg].ds_len;
    540  1.1  wdk 	blocks = (count + 63) >> 6;
    541  1.1  wdk 
    542  1.1  wdk 	/* Disable DMA interrupt if last segment */
    543  1.1  wdk 	if (seg+1 > esc->sc_dmamap->dm_nsegs) {
    544  1.1  wdk 		bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    545  1.1  wdk 				  RAMBO_MODE, esc->dm_mode & ~RB_INT_ENABLE);
    546  1.1  wdk 	}
    547  1.1  wdk 
    548  1.1  wdk 	/* Load transfer address for next DMA chain */
    549  1.1  wdk  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_LADDR, paddr);
    550  1.1  wdk 
    551  1.1  wdk 	/* DMA restarts when we enter a new block count */
    552  1.1  wdk  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, blocks);
    553  1.1  wdk }
    554  1.1  wdk 
    555  1.3  wdk int
    556  1.3  wdk asc_intr(arg)
    557  1.3  wdk 	void *arg;
    558  1.1  wdk {
    559  1.3  wdk 	register u_int32_t dma_stat;
    560  1.3  wdk 	struct asc_softc *esc = arg;
    561  1.3  wdk 	struct ncr53c9x_softc *sc = arg;
    562  1.3  wdk 
    563  1.3  wdk 	esc->sc_intrcnt.ev_count++;
    564  1.3  wdk 
    565  1.3  wdk 	/* Check for RAMBO DMA Interrupt */
    566  1.3  wdk 	dma_stat = bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE);
    567  1.3  wdk 	if (dma_stat & RB_INTR_PEND) {
    568  1.3  wdk 		rambo_dma_chain(esc);
    569  1.4  wdk 	}
    570  1.4  wdk 	/* Check for NCR 53c94 interrupt */
    571  1.4  wdk 	if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT) {
    572  1.4  wdk 		ncr53c9x_intr(sc);
    573  1.3  wdk 	}
    574  1.3  wdk 	return 0;
    575  1.1  wdk }
    576