Home | History | Annotate | Line # | Download | only in dev
mac68k5380.c revision 1.2
      1 /*	$NetBSD: mac68k5380.c,v 1.2 1995/09/02 03:19:37 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/syslog.h>
     41 #include <sys/buf.h>
     42 #include <scsi/scsi_all.h>
     43 #include <scsi/scsi_message.h>
     44 #include <scsi/scsiconf.h>
     45 
     46 /*
     47  * Include the driver definitions
     48  */
     49 #include <atari/dev/ncr5380reg.h>
     50 
     51 #include <machine/stdarg.h>
     52 
     53 #include "../mac68k/via.h"
     54 
     55 /*
     56  * Set the various driver options
     57  */
     58 #define	NREQ		18	/* Size of issue queue			*/
     59 #define	AUTO_SENSE	1	/* Automatically issue a request-sense 	*/
     60 
     61 #define	DRNAME		ncrscsi	/* used in various prints	*/
     62 #undef	DBG_SEL			/* Show the selection process		*/
     63 #undef	DBG_REQ			/* Show enqueued/ready requests		*/
     64 #undef	DBG_NOWRITE		/* Do not allow writes to the targets	*/
     65 #undef	DBG_PIO			/* Show the polled-I/O process		*/
     66 #undef	DBG_INF			/* Show information transfer process	*/
     67 #define	DBG_NOSTATIC		/* No static functions, all in DDB trace*/
     68 #define	DBG_PID			/* Keep track of driver			*/
     69 #undef 	REAL_DMA		/* Use DMA if sensible			*/
     70 #define fair_to_keep_dma()	1
     71 #define claimed_dma()		1
     72 #define reconsider_dma()
     73 #define	USE_PDMA	1	/* Use special pdma-transfer function	*/
     74 
     75 #define	ENABLE_NCR5380(sc)	cur_softc = sc;
     76 
     77 /*
     78  * softc of currently active controller (well, we only have one for now).
     79  */
     80 
     81 static struct ncr_softc	*cur_softc;
     82 
     83 struct scsi_5380 {
     84 	volatile u_char	scsi_5380[8*16]; /* 8 regs, 1 every 16th byte. */
     85 };
     86 
     87 extern vm_offset_t	SCSIBase;
     88 static volatile u_char	*ncr		= (volatile u_char *) 0x10000;
     89 static volatile u_char	*ncr_5380_with_drq	= (volatile u_char *)  0x6000;
     90 static volatile u_char	*ncr_5380_without_drq	= (volatile u_char *) 0x12000;
     91 
     92 #define SCSI_5380		((struct scsi_5380 *) ncr)
     93 #define GET_5380_REG(rnum)	SCSI_5380->scsi_5380[((rnum)<<4)]
     94 #define SET_5380_REG(rnum,val)	(SCSI_5380->scsi_5380[((rnum)<<4)] = (val))
     95 
     96 static __inline__ void
     97 scsi_clr_ipend()
     98 {
     99 	int	tmp;
    100 
    101 	tmp = GET_5380_REG(NCR5380_IRCV);
    102 }
    103 
    104 extern __inline__ void
    105 scsi_ienable()
    106 {
    107 	int	s;
    108 
    109 	s = splhigh();
    110 	if (VIA2 == VIA2OFF)
    111 		via_reg(VIA2, vIER) = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    112 	else
    113 		via_reg(VIA2, rIER) = 0x80 | (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    114 	splx(s);
    115 }
    116 
    117 extern __inline__ void
    118 scsi_idisable()
    119 {
    120 	int	s;
    121 
    122 	s = splhigh();
    123 	if (VIA2 == VIA2OFF)
    124 		via_reg(VIA2, vIER) = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    125 	else
    126 		via_reg(VIA2, rIER) = (V2IF_SCSIIRQ | V2IF_SCSIDRQ);
    127 	splx(s);
    128 }
    129 
    130 static void
    131 scsi_mach_init(sc)
    132 	struct ncr_softc	*sc;
    133 {
    134 	static int	initted = 0;
    135 
    136 	if (initted++)
    137 		panic("scsi_mach_init called again.\n");
    138 
    139 	ncr		= (volatile u_char *)
    140 			  (SCSIBase + (u_long) ncr);
    141 	ncr_5380_with_drq	= (volatile u_char *)
    142 			  (SCSIBase + (u_int) ncr_5380_with_drq);
    143 	ncr_5380_without_drq	= (volatile u_char *)
    144 			  (SCSIBase + (u_int) ncr_5380_without_drq);
    145 }
    146 
    147 static int
    148 machine_match(pdp, cdp, auxp, cd)
    149 	struct device	*pdp;
    150 	struct cfdata	*cdp;
    151 	void		*auxp;
    152 	struct cfdriver	*cd;
    153 {
    154 	if (matchbyname(pdp, cdp, auxp) == 0)
    155 		return 0;
    156 	if (!mac68k_machine.scsi80)
    157 		return 0;
    158 	if (cdp->cf_unit != 0)
    159 		return 0;
    160 	return 1;
    161 }
    162 
    163 #if USE_PDMA
    164 int		pdma_5380_dir = 0;
    165 void		(*pdma_xfer_fun)(void) = NULL;
    166 
    167 volatile int	pdma_5380_pending = 0;
    168 volatile u_char	*pending_5380_data;
    169 volatile u_long	pending_5380_count;
    170 
    171 #define DEBUG 1 /* 	Maybe we try with this off eventually. */
    172 #if DEBUG
    173 int		pdma_5380_sends = 0;
    174 int		pdma_5380_bytes = 0;
    175 
    176 volatile char	*pdma_5380_state="";
    177 void
    178 pdma_stat()
    179 {
    180 	printf("PDMA SCSI: %d xfers completed for %d bytes, pending = %d.\n",
    181 		pdma_5380_sends, pdma_5380_bytes, pdma_5380_pending);
    182 	printf("xfer fun = 0x%x, pdma_5380_dir = %d.\n",
    183 		pdma_xfer_fun, pdma_5380_dir);
    184 	printf("datap = 0x%x, remainder = %d.\n",
    185 		pending_5380_data, pending_5380_count);
    186 	printf("pdma_5380_state = %s.\n", pdma_5380_state);
    187 }
    188 #endif
    189 #endif
    190 
    191 void
    192 pdma_cleanup(void)
    193 {
    194 	SC_REQ	*reqp = connected;
    195 	int	bytes, s;
    196 
    197 	s = splbio();
    198 
    199 	pdma_5380_pending = 0;
    200 	pdma_xfer_fun = NULL;
    201 
    202 #if DEBUG
    203 	pdma_5380_state = "in pdma_cleanup().";
    204 	pdma_5380_sends++;
    205 	pdma_5380_bytes+=(reqp->xdata_len - pending_5380_count);
    206 #endif
    207 
    208 	/*
    209 	 * Update pointers.
    210 	 */
    211 	reqp->xdata_ptr += reqp->xdata_len - pending_5380_count;
    212 	reqp->xdata_len  = pending_5380_count;
    213 
    214 	/*
    215 	 * Reset DMA mode.
    216 	 */
    217 	SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
    218 
    219 	/*
    220 	 * Tell interrupt functions that DMA has ended.
    221 	 */
    222 	reqp->dr_flag &= ~DRIVER_IN_DMA;
    223 
    224 	SET_5380_REG(NCR5380_MODE, IMODE_BASE);
    225 	SET_5380_REG(NCR5380_ICOM, 0);
    226 
    227 	splx(s);
    228 
    229 	/*
    230 	 * Back for more punishment.
    231 	 */
    232 	run_main(cur_softc);
    233 }
    234 
    235 void
    236 ncr5380_irq_intr(void)
    237 {
    238 	if (pdma_xfer_fun) {
    239 #if DEBUG
    240 		pdma_5380_state = "got irq interrupt in xfer.";
    241 #endif
    242 		/*
    243 		 * If Mr. IRQ isn't set one might wonder how we got
    244 		 * here.  It does happen, though.
    245 		 */
    246 		if (!(GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET)) {
    247 			return;
    248 		}
    249 		/*
    250 		 * For a phase mis-match, ATN is a "don't care," IRQ is 1 and
    251 		 * all other bits in the Bus & Status Register are 0.  Also,
    252 		 * the current SCSI Bus Status Register has a 1 for BSY and
    253 		 * REQ.  Since we're just checking that this interrupt isn't a
    254 		 * reselection or a reset, we just check for either.
    255 		 */
    256 		if (   ((GET_5380_REG(NCR5380_DMSTAT) & (0xff & ~SC_ATN_STAT))
    257 			== SC_IRQ_SET)
    258 		    && (GET_5380_REG(NCR5380_IDSTAT) & (SC_S_BSY|SC_S_REQ))) {
    259 			pdma_cleanup();
    260 			return;
    261 		} else {
    262 			scsi_show();
    263 			panic("Spurious interrupt during PDMA xfer.\n");
    264 		}
    265 	}
    266 	if (GET_5380_REG(NCR5380_DMSTAT) & SC_IRQ_SET) {
    267 		scsi_idisable();
    268 		ncr_ctrl_intr(cur_softc);
    269 	}
    270 }
    271 
    272 void
    273 ncr5380_drq_intr(void)
    274 {
    275 #if USE_PDMA
    276 	if (pdma_xfer_fun) {
    277 #if DEBUG
    278 		pdma_5380_state = "got drq interrupt.";
    279 #endif
    280 		pdma_xfer_fun();
    281 	}
    282 #endif
    283 }
    284 
    285 #if USE_PDMA
    286 static void
    287 pdma_xfer_in()
    288 {
    289 	/*
    290 	 * Can we "unroll" this any?  I don't think so--in fact, I
    291 	 * question the safety of using long word transfers.  The
    292 	 * device could theoretically disconnect at any time.
    293 	 * The long word xfer is controlled by the Mac's circuitry,
    294 	 * and we can't know how much it transferred if the device
    295 	 * decides to disconnect on us.
    296 	 * If it does disconnect in the middle of a long xfer, it
    297 	 * should get a bus error--we might be able to derive from
    298 	 * that bus error where the transaction stopped, but I
    299 	 * don't want to think about that...
    300 	 */
    301 	while (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
    302 		if (pending_5380_count) {
    303 			*((u_char *) pending_5380_data)++ = *ncr_5380_with_drq;
    304 			pending_5380_count --;
    305 		} else {
    306 #if DEBUG
    307 			pdma_5380_state = "done in xfer in.";
    308 #endif
    309 			SET_5380_REG(NCR5380_MODE,
    310 				     GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
    311 			return;
    312 		}
    313 #if DEBUG
    314 	pdma_5380_state = "handled drq interrupt.";
    315 #endif
    316 }
    317 
    318 /*
    319  * Macroed for readability.
    320  */
    321 #define DONE   (   (GET_5380_REG(NCR5380_DMSTAT) & SC_ACK_STAT) \
    322 		|| (GET_5380_REG(NCR5380_IDSTAT) &    SC_S_REQ) )
    323 
    324 static void
    325 pdma_xfer_out()
    326 {
    327 	/*
    328 	 * See comment on pdma_xfer_in(), above.
    329 	 */
    330 	while (GET_5380_REG(NCR5380_DMSTAT) & SC_DMA_REQ)
    331 		if (pending_5380_count) {
    332 			*ncr_5380_with_drq = *((u_char *) pending_5380_data)++;
    333 			pending_5380_count --;
    334 		} else {
    335 #if DEBUG
    336 			pdma_5380_state = "done in xfer out--waiting.";
    337 #endif
    338 			while (!DONE);
    339 #if DEBUG
    340 			pdma_5380_state = "done in xfer out--really done.";
    341 #endif
    342 			pdma_cleanup();
    343 			return;
    344 		}
    345 #if DEBUG
    346 	pdma_5380_state = "handled drq interrupt.";
    347 #endif
    348 }
    349 
    350 #define SCSI_TIMEOUT_VAL	10000000
    351 
    352 static int
    353 transfer_pdma(phasep, data, count)
    354 	u_char	*phasep;
    355 	u_char	*data;
    356 	u_long	*count;
    357 {
    358 	SC_REQ	*reqp = connected;
    359 	int	len = *count, i, scsi_timeout = SCSI_TIMEOUT_VAL;
    360 	int	s, err;
    361 
    362 	if (pdma_5380_pending) {
    363 		panic("ncrscsi: transfer_pdma called when operation already "
    364 			"pending.\n");
    365 	}
    366 #if DEBUG
    367 	pdma_5380_state = "in transfer_pdma.";
    368 #endif
    369 
    370 	scsi_idisable();
    371 
    372 	/*
    373  	 * Don't bother with PDMA for short transfers or if we can't sleep.
    374  	 */
    375 	if ((reqp->dr_flag & DRIVER_NOINT) || (*count < 128)) {
    376 #if DEBUG
    377 		pdma_5380_state = "using transfer_pio.";
    378 #endif
    379 		transfer_pio(phasep, data, count);
    380 		return -1;
    381 	}
    382 
    383 	switch (*phasep) {
    384 	default:
    385 		panic("Unexpected phase in transfer_pdma.\n");
    386 	case PH_DATAOUT:
    387 		pdma_5380_dir = 1;
    388 		break;
    389 	case PH_DATAIN:
    390 		pdma_5380_dir = 2;
    391 		break;
    392 	}
    393 
    394 	/*
    395 	 * Match phases with target.
    396 	 */
    397 	SET_5380_REG(NCR5380_TCOM, *phasep);
    398 
    399 	/*
    400 	 * We are probably already at spl2(), so this is likely a no-op.
    401 	 * Paranoia.
    402 	 */
    403 	s = splbio();
    404 
    405 	/*
    406 	 * Clear pending interrupts.
    407 	 */
    408 	scsi_clr_ipend();
    409 
    410 	/*
    411 	 * Wait until target asserts BSY.
    412 	 */
    413 	while ( ((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
    414 		((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
    415 		((GET_5380_REG(NCR5380_IDSTAT) & SC_S_BSY) == 0) &&
    416 		 (--scsi_timeout) );
    417 	if (!scsi_timeout) {
    418 #if DIAGNOSTIC
    419 		printf("scsi timeout: waiting for BSY in %s.\n",
    420 			(pdma_5380_dir == 1) ? "pdma_out" : "pdma_in");
    421 #endif
    422 		goto scsi_timeout_error;
    423 	}
    424 
    425 	/*
    426 	 * Tell the driver that we're in DMA mode.
    427 	 */
    428 	reqp->dr_flag |= DRIVER_IN_DMA;
    429 
    430 	/*
    431 	 * Set DMA mode and assert data bus.
    432 	 */
    433 	SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) | SC_M_DMA);
    434 	SET_5380_REG(NCR5380_ICOM, GET_5380_REG(NCR5380_ICOM) | SC_ADTB);
    435 
    436 	/*
    437 	 * Load static/volatile values for DRQ interrupt handlers.
    438 	 */
    439 	pending_5380_data = (volatile u_char *) data;
    440 	pending_5380_count = len;
    441 
    442 #if DEBUG
    443 	pdma_5380_state = "wait for interrupt.";
    444 #endif
    445 
    446 	/*
    447 	 * Set the transfer function to be called on DRQ interrupts.
    448 	 * And note that we're waiting.
    449 	 */
    450 	pdma_xfer_fun = (pdma_5380_dir == 1) ? pdma_xfer_out : pdma_xfer_in;
    451 	pdma_5380_pending = 1;
    452 
    453 	/*
    454 	 * Initiate the DMA transaction--sending or receiving.
    455 	 */
    456 	if (pdma_5380_dir == 1) {
    457 		SET_5380_REG(NCR5380_DMSTAT, 0);
    458 	} else {
    459 		SET_5380_REG(NCR5380_IRCV, 0);
    460 	}
    461 
    462 	/*
    463 	 * Now that we're set up, enable interrupts and drop processor
    464 	 * priority back down.
    465 	 */
    466 	scsi_ienable();
    467 	splx(s);
    468 	return 0;
    469 
    470 scsi_timeout_error:
    471 	/*
    472 	 * Clear the DMA mode.
    473 	 */
    474 	SET_5380_REG(NCR5380_MODE, GET_5380_REG(NCR5380_MODE) & ~SC_M_DMA);
    475 	return -1;
    476 }
    477 #endif /* if USE_PDMA */
    478 
    479 /* Include general routines. */
    480 #include <atari/dev/ncr5380.c>
    481