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