Home | History | Annotate | Line # | Download | only in dev
flsc.c revision 1.44
      1  1.44       phx /*	$NetBSD: flsc.c,v 1.44 2010/10/18 22:02:25 phx Exp $ */
      2   1.5     veego 
      3   1.1    chopps /*
      4  1.16    mhitch  * Copyright (c) 1997 Michael L. Hitch
      5   1.1    chopps  * Copyright (c) 1995 Daniel Widenfalk
      6   1.1    chopps  * Copyright (c) 1994 Christian E. Hopps
      7   1.1    chopps  * Copyright (c) 1982, 1990 The Regents of the University of California.
      8   1.1    chopps  * All rights reserved.
      9   1.1    chopps  *
     10   1.1    chopps  * Redistribution and use in source and binary forms, with or without
     11   1.1    chopps  * modification, are permitted provided that the following conditions
     12   1.1    chopps  * are met:
     13   1.1    chopps  * 1. Redistributions of source code must retain the above copyright
     14   1.1    chopps  *    notice, this list of conditions and the following disclaimer.
     15   1.1    chopps  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1    chopps  *    notice, this list of conditions and the following disclaimer in the
     17   1.1    chopps  *    documentation and/or other materials provided with the distribution.
     18   1.1    chopps  * 3. All advertising materials mentioning features or use of this software
     19   1.1    chopps  *    must display the following acknowledgement:
     20  1.16    mhitch  *	This product includes software developed by Daniel Widenfalk
     21  1.16    mhitch  *	and Michael L. Hitch.
     22   1.1    chopps  * 4. Neither the name of the University nor the names of its contributors
     23   1.1    chopps  *    may be used to endorse or promote products derived from this software
     24   1.1    chopps  *    without specific prior written permission.
     25   1.1    chopps  *
     26   1.1    chopps  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27   1.1    chopps  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28   1.1    chopps  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29   1.1    chopps  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30   1.1    chopps  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31   1.1    chopps  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32   1.1    chopps  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33   1.1    chopps  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34   1.1    chopps  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35   1.1    chopps  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36   1.1    chopps  * SUCH DAMAGE.
     37   1.1    chopps  */
     38   1.1    chopps 
     39  1.16    mhitch /*
     40  1.16    mhitch  * Initial amiga Fastlane driver by Daniel Widenfalk.  Conversion to
     41  1.16    mhitch  * 53c9x MI driver by Michael L. Hitch (mhitch (at) montana.edu).
     42  1.16    mhitch  */
     43  1.21  jonathan 
     44  1.21  jonathan #include "opt_ddb.h"
     45  1.44       phx #ifdef __m68k__
     46  1.43       mrg #include "opt_m68k_arch.h"
     47  1.44       phx #endif
     48  1.29   aymeric 
     49  1.29   aymeric #include <sys/cdefs.h>
     50  1.44       phx __KERNEL_RCSID(0, "$NetBSD: flsc.c,v 1.44 2010/10/18 22:02:25 phx Exp $");
     51  1.16    mhitch 
     52  1.16    mhitch #include <sys/types.h>
     53   1.1    chopps #include <sys/param.h>
     54   1.1    chopps #include <sys/systm.h>
     55   1.1    chopps #include <sys/kernel.h>
     56  1.16    mhitch #include <sys/errno.h>
     57  1.16    mhitch #include <sys/ioctl.h>
     58   1.1    chopps #include <sys/device.h>
     59  1.16    mhitch #include <sys/buf.h>
     60  1.16    mhitch #include <sys/proc.h>
     61  1.16    mhitch #include <sys/queue.h>
     62  1.16    mhitch 
     63  1.32   thorpej #include <uvm/uvm_extern.h>
     64  1.32   thorpej 
     65  1.15    bouyer #include <dev/scsipi/scsi_all.h>
     66  1.15    bouyer #include <dev/scsipi/scsipi_all.h>
     67  1.15    bouyer #include <dev/scsipi/scsiconf.h>
     68  1.16    mhitch #include <dev/scsipi/scsi_message.h>
     69  1.16    mhitch 
     70  1.16    mhitch #include <machine/cpu.h>
     71  1.16    mhitch #include <machine/param.h>
     72  1.16    mhitch 
     73  1.16    mhitch #include <dev/ic/ncr53c9xreg.h>
     74  1.16    mhitch #include <dev/ic/ncr53c9xvar.h>
     75  1.16    mhitch 
     76   1.1    chopps #include <amiga/amiga/isr.h>
     77  1.16    mhitch #include <amiga/dev/flscvar.h>
     78   1.1    chopps #include <amiga/dev/zbusvar.h>
     79   1.1    chopps 
     80  1.41   tsutsui int	flscmatch(device_t, cfdata_t, void *);
     81  1.41   tsutsui void	flscattach(device_t, device_t, void *);
     82  1.16    mhitch 
     83  1.16    mhitch /* Linkup to the rest of the kernel */
     84  1.41   tsutsui CFATTACH_DECL_NEW(flsc, sizeof(struct flsc_softc),
     85  1.31   thorpej     flscmatch, flscattach, NULL, NULL);
     86   1.1    chopps 
     87  1.16    mhitch /*
     88  1.16    mhitch  * Functions and the switch for the MI code.
     89  1.16    mhitch  */
     90  1.41   tsutsui uint8_t	flsc_read_reg(struct ncr53c9x_softc *, int);
     91  1.41   tsutsui void	flsc_write_reg(struct ncr53c9x_softc *, int, uint8_t);
     92  1.28   aymeric int	flsc_dma_isintr(struct ncr53c9x_softc *);
     93  1.28   aymeric void	flsc_dma_reset(struct ncr53c9x_softc *);
     94  1.28   aymeric int	flsc_dma_intr(struct ncr53c9x_softc *);
     95  1.41   tsutsui int	flsc_dma_setup(struct ncr53c9x_softc *, uint8_t **,
     96  1.28   aymeric 	    size_t *, int, size_t *);
     97  1.28   aymeric void	flsc_dma_go(struct ncr53c9x_softc *);
     98  1.28   aymeric void	flsc_dma_stop(struct ncr53c9x_softc *);
     99  1.28   aymeric int	flsc_dma_isactive(struct ncr53c9x_softc *);
    100  1.28   aymeric void	flsc_clear_latched_intr(struct ncr53c9x_softc *);
    101  1.16    mhitch 
    102  1.16    mhitch struct ncr53c9x_glue flsc_glue = {
    103  1.16    mhitch 	flsc_read_reg,
    104  1.16    mhitch 	flsc_write_reg,
    105  1.16    mhitch 	flsc_dma_isintr,
    106  1.16    mhitch 	flsc_dma_reset,
    107  1.16    mhitch 	flsc_dma_intr,
    108  1.16    mhitch 	flsc_dma_setup,
    109  1.16    mhitch 	flsc_dma_go,
    110  1.16    mhitch 	flsc_dma_stop,
    111  1.16    mhitch 	flsc_dma_isactive,
    112  1.16    mhitch 	flsc_clear_latched_intr,
    113   1.3   thorpej };
    114   1.1    chopps 
    115  1.16    mhitch /* Maximum DMA transfer length to reduce impact on high-speed serial input */
    116  1.16    mhitch u_long flsc_max_dma = 1024;
    117  1.16    mhitch extern int ser_open_speed;
    118  1.16    mhitch 
    119  1.16    mhitch extern int ncr53c9x_debug;
    120  1.16    mhitch extern u_long scsi_nosync;
    121  1.16    mhitch extern int shift_nosync;
    122   1.1    chopps 
    123   1.1    chopps /*
    124   1.1    chopps  * if we are an Advanced Systems & Software FastlaneZ3
    125   1.1    chopps  */
    126   1.1    chopps int
    127  1.41   tsutsui flscmatch(device_t parent, cfdata_t cf, void *aux)
    128   1.1    chopps {
    129   1.1    chopps 	struct zbus_args *zap;
    130   1.1    chopps 
    131   1.1    chopps 	if (!is_a4000() && !is_a3000())
    132  1.41   tsutsui 		return 0;
    133   1.1    chopps 
    134  1.16    mhitch 	zap = aux;
    135   1.6        is 	if (zap->manid == 0x2140 && zap->prodid == 11
    136   1.6        is 	    && iszthreepa(zap->pa))
    137  1.41   tsutsui 		return 1;
    138   1.1    chopps 
    139  1.41   tsutsui 	return 0;
    140   1.1    chopps }
    141   1.1    chopps 
    142  1.16    mhitch /*
    143  1.16    mhitch  * Attach this instance, and then all the sub-devices
    144  1.16    mhitch  */
    145   1.1    chopps void
    146  1.41   tsutsui flscattach(device_t parent, device_t self, void *aux)
    147   1.1    chopps {
    148  1.41   tsutsui 	struct flsc_softc *fsc = device_private(self);
    149  1.16    mhitch 	struct ncr53c9x_softc *sc = &fsc->sc_ncr53c9x;
    150   1.1    chopps 	struct zbus_args  *zap;
    151   1.1    chopps 
    152  1.16    mhitch 	/*
    153  1.16    mhitch 	 * Set up the glue for MI code early; we use some of it here.
    154  1.16    mhitch 	 */
    155  1.41   tsutsui 	sc->sc_dev = self;
    156  1.16    mhitch 	sc->sc_glue = &flsc_glue;
    157  1.16    mhitch 
    158  1.16    mhitch 	/*
    159  1.16    mhitch 	 * Save the regs
    160  1.16    mhitch 	 */
    161  1.16    mhitch 	zap = aux;
    162  1.41   tsutsui 	fsc->sc_dmabase = (volatile uint8_t *)zap->va;
    163  1.41   tsutsui 	fsc->sc_reg = &((volatile uint8_t *)zap->va)[0x1000001];
    164  1.16    mhitch 
    165  1.36     lukem 	sc->sc_freq = 40;		/* Clocked at 40 MHz */
    166  1.16    mhitch 
    167  1.41   tsutsui 	aprint_normal(": address %p", fsc->sc_reg);
    168  1.16    mhitch 
    169  1.16    mhitch 	sc->sc_id = 7;
    170  1.16    mhitch 
    171  1.16    mhitch 	/*
    172  1.16    mhitch 	 * It is necessary to try to load the 2nd config register here,
    173  1.16    mhitch 	 * to find out what rev the flsc chip is, else the flsc_reset
    174  1.16    mhitch 	 * will not set up the defaults correctly.
    175  1.16    mhitch 	 */
    176  1.16    mhitch 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    177  1.16    mhitch 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
    178  1.16    mhitch 	sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
    179  1.16    mhitch 	sc->sc_rev = NCR_VARIANT_FAS216;
    180  1.16    mhitch 
    181  1.16    mhitch 	/*
    182  1.16    mhitch 	 * This is the value used to start sync negotiations
    183  1.16    mhitch 	 * Note that the NCR register "SYNCTP" is programmed
    184  1.16    mhitch 	 * in "clocks per byte", and has a minimum value of 4.
    185  1.16    mhitch 	 * The SCSI period used in negotiation is one-fourth
    186  1.16    mhitch 	 * of the time (in nanoseconds) needed to transfer one byte.
    187  1.16    mhitch 	 * Since the chip's clock is given in MHz, we have the following
    188  1.16    mhitch 	 * formula: 4 * period = (1000 / freq) * 4
    189  1.16    mhitch 	 */
    190  1.16    mhitch 	sc->sc_minsync = 1000 / sc->sc_freq;
    191  1.16    mhitch 
    192  1.16    mhitch 	if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
    193  1.16    mhitch 		sc->sc_minsync = 0;
    194  1.16    mhitch 
    195  1.16    mhitch 	/* Really no limit, but since we want to fit into the TCR... */
    196  1.16    mhitch 	sc->sc_maxxfer = 64 * 1024;
    197  1.16    mhitch 
    198  1.16    mhitch 	fsc->sc_portbits = 0xa0 | FLSC_PB_EDI | FLSC_PB_ESI;
    199  1.16    mhitch 	fsc->sc_hardbits = fsc->sc_reg[0x40];
    200  1.16    mhitch 
    201  1.41   tsutsui 	fsc->sc_alignbuf = (uint8_t *)((u_long)fsc->sc_unalignbuf & -4);
    202  1.17    mhitch 
    203  1.41   tsutsui 	device_cfdata(self)->cf_flags |=
    204  1.41   tsutsui 	    (scsi_nosync >> shift_nosync) & 0xffff;
    205  1.16    mhitch 	shift_nosync += 16;
    206  1.16    mhitch 	ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
    207  1.16    mhitch 	shift_nosync += 16;
    208  1.16    mhitch 
    209  1.16    mhitch 	/*
    210  1.16    mhitch 	 * Configure interrupts.
    211  1.16    mhitch 	 */
    212  1.26   tsutsui 	fsc->sc_isr.isr_intr = ncr53c9x_intr;
    213  1.16    mhitch 	fsc->sc_isr.isr_arg  = sc;
    214  1.16    mhitch 	fsc->sc_isr.isr_ipl  = 2;
    215  1.16    mhitch 	add_isr(&fsc->sc_isr);
    216  1.16    mhitch 
    217  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    218  1.16    mhitch 
    219  1.16    mhitch 	/*
    220  1.16    mhitch 	 * Now try to attach all the sub-devices
    221  1.16    mhitch 	 */
    222  1.27    bouyer 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    223  1.27    bouyer 	sc->sc_adapter.adapt_minphys = minphys;
    224  1.27    bouyer 	ncr53c9x_attach(sc);
    225  1.16    mhitch }
    226   1.1    chopps 
    227  1.16    mhitch /*
    228  1.16    mhitch  * Glue functions.
    229  1.16    mhitch  */
    230   1.1    chopps 
    231  1.41   tsutsui uint8_t
    232  1.28   aymeric flsc_read_reg(struct ncr53c9x_softc *sc, int reg)
    233  1.16    mhitch {
    234  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    235   1.1    chopps 
    236  1.16    mhitch 	return fsc->sc_reg[reg * 4];
    237   1.1    chopps }
    238   1.1    chopps 
    239  1.16    mhitch void
    240  1.41   tsutsui flsc_write_reg(struct ncr53c9x_softc *sc, int reg, uint8_t val)
    241   1.1    chopps {
    242  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    243  1.16    mhitch 	struct ncr53c9x_tinfo *ti;
    244  1.41   tsutsui 	uint8_t v = val;
    245  1.16    mhitch 
    246  1.16    mhitch 	if (fsc->sc_piomode && reg == NCR_CMD &&
    247  1.41   tsutsui 	    v == (NCRCMD_TRANS | NCRCMD_DMA)) {
    248  1.16    mhitch 		v = NCRCMD_TRANS;
    249  1.16    mhitch 	}
    250  1.16    mhitch 	/*
    251  1.25   thorpej 	 * Can't do synchronous transfers in XS_CTL_POLL mode:
    252  1.25   thorpej 	 * If starting XS_CTL_POLL command, clear defer sync negotiation
    253  1.25   thorpej 	 * by clearing the T_NEGOTIATE flag.  If starting XS_CTL_POLL and
    254  1.16    mhitch 	 * the device is currently running synchronous, force another
    255  1.16    mhitch 	 * T_NEGOTIATE with 0 offset.
    256  1.16    mhitch 	 */
    257  1.16    mhitch 	if (reg == NCR_SELID) {
    258  1.16    mhitch 		ti = &sc->sc_tinfo[
    259  1.27    bouyer 		    sc->sc_nexus->xs->xs_periph->periph_target];
    260  1.25   thorpej 		if (sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
    261  1.16    mhitch 			if (ti->flags & T_SYNCMODE) {
    262  1.16    mhitch 				ti->flags ^= T_SYNCMODE | T_NEGOTIATE;
    263  1.16    mhitch 			} else if (ti->flags & T_NEGOTIATE) {
    264  1.16    mhitch 				ti->flags ^= T_NEGOTIATE | T_SYNCHOFF;
    265  1.16    mhitch 				/* save T_NEGOTIATE in private flags? */
    266   1.1    chopps 			}
    267  1.16    mhitch 		} else {
    268  1.16    mhitch 			/*
    269  1.16    mhitch 			 * If we haven't attempted sync negotiation yet,
    270  1.16    mhitch 			 * do it now.
    271  1.16    mhitch 			 */
    272  1.16    mhitch 			if ((ti->flags & (T_SYNCMODE | T_SYNCHOFF)) ==
    273  1.16    mhitch 			    T_SYNCHOFF &&
    274  1.16    mhitch 			    sc->sc_minsync != 0)	/* XXX */
    275  1.16    mhitch 				ti->flags ^= T_NEGOTIATE | T_SYNCHOFF;
    276  1.16    mhitch 		}
    277  1.16    mhitch 	}
    278  1.16    mhitch 	if (reg == NCR_CMD && v == NCRCMD_SETATN  &&
    279  1.16    mhitch 	    sc->sc_flags & NCR_SYNCHNEGO &&
    280  1.25   thorpej 	     sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
    281  1.16    mhitch 		ti = &sc->sc_tinfo[
    282  1.27    bouyer 		    sc->sc_nexus->xs->xs_periph->periph_target];
    283  1.16    mhitch 		ti->offset = 0;
    284   1.1    chopps 	}
    285  1.16    mhitch 	fsc->sc_reg[reg * 4] = v;
    286   1.1    chopps }
    287   1.1    chopps 
    288  1.16    mhitch int
    289  1.28   aymeric flsc_dma_isintr(struct ncr53c9x_softc *sc)
    290   1.1    chopps {
    291  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    292  1.41   tsutsui 	unsigned int hardbits;
    293   1.1    chopps 
    294  1.16    mhitch 	hardbits = fsc->sc_reg[0x40];
    295  1.41   tsutsui 	if ((hardbits & FLSC_HB_IACT) != 0)
    296  1.16    mhitch 		return (fsc->sc_csr = 0);
    297  1.16    mhitch 
    298  1.16    mhitch 	if (sc->sc_state == NCR_CONNECTED || sc->sc_state == NCR_SELECTING)
    299  1.16    mhitch 		fsc->sc_portbits |= FLSC_PB_LED;
    300  1.16    mhitch 	else
    301  1.16    mhitch 		fsc->sc_portbits &= ~FLSC_PB_LED;
    302  1.16    mhitch 
    303  1.41   tsutsui 	if ((hardbits & FLSC_HB_CREQ) != 0 && (hardbits & FLSC_HB_MINT) == 0 &&
    304  1.41   tsutsui 	    (fsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) != 0) {
    305  1.16    mhitch 		return 1;
    306  1.16    mhitch 	}
    307  1.16    mhitch 	/* Do I still need this? */
    308  1.41   tsutsui 	if (fsc->sc_piomode && (fsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) != 0 &&
    309  1.41   tsutsui 	    (hardbits & FLSC_HB_MINT) == 0)
    310  1.16    mhitch 		return 1;
    311  1.16    mhitch 
    312  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits & ~FLSC_PB_INT_BITS;
    313  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    314  1.16    mhitch 	return 0;
    315   1.1    chopps }
    316   1.1    chopps 
    317   1.1    chopps void
    318  1.28   aymeric flsc_clear_latched_intr(struct ncr53c9x_softc *sc)
    319   1.1    chopps {
    320  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    321  1.16    mhitch 
    322  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits & ~FLSC_PB_INT_BITS;
    323  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    324   1.1    chopps }
    325   1.1    chopps 
    326   1.1    chopps void
    327  1.28   aymeric flsc_dma_reset(struct ncr53c9x_softc *sc)
    328   1.1    chopps {
    329  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    330  1.34       jmc 	struct ncr53c9x_tinfo *ti;
    331   1.1    chopps 
    332  1.34       jmc 	if (sc->sc_nexus)
    333  1.34       jmc 		ti = &sc->sc_tinfo[sc->sc_nexus->xs->xs_periph->periph_target];
    334  1.34       jmc 	else
    335  1.34       jmc 		ti = &sc->sc_tinfo[1];	/* XXX */
    336  1.34       jmc 	if (fsc->sc_active) {
    337  1.34       jmc 		printf("dmaaddr %p dmasize %d stat %x flags %x off %d ",
    338  1.34       jmc 		    *fsc->sc_dmaaddr, fsc->sc_dmasize,
    339  1.34       jmc 		    fsc->sc_reg[NCR_STAT * 4], ti->flags, ti->offset);
    340  1.34       jmc 		printf("per %d ff %x intr %x\n",
    341  1.34       jmc 		    ti->period, fsc->sc_reg[NCR_FFLAG * 4],
    342  1.34       jmc 		    fsc->sc_reg[NCR_INTR * 4]);
    343  1.16    mhitch #ifdef DDB
    344  1.34       jmc 		Debugger();
    345  1.16    mhitch #endif
    346  1.34       jmc 	}
    347  1.16    mhitch 	fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
    348  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    349  1.16    mhitch 	fsc->sc_reg[0x80] = 0;
    350  1.41   tsutsui 	*((volatile uint32_t *)fsc->sc_dmabase) = 0;
    351  1.16    mhitch 	fsc->sc_active = 0;
    352  1.16    mhitch 	fsc->sc_piomode = 0;
    353   1.1    chopps }
    354   1.1    chopps 
    355   1.1    chopps int
    356  1.28   aymeric flsc_dma_intr(struct ncr53c9x_softc *sc)
    357  1.16    mhitch {
    358  1.16    mhitch 	register struct flsc_softc *fsc = (struct flsc_softc *)sc;
    359  1.41   tsutsui 	uint8_t *p;
    360  1.41   tsutsui 	volatile uint8_t *cmdreg, *intrreg, *statreg, *fiforeg;
    361  1.41   tsutsui 	u_int flscphase, flscstat, flscintr;
    362  1.41   tsutsui 	int cnt;
    363  1.16    mhitch 
    364  1.16    mhitch 	NCR_DMA(("flsc_dma_intr: pio %d cnt %d int %x stat %x fifo %d ",
    365  1.16    mhitch 	    fsc->sc_piomode, fsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
    366  1.16    mhitch 	    fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
    367  1.41   tsutsui 	if ((fsc->sc_reg[0x40] & FLSC_HB_CREQ) == 0)
    368  1.16    mhitch 		printf("flsc_dma_intr: csr %x stat %x intr %x\n", fsc->sc_csr,
    369  1.16    mhitch 		    sc->sc_espstat, sc->sc_espintr);
    370  1.16    mhitch 	if (fsc->sc_active == 0) {
    371  1.16    mhitch 		printf("flsc_intr--inactive DMA\n");
    372  1.16    mhitch 		return -1;
    373  1.16    mhitch 	}
    374  1.16    mhitch 
    375  1.16    mhitch /* if DMA transfer, update sc_dmaaddr and sc_pdmalen, else PIO xfer */
    376  1.16    mhitch 	if (fsc->sc_piomode == 0) {
    377  1.16    mhitch 		fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
    378  1.16    mhitch 		fsc->sc_reg[0x40] = fsc->sc_portbits;
    379  1.16    mhitch 		fsc->sc_reg[0x80] = 0;
    380  1.41   tsutsui 		*((volatile uint32_t *)fsc->sc_dmabase) = 0;
    381  1.16    mhitch 		cnt = fsc->sc_reg[NCR_TCL * 4];
    382  1.16    mhitch 		cnt += fsc->sc_reg[NCR_TCM * 4] << 8;
    383  1.16    mhitch 		cnt += fsc->sc_reg[NCR_TCH * 4] << 16;
    384  1.16    mhitch 		if (!fsc->sc_datain) {
    385  1.16    mhitch 			cnt += fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
    386  1.16    mhitch 			fsc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
    387  1.16    mhitch 		}
    388  1.16    mhitch 		cnt = fsc->sc_dmasize - cnt;	/* number of bytes transferred */
    389  1.16    mhitch 		NCR_DMA(("DMA xferred %d\n", cnt));
    390  1.16    mhitch 		if (fsc->sc_xfr_align) {
    391  1.16    mhitch 			int i;
    392  1.16    mhitch 			for (i = 0; i < cnt; ++i)
    393  1.16    mhitch 				(*fsc->sc_dmaaddr)[i] = fsc->sc_alignbuf[i];
    394  1.16    mhitch 			fsc->sc_xfr_align = 0;
    395  1.16    mhitch 		}
    396  1.16    mhitch 		*fsc->sc_dmaaddr += cnt;
    397  1.16    mhitch 		*fsc->sc_pdmalen -= cnt;
    398  1.16    mhitch 		fsc->sc_active = 0;
    399  1.16    mhitch 		return 0;
    400  1.16    mhitch 	}
    401   1.1    chopps 
    402  1.16    mhitch 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    403  1.16    mhitch 		fsc->sc_active = 0;
    404  1.16    mhitch 		fsc->sc_piomode = 0;
    405  1.16    mhitch 		NCR_DMA(("no NCRINTR_BS\n"));
    406  1.16    mhitch 		return 0;
    407  1.16    mhitch 	}
    408   1.1    chopps 
    409  1.16    mhitch 	cnt = fsc->sc_dmasize;
    410  1.16    mhitch #if 0
    411  1.16    mhitch 	if (cnt == 0) {
    412  1.16    mhitch 		printf("data interrupt, but no count left.");
    413  1.16    mhitch 	}
    414  1.16    mhitch #endif
    415   1.1    chopps 
    416  1.16    mhitch 	p = *fsc->sc_dmaaddr;
    417  1.16    mhitch 	flscphase = sc->sc_phase;
    418  1.41   tsutsui 	flscstat = (u_int)sc->sc_espstat;
    419  1.41   tsutsui 	flscintr = (u_int)sc->sc_espintr;
    420  1.16    mhitch 	cmdreg = fsc->sc_reg + NCR_CMD * 4;
    421  1.16    mhitch 	fiforeg = fsc->sc_reg + NCR_FIFO * 4;
    422  1.16    mhitch 	statreg = fsc->sc_reg + NCR_STAT * 4;
    423  1.16    mhitch 	intrreg = fsc->sc_reg + NCR_INTR * 4;
    424  1.16    mhitch 	NCR_DMA(("PIO %d datain %d phase %d stat %x intr %x\n",
    425  1.16    mhitch 	    cnt, fsc->sc_datain, flscphase, flscstat, flscintr));
    426  1.16    mhitch 	do {
    427  1.16    mhitch 		if (fsc->sc_datain) {
    428  1.16    mhitch 			*p++ = *fiforeg;
    429  1.16    mhitch 			cnt--;
    430  1.16    mhitch 			if (flscphase == DATA_IN_PHASE) {
    431  1.16    mhitch 				*cmdreg = NCRCMD_TRANS;
    432  1.16    mhitch 			} else {
    433  1.16    mhitch 				fsc->sc_active = 0;
    434  1.16    mhitch 			}
    435  1.16    mhitch 	 	} else {
    436  1.16    mhitch NCR_DMA(("flsc_dma_intr: PIO out- phase %d cnt %d active %d\n", flscphase, cnt,
    437  1.16    mhitch     fsc->sc_active));
    438  1.16    mhitch 			if (   (flscphase == DATA_OUT_PHASE)
    439  1.16    mhitch 			    || (flscphase == MESSAGE_OUT_PHASE)) {
    440  1.16    mhitch 				int n;
    441  1.16    mhitch 				n = 16 - (fsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF);
    442  1.16    mhitch 				if (n > cnt)
    443  1.16    mhitch 					n = cnt;
    444  1.16    mhitch 				cnt -= n;
    445  1.16    mhitch 				while (n-- > 0)
    446  1.16    mhitch 					*fiforeg = *p++;
    447  1.16    mhitch 				*cmdreg = NCRCMD_TRANS;
    448  1.16    mhitch 			} else {
    449  1.16    mhitch 				fsc->sc_active = 0;
    450  1.16    mhitch 			}
    451  1.16    mhitch 		}
    452   1.1    chopps 
    453  1.16    mhitch 		if (fsc->sc_active && cnt) {
    454  1.41   tsutsui 			while ((*statreg & 0x80) == 0)
    455  1.41   tsutsui 				;
    456  1.16    mhitch 			flscstat = *statreg;
    457  1.16    mhitch 			flscintr = *intrreg;
    458  1.16    mhitch 			flscphase = (flscintr & NCRINTR_DIS)
    459  1.16    mhitch 				    ? /* Disconnected */ BUSFREE_PHASE
    460  1.16    mhitch 				    : flscstat & PHASE_MASK;
    461  1.16    mhitch 		}
    462  1.41   tsutsui 	} while (cnt && fsc->sc_active && (flscintr & NCRINTR_BS) != 0);
    463  1.16    mhitch #if 1
    464  1.16    mhitch if (fsc->sc_dmasize < 8 && cnt)
    465  1.16    mhitch   printf("flsc_dma_intr: short transfer: dmasize %d cnt %d\n",
    466  1.16    mhitch     fsc->sc_dmasize, cnt);
    467  1.16    mhitch #endif
    468  1.16    mhitch 	NCR_DMA(("flsc_dma_intr: PIO transfer [%d], %d->%d phase %d stat %x intr %x\n",
    469  1.16    mhitch 	    *fsc->sc_pdmalen, fsc->sc_dmasize, cnt, flscphase, flscstat, flscintr));
    470  1.16    mhitch 	sc->sc_phase = flscphase;
    471  1.41   tsutsui 	sc->sc_espstat = (uint8_t)flscstat;
    472  1.41   tsutsui 	sc->sc_espintr = (uint8_t)flscintr;
    473  1.16    mhitch 	*fsc->sc_dmaaddr = p;
    474  1.16    mhitch 	*fsc->sc_pdmalen -= fsc->sc_dmasize - cnt;
    475  1.16    mhitch 	fsc->sc_dmasize = cnt;
    476  1.16    mhitch 
    477  1.16    mhitch 	if (*fsc->sc_pdmalen == 0) {
    478  1.16    mhitch 		sc->sc_espstat |= NCRSTAT_TC;
    479  1.16    mhitch 		fsc->sc_piomode = 0;
    480   1.1    chopps 	}
    481  1.16    mhitch 	return 0;
    482   1.1    chopps }
    483   1.1    chopps 
    484   1.1    chopps int
    485  1.41   tsutsui flsc_dma_setup(struct ncr53c9x_softc *sc, uint8_t **addr, size_t *len,
    486  1.28   aymeric                int datain, size_t *dmasize)
    487  1.16    mhitch {
    488  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    489  1.24        is 	paddr_t pa;
    490  1.41   tsutsui 	uint8_t *ptr;
    491  1.16    mhitch 	size_t xfer;
    492  1.16    mhitch 
    493  1.41   tsutsui 	fsc->sc_dmaaddr = addr;
    494  1.16    mhitch 	fsc->sc_pdmalen = len;
    495  1.16    mhitch 	fsc->sc_datain = datain;
    496  1.16    mhitch 	fsc->sc_dmasize = *dmasize;
    497  1.25   thorpej 	if (sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
    498  1.16    mhitch 		/* polling mode, use PIO */
    499  1.16    mhitch 		*dmasize = fsc->sc_dmasize;
    500  1.16    mhitch 		NCR_DMA(("pfsc_dma_setup: PIO %p/%d [%d]\n", *addr,
    501  1.16    mhitch 		    fsc->sc_dmasize, *len));
    502  1.16    mhitch 		fsc->sc_piomode = 1;
    503  1.16    mhitch 		if (datain == 0) {
    504  1.16    mhitch 			int n;
    505  1.16    mhitch 			n = fsc->sc_dmasize;
    506  1.16    mhitch 			if (n > 16)
    507  1.16    mhitch 				n = 16;
    508  1.16    mhitch 			while (n-- > 0) {
    509  1.16    mhitch 				fsc->sc_reg[NCR_FIFO * 4] = **fsc->sc_dmaaddr;
    510  1.16    mhitch 				(*fsc->sc_pdmalen)--;
    511  1.16    mhitch 				(*fsc->sc_dmaaddr)++;
    512  1.16    mhitch 				--fsc->sc_dmasize;
    513  1.16    mhitch 			}
    514  1.16    mhitch 		}
    515  1.16    mhitch 		return 0;
    516  1.16    mhitch 	}
    517  1.16    mhitch 	/*
    518  1.16    mhitch 	 * DMA can be nasty for high-speed serial input, so limit the
    519  1.16    mhitch 	 * size of this DMA operation if the serial port is running at
    520  1.16    mhitch 	 * a high speed (higher than 19200 for now - should be adjusted
    521  1.33       wiz 	 * based on CPU type and speed?).
    522  1.16    mhitch 	 * XXX - add serial speed check XXX
    523  1.16    mhitch 	 */
    524  1.16    mhitch 	if (ser_open_speed > 19200 && flsc_max_dma != 0 &&
    525  1.16    mhitch 	    fsc->sc_dmasize > flsc_max_dma)
    526  1.16    mhitch 		fsc->sc_dmasize = flsc_max_dma;
    527  1.16    mhitch 	ptr = *addr;			/* Kernel virtual address */
    528  1.16    mhitch 	pa = kvtop(ptr);		/* Physical address of DMA */
    529  1.32   thorpej 	xfer = min(fsc->sc_dmasize, PAGE_SIZE - (pa & (PAGE_SIZE - 1)));
    530  1.16    mhitch 	fsc->sc_xfr_align = 0;
    531  1.16    mhitch 	fsc->sc_piomode = 0;
    532  1.16    mhitch 	fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
    533  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    534  1.16    mhitch 	fsc->sc_reg[0x80] = 0;
    535  1.41   tsutsui 	*((volatile uint32_t *)fsc->sc_dmabase) = 0;
    536  1.16    mhitch 
    537  1.16    mhitch 	/*
    538  1.16    mhitch 	 * If output and length < 16, copy to fifo
    539  1.16    mhitch 	 */
    540  1.16    mhitch 	if (datain == 0 && fsc->sc_dmasize < 16) {
    541  1.16    mhitch 		int n;
    542  1.16    mhitch 		for (n = 0; n < fsc->sc_dmasize; ++n)
    543  1.16    mhitch 			fsc->sc_reg[NCR_FIFO * 4] = *ptr++;
    544  1.16    mhitch 		NCR_DMA(("flsc_dma_setup: %d bytes written to fifo\n", n));
    545  1.16    mhitch 		fsc->sc_piomode = 1;
    546  1.16    mhitch 		fsc->sc_active = 1;
    547  1.16    mhitch 		*fsc->sc_pdmalen -= fsc->sc_dmasize;
    548  1.16    mhitch 		*fsc->sc_dmaaddr += fsc->sc_dmasize;
    549  1.16    mhitch 		*dmasize = fsc->sc_dmasize;
    550  1.16    mhitch 		fsc->sc_dmasize = 0;
    551  1.16    mhitch 		return 0;		/* All done */
    552  1.16    mhitch 	}
    553  1.16    mhitch 	/*
    554  1.16    mhitch 	 * If output and unaligned, copy unaligned data to fifo
    555  1.16    mhitch 	 */
    556  1.16    mhitch 	else if (datain == 0 && (int)ptr & 3) {
    557  1.16    mhitch 		int n = 4 - ((int)ptr & 3);
    558  1.16    mhitch 		NCR_DMA(("flsc_dma_setup: align %d bytes written to fifo\n", n));
    559  1.16    mhitch 		pa += n;
    560  1.16    mhitch 		xfer -= n;
    561  1.16    mhitch 		while (n--)
    562  1.16    mhitch 			fsc->sc_reg[NCR_FIFO * 4] = *ptr++;
    563  1.16    mhitch 	}
    564  1.16    mhitch 	/*
    565  1.16    mhitch 	 * If unaligned address, read unaligned bytes into alignment buffer
    566  1.16    mhitch 	 */
    567  1.16    mhitch 	else if ((int)ptr & 3 || xfer & 3) {
    568  1.38  christos 		pa = kvtop((void *)fsc->sc_alignbuf);
    569  1.41   tsutsui 		xfer = fsc->sc_dmasize = min(xfer, sizeof(fsc->sc_unalignbuf));
    570  1.16    mhitch 		NCR_DMA(("flsc_dma_setup: align read by %d bytes\n", xfer));
    571  1.16    mhitch 		fsc->sc_xfr_align = 1;
    572  1.16    mhitch 	}
    573  1.16    mhitch 	/*
    574  1.16    mhitch 	 * If length smaller than longword, read into alignment buffer
    575  1.16    mhitch 	 * XXX doesn't work for 1 or 2 bytes !!!!
    576  1.16    mhitch 	 */
    577  1.16    mhitch 	else if (fsc->sc_dmasize < 4) {
    578  1.16    mhitch 		NCR_DMA(("flsc_dma_setup: read remaining %d bytes\n",
    579  1.16    mhitch 		    fsc->sc_dmasize));
    580  1.38  christos 		pa = kvtop((void *)fsc->sc_alignbuf);
    581  1.16    mhitch 		fsc->sc_xfr_align = 1;
    582  1.16    mhitch 	}
    583  1.16    mhitch 	/*
    584  1.16    mhitch 	 * Finally, limit transfer length to multiple of 4 bytes.
    585  1.16    mhitch 	 */
    586  1.16    mhitch 	else {
    587  1.16    mhitch 		fsc->sc_dmasize &= -4;
    588  1.16    mhitch 		xfer &= -4;
    589  1.16    mhitch 	}
    590  1.16    mhitch 
    591  1.16    mhitch 	while (xfer < fsc->sc_dmasize) {
    592  1.41   tsutsui 		if ((pa + xfer) != kvtop(*addr + xfer))
    593  1.16    mhitch 			break;
    594  1.32   thorpej 		if ((fsc->sc_dmasize - xfer) < PAGE_SIZE)
    595  1.16    mhitch 			xfer = fsc->sc_dmasize;
    596   1.8        is 		else
    597  1.32   thorpej 			xfer += PAGE_SIZE;
    598  1.16    mhitch 	}
    599   1.1    chopps 
    600  1.16    mhitch 	fsc->sc_dmasize = xfer;
    601  1.16    mhitch 	*dmasize = fsc->sc_dmasize;
    602  1.16    mhitch 	fsc->sc_pa = pa;
    603  1.16    mhitch #if defined(M68040) || defined(M68060)
    604  1.16    mhitch 	if (mmutype == MMU_68040) {
    605  1.16    mhitch 		if (fsc->sc_xfr_align) {
    606  1.16    mhitch 			int n;
    607  1.41   tsutsui 			for (n = 0; n < sizeof(fsc->sc_unalignbuf); ++n)
    608  1.16    mhitch 				fsc->sc_alignbuf[n] = n | 0x80;
    609  1.16    mhitch 			dma_cachectl(fsc->sc_alignbuf,
    610  1.17    mhitch 			    sizeof(fsc->sc_unalignbuf));
    611  1.16    mhitch 		}
    612  1.16    mhitch 		else
    613  1.16    mhitch 			dma_cachectl(*fsc->sc_dmaaddr, fsc->sc_dmasize);
    614  1.16    mhitch 	}
    615  1.16    mhitch #endif
    616  1.16    mhitch 	fsc->sc_reg[0x80] = 0;
    617  1.41   tsutsui 	*((volatile uint32_t *)(fsc->sc_dmabase + (pa & 0x00fffffc))) = pa;
    618  1.16    mhitch 	fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
    619  1.16    mhitch 	fsc->sc_portbits |= FLSC_PB_ENABLE_DMA |
    620  1.16    mhitch 	    (fsc->sc_datain ? FLSC_PB_DMA_READ : FLSC_PB_DMA_WRITE);
    621  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    622  1.16    mhitch 	NCR_DMA(("flsc_dma_setup: DMA %p->%lx/%d [%d]\n",
    623  1.16    mhitch 	    ptr, pa, fsc->sc_dmasize, *len));
    624  1.16    mhitch 	fsc->sc_active = 1;
    625  1.16    mhitch 	return 0;
    626   1.1    chopps }
    627   1.1    chopps 
    628  1.16    mhitch void
    629  1.28   aymeric flsc_dma_go(struct ncr53c9x_softc *sc)
    630  1.16    mhitch {
    631  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    632   1.1    chopps 
    633  1.16    mhitch 	NCR_DMA(("flsc_dma_go: datain %d size %d\n", fsc->sc_datain,
    634  1.16    mhitch 	    fsc->sc_dmasize));
    635  1.25   thorpej 	if (sc->sc_nexus->xs->xs_control & XS_CTL_POLL) {
    636  1.16    mhitch 		fsc->sc_active = 1;
    637  1.16    mhitch 		return;
    638  1.16    mhitch 	} else if (fsc->sc_piomode == 0) {
    639  1.16    mhitch 		fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
    640  1.16    mhitch 		fsc->sc_portbits |= FLSC_PB_ENABLE_DMA |
    641  1.16    mhitch 		    (fsc->sc_datain ? FLSC_PB_DMA_READ : FLSC_PB_DMA_WRITE);
    642  1.16    mhitch 		fsc->sc_reg[0x40] = fsc->sc_portbits;
    643  1.16    mhitch 	}
    644  1.16    mhitch }
    645   1.1    chopps 
    646  1.16    mhitch void
    647  1.28   aymeric flsc_dma_stop(struct ncr53c9x_softc *sc)
    648  1.16    mhitch {
    649  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    650   1.1    chopps 
    651  1.16    mhitch 	fsc->sc_portbits &= ~FLSC_PB_DMA_BITS;
    652  1.16    mhitch 	fsc->sc_reg[0x40] = fsc->sc_portbits;
    653   1.1    chopps 
    654  1.16    mhitch 	fsc->sc_reg[0x80] = 0;
    655  1.41   tsutsui 	*((volatile uint32_t *)fsc->sc_dmabase) = 0;
    656  1.16    mhitch 	fsc->sc_piomode = 0;
    657  1.16    mhitch }
    658   1.1    chopps 
    659  1.16    mhitch int
    660  1.28   aymeric flsc_dma_isactive(struct ncr53c9x_softc *sc)
    661  1.16    mhitch {
    662  1.16    mhitch 	struct flsc_softc *fsc = (struct flsc_softc *)sc;
    663   1.1    chopps 
    664  1.16    mhitch 	return fsc->sc_active;
    665   1.1    chopps }
    666