Home | History | Annotate | Line # | Download | only in dev
atari5380.c revision 1.49
      1 /*	$NetBSD: atari5380.c,v 1.49 2009/03/14 15:36:03 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.49 2009/03/14 15:36:03 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(rnum, val)
    712 u_short	rnum, val;
    713 {
    714 	DMA->dma_mode = DMA_SCSI + rnum;
    715 	DMA->dma_data = val;
    716 }
    717 
    718 extern inline void
    719 scsi_falcon_ienable()
    720 {
    721 	single_inst_bset_b(MFP->mf_imrb, IB_DINT);
    722 }
    723 
    724 extern inline void
    725 scsi_falcon_idisable()
    726 {
    727 	single_inst_bclr_b(MFP->mf_imrb, IB_DINT);
    728 }
    729 
    730 extern inline void
    731 scsi_falcon_clr_ipend()
    732 {
    733 	int	tmp;
    734 
    735 	tmp = get_falcon_5380_reg(NCR5380_IRCV);
    736 	rem_sicallback((si_farg)ncr_ctrl_intr);
    737 }
    738 
    739 extern inline int
    740 scsi_falcon_ipending()
    741 {
    742 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
    743 		/*
    744 		 *  XXX: When DMA is running, we are only allowed to
    745 		 *       check the 5380 when DMA _might_ be finished.
    746 		 */
    747 		if (MFP->mf_gpip & IO_DINT)
    748 		    return (0); /* XXX: Actually: we're not allowed to check */
    749 
    750 		/* LWP: 28-06, must be a DMA interrupt! should the
    751 		 * ST-DMA unit be taken out of DMA mode?????
    752 		 */
    753 		DMA->dma_mode = 0x90;
    754 
    755 	}
    756 	return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET);
    757 }
    758 
    759 static int
    760 falcon_wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
    761 {
    762 	/*
    763 	 * Do not allow chains yet! See also comment with
    764 	 * falcon_poll_edma() !!!
    765 	 */
    766 	if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
    767 		reqp->dr_flag |= DRIVER_BOUNCING;
    768 		return(1);
    769 	}
    770 	/*
    771 	 * Never allow DMA to happen on a Falcon when the transfer
    772 	 * size is no multiple of 512. This is the transfer unit of the
    773 	 * ST DMA-controller.
    774 	 */
    775 	if(dm->dm_count & 511)
    776 		return(1);
    777 	return(0);
    778 }
    779 
    780 static	int falcon_lock = 0;
    781 
    782 extern inline int
    783 falcon_claimed_dma()
    784 {
    785 	if (falcon_lock != DMA_LOCK_GRANT) {
    786 		if (falcon_lock == DMA_LOCK_REQ) {
    787 			/*
    788 			 * DMA access is being claimed.
    789 			 */
    790 			return(0);
    791 		}
    792 		if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
    793 						cur_softc, &falcon_lock, 1))
    794 			return(0);
    795 	}
    796 	return(1);
    797 }
    798 
    799 extern inline void
    800 falcon_reconsider_dma()
    801 {
    802 	if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
    803 		/*
    804 		 * No need to keep DMA locked by us as we are not currently
    805 		 * connected and no disconnected jobs are pending.
    806 		 */
    807 		rem_sicallback((si_farg)ncr_ctrl_intr);
    808 		st_dmafree(cur_softc, &falcon_lock);
    809 	}
    810 
    811 	if (!falcon_lock && (issue_q != NULL)) {
    812 		/*
    813 		 * We must (re)claim DMA access as there are jobs
    814 		 * waiting in the issue queue.
    815 		 */
    816 		st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
    817 						cur_softc, &falcon_lock, 0);
    818 	}
    819 }
    820 
    821 static void
    822 fal1_dma(dir, nsects, reqp)
    823 u_int	dir, nsects;
    824 SC_REQ	*reqp;
    825 {
    826 	dir <<= 8;
    827 	st_dmaaddr_set((void *)reqp->dm_cur->dm_addr);
    828 	DMA->dma_mode = 0x90 | dir;
    829 	DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
    830 	DMA->dma_mode = 0x90 | dir;
    831 	DMA->dma_data = nsects;
    832 	delay(2);	/* _really_ needed (Thomas Gerner) */
    833 	DMA->dma_mode = 0x10 | dir;
    834 }
    835 
    836 static void
    837 scsi_falcon_dmasetup(SC_REQ *reqp, u_int phase, u_char mode)
    838 {
    839 	int	nsects = reqp->dm_cur->dm_count / 512; /* XXX */
    840 
    841 	/*
    842 	 * XXX: We should probably clear the fifo before putting the
    843 	 *      5380 into DMA-mode.
    844 	 */
    845 	if (PH_IN(phase)) {
    846 		set_falcon_5380_reg(NCR5380_ICOM, 0);
    847 		set_falcon_5380_reg(NCR5380_MODE, mode);
    848 		set_falcon_5380_reg(NCR5380_IRCV, 0);
    849 		fal1_dma(0, nsects, reqp);
    850 	}
    851 	else {
    852 		set_falcon_5380_reg(NCR5380_MODE, mode);
    853 		set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
    854 		set_falcon_5380_reg(NCR5380_DMSTAT, 0);
    855 		fal1_dma(1, nsects, reqp);
    856 	}
    857 }
    858 
    859 static int
    860 falcon_poll_edma(SC_REQ *reqp)
    861 {
    862 	int	timeout = 9000; /* XXX */
    863 
    864 	/*
    865 	 * Because of the Falcon hardware, it is impossible to reach
    866 	 * the 5380 while doing DMA-transfers. So we have to rely on
    867 	 * the interrupt line to determine if DMA-has finished. the
    868 	 * DMA-controller itself will never fire an interrupt. This means
    869 	 * that 'broken-up' DMA transfers are not (yet) possible on the
    870 	 * Falcon.
    871 	 */
    872 	for (;;) {
    873 		delay(20);
    874 		if (--timeout <= 0) {
    875 			ncr_tprint(reqp, "Timeout on polled transfer\n");
    876 			reqp->xs->error = XS_TIMEOUT;
    877 			return(0);
    878 		}
    879 		if (!(MFP->mf_gpip & IO_DINT))
    880 			break;
    881 	}
    882 	return(1);
    883 }
    884 
    885 static int
    886 falcon_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
    887 {
    888 	int	rv = 0;
    889 	int	st_dmastat;
    890 	u_long	bytes_done;
    891 
    892 	/*
    893 	 * Select sector counter register first (See Atari docu.)
    894 	 */
    895 	DMA->dma_mode = 0x90;
    896 	if (!(st_dmastat = DMA->dma_stat) & 0x01) {
    897 		/*
    898 		 * Misc. DMA-error according to Atari...
    899 		 */
    900 		ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
    901 							st_dmaaddr_get());
    902 		reqp->xs->error = XS_DRIVER_STUFFUP;
    903 		rv = 1;
    904 	}
    905 	/*
    906 	 * Because we NEVER start DMA on the Falcon when the data size
    907 	 * is not a multiple of 512 bytes, we can safely round down the
    908 	 * byte count on writes. We need to because in case of a disconnect,
    909 	 * the DMA has already prefetched the next couple of bytes.
    910 	 * On read, these byte counts are an error. They are logged and
    911 	 * should be handled by the mi-part of the driver.
    912 	 * NOTE: We formerly did this by using the 'byte-count-zero' bit
    913 	 *       of the DMA controller, but this didn't seem to work???
    914          *       [lwp 29/06/96]
    915 	 */
    916 	bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
    917 	if (bytes_done & 511) {
    918 		if (PH_IN(reqp->phase)) {
    919 			ncr_tprint(reqp, "Byte count on read not a multiple "
    920 					 "of 512 (%ld)\n", bytes_done);
    921 		}
    922 		bytes_done &= ~511;
    923 	}
    924 	if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0)
    925 		rv = 1;
    926 	return(rv);
    927 }
    928 
    929 static int
    930 falcon_can_access_5380()
    931 {
    932 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)
    933 		&& (MFP->mf_gpip & IO_DINT))
    934 			return(0);
    935 	return(1);
    936 }
    937 
    938 #endif /* defined(FALCON_SCSI) */
    939 
    940 #if defined(TT_SCSI) && defined(FALCON_SCSI)
    941 /*
    942  * Define some functions to support _both_ TT and Falcon SCSI
    943  */
    944 
    945 /*
    946  * The prototypes first...
    947  */
    948 static void	scsi_mach_init(struct ncr_softc *);
    949        void	scsi_ienable(void);
    950        void	scsi_idisable(void);
    951        void	scsi_clr_ipend(void);
    952        int	scsi_ipending(void);
    953        void	scsi_dma_setup(SC_REQ *, u_int, u_char);
    954        int	wrong_dma_range(SC_REQ *, struct dma_chain *);
    955        int	poll_edma(SC_REQ *);
    956        int	get_dma_result(SC_REQ *, u_long *);
    957        int	can_access_5380(void);
    958 
    959 /*
    960  * Register access will be done through the following 2 function pointers.
    961  */
    962 static u_char	(*get_5380_reg)(u_short);
    963 static void	(*set_5380_reg)(u_short, u_short);
    964 
    965 #define	GET_5380_REG	(*get_5380_reg)
    966 #define	SET_5380_REG	(*set_5380_reg)
    967 
    968 static void
    969 scsi_mach_init(struct ncr_softc *sc)
    970 {
    971 	if (machineid & ATARI_FALCON) {
    972 		get_5380_reg = get_falcon_5380_reg;
    973 		set_5380_reg = set_falcon_5380_reg;
    974 		scsi_falcon_init(sc);
    975 	}
    976 	else {
    977 		get_5380_reg = get_tt_5380_reg;
    978 		set_5380_reg = set_tt_5380_reg;
    979 		scsi_tt_init(sc);
    980 	}
    981 }
    982 
    983 extern inline void
    984 scsi_ienable()
    985 {
    986 	if (machineid & ATARI_FALCON)
    987 		scsi_falcon_ienable();
    988 	else scsi_tt_ienable();
    989 }
    990 
    991 extern inline void
    992 scsi_idisable()
    993 {
    994 	if (machineid & ATARI_FALCON)
    995 		scsi_falcon_idisable();
    996 	else scsi_tt_idisable();
    997 }
    998 
    999 extern inline void
   1000 scsi_clr_ipend()
   1001 {
   1002 	if (machineid & ATARI_FALCON)
   1003 		scsi_falcon_clr_ipend();
   1004 	else scsi_tt_clr_ipend();
   1005 }
   1006 
   1007 extern inline int
   1008 scsi_ipending()
   1009 {
   1010 	if (machineid & ATARI_FALCON)
   1011 		return(scsi_falcon_ipending());
   1012 	else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
   1013 }
   1014 
   1015 extern inline void
   1016 scsi_dma_setup(SC_REQ *reqp, u_int phase, u_char mbase)
   1017 {
   1018 	if (machineid & ATARI_FALCON)
   1019 		scsi_falcon_dmasetup(reqp, phase, mbase);
   1020 	else scsi_tt_dmasetup(reqp, phase, mbase);
   1021 }
   1022 
   1023 extern inline int
   1024 wrong_dma_range(SC_REQ *reqp, 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(SC_REQ *reqp)
   1033 {
   1034 	if (machineid & ATARI_FALCON)
   1035 		return(falcon_poll_edma(reqp));
   1036 	else return(tt_poll_edma(reqp));
   1037 }
   1038 
   1039 extern inline int
   1040 get_dma_result(SC_REQ *reqp, u_long *bytes_left)
   1041 {
   1042 	if (machineid & ATARI_FALCON)
   1043 		return(falcon_get_dma_result(reqp, bytes_left));
   1044 	else return(tt_get_dma_result(reqp, bytes_left));
   1045 }
   1046 
   1047 extern inline int
   1048 can_access_5380()
   1049 {
   1050 	if (machineid & ATARI_FALCON)
   1051 		return(falcon_can_access_5380());
   1052 	return(1);
   1053 }
   1054 
   1055 #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
   1056 
   1057 /*
   1058  * Locking stuff. All turns into NOP's on the TT.
   1059  */
   1060 #define	fair_to_keep_dma()	((machineid & ATARI_FALCON) ?		\
   1061 						!st_dmawanted() : 1)
   1062 #define	claimed_dma()		((machineid & ATARI_FALCON) ?		\
   1063 						falcon_claimed_dma() : 1)
   1064 #define reconsider_dma()	{					\
   1065 					if(machineid & ATARI_FALCON)	\
   1066 						falcon_reconsider_dma();\
   1067 				}
   1068 #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
   1069 
   1070 /**********************************************
   1071  * Functions present for both TT and Falcon.  *
   1072  **********************************************/
   1073 /*
   1074  * Our autoconfig matching function
   1075  */
   1076 static int
   1077 machine_match(struct device *pdp, void *match, void *auxp,
   1078 						struct cfdriver *cd)
   1079 {
   1080 	static int	we_matched = 0; /* Only one unit	*/
   1081 
   1082 	if (strcmp(auxp, cd->cd_name) || we_matched)
   1083 		return(0);
   1084 
   1085 	we_matched = 1;
   1086 	return(1);
   1087 }
   1088 
   1089 /*
   1090  * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
   1091  * pool. Allocation here is both contiguous and in the lower 16Mb of
   1092  * the address space. Thus being DMA-able for all controllers.
   1093  */
   1094 static u_char *
   1095 alloc_bounceb(u_long len)
   1096 {
   1097 	void	*tmp;
   1098 
   1099 	return((u_char *)alloc_stmem(len, &tmp));
   1100 }
   1101 
   1102 static void
   1103 free_bounceb(u_char *bounceb)
   1104 {
   1105 	free_stmem(bounceb);
   1106 }
   1107 
   1108 /*
   1109  * 5380 interrupt.
   1110  */
   1111 void
   1112 scsi_ctrl(int sr)
   1113 {
   1114 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
   1115 		scsi_idisable();
   1116 		if (!BASEPRI(sr))
   1117 			add_sicallback((si_farg)ncr_ctrl_intr,
   1118 						(void *)cur_softc, 0);
   1119 		else {
   1120 			spl1();
   1121 			ncr_ctrl_intr(cur_softc);
   1122 			spl0();
   1123 		}
   1124 	}
   1125 }
   1126 
   1127 /*
   1128  * DMA controller interrupt
   1129  */
   1130 void
   1131 scsi_dma(int sr)
   1132 {
   1133 	SC_REQ	*reqp;
   1134 
   1135 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
   1136 		scsi_idisable();
   1137 		if (!BASEPRI(sr))
   1138 			add_sicallback((si_farg)ncr_dma_intr,
   1139 					(void *)cur_softc, 0);
   1140 		else {
   1141 			spl1();
   1142 			ncr_dma_intr(cur_softc);
   1143 			spl0();
   1144 		}
   1145 	}
   1146 }
   1147 
   1148 /*
   1149  * Last but not least... Include the general driver code
   1150  */
   1151 #include <atari/dev/ncr5380.c>
   1152