blob: 86f1da22aaa59321287363bf9a08f76bdd54c32e [file] [log] [blame]
Thomas Gleixner09c434b2019-05-19 13:08:20 +01001// SPDX-License-Identifier: GPL-2.0-only
Linus Torvalds1da177e2005-04-16 15:20:36 -07002#include <linux/types.h>
3#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004#include <linux/ioport.h>
5#include <linux/init.h>
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +02006#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007#include <linux/spinlock.h>
8#include <linux/interrupt.h>
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +02009#include <linux/platform_device.h>
Paul Gortmakeracf3368f2011-05-27 09:47:43 -040010#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012#include <asm/page.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include <asm/amigaints.h>
14#include <asm/amigahw.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015
16#include "scsi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include "wd33c93.h"
18#include "a3000.h"
19
Adrian Bunk9387edb2009-03-04 12:06:08 -080020
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020021struct a3000_hostdata {
22 struct WD33C93_hostdata wh;
23 struct a3000_scsiregs *regs;
24};
25
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020026static irqreturn_t a3000_intr(int irq, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -070027{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020028 struct Scsi_Host *instance = data;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020029 struct a3000_hostdata *hdata = shost_priv(instance);
30 unsigned int status = hdata->regs->ISTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33 if (!(status & ISTR_INT_P))
34 return IRQ_NONE;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020035 if (status & ISTR_INTS) {
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020036 spin_lock_irqsave(instance->host_lock, flags);
37 wd33c93_intr(instance);
38 spin_unlock_irqrestore(instance->host_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 return IRQ_HANDLED;
40 }
Kefeng Wanga2cc7012019-10-18 11:18:38 +080041 pr_warn("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 return IRQ_NONE;
43}
44
Henrik Kretzschmar65396412006-09-12 23:49:33 +020045static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Geert Uytterhoevena8169e62009-05-17 13:35:07 +020047 struct Scsi_Host *instance = cmd->device->host;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020048 struct a3000_hostdata *hdata = shost_priv(instance);
49 struct WD33C93_hostdata *wh = &hdata->wh;
50 struct a3000_scsiregs *regs = hdata->regs;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020051 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
52 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
Geert Uytterhoeven21351012010-04-04 11:00:35 +020054 /*
55 * if the physical address has the wrong alignment, or if
56 * physical address is bad, or if it is a write and at the
57 * end of a physical memory chunk, then allocate a bounce
58 * buffer
59 */
60 if (addr & A3000_XFER_MASK) {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020061 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
62 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
63 GFP_KERNEL);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020064
65 /* can't allocate memory; use PIO */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020066 if (!wh->dma_bounce_buffer) {
67 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020068 return 1;
69 }
70
71 if (!dir_in) {
72 /* copy to bounce buffer for a write */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020073 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +020074 cmd->SCp.this_residual);
Geert Uytterhoeven21351012010-04-04 11:00:35 +020075 }
76
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020077 addr = virt_to_bus(wh->dma_bounce_buffer);
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 }
79
Geert Uytterhoeven21351012010-04-04 11:00:35 +020080 /* setup dma direction */
81 if (!dir_in)
82 cntr |= CNTR_DDIR;
83
84 /* remember direction */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +020085 wh->dma_dir = dir_in;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020086
Geert Uytterhoevend7537222009-05-17 12:17:23 +020087 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020088
89 /* setup DMA *physical* address */
Geert Uytterhoevend7537222009-05-17 12:17:23 +020090 regs->ACR = addr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +020091
92 if (dir_in) {
93 /* invalidate any cache */
94 cache_clear(addr, cmd->SCp.this_residual);
95 } else {
96 /* push any dirty cache */
97 cache_push(addr, cmd->SCp.this_residual);
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 }
99
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200100 /* start DMA */
101 mb(); /* make sure setup is completed */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200102 regs->ST_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200103 mb(); /* make sure DMA has started before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200105 /* return success */
106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107}
108
Henrik Kretzschmar65396412006-09-12 23:49:33 +0200109static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
110 int status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111{
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200112 struct a3000_hostdata *hdata = shost_priv(instance);
113 struct WD33C93_hostdata *wh = &hdata->wh;
114 struct a3000_scsiregs *regs = hdata->regs;
Geert Uytterhoevenafdbbc12010-04-04 11:00:39 +0200115
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200116 /* disable SCSI interrupts */
117 unsigned short cntr = CNTR_PDMD;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200119 if (!wh->dma_dir)
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200120 cntr |= CNTR_DDIR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200122 regs->CNTR = cntr;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200123 mb(); /* make sure CNTR is updated before next IO */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200125 /* flush if we were reading */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200126 if (wh->dma_dir) {
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200127 regs->FLUSH = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200128 mb(); /* don't allow prefetch */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200129 while (!(regs->ISTR & ISTR_FE_FLG))
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200130 barrier();
131 mb(); /* no IO until FLUSH is done */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200133
134 /* clear a possible interrupt */
135 /* I think that this CINT is only necessary if you are
136 * using the terminal count features. HM 7 Mar 1994
137 */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200138 regs->CINT = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200139
140 /* stop DMA */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200141 regs->SP_DMA = 1;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200142 mb(); /* make sure DMA is stopped before next IO */
143
144 /* restore the CONTROL bits (minus the direction flag) */
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200145 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200146 mb(); /* make sure CNTR is updated before next IO */
147
148 /* copy from a bounce buffer, if necessary */
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200149 if (status && wh->dma_bounce_buffer) {
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200150 if (SCpnt) {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200151 if (wh->dma_dir && SCpnt)
152 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200153 SCpnt->SCp.this_residual);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200154 kfree(wh->dma_bounce_buffer);
155 wh->dma_bounce_buffer = NULL;
156 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200157 } else {
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200158 kfree(wh->dma_bounce_buffer);
159 wh->dma_bounce_buffer = NULL;
160 wh->dma_bounce_len = 0;
Geert Uytterhoeven21351012010-04-04 11:00:35 +0200161 }
162 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200165static struct scsi_host_template amiga_a3000_scsi_template = {
166 .module = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 .name = "Amiga 3000 built-in SCSI",
Al Viro408bb252013-03-31 00:30:35 -0400168 .show_info = wd33c93_show_info,
169 .write_info = wd33c93_write_info,
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200170 .proc_name = "A3000",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 .queuecommand = wd33c93_queuecommand,
172 .eh_abort_handler = wd33c93_abort,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 .eh_host_reset_handler = wd33c93_host_reset,
174 .can_queue = CAN_QUEUE,
175 .this_id = 7,
176 .sg_tablesize = SG_ALL,
177 .cmd_per_lun = CMD_PER_LUN,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178};
179
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200180static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200182 struct resource *res;
183 struct Scsi_Host *instance;
184 int error;
185 struct a3000_scsiregs *regs;
186 wd33c93_regs wdregs;
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200187 struct a3000_hostdata *hdata;
Geert Uytterhoevend7537222009-05-17 12:17:23 +0200188
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200189 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
190 if (!res)
191 return -ENODEV;
192
193 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
194 return -EBUSY;
195
196 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200197 sizeof(struct a3000_hostdata));
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200198 if (!instance) {
199 error = -ENOMEM;
200 goto fail_alloc;
201 }
202
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200203 instance->irq = IRQ_AMIGA_PORTS;
204
Geert Uytterhoeven6112ea02011-01-09 11:03:43 +0100205 regs = ZTWO_VADDR(res->start);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200206 regs->DAWR = DAWR_A3000;
207
208 wdregs.SASR = &regs->SASR;
209 wdregs.SCMD = &regs->SCMD;
210
211 hdata = shost_priv(instance);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200212 hdata->wh.no_sync = 0xff;
213 hdata->wh.fast = 0;
214 hdata->wh.dma_mode = CTRL_DMA;
215 hdata->regs = regs;
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200216
217 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
218 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
219 "A3000 SCSI", instance);
220 if (error)
221 goto fail_irq;
222
223 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
224
225 error = scsi_add_host(instance, NULL);
226 if (error)
227 goto fail_host;
228
229 platform_set_drvdata(pdev, instance);
230
231 scsi_scan_host(instance);
232 return 0;
233
234fail_host:
235 free_irq(IRQ_AMIGA_PORTS, instance);
236fail_irq:
237 scsi_host_put(instance);
238fail_alloc:
239 release_mem_region(res->start, resource_size(res));
240 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241}
242
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200243static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
244{
245 struct Scsi_Host *instance = platform_get_drvdata(pdev);
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200246 struct a3000_hostdata *hdata = shost_priv(instance);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200247 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
248
Geert Uytterhoeven2b21d5e2010-04-12 21:55:15 +0200249 hdata->regs->CNTR = 0;
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200250 scsi_remove_host(instance);
251 free_irq(IRQ_AMIGA_PORTS, instance);
252 scsi_host_put(instance);
253 release_mem_region(res->start, resource_size(res));
254 return 0;
255}
256
257static struct platform_driver amiga_a3000_scsi_driver = {
258 .remove = __exit_p(amiga_a3000_scsi_remove),
259 .driver = {
260 .name = "amiga-a3000-scsi",
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200261 },
262};
263
Jingoo Hana915b842013-05-02 15:30:31 +0900264module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe);
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200265
266MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267MODULE_LICENSE("GPL");
Geert Uytterhoevenc2a24a42009-04-05 13:02:45 +0200268MODULE_ALIAS("platform:amiga-a3000-scsi");