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>
26 struct gvp11_hostdata {
27 struct WD33C93_hostdata wh;
28 struct gvp11_scsiregs *regs;
31 static irqreturn_t gvp11_intr(int irq, void *data)
33 struct Scsi_Host *instance = data;
34 struct gvp11_hostdata *hdata = shost_priv(instance);
35 unsigned int status = hdata->regs->CNTR;
38 if (!(status & GVP11_DMAC_INT_PENDING))
41 spin_lock_irqsave(instance->host_lock, flags);
42 wd33c93_intr(instance);
43 spin_unlock_irqrestore(instance->host_lock, flags);
47 static int gvp11_xfer_mask = 0;
49 void gvp11_setup(char *str, int *ints)
51 gvp11_xfer_mask = ints[1];
54 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
56 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
57 struct Scsi_Host *instance = cmd->device->host;
58 struct gvp11_hostdata *hdata = shost_priv(instance);
59 struct WD33C93_hostdata *wh = &hdata->wh;
60 struct gvp11_scsiregs *regs = hdata->regs;
61 unsigned short cntr = GVP11_DMAC_INT_ENABLE;
62 unsigned long addr = virt_to_bus(scsi_pointer->ptr);
64 static int scsi_alloc_out_of_range = 0;
66 /* use bounce buffer if the physical address is bad */
67 if (addr & wh->dma_xfer_mask) {
68 wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
70 if (!scsi_alloc_out_of_range) {
71 wh->dma_bounce_buffer =
72 kmalloc(wh->dma_bounce_len, GFP_KERNEL);
73 wh->dma_buffer_pool = BUF_SCSI_ALLOCED;
76 if (scsi_alloc_out_of_range ||
77 !wh->dma_bounce_buffer) {
78 wh->dma_bounce_buffer =
79 amiga_chip_alloc(wh->dma_bounce_len,
80 "GVP II SCSI Bounce Buffer");
82 if (!wh->dma_bounce_buffer) {
83 wh->dma_bounce_len = 0;
87 wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
90 /* check if the address of the bounce buffer is OK */
91 addr = virt_to_bus(wh->dma_bounce_buffer);
93 if (addr & wh->dma_xfer_mask) {
94 /* fall back to Chip RAM if address out of range */
95 if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED) {
96 kfree(wh->dma_bounce_buffer);
97 scsi_alloc_out_of_range = 1;
99 amiga_chip_free(wh->dma_bounce_buffer);
102 wh->dma_bounce_buffer =
103 amiga_chip_alloc(wh->dma_bounce_len,
104 "GVP II SCSI Bounce Buffer");
106 if (!wh->dma_bounce_buffer) {
107 wh->dma_bounce_len = 0;
111 addr = virt_to_bus(wh->dma_bounce_buffer);
112 wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
116 /* copy to bounce buffer for a write */
117 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
118 scsi_pointer->this_residual);
122 /* setup dma direction */
124 cntr |= GVP11_DMAC_DIR_WRITE;
126 wh->dma_dir = dir_in;
129 /* setup DMA *physical* address */
133 /* invalidate any cache */
134 cache_clear(addr, scsi_pointer->this_residual);
136 /* push any dirty cache */
137 cache_push(addr, scsi_pointer->this_residual);
140 bank_mask = (~wh->dma_xfer_mask >> 18) & 0x01c0;
142 regs->BANK = bank_mask & (addr >> 18);
151 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
154 struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
155 struct gvp11_hostdata *hdata = shost_priv(instance);
156 struct WD33C93_hostdata *wh = &hdata->wh;
157 struct gvp11_scsiregs *regs = hdata->regs;
161 /* remove write bit from CONTROL bits */
162 regs->CNTR = GVP11_DMAC_INT_ENABLE;
164 /* copy from a bounce buffer, if necessary */
165 if (status && wh->dma_bounce_buffer) {
166 if (wh->dma_dir && SCpnt)
167 memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
168 scsi_pointer->this_residual);
170 if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED)
171 kfree(wh->dma_bounce_buffer);
173 amiga_chip_free(wh->dma_bounce_buffer);
175 wh->dma_bounce_buffer = NULL;
176 wh->dma_bounce_len = 0;
180 static struct scsi_host_template gvp11_scsi_template = {
181 .module = THIS_MODULE,
182 .name = "GVP Series II SCSI",
183 .show_info = wd33c93_show_info,
184 .write_info = wd33c93_write_info,
185 .proc_name = "GVP11",
186 .queuecommand = wd33c93_queuecommand,
187 .eh_abort_handler = wd33c93_abort,
188 .eh_host_reset_handler = wd33c93_host_reset,
189 .can_queue = CAN_QUEUE,
191 .sg_tablesize = SG_ALL,
192 .cmd_per_lun = CMD_PER_LUN,
193 .dma_boundary = PAGE_SIZE - 1,
194 .cmd_size = sizeof(struct scsi_pointer),
197 static int check_wd33c93(struct gvp11_scsiregs *regs)
200 volatile unsigned char *sasr_3393, *scmd_3393;
201 unsigned char save_sasr;
205 * These darn GVP boards are a problem - it can be tough to tell
206 * whether or not they include a SCSI controller. This is the
207 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
208 * probes for a WD33c93 chip: If we find one, it's extremely
209 * likely that this card supports SCSI, regardless of Product_
210 * Code, Board_Size, etc.
213 /* Get pointers to the presumed register locations and save contents */
215 sasr_3393 = ®s->SASR;
216 scmd_3393 = ®s->SCMD;
217 save_sasr = *sasr_3393;
219 /* First test the AuxStatus Reg */
221 q = *sasr_3393; /* read it */
222 if (q & 0x08) /* bit 3 should always be clear */
224 *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
225 if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
226 *sasr_3393 = save_sasr; /* Oops - restore this byte */
229 if (*sasr_3393 != q) { /* should still read the same */
230 *sasr_3393 = save_sasr; /* Oops - restore this byte */
233 if (*scmd_3393 != q) /* and so should the image at 0x1f */
237 * Ok, we probably have a wd33c93, but let's check a few other places
238 * for good measure. Make sure that this works for both 'A and 'B
242 *sasr_3393 = WD_SCSI_STATUS;
244 *sasr_3393 = WD_SCSI_STATUS;
246 *sasr_3393 = WD_SCSI_STATUS;
248 *sasr_3393 = WD_SCSI_STATUS;
250 if (qq != q) /* should be read only */
252 *sasr_3393 = 0x1e; /* this register is unimplemented */
260 if (qq != q || qq != 0xff) /* should be read only, all 1's */
262 *sasr_3393 = WD_TIMEOUT_PERIOD;
264 *sasr_3393 = WD_TIMEOUT_PERIOD;
266 *sasr_3393 = WD_TIMEOUT_PERIOD;
268 *sasr_3393 = WD_TIMEOUT_PERIOD;
270 if (qq != (~q & 0xff)) /* should be read/write */
272 #endif /* CHECK_WD33C93 */
277 static int gvp11_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
279 struct Scsi_Host *instance;
280 unsigned long address;
283 unsigned int default_dma_xfer_mask;
284 struct gvp11_hostdata *hdata;
285 struct gvp11_scsiregs *regs;
288 default_dma_xfer_mask = ent->driver_data;
291 * Rumors state that some GVP ram boards use the same product
292 * code as the SCSI controllers. Therefore if the board-size
293 * is not 64KB we assume it is a ram board and bail out.
295 if (zorro_resource_len(z) != 0x10000)
298 address = z->resource.start;
299 if (!request_mem_region(address, 256, "wd33c93"))
302 regs = ZTWO_VADDR(address);
304 error = check_wd33c93(regs);
306 goto fail_check_or_alloc;
308 instance = scsi_host_alloc(&gvp11_scsi_template,
309 sizeof(struct gvp11_hostdata));
312 goto fail_check_or_alloc;
315 instance->irq = IRQ_AMIGA_PORTS;
316 instance->unique_id = z->slotaddr;
321 while (regs->CNTR & GVP11_DMAC_BUSY)
326 wdregs.SASR = ®s->SASR;
327 wdregs.SCMD = ®s->SCMD;
329 hdata = shost_priv(instance);
331 hdata->wh.dma_xfer_mask = gvp11_xfer_mask;
333 hdata->wh.dma_xfer_mask = default_dma_xfer_mask;
335 hdata->wh.no_sync = 0xff;
337 hdata->wh.dma_mode = CTRL_DMA;
341 * Check for 14MHz SCSI clock
343 epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
344 wd33c93_init(instance, wdregs, dma_setup, dma_stop,
345 (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
348 error = request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
349 "GVP11 SCSI", instance);
353 regs->CNTR = GVP11_DMAC_INT_ENABLE;
355 error = scsi_add_host(instance, NULL);
359 zorro_set_drvdata(z, instance);
360 scsi_scan_host(instance);
364 free_irq(IRQ_AMIGA_PORTS, instance);
366 scsi_host_put(instance);
368 release_mem_region(address, 256);
372 static void gvp11_remove(struct zorro_dev *z)
374 struct Scsi_Host *instance = zorro_get_drvdata(z);
375 struct gvp11_hostdata *hdata = shost_priv(instance);
377 hdata->regs->CNTR = 0;
378 scsi_remove_host(instance);
379 free_irq(IRQ_AMIGA_PORTS, instance);
380 scsi_host_put(instance);
381 release_mem_region(z->resource.start, 256);
385 * This should (hopefully) be the correct way to identify
386 * all the different GVP SCSI controllers (except for the
390 static struct zorro_device_id gvp11_zorro_tbl[] = {
391 { ZORRO_PROD_GVP_COMBO_030_R3_SCSI, ~0x00ffffff },
392 { ZORRO_PROD_GVP_SERIES_II, ~0x00ffffff },
393 { ZORRO_PROD_GVP_GFORCE_030_SCSI, ~0x01ffffff },
394 { ZORRO_PROD_GVP_A530_SCSI, ~0x01ffffff },
395 { ZORRO_PROD_GVP_COMBO_030_R4_SCSI, ~0x01ffffff },
396 { ZORRO_PROD_GVP_A1291, ~0x07ffffff },
397 { ZORRO_PROD_GVP_GFORCE_040_SCSI_1, ~0x07ffffff },
400 MODULE_DEVICE_TABLE(zorro, gvp11_zorro_tbl);
402 static struct zorro_driver gvp11_driver = {
404 .id_table = gvp11_zorro_tbl,
405 .probe = gvp11_probe,
406 .remove = gvp11_remove,
409 static int __init gvp11_init(void)
411 return zorro_register_driver(&gvp11_driver);
413 module_init(gvp11_init);
415 static void __exit gvp11_exit(void)
417 zorro_unregister_driver(&gvp11_driver);
419 module_exit(gvp11_exit);
421 MODULE_DESCRIPTION("GVP Series II SCSI");
422 MODULE_LICENSE("GPL");