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