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