1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/types.h>
3 #include <linux/init.h>
4 #include <linux/interrupt.h>
6 #include <linux/slab.h>
7 #include <linux/spinlock.h>
8 #include <linux/zorro.h>
9 #include <linux/module.h>
12 #include <asm/amigaints.h>
13 #include <asm/amigahw.h>
15 #include <scsi/scsi.h>
16 #include <scsi/scsi_cmnd.h>
17 #include <scsi/scsi_device.h>
18 #include <scsi/scsi_eh.h>
19 #include <scsi/scsi_tcq.h>
24 struct a2091_hostdata {
25 struct WD33C93_hostdata wh;
26 struct a2091_scsiregs *regs;
29 static irqreturn_t a2091_intr(int irq, void *data)
31 struct Scsi_Host *instance = data;
32 struct a2091_hostdata *hdata = shost_priv(instance);
33 unsigned int status = hdata->regs->ISTR;
36 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
39 spin_lock_irqsave(instance->host_lock, flags);
40 wd33c93_intr(instance);
41 spin_unlock_irqrestore(instance->host_lock, flags);
45 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
47 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
48 struct Scsi_Host *instance = cmd->device->host;
49 struct a2091_hostdata *hdata = shost_priv(instance);
50 struct WD33C93_hostdata *wh = &hdata->wh;
51 struct a2091_scsiregs *regs = hdata->regs;
52 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
53 unsigned long addr = virt_to_bus(scsi_pointer->ptr);
55 /* don't allow DMA if the physical address is bad */
56 if (addr & A2091_XFER_MASK) {
57 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
58 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
61 /* can't allocate memory; use PIO */
62 if (!wh->dma_bounce_buffer) {
63 wh->dma_bounce_len = 0;
67 /* get the physical address of the bounce buffer */
68 addr = virt_to_bus(wh->dma_bounce_buffer);
70 /* the bounce buffer may not be in the first 16M of physmem */
71 if (addr & A2091_XFER_MASK) {
72 /* we could use chipmem... maybe later */
73 kfree(wh->dma_bounce_buffer);
74 wh->dma_bounce_buffer = NULL;
75 wh->dma_bounce_len = 0;
80 /* copy to bounce buffer for a write */
81 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
82 scsi_pointer->this_residual);
86 /* setup dma direction */
90 /* remember direction */
95 /* setup DMA *physical* address */
99 /* invalidate any cache */
100 cache_clear(addr, scsi_pointer->this_residual);
102 /* push any dirty cache */
103 cache_push(addr, scsi_pointer->this_residual);
112 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
115 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
116 struct a2091_hostdata *hdata = shost_priv(instance);
117 struct WD33C93_hostdata *wh = &hdata->wh;
118 struct a2091_scsiregs *regs = hdata->regs;
120 /* disable SCSI interrupts */
121 unsigned short cntr = CNTR_PDMD;
126 /* disable SCSI interrupts */
129 /* flush if we were reading */
132 while (!(regs->ISTR & ISTR_FE_FLG))
136 /* clear a possible interrupt */
142 /* restore the CONTROL bits (minus the direction flag) */
143 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
145 /* copy from a bounce buffer, if necessary */
146 if (status && wh->dma_bounce_buffer) {
148 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
149 scsi_pointer->this_residual);
150 kfree(wh->dma_bounce_buffer);
151 wh->dma_bounce_buffer = NULL;
152 wh->dma_bounce_len = 0;
156 static struct scsi_host_template a2091_scsi_template = {
157 .module = THIS_MODULE,
158 .name = "Commodore A2091/A590 SCSI",
159 .show_info = wd33c93_show_info,
160 .write_info = wd33c93_write_info,
161 .proc_name = "A2901",
162 .queuecommand = wd33c93_queuecommand,
163 .eh_abort_handler = wd33c93_abort,
164 .eh_host_reset_handler = wd33c93_host_reset,
165 .can_queue = CAN_QUEUE,
167 .sg_tablesize = SG_ALL,
168 .cmd_per_lun = CMD_PER_LUN,
169 .dma_boundary = PAGE_SIZE - 1,
170 .cmd_size = sizeof(struct scsi_pointer),
173 static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
175 struct Scsi_Host *instance;
177 struct a2091_scsiregs *regs;
179 struct a2091_hostdata *hdata;
181 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
184 instance = scsi_host_alloc(&a2091_scsi_template,
185 sizeof(struct a2091_hostdata));
191 instance->irq = IRQ_AMIGA_PORTS;
192 instance->unique_id = z->slotaddr;
194 regs = ZTWO_VADDR(z->resource.start);
195 regs->DAWR = DAWR_A2091;
197 wdregs.SASR = ®s->SASR;
198 wdregs.SCMD = ®s->SCMD;
200 hdata = shost_priv(instance);
201 hdata->wh.no_sync = 0xff;
203 hdata->wh.dma_mode = CTRL_DMA;
206 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
207 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
208 "A2091 SCSI", instance);
212 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
214 error = scsi_add_host(instance, NULL);
218 zorro_set_drvdata(z, instance);
220 scsi_scan_host(instance);
224 free_irq(IRQ_AMIGA_PORTS, instance);
226 scsi_host_put(instance);
228 release_mem_region(z->resource.start, 256);
232 static void a2091_remove(struct zorro_dev *z)
234 struct Scsi_Host *instance = zorro_get_drvdata(z);
235 struct a2091_hostdata *hdata = shost_priv(instance);
237 hdata->regs->CNTR = 0;
238 scsi_remove_host(instance);
239 free_irq(IRQ_AMIGA_PORTS, instance);
240 scsi_host_put(instance);
241 release_mem_region(z->resource.start, 256);
244 static struct zorro_device_id a2091_zorro_tbl[] = {
245 { ZORRO_PROD_CBM_A590_A2091_1 },
246 { ZORRO_PROD_CBM_A590_A2091_2 },
249 MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
251 static struct zorro_driver a2091_driver = {
253 .id_table = a2091_zorro_tbl,
254 .probe = a2091_probe,
255 .remove = a2091_remove,
258 static int __init a2091_init(void)
260 return zorro_register_driver(&a2091_driver);
262 module_init(a2091_init);
264 static void __exit a2091_exit(void)
266 zorro_unregister_driver(&a2091_driver);
268 module_exit(a2091_exit);
270 MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
271 MODULE_LICENSE("GPL");