Home | History | Annotate | Line # | Download | only in pci
pcscp.c revision 1.36
      1 /*	$NetBSD: pcscp.c,v 1.36 2006/04/28 15:42:18 tsutsui Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1997, 1998, 1999 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  * NASA Ames Research Center; Izumi Tsutsui.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *	This product includes software developed by the NetBSD
     22  *	Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * pcscp.c: device dependent code for AMD Am53c974 (PCscsi-PCI)
     42  * written by Izumi Tsutsui <tsutsui (at) ceres.dti.ne.jp>
     43  *
     44  * Technical manual available at
     45  * http://www.amd.com/files/connectivitysolutions/networking/archivednetworking/19113.pdf
     46  */
     47 
     48 #include <sys/cdefs.h>
     49 __KERNEL_RCSID(0, "$NetBSD: pcscp.c,v 1.36 2006/04/28 15:42:18 tsutsui Exp $");
     50 
     51 #include <sys/param.h>
     52 #include <sys/systm.h>
     53 #include <sys/device.h>
     54 #include <sys/buf.h>
     55 
     56 #include <machine/bus.h>
     57 #include <machine/intr.h>
     58 
     59 #include <uvm/uvm_extern.h>
     60 
     61 #include <dev/scsipi/scsipi_all.h>
     62 #include <dev/scsipi/scsi_all.h>
     63 #include <dev/scsipi/scsiconf.h>
     64 
     65 #include <dev/pci/pcireg.h>
     66 #include <dev/pci/pcivar.h>
     67 #include <dev/pci/pcidevs.h>
     68 
     69 #include <dev/ic/ncr53c9xreg.h>
     70 #include <dev/ic/ncr53c9xvar.h>
     71 
     72 #include <dev/pci/pcscpreg.h>
     73 
     74 #define IO_MAP_REG	0x10
     75 
     76 struct pcscp_softc {
     77 	struct ncr53c9x_softc sc_ncr53c9x;	/* glue to MI code */
     78 
     79 	bus_space_tag_t sc_st;		/* bus space tag */
     80 	bus_space_handle_t sc_sh;	/* bus space handle */
     81 	void *sc_ih;			/* interrupt cookie */
     82 
     83 	bus_dma_tag_t sc_dmat;		/* DMA tag */
     84 
     85 	bus_dmamap_t sc_xfermap;	/* DMA map for transfers */
     86 
     87 	uint32_t *sc_mdladdr;		/* MDL array */
     88 	bus_dmamap_t sc_mdldmap;	/* MDL DMA map */
     89 
     90 	int	sc_active;		/* DMA state */
     91 	int	sc_datain;		/* DMA Data Direction */
     92 	size_t	sc_dmasize;		/* DMA size */
     93 	char	**sc_dmaaddr;		/* DMA address */
     94 	size_t	*sc_dmalen;		/* DMA length */
     95 };
     96 
     97 #define	READ_DMAREG(sc, reg) \
     98 	bus_space_read_4((sc)->sc_st, (sc)->sc_sh, (reg))
     99 #define	WRITE_DMAREG(sc, reg, var) \
    100 	bus_space_write_4((sc)->sc_st, (sc)->sc_sh, (reg), (var))
    101 
    102 #define	PCSCP_READ_REG(sc, reg)	\
    103 	bus_space_read_1((sc)->sc_st, (sc)->sc_sh, (reg) << 2)
    104 #define	PCSCP_WRITE_REG(sc, reg, val)	\
    105 	bus_space_write_1((sc)->sc_st, (sc)->sc_sh, (reg) << 2, (val))
    106 
    107 /*
    108  * Functions and the switch for the MI code.
    109  */
    110 
    111 static u_char	pcscp_read_reg(struct ncr53c9x_softc *, int);
    112 static void	pcscp_write_reg(struct ncr53c9x_softc *, int, u_char);
    113 static int	pcscp_dma_isintr(struct ncr53c9x_softc *);
    114 static void	pcscp_dma_reset(struct ncr53c9x_softc *);
    115 static int	pcscp_dma_intr(struct ncr53c9x_softc *);
    116 static int	pcscp_dma_setup(struct ncr53c9x_softc *, caddr_t *, size_t *,
    117 				int, size_t *);
    118 static void	pcscp_dma_go(struct ncr53c9x_softc *);
    119 static void	pcscp_dma_stop(struct ncr53c9x_softc *);
    120 static int	pcscp_dma_isactive(struct ncr53c9x_softc *);
    121 
    122 static struct ncr53c9x_glue pcscp_glue = {
    123 	pcscp_read_reg,
    124 	pcscp_write_reg,
    125 	pcscp_dma_isintr,
    126 	pcscp_dma_reset,
    127 	pcscp_dma_intr,
    128 	pcscp_dma_setup,
    129 	pcscp_dma_go,
    130 	pcscp_dma_stop,
    131 	pcscp_dma_isactive,
    132 	NULL,			/* gl_clear_latched_intr */
    133 };
    134 
    135 static int
    136 pcscp_match(struct device *parent, struct cfdata *match, void *aux)
    137 {
    138 	struct pci_attach_args *pa = aux;
    139 
    140 	if (PCI_VENDOR(pa->pa_id) != PCI_VENDOR_AMD)
    141 		return 0;
    142 
    143 	switch (PCI_PRODUCT(pa->pa_id)) {
    144 	case PCI_PRODUCT_AMD_PCSCSI_PCI:
    145 		return 1;
    146 	}
    147 	return 0;
    148 }
    149 
    150 /*
    151  * Attach this instance, and then all the sub-devices
    152  */
    153 static void
    154 pcscp_attach(struct device *parent, struct device *self, void *aux)
    155 {
    156 	struct pci_attach_args *pa = aux;
    157 	struct pcscp_softc *esc = (void *)self;
    158 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    159 	bus_space_tag_t iot;
    160 	bus_space_handle_t ioh;
    161 	pci_intr_handle_t ih;
    162 	const char *intrstr;
    163 	pcireg_t csr;
    164 	bus_dma_segment_t seg;
    165 	int error, rseg;
    166 	char devinfo[256];
    167 
    168 	pci_devinfo(pa->pa_id, pa->pa_class, 0, devinfo, sizeof(devinfo));
    169 	printf(": %s\n", devinfo);
    170 	printf("%s", sc->sc_dev.dv_xname);
    171 
    172 	if (pci_mapreg_map(pa, IO_MAP_REG, PCI_MAPREG_TYPE_IO, 0,
    173 	    &iot, &ioh, NULL, NULL)) {
    174 		printf(": unable to map registers\n");
    175 		return;
    176 	}
    177 
    178 	sc->sc_glue = &pcscp_glue;
    179 
    180 	esc->sc_st = iot;
    181 	esc->sc_sh = ioh;
    182 	esc->sc_dmat = pa->pa_dmat;
    183 
    184 	csr = pci_conf_read(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG);
    185 	pci_conf_write(pa->pa_pc, pa->pa_tag, PCI_COMMAND_STATUS_REG,
    186 	    csr | PCI_COMMAND_MASTER_ENABLE | PCI_COMMAND_IO_ENABLE);
    187 
    188 	/*
    189 	 * XXX More of this should be in ncr53c9x_attach(), but
    190 	 * XXX should we really poke around the chip that much in
    191 	 * XXX the MI code?  Think about this more...
    192 	 */
    193 
    194 	/*
    195 	 * Set up static configuration info.
    196 	 */
    197 
    198 	/*
    199 	 * XXX should read configuration from EEPROM?
    200 	 *
    201 	 * MI ncr53c9x driver does not support configuration
    202 	 * per each target device, though...
    203 	 */
    204 	sc->sc_id = 7;
    205 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    206 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
    207 	sc->sc_cfg3 = NCRAMDCFG3_IDM | NCRAMDCFG3_FCLK;
    208 	sc->sc_cfg4 = NCRAMDCFG4_GE12NS | NCRAMDCFG4_RADE;
    209 	sc->sc_rev = NCR_VARIANT_AM53C974;
    210 	sc->sc_features = NCR_F_FASTSCSI;
    211 	sc->sc_cfg3_fscsi = NCRAMDCFG3_FSCSI;
    212 	sc->sc_freq = 40; /* MHz */
    213 
    214 	/*
    215 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    216 	 * XXX but it appears to have some dependency on what sort
    217 	 * XXX of DMA we're hooked up to, etc.
    218 	 */
    219 
    220 	/*
    221 	 * This is the value used to start sync negotiations
    222 	 * Note that the NCR register "SYNCTP" is programmed
    223 	 * in "clocks per byte", and has a minimum value of 4.
    224 	 * The SCSI period used in negotiation is one-fourth
    225 	 * of the time (in nanoseconds) needed to transfer one byte.
    226 	 * Since the chip's clock is given in MHz, we have the following
    227 	 * formula: 4 * period = (1000 / freq) * 4
    228 	 */
    229 
    230 	sc->sc_minsync = 1000 / sc->sc_freq;
    231 
    232 	/* Really no limit, but since we want to fit into the TCR... */
    233 	sc->sc_maxxfer = 16 * 1024 * 1024;
    234 
    235 	/*
    236 	 * Create the DMA maps for the data transfers.
    237 	 */
    238 
    239 #define MDL_SEG_SIZE	0x1000 /* 4kbyte per segment */
    240 #define MDL_SEG_OFFSET	0x0FFF
    241 #define MDL_SIZE	(MAXPHYS / MDL_SEG_SIZE + 1) /* no hardware limit? */
    242 
    243 	if (bus_dmamap_create(esc->sc_dmat, MAXPHYS, MDL_SIZE, MDL_SEG_SIZE,
    244 	    MDL_SEG_SIZE, BUS_DMA_NOWAIT, &esc->sc_xfermap)) {
    245 		printf(": can't create DMA maps\n");
    246 		return;
    247 	}
    248 
    249 	/*
    250 	 * Allocate and map memory for the MDL.
    251 	 */
    252 
    253 	if ((error = bus_dmamem_alloc(esc->sc_dmat,
    254 	    sizeof(uint32_t) * MDL_SIZE, PAGE_SIZE, 0, &seg, 1, &rseg,
    255 	    BUS_DMA_NOWAIT)) != 0) {
    256 		printf(": unable to allocate memory for the MDL, error = %d\n",
    257 		    error);
    258 		goto fail_0;
    259 	}
    260 	if ((error = bus_dmamem_map(esc->sc_dmat, &seg, rseg,
    261 	    sizeof(uint32_t) * MDL_SIZE , (caddr_t *)&esc->sc_mdladdr,
    262 	    BUS_DMA_NOWAIT|BUS_DMA_COHERENT)) != 0) {
    263 		printf(": unable to map the MDL memory, error = %d\n", error);
    264 		goto fail_1;
    265 	}
    266 	if ((error = bus_dmamap_create(esc->sc_dmat,
    267 	    sizeof(uint32_t) * MDL_SIZE, 1, sizeof(uint32_t) * MDL_SIZE,
    268 	    0, BUS_DMA_NOWAIT, &esc->sc_mdldmap)) != 0) {
    269 		printf(": unable to map_create for the MDL, error = %d\n",
    270 		    error);
    271 		goto fail_2;
    272 	}
    273 	if ((error = bus_dmamap_load(esc->sc_dmat, esc->sc_mdldmap,
    274 	     esc->sc_mdladdr, sizeof(uint32_t) * MDL_SIZE,
    275 	     NULL, BUS_DMA_NOWAIT)) != 0) {
    276 		printf(": unable to load for the MDL, error = %d\n", error);
    277 		goto fail_3;
    278 	}
    279 
    280 	/* map and establish interrupt */
    281 	if (pci_intr_map(pa, &ih)) {
    282 		printf(": couldn't map interrupt\n");
    283 		goto fail_4;
    284 	}
    285 
    286 	intrstr = pci_intr_string(pa->pa_pc, ih);
    287 	esc->sc_ih = pci_intr_establish(pa->pa_pc, ih, IPL_BIO,
    288 	    ncr53c9x_intr, esc);
    289 	if (esc->sc_ih == NULL) {
    290 		printf(": couldn't establish interrupt");
    291 		if (intrstr != NULL)
    292 			printf(" at %s", intrstr);
    293 		printf("\n");
    294 		goto fail_4;
    295 	}
    296 	if (intrstr != NULL)
    297 		printf(": interrupting at %s\n", intrstr);
    298 
    299 	/* Do the common parts of attachment. */
    300 	printf("%s", sc->sc_dev.dv_xname);
    301 	sc->sc_adapter.adapt_minphys = minphys;
    302 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    303 	ncr53c9x_attach(sc);
    304 
    305 	/* Turn on target selection using the `DMA' method */
    306 	sc->sc_features |= NCR_F_DMASELECT;
    307 
    308 	return;
    309 
    310  fail_4:
    311 	bus_dmamap_unload(esc->sc_dmat, esc->sc_mdldmap);
    312  fail_3:
    313 	bus_dmamap_destroy(esc->sc_dmat, esc->sc_mdldmap);
    314  fail_2:
    315 	bus_dmamem_unmap(esc->sc_dmat, (caddr_t)esc->sc_mdldmap,
    316 	    sizeof(uint32_t) * MDL_SIZE);
    317  fail_1:
    318 	bus_dmamem_free(esc->sc_dmat, &seg, rseg);
    319  fail_0:
    320 	bus_dmamap_destroy(esc->sc_dmat, esc->sc_xfermap);
    321 }
    322 
    323 CFATTACH_DECL(pcscp, sizeof(struct pcscp_softc),
    324     pcscp_match, pcscp_attach, NULL, NULL);
    325 
    326 /*
    327  * Glue functions.
    328  */
    329 
    330 static u_char
    331 pcscp_read_reg(struct ncr53c9x_softc *sc, int reg)
    332 {
    333 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    334 
    335 	return PCSCP_READ_REG(esc, reg);
    336 }
    337 
    338 static void
    339 pcscp_write_reg(struct ncr53c9x_softc *sc, int reg, u_char v)
    340 {
    341 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    342 
    343 	PCSCP_WRITE_REG(esc, reg, v);
    344 }
    345 
    346 static int
    347 pcscp_dma_isintr(struct ncr53c9x_softc *sc)
    348 {
    349 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    350 
    351 	return (PCSCP_READ_REG(esc, NCR_STAT) & NCRSTAT_INT) != 0;
    352 }
    353 
    354 static void
    355 pcscp_dma_reset(struct ncr53c9x_softc *sc)
    356 {
    357 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    358 
    359 	WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE);
    360 
    361 	esc->sc_active = 0;
    362 }
    363 
    364 static int
    365 pcscp_dma_intr(struct ncr53c9x_softc *sc)
    366 {
    367 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    368 	int trans, resid, i;
    369 	bus_dmamap_t dmap = esc->sc_xfermap;
    370 	int datain = esc->sc_datain;
    371 	uint32_t dmastat;
    372 	char *p = NULL;
    373 
    374 	dmastat = READ_DMAREG(esc, DMA_STAT);
    375 
    376 	if (dmastat & DMASTAT_ERR) {
    377 		/* XXX not tested... */
    378 		WRITE_DMAREG(esc, DMA_CMD,
    379 		    DMACMD_ABORT | (datain ? DMACMD_DIR : 0));
    380 
    381 		printf("%s: error: DMA error detected; Aborting.\n",
    382 		    sc->sc_dev.dv_xname);
    383 		bus_dmamap_unload(esc->sc_dmat, dmap);
    384 		return -1;
    385 	}
    386 
    387 	if (dmastat & DMASTAT_ABT) {
    388 		/* XXX What should be done? */
    389 		printf("%s: dma_intr: DMA aborted.\n", sc->sc_dev.dv_xname);
    390 		WRITE_DMAREG(esc, DMA_CMD,
    391 		    DMACMD_IDLE | (datain ? DMACMD_DIR : 0));
    392 		esc->sc_active = 0;
    393 		return 0;
    394 	}
    395 
    396 #ifdef DIAGNOSTIC
    397 	/* This is an "assertion" :) */
    398 	if (esc->sc_active == 0)
    399 		panic("pcscp dmaintr: DMA wasn't active");
    400 #endif
    401 
    402 	/* DMA has stopped */
    403 
    404 	esc->sc_active = 0;
    405 
    406 	if (esc->sc_dmasize == 0) {
    407 		/* A "Transfer Pad" operation completed */
    408 		NCR_DMA(("dmaintr: discarded %d bytes (tcl=%d, tcm=%d)\n",
    409 		    PCSCP_READ_REG(esc, NCR_TCL) |
    410 		    (PCSCP_READ_REG(esc, NCR_TCM) << 8),
    411 		    PCSCP_READ_REG(esc, NCR_TCL),
    412 		    PCSCP_READ_REG(esc, NCR_TCM)));
    413 		return 0;
    414 	}
    415 
    416 	resid = 0;
    417 	/*
    418 	 * If a transfer onto the SCSI bus gets interrupted by the device
    419 	 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
    420 	 * as residual since the ESP counter registers get decremented as
    421 	 * bytes are clocked into the FIFO.
    422 	 */
    423 	if (!datain &&
    424 	    (resid = (PCSCP_READ_REG(esc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    425 		NCR_DMA(("pcscp_dma_intr: empty esp FIFO of %d ", resid));
    426 	}
    427 
    428 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    429 		/*
    430 		 * `Terminal count' is off, so read the residue
    431 		 * out of the ESP counter registers.
    432 		 */
    433 		if (datain) {
    434 			resid = PCSCP_READ_REG(esc, NCR_FFLAG) & NCRFIFO_FF;
    435 			while (resid > 1)
    436 				resid =
    437 				    PCSCP_READ_REG(esc, NCR_FFLAG) & NCRFIFO_FF;
    438 			WRITE_DMAREG(esc, DMA_CMD, DMACMD_BLAST | DMACMD_MDL |
    439 			    (datain ? DMACMD_DIR : 0));
    440 
    441 			for (i = 0; i < 0x8000; i++) /* XXX 0x8000 ? */
    442 				if (READ_DMAREG(esc, DMA_STAT) & DMASTAT_BCMP)
    443 					break;
    444 
    445 			/* See the below comments... */
    446 			if (resid)
    447 				p = *esc->sc_dmaaddr;
    448 		}
    449 
    450 		resid += PCSCP_READ_REG(esc, NCR_TCL) |
    451 		    (PCSCP_READ_REG(esc, NCR_TCM) << 8) |
    452 		    (PCSCP_READ_REG(esc, NCR_TCH) << 16);
    453 	} else {
    454 		while ((dmastat & DMASTAT_DONE) == 0)
    455 			dmastat = READ_DMAREG(esc, DMA_STAT);
    456 	}
    457 
    458 	WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | (datain ? DMACMD_DIR : 0));
    459 
    460 	/* sync MDL */
    461 	bus_dmamap_sync(esc->sc_dmat, esc->sc_mdldmap,
    462 	    0, sizeof(uint32_t) * dmap->dm_nsegs, BUS_DMASYNC_POSTWRITE);
    463 	/* sync transfer buffer */
    464 	bus_dmamap_sync(esc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    465 	    datain ? BUS_DMASYNC_POSTREAD : BUS_DMASYNC_POSTWRITE);
    466 	bus_dmamap_unload(esc->sc_dmat, dmap);
    467 
    468 	trans = esc->sc_dmasize - resid;
    469 
    470 	/*
    471 	 * From the technical manual notes:
    472 	 *
    473 	 * `In some odd byte conditions, one residual byte will be left
    474 	 *  in the SCSI FIFO, and the FIFO flags will never count to 0.
    475 	 *  When this happens, the residual byte should be retrieved
    476 	 *  via PIO following completion of the BLAST operation.'
    477 	 */
    478 
    479 	if (p) {
    480 		p += trans;
    481 		*p = PCSCP_READ_REG(esc, NCR_FIFO);
    482 		trans++;
    483 	}
    484 
    485 	if (trans < 0) {			/* transferred < 0 ? */
    486 #if 0
    487 		/*
    488 		 * This situation can happen in perfectly normal operation
    489 		 * if the ESP is reselected while using DMA to select
    490 		 * another target.  As such, don't print the warning.
    491 		 */
    492 		printf("%s: xfer (%d) > req (%d)\n",
    493 		    sc->sc_dev.dv_xname, trans, esc->sc_dmasize);
    494 #endif
    495 		trans = esc->sc_dmasize;
    496 	}
    497 
    498 	NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
    499 	    PCSCP_READ_REG(esc, NCR_TCL),
    500 	    PCSCP_READ_REG(esc, NCR_TCM),
    501 	    PCSCP_READ_REG(esc, NCR_TCH),
    502 	    trans, resid));
    503 
    504 	*esc->sc_dmalen -= trans;
    505 	*esc->sc_dmaaddr += trans;
    506 
    507 	return 0;
    508 }
    509 
    510 static int
    511 pcscp_dma_setup(struct ncr53c9x_softc *sc, caddr_t *addr, size_t *len,
    512     int datain, size_t *dmasize)
    513 {
    514 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    515 	bus_dmamap_t dmap = esc->sc_xfermap;
    516 	uint32_t *mdl;
    517 	int error, nseg, seg;
    518 	bus_addr_t s_offset, s_addr;
    519 
    520 	WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | (datain ? DMACMD_DIR : 0));
    521 
    522 	esc->sc_dmaaddr = addr;
    523 	esc->sc_dmalen = len;
    524 	esc->sc_dmasize = *dmasize;
    525 	esc->sc_datain = datain;
    526 
    527 #ifdef DIAGNOSTIC
    528 	if ((*dmasize / MDL_SEG_SIZE) > MDL_SIZE)
    529 		panic("pcscp: transfer size too large");
    530 #endif
    531 
    532 	/*
    533 	 * No need to set up DMA in `Transfer Pad' operation.
    534 	 * (case of *dmasize == 0)
    535 	 */
    536 	if (*dmasize == 0)
    537 		return 0;
    538 
    539 	error = bus_dmamap_load(esc->sc_dmat, dmap, *esc->sc_dmaaddr,
    540 	    *esc->sc_dmalen, NULL,
    541 	    ((sc->sc_nexus->xs->xs_control & XS_CTL_NOSLEEP) ?
    542 	    BUS_DMA_NOWAIT : BUS_DMA_WAITOK) | BUS_DMA_STREAMING |
    543 	    ((sc->sc_nexus->xs->xs_control & XS_CTL_DATA_IN) ?
    544 	     BUS_DMA_READ : BUS_DMA_WRITE));
    545 	if (error) {
    546 		printf("%s: unable to load dmamap, error = %d\n",
    547 		    sc->sc_dev.dv_xname, error);
    548 		return error;
    549 	}
    550 
    551 	/* set transfer length */
    552 	WRITE_DMAREG(esc, DMA_STC, *dmasize);
    553 
    554 	/* set up MDL */
    555 	mdl = esc->sc_mdladdr;
    556 	nseg = dmap->dm_nsegs;
    557 
    558 	/* the first segment is possibly not aligned with 4k MDL boundary */
    559 	s_addr = dmap->dm_segs[0].ds_addr;
    560 	s_offset = s_addr & MDL_SEG_OFFSET;
    561 	s_addr -= s_offset;
    562 
    563 	/* set the first MDL and offset */
    564 	WRITE_DMAREG(esc, DMA_SPA, s_offset);
    565 	*mdl++ = htole32(s_addr);
    566 
    567 	/* the rest dmamap segments are aligned with 4k boundary */
    568 	for (seg = 1; seg < nseg; seg++)
    569 		*mdl++ = htole32(dmap->dm_segs[seg].ds_addr);
    570 
    571 	return 0;
    572 }
    573 
    574 static void
    575 pcscp_dma_go(struct ncr53c9x_softc *sc)
    576 {
    577 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    578 	bus_dmamap_t dmap = esc->sc_xfermap, mdldmap = esc->sc_mdldmap;
    579 	int datain = esc->sc_datain;
    580 
    581 	/* No DMA transfer in Transfer Pad operation */
    582 	if (esc->sc_dmasize == 0)
    583 		return;
    584 
    585 	/* sync transfer buffer */
    586 	bus_dmamap_sync(esc->sc_dmat, dmap, 0, dmap->dm_mapsize,
    587 	    datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    588 
    589 	/* sync MDL */
    590 	bus_dmamap_sync(esc->sc_dmat, mdldmap,
    591 	    0, sizeof(uint32_t) * dmap->dm_nsegs, BUS_DMASYNC_PREWRITE);
    592 
    593 	/* set Starting MDL Address */
    594 	WRITE_DMAREG(esc, DMA_SMDLA, mdldmap->dm_segs[0].ds_addr);
    595 
    596 	/* set DMA command register bits */
    597 	/* XXX DMA Transfer Interrupt Enable bit is broken? */
    598 	WRITE_DMAREG(esc, DMA_CMD, DMACMD_IDLE | DMACMD_MDL |
    599 	    /* DMACMD_INTE | */
    600 	    (datain ? DMACMD_DIR : 0));
    601 
    602 	/* issue DMA start command */
    603 	WRITE_DMAREG(esc, DMA_CMD, DMACMD_START | DMACMD_MDL |
    604 	    /* DMACMD_INTE | */
    605 	    (datain ? DMACMD_DIR : 0));
    606 
    607 	esc->sc_active = 1;
    608 }
    609 
    610 static void
    611 pcscp_dma_stop(struct ncr53c9x_softc *sc)
    612 {
    613 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    614 
    615 	/* DMA stop */
    616 	/* XXX What should we do here ? */
    617 	WRITE_DMAREG(esc, DMA_CMD,
    618 	    DMACMD_ABORT | (esc->sc_datain ? DMACMD_DIR : 0));
    619 	bus_dmamap_unload(esc->sc_dmat, esc->sc_xfermap);
    620 
    621 	esc->sc_active = 0;
    622 }
    623 
    624 static int
    625 pcscp_dma_isactive(struct ncr53c9x_softc *sc)
    626 {
    627 	struct pcscp_softc *esc = (struct pcscp_softc *)sc;
    628 
    629 	/* XXX should check esc->sc_active? */
    630 	if ((READ_DMAREG(esc, DMA_CMD) & DMACMD_CMD) != DMACMD_IDLE)
    631 		return 1;
    632 	return 0;
    633 }
    634