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