Home | History | Annotate | Line # | Download | only in dev
atari5380.c revision 1.7
      1  1.7      leo /*	$NetBSD: atari5380.c,v 1.7 1996/02/14 08:09:47 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.1      leo /*
     43  1.1      leo  * Include the driver definitions
     44  1.1      leo  */
     45  1.1      leo #include <atari/dev/ncr5380reg.h>
     46  1.1      leo 
     47  1.1      leo #include <machine/stdarg.h>
     48  1.1      leo #include <machine/iomap.h>
     49  1.1      leo #include <machine/mfp.h>
     50  1.1      leo 
     51  1.1      leo #if defined(FALCON_SCSI)
     52  1.1      leo #include <machine/dma.h>
     53  1.1      leo #endif
     54  1.1      leo 
     55  1.1      leo /*
     56  1.1      leo  * This is crap, but because the interrupts now run at MFP spl-level (6),
     57  1.1      leo  * splbio() is not enough at some places. The code should be checked to
     58  1.1      leo  * find out where splhigh() is needed and where splbio() should be used.
     59  1.1      leo  * Now that I use this interrupt sceme, the spl values are fake!
     60  1.1      leo  */
     61  1.1      leo #undef splbio()
     62  1.1      leo #define splbio()	splhigh()
     63  1.1      leo 
     64  1.1      leo /*
     65  1.1      leo  * Set the various driver options
     66  1.1      leo  */
     67  1.1      leo #define	NREQ		18	/* Size of issue queue			*/
     68  1.1      leo #define	AUTO_SENSE	1	/* Automatically issue a request-sense 	*/
     69  1.1      leo 
     70  1.2      leo #define	DRNAME		ncrscsi	/* used in various prints		*/
     71  1.1      leo #undef	DBG_SEL			/* Show the selection process		*/
     72  1.1      leo #undef	DBG_REQ			/* Show enqueued/ready requests		*/
     73  1.2      leo #undef	DBG_ERR_RET		/* Show requests with != 0 return code	*/
     74  1.1      leo #undef	DBG_NOWRITE		/* Do not allow writes to the targets	*/
     75  1.1      leo #undef	DBG_PIO			/* Show the polled-I/O process		*/
     76  1.1      leo #undef	DBG_INF			/* Show information transfer process	*/
     77  1.1      leo #define	DBG_NOSTATIC		/* No static functions, all in DDB trace*/
     78  1.7      leo #define	DBG_PID		25	/* Keep track of driver			*/
     79  1.1      leo #define	REAL_DMA		/* Use DMA if sensible			*/
     80  1.1      leo #if defined(FALCON_SCSI)
     81  1.1      leo #define	REAL_DMA_POLL	1	/* 1: Poll for end of DMA-transfer	*/
     82  1.1      leo #else
     83  1.1      leo #define	REAL_DMA_POLL	0	/* 1: Poll for end of DMA-transfer	*/
     84  1.1      leo #endif
     85  1.1      leo #undef	USE_PDMA		/* Use special pdma-transfer function	*/
     86  1.4      leo #define MIN_PHYS	65536	/*BARF!!!!*/
     87  1.1      leo 
     88  1.1      leo /*
     89  1.1      leo  * The atari specific driver options
     90  1.1      leo  */
     91  1.1      leo #undef	NO_TTRAM_DMA		/* Do not use DMA to TT-ram. This	*/
     92  1.1      leo 				/*    fails on older atari's		*/
     93  1.1      leo #define	ENABLE_NCR5380(sc)	cur_softc = sc;
     94  1.1      leo 
     95  1.3      leo /*
     96  1.3      leo  * Functions that do nothing on the atari
     97  1.3      leo  */
     98  1.3      leo #define	pdma_ready()		0
     99  1.1      leo 
    100  1.1      leo #if defined(TT_SCSI)
    101  1.1      leo /*
    102  1.1      leo  * Define all the things we need of the DMA-controller
    103  1.1      leo  */
    104  1.1      leo #define	SCSI_DMA	((struct scsi_dma *)AD_SCSI_DMA)
    105  1.1      leo #define	SCSI_5380	((struct scsi_5380 *)AD_NCR5380)
    106  1.1      leo 
    107  1.1      leo struct scsi_dma {
    108  1.1      leo 	volatile u_char		s_dma_ptr[8];	/* use only the odd bytes */
    109  1.1      leo 	volatile u_char		s_dma_cnt[8];	/* use only the odd bytes */
    110  1.1      leo 	volatile u_char		s_dma_res[4];	/* data residue register  */
    111  1.1      leo 	volatile u_char		s_dma_gap;	/* not used		  */
    112  1.1      leo 	volatile u_char		s_dma_ctrl;	/* control register	  */
    113  1.1      leo };
    114  1.1      leo 
    115  1.1      leo #define	set_scsi_dma(addr, val)	(void)(					\
    116  1.1      leo 	{								\
    117  1.1      leo 	u_char	*address = (u_char*)addr+1;				\
    118  1.1      leo 	u_long	nval	 = (u_long)val;					\
    119  1.1      leo 	__asm("movepl	%0, %1@(0)": :"d" (nval), "a" (address));	\
    120  1.1      leo 	})
    121  1.1      leo 
    122  1.1      leo #define	get_scsi_dma(addr, res)	(					\
    123  1.1      leo 	{								\
    124  1.1      leo 	u_char	*address = (u_char*)addr+1;				\
    125  1.1      leo 	u_long	nval;							\
    126  1.1      leo 	__asm("movepl	%1@(0), %0": "=d" (nval) : "a" (address));	\
    127  1.1      leo 	res = (u_long)nval;						\
    128  1.1      leo 	})
    129  1.1      leo 
    130  1.1      leo /*
    131  1.1      leo  * Defines for TT-DMA control register
    132  1.1      leo  */
    133  1.1      leo #define	SD_BUSERR	0x80		/* 1 = transfer caused bus error*/
    134  1.1      leo #define	SD_ZERO		0x40		/* 1 = byte counter is zero	*/
    135  1.1      leo #define	SD_ENABLE	0x02		/* 1 = Enable DMA		*/
    136  1.1      leo #define	SD_OUT		0x01		/* Direction: memory to SCSI	*/
    137  1.1      leo #define	SD_IN		0x00		/* Direction: SCSI to memory	*/
    138  1.1      leo 
    139  1.1      leo /*
    140  1.1      leo  * Define the 5380 register set
    141  1.1      leo  */
    142  1.1      leo struct scsi_5380 {
    143  1.1      leo 	volatile u_char	scsi_5380[16];	/* use only the odd bytes	*/
    144  1.1      leo };
    145  1.1      leo #endif /* TT_SCSI */
    146  1.1      leo 
    147  1.1      leo /**********************************************
    148  1.1      leo  * Variables present for both TT and Falcon.  *
    149  1.1      leo  **********************************************/
    150  1.1      leo 
    151  1.1      leo /*
    152  1.1      leo  * Softc of currently active controller (a bit of fake; we only have one)
    153  1.1      leo  */
    154  1.1      leo static struct ncr_softc	*cur_softc;
    155  1.1      leo 
    156  1.1      leo #if defined(TT_SCSI) && !defined(FALCON_SCSI)
    157  1.1      leo /*
    158  1.1      leo  * We can be more efficient for some functions when only TT_SCSI is selected
    159  1.1      leo  */
    160  1.1      leo #define	GET_5380_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
    161  1.1      leo #define	SET_5380_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
    162  1.1      leo 
    163  1.1      leo #define scsi_mach_init(sc)	scsi_tt_init(sc)
    164  1.1      leo #define scsi_ienable()		scsi_tt_ienable()
    165  1.1      leo #define scsi_idisable()		scsi_tt_idisable()
    166  1.1      leo #define	scsi_clr_ipend()	scsi_tt_clr_ipend()
    167  1.7      leo #define scsi_ipending()		(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
    168  1.1      leo #define scsi_dma_setup(r,p,m)	scsi_tt_dmasetup(r, p, m)
    169  1.1      leo #define wrong_dma_range(r,d)	tt_wrong_dma_range(r, d)
    170  1.1      leo #define poll_edma(reqp)		tt_poll_edma(reqp)
    171  1.1      leo #define get_dma_result(r, b)	tt_get_dma_result(r, b)
    172  1.1      leo 
    173  1.1      leo #define fair_to_keep_dma()	1
    174  1.1      leo #define claimed_dma()		1
    175  1.1      leo #define reconsider_dma()
    176  1.1      leo 
    177  1.1      leo #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
    178  1.1      leo 
    179  1.1      leo #if defined(TT_SCSI)
    180  1.1      leo 
    181  1.1      leo /*
    182  1.1      leo  * Define these too, so we can use them locally...
    183  1.1      leo  */
    184  1.1      leo #define	GET_TT_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
    185  1.1      leo #define	SET_TT_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
    186  1.1      leo 
    187  1.1      leo #ifdef NO_TTRAM_DMA
    188  1.1      leo static int tt_wrong_dma_range(reqp, dm)
    189  1.1      leo SC_REQ			*reqp;
    190  1.1      leo struct dma_chain	*dm;
    191  1.1      leo {
    192  1.1      leo 	if (dm->dm_addr & 0xff000000) {
    193  1.1      leo 		reqp->dr_flag |= DRIVER_BOUNCING;
    194  1.1      leo 		return(1);
    195  1.1      leo 	}
    196  1.1      leo 	return(0);
    197  1.1      leo }
    198  1.1      leo #else
    199  1.1      leo #define	tt_wrong_dma_range(reqp, dm)	0
    200  1.1      leo #endif
    201  1.1      leo 
    202  1.1      leo static void scsi_tt_init(sc)
    203  1.1      leo struct ncr_softc	*sc;
    204  1.1      leo {
    205  1.1      leo 	/*
    206  1.1      leo 	 * Enable SCSI-related interrupts
    207  1.1      leo 	 */
    208  1.1      leo 	MFP2->mf_aer  |= 0x80;		/* SCSI IRQ goes HIGH!!!!!	*/
    209  1.1      leo 
    210  1.1      leo 	MFP2->mf_ierb |= IB_SCDM;	/* SCSI-dma interrupts		*/
    211  1.1      leo 	MFP2->mf_iprb &= ~IB_SCDM;
    212  1.1      leo 	MFP2->mf_imrb |= IB_SCDM;
    213  1.1      leo 
    214  1.1      leo 	MFP2->mf_iera |= IA_SCSI;	/* SCSI-5380 interrupts		*/
    215  1.1      leo 	MFP2->mf_ipra &= ~IA_SCSI;
    216  1.1      leo 	MFP2->mf_imra |= IA_SCSI;
    217  1.1      leo 
    218  1.1      leo 	/*
    219  1.1      leo 	 * LWP: DMA transfers to TT-ram causes data to be garbeled
    220  1.1      leo 	 * without notice on some revisons of the TT-mainboard.
    221  1.1      leo 	 * When program's generate misterious Segmentations faults,
    222  1.1      leo 	 * try turning on NO_TTRAM_DMA.
    223  1.1      leo 	 */
    224  1.1      leo #ifdef NO_TTRAM_DMA
    225  1.1      leo 	printf(": DMA to TT-RAM is disabled!");
    226  1.1      leo #endif
    227  1.1      leo }
    228  1.1      leo 
    229  1.1      leo static u_char get_tt_5380_reg(rnum)
    230  1.1      leo u_short	rnum;
    231  1.1      leo {
    232  1.1      leo 	return(SCSI_5380->scsi_5380[(rnum << 1) | 1]);
    233  1.1      leo }
    234  1.1      leo 
    235  1.1      leo static void set_tt_5380_reg(rnum, val)
    236  1.1      leo u_short	rnum, val;
    237  1.1      leo {
    238  1.1      leo 	SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
    239  1.1      leo }
    240  1.1      leo 
    241  1.1      leo extern __inline__ void scsi_tt_ienable()
    242  1.1      leo {
    243  1.1      leo 	int	sps = splbio();
    244  1.1      leo 	MFP2->mf_ierb |= IB_SCDM;
    245  1.1      leo 	MFP2->mf_iera |= IA_SCSI;
    246  1.1      leo 	splx(sps);
    247  1.1      leo }
    248  1.1      leo 
    249  1.1      leo extern __inline__ scsi_tt_idisable()
    250  1.1      leo {
    251  1.1      leo 	int	sps = splbio();
    252  1.1      leo 	MFP2->mf_ierb &= ~IB_SCDM;
    253  1.1      leo 	MFP2->mf_iera &= ~IA_SCSI;
    254  1.1      leo 	splx(sps);
    255  1.1      leo }
    256  1.1      leo 
    257  1.1      leo extern __inline__ scsi_tt_clr_ipend()
    258  1.1      leo {
    259  1.1      leo 	int	tmp;
    260  1.1      leo 
    261  1.1      leo 	SCSI_DMA->s_dma_ctrl = 0;
    262  1.1      leo 	tmp = GET_TT_REG(NCR5380_IRCV);
    263  1.1      leo }
    264  1.1      leo 
    265  1.1      leo static void scsi_tt_dmasetup(reqp, phase, mode)
    266  1.1      leo SC_REQ	*reqp;
    267  1.1      leo u_int	phase;
    268  1.1      leo u_char	mode;
    269  1.1      leo {
    270  1.1      leo 	if (PH_IN(phase)) {
    271  1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_IN;
    272  1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
    273  1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
    274  1.1      leo 		SET_TT_REG(NCR5380_ICOM, 0);
    275  1.1      leo 		SET_TT_REG(NCR5380_MODE, mode);
    276  1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_ENABLE;
    277  1.1      leo 		SET_TT_REG(NCR5380_IRCV, 0);
    278  1.1      leo 	}
    279  1.1      leo 	else {
    280  1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_OUT;
    281  1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_ptr), reqp->dm_cur->dm_addr);
    282  1.1      leo 		set_scsi_dma(&(SCSI_DMA->s_dma_cnt), reqp->dm_cur->dm_count);
    283  1.1      leo 		SET_TT_REG(NCR5380_MODE, mode);
    284  1.1      leo 		SET_TT_REG(NCR5380_ICOM, SC_ADTB);
    285  1.1      leo 		SET_TT_REG(NCR5380_DMSTAT, 0);
    286  1.1      leo 		SCSI_DMA->s_dma_ctrl = SD_ENABLE|SD_OUT;
    287  1.1      leo 	}
    288  1.1      leo }
    289  1.1      leo 
    290  1.1      leo static int
    291  1.1      leo tt_poll_edma(reqp)
    292  1.1      leo SC_REQ	*reqp;
    293  1.1      leo {
    294  1.1      leo 	u_char	dmstat, dmastat;
    295  1.1      leo 	int	timeout = 9000; /* XXX */
    296  1.1      leo 
    297  1.1      leo 	/*
    298  1.1      leo 	 * We wait here until the DMA has finished. This can be
    299  1.1      leo 	 * achieved by checking the following conditions:
    300  1.1      leo 	 *   - 5380:
    301  1.1      leo 	 *	- End of DMA flag is set
    302  1.1      leo 	 *	- We lost BSY (error!!)
    303  1.1      leo 	 *	- A phase mismatch has occured (partial transfer)
    304  1.1      leo 	 *   - DMA-controller:
    305  1.1      leo 	 *	- A bus error occurred (Kernel error!!)
    306  1.1      leo 	 *	- All bytes are transferred
    307  1.1      leo 	 * If one of the terminating conditions was met, we call
    308  1.1      leo 	 * 'dma_ready' to check errors and perform the bookkeeping.
    309  1.1      leo 	 */
    310  1.1      leo 
    311  1.1      leo 	for (;;) {
    312  1.1      leo 		delay(20);
    313  1.1      leo 		if (--timeout <= 0) {
    314  1.1      leo 			ncr_tprint(reqp, "timeout on polled transfer\n");
    315  1.1      leo 			reqp->xs->error = XS_DRIVER_STUFFUP;
    316  1.1      leo 			return(0);
    317  1.1      leo 		}
    318  1.1      leo 		dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    319  1.1      leo 		dmastat = SCSI_DMA->s_dma_ctrl;
    320  1.1      leo 		if (dmstat & (SC_END_DMA|SC_BSY_ERR|SC_IRQ_SET))
    321  1.1      leo 			break;
    322  1.1      leo 		if (!(dmstat & SC_PHS_MTCH))
    323  1.1      leo 			break;
    324  1.1      leo 		if (dmastat & (SD_BUSERR|SD_ZERO))
    325  1.1      leo 			break;
    326  1.1      leo 	}
    327  1.1      leo 	return(1);
    328  1.1      leo }
    329  1.1      leo 
    330  1.2      leo /*
    331  1.2      leo  * Convert physical DMA address to a virtual address.
    332  1.2      leo  */
    333  1.2      leo static u_char *
    334  1.2      leo ptov(reqp, phaddr)
    335  1.2      leo SC_REQ	*reqp;
    336  1.2      leo u_long	*phaddr;
    337  1.2      leo {
    338  1.2      leo 	struct dma_chain	*dm;
    339  1.2      leo 	u_char			*vaddr;
    340  1.2      leo 
    341  1.2      leo 	dm = reqp->dm_chain;
    342  1.2      leo 	vaddr = reqp->xdata_ptr;
    343  1.2      leo 	for(; dm < reqp->dm_cur; dm++)
    344  1.2      leo 		vaddr += dm->dm_count;
    345  1.2      leo 	vaddr += (u_long)phaddr - dm->dm_addr;
    346  1.2      leo 	return(vaddr);
    347  1.2      leo }
    348  1.2      leo 
    349  1.1      leo static int
    350  1.1      leo tt_get_dma_result(reqp, bytes_left)
    351  1.1      leo SC_REQ	*reqp;
    352  1.1      leo u_long	*bytes_left;
    353  1.1      leo {
    354  1.1      leo 	int	dmastat, dmstat;
    355  1.1      leo 	u_char	*byte_p;
    356  1.1      leo 	u_long	leftover;
    357  1.1      leo 
    358  1.1      leo 	dmastat = SCSI_DMA->s_dma_ctrl;
    359  1.1      leo 	dmstat  = GET_TT_REG(NCR5380_DMSTAT);
    360  1.1      leo 	get_scsi_dma(SCSI_DMA->s_dma_cnt, leftover);
    361  1.1      leo 	get_scsi_dma(SCSI_DMA->s_dma_ptr, (u_long)byte_p);
    362  1.1      leo 
    363  1.1      leo 	if (dmastat & SD_BUSERR) {
    364  1.1      leo 		/*
    365  1.1      leo 		 * The DMA-controller seems to access 8 bytes beyond
    366  1.1      leo 		 * it's limits on output. Therefore check also the byte
    367  1.1      leo 		 * count. If it's zero, ignore the bus error.
    368  1.1      leo 		 */
    369  1.1      leo 		if (leftover != 0) {
    370  1.1      leo 			ncr_tprint(reqp,
    371  1.1      leo 				"SCSI-DMA buserror - accessing 0x%x\n", byte_p);
    372  1.1      leo 			reqp->xs->error = XS_DRIVER_STUFFUP;
    373  1.1      leo 		}
    374  1.1      leo 	}
    375  1.1      leo 
    376  1.1      leo 	/*
    377  1.1      leo 	 * We handle the following special condition below:
    378  1.1      leo 	 *  -- The device disconnects in the middle of a write operation --
    379  1.1      leo 	 * In this case, the 5380 has already pre-fetched the next byte from
    380  1.1      leo 	 * the DMA-controller before the phase mismatch occurs. Therefore,
    381  1.1      leo 	 * leftover is 1 too low.
    382  1.1      leo 	 * This does not always happen! Therefore, we only do this when
    383  1.1      leo 	 * leftover is odd. This assumes that DMA transfers are _even_! This
    384  1.1      leo 	 * is normally the case on disks and types but might not always be.
    385  1.1      leo 	 * XXX: Check if ACK is consistently high on these occasions LWP
    386  1.1      leo 	 */
    387  1.1      leo 	if ((leftover & 1) && !(dmstat & SC_PHS_MTCH) && PH_OUT(reqp->phase))
    388  1.1      leo 		leftover++;
    389  1.1      leo 
    390  1.1      leo 	/*
    391  1.1      leo 	 * Check if there are some 'restbytes' left in the DMA-controller.
    392  1.1      leo 	 */
    393  1.1      leo 	if (((u_long)byte_p & 3) && PH_IN(reqp->phase)) {
    394  1.1      leo 		u_char	*p, *q;
    395  1.1      leo 
    396  1.2      leo 		p = ptov(reqp, (u_long)byte_p & ~3);
    397  1.1      leo 		q = (u_char*)&(SCSI_DMA->s_dma_res);
    398  1.2      leo 		switch ((u_long)byte_p & 3) {
    399  1.1      leo 			case 3: *p++ = *q++;
    400  1.1      leo 			case 2: *p++ = *q++;
    401  1.1      leo 			case 1: *p++ = *q++;
    402  1.1      leo 		}
    403  1.1      leo 	}
    404  1.1      leo 	*bytes_left = leftover;
    405  1.1      leo 	return ((dmastat & (SD_BUSERR|SD_ZERO)) ? 1 : 0);
    406  1.1      leo }
    407  1.1      leo 
    408  1.1      leo #endif /* defined(TT_SCSI) */
    409  1.1      leo 
    410  1.1      leo #if defined(FALCON_SCSI) && !defined(TT_SCSI)
    411  1.1      leo 
    412  1.1      leo #define	GET_5380_REG(rnum)	get_falcon_5380_reg(rnum)
    413  1.1      leo #define	SET_5380_REG(rnum,val)	set_falcon_5380_reg(rnum, val)
    414  1.1      leo #define scsi_mach_init(sc)	scsi_falcon_init(sc)
    415  1.1      leo #define scsi_ienable()		scsi_falcon_ienable()
    416  1.1      leo #define scsi_idisable()		scsi_falcon_idisable()
    417  1.1      leo #define	scsi_clr_ipend()	scsi_falcon_clr_ipend()
    418  1.7      leo #define	scsi_ipending()		scsi_falcon_ipending()
    419  1.1      leo #define scsi_dma_setup(r,p,m)	scsi_falcon_dmasetup(r, p, m)
    420  1.1      leo #define wrong_dma_range(r,d)	falcon_wrong_dma_range(r, d)
    421  1.1      leo #define poll_edma(reqp)		falcon_poll_edma(reqp)
    422  1.1      leo #define get_dma_result(r, b)	falcon_get_dma_result(r, b)
    423  1.1      leo 
    424  1.1      leo #define fair_to_keep_dma()	(!st_dmawanted())
    425  1.1      leo #define claimed_dma()		falcon_claimed_dma()
    426  1.1      leo #define reconsider_dma()	falcon_reconsider_dma()
    427  1.1      leo 
    428  1.1      leo #endif /* defined(FALCON_SCSI) && !defined(TT_SCSI) */
    429  1.1      leo 
    430  1.1      leo #if defined(FALCON_SCSI)
    431  1.1      leo 
    432  1.1      leo static void fscsi_int __P((void));
    433  1.1      leo 
    434  1.1      leo static void scsi_falcon_init(sc)
    435  1.1      leo struct ncr_softc	*sc;
    436  1.1      leo {
    437  1.1      leo 	/*
    438  1.1      leo 	 * Enable disk related interrupts
    439  1.1      leo 	 */
    440  1.1      leo 	MFP->mf_ierb  |= IB_DINT;
    441  1.1      leo 	MFP->mf_iprb  &= ~IB_DINT;
    442  1.1      leo 	MFP->mf_imrb  |= IB_DINT;
    443  1.1      leo }
    444  1.1      leo 
    445  1.1      leo static u_char get_falcon_5380_reg(rnum)
    446  1.1      leo u_short	rnum;
    447  1.1      leo {
    448  1.1      leo 	DMA->dma_mode = DMA_SCSI + rnum;
    449  1.1      leo 	return(DMA->dma_data);
    450  1.1      leo }
    451  1.1      leo 
    452  1.1      leo static void set_falcon_5380_reg(rnum, val)
    453  1.1      leo u_short	rnum, val;
    454  1.1      leo {
    455  1.1      leo 	DMA->dma_mode = DMA_SCSI + rnum;
    456  1.1      leo 	DMA->dma_data = val;
    457  1.1      leo }
    458  1.1      leo 
    459  1.1      leo extern __inline__ void scsi_falcon_ienable()
    460  1.1      leo {
    461  1.1      leo 	MFP->mf_ierb  |= IB_DINT;
    462  1.1      leo }
    463  1.1      leo 
    464  1.7      leo extern __inline__ void scsi_falcon_idisable()
    465  1.1      leo {
    466  1.1      leo 	MFP->mf_ierb  &= ~IB_DINT;
    467  1.1      leo }
    468  1.1      leo 
    469  1.7      leo extern __inline__ void scsi_falcon_clr_ipend()
    470  1.1      leo {
    471  1.1      leo 	int	tmp;
    472  1.1      leo 
    473  1.1      leo 	tmp = get_falcon_5380_reg(NCR5380_IRCV);
    474  1.1      leo }
    475  1.1      leo 
    476  1.7      leo extern __inline__ int scsi_falcon_ipending()
    477  1.7      leo {
    478  1.7      leo 	return(!(MFP->mf_gpip & IO_DINT)
    479  1.7      leo 		&& (get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET));
    480  1.7      leo }
    481  1.7      leo 
    482  1.1      leo static int falcon_wrong_dma_range(reqp, dm)
    483  1.1      leo SC_REQ			*reqp;
    484  1.1      leo struct dma_chain	*dm;
    485  1.1      leo {
    486  1.1      leo 	/*
    487  1.1      leo 	 * Do not allow chains yet! See also comment with
    488  1.1      leo 	 * falcon_poll_edma() !!!
    489  1.1      leo 	 */
    490  1.1      leo 	if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
    491  1.1      leo 		reqp->dr_flag |= DRIVER_BOUNCING;
    492  1.1      leo 		return(1);
    493  1.1      leo 	}
    494  1.2      leo 	/*
    495  1.2      leo 	 * Never allow DMA to happen on a Falcon when the transfer
    496  1.2      leo 	 * size is no multiple of 512. This is the transfer unit of the
    497  1.2      leo 	 * ST DMA-controller.
    498  1.2      leo 	 */
    499  1.2      leo 	if(dm->dm_count & 511)
    500  1.2      leo 		return(1);
    501  1.1      leo 	return(0);
    502  1.1      leo }
    503  1.1      leo 
    504  1.1      leo static	int falcon_lock = 0;
    505  1.1      leo 
    506  1.1      leo extern __inline__ falcon_claimed_dma()
    507  1.1      leo {
    508  1.1      leo 	if (!(falcon_lock & DMA_LOCK_GRANT)) {
    509  1.1      leo 		if (falcon_lock) {
    510  1.1      leo 			/*
    511  1.1      leo 			 * DMA access is being claimed.
    512  1.1      leo 			 */
    513  1.1      leo 			return(0);
    514  1.1      leo 		}
    515  1.1      leo 		if (!st_dmagrab(fscsi_int, run_main, &connected,&falcon_lock,1))
    516  1.1      leo 			return(0);
    517  1.1      leo 	}
    518  1.1      leo 	return(1);
    519  1.1      leo }
    520  1.1      leo 
    521  1.1      leo extern __inline__ void falcon_reconsider_dma()
    522  1.1      leo {
    523  1.1      leo 	if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
    524  1.1      leo 		/*
    525  1.1      leo 		 * No need to keep DMA locked by us as we are not currently
    526  1.1      leo 		 * connected and no disconnected jobs are pending.
    527  1.1      leo 		 */
    528  1.1      leo 		st_dmafree(&connected, &falcon_lock);
    529  1.1      leo 	}
    530  1.1      leo 
    531  1.1      leo 	if (!falcon_lock && (issue_q != NULL)) {
    532  1.1      leo 		/*
    533  1.1      leo 		 * We must (re)claim DMA access as there are jobs
    534  1.1      leo 		 * waiting in the issue queue.
    535  1.1      leo 		 */
    536  1.1      leo 		st_dmagrab(fscsi_int, run_main, &connected, &falcon_lock, 0);
    537  1.1      leo 	}
    538  1.1      leo }
    539  1.1      leo 
    540  1.1      leo static void fal1_dma(dir, nsects, reqp)
    541  1.1      leo u_int	dir, nsects;
    542  1.1      leo SC_REQ	*reqp;
    543  1.1      leo {
    544  1.1      leo 	dir <<= 8;
    545  1.1      leo 	st_dmaaddr_set((caddr_t)reqp->dm_cur->dm_addr);
    546  1.1      leo 	DMA->dma_mode = 0x90 | dir;
    547  1.1      leo 	DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
    548  1.1      leo 	DMA->dma_mode = 0x90 | dir;
    549  1.1      leo 	DMA->dma_data = nsects;
    550  1.7      leo 	delay(2);	/* _really_ needed (Thomas Gerner) */
    551  1.1      leo 	DMA->dma_mode = 0x10 | dir;
    552  1.1      leo }
    553  1.1      leo 
    554  1.1      leo static void scsi_falcon_dmasetup(reqp, phase, mode)
    555  1.1      leo SC_REQ	*reqp;
    556  1.1      leo u_int	phase;
    557  1.1      leo u_char	mode;
    558  1.1      leo {
    559  1.1      leo 	int	nsects = reqp->dm_cur->dm_count / 512; /* XXX */
    560  1.1      leo 
    561  1.1      leo 	/*
    562  1.1      leo 	 * XXX: We should probably clear the fifo before putting the
    563  1.1      leo 	 *      5380 into DMA-mode.
    564  1.1      leo 	 */
    565  1.1      leo 	if (PH_IN(phase)) {
    566  1.1      leo 		set_falcon_5380_reg(NCR5380_ICOM, 0);
    567  1.1      leo 		set_falcon_5380_reg(NCR5380_MODE, mode);
    568  1.1      leo 		set_falcon_5380_reg(NCR5380_IRCV, 0);
    569  1.1      leo 		fal1_dma(0, nsects, reqp);
    570  1.1      leo 	}
    571  1.1      leo 	else {
    572  1.1      leo 		set_falcon_5380_reg(NCR5380_MODE, mode);
    573  1.1      leo 		set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
    574  1.1      leo 		set_falcon_5380_reg(NCR5380_DMSTAT, 0);
    575  1.1      leo 		fal1_dma(1, nsects, reqp);
    576  1.1      leo 	}
    577  1.1      leo }
    578  1.1      leo 
    579  1.1      leo /*
    580  1.1      leo  * Falcon SCSI interrupt. _Always_ called at spl1!
    581  1.1      leo  */
    582  1.1      leo static void fscsi_int()
    583  1.1      leo {
    584  1.1      leo 	int	itype;
    585  1.1      leo 	int	dma_done;
    586  1.1      leo 
    587  1.7      leo 	if (scsi_falcon_ipending()) {
    588  1.1      leo 		scsi_falcon_idisable();
    589  1.1      leo 		ncr_ctrl_intr(cur_softc);
    590  1.1      leo 	}
    591  1.1      leo }
    592  1.1      leo 
    593  1.1      leo static int
    594  1.1      leo falcon_poll_edma(reqp)
    595  1.1      leo SC_REQ	*reqp;
    596  1.1      leo {
    597  1.1      leo 	int	timeout = 9000; /* XXX */
    598  1.1      leo 
    599  1.1      leo 	/*
    600  1.1      leo 	 * Because of the Falcon hardware, it is impossible to reach
    601  1.1      leo 	 * the 5380 while doing DMA-transfers. So we have to rely on
    602  1.1      leo 	 * the interrupt line to determine if DMA-has finished. the
    603  1.1      leo 	 * DMA-controller itself will never fire an interrupt. This means
    604  1.1      leo 	 * that 'broken-up' DMA transfers are not (yet) possible on the
    605  1.1      leo 	 * Falcon.
    606  1.1      leo 	 */
    607  1.1      leo 	for (;;) {
    608  1.1      leo 		delay(20);
    609  1.1      leo 		if (--timeout <= 0) {
    610  1.1      leo 			ncr_tprint(reqp, "Timeout on polled transfer\n");
    611  1.1      leo 			reqp->xs->error = XS_DRIVER_STUFFUP;
    612  1.1      leo 			return(0);
    613  1.1      leo 		}
    614  1.1      leo 		if (!(MFP->mf_gpip & IO_DINT))
    615  1.1      leo 			break;
    616  1.1      leo 	}
    617  1.1      leo 	return(1);
    618  1.1      leo }
    619  1.1      leo 
    620  1.1      leo static int
    621  1.1      leo falcon_get_dma_result(reqp, bytes_left)
    622  1.1      leo SC_REQ	*reqp;
    623  1.1      leo u_long	*bytes_left;
    624  1.1      leo {
    625  1.1      leo 	int	rv = 0;
    626  1.1      leo 	int	st_dmastat;
    627  1.1      leo 	u_long	bytes_done;
    628  1.1      leo 
    629  1.1      leo 	/*
    630  1.1      leo 	 * Select sector counter register first (See Atari docu.)
    631  1.1      leo 	 */
    632  1.1      leo 	DMA->dma_mode = 0x90;
    633  1.1      leo 	if (!(st_dmastat = DMA->dma_stat) & 0x01) {
    634  1.1      leo 		/*
    635  1.1      leo 		 * Misc. DMA-error according to Atari...
    636  1.1      leo 		 */
    637  1.1      leo 		ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
    638  1.1      leo 							st_dmaaddr_get());
    639  1.1      leo 		reqp->xs->error = XS_DRIVER_STUFFUP;
    640  1.1      leo 		rv = 1;
    641  1.1      leo 	}
    642  1.1      leo 	if (st_dmastat & 0x02) {
    643  1.1      leo 		/*
    644  1.1      leo 		 * Bytecount not zero.... As the fifo loads in 16 byte
    645  1.1      leo 		 * chunks, check if bytes are stuck in fifo.
    646  1.1      leo 		 * As we don't use DMA on chunks less than 512 bytes
    647  1.1      leo 		 * on the Falcon, report any residual not a multiple of
    648  1.1      leo 		 * 512 as an error...
    649  1.1      leo 		 */
    650  1.1      leo 		bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
    651  1.1      leo 		if (bytes_done & 511) {
    652  1.1      leo 			ncr_tprint(reqp, "Some bytes stuck in fifo\n");
    653  1.1      leo 			bytes_done &= ~511;
    654  1.1      leo 			reqp->xs->error = XS_DRIVER_STUFFUP;
    655  1.1      leo 		}
    656  1.1      leo 		*bytes_left = reqp->dm_cur->dm_count - bytes_done;
    657  1.1      leo 	}
    658  1.1      leo 	else {
    659  1.1      leo 		*bytes_left = 0;
    660  1.1      leo 		rv = 1;
    661  1.1      leo 	}
    662  1.1      leo 	return(rv);
    663  1.1      leo }
    664  1.1      leo 
    665  1.1      leo #endif /* defined(FALCON_SCSI) */
    666  1.1      leo 
    667  1.1      leo #if defined(TT_SCSI) && defined(FALCON_SCSI)
    668  1.1      leo /*
    669  1.1      leo  * Define some functions to support _both_ TT and Falcon SCSI
    670  1.1      leo  */
    671  1.1      leo 
    672  1.1      leo /*
    673  1.1      leo  * Register access will be done through the following 2 function pointers.
    674  1.1      leo  */
    675  1.1      leo static u_char	(*get_5380_reg)();
    676  1.1      leo static void	(*set_5380_reg)();
    677  1.1      leo 
    678  1.1      leo #define	GET_5380_REG	(*get_5380_reg)
    679  1.1      leo #define	SET_5380_REG	(*set_5380_reg)
    680  1.1      leo 
    681  1.1      leo static void scsi_mach_init(sc)
    682  1.1      leo struct ncr_softc	*sc;
    683  1.1      leo {
    684  1.1      leo 	if (machineid & ATARI_FALCON) {
    685  1.1      leo 		get_5380_reg = get_falcon_5380_reg;
    686  1.1      leo 		set_5380_reg = set_falcon_5380_reg;
    687  1.1      leo 		scsi_falcon_init(sc);
    688  1.1      leo 	}
    689  1.1      leo 	else {
    690  1.1      leo 		get_5380_reg = get_tt_5380_reg;
    691  1.1      leo 		set_5380_reg = set_tt_5380_reg;
    692  1.1      leo 		scsi_tt_init(sc);
    693  1.1      leo 	}
    694  1.1      leo }
    695  1.1      leo 
    696  1.1      leo extern __inline__ void scsi_ienable()
    697  1.1      leo {
    698  1.1      leo 	if (machineid & ATARI_FALCON)
    699  1.1      leo 		scsi_falcon_ienable();
    700  1.1      leo 	else scsi_tt_ienable();
    701  1.1      leo }
    702  1.1      leo 
    703  1.1      leo extern __inline__ void scsi_idisable()
    704  1.1      leo {
    705  1.1      leo 	if (machineid & ATARI_FALCON)
    706  1.1      leo 		scsi_falcon_idisable();
    707  1.1      leo 	else scsi_tt_idisable();
    708  1.1      leo }
    709  1.1      leo 
    710  1.7      leo extern __inline__ void scsi_clr_ipend()
    711  1.1      leo {
    712  1.1      leo 	if (machineid & ATARI_FALCON)
    713  1.1      leo 		scsi_falcon_clr_ipend();
    714  1.1      leo 	else scsi_tt_clr_ipend();
    715  1.7      leo }
    716  1.7      leo 
    717  1.7      leo extern __inline__ int scsi_ipending()
    718  1.7      leo {
    719  1.7      leo 	if (machineid & ATARI_FALCON)
    720  1.7      leo 		return(scsi_falcon_ipending());
    721  1.7      leo 	else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
    722  1.1      leo }
    723  1.1      leo 
    724  1.1      leo extern __inline__ scsi_dma_setup(reqp, phase, mbase)
    725  1.1      leo SC_REQ	*reqp;
    726  1.1      leo u_int	phase;
    727  1.1      leo u_char	mbase;
    728  1.1      leo {
    729  1.1      leo 	if (machineid & ATARI_FALCON)
    730  1.1      leo 		scsi_falcon_dmasetup(reqp, phase, mbase);
    731  1.1      leo 	else scsi_tt_dmasetup(reqp, phase, mbase);
    732  1.1      leo }
    733  1.1      leo 
    734  1.1      leo extern __inline__ int wrong_dma_range(reqp, dm)
    735  1.1      leo SC_REQ			*reqp;
    736  1.1      leo struct dma_chain	*dm;
    737  1.1      leo {
    738  1.1      leo 	if (machineid & ATARI_FALCON)
    739  1.1      leo 		return(falcon_wrong_dma_range(reqp, dm));
    740  1.1      leo 	else return(tt_wrong_dma_range(reqp, dm));
    741  1.1      leo }
    742  1.1      leo 
    743  1.1      leo extern __inline__ int poll_edma(reqp)
    744  1.1      leo SC_REQ	*reqp;
    745  1.1      leo {
    746  1.1      leo 	if (machineid & ATARI_FALCON)
    747  1.1      leo 		return(falcon_poll_edma(reqp));
    748  1.1      leo 	else return(tt_poll_edma(reqp));
    749  1.1      leo }
    750  1.1      leo 
    751  1.1      leo extern __inline__ int get_dma_result(reqp, bytes_left)
    752  1.1      leo SC_REQ	*reqp;
    753  1.1      leo u_long	*bytes_left;
    754  1.1      leo {
    755  1.1      leo 	if (machineid & ATARI_FALCON)
    756  1.1      leo 		return(falcon_get_dma_result(reqp, bytes_left));
    757  1.1      leo 	else return(tt_get_dma_result(reqp, bytes_left));
    758  1.1      leo }
    759  1.1      leo 
    760  1.1      leo /*
    761  1.1      leo  * Locking stuff. All turns into NOP's on the TT.
    762  1.1      leo  */
    763  1.1      leo #define	fair_to_keep_dma()	((machineid & ATARI_FALCON) ?		\
    764  1.1      leo 						!st_dmawanted() : 1)
    765  1.1      leo #define	claimed_dma()		((machineid & ATARI_FALCON) ?		\
    766  1.1      leo 						falcon_claimed_dma() : 1)
    767  1.1      leo #define reconsider_dma()	{					\
    768  1.1      leo 					if(machineid & ATARI_FALCON)	\
    769  1.1      leo 						falcon_reconsider_dma();\
    770  1.1      leo 				}
    771  1.1      leo #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
    772  1.1      leo 
    773  1.1      leo /**********************************************
    774  1.1      leo  * Functions present for both TT and Falcon.  *
    775  1.1      leo  **********************************************/
    776  1.1      leo /*
    777  1.1      leo  * Our autoconfig matching function
    778  1.1      leo  */
    779  1.1      leo static int
    780  1.1      leo machine_match(pdp, cdp, auxp, cd)
    781  1.1      leo struct device	*pdp;
    782  1.1      leo struct cfdata	*cdp;
    783  1.1      leo void		*auxp;
    784  1.1      leo struct cfdriver *cd;
    785  1.1      leo {
    786  1.1      leo 	if (strcmp(auxp, cd->cd_name))
    787  1.1      leo 		return(0);
    788  1.1      leo 	if (cdp->cf_unit != 0)	/* Only one unit	*/
    789  1.1      leo 		return(0);
    790  1.1      leo 	return(1);
    791  1.1      leo }
    792  1.1      leo 
    793  1.1      leo /*
    794  1.1      leo  * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
    795  1.1      leo  * pool. Allocation here is both contiguous and in the lower 16Mb of
    796  1.1      leo  * the address space. Thus being DMA-able for all controllers.
    797  1.1      leo  */
    798  1.1      leo static u_char *
    799  1.1      leo alloc_bounceb(len)
    800  1.1      leo u_long len;
    801  1.1      leo {
    802  1.1      leo 	u_long	tmp;
    803  1.1      leo 
    804  1.1      leo 	return((u_char *)alloc_stmem(len, &tmp));
    805  1.1      leo }
    806  1.1      leo 
    807  1.1      leo static void
    808  1.1      leo free_bounceb(bounceb)
    809  1.1      leo u_char	*bounceb;
    810  1.1      leo {
    811  1.1      leo 	free_stmem(bounceb);
    812  1.1      leo }
    813  1.1      leo 
    814  1.1      leo /*
    815  1.1      leo  * 5380 interrupt.
    816  1.1      leo  */
    817  1.1      leo scsi_ctrl(sr)
    818  1.1      leo int	sr;	/* sr at time of interrupt */
    819  1.1      leo {
    820  1.1      leo 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
    821  1.1      leo 		scsi_idisable();
    822  1.1      leo 		if (!BASEPRI(sr))
    823  1.1      leo 			add_sicallback(ncr_ctrl_intr, cur_softc, 0);
    824  1.1      leo 		else {
    825  1.1      leo 			spl1();
    826  1.1      leo 			ncr_ctrl_intr(cur_softc);
    827  1.1      leo 		}
    828  1.1      leo 	}
    829  1.1      leo }
    830  1.1      leo 
    831  1.1      leo /*
    832  1.1      leo  * DMA controller interrupt
    833  1.1      leo  */
    834  1.1      leo scsi_dma(sr)
    835  1.1      leo int	sr;	/* sr at time of interrupt */
    836  1.1      leo {
    837  1.1      leo 	SC_REQ	*reqp;
    838  1.1      leo 
    839  1.1      leo 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
    840  1.1      leo 		scsi_idisable();
    841  1.1      leo 		if (!BASEPRI(sr))
    842  1.1      leo 			add_sicallback(ncr_dma_intr, cur_softc, 0);
    843  1.1      leo 		else {
    844  1.1      leo 			spl1();
    845  1.1      leo 			ncr_dma_intr(cur_softc);
    846  1.1      leo 		}
    847  1.1      leo 	}
    848  1.1      leo }
    849  1.1      leo 
    850  1.1      leo /*
    851  1.1      leo  * Last but not least... Include the general driver code
    852  1.1      leo  */
    853  1.6  mycroft #include <atari/dev/ncr5380.c>
    854