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/amigaints.h> 141da177e4SLinus Torvalds #include <asm/amigahw.h> 151da177e4SLinus Torvalds 1653555fb7SBart Van Assche #include <scsi/scsi.h> 1753555fb7SBart Van Assche #include <scsi/scsi_cmnd.h> 1853555fb7SBart Van Assche #include <scsi/scsi_device.h> 1953555fb7SBart Van Assche #include <scsi/scsi_eh.h> 2053555fb7SBart Van Assche #include <scsi/scsi_tcq.h> 211da177e4SLinus Torvalds #include "wd33c93.h" 221da177e4SLinus Torvalds #include "a3000.h" 231da177e4SLinus Torvalds 249387edbeSAdrian Bunk 252b21d5e4SGeert Uytterhoeven struct a3000_hostdata { 262b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata wh; 272b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs; 282b21d5e4SGeert Uytterhoeven }; 292b21d5e4SGeert Uytterhoeven 30a8169e60SGeert Uytterhoeven static irqreturn_t a3000_intr(int irq, void *data) 311da177e4SLinus Torvalds { 32a8169e60SGeert Uytterhoeven struct Scsi_Host *instance = data; 332b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 342b21d5e4SGeert Uytterhoeven unsigned int status = hdata->regs->ISTR; 351da177e4SLinus Torvalds unsigned long flags; 361da177e4SLinus Torvalds 371da177e4SLinus Torvalds if (!(status & ISTR_INT_P)) 381da177e4SLinus Torvalds return IRQ_NONE; 3921351013SGeert Uytterhoeven if (status & ISTR_INTS) { 40a8169e60SGeert Uytterhoeven spin_lock_irqsave(instance->host_lock, flags); 41a8169e60SGeert Uytterhoeven wd33c93_intr(instance); 42a8169e60SGeert Uytterhoeven spin_unlock_irqrestore(instance->host_lock, flags); 431da177e4SLinus Torvalds return IRQ_HANDLED; 441da177e4SLinus Torvalds } 45a2cc701bSKefeng Wang pr_warn("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 { 51*dbb2da55SBart Van Assche struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd); 52a8169e60SGeert Uytterhoeven struct Scsi_Host *instance = cmd->device->host; 532b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 542b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata *wh = &hdata->wh; 552b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs = hdata->regs; 561da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD | CNTR_INTEN; 57*dbb2da55SBart Van Assche unsigned long addr = virt_to_bus(scsi_pointer->ptr); 581da177e4SLinus Torvalds 591da177e4SLinus Torvalds /* 601da177e4SLinus Torvalds * if the physical address has the wrong alignment, or if 611da177e4SLinus Torvalds * physical address is bad, or if it is a write and at the 621da177e4SLinus Torvalds * end of a physical memory chunk, then allocate a bounce 631da177e4SLinus Torvalds * buffer 641da177e4SLinus Torvalds */ 6521351013SGeert Uytterhoeven if (addr & A3000_XFER_MASK) { 66*dbb2da55SBart Van Assche wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff; 672b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len, 68afdbbc16SGeert Uytterhoeven GFP_KERNEL); 691da177e4SLinus Torvalds 701da177e4SLinus Torvalds /* can't allocate memory; use PIO */ 712b21d5e4SGeert Uytterhoeven if (!wh->dma_bounce_buffer) { 722b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 731da177e4SLinus Torvalds return 1; 741da177e4SLinus Torvalds } 751da177e4SLinus Torvalds 761da177e4SLinus Torvalds if (!dir_in) { 771da177e4SLinus Torvalds /* copy to bounce buffer for a write */ 78*dbb2da55SBart Van Assche memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr, 79*dbb2da55SBart Van Assche scsi_pointer->this_residual); 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 822b21d5e4SGeert Uytterhoeven addr = virt_to_bus(wh->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 */ 902b21d5e4SGeert Uytterhoeven wh->dma_dir = dir_in; 911da177e4SLinus Torvalds 92d753722eSGeert Uytterhoeven regs->CNTR = cntr; 931da177e4SLinus Torvalds 941da177e4SLinus Torvalds /* setup DMA *physical* address */ 95d753722eSGeert Uytterhoeven regs->ACR = addr; 961da177e4SLinus Torvalds 9721351013SGeert Uytterhoeven if (dir_in) { 981da177e4SLinus Torvalds /* invalidate any cache */ 99*dbb2da55SBart Van Assche cache_clear(addr, scsi_pointer->this_residual); 10021351013SGeert Uytterhoeven } else { 1011da177e4SLinus Torvalds /* push any dirty cache */ 102*dbb2da55SBart Van Assche cache_push(addr, scsi_pointer->this_residual); 10321351013SGeert Uytterhoeven } 1041da177e4SLinus Torvalds 1051da177e4SLinus Torvalds /* start DMA */ 1061da177e4SLinus Torvalds mb(); /* make sure setup is completed */ 107d753722eSGeert Uytterhoeven regs->ST_DMA = 1; 1081da177e4SLinus Torvalds mb(); /* make sure DMA has started before next IO */ 1091da177e4SLinus Torvalds 1101da177e4SLinus Torvalds /* return success */ 1111da177e4SLinus Torvalds return 0; 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds 11465396410SHenrik Kretzschmar static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, 1151da177e4SLinus Torvalds int status) 1161da177e4SLinus Torvalds { 117*dbb2da55SBart Van Assche struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt); 1182b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 1192b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata *wh = &hdata->wh; 1202b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs = hdata->regs; 121afdbbc16SGeert Uytterhoeven 1221da177e4SLinus Torvalds /* disable SCSI interrupts */ 1231da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD; 1241da177e4SLinus Torvalds 1252b21d5e4SGeert Uytterhoeven if (!wh->dma_dir) 1261da177e4SLinus Torvalds cntr |= CNTR_DDIR; 1271da177e4SLinus Torvalds 128d753722eSGeert Uytterhoeven regs->CNTR = cntr; 1291da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1301da177e4SLinus Torvalds 1311da177e4SLinus Torvalds /* flush if we were reading */ 1322b21d5e4SGeert Uytterhoeven if (wh->dma_dir) { 133d753722eSGeert Uytterhoeven regs->FLUSH = 1; 1341da177e4SLinus Torvalds mb(); /* don't allow prefetch */ 135d753722eSGeert Uytterhoeven while (!(regs->ISTR & ISTR_FE_FLG)) 1361da177e4SLinus Torvalds barrier(); 1371da177e4SLinus Torvalds mb(); /* no IO until FLUSH is done */ 1381da177e4SLinus Torvalds } 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds /* clear a possible interrupt */ 1411da177e4SLinus Torvalds /* I think that this CINT is only necessary if you are 1421da177e4SLinus Torvalds * using the terminal count features. HM 7 Mar 1994 1431da177e4SLinus Torvalds */ 144d753722eSGeert Uytterhoeven regs->CINT = 1; 1451da177e4SLinus Torvalds 1461da177e4SLinus Torvalds /* stop DMA */ 147d753722eSGeert Uytterhoeven regs->SP_DMA = 1; 1481da177e4SLinus Torvalds mb(); /* make sure DMA is stopped before next IO */ 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds /* restore the CONTROL bits (minus the direction flag) */ 151d753722eSGeert Uytterhoeven regs->CNTR = CNTR_PDMD | CNTR_INTEN; 1521da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds /* copy from a bounce buffer, if necessary */ 1552b21d5e4SGeert Uytterhoeven if (status && wh->dma_bounce_buffer) { 156cc0455faSBoaz Harrosh if (SCpnt) { 1572b21d5e4SGeert Uytterhoeven if (wh->dma_dir && SCpnt) 158*dbb2da55SBart Van Assche memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer, 159*dbb2da55SBart Van Assche scsi_pointer->this_residual); 1602b21d5e4SGeert Uytterhoeven kfree(wh->dma_bounce_buffer); 1612b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = NULL; 1622b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 1631da177e4SLinus Torvalds } else { 1642b21d5e4SGeert Uytterhoeven kfree(wh->dma_bounce_buffer); 1652b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = NULL; 1662b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds } 1691da177e4SLinus Torvalds } 1701da177e4SLinus Torvalds 171c2a24a4cSGeert Uytterhoeven static struct scsi_host_template amiga_a3000_scsi_template = { 172c2a24a4cSGeert Uytterhoeven .module = THIS_MODULE, 1731da177e4SLinus Torvalds .name = "Amiga 3000 built-in SCSI", 174408bb25bSAl Viro .show_info = wd33c93_show_info, 175408bb25bSAl Viro .write_info = wd33c93_write_info, 176c2a24a4cSGeert Uytterhoeven .proc_name = "A3000", 1771da177e4SLinus Torvalds .queuecommand = wd33c93_queuecommand, 1781da177e4SLinus Torvalds .eh_abort_handler = wd33c93_abort, 1791da177e4SLinus Torvalds .eh_host_reset_handler = wd33c93_host_reset, 1801da177e4SLinus Torvalds .can_queue = CAN_QUEUE, 1811da177e4SLinus Torvalds .this_id = 7, 1821da177e4SLinus Torvalds .sg_tablesize = SG_ALL, 1831da177e4SLinus Torvalds .cmd_per_lun = CMD_PER_LUN, 184*dbb2da55SBart Van Assche .cmd_size = sizeof(struct scsi_pointer), 1851da177e4SLinus Torvalds }; 1861da177e4SLinus Torvalds 187c2a24a4cSGeert Uytterhoeven static int __init amiga_a3000_scsi_probe(struct platform_device *pdev) 1881da177e4SLinus Torvalds { 189c2a24a4cSGeert Uytterhoeven struct resource *res; 190c2a24a4cSGeert Uytterhoeven struct Scsi_Host *instance; 191c2a24a4cSGeert Uytterhoeven int error; 192c2a24a4cSGeert Uytterhoeven struct a3000_scsiregs *regs; 193c2a24a4cSGeert Uytterhoeven wd33c93_regs wdregs; 1942b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata; 195d753722eSGeert Uytterhoeven 196c2a24a4cSGeert Uytterhoeven res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 197c2a24a4cSGeert Uytterhoeven if (!res) 198c2a24a4cSGeert Uytterhoeven return -ENODEV; 199c2a24a4cSGeert Uytterhoeven 200c2a24a4cSGeert Uytterhoeven if (!request_mem_region(res->start, resource_size(res), "wd33c93")) 201c2a24a4cSGeert Uytterhoeven return -EBUSY; 202c2a24a4cSGeert Uytterhoeven 203c2a24a4cSGeert Uytterhoeven instance = scsi_host_alloc(&amiga_a3000_scsi_template, 2042b21d5e4SGeert Uytterhoeven sizeof(struct a3000_hostdata)); 205c2a24a4cSGeert Uytterhoeven if (!instance) { 206c2a24a4cSGeert Uytterhoeven error = -ENOMEM; 207c2a24a4cSGeert Uytterhoeven goto fail_alloc; 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds 210c2a24a4cSGeert Uytterhoeven instance->irq = IRQ_AMIGA_PORTS; 211c2a24a4cSGeert Uytterhoeven 2126112ea08SGeert Uytterhoeven regs = ZTWO_VADDR(res->start); 213c2a24a4cSGeert Uytterhoeven regs->DAWR = DAWR_A3000; 214c2a24a4cSGeert Uytterhoeven 215c2a24a4cSGeert Uytterhoeven wdregs.SASR = ®s->SASR; 216c2a24a4cSGeert Uytterhoeven wdregs.SCMD = ®s->SCMD; 217c2a24a4cSGeert Uytterhoeven 218c2a24a4cSGeert Uytterhoeven hdata = shost_priv(instance); 2192b21d5e4SGeert Uytterhoeven hdata->wh.no_sync = 0xff; 2202b21d5e4SGeert Uytterhoeven hdata->wh.fast = 0; 2212b21d5e4SGeert Uytterhoeven hdata->wh.dma_mode = CTRL_DMA; 2222b21d5e4SGeert Uytterhoeven hdata->regs = regs; 223c2a24a4cSGeert Uytterhoeven 224c2a24a4cSGeert Uytterhoeven wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15); 225c2a24a4cSGeert Uytterhoeven error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, 226c2a24a4cSGeert Uytterhoeven "A3000 SCSI", instance); 227c2a24a4cSGeert Uytterhoeven if (error) 228c2a24a4cSGeert Uytterhoeven goto fail_irq; 229c2a24a4cSGeert Uytterhoeven 230c2a24a4cSGeert Uytterhoeven regs->CNTR = CNTR_PDMD | CNTR_INTEN; 231c2a24a4cSGeert Uytterhoeven 232c2a24a4cSGeert Uytterhoeven error = scsi_add_host(instance, NULL); 233c2a24a4cSGeert Uytterhoeven if (error) 234c2a24a4cSGeert Uytterhoeven goto fail_host; 235c2a24a4cSGeert Uytterhoeven 236c2a24a4cSGeert Uytterhoeven platform_set_drvdata(pdev, instance); 237c2a24a4cSGeert Uytterhoeven 238c2a24a4cSGeert Uytterhoeven scsi_scan_host(instance); 239c2a24a4cSGeert Uytterhoeven return 0; 240c2a24a4cSGeert Uytterhoeven 241c2a24a4cSGeert Uytterhoeven fail_host: 242c2a24a4cSGeert Uytterhoeven free_irq(IRQ_AMIGA_PORTS, instance); 243c2a24a4cSGeert Uytterhoeven fail_irq: 244c2a24a4cSGeert Uytterhoeven scsi_host_put(instance); 245c2a24a4cSGeert Uytterhoeven fail_alloc: 246c2a24a4cSGeert Uytterhoeven release_mem_region(res->start, resource_size(res)); 247c2a24a4cSGeert Uytterhoeven return error; 248c2a24a4cSGeert Uytterhoeven } 249c2a24a4cSGeert Uytterhoeven 250c2a24a4cSGeert Uytterhoeven static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev) 251c2a24a4cSGeert Uytterhoeven { 252c2a24a4cSGeert Uytterhoeven struct Scsi_Host *instance = platform_get_drvdata(pdev); 2532b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 254c2a24a4cSGeert Uytterhoeven struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 255c2a24a4cSGeert Uytterhoeven 2562b21d5e4SGeert Uytterhoeven hdata->regs->CNTR = 0; 257c2a24a4cSGeert Uytterhoeven scsi_remove_host(instance); 258c2a24a4cSGeert Uytterhoeven free_irq(IRQ_AMIGA_PORTS, instance); 259c2a24a4cSGeert Uytterhoeven scsi_host_put(instance); 260c2a24a4cSGeert Uytterhoeven release_mem_region(res->start, resource_size(res)); 261c2a24a4cSGeert Uytterhoeven return 0; 262c2a24a4cSGeert Uytterhoeven } 263c2a24a4cSGeert Uytterhoeven 264c2a24a4cSGeert Uytterhoeven static struct platform_driver amiga_a3000_scsi_driver = { 265c2a24a4cSGeert Uytterhoeven .remove = __exit_p(amiga_a3000_scsi_remove), 266c2a24a4cSGeert Uytterhoeven .driver = { 267c2a24a4cSGeert Uytterhoeven .name = "amiga-a3000-scsi", 268c2a24a4cSGeert Uytterhoeven }, 269c2a24a4cSGeert Uytterhoeven }; 270c2a24a4cSGeert Uytterhoeven 271a915b84aSJingoo Han module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe); 272c2a24a4cSGeert Uytterhoeven 273c2a24a4cSGeert Uytterhoeven MODULE_DESCRIPTION("Amiga 3000 built-in SCSI"); 2741da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 275c2a24a4cSGeert Uytterhoeven MODULE_ALIAS("platform:amiga-a3000-scsi"); 276