109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only 21da177e4SLinus Torvalds #include <linux/types.h> 31da177e4SLinus Torvalds #include <linux/mm.h> 41da177e4SLinus Torvalds #include <linux/ioport.h> 51da177e4SLinus Torvalds #include <linux/init.h> 6c2a24a4cSGeert Uytterhoeven #include <linux/slab.h> 71da177e4SLinus Torvalds #include <linux/spinlock.h> 81da177e4SLinus Torvalds #include <linux/interrupt.h> 9c2a24a4cSGeert Uytterhoeven #include <linux/platform_device.h> 10acf3368fSPaul Gortmaker #include <linux/module.h> 111da177e4SLinus Torvalds 121da177e4SLinus Torvalds #include <asm/page.h> 131da177e4SLinus Torvalds #include <asm/pgtable.h> 141da177e4SLinus Torvalds #include <asm/amigaints.h> 151da177e4SLinus Torvalds #include <asm/amigahw.h> 161da177e4SLinus Torvalds 171da177e4SLinus Torvalds #include "scsi.h" 181da177e4SLinus Torvalds #include "wd33c93.h" 191da177e4SLinus Torvalds #include "a3000.h" 201da177e4SLinus Torvalds 219387edbeSAdrian Bunk 222b21d5e4SGeert Uytterhoeven struct a3000_hostdata { 232b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata wh; 242b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs; 252b21d5e4SGeert Uytterhoeven }; 262b21d5e4SGeert Uytterhoeven 27a8169e60SGeert Uytterhoeven static irqreturn_t a3000_intr(int irq, void *data) 281da177e4SLinus Torvalds { 29a8169e60SGeert Uytterhoeven struct Scsi_Host *instance = data; 302b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 312b21d5e4SGeert Uytterhoeven unsigned int status = hdata->regs->ISTR; 321da177e4SLinus Torvalds unsigned long flags; 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds if (!(status & ISTR_INT_P)) 351da177e4SLinus Torvalds return IRQ_NONE; 3621351013SGeert Uytterhoeven if (status & ISTR_INTS) { 37a8169e60SGeert Uytterhoeven spin_lock_irqsave(instance->host_lock, flags); 38a8169e60SGeert Uytterhoeven wd33c93_intr(instance); 39a8169e60SGeert Uytterhoeven spin_unlock_irqrestore(instance->host_lock, flags); 401da177e4SLinus Torvalds return IRQ_HANDLED; 411da177e4SLinus Torvalds } 42*a2cc701bSKefeng Wang pr_warn("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 { 48a8169e60SGeert Uytterhoeven struct Scsi_Host *instance = cmd->device->host; 492b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 502b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata *wh = &hdata->wh; 512b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs = hdata->regs; 521da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD | CNTR_INTEN; 531da177e4SLinus Torvalds unsigned long addr = virt_to_bus(cmd->SCp.ptr); 541da177e4SLinus Torvalds 551da177e4SLinus Torvalds /* 561da177e4SLinus Torvalds * if the physical address has the wrong alignment, or if 571da177e4SLinus Torvalds * physical address is bad, or if it is a write and at the 581da177e4SLinus Torvalds * end of a physical memory chunk, then allocate a bounce 591da177e4SLinus Torvalds * buffer 601da177e4SLinus Torvalds */ 6121351013SGeert Uytterhoeven if (addr & A3000_XFER_MASK) { 622b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff; 632b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len, 64afdbbc16SGeert Uytterhoeven GFP_KERNEL); 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* can't allocate memory; use PIO */ 672b21d5e4SGeert Uytterhoeven if (!wh->dma_bounce_buffer) { 682b21d5e4SGeert Uytterhoeven wh->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 */ 742b21d5e4SGeert Uytterhoeven memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr, 75afdbbc16SGeert Uytterhoeven cmd->SCp.this_residual); 761da177e4SLinus Torvalds } 771da177e4SLinus Torvalds 782b21d5e4SGeert Uytterhoeven addr = virt_to_bus(wh->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 */ 862b21d5e4SGeert Uytterhoeven wh->dma_dir = dir_in; 871da177e4SLinus Torvalds 88d753722eSGeert Uytterhoeven regs->CNTR = cntr; 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds /* setup DMA *physical* address */ 91d753722eSGeert Uytterhoeven regs->ACR = addr; 921da177e4SLinus Torvalds 9321351013SGeert Uytterhoeven if (dir_in) { 941da177e4SLinus Torvalds /* invalidate any cache */ 951da177e4SLinus Torvalds cache_clear(addr, cmd->SCp.this_residual); 9621351013SGeert Uytterhoeven } else { 971da177e4SLinus Torvalds /* push any dirty cache */ 981da177e4SLinus Torvalds cache_push(addr, cmd->SCp.this_residual); 9921351013SGeert Uytterhoeven } 1001da177e4SLinus Torvalds 1011da177e4SLinus Torvalds /* start DMA */ 1021da177e4SLinus Torvalds mb(); /* make sure setup is completed */ 103d753722eSGeert Uytterhoeven regs->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 { 1132b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 1142b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata *wh = &hdata->wh; 1152b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs = hdata->regs; 116afdbbc16SGeert Uytterhoeven 1171da177e4SLinus Torvalds /* disable SCSI interrupts */ 1181da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD; 1191da177e4SLinus Torvalds 1202b21d5e4SGeert Uytterhoeven if (!wh->dma_dir) 1211da177e4SLinus Torvalds cntr |= CNTR_DDIR; 1221da177e4SLinus Torvalds 123d753722eSGeert Uytterhoeven regs->CNTR = cntr; 1241da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1251da177e4SLinus Torvalds 1261da177e4SLinus Torvalds /* flush if we were reading */ 1272b21d5e4SGeert Uytterhoeven if (wh->dma_dir) { 128d753722eSGeert Uytterhoeven regs->FLUSH = 1; 1291da177e4SLinus Torvalds mb(); /* don't allow prefetch */ 130d753722eSGeert Uytterhoeven while (!(regs->ISTR & ISTR_FE_FLG)) 1311da177e4SLinus Torvalds barrier(); 1321da177e4SLinus Torvalds mb(); /* no IO until FLUSH is done */ 1331da177e4SLinus Torvalds } 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds /* clear a possible interrupt */ 1361da177e4SLinus Torvalds /* I think that this CINT is only necessary if you are 1371da177e4SLinus Torvalds * using the terminal count features. HM 7 Mar 1994 1381da177e4SLinus Torvalds */ 139d753722eSGeert Uytterhoeven regs->CINT = 1; 1401da177e4SLinus Torvalds 1411da177e4SLinus Torvalds /* stop DMA */ 142d753722eSGeert Uytterhoeven regs->SP_DMA = 1; 1431da177e4SLinus Torvalds mb(); /* make sure DMA is stopped before next IO */ 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds /* restore the CONTROL bits (minus the direction flag) */ 146d753722eSGeert Uytterhoeven regs->CNTR = CNTR_PDMD | CNTR_INTEN; 1471da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1481da177e4SLinus Torvalds 1491da177e4SLinus Torvalds /* copy from a bounce buffer, if necessary */ 1502b21d5e4SGeert Uytterhoeven if (status && wh->dma_bounce_buffer) { 151cc0455faSBoaz Harrosh if (SCpnt) { 1522b21d5e4SGeert Uytterhoeven if (wh->dma_dir && SCpnt) 1532b21d5e4SGeert Uytterhoeven memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer, 1541da177e4SLinus Torvalds SCpnt->SCp.this_residual); 1552b21d5e4SGeert Uytterhoeven kfree(wh->dma_bounce_buffer); 1562b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = NULL; 1572b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 1581da177e4SLinus Torvalds } else { 1592b21d5e4SGeert Uytterhoeven kfree(wh->dma_bounce_buffer); 1602b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = NULL; 1612b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 1621da177e4SLinus Torvalds } 1631da177e4SLinus Torvalds } 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 166c2a24a4cSGeert Uytterhoeven static struct scsi_host_template amiga_a3000_scsi_template = { 167c2a24a4cSGeert Uytterhoeven .module = THIS_MODULE, 1681da177e4SLinus Torvalds .name = "Amiga 3000 built-in SCSI", 169408bb25bSAl Viro .show_info = wd33c93_show_info, 170408bb25bSAl Viro .write_info = wd33c93_write_info, 171c2a24a4cSGeert Uytterhoeven .proc_name = "A3000", 1721da177e4SLinus Torvalds .queuecommand = wd33c93_queuecommand, 1731da177e4SLinus Torvalds .eh_abort_handler = wd33c93_abort, 1741da177e4SLinus Torvalds .eh_host_reset_handler = wd33c93_host_reset, 1751da177e4SLinus Torvalds .can_queue = CAN_QUEUE, 1761da177e4SLinus Torvalds .this_id = 7, 1771da177e4SLinus Torvalds .sg_tablesize = SG_ALL, 1781da177e4SLinus Torvalds .cmd_per_lun = CMD_PER_LUN, 1791da177e4SLinus Torvalds }; 1801da177e4SLinus Torvalds 181c2a24a4cSGeert Uytterhoeven static int __init amiga_a3000_scsi_probe(struct platform_device *pdev) 1821da177e4SLinus Torvalds { 183c2a24a4cSGeert Uytterhoeven struct resource *res; 184c2a24a4cSGeert Uytterhoeven struct Scsi_Host *instance; 185c2a24a4cSGeert Uytterhoeven int error; 186c2a24a4cSGeert Uytterhoeven struct a3000_scsiregs *regs; 187c2a24a4cSGeert Uytterhoeven wd33c93_regs wdregs; 1882b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata; 189d753722eSGeert Uytterhoeven 190c2a24a4cSGeert Uytterhoeven res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 191c2a24a4cSGeert Uytterhoeven if (!res) 192c2a24a4cSGeert Uytterhoeven return -ENODEV; 193c2a24a4cSGeert Uytterhoeven 194c2a24a4cSGeert Uytterhoeven if (!request_mem_region(res->start, resource_size(res), "wd33c93")) 195c2a24a4cSGeert Uytterhoeven return -EBUSY; 196c2a24a4cSGeert Uytterhoeven 197c2a24a4cSGeert Uytterhoeven instance = scsi_host_alloc(&amiga_a3000_scsi_template, 1982b21d5e4SGeert Uytterhoeven sizeof(struct a3000_hostdata)); 199c2a24a4cSGeert Uytterhoeven if (!instance) { 200c2a24a4cSGeert Uytterhoeven error = -ENOMEM; 201c2a24a4cSGeert Uytterhoeven goto fail_alloc; 2021da177e4SLinus Torvalds } 2031da177e4SLinus Torvalds 204c2a24a4cSGeert Uytterhoeven instance->irq = IRQ_AMIGA_PORTS; 205c2a24a4cSGeert Uytterhoeven 2066112ea08SGeert Uytterhoeven regs = ZTWO_VADDR(res->start); 207c2a24a4cSGeert Uytterhoeven regs->DAWR = DAWR_A3000; 208c2a24a4cSGeert Uytterhoeven 209c2a24a4cSGeert Uytterhoeven wdregs.SASR = ®s->SASR; 210c2a24a4cSGeert Uytterhoeven wdregs.SCMD = ®s->SCMD; 211c2a24a4cSGeert Uytterhoeven 212c2a24a4cSGeert Uytterhoeven hdata = shost_priv(instance); 2132b21d5e4SGeert Uytterhoeven hdata->wh.no_sync = 0xff; 2142b21d5e4SGeert Uytterhoeven hdata->wh.fast = 0; 2152b21d5e4SGeert Uytterhoeven hdata->wh.dma_mode = CTRL_DMA; 2162b21d5e4SGeert Uytterhoeven hdata->regs = regs; 217c2a24a4cSGeert Uytterhoeven 218c2a24a4cSGeert Uytterhoeven wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15); 219c2a24a4cSGeert Uytterhoeven error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, 220c2a24a4cSGeert Uytterhoeven "A3000 SCSI", instance); 221c2a24a4cSGeert Uytterhoeven if (error) 222c2a24a4cSGeert Uytterhoeven goto fail_irq; 223c2a24a4cSGeert Uytterhoeven 224c2a24a4cSGeert Uytterhoeven regs->CNTR = CNTR_PDMD | CNTR_INTEN; 225c2a24a4cSGeert Uytterhoeven 226c2a24a4cSGeert Uytterhoeven error = scsi_add_host(instance, NULL); 227c2a24a4cSGeert Uytterhoeven if (error) 228c2a24a4cSGeert Uytterhoeven goto fail_host; 229c2a24a4cSGeert Uytterhoeven 230c2a24a4cSGeert Uytterhoeven platform_set_drvdata(pdev, instance); 231c2a24a4cSGeert Uytterhoeven 232c2a24a4cSGeert Uytterhoeven scsi_scan_host(instance); 233c2a24a4cSGeert Uytterhoeven return 0; 234c2a24a4cSGeert Uytterhoeven 235c2a24a4cSGeert Uytterhoeven fail_host: 236c2a24a4cSGeert Uytterhoeven free_irq(IRQ_AMIGA_PORTS, instance); 237c2a24a4cSGeert Uytterhoeven fail_irq: 238c2a24a4cSGeert Uytterhoeven scsi_host_put(instance); 239c2a24a4cSGeert Uytterhoeven fail_alloc: 240c2a24a4cSGeert Uytterhoeven release_mem_region(res->start, resource_size(res)); 241c2a24a4cSGeert Uytterhoeven return error; 242c2a24a4cSGeert Uytterhoeven } 243c2a24a4cSGeert Uytterhoeven 244c2a24a4cSGeert Uytterhoeven static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev) 245c2a24a4cSGeert Uytterhoeven { 246c2a24a4cSGeert Uytterhoeven struct Scsi_Host *instance = platform_get_drvdata(pdev); 2472b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 248c2a24a4cSGeert Uytterhoeven struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 249c2a24a4cSGeert Uytterhoeven 2502b21d5e4SGeert Uytterhoeven hdata->regs->CNTR = 0; 251c2a24a4cSGeert Uytterhoeven scsi_remove_host(instance); 252c2a24a4cSGeert Uytterhoeven free_irq(IRQ_AMIGA_PORTS, instance); 253c2a24a4cSGeert Uytterhoeven scsi_host_put(instance); 254c2a24a4cSGeert Uytterhoeven release_mem_region(res->start, resource_size(res)); 255c2a24a4cSGeert Uytterhoeven return 0; 256c2a24a4cSGeert Uytterhoeven } 257c2a24a4cSGeert Uytterhoeven 258c2a24a4cSGeert Uytterhoeven static struct platform_driver amiga_a3000_scsi_driver = { 259c2a24a4cSGeert Uytterhoeven .remove = __exit_p(amiga_a3000_scsi_remove), 260c2a24a4cSGeert Uytterhoeven .driver = { 261c2a24a4cSGeert Uytterhoeven .name = "amiga-a3000-scsi", 262c2a24a4cSGeert Uytterhoeven }, 263c2a24a4cSGeert Uytterhoeven }; 264c2a24a4cSGeert Uytterhoeven 265a915b84aSJingoo Han module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe); 266c2a24a4cSGeert Uytterhoeven 267c2a24a4cSGeert Uytterhoeven MODULE_DESCRIPTION("Amiga 3000 built-in SCSI"); 2681da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 269c2a24a4cSGeert Uytterhoeven MODULE_ALIAS("platform:amiga-a3000-scsi"); 270