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