Home | History | Annotate | Line # | Download | only in dev
esp.c revision 1.3
      1  1.3  tsubai /*	$NetBSD: esp.c,v 1.3 1998/06/05 12:22:44 tsubai Exp $	*/
      2  1.1  tsubai 
      3  1.1  tsubai /*-
      4  1.1  tsubai  * Copyright (c) 1997 The NetBSD Foundation, Inc.
      5  1.1  tsubai  * All rights reserved.
      6  1.1  tsubai  *
      7  1.1  tsubai  * This code is derived from software contributed to The NetBSD Foundation
      8  1.1  tsubai  * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
      9  1.1  tsubai  * NASA Ames Research Center.
     10  1.1  tsubai  *
     11  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
     12  1.1  tsubai  * modification, are permitted provided that the following conditions
     13  1.1  tsubai  * are met:
     14  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     15  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     16  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     17  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     18  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     19  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     20  1.1  tsubai  *    must display the following acknowledgement:
     21  1.1  tsubai  *	This product includes software developed by the NetBSD
     22  1.1  tsubai  *	Foundation, Inc. and its contributors.
     23  1.1  tsubai  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  1.1  tsubai  *    contributors may be used to endorse or promote products derived
     25  1.1  tsubai  *    from this software without specific prior written permission.
     26  1.1  tsubai  *
     27  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  1.1  tsubai  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  1.1  tsubai  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  1.1  tsubai  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  1.1  tsubai  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  1.1  tsubai  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  1.1  tsubai  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  1.1  tsubai  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  1.1  tsubai  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  1.1  tsubai  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  1.1  tsubai  * POSSIBILITY OF SUCH DAMAGE.
     38  1.1  tsubai  */
     39  1.1  tsubai 
     40  1.1  tsubai /*
     41  1.1  tsubai  * Copyright (c) 1996 Charles M. Hannum.  All rights reserved.
     42  1.1  tsubai  *
     43  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
     44  1.1  tsubai  * modification, are permitted provided that the following conditions
     45  1.1  tsubai  * are met:
     46  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     47  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     48  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     49  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     50  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     51  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     52  1.1  tsubai  *    must display the following acknowledgement:
     53  1.1  tsubai  *	This product includes software developed by Charles M. Hannum.
     54  1.1  tsubai  * 4. The name of the author may not be used to endorse or promote products
     55  1.1  tsubai  *    derived from this software without specific prior written permission.
     56  1.1  tsubai  *
     57  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     58  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     59  1.1  tsubai  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     60  1.1  tsubai  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     61  1.1  tsubai  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     62  1.1  tsubai  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     63  1.1  tsubai  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     64  1.1  tsubai  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     65  1.1  tsubai  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     66  1.1  tsubai  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     67  1.1  tsubai  */
     68  1.1  tsubai 
     69  1.1  tsubai /*
     70  1.1  tsubai  * Copyright (c) 1994 Peter Galbavy
     71  1.1  tsubai  * Copyright (c) 1995 Paul Kranenburg
     72  1.1  tsubai  * All rights reserved.
     73  1.1  tsubai  *
     74  1.1  tsubai  * Redistribution and use in source and binary forms, with or without
     75  1.1  tsubai  * modification, are permitted provided that the following conditions
     76  1.1  tsubai  * are met:
     77  1.1  tsubai  * 1. Redistributions of source code must retain the above copyright
     78  1.1  tsubai  *    notice, this list of conditions and the following disclaimer.
     79  1.1  tsubai  * 2. Redistributions in binary form must reproduce the above copyright
     80  1.1  tsubai  *    notice, this list of conditions and the following disclaimer in the
     81  1.1  tsubai  *    documentation and/or other materials provided with the distribution.
     82  1.1  tsubai  * 3. All advertising materials mentioning features or use of this software
     83  1.1  tsubai  *    must display the following acknowledgement:
     84  1.1  tsubai  *	This product includes software developed by Peter Galbavy
     85  1.1  tsubai  * 4. The name of the author may not be used to endorse or promote products
     86  1.1  tsubai  *    derived from this software without specific prior written permission.
     87  1.1  tsubai  *
     88  1.1  tsubai  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     89  1.1  tsubai  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     90  1.1  tsubai  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     91  1.1  tsubai  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     92  1.1  tsubai  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     93  1.1  tsubai  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     94  1.1  tsubai  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     95  1.1  tsubai  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     96  1.1  tsubai  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     97  1.1  tsubai  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     98  1.1  tsubai  * POSSIBILITY OF SUCH DAMAGE.
     99  1.1  tsubai  */
    100  1.1  tsubai 
    101  1.1  tsubai /*
    102  1.1  tsubai  * Based on aic6360 by Jarle Greipsland
    103  1.1  tsubai  *
    104  1.1  tsubai  * Acknowledgements: Many of the algorithms used in this driver are
    105  1.1  tsubai  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
    106  1.1  tsubai  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
    107  1.1  tsubai  */
    108  1.1  tsubai 
    109  1.1  tsubai #include <sys/types.h>
    110  1.1  tsubai #include <sys/param.h>
    111  1.1  tsubai #include <sys/systm.h>
    112  1.1  tsubai #include <sys/kernel.h>
    113  1.1  tsubai #include <sys/errno.h>
    114  1.1  tsubai #include <sys/ioctl.h>
    115  1.1  tsubai #include <sys/device.h>
    116  1.1  tsubai #include <sys/buf.h>
    117  1.1  tsubai #include <sys/proc.h>
    118  1.1  tsubai #include <sys/user.h>
    119  1.1  tsubai #include <sys/queue.h>
    120  1.1  tsubai #include <sys/malloc.h>
    121  1.1  tsubai 
    122  1.1  tsubai #include <vm/vm_param.h>	/* for trunc_page */
    123  1.1  tsubai 
    124  1.1  tsubai #include <dev/scsipi/scsi_all.h>
    125  1.1  tsubai #include <dev/scsipi/scsipi_all.h>
    126  1.1  tsubai #include <dev/scsipi/scsiconf.h>
    127  1.1  tsubai #include <dev/scsipi/scsi_message.h>
    128  1.1  tsubai 
    129  1.1  tsubai #include <dev/ofw/openfirm.h>
    130  1.1  tsubai 
    131  1.1  tsubai #include <machine/cpu.h>
    132  1.1  tsubai #include <machine/autoconf.h>
    133  1.1  tsubai #include <machine/pio.h>
    134  1.1  tsubai 
    135  1.1  tsubai #include <dev/ic/ncr53c9xreg.h>
    136  1.1  tsubai #include <dev/ic/ncr53c9xvar.h>
    137  1.1  tsubai 
    138  1.1  tsubai #include <macppc/dev/dbdma.h>
    139  1.1  tsubai #include <macppc/dev/espvar.h>
    140  1.1  tsubai 
    141  1.1  tsubai void	espattach	__P((struct device *, struct device *, void *));
    142  1.1  tsubai int	espmatch	__P((struct device *, struct cfdata *, void *));
    143  1.1  tsubai 
    144  1.1  tsubai /* Linkup to the rest of the kernel */
    145  1.1  tsubai struct cfattach esp_ca = {
    146  1.1  tsubai 	sizeof(struct esp_softc), espmatch, espattach
    147  1.1  tsubai };
    148  1.1  tsubai 
    149  1.1  tsubai struct scsipi_adapter esp_switch = {
    150  1.1  tsubai 	ncr53c9x_scsi_cmd,
    151  1.1  tsubai 	minphys,		/* no max at this level; handled by DMA code */
    152  1.1  tsubai 	NULL,
    153  1.1  tsubai 	NULL,
    154  1.1  tsubai };
    155  1.1  tsubai 
    156  1.1  tsubai struct scsipi_device esp_dev = {
    157  1.1  tsubai 	NULL,			/* Use default error handler */
    158  1.1  tsubai 	NULL,			/* have a queue, served by this */
    159  1.1  tsubai 	NULL,			/* have no async handler */
    160  1.1  tsubai 	NULL,			/* Use default 'done' routine */
    161  1.1  tsubai };
    162  1.1  tsubai 
    163  1.1  tsubai /*
    164  1.1  tsubai  * Functions and the switch for the MI code.
    165  1.1  tsubai  */
    166  1.1  tsubai u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    167  1.1  tsubai void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    168  1.1  tsubai int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    169  1.1  tsubai void	esp_dma_reset __P((struct ncr53c9x_softc *));
    170  1.1  tsubai int	esp_dma_intr __P((struct ncr53c9x_softc *));
    171  1.1  tsubai int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    172  1.1  tsubai 	    size_t *, int, size_t *));
    173  1.1  tsubai void	esp_dma_go __P((struct ncr53c9x_softc *));
    174  1.1  tsubai void	esp_dma_stop __P((struct ncr53c9x_softc *));
    175  1.1  tsubai int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    176  1.1  tsubai 
    177  1.1  tsubai struct ncr53c9x_glue esp_glue = {
    178  1.1  tsubai 	esp_read_reg,
    179  1.1  tsubai 	esp_write_reg,
    180  1.1  tsubai 	esp_dma_isintr,
    181  1.1  tsubai 	esp_dma_reset,
    182  1.1  tsubai 	esp_dma_intr,
    183  1.1  tsubai 	esp_dma_setup,
    184  1.1  tsubai 	esp_dma_go,
    185  1.1  tsubai 	esp_dma_stop,
    186  1.1  tsubai 	esp_dma_isactive,
    187  1.1  tsubai 	NULL,			/* gl_clear_latched_intr */
    188  1.1  tsubai };
    189  1.1  tsubai 
    190  1.1  tsubai static int espdmaintr __P((struct esp_softc *));
    191  1.1  tsubai static void esp_shutdownhook __P((void *));
    192  1.1  tsubai 
    193  1.1  tsubai int
    194  1.1  tsubai espmatch(parent, cf, aux)
    195  1.1  tsubai 	struct device *parent;
    196  1.1  tsubai 	struct cfdata *cf;
    197  1.1  tsubai 	void *aux;
    198  1.1  tsubai {
    199  1.1  tsubai 	struct confargs *ca = aux;
    200  1.1  tsubai 
    201  1.1  tsubai 	if (strcmp(ca->ca_name, "53c94") != 0)
    202  1.1  tsubai 		return 0;
    203  1.1  tsubai 
    204  1.1  tsubai 	if (ca->ca_nreg != 16)
    205  1.1  tsubai 		return 0;
    206  1.1  tsubai 	if (ca->ca_nintr != 8)
    207  1.1  tsubai 		return 0;
    208  1.1  tsubai 
    209  1.1  tsubai 	return 1;
    210  1.1  tsubai }
    211  1.1  tsubai 
    212  1.1  tsubai /*
    213  1.1  tsubai  * Attach this instance, and then all the sub-devices
    214  1.1  tsubai  */
    215  1.1  tsubai void
    216  1.1  tsubai espattach(parent, self, aux)
    217  1.1  tsubai 	struct device *parent, *self;
    218  1.1  tsubai 	void *aux;
    219  1.1  tsubai {
    220  1.1  tsubai 	register struct confargs *ca = aux;
    221  1.1  tsubai 	struct esp_softc *esc = (void *)self;
    222  1.1  tsubai 	struct ncr53c9x_softc *sc = &esc->sc_ncr53c9x;
    223  1.1  tsubai 	u_int *reg;
    224  1.1  tsubai 	int sz;
    225  1.1  tsubai 
    226  1.1  tsubai 	/*
    227  1.1  tsubai 	 * Set up glue for MI code early; we use some of it here.
    228  1.1  tsubai 	 */
    229  1.1  tsubai 	sc->sc_glue = &esp_glue;
    230  1.1  tsubai 
    231  1.1  tsubai 	esc->sc_node = ca->ca_node;
    232  1.1  tsubai 	esc->sc_pri = ca->ca_intr[0];
    233  1.1  tsubai 	printf(" irq %d", esc->sc_pri);
    234  1.1  tsubai 
    235  1.1  tsubai 	/*
    236  1.1  tsubai 	 * Map my registers in.
    237  1.1  tsubai 	 */
    238  1.1  tsubai 	reg = ca->ca_reg;
    239  1.1  tsubai 	esc->sc_reg =    mapiodev(ca->ca_baseaddr + reg[0], reg[1]);
    240  1.1  tsubai 	esc->sc_dmareg = mapiodev(ca->ca_baseaddr + reg[2], reg[3]);
    241  1.1  tsubai 
    242  1.1  tsubai 	/* Allocate 16-byte aligned dma command space */
    243  1.1  tsubai 	esc->sc_dmacmd = dbdma_alloc(sizeof(dbdma_command_t) * 20);
    244  1.1  tsubai 
    245  1.1  tsubai 	/* Other settings */
    246  1.1  tsubai 	sc->sc_id = 7;
    247  1.1  tsubai 	sz = OF_getprop(ca->ca_node, "clock-frequency",
    248  1.1  tsubai 		&sc->sc_freq, sizeof(int));
    249  1.1  tsubai 	if (sz != sizeof(int))
    250  1.1  tsubai 		sc->sc_freq = 25000000;
    251  1.1  tsubai 
    252  1.1  tsubai 	/* gimme Mhz */
    253  1.1  tsubai 	sc->sc_freq /= 1000000;
    254  1.1  tsubai 
    255  1.1  tsubai 	/* esc->sc_dma->sc_esp = esc;*/
    256  1.1  tsubai 
    257  1.1  tsubai 	/*
    258  1.1  tsubai 	 * XXX More of this should be in ncr53c9x_attach(), but
    259  1.1  tsubai 	 * XXX should we really poke around the chip that much in
    260  1.1  tsubai 	 * XXX the MI code?  Think about this more...
    261  1.1  tsubai 	 */
    262  1.1  tsubai 
    263  1.1  tsubai 	/*
    264  1.1  tsubai 	 * Set up static configuration info.
    265  1.1  tsubai 	 */
    266  1.1  tsubai 	sc->sc_cfg1 = sc->sc_id | NCRCFG1_PARENB;
    267  1.1  tsubai 	sc->sc_cfg2 = NCRCFG2_SCSI2; /* | NCRCFG2_FE */
    268  1.1  tsubai 	sc->sc_cfg3 = NCRCFG3_CDB;
    269  1.1  tsubai 	sc->sc_rev = NCR_VARIANT_NCR53C94;
    270  1.1  tsubai 
    271  1.1  tsubai 	/*
    272  1.1  tsubai 	 * XXX minsync and maxxfer _should_ be set up in MI code,
    273  1.1  tsubai 	 * XXX but it appears to have some dependency on what sort
    274  1.1  tsubai 	 * XXX of DMA we're hooked up to, etc.
    275  1.1  tsubai 	 */
    276  1.1  tsubai 
    277  1.1  tsubai 	/*
    278  1.1  tsubai 	 * This is the value used to start sync negotiations
    279  1.1  tsubai 	 * Note that the NCR register "SYNCTP" is programmed
    280  1.1  tsubai 	 * in "clocks per byte", and has a minimum value of 4.
    281  1.1  tsubai 	 * The SCSI period used in negotiation is one-fourth
    282  1.1  tsubai 	 * of the time (in nanoseconds) needed to transfer one byte.
    283  1.1  tsubai 	 * Since the chip's clock is given in MHz, we have the following
    284  1.1  tsubai 	 * formula: 4 * period = (1000 / freq) * 4
    285  1.1  tsubai 	 */
    286  1.1  tsubai 	sc->sc_minsync = 1000 / sc->sc_freq;
    287  1.1  tsubai 
    288  1.1  tsubai 	sc->sc_maxxfer = 64 * 1024;
    289  1.1  tsubai 
    290  1.1  tsubai 	/* and the interuppts */
    291  1.1  tsubai 	intr_establish(esc->sc_pri, IST_LEVEL, IPL_BIO, (void *)ncr53c9x_intr,
    292  1.1  tsubai 	    sc);
    293  1.1  tsubai 
    294  1.1  tsubai 	/* Do the common parts of attachment. */
    295  1.1  tsubai 	ncr53c9x_attach(sc, &esp_switch, &esp_dev);
    296  1.1  tsubai 
    297  1.1  tsubai 	/* Turn on target selection using the `dma' method */
    298  1.1  tsubai 	ncr53c9x_dmaselect = 1;
    299  1.1  tsubai 
    300  1.1  tsubai 	/* Reset SCSI bus when halt. */
    301  1.1  tsubai 	shutdownhook_establish(esp_shutdownhook, sc);
    302  1.1  tsubai }
    303  1.1  tsubai 
    304  1.1  tsubai /*
    305  1.1  tsubai  * Glue functions.
    306  1.1  tsubai  */
    307  1.1  tsubai 
    308  1.1  tsubai u_char
    309  1.1  tsubai esp_read_reg(sc, reg)
    310  1.1  tsubai 	struct ncr53c9x_softc *sc;
    311  1.1  tsubai 	int reg;
    312  1.1  tsubai {
    313  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    314  1.1  tsubai 
    315  1.1  tsubai 	return in8(&esc->sc_reg[reg * 16]);
    316  1.1  tsubai 	/*return (esc->sc_reg[reg * 16]);*/
    317  1.1  tsubai }
    318  1.1  tsubai 
    319  1.1  tsubai void
    320  1.1  tsubai esp_write_reg(sc, reg, val)
    321  1.1  tsubai 	struct ncr53c9x_softc *sc;
    322  1.1  tsubai 	int reg;
    323  1.1  tsubai 	u_char val;
    324  1.1  tsubai {
    325  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    326  1.1  tsubai 	u_char v = val;
    327  1.1  tsubai 
    328  1.1  tsubai 	out8(&esc->sc_reg[reg * 16], v);
    329  1.1  tsubai 	/*esc->sc_reg[reg * 16] = v;*/
    330  1.1  tsubai }
    331  1.1  tsubai 
    332  1.1  tsubai int
    333  1.1  tsubai esp_dma_isintr(sc)
    334  1.1  tsubai 	struct ncr53c9x_softc *sc;
    335  1.1  tsubai {
    336  1.1  tsubai 	return esp_read_reg(sc, NCR_STAT) & NCRSTAT_INT;
    337  1.1  tsubai }
    338  1.1  tsubai 
    339  1.1  tsubai void
    340  1.1  tsubai esp_dma_reset(sc)
    341  1.1  tsubai 	struct ncr53c9x_softc *sc;
    342  1.1  tsubai {
    343  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    344  1.1  tsubai 
    345  1.1  tsubai 	dbdma_stop(esc->sc_dmareg);
    346  1.1  tsubai 	esc->sc_dmaactive = 0;
    347  1.1  tsubai }
    348  1.1  tsubai 
    349  1.1  tsubai int
    350  1.1  tsubai esp_dma_intr(sc)
    351  1.1  tsubai 	struct ncr53c9x_softc *sc;
    352  1.1  tsubai {
    353  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    354  1.1  tsubai 
    355  1.1  tsubai 	return (espdmaintr(esc));
    356  1.1  tsubai }
    357  1.1  tsubai 
    358  1.1  tsubai int
    359  1.1  tsubai esp_dma_setup(sc, addr, len, datain, dmasize)
    360  1.1  tsubai 	struct ncr53c9x_softc *sc;
    361  1.1  tsubai 	caddr_t *addr;
    362  1.1  tsubai 	size_t *len;
    363  1.1  tsubai 	int datain;
    364  1.1  tsubai 	size_t *dmasize;
    365  1.1  tsubai {
    366  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    367  1.1  tsubai 	dbdma_command_t *cmdp;
    368  1.1  tsubai 	u_int cmd;
    369  1.1  tsubai 	u_int va;
    370  1.1  tsubai 	int count, offset;
    371  1.1  tsubai 
    372  1.1  tsubai 	cmdp = esc->sc_dmacmd;
    373  1.1  tsubai 	cmd = datain ? DBDMA_CMD_IN_MORE : DBDMA_CMD_OUT_MORE;
    374  1.1  tsubai 
    375  1.1  tsubai 	count = *dmasize;
    376  1.1  tsubai 
    377  1.1  tsubai 	if (count / NBPG > 32)
    378  1.1  tsubai 		panic("esp: transfer size >= 128k");
    379  1.1  tsubai 
    380  1.1  tsubai 	esc->sc_dmaaddr = addr;
    381  1.1  tsubai 	esc->sc_dmalen = len;
    382  1.1  tsubai 	esc->sc_dmasize = count;
    383  1.1  tsubai 
    384  1.1  tsubai 	va = (u_int)*esc->sc_dmaaddr;
    385  1.1  tsubai 	offset = va & PGOFSET;
    386  1.1  tsubai 
    387  1.1  tsubai 	/* if va is not page-aligned, setup the first page */
    388  1.1  tsubai 	if (offset != 0) {
    389  1.1  tsubai 		int rest = NBPG - offset;	/* the rest of the page */
    390  1.1  tsubai 
    391  1.1  tsubai 		if (count > rest) {		/* if continues to next page */
    392  1.1  tsubai 			DBDMA_BUILD(cmdp, cmd, 0, rest, kvtop((caddr_t)va),
    393  1.1  tsubai 				DBDMA_INT_NEVER, DBDMA_WAIT_NEVER,
    394  1.1  tsubai 				DBDMA_BRANCH_NEVER);
    395  1.1  tsubai 			count -= rest;
    396  1.1  tsubai 			va += rest;
    397  1.1  tsubai 			cmdp++;
    398  1.1  tsubai 		}
    399  1.1  tsubai 	}
    400  1.1  tsubai 
    401  1.1  tsubai 	/* now va is page-aligned */
    402  1.1  tsubai 	while (count > NBPG) {
    403  1.1  tsubai 		DBDMA_BUILD(cmdp, cmd, 0, NBPG, kvtop((caddr_t)va),
    404  1.1  tsubai 			DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    405  1.1  tsubai 		count -= NBPG;
    406  1.1  tsubai 		va += NBPG;
    407  1.1  tsubai 		cmdp++;
    408  1.1  tsubai 	}
    409  1.1  tsubai 
    410  1.1  tsubai 	/* the last page (count <= NBPG here) */
    411  1.1  tsubai 	cmd = datain ? DBDMA_CMD_IN_LAST : DBDMA_CMD_OUT_LAST;
    412  1.1  tsubai 	DBDMA_BUILD(cmdp, cmd , 0, count, kvtop((caddr_t)va),
    413  1.1  tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    414  1.1  tsubai 	cmdp++;
    415  1.1  tsubai 
    416  1.1  tsubai 	DBDMA_BUILD(cmdp, DBDMA_CMD_STOP, 0, 0, 0,
    417  1.1  tsubai 		DBDMA_INT_NEVER, DBDMA_WAIT_NEVER, DBDMA_BRANCH_NEVER);
    418  1.1  tsubai 
    419  1.1  tsubai 	esc->sc_dma_direction = datain ? D_WRITE : 0;
    420  1.1  tsubai 
    421  1.1  tsubai 	return 0;
    422  1.1  tsubai }
    423  1.1  tsubai 
    424  1.1  tsubai void
    425  1.1  tsubai esp_dma_go(sc)
    426  1.1  tsubai 	struct ncr53c9x_softc *sc;
    427  1.1  tsubai {
    428  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    429  1.1  tsubai 
    430  1.1  tsubai 	dbdma_start(esc->sc_dmareg, esc->sc_dmacmd);
    431  1.1  tsubai 	esc->sc_dmaactive = 1;
    432  1.1  tsubai }
    433  1.1  tsubai 
    434  1.1  tsubai void
    435  1.1  tsubai esp_dma_stop(sc)
    436  1.1  tsubai 	struct ncr53c9x_softc *sc;
    437  1.1  tsubai {
    438  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    439  1.1  tsubai 
    440  1.1  tsubai 	dbdma_stop(esc->sc_dmareg);
    441  1.1  tsubai 	esc->sc_dmaactive = 0;
    442  1.1  tsubai }
    443  1.1  tsubai 
    444  1.1  tsubai int
    445  1.1  tsubai esp_dma_isactive(sc)
    446  1.1  tsubai 	struct ncr53c9x_softc *sc;
    447  1.1  tsubai {
    448  1.1  tsubai 	struct esp_softc *esc = (struct esp_softc *)sc;
    449  1.1  tsubai 
    450  1.1  tsubai 	return (esc->sc_dmaactive);
    451  1.1  tsubai }
    452  1.1  tsubai 
    453  1.1  tsubai 
    454  1.1  tsubai /*
    455  1.1  tsubai  * Pseudo (chained) interrupt from the esp driver to kick the
    456  1.1  tsubai  * current running DMA transfer. I am replying on espintr() to
    457  1.1  tsubai  * pickup and clean errors for now
    458  1.1  tsubai  *
    459  1.1  tsubai  * return 1 if it was a DMA continue.
    460  1.1  tsubai  */
    461  1.1  tsubai int
    462  1.1  tsubai espdmaintr(sc)
    463  1.1  tsubai 	struct esp_softc *sc;
    464  1.1  tsubai {
    465  1.1  tsubai 	struct ncr53c9x_softc *nsc = (struct ncr53c9x_softc *)sc;
    466  1.1  tsubai 	int trans, resid;
    467  1.1  tsubai 	u_long csr = sc->sc_dma_direction;
    468  1.1  tsubai 
    469  1.1  tsubai #if 0
    470  1.1  tsubai 	if (csr & D_ERR_PEND) {
    471  1.1  tsubai 		DMACSR(sc) &= ~D_EN_DMA;	/* Stop DMA */
    472  1.1  tsubai 		DMACSR(sc) |= D_INVALIDATE;
    473  1.1  tsubai 		printf("%s: error: csr=%s\n", nsc->sc_dev.dv_xname,
    474  1.1  tsubai 			bitmask_snprintf(csr, DMACSRBITS, bits, sizeof(bits)));
    475  1.1  tsubai 		return -1;
    476  1.1  tsubai 	}
    477  1.1  tsubai #endif
    478  1.1  tsubai 
    479  1.1  tsubai 	/* This is an "assertion" :) */
    480  1.1  tsubai 	if (sc->sc_dmaactive == 0)
    481  1.1  tsubai 		panic("dmaintr: DMA wasn't active");
    482  1.1  tsubai 
    483  1.1  tsubai 	/* dbdma_flush(sc->sc_dmareg); */
    484  1.1  tsubai 
    485  1.1  tsubai 	/* DMA has stopped */
    486  1.1  tsubai 	dbdma_stop(sc->sc_dmareg);
    487  1.1  tsubai 	sc->sc_dmaactive = 0;
    488  1.1  tsubai 
    489  1.1  tsubai 	if (sc->sc_dmasize == 0) {
    490  1.1  tsubai 		/* A "Transfer Pad" operation completed */
    491  1.1  tsubai 		NCR_DMA(("dmaintr: discarded %d bytes (tcl=%d, tcm=%d)\n",
    492  1.1  tsubai 			NCR_READ_REG(nsc, NCR_TCL) |
    493  1.1  tsubai 				(NCR_READ_REG(nsc, NCR_TCM) << 8),
    494  1.1  tsubai 			NCR_READ_REG(nsc, NCR_TCL),
    495  1.1  tsubai 			NCR_READ_REG(nsc, NCR_TCM)));
    496  1.1  tsubai 		return 0;
    497  1.1  tsubai 	}
    498  1.1  tsubai 
    499  1.1  tsubai 	resid = 0;
    500  1.1  tsubai 	/*
    501  1.1  tsubai 	 * If a transfer onto the SCSI bus gets interrupted by the device
    502  1.1  tsubai 	 * (e.g. for a SAVEPOINTER message), the data in the FIFO counts
    503  1.1  tsubai 	 * as residual since the ESP counter registers get decremented as
    504  1.1  tsubai 	 * bytes are clocked into the FIFO.
    505  1.1  tsubai 	 */
    506  1.1  tsubai 	if (!(csr & D_WRITE) &&
    507  1.1  tsubai 	    (resid = (NCR_READ_REG(nsc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    508  1.1  tsubai 		NCR_DMA(("dmaintr: empty esp FIFO of %d ", resid));
    509  1.1  tsubai 	}
    510  1.1  tsubai 
    511  1.1  tsubai 	if ((nsc->sc_espstat & NCRSTAT_TC) == 0) {
    512  1.1  tsubai 		/*
    513  1.1  tsubai 		 * `Terminal count' is off, so read the residue
    514  1.1  tsubai 		 * out of the ESP counter registers.
    515  1.1  tsubai 		 */
    516  1.1  tsubai 		resid += (NCR_READ_REG(nsc, NCR_TCL) |
    517  1.1  tsubai 			  (NCR_READ_REG(nsc, NCR_TCM) << 8) |
    518  1.1  tsubai 			   ((nsc->sc_cfg2 & NCRCFG2_FE)
    519  1.1  tsubai 				? (NCR_READ_REG(nsc, NCR_TCH) << 16)
    520  1.1  tsubai 				: 0));
    521  1.1  tsubai 
    522  1.1  tsubai 		if (resid == 0 && sc->sc_dmasize == 65536 &&
    523  1.1  tsubai 		    (nsc->sc_cfg2 & NCRCFG2_FE) == 0)
    524  1.1  tsubai 			/* A transfer of 64K is encoded as `TCL=TCM=0' */
    525  1.1  tsubai 			resid = 65536;
    526  1.1  tsubai 	}
    527  1.1  tsubai 
    528  1.1  tsubai 	trans = sc->sc_dmasize - resid;
    529  1.1  tsubai 	if (trans < 0) {			/* transferred < 0 ? */
    530  1.1  tsubai #if 0
    531  1.1  tsubai 		/*
    532  1.1  tsubai 		 * This situation can happen in perfectly normal operation
    533  1.1  tsubai 		 * if the ESP is reselected while using DMA to select
    534  1.1  tsubai 		 * another target.  As such, don't print the warning.
    535  1.1  tsubai 		 */
    536  1.1  tsubai 		printf("%s: xfer (%d) > req (%d)\n",
    537  1.1  tsubai 		    sc->sc_dev.dv_xname, trans, sc->sc_dmasize);
    538  1.1  tsubai #endif
    539  1.1  tsubai 		trans = sc->sc_dmasize;
    540  1.1  tsubai 	}
    541  1.1  tsubai 
    542  1.1  tsubai 	NCR_DMA(("dmaintr: tcl=%d, tcm=%d, tch=%d; trans=%d, resid=%d\n",
    543  1.1  tsubai 		NCR_READ_REG(nsc, NCR_TCL),
    544  1.1  tsubai 		NCR_READ_REG(nsc, NCR_TCM),
    545  1.1  tsubai 		(nsc->sc_cfg2 & NCRCFG2_FE)
    546  1.1  tsubai 			? NCR_READ_REG(nsc, NCR_TCH) : 0,
    547  1.1  tsubai 		trans, resid));
    548  1.1  tsubai 
    549  1.2  tsubai #if 0
    550  1.3  tsubai 	if (csr & D_WRITE)
    551  1.3  tsubai 		flushcache(*sc->sc_dmaaddr, trans);
    552  1.2  tsubai #endif
    553  1.1  tsubai 
    554  1.1  tsubai 	*sc->sc_dmalen -= trans;
    555  1.1  tsubai 	*sc->sc_dmaaddr += trans;
    556  1.1  tsubai 
    557  1.1  tsubai #if 0	/* this is not normal operation just yet */
    558  1.1  tsubai 	if (*sc->sc_dmalen == 0 ||
    559  1.1  tsubai 	    nsc->sc_phase != nsc->sc_prevphase)
    560  1.1  tsubai 		return 0;
    561  1.1  tsubai 
    562  1.1  tsubai 	/* and again */
    563  1.1  tsubai 	dma_start(sc, sc->sc_dmaaddr, sc->sc_dmalen, DMACSR(sc) & D_WRITE);
    564  1.1  tsubai 	return 1;
    565  1.1  tsubai #endif
    566  1.1  tsubai 	return 0;
    567  1.1  tsubai }
    568  1.1  tsubai 
    569  1.1  tsubai void
    570  1.1  tsubai esp_shutdownhook(arg)
    571  1.1  tsubai 	void *arg;
    572  1.1  tsubai {
    573  1.1  tsubai 	struct ncr53c9x_softc *sc = arg;
    574  1.1  tsubai 
    575  1.1  tsubai 	NCRCMD(sc, NCRCMD_RSTSCSI);
    576  1.1  tsubai }
    577