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