Home | History | Annotate | Line # | Download | only in vsa
asc_vsbus.c revision 1.41
      1  1.41     rmind /*	$NetBSD: asc_vsbus.c,v 1.41 2009/11/21 04:45:39 rmind Exp $	*/
      2   1.1      matt 
      3   1.1      matt /*-
      4   1.1      matt  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      5   1.1      matt  * All rights reserved.
      6   1.1      matt  *
      7   1.1      matt  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      matt  * by Charles M. Hannum.
      9   1.1      matt  *
     10   1.1      matt  * Redistribution and use in source and binary forms, with or without
     11   1.1      matt  * modification, are permitted provided that the following conditions
     12   1.1      matt  * are met:
     13   1.1      matt  * 1. Redistributions of source code must retain the above copyright
     14   1.1      matt  *    notice, this list of conditions and the following disclaimer.
     15   1.1      matt  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      matt  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      matt  *    documentation and/or other materials provided with the distribution.
     18   1.1      matt  *
     19   1.1      matt  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20   1.1      matt  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21   1.1      matt  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22   1.1      matt  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23   1.1      matt  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24   1.1      matt  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25   1.1      matt  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26   1.1      matt  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27   1.1      matt  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28   1.1      matt  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29   1.1      matt  * POSSIBILITY OF SUCH DAMAGE.
     30   1.1      matt  */
     31   1.1      matt 
     32  1.31       chs #include <sys/cdefs.h>			/* RCS ID & Copyright macro defns */
     33   1.9     ragge 
     34  1.41     rmind __KERNEL_RCSID(0, "$NetBSD: asc_vsbus.c,v 1.41 2009/11/21 04:45:39 rmind Exp $");
     35   1.1      matt 
     36  1.31       chs #include "locators.h"
     37  1.31       chs #include "opt_cputype.h"
     38   1.1      matt 
     39   1.1      matt #include <sys/types.h>
     40   1.1      matt #include <sys/param.h>
     41   1.1      matt #include <sys/systm.h>
     42   1.1      matt #include <sys/kernel.h>
     43   1.1      matt #include <sys/errno.h>
     44   1.1      matt #include <sys/ioctl.h>
     45   1.1      matt #include <sys/device.h>
     46   1.1      matt #include <sys/buf.h>
     47   1.1      matt #include <sys/proc.h>
     48   1.1      matt #include <sys/reboot.h>
     49   1.1      matt #include <sys/queue.h>
     50   1.1      matt 
     51   1.1      matt #include <dev/scsipi/scsi_all.h>
     52   1.1      matt #include <dev/scsipi/scsipi_all.h>
     53   1.1      matt #include <dev/scsipi/scsiconf.h>
     54   1.1      matt #include <dev/scsipi/scsi_message.h>
     55   1.1      matt 
     56   1.1      matt #include <machine/bus.h>
     57   1.5      matt #include <machine/vmparam.h>
     58   1.1      matt 
     59   1.1      matt #include <dev/ic/ncr53c9xreg.h>
     60   1.1      matt #include <dev/ic/ncr53c9xvar.h>
     61   1.1      matt 
     62   1.1      matt #include <machine/cpu.h>
     63   1.1      matt #include <machine/sid.h>
     64   1.1      matt #include <machine/scb.h>
     65   1.1      matt #include <machine/vsbus.h>
     66   1.9     ragge #include <machine/clock.h>	/* for SCSI ctlr ID# XXX */
     67   1.1      matt 
     68   1.1      matt struct asc_vsbus_softc {
     69   1.1      matt 	struct ncr53c9x_softc sc_ncr53c9x;	/* Must be first */
     70  1.15      matt 	struct evcnt sc_intrcnt;		/* count interrupts */
     71   1.1      matt 	bus_space_tag_t sc_bst;			/* bus space tag */
     72   1.1      matt 	bus_space_handle_t sc_bsh;		/* bus space handle */
     73  1.11      matt 	bus_space_handle_t sc_dirh;		/* scsi direction handle */
     74  1.11      matt 	bus_space_handle_t sc_adrh;		/* scsi address handle */
     75   1.4      matt 	bus_space_handle_t sc_ncrh;		/* ncr bus space handle */
     76  1.29       wiz 	bus_dma_tag_t sc_dmat;			/* bus DMA tag */
     77   1.1      matt 	bus_dmamap_t sc_dmamap;
     78  1.39   tsutsui 	uint8_t **sc_dmaaddr;
     79   1.1      matt 	size_t *sc_dmalen;
     80   1.1      matt 	size_t sc_dmasize;
     81   1.1      matt 	unsigned int sc_flags;
     82   1.6      matt #define	ASC_FROMMEMORY		0x0001		/* Must be 1 */
     83   1.6      matt #define	ASC_DMAACTIVE		0x0002
     84   1.4      matt #define	ASC_MAPLOADED		0x0004
     85   1.6      matt 	unsigned long sc_xfers;
     86   1.1      matt };
     87   1.1      matt 
     88  1.11      matt #define	ASC_REG_KA46_ADR	0x0000
     89  1.11      matt #define	ASC_REG_KA46_DIR	0x000C
     90  1.13      matt #define	ASC_REG_KA49_ADR	0x0000
     91  1.13      matt #define	ASC_REG_KA49_DIR	0x0004
     92   1.1      matt #define	ASC_REG_NCR		0x0080
     93   1.1      matt #define	ASC_REG_END		0x00B0
     94   1.1      matt 
     95   1.1      matt #define	ASC_MAXXFERSIZE		65536
     96   1.5      matt #define	ASC_FREQUENCY		25000000
     97   1.1      matt 
     98  1.38      matt static int asc_vsbus_match(device_t, cfdata_t, void *);
     99  1.38      matt static void asc_vsbus_attach(device_t, device_t, void *);
    100   1.1      matt 
    101  1.39   tsutsui CFATTACH_DECL_NEW(asc_vsbus, sizeof(struct asc_vsbus_softc),
    102  1.28   thorpej     asc_vsbus_match, asc_vsbus_attach, NULL, NULL);
    103   1.1      matt 
    104   1.1      matt /*
    105   1.1      matt  * Functions and the switch for the MI code
    106   1.1      matt  */
    107  1.39   tsutsui static uint8_t	asc_vsbus_read_reg(struct ncr53c9x_softc *, int);
    108  1.39   tsutsui static void	asc_vsbus_write_reg(struct ncr53c9x_softc *, int, uint8_t);
    109  1.20      matt static int	asc_vsbus_dma_isintr(struct ncr53c9x_softc *);
    110  1.20      matt static void	asc_vsbus_dma_reset(struct ncr53c9x_softc *);
    111  1.20      matt static int	asc_vsbus_dma_intr(struct ncr53c9x_softc *);
    112  1.39   tsutsui static int	asc_vsbus_dma_setup(struct ncr53c9x_softc *, uint8_t **,
    113  1.20      matt 		    size_t *, int, size_t *);
    114  1.20      matt static void	asc_vsbus_dma_go(struct ncr53c9x_softc *);
    115  1.20      matt static void	asc_vsbus_dma_stop(struct ncr53c9x_softc *);
    116  1.20      matt static int	asc_vsbus_dma_isactive(struct ncr53c9x_softc *);
    117   1.1      matt 
    118  1.38      matt static const struct ncr53c9x_glue asc_vsbus_glue = {
    119  1.38      matt 	.gl_read_reg	= asc_vsbus_read_reg,
    120  1.38      matt 	.gl_write_reg	= asc_vsbus_write_reg,
    121  1.38      matt 	.gl_dma_isintr	= asc_vsbus_dma_isintr,
    122  1.38      matt 	.gl_dma_reset	= asc_vsbus_dma_reset,
    123  1.38      matt 	.gl_dma_intr	= asc_vsbus_dma_intr,
    124  1.38      matt 	.gl_dma_setup	= asc_vsbus_dma_setup,
    125  1.38      matt 	.gl_dma_go	= asc_vsbus_dma_go,
    126  1.38      matt 	.gl_dma_stop	= asc_vsbus_dma_stop,
    127  1.38      matt 	.gl_dma_isactive = asc_vsbus_dma_isactive,
    128   1.1      matt };
    129   1.1      matt 
    130  1.38      matt static uint8_t asc_attached;		/* can't have more than one asc */
    131  1.12      matt 
    132   1.1      matt static int
    133  1.39   tsutsui asc_vsbus_match(device_t parent, cfdata_t cf, void *aux)
    134   1.1      matt {
    135  1.38      matt 	struct vsbus_attach_args * const va = aux;
    136  1.38      matt 	volatile uint8_t *ncr_regs;
    137   1.1      matt 	int dummy;
    138  1.12      matt 
    139  1.12      matt 	if (asc_attached)
    140  1.12      matt 		return 0;
    141   1.1      matt 
    142  1.14      matt 	if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
    143  1.31       chs 		if (cf->cf_loc[VSBUSCF_CSR] != 0x200c0080)
    144  1.14      matt 			return 0;
    145  1.22     ragge 	} else if (vax_boardtype == VAX_BTYP_49 ||
    146  1.22     ragge 	    vax_boardtype == VAX_BTYP_53) {
    147  1.31       chs 		if (cf->cf_loc[VSBUSCF_CSR] != 0x26000080)
    148  1.14      matt 			return 0;
    149  1.14      matt 	} else {
    150  1.12      matt 		return 0;
    151  1.14      matt 	}
    152  1.12      matt 
    153  1.38      matt 	ncr_regs = (volatile uint8_t *) va->va_addr;
    154   1.1      matt 
    155   1.1      matt 	/*  *** need to generate an interrupt here
    156   1.1      matt 	 * From trial and error, I've determined that an INT is generated
    157   1.1      matt 	 * only when the following sequence of events occurs:
    158   1.1      matt 	 *   1) The interrupt status register (0x05) must be read.
    159   1.1      matt 	 *   2) SCSI bus reset interrupt must be enabled
    160   1.1      matt 	 *   3) SCSI bus reset command must be sent
    161   1.1      matt 	 *   4) NOP command must be sent
    162   1.1      matt 	 */
    163   1.1      matt 
    164   1.3      matt 	dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
    165   1.8     ragge         ncr_regs[NCR_CFG1 << 2] = 0x06; /* we're ID 6, turn on INT for SCSI reset */
    166   1.3      matt         ncr_regs[NCR_CMD << 2] = NCRCMD_RSTSCSI; /* send the reset */
    167   1.3      matt         ncr_regs[NCR_CMD << 2] = NCRCMD_NOP; /* send a NOP */
    168   1.1      matt 	DELAY(10000);
    169   1.1      matt 
    170   1.3      matt 	dummy = ncr_regs[NCR_INTR << 2] & 0xFF;
    171   1.3      matt 	return (dummy & NCRINTR_SBR) != 0;
    172   1.1      matt }
    173   1.1      matt 
    174   1.1      matt 
    175   1.1      matt /*
    176   1.1      matt  * Attach this instance, and then all the sub-devices
    177   1.1      matt  */
    178   1.1      matt static void
    179  1.38      matt asc_vsbus_attach(device_t parent, device_t self, void *aux)
    180   1.1      matt {
    181  1.38      matt 	struct vsbus_attach_args * const va = aux;
    182  1.38      matt 	struct asc_vsbus_softc * const asc = device_private(self);
    183  1.38      matt 	struct ncr53c9x_softc * const sc = &asc->sc_ncr53c9x;
    184   1.1      matt 	int error;
    185   1.1      matt 
    186  1.12      matt 	asc_attached = 1;
    187   1.1      matt 	/*
    188   1.1      matt 	 * Set up glue for MI code early; we use some of it here.
    189   1.1      matt 	 */
    190  1.39   tsutsui 	sc->sc_dev = self;
    191   1.1      matt 	sc->sc_glue = &asc_vsbus_glue;
    192   1.1      matt 
    193  1.36      matt 	asc->sc_bst = va->va_memt;
    194   1.1      matt 	asc->sc_dmat = va->va_dmat;
    195   1.1      matt 
    196   1.1      matt 	error = bus_space_map(asc->sc_bst, va->va_paddr - ASC_REG_NCR,
    197   1.1      matt 	    ASC_REG_END, 0, &asc->sc_bsh);
    198   1.1      matt 	if (error) {
    199  1.38      matt 		aprint_error(": failed to map registers: error=%d\n", error);
    200   1.1      matt 		return;
    201   1.1      matt 	}
    202   1.4      matt 	error = bus_space_subregion(asc->sc_bst, asc->sc_bsh, ASC_REG_NCR,
    203   1.4      matt 	    ASC_REG_END - ASC_REG_NCR, &asc->sc_ncrh);
    204   1.4      matt 	if (error) {
    205  1.39   tsutsui 		aprint_error(": failed to map ncr registers: error=%d\n",
    206  1.39   tsutsui 		    error);
    207   1.4      matt 		return;
    208   1.4      matt 	}
    209  1.11      matt 	if (vax_boardtype == VAX_BTYP_46 || vax_boardtype == VAX_BTYP_48) {
    210  1.11      matt 		error = bus_space_subregion(asc->sc_bst, asc->sc_bsh,
    211  1.38      matt 		    ASC_REG_KA46_ADR, sizeof(uint32_t), &asc->sc_adrh);
    212  1.11      matt 		if (error) {
    213  1.38      matt 			aprint_error(": failed to map adr register: error=%d\n",
    214  1.11      matt 			     error);
    215  1.11      matt 			return;
    216  1.11      matt 		}
    217  1.11      matt 		error = bus_space_subregion(asc->sc_bst, asc->sc_bsh,
    218  1.38      matt 		    ASC_REG_KA46_DIR, sizeof(uint32_t), &asc->sc_dirh);
    219  1.11      matt 		if (error) {
    220  1.38      matt 			aprint_error(": failed to map dir register: error=%d\n",
    221  1.11      matt 			     error);
    222  1.11      matt 			return;
    223  1.11      matt 		}
    224  1.11      matt 	} else {
    225  1.11      matt 		/* This is a gross and disgusting kludge but it'll
    226  1.11      matt 		 * save a bunch of ugly code.  Unlike the VS4000/60,
    227  1.11      matt 		 * the SCSI Address and direction registers are not
    228  1.11      matt 		 * near the SCSI NCR registers and are inside the
    229  1.11      matt 		 * block of general VAXstation registers.  So we grab
    230  1.11      matt 		 * them from there and knowing the internals of the
    231  1.11      matt 		 * bus_space implementation, we cast to bus_space_handles.
    232  1.11      matt 		 */
    233  1.38      matt 		struct vsbus_softc *vsc = device_private(parent);
    234  1.39   tsutsui 		asc->sc_adrh =
    235  1.39   tsutsui 		    (bus_space_handle_t)(vsc->sc_vsregs + ASC_REG_KA49_ADR);
    236  1.39   tsutsui 		asc->sc_dirh =
    237  1.39   tsutsui 		    (bus_space_handle_t)(vsc->sc_vsregs + ASC_REG_KA49_DIR);
    238  1.14      matt #if 0
    239  1.38      matt 		printf("\n%s: adrh=0x%08lx dirh=0x%08lx", device_xname(self),
    240  1.39   tsutsui 		    asc->sc_adrh, asc->sc_dirh);
    241  1.39   tsutsui 		ncr53c9x_debug = NCR_SHOWDMA | NCR_SHOWINTS | NCR_SHOWCMDS |
    242  1.39   tsutsui 		    NCR_SHOWPHASE | NCR_SHOWSTART | NCR_SHOWMSGS;
    243  1.14      matt #endif
    244  1.11      matt 	}
    245   1.4      matt 	error = bus_dmamap_create(asc->sc_dmat, ASC_MAXXFERSIZE, 1,
    246   1.4      matt 	    ASC_MAXXFERSIZE, 0, BUS_DMA_NOWAIT, &asc->sc_dmamap);
    247   1.1      matt 
    248  1.37  christos #if defined(VAX46) || defined(VAX48) || defined(VAX49) || defined(VAXANY)
    249  1.37  christos 	if(vax_boardtype != VAX_BTYP_53)
    250  1.37  christos 		/* SCSI ID is store in the clock NVRAM at magic address 0xbc */
    251  1.39   tsutsui 		sc->sc_id = (clk_page[0xbc / 2] >> clk_tweak) & 7;
    252  1.37  christos 	else
    253  1.37  christos #endif
    254  1.37  christos 		sc->sc_id = 6; /* XXX need to get this from VMB */
    255   1.1      matt 	sc->sc_freq = ASC_FREQUENCY;
    256   1.1      matt 
    257  1.33     lukem 	/* gimme MHz */
    258   1.1      matt 	sc->sc_freq /= 1000000;
    259   1.1      matt 
    260  1.39   tsutsui 	scb_vecalloc(va->va_cvec, (void (*)(void *))ncr53c9x_intr,
    261  1.15      matt 	    &asc->sc_ncr53c9x, SCB_ISTACK, &asc->sc_intrcnt);
    262  1.16      matt 	evcnt_attach_dynamic(&asc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    263  1.38      matt 	    device_xname(self), "intr");
    264   1.1      matt 
    265   1.1      matt 	/*
    266   1.1      matt 	 * XXX More of this should be in ncr53c9x_attach(), but
    267   1.1      matt 	 * XXX should we really poke around the chip that much in
    268   1.1      matt 	 * XXX the MI code?  Think about this more...
    269   1.1      matt 	 */
    270   1.1      matt 
    271   1.1      matt 	/*
    272   1.1      matt 	 * Set up static configuration info.
    273   1.1      matt 	 */
    274   1.1      matt 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    275   1.1      matt 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    276   1.4      matt 	sc->sc_cfg3 = 0;
    277   1.1      matt 	sc->sc_rev = NCR_VARIANT_NCR53C94;
    278   1.1      matt 
    279   1.1      matt 	/*
    280   1.1      matt 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    281   1.1      matt 	 * XXX but it appears to have some dependency on what sort
    282   1.1      matt 	 * XXX of DMA we're hooked up to, etc.
    283   1.1      matt 	 */
    284   1.1      matt 
    285   1.1      matt 	/*
    286   1.1      matt 	 * This is the value used to start sync negotiations
    287   1.1      matt 	 * Note that the NCR register "SYNCTP" is programmed
    288   1.1      matt 	 * in "clocks per byte", and has a minimum value of 4.
    289   1.1      matt 	 * The SCSI period used in negotiation is one-fourth
    290   1.1      matt 	 * of the time (in nanoseconds) needed to transfer one byte.
    291   1.1      matt 	 * Since the chip's clock is given in MHz, we have the following
    292   1.1      matt 	 * formula: 4 * period = (1000 / freq) * 4
    293   1.1      matt 	 */
    294   1.1      matt 	sc->sc_minsync = (1000 / sc->sc_freq);
    295  1.25     chuck 	sc->sc_maxxfer = 64 * 1024;
    296   1.1      matt 
    297  1.38      matt 	aprint_normal("\n%s", device_xname(self)); /* Pretty print */
    298   1.3      matt 
    299   1.1      matt 	/* Do the common parts of attachment. */
    300  1.23    bouyer 	sc->sc_adapter.adapt_minphys = minphys;
    301  1.23    bouyer 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    302  1.23    bouyer 	ncr53c9x_attach(sc);
    303   1.1      matt }
    304   1.1      matt 
    305   1.1      matt /*
    306   1.1      matt  * Glue functions.
    307   1.1      matt  */
    308   1.1      matt 
    309  1.39   tsutsui static uint8_t
    310   1.1      matt asc_vsbus_read_reg(struct ncr53c9x_softc *sc, int reg)
    311   1.1      matt {
    312  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    313   1.1      matt 
    314   1.4      matt 	return bus_space_read_1(asc->sc_bst, asc->sc_ncrh,
    315  1.38      matt 	    reg * sizeof(uint32_t));
    316   1.1      matt }
    317   1.1      matt 
    318   1.1      matt static void
    319  1.39   tsutsui asc_vsbus_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
    320   1.1      matt {
    321  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    322   1.1      matt 
    323   1.4      matt 	bus_space_write_1(asc->sc_bst, asc->sc_ncrh,
    324  1.38      matt 	    reg * sizeof(uint32_t), val);
    325   1.1      matt }
    326   1.1      matt 
    327   1.1      matt static int
    328  1.38      matt asc_vsbus_dma_isintr(struct ncr53c9x_softc *sc)
    329   1.1      matt {
    330  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    331  1.39   tsutsui 
    332   1.4      matt 	return bus_space_read_1(asc->sc_bst, asc->sc_ncrh,
    333  1.38      matt 	    NCR_STAT * sizeof(uint32_t)) & NCRSTAT_INT;
    334   1.1      matt }
    335   1.1      matt 
    336   1.1      matt static void
    337  1.38      matt asc_vsbus_dma_reset(struct ncr53c9x_softc *sc)
    338   1.1      matt {
    339  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    340   1.1      matt 
    341   1.4      matt 	if (asc->sc_flags & ASC_MAPLOADED)
    342   1.4      matt 		bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
    343   1.4      matt 	asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
    344   1.1      matt }
    345   1.1      matt 
    346   1.1      matt static int
    347  1.38      matt asc_vsbus_dma_intr(struct ncr53c9x_softc *sc)
    348   1.1      matt {
    349  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    350   1.4      matt 	u_int tcl, tcm;
    351   1.4      matt 	int trans, resid;
    352   1.4      matt 
    353   1.4      matt 	if ((asc->sc_flags & ASC_DMAACTIVE) == 0)
    354  1.39   tsutsui 		panic("%s: DMA wasn't active", __func__);
    355   1.4      matt 
    356   1.4      matt 	asc->sc_flags &= ~ASC_DMAACTIVE;
    357   1.4      matt 
    358   1.4      matt 	if (asc->sc_dmasize == 0) {
    359   1.4      matt 		/* A "Transfer Pad" operation completed */
    360   1.4      matt 		tcl = NCR_READ_REG(sc, NCR_TCL);
    361   1.4      matt 		tcm = NCR_READ_REG(sc, NCR_TCM);
    362   1.4      matt 		NCR_DMA(("asc_vsbus_intr: discarded %d bytes (tcl=%d, tcm=%d)\n",
    363   1.4      matt 		    tcl | (tcm << 8), tcl, tcm));
    364   1.4      matt 		return 0;
    365   1.4      matt 	}
    366   1.4      matt 
    367   1.4      matt 	resid = 0;
    368   1.4      matt 	if ((resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    369   1.4      matt 		NCR_DMA(("asc_vsbus_intr: empty FIFO of %d ", resid));
    370   1.4      matt 		DELAY(1);
    371   1.4      matt 	}
    372   1.6      matt 	if (asc->sc_flags & ASC_MAPLOADED) {
    373   1.6      matt 		bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
    374   1.6      matt 				0, asc->sc_dmasize,
    375   1.6      matt 				asc->sc_flags & ASC_FROMMEMORY
    376   1.6      matt 					? BUS_DMASYNC_POSTWRITE
    377   1.6      matt 					: BUS_DMASYNC_POSTREAD);
    378   1.4      matt 		bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
    379   1.6      matt 	}
    380   1.4      matt 	asc->sc_flags &= ~ASC_MAPLOADED;
    381   1.4      matt 
    382   1.4      matt 	resid += (tcl = NCR_READ_REG(sc, NCR_TCL));
    383   1.4      matt 	resid += (tcm = NCR_READ_REG(sc, NCR_TCM)) << 8;
    384   1.4      matt 
    385   1.4      matt 	trans = asc->sc_dmasize - resid;
    386   1.4      matt 	if (trans < 0) {			/* transferred < 0 ? */
    387  1.19      matt 		printf("asc_vsbus_intr: xfer (%d) > req (%lu)\n",
    388  1.19      matt 		    trans, (u_long) asc->sc_dmasize);
    389   1.4      matt 		trans = asc->sc_dmasize;
    390   1.4      matt 	}
    391   1.4      matt 	NCR_DMA(("asc_vsbus_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
    392   1.4      matt 	    tcl, tcm, trans, resid));
    393   1.1      matt 
    394   1.4      matt 	*asc->sc_dmalen -= trans;
    395  1.39   tsutsui 	*asc->sc_dmaaddr += trans;
    396   1.4      matt 
    397   1.6      matt 	asc->sc_xfers++;
    398   1.1      matt 	return 0;
    399   1.1      matt }
    400   1.1      matt 
    401   1.1      matt static int
    402  1.39   tsutsui asc_vsbus_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
    403   1.1      matt 		    int datain, size_t *dmasize)
    404   1.1      matt {
    405  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    406   1.1      matt 
    407   1.1      matt 	asc->sc_dmaaddr = addr;
    408   1.1      matt 	asc->sc_dmalen = len;
    409   1.1      matt 	if (datain) {
    410   1.6      matt 		asc->sc_flags &= ~ASC_FROMMEMORY;
    411   1.1      matt 	} else {
    412   1.6      matt 		asc->sc_flags |= ASC_FROMMEMORY;
    413   1.1      matt 	}
    414  1.39   tsutsui 	if ((vaddr_t)*asc->sc_dmaaddr < VM_MIN_KERNEL_ADDRESS)
    415  1.39   tsutsui 		panic("%s: DMA address (%p) outside of kernel",
    416  1.39   tsutsui 		    __func__, *asc->sc_dmaaddr);
    417   1.1      matt 
    418  1.38      matt         NCR_DMA(("%s: start %d@%p,%d\n", device_xname(&sc->sc_dev),
    419  1.38      matt             (int)*asc->sc_dmalen, *asc->sc_dmaaddr,
    420  1.38      matt 	    (asc->sc_flags & ASC_FROMMEMORY)));
    421   1.1      matt 	*dmasize = asc->sc_dmasize = min(*dmasize, ASC_MAXXFERSIZE);
    422   1.1      matt 
    423   1.1      matt 	if (asc->sc_dmasize) {
    424   1.1      matt 		if (bus_dmamap_load(asc->sc_dmat, asc->sc_dmamap,
    425   1.1      matt 				*asc->sc_dmaaddr, asc->sc_dmasize,
    426   1.1      matt 				NULL /* kernel address */,
    427   1.7      matt 				BUS_DMA_NOWAIT|VAX_BUS_DMA_SPILLPAGE))
    428  1.38      matt 			panic("%s: cannot load DMA map",
    429  1.39   tsutsui 			    device_xname(sc->sc_dev));
    430   1.1      matt 		bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
    431   1.6      matt 				0, asc->sc_dmasize,
    432   1.6      matt 				asc->sc_flags & ASC_FROMMEMORY
    433   1.6      matt 					? BUS_DMASYNC_PREWRITE
    434   1.6      matt 					: BUS_DMASYNC_PREREAD);
    435  1.11      matt 		bus_space_write_4(asc->sc_bst, asc->sc_adrh, 0,
    436   1.1      matt 				  asc->sc_dmamap->dm_segs[0].ds_addr);
    437  1.11      matt 		bus_space_write_4(asc->sc_bst, asc->sc_dirh, 0,
    438   1.6      matt 				  asc->sc_flags & ASC_FROMMEMORY);
    439  1.38      matt 		NCR_DMA(("%s: dma-load %lu@0x%08lx\n",
    440  1.39   tsutsui 		    device_xname(sc->sc_dev),
    441  1.38      matt 		    asc->sc_dmamap->dm_segs[0].ds_len,
    442  1.38      matt 		    asc->sc_dmamap->dm_segs[0].ds_addr));
    443   1.4      matt 		asc->sc_flags |= ASC_MAPLOADED;
    444   1.1      matt 	}
    445   1.1      matt 
    446   1.1      matt 	return 0;
    447   1.1      matt }
    448   1.1      matt 
    449   1.1      matt static void
    450   1.1      matt asc_vsbus_dma_go(struct ncr53c9x_softc *sc)
    451   1.1      matt {
    452  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    453   1.1      matt 
    454   1.4      matt 	asc->sc_flags |= ASC_DMAACTIVE;
    455   1.1      matt }
    456   1.1      matt 
    457   1.1      matt static void
    458   1.1      matt asc_vsbus_dma_stop(struct ncr53c9x_softc *sc)
    459   1.1      matt {
    460  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    461   1.1      matt 
    462   1.6      matt 	if (asc->sc_flags & ASC_MAPLOADED) {
    463   1.6      matt 		bus_dmamap_sync(asc->sc_dmat, asc->sc_dmamap,
    464   1.6      matt 				0, asc->sc_dmasize,
    465   1.6      matt 				asc->sc_flags & ASC_FROMMEMORY
    466   1.6      matt 					? BUS_DMASYNC_POSTWRITE
    467   1.6      matt 					: BUS_DMASYNC_POSTREAD);
    468   1.4      matt 		bus_dmamap_unload(asc->sc_dmat, asc->sc_dmamap);
    469   1.6      matt 	}
    470   1.4      matt 
    471   1.4      matt 	asc->sc_flags &= ~(ASC_DMAACTIVE|ASC_MAPLOADED);
    472   1.1      matt }
    473   1.1      matt 
    474   1.1      matt static int
    475   1.1      matt asc_vsbus_dma_isactive(struct ncr53c9x_softc *sc)
    476   1.1      matt {
    477  1.38      matt 	struct asc_vsbus_softc * const asc = (struct asc_vsbus_softc *)sc;
    478   1.1      matt 
    479   1.1      matt 	return (asc->sc_flags & ASC_DMAACTIVE) != 0;
    480   1.1      matt }
    481