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