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