xref: /linux/drivers/scsi/sun3_scsi.c (revision 1e641664301744f0d381de43ae1e12343e60b479)
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  * Adapted from mac_scsinew.c:
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * Generic Macintosh NCR5380 driver
101da177e4SLinus Torvalds  *
111da177e4SLinus Torvalds  * Copyright 1998, Michael Schmitz <mschmitz@lbl.gov>
121da177e4SLinus Torvalds  *
131da177e4SLinus Torvalds  * derived in part from:
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds /*
161da177e4SLinus Torvalds  * Generic Generic NCR5380 driver
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * Copyright 1995, Russell King
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * ALPHA RELEASE 1.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * For more information, please consult
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * NCR 5380 Family
251da177e4SLinus Torvalds  * SCSI Protocol Controller
261da177e4SLinus Torvalds  * Databook
271da177e4SLinus Torvalds  *
281da177e4SLinus Torvalds  * NCR Microelectronics
291da177e4SLinus Torvalds  * 1635 Aeroplaza Drive
301da177e4SLinus Torvalds  * Colorado Springs, CO 80916
311da177e4SLinus Torvalds  * 1+ (719) 578-3400
321da177e4SLinus Torvalds  * 1+ (800) 334-5454
331da177e4SLinus Torvalds  */
341da177e4SLinus Torvalds 
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds /*
371da177e4SLinus Torvalds  * This is from mac_scsi.h, but hey, maybe this is useful for Sun3 too! :)
381da177e4SLinus Torvalds  *
391da177e4SLinus Torvalds  * Options :
401da177e4SLinus Torvalds  *
411da177e4SLinus Torvalds  * PARITY - enable parity checking.  Not supported.
421da177e4SLinus Torvalds  *
431da177e4SLinus Torvalds  * SCSI2 - enable support for SCSI-II tagged queueing.  Untested.
441da177e4SLinus Torvalds  *
451da177e4SLinus Torvalds  * USLEEP - enable support for devices that don't disconnect.  Untested.
461da177e4SLinus Torvalds  */
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * $Log: sun3_NCR5380.c,v $
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds #define AUTOSENSE
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #include <linux/types.h>
551da177e4SLinus Torvalds #include <linux/stddef.h>
561da177e4SLinus Torvalds #include <linux/ctype.h>
571da177e4SLinus Torvalds #include <linux/delay.h>
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds #include <linux/module.h>
601da177e4SLinus Torvalds #include <linux/signal.h>
611da177e4SLinus Torvalds #include <linux/ioport.h>
621da177e4SLinus Torvalds #include <linux/init.h>
631da177e4SLinus Torvalds #include <linux/blkdev.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #include <asm/io.h>
661da177e4SLinus Torvalds #include <asm/system.h>
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds #include <asm/sun3ints.h>
691da177e4SLinus Torvalds #include <asm/dvma.h>
701da177e4SLinus Torvalds #include <asm/idprom.h>
711da177e4SLinus Torvalds #include <asm/machines.h>
721da177e4SLinus Torvalds 
731da177e4SLinus Torvalds /* dma on! */
741da177e4SLinus Torvalds #define REAL_DMA
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds #include "scsi.h"
77c39e7eeeSSam Creasey #include "initio.h"
781da177e4SLinus Torvalds #include <scsi/scsi_host.h>
791da177e4SLinus Torvalds #include "sun3_scsi.h"
801da177e4SLinus Torvalds 
811da177e4SLinus Torvalds static void NCR5380_print(struct Scsi_Host *instance);
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /* #define OLDDMA */
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds #define USE_WRAPPER
861da177e4SLinus Torvalds /*#define RESET_BOOT */
871da177e4SLinus Torvalds #define DRIVER_SETUP
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds #define NDEBUG 0
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds /*
921da177e4SLinus Torvalds  * BUG can be used to trigger a strange code-size related hang on 2.1 kernels
931da177e4SLinus Torvalds  */
941da177e4SLinus Torvalds #ifdef BUG
951da177e4SLinus Torvalds #undef RESET_BOOT
961da177e4SLinus Torvalds #undef DRIVER_SETUP
971da177e4SLinus Torvalds #endif
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds /* #define SUPPORT_TAGS */
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds #define	ENABLE_IRQ()	enable_irq( IRQ_SUN3_SCSI );
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 
1047d12e780SDavid Howells static irqreturn_t scsi_sun3_intr(int irq, void *dummy);
1051da177e4SLinus Torvalds static inline unsigned char sun3scsi_read(int reg);
1061da177e4SLinus Torvalds static inline void sun3scsi_write(int reg, int value);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds static int setup_can_queue = -1;
1091da177e4SLinus Torvalds module_param(setup_can_queue, int, 0);
1101da177e4SLinus Torvalds static int setup_cmd_per_lun = -1;
1111da177e4SLinus Torvalds module_param(setup_cmd_per_lun, int, 0);
1121da177e4SLinus Torvalds static int setup_sg_tablesize = -1;
1131da177e4SLinus Torvalds module_param(setup_sg_tablesize, int, 0);
1141da177e4SLinus Torvalds #ifdef SUPPORT_TAGS
1151da177e4SLinus Torvalds static int setup_use_tagged_queuing = -1;
1161da177e4SLinus Torvalds module_param(setup_use_tagged_queuing, int, 0);
1171da177e4SLinus Torvalds #endif
1181da177e4SLinus Torvalds static int setup_hostid = -1;
1191da177e4SLinus Torvalds module_param(setup_hostid, int, 0);
1201da177e4SLinus Torvalds 
121811c9366SHenne static struct scsi_cmnd *sun3_dma_setup_done = NULL;
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds #define	AFTER_RESET_DELAY	(HZ/2)
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /* ms to wait after hitting dma regs */
1261da177e4SLinus Torvalds #define SUN3_DMA_DELAY 10
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /* dvma buffer to allocate -- 32k should hopefully be more than sufficient */
1291da177e4SLinus Torvalds #define SUN3_DVMA_BUFSIZE 0xe000
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds /* minimum number of bytes to do dma on */
1321da177e4SLinus Torvalds #define SUN3_DMA_MINSIZE 128
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static volatile unsigned char *sun3_scsi_regp;
1351da177e4SLinus Torvalds static volatile struct sun3_dma_regs *dregs;
1361da177e4SLinus Torvalds #ifdef OLDDMA
1371da177e4SLinus Torvalds static unsigned char *dmabuf = NULL; /* dma memory buffer */
1381da177e4SLinus Torvalds #endif
1391da177e4SLinus Torvalds static struct sun3_udc_regs *udc_regs = NULL;
1401da177e4SLinus Torvalds static unsigned char *sun3_dma_orig_addr = NULL;
1411da177e4SLinus Torvalds static unsigned long sun3_dma_orig_count = 0;
1421da177e4SLinus Torvalds static int sun3_dma_active = 0;
1431da177e4SLinus Torvalds static unsigned long last_residual = 0;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds /*
1461da177e4SLinus Torvalds  * NCR 5380 register access functions
1471da177e4SLinus Torvalds  */
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds static inline unsigned char sun3scsi_read(int reg)
1501da177e4SLinus Torvalds {
1511da177e4SLinus Torvalds 	return( sun3_scsi_regp[reg] );
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds static inline void sun3scsi_write(int reg, int value)
1551da177e4SLinus Torvalds {
1561da177e4SLinus Torvalds 	sun3_scsi_regp[reg] = value;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /* dma controller register access functions */
1601da177e4SLinus Torvalds 
1611da177e4SLinus Torvalds static inline unsigned short sun3_udc_read(unsigned char reg)
1621da177e4SLinus Torvalds {
1631da177e4SLinus Torvalds 	unsigned short ret;
1641da177e4SLinus Torvalds 
1651da177e4SLinus Torvalds 	dregs->udc_addr = UDC_CSR;
1661da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
1671da177e4SLinus Torvalds 	ret = dregs->udc_data;
1681da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 	return ret;
1711da177e4SLinus Torvalds }
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds static inline void sun3_udc_write(unsigned short val, unsigned char reg)
1741da177e4SLinus Torvalds {
1751da177e4SLinus Torvalds 	dregs->udc_addr = reg;
1761da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
1771da177e4SLinus Torvalds 	dregs->udc_data = val;
1781da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds  * XXX: status debug
1831da177e4SLinus Torvalds  */
1841da177e4SLinus Torvalds static struct Scsi_Host *default_instance;
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
187d0be4a7dSChristoph Hellwig  * Function : int sun3scsi_detect(struct scsi_host_template * tpnt)
1881da177e4SLinus Torvalds  *
1891da177e4SLinus Torvalds  * Purpose : initializes mac NCR5380 driver based on the
1901da177e4SLinus Torvalds  *	command line / compile time port and irq definitions.
1911da177e4SLinus Torvalds  *
1921da177e4SLinus Torvalds  * Inputs : tpnt - template for this SCSI adapter.
1931da177e4SLinus Torvalds  *
1941da177e4SLinus Torvalds  * Returns : 1 if a host adapter was found, 0 if not.
1951da177e4SLinus Torvalds  *
1961da177e4SLinus Torvalds  */
1971da177e4SLinus Torvalds 
198d0be4a7dSChristoph Hellwig int sun3scsi_detect(struct scsi_host_template * tpnt)
1991da177e4SLinus Torvalds {
2001da177e4SLinus Torvalds 	unsigned long ioaddr;
2011da177e4SLinus Torvalds 	static int called = 0;
2021da177e4SLinus Torvalds 	struct Scsi_Host *instance;
2031da177e4SLinus Torvalds 
2041da177e4SLinus Torvalds 	/* check that this machine has an onboard 5380 */
2051da177e4SLinus Torvalds 	switch(idprom->id_machtype) {
2061da177e4SLinus Torvalds 	case SM_SUN3|SM_3_50:
2071da177e4SLinus Torvalds 	case SM_SUN3|SM_3_60:
2081da177e4SLinus Torvalds 		break;
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	default:
2111da177e4SLinus Torvalds 		return 0;
2121da177e4SLinus Torvalds 	}
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	if(called)
2151da177e4SLinus Torvalds 		return 0;
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 	tpnt->proc_name = "Sun3 5380 SCSI";
2181da177e4SLinus Torvalds 
2191da177e4SLinus Torvalds 	/* setup variables */
2201da177e4SLinus Torvalds 	tpnt->can_queue =
2211da177e4SLinus Torvalds 		(setup_can_queue > 0) ? setup_can_queue : CAN_QUEUE;
2221da177e4SLinus Torvalds 	tpnt->cmd_per_lun =
2231da177e4SLinus Torvalds 		(setup_cmd_per_lun > 0) ? setup_cmd_per_lun : CMD_PER_LUN;
2241da177e4SLinus Torvalds 	tpnt->sg_tablesize =
2251da177e4SLinus Torvalds 		(setup_sg_tablesize >= 0) ? setup_sg_tablesize : SG_TABLESIZE;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	if (setup_hostid >= 0)
2281da177e4SLinus Torvalds 		tpnt->this_id = setup_hostid;
2291da177e4SLinus Torvalds 	else {
2301da177e4SLinus Torvalds 		/* use 7 as default */
2311da177e4SLinus Torvalds 		tpnt->this_id = 7;
2321da177e4SLinus Torvalds 	}
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 	ioaddr = (unsigned long)ioremap(IOBASE_SUN3_SCSI, PAGE_SIZE);
2351da177e4SLinus Torvalds 	sun3_scsi_regp = (unsigned char *)ioaddr;
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	dregs = (struct sun3_dma_regs *)(((unsigned char *)ioaddr) + 8);
2381da177e4SLinus Torvalds 
2391da177e4SLinus Torvalds 	if((udc_regs = dvma_malloc(sizeof(struct sun3_udc_regs)))
2401da177e4SLinus Torvalds 	   == NULL) {
2411da177e4SLinus Torvalds 	     printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
2421da177e4SLinus Torvalds 	     return 0;
2431da177e4SLinus Torvalds 	}
2441da177e4SLinus Torvalds #ifdef OLDDMA
2451da177e4SLinus Torvalds 	if((dmabuf = dvma_malloc_align(SUN3_DVMA_BUFSIZE, 0x10000)) == NULL) {
2461da177e4SLinus Torvalds 	     printk("SUN3 Scsi couldn't allocate DVMA memory!\n");
2471da177e4SLinus Torvalds 	     return 0;
2481da177e4SLinus Torvalds 	}
2491da177e4SLinus Torvalds #endif
2501da177e4SLinus Torvalds #ifdef SUPPORT_TAGS
2511da177e4SLinus Torvalds 	if (setup_use_tagged_queuing < 0)
2521da177e4SLinus Torvalds 		setup_use_tagged_queuing = USE_TAGGED_QUEUING;
2531da177e4SLinus Torvalds #endif
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	instance = scsi_register (tpnt, sizeof(struct NCR5380_hostdata));
2561da177e4SLinus Torvalds 	if(instance == NULL)
2571da177e4SLinus Torvalds 		return 0;
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	default_instance = instance;
2601da177e4SLinus Torvalds 
2611da177e4SLinus Torvalds         instance->io_port = (unsigned long) ioaddr;
2621da177e4SLinus Torvalds 	instance->irq = IRQ_SUN3_SCSI;
2631da177e4SLinus Torvalds 
2641da177e4SLinus Torvalds 	NCR5380_init(instance, 0);
2651da177e4SLinus Torvalds 
2661da177e4SLinus Torvalds 	instance->n_io_port = 32;
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds         ((struct NCR5380_hostdata *)instance->hostdata)->ctrl = 0;
2691da177e4SLinus Torvalds 
2701da177e4SLinus Torvalds 	if (request_irq(instance->irq, scsi_sun3_intr,
271*1e641664SJeff Garzik 			     0, "Sun3SCSI-5380", instance)) {
2721da177e4SLinus Torvalds #ifndef REAL_DMA
2731da177e4SLinus Torvalds 		printk("scsi%d: IRQ%d not free, interrupts disabled\n",
2741da177e4SLinus Torvalds 		       instance->host_no, instance->irq);
2751da177e4SLinus Torvalds 		instance->irq = SCSI_IRQ_NONE;
2761da177e4SLinus Torvalds #else
2771da177e4SLinus Torvalds 		printk("scsi%d: IRQ%d not free, bailing out\n",
2781da177e4SLinus Torvalds 		       instance->host_no, instance->irq);
2791da177e4SLinus Torvalds 		return 0;
2801da177e4SLinus Torvalds #endif
2811da177e4SLinus Torvalds 	}
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	printk("scsi%d: Sun3 5380 at port %lX irq", instance->host_no, instance->io_port);
2841da177e4SLinus Torvalds 	if (instance->irq == SCSI_IRQ_NONE)
2851da177e4SLinus Torvalds 		printk ("s disabled");
2861da177e4SLinus Torvalds 	else
2871da177e4SLinus Torvalds 		printk (" %d", instance->irq);
2881da177e4SLinus Torvalds 	printk(" options CAN_QUEUE=%d CMD_PER_LUN=%d release=%d",
2891da177e4SLinus Torvalds 	       instance->can_queue, instance->cmd_per_lun,
2901da177e4SLinus Torvalds 	       SUN3SCSI_PUBLIC_RELEASE);
2911da177e4SLinus Torvalds 	printk("\nscsi%d:", instance->host_no);
2921da177e4SLinus Torvalds 	NCR5380_print_options(instance);
2931da177e4SLinus Torvalds 	printk("\n");
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	dregs->csr = 0;
2961da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
2971da177e4SLinus Torvalds 	dregs->csr = CSR_SCSI | CSR_FIFO | CSR_INTR;
2981da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
2991da177e4SLinus Torvalds 	dregs->fifo_count = 0;
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds 	called = 1;
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds #ifdef RESET_BOOT
3041da177e4SLinus Torvalds 	sun3_scsi_reset_boot(instance);
3051da177e4SLinus Torvalds #endif
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	return 1;
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds int sun3scsi_release (struct Scsi_Host *shpnt)
3111da177e4SLinus Torvalds {
3121da177e4SLinus Torvalds 	if (shpnt->irq != SCSI_IRQ_NONE)
313*1e641664SJeff Garzik 		free_irq(shpnt->irq, shpnt);
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds 	iounmap((void *)sun3_scsi_regp);
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	return 0;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds #ifdef RESET_BOOT
3211da177e4SLinus Torvalds /*
3221da177e4SLinus Torvalds  * Our 'bus reset on boot' function
3231da177e4SLinus Torvalds  */
3241da177e4SLinus Torvalds 
3251da177e4SLinus Torvalds static void sun3_scsi_reset_boot(struct Scsi_Host *instance)
3261da177e4SLinus Torvalds {
3271da177e4SLinus Torvalds 	unsigned long end;
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	NCR5380_local_declare();
3301da177e4SLinus Torvalds 	NCR5380_setup(instance);
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds 	/*
3331da177e4SLinus Torvalds 	 * Do a SCSI reset to clean up the bus during initialization. No
3341da177e4SLinus Torvalds 	 * messing with the queues, interrupts, or locks necessary here.
3351da177e4SLinus Torvalds 	 */
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds 	printk( "Sun3 SCSI: resetting the SCSI bus..." );
3381da177e4SLinus Torvalds 
3391da177e4SLinus Torvalds 	/* switch off SCSI IRQ - catch an interrupt without IRQ bit set else */
3401da177e4SLinus Torvalds //       	sun3_disable_irq( IRQ_SUN3_SCSI );
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds 	/* get in phase */
3431da177e4SLinus Torvalds 	NCR5380_write( TARGET_COMMAND_REG,
3441da177e4SLinus Torvalds 		      PHASE_SR_TO_TCR( NCR5380_read(STATUS_REG) ));
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds 	/* assert RST */
3471da177e4SLinus Torvalds 	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE | ICR_ASSERT_RST );
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	/* The min. reset hold time is 25us, so 40us should be enough */
3501da177e4SLinus Torvalds 	udelay( 50 );
3511da177e4SLinus Torvalds 
3521da177e4SLinus Torvalds 	/* reset RST and interrupt */
3531da177e4SLinus Torvalds 	NCR5380_write( INITIATOR_COMMAND_REG, ICR_BASE );
3541da177e4SLinus Torvalds 	NCR5380_read( RESET_PARITY_INTERRUPT_REG );
3551da177e4SLinus Torvalds 
3561da177e4SLinus Torvalds 	for( end = jiffies + AFTER_RESET_DELAY; time_before(jiffies, end); )
3571da177e4SLinus Torvalds 		barrier();
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	/* switch on SCSI IRQ again */
3601da177e4SLinus Torvalds //       	sun3_enable_irq( IRQ_SUN3_SCSI );
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds 	printk( " done\n" );
3631da177e4SLinus Torvalds }
3641da177e4SLinus Torvalds #endif
3651da177e4SLinus Torvalds 
3661da177e4SLinus Torvalds const char * sun3scsi_info (struct Scsi_Host *spnt) {
3671da177e4SLinus Torvalds     return "";
3681da177e4SLinus Torvalds }
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds // safe bits for the CSR
3711da177e4SLinus Torvalds #define CSR_GOOD 0x060f
3721da177e4SLinus Torvalds 
3737d12e780SDavid Howells static irqreturn_t scsi_sun3_intr(int irq, void *dummy)
3741da177e4SLinus Torvalds {
3751da177e4SLinus Torvalds 	unsigned short csr = dregs->csr;
3761da177e4SLinus Torvalds 	int handled = 0;
3771da177e4SLinus Torvalds 
3781da177e4SLinus Torvalds 	if(csr & ~CSR_GOOD) {
3791da177e4SLinus Torvalds 		if(csr & CSR_DMA_BUSERR) {
3801da177e4SLinus Torvalds 			printk("scsi%d: bus error in dma\n", default_instance->host_no);
3811da177e4SLinus Torvalds 		}
3821da177e4SLinus Torvalds 
3831da177e4SLinus Torvalds 		if(csr & CSR_DMA_CONFLICT) {
3841da177e4SLinus Torvalds 			printk("scsi%d: dma conflict\n", default_instance->host_no);
3851da177e4SLinus Torvalds 		}
3861da177e4SLinus Torvalds 		handled = 1;
3871da177e4SLinus Torvalds 	}
3881da177e4SLinus Torvalds 
3891da177e4SLinus Torvalds 	if(csr & (CSR_SDB_INT | CSR_DMA_INT)) {
3907d12e780SDavid Howells 		NCR5380_intr(irq, dummy);
3911da177e4SLinus Torvalds 		handled = 1;
3921da177e4SLinus Torvalds 	}
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	return IRQ_RETVAL(handled);
3951da177e4SLinus Torvalds }
3961da177e4SLinus Torvalds 
3971da177e4SLinus Torvalds /*
3981da177e4SLinus Torvalds  * Debug stuff - to be called on NMI, or sysrq key. Use at your own risk;
3991da177e4SLinus Torvalds  * reentering NCR5380_print_status seems to have ugly side effects
4001da177e4SLinus Torvalds  */
4011da177e4SLinus Torvalds 
4021da177e4SLinus Torvalds /* this doesn't seem to get used at all -- sam */
4031da177e4SLinus Torvalds #if 0
4041da177e4SLinus Torvalds void sun3_sun3_debug (void)
4051da177e4SLinus Torvalds {
4061da177e4SLinus Torvalds 	unsigned long flags;
4071da177e4SLinus Torvalds 	NCR5380_local_declare();
4081da177e4SLinus Torvalds 
4091da177e4SLinus Torvalds 	if (default_instance) {
4101da177e4SLinus Torvalds 			local_irq_save(flags);
4111da177e4SLinus Torvalds 			NCR5380_print_status(default_instance);
4121da177e4SLinus Torvalds 			local_irq_restore(flags);
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds }
4151da177e4SLinus Torvalds #endif
4161da177e4SLinus Torvalds 
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds /* sun3scsi_dma_setup() -- initialize the dma controller for a read/write */
4191da177e4SLinus Torvalds static unsigned long sun3scsi_dma_setup(void *data, unsigned long count, int write_flag)
4201da177e4SLinus Torvalds {
4211da177e4SLinus Torvalds #ifdef OLDDMA
4221da177e4SLinus Torvalds 	if(write_flag)
4231da177e4SLinus Torvalds 		memcpy(dmabuf, data, count);
4241da177e4SLinus Torvalds 	else {
4251da177e4SLinus Torvalds 		sun3_dma_orig_addr = data;
4261da177e4SLinus Torvalds 		sun3_dma_orig_count = count;
4271da177e4SLinus Torvalds 	}
4281da177e4SLinus Torvalds #else
4291da177e4SLinus Torvalds 	void *addr;
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds 	if(sun3_dma_orig_addr != NULL)
4321da177e4SLinus Torvalds 		dvma_unmap(sun3_dma_orig_addr);
4331da177e4SLinus Torvalds 
4341da177e4SLinus Torvalds //	addr = sun3_dvma_page((unsigned long)data, (unsigned long)dmabuf);
4351da177e4SLinus Torvalds 	addr = (void *)dvma_map((unsigned long) data, count);
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds 	sun3_dma_orig_addr = addr;
4381da177e4SLinus Torvalds 	sun3_dma_orig_count = count;
4391da177e4SLinus Torvalds #endif
4401da177e4SLinus Torvalds 	dregs->fifo_count = 0;
4411da177e4SLinus Torvalds 	sun3_udc_write(UDC_RESET, UDC_CSR);
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	/* reset fifo */
4441da177e4SLinus Torvalds 	dregs->csr &= ~CSR_FIFO;
4451da177e4SLinus Torvalds 	dregs->csr |= CSR_FIFO;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	/* set direction */
4481da177e4SLinus Torvalds 	if(write_flag)
4491da177e4SLinus Torvalds 		dregs->csr |= CSR_SEND;
4501da177e4SLinus Torvalds 	else
4511da177e4SLinus Torvalds 		dregs->csr &= ~CSR_SEND;
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	/* byte count for fifo */
4541da177e4SLinus Torvalds 	dregs->fifo_count = count;
4551da177e4SLinus Torvalds 
4561da177e4SLinus Torvalds 	sun3_udc_write(UDC_RESET, UDC_CSR);
4571da177e4SLinus Torvalds 
4581da177e4SLinus Torvalds 	/* reset fifo */
4591da177e4SLinus Torvalds 	dregs->csr &= ~CSR_FIFO;
4601da177e4SLinus Torvalds 	dregs->csr |= CSR_FIFO;
4611da177e4SLinus Torvalds 
4621da177e4SLinus Torvalds 	if(dregs->fifo_count != count) {
4631da177e4SLinus Torvalds 		printk("scsi%d: fifo_mismatch %04x not %04x\n",
4641da177e4SLinus Torvalds 		       default_instance->host_no, dregs->fifo_count,
4651da177e4SLinus Torvalds 		       (unsigned int) count);
4661da177e4SLinus Torvalds 		NCR5380_print(default_instance);
4671da177e4SLinus Torvalds 	}
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	/* setup udc */
4701da177e4SLinus Torvalds #ifdef OLDDMA
4711da177e4SLinus Torvalds 	udc_regs->addr_hi = ((dvma_vtob(dmabuf) & 0xff0000) >> 8);
4721da177e4SLinus Torvalds 	udc_regs->addr_lo = (dvma_vtob(dmabuf) & 0xffff);
4731da177e4SLinus Torvalds #else
4741da177e4SLinus Torvalds 	udc_regs->addr_hi = (((unsigned long)(addr) & 0xff0000) >> 8);
4751da177e4SLinus Torvalds 	udc_regs->addr_lo = ((unsigned long)(addr) & 0xffff);
4761da177e4SLinus Torvalds #endif
4771da177e4SLinus Torvalds 	udc_regs->count = count/2; /* count in words */
4781da177e4SLinus Torvalds 	udc_regs->mode_hi = UDC_MODE_HIWORD;
4791da177e4SLinus Torvalds 	if(write_flag) {
4801da177e4SLinus Torvalds 		if(count & 1)
4811da177e4SLinus Torvalds 			udc_regs->count++;
4821da177e4SLinus Torvalds 		udc_regs->mode_lo = UDC_MODE_LSEND;
4831da177e4SLinus Torvalds 		udc_regs->rsel = UDC_RSEL_SEND;
4841da177e4SLinus Torvalds 	} else {
4851da177e4SLinus Torvalds 		udc_regs->mode_lo = UDC_MODE_LRECV;
4861da177e4SLinus Torvalds 		udc_regs->rsel = UDC_RSEL_RECV;
4871da177e4SLinus Torvalds 	}
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds 	/* announce location of regs block */
4901da177e4SLinus Torvalds 	sun3_udc_write(((dvma_vtob(udc_regs) & 0xff0000) >> 8),
4911da177e4SLinus Torvalds 		       UDC_CHN_HI);
4921da177e4SLinus Torvalds 
4931da177e4SLinus Torvalds 	sun3_udc_write((dvma_vtob(udc_regs) & 0xffff), UDC_CHN_LO);
4941da177e4SLinus Torvalds 
4951da177e4SLinus Torvalds 	/* set dma master on */
4961da177e4SLinus Torvalds 	sun3_udc_write(0xd, UDC_MODE);
4971da177e4SLinus Torvalds 
4981da177e4SLinus Torvalds 	/* interrupt enable */
4991da177e4SLinus Torvalds 	sun3_udc_write(UDC_INT_ENABLE, UDC_CSR);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds        	return count;
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds }
5041da177e4SLinus Torvalds 
5051da177e4SLinus Torvalds static inline unsigned long sun3scsi_dma_count(struct Scsi_Host *instance)
5061da177e4SLinus Torvalds {
5071da177e4SLinus Torvalds 	unsigned short resid;
5081da177e4SLinus Torvalds 
5091da177e4SLinus Torvalds 	dregs->udc_addr = 0x32;
5101da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
5111da177e4SLinus Torvalds 	resid = dregs->udc_data;
5121da177e4SLinus Torvalds 	udelay(SUN3_DMA_DELAY);
5131da177e4SLinus Torvalds 	resid *= 2;
5141da177e4SLinus Torvalds 
5151da177e4SLinus Torvalds 	return (unsigned long) resid;
5161da177e4SLinus Torvalds }
5171da177e4SLinus Torvalds 
5181da177e4SLinus Torvalds static inline unsigned long sun3scsi_dma_residual(struct Scsi_Host *instance)
5191da177e4SLinus Torvalds {
5201da177e4SLinus Torvalds 	return last_residual;
5211da177e4SLinus Torvalds }
5221da177e4SLinus Torvalds 
523811c9366SHenne static inline unsigned long sun3scsi_dma_xfer_len(unsigned long wanted,
524811c9366SHenne 						  struct scsi_cmnd *cmd,
5251da177e4SLinus Torvalds 						  int write_flag)
5261da177e4SLinus Torvalds {
5274aff5e23SJens Axboe 	if(blk_fs_request(cmd->request))
5281da177e4SLinus Torvalds  		return wanted;
5291da177e4SLinus Torvalds 	else
5301da177e4SLinus Torvalds 		return 0;
5311da177e4SLinus Torvalds }
5321da177e4SLinus Torvalds 
5331da177e4SLinus Torvalds static inline int sun3scsi_dma_start(unsigned long count, unsigned char *data)
5341da177e4SLinus Torvalds {
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds     sun3_udc_write(UDC_CHN_START, UDC_CSR);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds     return 0;
5391da177e4SLinus Torvalds }
5401da177e4SLinus Torvalds 
5411da177e4SLinus Torvalds /* clean up after our dma is done */
5421da177e4SLinus Torvalds static int sun3scsi_dma_finish(int write_flag)
5431da177e4SLinus Torvalds {
5441da177e4SLinus Torvalds 	unsigned short count;
5451da177e4SLinus Torvalds 	unsigned short fifo;
5461da177e4SLinus Torvalds 	int ret = 0;
5471da177e4SLinus Torvalds 
5481da177e4SLinus Torvalds 	sun3_dma_active = 0;
5491da177e4SLinus Torvalds #if 1
5501da177e4SLinus Torvalds 	// check to empty the fifo on a read
5511da177e4SLinus Torvalds 	if(!write_flag) {
5521da177e4SLinus Torvalds 		int tmo = 20000; /* .2 sec */
5531da177e4SLinus Torvalds 
5541da177e4SLinus Torvalds 		while(1) {
5551da177e4SLinus Torvalds 			if(dregs->csr & CSR_FIFO_EMPTY)
5561da177e4SLinus Torvalds 				break;
5571da177e4SLinus Torvalds 
5581da177e4SLinus Torvalds 			if(--tmo <= 0) {
5591da177e4SLinus Torvalds 				printk("sun3scsi: fifo failed to empty!\n");
5601da177e4SLinus Torvalds 				return 1;
5611da177e4SLinus Torvalds 			}
5621da177e4SLinus Torvalds 			udelay(10);
5631da177e4SLinus Torvalds 		}
5641da177e4SLinus Torvalds 	}
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds #endif
5671da177e4SLinus Torvalds 
5681da177e4SLinus Torvalds 	count = sun3scsi_dma_count(default_instance);
5691da177e4SLinus Torvalds #ifdef OLDDMA
5701da177e4SLinus Torvalds 
5711da177e4SLinus Torvalds 	/* if we've finished a read, copy out the data we read */
5721da177e4SLinus Torvalds  	if(sun3_dma_orig_addr) {
5731da177e4SLinus Torvalds 		/* check for residual bytes after dma end */
5741da177e4SLinus Torvalds 		if(count && (NCR5380_read(BUS_AND_STATUS_REG) &
5751da177e4SLinus Torvalds 			     (BASR_PHASE_MATCH | BASR_ACK))) {
5761da177e4SLinus Torvalds 			printk("scsi%d: sun3_scsi_finish: read overrun baby... ", default_instance->host_no);
5771da177e4SLinus Torvalds 			printk("basr now %02x\n", NCR5380_read(BUS_AND_STATUS_REG));
5781da177e4SLinus Torvalds 			ret = count;
5791da177e4SLinus Torvalds 		}
5801da177e4SLinus Torvalds 
5811da177e4SLinus Torvalds 		/* copy in what we dma'd no matter what */
5821da177e4SLinus Torvalds 		memcpy(sun3_dma_orig_addr, dmabuf, sun3_dma_orig_count);
5831da177e4SLinus Torvalds 		sun3_dma_orig_addr = NULL;
5841da177e4SLinus Torvalds 
5851da177e4SLinus Torvalds 	}
5861da177e4SLinus Torvalds #else
5871da177e4SLinus Torvalds 
5881da177e4SLinus Torvalds 	fifo = dregs->fifo_count;
5891da177e4SLinus Torvalds 	last_residual = fifo;
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	/* empty bytes from the fifo which didn't make it */
5921da177e4SLinus Torvalds 	if((!write_flag) && (count - fifo) == 2) {
5931da177e4SLinus Torvalds 		unsigned short data;
5941da177e4SLinus Torvalds 		unsigned char *vaddr;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 		data = dregs->fifo_data;
5971da177e4SLinus Torvalds 		vaddr = (unsigned char *)dvma_btov(sun3_dma_orig_addr);
5981da177e4SLinus Torvalds 
5991da177e4SLinus Torvalds 		vaddr += (sun3_dma_orig_count - fifo);
6001da177e4SLinus Torvalds 
6011da177e4SLinus Torvalds 		vaddr[-2] = (data & 0xff00) >> 8;
6021da177e4SLinus Torvalds 		vaddr[-1] = (data & 0xff);
6031da177e4SLinus Torvalds 	}
6041da177e4SLinus Torvalds 
6051da177e4SLinus Torvalds 	dvma_unmap(sun3_dma_orig_addr);
6061da177e4SLinus Torvalds 	sun3_dma_orig_addr = NULL;
6071da177e4SLinus Torvalds #endif
6081da177e4SLinus Torvalds 	sun3_udc_write(UDC_RESET, UDC_CSR);
6091da177e4SLinus Torvalds 	dregs->fifo_count = 0;
6101da177e4SLinus Torvalds 	dregs->csr &= ~CSR_SEND;
6111da177e4SLinus Torvalds 
6121da177e4SLinus Torvalds 	/* reset fifo */
6131da177e4SLinus Torvalds 	dregs->csr &= ~CSR_FIFO;
6141da177e4SLinus Torvalds 	dregs->csr |= CSR_FIFO;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	sun3_dma_setup_done = NULL;
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	return ret;
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds }
6211da177e4SLinus Torvalds 
6221da177e4SLinus Torvalds #include "sun3_NCR5380.c"
6231da177e4SLinus Torvalds 
624d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
6251da177e4SLinus Torvalds 	.name			= SUN3_SCSI_NAME,
6261da177e4SLinus Torvalds 	.detect			= sun3scsi_detect,
6271da177e4SLinus Torvalds 	.release		= sun3scsi_release,
6281da177e4SLinus Torvalds 	.info			= sun3scsi_info,
6291da177e4SLinus Torvalds 	.queuecommand		= sun3scsi_queue_command,
6301da177e4SLinus Torvalds 	.eh_abort_handler      	= sun3scsi_abort,
6311da177e4SLinus Torvalds 	.eh_bus_reset_handler  	= sun3scsi_bus_reset,
6321da177e4SLinus Torvalds 	.can_queue		= CAN_QUEUE,
6331da177e4SLinus Torvalds 	.this_id		= 7,
6341da177e4SLinus Torvalds 	.sg_tablesize		= SG_TABLESIZE,
6351da177e4SLinus Torvalds 	.cmd_per_lun		= CMD_PER_LUN,
6361da177e4SLinus Torvalds 	.use_clustering		= DISABLE_CLUSTERING
6371da177e4SLinus Torvalds };
6381da177e4SLinus Torvalds 
6391da177e4SLinus Torvalds 
6401da177e4SLinus Torvalds #include "scsi_module.c"
6411da177e4SLinus Torvalds 
6421da177e4SLinus Torvalds MODULE_LICENSE("GPL");
643