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