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