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