Home | History | Annotate | Line # | Download | only in jazz
asc.c revision 1.22
      1  1.22  tsutsui /*	$NetBSD: asc.c,v 1.22 2008/04/13 04:55:52 tsutsui Exp $	*/
      2  1.12  tsutsui 
      3  1.12  tsutsui /*
      4  1.12  tsutsui  * Copyright (c) 2003 Izumi Tsutsui.
      5  1.12  tsutsui  * All rights reserved.
      6   1.1       ur  *
      7   1.1       ur  * Redistribution and use in source and binary forms, with or without
      8   1.1       ur  * modification, are permitted provided that the following conditions
      9   1.1       ur  * are met:
     10   1.1       ur  * 1. Redistributions of source code must retain the above copyright
     11   1.1       ur  *    notice, this list of conditions and the following disclaimer.
     12   1.1       ur  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       ur  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       ur  *    documentation and/or other materials provided with the distribution.
     15  1.12  tsutsui  * 3. The name of the author may not be used to endorse or promote products
     16  1.12  tsutsui  *    derived from this software without specific prior written permission.
     17   1.1       ur  *
     18  1.12  tsutsui  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     19  1.12  tsutsui  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     20  1.12  tsutsui  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     21  1.12  tsutsui  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     22  1.12  tsutsui  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     23  1.12  tsutsui  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     24  1.12  tsutsui  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     25  1.12  tsutsui  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     26  1.12  tsutsui  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     27  1.12  tsutsui  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     28   1.1       ur  */
     29  1.13    lukem 
     30  1.13    lukem #include <sys/cdefs.h>
     31  1.22  tsutsui __KERNEL_RCSID(0, "$NetBSD: asc.c,v 1.22 2008/04/13 04:55:52 tsutsui Exp $");
     32   1.1       ur 
     33   1.1       ur #include <sys/param.h>
     34   1.1       ur #include <sys/systm.h>
     35  1.12  tsutsui #include <sys/device.h>
     36   1.1       ur #include <sys/buf.h>
     37  1.12  tsutsui 
     38  1.12  tsutsui #include <machine/autoconf.h>
     39  1.12  tsutsui #include <machine/bus.h>
     40  1.12  tsutsui 
     41   1.1       ur #include <uvm/uvm_extern.h>
     42   1.1       ur 
     43  1.12  tsutsui #include <dev/scsipi/scsipi_all.h>
     44   1.1       ur #include <dev/scsipi/scsi_all.h>
     45   1.1       ur #include <dev/scsipi/scsiconf.h>
     46   1.1       ur 
     47   1.1       ur #include <arc/jazz/jazziovar.h>
     48   1.1       ur #include <arc/jazz/dma.h>
     49   1.1       ur #include <arc/jazz/pica.h>
     50   1.1       ur 
     51  1.12  tsutsui #include <dev/ic/ncr53c9xreg.h>
     52  1.12  tsutsui #include <dev/ic/ncr53c9xvar.h>
     53   1.1       ur 
     54  1.12  tsutsui #define ASC_NPORTS	0x10
     55  1.12  tsutsui #define ASC_ID_53CF94	0xa2	/* XXX should be in MI ncr53c9xreg.h? */
     56  1.18  tsutsui #define ASC_ID_FAS216	0x12	/* XXX should be in MI ncr53c9xreg.h? */
     57   1.1       ur 
     58   1.1       ur struct asc_softc {
     59  1.12  tsutsui 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
     60   1.1       ur 
     61  1.12  tsutsui 	bus_space_tag_t sc_iot;		/* bus space tag */
     62  1.12  tsutsui 	bus_space_handle_t sc_ioh;	/* bus space handle */
     63  1.12  tsutsui 	bus_space_handle_t sc_dmaioh;	/* bus space handle for DMAC */
     64  1.12  tsutsui 
     65  1.12  tsutsui 	bus_dma_tag_t sc_dmat;		/* DMA tag */
     66  1.12  tsutsui 	bus_dmamap_t sc_dmamap;		/* DMA map for transfers */
     67  1.12  tsutsui 
     68  1.12  tsutsui 	int     sc_active;              /* DMA state */
     69  1.12  tsutsui 	int     sc_datain;              /* DMA Data Direction */
     70  1.12  tsutsui 	size_t  sc_dmasize;             /* DMA size */
     71  1.22  tsutsui 	uint8_t **sc_dmaaddr;           /* DMA address */
     72  1.12  tsutsui 	size_t  *sc_dmalen;             /* DMA length */
     73   1.3     soda };
     74   1.3     soda 
     75   1.1       ur /*
     76   1.1       ur  * Autoconfiguration data for config.
     77   1.1       ur  */
     78  1.22  tsutsui int asc_match(device_t, cfdata_t, void *);
     79  1.22  tsutsui void asc_attach(device_t, device_t, void *);
     80   1.1       ur 
     81  1.22  tsutsui CFATTACH_DECL_NEW(asc, sizeof(struct asc_softc),
     82  1.12  tsutsui     asc_match, asc_attach, NULL, NULL);
     83   1.1       ur 
     84  1.16  tsutsui static void asc_minphys(struct buf *);
     85  1.16  tsutsui 
     86   1.1       ur /*
     87  1.12  tsutsui  *  Functions and the switch for the MI code.
     88   1.1       ur  */
     89  1.22  tsutsui uint8_t asc_read_reg(struct ncr53c9x_softc *, int);
     90  1.22  tsutsui void asc_write_reg(struct ncr53c9x_softc *, int, uint8_t);
     91  1.12  tsutsui int asc_dma_isintr(struct ncr53c9x_softc *);
     92  1.12  tsutsui void asc_dma_reset(struct ncr53c9x_softc *);
     93  1.12  tsutsui int asc_dma_intr(struct ncr53c9x_softc *);
     94  1.22  tsutsui int asc_dma_setup(struct ncr53c9x_softc *, uint8_t **, size_t *, int, size_t *);
     95  1.12  tsutsui void asc_dma_go(struct ncr53c9x_softc *);
     96  1.12  tsutsui void asc_dma_stop(struct ncr53c9x_softc *);
     97  1.12  tsutsui int asc_dma_isactive(struct ncr53c9x_softc *);
     98  1.12  tsutsui 
     99  1.12  tsutsui struct ncr53c9x_glue asc_glue = {
    100  1.12  tsutsui 	asc_read_reg,
    101  1.12  tsutsui 	asc_write_reg,
    102  1.12  tsutsui 	asc_dma_isintr,
    103  1.12  tsutsui 	asc_dma_reset,
    104  1.12  tsutsui 	asc_dma_intr,
    105  1.12  tsutsui 	asc_dma_setup,
    106  1.12  tsutsui 	asc_dma_go,
    107  1.12  tsutsui 	asc_dma_stop,
    108  1.12  tsutsui 	asc_dma_isactive,
    109  1.12  tsutsui 	NULL			/* gl_clear_latched_intr */
    110  1.12  tsutsui };
    111   1.1       ur 
    112   1.1       ur /*
    113   1.1       ur  * Match driver based on name
    114   1.1       ur  */
    115   1.1       ur int
    116  1.22  tsutsui asc_match(device_t parent, cfdata_t cf, void *aux)
    117   1.1       ur {
    118   1.1       ur 	struct jazzio_attach_args *ja = aux;
    119   1.1       ur 
    120   1.9  tsutsui 	if (strcmp(ja->ja_name, "ESP216") != 0)
    121  1.12  tsutsui 		return 0;
    122  1.12  tsutsui 	return 1;
    123   1.1       ur }
    124   1.1       ur 
    125   1.1       ur void
    126  1.22  tsutsui asc_attach(device_t parent, device_t self, void *aux)
    127   1.1       ur {
    128  1.22  tsutsui 	struct asc_softc *asc = device_private(self);
    129  1.22  tsutsui 	struct ncr53c9x_softc *sc = &asc->sc_ncr53c9x;
    130   1.1       ur 	struct jazzio_attach_args *ja = aux;
    131  1.12  tsutsui 	bus_space_tag_t iot;
    132  1.18  tsutsui 	uint8_t asc_id;
    133   1.1       ur 
    134  1.12  tsutsui #if 0
    135  1.12  tsutsui 	/* Need info from platform dependent config?? */
    136   1.3     soda 	if (asc_conf == NULL)
    137   1.3     soda 		panic("asc_conf isn't initialized");
    138  1.12  tsutsui #endif
    139   1.3     soda 
    140  1.22  tsutsui 	sc->sc_dev = self;
    141  1.12  tsutsui 	sc->sc_glue = &asc_glue;
    142   1.1       ur 
    143  1.12  tsutsui 	asc->sc_iot = iot = ja->ja_bust;
    144  1.12  tsutsui 	asc->sc_dmat = ja->ja_dmat;
    145   1.3     soda 
    146  1.12  tsutsui 	if (bus_space_map(iot, ja->ja_addr, ASC_NPORTS, 0, &asc->sc_ioh)) {
    147  1.22  tsutsui 		aprint_error(": unable to map I/O space\n");
    148  1.12  tsutsui 		return;
    149  1.12  tsutsui 	}
    150   1.1       ur 
    151  1.12  tsutsui 	if (bus_space_map(iot, R4030_SYS_DMA0_REGS, R4030_DMA_RANGE,
    152  1.12  tsutsui 	    0, &asc->sc_dmaioh)) {
    153  1.22  tsutsui 		aprint_error(": unable to map DMA I/O space\n");
    154  1.12  tsutsui 		goto out1;
    155  1.12  tsutsui 	}
    156   1.1       ur 
    157  1.12  tsutsui 	if (bus_dmamap_create(asc->sc_dmat, MAXPHYS, 1, MAXPHYS, 0,
    158  1.22  tsutsui 	    BUS_DMA_ALLOCNOW | BUS_DMA_NOWAIT, &asc->sc_dmamap)) {
    159  1.22  tsutsui 		aprint_error(": unable to create DMA map\n");
    160  1.12  tsutsui 		goto out2;
    161  1.12  tsutsui 	}
    162   1.1       ur 
    163   1.1       ur 	/*
    164  1.12  tsutsui 	 * XXX More of this should be in ncr53c9x_attach(), but
    165  1.12  tsutsui 	 * XXX should we really poke around the chip that much in
    166  1.12  tsutsui 	 * XXX the MI code?  Think about this more...
    167   1.1       ur 	 */
    168   1.1       ur 
    169  1.12  tsutsui 	/*
    170  1.12  tsutsui 	 * Set up static configuration info.
    171  1.12  tsutsui 	 */
    172  1.12  tsutsui 	sc->sc_id = 7; /* XXX should be taken from ARC BIOS */
    173  1.12  tsutsui 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    174   1.1       ur 
    175   1.1       ur 	/* identify 53CF9x-2 or not */
    176  1.12  tsutsui 	asc_write_reg(sc, NCR_CMD, NCRCMD_RSTCHIP);
    177  1.12  tsutsui 	DELAY(25);
    178  1.12  tsutsui 	asc_write_reg(sc, NCR_CMD, NCRCMD_DMA | NCRCMD_NOP);
    179  1.12  tsutsui 	DELAY(25);
    180  1.12  tsutsui 	asc_write_reg(sc, NCR_CFG2, NCRCFG2_FE);
    181  1.12  tsutsui 	DELAY(25);
    182  1.12  tsutsui 	asc_write_reg(sc, NCR_CMD, NCRCMD_DMA | NCRCMD_NOP);
    183  1.12  tsutsui 	DELAY(25);
    184  1.18  tsutsui 	asc_id = asc_read_reg(sc, NCR_TCH);
    185  1.18  tsutsui 	if (asc_id == ASC_ID_53CF94 || asc_id == ASC_ID_FAS216) {
    186  1.12  tsutsui 		/* XXX should be have NCR_VARIANT_NCR53CF94? */
    187  1.12  tsutsui 		sc->sc_rev = NCR_VARIANT_NCR53C94;
    188  1.12  tsutsui 		sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
    189  1.12  tsutsui 		sc->sc_cfg3 = NCRF9XCFG3_IDM | NCRF9XCFG3_FCLK;
    190  1.12  tsutsui 		sc->sc_features = NCR_F_FASTSCSI;
    191  1.12  tsutsui 		sc->sc_cfg3_fscsi = NCRF9XCFG3_FSCSI;
    192  1.12  tsutsui 		sc->sc_freq = 40; /* MHz */
    193  1.12  tsutsui 		sc->sc_maxxfer = 16 * 1024 * 1024;
    194  1.12  tsutsui 	} else {
    195  1.12  tsutsui 		sc->sc_rev = NCR_VARIANT_NCR53C94;
    196  1.12  tsutsui 		sc->sc_freq = 25; /* MHz */
    197  1.12  tsutsui 		sc->sc_maxxfer = 64 * 1024;
    198  1.12  tsutsui 	}
    199   1.1       ur 
    200   1.1       ur 	/*
    201  1.12  tsutsui 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    202  1.12  tsutsui 	 * XXX but it appears to have some dependency on what sort
    203  1.12  tsutsui 	 * XXX of DMA we're hooked up to, etc.
    204   1.1       ur 	 */
    205   1.1       ur 
    206   1.1       ur 	/*
    207  1.12  tsutsui 	 * This is the value used to start sync negotiations
    208  1.12  tsutsui 	 * Note that the NCR register "SYNCTP" is programmed
    209  1.12  tsutsui 	 * in "clocks per byte", and has a minimum value of 4.
    210  1.12  tsutsui 	 * The SCSI period used in negotiation is one-fourth
    211  1.12  tsutsui 	 * of the time (in nanoseconds) needed to transfer one byte.
    212  1.12  tsutsui 	 * Since the chip's clock is given in MHz, we have the following
    213  1.12  tsutsui 	 * formula: 4 * period = (1000 / freq) * 4
    214   1.1       ur 	 */
    215  1.12  tsutsui 	sc->sc_minsync = 1000 / sc->sc_freq;
    216  1.12  tsutsui 
    217  1.12  tsutsui 	/* establish interrupt */
    218  1.12  tsutsui 	jazzio_intr_establish(ja->ja_intr, ncr53c9x_intr, asc);
    219  1.12  tsutsui 
    220  1.12  tsutsui 	/* Do the common parts of attachment. */
    221  1.16  tsutsui 	sc->sc_adapter.adapt_minphys = asc_minphys;
    222  1.12  tsutsui 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    223  1.12  tsutsui 	ncr53c9x_attach(sc);
    224   1.1       ur 
    225  1.16  tsutsui #if 0
    226  1.12  tsutsui 	/* Turn on target selection using the `DMA' method */
    227  1.12  tsutsui 	sc->sc_features |= NCR_F_DMASELECT;
    228  1.16  tsutsui #endif
    229  1.12  tsutsui 	return;
    230   1.1       ur 
    231  1.12  tsutsui  out2:
    232  1.12  tsutsui 	bus_space_unmap(iot, asc->sc_dmaioh, R4030_DMA_RANGE);
    233  1.12  tsutsui  out1:
    234  1.12  tsutsui 	bus_space_unmap(iot, asc->sc_ioh, ASC_NPORTS);
    235   1.1       ur }
    236   1.1       ur 
    237  1.16  tsutsui 
    238  1.16  tsutsui static void
    239  1.16  tsutsui asc_minphys(struct buf *bp)
    240  1.16  tsutsui {
    241  1.16  tsutsui 
    242  1.16  tsutsui #define ASC_MAX_XFER	(32 * 1024)	/* XXX can't xfer 64kbytes? */
    243  1.16  tsutsui 
    244  1.16  tsutsui 	if (bp->b_bcount > ASC_MAX_XFER)
    245  1.16  tsutsui 		bp->b_bcount = ASC_MAX_XFER;
    246  1.16  tsutsui 	minphys(bp);
    247  1.16  tsutsui }
    248  1.16  tsutsui 
    249   1.1       ur /*
    250  1.12  tsutsui  * Glue functions.
    251   1.1       ur  */
    252  1.12  tsutsui 
    253  1.22  tsutsui uint8_t
    254  1.15  tsutsui asc_read_reg(struct ncr53c9x_softc *sc, int reg)
    255   1.2   bouyer {
    256  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    257   1.1       ur 
    258  1.12  tsutsui 	return bus_space_read_1(asc->sc_iot, asc->sc_ioh, reg);
    259   1.1       ur }
    260   1.1       ur 
    261   1.2   bouyer void
    262  1.22  tsutsui asc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
    263   1.1       ur {
    264  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    265   1.1       ur 
    266  1.12  tsutsui 	bus_space_write_1(asc->sc_iot, asc->sc_ioh, reg, val);
    267   1.1       ur }
    268   1.1       ur 
    269  1.12  tsutsui int
    270  1.15  tsutsui asc_dma_isintr(struct ncr53c9x_softc *sc)
    271   1.1       ur {
    272   1.1       ur 
    273  1.12  tsutsui 	return asc_read_reg(sc, NCR_STAT) & NCRSTAT_INT;
    274   1.1       ur }
    275   1.1       ur 
    276  1.12  tsutsui void
    277  1.15  tsutsui asc_dma_reset(struct ncr53c9x_softc *sc)
    278   1.1       ur {
    279  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    280   1.1       ur 
    281  1.12  tsutsui 	/* halt DMA */
    282  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_ENAB, 0);
    283  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_MODE, 0);
    284   1.1       ur }
    285   1.1       ur 
    286   1.1       ur int
    287  1.15  tsutsui asc_dma_intr(struct ncr53c9x_softc *sc)
    288   1.1       ur {
    289  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    290  1.12  tsutsui 	int datain, resid, trans;
    291   1.1       ur 
    292  1.12  tsutsui 	datain = asc->sc_datain;
    293   1.1       ur 
    294  1.12  tsutsui #ifdef DIAGNOSTIC
    295  1.12  tsutsui 	/* This is an "assertion" :) */
    296  1.12  tsutsui 	if (asc->sc_active == 0)
    297  1.22  tsutsui 		panic("%s: DMA wasn't active", __func__);
    298   1.1       ur #endif
    299   1.1       ur 
    300  1.12  tsutsui 	/* DMA has stopped */
    301   1.1       ur 
    302  1.12  tsutsui 	asc->sc_active = 0;
    303   1.1       ur 
    304  1.12  tsutsui 	if (asc->sc_dmasize == 0) {
    305  1.12  tsutsui 		/* A "Transfer Pad" operation complete */
    306  1.12  tsutsui 		NCR_DMA(("dmaintr: discarded %d bytes (tcl=%d, tcm=%d)\n",
    307  1.12  tsutsui 		    NCR_READ_REG(sc, NCR_TCL) |
    308  1.12  tsutsui 		    (NCR_READ_REG(sc, NCR_TCM) << 8),
    309  1.12  tsutsui 		    NCR_READ_REG(sc, NCR_TCL),
    310  1.12  tsutsui 		    NCR_READ_REG(sc, NCR_TCM)));
    311   1.1       ur 
    312  1.12  tsutsui 		return 0;
    313   1.1       ur 	}
    314   1.1       ur 
    315  1.12  tsutsui 	resid = 0;
    316   1.1       ur 
    317   1.1       ur 	/*
    318  1.12  tsutsui 	 * If a transfer onto the SCSI bus gets interrupted by the device
    319  1.12  tsutsui 	 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
    320  1.12  tsutsui 	 * as residual since the ESP counter registers get decremented as
    321  1.12  tsutsui 	 * bytes are clocked into the FIFO.
    322   1.1       ur 	 */
    323  1.12  tsutsui 	if (!datain &&
    324  1.12  tsutsui 	    (resid = (asc_read_reg(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    325  1.12  tsutsui 		NCR_DMA(("asc_dma_intr: empty asc FIFO of %d ", resid));
    326   1.1       ur 	}
    327   1.1       ur 
    328  1.12  tsutsui 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    329  1.12  tsutsui 		/*
    330  1.12  tsutsui 		 * `Terminal count' is off, so read the residue
    331  1.12  tsutsui 		 * out of the ASC counter registers.
    332  1.12  tsutsui 		 */
    333  1.12  tsutsui 		resid += (NCR_READ_REG(sc, NCR_TCL) |
    334  1.12  tsutsui 		    (NCR_READ_REG(sc, NCR_TCM) << 8) |
    335  1.12  tsutsui 		    ((sc->sc_cfg2 & NCRCFG2_FE)
    336  1.12  tsutsui 		    ? (NCR_READ_REG(sc, NCR_TCH) << 16) : 0));
    337  1.12  tsutsui 
    338  1.12  tsutsui 		if (resid == 0 && asc->sc_dmasize == 65536 &&
    339  1.12  tsutsui 		    (sc->sc_cfg2 & NCRCFG2_FE) == 0)
    340  1.12  tsutsui 			/* A transfer of 64K is encoded as `TCL=TCM=0' */
    341  1.12  tsutsui 			resid = 65536;
    342  1.12  tsutsui 	}
    343  1.12  tsutsui 
    344  1.12  tsutsui 	/* halt DMA */
    345  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_COUNT, 0);
    346  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_ENAB, 0);
    347  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_MODE, 0);
    348  1.12  tsutsui 
    349  1.12  tsutsui 	bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
    350  1.12  tsutsui 	    0, asc->sc_dmamap->dm_mapsize,
    351  1.12  tsutsui 	    datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    352  1.12  tsutsui 	bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
    353   1.1       ur 
    354  1.12  tsutsui 	trans = asc->sc_dmasize - resid;
    355   1.1       ur 
    356  1.12  tsutsui 	if (trans < 0) {		/* transfered < 0 ? */
    357  1.12  tsutsui #if 0
    358   1.1       ur 		/*
    359  1.12  tsutsui 		 * This situation can happen in perfectly normal operation
    360  1.12  tsutsui 		 * if the ESP is reselected while using DMA to select
    361  1.12  tsutsui 		 * another target.  As such, don't print the warning.
    362   1.1       ur 		 */
    363  1.12  tsutsui 		printf("%s: xfer (%d) > req (%d)\n",
    364  1.12  tsutsui 		    sc->sc_dev.dv_xname, trans, asc->sc_dmasize);
    365   1.1       ur #endif
    366  1.12  tsutsui 		trans = asc->sc_dmasize;
    367   1.1       ur 	}
    368  1.12  tsutsui 	NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
    369  1.12  tsutsui 	    NCR_READ_REG(sc, NCR_TCL),
    370  1.12  tsutsui 	    NCR_READ_REG(sc, NCR_TCM),
    371  1.12  tsutsui 	    (sc->sc_cfg2 & NCRCFG2_FE) ? NCR_READ_REG(sc, NCR_TCH) : 0,
    372  1.12  tsutsui 	    trans, resid));
    373   1.1       ur 
    374  1.12  tsutsui 	*asc->sc_dmalen -= trans;
    375  1.12  tsutsui 	*asc->sc_dmaaddr += trans;
    376   1.1       ur 
    377  1.12  tsutsui 	return 0;
    378   1.1       ur }
    379   1.1       ur 
    380  1.12  tsutsui int
    381  1.22  tsutsui asc_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
    382  1.15  tsutsui     int datain, size_t *dmasize)
    383  1.12  tsutsui {
    384  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    385  1.12  tsutsui 
    386  1.12  tsutsui 	/* halt DMA */
    387  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_ENAB, 0);
    388  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_MODE, 0);
    389  1.12  tsutsui 
    390  1.22  tsutsui 	asc->sc_dmaaddr = addr;
    391  1.12  tsutsui 	asc->sc_dmalen = len;
    392  1.12  tsutsui 	asc->sc_dmasize = *dmasize;
    393  1.12  tsutsui 	asc->sc_datain = datain;
    394  1.12  tsutsui 
    395  1.12  tsutsui 	/*
    396  1.12  tsutsui 	 * No need to set up DMA in `Transfer Pad' operation.
    397  1.12  tsutsui 	 */
    398  1.12  tsutsui 	if (*dmasize == 0)
    399  1.12  tsutsui 		return 0;
    400  1.12  tsutsui 
    401  1.12  tsutsui 	bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap, *addr, *len, NULL,
    402  1.12  tsutsui 	    ((sc->sc_nexus->xs->xs_control & XS_CTL_NOSLEEP) ?
    403  1.12  tsutsui 	    BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    404  1.12  tsutsui 	    (datain ? BUS_DMA_READ : BUS_DMA_WRITE));
    405  1.12  tsutsui 	bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
    406  1.12  tsutsui 	    0, asc->sc_dmamap->dm_mapsize,
    407  1.12  tsutsui 	    datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    408   1.1       ur 
    409  1.12  tsutsui 	/* load transfer parameters */
    410  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh,
    411  1.12  tsutsui 	    R4030_DMA_ADDR, asc->sc_dmamap->dm_segs[0].ds_addr);
    412  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh,
    413  1.12  tsutsui 	    R4030_DMA_COUNT, asc->sc_dmamap->dm_segs[0].ds_len);
    414  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh,
    415  1.12  tsutsui 	    R4030_DMA_MODE, R4030_DMA_MODE_160NS | R4030_DMA_MODE_16);
    416  1.12  tsutsui 
    417  1.12  tsutsui 	/* start DMA */
    418  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh,
    419  1.12  tsutsui 	    R4030_DMA_ENAB, R4030_DMA_ENAB_RUN |
    420  1.12  tsutsui 	    (asc->sc_datain ? R4030_DMA_ENAB_READ : R4030_DMA_ENAB_WRITE));
    421   1.1       ur 
    422  1.16  tsutsui 	return 0;
    423  1.16  tsutsui }
    424  1.16  tsutsui 
    425  1.16  tsutsui void
    426  1.16  tsutsui asc_dma_go(struct ncr53c9x_softc *sc)
    427  1.16  tsutsui {
    428  1.16  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    429  1.16  tsutsui 
    430  1.16  tsutsui 	/* No DMA transfer in Transfer Pad operation */
    431  1.16  tsutsui 	if (asc->sc_dmasize == 0)
    432  1.16  tsutsui 		return;
    433  1.16  tsutsui 
    434  1.12  tsutsui 	asc->sc_active = 1;
    435   1.1       ur }
    436   1.1       ur 
    437  1.12  tsutsui void
    438  1.15  tsutsui asc_dma_stop(struct ncr53c9x_softc *sc)
    439   1.1       ur {
    440  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    441   1.1       ur 
    442  1.12  tsutsui 	/* halt DMA */
    443  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_ENAB, 0);
    444  1.12  tsutsui 	bus_space_write_4(asc->sc_iot, asc->sc_dmaioh, R4030_DMA_MODE, 0);
    445   1.1       ur 
    446  1.14  tsutsui 	bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
    447  1.14  tsutsui 
    448  1.12  tsutsui 	asc->sc_active = 0;
    449   1.1       ur }
    450   1.1       ur 
    451  1.12  tsutsui int
    452  1.15  tsutsui asc_dma_isactive(struct ncr53c9x_softc *sc)
    453   1.1       ur {
    454  1.12  tsutsui 	struct asc_softc *asc = (struct asc_softc *)sc;
    455   1.1       ur 
    456  1.12  tsutsui 	return asc->sc_active;
    457   1.1       ur }
    458