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