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