Home | History | Annotate | Line # | Download | only in obio
esp.c revision 1.13
      1  1.13  briggs /*	$NetBSD: esp.c,v 1.13 1997/11/19 13:01:32 briggs Exp $	*/
      2   1.1  briggs 
      3   1.1  briggs /*
      4  1.10  briggs  * Copyright (c) 1997 Jason R. Thorpe.
      5  1.10  briggs  * All rights reserved.
      6   1.1  briggs  *
      7   1.1  briggs  * Redistribution and use in source and binary forms, with or without
      8   1.1  briggs  * modification, are permitted provided that the following conditions
      9   1.1  briggs  * are met:
     10   1.1  briggs  * 1. Redistributions of source code must retain the above copyright
     11   1.1  briggs  *    notice, this list of conditions and the following disclaimer.
     12   1.1  briggs  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1  briggs  *    notice, this list of conditions and the following disclaimer in the
     14   1.1  briggs  *    documentation and/or other materials provided with the distribution.
     15   1.1  briggs  * 3. All advertising materials mentioning features or use of this software
     16   1.1  briggs  *    must display the following acknowledgement:
     17  1.10  briggs  *	This product includes software developed for the NetBSD Project
     18  1.10  briggs  *	by Jason R. Thorpe.
     19   1.1  briggs  * 4. The name of the author may not be used to endorse or promote products
     20   1.1  briggs  *    derived from this software without specific prior written permission.
     21   1.1  briggs  *
     22   1.1  briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     23   1.1  briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     24   1.1  briggs  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     25   1.1  briggs  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     26   1.1  briggs  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     27   1.1  briggs  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     28   1.1  briggs  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     29   1.1  briggs  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     30   1.1  briggs  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     31   1.1  briggs  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     32   1.1  briggs  */
     33   1.1  briggs 
     34   1.1  briggs /*
     35   1.1  briggs  * Copyright (c) 1994 Peter Galbavy
     36   1.1  briggs  * Copyright (c) 1995 Paul Kranenburg
     37   1.1  briggs  * All rights reserved.
     38   1.1  briggs  *
     39   1.1  briggs  * Redistribution and use in source and binary forms, with or without
     40   1.1  briggs  * modification, are permitted provided that the following conditions
     41   1.1  briggs  * are met:
     42   1.1  briggs  * 1. Redistributions of source code must retain the above copyright
     43   1.1  briggs  *    notice, this list of conditions and the following disclaimer.
     44   1.1  briggs  * 2. Redistributions in binary form must reproduce the above copyright
     45   1.1  briggs  *    notice, this list of conditions and the following disclaimer in the
     46   1.1  briggs  *    documentation and/or other materials provided with the distribution.
     47   1.1  briggs  * 3. All advertising materials mentioning features or use of this software
     48   1.1  briggs  *    must display the following acknowledgement:
     49   1.1  briggs  *	This product includes software developed by Peter Galbavy
     50   1.1  briggs  * 4. The name of the author may not be used to endorse or promote products
     51   1.1  briggs  *    derived from this software without specific prior written permission.
     52   1.1  briggs  *
     53   1.1  briggs  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     54   1.1  briggs  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     55   1.1  briggs  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     56   1.1  briggs  * DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
     57   1.1  briggs  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
     58   1.1  briggs  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     59   1.1  briggs  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     60   1.1  briggs  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     61   1.1  briggs  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
     62   1.1  briggs  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     63   1.1  briggs  * POSSIBILITY OF SUCH DAMAGE.
     64   1.1  briggs  */
     65   1.1  briggs 
     66   1.1  briggs /*
     67   1.1  briggs  * Based on aic6360 by Jarle Greipsland
     68   1.1  briggs  *
     69   1.1  briggs  * Acknowledgements: Many of the algorithms used in this driver are
     70   1.1  briggs  * inspired by the work of Julian Elischer (julian (at) tfs.com) and
     71   1.1  briggs  * Charles Hannum (mycroft (at) duality.gnu.ai.mit.edu).  Thanks a million!
     72  1.10  briggs  */
     73  1.10  briggs 
     74  1.10  briggs /*
     75  1.10  briggs  * Initial m68k mac support from Allen Briggs <briggs (at) macbsd.com>
     76  1.10  briggs  * (basically consisting of the match, a bit of the attach, and the
     77  1.10  briggs  *  "DMA" glue functions).
     78   1.1  briggs  */
     79   1.1  briggs 
     80   1.1  briggs #include <sys/types.h>
     81   1.1  briggs #include <sys/param.h>
     82   1.1  briggs #include <sys/systm.h>
     83   1.1  briggs #include <sys/kernel.h>
     84   1.1  briggs #include <sys/errno.h>
     85   1.1  briggs #include <sys/ioctl.h>
     86   1.1  briggs #include <sys/device.h>
     87   1.1  briggs #include <sys/buf.h>
     88   1.1  briggs #include <sys/proc.h>
     89   1.1  briggs #include <sys/user.h>
     90   1.1  briggs #include <sys/queue.h>
     91   1.1  briggs 
     92  1.11  bouyer #include <dev/scsipi/scsi_all.h>
     93  1.11  bouyer #include <dev/scsipi/scsipi_all.h>
     94  1.11  bouyer #include <dev/scsipi/scsiconf.h>
     95  1.11  bouyer #include <dev/scsipi/scsi_message.h>
     96   1.1  briggs 
     97   1.1  briggs #include <machine/cpu.h>
     98  1.12  briggs #include <machine/bus.h>
     99   1.1  briggs #include <machine/param.h>
    100   1.1  briggs 
    101   1.7  briggs #include <dev/ic/ncr53c9xreg.h>
    102   1.7  briggs #include <dev/ic/ncr53c9xvar.h>
    103   1.7  briggs 
    104   1.1  briggs #include <machine/viareg.h>
    105   1.1  briggs 
    106   1.1  briggs #include <mac68k/dev/espvar.h>
    107  1.12  briggs #include <mac68k/dev/obiovar.h>
    108   1.3  briggs 
    109   1.7  briggs void	espattach	__P((struct device *, struct device *, void *));
    110   1.9  scottr int	espmatch	__P((struct device *, struct cfdata *, void *));
    111   1.1  briggs 
    112   1.1  briggs /* Linkup to the rest of the kernel */
    113   1.1  briggs struct cfattach esp_ca = {
    114   1.1  briggs 	sizeof(struct esp_softc), espmatch, espattach
    115   1.1  briggs };
    116   1.1  briggs 
    117   1.1  briggs struct cfdriver esp_cd = {
    118   1.1  briggs 	NULL, "esp", DV_DULL
    119   1.1  briggs };
    120   1.1  briggs 
    121  1.11  bouyer struct scsipi_adapter esp_switch = {
    122   1.7  briggs 	ncr53c9x_scsi_cmd,
    123   1.1  briggs 	minphys,		/* no max at this level; handled by DMA code */
    124   1.1  briggs 	NULL,
    125   1.1  briggs 	NULL,
    126   1.1  briggs };
    127   1.1  briggs 
    128  1.11  bouyer struct scsipi_device esp_dev = {
    129   1.1  briggs 	NULL,			/* Use default error handler */
    130   1.1  briggs 	NULL,			/* have a queue, served by this */
    131   1.1  briggs 	NULL,			/* have no async handler */
    132   1.1  briggs 	NULL,			/* Use default 'done' routine */
    133   1.1  briggs };
    134   1.1  briggs 
    135   1.7  briggs /*
    136   1.7  briggs  * Functions and the switch for the MI code.
    137   1.7  briggs  */
    138   1.7  briggs u_char	esp_read_reg __P((struct ncr53c9x_softc *, int));
    139   1.7  briggs void	esp_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    140   1.7  briggs int	esp_dma_isintr __P((struct ncr53c9x_softc *));
    141   1.7  briggs void	esp_dma_reset __P((struct ncr53c9x_softc *));
    142   1.7  briggs int	esp_dma_intr __P((struct ncr53c9x_softc *));
    143   1.7  briggs int	esp_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    144   1.7  briggs 	    size_t *, int, size_t *));
    145   1.7  briggs void	esp_dma_go __P((struct ncr53c9x_softc *));
    146   1.7  briggs void	esp_dma_stop __P((struct ncr53c9x_softc *));
    147   1.7  briggs int	esp_dma_isactive __P((struct ncr53c9x_softc *));
    148  1.12  briggs void	esp_quick_write_reg __P((struct ncr53c9x_softc *, int, u_char));
    149  1.12  briggs int	esp_quick_dma_intr __P((struct ncr53c9x_softc *));
    150  1.12  briggs int	esp_quick_dma_setup __P((struct ncr53c9x_softc *, caddr_t *,
    151  1.12  briggs 	    size_t *, int, size_t *));
    152  1.12  briggs void	esp_quick_dma_go __P((struct ncr53c9x_softc *));
    153  1.12  briggs 
    154  1.12  briggs static __inline__ int esp_dafb_have_dreq __P((struct esp_softc *esc));
    155  1.12  briggs static __inline__ int esp_iosb_have_dreq __P((struct esp_softc *esc));
    156  1.12  briggs int (*esp_have_dreq) __P((struct esp_softc *esc));
    157   1.7  briggs 
    158   1.7  briggs struct ncr53c9x_glue esp_glue = {
    159   1.7  briggs 	esp_read_reg,
    160   1.7  briggs 	esp_write_reg,
    161   1.7  briggs 	esp_dma_isintr,
    162   1.7  briggs 	esp_dma_reset,
    163   1.7  briggs 	esp_dma_intr,
    164   1.7  briggs 	esp_dma_setup,
    165   1.7  briggs 	esp_dma_go,
    166   1.7  briggs 	esp_dma_stop,
    167   1.7  briggs 	esp_dma_isactive,
    168   1.7  briggs 	NULL,			/* gl_clear_latched_intr */
    169   1.7  briggs };
    170   1.7  briggs 
    171   1.1  briggs int
    172   1.9  scottr espmatch(parent, cf, aux)
    173   1.1  briggs 	struct device *parent;
    174   1.6  scottr 	struct cfdata *cf;
    175   1.6  scottr 	void *aux;
    176   1.1  briggs {
    177  1.12  briggs 	int	found = 0;
    178  1.12  briggs 
    179  1.12  briggs 	if ((cf->cf_unit == 0) && mac68k_machine.scsi96) {
    180  1.12  briggs 		found = 1;
    181  1.12  briggs 	}
    182  1.12  briggs 	if ((cf->cf_unit == 1) && mac68k_machine.scsi96_2) {
    183  1.12  briggs 		found = 1;
    184  1.12  briggs 	}
    185  1.12  briggs 
    186  1.12  briggs 	return found;
    187   1.1  briggs }
    188   1.1  briggs 
    189   1.1  briggs /*
    190   1.1  briggs  * Attach this instance, and then all the sub-devices
    191   1.1  briggs  */
    192   1.1  briggs void
    193   1.1  briggs espattach(parent, self, aux)
    194   1.1  briggs 	struct device *parent, *self;
    195   1.1  briggs 	void *aux;
    196   1.1  briggs {
    197  1.12  briggs 	struct obio_attach_args *oa = (struct obio_attach_args *)aux;
    198   1.1  briggs 	extern vm_offset_t	SCSIBase;
    199  1.12  briggs 	struct esp_softc	*esc = (void *)self;
    200  1.12  briggs 	struct ncr53c9x_softc	*sc = &esc->sc_ncr53c9x;
    201  1.12  briggs 	int			quick = 0;
    202  1.12  briggs 	unsigned long		reg_offset;
    203  1.12  briggs 
    204  1.12  briggs 	reg_offset = SCSIBase - IOBase;
    205  1.12  briggs 	esc->sc_tag = oa->oa_tag;
    206  1.12  briggs 	/*
    207  1.12  briggs 	 * For Wombat, Primus and Optimus motherboards, DREQ is
    208  1.12  briggs 	 * visible on bit 0 of the IOSB's emulated VIA2 vIFR (and
    209  1.12  briggs 	 * the scsi registers are offset 0x1000 bytes from IOBase).
    210  1.12  briggs 	 *
    211  1.12  briggs 	 * For the Q700/900/950 it's at f9800024 for bus 0 and
    212  1.12  briggs 	 * f9800028 for bus 1 (900/950).  For these machines, that is also
    213  1.12  briggs 	 * a (12-bit) configuration register for DAFB's control of the
    214  1.12  briggs 	 * pseudo-DMA timing.  The default value is 0x1d1.
    215  1.12  briggs 	 */
    216  1.12  briggs 	esp_have_dreq = esp_dafb_have_dreq;
    217  1.12  briggs 	if (sc->sc_dev.dv_unit == 0) {
    218  1.12  briggs 		if (reg_offset == 0x10000) {
    219  1.12  briggs 			quick = 1;
    220  1.12  briggs 			esp_have_dreq = esp_iosb_have_dreq;
    221  1.12  briggs 		} else if (reg_offset == 0x18000) {
    222  1.12  briggs 			quick = 0;
    223  1.12  briggs 		} else {
    224  1.12  briggs 			if (bus_space_map(esc->sc_tag, 0xf9800024,
    225  1.12  briggs 					  4, 0, &esc->sc_bsh)) {
    226  1.12  briggs 				printf("failed to map 4 at 0xf9800024.\n");
    227  1.12  briggs 			} else {
    228  1.12  briggs 				quick = 1;
    229  1.12  briggs 				bus_space_write_4(esc->sc_tag,
    230  1.12  briggs 						  esc->sc_bsh, 0, 0x1d1);
    231  1.12  briggs 			}
    232  1.12  briggs 		}
    233  1.12  briggs 	} else {
    234  1.12  briggs 		if (bus_space_map(esc->sc_tag, 0xf9800028,
    235  1.12  briggs 				  4, 0, &esc->sc_bsh)) {
    236  1.12  briggs 			printf("failed to map 4 at 0xf9800028.\n");
    237  1.12  briggs 		} else {
    238  1.12  briggs 			quick = 1;
    239  1.12  briggs 			bus_space_write_4(esc->sc_tag, esc->sc_bsh, 0, 0x1d1);
    240  1.12  briggs 		}
    241  1.12  briggs 	}
    242  1.12  briggs 	if (quick) {
    243  1.12  briggs 		esp_glue.gl_write_reg = esp_quick_write_reg;
    244  1.12  briggs 		esp_glue.gl_dma_intr = esp_quick_dma_intr;
    245  1.12  briggs 		esp_glue.gl_dma_setup = esp_quick_dma_setup;
    246  1.12  briggs 		esp_glue.gl_dma_go = esp_quick_dma_go;
    247  1.12  briggs 	}
    248   1.1  briggs 
    249   1.1  briggs 	/*
    250   1.7  briggs 	 * Set up the glue for MI code early; we use some of it here.
    251   1.1  briggs 	 */
    252   1.7  briggs 	sc->sc_glue = &esp_glue;
    253   1.1  briggs 
    254   1.1  briggs 	/*
    255   1.7  briggs 	 * Save the regs
    256   1.1  briggs 	 */
    257   1.1  briggs 	if (sc->sc_dev.dv_unit == 0) {
    258   1.2  briggs 
    259   1.7  briggs 		esc->sc_reg = (volatile u_char *) SCSIBase;
    260   1.8  scottr 		via2_register_irq(VIA2_SCSIIRQ,
    261   1.8  scottr 		    (void (*)(void *))ncr53c9x_intr, esc);
    262   1.7  briggs 		esc->irq_mask = V2IF_SCSIIRQ;
    263   1.2  briggs 		if (reg_offset == 0x10000) {
    264   1.2  briggs 			sc->sc_freq = 16500000;
    265   1.2  briggs 		} else {
    266   1.2  briggs 			sc->sc_freq = 25000000;
    267   1.2  briggs 		}
    268  1.12  briggs 
    269  1.12  briggs 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    270  1.12  briggs 			printf(" (quick)");
    271  1.12  briggs 		}
    272   1.1  briggs 	} else {
    273   1.7  briggs 		esc->sc_reg = (volatile u_char *) SCSIBase + 0x402;
    274   1.8  scottr 		via2_register_irq(VIA2_SCSIDRQ,
    275   1.8  scottr 		    (void (*)(void *))ncr53c9x_intr, esc);
    276   1.7  briggs 		esc->irq_mask = V2IF_SCSIDRQ; /* V2IF_T1? */
    277   1.2  briggs 		sc->sc_freq = 25000000;
    278  1.12  briggs 
    279  1.12  briggs 		if (esp_glue.gl_dma_go == esp_quick_dma_go) {
    280  1.12  briggs 			printf(" (quick)");
    281  1.12  briggs 		}
    282   1.1  briggs 	}
    283   1.7  briggs 
    284   1.7  briggs 	printf(": address %p", esc->sc_reg);
    285   1.1  briggs 
    286   1.1  briggs 	sc->sc_id = 7;
    287   1.1  briggs 
    288   1.1  briggs 	/* gimme Mhz */
    289   1.1  briggs 	sc->sc_freq /= 1000000;
    290   1.1  briggs 
    291   1.1  briggs 	/*
    292   1.1  briggs 	 * It is necessary to try to load the 2nd config register here,
    293   1.1  briggs 	 * to find out what rev the esp chip is, else the esp_reset
    294   1.1  briggs 	 * will not set up the defaults correctly.
    295   1.1  briggs 	 */
    296  1.13  briggs 	sc->sc_cfg1 = sc->sc_id; /* | NCRCFG1_PARENB; */
    297   1.7  briggs 	sc->sc_cfg2 = NCRCFG2_SCSI2;
    298   1.3  briggs 	sc->sc_cfg3 = 0;
    299   1.7  briggs 	sc->sc_rev = NCR_VARIANT_NCR53C96;
    300   1.1  briggs 
    301   1.1  briggs 	/*
    302   1.1  briggs 	 * This is the value used to start sync negotiations
    303   1.7  briggs 	 * Note that the NCR register "SYNCTP" is programmed
    304   1.1  briggs 	 * in "clocks per byte", and has a minimum value of 4.
    305   1.1  briggs 	 * The SCSI period used in negotiation is one-fourth
    306   1.1  briggs 	 * of the time (in nanoseconds) needed to transfer one byte.
    307   1.1  briggs 	 * Since the chip's clock is given in MHz, we have the following
    308   1.1  briggs 	 * formula: 4 * period = (1000 / freq) * 4
    309   1.1  briggs 	 */
    310   1.1  briggs 	sc->sc_minsync = 1000 / sc->sc_freq;
    311   1.1  briggs 
    312   1.1  briggs 	sc->sc_minsync = 0;	/* No synchronous xfers w/o DMA */
    313   1.1  briggs 	/* Really no limit, but since we want to fit into the TCR... */
    314  1.12  briggs 	sc->sc_maxxfer = 8 * 1024; /*64 * 1024; XXX */
    315   1.1  briggs 
    316   1.1  briggs 	/*
    317   1.7  briggs 	 * Now try to attach all the sub-devices
    318   1.1  briggs 	 */
    319   1.7  briggs 	ncr53c9x_attach(sc, &esp_switch, &esp_dev);
    320   1.1  briggs 
    321   1.1  briggs 	/*
    322   1.7  briggs 	 * Configure interrupts.
    323   1.1  briggs 	 */
    324   1.1  briggs 	via2_reg(vPCR) = 0x22;
    325   1.7  briggs 	via2_reg(vIFR) = esc->irq_mask;
    326   1.7  briggs 	via2_reg(vIER) = 0x80 | esc->irq_mask;
    327   1.1  briggs }
    328   1.1  briggs 
    329   1.1  briggs /*
    330   1.7  briggs  * Glue functions.
    331   1.1  briggs  */
    332   1.1  briggs 
    333   1.7  briggs u_char
    334   1.7  briggs esp_read_reg(sc, reg)
    335   1.7  briggs 	struct ncr53c9x_softc *sc;
    336   1.7  briggs 	int reg;
    337   1.1  briggs {
    338   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    339   1.1  briggs 
    340   1.7  briggs 	return esc->sc_reg[reg * 16];
    341   1.1  briggs }
    342   1.1  briggs 
    343   1.1  briggs void
    344   1.7  briggs esp_write_reg(sc, reg, val)
    345   1.7  briggs 	struct ncr53c9x_softc *sc;
    346   1.7  briggs 	int reg;
    347   1.7  briggs 	u_char val;
    348   1.1  briggs {
    349   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    350   1.7  briggs 	u_char v = val;
    351   1.1  briggs 
    352   1.7  briggs 	if (reg == NCR_CMD && v == (NCRCMD_TRANS|NCRCMD_DMA)) {
    353   1.7  briggs 		v = NCRCMD_TRANS;
    354   1.1  briggs 	}
    355   1.7  briggs 	esc->sc_reg[reg * 16] = v;
    356   1.1  briggs }
    357   1.1  briggs 
    358  1.12  briggs void
    359  1.12  briggs esp_dma_stop(sc)
    360  1.12  briggs 	struct ncr53c9x_softc *sc;
    361  1.12  briggs {
    362  1.12  briggs }
    363  1.12  briggs 
    364  1.12  briggs int
    365  1.12  briggs esp_dma_isactive(sc)
    366  1.12  briggs 	struct ncr53c9x_softc *sc;
    367  1.12  briggs {
    368  1.12  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    369  1.12  briggs 
    370  1.12  briggs 	return esc->sc_active;
    371  1.12  briggs }
    372  1.12  briggs 
    373   1.7  briggs int
    374   1.7  briggs esp_dma_isintr(sc)
    375   1.7  briggs 	struct ncr53c9x_softc *sc;
    376   1.1  briggs {
    377   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    378   1.1  briggs 
    379   1.7  briggs 	return esc->sc_reg[NCR_STAT * 16] & 0x80;
    380   1.1  briggs }
    381   1.1  briggs 
    382   1.1  briggs void
    383   1.7  briggs esp_dma_reset(sc)
    384   1.7  briggs 	struct ncr53c9x_softc *sc;
    385   1.1  briggs {
    386   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    387   1.1  briggs 
    388   1.7  briggs 	esc->sc_active = 0;
    389   1.7  briggs 	esc->sc_tc = 0;
    390   1.1  briggs }
    391   1.1  briggs 
    392   1.7  briggs int
    393   1.7  briggs esp_dma_intr(sc)
    394   1.7  briggs 	struct ncr53c9x_softc *sc;
    395   1.1  briggs {
    396   1.7  briggs 	register struct esp_softc *esc = (struct esp_softc *)sc;
    397   1.7  briggs 	register u_char	*p;
    398   1.7  briggs 	volatile u_char *cmdreg, *intrreg, *statreg, *fiforeg;
    399   1.7  briggs 	register u_int	espphase, espstat, espintr;
    400   1.7  briggs 	register int	cnt;
    401   1.1  briggs 
    402   1.7  briggs 	if (esc->sc_active == 0) {
    403   1.7  briggs 		printf("dma_intr--inactive DMA\n");
    404   1.7  briggs 		return -1;
    405   1.1  briggs 	}
    406   1.1  briggs 
    407   1.7  briggs 	if ((sc->sc_espintr & NCRINTR_BS) == 0) {
    408   1.7  briggs 		esc->sc_active = 0;
    409   1.7  briggs 		return 0;
    410   1.1  briggs 	}
    411   1.1  briggs 
    412  1.12  briggs 	cnt = *esc->sc_dmalen;
    413  1.12  briggs 	if (*esc->sc_dmalen == 0) {
    414   1.7  briggs 		printf("data interrupt, but no count left.");
    415   1.1  briggs 	}
    416   1.1  briggs 
    417   1.7  briggs 	p = *esc->sc_dmaaddr;
    418   1.7  briggs 	espphase = sc->sc_phase;
    419   1.7  briggs 	espstat = (u_int) sc->sc_espstat;
    420   1.7  briggs 	espintr = (u_int) sc->sc_espintr;
    421   1.7  briggs 	cmdreg = esc->sc_reg + NCR_CMD * 16;
    422   1.7  briggs 	fiforeg = esc->sc_reg + NCR_FIFO * 16;
    423   1.7  briggs 	statreg = esc->sc_reg + NCR_STAT * 16;
    424   1.7  briggs 	intrreg = esc->sc_reg + NCR_INTR * 16;
    425   1.7  briggs 	do {
    426   1.7  briggs 		if (esc->sc_datain) {
    427   1.7  briggs 			*p++ = *fiforeg;
    428   1.7  briggs 			cnt--;
    429   1.7  briggs 			if (espphase == DATA_IN_PHASE) {
    430   1.7  briggs 				*cmdreg = NCRCMD_TRANS;
    431   1.7  briggs 			} else {
    432   1.7  briggs 				esc->sc_active = 0;
    433   1.7  briggs 			}
    434   1.7  briggs 	 	} else {
    435   1.7  briggs 			if (   (espphase == DATA_OUT_PHASE)
    436   1.7  briggs 			    || (espphase == MESSAGE_OUT_PHASE)) {
    437   1.7  briggs 				*fiforeg = *p++;
    438   1.7  briggs 				cnt--;
    439   1.7  briggs 				*cmdreg = NCRCMD_TRANS;
    440   1.7  briggs 			} else {
    441   1.7  briggs 				esc->sc_active = 0;
    442   1.7  briggs 			}
    443   1.1  briggs 		}
    444   1.1  briggs 
    445   1.7  briggs 		if (esc->sc_active) {
    446   1.7  briggs 			while (!(*statreg & 0x80));
    447   1.7  briggs 			espstat = *statreg;
    448   1.7  briggs 			espintr = *intrreg;
    449   1.7  briggs 			espphase = (espintr & NCRINTR_DIS)
    450   1.7  briggs 				    ? /* Disconnected */ BUSFREE_PHASE
    451   1.7  briggs 				    : espstat & PHASE_MASK;
    452   1.1  briggs 		}
    453   1.7  briggs 	} while (esc->sc_active && (espintr & NCRINTR_BS));
    454   1.7  briggs 	sc->sc_phase = espphase;
    455   1.7  briggs 	sc->sc_espstat = (u_char) espstat;
    456   1.7  briggs 	sc->sc_espintr = (u_char) espintr;
    457   1.7  briggs 	*esc->sc_dmaaddr = p;
    458  1.12  briggs 	*esc->sc_dmalen = cnt;
    459   1.1  briggs 
    460  1.12  briggs 	if (*esc->sc_dmalen == 0) {
    461   1.7  briggs 		esc->sc_tc = NCRSTAT_TC;
    462   1.1  briggs 	}
    463   1.7  briggs 	sc->sc_espstat |= esc->sc_tc;
    464   1.7  briggs 	return 0;
    465   1.1  briggs }
    466   1.1  briggs 
    467   1.1  briggs int
    468   1.7  briggs esp_dma_setup(sc, addr, len, datain, dmasize)
    469   1.7  briggs 	struct ncr53c9x_softc *sc;
    470   1.7  briggs 	caddr_t *addr;
    471   1.7  briggs 	size_t *len;
    472   1.7  briggs 	int datain;
    473   1.7  briggs 	size_t *dmasize;
    474   1.1  briggs {
    475   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    476   1.1  briggs 
    477   1.7  briggs 	esc->sc_dmaaddr = addr;
    478  1.12  briggs 	esc->sc_dmalen = len;
    479   1.7  briggs 	esc->sc_datain = datain;
    480   1.7  briggs 	esc->sc_dmasize = *dmasize;
    481   1.7  briggs 	esc->sc_tc = 0;
    482   1.1  briggs 
    483   1.7  briggs 	return 0;
    484   1.1  briggs }
    485   1.1  briggs 
    486   1.1  briggs void
    487   1.7  briggs esp_dma_go(sc)
    488   1.7  briggs 	struct ncr53c9x_softc *sc;
    489   1.1  briggs {
    490   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    491   1.1  briggs 
    492   1.7  briggs 	if (esc->sc_datain == 0) {
    493   1.7  briggs 		esc->sc_reg[NCR_FIFO * 16] = **esc->sc_dmaaddr;
    494  1.12  briggs 		(*esc->sc_dmalen)--;
    495   1.7  briggs 		(*esc->sc_dmaaddr)++;
    496   1.1  briggs 	}
    497   1.7  briggs 	esc->sc_active = 1;
    498   1.1  briggs }
    499   1.1  briggs 
    500   1.1  briggs void
    501  1.12  briggs esp_quick_write_reg(sc, reg, val)
    502   1.7  briggs 	struct ncr53c9x_softc *sc;
    503  1.12  briggs 	int reg;
    504  1.12  briggs 	u_char val;
    505   1.1  briggs {
    506  1.12  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    507  1.12  briggs 	u_char v = val;
    508  1.12  briggs 
    509  1.12  briggs 	esc->sc_reg[reg * 16] = v;
    510   1.1  briggs }
    511   1.1  briggs 
    512   1.1  briggs int
    513  1.12  briggs esp_quick_dma_intr(sc)
    514  1.12  briggs 	struct ncr53c9x_softc *sc;
    515  1.12  briggs {
    516  1.12  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    517  1.12  briggs 	int trans=0, resid=0;
    518  1.12  briggs 
    519  1.12  briggs 	if (esc->sc_active == 0)
    520  1.12  briggs 		panic("dma_intr--inactive DMA\n");
    521  1.12  briggs 
    522  1.12  briggs 	esc->sc_active = 0;
    523  1.12  briggs 
    524  1.12  briggs 	if (esc->sc_dmasize == 0) {
    525  1.12  briggs 		int	res;
    526  1.12  briggs 
    527  1.12  briggs 		res = 65536;
    528  1.12  briggs 		res -= NCR_READ_REG(sc, NCR_TCL);
    529  1.12  briggs 		res -= NCR_READ_REG(sc, NCR_TCM) << 8;
    530  1.12  briggs 		printf("dmaintr: discarded %d b (last transfer was %d b).\n",
    531  1.12  briggs 			res, esc->sc_prevdmasize);
    532  1.12  briggs 		return 0;
    533  1.12  briggs 	}
    534  1.12  briggs 
    535  1.12  briggs 	if (esc->sc_datain &&
    536  1.12  briggs 	    (resid = (NCR_READ_REG(sc, NCR_FFLAG) & NCRFIFO_FF)) != 0) {
    537  1.12  briggs 		printf("dmaintr: empty FIFO of %d\n", resid);
    538  1.12  briggs 		DELAY(1);
    539  1.12  briggs 	}
    540  1.12  briggs 
    541  1.12  briggs 	if ((sc->sc_espstat & NCRSTAT_TC) == 0) {
    542  1.12  briggs 		resid += NCR_READ_REG(sc, NCR_TCL);
    543  1.12  briggs 		resid += NCR_READ_REG(sc, NCR_TCM) << 8;
    544  1.12  briggs 
    545  1.12  briggs 		if (resid == 0)
    546  1.12  briggs 			resid = 65536;
    547  1.12  briggs 	}
    548  1.12  briggs 
    549  1.12  briggs 	trans = esc->sc_dmasize - resid;
    550  1.12  briggs 	if (trans < 0) {
    551  1.12  briggs 		printf("dmaintr: trans < 0????");
    552  1.12  briggs 		trans = esc->sc_dmasize;
    553  1.12  briggs 	}
    554  1.12  briggs 
    555  1.12  briggs 	NCR_DMA(("dmaintr: trans %d, resid %d.\n", trans, resid));
    556  1.12  briggs 	*esc->sc_dmaaddr += trans;
    557  1.12  briggs 	*esc->sc_dmalen -= trans;
    558  1.12  briggs 
    559  1.12  briggs 	return 0;
    560  1.12  briggs }
    561  1.12  briggs 
    562  1.12  briggs int
    563  1.12  briggs esp_quick_dma_setup(sc, addr, len, datain, dmasize)
    564  1.12  briggs 	struct ncr53c9x_softc *sc;
    565  1.12  briggs 	caddr_t *addr;
    566  1.12  briggs 	size_t *len;
    567  1.12  briggs 	int datain;
    568  1.12  briggs 	size_t *dmasize;
    569  1.12  briggs {
    570  1.12  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    571  1.12  briggs 
    572  1.12  briggs 	esc->sc_dmaaddr = addr;
    573  1.12  briggs 	esc->sc_dmalen = len;
    574  1.12  briggs 
    575  1.12  briggs 	esc->sc_pdmaddr = (u_int16_t *) *addr;
    576  1.12  briggs 	esc->sc_pdmalen = *len;
    577  1.13  briggs 	if (esc->sc_pdmalen & 1) {
    578  1.13  briggs 		esc->sc_pdmalen--;
    579  1.13  briggs 		esc->sc_pad = 1;
    580  1.13  briggs 	} else {
    581  1.13  briggs 		esc->sc_pad = 0;
    582  1.13  briggs 	}
    583  1.12  briggs 
    584  1.12  briggs 	esc->sc_datain = datain;
    585  1.12  briggs 	esc->sc_prevdmasize = esc->sc_dmasize;
    586  1.12  briggs 	esc->sc_dmasize = *dmasize;
    587  1.12  briggs 
    588  1.12  briggs 	return 0;
    589  1.12  briggs }
    590  1.12  briggs 
    591  1.12  briggs static __inline__ int
    592  1.12  briggs esp_dafb_have_dreq(esc)
    593  1.12  briggs 	struct esp_softc *esc;
    594  1.12  briggs {
    595  1.12  briggs 	u_int32_t r;
    596  1.12  briggs 
    597  1.12  briggs 	r = bus_space_read_4(esc->sc_tag, esc->sc_bsh, 0);
    598  1.12  briggs 	return (r & 0x200);
    599  1.12  briggs }
    600  1.12  briggs 
    601  1.12  briggs static __inline__ int
    602  1.12  briggs esp_iosb_have_dreq(esc)
    603  1.12  briggs 	struct esp_softc *esc;
    604  1.12  briggs {
    605  1.12  briggs 	return (via2_reg(vIFR) & V2IF_SCSIDRQ);
    606  1.12  briggs }
    607  1.12  briggs 
    608  1.12  briggs static int espspl=-1;
    609  1.12  briggs #define __splx(s) __asm __volatile ("movew %0,sr" : : "di" (s));
    610  1.12  briggs #define __spl2()  __splx(PSL_S|PSL_IPL2)
    611  1.12  briggs #define __spl4()  __splx(PSL_S|PSL_IPL4)
    612  1.12  briggs 
    613  1.12  briggs void
    614  1.12  briggs esp_quick_dma_go(sc)
    615   1.7  briggs 	struct ncr53c9x_softc *sc;
    616   1.1  briggs {
    617   1.7  briggs 	struct esp_softc *esc = (struct esp_softc *)sc;
    618  1.12  briggs 	extern int *nofault;
    619  1.12  briggs 	label_t faultbuf;
    620  1.12  briggs 	u_int16_t volatile *pdma;
    621  1.12  briggs 	u_char volatile *statreg;
    622  1.12  briggs 
    623  1.12  briggs 	esc->sc_active = 1;
    624  1.12  briggs 
    625  1.12  briggs 	espspl = spl2();
    626  1.12  briggs 
    627  1.12  briggs restart_dmago:
    628  1.12  briggs 	nofault = (int *) &faultbuf;
    629  1.12  briggs 	if (setjmp((label_t *) nofault)) {
    630  1.12  briggs 		int	i=0;
    631  1.12  briggs 
    632  1.12  briggs 		nofault = (int *) 0;
    633  1.12  briggs 		statreg = esc->sc_reg + NCR_STAT * 16;
    634  1.12  briggs 		for (;;) {
    635  1.12  briggs 			if (*statreg & 0x80) {
    636  1.12  briggs 				goto gotintr;
    637  1.12  briggs 			}
    638  1.12  briggs 
    639  1.12  briggs 			if (esp_have_dreq(esc)) {
    640  1.12  briggs 				break;
    641  1.12  briggs 			}
    642  1.12  briggs 
    643  1.12  briggs 			DELAY(1);
    644  1.12  briggs 			if (i++ > 10000)
    645  1.12  briggs 				panic("esp_dma_go: Argh!");
    646  1.12  briggs 		}
    647  1.12  briggs 		goto restart_dmago;
    648  1.12  briggs 	}
    649  1.12  briggs 
    650  1.12  briggs 	statreg = esc->sc_reg + NCR_STAT * 16;
    651  1.12  briggs 	pdma = (u_int16_t *) (esc->sc_reg + 0x100);
    652   1.1  briggs 
    653  1.12  briggs #define WAIT while (!esp_have_dreq(esc)) if (*statreg & 0x80) goto gotintr
    654  1.12  briggs 
    655  1.12  briggs 	if (esc->sc_datain == 0) {
    656  1.12  briggs 		while (esc->sc_pdmalen) {
    657  1.12  briggs 			WAIT;
    658  1.12  briggs 			__spl4(); *pdma = *(esc->sc_pdmaddr)++; __spl2()
    659  1.12  briggs 			esc->sc_pdmalen -= 2;
    660  1.12  briggs 		}
    661  1.13  briggs 		if (esc->sc_pad) {
    662  1.13  briggs 			unsigned short	us;
    663  1.13  briggs 			unsigned char	*c;
    664  1.13  briggs 			c = (unsigned char *) esc->sc_pdmaddr;
    665  1.13  briggs 			us = *c;
    666  1.13  briggs 			WAIT;
    667  1.13  briggs 			__spl4(); *pdma = us; __spl2()
    668  1.13  briggs 		}
    669  1.12  briggs 	} else {
    670  1.12  briggs 		while (esc->sc_pdmalen) {
    671  1.12  briggs 			WAIT;
    672  1.12  briggs 			__spl4(); *(esc->sc_pdmaddr)++ = *pdma; __spl2()
    673  1.12  briggs 			esc->sc_pdmalen -= 2;
    674  1.13  briggs 		}
    675  1.13  briggs 		if (esc->sc_pad) {
    676  1.13  briggs 			unsigned short	us;
    677  1.13  briggs 			unsigned char	*c;
    678  1.13  briggs 			WAIT;
    679  1.13  briggs 			__spl4(); us = *pdma; __spl2()
    680  1.13  briggs 			c = (unsigned char *) esc->sc_pdmaddr;
    681  1.13  briggs 			*c = us & 0xff;
    682  1.12  briggs 		}
    683  1.12  briggs 	}
    684  1.12  briggs #undef WAIT
    685  1.12  briggs 
    686  1.12  briggs 	nofault = (int *) 0;
    687  1.12  briggs 
    688  1.12  briggs 	if ((*statreg & 0x80) == 0) {
    689  1.12  briggs 		if (espspl != -1) splx(espspl); espspl = -1;
    690  1.12  briggs 		return;
    691  1.12  briggs 	}
    692  1.12  briggs 
    693  1.12  briggs gotintr:
    694  1.12  briggs 	ncr53c9x_intr(sc);
    695  1.12  briggs 	if (espspl != -1) splx(espspl); espspl = -1;
    696   1.1  briggs }
    697