11da177e4SLinus Torvalds #include <linux/types.h> 21da177e4SLinus Torvalds #include <linux/mm.h> 35a0e3ad6STejun Heo #include <linux/slab.h> 41da177e4SLinus Torvalds #include <linux/blkdev.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 24*21351013SGeert Uytterhoeven 251da177e4SLinus Torvalds #define DMA(ptr) ((a3000_scsiregs *)((ptr)->base)) 261da177e4SLinus Torvalds #define HDATA(ptr) ((struct WD33C93_hostdata *)((ptr)->hostdata)) 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds static struct Scsi_Host *a3000_host = NULL; 291da177e4SLinus Torvalds 309387edbeSAdrian Bunk static int a3000_release(struct Scsi_Host *instance); 319387edbeSAdrian Bunk 327d12e780SDavid Howells static irqreturn_t a3000_intr(int irq, void *dummy) 331da177e4SLinus Torvalds { 341da177e4SLinus Torvalds unsigned long flags; 351da177e4SLinus Torvalds unsigned int status = DMA(a3000_host)->ISTR; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds if (!(status & ISTR_INT_P)) 381da177e4SLinus Torvalds return IRQ_NONE; 39*21351013SGeert Uytterhoeven if (status & ISTR_INTS) { 401da177e4SLinus Torvalds spin_lock_irqsave(a3000_host->host_lock, flags); 411da177e4SLinus Torvalds wd33c93_intr(a3000_host); 421da177e4SLinus Torvalds spin_unlock_irqrestore(a3000_host->host_lock, flags); 431da177e4SLinus Torvalds return IRQ_HANDLED; 441da177e4SLinus Torvalds } 451da177e4SLinus Torvalds printk("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status); 461da177e4SLinus Torvalds return IRQ_NONE; 471da177e4SLinus Torvalds } 481da177e4SLinus Torvalds 4965396410SHenrik Kretzschmar static int dma_setup(struct scsi_cmnd *cmd, int dir_in) 501da177e4SLinus Torvalds { 511da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD | CNTR_INTEN; 521da177e4SLinus Torvalds unsigned long addr = virt_to_bus(cmd->SCp.ptr); 531da177e4SLinus Torvalds 541da177e4SLinus Torvalds /* 551da177e4SLinus Torvalds * if the physical address has the wrong alignment, or if 561da177e4SLinus Torvalds * physical address is bad, or if it is a write and at the 571da177e4SLinus Torvalds * end of a physical memory chunk, then allocate a bounce 581da177e4SLinus Torvalds * buffer 591da177e4SLinus Torvalds */ 60*21351013SGeert Uytterhoeven if (addr & A3000_XFER_MASK) { 61*21351013SGeert Uytterhoeven HDATA(a3000_host)->dma_bounce_len = 62*21351013SGeert Uytterhoeven (cmd->SCp.this_residual + 511) & ~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 93*21351013SGeert Uytterhoeven if (dir_in) { 941da177e4SLinus Torvalds /* invalidate any cache */ 951da177e4SLinus Torvalds cache_clear(addr, cmd->SCp.this_residual); 96*21351013SGeert Uytterhoeven } else { 971da177e4SLinus Torvalds /* push any dirty cache */ 981da177e4SLinus Torvalds cache_push(addr, cmd->SCp.this_residual); 99*21351013SGeert Uytterhoeven } 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* start DMA */ 1021da177e4SLinus Torvalds mb(); /* make sure setup is completed */ 1031da177e4SLinus Torvalds DMA(a3000_host)->ST_DMA = 1; 1041da177e4SLinus Torvalds mb(); /* make sure DMA has started before next IO */ 1051da177e4SLinus Torvalds 1061da177e4SLinus Torvalds /* return success */ 1071da177e4SLinus Torvalds return 0; 1081da177e4SLinus Torvalds } 1091da177e4SLinus Torvalds 11065396410SHenrik Kretzschmar static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, 1111da177e4SLinus Torvalds int status) 1121da177e4SLinus Torvalds { 1131da177e4SLinus Torvalds /* disable SCSI interrupts */ 1141da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD; 1151da177e4SLinus Torvalds 1161da177e4SLinus Torvalds if (!HDATA(instance)->dma_dir) 1171da177e4SLinus Torvalds cntr |= CNTR_DDIR; 1181da177e4SLinus Torvalds 1191da177e4SLinus Torvalds DMA(instance)->CNTR = cntr; 1201da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1211da177e4SLinus Torvalds 1221da177e4SLinus Torvalds /* flush if we were reading */ 1231da177e4SLinus Torvalds if (HDATA(instance)->dma_dir) { 1241da177e4SLinus Torvalds DMA(instance)->FLUSH = 1; 1251da177e4SLinus Torvalds mb(); /* don't allow prefetch */ 1261da177e4SLinus Torvalds while (!(DMA(instance)->ISTR & ISTR_FE_FLG)) 1271da177e4SLinus Torvalds barrier(); 1281da177e4SLinus Torvalds mb(); /* no IO until FLUSH is done */ 1291da177e4SLinus Torvalds } 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* clear a possible interrupt */ 1321da177e4SLinus Torvalds /* I think that this CINT is only necessary if you are 1331da177e4SLinus Torvalds * using the terminal count features. HM 7 Mar 1994 1341da177e4SLinus Torvalds */ 1351da177e4SLinus Torvalds DMA(instance)->CINT = 1; 1361da177e4SLinus Torvalds 1371da177e4SLinus Torvalds /* stop DMA */ 1381da177e4SLinus Torvalds DMA(instance)->SP_DMA = 1; 1391da177e4SLinus Torvalds mb(); /* make sure DMA is stopped before next IO */ 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* restore the CONTROL bits (minus the direction flag) */ 1421da177e4SLinus Torvalds DMA(instance)->CNTR = CNTR_PDMD | CNTR_INTEN; 1431da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* copy from a bounce buffer, if necessary */ 1461da177e4SLinus Torvalds if (status && HDATA(instance)->dma_bounce_buffer) { 147cc0455faSBoaz Harrosh if (SCpnt) { 1481da177e4SLinus Torvalds if (HDATA(instance)->dma_dir && SCpnt) 1491da177e4SLinus Torvalds memcpy(SCpnt->SCp.ptr, 1501da177e4SLinus Torvalds HDATA(instance)->dma_bounce_buffer, 1511da177e4SLinus Torvalds SCpnt->SCp.this_residual); 1521da177e4SLinus Torvalds kfree(HDATA(instance)->dma_bounce_buffer); 1531da177e4SLinus Torvalds HDATA(instance)->dma_bounce_buffer = NULL; 1541da177e4SLinus Torvalds HDATA(instance)->dma_bounce_len = 0; 1551da177e4SLinus Torvalds } else { 1561da177e4SLinus Torvalds kfree(HDATA(instance)->dma_bounce_buffer); 1571da177e4SLinus Torvalds HDATA(instance)->dma_bounce_buffer = NULL; 1581da177e4SLinus Torvalds HDATA(instance)->dma_bounce_len = 0; 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds } 1621da177e4SLinus Torvalds 1639387edbeSAdrian Bunk static int __init a3000_detect(struct scsi_host_template *tpnt) 1641da177e4SLinus Torvalds { 1651da177e4SLinus Torvalds wd33c93_regs regs; 1661da177e4SLinus Torvalds 1671da177e4SLinus Torvalds if (!MACH_IS_AMIGA || !AMIGAHW_PRESENT(A3000_SCSI)) 1681da177e4SLinus Torvalds return 0; 1691da177e4SLinus Torvalds if (!request_mem_region(0xDD0000, 256, "wd33c93")) 1701da177e4SLinus Torvalds return 0; 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds tpnt->proc_name = "A3000"; 1731da177e4SLinus Torvalds tpnt->proc_info = &wd33c93_proc_info; 1741da177e4SLinus Torvalds 1751da177e4SLinus Torvalds a3000_host = scsi_register(tpnt, sizeof(struct WD33C93_hostdata)); 1761da177e4SLinus Torvalds if (a3000_host == NULL) 1771da177e4SLinus Torvalds goto fail_register; 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds a3000_host->base = ZTWO_VADDR(0xDD0000); 1801da177e4SLinus Torvalds a3000_host->irq = IRQ_AMIGA_PORTS; 1811da177e4SLinus Torvalds DMA(a3000_host)->DAWR = DAWR_A3000; 1821da177e4SLinus Torvalds regs.SASR = &(DMA(a3000_host)->SASR); 1831da177e4SLinus Torvalds regs.SCMD = &(DMA(a3000_host)->SCMD); 184be3cb3d8SThomas Bogendoerfer HDATA(a3000_host)->no_sync = 0xff; 185be3cb3d8SThomas Bogendoerfer HDATA(a3000_host)->fast = 0; 186be3cb3d8SThomas Bogendoerfer HDATA(a3000_host)->dma_mode = CTRL_DMA; 1871da177e4SLinus Torvalds wd33c93_init(a3000_host, regs, dma_setup, dma_stop, WD33C93_FS_12_15); 1881d6f359aSThomas Gleixner if (request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, "A3000 SCSI", 1891da177e4SLinus Torvalds a3000_intr)) 1901da177e4SLinus Torvalds goto fail_irq; 1911da177e4SLinus Torvalds DMA(a3000_host)->CNTR = CNTR_PDMD | CNTR_INTEN; 1921da177e4SLinus Torvalds 1931da177e4SLinus Torvalds return 1; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds fail_irq: 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 2379387edbeSAdrian Bunk static int a3000_release(struct Scsi_Host *instance) 2381da177e4SLinus Torvalds { 2391da177e4SLinus Torvalds DMA(instance)->CNTR = 0; 2401da177e4SLinus Torvalds release_mem_region(0xDD0000, 256); 2411da177e4SLinus Torvalds free_irq(IRQ_AMIGA_PORTS, a3000_intr); 2421da177e4SLinus Torvalds return 1; 2431da177e4SLinus Torvalds } 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 246