xref: /netbsd-src/sys/arch/atari/dev/atari5380.c (revision 10ad5ffa714ce1a679dcc9dd8159648df2d67b5a)
1 /*	$NetBSD: atari5380.c,v 1.52 2009/07/08 12:23:09 tsutsui Exp $	*/
2 
3 /*
4  * Copyright (c) 1995 Leo Weppelman.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer.
12  * 2. Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  * 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.52 2009/07/08 12:23:09 tsutsui 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 #include <machine/intr.h>
65 
66 #if defined(FALCON_SCSI)
67 #include <machine/dma.h>
68 #endif
69 
70 /*
71  * Set the various driver options
72  */
73 #define	NREQ		18	/* Size of issue queue			*/
74 #define	AUTO_SENSE	1	/* Automatically issue a request-sense 	*/
75 
76 #define	DRNAME		ncrscsi	/* used in various prints		*/
77 #undef	DBG_SEL			/* Show the selection process		*/
78 #undef	DBG_REQ			/* Show enqueued/ready requests		*/
79 #undef	DBG_ERR_RET		/* Show requests with != 0 return code	*/
80 #undef	DBG_NOWRITE		/* Do not allow writes to the targets	*/
81 #undef	DBG_PIO			/* Show the polled-I/O process		*/
82 #undef	DBG_INF			/* Show information transfer process	*/
83 #define	DBG_NOSTATIC		/* No static functions, all in DDB trace*/
84 #define	DBG_PID		15	/* Keep track of driver			*/
85 #define	REAL_DMA		/* Use DMA if sensible			*/
86 #if defined(FALCON_SCSI)
87 #define	REAL_DMA_POLL	1	/* 1: Poll for end of DMA-transfer	*/
88 #else
89 #define	REAL_DMA_POLL	0	/* 1: Poll for end of DMA-transfer	*/
90 #endif
91 #undef	USE_PDMA		/* Use special pdma-transfer function	*/
92 #define MIN_PHYS	65536	/*BARF!!!!*/
93 
94 /*
95  * Include more driver definitions
96  */
97 #include <atari/dev/ncr5380var.h>
98 
99 /*
100  * The atari specific driver options
101  */
102 #undef	NO_TTRAM_DMA		/* Do not use DMA to TT-ram. This	*/
103 				/*    fails on older atari's		*/
104 #define	ENABLE_NCR5380(sc)	cur_softc = sc;
105 
106 static u_char	*alloc_bounceb(u_long);
107 static void	free_bounceb(u_char *);
108 static int	machine_match(struct device *, void *, void *,
109 							struct cfdriver *);
110        void	scsi_ctrl(int);
111        void	scsi_dma(int);
112 
113 /*
114  * Functions that do nothing on the atari
115  */
116 #define	pdma_ready()		0
117 
118 #if defined(TT_SCSI)
119 
120 void	ncr5380_drq_intr(int);
121 
122 /*
123  * Define all the things we need of the DMA-controller
124  */
125 #define	SCSI_DMA	((struct scsi_dma *)AD_SCSI_DMA)
126 #define	SCSI_5380	((struct scsi_5380 *)AD_NCR5380)
127 
128 struct scsi_dma {
129 	volatile u_char		s_dma_ptr[8];	/* use only the odd bytes */
130 	volatile u_char		s_dma_cnt[8];	/* use only the odd bytes */
131 	volatile u_char		s_dma_res[4];	/* data residue register  */
132 	volatile u_char		s_dma_gap;	/* not used		  */
133 	volatile u_char		s_dma_ctrl;	/* control register	  */
134 	volatile u_char		s_dma_gap2;	/* not used		  */
135 	volatile u_char		s_hdma_ctrl;	/* Hades control register */
136 };
137 
138 static inline void set_scsi_dma(volatile u_char *, u_long);
139 static inline u_long get_scsi_dma(volatile u_char *);
140 
141 static inline void
142 set_scsi_dma(volatile u_char *addr, u_long val)
143 {
144 	volatile u_char *address;
145 
146 	address = addr + 1;
147 	__asm("movepl	%0, %1@(0)": :"d" (val), "a" (address));
148 }
149 
150 static inline u_long
151 get_scsi_dma(volatile u_char *addr)
152 {
153 	volatile u_char	*address;
154 	u_long	nval;
155 
156 	address = addr + 1;
157 	__asm("movepl	%1@(0), %0": "=d" (nval) : "a" (address));
158 
159 	return nval;
160 }
161 
162 /*
163  * Defines for TT-DMA control register
164  */
165 #define	SD_BUSERR	0x80		/* 1 = transfer caused bus error*/
166 #define	SD_ZERO		0x40		/* 1 = byte counter is zero	*/
167 #define	SD_ENABLE	0x02		/* 1 = Enable DMA		*/
168 #define	SD_OUT		0x01		/* Direction: memory to SCSI	*/
169 #define	SD_IN		0x00		/* Direction: SCSI to memory	*/
170 
171 /*
172  * Defines for Hades-DMA control register
173  */
174 #define	SDH_BUSERR	0x02		/* 1 = Bus error		*/
175 #define SDH_EOP		0x01		/* 1 = Signal EOP on 5380	*/
176 #define	SDH_ZERO	0x40		/* 1 = Byte counter is zero	*/
177 
178 /*
179  * Define the 5380 register set
180  */
181 struct scsi_5380 {
182 	volatile u_char	scsi_5380[16];	/* use only the odd bytes	*/
183 };
184 #endif /* TT_SCSI */
185 
186 /**********************************************
187  * Variables present for both TT and Falcon.  *
188  **********************************************/
189 
190 /*
191  * Softc of currently active controller (a bit of fake; we only have one)
192  */
193 static struct ncr_softc	*cur_softc;
194 
195 #if defined(TT_SCSI) && !defined(FALCON_SCSI)
196 /*
197  * We can be more efficient for some functions when only TT_SCSI is selected
198  */
199 #define	GET_5380_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
200 #define	SET_5380_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
201 
202 #define scsi_mach_init(sc)	scsi_tt_init(sc)
203 #define scsi_ienable()		scsi_tt_ienable()
204 #define scsi_idisable()		scsi_tt_idisable()
205 #define	scsi_clr_ipend()	scsi_tt_clr_ipend()
206 #define scsi_ipending()		(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)
207 #define scsi_dma_setup(r,p,m)	scsi_tt_dmasetup(r, p, m)
208 #define wrong_dma_range(r,d)	tt_wrong_dma_range(r, d)
209 #define poll_edma(reqp)		tt_poll_edma(reqp)
210 #define get_dma_result(r, b)	tt_get_dma_result(r, b)
211 #define	can_access_5380()	1
212 #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
213 
214 #define fair_to_keep_dma()	1
215 #define claimed_dma()		1
216 #define reconsider_dma()
217 
218 #endif /* defined(TT_SCSI) && !defined(FALCON_SCSI) */
219 
220 #if defined(TT_SCSI)
221 
222 /*
223  * Prototype functions defined below
224  */
225 #ifdef NO_TTRAM_DMA
226 static int tt_wrong_dma_range(SC_REQ *, struct dma_chain *);
227 #endif
228 static void	scsi_tt_init(struct ncr_softc *);
229 static u_char	get_tt_5380_reg(u_short);
230 static void	set_tt_5380_reg(u_short, u_short);
231 static void	scsi_tt_dmasetup(SC_REQ *, u_int, u_char);
232 static int	tt_poll_edma(SC_REQ *);
233 static u_char	*ptov(SC_REQ *, u_long*);
234 static int	tt_get_dma_result(SC_REQ *, u_long *);
235        void	scsi_tt_ienable(void);
236        void	scsi_tt_idisable(void);
237        void	scsi_tt_clr_ipend(void);
238 
239 /*
240  * Define these too, so we can use them locally...
241  */
242 #define	GET_TT_REG(rnum)	SCSI_5380->scsi_5380[(rnum << 1) | 1]
243 #define	SET_TT_REG(rnum,val)	(SCSI_5380->scsi_5380[(rnum << 1) | 1] = val)
244 
245 #ifdef NO_TTRAM_DMA
246 static int
247 tt_wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
248 {
249 	if (dm->dm_addr & 0xff000000) {
250 		reqp->dr_flag |= DRIVER_BOUNCING;
251 		return(1);
252 	}
253 	return(0);
254 }
255 #else
256 #define	tt_wrong_dma_range(reqp, dm)	0
257 #endif
258 
259 static void
260 scsi_tt_init(struct ncr_softc *sc)
261 {
262 	/*
263 	 * Enable SCSI-related interrupts
264 	 */
265 	MFP2->mf_aer  |= 0x80;		/* SCSI IRQ goes HIGH!!!!!	*/
266 
267 	if (machineid & ATARI_TT) {
268 		/* SCSI-DMA interrupts		*/
269 		MFP2->mf_ierb |= IB_SCDM;
270 		MFP2->mf_iprb  = (u_int8_t)~IB_SCDM;
271 		MFP2->mf_imrb |= IB_SCDM;
272 	}
273 	else if (machineid & ATARI_HADES) {
274 		SCSI_DMA->s_hdma_ctrl = 0;
275 
276 		if (intr_establish(2, AUTO_VEC, 0,
277 					(hw_ifun_t)ncr5380_drq_intr,
278 					NULL) == NULL)
279 		panic("scsi_tt_init: Can't establish drq-interrupt");
280 	}
281 	else panic("scsi_tt_init: should not come here");
282 
283 	MFP2->mf_iera |= IA_SCSI;	/* SCSI-5380 interrupts		*/
284 	MFP2->mf_ipra  = (u_int8_t)~IA_SCSI;
285 	MFP2->mf_imra |= IA_SCSI;
286 
287 	/*
288 	 * LWP: DMA transfers to TT-ram causes data to be garbled
289 	 * without notice on some TT-mainboard revisions.
290 	 * If programs generate mysterious Segmentations faults,
291 	 * try enabling NO_TTRAM_DMA.
292 	 */
293 #ifdef NO_TTRAM_DMA
294 	printf(": DMA to TT-RAM is disabled!");
295 #endif
296 }
297 
298 static u_char
299 get_tt_5380_reg(u_short rnum)
300 {
301 	return(SCSI_5380->scsi_5380[(rnum << 1) | 1]);
302 }
303 
304 static void
305 set_tt_5380_reg(u_short rnum, u_short val)
306 {
307 	SCSI_5380->scsi_5380[(rnum << 1) | 1] = val;
308 }
309 
310 extern inline void
311 scsi_tt_ienable(void)
312 {
313 	if (machineid & ATARI_TT)
314 		single_inst_bset_b(MFP2->mf_imrb, IB_SCDM);
315 	single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
316 }
317 
318 extern inline void
319 scsi_tt_idisable(void)
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 extern 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 = (u_int8_t)~IB_SCDM;
335 	MFP2->mf_ipra = (u_int8_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, u_char	mode)
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 	}
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 	u_char	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))
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 u_char *
423 ptov(SC_REQ *reqp, u_long *phaddr)
424 {
425 	struct dma_chain	*dm;
426 	u_char			*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 	u_char	*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 = (u_char *)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) && !(dmstat & SC_PHS_MTCH) && 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) && ((u_long)byte_p & 3)
479 	    && PH_IN(reqp->phase)) {
480 		u_char	*p;
481 		volatile u_char *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 u_char *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 u_char		*data_p = (volatile u_char *)(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, IA_SCSI);
543 				return;
544 			}
545 
546 			/*
547 			 * Signal EOP to 5380
548 			 */
549 			SCSI_DMA->s_hdma_ctrl |= SDH_EOP;
550 		}
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, (void *)cur_softc, 0);
574 
575 		/*
576 		 * Clear DMA-mode
577 		 */
578 		SCSI_DMA->s_dma_ctrl &= ~SD_ENABLE;
579 		if (!poll)
580 			single_inst_bset_b(MFP2->mf_imra, IA_SCSI);
581 
582 		return;
583 	}
584 
585 	write = (SCSI_DMA->s_dma_ctrl & SD_OUT) ? 1 : 0;
586 #if DBG_PID
587 	if (write) {
588 		PID("drq (in)");
589 	} else {
590 		PID("drq (out)");
591 	}
592 #endif
593 
594 	count = get_scsi_dma(SCSI_DMA->s_dma_cnt);
595 	dma_ptr = (u_char *)get_scsi_dma(SCSI_DMA->s_dma_ptr);
596 
597 	/*
598 	 * Keep pushing bytes until we're done or a bus-error
599 	 * signals that the SCSI controller is not ready.
600 	 * NOTE: I tried some optimalizations in these loops,
601 	 *       but they had no effect on transfer speed.
602 	 */
603 	if (write) {
604 		while(count--) {
605 			*data_p = *dma_ptr++;
606 		}
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 	return;
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 u_char	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, u_char);
681 static int	falcon_poll_edma(SC_REQ  *);
682 static int	falcon_get_dma_result(SC_REQ  *, u_long *);
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 	 * Enable disk related interrupts
696 	 */
697 	MFP->mf_ierb  |= IB_DINT;
698 	MFP->mf_iprb   = (u_int8_t)~IB_DINT;
699 	MFP->mf_imrb  |= IB_DINT;
700 }
701 
702 static u_char
703 get_falcon_5380_reg(u_short rnum)
704 {
705 	DMA->dma_mode = DMA_SCSI + rnum;
706 	return(DMA->dma_data);
707 }
708 
709 static void
710 set_falcon_5380_reg(u_short rnum, u_short val)
711 {
712 	DMA->dma_mode = DMA_SCSI + rnum;
713 	DMA->dma_data = val;
714 }
715 
716 extern inline void
717 scsi_falcon_ienable(void)
718 {
719 	single_inst_bset_b(MFP->mf_imrb, IB_DINT);
720 }
721 
722 extern inline void
723 scsi_falcon_idisable(void)
724 {
725 	single_inst_bclr_b(MFP->mf_imrb, IB_DINT);
726 }
727 
728 extern inline void
729 scsi_falcon_clr_ipend(void)
730 {
731 	int	tmp;
732 
733 	tmp = get_falcon_5380_reg(NCR5380_IRCV);
734 	rem_sicallback((si_farg)ncr_ctrl_intr);
735 }
736 
737 extern inline int
738 scsi_falcon_ipending(void)
739 {
740 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)) {
741 		/*
742 		 *  XXX: When DMA is running, we are only allowed to
743 		 *       check the 5380 when DMA _might_ be finished.
744 		 */
745 		if (MFP->mf_gpip & IO_DINT)
746 		    return (0); /* XXX: Actually: we're not allowed to check */
747 
748 		/* LWP: 28-06, must be a DMA interrupt! should the
749 		 * ST-DMA unit be taken out of DMA mode?????
750 		 */
751 		DMA->dma_mode = 0x90;
752 
753 	}
754 	return(get_falcon_5380_reg(NCR5380_DMSTAT) & SC_IRQ_SET);
755 }
756 
757 static int
758 falcon_wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
759 {
760 	/*
761 	 * Do not allow chains yet! See also comment with
762 	 * falcon_poll_edma() !!!
763 	 */
764 	if (((dm - reqp->dm_chain) > 0) || (dm->dm_addr & 0xff000000)) {
765 		reqp->dr_flag |= DRIVER_BOUNCING;
766 		return(1);
767 	}
768 	/*
769 	 * Never allow DMA to happen on a Falcon when the transfer
770 	 * size is no multiple of 512. This is the transfer unit of the
771 	 * ST DMA-controller.
772 	 */
773 	if(dm->dm_count & 511)
774 		return(1);
775 	return(0);
776 }
777 
778 static	int falcon_lock = 0;
779 
780 extern inline int
781 falcon_claimed_dma(void)
782 {
783 	if (falcon_lock != DMA_LOCK_GRANT) {
784 		if (falcon_lock == DMA_LOCK_REQ) {
785 			/*
786 			 * DMA access is being claimed.
787 			 */
788 			return(0);
789 		}
790 		if (!st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
791 						cur_softc, &falcon_lock, 1))
792 			return(0);
793 	}
794 	return(1);
795 }
796 
797 extern inline void
798 falcon_reconsider_dma(void)
799 {
800 	if (falcon_lock && (connected == NULL) && (discon_q == NULL)) {
801 		/*
802 		 * No need to keep DMA locked by us as we are not currently
803 		 * connected and no disconnected jobs are pending.
804 		 */
805 		rem_sicallback((si_farg)ncr_ctrl_intr);
806 		st_dmafree(cur_softc, &falcon_lock);
807 	}
808 
809 	if (!falcon_lock && (issue_q != NULL)) {
810 		/*
811 		 * We must (re)claim DMA access as there are jobs
812 		 * waiting in the issue queue.
813 		 */
814 		st_dmagrab((dma_farg)ncr_ctrl_intr, (dma_farg)run_main,
815 						cur_softc, &falcon_lock, 0);
816 	}
817 }
818 
819 static void
820 fal1_dma(u_int dir, u_int nsects, SC_REQ *reqp)
821 {
822 	dir <<= 8;
823 	st_dmaaddr_set((void *)reqp->dm_cur->dm_addr);
824 	DMA->dma_mode = 0x90 | dir;
825 	DMA->dma_mode = 0x90 | (dir ^ DMA_WRBIT);
826 	DMA->dma_mode = 0x90 | dir;
827 	DMA->dma_data = nsects;
828 	delay(2);	/* _really_ needed (Thomas Gerner) */
829 	DMA->dma_mode = 0x10 | dir;
830 }
831 
832 static void
833 scsi_falcon_dmasetup(SC_REQ *reqp, u_int phase, u_char mode)
834 {
835 	int	nsects = reqp->dm_cur->dm_count / 512; /* XXX */
836 
837 	/*
838 	 * XXX: We should probably clear the fifo before putting the
839 	 *      5380 into DMA-mode.
840 	 */
841 	if (PH_IN(phase)) {
842 		set_falcon_5380_reg(NCR5380_ICOM, 0);
843 		set_falcon_5380_reg(NCR5380_MODE, mode);
844 		set_falcon_5380_reg(NCR5380_IRCV, 0);
845 		fal1_dma(0, nsects, reqp);
846 	}
847 	else {
848 		set_falcon_5380_reg(NCR5380_MODE, mode);
849 		set_falcon_5380_reg(NCR5380_ICOM, SC_ADTB);
850 		set_falcon_5380_reg(NCR5380_DMSTAT, 0);
851 		fal1_dma(1, nsects, reqp);
852 	}
853 }
854 
855 static int
856 falcon_poll_edma(SC_REQ *reqp)
857 {
858 	int	timeout = 9000; /* XXX */
859 
860 	/*
861 	 * Because of the Falcon hardware, it is impossible to reach
862 	 * the 5380 while doing DMA-transfers. So we have to rely on
863 	 * the interrupt line to determine if DMA-has finished. the
864 	 * DMA-controller itself will never fire an interrupt. This means
865 	 * that 'broken-up' DMA transfers are not (yet) possible on the
866 	 * Falcon.
867 	 */
868 	for (;;) {
869 		delay(20);
870 		if (--timeout <= 0) {
871 			ncr_tprint(reqp, "Timeout on polled transfer\n");
872 			reqp->xs->error = XS_TIMEOUT;
873 			return(0);
874 		}
875 		if (!(MFP->mf_gpip & IO_DINT))
876 			break;
877 	}
878 	return(1);
879 }
880 
881 static int
882 falcon_get_dma_result(SC_REQ *reqp, u_long *bytes_left)
883 {
884 	int	rv = 0;
885 	int	st_dmastat;
886 	u_long	bytes_done;
887 
888 	/*
889 	 * Select sector counter register first (See Atari docu.)
890 	 */
891 	DMA->dma_mode = 0x90;
892 	if (!(st_dmastat = DMA->dma_stat) & 0x01) {
893 		/*
894 		 * Misc. DMA-error according to Atari...
895 		 */
896 		ncr_tprint(reqp, "Unknow ST-SCSI error near 0x%x\n",
897 							st_dmaaddr_get());
898 		reqp->xs->error = XS_DRIVER_STUFFUP;
899 		rv = 1;
900 	}
901 	/*
902 	 * Because we NEVER start DMA on the Falcon when the data size
903 	 * is not a multiple of 512 bytes, we can safely round down the
904 	 * byte count on writes. We need to because in case of a disconnect,
905 	 * the DMA has already prefetched the next couple of bytes.
906 	 * On read, these byte counts are an error. They are logged and
907 	 * should be handled by the mi-part of the driver.
908 	 * NOTE: We formerly did this by using the 'byte-count-zero' bit
909 	 *       of the DMA controller, but this didn't seem to work???
910          *       [lwp 29/06/96]
911 	 */
912 	bytes_done = st_dmaaddr_get() - reqp->dm_cur->dm_addr;
913 	if (bytes_done & 511) {
914 		if (PH_IN(reqp->phase)) {
915 			ncr_tprint(reqp, "Byte count on read not a multiple "
916 					 "of 512 (%ld)\n", bytes_done);
917 		}
918 		bytes_done &= ~511;
919 	}
920 	if ((*bytes_left = reqp->dm_cur->dm_count - bytes_done) == 0)
921 		rv = 1;
922 	return(rv);
923 }
924 
925 static int
926 falcon_can_access_5380()
927 {
928 	if (connected && (connected->dr_flag & DRIVER_IN_DMA)
929 		&& (MFP->mf_gpip & IO_DINT))
930 			return(0);
931 	return(1);
932 }
933 
934 #endif /* defined(FALCON_SCSI) */
935 
936 #if defined(TT_SCSI) && defined(FALCON_SCSI)
937 /*
938  * Define some functions to support _both_ TT and Falcon SCSI
939  */
940 
941 /*
942  * The prototypes first...
943  */
944 static void	scsi_mach_init(struct ncr_softc *);
945        void	scsi_ienable(void);
946        void	scsi_idisable(void);
947        void	scsi_clr_ipend(void);
948        int	scsi_ipending(void);
949        void	scsi_dma_setup(SC_REQ *, u_int, u_char);
950        int	wrong_dma_range(SC_REQ *, struct dma_chain *);
951        int	poll_edma(SC_REQ *);
952        int	get_dma_result(SC_REQ *, u_long *);
953        int	can_access_5380(void);
954 
955 /*
956  * Register access will be done through the following 2 function pointers.
957  */
958 static u_char	(*get_5380_reg)(u_short);
959 static void	(*set_5380_reg)(u_short, u_short);
960 
961 #define	GET_5380_REG	(*get_5380_reg)
962 #define	SET_5380_REG	(*set_5380_reg)
963 
964 static void
965 scsi_mach_init(struct ncr_softc *sc)
966 {
967 	if (machineid & ATARI_FALCON) {
968 		get_5380_reg = get_falcon_5380_reg;
969 		set_5380_reg = set_falcon_5380_reg;
970 		scsi_falcon_init(sc);
971 	}
972 	else {
973 		get_5380_reg = get_tt_5380_reg;
974 		set_5380_reg = set_tt_5380_reg;
975 		scsi_tt_init(sc);
976 	}
977 }
978 
979 extern inline void
980 scsi_ienable(void)
981 {
982 	if (machineid & ATARI_FALCON)
983 		scsi_falcon_ienable();
984 	else scsi_tt_ienable();
985 }
986 
987 extern inline void
988 scsi_idisable(void)
989 {
990 	if (machineid & ATARI_FALCON)
991 		scsi_falcon_idisable();
992 	else scsi_tt_idisable();
993 }
994 
995 extern inline void
996 scsi_clr_ipend(void)
997 {
998 	if (machineid & ATARI_FALCON)
999 		scsi_falcon_clr_ipend();
1000 	else scsi_tt_clr_ipend();
1001 }
1002 
1003 extern inline int
1004 scsi_ipending(void)
1005 {
1006 	if (machineid & ATARI_FALCON)
1007 		return(scsi_falcon_ipending());
1008 	else return (GET_TT_REG(NCR5380_DMSTAT) & SC_IRQ_SET);
1009 }
1010 
1011 extern inline void
1012 scsi_dma_setup(SC_REQ *reqp, u_int phase, u_char mbase)
1013 {
1014 	if (machineid & ATARI_FALCON)
1015 		scsi_falcon_dmasetup(reqp, phase, mbase);
1016 	else scsi_tt_dmasetup(reqp, phase, mbase);
1017 }
1018 
1019 extern inline int
1020 wrong_dma_range(SC_REQ *reqp, struct dma_chain *dm)
1021 {
1022 	if (machineid & ATARI_FALCON)
1023 		return(falcon_wrong_dma_range(reqp, dm));
1024 	else return(tt_wrong_dma_range(reqp, dm));
1025 }
1026 
1027 extern inline int
1028 poll_edma(SC_REQ *reqp)
1029 {
1030 	if (machineid & ATARI_FALCON)
1031 		return(falcon_poll_edma(reqp));
1032 	else return(tt_poll_edma(reqp));
1033 }
1034 
1035 extern inline int
1036 get_dma_result(SC_REQ *reqp, u_long *bytes_left)
1037 {
1038 	if (machineid & ATARI_FALCON)
1039 		return(falcon_get_dma_result(reqp, bytes_left));
1040 	else return(tt_get_dma_result(reqp, bytes_left));
1041 }
1042 
1043 extern inline int
1044 can_access_5380()
1045 {
1046 	if (machineid & ATARI_FALCON)
1047 		return(falcon_can_access_5380());
1048 	return(1);
1049 }
1050 
1051 #define emulated_dma()		((machineid & ATARI_HADES) ? 1 : 0)
1052 
1053 /*
1054  * Locking stuff. All turns into NOP's on the TT.
1055  */
1056 #define	fair_to_keep_dma()	((machineid & ATARI_FALCON) ?		\
1057 						!st_dmawanted() : 1)
1058 #define	claimed_dma()		((machineid & ATARI_FALCON) ?		\
1059 						falcon_claimed_dma() : 1)
1060 #define reconsider_dma()	{					\
1061 					if(machineid & ATARI_FALCON)	\
1062 						falcon_reconsider_dma();\
1063 				}
1064 #endif /* defined(TT_SCSI) && defined(FALCON_SCSI) */
1065 
1066 /**********************************************
1067  * Functions present for both TT and Falcon.  *
1068  **********************************************/
1069 /*
1070  * Our autoconfig matching function
1071  */
1072 static int
1073 machine_match(struct device *pdp, void *match, void *auxp,
1074 						struct cfdriver *cd)
1075 {
1076 	static int	we_matched = 0; /* Only one unit	*/
1077 
1078 	if (strcmp(auxp, cd->cd_name) || we_matched)
1079 		return(0);
1080 
1081 	we_matched = 1;
1082 	return(1);
1083 }
1084 
1085 /*
1086  * Bounce buffer (de)allocation. Those buffers are gotten from the ST-mem
1087  * pool. Allocation here is both contiguous and in the lower 16Mb of
1088  * the address space. Thus being DMA-able for all controllers.
1089  */
1090 static u_char *
1091 alloc_bounceb(u_long len)
1092 {
1093 	void	*tmp;
1094 
1095 	return((u_char *)alloc_stmem(len, &tmp));
1096 }
1097 
1098 static void
1099 free_bounceb(u_char *bounceb)
1100 {
1101 	free_stmem(bounceb);
1102 }
1103 
1104 /*
1105  * 5380 interrupt.
1106  */
1107 void
1108 scsi_ctrl(int sr)
1109 {
1110 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
1111 		scsi_idisable();
1112 		if (!BASEPRI(sr))
1113 			add_sicallback((si_farg)ncr_ctrl_intr,
1114 						(void *)cur_softc, 0);
1115 		else {
1116 			spl1();
1117 			ncr_ctrl_intr(cur_softc);
1118 			spl0();
1119 		}
1120 	}
1121 }
1122 
1123 /*
1124  * DMA controller interrupt
1125  */
1126 void
1127 scsi_dma(int sr)
1128 {
1129 	SC_REQ	*reqp;
1130 
1131 	if ((reqp = connected) && (reqp->dr_flag & DRIVER_IN_DMA)) {
1132 		scsi_idisable();
1133 		if (!BASEPRI(sr))
1134 			add_sicallback((si_farg)ncr_dma_intr,
1135 					(void *)cur_softc, 0);
1136 		else {
1137 			spl1();
1138 			ncr_dma_intr(cur_softc);
1139 			spl0();
1140 		}
1141 	}
1142 }
1143 
1144 /*
1145  * Last but not least... Include the general driver code
1146  */
1147 #include <atari/dev/ncr5380.c>
1148