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