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