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