xref: /openbmc/linux/drivers/scsi/a3000.c (revision 65396410af63db90d6428c678ff84aa652c3c1ec)
11da177e4SLinus Torvalds #include <linux/types.h>
21da177e4SLinus Torvalds #include <linux/mm.h>
31da177e4SLinus Torvalds #include <linux/blkdev.h>
41da177e4SLinus Torvalds #include <linux/sched.h>
51da177e4SLinus Torvalds #include <linux/ioport.h>
61da177e4SLinus Torvalds #include <linux/init.h>
71da177e4SLinus Torvalds #include <linux/spinlock.h>
81da177e4SLinus Torvalds #include <linux/interrupt.h>
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds #include <asm/setup.h>
111da177e4SLinus Torvalds #include <asm/page.h>
121da177e4SLinus Torvalds #include <asm/pgtable.h>
131da177e4SLinus Torvalds #include <asm/amigaints.h>
141da177e4SLinus Torvalds #include <asm/amigahw.h>
151da177e4SLinus Torvalds #include <asm/irq.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #include "scsi.h"
181da177e4SLinus Torvalds #include <scsi/scsi_host.h>
191da177e4SLinus Torvalds #include "wd33c93.h"
201da177e4SLinus Torvalds #include "a3000.h"
211da177e4SLinus Torvalds 
221da177e4SLinus Torvalds #include<linux/stat.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define DMA(ptr) ((a3000_scsiregs *)((ptr)->base))
251da177e4SLinus Torvalds #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata))
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds static struct Scsi_Host *a3000_host = NULL;
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds static irqreturn_t a3000_intr (int irq, void *dummy, struct pt_regs *fp)
301da177e4SLinus Torvalds {
311da177e4SLinus Torvalds 	unsigned long flags;
321da177e4SLinus Torvalds 	unsigned int status = DMA(a3000_host)->ISTR;
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds 	if (!(status & ISTR_INT_P))
351da177e4SLinus Torvalds 		return IRQ_NONE;
361da177e4SLinus Torvalds 	if (status & ISTR_INTS)
371da177e4SLinus Torvalds 	{
381da177e4SLinus Torvalds 		spin_lock_irqsave(a3000_host->host_lock, flags);
391da177e4SLinus Torvalds 		wd33c93_intr (a3000_host);
401da177e4SLinus Torvalds 		spin_unlock_irqrestore(a3000_host->host_lock, flags);
411da177e4SLinus Torvalds 		return IRQ_HANDLED;
421da177e4SLinus Torvalds 	}
431da177e4SLinus Torvalds 	printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
441da177e4SLinus Torvalds 	return IRQ_NONE;
451da177e4SLinus Torvalds }
461da177e4SLinus Torvalds 
47*65396410SHenrik Kretzschmar static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
481da177e4SLinus Torvalds {
491da177e4SLinus Torvalds     unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
501da177e4SLinus Torvalds     unsigned long addr = virt_to_bus(cmd->SCp.ptr);
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds     /*
531da177e4SLinus Torvalds      * if the physical address has the wrong alignment, or if
541da177e4SLinus Torvalds      * physical address is bad, or if it is a write and at the
551da177e4SLinus Torvalds      * end of a physical memory chunk, then allocate a bounce
561da177e4SLinus Torvalds      * buffer
571da177e4SLinus Torvalds      */
581da177e4SLinus Torvalds     if (addr & A3000_XFER_MASK ||
591da177e4SLinus Torvalds 	(!dir_in && mm_end_of_chunk (addr, cmd->SCp.this_residual)))
601da177e4SLinus Torvalds     {
611da177e4SLinus Torvalds 	HDATA(a3000_host)->dma_bounce_len = (cmd->SCp.this_residual + 511)
621da177e4SLinus Torvalds 	    & ~0x1ff;
631da177e4SLinus Torvalds 	HDATA(a3000_host)->dma_bounce_buffer =
641da177e4SLinus Torvalds 	    kmalloc (HDATA(a3000_host)->dma_bounce_len, GFP_KERNEL);
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	/* can't allocate memory; use PIO */
671da177e4SLinus Torvalds 	if (!HDATA(a3000_host)->dma_bounce_buffer) {
681da177e4SLinus Torvalds 	    HDATA(a3000_host)->dma_bounce_len = 0;
691da177e4SLinus Torvalds 	    return 1;
701da177e4SLinus Torvalds 	}
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds 	if (!dir_in) {
731da177e4SLinus Torvalds 	    /* copy to bounce buffer for a write */
741da177e4SLinus Torvalds 	    if (cmd->use_sg) {
751da177e4SLinus Torvalds 		memcpy (HDATA(a3000_host)->dma_bounce_buffer,
761da177e4SLinus Torvalds 			cmd->SCp.ptr, cmd->SCp.this_residual);
771da177e4SLinus Torvalds 	    } else
781da177e4SLinus Torvalds 		memcpy (HDATA(a3000_host)->dma_bounce_buffer,
791da177e4SLinus Torvalds 			cmd->request_buffer, cmd->request_bufflen);
801da177e4SLinus Torvalds 	}
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	addr = virt_to_bus(HDATA(a3000_host)->dma_bounce_buffer);
831da177e4SLinus Torvalds     }
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds     /* setup dma direction */
861da177e4SLinus Torvalds     if (!dir_in)
871da177e4SLinus Torvalds 	cntr |= CNTR_DDIR;
881da177e4SLinus Torvalds 
891da177e4SLinus Torvalds     /* remember direction */
901da177e4SLinus Torvalds     HDATA(a3000_host)->dma_dir = dir_in;
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds     DMA(a3000_host)->CNTR = cntr;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds     /* setup DMA *physical* address */
951da177e4SLinus Torvalds     DMA(a3000_host)->ACR = addr;
961da177e4SLinus Torvalds 
971da177e4SLinus Torvalds     if (dir_in)
981da177e4SLinus Torvalds   	/* invalidate any cache */
991da177e4SLinus Torvalds 	cache_clear (addr, cmd->SCp.this_residual);
1001da177e4SLinus Torvalds     else
1011da177e4SLinus Torvalds 	/* push any dirty cache */
1021da177e4SLinus Torvalds 	cache_push (addr, cmd->SCp.this_residual);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds     /* start DMA */
1051da177e4SLinus Torvalds     mb();			/* make sure setup is completed */
1061da177e4SLinus Torvalds     DMA(a3000_host)->ST_DMA = 1;
1071da177e4SLinus Torvalds     mb();			/* make sure DMA has started before next IO */
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds     /* return success */
1101da177e4SLinus Torvalds     return 0;
1111da177e4SLinus Torvalds }
1121da177e4SLinus Torvalds 
113*65396410SHenrik Kretzschmar static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
1141da177e4SLinus Torvalds 		     int status)
1151da177e4SLinus Torvalds {
1161da177e4SLinus Torvalds     /* disable SCSI interrupts */
1171da177e4SLinus Torvalds     unsigned short cntr = CNTR_PDMD;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds     if (!HDATA(instance)->dma_dir)
1201da177e4SLinus Torvalds 	cntr |= CNTR_DDIR;
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds     DMA(instance)->CNTR = cntr;
1231da177e4SLinus Torvalds     mb();			/* make sure CNTR is updated before next IO */
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds     /* flush if we were reading */
1261da177e4SLinus Torvalds     if (HDATA(instance)->dma_dir) {
1271da177e4SLinus Torvalds 	DMA(instance)->FLUSH = 1;
1281da177e4SLinus Torvalds 	mb();			/* don't allow prefetch */
1291da177e4SLinus Torvalds 	while (!(DMA(instance)->ISTR & ISTR_FE_FLG))
1301da177e4SLinus Torvalds 	    barrier();
1311da177e4SLinus Torvalds 	mb();			/* no IO until FLUSH is done */
1321da177e4SLinus Torvalds     }
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds     /* clear a possible interrupt */
1351da177e4SLinus Torvalds     /* I think that this CINT is only necessary if you are
1361da177e4SLinus Torvalds      * using the terminal count features.   HM 7 Mar 1994
1371da177e4SLinus Torvalds      */
1381da177e4SLinus Torvalds     DMA(instance)->CINT = 1;
1391da177e4SLinus Torvalds 
1401da177e4SLinus Torvalds     /* stop DMA */
1411da177e4SLinus Torvalds     DMA(instance)->SP_DMA = 1;
1421da177e4SLinus Torvalds     mb();			/* make sure DMA is stopped before next IO */
1431da177e4SLinus Torvalds 
1441da177e4SLinus Torvalds     /* restore the CONTROL bits (minus the direction flag) */
1451da177e4SLinus Torvalds     DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN;
1461da177e4SLinus Torvalds     mb();			/* make sure CNTR is updated before next IO */
1471da177e4SLinus Torvalds 
1481da177e4SLinus Torvalds     /* copy from a bounce buffer, if necessary */
1491da177e4SLinus Torvalds     if (status && HDATA(instance)->dma_bounce_buffer) {
1501da177e4SLinus Torvalds 	if (SCpnt && SCpnt->use_sg) {
1511da177e4SLinus Torvalds 	    if (HDATA(instance)->dma_dir && SCpnt)
1521da177e4SLinus Torvalds 		memcpy (SCpnt->SCp.ptr,
1531da177e4SLinus Torvalds 			HDATA(instance)->dma_bounce_buffer,
1541da177e4SLinus Torvalds 			SCpnt->SCp.this_residual);
1551da177e4SLinus Torvalds 	    kfree (HDATA(instance)->dma_bounce_buffer);
1561da177e4SLinus Torvalds 	    HDATA(instance)->dma_bounce_buffer = NULL;
1571da177e4SLinus Torvalds 	    HDATA(instance)->dma_bounce_len = 0;
1581da177e4SLinus Torvalds 	} else {
1591da177e4SLinus Torvalds 	    if (HDATA(instance)->dma_dir && SCpnt)
1601da177e4SLinus Torvalds 		memcpy (SCpnt->request_buffer,
1611da177e4SLinus Torvalds 			HDATA(instance)->dma_bounce_buffer,
1621da177e4SLinus Torvalds 			SCpnt->request_bufflen);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	    kfree (HDATA(instance)->dma_bounce_buffer);
1651da177e4SLinus Torvalds 	    HDATA(instance)->dma_bounce_buffer = NULL;
1661da177e4SLinus Torvalds 	    HDATA(instance)->dma_bounce_len = 0;
1671da177e4SLinus Torvalds 	}
1681da177e4SLinus Torvalds     }
1691da177e4SLinus Torvalds }
1701da177e4SLinus Torvalds 
171d0be4a7dSChristoph Hellwig int __init a3000_detect(struct scsi_host_template *tpnt)
1721da177e4SLinus Torvalds {
1731da177e4SLinus Torvalds     wd33c93_regs regs;
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds     if  (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI))
1761da177e4SLinus Torvalds 	return 0;
1771da177e4SLinus Torvalds     if (!request_mem_region(0xDD0000, 256, "wd33c93"))
1781da177e4SLinus Torvalds 	return 0;
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds     tpnt->proc_name = "A3000";
1811da177e4SLinus Torvalds     tpnt->proc_info = &wd33c93_proc_info;
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds     a3000_host = scsi_register (tpnt, sizeof(struct WD33C93_hostdata));
1841da177e4SLinus Torvalds     if (a3000_host == NULL)
1851da177e4SLinus Torvalds 	goto fail_register;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds     a3000_host->base = ZTWO_VADDR(0xDD0000);
1881da177e4SLinus Torvalds     a3000_host->irq = IRQ_AMIGA_PORTS;
1891da177e4SLinus Torvalds     DMA(a3000_host)->DAWR = DAWR_A3000;
1901da177e4SLinus Torvalds     regs.SASR = &(DMA(a3000_host)->SASR);
1911da177e4SLinus Torvalds     regs.SCMD = &(DMA(a3000_host)->SCMD);
1921da177e4SLinus Torvalds     wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15);
1931d6f359aSThomas Gleixner     if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI",
1941da177e4SLinus Torvalds 		    a3000_intr))
1951da177e4SLinus Torvalds         goto fail_irq;
1961da177e4SLinus Torvalds     DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN;
1971da177e4SLinus Torvalds 
1981da177e4SLinus Torvalds     return 1;
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds fail_irq:
2011da177e4SLinus Torvalds     wd33c93_release();
2021da177e4SLinus Torvalds     scsi_unregister(a3000_host);
2031da177e4SLinus Torvalds fail_register:
2041da177e4SLinus Torvalds     release_mem_region(0xDD0000, 256);
2051da177e4SLinus Torvalds     return 0;
2061da177e4SLinus Torvalds }
2071da177e4SLinus Torvalds 
208*65396410SHenrik Kretzschmar static int a3000_bus_reset(struct scsi_cmnd *cmd)
2091da177e4SLinus Torvalds {
2101da177e4SLinus Torvalds 	/* FIXME perform bus-specific reset */
211df0ae249SJeff Garzik  
212df0ae249SJeff Garzik  	/* FIXME 2: kill this entire function, which should
213df0ae249SJeff Garzik  	   cause mid-layer to call wd33c93_host_reset anyway? */
21468b3aa7cSJeff Garzik  
21568b3aa7cSJeff Garzik  	spin_lock_irq(cmd->device->host->host_lock);
2161da177e4SLinus Torvalds 	wd33c93_host_reset(cmd);
21768b3aa7cSJeff Garzik  	spin_unlock_irq(cmd->device->host->host_lock);
21868b3aa7cSJeff Garzik  
2191da177e4SLinus Torvalds 	return SUCCESS;
2201da177e4SLinus Torvalds }
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds #define HOSTS_C
2231da177e4SLinus Torvalds 
224d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = {
2251da177e4SLinus Torvalds 	.proc_name		= "A3000",
2261da177e4SLinus Torvalds 	.name			= "Amiga 3000 built-in SCSI",
2271da177e4SLinus Torvalds 	.detect			= a3000_detect,
2281da177e4SLinus Torvalds 	.release		= a3000_release,
2291da177e4SLinus Torvalds 	.queuecommand		= wd33c93_queuecommand,
2301da177e4SLinus Torvalds 	.eh_abort_handler	= wd33c93_abort,
2311da177e4SLinus Torvalds 	.eh_bus_reset_handler	= a3000_bus_reset,
2321da177e4SLinus Torvalds 	.eh_host_reset_handler	= wd33c93_host_reset,
2331da177e4SLinus Torvalds 	.can_queue		= CAN_QUEUE,
2341da177e4SLinus Torvalds 	.this_id		= 7,
2351da177e4SLinus Torvalds 	.sg_tablesize		= SG_ALL,
2361da177e4SLinus Torvalds 	.cmd_per_lun		= CMD_PER_LUN,
2371da177e4SLinus Torvalds 	.use_clustering		= ENABLE_CLUSTERING
2381da177e4SLinus Torvalds };
2391da177e4SLinus Torvalds 
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds #include "scsi_module.c"
2421da177e4SLinus Torvalds 
2431da177e4SLinus Torvalds int a3000_release(struct Scsi_Host *instance)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds     wd33c93_release();
2461da177e4SLinus Torvalds     DMA(instance)->CNTR = 0;
2471da177e4SLinus Torvalds     release_mem_region(0xDD0000, 256);
2481da177e4SLinus Torvalds     free_irq(IRQ_AMIGA_PORTS, a3000_intr);
2491da177e4SLinus Torvalds     return 1;
2501da177e4SLinus Torvalds }
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds MODULE_LICENSE("GPL");
253