Home | History | Annotate | Line # | Download | only in obio
esp.c revision 1.29.8.2
      1  1.29.8.2  nathanw /*	$NetBSD: esp.c,v 1.29.8.2 2002/01/08 00:26:08 nathanw Exp $	*/
      2  1.29.8.2  nathanw 
      3  1.29.8.2  nathanw /*
      4  1.29.8.2  nathanw  * Copyright (c) 1997 Jason R. Thorpe.
      5  1.29.8.2  nathanw  * All rights reserved.
      6  1.29.8.2  nathanw  *
      7  1.29.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
      8  1.29.8.2  nathanw  * modification, are permitted provided that the following conditions
      9  1.29.8.2  nathanw  * are met:
     10  1.29.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     11  1.29.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     12  1.29.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     13  1.29.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     14  1.29.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     15  1.29.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     16  1.29.8.2  nathanw  *    must display the following acknowledgement:
     17  1.29.8.2  nathanw  *	This product includes software developed for the NetBSD Project
     18  1.29.8.2  nathanw  *	by Jason R. Thorpe.
     19  1.29.8.2  nathanw  * 4. The name of the author may not be used to endorse or promote products
     20  1.29.8.2  nathanw  *    derived from this software without specific prior written permission.
     21  1.29.8.2  nathanw  *
     22  1.29.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23  1.29.8.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24  1.29.8.2  nathanw  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25  1.29.8.2  nathanw  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26  1.29.8.2  nathanw  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27  1.29.8.2  nathanw  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28  1.29.8.2  nathanw  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29  1.29.8.2  nathanw  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30  1.29.8.2  nathanw  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31  1.29.8.2  nathanw  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32  1.29.8.2  nathanw  */
     33  1.29.8.2  nathanw 
     34  1.29.8.2  nathanw /*
     35  1.29.8.2  nathanw  * Copyright (c) 1994 Peter Galbavy
     36  1.29.8.2  nathanw  * All rights reserved.
     37  1.29.8.2  nathanw  *
     38  1.29.8.2  nathanw  * Redistribution and use in source and binary forms, with or without
     39  1.29.8.2  nathanw  * modification, are permitted provided that the following conditions
     40  1.29.8.2  nathanw  * are met:
     41  1.29.8.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     42  1.29.8.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     43  1.29.8.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     44  1.29.8.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     45  1.29.8.2  nathanw  *    documentation and/or other materials provided with the distribution.
     46  1.29.8.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     47  1.29.8.2  nathanw  *    must display the following acknowledgement:
     48  1.29.8.2  nathanw  *	This product includes software developed by Peter Galbavy
     49  1.29.8.2  nathanw  * 4. The name of the author may not be used to endorse or promote products
     50  1.29.8.2  nathanw  *    derived from this software without specific prior written permission.
     51  1.29.8.2  nathanw  *
     52  1.29.8.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     53  1.29.8.2  nathanw  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     54  1.29.8.2  nathanw  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     55  1.29.8.2  nathanw  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     56  1.29.8.2  nathanw  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     57  1.29.8.2  nathanw  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     58  1.29.8.2  nathanw  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     59  1.29.8.2  nathanw  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     60  1.29.8.2  nathanw  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     61  1.29.8.2  nathanw  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     62  1.29.8.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     63  1.29.8.2  nathanw  */
     64  1.29.8.2  nathanw 
     65  1.29.8.2  nathanw /*
     66  1.29.8.2  nathanw  * Based on aic6360 by Jarle Greipsland
     67  1.29.8.2  nathanw  *
     68  1.29.8.2  nathanw  * Acknowledgements: Many of the algorithms used in this driver are
     69  1.29.8.2  nathanw  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     70  1.29.8.2  nathanw  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     71  1.29.8.2  nathanw  */
     72  1.29.8.2  nathanw 
     73  1.29.8.2  nathanw /*
     74  1.29.8.2  nathanw  * Initial m68k mac support from Allen Briggs <briggs (at) macbsd.com>
     75  1.29.8.2  nathanw  * (basically consisting of the match, a bit of the attach, and the
     76  1.29.8.2  nathanw  *  "DMA" glue functions).
     77  1.29.8.2  nathanw  */
     78  1.29.8.2  nathanw 
     79  1.29.8.2  nathanw #include <sys/types.h>
     80  1.29.8.2  nathanw #include <sys/param.h>
     81  1.29.8.2  nathanw #include <sys/systm.h>
     82  1.29.8.2  nathanw #include <sys/kernel.h>
     83  1.29.8.2  nathanw #include <sys/errno.h>
     84  1.29.8.2  nathanw #include <sys/ioctl.h>
     85  1.29.8.2  nathanw #include <sys/device.h>
     86  1.29.8.2  nathanw #include <sys/buf.h>
     87  1.29.8.2  nathanw #include <sys/proc.h>
     88  1.29.8.2  nathanw #include <sys/user.h>
     89  1.29.8.2  nathanw #include <sys/queue.h>
     90  1.29.8.2  nathanw 
     91  1.29.8.2  nathanw #include <dev/scsipi/scsi_all.h>
     92  1.29.8.2  nathanw #include <dev/scsipi/scsipi_all.h>
     93  1.29.8.2  nathanw #include <dev/scsipi/scsiconf.h>
     94  1.29.8.2  nathanw #include <dev/scsipi/scsi_message.h>
     95  1.29.8.2  nathanw 
     96  1.29.8.2  nathanw #include <machine/cpu.h>
     97  1.29.8.2  nathanw #include <machine/bus.h>
     98  1.29.8.2  nathanw #include <machine/param.h>
     99  1.29.8.2  nathanw 
    100  1.29.8.2  nathanw #include <dev/ic/ncr53c9xreg.h>
    101  1.29.8.2  nathanw #include <dev/ic/ncr53c9xvar.h>
    102  1.29.8.2  nathanw 
    103  1.29.8.2  nathanw #include <machine/viareg.h>
    104  1.29.8.2  nathanw 
    105  1.29.8.2  nathanw #include <mac68k/obio/espvar.h>
    106  1.29.8.2  nathanw #include <mac68k/obio/obiovar.h>
    107  1.29.8.2  nathanw 
    108  1.29.8.2  nathanw void	espattach	__P((struct device *, struct device *, void *));
    109  1.29.8.2  nathanw int	espmatch	__P((struct device *, struct cfdata *, void *));
    110  1.29.8.2  nathanw 
    111  1.29.8.2  nathanw /* Linkup to the rest of the kernel */
    112  1.29.8.2  nathanw struct cfattach esp_ca = {
    113  1.29.8.2  nathanw 	sizeof(struct esp_softc), espmatch, espattach
    114  1.29.8.2  nathanw };
    115  1.29.8.2  nathanw 
    116  1.29.8.2  nathanw /*
    117  1.29.8.2  nathanw  * Functions and the switch for the MI code.
    118  1.29.8.2  nathanw  */
    119  1.29.8.2  nathanw u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    120  1.29.8.2  nathanw void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    121  1.29.8.2  nathanw int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    122  1.29.8.2  nathanw void	esp_dma_reset __P((struct ncr53c9x_softc *));
    123  1.29.8.2  nathanw int	esp_dma_intr __P((struct ncr53c9x_softc *));
    124  1.29.8.2  nathanw int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    125  1.29.8.2  nathanw 	    size_t *, int, size_t *));
    126  1.29.8.2  nathanw void	esp_dma_go __P((struct ncr53c9x_softc *));
    127  1.29.8.2  nathanw void	esp_dma_stop __P((struct ncr53c9x_softc *));
    128  1.29.8.2  nathanw int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    129  1.29.8.2  nathanw void	esp_quick_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    130  1.29.8.2  nathanw int	esp_quick_dma_intr __P((struct ncr53c9x_softc *));
    131  1.29.8.2  nathanw int	esp_quick_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    132  1.29.8.2  nathanw 	    size_t *, int, size_t *));
    133  1.29.8.2  nathanw void	esp_quick_dma_go __P((struct ncr53c9x_softc *));
    134  1.29.8.2  nathanw 
    135  1.29.8.2  nathanw void	esp_intr __P((void *sc));
    136  1.29.8.2  nathanw void	esp_dualbus_intr __P((void *sc));
    137  1.29.8.2  nathanw static struct esp_softc		*esp0 = NULL, *esp1 = NULL;
    138  1.29.8.2  nathanw 
    139  1.29.8.2  nathanw static __inline__ int esp_dafb_have_dreq __P((struct esp_softc *esc));
    140  1.29.8.2  nathanw static __inline__ int esp_iosb_have_dreq __P((struct esp_softc *esc));
    141  1.29.8.2  nathanw int (*esp_have_dreq) __P((struct esp_softc *esc));
    142  1.29.8.2  nathanw 
    143  1.29.8.2  nathanw struct ncr53c9x_glue esp_glue = {
    144  1.29.8.2  nathanw 	esp_read_reg,
    145  1.29.8.2  nathanw 	esp_write_reg,
    146  1.29.8.2  nathanw 	esp_dma_isintr,
    147  1.29.8.2  nathanw 	esp_dma_reset,
    148  1.29.8.2  nathanw 	esp_dma_intr,
    149  1.29.8.2  nathanw 	esp_dma_setup,
    150  1.29.8.2  nathanw 	esp_dma_go,
    151  1.29.8.2  nathanw 	esp_dma_stop,
    152  1.29.8.2  nathanw 	esp_dma_isactive,
    153  1.29.8.2  nathanw 	NULL,			/* gl_clear_latched_intr */
    154  1.29.8.2  nathanw };
    155  1.29.8.2  nathanw 
    156  1.29.8.2  nathanw int
    157  1.29.8.2  nathanw espmatch(parent, cf, aux)
    158  1.29.8.2  nathanw 	struct device *parent;
    159  1.29.8.2  nathanw 	struct cfdata *cf;
    160  1.29.8.2  nathanw 	void *aux;
    161  1.29.8.2  nathanw {
    162  1.29.8.2  nathanw 	int	found = 0;
    163  1.29.8.2  nathanw 
    164  1.29.8.2  nathanw 	if ((cf->cf_unit == 0) && mac68k_machine.scsi96) {
    165  1.29.8.2  nathanw 		found = 1;
    166  1.29.8.2  nathanw 	}
    167  1.29.8.2  nathanw 	if ((cf->cf_unit == 1) && mac68k_machine.scsi96_2) {
    168  1.29.8.2  nathanw 		found = 1;
    169  1.29.8.2  nathanw 	}
    170  1.29.8.2  nathanw 
    171  1.29.8.2  nathanw 	return found;
    172  1.29.8.2  nathanw }
    173  1.29.8.2  nathanw 
    174  1.29.8.2  nathanw /*
    175  1.29.8.2  nathanw  * Attach this instance, and then all the sub-devices
    176  1.29.8.2  nathanw  */
    177  1.29.8.2  nathanw void
    178  1.29.8.2  nathanw espattach(parent, self, aux)
    179  1.29.8.2  nathanw 	struct device *parent, *self;
    180  1.29.8.2  nathanw 	void *aux;
    181  1.29.8.2  nathanw {
    182  1.29.8.2  nathanw 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    183  1.29.8.2  nathanw 	extern vaddr_t		SCSIBase;
    184  1.29.8.2  nathanw 	struct esp_softc	*esc = (void *)self;
    185  1.29.8.2  nathanw 	struct ncr53c9x_softc	*sc = &esc->sc_ncr53c9x;
    186  1.29.8.2  nathanw 	int			quick = 0;
    187  1.29.8.2  nathanw 	unsigned long		reg_offset;
    188  1.29.8.2  nathanw 
    189  1.29.8.2  nathanw 	reg_offset = SCSIBase - IOBase;
    190  1.29.8.2  nathanw 	esc->sc_tag = oa->oa_tag;
    191  1.29.8.2  nathanw 	/*
    192  1.29.8.2  nathanw 	 * For Wombat, Primus and Optimus motherboards, DREQ is
    193  1.29.8.2  nathanw 	 * visible on bit 0 of the IOSB's emulated VIA2 vIFR (and
    194  1.29.8.2  nathanw 	 * the scsi registers are offset 0x1000 bytes from IOBase).
    195  1.29.8.2  nathanw 	 *
    196  1.29.8.2  nathanw 	 * For the Q700/900/950 it's at f9800024 for bus 0 and
    197  1.29.8.2  nathanw 	 * f9800028 for bus 1 (900/950).  For these machines, that is also
    198  1.29.8.2  nathanw 	 * a (12-bit) configuration register for DAFB's control of the
    199  1.29.8.2  nathanw 	 * pseudo-DMA timing.  The default value is 0x1d1.
    200  1.29.8.2  nathanw 	 */
    201  1.29.8.2  nathanw 	esp_have_dreq = esp_dafb_have_dreq;
    202  1.29.8.2  nathanw 	if (sc->sc_dev.dv_unit == 0) {
    203  1.29.8.2  nathanw 		if (reg_offset == 0x10000) {
    204  1.29.8.2  nathanw 			quick = 1;
    205  1.29.8.2  nathanw 			esp_have_dreq = esp_iosb_have_dreq;
    206  1.29.8.2  nathanw 		} else if (reg_offset == 0x18000) {
    207  1.29.8.2  nathanw 			quick = 0;
    208  1.29.8.2  nathanw 		} else {
    209  1.29.8.2  nathanw 			if (bus_space_map(esc->sc_tag, 0xf9800024,
    210  1.29.8.2  nathanw 					  4, 0, &esc->sc_bsh)) {
    211  1.29.8.2  nathanw 				printf("failed to map 4 at 0xf9800024.\n");
    212  1.29.8.2  nathanw 			} else {
    213  1.29.8.2  nathanw 				quick = 1;
    214  1.29.8.2  nathanw 				bus_space_write_4(esc->sc_tag,
    215  1.29.8.2  nathanw 						  esc->sc_bsh, 0, 0x1d1);
    216  1.29.8.2  nathanw 			}
    217  1.29.8.2  nathanw 		}
    218  1.29.8.2  nathanw 	} else {
    219  1.29.8.2  nathanw 		if (bus_space_map(esc->sc_tag, 0xf9800028,
    220  1.29.8.2  nathanw 				  4, 0, &esc->sc_bsh)) {
    221  1.29.8.2  nathanw 			printf("failed to map 4 at 0xf9800028.\n");
    222  1.29.8.2  nathanw 		} else {
    223  1.29.8.2  nathanw 			quick = 1;
    224  1.29.8.2  nathanw 			bus_space_write_4(esc->sc_tag, esc->sc_bsh, 0, 0x1d1);
    225  1.29.8.2  nathanw 		}
    226  1.29.8.2  nathanw 	}
    227  1.29.8.2  nathanw 	if (quick) {
    228  1.29.8.2  nathanw 		esp_glue.gl_write_reg = esp_quick_write_reg;
    229  1.29.8.2  nathanw 		esp_glue.gl_dma_intr = esp_quick_dma_intr;
    230  1.29.8.2  nathanw 		esp_glue.gl_dma_setup = esp_quick_dma_setup;
    231  1.29.8.2  nathanw 		esp_glue.gl_dma_go = esp_quick_dma_go;
    232  1.29.8.2  nathanw 	}
    233  1.29.8.2  nathanw 
    234  1.29.8.2  nathanw 	/*
    235  1.29.8.2  nathanw 	 * Set up the glue for MI code early; we use some of it here.
    236  1.29.8.2  nathanw 	 */
    237  1.29.8.2  nathanw 	sc->sc_glue = &esp_glue;
    238  1.29.8.2  nathanw 
    239  1.29.8.2  nathanw 	/*
    240  1.29.8.2  nathanw 	 * Save the regs
    241  1.29.8.2  nathanw 	 */
    242  1.29.8.2  nathanw 	if (sc->sc_dev.dv_unit == 0) {
    243  1.29.8.2  nathanw 		esp0 = esc;
    244  1.29.8.2  nathanw 
    245  1.29.8.2  nathanw 		esc->sc_reg = (volatile u_char *) SCSIBase;
    246  1.29.8.2  nathanw 		via2_register_irq(VIA2_SCSIIRQ, esp_intr, esc);
    247  1.29.8.2  nathanw 		esc->irq_mask = V2IF_SCSIIRQ;
    248  1.29.8.2  nathanw 		if (reg_offset == 0x10000) {
    249  1.29.8.2  nathanw 			/* From the Q650 developer's note */
    250  1.29.8.2  nathanw 			sc->sc_freq = 16500000;
    251  1.29.8.2  nathanw 		} else {
    252  1.29.8.2  nathanw 			sc->sc_freq = 25000000;
    253  1.29.8.2  nathanw 		}
    254  1.29.8.2  nathanw 
    255  1.29.8.2  nathanw 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    256  1.29.8.2  nathanw 			printf(" (quick)");
    257  1.29.8.2  nathanw 		}
    258  1.29.8.2  nathanw 	} else {
    259  1.29.8.2  nathanw 		esp1 = esc;
    260  1.29.8.2  nathanw 
    261  1.29.8.2  nathanw 		esc->sc_reg = (volatile u_char *) SCSIBase + 0x402;
    262  1.29.8.2  nathanw 		via2_register_irq(VIA2_SCSIIRQ, esp_dualbus_intr, NULL);
    263  1.29.8.2  nathanw 		esc->irq_mask = 0;
    264  1.29.8.2  nathanw 		sc->sc_freq = 25000000;
    265  1.29.8.2  nathanw 
    266  1.29.8.2  nathanw 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    267  1.29.8.2  nathanw 			printf(" (quick)");
    268  1.29.8.2  nathanw 		}
    269  1.29.8.2  nathanw 	}
    270  1.29.8.2  nathanw 
    271  1.29.8.2  nathanw 	printf(": address %p", esc->sc_reg);
    272  1.29.8.2  nathanw 
    273  1.29.8.2  nathanw 	sc->sc_id = 7;
    274  1.29.8.2  nathanw 
    275  1.29.8.2  nathanw 	/* gimme Mhz */
    276  1.29.8.2  nathanw 	sc->sc_freq /= 1000000;
    277  1.29.8.2  nathanw 
    278  1.29.8.2  nathanw 	/*
    279  1.29.8.2  nathanw 	 * It is necessary to try to load the 2nd config register here,
    280  1.29.8.2  nathanw 	 * to find out what rev the esp chip is, else the esp_reset
    281  1.29.8.2  nathanw 	 * will not set up the defaults correctly.
    282  1.29.8.2  nathanw 	 */
    283  1.29.8.2  nathanw 	sc->sc_cfg1 = sc->sc_id; /* | NCRCFG1_PARENB; */
    284  1.29.8.2  nathanw 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    285  1.29.8.2  nathanw 	sc->sc_cfg3 = 0;
    286  1.29.8.2  nathanw 	sc->sc_rev = NCR_VARIANT_NCR53C96;
    287  1.29.8.2  nathanw 
    288  1.29.8.2  nathanw 	/*
    289  1.29.8.2  nathanw 	 * This is the value used to start sync negotiations
    290  1.29.8.2  nathanw 	 * Note that the NCR register "SYNCTP" is programmed
    291  1.29.8.2  nathanw 	 * in "clocks per byte", and has a minimum value of 4.
    292  1.29.8.2  nathanw 	 * The SCSI period used in negotiation is one-fourth
    293  1.29.8.2  nathanw 	 * of the time (in nanoseconds) needed to transfer one byte.
    294  1.29.8.2  nathanw 	 * Since the chip's clock is given in MHz, we have the following
    295  1.29.8.2  nathanw 	 * formula: 4 * period = (1000 / freq) * 4
    296  1.29.8.2  nathanw 	 */
    297  1.29.8.2  nathanw 	sc->sc_minsync = 1000 / sc->sc_freq;
    298  1.29.8.2  nathanw 
    299  1.29.8.2  nathanw 	/* We need this to fit into the TCR... */
    300  1.29.8.2  nathanw 	sc->sc_maxxfer = 64 * 1024;
    301  1.29.8.2  nathanw 
    302  1.29.8.2  nathanw 	if (!quick) {
    303  1.29.8.2  nathanw 		sc->sc_minsync = 0;	/* No synchronous xfers w/o DMA */
    304  1.29.8.2  nathanw 		sc->sc_maxxfer = 8 * 1024;
    305  1.29.8.2  nathanw 	}
    306  1.29.8.2  nathanw 
    307  1.29.8.2  nathanw 	/*
    308  1.29.8.2  nathanw 	 * Configure interrupts.
    309  1.29.8.2  nathanw 	 */
    310  1.29.8.2  nathanw 	if (esc->irq_mask) {
    311  1.29.8.2  nathanw 		via2_reg(vPCR) = 0x22;
    312  1.29.8.2  nathanw 		via2_reg(vIFR) = esc->irq_mask;
    313  1.29.8.2  nathanw 		via2_reg(vIER) = 0x80 | esc->irq_mask;
    314  1.29.8.2  nathanw 	}
    315  1.29.8.2  nathanw 
    316  1.29.8.2  nathanw 	/*
    317  1.29.8.2  nathanw 	 * Now try to attach all the sub-devices
    318  1.29.8.2  nathanw 	 */
    319  1.29.8.2  nathanw 	sc->sc_adapter.adapt_minphys = minphys;
    320  1.29.8.2  nathanw 	sc->sc_adapter.adapt_request = ncr53c9x_scsipi_request;
    321  1.29.8.2  nathanw 	ncr53c9x_attach(sc);
    322  1.29.8.2  nathanw }
    323  1.29.8.2  nathanw 
    324  1.29.8.2  nathanw /*
    325  1.29.8.2  nathanw  * Glue functions.
    326  1.29.8.2  nathanw  */
    327  1.29.8.2  nathanw 
    328  1.29.8.2  nathanw u_char
    329  1.29.8.2  nathanw esp_read_reg(sc, reg)
    330  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    331  1.29.8.2  nathanw 	int reg;
    332  1.29.8.2  nathanw {
    333  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    334  1.29.8.2  nathanw 
    335  1.29.8.2  nathanw 	return esc->sc_reg[reg * 16];
    336  1.29.8.2  nathanw }
    337  1.29.8.2  nathanw 
    338  1.29.8.2  nathanw void
    339  1.29.8.2  nathanw esp_write_reg(sc, reg, val)
    340  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    341  1.29.8.2  nathanw 	int reg;
    342  1.29.8.2  nathanw 	u_char val;
    343  1.29.8.2  nathanw {
    344  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    345  1.29.8.2  nathanw 	u_char	v = val;
    346  1.29.8.2  nathanw 
    347  1.29.8.2  nathanw 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA)) {
    348  1.29.8.2  nathanw 		v = NCRCMD_TRANS;
    349  1.29.8.2  nathanw 	}
    350  1.29.8.2  nathanw 	esc->sc_reg[reg * 16] = v;
    351  1.29.8.2  nathanw }
    352  1.29.8.2  nathanw 
    353  1.29.8.2  nathanw void
    354  1.29.8.2  nathanw esp_dma_stop(sc)
    355  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    356  1.29.8.2  nathanw {
    357  1.29.8.2  nathanw }
    358  1.29.8.2  nathanw 
    359  1.29.8.2  nathanw int
    360  1.29.8.2  nathanw esp_dma_isactive(sc)
    361  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    362  1.29.8.2  nathanw {
    363  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    364  1.29.8.2  nathanw 
    365  1.29.8.2  nathanw 	return esc->sc_active;
    366  1.29.8.2  nathanw }
    367  1.29.8.2  nathanw 
    368  1.29.8.2  nathanw int
    369  1.29.8.2  nathanw esp_dma_isintr(sc)
    370  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    371  1.29.8.2  nathanw {
    372  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    373  1.29.8.2  nathanw 
    374  1.29.8.2  nathanw 	return esc->sc_reg[NCR_STAT * 16] & 0x80;
    375  1.29.8.2  nathanw }
    376  1.29.8.2  nathanw 
    377  1.29.8.2  nathanw void
    378  1.29.8.2  nathanw esp_dma_reset(sc)
    379  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    380  1.29.8.2  nathanw {
    381  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    382  1.29.8.2  nathanw 
    383  1.29.8.2  nathanw 	esc->sc_active = 0;
    384  1.29.8.2  nathanw 	esc->sc_tc = 0;
    385  1.29.8.2  nathanw }
    386  1.29.8.2  nathanw 
    387  1.29.8.2  nathanw int
    388  1.29.8.2  nathanw esp_dma_intr(sc)
    389  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    390  1.29.8.2  nathanw {
    391  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    392  1.29.8.2  nathanw 	volatile u_char *cmdreg, *intrreg, *statreg, *fiforeg;
    393  1.29.8.2  nathanw 	u_char	*p;
    394  1.29.8.2  nathanw 	u_int	espphase, espstat, espintr;
    395  1.29.8.2  nathanw 	int	cnt, s;
    396  1.29.8.2  nathanw 
    397  1.29.8.2  nathanw 	if (esc->sc_active == 0) {
    398  1.29.8.2  nathanw 		printf("dma_intr--inactive DMA\n");
    399  1.29.8.2  nathanw 		return -1;
    400  1.29.8.2  nathanw 	}
    401  1.29.8.2  nathanw 
    402  1.29.8.2  nathanw 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    403  1.29.8.2  nathanw 		esc->sc_active = 0;
    404  1.29.8.2  nathanw 		return 0;
    405  1.29.8.2  nathanw 	}
    406  1.29.8.2  nathanw 
    407  1.29.8.2  nathanw 	cnt = *esc->sc_dmalen;
    408  1.29.8.2  nathanw 	if (*esc->sc_dmalen == 0) {
    409  1.29.8.2  nathanw 		printf("data interrupt, but no count left.");
    410  1.29.8.2  nathanw 	}
    411  1.29.8.2  nathanw 
    412  1.29.8.2  nathanw 	p = *esc->sc_dmaaddr;
    413  1.29.8.2  nathanw 	espphase = sc->sc_phase;
    414  1.29.8.2  nathanw 	espstat = (u_int) sc->sc_espstat;
    415  1.29.8.2  nathanw 	espintr = (u_int) sc->sc_espintr;
    416  1.29.8.2  nathanw 	cmdreg = esc->sc_reg + NCR_CMD * 16;
    417  1.29.8.2  nathanw 	fiforeg = esc->sc_reg + NCR_FIFO * 16;
    418  1.29.8.2  nathanw 	statreg = esc->sc_reg + NCR_STAT * 16;
    419  1.29.8.2  nathanw 	intrreg = esc->sc_reg + NCR_INTR * 16;
    420  1.29.8.2  nathanw 	do {
    421  1.29.8.2  nathanw 		if (esc->sc_datain) {
    422  1.29.8.2  nathanw 			*p++ = *fiforeg;
    423  1.29.8.2  nathanw 			cnt--;
    424  1.29.8.2  nathanw 			if (espphase == DATA_IN_PHASE) {
    425  1.29.8.2  nathanw 				*cmdreg = NCRCMD_TRANS;
    426  1.29.8.2  nathanw 			} else {
    427  1.29.8.2  nathanw 				esc->sc_active = 0;
    428  1.29.8.2  nathanw 			}
    429  1.29.8.2  nathanw 	 	} else {
    430  1.29.8.2  nathanw 			if (   (espphase == DATA_OUT_PHASE)
    431  1.29.8.2  nathanw 			    || (espphase == MESSAGE_OUT_PHASE)) {
    432  1.29.8.2  nathanw 				*fiforeg = *p++;
    433  1.29.8.2  nathanw 				cnt--;
    434  1.29.8.2  nathanw 				*cmdreg = NCRCMD_TRANS;
    435  1.29.8.2  nathanw 			} else {
    436  1.29.8.2  nathanw 				esc->sc_active = 0;
    437  1.29.8.2  nathanw 			}
    438  1.29.8.2  nathanw 		}
    439  1.29.8.2  nathanw 
    440  1.29.8.2  nathanw 		if (esc->sc_active) {
    441  1.29.8.2  nathanw 			while (!(*statreg & 0x80));
    442  1.29.8.2  nathanw 			s = splhigh();
    443  1.29.8.2  nathanw 			espstat = *statreg;
    444  1.29.8.2  nathanw 			espintr = *intrreg;
    445  1.29.8.2  nathanw 			espphase = (espintr & NCRINTR_DIS)
    446  1.29.8.2  nathanw 				    ? /* Disconnected */ BUSFREE_PHASE
    447  1.29.8.2  nathanw 				    : espstat & PHASE_MASK;
    448  1.29.8.2  nathanw 			splx(s);
    449  1.29.8.2  nathanw 		}
    450  1.29.8.2  nathanw 	} while (esc->sc_active && (espintr & NCRINTR_BS));
    451  1.29.8.2  nathanw 	sc->sc_phase = espphase;
    452  1.29.8.2  nathanw 	sc->sc_espstat = (u_char) espstat;
    453  1.29.8.2  nathanw 	sc->sc_espintr = (u_char) espintr;
    454  1.29.8.2  nathanw 	*esc->sc_dmaaddr = p;
    455  1.29.8.2  nathanw 	*esc->sc_dmalen = cnt;
    456  1.29.8.2  nathanw 
    457  1.29.8.2  nathanw 	if (*esc->sc_dmalen == 0) {
    458  1.29.8.2  nathanw 		esc->sc_tc = NCRSTAT_TC;
    459  1.29.8.2  nathanw 	}
    460  1.29.8.2  nathanw 	sc->sc_espstat |= esc->sc_tc;
    461  1.29.8.2  nathanw 	return 0;
    462  1.29.8.2  nathanw }
    463  1.29.8.2  nathanw 
    464  1.29.8.2  nathanw int
    465  1.29.8.2  nathanw esp_dma_setup(sc, addr, len, datain, dmasize)
    466  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    467  1.29.8.2  nathanw 	caddr_t *addr;
    468  1.29.8.2  nathanw 	size_t *len;
    469  1.29.8.2  nathanw 	int datain;
    470  1.29.8.2  nathanw 	size_t *dmasize;
    471  1.29.8.2  nathanw {
    472  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    473  1.29.8.2  nathanw 
    474  1.29.8.2  nathanw 	esc->sc_dmaaddr = addr;
    475  1.29.8.2  nathanw 	esc->sc_dmalen = len;
    476  1.29.8.2  nathanw 	esc->sc_datain = datain;
    477  1.29.8.2  nathanw 	esc->sc_dmasize = *dmasize;
    478  1.29.8.2  nathanw 	esc->sc_tc = 0;
    479  1.29.8.2  nathanw 
    480  1.29.8.2  nathanw 	return 0;
    481  1.29.8.2  nathanw }
    482  1.29.8.2  nathanw 
    483  1.29.8.2  nathanw void
    484  1.29.8.2  nathanw esp_dma_go(sc)
    485  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    486  1.29.8.2  nathanw {
    487  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    488  1.29.8.2  nathanw 
    489  1.29.8.2  nathanw 	if (esc->sc_datain == 0) {
    490  1.29.8.2  nathanw 		esc->sc_reg[NCR_FIFO * 16] = **esc->sc_dmaaddr;
    491  1.29.8.2  nathanw 		(*esc->sc_dmalen)--;
    492  1.29.8.2  nathanw 		(*esc->sc_dmaaddr)++;
    493  1.29.8.2  nathanw 	}
    494  1.29.8.2  nathanw 	esc->sc_active = 1;
    495  1.29.8.2  nathanw }
    496  1.29.8.2  nathanw 
    497  1.29.8.2  nathanw void
    498  1.29.8.2  nathanw esp_quick_write_reg(sc, reg, val)
    499  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    500  1.29.8.2  nathanw 	int reg;
    501  1.29.8.2  nathanw 	u_char val;
    502  1.29.8.2  nathanw {
    503  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    504  1.29.8.2  nathanw 
    505  1.29.8.2  nathanw 	esc->sc_reg[reg * 16] = val;
    506  1.29.8.2  nathanw }
    507  1.29.8.2  nathanw 
    508  1.29.8.2  nathanw #if DEBUG
    509  1.29.8.2  nathanw int mac68k_esp_debug=0;
    510  1.29.8.2  nathanw #endif
    511  1.29.8.2  nathanw 
    512  1.29.8.2  nathanw int
    513  1.29.8.2  nathanw esp_quick_dma_intr(sc)
    514  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    515  1.29.8.2  nathanw {
    516  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    517  1.29.8.2  nathanw 	int trans=0, resid=0;
    518  1.29.8.2  nathanw 
    519  1.29.8.2  nathanw 	if (esc->sc_active == 0)
    520  1.29.8.2  nathanw 		panic("dma_intr--inactive DMA\n");
    521  1.29.8.2  nathanw 
    522  1.29.8.2  nathanw 	esc->sc_active = 0;
    523  1.29.8.2  nathanw 
    524  1.29.8.2  nathanw 	if (esc->sc_dmasize == 0) {
    525  1.29.8.2  nathanw 		int	res;
    526  1.29.8.2  nathanw 
    527  1.29.8.2  nathanw 		res = NCR_READ_REG(sc, NCR_TCL);
    528  1.29.8.2  nathanw 		res += NCR_READ_REG(sc, NCR_TCM) << 8;
    529  1.29.8.2  nathanw 		/* This can happen in the case of a TRPAD operation */
    530  1.29.8.2  nathanw 		/* Pretend that it was complete */
    531  1.29.8.2  nathanw 		sc->sc_espstat |= NCRSTAT_TC;
    532  1.29.8.2  nathanw #if DEBUG
    533  1.29.8.2  nathanw 		if (mac68k_esp_debug) {
    534  1.29.8.2  nathanw 			printf("dmaintr: DMA xfer of zero xferred %d\n",
    535  1.29.8.2  nathanw 			    65536 - res);
    536  1.29.8.2  nathanw 		}
    537  1.29.8.2  nathanw #endif
    538  1.29.8.2  nathanw 		return 0;
    539  1.29.8.2  nathanw 	}
    540  1.29.8.2  nathanw 
    541  1.29.8.2  nathanw 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    542  1.29.8.2  nathanw 		if (esc->sc_datain == 0) {
    543  1.29.8.2  nathanw 			resid = NCR_READ_REG(sc, NCR_FFLAG) & 0x1f;
    544  1.29.8.2  nathanw #if DEBUG
    545  1.29.8.2  nathanw 			if (mac68k_esp_debug) {
    546  1.29.8.2  nathanw 				printf("Write FIFO residual %d bytes\n", resid);
    547  1.29.8.2  nathanw 			}
    548  1.29.8.2  nathanw #endif
    549  1.29.8.2  nathanw 		}
    550  1.29.8.2  nathanw 		resid += NCR_READ_REG(sc, NCR_TCL);
    551  1.29.8.2  nathanw 		resid += NCR_READ_REG(sc, NCR_TCM) << 8;
    552  1.29.8.2  nathanw 		if (resid == 0)
    553  1.29.8.2  nathanw 			resid = 65536;
    554  1.29.8.2  nathanw 	}
    555  1.29.8.2  nathanw 
    556  1.29.8.2  nathanw 	trans = esc->sc_dmasize - resid;
    557  1.29.8.2  nathanw 	if (trans < 0) {
    558  1.29.8.2  nathanw 		printf("dmaintr: trans < 0????");
    559  1.29.8.2  nathanw 		trans = *esc->sc_dmalen;
    560  1.29.8.2  nathanw 	}
    561  1.29.8.2  nathanw 
    562  1.29.8.2  nathanw 	NCR_DMA(("dmaintr: trans %d, resid %d.\n", trans, resid));
    563  1.29.8.2  nathanw #if DEBUG
    564  1.29.8.2  nathanw 	if (mac68k_esp_debug) {
    565  1.29.8.2  nathanw 		printf("eqd_intr: trans %d, resid %d.\n", trans, resid);
    566  1.29.8.2  nathanw 	}
    567  1.29.8.2  nathanw #endif
    568  1.29.8.2  nathanw 	*esc->sc_dmaaddr += trans;
    569  1.29.8.2  nathanw 	*esc->sc_dmalen -= trans;
    570  1.29.8.2  nathanw 
    571  1.29.8.2  nathanw 	return 0;
    572  1.29.8.2  nathanw }
    573  1.29.8.2  nathanw 
    574  1.29.8.2  nathanw int
    575  1.29.8.2  nathanw esp_quick_dma_setup(sc, addr, len, datain, dmasize)
    576  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    577  1.29.8.2  nathanw 	caddr_t *addr;
    578  1.29.8.2  nathanw 	size_t *len;
    579  1.29.8.2  nathanw 	int datain;
    580  1.29.8.2  nathanw 	size_t *dmasize;
    581  1.29.8.2  nathanw {
    582  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    583  1.29.8.2  nathanw 
    584  1.29.8.2  nathanw 	esc->sc_dmaaddr = addr;
    585  1.29.8.2  nathanw 	esc->sc_dmalen = len;
    586  1.29.8.2  nathanw 
    587  1.29.8.2  nathanw 	if (*len & 1) {
    588  1.29.8.2  nathanw 		esc->sc_pad = 1;
    589  1.29.8.2  nathanw 	} else {
    590  1.29.8.2  nathanw 		esc->sc_pad = 0;
    591  1.29.8.2  nathanw 	}
    592  1.29.8.2  nathanw 
    593  1.29.8.2  nathanw 	esc->sc_datain = datain;
    594  1.29.8.2  nathanw 	esc->sc_dmasize = *dmasize;
    595  1.29.8.2  nathanw 
    596  1.29.8.2  nathanw #if DIAGNOSTIC
    597  1.29.8.2  nathanw 	if (esc->sc_dmasize == 0) {
    598  1.29.8.2  nathanw 		/* This can happen in the case of a TRPAD operation */
    599  1.29.8.2  nathanw 	}
    600  1.29.8.2  nathanw #endif
    601  1.29.8.2  nathanw #if DEBUG
    602  1.29.8.2  nathanw 	if (mac68k_esp_debug) {
    603  1.29.8.2  nathanw 	printf("eqd_setup: addr %lx, len %lx, in? %d, dmasize %lx\n",
    604  1.29.8.2  nathanw 	    (long) *addr, (long) *len, datain, (long) esc->sc_dmasize);
    605  1.29.8.2  nathanw 	}
    606  1.29.8.2  nathanw #endif
    607  1.29.8.2  nathanw 
    608  1.29.8.2  nathanw 	return 0;
    609  1.29.8.2  nathanw }
    610  1.29.8.2  nathanw 
    611  1.29.8.2  nathanw static __inline__ int
    612  1.29.8.2  nathanw esp_dafb_have_dreq(esc)
    613  1.29.8.2  nathanw 	struct esp_softc *esc;
    614  1.29.8.2  nathanw {
    615  1.29.8.2  nathanw 	return (*(volatile u_int32_t *)(esc->sc_bsh.base) & 0x200);
    616  1.29.8.2  nathanw }
    617  1.29.8.2  nathanw 
    618  1.29.8.2  nathanw static __inline__ int
    619  1.29.8.2  nathanw esp_iosb_have_dreq(esc)
    620  1.29.8.2  nathanw 	struct esp_softc *esc;
    621  1.29.8.2  nathanw {
    622  1.29.8.2  nathanw 	return (via2_reg(vIFR) & V2IF_SCSIDRQ);
    623  1.29.8.2  nathanw }
    624  1.29.8.2  nathanw 
    625  1.29.8.2  nathanw static volatile int espspl=-1;
    626  1.29.8.2  nathanw 
    627  1.29.8.2  nathanw /*
    628  1.29.8.2  nathanw  * Apple "DMA" is weird.
    629  1.29.8.2  nathanw  *
    630  1.29.8.2  nathanw  * Basically, the CPU acts like the DMA controller.  The DREQ/ off the
    631  1.29.8.2  nathanw  * chip goes to a register that we've mapped at attach time (on the
    632  1.29.8.2  nathanw  * IOSB or DAFB, depending on the machine).  Apple also provides some
    633  1.29.8.2  nathanw  * space for which the memory controller handshakes data to/from the
    634  1.29.8.2  nathanw  * NCR chip with the DACK/ line.  This space appears to be mapped over
    635  1.29.8.2  nathanw  * and over, every 4 bytes, but only the lower 16 bits are valid (but
    636  1.29.8.2  nathanw  * reading the upper 16 bits will handshake DACK/ just fine, so if you
    637  1.29.8.2  nathanw  * read *u_int16_t++ = *u_int16_t++ in a loop, you'll get
    638  1.29.8.2  nathanw  * <databyte><databyte>0xff0xff<databyte><databyte>0xff0xff...
    639  1.29.8.2  nathanw  *
    640  1.29.8.2  nathanw  * When you're attempting to read or write memory to this DACK/ed space,
    641  1.29.8.2  nathanw  * and the NCR is not ready for some timeout period, the system will
    642  1.29.8.2  nathanw  * generate a bus error.  This might be for one of several reasons:
    643  1.29.8.2  nathanw  *
    644  1.29.8.2  nathanw  *	1) (on write) The FIFO is full and is not draining.
    645  1.29.8.2  nathanw  *	2) (on read) The FIFO is empty and is not filling.
    646  1.29.8.2  nathanw  *	3) An interrupt condition has occurred.
    647  1.29.8.2  nathanw  *	4) Anything else?
    648  1.29.8.2  nathanw  *
    649  1.29.8.2  nathanw  * So if a bus error occurs, we first turn off the nofault bus error handler,
    650  1.29.8.2  nathanw  * then we check for an interrupt (which would render the first two
    651  1.29.8.2  nathanw  * possibilities moot).  If there's no interrupt, check for a DREQ/.  If we
    652  1.29.8.2  nathanw  * have that, then attempt to resume stuffing (or unstuffing) the FIFO.  If
    653  1.29.8.2  nathanw  * neither condition holds, pause briefly and check again.
    654  1.29.8.2  nathanw  *
    655  1.29.8.2  nathanw  * NOTE!!!  In order to make allowances for the hardware structure of
    656  1.29.8.2  nathanw  *          the mac, spl values in here are hardcoded!!!!!!!!!
    657  1.29.8.2  nathanw  *          This is done to allow serial interrupts to get in during
    658  1.29.8.2  nathanw  *          scsi transfers.  This is ugly.
    659  1.29.8.2  nathanw  */
    660  1.29.8.2  nathanw void
    661  1.29.8.2  nathanw esp_quick_dma_go(sc)
    662  1.29.8.2  nathanw 	struct ncr53c9x_softc *sc;
    663  1.29.8.2  nathanw {
    664  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    665  1.29.8.2  nathanw 	extern long mac68k_a2_fromfault;
    666  1.29.8.2  nathanw 	extern int *nofault;
    667  1.29.8.2  nathanw 	label_t faultbuf;
    668  1.29.8.2  nathanw 	u_int16_t volatile *pdma;
    669  1.29.8.2  nathanw 	u_int16_t *addr;
    670  1.29.8.2  nathanw 	int		len, res;
    671  1.29.8.2  nathanw 	u_short		cnt32, cnt2;
    672  1.29.8.2  nathanw 	u_char volatile *statreg;
    673  1.29.8.2  nathanw 
    674  1.29.8.2  nathanw 	esc->sc_active = 1;
    675  1.29.8.2  nathanw 
    676  1.29.8.2  nathanw 	espspl = splhigh();
    677  1.29.8.2  nathanw 
    678  1.29.8.2  nathanw 	addr = (u_int16_t *) *esc->sc_dmaaddr;
    679  1.29.8.2  nathanw 	len  = esc->sc_dmasize;
    680  1.29.8.2  nathanw 
    681  1.29.8.2  nathanw restart_dmago:
    682  1.29.8.2  nathanw #if DEBUG
    683  1.29.8.2  nathanw 	if (mac68k_esp_debug) {
    684  1.29.8.2  nathanw 		printf("eqdg: a %lx, l %lx, in? %d ... ",
    685  1.29.8.2  nathanw 		    (long) addr, (long) len, esc->sc_datain);
    686  1.29.8.2  nathanw 	}
    687  1.29.8.2  nathanw #endif
    688  1.29.8.2  nathanw 	nofault = (int *) &faultbuf;
    689  1.29.8.2  nathanw 	if (setjmp((label_t *) nofault)) {
    690  1.29.8.2  nathanw 		int	i=0;
    691  1.29.8.2  nathanw 
    692  1.29.8.2  nathanw 		nofault = (int *) 0;
    693  1.29.8.2  nathanw #if DEBUG
    694  1.29.8.2  nathanw 		if (mac68k_esp_debug) {
    695  1.29.8.2  nathanw 			printf("be\n");
    696  1.29.8.2  nathanw 		}
    697  1.29.8.2  nathanw #endif
    698  1.29.8.2  nathanw 		/*
    699  1.29.8.2  nathanw 		 * Bus error...
    700  1.29.8.2  nathanw 		 * So, we first check for an interrupt.  If we have
    701  1.29.8.2  nathanw 		 * one, go handle it.  Next we check for DREQ/.  If
    702  1.29.8.2  nathanw 		 * we have it, then we restart the transfer.  If
    703  1.29.8.2  nathanw 		 * neither, then loop until we get one or the other.
    704  1.29.8.2  nathanw 		 */
    705  1.29.8.2  nathanw 		statreg = esc->sc_reg + NCR_STAT * 16;
    706  1.29.8.2  nathanw 		for (;;) {
    707  1.29.8.2  nathanw 			spl2();		/* Give serial a chance... */
    708  1.29.8.2  nathanw 			splhigh();	/* That's enough... */
    709  1.29.8.2  nathanw 
    710  1.29.8.2  nathanw 			if (*statreg & 0x80) {
    711  1.29.8.2  nathanw 				goto gotintr;
    712  1.29.8.2  nathanw 			}
    713  1.29.8.2  nathanw 
    714  1.29.8.2  nathanw 			if (esp_have_dreq(esc)) {
    715  1.29.8.2  nathanw 				/*
    716  1.29.8.2  nathanw 				 * Get the remaining length from the address
    717  1.29.8.2  nathanw 				 * differential.
    718  1.29.8.2  nathanw 				 */
    719  1.29.8.2  nathanw 				addr = (u_int16_t *) mac68k_a2_fromfault;
    720  1.29.8.2  nathanw 				len = esc->sc_dmasize -
    721  1.29.8.2  nathanw 				    ((long) addr - (long) *esc->sc_dmaaddr);
    722  1.29.8.2  nathanw 
    723  1.29.8.2  nathanw 				if (esc->sc_datain == 0) {
    724  1.29.8.2  nathanw 					/*
    725  1.29.8.2  nathanw 					 * Let the FIFO drain before we read
    726  1.29.8.2  nathanw 					 * the transfer count.
    727  1.29.8.2  nathanw 					 * Do we need to do this?
    728  1.29.8.2  nathanw 					 * Can we do this?
    729  1.29.8.2  nathanw 					 */
    730  1.29.8.2  nathanw 					while (NCR_READ_REG(sc, NCR_FFLAG)
    731  1.29.8.2  nathanw 					    & 0x1f);
    732  1.29.8.2  nathanw 					/*
    733  1.29.8.2  nathanw 					 * Get the length from the transfer
    734  1.29.8.2  nathanw 					 * counters.
    735  1.29.8.2  nathanw 					 */
    736  1.29.8.2  nathanw 					res = NCR_READ_REG(sc, NCR_TCL);
    737  1.29.8.2  nathanw 					res += NCR_READ_REG(sc, NCR_TCM) << 8;
    738  1.29.8.2  nathanw 					/*
    739  1.29.8.2  nathanw 					 * If they don't agree,
    740  1.29.8.2  nathanw 					 * adjust accordingly.
    741  1.29.8.2  nathanw 					 */
    742  1.29.8.2  nathanw 					while (res > len) {
    743  1.29.8.2  nathanw 						len+=2; addr--;
    744  1.29.8.2  nathanw 					}
    745  1.29.8.2  nathanw 					if (res != len) {
    746  1.29.8.2  nathanw 						panic("esp_quick_dma_go: res %d != len %d\n",
    747  1.29.8.2  nathanw 							res, len);
    748  1.29.8.2  nathanw 					}
    749  1.29.8.2  nathanw 				}
    750  1.29.8.2  nathanw 				break;
    751  1.29.8.2  nathanw 			}
    752  1.29.8.2  nathanw 
    753  1.29.8.2  nathanw 			DELAY(1);
    754  1.29.8.2  nathanw 			if (i++ > 1000000)
    755  1.29.8.2  nathanw 				panic("esp_dma_go: Bus error, but no condition!  Argh!");
    756  1.29.8.2  nathanw 		}
    757  1.29.8.2  nathanw 		goto restart_dmago;
    758  1.29.8.2  nathanw 	}
    759  1.29.8.2  nathanw 
    760  1.29.8.2  nathanw 	len &= ~1;
    761  1.29.8.2  nathanw 
    762  1.29.8.2  nathanw 	statreg = esc->sc_reg + NCR_STAT * 16;
    763  1.29.8.2  nathanw 	pdma = (u_int16_t *) (esc->sc_reg + 0x100);
    764  1.29.8.2  nathanw 
    765  1.29.8.2  nathanw 	/*
    766  1.29.8.2  nathanw 	 * These loops are unrolled into assembly for two reasons:
    767  1.29.8.2  nathanw 	 * 1) We can make sure that they are as efficient as possible, and
    768  1.29.8.2  nathanw 	 * 2) (more importantly) we need the address that we are reading
    769  1.29.8.2  nathanw 	 *    from or writing to to be in a2.
    770  1.29.8.2  nathanw 	 */
    771  1.29.8.2  nathanw 	cnt32 = len / 32;
    772  1.29.8.2  nathanw 	cnt2 = (len % 32) / 2;
    773  1.29.8.2  nathanw 	if (esc->sc_datain == 0) {
    774  1.29.8.2  nathanw 		/* while (cnt32--) { 16 instances of *pdma = *addr++; } */
    775  1.29.8.2  nathanw 		/* while (cnt2--) { *pdma = *addr++; } */
    776  1.29.8.2  nathanw 		__asm __volatile ("
    777  1.29.8.2  nathanw 				movl %1, %%a2
    778  1.29.8.2  nathanw 				movl %2, %%a3
    779  1.29.8.2  nathanw 				movw %3, %%d2
    780  1.29.8.2  nathanw 				cmpw #0, %%d2
    781  1.29.8.2  nathanw 				beq  2f
    782  1.29.8.2  nathanw 				subql #1, %%d2
    783  1.29.8.2  nathanw 			1:	movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    784  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    785  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    786  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    787  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    788  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    789  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    790  1.29.8.2  nathanw 				movw %%a2@+,%%a3@; movw %%a2@+,%%a3@
    791  1.29.8.2  nathanw 				movw #8704,%%sr
    792  1.29.8.2  nathanw 				movw #9728,%%sr
    793  1.29.8.2  nathanw 				dbra %%d2, 1b
    794  1.29.8.2  nathanw 			2:	movw %4, %%d2
    795  1.29.8.2  nathanw 				cmpw #0, %%d2
    796  1.29.8.2  nathanw 				beq  4f
    797  1.29.8.2  nathanw 				subql #1, %%d2
    798  1.29.8.2  nathanw 			3:	movw %%a2@+,%%a3@
    799  1.29.8.2  nathanw 				dbra %%d2, 3b
    800  1.29.8.2  nathanw 			4:	movl %%a2, %0"
    801  1.29.8.2  nathanw 			: "=g" (addr)
    802  1.29.8.2  nathanw 			: "0" (addr), "g" (pdma), "g" (cnt32), "g" (cnt2)
    803  1.29.8.2  nathanw 			: "a2", "a3", "d2");
    804  1.29.8.2  nathanw 		if (esc->sc_pad) {
    805  1.29.8.2  nathanw 			unsigned char	*c;
    806  1.29.8.2  nathanw 			c = (unsigned char *) addr;
    807  1.29.8.2  nathanw 			/* Wait for DREQ */
    808  1.29.8.2  nathanw 			while (!esp_have_dreq(esc)) {
    809  1.29.8.2  nathanw 				if (*statreg & 0x80) {
    810  1.29.8.2  nathanw 					nofault = (int *) 0;
    811  1.29.8.2  nathanw 					goto gotintr;
    812  1.29.8.2  nathanw 				}
    813  1.29.8.2  nathanw 			}
    814  1.29.8.2  nathanw 			*(unsigned char *)pdma = *c;
    815  1.29.8.2  nathanw 		}
    816  1.29.8.2  nathanw 	} else {
    817  1.29.8.2  nathanw 		/* while (cnt32--) { 16 instances of *addr++ = *pdma; } */
    818  1.29.8.2  nathanw 		/* while (cnt2--) { *addr++ = *pdma; } */
    819  1.29.8.2  nathanw 		__asm __volatile ("
    820  1.29.8.2  nathanw 				movl %1, %%a2
    821  1.29.8.2  nathanw 				movl %2, %%a3
    822  1.29.8.2  nathanw 				movw %3, %%d2
    823  1.29.8.2  nathanw 				cmpw #0, %%d2
    824  1.29.8.2  nathanw 				beq  6f
    825  1.29.8.2  nathanw 				subql #1, %%d2
    826  1.29.8.2  nathanw 			5:	movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    827  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    828  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    829  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    830  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    831  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    832  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    833  1.29.8.2  nathanw 				movw %%a3@,%%a2@+; movw %%a3@,%%a2@+
    834  1.29.8.2  nathanw 				movw #8704,%%sr
    835  1.29.8.2  nathanw 				movw #9728,%%sr
    836  1.29.8.2  nathanw 				dbra %%d2, 5b
    837  1.29.8.2  nathanw 			6:	movw %4, %%d2
    838  1.29.8.2  nathanw 				cmpw #0, %%d2
    839  1.29.8.2  nathanw 				beq  8f
    840  1.29.8.2  nathanw 				subql #1, %%d2
    841  1.29.8.2  nathanw 			7:	movw %%a3@,%%a2@+
    842  1.29.8.2  nathanw 				dbra %%d2, 7b
    843  1.29.8.2  nathanw 			8:	movl %%a2, %0"
    844  1.29.8.2  nathanw 			: "=g" (addr)
    845  1.29.8.2  nathanw 			: "0" (addr), "g" (pdma), "g" (cnt32), "g" (cnt2)
    846  1.29.8.2  nathanw 			: "a2", "a3", "d2");
    847  1.29.8.2  nathanw 		if (esc->sc_pad) {
    848  1.29.8.2  nathanw 			unsigned char	*c;
    849  1.29.8.2  nathanw 			c = (unsigned char *) addr;
    850  1.29.8.2  nathanw 			/* Wait for DREQ */
    851  1.29.8.2  nathanw 			while (!esp_have_dreq(esc)) {
    852  1.29.8.2  nathanw 				if (*statreg & 0x80) {
    853  1.29.8.2  nathanw 					nofault = (int *) 0;
    854  1.29.8.2  nathanw 					goto gotintr;
    855  1.29.8.2  nathanw 				}
    856  1.29.8.2  nathanw 			}
    857  1.29.8.2  nathanw 			*c = *(unsigned char *)pdma;
    858  1.29.8.2  nathanw 		}
    859  1.29.8.2  nathanw 	}
    860  1.29.8.2  nathanw 
    861  1.29.8.2  nathanw 	nofault = (int *) 0;
    862  1.29.8.2  nathanw 
    863  1.29.8.2  nathanw 	/*
    864  1.29.8.2  nathanw 	 * If we have not received an interrupt yet, we should shortly,
    865  1.29.8.2  nathanw 	 * and we can't prevent it, so return and wait for it.
    866  1.29.8.2  nathanw 	 */
    867  1.29.8.2  nathanw 	if ((*statreg & 0x80) == 0) {
    868  1.29.8.2  nathanw #if DEBUG
    869  1.29.8.2  nathanw 		if (mac68k_esp_debug) {
    870  1.29.8.2  nathanw 			printf("g.\n");
    871  1.29.8.2  nathanw 		}
    872  1.29.8.2  nathanw #endif
    873  1.29.8.2  nathanw 		if (espspl != -1) splx(espspl); espspl = -1;
    874  1.29.8.2  nathanw 		return;
    875  1.29.8.2  nathanw 	}
    876  1.29.8.2  nathanw 
    877  1.29.8.2  nathanw gotintr:
    878  1.29.8.2  nathanw #if DEBUG
    879  1.29.8.2  nathanw 	if (mac68k_esp_debug) {
    880  1.29.8.2  nathanw 		printf("g!\n");
    881  1.29.8.2  nathanw 	}
    882  1.29.8.2  nathanw #endif
    883  1.29.8.2  nathanw 	ncr53c9x_intr(sc);
    884  1.29.8.2  nathanw 	if (espspl != -1) splx(espspl); espspl = -1;
    885  1.29.8.2  nathanw }
    886  1.29.8.2  nathanw 
    887  1.29.8.2  nathanw void
    888  1.29.8.2  nathanw esp_intr(sc)
    889  1.29.8.2  nathanw 	void *sc;
    890  1.29.8.2  nathanw {
    891  1.29.8.2  nathanw 	struct esp_softc *esc = (struct esp_softc *)sc;
    892  1.29.8.2  nathanw 
    893  1.29.8.2  nathanw 	if (esc->sc_reg[NCR_STAT * 16] & 0x80) {
    894  1.29.8.2  nathanw 		ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
    895  1.29.8.2  nathanw 	}
    896  1.29.8.2  nathanw }
    897  1.29.8.2  nathanw 
    898  1.29.8.2  nathanw void
    899  1.29.8.2  nathanw esp_dualbus_intr(sc)
    900  1.29.8.2  nathanw 	void *sc;
    901  1.29.8.2  nathanw {
    902  1.29.8.2  nathanw 	if (esp0 && (esp0->sc_reg[NCR_STAT * 16] & 0x80)) {
    903  1.29.8.2  nathanw 		ncr53c9x_intr((struct ncr53c9x_softc *) esp0);
    904  1.29.8.2  nathanw 	}
    905  1.29.8.2  nathanw 
    906  1.29.8.2  nathanw 	if (esp1 && (esp1->sc_reg[NCR_STAT * 16] & 0x80)) {
    907  1.29.8.2  nathanw 		ncr53c9x_intr((struct ncr53c9x_softc *) esp1);
    908  1.29.8.2  nathanw 	}
    909  1.29.8.2  nathanw }
    910