1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
4 #include <linux/ioport.h>
5 #include <linux/init.h>
6 #include <linux/slab.h>
7 #include <linux/spinlock.h>
8 #include <linux/interrupt.h>
9 #include <linux/platform_device.h>
10 #include <linux/module.h>
13 #include <asm/amigaints.h>
14 #include <asm/amigahw.h>
16 #include <scsi/scsi.h>
17 #include <scsi/scsi_cmnd.h>
18 #include <scsi/scsi_device.h>
19 #include <scsi/scsi_eh.h>
20 #include <scsi/scsi_tcq.h>
25 struct a3000_hostdata {
26 struct WD33C93_hostdata wh;
27 struct a3000_scsiregs *regs;
30 static irqreturn_t a3000_intr(int irq, void *data)
32 struct Scsi_Host *instance = data;
33 struct a3000_hostdata *hdata = shost_priv(instance);
34 unsigned int status = hdata->regs->ISTR;
37 if (!(status & ISTR_INT_P))
39 if (status & ISTR_INTS) {
40 spin_lock_irqsave(instance->host_lock, flags);
41 wd33c93_intr(instance);
42 spin_unlock_irqrestore(instance->host_lock, flags);
45 pr_warn("Non-serviced A3000 SCSI-interrupt? ISTR = %02x\n", status);
49 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
51 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
52 struct Scsi_Host *instance = cmd->device->host;
53 struct a3000_hostdata *hdata = shost_priv(instance);
54 struct WD33C93_hostdata *wh = &hdata->wh;
55 struct a3000_scsiregs *regs = hdata->regs;
56 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
57 unsigned long addr = virt_to_bus(scsi_pointer->ptr);
60 * if the physical address has the wrong alignment, or if
61 * physical address is bad, or if it is a write and at the
62 * end of a physical memory chunk, then allocate a bounce
65 if (addr & A3000_XFER_MASK) {
66 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
67 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
70 /* can't allocate memory; use PIO */
71 if (!wh->dma_bounce_buffer) {
72 wh->dma_bounce_len = 0;
77 /* copy to bounce buffer for a write */
78 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
79 scsi_pointer->this_residual);
82 addr = virt_to_bus(wh->dma_bounce_buffer);
85 /* setup dma direction */
89 /* remember direction */
94 /* setup DMA *physical* address */
98 /* invalidate any cache */
99 cache_clear(addr, scsi_pointer->this_residual);
101 /* push any dirty cache */
102 cache_push(addr, scsi_pointer->this_residual);
106 mb(); /* make sure setup is completed */
108 mb(); /* make sure DMA has started before next IO */
114 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
117 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
118 struct a3000_hostdata *hdata = shost_priv(instance);
119 struct WD33C93_hostdata *wh = &hdata->wh;
120 struct a3000_scsiregs *regs = hdata->regs;
122 /* disable SCSI interrupts */
123 unsigned short cntr = CNTR_PDMD;
129 mb(); /* make sure CNTR is updated before next IO */
131 /* flush if we were reading */
134 mb(); /* don't allow prefetch */
135 while (!(regs->ISTR & ISTR_FE_FLG))
137 mb(); /* no IO until FLUSH is done */
140 /* clear a possible interrupt */
141 /* I think that this CINT is only necessary if you are
142 * using the terminal count features. HM 7 Mar 1994
148 mb(); /* make sure DMA is stopped before next IO */
150 /* restore the CONTROL bits (minus the direction flag) */
151 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
152 mb(); /* make sure CNTR is updated before next IO */
154 /* copy from a bounce buffer, if necessary */
155 if (status && wh->dma_bounce_buffer) {
157 if (wh->dma_dir && SCpnt)
158 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
159 scsi_pointer->this_residual);
160 kfree(wh->dma_bounce_buffer);
161 wh->dma_bounce_buffer = NULL;
162 wh->dma_bounce_len = 0;
164 kfree(wh->dma_bounce_buffer);
165 wh->dma_bounce_buffer = NULL;
166 wh->dma_bounce_len = 0;
171 static struct scsi_host_template amiga_a3000_scsi_template = {
172 .module = THIS_MODULE,
173 .name = "Amiga 3000 built-in SCSI",
174 .show_info = wd33c93_show_info,
175 .write_info = wd33c93_write_info,
176 .proc_name = "A3000",
177 .queuecommand = wd33c93_queuecommand,
178 .eh_abort_handler = wd33c93_abort,
179 .eh_host_reset_handler = wd33c93_host_reset,
180 .can_queue = CAN_QUEUE,
182 .sg_tablesize = SG_ALL,
183 .cmd_per_lun = CMD_PER_LUN,
184 .cmd_size = sizeof(struct scsi_pointer),
187 static int __init amiga_a3000_scsi_probe(struct platform_device *pdev)
189 struct resource *res;
190 struct Scsi_Host *instance;
192 struct a3000_scsiregs *regs;
194 struct a3000_hostdata *hdata;
196 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
200 if (!request_mem_region(res->start, resource_size(res), "wd33c93"))
203 instance = scsi_host_alloc(&amiga_a3000_scsi_template,
204 sizeof(struct a3000_hostdata));
210 instance->irq = IRQ_AMIGA_PORTS;
212 regs = ZTWO_VADDR(res->start);
213 regs->DAWR = DAWR_A3000;
215 wdregs.SASR = ®s->SASR;
216 wdregs.SCMD = ®s->SCMD;
218 hdata = shost_priv(instance);
219 hdata->wh.no_sync = 0xff;
221 hdata->wh.dma_mode = CTRL_DMA;
224 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_12_15);
225 error = request_irq(IRQ_AMIGA_PORTS, a3000_intr, IRQF_SHARED,
226 "A3000 SCSI", instance);
230 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
232 error = scsi_add_host(instance, NULL);
236 platform_set_drvdata(pdev, instance);
238 scsi_scan_host(instance);
242 free_irq(IRQ_AMIGA_PORTS, instance);
244 scsi_host_put(instance);
246 release_mem_region(res->start, resource_size(res));
250 static int __exit amiga_a3000_scsi_remove(struct platform_device *pdev)
252 struct Scsi_Host *instance = platform_get_drvdata(pdev);
253 struct a3000_hostdata *hdata = shost_priv(instance);
254 struct resource *res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
256 hdata->regs->CNTR = 0;
257 scsi_remove_host(instance);
258 free_irq(IRQ_AMIGA_PORTS, instance);
259 scsi_host_put(instance);
260 release_mem_region(res->start, resource_size(res));
264 static struct platform_driver amiga_a3000_scsi_driver = {
265 .remove = __exit_p(amiga_a3000_scsi_remove),
267 .name = "amiga-a3000-scsi",
271 module_platform_driver_probe(amiga_a3000_scsi_driver, amiga_a3000_scsi_probe);
273 MODULE_DESCRIPTION("Amiga 3000 built-in SCSI");
274 MODULE_LICENSE("GPL");
275 MODULE_ALIAS("platform:amiga-a3000-scsi");