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 28*9387edbeSAdrian Bunk static int a3000_release(struct Scsi_Host *instance); 29*9387edbeSAdrian Bunk 307d12e780SDavid Howells static irqreturn_t a3000_intr (int irq, void *dummy) 311da177e4SLinus Torvalds { 321da177e4SLinus Torvalds unsigned long flags; 331da177e4SLinus Torvalds unsigned int status = DMA(a3000_host)->ISTR; 341da177e4SLinus Torvalds 351da177e4SLinus Torvalds if (!(status & ISTR_INT_P)) 361da177e4SLinus Torvalds return IRQ_NONE; 371da177e4SLinus Torvalds if (status & ISTR_INTS) 381da177e4SLinus Torvalds { 391da177e4SLinus Torvalds spin_lock_irqsave(a3000_host->host_lock, flags); 401da177e4SLinus Torvalds wd33c93_intr (a3000_host); 411da177e4SLinus Torvalds spin_unlock_irqrestore(a3000_host->host_lock, flags); 421da177e4SLinus Torvalds return IRQ_HANDLED; 431da177e4SLinus Torvalds } 441da177e4SLinus Torvalds printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status); 451da177e4SLinus Torvalds return IRQ_NONE; 461da177e4SLinus Torvalds } 471da177e4SLinus Torvalds 4865396410SHenrik Kretzschmar static int dma_setup(struct scsi_cmnd *cmd, int dir_in) 491da177e4SLinus Torvalds { 501da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD | CNTR_INTEN; 511da177e4SLinus Torvalds unsigned long addr = virt_to_bus(cmd->SCp.ptr); 521da177e4SLinus Torvalds 531da177e4SLinus Torvalds /* 541da177e4SLinus Torvalds * if the physical address has the wrong alignment, or if 551da177e4SLinus Torvalds * physical address is bad, or if it is a write and at the 561da177e4SLinus Torvalds * end of a physical memory chunk, then allocate a bounce 571da177e4SLinus Torvalds * buffer 581da177e4SLinus Torvalds */ 597b892806SAdrian Bunk if (addr & A3000_XFER_MASK) 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 memcpy (HDATA(a3000_host)->dma_bounce_buffer, 751da177e4SLinus Torvalds cmd->SCp.ptr, cmd->SCp.this_residual); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 781da177e4SLinus Torvalds addr = virt_to_bus(HDATA(a3000_host)->dma_bounce_buffer); 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 811da177e4SLinus Torvalds /* setup dma direction */ 821da177e4SLinus Torvalds if (!dir_in) 831da177e4SLinus Torvalds cntr |= CNTR_DDIR; 841da177e4SLinus Torvalds 851da177e4SLinus Torvalds /* remember direction */ 861da177e4SLinus Torvalds HDATA(a3000_host)->dma_dir = dir_in; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds DMA(a3000_host)->CNTR = cntr; 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* setup DMA *physical* address */ 911da177e4SLinus Torvalds DMA(a3000_host)->ACR = addr; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds if (dir_in) 941da177e4SLinus Torvalds /* invalidate any cache */ 951da177e4SLinus Torvalds cache_clear (addr, cmd->SCp.this_residual); 961da177e4SLinus Torvalds else 971da177e4SLinus Torvalds /* push any dirty cache */ 981da177e4SLinus Torvalds cache_push (addr, cmd->SCp.this_residual); 991da177e4SLinus Torvalds 1001da177e4SLinus Torvalds /* start DMA */ 1011da177e4SLinus Torvalds mb(); /* make sure setup is completed */ 1021da177e4SLinus Torvalds DMA(a3000_host)->ST_DMA = 1; 1031da177e4SLinus Torvalds mb(); /* make sure DMA has started before next IO */ 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* return success */ 1061da177e4SLinus Torvalds return 0; 1071da177e4SLinus Torvalds } 1081da177e4SLinus Torvalds 10965396410SHenrik Kretzschmar static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, 1101da177e4SLinus Torvalds int status) 1111da177e4SLinus Torvalds { 1121da177e4SLinus Torvalds /* disable SCSI interrupts */ 1131da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD; 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds if (!HDATA(instance)->dma_dir) 1161da177e4SLinus Torvalds cntr |= CNTR_DDIR; 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds DMA(instance)->CNTR = cntr; 1191da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1201da177e4SLinus Torvalds 1211da177e4SLinus Torvalds /* flush if we were reading */ 1221da177e4SLinus Torvalds if (HDATA(instance)->dma_dir) { 1231da177e4SLinus Torvalds DMA(instance)->FLUSH = 1; 1241da177e4SLinus Torvalds mb(); /* don't allow prefetch */ 1251da177e4SLinus Torvalds while (!(DMA(instance)->ISTR & ISTR_FE_FLG)) 1261da177e4SLinus Torvalds barrier(); 1271da177e4SLinus Torvalds mb(); /* no IO until FLUSH is done */ 1281da177e4SLinus Torvalds } 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds /* clear a possible interrupt */ 1311da177e4SLinus Torvalds /* I think that this CINT is only necessary if you are 1321da177e4SLinus Torvalds * using the terminal count features. HM 7 Mar 1994 1331da177e4SLinus Torvalds */ 1341da177e4SLinus Torvalds DMA(instance)->CINT = 1; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds /* stop DMA */ 1371da177e4SLinus Torvalds DMA(instance)->SP_DMA = 1; 1381da177e4SLinus Torvalds mb(); /* make sure DMA is stopped before next IO */ 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* restore the CONTROL bits (minus the direction flag) */ 1411da177e4SLinus Torvalds DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN; 1421da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* copy from a bounce buffer, if necessary */ 1451da177e4SLinus Torvalds if (status && HDATA(instance)->dma_bounce_buffer) { 146cc0455faSBoaz Harrosh if (SCpnt) { 1471da177e4SLinus Torvalds if (HDATA(instance)->dma_dir && SCpnt) 1481da177e4SLinus Torvalds memcpy (SCpnt->SCp.ptr, 1491da177e4SLinus Torvalds HDATA(instance)->dma_bounce_buffer, 1501da177e4SLinus Torvalds SCpnt->SCp.this_residual); 1511da177e4SLinus Torvalds kfree (HDATA(instance)->dma_bounce_buffer); 1521da177e4SLinus Torvalds HDATA(instance)->dma_bounce_buffer = NULL; 1531da177e4SLinus Torvalds HDATA(instance)->dma_bounce_len = 0; 1541da177e4SLinus Torvalds } else { 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 } 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 162*9387edbeSAdrian Bunk static int __init a3000_detect(struct scsi_host_template *tpnt) 1631da177e4SLinus Torvalds { 1641da177e4SLinus Torvalds wd33c93_regs regs; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI)) 1671da177e4SLinus Torvalds return 0; 1681da177e4SLinus Torvalds if (!request_mem_region(0xDD0000, 256, "wd33c93")) 1691da177e4SLinus Torvalds return 0; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds tpnt->proc_name = "A3000"; 1721da177e4SLinus Torvalds tpnt->proc_info = &wd33c93_proc_info; 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds a3000_host = scsi_register (tpnt, sizeof(struct WD33C93_hostdata)); 1751da177e4SLinus Torvalds if (a3000_host == NULL) 1761da177e4SLinus Torvalds goto fail_register; 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds a3000_host->base = ZTWO_VADDR(0xDD0000); 1791da177e4SLinus Torvalds a3000_host->irq = IRQ_AMIGA_PORTS; 1801da177e4SLinus Torvalds DMA(a3000_host)->DAWR = DAWR_A3000; 1811da177e4SLinus Torvalds regs.SASR = &(DMA(a3000_host)->SASR); 1821da177e4SLinus Torvalds regs.SCMD = &(DMA(a3000_host)->SCMD); 183be3cb3d8SThomas Bogendoerfer HDATA(a3000_host)->no_sync = 0xff; 184be3cb3d8SThomas Bogendoerfer HDATA(a3000_host)->fast = 0; 185be3cb3d8SThomas Bogendoerfer HDATA(a3000_host)->dma_mode = CTRL_DMA; 1861da177e4SLinus Torvalds wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15); 1871d6f359aSThomas Gleixner if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI", 1881da177e4SLinus Torvalds a3000_intr)) 1891da177e4SLinus Torvalds goto fail_irq; 1901da177e4SLinus Torvalds DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN; 1911da177e4SLinus Torvalds 1921da177e4SLinus Torvalds return 1; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds fail_irq: 1951da177e4SLinus Torvalds wd33c93_release(); 1961da177e4SLinus Torvalds scsi_unregister(a3000_host); 1971da177e4SLinus Torvalds fail_register: 1981da177e4SLinus Torvalds release_mem_region(0xDD0000, 256); 1991da177e4SLinus Torvalds return 0; 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 20265396410SHenrik Kretzschmar static int a3000_bus_reset(struct scsi_cmnd *cmd) 2031da177e4SLinus Torvalds { 2041da177e4SLinus Torvalds /* FIXME perform bus-specific reset */ 205df0ae249SJeff Garzik 206df0ae249SJeff Garzik /* FIXME 2: kill this entire function, which should 207df0ae249SJeff Garzik cause mid-layer to call wd33c93_host_reset anyway? */ 20868b3aa7cSJeff Garzik 20968b3aa7cSJeff Garzik spin_lock_irq(cmd->device->host->host_lock); 2101da177e4SLinus Torvalds wd33c93_host_reset(cmd); 21168b3aa7cSJeff Garzik spin_unlock_irq(cmd->device->host->host_lock); 21268b3aa7cSJeff Garzik 2131da177e4SLinus Torvalds return SUCCESS; 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds 2161da177e4SLinus Torvalds #define HOSTS_C 2171da177e4SLinus Torvalds 218d0be4a7dSChristoph Hellwig static struct scsi_host_template driver_template = { 2191da177e4SLinus Torvalds .proc_name = "A3000", 2201da177e4SLinus Torvalds .name = "Amiga 3000 built-in SCSI", 2211da177e4SLinus Torvalds .detect = a3000_detect, 2221da177e4SLinus Torvalds .release = a3000_release, 2231da177e4SLinus Torvalds .queuecommand = wd33c93_queuecommand, 2241da177e4SLinus Torvalds .eh_abort_handler = wd33c93_abort, 2251da177e4SLinus Torvalds .eh_bus_reset_handler = a3000_bus_reset, 2261da177e4SLinus Torvalds .eh_host_reset_handler = wd33c93_host_reset, 2271da177e4SLinus Torvalds .can_queue = CAN_QUEUE, 2281da177e4SLinus Torvalds .this_id = 7, 2291da177e4SLinus Torvalds .sg_tablesize = SG_ALL, 2301da177e4SLinus Torvalds .cmd_per_lun = CMD_PER_LUN, 2311da177e4SLinus Torvalds .use_clustering = ENABLE_CLUSTERING 2321da177e4SLinus Torvalds }; 2331da177e4SLinus Torvalds 2341da177e4SLinus Torvalds 2351da177e4SLinus Torvalds #include "scsi_module.c" 2361da177e4SLinus Torvalds 237*9387edbeSAdrian Bunk static int a3000_release(struct Scsi_Host *instance) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds wd33c93_release(); 2401da177e4SLinus Torvalds DMA(instance)->CNTR = 0; 2411da177e4SLinus Torvalds release_mem_region(0xDD0000, 256); 2421da177e4SLinus Torvalds free_irq(IRQ_AMIGA_PORTS, a3000_intr); 2431da177e4SLinus Torvalds return 1; 2441da177e4SLinus Torvalds } 2451da177e4SLinus Torvalds 2461da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 247