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;
30 #define DMA_DIR(d) ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
32 static irqreturn_t a2091_intr(int irq, void *data)
34 struct Scsi_Host *instance = data;
35 struct a2091_hostdata *hdata = shost_priv(instance);
36 unsigned int status = hdata->regs->ISTR;
39 if (!(status & (ISTR_INT_F | ISTR_INT_P)) || !(status & ISTR_INTS))
42 spin_lock_irqsave(instance->host_lock, flags);
43 wd33c93_intr(instance);
44 spin_unlock_irqrestore(instance->host_lock, flags);
48 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
50 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
51 unsigned long len = scsi_pointer->this_residual;
52 struct Scsi_Host *instance = cmd->device->host;
53 struct a2091_hostdata *hdata = shost_priv(instance);
54 struct WD33C93_hostdata *wh = &hdata->wh;
55 struct a2091_scsiregs *regs = hdata->regs;
56 unsigned short cntr = CNTR_PDMD | CNTR_INTEN;
59 addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
60 len, DMA_DIR(dir_in));
61 if (dma_mapping_error(hdata->dev, addr)) {
62 dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
66 scsi_pointer->dma_handle = addr;
68 /* don't allow DMA if the physical address is bad */
69 if (addr & A2091_XFER_MASK) {
70 /* drop useless mapping */
71 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
72 scsi_pointer->this_residual,
74 scsi_pointer->dma_handle = (dma_addr_t) NULL;
76 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
77 wh->dma_bounce_buffer = kmalloc(wh->dma_bounce_len,
80 /* can't allocate memory; use PIO */
81 if (!wh->dma_bounce_buffer) {
82 wh->dma_bounce_len = 0;
87 /* copy to bounce buffer for a write */
88 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
89 scsi_pointer->this_residual);
92 /* will flush/invalidate cache for us */
93 addr = dma_map_single(hdata->dev, wh->dma_bounce_buffer,
94 wh->dma_bounce_len, DMA_DIR(dir_in));
95 /* can't map buffer; use PIO */
96 if (dma_mapping_error(hdata->dev, addr)) {
97 dev_warn(hdata->dev, "cannot map bounce buffer %p\n",
98 wh->dma_bounce_buffer);
102 /* the bounce buffer may not be in the first 16M of physmem */
103 if (addr & A2091_XFER_MASK) {
104 /* we could use chipmem... maybe later */
105 kfree(wh->dma_bounce_buffer);
106 wh->dma_bounce_buffer = NULL;
107 wh->dma_bounce_len = 0;
111 scsi_pointer->dma_handle = addr;
114 /* setup dma direction */
118 /* remember direction */
119 wh->dma_dir = dir_in;
123 /* setup DMA *physical* address */
126 /* no more cache flush here - dma_map_single() takes care */
135 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
138 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
139 struct a2091_hostdata *hdata = shost_priv(instance);
140 struct WD33C93_hostdata *wh = &hdata->wh;
141 struct a2091_scsiregs *regs = hdata->regs;
143 /* disable SCSI interrupts */
144 unsigned short cntr = CNTR_PDMD;
149 /* disable SCSI interrupts */
152 /* flush if we were reading */
155 while (!(regs->ISTR & ISTR_FE_FLG))
159 /* clear a possible interrupt */
165 /* restore the CONTROL bits (minus the direction flag) */
166 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
168 dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
169 scsi_pointer->this_residual,
170 DMA_DIR(wh->dma_dir));
172 /* copy from a bounce buffer, if necessary */
173 if (status && wh->dma_bounce_buffer) {
175 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
176 scsi_pointer->this_residual);
177 kfree(wh->dma_bounce_buffer);
178 wh->dma_bounce_buffer = NULL;
179 wh->dma_bounce_len = 0;
183 static const struct scsi_host_template a2091_scsi_template = {
184 .module = THIS_MODULE,
185 .name = "Commodore A2091/A590 SCSI",
186 .show_info = wd33c93_show_info,
187 .write_info = wd33c93_write_info,
188 .proc_name = "A2901",
189 .queuecommand = wd33c93_queuecommand,
190 .eh_abort_handler = wd33c93_abort,
191 .eh_host_reset_handler = wd33c93_host_reset,
192 .can_queue = CAN_QUEUE,
194 .sg_tablesize = SG_ALL,
195 .cmd_per_lun = CMD_PER_LUN,
196 .dma_boundary = PAGE_SIZE - 1,
197 .cmd_size = sizeof(struct scsi_pointer),
200 static int a2091_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
202 struct Scsi_Host *instance;
204 struct a2091_scsiregs *regs;
206 struct a2091_hostdata *hdata;
208 if (dma_set_mask_and_coherent(&z->dev, DMA_BIT_MASK(24))) {
209 dev_warn(&z->dev, "cannot use 24 bit DMA\n");
213 if (!request_mem_region(z->resource.start, 256, "wd33c93"))
216 instance = scsi_host_alloc(&a2091_scsi_template,
217 sizeof(struct a2091_hostdata));
223 instance->irq = IRQ_AMIGA_PORTS;
224 instance->unique_id = z->slotaddr;
226 regs = ZTWO_VADDR(z->resource.start);
227 regs->DAWR = DAWR_A2091;
229 wdregs.SASR = ®s->SASR;
230 wdregs.SCMD = ®s->SCMD;
232 hdata = shost_priv(instance);
233 hdata->dev = &z->dev;
234 hdata->wh.no_sync = 0xff;
236 hdata->wh.dma_mode = CTRL_DMA;
239 wd33c93_init(instance, wdregs, dma_setup, dma_stop, WD33C93_FS_8_10);
240 error = request_irq(IRQ_AMIGA_PORTS, a2091_intr, IRQF_SHARED,
241 "A2091 SCSI", instance);
245 regs->CNTR = CNTR_PDMD | CNTR_INTEN;
247 error = scsi_add_host(instance, NULL);
251 zorro_set_drvdata(z, instance);
253 scsi_scan_host(instance);
257 free_irq(IRQ_AMIGA_PORTS, instance);
259 scsi_host_put(instance);
261 release_mem_region(z->resource.start, 256);
265 static void a2091_remove(struct zorro_dev *z)
267 struct Scsi_Host *instance = zorro_get_drvdata(z);
268 struct a2091_hostdata *hdata = shost_priv(instance);
270 hdata->regs->CNTR = 0;
271 scsi_remove_host(instance);
272 free_irq(IRQ_AMIGA_PORTS, instance);
273 scsi_host_put(instance);
274 release_mem_region(z->resource.start, 256);
277 static struct zorro_device_id a2091_zorro_tbl[] = {
278 { ZORRO_PROD_CBM_A590_A2091_1 },
279 { ZORRO_PROD_CBM_A590_A2091_2 },
282 MODULE_DEVICE_TABLE(zorro, a2091_zorro_tbl);
284 static struct zorro_driver a2091_driver = {
286 .id_table = a2091_zorro_tbl,
287 .probe = a2091_probe,
288 .remove = a2091_remove,
291 static int __init a2091_init(void)
293 return zorro_register_driver(&a2091_driver);
295 module_init(a2091_init);
297 static void __exit a2091_exit(void)
299 zorro_unregister_driver(&a2091_driver);
301 module_exit(a2091_exit);
303 MODULE_DESCRIPTION("Commodore A2091/A590 SCSI");
304 MODULE_LICENSE("GPL");