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