Home | History | Annotate | Line # | Download | only in dev
bztzsc.c revision 1.15.8.2
      1  1.15.8.2  nathanw /*	$NetBSD: bztzsc.c,v 1.15.8.2 2002/02/28 04:06:33 nathanw Exp $ */
      2  1.15.8.2  nathanw 
      3  1.15.8.2  nathanw /*
      4  1.15.8.2  nathanw  * Copyright (c) 1997 Michael L. Hitch
      5  1.15.8.2  nathanw  * Copyright (c) 1996 Ignatios Souvatzis
      6  1.15.8.2  nathanw  * Copyright (c) 1982, 1990 The Regents of the University of California.
      7  1.15.8.2  nathanw  * All rights reserved.
      8  1.15.8.2  nathanw  *
      9  1.15.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
     10  1.15.8.2  nathanw  * modification, are permitted provided that the following conditions
     11  1.15.8.2  nathanw  * are met:
     12  1.15.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     13  1.15.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     14  1.15.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.15.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     16  1.15.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     17  1.15.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     18  1.15.8.2  nathanw  *    must display the following acknowledgement:
     19  1.15.8.2  nathanw  *	This product contains software written by Ignatios Souvatzis and
     20  1.15.8.2  nathanw  *	Michael L. Hitch for the NetBSD project.
     21  1.15.8.2  nathanw  * 4. Neither the name of the University nor the names of its contributors
     22  1.15.8.2  nathanw  *    may be used to endorse or promote products derived from this software
     23  1.15.8.2  nathanw  *    without specific prior written permission.
     24  1.15.8.2  nathanw  *
     25  1.15.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     26  1.15.8.2  nathanw  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     27  1.15.8.2  nathanw  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     28  1.15.8.2  nathanw  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     29  1.15.8.2  nathanw  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     30  1.15.8.2  nathanw  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     31  1.15.8.2  nathanw  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     32  1.15.8.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     33  1.15.8.2  nathanw  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     34  1.15.8.2  nathanw  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     35  1.15.8.2  nathanw  * SUCH DAMAGE.
     36  1.15.8.2  nathanw  *
     37  1.15.8.2  nathanw  */
     38  1.15.8.2  nathanw 
     39  1.15.8.2  nathanw /*
     40  1.15.8.2  nathanw  * Initial amiga Blizzard 2060 driver by Ingatios Souvatzis.  Conversion to
     41  1.15.8.2  nathanw  * 53c9x MI driver by Michael L. Hitch (mhitch (at) montana.edu).
     42  1.15.8.2  nathanw  */
     43  1.15.8.2  nathanw 
     44  1.15.8.2  nathanw #include <sys/cdefs.h>
     45  1.15.8.2  nathanw __KERNEL_RCSID(0, "$NetBSD: bztzsc.c,v 1.15.8.2 2002/02/28 04:06:33 nathanw Exp $");
     46  1.15.8.2  nathanw 
     47  1.15.8.2  nathanw #include <sys/types.h>
     48  1.15.8.2  nathanw #include <sys/param.h>
     49  1.15.8.2  nathanw #include <sys/systm.h>
     50  1.15.8.2  nathanw #include <sys/kernel.h>
     51  1.15.8.2  nathanw #include <sys/errno.h>
     52  1.15.8.2  nathanw #include <sys/ioctl.h>
     53  1.15.8.2  nathanw #include <sys/device.h>
     54  1.15.8.2  nathanw #include <sys/buf.h>
     55  1.15.8.2  nathanw #include <sys/proc.h>
     56  1.15.8.2  nathanw #include <sys/user.h>
     57  1.15.8.2  nathanw #include <sys/queue.h>
     58  1.15.8.2  nathanw 
     59  1.15.8.2  nathanw #include <dev/scsipi/scsi_all.h>
     60  1.15.8.2  nathanw #include <dev/scsipi/scsipi_all.h>
     61  1.15.8.2  nathanw #include <dev/scsipi/scsiconf.h>
     62  1.15.8.2  nathanw #include <dev/scsipi/scsi_message.h>
     63  1.15.8.2  nathanw 
     64  1.15.8.2  nathanw #include <machine/cpu.h>
     65  1.15.8.2  nathanw #include <machine/param.h>
     66  1.15.8.2  nathanw 
     67  1.15.8.2  nathanw #include <dev/ic/ncr53c9xreg.h>
     68  1.15.8.2  nathanw #include <dev/ic/ncr53c9xvar.h>
     69  1.15.8.2  nathanw 
     70  1.15.8.2  nathanw #include <amiga/amiga/isr.h>
     71  1.15.8.2  nathanw #include <amiga/dev/bztzscvar.h>
     72  1.15.8.2  nathanw #include <amiga/dev/zbusvar.h>
     73  1.15.8.2  nathanw 
     74  1.15.8.2  nathanw void	bztzscattach(struct device *, struct device *, void *);
     75  1.15.8.2  nathanw int	bztzscmatch(struct device *, struct cfdata *, void *);
     76  1.15.8.2  nathanw 
     77  1.15.8.2  nathanw /* Linkup to the rest of the kernel */
     78  1.15.8.2  nathanw struct cfattach bztzsc_ca = {
     79  1.15.8.2  nathanw 	sizeof(struct bztzsc_softc), bztzscmatch, bztzscattach
     80  1.15.8.2  nathanw };
     81  1.15.8.2  nathanw 
     82  1.15.8.2  nathanw /*
     83  1.15.8.2  nathanw  * Functions and the switch for the MI code.
     84  1.15.8.2  nathanw  */
     85  1.15.8.2  nathanw u_char	bztzsc_read_reg(struct ncr53c9x_softc *, int);
     86  1.15.8.2  nathanw void	bztzsc_write_reg(struct ncr53c9x_softc *, int, u_char);
     87  1.15.8.2  nathanw int	bztzsc_dma_isintr(struct ncr53c9x_softc *);
     88  1.15.8.2  nathanw void	bztzsc_dma_reset(struct ncr53c9x_softc *);
     89  1.15.8.2  nathanw int	bztzsc_dma_intr(struct ncr53c9x_softc *);
     90  1.15.8.2  nathanw int	bztzsc_dma_setup(struct ncr53c9x_softc *, caddr_t *,
     91  1.15.8.2  nathanw 	    size_t *, int, size_t *);
     92  1.15.8.2  nathanw void	bztzsc_dma_go(struct ncr53c9x_softc *);
     93  1.15.8.2  nathanw void	bztzsc_dma_stop(struct ncr53c9x_softc *);
     94  1.15.8.2  nathanw int	bztzsc_dma_isactive(struct ncr53c9x_softc *);
     95  1.15.8.2  nathanw 
     96  1.15.8.2  nathanw struct ncr53c9x_glue bztzsc_glue = {
     97  1.15.8.2  nathanw 	bztzsc_read_reg,
     98  1.15.8.2  nathanw 	bztzsc_write_reg,
     99  1.15.8.2  nathanw 	bztzsc_dma_isintr,
    100  1.15.8.2  nathanw 	bztzsc_dma_reset,
    101  1.15.8.2  nathanw 	bztzsc_dma_intr,
    102  1.15.8.2  nathanw 	bztzsc_dma_setup,
    103  1.15.8.2  nathanw 	bztzsc_dma_go,
    104  1.15.8.2  nathanw 	bztzsc_dma_stop,
    105  1.15.8.2  nathanw 	bztzsc_dma_isactive,
    106  1.15.8.2  nathanw 	0,
    107  1.15.8.2  nathanw };
    108  1.15.8.2  nathanw 
    109  1.15.8.2  nathanw /* Maximum DMA transfer length to reduce impact on high-speed serial input */
    110  1.15.8.2  nathanw u_long bztzsc_max_dma = 1024;
    111  1.15.8.2  nathanw extern int ser_open_speed;
    112  1.15.8.2  nathanw 
    113  1.15.8.2  nathanw u_long bztzsc_cnt_pio = 0;	/* number of PIO transfers */
    114  1.15.8.2  nathanw u_long bztzsc_cnt_dma = 0;	/* number of DMA transfers */
    115  1.15.8.2  nathanw u_long bztzsc_cnt_dma2 = 0;	/* number of DMA transfers broken up */
    116  1.15.8.2  nathanw u_long bztzsc_cnt_dma3 = 0;	/* number of pages combined */
    117  1.15.8.2  nathanw 
    118  1.15.8.2  nathanw #ifdef DEBUG
    119  1.15.8.2  nathanw struct {
    120  1.15.8.2  nathanw 	u_char hardbits;
    121  1.15.8.2  nathanw 	u_char status;
    122  1.15.8.2  nathanw 	u_char xx;
    123  1.15.8.2  nathanw 	u_char yy;
    124  1.15.8.2  nathanw } bztzsc_trace[128];
    125  1.15.8.2  nathanw int bztzsc_trace_ptr = 0;
    126  1.15.8.2  nathanw int bztzsc_trace_enable = 1;
    127  1.15.8.2  nathanw void bztzsc_dump(void);
    128  1.15.8.2  nathanw #endif
    129  1.15.8.2  nathanw 
    130  1.15.8.2  nathanw /*
    131  1.15.8.2  nathanw  * if we are a Phase5 Blizzard 2060 SCSI
    132  1.15.8.2  nathanw  */
    133  1.15.8.2  nathanw int
    134  1.15.8.2  nathanw bztzscmatch(struct device *parent, struct cfdata *cf, void *aux)
    135  1.15.8.2  nathanw {
    136  1.15.8.2  nathanw 	struct zbus_args *zap;
    137  1.15.8.2  nathanw 	volatile u_char *regs;
    138  1.15.8.2  nathanw 
    139  1.15.8.2  nathanw 	zap = aux;
    140  1.15.8.2  nathanw 	if (zap->manid != 0x2140 || zap->prodid != 24)
    141  1.15.8.2  nathanw 		return(0);
    142  1.15.8.2  nathanw 	regs = &((volatile u_char *)zap->va)[0x1ff00];
    143  1.15.8.2  nathanw 	if (badaddr((caddr_t)regs))
    144  1.15.8.2  nathanw 		return(0);
    145  1.15.8.2  nathanw 	regs[NCR_CFG1 * 4] = 0;
    146  1.15.8.2  nathanw 	regs[NCR_CFG1 * 4] = NCRCFG1_PARENB | 7;
    147  1.15.8.2  nathanw 	delay(5);
    148  1.15.8.2  nathanw 	if (regs[NCR_CFG1 * 4] != (NCRCFG1_PARENB | 7))
    149  1.15.8.2  nathanw 		return(0);
    150  1.15.8.2  nathanw 	return(1);
    151  1.15.8.2  nathanw }
    152  1.15.8.2  nathanw 
    153  1.15.8.2  nathanw /*
    154  1.15.8.2  nathanw  * Attach this instance, and then all the sub-devices
    155  1.15.8.2  nathanw  */
    156  1.15.8.2  nathanw void
    157  1.15.8.2  nathanw bztzscattach(struct device *parent, struct device *self, void *aux)
    158  1.15.8.2  nathanw {
    159  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (void *)self;
    160  1.15.8.2  nathanw 	struct ncr53c9x_softc *sc = &bsc->sc_ncr53c9x;
    161  1.15.8.2  nathanw 	struct zbus_args  *zap;
    162  1.15.8.2  nathanw 	extern u_long scsi_nosync;
    163  1.15.8.2  nathanw 	extern int shift_nosync;
    164  1.15.8.2  nathanw 	extern int ncr53c9x_debug;
    165  1.15.8.2  nathanw 
    166  1.15.8.2  nathanw 	/*
    167  1.15.8.2  nathanw 	 * Set up the glue for MI code early; we use some of it here.
    168  1.15.8.2  nathanw 	 */
    169  1.15.8.2  nathanw 	sc->sc_glue = &bztzsc_glue;
    170  1.15.8.2  nathanw 
    171  1.15.8.2  nathanw 	/*
    172  1.15.8.2  nathanw 	 * Save the regs
    173  1.15.8.2  nathanw 	 */
    174  1.15.8.2  nathanw 	zap = aux;
    175  1.15.8.2  nathanw 	bsc->sc_reg = &((volatile u_char *)zap->va)[0x1ff00];
    176  1.15.8.2  nathanw 	bsc->sc_dmabase = &bsc->sc_reg[0xf0];
    177  1.15.8.2  nathanw 
    178  1.15.8.2  nathanw 	sc->sc_freq = 40;		/* Clocked at 40Mhz */
    179  1.15.8.2  nathanw 
    180  1.15.8.2  nathanw 	printf(": address %p", bsc->sc_reg);
    181  1.15.8.2  nathanw 
    182  1.15.8.2  nathanw 	sc->sc_id = 7;
    183  1.15.8.2  nathanw 
    184  1.15.8.2  nathanw 	/*
    185  1.15.8.2  nathanw 	 * It is necessary to try to load the 2nd config register here,
    186  1.15.8.2  nathanw 	 * to find out what rev the FAS chip is, else the ncr53c9x_reset
    187  1.15.8.2  nathanw 	 * will not set up the defaults correctly.
    188  1.15.8.2  nathanw 	 */
    189  1.15.8.2  nathanw 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    190  1.15.8.2  nathanw 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
    191  1.15.8.2  nathanw 	sc->sc_cfg3 = 0x08 /*FCLK*/ | NCRESPCFG3_FSCSI | NCRESPCFG3_CDB;
    192  1.15.8.2  nathanw 	sc->sc_rev = NCR_VARIANT_FAS216;
    193  1.15.8.2  nathanw 
    194  1.15.8.2  nathanw 	/*
    195  1.15.8.2  nathanw 	 * This is the value used to start sync negotiations
    196  1.15.8.2  nathanw 	 * Note that the NCR register "SYNCTP" is programmed
    197  1.15.8.2  nathanw 	 * in "clocks per byte", and has a minimum value of 4.
    198  1.15.8.2  nathanw 	 * The SCSI period used in negotiation is one-fourth
    199  1.15.8.2  nathanw 	 * of the time (in nanoseconds) needed to transfer one byte.
    200  1.15.8.2  nathanw 	 * Since the chip's clock is given in MHz, we have the following
    201  1.15.8.2  nathanw 	 * formula: 4 * period = (1000 / freq) * 4
    202  1.15.8.2  nathanw 	 */
    203  1.15.8.2  nathanw 	sc->sc_minsync = 1000 / sc->sc_freq;
    204  1.15.8.2  nathanw 
    205  1.15.8.2  nathanw 	/*
    206  1.15.8.2  nathanw 	 * get flags from -I argument and set cf_flags.
    207  1.15.8.2  nathanw 	 * NOTE: low 8 bits are to disable disconnect, and the next
    208  1.15.8.2  nathanw 	 *       8 bits are to disable sync.
    209  1.15.8.2  nathanw 	 */
    210  1.15.8.2  nathanw 	sc->sc_dev.dv_cfdata->cf_flags |= (scsi_nosync >> shift_nosync)
    211  1.15.8.2  nathanw 	    & 0xffff;
    212  1.15.8.2  nathanw 	shift_nosync += 16;
    213  1.15.8.2  nathanw 
    214  1.15.8.2  nathanw 	/* Use next 16 bits of -I argument to set ncr53c9x_debug flags */
    215  1.15.8.2  nathanw 	ncr53c9x_debug |= (scsi_nosync >> shift_nosync) & 0xffff;
    216  1.15.8.2  nathanw 	shift_nosync += 16;
    217  1.15.8.2  nathanw 
    218  1.15.8.2  nathanw #if 1
    219  1.15.8.2  nathanw 	if (((scsi_nosync >> shift_nosync) & 0xff00) == 0xff00)
    220  1.15.8.2  nathanw 		sc->sc_minsync = 0;
    221  1.15.8.2  nathanw #endif
    222  1.15.8.2  nathanw 
    223  1.15.8.2  nathanw 	/* Really no limit, but since we want to fit into the TCR... */
    224  1.15.8.2  nathanw 	sc->sc_maxxfer = 64 * 1024;
    225  1.15.8.2  nathanw 
    226  1.15.8.2  nathanw 	bsc->sc_reg[0xe0] = BZTZSC_PB_LED;	/* Turn LED off */
    227  1.15.8.2  nathanw 
    228  1.15.8.2  nathanw 	/*
    229  1.15.8.2  nathanw 	 * Configure interrupts.
    230  1.15.8.2  nathanw 	 */
    231  1.15.8.2  nathanw 	bsc->sc_isr.isr_intr = ncr53c9x_intr;
    232  1.15.8.2  nathanw 	bsc->sc_isr.isr_arg  = sc;
    233  1.15.8.2  nathanw 	bsc->sc_isr.isr_ipl  = 2;
    234  1.15.8.2  nathanw 	add_isr(&bsc->sc_isr);
    235  1.15.8.2  nathanw 
    236  1.15.8.2  nathanw 	/*
    237  1.15.8.2  nathanw 	 * Now try to attach all the sub-devices
    238  1.15.8.2  nathanw 	 */
    239  1.15.8.2  nathanw 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    240  1.15.8.2  nathanw 	sc->sc_adapter.adapt_minphys = minphys;
    241  1.15.8.2  nathanw 	ncr53c9x_attach(sc);
    242  1.15.8.2  nathanw }
    243  1.15.8.2  nathanw 
    244  1.15.8.2  nathanw /*
    245  1.15.8.2  nathanw  * Glue functions.
    246  1.15.8.2  nathanw  */
    247  1.15.8.2  nathanw 
    248  1.15.8.2  nathanw u_char
    249  1.15.8.2  nathanw bztzsc_read_reg(struct ncr53c9x_softc *sc, int reg)
    250  1.15.8.2  nathanw {
    251  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    252  1.15.8.2  nathanw 
    253  1.15.8.2  nathanw 	return bsc->sc_reg[reg * 4];
    254  1.15.8.2  nathanw }
    255  1.15.8.2  nathanw 
    256  1.15.8.2  nathanw void
    257  1.15.8.2  nathanw bztzsc_write_reg(struct ncr53c9x_softc *sc, int reg, u_char val)
    258  1.15.8.2  nathanw {
    259  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    260  1.15.8.2  nathanw 	u_char v = val;
    261  1.15.8.2  nathanw 
    262  1.15.8.2  nathanw 	bsc->sc_reg[reg * 4] = v;
    263  1.15.8.2  nathanw #ifdef DEBUG
    264  1.15.8.2  nathanw if (bztzsc_trace_enable/* && sc->sc_nexus && sc->sc_nexus->xs->xs_control & XS_CTL_POLL*/ &&
    265  1.15.8.2  nathanw   reg == NCR_CMD/* && bsc->sc_active*/) {
    266  1.15.8.2  nathanw   bztzsc_trace[(bztzsc_trace_ptr - 1) & 127].yy = v;
    267  1.15.8.2  nathanw /*  printf(" cmd %x", v);*/
    268  1.15.8.2  nathanw }
    269  1.15.8.2  nathanw #endif
    270  1.15.8.2  nathanw }
    271  1.15.8.2  nathanw 
    272  1.15.8.2  nathanw int
    273  1.15.8.2  nathanw bztzsc_dma_isintr(struct ncr53c9x_softc *sc)
    274  1.15.8.2  nathanw {
    275  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    276  1.15.8.2  nathanw 
    277  1.15.8.2  nathanw 	if ((bsc->sc_reg[NCR_STAT * 4] & NCRSTAT_INT) == 0)
    278  1.15.8.2  nathanw 		return 0;
    279  1.15.8.2  nathanw 
    280  1.15.8.2  nathanw 	if (sc->sc_state == NCR_CONNECTED)
    281  1.15.8.2  nathanw 		bsc->sc_reg[0xe0] = 0;			/* Turn LED on */
    282  1.15.8.2  nathanw 	else
    283  1.15.8.2  nathanw 		bsc->sc_reg[0xe0] = BZTZSC_PB_LED;	/* Turn LED off */
    284  1.15.8.2  nathanw 
    285  1.15.8.2  nathanw #ifdef DEBUG
    286  1.15.8.2  nathanw if (/*sc->sc_nexus && sc->sc_nexus->xs->xs_control & XS_CTL_POLL &&*/ bztzsc_trace_enable) {
    287  1.15.8.2  nathanw   bztzsc_trace[bztzsc_trace_ptr].status = bsc->sc_reg[NCR_STAT * 4];
    288  1.15.8.2  nathanw   bztzsc_trace[bztzsc_trace_ptr].xx = bsc->sc_reg[NCR_CMD * 4];
    289  1.15.8.2  nathanw   bztzsc_trace[bztzsc_trace_ptr].yy = bsc->sc_active;
    290  1.15.8.2  nathanw   bztzsc_trace_ptr = (bztzsc_trace_ptr + 1) & 127;
    291  1.15.8.2  nathanw }
    292  1.15.8.2  nathanw #endif
    293  1.15.8.2  nathanw 	return 1;
    294  1.15.8.2  nathanw }
    295  1.15.8.2  nathanw 
    296  1.15.8.2  nathanw void
    297  1.15.8.2  nathanw bztzsc_dma_reset(struct ncr53c9x_softc *sc)
    298  1.15.8.2  nathanw {
    299  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    300  1.15.8.2  nathanw 
    301  1.15.8.2  nathanw 	bsc->sc_active = 0;
    302  1.15.8.2  nathanw }
    303  1.15.8.2  nathanw 
    304  1.15.8.2  nathanw int
    305  1.15.8.2  nathanw bztzsc_dma_intr(struct ncr53c9x_softc *sc)
    306  1.15.8.2  nathanw {
    307  1.15.8.2  nathanw 	register struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    308  1.15.8.2  nathanw 	register int	cnt;
    309  1.15.8.2  nathanw 
    310  1.15.8.2  nathanw 	NCR_DMA(("bztzsc_dma_intr: cnt %d int %x stat %x fifo %d ",
    311  1.15.8.2  nathanw 	    bsc->sc_dmasize, sc->sc_espintr, sc->sc_espstat,
    312  1.15.8.2  nathanw 	    bsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF));
    313  1.15.8.2  nathanw 	if (bsc->sc_active == 0) {
    314  1.15.8.2  nathanw 		printf("bztzsc_intr--inactive DMA\n");
    315  1.15.8.2  nathanw 		return -1;
    316  1.15.8.2  nathanw 	}
    317  1.15.8.2  nathanw 
    318  1.15.8.2  nathanw 	/* update sc_dmaaddr and sc_pdmalen */
    319  1.15.8.2  nathanw 	cnt = bsc->sc_reg[NCR_TCL * 4];
    320  1.15.8.2  nathanw 	cnt += bsc->sc_reg[NCR_TCM * 4] << 8;
    321  1.15.8.2  nathanw 	cnt += bsc->sc_reg[NCR_TCH * 4] << 16;
    322  1.15.8.2  nathanw 	if (!bsc->sc_datain) {
    323  1.15.8.2  nathanw 		cnt += bsc->sc_reg[NCR_FFLAG * 4] & NCRFIFO_FF;
    324  1.15.8.2  nathanw 		bsc->sc_reg[NCR_CMD * 4] = NCRCMD_FLUSH;
    325  1.15.8.2  nathanw 	}
    326  1.15.8.2  nathanw 	cnt = bsc->sc_dmasize - cnt;	/* number of bytes transferred */
    327  1.15.8.2  nathanw 	NCR_DMA(("DMA xferred %d\n", cnt));
    328  1.15.8.2  nathanw 	if (bsc->sc_xfr_align) {
    329  1.15.8.2  nathanw 		bcopy(bsc->sc_alignbuf, *bsc->sc_dmaaddr, cnt);
    330  1.15.8.2  nathanw 		bsc->sc_xfr_align = 0;
    331  1.15.8.2  nathanw 	}
    332  1.15.8.2  nathanw 	*bsc->sc_dmaaddr += cnt;
    333  1.15.8.2  nathanw 	*bsc->sc_pdmalen -= cnt;
    334  1.15.8.2  nathanw 	bsc->sc_active = 0;
    335  1.15.8.2  nathanw 	return 0;
    336  1.15.8.2  nathanw }
    337  1.15.8.2  nathanw 
    338  1.15.8.2  nathanw int
    339  1.15.8.2  nathanw bztzsc_dma_setup(struct ncr53c9x_softc *sc, caddr_t *addr, size_t *len,
    340  1.15.8.2  nathanw                  int datain, size_t *dmasize)
    341  1.15.8.2  nathanw {
    342  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    343  1.15.8.2  nathanw 	paddr_t pa;
    344  1.15.8.2  nathanw 	u_char *ptr;
    345  1.15.8.2  nathanw 	size_t xfer;
    346  1.15.8.2  nathanw 
    347  1.15.8.2  nathanw 	bsc->sc_dmaaddr = addr;
    348  1.15.8.2  nathanw 	bsc->sc_pdmalen = len;
    349  1.15.8.2  nathanw 	bsc->sc_datain = datain;
    350  1.15.8.2  nathanw 	bsc->sc_dmasize = *dmasize;
    351  1.15.8.2  nathanw 	/*
    352  1.15.8.2  nathanw 	 * DMA can be nasty for high-speed serial input, so limit the
    353  1.15.8.2  nathanw 	 * size of this DMA operation if the serial port is running at
    354  1.15.8.2  nathanw 	 * a high speed (higher than 19200 for now - should be adjusted
    355  1.15.8.2  nathanw 	 * based on cpu type and speed?).
    356  1.15.8.2  nathanw 	 * XXX - add serial speed check XXX
    357  1.15.8.2  nathanw 	 */
    358  1.15.8.2  nathanw 	if (ser_open_speed > 19200 && bztzsc_max_dma != 0 &&
    359  1.15.8.2  nathanw 	    bsc->sc_dmasize > bztzsc_max_dma)
    360  1.15.8.2  nathanw 		bsc->sc_dmasize = bztzsc_max_dma;
    361  1.15.8.2  nathanw 	ptr = *addr;			/* Kernel virtual address */
    362  1.15.8.2  nathanw 	pa = kvtop(ptr);		/* Physical address of DMA */
    363  1.15.8.2  nathanw 	xfer = min(bsc->sc_dmasize, NBPG - (pa & (NBPG - 1)));
    364  1.15.8.2  nathanw 	bsc->sc_xfr_align = 0;
    365  1.15.8.2  nathanw 	/*
    366  1.15.8.2  nathanw 	 * If output and unaligned, stuff odd byte into FIFO
    367  1.15.8.2  nathanw 	 */
    368  1.15.8.2  nathanw 	if (datain == 0 && (int)ptr & 1) {
    369  1.15.8.2  nathanw 		NCR_DMA(("bztzsc_dma_setup: align byte written to fifo\n"));
    370  1.15.8.2  nathanw 		pa++;
    371  1.15.8.2  nathanw 		xfer--;			/* XXXX CHECK THIS !!!! XXXX */
    372  1.15.8.2  nathanw 		bsc->sc_reg[NCR_FIFO * 4] = *ptr++;
    373  1.15.8.2  nathanw 	}
    374  1.15.8.2  nathanw 	/*
    375  1.15.8.2  nathanw 	 * If unaligned address, read unaligned bytes into alignment buffer
    376  1.15.8.2  nathanw 	 */
    377  1.15.8.2  nathanw 	else if ((int)ptr & 1) {
    378  1.15.8.2  nathanw 		pa = kvtop((caddr_t)&bsc->sc_alignbuf);
    379  1.15.8.2  nathanw 		xfer = bsc->sc_dmasize = min(xfer, sizeof (bsc->sc_alignbuf));
    380  1.15.8.2  nathanw 		NCR_DMA(("bztzsc_dma_setup: align read by %d bytes\n", xfer));
    381  1.15.8.2  nathanw 		bsc->sc_xfr_align = 1;
    382  1.15.8.2  nathanw 	}
    383  1.15.8.2  nathanw ++bztzsc_cnt_dma;		/* number of DMA operations */
    384  1.15.8.2  nathanw 
    385  1.15.8.2  nathanw 	while (xfer < bsc->sc_dmasize) {
    386  1.15.8.2  nathanw 		if ((pa + xfer) != kvtop(*addr + xfer))
    387  1.15.8.2  nathanw 			break;
    388  1.15.8.2  nathanw 		if ((bsc->sc_dmasize - xfer) < NBPG)
    389  1.15.8.2  nathanw 			xfer = bsc->sc_dmasize;
    390  1.15.8.2  nathanw 		else
    391  1.15.8.2  nathanw 			xfer += NBPG;
    392  1.15.8.2  nathanw ++bztzsc_cnt_dma3;
    393  1.15.8.2  nathanw 	}
    394  1.15.8.2  nathanw if (xfer != *len)
    395  1.15.8.2  nathanw   ++bztzsc_cnt_dma2;
    396  1.15.8.2  nathanw 
    397  1.15.8.2  nathanw 	bsc->sc_dmasize = xfer;
    398  1.15.8.2  nathanw 	*dmasize = bsc->sc_dmasize;
    399  1.15.8.2  nathanw 	bsc->sc_pa = pa;
    400  1.15.8.2  nathanw #if defined(M68040) || defined(M68060)
    401  1.15.8.2  nathanw 	if (mmutype == MMU_68040) {
    402  1.15.8.2  nathanw 		if (bsc->sc_xfr_align) {
    403  1.15.8.2  nathanw 			dma_cachectl(bsc->sc_alignbuf,
    404  1.15.8.2  nathanw 			    sizeof(bsc->sc_alignbuf));
    405  1.15.8.2  nathanw 		}
    406  1.15.8.2  nathanw 		else
    407  1.15.8.2  nathanw 			dma_cachectl(*bsc->sc_dmaaddr, bsc->sc_dmasize);
    408  1.15.8.2  nathanw 	}
    409  1.15.8.2  nathanw #endif
    410  1.15.8.2  nathanw 
    411  1.15.8.2  nathanw 	pa >>= 1;
    412  1.15.8.2  nathanw 	if (!bsc->sc_datain)
    413  1.15.8.2  nathanw 		pa |= 0x80000000;
    414  1.15.8.2  nathanw 	bsc->sc_dmabase[12] = (u_int8_t)(pa);
    415  1.15.8.2  nathanw 	bsc->sc_dmabase[8] = (u_int8_t)(pa >> 8);
    416  1.15.8.2  nathanw 	bsc->sc_dmabase[4] = (u_int8_t)(pa >> 16);
    417  1.15.8.2  nathanw 	bsc->sc_dmabase[0] = (u_int8_t)(pa >> 24);
    418  1.15.8.2  nathanw 	bsc->sc_active = 1;
    419  1.15.8.2  nathanw 	return 0;
    420  1.15.8.2  nathanw }
    421  1.15.8.2  nathanw 
    422  1.15.8.2  nathanw void
    423  1.15.8.2  nathanw bztzsc_dma_go(struct ncr53c9x_softc *sc)
    424  1.15.8.2  nathanw {
    425  1.15.8.2  nathanw }
    426  1.15.8.2  nathanw 
    427  1.15.8.2  nathanw void
    428  1.15.8.2  nathanw bztzsc_dma_stop(struct ncr53c9x_softc *sc)
    429  1.15.8.2  nathanw {
    430  1.15.8.2  nathanw }
    431  1.15.8.2  nathanw 
    432  1.15.8.2  nathanw int
    433  1.15.8.2  nathanw bztzsc_dma_isactive(struct ncr53c9x_softc *sc)
    434  1.15.8.2  nathanw {
    435  1.15.8.2  nathanw 	struct bztzsc_softc *bsc = (struct bztzsc_softc *)sc;
    436  1.15.8.2  nathanw 
    437  1.15.8.2  nathanw 	return bsc->sc_active;
    438  1.15.8.2  nathanw }
    439  1.15.8.2  nathanw 
    440  1.15.8.2  nathanw #ifdef DEBUG
    441  1.15.8.2  nathanw void
    442  1.15.8.2  nathanw bztzsc_dump(void)
    443  1.15.8.2  nathanw {
    444  1.15.8.2  nathanw 	int i;
    445  1.15.8.2  nathanw 
    446  1.15.8.2  nathanw 	i = bztzsc_trace_ptr;
    447  1.15.8.2  nathanw 	printf("bztzsc_trace dump: ptr %x\n", bztzsc_trace_ptr);
    448  1.15.8.2  nathanw 	do {
    449  1.15.8.2  nathanw 		if (bztzsc_trace[i].hardbits == 0) {
    450  1.15.8.2  nathanw 			i = (i + 1) & 127;
    451  1.15.8.2  nathanw 			continue;
    452  1.15.8.2  nathanw 		}
    453  1.15.8.2  nathanw 		printf("%02x%02x%02x%02x(", bztzsc_trace[i].hardbits,
    454  1.15.8.2  nathanw 		    bztzsc_trace[i].status, bztzsc_trace[i].xx, bztzsc_trace[i].yy);
    455  1.15.8.2  nathanw 		if (bztzsc_trace[i].status & NCRSTAT_INT)
    456  1.15.8.2  nathanw 			printf("NCRINT/");
    457  1.15.8.2  nathanw 		if (bztzsc_trace[i].status & NCRSTAT_TC)
    458  1.15.8.2  nathanw 			printf("NCRTC/");
    459  1.15.8.2  nathanw 		switch(bztzsc_trace[i].status & NCRSTAT_PHASE) {
    460  1.15.8.2  nathanw 		case 0:
    461  1.15.8.2  nathanw 			printf("dataout"); break;
    462  1.15.8.2  nathanw 		case 1:
    463  1.15.8.2  nathanw 			printf("datain"); break;
    464  1.15.8.2  nathanw 		case 2:
    465  1.15.8.2  nathanw 			printf("cmdout"); break;
    466  1.15.8.2  nathanw 		case 3:
    467  1.15.8.2  nathanw 			printf("status"); break;
    468  1.15.8.2  nathanw 		case 6:
    469  1.15.8.2  nathanw 			printf("msgout"); break;
    470  1.15.8.2  nathanw 		case 7:
    471  1.15.8.2  nathanw 			printf("msgin"); break;
    472  1.15.8.2  nathanw 		default:
    473  1.15.8.2  nathanw 			printf("phase%d?", bztzsc_trace[i].status & NCRSTAT_PHASE);
    474  1.15.8.2  nathanw 		}
    475  1.15.8.2  nathanw 		printf(") ");
    476  1.15.8.2  nathanw 		i = (i + 1) & 127;
    477  1.15.8.2  nathanw 	} while (i != bztzsc_trace_ptr);
    478  1.15.8.2  nathanw 	printf("\n");
    479  1.15.8.2  nathanw }
    480  1.15.8.2  nathanw #endif
    481