Home | History | Annotate | Line # | Download | only in dev
mac68k5380.c revision 1.10
      1 /*	$NetBSD: mac68k5380.c,v 1.10 1995/09/23 01:11:42 briggs Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1995 Allen Briggs
      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 Allen Briggs
     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  * Derived from atari5380.c for the mac68k port of NetBSD.
     33  *
     34  */
     35 
     36 #include <sys/param.h>
     37 #include <sys/systm.h>
     38 #include <sys/kernel.h>
     39 #include <sys/device.h>
     40 #include <sys/dkstat.h>
     41 #include <sys/syslog.h>
     42 #include <sys/buf.h>
     43 #include <scsi/scsi_all.h>
     44 #include <scsi/scsi_message.h>
     45 #include <scsi/scsiconf.h>
     46 
     47 /*
     48  * Include the driver definitions
     49  */
     50 #include <mac68k/dev/ncr5380reg.h>
     51 
     52 #include <machine/stdarg.h>
     53 
     54 #include "../mac68k/via.h"
     55 
     56 /*
     57  * Set the various driver options
     58  */
     59 #define	NREQ		18	/* Size of issue queue			*/
     60 #define	AUTO_SENSE	1	/* Automatically issue a request-sense 	*/
     61 
     62 #define	DRNAME		ncrscsi	/* used in various prints	*/
     63 #undef	DBG_SEL			/* Show the selection process		*/
     64 #undef	DBG_REQ			/* Show enqueued/ready requests		*/
     65 #undef	DBG_NOWRITE		/* Do not allow writes to the targets	*/
     66 #undef	DBG_PIO			/* Show the polled-I/O process		*/
     67 #undef	DBG_INF			/* Show information transfer process	*/
     68 #define	DBG_NOSTATIC		/* No static functions, all in DDB trace*/
     69 #undef	DBG_PID			/* Keep track of driver			*/
     70 #undef 	REAL_DMA		/* Use DMA if sensible			*/
     71 #define fair_to_keep_dma()	1
     72 #define claimed_dma()		1
     73 #define reconsider_dma()
     74 #define	USE_PDMA	1	/* Use special pdma-transfer function	*/
     75 #define MIN_PHYS	0x2000	/* pdma space w/ /DSACK is only 0x2000  */
     76 
     77 #define	ENABLE_NCR5380(sc)	cur_softc = sc;
     78 
     79 /*
     80  * softc of currently active controller (well, we only have one for now).
     81  */
     82 
     83 static struct ncr_softc	*cur_softc;
     84 
     85 struct scsi_5380 {
     86 	volatile u_char	scsi_5380[8*16]; /* 8 regs, 1 every 16th byte. */
     87 };
     88 
     89 extern vm_offset_t	SCSIBase;
     90 static volatile u_char	*ncr		= (volatile u_char *) 0x10000;
     91 static volatile u_char	*ncr_5380_with_drq	= (volatile u_char *)  0x6000;
     92 static volatile u_char	*ncr_5380_without_drq	= (volatile u_char *) 0x12000;
     93 
     94 static volatile u_char	*scsi_enable		= NULL;
     95 
     96 #define SCSI_5380		((struct scsi_5380 *) ncr)
     97 #define GET_5380_REG(rnum)	SCSI_5380->scsi_5380[((rnum)<<4)]
     98 #define SET_5380_REG(rnum,val)	(SCSI_5380->scsi_5380[((rnum)<<4)] = (val))
     99 
    100 void	ncr5380_irq_intr(void *);
    101 void	ncr5380_drq_intr(void *);
    102 
    103 static __inline__ void
    104 scsi_clr_ipend()
    105 {
    106 	int	tmp;
    107 
    108 	tmp = GET_5380_REG(NCR5380_IRCV);
    109 }
    110 
    111 extern __inline__ void
    112 scsi_ienable()
    113 {
    114 	int	s;
    115 
    116 	s = splhigh();
    117 	*scsi_enable = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    118 	splx(s);
    119 }
    120 
    121 extern __inline__ void
    122 scsi_idisable()
    123 {
    124 	int	s;
    125 
    126 	s = splhigh();
    127 	*scsi_enable = V2IF_SCSIIRQ | V2IF_SCSIDRQ;
    128 	splx(s);
    129 }
    130 
    131 static void
    132 scsi_mach_init(sc)
    133 	struct ncr_softc	*sc;
    134 {
    135 	static int	initted = 0;
    136 
    137 	if (initted++)
    138 		panic("scsi_mach_init called again.\n");
    139 
    140 	ncr		= (volatile u_char *)
    141 			  (SCSIBase + (u_long) ncr);
    142 	ncr_5380_with_drq	= (volatile u_char *)
    143 			  (SCSIBase + (u_int) ncr_5380_with_drq);
    144 	ncr_5380_without_drq	= (volatile u_char *)
    145 			  (SCSIBase + (u_int) ncr_5380_without_drq);
    146 
    147 	if (VIA2 == VIA2OFF)
    148 		scsi_enable = Via1Base + VIA2 * 0x2000 + vIER;
    149 	else
    150 		scsi_enable = Via1Base + VIA2 * 0x2000 + rIER;
    151 
    152 	mac68k_register_scsi_irq(ncr5380_irq_intr, sc);
    153 	mac68k_register_scsi_drq(ncr5380_drq_intr, sc);
    154 }
    155 
    156 static int
    157 machine_match(pdp, cdp, auxp, cd)
    158 	struct device	*pdp;
    159 	struct cfdata	*cdp;
    160 	void		*auxp;
    161 	struct cfdriver	*cd;
    162 {
    163 	if (matchbyname(pdp, cdp, auxp) == 0)
    164 		return 0;
    165 	if (!mac68k_machine.scsi80)
    166 		return 0;
    167 	if (cdp->cf_unit != 0)
    168 		return 0;
    169 	return 1;
    170 }
    171 
    172 #if USE_PDMA
    173 int	pdma_5380_dir = 0;
    174 
    175 u_char	*pending_5380_data;
    176 u_long	pending_5380_count;
    177 
    178 /* #define DEBUG 1 	Maybe we try with this off eventually. */
    179 
    180 #if DEBUG
    181 int		pdma_5380_sends = 0;
    182 int		pdma_5380_bytes = 0;
    183 
    184 char	*pdma_5380_state="";
    185 
    186 void
    187 pdma_stat()
    188 {
    189 	printf("PDMA SCSI: %d xfers completed for %d bytes.\n",
    190 		pdma_5380_sends, pdma_5380_bytes);
    191 	printf("pdma_5380_dir = %d.\n",
    192 		pdma_5380_dir);
    193 	printf("datap = 0x%x, remainder = %d.\n",
    194 		pending_5380_data, pending_5380_count);
    195 	printf("pdma_5380_state = %s.\n", pdma_5380_state);
    196 }
    197 #endif
    198 #endif
    199 
    200 void
    201 pdma_cleanup(void)
    202 {
    203 	SC_REQ	*reqp = connected;
    204 	int	bytes, s;
    205 
    206 	s = splbio();
    207 
    208 	pdma_5380_dir = 0;
    209 
    210 #if DEBUG
    211 	pdma_5380_state = "in pdma_cleanup().";
    212 	pdma_5380_sends++;
    213 	pdma_5380_bytes+=(reqp->xdata_len - pending_5380_count);
    214 #endif
    215 
    216 	/*
    217 	 * Update pointers.
    218 	 */
    219 	reqp->xdata_ptr += reqp->xdata_len - pending_5380_count;
    220 	reqp->xdata_len  = pending_5380_count;
    221 
    222 	/*
    223 	 * Reset DMA mode.
    224 	 */
    225 	SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
    226 
    227 	/*
    228 	 * Clear any pending interrupts.
    229 	 */
    230 	scsi_clr_ipend();
    231 
    232 	/*
    233 	 * Tell interrupt functions that DMA has ended.
    234 	 */
    235 	reqp->dr_flag &= ~DRIVER_IN_DMA;
    236 
    237 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    238 	SET_5380_REG(NCR5380_ICOM, 0);
    239 
    240 	splx(s);
    241 
    242 	/*
    243 	 * Back for more punishment.
    244 	 */
    245 	run_main(cur_softc);
    246 }
    247 
    248 static __inline__ int
    249 pdma_ready()
    250 {
    251 	if (pdma_5380_dir) {
    252 #if DEBUG
    253 		pdma_5380_state = "got irq interrupt in xfer.";
    254 #endif
    255 		/*
    256 		 * If Mr. IRQ isn't set one might wonder how we got
    257 		 * here.  It does happen, though.
    258 		 */
    259 		if (!(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)) {
    260 			return 0;
    261 		}
    262 		/*
    263 		 * For a phase mis-match, ATN is a "don't care," IRQ is 1 and
    264 		 * all other bits in the Bus & Status Register are 0.  Also,
    265 		 * the current SCSI Bus Status Register has a 1 for BSY and
    266 		 * REQ.  Since we're just checking that this interrupt isn't a
    267 		 * reselection or a reset, we just check for either.
    268 		 */
    269 		if (   ((GET_5380_REG(NCR5380_DMSTAT) & (0xff & ~SC_ATN_STAT))
    270 			== SC_IRQ_SET)
    271 		    && (GET_5380_REG(NCR5380_IDSTAT) & (SC_S_BSY|SC_S_REQ))) {
    272 			pdma_cleanup();
    273 			return 1;
    274 		} else {
    275 			scsi_show();
    276 			panic("Spurious interrupt during PDMA xfer.\n");
    277 		}
    278 	}
    279 	return 0;
    280 }
    281 
    282 void
    283 ncr5380_irq_intr(p)
    284 	void	*p;
    285 {
    286 	struct ncr_softc	*sc = p;
    287 
    288 	if (pdma_ready()) {
    289 		return;
    290 	}
    291 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
    292 		scsi_idisable();
    293 		ncr_ctrl_intr(cur_softc);
    294 	}
    295 }
    296 
    297 /*
    298  * This is the meat of the PDMA transfer.
    299  * When we get here, we shove data as fast as the mac can take it.
    300  * We depend on several things:
    301  *   * All macs after the Mac Plus that have a 5380 chip should have a general
    302  *     logic IC that handshakes data for blind transfers.
    303  *   * If the SCSI controller finishes sending/receiving data before we do,
    304  *     the same general logic IC will generate a /BERR for us in short order.
    305  *   * The fault address for said /BERR minus the base address for the
    306  *     transfer will be the amount of data that was actually written.
    307  *
    308  * We use the nofault flag and the setjmp/longjmp in locore.s so we can
    309  * detect and handle the bus error for early termination of a command.
    310  * This is usually caused by a disconnecting target.
    311  */
    312 void
    313 ncr5380_drq_intr(p)
    314 	void	*p;
    315 {
    316 #if USE_PDMA
    317 extern	int			*nofault, mac68k_buserr_addr;
    318 	struct ncr_softc	*sc = p;
    319 	label_t			faultbuf;
    320 	register int		count;
    321 	volatile u_int32_t	*long_drq;
    322 	u_int32_t		*long_data;
    323 	volatile u_int8_t	*drq;
    324 	u_int8_t		*data;
    325 
    326 	/*
    327 	 * If we're not ready to xfer data, just return.
    328 	 */
    329 	if (   !(GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
    330 	    || !pdma_5380_dir)
    331 		return;
    332 
    333 #if DEBUG
    334 	pdma_5380_state = "got drq interrupt.";
    335 #endif
    336 
    337 	/*
    338 	 * Setup for a possible bus error caused by SCSI controller
    339 	 * switching out of DATA-IN/OUT before we're done with the
    340 	 * current transfer.
    341 	 */
    342 	nofault = (int *) &faultbuf;
    343 
    344 	if (setjmp((label_t *) nofault)) {
    345 		nofault = (int *) 0;
    346 #if DEBUG
    347 		pdma_5380_state = "buserr in xfer.";
    348 #endif
    349 		count = (  (u_long) mac68k_buserr_addr
    350 			 - (u_long) ncr_5380_with_drq);
    351 		if ((count < 0) || (count > pending_5380_count)) {
    352 			printf("pdma in: count = %d (0x%x) (pending "
    353 				"count %d)\n", count, count,
    354 				pending_5380_count);
    355 			panic("something is wrong");
    356 		}
    357 
    358 		pending_5380_data += count;
    359 		pending_5380_count -= count;
    360 
    361 #if DEBUG
    362 		pdma_5380_state = "handled bus error in xfer.";
    363 #endif
    364 		mac68k_buserr_addr = 0;
    365 		return;
    366 	}
    367 
    368 	if (pdma_5380_dir == 2) { /* Data In */
    369 		int	resid;
    370 
    371 		/*
    372 		 * Get the dest address aligned.
    373 		 */
    374 		resid = count = 4 - (((int) pending_5380_data) & 0x3);
    375 		if (count < 4) {
    376 			data = (u_int8_t *) pending_5380_data;
    377 			drq = (u_int8_t *) ncr_5380_with_drq;
    378 			while (count) {
    379 #define R1	*data++ = *drq++
    380 				R1; count--;
    381 #undef R1
    382 			}
    383 			pending_5380_data += resid;
    384 			pending_5380_count -= resid;
    385 		}
    386 
    387 		/*
    388 		 * Get ready to start the transfer.
    389 		 */
    390 		count = pending_5380_count;
    391 		long_drq = (volatile u_int32_t *) ncr_5380_with_drq;
    392 		long_data = (u_int32_t *) pending_5380_data;
    393 
    394 #define R4	*long_data++ = *long_drq++
    395 		while ( count >= 512 ) {
    396 			if (!(GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)) {
    397 				nofault = (int *) 0;
    398 
    399 				pending_5380_data += (pending_5380_count-count);
    400 				pending_5380_count = count;
    401 #if DEBUG
    402 				pdma_5380_state = "drq low";
    403 #endif
    404 				return;
    405 			}
    406 			R4; R4; R4; R4; R4; R4; R4; R4;
    407 			R4; R4; R4; R4; R4; R4; R4; R4;	/* 64 */
    408 			R4; R4; R4; R4; R4; R4; R4; R4;
    409 			R4; R4; R4; R4; R4; R4; R4; R4;	/* 128 */
    410 			R4; R4; R4; R4; R4; R4; R4; R4;
    411 			R4; R4; R4; R4; R4; R4; R4; R4;
    412 			R4; R4; R4; R4; R4; R4; R4; R4;
    413 			R4; R4; R4; R4; R4; R4; R4; R4;	/* 256 */
    414 			R4; R4; R4; R4; R4; R4; R4; R4;
    415 			R4; R4; R4; R4; R4; R4; R4; R4;
    416 			R4; R4; R4; R4; R4; R4; R4; R4;
    417 			R4; R4; R4; R4; R4; R4; R4; R4;
    418 			R4; R4; R4; R4; R4; R4; R4; R4;
    419 			R4; R4; R4; R4; R4; R4; R4; R4;
    420 			R4; R4; R4; R4; R4; R4; R4; R4;
    421 			R4; R4; R4; R4; R4; R4; R4; R4;	/* 512 */
    422 			count -= 512;
    423 		}
    424 		while (count >= 4) {
    425 			R4; count -= 4;
    426 		}
    427 #undef R4
    428 		data = (u_int8_t *) long_data;
    429 		drq = (u_int8_t *) long_drq;
    430 		while (count) {
    431 #define R1	*data++ = *drq++
    432 			R1; count--;
    433 #undef R1
    434 		}
    435 	} else {
    436 		int	resid;
    437 
    438 		/*
    439 		 * Get the source address aligned.
    440 		 */
    441 		resid = count = 4 - (((int) pending_5380_data) & 0x3);
    442 		if (count < 4) {
    443 			data = (u_int8_t *) pending_5380_data;
    444 			drq = (u_int8_t *) ncr_5380_with_drq;
    445 			while (count) {
    446 #define W1	*drq++ = *data++
    447 				W1; count--;
    448 #undef W1
    449 			}
    450 			pending_5380_data += resid;
    451 			pending_5380_count -= resid;
    452 		}
    453 
    454 		/*
    455 		 * Get ready to start the transfer.
    456 		 */
    457 		count = pending_5380_count;
    458 		long_drq = (volatile u_int32_t *) ncr_5380_with_drq;
    459 		long_data = (u_int32_t *) pending_5380_data;
    460 
    461 #define W4	*long_drq++ = *long_data++
    462 		while ( count >= 64 ) {
    463 			W4; W4; W4; W4; W4; W4; W4; W4;
    464 			W4; W4; W4; W4; W4; W4; W4; W4;
    465 			count -= 64;
    466 		}
    467 		while (count >= 4) {
    468 			W4; count -= 4;
    469 		}
    470 #undef W4
    471 		data = (u_int8_t *) long_data;
    472 		drq = (u_int8_t *) long_drq;
    473 		while (count) {
    474 #define W1	*drq++ = *data++
    475 			W1; count--;
    476 		}
    477 #undef W1
    478 	}
    479 
    480 	/*
    481 	 * OK.  No bus error occurred above.  Clear the nofault flag
    482 	 * so we no longer short-circuit bus errors.
    483 	 */
    484 	nofault = (int *) 0;
    485 
    486 #if DEBUG
    487 	pdma_5380_state = "done in xfer--waiting.";
    488 #endif
    489 
    490 	/*
    491 	 * Is this necessary?
    492 	 */
    493 	while (!(   (GET_5380_REG(NCR5380_DMSTAT) & SC_ACK_STAT)
    494 		 || (GET_5380_REG(NCR5380_IDSTAT) &    SC_S_REQ) ));
    495 
    496 	/*
    497 	 * Update pointers for pdma_cleanup().
    498 	 */
    499 	pending_5380_data += pending_5380_count;
    500 	pending_5380_count = 0;
    501 
    502 #if DEBUG
    503 	pdma_5380_state = "done in xfer.";
    504 #endif
    505 
    506 	pdma_cleanup();
    507 	return;
    508 #endif	/* if USE_PDMA */
    509 }
    510 
    511 #if USE_PDMA
    512 
    513 #define SCSI_TIMEOUT_VAL	10000000
    514 
    515 static int
    516 transfer_pdma(phasep, data, count)
    517 	u_char	*phasep;
    518 	u_char	*data;
    519 	u_long	*count;
    520 {
    521 	SC_REQ	*reqp = connected;
    522 	int	len = *count, i, scsi_timeout = SCSI_TIMEOUT_VAL;
    523 	int	s, err;
    524 
    525 	if (pdma_5380_dir) {
    526 		panic("ncrscsi: transfer_pdma called when operation already "
    527 			"pending.\n");
    528 	}
    529 #if DEBUG
    530 	pdma_5380_state = "in transfer_pdma.";
    531 #endif
    532 
    533 	/*
    534  	 * Don't bother with PDMA if we can't sleep or for small transfers.
    535  	 */
    536 	if (reqp->dr_flag & DRIVER_NOINT) {
    537 #if DEBUG
    538 		pdma_5380_state = "pdma, actually using transfer_pio.";
    539 #endif
    540 		transfer_pio(phasep, data, count, 0);
    541 		return -1;
    542 	}
    543 
    544 	/*
    545 	 * We are probably already at spl2(), so this is likely a no-op.
    546 	 * Paranoia.
    547 	 */
    548 	s = splbio();
    549 
    550 	scsi_idisable();
    551 
    552 	/*
    553 	 * Match phases with target.
    554 	 */
    555 	SET_5380_REG(NCR5380_TCOM, *phasep);
    556 
    557 	/*
    558 	 * Clear pending interrupts.
    559 	 */
    560 	scsi_clr_ipend();
    561 
    562 	/*
    563 	 * Wait until target asserts BSY.
    564 	 */
    565 	while (    ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0)
    566 		&& (--scsi_timeout) );
    567 	if (!scsi_timeout) {
    568 #if DIAGNOSTIC
    569 		printf("scsi timeout: waiting for BSY in %s.\n",
    570 			(*phasep == PH_DATAOUT) ? "pdma_out" : "pdma_in");
    571 #endif
    572 		goto scsi_timeout_error;
    573 	}
    574 
    575 	/*
    576 	 * Tell the driver that we're in DMA mode.
    577 	 */
    578 	reqp->dr_flag |= DRIVER_IN_DMA;
    579 
    580 	/*
    581 	 * Set DMA mode and assert data bus.
    582 	 */
    583 	SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) | SC_M_DMA);
    584 	SET_5380_REG(NCR5380_ICOM, GET_5380_REG(NCR5380_ICOM) | SC_ADTB);
    585 
    586 	/*
    587 	 * Load transfer values for DRQ interrupt handlers.
    588 	 */
    589 	pending_5380_data = data;
    590 	pending_5380_count = len;
    591 
    592 #if DEBUG
    593 	pdma_5380_state = "wait for interrupt.";
    594 #endif
    595 
    596 	/*
    597 	 * Set the transfer function to be called on DRQ interrupts.
    598 	 * And note that we're waiting.
    599 	 */
    600 	switch (*phasep) {
    601 	default:
    602 		panic("Unexpected phase in transfer_pdma.\n");
    603 	case PH_DATAOUT:
    604 		pdma_5380_dir = 1;
    605 		SET_5380_REG(NCR5380_DMSTAT, 0);
    606 		break;
    607 	case PH_DATAIN:
    608 		pdma_5380_dir = 2;
    609 		SET_5380_REG(NCR5380_IRCV, 0);
    610 		break;
    611 	}
    612 
    613 	/*
    614 	 * Now that we're set up, enable interrupts and drop processor
    615 	 * priority back down.
    616 	 */
    617 	scsi_ienable();
    618 	splx(s);
    619 	return 0;
    620 
    621 scsi_timeout_error:
    622 	/*
    623 	 * Clear the DMA mode.
    624 	 */
    625 	SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
    626 	return -1;
    627 }
    628 #endif /* if USE_PDMA */
    629 
    630 /* Include general routines. */
    631 #include <mac68k/dev/ncr5380.c>
    632