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