Home | History | Annotate | Line # | Download | only in obio
asc.c revision 1.9.8.3
      1  1.9.8.3  nathanw /*	$NetBSD: asc.c,v 1.9.8.3 2002/10/18 02:38:54 nathanw Exp $	*/
      2  1.9.8.2  nathanw /*-
      3  1.9.8.2  nathanw  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      4  1.9.8.2  nathanw  * All rights reserved.
      5  1.9.8.2  nathanw  *
      6  1.9.8.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      7  1.9.8.2  nathanw  * by Wayne Knowles
      8  1.9.8.2  nathanw  *
      9  1.9.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
     10  1.9.8.2  nathanw  * modification, are permitted provided that the following conditions
     11  1.9.8.2  nathanw  * are met:
     12  1.9.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     13  1.9.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     14  1.9.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     15  1.9.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     16  1.9.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     17  1.9.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     18  1.9.8.2  nathanw  *    must display the following acknowledgement:
     19  1.9.8.2  nathanw  *        This product includes software developed by the NetBSD
     20  1.9.8.2  nathanw  *        Foundation, Inc. and its contributors.
     21  1.9.8.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     22  1.9.8.2  nathanw  *    contributors may be used to endorse or promote products derived
     23  1.9.8.2  nathanw  *    from this software without specific prior written permission.
     24  1.9.8.2  nathanw  *
     25  1.9.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     26  1.9.8.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     27  1.9.8.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     28  1.9.8.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     29  1.9.8.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     30  1.9.8.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     31  1.9.8.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     32  1.9.8.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     33  1.9.8.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     34  1.9.8.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     35  1.9.8.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     36  1.9.8.2  nathanw  */
     37  1.9.8.2  nathanw 
     38  1.9.8.2  nathanw #include <sys/types.h>
     39  1.9.8.2  nathanw #include <sys/param.h>
     40  1.9.8.2  nathanw #include <sys/systm.h>
     41  1.9.8.2  nathanw #include <sys/kernel.h>
     42  1.9.8.2  nathanw #include <sys/errno.h>
     43  1.9.8.2  nathanw #include <sys/device.h>
     44  1.9.8.2  nathanw #include <sys/buf.h>
     45  1.9.8.2  nathanw #include <sys/malloc.h>
     46  1.9.8.2  nathanw 
     47  1.9.8.2  nathanw #include <dev/scsipi/scsi_all.h>
     48  1.9.8.2  nathanw #include <dev/scsipi/scsipi_all.h>
     49  1.9.8.2  nathanw #include <dev/scsipi/scsiconf.h>
     50  1.9.8.2  nathanw #include <dev/scsipi/scsi_message.h>
     51  1.9.8.2  nathanw 
     52  1.9.8.2  nathanw #include <machine/cpu.h>
     53  1.9.8.2  nathanw #include <machine/autoconf.h>
     54  1.9.8.2  nathanw #include <machine/mainboard.h>
     55  1.9.8.2  nathanw #include <machine/bus.h>
     56  1.9.8.2  nathanw 
     57  1.9.8.2  nathanw #include <mipsco/obio/rambo.h>
     58  1.9.8.2  nathanw 
     59  1.9.8.2  nathanw #include <dev/ic/ncr53c9xreg.h>
     60  1.9.8.2  nathanw #include <dev/ic/ncr53c9xvar.h>
     61  1.9.8.2  nathanw 
     62  1.9.8.2  nathanw struct asc_softc {
     63  1.9.8.2  nathanw 	struct ncr53c9x_softc	sc_ncr53c9x;	/* glue to MI code */
     64  1.9.8.2  nathanw         struct evcnt		sc_intrcnt; 	/* Interrupt counter */
     65  1.9.8.2  nathanw 	bus_space_tag_t		sc_bst;
     66  1.9.8.2  nathanw 	bus_space_handle_t	sc_bsh;		/* NCR 53c94 registers */
     67  1.9.8.2  nathanw 	bus_space_handle_t	dm_bsh;		/* RAMBO registers */
     68  1.9.8.2  nathanw 	bus_dma_tag_t		sc_dmat;
     69  1.9.8.2  nathanw         bus_dmamap_t		sc_dmamap;
     70  1.9.8.2  nathanw         caddr_t			*sc_dmaaddr;
     71  1.9.8.2  nathanw 	size_t			*sc_dmalen;
     72  1.9.8.2  nathanw 	size_t			sc_dmasize;
     73  1.9.8.2  nathanw 	int			sc_flags;
     74  1.9.8.2  nathanw #define DMA_IDLE	0x0
     75  1.9.8.2  nathanw #define	DMA_PULLUP	0x1
     76  1.9.8.2  nathanw #define	DMA_ACTIVE	0x2
     77  1.9.8.2  nathanw #define	DMA_MAPLOADED	0x4
     78  1.9.8.2  nathanw         u_int32_t		dm_mode;
     79  1.9.8.2  nathanw         int			dm_curseg;
     80  1.9.8.2  nathanw };
     81  1.9.8.2  nathanw 
     82  1.9.8.2  nathanw static int	ascmatch  (struct device *, struct cfdata *, void *);
     83  1.9.8.2  nathanw static void	ascattach (struct device *, struct device *, void *);
     84  1.9.8.2  nathanw 
     85  1.9.8.3  nathanw CFATTACH_DECL(asc, sizeof(struct asc_softc),
     86  1.9.8.3  nathanw     ascmatch, ascattach, NULL, NULL);
     87  1.9.8.2  nathanw 
     88  1.9.8.2  nathanw /*
     89  1.9.8.2  nathanw  * Functions and the switch for the MI code.
     90  1.9.8.2  nathanw  */
     91  1.9.8.2  nathanw static u_char	asc_read_reg (struct ncr53c9x_softc *, int);
     92  1.9.8.2  nathanw static void	asc_write_reg (struct ncr53c9x_softc *, int, u_char);
     93  1.9.8.2  nathanw static int	asc_dma_isintr (struct ncr53c9x_softc *);
     94  1.9.8.2  nathanw static void	asc_dma_reset (struct ncr53c9x_softc *);
     95  1.9.8.2  nathanw static int	asc_dma_intr (struct ncr53c9x_softc *);
     96  1.9.8.2  nathanw static int	asc_dma_setup (struct ncr53c9x_softc *, caddr_t *,
     97  1.9.8.2  nathanw 				    size_t *, int, size_t *);
     98  1.9.8.2  nathanw static void	asc_dma_go (struct ncr53c9x_softc *);
     99  1.9.8.2  nathanw static void	asc_dma_stop (struct ncr53c9x_softc *);
    100  1.9.8.2  nathanw static int	asc_dma_isactive (struct ncr53c9x_softc *);
    101  1.9.8.2  nathanw 
    102  1.9.8.2  nathanw static struct ncr53c9x_glue asc_glue = {
    103  1.9.8.2  nathanw 	asc_read_reg,
    104  1.9.8.2  nathanw 	asc_write_reg,
    105  1.9.8.2  nathanw 	asc_dma_isintr,
    106  1.9.8.2  nathanw 	asc_dma_reset,
    107  1.9.8.2  nathanw 	asc_dma_intr,
    108  1.9.8.2  nathanw 	asc_dma_setup,
    109  1.9.8.2  nathanw 	asc_dma_go,
    110  1.9.8.2  nathanw 	asc_dma_stop,
    111  1.9.8.2  nathanw 	asc_dma_isactive,
    112  1.9.8.2  nathanw 	NULL,			/* gl_clear_latched_intr */
    113  1.9.8.2  nathanw };
    114  1.9.8.2  nathanw 
    115  1.9.8.2  nathanw static int	asc_intr (void *);
    116  1.9.8.2  nathanw 
    117  1.9.8.2  nathanw #define MAX_SCSI_XFER   (64*1024)
    118  1.9.8.2  nathanw #define	MAX_DMA_SZ	MAX_SCSI_XFER
    119  1.9.8.2  nathanw #define	DMA_SEGS	(MAX_DMA_SZ/NBPG)
    120  1.9.8.2  nathanw 
    121  1.9.8.2  nathanw static int
    122  1.9.8.2  nathanw ascmatch(struct device *parent, struct cfdata *cf, void *aux)
    123  1.9.8.2  nathanw {
    124  1.9.8.2  nathanw 	return 1;
    125  1.9.8.2  nathanw }
    126  1.9.8.2  nathanw 
    127  1.9.8.2  nathanw static void
    128  1.9.8.2  nathanw ascattach(struct device *parent, struct device *self, void *aux)
    129  1.9.8.2  nathanw {
    130  1.9.8.2  nathanw 	struct confargs *ca = aux;
    131  1.9.8.2  nathanw 	struct asc_softc *esc = (void *)self;
    132  1.9.8.2  nathanw 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    133  1.9.8.2  nathanw 
    134  1.9.8.2  nathanw 	/*
    135  1.9.8.2  nathanw 	 * Set up glue for MI code early; we use some of it here.
    136  1.9.8.2  nathanw 	 */
    137  1.9.8.2  nathanw 	sc->sc_glue = &asc_glue;
    138  1.9.8.2  nathanw 
    139  1.9.8.2  nathanw 	esc->sc_bst = ca->ca_bustag;
    140  1.9.8.2  nathanw 	esc->sc_dmat = ca->ca_dmatag;
    141  1.9.8.2  nathanw 
    142  1.9.8.2  nathanw 	if (bus_space_map(ca->ca_bustag, ca->ca_addr,
    143  1.9.8.2  nathanw 			  16*4,	/* sizeof (ncr53c9xreg) */
    144  1.9.8.2  nathanw 			  BUS_SPACE_MAP_LINEAR,
    145  1.9.8.2  nathanw 			  &esc->sc_bsh) != 0) {
    146  1.9.8.2  nathanw 		printf(": cannot map registers\n");
    147  1.9.8.2  nathanw 		return;
    148  1.9.8.2  nathanw 	}
    149  1.9.8.2  nathanw 
    150  1.9.8.2  nathanw 	if (bus_space_map(ca->ca_bustag, RAMBO_BASE, sizeof(struct rambo_ch),
    151  1.9.8.2  nathanw 			  BUS_SPACE_MAP_LINEAR,
    152  1.9.8.2  nathanw 			  &esc->dm_bsh) != 0) {
    153  1.9.8.2  nathanw 		printf(": cannot map dma registers\n");
    154  1.9.8.2  nathanw 		return;
    155  1.9.8.2  nathanw 	}
    156  1.9.8.2  nathanw 
    157  1.9.8.2  nathanw         if (bus_dmamap_create(esc->sc_dmat, MAX_DMA_SZ,
    158  1.9.8.2  nathanw 			      DMA_SEGS, MAX_DMA_SZ, RB_BOUNDRY,
    159  1.9.8.2  nathanw 			      BUS_DMA_WAITOK,
    160  1.9.8.2  nathanw 			      &esc->sc_dmamap) != 0) {
    161  1.9.8.2  nathanw 		printf(": failed to create dmamap\n");
    162  1.9.8.2  nathanw 		return;
    163  1.9.8.2  nathanw         }
    164  1.9.8.2  nathanw 
    165  1.9.8.2  nathanw 	evcnt_attach_dynamic(&esc->sc_intrcnt, EVCNT_TYPE_INTR, NULL,
    166  1.9.8.2  nathanw 			     self->dv_xname, "intr");
    167  1.9.8.2  nathanw 
    168  1.9.8.2  nathanw 	esc->sc_flags = DMA_IDLE;
    169  1.9.8.2  nathanw 	asc_dma_reset(sc);
    170  1.9.8.2  nathanw 
    171  1.9.8.2  nathanw 	/* Other settings */
    172  1.9.8.2  nathanw 	sc->sc_id = 7;
    173  1.9.8.2  nathanw 	sc->sc_freq = 24;	/* 24 MHz clock */
    174  1.9.8.2  nathanw 
    175  1.9.8.2  nathanw 	/*
    176  1.9.8.2  nathanw 	 * Setup for genuine NCR 53C94 SCSI Controller
    177  1.9.8.2  nathanw 	 */
    178  1.9.8.2  nathanw 
    179  1.9.8.2  nathanw 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    180  1.9.8.2  nathanw 	sc->sc_cfg2 = NCRCFG2_SCSI2 | NCRCFG2_FE;
    181  1.9.8.2  nathanw 	sc->sc_cfg3 = NCRCFG3_CDB | NCRCFG3_QTE | NCRCFG3_FSCSI;
    182  1.9.8.2  nathanw 	sc->sc_rev = NCR_VARIANT_NCR53C94;
    183  1.9.8.2  nathanw 
    184  1.9.8.2  nathanw 	sc->sc_minsync = (1000 / sc->sc_freq) * 5 / 4;
    185  1.9.8.2  nathanw 	sc->sc_maxxfer = MAX_SCSI_XFER;
    186  1.9.8.2  nathanw 
    187  1.9.8.2  nathanw #ifdef OLDNCR
    188  1.9.8.2  nathanw 	if (!NCR_READ_REG(sc, NCR_CFG3)) {
    189  1.9.8.2  nathanw 		printf(" [old revision]");
    190  1.9.8.2  nathanw 		sc->sc_cfg2 = 0;
    191  1.9.8.2  nathanw 		sc->sc_cfg3 = 0;
    192  1.9.8.2  nathanw 		sc->sc_minsync = 0;
    193  1.9.8.2  nathanw 	}
    194  1.9.8.2  nathanw #endif
    195  1.9.8.2  nathanw 
    196  1.9.8.2  nathanw 	sc->sc_adapter.adapt_minphys = minphys;
    197  1.9.8.2  nathanw 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    198  1.9.8.2  nathanw 	ncr53c9x_attach(sc);
    199  1.9.8.2  nathanw 
    200  1.9.8.2  nathanw 	bus_intr_establish(esc->sc_bst, SYS_INTR_SCSI, 0, 0, asc_intr, esc);
    201  1.9.8.2  nathanw }
    202  1.9.8.2  nathanw 
    203  1.9.8.2  nathanw /*
    204  1.9.8.2  nathanw  * Glue functions.
    205  1.9.8.2  nathanw  */
    206  1.9.8.2  nathanw 
    207  1.9.8.2  nathanw static u_char
    208  1.9.8.2  nathanw asc_read_reg(struct ncr53c9x_softc *sc, int reg)
    209  1.9.8.2  nathanw {
    210  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    211  1.9.8.2  nathanw 
    212  1.9.8.2  nathanw 	return bus_space_read_1(esc->sc_bst, esc->sc_bsh, reg * 4 + 3);
    213  1.9.8.2  nathanw }
    214  1.9.8.2  nathanw 
    215  1.9.8.2  nathanw static void
    216  1.9.8.2  nathanw asc_write_reg(struct ncr53c9x_softc *sc, int reg, u_char val)
    217  1.9.8.2  nathanw {
    218  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    219  1.9.8.2  nathanw 
    220  1.9.8.2  nathanw 	bus_space_write_1(esc->sc_bst, esc->sc_bsh, reg * 4 + 3, val);
    221  1.9.8.2  nathanw }
    222  1.9.8.2  nathanw 
    223  1.9.8.2  nathanw static void
    224  1.9.8.2  nathanw dma_status(struct ncr53c9x_softc *sc)
    225  1.9.8.2  nathanw {
    226  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    227  1.9.8.2  nathanw 	int    count;
    228  1.9.8.2  nathanw 	int    stat;
    229  1.9.8.2  nathanw 	void   *addr;
    230  1.9.8.2  nathanw 	u_int32_t  tc;
    231  1.9.8.2  nathanw 
    232  1.9.8.2  nathanw 	tc = (asc_read_reg(sc, NCR_TCM)<<8) + asc_read_reg(sc, NCR_TCL);
    233  1.9.8.2  nathanw 	count = bus_space_read_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT);
    234  1.9.8.2  nathanw 	stat  = bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE);
    235  1.9.8.2  nathanw 	addr  = (void *)
    236  1.9.8.2  nathanw 	        bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_CADDR);
    237  1.9.8.2  nathanw 
    238  1.9.8.2  nathanw 	printf("rambo status: cnt=%x addr=%p stat=%08x tc=%04x "
    239  1.9.8.2  nathanw 		 "ncr_stat=0x%02x ncr_fifo=0x%02x\n",
    240  1.9.8.2  nathanw 		 count, addr, stat, tc,
    241  1.9.8.2  nathanw 		 asc_read_reg(sc, NCR_STAT),
    242  1.9.8.2  nathanw 		 asc_read_reg(sc, NCR_FFLAG));
    243  1.9.8.2  nathanw }
    244  1.9.8.2  nathanw 
    245  1.9.8.2  nathanw static __inline void
    246  1.9.8.2  nathanw check_fifo(struct asc_softc *esc)
    247  1.9.8.2  nathanw {
    248  1.9.8.2  nathanw 	register int i=100;
    249  1.9.8.2  nathanw 
    250  1.9.8.2  nathanw 	while (i && !(bus_space_read_4(esc->sc_bst, esc->dm_bsh,
    251  1.9.8.2  nathanw 				       RAMBO_MODE) & RB_FIFO_EMPTY)) {
    252  1.9.8.2  nathanw 		 DELAY(1); i--;
    253  1.9.8.2  nathanw 	}
    254  1.9.8.2  nathanw 
    255  1.9.8.2  nathanw 	if (!i) {
    256  1.9.8.2  nathanw 		dma_status((void *)esc);
    257  1.9.8.2  nathanw 		panic("fifo didn't flush");
    258  1.9.8.2  nathanw 	}
    259  1.9.8.2  nathanw }
    260  1.9.8.2  nathanw 
    261  1.9.8.2  nathanw static int
    262  1.9.8.2  nathanw asc_dma_isintr(struct ncr53c9x_softc *sc)
    263  1.9.8.2  nathanw {
    264  1.9.8.2  nathanw 	return NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT;
    265  1.9.8.2  nathanw }
    266  1.9.8.2  nathanw 
    267  1.9.8.2  nathanw static void
    268  1.9.8.2  nathanw asc_dma_reset(struct ncr53c9x_softc *sc)
    269  1.9.8.2  nathanw {
    270  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    271  1.9.8.2  nathanw 
    272  1.9.8.2  nathanw  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, 0);
    273  1.9.8.2  nathanw 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE,
    274  1.9.8.2  nathanw 			  RB_CLRFIFO|RB_CLRERROR);
    275  1.9.8.2  nathanw 	DELAY(10);
    276  1.9.8.2  nathanw  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    277  1.9.8.2  nathanw 
    278  1.9.8.2  nathanw 	if (esc->sc_flags & DMA_MAPLOADED)
    279  1.9.8.2  nathanw 		bus_dmamap_unload(esc->sc_dmat, esc->sc_dmamap);
    280  1.9.8.2  nathanw 
    281  1.9.8.2  nathanw 	esc->sc_flags = DMA_IDLE;
    282  1.9.8.2  nathanw }
    283  1.9.8.2  nathanw 
    284  1.9.8.2  nathanw /*
    285  1.9.8.2  nathanw  * Setup a DMA transfer
    286  1.9.8.2  nathanw  */
    287  1.9.8.2  nathanw 
    288  1.9.8.2  nathanw static int
    289  1.9.8.2  nathanw asc_dma_setup(struct ncr53c9x_softc *sc, caddr_t *addr, size_t *len,
    290  1.9.8.2  nathanw 	      int datain, size_t *dmasize)
    291  1.9.8.2  nathanw {
    292  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    293  1.9.8.2  nathanw 	paddr_t paddr;
    294  1.9.8.2  nathanw         size_t count, blocks;
    295  1.9.8.2  nathanw 	int prime, err;
    296  1.9.8.2  nathanw 
    297  1.9.8.2  nathanw #ifdef DIAGNOSTIC
    298  1.9.8.2  nathanw 	if (esc->sc_flags & DMA_ACTIVE) {
    299  1.9.8.2  nathanw 		dma_status(sc);
    300  1.9.8.2  nathanw 		panic("DMA active");
    301  1.9.8.2  nathanw 	}
    302  1.9.8.2  nathanw #endif
    303  1.9.8.2  nathanw 
    304  1.9.8.2  nathanw 	esc->sc_dmaaddr = addr;
    305  1.9.8.2  nathanw 	esc->sc_dmalen  = len;
    306  1.9.8.2  nathanw 	esc->sc_dmasize = *dmasize;
    307  1.9.8.2  nathanw 	esc->sc_flags   = datain ? DMA_PULLUP : 0;
    308  1.9.8.2  nathanw 
    309  1.9.8.2  nathanw 	NCR_DMA(("asc_dma_setup va=%p len=%d datain=%d count=%d\n",
    310  1.9.8.2  nathanw 		 *addr, *len, datain, esc->sc_dmasize));
    311  1.9.8.2  nathanw 
    312  1.9.8.2  nathanw 	if (esc->sc_dmasize == 0)
    313  1.9.8.2  nathanw 		return 0;
    314  1.9.8.2  nathanw 
    315  1.9.8.2  nathanw 	/* have dmamap for the transfering addresses */
    316  1.9.8.2  nathanw 	if ((err=bus_dmamap_load(esc->sc_dmat, esc->sc_dmamap,
    317  1.9.8.2  nathanw 				*esc->sc_dmaaddr, esc->sc_dmasize,
    318  1.9.8.2  nathanw 				NULL /* kernel address */,
    319  1.9.8.2  nathanw 				BUS_DMA_NOWAIT)) != 0)
    320  1.9.8.2  nathanw 		panic("%s: bus_dmamap_load err=%d", sc->sc_dev.dv_xname, err);
    321  1.9.8.2  nathanw 
    322  1.9.8.2  nathanw 	esc->sc_flags |= DMA_MAPLOADED;
    323  1.9.8.2  nathanw 
    324  1.9.8.2  nathanw 	paddr  = esc->sc_dmamap->dm_segs[0].ds_addr;
    325  1.9.8.2  nathanw 	count  = esc->sc_dmamap->dm_segs[0].ds_len;
    326  1.9.8.2  nathanw 	prime  = (u_int32_t)paddr & 0x3f;
    327  1.9.8.2  nathanw 	blocks = (prime + count + 63) >> 6;
    328  1.9.8.2  nathanw 
    329  1.9.8.2  nathanw 	esc->dm_mode = (datain ? RB_DMA_WR : RB_DMA_RD);
    330  1.9.8.2  nathanw 
    331  1.9.8.2  nathanw 	/* Set transfer direction and disable DMA */
    332  1.9.8.2  nathanw  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, esc->dm_mode);
    333  1.9.8.2  nathanw 
    334  1.9.8.2  nathanw 	/* Load DMA transfer address */
    335  1.9.8.2  nathanw  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_LADDR,
    336  1.9.8.2  nathanw 			  paddr & ~0x3f);
    337  1.9.8.2  nathanw 
    338  1.9.8.2  nathanw 	/* Load number of blocks to DMA (1 block = 64 bytes) */
    339  1.9.8.2  nathanw  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, blocks);
    340  1.9.8.2  nathanw 
    341  1.9.8.2  nathanw 	/* If non block-aligned transfer prime FIFO manually */
    342  1.9.8.2  nathanw 	if (prime) {
    343  1.9.8.2  nathanw 		/* Enable DMA to prime the FIFO buffer */
    344  1.9.8.2  nathanw 		bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    345  1.9.8.2  nathanw 				  RAMBO_MODE, esc->dm_mode | RB_DMA_ENABLE);
    346  1.9.8.2  nathanw 
    347  1.9.8.2  nathanw 		if (esc->sc_flags & DMA_PULLUP) {
    348  1.9.8.2  nathanw 			/* Read from NCR 53c94 controller*/
    349  1.9.8.2  nathanw 			u_int16_t *p;
    350  1.9.8.2  nathanw 
    351  1.9.8.2  nathanw 			p = (u_int16_t *)((u_int32_t)*esc->sc_dmaaddr & ~0x3f);
    352  1.9.8.2  nathanw 			bus_space_write_multi_2(esc->sc_bst, esc->dm_bsh,
    353  1.9.8.2  nathanw 						RAMBO_FIFO, p, prime>>1);
    354  1.9.8.2  nathanw 		} else
    355  1.9.8.2  nathanw 			/* Write to NCR 53C94 controller */
    356  1.9.8.2  nathanw 			while (prime > 0) {
    357  1.9.8.2  nathanw 				(void)bus_space_read_2(esc->sc_bst,
    358  1.9.8.2  nathanw 						       esc->dm_bsh,
    359  1.9.8.2  nathanw 						       RAMBO_FIFO);
    360  1.9.8.2  nathanw 				prime -= 2;
    361  1.9.8.2  nathanw 			}
    362  1.9.8.2  nathanw 		/* Leave DMA disabled while we setup NCR controller */
    363  1.9.8.2  nathanw 		bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE,
    364  1.9.8.2  nathanw 				  esc->dm_mode);
    365  1.9.8.2  nathanw 	}
    366  1.9.8.2  nathanw 
    367  1.9.8.2  nathanw 	bus_dmamap_sync(esc->sc_dmat, esc->sc_dmamap, 0, esc->sc_dmasize,
    368  1.9.8.2  nathanw 			datain ? BUS_DMASYNC_PREREAD : BUS_DMASYNC_PREWRITE);
    369  1.9.8.2  nathanw 
    370  1.9.8.2  nathanw 	esc->dm_curseg = 0;
    371  1.9.8.2  nathanw 	esc->dm_mode |= RB_DMA_ENABLE;
    372  1.9.8.2  nathanw 	if (esc->sc_dmamap->dm_nsegs > 1)
    373  1.9.8.2  nathanw 		esc->dm_mode |= RB_INT_ENABLE;	/* Requires DMA chaining */
    374  1.9.8.2  nathanw 
    375  1.9.8.2  nathanw 	return 0;
    376  1.9.8.2  nathanw }
    377  1.9.8.2  nathanw 
    378  1.9.8.2  nathanw static void
    379  1.9.8.2  nathanw asc_dma_go(struct ncr53c9x_softc *sc)
    380  1.9.8.2  nathanw {
    381  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    382  1.9.8.2  nathanw 
    383  1.9.8.2  nathanw 	/* Start DMA */
    384  1.9.8.2  nathanw 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, esc->dm_mode);
    385  1.9.8.2  nathanw 
    386  1.9.8.2  nathanw 	esc->sc_flags |= DMA_ACTIVE;
    387  1.9.8.2  nathanw }
    388  1.9.8.2  nathanw 
    389  1.9.8.2  nathanw static int
    390  1.9.8.2  nathanw asc_dma_intr(struct ncr53c9x_softc *sc)
    391  1.9.8.2  nathanw {
    392  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    393  1.9.8.2  nathanw 
    394  1.9.8.2  nathanw 	size_t      resid, len;
    395  1.9.8.2  nathanw 	int         trans;
    396  1.9.8.2  nathanw 	u_int32_t   status;
    397  1.9.8.2  nathanw 	u_int tcl, tcm;
    398  1.9.8.2  nathanw 
    399  1.9.8.2  nathanw #ifdef DIAGNOSTIC
    400  1.9.8.2  nathanw 	if (!(esc->sc_flags & DMA_ACTIVE)) {
    401  1.9.8.2  nathanw 		dma_status(sc);
    402  1.9.8.2  nathanw 		panic("DMA not active");
    403  1.9.8.2  nathanw 	}
    404  1.9.8.2  nathanw #endif
    405  1.9.8.2  nathanw 
    406  1.9.8.2  nathanw 	resid = 0;
    407  1.9.8.2  nathanw 	if (!(esc->sc_flags & DMA_PULLUP) &&
    408  1.9.8.2  nathanw 	    (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    409  1.9.8.2  nathanw 		NCR_DMA(("asc_intr: empty FIFO of %d ", resid));
    410  1.9.8.2  nathanw 		DELAY(10);
    411  1.9.8.2  nathanw 	}
    412  1.9.8.2  nathanw 
    413  1.9.8.2  nathanw 	resid += (tcl = NCR_READ_REG(sc, NCR_TCL)) +
    414  1.9.8.2  nathanw 		((tcm = NCR_READ_REG(sc, NCR_TCM)) << 8);
    415  1.9.8.2  nathanw 
    416  1.9.8.2  nathanw 	if (esc->sc_dmasize == 0) { /* Transfer pad operation */
    417  1.9.8.2  nathanw 		NCR_DMA(("asc_intr: discard %d bytes\n", resid));
    418  1.9.8.2  nathanw 		return 0;
    419  1.9.8.2  nathanw 	}
    420  1.9.8.2  nathanw 
    421  1.9.8.2  nathanw 	trans = esc->sc_dmasize - resid;
    422  1.9.8.2  nathanw 	if (trans < 0) {			/* transferred < 0 ? */
    423  1.9.8.2  nathanw 		printf("asc_intr: xfer (%d) > req (%d)\n",
    424  1.9.8.2  nathanw 		       trans, esc->sc_dmasize);
    425  1.9.8.2  nathanw 		trans = esc->sc_dmasize;
    426  1.9.8.2  nathanw 	}
    427  1.9.8.2  nathanw 
    428  1.9.8.2  nathanw 	NCR_DMA(("asc_intr: tcl=%d, tcm=%d; trans=%d, resid=%d\n",
    429  1.9.8.2  nathanw 		 tcl, tcm, trans, resid));
    430  1.9.8.2  nathanw 
    431  1.9.8.2  nathanw 	status = bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE);
    432  1.9.8.2  nathanw 
    433  1.9.8.2  nathanw 	if (!(status & RB_FIFO_EMPTY)) { /* Data left in RAMBO FIFO */
    434  1.9.8.2  nathanw 		if (esc->sc_flags & DMA_PULLUP) { /* SCSI Read */
    435  1.9.8.2  nathanw 			paddr_t ptr;
    436  1.9.8.2  nathanw 			u_int16_t *p;
    437  1.9.8.2  nathanw 
    438  1.9.8.2  nathanw 			resid  = status & 0x1f;
    439  1.9.8.2  nathanw 
    440  1.9.8.2  nathanw 			/* take the address of block to fixed up */
    441  1.9.8.2  nathanw 			ptr = bus_space_read_4(esc->sc_bst, esc->dm_bsh,
    442  1.9.8.2  nathanw 					       RAMBO_CADDR);
    443  1.9.8.2  nathanw 			/* find the starting address of fractional data */
    444  1.9.8.2  nathanw 			p = (u_int16_t *)MIPS_PHYS_TO_KSEG0(ptr+(resid<<1));
    445  1.9.8.2  nathanw 
    446  1.9.8.2  nathanw 			/* duplicate trailing data to FIFO for force flush */
    447  1.9.8.2  nathanw 			len = RB_BLK_CNT - resid;
    448  1.9.8.2  nathanw 			bus_space_write_multi_2(esc->sc_bst, esc->dm_bsh,
    449  1.9.8.2  nathanw 						RAMBO_FIFO, p, len);
    450  1.9.8.2  nathanw 			check_fifo(esc);
    451  1.9.8.2  nathanw 		} else {		/* SCSI Write */
    452  1.9.8.2  nathanw 			bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    453  1.9.8.2  nathanw 					  RAMBO_MODE, 0);
    454  1.9.8.2  nathanw 			bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    455  1.9.8.2  nathanw 					  RAMBO_MODE, RB_CLRFIFO);
    456  1.9.8.2  nathanw 		}
    457  1.9.8.2  nathanw 	}
    458  1.9.8.2  nathanw 
    459  1.9.8.2  nathanw  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, 0);
    460  1.9.8.2  nathanw 
    461  1.9.8.2  nathanw 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    462  1.9.8.2  nathanw 
    463  1.9.8.2  nathanw 	bus_dmamap_sync(esc->sc_dmat, esc->sc_dmamap,
    464  1.9.8.2  nathanw 			0, esc->sc_dmasize,
    465  1.9.8.2  nathanw 			(esc->sc_flags & DMA_PULLUP)
    466  1.9.8.2  nathanw 			  ? BUS_DMASYNC_POSTREAD
    467  1.9.8.2  nathanw 			  : BUS_DMASYNC_POSTWRITE);
    468  1.9.8.2  nathanw 	bus_dmamap_unload(esc->sc_dmat, esc->sc_dmamap);
    469  1.9.8.2  nathanw 
    470  1.9.8.2  nathanw 	*esc->sc_dmaaddr += trans;
    471  1.9.8.2  nathanw 	*esc->sc_dmalen  -= trans;
    472  1.9.8.2  nathanw 
    473  1.9.8.2  nathanw 	esc->sc_flags = DMA_IDLE;
    474  1.9.8.2  nathanw 
    475  1.9.8.2  nathanw 	return 0;
    476  1.9.8.2  nathanw }
    477  1.9.8.2  nathanw 
    478  1.9.8.2  nathanw 
    479  1.9.8.2  nathanw static void
    480  1.9.8.2  nathanw asc_dma_stop(struct ncr53c9x_softc *sc)
    481  1.9.8.2  nathanw {
    482  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    483  1.9.8.2  nathanw 
    484  1.9.8.2  nathanw 	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE, 0);
    485  1.9.8.2  nathanw 	if (esc->sc_flags & DMA_MAPLOADED)
    486  1.9.8.2  nathanw 		bus_dmamap_unload(esc->sc_dmat, esc->sc_dmamap);
    487  1.9.8.2  nathanw 	esc->sc_flags = DMA_IDLE;
    488  1.9.8.2  nathanw }
    489  1.9.8.2  nathanw 
    490  1.9.8.2  nathanw static int
    491  1.9.8.2  nathanw asc_dma_isactive(struct ncr53c9x_softc *sc)
    492  1.9.8.2  nathanw {
    493  1.9.8.2  nathanw 	struct asc_softc *esc = (struct asc_softc *)sc;
    494  1.9.8.2  nathanw 	return (esc->sc_flags & DMA_ACTIVE)? 1 : 0;
    495  1.9.8.2  nathanw }
    496  1.9.8.2  nathanw 
    497  1.9.8.2  nathanw static void
    498  1.9.8.2  nathanw rambo_dma_chain(struct asc_softc *esc)
    499  1.9.8.2  nathanw {
    500  1.9.8.2  nathanw 	int seg;
    501  1.9.8.2  nathanw 	size_t	count, blocks;
    502  1.9.8.2  nathanw 	paddr_t paddr;
    503  1.9.8.2  nathanw 
    504  1.9.8.2  nathanw 	seg = ++esc->dm_curseg;
    505  1.9.8.2  nathanw 
    506  1.9.8.2  nathanw #ifdef DIAGNOSTIC
    507  1.9.8.2  nathanw 	if (!(esc->sc_flags & DMA_ACTIVE) || seg > esc->sc_dmamap->dm_nsegs)
    508  1.9.8.2  nathanw 		panic("Unexpected DMA chaining intr");
    509  1.9.8.2  nathanw 
    510  1.9.8.2  nathanw 	/* Interrupt can only occur at terminal count, but double check */
    511  1.9.8.2  nathanw 	if (bus_space_read_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT)) {
    512  1.9.8.2  nathanw 		dma_status((void *)esc);
    513  1.9.8.2  nathanw 		panic("rambo blkcnt != 0");
    514  1.9.8.2  nathanw 	}
    515  1.9.8.2  nathanw #endif
    516  1.9.8.2  nathanw 
    517  1.9.8.2  nathanw 	paddr  = esc->sc_dmamap->dm_segs[seg].ds_addr;
    518  1.9.8.2  nathanw 	count  = esc->sc_dmamap->dm_segs[seg].ds_len;
    519  1.9.8.2  nathanw 	blocks = (count + 63) >> 6;
    520  1.9.8.2  nathanw 
    521  1.9.8.2  nathanw 	/* Disable DMA interrupt if last segment */
    522  1.9.8.2  nathanw 	if (seg+1 > esc->sc_dmamap->dm_nsegs) {
    523  1.9.8.2  nathanw 		bus_space_write_4(esc->sc_bst, esc->dm_bsh,
    524  1.9.8.2  nathanw 				  RAMBO_MODE, esc->dm_mode & ~RB_INT_ENABLE);
    525  1.9.8.2  nathanw 	}
    526  1.9.8.2  nathanw 
    527  1.9.8.2  nathanw 	/* Load transfer address for next DMA chain */
    528  1.9.8.2  nathanw  	bus_space_write_4(esc->sc_bst, esc->dm_bsh, RAMBO_LADDR, paddr);
    529  1.9.8.2  nathanw 
    530  1.9.8.2  nathanw 	/* DMA restarts when we enter a new block count */
    531  1.9.8.2  nathanw  	bus_space_write_2(esc->sc_bst, esc->dm_bsh, RAMBO_BLKCNT, blocks);
    532  1.9.8.2  nathanw }
    533  1.9.8.2  nathanw 
    534  1.9.8.2  nathanw static int
    535  1.9.8.2  nathanw asc_intr(void *arg)
    536  1.9.8.2  nathanw {
    537  1.9.8.2  nathanw 	register u_int32_t dma_stat;
    538  1.9.8.2  nathanw 	struct asc_softc *esc = arg;
    539  1.9.8.2  nathanw 	struct ncr53c9x_softc *sc = arg;
    540  1.9.8.2  nathanw 
    541  1.9.8.2  nathanw 	esc->sc_intrcnt.ev_count++;
    542  1.9.8.2  nathanw 
    543  1.9.8.2  nathanw 	/* Check for RAMBO DMA Interrupt */
    544  1.9.8.2  nathanw 	dma_stat = bus_space_read_4(esc->sc_bst, esc->dm_bsh, RAMBO_MODE);
    545  1.9.8.2  nathanw 	if (dma_stat & RB_INTR_PEND) {
    546  1.9.8.2  nathanw 		rambo_dma_chain(esc);
    547  1.9.8.2  nathanw 	}
    548  1.9.8.2  nathanw 	/* Check for NCR 53c94 interrupt */
    549  1.9.8.2  nathanw 	if (NCR_READ_REG(sc, NCR_STAT) & NCRSTAT_INT) {
    550  1.9.8.2  nathanw 		ncr53c9x_intr(sc);
    551  1.9.8.2  nathanw 	}
    552  1.9.8.2  nathanw 	return 0;
    553  1.9.8.2  nathanw }
    554