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