Home | History | Annotate | Line # | Download | only in dev
atari5380.c revision 1.18
      1  1.18      leo /*	$NetBSD: atari5380.c,v 1.18 1996/09/16 06:20:44 leo Exp $	*/
      2   1.1      leo 
      3   1.1      leo /*
      4   1.1      leo  * Copyright (c) 1995 Leo Weppelman.
      5   1.1      leo  * All rights reserved.
      6   1.1      leo  *
      7   1.1      leo  * Redistribution and use in source and binary forms, with or without
      8   1.1      leo  * modification, are permitted provided that the following conditions
      9   1.1      leo  * are met:
     10   1.1      leo  * 1. Redistributions of source code must retain the above copyright
     11   1.1      leo  *    notice, this list of conditions and the following disclaimer.
     12   1.1      leo  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1      leo  *    notice, this list of conditions and the following disclaimer in the
     14   1.1      leo  *    documentation and/or other materials provided with the distribution.
     15   1.1      leo  * 3. All advertising materials mentioning features or use of this software
     16   1.1      leo  *    must display the following acknowledgement:
     17   1.1      leo  *      This product includes software developed by Leo Weppelman.
     18   1.1      leo  * 4. The name of the author may not be used to endorse or promote products
     19   1.1      leo  *    derived from this software without specific prior written permission
     20   1.1      leo  *
     21   1.1      leo  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     22   1.1      leo  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     23   1.1      leo  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     24   1.1      leo  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     25   1.1      leo  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     26   1.1      leo  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     27   1.1      leo  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     28   1.1      leo  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     29   1.1      leo  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     30   1.1      leo  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     31   1.1      leo  */
     32   1.1      leo 
     33   1.1      leo #include <sys/param.h>
     34   1.1      leo #include <sys/systm.h>
     35   1.1      leo #include <sys/kernel.h>
     36   1.1      leo #include <sys/device.h>
     37   1.1      leo #include <sys/buf.h>
     38   1.1      leo #include <scsi/scsi_all.h>
     39   1.1      leo #include <scsi/scsi_message.h>
     40   1.1      leo #include <scsi/scsiconf.h>
     41   1.1      leo 
     42  1.18      leo #include <m68k/asm_single.h>
     43  1.18      leo 
     44  1.11      leo #include <atari/atari/stalloc.h>
     45  1.11      leo 
     46   1.1      leo /*
     47   1.1      leo  * Include the driver definitions
     48   1.1      leo  */
     49   1.1      leo #include <atari/dev/ncr5380reg.h>
     50   1.1      leo 
     51   1.1      leo #include <machine/stdarg.h>
     52   1.1      leo #include <machine/iomap.h>
     53   1.1      leo #include <machine/mfp.h>
     54   1.1      leo 
     55   1.1      leo #if defined(FALCON_SCSI)
     56   1.1      leo #include <machine/dma.h>
     57   1.1      leo #endif
     58   1.1      leo 
     59   1.1      leo /*
     60   1.1      leo  * Set the various driver options
     61   1.1      leo  */
     62   1.1      leo #define	NREQ		18	/* Size of issue queue			*/
     63   1.1      leo #define	AUTO_SENSE	1	/* Automatically issue a request-sense 	*/
     64   1.1      leo 
     65   1.2      leo #define	DRNAME		ncrscsi	/* used in various prints		*/
     66   1.1      leo #undef	DBG_SEL			/* Show the selection process		*/
     67   1.1      leo #undef	DBG_REQ			/* Show enqueued/ready requests		*/
     68   1.2      leo #undef	DBG_ERR_RET		/* Show requests with != 0 return code	*/
     69   1.1      leo #undef	DBG_NOWRITE		/* Do not allow writes to the targets	*/
     70   1.1      leo #undef	DBG_PIO			/* Show the polled-I/O process		*/
     71   1.1      leo #undef	DBG_INF			/* Show information transfer process	*/
     72   1.1      leo #define	DBG_NOSTATIC		/* No static functions, all in DDB trace*/
     73  1.14      leo #define	DBG_PID		15	/* Keep track of driver			*/
     74   1.1      leo #define	REAL_DMA		/* Use DMA if sensible			*/
     75  1.17      leo #if defined(FALCON_SCSI)
     76   1.1      leo #define	REAL_DMA_POLL	1	/* 1: Poll for end of DMA-transfer	*/
     77   1.1      leo #else
     78   1.1      leo #define	REAL_DMA_POLL	0	/* 1: Poll for end of DMA-transfer	*/
     79   1.1      leo #endif
     80   1.1      leo #undef	USE_PDMA		/* Use special pdma-transfer function	*/
     81   1.4      leo #define MIN_PHYS	65536	/*BARF!!!!*/
     82   1.1      leo 
     83   1.1      leo /*
     84   1.8      leo  * Include more driver definitions
     85   1.8      leo  */
     86   1.8      leo #include <atari/dev/ncr5380var.h>
     87   1.8      leo 
     88   1.8      leo 
     89   1.8      leo /*
     90   1.8      leo  * This is crap, but because the interrupts now run at MFP spl-level (6),
     91   1.8      leo  * splbio() is not enough at some places. The code should be checked to
     92   1.8      leo  * find out where splhigh() is needed and where splbio() should be used.
     93   1.8      leo  * Now that I use this interrupt sceme, the spl values are fake!
     94   1.8      leo  */
     95   1.8      leo #undef splbio()
     96   1.8      leo #define splbio()	splhigh()
     97   1.8      leo 
     98   1.8      leo /*
     99   1.1      leo  * The atari specific driver options
    100   1.1      leo  */
    101   1.1      leo #undef	NO_TTRAM_DMA		/* Do not use DMA to TT-ram. This	*/
    102   1.1      leo 				/*    fails on older atari's		*/
    103   1.1      leo #define	ENABLE_NCR5380(sc)	cur_softc = sc;
    104   1.1      leo 
    105   1.9      leo static u_char	*alloc_bounceb __P((u_long));
    106   1.9      leo static void	free_bounceb __P((u_char *));
    107  1.10  thorpej static int	machine_match __P((struct device *, void *, void *,
    108   1.9      leo 							struct cfdriver *));
    109   1.9      leo        void	scsi_ctrl __P((int));
    110   1.9      leo        void	scsi_dma __P((int));
    111   1.9      leo 
    112   1.3      leo /*
    113   1.3      leo  * Functions that do nothing on the atari
    114   1.3      leo  */
    115   1.3      leo #define	pdma_ready()		0
    116   1.1      leo 
    117   1.1      leo #if defined(TT_SCSI)
    118  1.18      leo 
    119  1.18      leo void	ncr5380_drq_intr __P((void));
    120  1.18      leo 
    121   1.1      leo /*
    122   1.1      leo  * Define all the things we need of the DMA-controller
    123   1.1      leo  */
    124   1.1      leo #define	SCSI_DMA	((struct scsi_dma *)AD_SCSI_DMA)
    125   1.1      leo #define	SCSI_5380	((struct scsi_5380 *)AD_NCR5380)
    126   1.1      leo 
    127   1.1      leo struct scsi_dma {
    128   1.1      leo 	volatile u_char		s_dma_ptr[8];	/* use only the odd bytes */
    129   1.1      leo 	volatile u_char		s_dma_cnt[8];	/* use only the odd bytes */
    130   1.1      leo 	volatile u_char		s_dma_res[4];	/* data residue register  */
    131   1.1      leo 	volatile u_char		s_dma_gap;	/* not used		  */
    132   1.1      leo 	volatile u_char		s_dma_ctrl;	/* control register	  */
    133  1.18      leo 	volatile u_char		s_dma_gap2;	/* not used		  */
    134  1.18      leo 	volatile u_char		s_hdma_ctrl;	/* Hades control register */
    135   1.1      leo };
    136   1.1      leo 
    137   1.1      leo #define	set_scsi_dma(addr, val)	(void)(					\
    138   1.1      leo 	{								\
    139   1.1      leo 	u_char	*address = (u_char*)addr+1;				\
    140   1.1      leo 	u_long	nval	 = (u_long)val;					\
    141   1.1      leo 	__asm("movepl	%0, %1@(0)": :"d" (nval), "a" (address));	\
    142   1.1      leo 	})
    143   1.1      leo 
    144   1.1      leo #define	get_scsi_dma(addr, res)	(					\
    145   1.1      leo 	{								\
    146   1.1      leo 	u_char	*address = (u_char*)addr+1;				\
    147   1.1      leo 	u_long	nval;							\
    148   1.1      leo 	__asm("movepl	%1@(0), %0": "=d" (nval) : "a" (address));	\
    149   1.1      leo 	res = (u_long)nval;						\
    150   1.1      leo 	})
    151   1.1      leo 
    152   1.1      leo /*
    153   1.1      leo  * Defines for TT-DMA control register
    154   1.1      leo  */
    155   1.1      leo #define	SD_BUSERR	0x80		/* 1 = transfer caused bus error*/
    156   1.1      leo #define	SD_ZERO		0x40		/* 1 = byte counter is zero	*/
    157   1.1      leo #define	SD_ENABLE	0x02		/* 1 = Enable DMA		*/
    158   1.1      leo #define	SD_OUT		0x01		/* Direction: memory to SCSI	*/
    159   1.1      leo #define	SD_IN		0x00		/* Direction: SCSI to memory	*/
    160   1.1      leo 
    161   1.1      leo /*
    162  1.18      leo  * Defines for Hades-DMA control register
    163  1.18      leo  */
    164  1.18      leo #define	SDH_BUSERR	0x02		/* 1 = Bus error		*/
    165  1.18      leo #define SDH_EOP		0x01		/* 1 = Signal EOP on 5380	*/
    166  1.18      leo #define	SDH_ZERO	0x40		/* 1 = Byte counter is zero	*/
    167  1.18      leo 
    168  1.18      leo /*
    169   1.1      leo  * Define the 5380 register set
    170   1.1      leo  */
    171   1.1      leo struct scsi_5380 {
    172   1.1      leo 	volatile u_char	scsi_5380[16];	/* use only the odd bytes	*/
    173   1.1      leo };
    174   1.1      leo #endif /* TT_SCSI */
    175   1.1      leo 
    176   1.1      leo /**********************************************
    177   1.1      leo  * Variables present for both TT and Falcon.  *
    178   1.1      leo  **********************************************/
    179   1.1      leo 
    180   1.1      leo /*
    181   1.1      leo  * Softc of currently active controller (a bit of fake; we only have one)
    182   1.1      leo  */
    183   1.1      leo static struct ncr_softc	*cur_softc;
    184   1.1      leo 
    185   1.1      leo #if defined(TT_SCSI) && !defined(FALCON_SCSI)
    186   1.1      leo /*
    187   1.1      leo  * We can be more efficient for some functions when only TT_SCSI is selected
    188   1.1      leo  */
    189   1.1      leo #define	GET_5380_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
    190   1.1      leo #define	SET_5380_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
    191   1.1      leo 
    192   1.1      leo #define scsi_mach_init(sc)	scsi_tt_init(sc)
    193   1.1      leo #define scsi_ienable()		scsi_tt_ienable()
    194   1.1      leo #define scsi_idisable()		scsi_tt_idisable()
    195   1.1      leo #define	scsi_clr_ipend()	scsi_tt_clr_ipend()
    196   1.7      leo #define scsi_ipending()		(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
    197   1.1      leo #define scsi_dma_setup(r,p,m)	scsi_tt_dmasetup(r, p, m)
    198   1.1      leo #define wrong_dma_range(r,d)	tt_wrong_dma_range(r, d)
    199   1.1      leo #define poll_edma(reqp)		tt_poll_edma(reqp)
    200   1.1      leo #define get_dma_result(r, b)	tt_get_dma_result(r, b)
    201  1.16      leo #define	can_access_5380()	1
    202  1.18      leo #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
    203   1.1      leo 
    204   1.1      leo #define fair_to_keep_dma()	1
    205   1.1      leo #define claimed_dma()		1
    206   1.1      leo #define reconsider_dma()
    207   1.1      leo 
    208   1.1      leo #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
    209   1.1      leo 
    210   1.1      leo #if defined(TT_SCSI)
    211   1.1      leo 
    212   1.1      leo /*
    213   1.9      leo  * Prototype functions defined below
    214   1.9      leo  */
    215   1.9      leo #ifdef NO_TTRAM_DMA
    216   1.9      leo static int tt_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
    217   1.9      leo #endif
    218   1.9      leo static void	scsi_tt_init __P((struct ncr_softc *));
    219   1.9      leo static u_char	get_tt_5380_reg __P((u_short));
    220   1.9      leo static void	set_tt_5380_reg __P((u_short, u_short));
    221   1.9      leo static void	scsi_tt_dmasetup __P((SC_REQ *, u_int, u_char));
    222   1.9      leo static int	tt_poll_edma __P((SC_REQ *));
    223   1.9      leo static u_char	*ptov __P((SC_REQ *, u_long*));
    224   1.9      leo static int	tt_get_dma_result __P((SC_REQ *, u_long *));
    225   1.9      leo        void	scsi_tt_ienable __P((void));
    226   1.9      leo        void	scsi_tt_idisable __P((void));
    227   1.9      leo        void	scsi_tt_clr_ipend __P((void));
    228   1.9      leo 
    229   1.9      leo /*
    230   1.1      leo  * Define these too, so we can use them locally...
    231   1.1      leo  */
    232   1.1      leo #define	GET_TT_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
    233   1.1      leo #define	SET_TT_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
    234   1.1      leo 
    235   1.1      leo #ifdef NO_TTRAM_DMA
    236   1.9      leo static int
    237   1.9      leo tt_wrong_dma_range(reqp, dm)
    238   1.1      leo SC_REQ			*reqp;
    239   1.1      leo struct dma_chain	*dm;
    240   1.1      leo {
    241   1.1      leo 	if (dm->dm_addr & 0xff000000) {
    242   1.1      leo 		reqp->dr_flag |= DRIVER_BOUNCING;
    243   1.1      leo 		return(1);
    244   1.1      leo 	}
    245   1.1      leo 	return(0);
    246   1.1      leo }
    247   1.1      leo #else
    248   1.1      leo #define	tt_wrong_dma_range(reqp, dm)	0
    249   1.1      leo #endif
    250   1.1      leo 
    251   1.9      leo static void
    252   1.9      leo scsi_tt_init(struct ncr_softc *sc)
    253   1.1      leo {
    254   1.1      leo 	/*
    255   1.1      leo 	 * Enable SCSI-related interrupts
    256   1.1      leo 	 */
    257   1.1      leo 	MFP2->mf_aer  |= 0x80;		/* SCSI IRQ goes HIGH!!!!!	*/
    258   1.1      leo 
    259  1.18      leo 	if (machineid & ATARI_TT) {
    260  1.18      leo 		/* SCSI-dma interrupts		*/
    261  1.18      leo 		MFP2->mf_ierb |= IB_SCDM;
    262  1.18      leo 		MFP2->mf_iprb &= ~IB_SCDM;
    263  1.18      leo 		MFP2->mf_imrb |= IB_SCDM;
    264  1.18      leo 	}
    265  1.18      leo 	else SCSI_DMA->s_hdma_ctrl = 0;
    266   1.1      leo 
    267   1.1      leo 	MFP2->mf_iera |= IA_SCSI;	/* SCSI-5380 interrupts		*/
    268   1.1      leo 	MFP2->mf_ipra &= ~IA_SCSI;
    269   1.1      leo 	MFP2->mf_imra |= IA_SCSI;
    270   1.1      leo 
    271   1.1      leo 	/*
    272   1.1      leo 	 * LWP: DMA transfers to TT-ram causes data to be garbeled
    273   1.1      leo 	 * without notice on some revisons of the TT-mainboard.
    274   1.1      leo 	 * When program's generate misterious Segmentations faults,
    275   1.1      leo 	 * try turning on NO_TTRAM_DMA.
    276   1.1      leo 	 */
    277   1.1      leo #ifdef NO_TTRAM_DMA
    278   1.1      leo 	printf(": DMA to TT-RAM is disabled!");
    279   1.1      leo #endif
    280   1.1      leo }
    281   1.1      leo 
    282   1.9      leo static u_char
    283   1.9      leo get_tt_5380_reg(u_short rnum)
    284   1.1      leo {
    285   1.1      leo 	return(SCSI_5380->scsi_5380[(rnum << 1) | 1]);
    286   1.1      leo }
    287   1.1      leo 
    288   1.9      leo static void
    289   1.9      leo set_tt_5380_reg(u_short rnum, u_short val)
    290   1.1      leo {
    291   1.1      leo 	SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
    292   1.1      leo }
    293   1.1      leo 
    294   1.9      leo extern __inline__ void
    295   1.9      leo scsi_tt_ienable(void)
    296   1.1      leo {
    297  1.18      leo 	if (machineid & ATARI_TT)
    298  1.18      leo 		single_inst_bset_b(MFP2->mf_imrb, IB_SCDM);
    299  1.18      leo 	single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    300   1.1      leo }
    301   1.1      leo 
    302   1.9      leo extern __inline__ void
    303   1.9      leo scsi_tt_idisable(void)
    304   1.1      leo {
    305  1.18      leo 	if (machineid & ATARI_TT)
    306  1.18      leo 		single_inst_bclr_b(MFP2->mf_imrb, IB_SCDM);
    307  1.18      leo 	single_inst_bclr_b(MFP2->mf_imra, IA_SCSI);
    308   1.1      leo }
    309   1.1      leo 
    310   1.9      leo extern __inline__ void
    311   1.9      leo scsi_tt_clr_ipend(void)
    312   1.1      leo {
    313   1.1      leo 	int	tmp;
    314   1.1      leo 
    315   1.1      leo 	SCSI_DMA->s_dma_ctrl = 0;
    316   1.1      leo 	tmp = GET_TT_REG(NCR5380_IRCV);
    317  1.18      leo 	if (machineid & ATARI_TT)
    318  1.18      leo 		single_inst_bclr_b(MFP2->mf_iprb, IB_SCDM);
    319  1.18      leo 	single_inst_bclr_b(MFP2->mf_ipra, IA_SCSI);
    320   1.1      leo }
    321   1.1      leo 
    322   1.9      leo static void
    323   1.9      leo scsi_tt_dmasetup(SC_REQ *reqp, u_int phase, u_char	mode)
    324   1.1      leo {
    325   1.1      leo 	if (PH_IN(phase)) {
    326   1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_IN;
    327  1.18      leo 		if (machineid & ATARI_HADES)
    328  1.18      leo 		    SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP);
    329   1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
    330   1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
    331   1.1      leo 		SET_TT_REG(NCR5380_ICOM, 0);
    332   1.1      leo 		SET_TT_REG(NCR5380_MODE, mode);
    333   1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_ENABLE;
    334   1.1      leo 		SET_TT_REG(NCR5380_IRCV, 0);
    335   1.1      leo 	}
    336   1.1      leo 	else {
    337   1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_OUT;
    338  1.18      leo 		if (machineid & ATARI_HADES)
    339  1.18      leo 		    SCSI_DMA->s_hdma_ctrl &= ~(SDH_BUSERR|SDH_EOP);
    340   1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
    341   1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
    342   1.1      leo 		SET_TT_REG(NCR5380_MODE, mode);
    343   1.1      leo 		SET_TT_REG(NCR5380_ICOM, SC_ADTB);
    344   1.1      leo 		SET_TT_REG(NCR5380_DMSTAT, 0);
    345   1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT;
    346   1.1      leo 	}
    347   1.1      leo }
    348   1.1      leo 
    349   1.1      leo static int
    350   1.8      leo tt_poll_edma(SC_REQ *reqp)
    351   1.1      leo {
    352   1.1      leo 	u_char	dmstat, dmastat;
    353   1.1      leo 	int	timeout = 9000; /* XXX */
    354   1.1      leo 
    355   1.1      leo 	/*
    356   1.1      leo 	 * We wait here until the DMA has finished. This can be
    357   1.1      leo 	 * achieved by checking the following conditions:
    358   1.1      leo 	 *   - 5380:
    359   1.1      leo 	 *	- End of DMA flag is set
    360   1.1      leo 	 *	- We lost BSY (error!!)
    361   1.1      leo 	 *	- A phase mismatch has occured (partial transfer)
    362   1.1      leo 	 *   - DMA-controller:
    363   1.1      leo 	 *	- A bus error occurred (Kernel error!!)
    364   1.1      leo 	 *	- All bytes are transferred
    365   1.1      leo 	 * If one of the terminating conditions was met, we call
    366   1.1      leo 	 * 'dma_ready' to check errors and perform the bookkeeping.
    367   1.1      leo 	 */
    368   1.1      leo 
    369   1.1      leo 	for (;;) {
    370   1.1      leo 		delay(20);
    371   1.1      leo 		if (--timeout <= 0) {
    372   1.1      leo 			ncr_tprint(reqp, "timeout on polled transfer\n");
    373  1.11      leo 			reqp->xs->error = XS_TIMEOUT;
    374   1.1      leo 			return(0);
    375   1.1      leo 		}
    376   1.1      leo 		dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    377   1.1      leo 		dmastat = SCSI_DMA->s_dma_ctrl;
    378   1.1      leo 		if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET))
    379   1.1      leo 			break;
    380   1.1      leo 		if (!(dmstat & SC_PHS_MTCH))
    381   1.1      leo 			break;
    382   1.1      leo 		if (dmastat & (SD_BUSERR|SD_ZERO))
    383   1.1      leo 			break;
    384   1.1      leo 	}
    385   1.1      leo 	return(1);
    386   1.1      leo }
    387   1.1      leo 
    388   1.2      leo /*
    389   1.2      leo  * Convert physical DMA address to a virtual address.
    390   1.2      leo  */
    391   1.2      leo static u_char *
    392   1.8      leo ptov(SC_REQ *reqp, u_long *phaddr)
    393   1.2      leo {
    394   1.2      leo 	struct dma_chain	*dm;
    395   1.2      leo 	u_char			*vaddr;
    396   1.2      leo 
    397   1.2      leo 	dm = reqp->dm_chain;
    398   1.2      leo 	vaddr = reqp->xdata_ptr;
    399   1.2      leo 	for(; dm < reqp->dm_cur; dm++)
    400   1.2      leo 		vaddr += dm->dm_count;
    401   1.2      leo 	vaddr += (u_long)phaddr - dm->dm_addr;
    402   1.2      leo 	return(vaddr);
    403   1.2      leo }
    404   1.2      leo 
    405   1.1      leo static int
    406   1.8      leo tt_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
    407   1.1      leo {
    408   1.1      leo 	int	dmastat, dmstat;
    409   1.1      leo 	u_char	*byte_p;
    410   1.1      leo 	u_long	leftover;
    411   1.1      leo 
    412   1.1      leo 	dmastat = SCSI_DMA->s_dma_ctrl;
    413   1.1      leo 	dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    414   1.1      leo 	get_scsi_dma(SCSI_DMA->s_dma_cnt, leftover);
    415   1.1      leo 	get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)byte_p);
    416   1.1      leo 
    417   1.1      leo 	if (dmastat & SD_BUSERR) {
    418   1.1      leo 		/*
    419   1.1      leo 		 * The DMA-controller seems to access 8 bytes beyond
    420   1.1      leo 		 * it's limits on output. Therefore check also the byte
    421   1.1      leo 		 * count. If it's zero, ignore the bus error.
    422   1.1      leo 		 */
    423   1.1      leo 		if (leftover != 0) {
    424   1.1      leo 			ncr_tprint(reqp,
    425   1.1      leo 				"SCSI-DMA buserror - accessing 0x%x\n", byte_p);
    426   1.1      leo 			reqp->xs->error = XS_DRIVER_STUFFUP;
    427   1.1      leo 		}
    428   1.1      leo 	}
    429   1.1      leo 
    430   1.1      leo 	/*
    431   1.1      leo 	 * We handle the following special condition below:
    432   1.1      leo 	 *  -- The device disconnects in the middle of a write operation --
    433   1.1      leo 	 * In this case, the 5380 has already pre-fetched the next byte from
    434   1.1      leo 	 * the DMA-controller before the phase mismatch occurs. Therefore,
    435   1.1      leo 	 * leftover is 1 too low.
    436   1.1      leo 	 * This does not always happen! Therefore, we only do this when
    437   1.1      leo 	 * leftover is odd. This assumes that DMA transfers are _even_! This
    438   1.1      leo 	 * is normally the case on disks and types but might not always be.
    439   1.1      leo 	 * XXX: Check if ACK is consistently high on these occasions LWP
    440   1.1      leo 	 */
    441   1.1      leo 	if ((leftover & 1) && !(dmstat & SC_PHS_MTCH) && PH_OUT(reqp->phase))
    442   1.1      leo 		leftover++;
    443   1.1      leo 
    444   1.1      leo 	/*
    445   1.1      leo 	 * Check if there are some 'restbytes' left in the DMA-controller.
    446   1.1      leo 	 */
    447  1.18      leo 	if ((machineid & ATARI_TT) && ((u_long)byte_p & 3)
    448  1.18      leo 	    && PH_IN(reqp->phase)) {
    449   1.1      leo 		u_char	*p, *q;
    450   1.1      leo 
    451   1.8      leo 		p = ptov(reqp, (u_long *)((u_long)byte_p & ~3));
    452   1.1      leo 		q = (u_char*)&(SCSI_DMA->s_dma_res);
    453   1.2      leo 		switch ((u_long)byte_p & 3) {
    454   1.1      leo 			case 3: *p++ = *q++;
    455   1.1      leo 			case 2: *p++ = *q++;
    456   1.1      leo 			case 1: *p++ = *q++;
    457   1.1      leo 		}
    458   1.1      leo 	}
    459   1.1      leo 	*bytes_left = leftover;
    460   1.1      leo 	return ((dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0);
    461   1.1      leo }
    462   1.1      leo 
    463  1.18      leo static u_char *dma_ptr;
    464  1.18      leo static int dbgflag = 3;
    465  1.18      leo void
    466  1.18      leo ncr5380_drq_intr()
    467  1.18      leo {
    468  1.18      leo extern	int			*nofault;
    469  1.18      leo 	label_t			faultbuf;
    470  1.18      leo 	int			write;
    471  1.18      leo 	u_long	 		count;
    472  1.18      leo 	u_char			*data_p = (u_char*)(stio_addr+0x741);
    473  1.18      leo 
    474  1.18      leo 	/*
    475  1.18      leo 	 * Block SCSI interrupts while emulating DMA. They come
    476  1.18      leo 	 * at a higher priority.
    477  1.18      leo 	 */
    478  1.18      leo 	single_inst_bclr_b(MFP2->mf_imra, ~IA_SCSI);
    479  1.18      leo 
    480  1.18      leo 	/*
    481  1.18      leo 	 * Setup for a possible bus error caused by SCSI controller
    482  1.18      leo 	 * switching out of DATA-IN/OUT before we're done with the
    483  1.18      leo 	 * current transfer.
    484  1.18      leo 	 */
    485  1.18      leo 	nofault = (int *) &faultbuf;
    486  1.18      leo 
    487  1.18      leo 	if (setjmp((label_t *) nofault)) {
    488  1.18      leo 		u_char	*ptr;
    489  1.18      leo 		u_long	cnt, tmp;
    490  1.18      leo 
    491  1.18      leo 		PID("drq berr");
    492  1.18      leo 		nofault = (int *) 0;
    493  1.18      leo 
    494  1.18      leo 		/*
    495  1.18      leo 		 * Determine number of bytes transferred
    496  1.18      leo 		 */
    497  1.18      leo 		get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)ptr);
    498  1.18      leo 		cnt = dma_ptr - ptr;
    499  1.18      leo 
    500  1.18      leo 		if (cnt != 0) {
    501  1.18      leo 			/*
    502  1.18      leo 			 * Update the dma pointer/count fields
    503  1.18      leo 			 */
    504  1.18      leo 			set_scsi_dma(SCSI_DMA->s_dma_ptr, dma_ptr);
    505  1.18      leo 			get_scsi_dma(SCSI_DMA->s_dma_cnt, tmp);
    506  1.18      leo 			set_scsi_dma(SCSI_DMA->s_dma_cnt, tmp - cnt);
    507  1.18      leo 
    508  1.18      leo 			if (tmp > cnt) {
    509  1.18      leo 				/*
    510  1.18      leo 				 * Still more to transfer
    511  1.18      leo 				 */
    512  1.18      leo 				single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    513  1.18      leo 				return;
    514  1.18      leo 			}
    515  1.18      leo 
    516  1.18      leo 			/*
    517  1.18      leo 			 * Signal EOP to 5380
    518  1.18      leo 			 */
    519  1.18      leo 			SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
    520  1.18      leo 		}
    521  1.18      leo 		else {
    522  1.18      leo 			/*
    523  1.18      leo 			 * Set the bus-error bit
    524  1.18      leo 			 */
    525  1.18      leo 			SCSI_DMA->s_hdma_ctrl |= SDH_BUSERR;
    526  1.18      leo 		}
    527  1.18      leo 
    528  1.18      leo 		/*
    529  1.18      leo 		 * Schedule an interrupt
    530  1.18      leo 		 */
    531  1.18      leo 		if (SCSI_DMA->s_dma_ctrl & SD_ENABLE)
    532  1.18      leo 		    add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0);
    533  1.18      leo 
    534  1.18      leo 		/*
    535  1.18      leo 		 * Clear DMA-mode
    536  1.18      leo 		 */
    537  1.18      leo 		SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
    538  1.18      leo 		single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    539  1.18      leo 
    540  1.18      leo 		return;
    541  1.18      leo 	}
    542  1.18      leo 
    543  1.18      leo 	write = (SCSI_DMA->s_dma_ctrl & SD_OUT) ? 1 : 0;
    544  1.18      leo #if DBG_PID
    545  1.18      leo 	if (write) {
    546  1.18      leo 		PID("drq (in)");
    547  1.18      leo 	} else {
    548  1.18      leo 		PID("drq (out)");
    549  1.18      leo 	}
    550  1.18      leo #endif
    551  1.18      leo 
    552  1.18      leo 	get_scsi_dma(SCSI_DMA->s_dma_cnt, count);
    553  1.18      leo 	get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr);
    554  1.18      leo 
    555  1.18      leo 	/*
    556  1.18      leo 	 * Keep pushing bytes until we're done or a bus-error
    557  1.18      leo 	 * signals that the SCSI controller is not ready.
    558  1.18      leo 	 * NOTE: I tried some optimalizations in these loops,
    559  1.18      leo 	 *       but they had no effect on transfer speed.
    560  1.18      leo 	 */
    561  1.18      leo 	if (write) {
    562  1.18      leo 		while(count--) {
    563  1.18      leo 			*data_p = *dma_ptr++;
    564  1.18      leo 		}
    565  1.18      leo 	}
    566  1.18      leo 	else {
    567  1.18      leo 		while(count--) {
    568  1.18      leo 			*dma_ptr++ = *data_p;
    569  1.18      leo 		}
    570  1.18      leo 	}
    571  1.18      leo 
    572  1.18      leo 	/*
    573  1.18      leo 	 * Schedule an interrupt
    574  1.18      leo 	 */
    575  1.18      leo 	if (SCSI_DMA->s_dma_ctrl & SD_ENABLE)
    576  1.18      leo 	    add_sicallback((si_farg)ncr_dma_intr, (void *)cur_softc, 0);
    577  1.18      leo 
    578  1.18      leo 	/*
    579  1.18      leo 	 * Clear DMA-mode
    580  1.18      leo 	 */
    581  1.18      leo 	SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
    582  1.18      leo 
    583  1.18      leo 	/*
    584  1.18      leo 	 * OK.  No bus error occurred above.  Clear the nofault flag
    585  1.18      leo 	 * so we no longer short-circuit bus errors.
    586  1.18      leo 	 */
    587  1.18      leo 	nofault = (int *) 0;
    588  1.18      leo 
    589  1.18      leo 	/*
    590  1.18      leo 	 * Update the DMA 'registers' to reflect that all bytes
    591  1.18      leo 	 * have been transfered and tell this to the 5380 too.
    592  1.18      leo 	 */
    593  1.18      leo 	set_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)dma_ptr);
    594  1.18      leo 	set_scsi_dma(SCSI_DMA->s_dma_cnt, 0);
    595  1.18      leo 	SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
    596  1.18      leo 
    597  1.18      leo 	PID("end drq");
    598  1.18      leo 	single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
    599  1.18      leo 
    600  1.18      leo 	return;
    601  1.18      leo }
    602  1.18      leo 
    603   1.1      leo #endif /* defined(TT_SCSI) */
    604   1.1      leo 
    605   1.1      leo #if defined(FALCON_SCSI) && !defined(TT_SCSI)
    606   1.1      leo 
    607   1.1      leo #define	GET_5380_REG(rnum)	get_falcon_5380_reg(rnum)
    608   1.1      leo #define	SET_5380_REG(rnum,val)	set_falcon_5380_reg(rnum, val)
    609   1.1      leo #define scsi_mach_init(sc)	scsi_falcon_init(sc)
    610   1.1      leo #define scsi_ienable()		scsi_falcon_ienable()
    611   1.1      leo #define scsi_idisable()		scsi_falcon_idisable()
    612   1.1      leo #define	scsi_clr_ipend()	scsi_falcon_clr_ipend()
    613   1.7      leo #define	scsi_ipending()		scsi_falcon_ipending()
    614   1.1      leo #define scsi_dma_setup(r,p,m)	scsi_falcon_dmasetup(r, p, m)
    615   1.1      leo #define wrong_dma_range(r,d)	falcon_wrong_dma_range(r, d)
    616   1.1      leo #define poll_edma(reqp)		falcon_poll_edma(reqp)
    617   1.1      leo #define get_dma_result(r, b)	falcon_get_dma_result(r, b)
    618  1.16      leo #define	can_access_5380()	falcon_can_access_5380()
    619  1.18      leo #define	emulated_dma()		0
    620   1.1      leo 
    621   1.1      leo #define fair_to_keep_dma()	(!st_dmawanted())
    622   1.1      leo #define claimed_dma()		falcon_claimed_dma()
    623   1.1      leo #define reconsider_dma()	falcon_reconsider_dma()
    624   1.1      leo 
    625   1.1      leo #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */
    626   1.1      leo 
    627   1.1      leo #if defined(FALCON_SCSI)
    628   1.1      leo 
    629   1.9      leo /*
    630   1.9      leo  * Prototype functions defined below
    631   1.9      leo  */
    632   1.9      leo static void	scsi_falcon_init __P((struct ncr_softc *));
    633   1.9      leo static u_char	get_falcon_5380_reg __P((u_short));
    634   1.9      leo static void	set_falcon_5380_reg __P((u_short, u_short));
    635   1.9      leo static int	falcon_wrong_dma_range __P((SC_REQ *, struct dma_chain *));
    636   1.9      leo static void	fal1_dma __P((u_int, u_int, SC_REQ *));
    637   1.9      leo static void	scsi_falcon_dmasetup __P((SC_REQ  *, u_int, u_char));
    638   1.9      leo static int	falcon_poll_edma __P((SC_REQ  *));
    639   1.9      leo static int	falcon_get_dma_result __P((SC_REQ  *, u_long *));
    640  1.16      leo        int	falcon_can_access_5380 __P((void));
    641  1.13      leo        void	scsi_falcon_clr_ipend __P((void));
    642  1.13      leo        void	scsi_falcon_idisable __P((void));
    643   1.9      leo        void	scsi_falcon_ienable __P((void));
    644  1.13      leo        int	scsi_falcon_ipending __P((void));
    645  1.13      leo        int	falcon_claimed_dma __P((void));
    646  1.13      leo        void	falcon_reconsider_dma __P((void));
    647   1.1      leo 
    648   1.9      leo static void
    649   1.9      leo scsi_falcon_init(sc)
    650   1.1      leo struct ncr_softc	*sc;
    651   1.1      leo {
    652   1.1      leo 	/*
    653   1.1      leo 	 * Enable disk related interrupts
    654   1.1      leo 	 */
    655   1.1      leo 	MFP->mf_ierb  |= IB_DINT;
    656   1.1      leo 	MFP->mf_iprb  &= ~IB_DINT;
    657   1.1      leo 	MFP->mf_imrb  |= IB_DINT;
    658   1.1      leo }
    659   1.1      leo 
    660   1.9      leo static u_char
    661   1.9      leo get_falcon_5380_reg(rnum)
    662   1.1      leo u_short	rnum;
    663   1.1      leo {
    664   1.1      leo 	DMA->dma_mode = DMA_SCSI + rnum;
    665   1.1      leo 	return(DMA->dma_data);
    666   1.1      leo }
    667   1.1      leo 
    668   1.9      leo static void
    669   1.9      leo set_falcon_5380_reg(rnum, val)
    670   1.1      leo u_short	rnum, val;
    671   1.1      leo {
    672   1.1      leo 	DMA->dma_mode = DMA_SCSI + rnum;
    673   1.1      leo 	DMA->dma_data = val;
    674   1.1      leo }
    675   1.1      leo 
    676   1.9      leo extern __inline__ void
    677   1.9      leo scsi_falcon_ienable()
    678   1.1      leo {
    679  1.18      leo 	single_inst_bset_b(MFP->mf_ierb, IB_DINT);
    680   1.1      leo }
    681   1.1      leo 
    682   1.9      leo extern __inline__ void
    683   1.9      leo scsi_falcon_idisable()
    684   1.1      leo {
    685  1.18      leo 	single_inst_bclr_b(MFP->mf_ierb, ~IB_DINT);
    686   1.1      leo }
    687   1.1      leo 
    688   1.9      leo extern __inline__ void
    689   1.9      leo scsi_falcon_clr_ipend()
    690   1.1      leo {
    691   1.1      leo 	int	tmp;
    692   1.1      leo 
    693   1.1      leo 	tmp = get_falcon_5380_reg(NCR5380_IRCV);
    694  1.15      leo 	rem_sicallback((si_farg)ncr_ctrl_intr);
    695   1.1      leo }
    696   1.1      leo 
    697   1.9      leo extern __inline__ int
    698   1.9      leo scsi_falcon_ipending()
    699   1.7      leo {
    700  1.13      leo 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
    701  1.13      leo 		/*
    702  1.13      leo 		 *  XXX: When DMA is running, we are only allowed to
    703  1.13      leo 		 *       check the 5380 when DMA _might_ be finished.
    704  1.13      leo 		 */
    705  1.13      leo 		if (MFP->mf_gpip & IO_DINT)
    706  1.13      leo 		    return (0); /* XXX: Actually: we're not allowed to check */
    707  1.16      leo 
    708  1.16      leo 		/* LWP: 28-06, must be a dma interrupt! should the
    709  1.16      leo 		 * ST-DMA unit be taken out of dma mode?????
    710  1.16      leo 		 */
    711  1.16      leo 		DMA->dma_mode = 0x90;
    712  1.16      leo 
    713  1.13      leo 	}
    714  1.13      leo 	return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET);
    715   1.7      leo }
    716   1.7      leo 
    717   1.9      leo static int
    718   1.9      leo falcon_wrong_dma_range(reqp, dm)
    719   1.1      leo SC_REQ			*reqp;
    720   1.1      leo struct dma_chain	*dm;
    721   1.1      leo {
    722   1.1      leo 	/*
    723   1.1      leo 	 * Do not allow chains yet! See also comment with
    724   1.1      leo 	 * falcon_poll_edma() !!!
    725   1.1      leo 	 */
    726   1.1      leo 	if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
    727   1.1      leo 		reqp->dr_flag |= DRIVER_BOUNCING;
    728   1.1      leo 		return(1);
    729   1.1      leo 	}
    730   1.2      leo 	/*
    731   1.2      leo 	 * Never allow DMA to happen on a Falcon when the transfer
    732   1.2      leo 	 * size is no multiple of 512. This is the transfer unit of the
    733   1.2      leo 	 * ST DMA-controller.
    734   1.2      leo 	 */
    735   1.2      leo 	if(dm->dm_count & 511)
    736   1.2      leo 		return(1);
    737   1.1      leo 	return(0);
    738   1.1      leo }
    739   1.1      leo 
    740   1.1      leo static	int falcon_lock = 0;
    741   1.1      leo 
    742   1.9      leo extern __inline__ int
    743   1.9      leo falcon_claimed_dma()
    744   1.1      leo {
    745  1.11      leo 	if (falcon_lock != DMA_LOCK_GRANT) {
    746  1.11      leo 		if (falcon_lock == DMA_LOCK_REQ) {
    747   1.1      leo 			/*
    748   1.1      leo 			 * DMA access is being claimed.
    749   1.1      leo 			 */
    750   1.1      leo 			return(0);
    751   1.1      leo 		}
    752  1.15      leo 		if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
    753  1.15      leo 						cur_softc, &falcon_lock, 1))
    754   1.1      leo 			return(0);
    755   1.1      leo 	}
    756   1.1      leo 	return(1);
    757   1.1      leo }
    758   1.1      leo 
    759   1.9      leo extern __inline__ void
    760   1.9      leo falcon_reconsider_dma()
    761   1.1      leo {
    762   1.1      leo 	if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
    763   1.1      leo 		/*
    764   1.1      leo 		 * No need to keep DMA locked by us as we are not currently
    765   1.1      leo 		 * connected and no disconnected jobs are pending.
    766   1.1      leo 		 */
    767  1.15      leo 		rem_sicallback((si_farg)ncr_ctrl_intr);
    768  1.15      leo 		st_dmafree(cur_softc, &falcon_lock);
    769   1.1      leo 	}
    770   1.1      leo 
    771   1.1      leo 	if (!falcon_lock && (issue_q != NULL)) {
    772   1.1      leo 		/*
    773   1.1      leo 		 * We must (re)claim DMA access as there are jobs
    774   1.1      leo 		 * waiting in the issue queue.
    775   1.1      leo 		 */
    776  1.15      leo 		st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
    777  1.15      leo 						cur_softc, &falcon_lock, 0);
    778   1.1      leo 	}
    779   1.1      leo }
    780   1.1      leo 
    781   1.9      leo static void
    782   1.9      leo fal1_dma(dir, nsects, reqp)
    783   1.1      leo u_int	dir, nsects;
    784   1.1      leo SC_REQ	*reqp;
    785   1.1      leo {
    786   1.1      leo 	dir <<= 8;
    787   1.1      leo 	st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr);
    788   1.1      leo 	DMA->dma_mode = 0x90 | dir;
    789   1.1      leo 	DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
    790   1.1      leo 	DMA->dma_mode = 0x90 | dir;
    791   1.1      leo 	DMA->dma_data = nsects;
    792   1.7      leo 	delay(2);	/* _really_ needed (Thomas Gerner) */
    793   1.1      leo 	DMA->dma_mode = 0x10 | dir;
    794   1.1      leo }
    795   1.1      leo 
    796   1.9      leo static void
    797   1.9      leo scsi_falcon_dmasetup(reqp, phase, mode)
    798   1.1      leo SC_REQ	*reqp;
    799   1.1      leo u_int	phase;
    800   1.1      leo u_char	mode;
    801   1.1      leo {
    802   1.1      leo 	int	nsects = reqp->dm_cur->dm_count / 512; /* XXX */
    803   1.1      leo 
    804   1.1      leo 	/*
    805   1.1      leo 	 * XXX: We should probably clear the fifo before putting the
    806   1.1      leo 	 *      5380 into DMA-mode.
    807   1.1      leo 	 */
    808   1.1      leo 	if (PH_IN(phase)) {
    809   1.1      leo 		set_falcon_5380_reg(NCR5380_ICOM, 0);
    810   1.1      leo 		set_falcon_5380_reg(NCR5380_MODE, mode);
    811   1.1      leo 		set_falcon_5380_reg(NCR5380_IRCV, 0);
    812   1.1      leo 		fal1_dma(0, nsects, reqp);
    813   1.1      leo 	}
    814   1.1      leo 	else {
    815   1.1      leo 		set_falcon_5380_reg(NCR5380_MODE, mode);
    816   1.1      leo 		set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
    817   1.1      leo 		set_falcon_5380_reg(NCR5380_DMSTAT, 0);
    818   1.1      leo 		fal1_dma(1, nsects, reqp);
    819   1.1      leo 	}
    820   1.1      leo }
    821   1.1      leo 
    822   1.1      leo static int
    823   1.1      leo falcon_poll_edma(reqp)
    824   1.1      leo SC_REQ	*reqp;
    825   1.1      leo {
    826   1.1      leo 	int	timeout = 9000; /* XXX */
    827   1.1      leo 
    828   1.1      leo 	/*
    829   1.1      leo 	 * Because of the Falcon hardware, it is impossible to reach
    830   1.1      leo 	 * the 5380 while doing DMA-transfers. So we have to rely on
    831   1.1      leo 	 * the interrupt line to determine if DMA-has finished. the
    832   1.1      leo 	 * DMA-controller itself will never fire an interrupt. This means
    833   1.1      leo 	 * that 'broken-up' DMA transfers are not (yet) possible on the
    834   1.1      leo 	 * Falcon.
    835   1.1      leo 	 */
    836   1.1      leo 	for (;;) {
    837   1.1      leo 		delay(20);
    838   1.1      leo 		if (--timeout <= 0) {
    839   1.1      leo 			ncr_tprint(reqp, "Timeout on polled transfer\n");
    840  1.11      leo 			reqp->xs->error = XS_TIMEOUT;
    841   1.1      leo 			return(0);
    842   1.1      leo 		}
    843   1.1      leo 		if (!(MFP->mf_gpip & IO_DINT))
    844   1.1      leo 			break;
    845   1.1      leo 	}
    846   1.1      leo 	return(1);
    847   1.1      leo }
    848   1.1      leo 
    849   1.1      leo static int
    850   1.1      leo falcon_get_dma_result(reqp, bytes_left)
    851   1.1      leo SC_REQ	*reqp;
    852   1.1      leo u_long	*bytes_left;
    853   1.1      leo {
    854   1.1      leo 	int	rv = 0;
    855   1.1      leo 	int	st_dmastat;
    856   1.1      leo 	u_long	bytes_done;
    857   1.1      leo 
    858   1.1      leo 	/*
    859   1.1      leo 	 * Select sector counter register first (See Atari docu.)
    860   1.1      leo 	 */
    861   1.1      leo 	DMA->dma_mode = 0x90;
    862   1.1      leo 	if (!(st_dmastat = DMA->dma_stat) & 0x01) {
    863   1.1      leo 		/*
    864   1.1      leo 		 * Misc. DMA-error according to Atari...
    865   1.1      leo 		 */
    866   1.1      leo 		ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
    867   1.1      leo 							st_dmaaddr_get());
    868   1.1      leo 		reqp->xs->error = XS_DRIVER_STUFFUP;
    869   1.1      leo 		rv = 1;
    870   1.1      leo 	}
    871  1.16      leo 	/*
    872  1.16      leo 	 * Because we NEVER start DMA on the Falcon when the data size
    873  1.16      leo 	 * is not a multiple of 512 bytes, we can safely round down the
    874  1.16      leo 	 * byte count on writes. We need to because in case of a disconnect,
    875  1.16      leo 	 * the DMA has already prefetched the next couple of bytes.
    876  1.16      leo 	 * On read, these byte counts are an error. They are logged and
    877  1.16      leo 	 * should be handled by the mi-part of the driver.
    878  1.16      leo 	 * NOTE: We formerly did this by using the 'byte-count-zero' bit
    879  1.16      leo 	 *       of the DMA controller, but this didn't seem to work???
    880  1.16      leo          *       [lwp 29/06/96]
    881  1.16      leo 	 */
    882  1.16      leo 	bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
    883  1.16      leo 	if (bytes_done & 511) {
    884  1.16      leo 		if (PH_IN(reqp->phase)) {
    885  1.16      leo 			ncr_tprint(reqp, "Byte count on read not a multiple "
    886  1.16      leo 					 "of 512 (%ld)\n", bytes_done);
    887   1.1      leo 		}
    888  1.16      leo 		bytes_done &= ~511;
    889   1.1      leo 	}
    890  1.16      leo 	if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0)
    891   1.1      leo 		rv = 1;
    892   1.1      leo 	return(rv);
    893   1.1      leo }
    894   1.1      leo 
    895  1.16      leo static int
    896  1.16      leo falcon_can_access_5380()
    897  1.16      leo {
    898  1.16      leo 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)
    899  1.16      leo 		&& (MFP->mf_gpip & IO_DINT))
    900  1.16      leo 			return(0);
    901  1.16      leo 	return(1);
    902  1.16      leo }
    903  1.16      leo 
    904   1.1      leo #endif /* defined(FALCON_SCSI) */
    905   1.1      leo 
    906   1.1      leo #if defined(TT_SCSI) && defined(FALCON_SCSI)
    907   1.1      leo /*
    908   1.1      leo  * Define some functions to support _both_ TT and Falcon SCSI
    909   1.1      leo  */
    910   1.1      leo 
    911   1.1      leo /*
    912   1.9      leo  * The prototypes first...
    913   1.9      leo  */
    914   1.9      leo static void	scsi_mach_init __P((struct ncr_softc *));
    915   1.9      leo        void	scsi_ienable __P((void));
    916   1.9      leo        void	scsi_idisable __P((void));
    917   1.9      leo        void	scsi_clr_ipend __P((void));
    918   1.9      leo        int	scsi_ipending __P((void));
    919   1.9      leo        void	scsi_dma_setup __P((SC_REQ *, u_int, u_char));
    920   1.9      leo        int	wrong_dma_range __P((SC_REQ *, struct dma_chain *));
    921   1.9      leo        int	poll_edma __P((SC_REQ *));
    922   1.9      leo        int	get_dma_result __P((SC_REQ *, u_long *));
    923  1.16      leo        int	can_access_5380 __P((void));
    924   1.9      leo 
    925   1.9      leo /*
    926   1.1      leo  * Register access will be done through the following 2 function pointers.
    927   1.1      leo  */
    928   1.9      leo static u_char	(*get_5380_reg) __P((u_short));
    929   1.9      leo static void	(*set_5380_reg) __P((u_short, u_short));
    930   1.1      leo 
    931   1.1      leo #define	GET_5380_REG	(*get_5380_reg)
    932   1.1      leo #define	SET_5380_REG	(*set_5380_reg)
    933   1.1      leo 
    934   1.9      leo static void
    935   1.9      leo scsi_mach_init(sc)
    936   1.1      leo struct ncr_softc	*sc;
    937   1.1      leo {
    938   1.1      leo 	if (machineid & ATARI_FALCON) {
    939   1.1      leo 		get_5380_reg = get_falcon_5380_reg;
    940   1.1      leo 		set_5380_reg = set_falcon_5380_reg;
    941   1.1      leo 		scsi_falcon_init(sc);
    942   1.1      leo 	}
    943   1.1      leo 	else {
    944   1.1      leo 		get_5380_reg = get_tt_5380_reg;
    945   1.1      leo 		set_5380_reg = set_tt_5380_reg;
    946   1.1      leo 		scsi_tt_init(sc);
    947   1.1      leo 	}
    948   1.1      leo }
    949   1.1      leo 
    950   1.9      leo extern __inline__ void
    951   1.9      leo scsi_ienable()
    952   1.1      leo {
    953   1.1      leo 	if (machineid & ATARI_FALCON)
    954   1.1      leo 		scsi_falcon_ienable();
    955   1.1      leo 	else scsi_tt_ienable();
    956   1.1      leo }
    957   1.1      leo 
    958   1.9      leo extern __inline__ void
    959   1.9      leo scsi_idisable()
    960   1.1      leo {
    961   1.1      leo 	if (machineid & ATARI_FALCON)
    962   1.1      leo 		scsi_falcon_idisable();
    963   1.1      leo 	else scsi_tt_idisable();
    964   1.1      leo }
    965   1.1      leo 
    966   1.9      leo extern __inline__ void
    967   1.9      leo scsi_clr_ipend()
    968   1.1      leo {
    969   1.1      leo 	if (machineid & ATARI_FALCON)
    970   1.1      leo 		scsi_falcon_clr_ipend();
    971   1.1      leo 	else scsi_tt_clr_ipend();
    972   1.7      leo }
    973   1.7      leo 
    974   1.9      leo extern __inline__ int
    975   1.9      leo scsi_ipending()
    976   1.7      leo {
    977   1.7      leo 	if (machineid & ATARI_FALCON)
    978   1.7      leo 		return(scsi_falcon_ipending());
    979   1.7      leo 	else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
    980   1.1      leo }
    981   1.1      leo 
    982   1.9      leo extern __inline__ void
    983   1.9      leo scsi_dma_setup(reqp, phase, mbase)
    984   1.1      leo SC_REQ	*reqp;
    985   1.1      leo u_int	phase;
    986   1.1      leo u_char	mbase;
    987   1.1      leo {
    988   1.1      leo 	if (machineid & ATARI_FALCON)
    989   1.1      leo 		scsi_falcon_dmasetup(reqp, phase, mbase);
    990   1.1      leo 	else scsi_tt_dmasetup(reqp, phase, mbase);
    991   1.1      leo }
    992   1.1      leo 
    993   1.9      leo extern __inline__ int
    994   1.9      leo wrong_dma_range(reqp, dm)
    995   1.1      leo SC_REQ			*reqp;
    996   1.1      leo struct dma_chain	*dm;
    997   1.1      leo {
    998   1.1      leo 	if (machineid & ATARI_FALCON)
    999   1.1      leo 		return(falcon_wrong_dma_range(reqp, dm));
   1000   1.1      leo 	else return(tt_wrong_dma_range(reqp, dm));
   1001   1.1      leo }
   1002   1.1      leo 
   1003   1.9      leo extern __inline__ int
   1004   1.9      leo poll_edma(reqp)
   1005   1.1      leo SC_REQ	*reqp;
   1006   1.1      leo {
   1007   1.1      leo 	if (machineid & ATARI_FALCON)
   1008   1.1      leo 		return(falcon_poll_edma(reqp));
   1009   1.1      leo 	else return(tt_poll_edma(reqp));
   1010   1.1      leo }
   1011   1.1      leo 
   1012   1.9      leo extern __inline__ int
   1013   1.9      leo get_dma_result(reqp, bytes_left)
   1014   1.1      leo SC_REQ	*reqp;
   1015   1.1      leo u_long	*bytes_left;
   1016   1.1      leo {
   1017   1.1      leo 	if (machineid & ATARI_FALCON)
   1018   1.1      leo 		return(falcon_get_dma_result(reqp, bytes_left));
   1019   1.1      leo 	else return(tt_get_dma_result(reqp, bytes_left));
   1020   1.1      leo }
   1021   1.1      leo 
   1022  1.16      leo extern __inline__ int
   1023  1.16      leo can_access_5380()
   1024  1.16      leo {
   1025  1.16      leo 	if (machineid & ATARI_FALCON)
   1026  1.16      leo 		return(falcon_can_access_5380());
   1027  1.16      leo 	return(1);
   1028  1.16      leo }
   1029  1.18      leo 
   1030  1.18      leo #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
   1031  1.16      leo 
   1032   1.1      leo /*
   1033   1.1      leo  * Locking stuff. All turns into NOP's on the TT.
   1034   1.1      leo  */
   1035   1.1      leo #define	fair_to_keep_dma()	((machineid & ATARI_FALCON) ?		\
   1036   1.1      leo 						!st_dmawanted() : 1)
   1037   1.1      leo #define	claimed_dma()		((machineid & ATARI_FALCON) ?		\
   1038   1.1      leo 						falcon_claimed_dma() : 1)
   1039   1.1      leo #define reconsider_dma()	{					\
   1040   1.1      leo 					if(machineid & ATARI_FALCON)	\
   1041   1.1      leo 						falcon_reconsider_dma();\
   1042   1.1      leo 				}
   1043   1.1      leo #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
   1044   1.1      leo 
   1045   1.1      leo /**********************************************
   1046   1.1      leo  * Functions present for both TT and Falcon.  *
   1047   1.1      leo  **********************************************/
   1048   1.1      leo /*
   1049   1.1      leo  * Our autoconfig matching function
   1050   1.1      leo  */
   1051   1.1      leo static int
   1052  1.10  thorpej machine_match(struct device *pdp, void *match, void *auxp,
   1053   1.8      leo 						struct cfdriver *cd)
   1054   1.1      leo {
   1055  1.10  thorpej 	struct cfdata *cdp = match;
   1056  1.10  thorpej 
   1057   1.1      leo 	if (strcmp(auxp, cd->cd_name))
   1058   1.1      leo 		return(0);
   1059   1.1      leo 	if (cdp->cf_unit != 0)	/* Only one unit	*/
   1060   1.1      leo 		return(0);
   1061   1.1      leo 	return(1);
   1062   1.1      leo }
   1063   1.1      leo 
   1064   1.1      leo /*
   1065   1.1      leo  * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
   1066   1.1      leo  * pool. Allocation here is both contiguous and in the lower 16Mb of
   1067   1.1      leo  * the address space. Thus being DMA-able for all controllers.
   1068   1.1      leo  */
   1069   1.1      leo static u_char *
   1070   1.8      leo alloc_bounceb(u_long len)
   1071   1.1      leo {
   1072  1.12      leo 	void	*tmp;
   1073   1.1      leo 
   1074   1.1      leo 	return((u_char *)alloc_stmem(len, &tmp));
   1075   1.1      leo }
   1076   1.1      leo 
   1077   1.1      leo static void
   1078   1.8      leo free_bounceb(u_char *bounceb)
   1079   1.1      leo {
   1080   1.1      leo 	free_stmem(bounceb);
   1081   1.1      leo }
   1082   1.1      leo 
   1083   1.1      leo /*
   1084   1.1      leo  * 5380 interrupt.
   1085   1.1      leo  */
   1086   1.8      leo void
   1087   1.8      leo scsi_ctrl(int sr)
   1088   1.1      leo {
   1089   1.1      leo 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
   1090   1.1      leo 		scsi_idisable();
   1091   1.1      leo 		if (!BASEPRI(sr))
   1092  1.12      leo 			add_sicallback((si_farg)ncr_ctrl_intr,
   1093  1.12      leo 						(void *)cur_softc, 0);
   1094   1.1      leo 		else {
   1095   1.1      leo 			spl1();
   1096   1.1      leo 			ncr_ctrl_intr(cur_softc);
   1097  1.16      leo 			spl0();
   1098   1.1      leo 		}
   1099   1.1      leo 	}
   1100   1.1      leo }
   1101   1.1      leo 
   1102   1.1      leo /*
   1103   1.1      leo  * DMA controller interrupt
   1104   1.1      leo  */
   1105   1.8      leo void
   1106   1.8      leo scsi_dma(int sr)
   1107   1.1      leo {
   1108   1.1      leo 	SC_REQ	*reqp;
   1109   1.1      leo 
   1110   1.1      leo 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
   1111   1.1      leo 		scsi_idisable();
   1112   1.1      leo 		if (!BASEPRI(sr))
   1113  1.12      leo 			add_sicallback((si_farg)ncr_dma_intr,
   1114  1.12      leo 					(void *)cur_softc, 0);
   1115   1.1      leo 		else {
   1116   1.1      leo 			spl1();
   1117   1.1      leo 			ncr_dma_intr(cur_softc);
   1118  1.16      leo 			spl0();
   1119   1.1      leo 		}
   1120   1.1      leo 	}
   1121   1.1      leo }
   1122   1.1      leo 
   1123   1.1      leo /*
   1124   1.1      leo  * Last but not least... Include the general driver code
   1125   1.1      leo  */
   1126   1.6  mycroft #include <atari/dev/ncr5380.c>
   1127