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