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