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