xref: /netbsd-src/sys/arch/mac68k/dev/sbc.c (revision 8e33eff89e26cf71871ead62f0d5063e1313c33a)
1 /*	$NetBSD: sbc.c,v 1.60 2024/09/14 21:11:07 nat Exp $	*/
2 
3 /*
4  * Copyright (C) 1996 Scott Reynolds.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. The name of the author may not be used to endorse or promote products
15  *    derived from this software without specific prior written permission
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 /*
30  * This file contains only the machine-dependent parts of the mac68k
31  * NCR 5380 SCSI driver.  (Autoconfig stuff and PDMA functions.)
32  * The machine-independent parts are in ncr5380sbc.c
33  *
34  * Supported hardware includes:
35  * Macintosh II family 5380-based controller
36  *
37  * Credits, history:
38  *
39  * Scott Reynolds wrote this module, based on work by Allen Briggs
40  * (mac68k), Gordon W. Ross and David Jones (sun3), and Leo Weppelman
41  * (atari).  Thanks to Allen for supplying crucial interpretation of the
42  * NetBSD/mac68k 1.1 'ncrscsi' driver.  Also, Allen, Gordon, and Jason
43  * Thorpe all helped to refine this code, and were considerable sources
44  * of moral support.
45  */
46 
47 #include <sys/cdefs.h>
48 __KERNEL_RCSID(0, "$NetBSD: sbc.c,v 1.60 2024/09/14 21:11:07 nat Exp $");
49 
50 #include "opt_ddb.h"
51 
52 #include <sys/types.h>
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/kernel.h>
56 #include <sys/errno.h>
57 #include <sys/device.h>
58 #include <sys/buf.h>
59 #include <sys/proc.h>
60 
61 #include <dev/scsipi/scsi_all.h>
62 #include <dev/scsipi/scsipi_all.h>
63 #include <dev/scsipi/scsipi_debug.h>
64 #include <dev/scsipi/scsiconf.h>
65 
66 #include <dev/ic/ncr5380reg.h>
67 #include <dev/ic/ncr5380var.h>
68 
69 #include <machine/cpu.h>
70 #include <machine/viareg.h>
71 
72 #include <mac68k/dev/sbcreg.h>
73 #include <mac68k/dev/sbcvar.h>
74 
75 /* SBC_DEBUG --  relies on DDB */
76 #ifdef SBC_DEBUG
77 # define	SBC_DB_INTR	0x01
78 # define	SBC_DB_DMA	0x02
79 # define	SBC_DB_REG	0x04
80 # define	SBC_DB_BREAK	0x08
81 # ifndef DDB
82 #  define	Debugger()	printf("Debug: sbc.c:%d\n", __LINE__)
83 # endif
84 # define	SBC_BREAK \
85 		do { if (sbc_debug & SBC_DB_BREAK) Debugger(); } while (0)
86 #else
87 # define	SBC_BREAK
88 #endif
89 
90 
91 int	sbc_debug = 0 /* | SBC_DB_INTR | SBC_DB_DMA */;
92 int	sbc_link_flags = 0 /* | SDEV_DB2 */;
93 int	sbc_options = 0 /* | SBC_PDMA */;
94 
95 extern label_t	*nofault;
96 extern void *	m68k_fault_addr;
97 
98 static	int	sbc_wait_busy(struct ncr5380_softc *);
99 static	int	sbc_ready(struct ncr5380_softc *);
100 static	int	sbc_wait_dreq(struct ncr5380_softc *);
101 
102 
103 /***
104  * General support for Mac-specific SCSI logic.
105  ***/
106 
107 /* These are used in the following inline functions. */
108 int sbc_wait_busy_timo = 1000 * 5000;	/* X2 = 10 S. */
109 int sbc_ready_timo = 1000 * 5000;	/* X2 = 10 S. */
110 int sbc_wait_dreq_timo = 1000 * 5000;	/* X2 = 10 S. */
111 
112 /* Return zero on success. */
113 static inline int
114 sbc_wait_busy(struct ncr5380_softc *sc)
115 {
116 	int timo = sbc_wait_busy_timo;
117 	for (;;) {
118 		if (SCI_BUSY(sc)) {
119 			timo = 0;	/* return 0 */
120 			break;
121 		}
122 		if (--timo < 0)
123 			break;	/* return -1 */
124 		delay(2);
125 	}
126 	return (timo);
127 }
128 
129 static inline int
130 sbc_ready(struct ncr5380_softc *sc)
131 {
132 	int timo = sbc_ready_timo;
133 
134 	for (;;) {
135 		if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
136 		    == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
137 			timo = 0;
138 			break;
139 		}
140 		if (((*sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0)
141 		    || (SCI_BUSY(sc) == 0)) {
142 			timo = -1;
143 			break;
144 		}
145 		if (--timo < 0)
146 			break;	/* return -1 */
147 		delay(2);
148 	}
149 	return (timo);
150 }
151 
152 static inline int
153 sbc_wait_dreq(struct ncr5380_softc *sc)
154 {
155 	int timo = sbc_wait_dreq_timo;
156 
157 	for (;;) {
158 		if ((*sc->sci_csr & (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH))
159 		    == (SCI_CSR_DREQ|SCI_CSR_PHASE_MATCH)) {
160 			timo = 0;
161 			break;
162 		}
163 		if (--timo < 0)
164 			break;	/* return -1 */
165 		delay(2);
166 	}
167 	return (timo);
168 }
169 
170 void
171 sbc_irq_intr(void *p)
172 {
173 	struct ncr5380_softc *ncr_sc = p;
174 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
175 	int claimed = 0;
176 
177 	/* How we ever arrive here without IRQ set is a mystery... */
178 	if (*ncr_sc->sci_csr & SCI_CSR_INT) {
179 #ifdef SBC_DEBUG
180 		if (sbc_debug & SBC_DB_INTR)
181 			decode_5380_intr(ncr_sc);
182 #endif
183 		if (!cold)
184 			claimed = ncr5380_intr(ncr_sc);
185 		if (!claimed) {
186 			if (((*ncr_sc->sci_csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT)
187 			    && ((*ncr_sc->sci_bus_csr & ~SCI_BUS_RST) == 0)) {
188 				SCI_CLR_INTR(ncr_sc);	/* RST interrupt */
189 				if (sc->sc_clrintr)
190 					(*sc->sc_clrintr)(ncr_sc);
191 			}
192 #ifdef SBC_DEBUG
193 			else {
194 				printf("%s: spurious intr\n",
195 				    device_xname(ncr_sc->sc_dev));
196 				SBC_BREAK;
197 			}
198 #endif
199 		}
200 	}
201 }
202 
203 #ifdef SBC_DEBUG
204 void
205 decode_5380_intr(struct ncr5380_softc *ncr_sc)
206 {
207 	u_int8_t csr = *ncr_sc->sci_csr;
208 	u_int8_t bus_csr = *ncr_sc->sci_bus_csr;
209 
210 	if (((csr & ~(SCI_CSR_PHASE_MATCH | SCI_CSR_ATN)) == SCI_CSR_INT) &&
211 	    ((bus_csr & ~(SCI_BUS_MSG | SCI_BUS_CD | SCI_BUS_IO | SCI_BUS_DBP)) == SCI_BUS_SEL)) {
212 		if (csr & SCI_BUS_IO)
213 			printf("%s: reselect\n", device_xname(ncr_sc->sc_dev));
214 		else
215 			printf("%s: select\n", device_xname(ncr_sc->sc_dev));
216 	} else if (((csr & ~SCI_CSR_ACK) == (SCI_CSR_DONE | SCI_CSR_INT)) &&
217 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
218 		printf("%s: DMA eop\n", device_xname(ncr_sc->sc_dev));
219 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == SCI_CSR_INT) &&
220 	    ((bus_csr & ~SCI_BUS_RST) == 0))
221 		printf("%s: bus reset\n", device_xname(ncr_sc->sc_dev));
222 	else if (((csr & ~(SCI_CSR_DREQ | SCI_CSR_ATN | SCI_CSR_ACK)) == (SCI_CSR_PERR | SCI_CSR_INT | SCI_CSR_PHASE_MATCH)) &&
223 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_SEL)) == SCI_BUS_BSY))
224 		printf("%s: parity error\n", device_xname(ncr_sc->sc_dev));
225 	else if (((csr & ~SCI_CSR_ATN) == SCI_CSR_INT) &&
226 	    ((bus_csr & (SCI_BUS_RST | SCI_BUS_BSY | SCI_BUS_REQ | SCI_BUS_SEL)) == (SCI_BUS_BSY | SCI_BUS_REQ)))
227 		printf("%s: phase mismatch\n", device_xname(ncr_sc->sc_dev));
228 	else if (((csr & ~SCI_CSR_PHASE_MATCH) == (SCI_CSR_INT | SCI_CSR_DISC)) &&
229 	    (bus_csr == 0))
230 		printf("%s: disconnect\n", device_xname(ncr_sc->sc_dev));
231 	else
232 		printf("%s: unknown intr: csr=%x, bus_csr=%x\n",
233 		    device_xname(ncr_sc->sc_dev), csr, bus_csr);
234 }
235 #endif
236 
237 
238 /***
239  * The following code implements polled PDMA.
240  ***/
241 
242 int
243 sbc_pdma_in(struct ncr5380_softc *ncr_sc, int phase, int datalen, u_char *data)
244 {
245 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
246 	volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
247 	volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
248 	label_t faultbuf;
249 	int resid, s;
250 
251 	if (datalen < ncr_sc->sc_min_dma_len ||
252 	    (sc->sc_options & SBC_PDMA) == 0 ||
253 	    (ncr_sc->sc_current != NULL &&
254 	    (ncr_sc->sc_current->sr_xs->xs_control & XS_CTL_POLL)))
255 		return ncr5380_pio_in(ncr_sc, phase, datalen, data);
256 
257 	s = splbio();
258 	if (sbc_wait_busy(ncr_sc)) {
259 		splx(s);
260 		return 0;
261 	}
262 
263 	*ncr_sc->sci_mode |= SCI_MODE_DMA;
264 	*ncr_sc->sci_irecv = 0;
265 
266 	resid = datalen;
267 
268 	/*
269 	 * Setup for a possible bus error caused by SCSI controller
270 	 * switching out of DATA OUT before we're done with the
271 	 * current transfer.  (See comment before sbc_drq_intr().)
272 	 */
273 	nofault = &faultbuf;
274 	if (setjmp(nofault)) {
275 		goto interrupt;
276 	}
277 
278 #define R4	*(u_int32_t *)data = *long_data, data += 4;
279 	for (; resid >= 128; resid -= 128) {
280 		if (sbc_ready(ncr_sc))
281 			goto interrupt;
282 		R4; R4; R4; R4; R4; R4; R4; R4;
283 		R4; R4; R4; R4; R4; R4; R4; R4;
284 		R4; R4; R4; R4; R4; R4; R4; R4;
285 		R4; R4; R4; R4; R4; R4; R4; R4;		/* 128 */
286 	}
287 	while (resid) {
288 		if (sbc_ready(ncr_sc))
289 			goto interrupt;
290 		*(u_int8_t *)data = *byte_data, data += 1;
291 		resid--;
292 	}
293 #undef R4
294 
295 interrupt:
296 	nofault = NULL;
297 	SCI_CLR_INTR(ncr_sc);
298 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
299 	*ncr_sc->sci_icmd = 0;
300 	splx(s);
301 	return (datalen - resid);
302 }
303 
304 int
305 sbc_pdma_out(struct ncr5380_softc *ncr_sc, int phase, int datalen, u_char *data)
306 {
307 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
308 	volatile u_int32_t *long_data = (u_int32_t *)sc->sc_drq_addr;
309 	volatile u_int8_t *byte_data = (u_int8_t *)sc->sc_nodrq_addr;
310 	label_t faultbuf;
311 	int resid, s;
312 	u_int8_t icmd;
313 
314 #if 1
315 	/* Work around lame gcc initialization bug */
316 	(void)&data;
317 #endif
318 
319 	if (datalen < ncr_sc->sc_min_dma_len ||
320 	    (sc->sc_options & SBC_PDMA) == 0 ||
321 	    (sc->sc_options & SBC_PDMA_NO_WRITE) ||
322 	    (ncr_sc->sc_current != NULL &&
323 	    (ncr_sc->sc_current->sr_xs->xs_control & XS_CTL_POLL)))
324 		return ncr5380_pio_out(ncr_sc, phase, datalen, data);
325 
326 	s = splbio();
327 	if (sbc_wait_busy(ncr_sc)) {
328 		splx(s);
329 		return 0;
330 	}
331 
332 	icmd = *(ncr_sc->sci_icmd) & SCI_ICMD_RMASK;
333 	*ncr_sc->sci_icmd = icmd | SCI_ICMD_DATA;
334 	*ncr_sc->sci_mode |= SCI_MODE_DMA;
335 	*ncr_sc->sci_dma_send = 0;
336 
337 	/*
338 	 * Setup for a possible bus error caused by SCSI controller
339 	 * switching out of DATA OUT before we're done with the
340 	 * current transfer.  (See comment before sbc_drq_intr().)
341 	 */
342 	nofault = &faultbuf;
343 
344 	if (setjmp(nofault)) {
345 		printf("buf = 0x%lx, fault = 0x%lx\n",
346 		    (u_long)sc->sc_drq_addr, (u_long)m68k_fault_addr);
347 		panic("Unexpected bus error in sbc_pdma_out()");
348 	}
349 
350 #define W1	*byte_data = *(u_int8_t *)data, data += 1
351 #define W4	*long_data = *(u_int32_t *)data, data += 4
352 	for (resid = datalen; resid >= 64; resid -= 64) {
353 		if (sbc_ready(ncr_sc))
354 			goto interrupt;
355 		W1;
356 		if (sbc_ready(ncr_sc))
357 			goto interrupt;
358 		W1;
359 		if (sbc_ready(ncr_sc))
360 			goto interrupt;
361 		W1;
362 		if (sbc_ready(ncr_sc))
363 			goto interrupt;
364 		W1;
365 		if (sbc_ready(ncr_sc))
366 			goto interrupt;
367 		W4; W4; W4; W4;
368 		W4; W4; W4; W4;
369 		W4; W4; W4; W4;
370 		W4; W4; W4;
371 	}
372 	while (resid) {
373 		if (sbc_ready(ncr_sc))
374 			goto interrupt;
375 		W1;
376 		resid--;
377 	}
378 #undef  W1
379 #undef  W4
380 	if (sbc_wait_dreq(ncr_sc))
381 		printf("%s: timeout waiting for DREQ.\n",
382 		    device_xname(ncr_sc->sc_dev));
383 
384 	*byte_data = 0;
385 	goto done;
386 
387 interrupt:
388 	if ((*ncr_sc->sci_csr & SCI_CSR_PHASE_MATCH) == 0) {
389 		*ncr_sc->sci_icmd = icmd & ~SCI_ICMD_DATA;
390 		--resid;
391 	}
392 
393 done:
394 	SCI_CLR_INTR(ncr_sc);
395 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
396 	*ncr_sc->sci_icmd = icmd;
397 	splx(s);
398 	return (datalen - resid);
399 }
400 
401 
402 /***
403  * The following code implements interrupt-driven PDMA.
404  ***/
405 
406 /*
407  * This is the meat of the PDMA transfer.
408  * When we get here, we shove data as fast as the mac can take it.
409  * We depend on several things:
410  *   * All macs after the Mac Plus that have a 5380 chip should have a general
411  *     logic IC that handshakes data for blind transfers.
412  *   * If the SCSI controller finishes sending/receiving data before we do,
413  *     the same general logic IC will generate a /BERR for us in short order.
414  *   * The fault address for said /BERR minus the base address for the
415  *     transfer will be the amount of data that was actually written.
416  *
417  * We use the nofault flag and the setjmp/longjmp in locore.s so we can
418  * detect and handle the bus error for early termination of a command.
419  * This is usually caused by a disconnecting target.
420  */
421 void
422 sbc_drq_intr(void *p)
423 {
424 	struct sbc_softc *sc = (struct sbc_softc *)p;
425 	struct ncr5380_softc *ncr_sc = (struct ncr5380_softc *)p;
426 	struct sci_req *sr = ncr_sc->sc_current;
427 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
428 	label_t faultbuf;
429 	volatile u_int32_t *long_drq;
430 	u_int32_t *long_data;
431 	volatile u_int8_t *drq = 0;	/* XXX gcc4 -Wuninitialized */
432 	u_int8_t *data;
433 	int block, count, dcount, resid;
434 
435 	/*
436 	 * If we're not ready to xfer data, or have no more, just return.
437 	 */
438 	if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0 || dh->dh_len == 0)
439 		return;
440 
441 #ifdef SBC_DEBUG
442 	if (sbc_debug & SBC_DB_INTR)
443 		printf("%s: drq intr, dh_len=0x%x, dh_flags=0x%x\n",
444 		    device_xname(ncr_sc->sc_dev), dh->dh_len, dh->dh_flags);
445 #endif
446 
447 	/*
448 	 * Setup for a possible bus error caused by SCSI controller
449 	 * switching out of DATA-IN/OUT before we're done with the
450 	 * current transfer.
451 	 */
452 	nofault = &faultbuf;
453 
454 	if (setjmp(nofault)) {
455 		nofault = (label_t *)0;
456 		if ((dh->dh_flags & SBC_DH_DONE) == 0) {
457 			count = ((  (u_long)m68k_fault_addr
458 				  - (u_long)sc->sc_drq_addr));
459 
460 			if ((count < 0) || (count > dh->dh_len)) {
461 				printf("%s: complete=0x%x (pending 0x%x)\n",
462 				    device_xname(ncr_sc->sc_dev), count,
463 				    dh->dh_len);
464 				panic("something is wrong");
465 			}
466 
467 			dh->dh_addr += count;
468 			dh->dh_len -= count;
469 		} else
470 			count = 0;
471 
472 #ifdef SBC_DEBUG
473 		if (sbc_debug & SBC_DB_INTR)
474 			printf("%s: drq /berr, complete=0x%x (pending 0x%x)\n",
475 			   device_xname(ncr_sc->sc_dev), count, dh->dh_len);
476 #endif
477 		m68k_fault_addr = 0;
478 
479 		return;
480 	}
481 
482 	if (dh->dh_flags & SBC_DH_OUT) { /* Data Out */
483 		dcount = 0;
484 
485 		/*
486 		 * Get the source address aligned.
487 		 */
488 		resid =
489 		    count = uimin(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
490 		if (count && count < 4) {
491 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
492 			data = (u_int8_t *)dh->dh_addr;
493 
494 #define W1		*drq++ = *data++
495 			while (count) {
496 				W1; count--;
497 			}
498 #undef W1
499 			dh->dh_addr += resid;
500 			dh->dh_len -= resid;
501 		}
502 
503 		/*
504 		 * Start the transfer.
505 		 */
506 		while (dh->dh_len) {
507 			if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0)
508 				goto no_more;
509 
510 			block = 512;
511 			if (resid && resid != 4)
512 				block -= 4;
513 			resid = 0;
514 
515 			dcount = count = uimin(dh->dh_len, block);
516 			long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
517 			long_data = (u_int32_t *)dh->dh_addr;
518 
519 #define W4		*long_drq++ = *long_data++
520 			while (count >= 64) {
521 				W4; W4; W4; W4; W4; W4; W4; W4;
522 				W4; W4; W4; W4; W4; W4; W4; W4; /*  64 */
523 				count -= 64;
524 			}
525 			while (count >= 4) {
526 				W4; count -= 4;
527 			}
528 #undef W4
529 			data = (u_int8_t *)long_data;
530 			drq = (volatile u_int8_t *)long_drq;
531 
532 #define W1		*drq++ = *data++
533 			while (count) {
534 				W1; count--;
535 			}
536 #undef W1
537 			dh->dh_len -= dcount;
538 			dh->dh_addr += dcount;
539 		}
540 		dh->dh_flags |= SBC_DH_DONE;
541 
542 		/*
543 		 * XXX -- Read a byte from the SBC to trigger a /BERR.
544 		 * This seems to be necessary for us to notice that
545 		 * the target has disconnected.  Ick.  06 jun 1996 (sr)
546 		 */
547 		(void)*drq;
548 	} else {	/* Data In */
549 		/*
550 		 * Get the dest address aligned.
551 		 */
552 		resid =
553 		    count = uimin(dh->dh_len, 4 - (((int)dh->dh_addr) & 0x3));
554 		if (count && count < 4) {
555 			data = (u_int8_t *)dh->dh_addr;
556 			drq = (volatile u_int8_t *)sc->sc_drq_addr;
557 			while (count) {
558 				*data++ = *drq++;
559 				count--;
560 			}
561 			dh->dh_addr += resid;
562 			dh->dh_len -= resid;
563 		}
564 
565 		/*
566 		 * Start the transfer.
567 		 */
568 		while (dh->dh_len) {
569 			if ((*ncr_sc->sci_csr & SCI_CSR_DREQ) == 0)
570 				goto no_more;
571 
572 			block = 512;
573 			if (resid && resid != 4)
574 				block -= 4;
575 			resid = 0;
576 
577 			dcount = count = uimin(dh->dh_len, block);
578 			long_data = (u_int32_t *)dh->dh_addr;
579 			long_drq = (volatile u_int32_t *)sc->sc_drq_addr;
580 
581 #define R4		*long_data++ = *long_drq++
582 			while (count >= 64) {
583 				R4; R4; R4; R4; R4; R4; R4; R4;
584 				R4; R4; R4; R4; R4; R4; R4; R4;	/* 64 */
585 				count -= 64;
586 			}
587 			while (count >= 4) {
588 				R4; count -= 4;
589 			}
590 #undef R4
591 			data = (u_int8_t *)long_data;
592 			drq = (volatile u_int8_t *)long_drq;
593 			while (count) {
594 				*data++ = *drq++;
595 				count--;
596 			}
597 			dh->dh_len -= dcount;
598 			dh->dh_addr += dcount;
599 		}
600 		dh->dh_flags |= SBC_DH_DONE;
601 	}
602 
603 no_more:
604 	/*
605 	 * OK.  No bus error occurred above.  Clear the nofault flag
606 	 * so we no longer short-circuit bus errors.
607 	 */
608 	nofault = (label_t *)0;
609 
610 #ifdef SBC_DEBUG
611 	if (sbc_debug & (SBC_DB_REG | SBC_DB_INTR))
612 		printf("%s: drq intr complete: csr=0x%x, bus_csr=0x%x\n",
613 		    device_xname(ncr_sc->sc_dev), *ncr_sc->sci_csr,
614 		    *ncr_sc->sci_bus_csr);
615 #endif
616 }
617 
618 void
619 sbc_dma_alloc(struct ncr5380_softc *ncr_sc)
620 {
621 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
622 	struct sci_req *sr = ncr_sc->sc_current;
623 	struct scsipi_xfer *xs = sr->sr_xs;
624 	struct sbc_pdma_handle *dh;
625 	int		i, xlen;
626 
627 #ifdef DIAGNOSTIC
628 	if (sr->sr_dma_hand != NULL)
629 		panic("sbc_dma_alloc: already have PDMA handle");
630 #endif
631 
632 	/* Polled transfers shouldn't allocate a PDMA handle. */
633 	if (sr->sr_flags & SR_IMMED)
634 		return;
635 
636 	xlen = ncr_sc->sc_datalen;
637 
638 	/* Make sure our caller checked sc_min_dma_len. */
639 	if (xlen < MIN_DMA_LEN)
640 		panic("sbc_dma_alloc: len=0x%x", xlen);
641 
642 	/*
643 	 * Find free PDMA handle.  Guaranteed to find one since we
644 	 * have as many PDMA handles as the driver has processes.
645 	 * (instances?)
646 	 */
647 	 for (i = 0; i < SCI_OPENINGS; i++) {
648 		if ((sc->sc_pdma[i].dh_flags & SBC_DH_BUSY) == 0)
649 			goto found;
650 	}
651 	panic("sbc: no free PDMA handles");
652 found:
653 	dh = &sc->sc_pdma[i];
654 	dh->dh_flags = SBC_DH_BUSY;
655 	dh->dh_addr = ncr_sc->sc_dataptr;
656 	dh->dh_len = xlen;
657 
658 	/* Copy the 'write' flag for convenience. */
659 	if (xs->xs_control & XS_CTL_DATA_OUT)
660 		dh->dh_flags |= SBC_DH_OUT;
661 
662 	sr->sr_dma_hand = dh;
663 }
664 
665 void
666 sbc_dma_free(struct ncr5380_softc *ncr_sc)
667 {
668 	struct sci_req *sr = ncr_sc->sc_current;
669 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
670 
671 #ifdef DIAGNOSTIC
672 	if (sr->sr_dma_hand == NULL)
673 		panic("sbc_dma_free: no DMA handle");
674 #endif
675 
676 	if (ncr_sc->sc_state & NCR_DOINGDMA)
677 		panic("sbc_dma_free: free while in progress");
678 
679 	if (dh->dh_flags & SBC_DH_BUSY) {
680 		dh->dh_flags = 0;
681 		dh->dh_addr = NULL;
682 		dh->dh_len = 0;
683 	}
684 	sr->sr_dma_hand = NULL;
685 }
686 
687 void
688 sbc_dma_poll(struct ncr5380_softc *ncr_sc)
689 {
690 	struct sci_req *sr = ncr_sc->sc_current;
691 
692 	/*
693 	 * We shouldn't arrive here; if SR_IMMED is set, then
694 	 * dma_alloc() should have refused to allocate a handle
695 	 * for the transfer.  This forces the polled PDMA code
696 	 * to handle the request...
697 	 */
698 #ifdef SBC_DEBUG
699 	if (sbc_debug & SBC_DB_DMA)
700 		printf("%s: lost DRQ interrupt?\n",
701 		    device_xname(ncr_sc->sc_dev));
702 #endif
703 	sr->sr_flags |= SR_OVERDUE;
704 }
705 
706 void
707 sbc_dma_setup(struct ncr5380_softc *ncr_sc)
708 {
709 	/* Not needed; we don't have real DMA */
710 }
711 
712 void
713 sbc_dma_start(struct ncr5380_softc *ncr_sc)
714 {
715 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
716 	struct sci_req *sr = ncr_sc->sc_current;
717 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
718 
719 	/*
720 	 * Match bus phase, clear pending interrupts, set DMA mode, and
721 	 * assert data bus (for writing only), then start the transfer.
722 	 */
723 	if (dh->dh_flags & SBC_DH_OUT) {
724 		*ncr_sc->sci_tcmd = PHASE_DATA_OUT;
725 		SCI_CLR_INTR(ncr_sc);
726 		if (sc->sc_clrintr)
727 			(*sc->sc_clrintr)(ncr_sc);
728 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
729 		*ncr_sc->sci_icmd = SCI_ICMD_DATA;
730 		*ncr_sc->sci_dma_send = 0;
731 	} else {
732 		*ncr_sc->sci_tcmd = PHASE_DATA_IN;
733 		SCI_CLR_INTR(ncr_sc);
734 		if (sc->sc_clrintr)
735 			(*sc->sc_clrintr)(ncr_sc);
736 		*ncr_sc->sci_mode |= SCI_MODE_DMA;
737 		*ncr_sc->sci_icmd = 0;
738 		*ncr_sc->sci_irecv = 0;
739 	}
740 	ncr_sc->sc_state |= NCR_DOINGDMA;
741 
742 #ifdef SBC_DEBUG
743 	if (sbc_debug & SBC_DB_DMA)
744 		printf("%s: PDMA started, va=%p, len=0x%x\n",
745 		    device_xname(ncr_sc->sc_dev), dh->dh_addr, dh->dh_len);
746 #endif
747 }
748 
749 void
750 sbc_dma_eop(struct ncr5380_softc *ncr_sc)
751 {
752 	/* Not used; the EOP pin is wired high (GMFH, pp. 389-390) */
753 }
754 
755 void
756 sbc_dma_stop(struct ncr5380_softc *ncr_sc)
757 {
758 	struct sbc_softc *sc = (struct sbc_softc *)ncr_sc;
759 	struct sci_req *sr = ncr_sc->sc_current;
760 	struct sbc_pdma_handle *dh = sr->sr_dma_hand;
761 	int ntrans;
762 
763 	if ((ncr_sc->sc_state & NCR_DOINGDMA) == 0) {
764 #ifdef SBC_DEBUG
765 		if (sbc_debug & SBC_DB_DMA)
766 			printf("%s: dma_stop: DMA not running\n",
767 			    device_xname(ncr_sc->sc_dev));
768 #endif
769 		return;
770 	}
771 	ncr_sc->sc_state &= ~NCR_DOINGDMA;
772 
773 	if ((ncr_sc->sc_state & NCR_ABORTING) == 0) {
774 		ntrans = ncr_sc->sc_datalen - dh->dh_len;
775 
776 #ifdef SBC_DEBUG
777 		if (sbc_debug & SBC_DB_DMA)
778 			printf("%s: dma_stop: ntrans=0x%x\n",
779 			    device_xname(ncr_sc->sc_dev), ntrans);
780 #endif
781 
782 		if (ntrans > ncr_sc->sc_datalen)
783 			panic("sbc_dma_stop: excess transfer");
784 
785 		/* Adjust data pointer */
786 		ncr_sc->sc_dataptr += ntrans;
787 		ncr_sc->sc_datalen -= ntrans;
788 
789 		/* Clear any pending interrupts. */
790 		SCI_CLR_INTR(ncr_sc);
791 		if (sc->sc_clrintr)
792 			(*sc->sc_clrintr)(ncr_sc);
793 	}
794 
795 	/* Put SBIC back into PIO mode. */
796 	*ncr_sc->sci_mode &= ~SCI_MODE_DMA;
797 	*ncr_sc->sci_icmd = 0;
798 
799 #ifdef SBC_DEBUG
800 	if (sbc_debug & SBC_DB_REG)
801 		printf("%s: dma_stop: csr=0x%x, bus_csr=0x%x\n",
802 		    device_xname(ncr_sc->sc_dev), *ncr_sc->sci_csr,
803 		    *ncr_sc->sci_bus_csr);
804 #endif
805 }
806