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