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 16*53555fb7SBart Van Assche #include <scsi/scsi.h> 17*53555fb7SBart Van Assche #include <scsi/scsi_cmnd.h> 18*53555fb7SBart Van Assche #include <scsi/scsi_device.h> 19*53555fb7SBart Van Assche #include <scsi/scsi_eh.h> 20*53555fb7SBart 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 { 51a8169e60SGeert Uytterhoeven struct Scsi_Host *instance = cmd->device->host; 522b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 532b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata *wh = &hdata->wh; 542b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs = hdata->regs; 551da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD | CNTR_INTEN; 561da177e4SLinus Torvalds unsigned long addr = virt_to_bus(cmd->SCp.ptr); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds /* 591da177e4SLinus Torvalds * if the physical address has the wrong alignment, or if 601da177e4SLinus Torvalds * physical address is bad, or if it is a write and at the 611da177e4SLinus Torvalds * end of a physical memory chunk, then allocate a bounce 621da177e4SLinus Torvalds * buffer 631da177e4SLinus Torvalds */ 6421351013SGeert Uytterhoeven if (addr & A3000_XFER_MASK) { 652b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff; 662b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len, 67afdbbc16SGeert Uytterhoeven GFP_KERNEL); 681da177e4SLinus Torvalds 691da177e4SLinus Torvalds /* can't allocate memory; use PIO */ 702b21d5e4SGeert Uytterhoeven if (!wh->dma_bounce_buffer) { 712b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 721da177e4SLinus Torvalds return 1; 731da177e4SLinus Torvalds } 741da177e4SLinus Torvalds 751da177e4SLinus Torvalds if (!dir_in) { 761da177e4SLinus Torvalds /* copy to bounce buffer for a write */ 772b21d5e4SGeert Uytterhoeven memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr, 78afdbbc16SGeert Uytterhoeven cmd->SCp.this_residual); 791da177e4SLinus Torvalds } 801da177e4SLinus Torvalds 812b21d5e4SGeert Uytterhoeven addr = virt_to_bus(wh->dma_bounce_buffer); 821da177e4SLinus Torvalds } 831da177e4SLinus Torvalds 841da177e4SLinus Torvalds /* setup dma direction */ 851da177e4SLinus Torvalds if (!dir_in) 861da177e4SLinus Torvalds cntr |= CNTR_DDIR; 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds /* remember direction */ 892b21d5e4SGeert Uytterhoeven wh->dma_dir = dir_in; 901da177e4SLinus Torvalds 91d753722eSGeert Uytterhoeven regs->CNTR = cntr; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds /* setup DMA *physical* address */ 94d753722eSGeert Uytterhoeven regs->ACR = addr; 951da177e4SLinus Torvalds 9621351013SGeert Uytterhoeven if (dir_in) { 971da177e4SLinus Torvalds /* invalidate any cache */ 981da177e4SLinus Torvalds cache_clear(addr, cmd->SCp.this_residual); 9921351013SGeert Uytterhoeven } else { 1001da177e4SLinus Torvalds /* push any dirty cache */ 1011da177e4SLinus Torvalds cache_push(addr, cmd->SCp.this_residual); 10221351013SGeert Uytterhoeven } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds /* start DMA */ 1051da177e4SLinus Torvalds mb(); /* make sure setup is completed */ 106d753722eSGeert Uytterhoeven regs->ST_DMA = 1; 1071da177e4SLinus Torvalds mb(); /* make sure DMA has started before next IO */ 1081da177e4SLinus Torvalds 1091da177e4SLinus Torvalds /* return success */ 1101da177e4SLinus Torvalds return 0; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds 11365396410SHenrik Kretzschmar static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt, 1141da177e4SLinus Torvalds int status) 1151da177e4SLinus Torvalds { 1162b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 1172b21d5e4SGeert Uytterhoeven struct WD33C93_hostdata *wh = &hdata->wh; 1182b21d5e4SGeert Uytterhoeven struct a3000_scsiregs *regs = hdata->regs; 119afdbbc16SGeert Uytterhoeven 1201da177e4SLinus Torvalds /* disable SCSI interrupts */ 1211da177e4SLinus Torvalds unsigned short cntr = CNTR_PDMD; 1221da177e4SLinus Torvalds 1232b21d5e4SGeert Uytterhoeven if (!wh->dma_dir) 1241da177e4SLinus Torvalds cntr |= CNTR_DDIR; 1251da177e4SLinus Torvalds 126d753722eSGeert Uytterhoeven regs->CNTR = cntr; 1271da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds /* flush if we were reading */ 1302b21d5e4SGeert Uytterhoeven if (wh->dma_dir) { 131d753722eSGeert Uytterhoeven regs->FLUSH = 1; 1321da177e4SLinus Torvalds mb(); /* don't allow prefetch */ 133d753722eSGeert Uytterhoeven while (!(regs->ISTR & ISTR_FE_FLG)) 1341da177e4SLinus Torvalds barrier(); 1351da177e4SLinus Torvalds mb(); /* no IO until FLUSH is done */ 1361da177e4SLinus Torvalds } 1371da177e4SLinus Torvalds 1381da177e4SLinus Torvalds /* clear a possible interrupt */ 1391da177e4SLinus Torvalds /* I think that this CINT is only necessary if you are 1401da177e4SLinus Torvalds * using the terminal count features. HM 7 Mar 1994 1411da177e4SLinus Torvalds */ 142d753722eSGeert Uytterhoeven regs->CINT = 1; 1431da177e4SLinus Torvalds 1441da177e4SLinus Torvalds /* stop DMA */ 145d753722eSGeert Uytterhoeven regs->SP_DMA = 1; 1461da177e4SLinus Torvalds mb(); /* make sure DMA is stopped before next IO */ 1471da177e4SLinus Torvalds 1481da177e4SLinus Torvalds /* restore the CONTROL bits (minus the direction flag) */ 149d753722eSGeert Uytterhoeven regs->CNTR = CNTR_PDMD | CNTR_INTEN; 1501da177e4SLinus Torvalds mb(); /* make sure CNTR is updated before next IO */ 1511da177e4SLinus Torvalds 1521da177e4SLinus Torvalds /* copy from a bounce buffer, if necessary */ 1532b21d5e4SGeert Uytterhoeven if (status && wh->dma_bounce_buffer) { 154cc0455faSBoaz Harrosh if (SCpnt) { 1552b21d5e4SGeert Uytterhoeven if (wh->dma_dir && SCpnt) 1562b21d5e4SGeert Uytterhoeven memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer, 1571da177e4SLinus Torvalds SCpnt->SCp.this_residual); 1582b21d5e4SGeert Uytterhoeven kfree(wh->dma_bounce_buffer); 1592b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = NULL; 1602b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 1611da177e4SLinus Torvalds } else { 1622b21d5e4SGeert Uytterhoeven kfree(wh->dma_bounce_buffer); 1632b21d5e4SGeert Uytterhoeven wh->dma_bounce_buffer = NULL; 1642b21d5e4SGeert Uytterhoeven wh->dma_bounce_len = 0; 1651da177e4SLinus Torvalds } 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds } 1681da177e4SLinus Torvalds 169c2a24a4cSGeert Uytterhoeven static struct scsi_host_template amiga_a3000_scsi_template = { 170c2a24a4cSGeert Uytterhoeven .module = THIS_MODULE, 1711da177e4SLinus Torvalds .name = "Amiga 3000 built-in SCSI", 172408bb25bSAl Viro .show_info = wd33c93_show_info, 173408bb25bSAl Viro .write_info = wd33c93_write_info, 174c2a24a4cSGeert Uytterhoeven .proc_name = "A3000", 1751da177e4SLinus Torvalds .queuecommand = wd33c93_queuecommand, 1761da177e4SLinus Torvalds .eh_abort_handler = wd33c93_abort, 1771da177e4SLinus Torvalds .eh_host_reset_handler = wd33c93_host_reset, 1781da177e4SLinus Torvalds .can_queue = CAN_QUEUE, 1791da177e4SLinus Torvalds .this_id = 7, 1801da177e4SLinus Torvalds .sg_tablesize = SG_ALL, 1811da177e4SLinus Torvalds .cmd_per_lun = CMD_PER_LUN, 1821da177e4SLinus Torvalds }; 1831da177e4SLinus Torvalds 184c2a24a4cSGeert Uytterhoeven static int __init amiga_a3000_scsi_probe(struct platform_device *pdev) 1851da177e4SLinus Torvalds { 186c2a24a4cSGeert Uytterhoeven struct resource *res; 187c2a24a4cSGeert Uytterhoeven struct Scsi_Host *instance; 188c2a24a4cSGeert Uytterhoeven int error; 189c2a24a4cSGeert Uytterhoeven struct a3000_scsiregs *regs; 190c2a24a4cSGeert Uytterhoeven wd33c93_regs wdregs; 1912b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata; 192d753722eSGeert Uytterhoeven 193c2a24a4cSGeert Uytterhoeven res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 194c2a24a4cSGeert Uytterhoeven if (!res) 195c2a24a4cSGeert Uytterhoeven return -ENODEV; 196c2a24a4cSGeert Uytterhoeven 197c2a24a4cSGeert Uytterhoeven if (!request_mem_region(res->start, resource_size(res), "wd33c93")) 198c2a24a4cSGeert Uytterhoeven return -EBUSY; 199c2a24a4cSGeert Uytterhoeven 200c2a24a4cSGeert Uytterhoeven instance = scsi_host_alloc(&amiga_a3000_scsi_template, 2012b21d5e4SGeert Uytterhoeven sizeof(struct a3000_hostdata)); 202c2a24a4cSGeert Uytterhoeven if (!instance) { 203c2a24a4cSGeert Uytterhoeven error = -ENOMEM; 204c2a24a4cSGeert Uytterhoeven goto fail_alloc; 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 207c2a24a4cSGeert Uytterhoeven instance->irq = IRQ_AMIGA_PORTS; 208c2a24a4cSGeert Uytterhoeven 2096112ea08SGeert Uytterhoeven regs = ZTWO_VADDR(res->start); 210c2a24a4cSGeert Uytterhoeven regs->DAWR = DAWR_A3000; 211c2a24a4cSGeert Uytterhoeven 212c2a24a4cSGeert Uytterhoeven wdregs.SASR = ®s->SASR; 213c2a24a4cSGeert Uytterhoeven wdregs.SCMD = ®s->SCMD; 214c2a24a4cSGeert Uytterhoeven 215c2a24a4cSGeert Uytterhoeven hdata = shost_priv(instance); 2162b21d5e4SGeert Uytterhoeven hdata->wh.no_sync = 0xff; 2172b21d5e4SGeert Uytterhoeven hdata->wh.fast = 0; 2182b21d5e4SGeert Uytterhoeven hdata->wh.dma_mode = CTRL_DMA; 2192b21d5e4SGeert Uytterhoeven hdata->regs = regs; 220c2a24a4cSGeert Uytterhoeven 221c2a24a4cSGeert Uytterhoeven wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15); 222c2a24a4cSGeert Uytterhoeven error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED, 223c2a24a4cSGeert Uytterhoeven "A3000 SCSI", instance); 224c2a24a4cSGeert Uytterhoeven if (error) 225c2a24a4cSGeert Uytterhoeven goto fail_irq; 226c2a24a4cSGeert Uytterhoeven 227c2a24a4cSGeert Uytterhoeven regs->CNTR = CNTR_PDMD | CNTR_INTEN; 228c2a24a4cSGeert Uytterhoeven 229c2a24a4cSGeert Uytterhoeven error = scsi_add_host(instance, NULL); 230c2a24a4cSGeert Uytterhoeven if (error) 231c2a24a4cSGeert Uytterhoeven goto fail_host; 232c2a24a4cSGeert Uytterhoeven 233c2a24a4cSGeert Uytterhoeven platform_set_drvdata(pdev, instance); 234c2a24a4cSGeert Uytterhoeven 235c2a24a4cSGeert Uytterhoeven scsi_scan_host(instance); 236c2a24a4cSGeert Uytterhoeven return 0; 237c2a24a4cSGeert Uytterhoeven 238c2a24a4cSGeert Uytterhoeven fail_host: 239c2a24a4cSGeert Uytterhoeven free_irq(IRQ_AMIGA_PORTS, instance); 240c2a24a4cSGeert Uytterhoeven fail_irq: 241c2a24a4cSGeert Uytterhoeven scsi_host_put(instance); 242c2a24a4cSGeert Uytterhoeven fail_alloc: 243c2a24a4cSGeert Uytterhoeven release_mem_region(res->start, resource_size(res)); 244c2a24a4cSGeert Uytterhoeven return error; 245c2a24a4cSGeert Uytterhoeven } 246c2a24a4cSGeert Uytterhoeven 247c2a24a4cSGeert Uytterhoeven static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev) 248c2a24a4cSGeert Uytterhoeven { 249c2a24a4cSGeert Uytterhoeven struct Scsi_Host *instance = platform_get_drvdata(pdev); 2502b21d5e4SGeert Uytterhoeven struct a3000_hostdata *hdata = shost_priv(instance); 251c2a24a4cSGeert Uytterhoeven struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 252c2a24a4cSGeert Uytterhoeven 2532b21d5e4SGeert Uytterhoeven hdata->regs->CNTR = 0; 254c2a24a4cSGeert Uytterhoeven scsi_remove_host(instance); 255c2a24a4cSGeert Uytterhoeven free_irq(IRQ_AMIGA_PORTS, instance); 256c2a24a4cSGeert Uytterhoeven scsi_host_put(instance); 257c2a24a4cSGeert Uytterhoeven release_mem_region(res->start, resource_size(res)); 258c2a24a4cSGeert Uytterhoeven return 0; 259c2a24a4cSGeert Uytterhoeven } 260c2a24a4cSGeert Uytterhoeven 261c2a24a4cSGeert Uytterhoeven static struct platform_driver amiga_a3000_scsi_driver = { 262c2a24a4cSGeert Uytterhoeven .remove = __exit_p(amiga_a3000_scsi_remove), 263c2a24a4cSGeert Uytterhoeven .driver = { 264c2a24a4cSGeert Uytterhoeven .name = "amiga-a3000-scsi", 265c2a24a4cSGeert Uytterhoeven }, 266c2a24a4cSGeert Uytterhoeven }; 267c2a24a4cSGeert Uytterhoeven 268a915b84aSJingoo Han module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe); 269c2a24a4cSGeert Uytterhoeven 270c2a24a4cSGeert Uytterhoeven MODULE_DESCRIPTION("Amiga 3000 built-in SCSI"); 2711da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 272c2a24a4cSGeert Uytterhoeven MODULE_ALIAS("platform:amiga-a3000-scsi"); 273