xref: /openbmc/linux/drivers/scsi/sun3_scsi_vme.c (revision 1da177e4)
11da177e4SLinus Torvalds  /*
21da177e4SLinus Torvalds  * Sun3 SCSI stuff by Erik Verbruggen (erik@bigmama.xtdnet.nl)
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  * Sun3 DMA routines added by Sam Creasey (sammy@sammy.net)
51da177e4SLinus Torvalds  *
61da177e4SLinus Torvalds  * VME support added by Sam Creasey
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  * Adapted from sun3_scsi.c -- see there for other headers
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  * TODO: modify this driver to support multiple Sun3 SCSI VME boards
111da177e4SLinus Torvalds  *
121da177e4SLinus Torvalds  */
131da177e4SLinus Torvalds 
141da177e4SLinus Torvalds #define AUTOSENSE
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/types.h>
171da177e4SLinus Torvalds #include <linux/stddef.h>
181da177e4SLinus Torvalds #include <linux/ctype.h>
191da177e4SLinus Torvalds #include <linux/delay.h>
201da177e4SLinus Torvalds 
211da177e4SLinus Torvalds #include <linux/module.h>
221da177e4SLinus Torvalds #include <linux/signal.h>
231da177e4SLinus Torvalds #include <linux/sched.h>
241da177e4SLinus Torvalds #include <linux/ioport.h>
251da177e4SLinus Torvalds #include <linux/init.h>
261da177e4SLinus Torvalds #include <linux/blkdev.h>
271da177e4SLinus Torvalds 
281da177e4SLinus Torvalds #include <asm/io.h>
291da177e4SLinus Torvalds #include <asm/system.h>
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <asm/sun3ints.h>
321da177e4SLinus Torvalds #include <asm/dvma.h>
331da177e4SLinus Torvalds #include <asm/idprom.h>
341da177e4SLinus Torvalds #include <asm/machines.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define SUN3_SCSI_VME
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #undef SUN3_SCSI_DEBUG
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /* dma on! */
411da177e4SLinus Torvalds #define REAL_DMA
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds #include "scsi.h"
441da177e4SLinus Torvalds #include <scsi/scsi_host.h>
451da177e4SLinus Torvalds #include "sun3_scsi.h"
461da177e4SLinus Torvalds #include "NCR5380.h"
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds extern int sun3_map_test(unsigned long, char *);
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define USE_WRAPPER
511da177e4SLinus Torvalds /*#define RESET_BOOT */
521da177e4SLinus Torvalds #define DRIVER_SETUP
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define NDEBUG 0
551da177e4SLinus Torvalds 
561da177e4SLinus Torvalds /*
571da177e4SLinus Torvalds  * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
581da177e4SLinus Torvalds  */
591da177e4SLinus Torvalds #ifdef BUG
601da177e4SLinus Torvalds #undef RESET_BOOT
611da177e4SLinus Torvalds #undef DRIVER_SETUP
621da177e4SLinus Torvalds #endif
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds /* #define SUPPORT_TAGS */
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds //#define	ENABLE_IRQ()	enable_irq( SUN3_VEC_VMESCSI0 );
671da177e4SLinus Torvalds #define ENABLE_IRQ()
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds static irqreturn_t scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp);
711da177e4SLinus Torvalds static inline unsigned char sun3scsi_read(int reg);
721da177e4SLinus Torvalds static inline void sun3scsi_write(int reg, int value);
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static int setup_can_queue = -1;
751da177e4SLinus Torvalds module_param(setup_can_queue, int, 0);
761da177e4SLinus Torvalds static int setup_cmd_per_lun = -1;
771da177e4SLinus Torvalds module_param(setup_cmd_per_lun, int, 0);
781da177e4SLinus Torvalds static int setup_sg_tablesize = -1;
791da177e4SLinus Torvalds module_param(setup_sg_tablesize, int, 0);
801da177e4SLinus Torvalds #ifdef SUPPORT_TAGS
811da177e4SLinus Torvalds static int setup_use_tagged_queuing = -1;
821da177e4SLinus Torvalds module_param(setup_use_tagged_queuing, int, 0);
831da177e4SLinus Torvalds #endif
841da177e4SLinus Torvalds static int setup_hostid = -1;
851da177e4SLinus Torvalds module_param(setup_hostid, int, 0);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static Scsi_Cmnd *sun3_dma_setup_done = NULL;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #define	AFTER_RESET_DELAY	(HZ/2)
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /* ms to wait after hitting dma regs */
921da177e4SLinus Torvalds #define SUN3_DMA_DELAY 10
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
951da177e4SLinus Torvalds #define SUN3_DVMA_BUFSIZE 0xe000
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds /* minimum number of bytes to do dma on */
981da177e4SLinus Torvalds #define SUN3_DMA_MINSIZE 128
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static volatile unsigned char *sun3_scsi_regp;
1011da177e4SLinus Torvalds static volatile struct sun3_dma_regs *dregs;
1021da177e4SLinus Torvalds #ifdef OLDDMA
1031da177e4SLinus Torvalds static unsigned char *dmabuf = NULL; /* dma memory buffer */
1041da177e4SLinus Torvalds #endif
1051da177e4SLinus Torvalds static unsigned char *sun3_dma_orig_addr = NULL;
1061da177e4SLinus Torvalds static unsigned long sun3_dma_orig_count = 0;
1071da177e4SLinus Torvalds static int sun3_dma_active = 0;
1081da177e4SLinus Torvalds static unsigned long last_residual = 0;
1091da177e4SLinus Torvalds 
1101da177e4SLinus Torvalds /*
1111da177e4SLinus Torvalds  * NCR 5380 register access functions
1121da177e4SLinus Torvalds  */
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds static inline unsigned char sun3scsi_read(int reg)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds 	return( sun3_scsi_regp[reg] );
1171da177e4SLinus Torvalds }
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds static inline void sun3scsi_write(int reg, int value)
1201da177e4SLinus Torvalds {
1211da177e4SLinus Torvalds 	sun3_scsi_regp[reg] = value;
1221da177e4SLinus Torvalds }
1231da177e4SLinus Torvalds 
1241da177e4SLinus Torvalds /*
1251da177e4SLinus Torvalds  * XXX: status debug
1261da177e4SLinus Torvalds  */
1271da177e4SLinus Torvalds static struct Scsi_Host *default_instance;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds /*
1301da177e4SLinus Torvalds  * Function : int sun3scsi_detect(Scsi_Host_Template * tpnt)
1311da177e4SLinus Torvalds  *
1321da177e4SLinus Torvalds  * Purpose : initializes mac NCR5380 driver based on the
1331da177e4SLinus Torvalds  *	command line / compile time port and irq definitions.
1341da177e4SLinus Torvalds  *
1351da177e4SLinus Torvalds  * Inputs : tpnt - template for this SCSI adapter.
1361da177e4SLinus Torvalds  *
1371da177e4SLinus Torvalds  * Returns : 1 if a host adapter was found, 0 if not.
1381da177e4SLinus Torvalds  *
1391da177e4SLinus Torvalds  */
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds static int sun3scsi_detect(Scsi_Host_Template * tpnt)
1421da177e4SLinus Torvalds {
1431da177e4SLinus Torvalds 	unsigned long ioaddr, irq = 0;
1441da177e4SLinus Torvalds 	static int called = 0;
1451da177e4SLinus Torvalds 	struct Scsi_Host *instance;
1461da177e4SLinus Torvalds 	int i;
1471da177e4SLinus Torvalds 	unsigned long addrs[3] = { IOBASE_SUN3_VMESCSI,
1481da177e4SLinus Torvalds 				   IOBASE_SUN3_VMESCSI + 0x4000,
1491da177e4SLinus Torvalds 				   0 };
1501da177e4SLinus Torvalds 	unsigned long vecs[3] = { SUN3_VEC_VMESCSI0,
1511da177e4SLinus Torvalds 				  SUN3_VEC_VMESCSI1,
1521da177e4SLinus Torvalds 				  0 };
1531da177e4SLinus Torvalds 	/* check that this machine has an onboard 5380 */
1541da177e4SLinus Torvalds 	switch(idprom->id_machtype) {
1551da177e4SLinus Torvalds 	case SM_SUN3|SM_3_160:
1561da177e4SLinus Torvalds 	case SM_SUN3|SM_3_260:
1571da177e4SLinus Torvalds 		break;
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 	default:
1601da177e4SLinus Torvalds 		return 0;
1611da177e4SLinus Torvalds 	}
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds 	if(called)
1641da177e4SLinus Torvalds 		return 0;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	tpnt->proc_name = "Sun3 5380 VME SCSI";
1671da177e4SLinus Torvalds 
1681da177e4SLinus Torvalds 	/* setup variables */
1691da177e4SLinus Torvalds 	tpnt->can_queue =
1701da177e4SLinus Torvalds 		(setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
1711da177e4SLinus Torvalds 	tpnt->cmd_per_lun =
1721da177e4SLinus Torvalds 		(setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
1731da177e4SLinus Torvalds 	tpnt->sg_tablesize =
1741da177e4SLinus Torvalds 		(setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	if (setup_hostid >= 0)
1771da177e4SLinus Torvalds 		tpnt->this_id = setup_hostid;
1781da177e4SLinus Torvalds 	else {
1791da177e4SLinus Torvalds 		/* use 7 as default */
1801da177e4SLinus Torvalds 		tpnt->this_id = 7;
1811da177e4SLinus Torvalds 	}
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	ioaddr = 0;
1841da177e4SLinus Torvalds 	for(i = 0; addrs[i] != 0; i++) {
1851da177e4SLinus Torvalds 		unsigned char x;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 		ioaddr = (unsigned long)sun3_ioremap(addrs[i], PAGE_SIZE,
1881da177e4SLinus Torvalds 						     SUN3_PAGE_TYPE_VME16);
1891da177e4SLinus Torvalds 		irq = vecs[i];
1901da177e4SLinus Torvalds 		sun3_scsi_regp = (unsigned char *)ioaddr;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 		dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 		if(sun3_map_test((unsigned long)dregs, &x)) {
1951da177e4SLinus Torvalds 			unsigned short oldcsr;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 			oldcsr = dregs->csr;
1981da177e4SLinus Torvalds 			dregs->csr = 0;
1991da177e4SLinus Torvalds 			udelay(SUN3_DMA_DELAY);
2001da177e4SLinus Torvalds 			if(dregs->csr == 0x1400)
2011da177e4SLinus Torvalds 				break;
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds 			dregs->csr = oldcsr;
2041da177e4SLinus Torvalds 		}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 		iounmap((void *)ioaddr);
2071da177e4SLinus Torvalds 		ioaddr = 0;
2081da177e4SLinus Torvalds 	}
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	if(!ioaddr)
2111da177e4SLinus Torvalds 		return 0;
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds #ifdef SUPPORT_TAGS
2141da177e4SLinus Torvalds 	if (setup_use_tagged_queuing < 0)
2151da177e4SLinus Torvalds 		setup_use_tagged_queuing = USE_TAGGED_QUEUING;
2161da177e4SLinus Torvalds #endif
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds 	instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
2191da177e4SLinus Torvalds 	if(instance == NULL)
2201da177e4SLinus Torvalds 		return 0;
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	default_instance = instance;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds         instance->io_port = (unsigned long) ioaddr;
2251da177e4SLinus Torvalds 	instance->irq = irq;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	NCR5380_init(instance, 0);
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 	instance->n_io_port = 32;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds         ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	if (request_irq(instance->irq, scsi_sun3_intr,
2341da177e4SLinus Torvalds 			     0, "Sun3SCSI-5380VME", NULL)) {
2351da177e4SLinus Torvalds #ifndef REAL_DMA
2361da177e4SLinus Torvalds 		printk("scsi%d: IRQ%d not free, interrupts disabled\n",
2371da177e4SLinus Torvalds 		       instance->host_no, instance->irq);
2381da177e4SLinus Torvalds 		instance->irq = SCSI_IRQ_NONE;
2391da177e4SLinus Torvalds #else
2401da177e4SLinus Torvalds 		printk("scsi%d: IRQ%d not free, bailing out\n",
2411da177e4SLinus Torvalds 		       instance->host_no, instance->irq);
2421da177e4SLinus Torvalds 		return 0;
2431da177e4SLinus Torvalds #endif
2441da177e4SLinus Torvalds 	}
2451da177e4SLinus Torvalds 
2461da177e4SLinus Torvalds 	printk("scsi%d: Sun3 5380 VME at port %lX irq", instance->host_no, instance->io_port);
2471da177e4SLinus Torvalds 	if (instance->irq == SCSI_IRQ_NONE)
2481da177e4SLinus Torvalds 		printk ("s disabled");
2491da177e4SLinus Torvalds 	else
2501da177e4SLinus Torvalds 		printk (" %d", instance->irq);
2511da177e4SLinus Torvalds 	printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
2521da177e4SLinus Torvalds 	       instance->can_queue, instance->cmd_per_lun,
2531da177e4SLinus Torvalds 	       SUN3SCSI_PUBLIC_RELEASE);
2541da177e4SLinus Torvalds 	printk("\nscsi%d:", instance->host_no);
2551da177e4SLinus Torvalds 	NCR5380_print_options(instance);
2561da177e4SLinus Torvalds 	printk("\n");
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 	dregs->csr = 0;
2591da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
2601da177e4SLinus Torvalds 	dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
2611da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
2621da177e4SLinus Torvalds 	dregs->fifo_count = 0;
2631da177e4SLinus Torvalds 	dregs->fifo_count_hi = 0;
2641da177e4SLinus Torvalds 	dregs->dma_addr_hi = 0;
2651da177e4SLinus Torvalds 	dregs->dma_addr_lo = 0;
2661da177e4SLinus Torvalds 	dregs->dma_count_hi = 0;
2671da177e4SLinus Torvalds 	dregs->dma_count_lo = 0;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	dregs->ivect = VME_DATA24 | (instance->irq & 0xff);
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds 	called = 1;
2721da177e4SLinus Torvalds 
2731da177e4SLinus Torvalds #ifdef RESET_BOOT
2741da177e4SLinus Torvalds 	sun3_scsi_reset_boot(instance);
2751da177e4SLinus Torvalds #endif
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 	return 1;
2781da177e4SLinus Torvalds }
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds int sun3scsi_release (struct Scsi_Host *shpnt)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	if (shpnt->irq != SCSI_IRQ_NONE)
2831da177e4SLinus Torvalds 		free_irq (shpnt->irq, NULL);
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	iounmap((void *)sun3_scsi_regp);
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	return 0;
2881da177e4SLinus Torvalds }
2891da177e4SLinus Torvalds 
2901da177e4SLinus Torvalds #ifdef RESET_BOOT
2911da177e4SLinus Torvalds /*
2921da177e4SLinus Torvalds  * Our 'bus reset on boot' function
2931da177e4SLinus Torvalds  */
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	unsigned long end;
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds 	NCR5380_local_declare();
3001da177e4SLinus Torvalds 	NCR5380_setup(instance);
3011da177e4SLinus Torvalds 
3021da177e4SLinus Torvalds 	/*
3031da177e4SLinus Torvalds 	 * Do a SCSI reset to clean up the bus during initialization. No
3041da177e4SLinus Torvalds 	 * messing with the queues, interrupts, or locks necessary here.
3051da177e4SLinus Torvalds 	 */
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	printk( "Sun3 SCSI: resetting the SCSI bus..." );
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	/* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
3101da177e4SLinus Torvalds //       	sun3_disable_irq( IRQ_SUN3_SCSI );
3111da177e4SLinus Torvalds 
3121da177e4SLinus Torvalds 	/* get in phase */
3131da177e4SLinus Torvalds 	NCR5380_write( TARGET_COMMAND_REG,
3141da177e4SLinus Torvalds 		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
3151da177e4SLinus Torvalds 
3161da177e4SLinus Torvalds 	/* assert RST */
3171da177e4SLinus Torvalds 	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	/* The min. reset hold time is 25us, so 40us should be enough */
3201da177e4SLinus Torvalds 	udelay( 50 );
3211da177e4SLinus Torvalds 
3221da177e4SLinus Torvalds 	/* reset RST and interrupt */
3231da177e4SLinus Torvalds 	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
3241da177e4SLinus Torvalds 	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
3251da177e4SLinus Torvalds 
3261da177e4SLinus Torvalds 	for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
3271da177e4SLinus Torvalds 		barrier();
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	/* switch on SCSI IRQ again */
3301da177e4SLinus Torvalds //       	sun3_enable_irq( IRQ_SUN3_SCSI );
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	printk( " done\n" );
3331da177e4SLinus Torvalds }
3341da177e4SLinus Torvalds #endif
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds static const char * sun3scsi_info (struct Scsi_Host *spnt) {
3371da177e4SLinus Torvalds     return "";
3381da177e4SLinus Torvalds }
3391da177e4SLinus Torvalds 
3401da177e4SLinus Torvalds // safe bits for the CSR
3411da177e4SLinus Torvalds #define CSR_GOOD 0x060f
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds static irqreturn_t scsi_sun3_intr(int irq, void *dummy, struct pt_regs *fp)
3441da177e4SLinus Torvalds {
3451da177e4SLinus Torvalds 	unsigned short csr = dregs->csr;
3461da177e4SLinus Torvalds 	int handled = 0;
3471da177e4SLinus Torvalds 
3481da177e4SLinus Torvalds 	dregs->csr &= ~CSR_DMA_ENABLE;
3491da177e4SLinus Torvalds 
3501da177e4SLinus Torvalds 
3511da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
3521da177e4SLinus Torvalds 	printk("scsi_intr csr %x\n", csr);
3531da177e4SLinus Torvalds #endif
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	if(csr & ~CSR_GOOD) {
3561da177e4SLinus Torvalds 		if(csr & CSR_DMA_BUSERR) {
3571da177e4SLinus Torvalds 			printk("scsi%d: bus error in dma\n", default_instance->host_no);
3581da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
3591da177e4SLinus Torvalds 			printk("scsi: residual %x count %x addr %p dmaaddr %x\n",
3601da177e4SLinus Torvalds 			       dregs->fifo_count,
3611da177e4SLinus Torvalds 			       dregs->dma_count_lo | (dregs->dma_count_hi << 16),
3621da177e4SLinus Torvalds 			       sun3_dma_orig_addr,
3631da177e4SLinus Torvalds 			       dregs->dma_addr_lo | (dregs->dma_addr_hi << 16));
3641da177e4SLinus Torvalds #endif
3651da177e4SLinus Torvalds 		}
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 		if(csr & CSR_DMA_CONFLICT) {
3681da177e4SLinus Torvalds 			printk("scsi%d: dma conflict\n", default_instance->host_no);
3691da177e4SLinus Torvalds 		}
3701da177e4SLinus Torvalds 		handled = 1;
3711da177e4SLinus Torvalds 	}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 	if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
3741da177e4SLinus Torvalds 		NCR5380_intr(irq, dummy, fp);
3751da177e4SLinus Torvalds 		handled = 1;
3761da177e4SLinus Torvalds 	}
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	return IRQ_RETVAL(handled);
3791da177e4SLinus Torvalds }
3801da177e4SLinus Torvalds 
3811da177e4SLinus Torvalds /*
3821da177e4SLinus Torvalds  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
3831da177e4SLinus Torvalds  * reentering NCR5380_print_status seems to have ugly side effects
3841da177e4SLinus Torvalds  */
3851da177e4SLinus Torvalds 
3861da177e4SLinus Torvalds /* this doesn't seem to get used at all -- sam */
3871da177e4SLinus Torvalds #if 0
3881da177e4SLinus Torvalds void sun3_sun3_debug (void)
3891da177e4SLinus Torvalds {
3901da177e4SLinus Torvalds 	unsigned long flags;
3911da177e4SLinus Torvalds 	NCR5380_local_declare();
3921da177e4SLinus Torvalds 
3931da177e4SLinus Torvalds 	if (default_instance) {
3941da177e4SLinus Torvalds 			local_irq_save(flags);
3951da177e4SLinus Torvalds 			NCR5380_print_status(default_instance);
3961da177e4SLinus Torvalds 			local_irq_restore(flags);
3971da177e4SLinus Torvalds 	}
3981da177e4SLinus Torvalds }
3991da177e4SLinus Torvalds #endif
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
4031da177e4SLinus Torvalds static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
4041da177e4SLinus Torvalds {
4051da177e4SLinus Torvalds 	void *addr;
4061da177e4SLinus Torvalds 
4071da177e4SLinus Torvalds 	if(sun3_dma_orig_addr != NULL)
4081da177e4SLinus Torvalds 		dvma_unmap(sun3_dma_orig_addr);
4091da177e4SLinus Torvalds 
4101da177e4SLinus Torvalds //	addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
4111da177e4SLinus Torvalds 	addr = (void *)dvma_map_vme((unsigned long) data, count);
4121da177e4SLinus Torvalds 
4131da177e4SLinus Torvalds 	sun3_dma_orig_addr = addr;
4141da177e4SLinus Torvalds 	sun3_dma_orig_count = count;
4151da177e4SLinus Torvalds 
4161da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
4171da177e4SLinus Torvalds 	printk("scsi: dma_setup addr %p count %x\n", addr, count);
4181da177e4SLinus Torvalds #endif
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds //	dregs->fifo_count = 0;
4211da177e4SLinus Torvalds #if 0
4221da177e4SLinus Torvalds 	/* reset fifo */
4231da177e4SLinus Torvalds 	dregs->csr &= ~CSR_FIFO;
4241da177e4SLinus Torvalds 	dregs->csr |= CSR_FIFO;
4251da177e4SLinus Torvalds #endif
4261da177e4SLinus Torvalds 	/* set direction */
4271da177e4SLinus Torvalds 	if(write_flag)
4281da177e4SLinus Torvalds 		dregs->csr |= CSR_SEND;
4291da177e4SLinus Torvalds 	else
4301da177e4SLinus Torvalds 		dregs->csr &= ~CSR_SEND;
4311da177e4SLinus Torvalds 
4321da177e4SLinus Torvalds 	/* reset fifo */
4331da177e4SLinus Torvalds //	dregs->csr &= ~CSR_FIFO;
4341da177e4SLinus Torvalds //	dregs->csr |= CSR_FIFO;
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	dregs->csr |= CSR_PACK_ENABLE;
4371da177e4SLinus Torvalds 
4381da177e4SLinus Torvalds 	dregs->dma_addr_hi = ((unsigned long)addr >> 16);
4391da177e4SLinus Torvalds 	dregs->dma_addr_lo = ((unsigned long)addr & 0xffff);
4401da177e4SLinus Torvalds 
4411da177e4SLinus Torvalds 	dregs->dma_count_hi = 0;
4421da177e4SLinus Torvalds 	dregs->dma_count_lo = 0;
4431da177e4SLinus Torvalds 	dregs->fifo_count_hi = 0;
4441da177e4SLinus Torvalds 	dregs->fifo_count = 0;
4451da177e4SLinus Torvalds 
4461da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
4471da177e4SLinus Torvalds 	printk("scsi: dma_setup done csr %x\n", dregs->csr);
4481da177e4SLinus Torvalds #endif
4491da177e4SLinus Torvalds        	return count;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds }
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
4541da177e4SLinus Torvalds {
4551da177e4SLinus Torvalds 	return last_residual;
4561da177e4SLinus Torvalds }
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted, Scsi_Cmnd *cmd,
4591da177e4SLinus Torvalds 				    int write_flag)
4601da177e4SLinus Torvalds {
4611da177e4SLinus Torvalds 	if(cmd->request->flags & REQ_CMD)
4621da177e4SLinus Torvalds  		return wanted;
4631da177e4SLinus Torvalds 	else
4641da177e4SLinus Torvalds 		return 0;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static int sun3scsi_dma_start(unsigned long count, char *data)
4681da177e4SLinus Torvalds {
4691da177e4SLinus Torvalds 
4701da177e4SLinus Torvalds 	unsigned short csr;
4711da177e4SLinus Torvalds 
4721da177e4SLinus Torvalds 	csr = dregs->csr;
4731da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
4741da177e4SLinus Torvalds 	printk("scsi: dma_start data %p count %x csr %x fifo %x\n", data, count, csr, dregs->fifo_count);
4751da177e4SLinus Torvalds #endif
4761da177e4SLinus Torvalds 
4771da177e4SLinus Torvalds 	dregs->dma_count_hi = (sun3_dma_orig_count >> 16);
4781da177e4SLinus Torvalds 	dregs->dma_count_lo = (sun3_dma_orig_count & 0xffff);
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	dregs->fifo_count_hi = (sun3_dma_orig_count >> 16);
4811da177e4SLinus Torvalds 	dregs->fifo_count = (sun3_dma_orig_count & 0xffff);
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds //	if(!(csr & CSR_DMA_ENABLE))
4841da177e4SLinus Torvalds //		dregs->csr |= CSR_DMA_ENABLE;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	return 0;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds /* clean up after our dma is done */
4901da177e4SLinus Torvalds static int sun3scsi_dma_finish(int write_flag)
4911da177e4SLinus Torvalds {
4921da177e4SLinus Torvalds 	unsigned short fifo;
4931da177e4SLinus Torvalds 	int ret = 0;
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	sun3_dma_active = 0;
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	dregs->csr &= ~CSR_DMA_ENABLE;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	fifo = dregs->fifo_count;
5001da177e4SLinus Torvalds 	if(write_flag) {
5011da177e4SLinus Torvalds 		if((fifo > 0) && (fifo < sun3_dma_orig_count))
5021da177e4SLinus Torvalds 			fifo++;
5031da177e4SLinus Torvalds 	}
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds 	last_residual = fifo;
5061da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
5071da177e4SLinus Torvalds 	printk("scsi: residual %x total %x\n", fifo, sun3_dma_orig_count);
5081da177e4SLinus Torvalds #endif
5091da177e4SLinus Torvalds 	/* empty bytes from the fifo which didn't make it */
5101da177e4SLinus Torvalds 	if((!write_flag) && (dregs->csr & CSR_LEFT)) {
5111da177e4SLinus Torvalds 		unsigned char *vaddr;
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds #ifdef SUN3_SCSI_DEBUG
5141da177e4SLinus Torvalds 		printk("scsi: got left over bytes\n");
5151da177e4SLinus Torvalds #endif
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 		vaddr = (unsigned char *)dvma_vmetov(sun3_dma_orig_addr);
5181da177e4SLinus Torvalds 
5191da177e4SLinus Torvalds 		vaddr += (sun3_dma_orig_count - fifo);
5201da177e4SLinus Torvalds 		vaddr--;
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 		switch(dregs->csr & CSR_LEFT) {
5231da177e4SLinus Torvalds 		case CSR_LEFT_3:
5241da177e4SLinus Torvalds 			*vaddr = (dregs->bpack_lo & 0xff00) >> 8;
5251da177e4SLinus Torvalds 			vaddr--;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 		case CSR_LEFT_2:
5281da177e4SLinus Torvalds 			*vaddr = (dregs->bpack_hi & 0x00ff);
5291da177e4SLinus Torvalds 			vaddr--;
5301da177e4SLinus Torvalds 
5311da177e4SLinus Torvalds 		case CSR_LEFT_1:
5321da177e4SLinus Torvalds 			*vaddr = (dregs->bpack_hi & 0xff00) >> 8;
5331da177e4SLinus Torvalds 			break;
5341da177e4SLinus Torvalds 		}
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 
5371da177e4SLinus Torvalds 	}
5381da177e4SLinus Torvalds 
5391da177e4SLinus Torvalds 	dvma_unmap(sun3_dma_orig_addr);
5401da177e4SLinus Torvalds 	sun3_dma_orig_addr = NULL;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	dregs->dma_addr_hi = 0;
5431da177e4SLinus Torvalds 	dregs->dma_addr_lo = 0;
5441da177e4SLinus Torvalds 	dregs->dma_count_hi = 0;
5451da177e4SLinus Torvalds 	dregs->dma_count_lo = 0;
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds 	dregs->fifo_count = 0;
5481da177e4SLinus Torvalds 	dregs->fifo_count_hi = 0;
5491da177e4SLinus Torvalds 
5501da177e4SLinus Torvalds 	dregs->csr &= ~CSR_SEND;
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds //	dregs->csr |= CSR_DMA_ENABLE;
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds #if 0
5551da177e4SLinus Torvalds 	/* reset fifo */
5561da177e4SLinus Torvalds 	dregs->csr &= ~CSR_FIFO;
5571da177e4SLinus Torvalds 	dregs->csr |= CSR_FIFO;
5581da177e4SLinus Torvalds #endif
5591da177e4SLinus Torvalds 	sun3_dma_setup_done = NULL;
5601da177e4SLinus Torvalds 
5611da177e4SLinus Torvalds 	return ret;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds }
5641da177e4SLinus Torvalds 
5651da177e4SLinus Torvalds #include "sun3_NCR5380.c"
5661da177e4SLinus Torvalds 
5671da177e4SLinus Torvalds static Scsi_Host_Template driver_template = {
5681da177e4SLinus Torvalds 	.name			= SUN3_SCSI_NAME,
5691da177e4SLinus Torvalds 	.detect			= sun3scsi_detect,
5701da177e4SLinus Torvalds 	.release		= sun3scsi_release,
5711da177e4SLinus Torvalds 	.info			= sun3scsi_info,
5721da177e4SLinus Torvalds 	.queuecommand		= sun3scsi_queue_command,
5731da177e4SLinus Torvalds 	.eh_abort_handler      	= sun3scsi_abort,
5741da177e4SLinus Torvalds 	.eh_bus_reset_handler  	= sun3scsi_bus_reset,
5751da177e4SLinus Torvalds 	.can_queue		= CAN_QUEUE,
5761da177e4SLinus Torvalds 	.this_id		= 7,
5771da177e4SLinus Torvalds 	.sg_tablesize		= SG_TABLESIZE,
5781da177e4SLinus Torvalds 	.cmd_per_lun		= CMD_PER_LUN,
5791da177e4SLinus Torvalds 	.use_clustering		= DISABLE_CLUSTERING
5801da177e4SLinus Torvalds };
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 
5831da177e4SLinus Torvalds #include "scsi_module.c"
5841da177e4SLinus Torvalds 
585