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