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