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