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