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/pgtable.h>
13 #include <asm/amigaints.h>
14 #include <asm/amigahw.h>
23 struct gvp11_hostdata {
24 struct WD33C93_hostdata wh;
25 struct gvp11_scsiregs *regs;
28 static irqreturn_t gvp11_intr(int irq, void *data)
30 struct Scsi_Host *instance = data;
31 struct gvp11_hostdata *hdata = shost_priv(instance);
32 unsigned int status = hdata->regs->CNTR;
35 if (!(status & GVP11_DMAC_INT_PENDING))
38 spin_lock_irqsave(instance->host_lock, flags);
39 wd33c93_intr(instance);
40 spin_unlock_irqrestore(instance->host_lock, flags);
44 static int gvp11_xfer_mask = 0;
46 void gvp11_setup(char *str, int *ints)
48 gvp11_xfer_mask = ints[1];
51 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
53 struct Scsi_Host *instance = cmd->device->host;
54 struct gvp11_hostdata *hdata = shost_priv(instance);
55 struct WD33C93_hostdata *wh = &hdata->wh;
56 struct gvp11_scsiregs *regs = hdata->regs;
57 unsigned short cntr = GVP11_DMAC_INT_ENABLE;
58 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
60 static int scsi_alloc_out_of_range = 0;
62 /* use bounce buffer if the physical address is bad */
63 if (addr & wh->dma_xfer_mask) {
64 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
66 if (!scsi_alloc_out_of_range) {
67 wh->dma_bounce_buffer =
68 kmalloc(wh->dma_bounce_len, GFP_KERNEL);
69 wh->dma_buffer_pool = BUF_SCSI_ALLOCED;
72 if (scsi_alloc_out_of_range ||
73 !wh->dma_bounce_buffer) {
74 wh->dma_bounce_buffer =
75 amiga_chip_alloc(wh->dma_bounce_len,
76 "GVP II SCSI Bounce Buffer");
78 if (!wh->dma_bounce_buffer) {
79 wh->dma_bounce_len = 0;
83 wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
86 /* check if the address of the bounce buffer is OK */
87 addr = virt_to_bus(wh->dma_bounce_buffer);
89 if (addr & wh->dma_xfer_mask) {
90 /* fall back to Chip RAM if address out of range */
91 if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED) {
92 kfree(wh->dma_bounce_buffer);
93 scsi_alloc_out_of_range = 1;
95 amiga_chip_free(wh->dma_bounce_buffer);
98 wh->dma_bounce_buffer =
99 amiga_chip_alloc(wh->dma_bounce_len,
100 "GVP II SCSI Bounce Buffer");
102 if (!wh->dma_bounce_buffer) {
103 wh->dma_bounce_len = 0;
107 addr = virt_to_bus(wh->dma_bounce_buffer);
108 wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
112 /* copy to bounce buffer for a write */
113 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
114 cmd->SCp.this_residual);
118 /* setup dma direction */
120 cntr |= GVP11_DMAC_DIR_WRITE;
122 wh->dma_dir = dir_in;
125 /* setup DMA *physical* address */
129 /* invalidate any cache */
130 cache_clear(addr, cmd->SCp.this_residual);
132 /* push any dirty cache */
133 cache_push(addr, cmd->SCp.this_residual);
136 bank_mask = (~wh->dma_xfer_mask >> 18) & 0x01c0;
138 regs->BANK = bank_mask & (addr >> 18);
147 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
150 struct gvp11_hostdata *hdata = shost_priv(instance);
151 struct WD33C93_hostdata *wh = &hdata->wh;
152 struct gvp11_scsiregs *regs = hdata->regs;
156 /* remove write bit from CONTROL bits */
157 regs->CNTR = GVP11_DMAC_INT_ENABLE;
159 /* copy from a bounce buffer, if necessary */
160 if (status && wh->dma_bounce_buffer) {
161 if (wh->dma_dir && SCpnt)
162 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
163 SCpnt->SCp.this_residual);
165 if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED)
166 kfree(wh->dma_bounce_buffer);
168 amiga_chip_free(wh->dma_bounce_buffer);
170 wh->dma_bounce_buffer = NULL;
171 wh->dma_bounce_len = 0;
175 static struct scsi_host_template gvp11_scsi_template = {
176 .module = THIS_MODULE,
177 .name = "GVP Series II SCSI",
178 .show_info = wd33c93_show_info,
179 .write_info = wd33c93_write_info,
180 .proc_name = "GVP11",
181 .queuecommand = wd33c93_queuecommand,
182 .eh_abort_handler = wd33c93_abort,
183 .eh_host_reset_handler = wd33c93_host_reset,
184 .can_queue = CAN_QUEUE,
186 .sg_tablesize = SG_ALL,
187 .cmd_per_lun = CMD_PER_LUN,
188 .dma_boundary = PAGE_SIZE - 1,
191 static int check_wd33c93(struct gvp11_scsiregs *regs)
194 volatile unsigned char *sasr_3393, *scmd_3393;
195 unsigned char save_sasr;
199 * These darn GVP boards are a problem - it can be tough to tell
200 * whether or not they include a SCSI controller. This is the
201 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
202 * probes for a WD33c93 chip: If we find one, it's extremely
203 * likely that this card supports SCSI, regardless of Product_
204 * Code, Board_Size, etc.
207 /* Get pointers to the presumed register locations and save contents */
209 sasr_3393 = ®s->SASR;
210 scmd_3393 = ®s->SCMD;
211 save_sasr = *sasr_3393;
213 /* First test the AuxStatus Reg */
215 q = *sasr_3393; /* read it */
216 if (q & 0x08) /* bit 3 should always be clear */
218 *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
219 if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
220 *sasr_3393 = save_sasr; /* Oops - restore this byte */
223 if (*sasr_3393 != q) { /* should still read the same */
224 *sasr_3393 = save_sasr; /* Oops - restore this byte */
227 if (*scmd_3393 != q) /* and so should the image at 0x1f */
231 * Ok, we probably have a wd33c93, but let's check a few other places
232 * for good measure. Make sure that this works for both 'A and 'B
236 *sasr_3393 = WD_SCSI_STATUS;
238 *sasr_3393 = WD_SCSI_STATUS;
240 *sasr_3393 = WD_SCSI_STATUS;
242 *sasr_3393 = WD_SCSI_STATUS;
244 if (qq != q) /* should be read only */
246 *sasr_3393 = 0x1e; /* this register is unimplemented */
254 if (qq != q || qq != 0xff) /* should be read only, all 1's */
256 *sasr_3393 = WD_TIMEOUT_PERIOD;
258 *sasr_3393 = WD_TIMEOUT_PERIOD;
260 *sasr_3393 = WD_TIMEOUT_PERIOD;
262 *sasr_3393 = WD_TIMEOUT_PERIOD;
264 if (qq != (~q & 0xff)) /* should be read/write */
266 #endif /* CHECK_WD33C93 */
271 static int gvp11_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
273 struct Scsi_Host *instance;
274 unsigned long address;
277 unsigned int default_dma_xfer_mask;
278 struct gvp11_hostdata *hdata;
279 struct gvp11_scsiregs *regs;
282 default_dma_xfer_mask = ent->driver_data;
285 * Rumors state that some GVP ram boards use the same product
286 * code as the SCSI controllers. Therefore if the board-size
287 * is not 64KB we assume it is a ram board and bail out.
289 if (zorro_resource_len(z) != 0x10000)
292 address = z->resource.start;
293 if (!request_mem_region(address, 256, "wd33c93"))
296 regs = ZTWO_VADDR(address);
298 error = check_wd33c93(regs);
300 goto fail_check_or_alloc;
302 instance = scsi_host_alloc(&gvp11_scsi_template,
303 sizeof(struct gvp11_hostdata));
306 goto fail_check_or_alloc;
309 instance->irq = IRQ_AMIGA_PORTS;
310 instance->unique_id = z->slotaddr;
315 while (regs->CNTR & GVP11_DMAC_BUSY)
320 wdregs.SASR = ®s->SASR;
321 wdregs.SCMD = ®s->SCMD;
323 hdata = shost_priv(instance);
325 hdata->wh.dma_xfer_mask = gvp11_xfer_mask;
327 hdata->wh.dma_xfer_mask = default_dma_xfer_mask;
329 hdata->wh.no_sync = 0xff;
331 hdata->wh.dma_mode = CTRL_DMA;
335 * Check for 14MHz SCSI clock
337 epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
338 wd33c93_init(instance, wdregs, dma_setup, dma_stop,
339 (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
342 error = request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
343 "GVP11 SCSI", instance);
347 regs->CNTR = GVP11_DMAC_INT_ENABLE;
349 error = scsi_add_host(instance, NULL);
353 zorro_set_drvdata(z, instance);
354 scsi_scan_host(instance);
358 free_irq(IRQ_AMIGA_PORTS, instance);
360 scsi_host_put(instance);
362 release_mem_region(address, 256);
366 static void gvp11_remove(struct zorro_dev *z)
368 struct Scsi_Host *instance = zorro_get_drvdata(z);
369 struct gvp11_hostdata *hdata = shost_priv(instance);
371 hdata->regs->CNTR = 0;
372 scsi_remove_host(instance);
373 free_irq(IRQ_AMIGA_PORTS, instance);
374 scsi_host_put(instance);
375 release_mem_region(z->resource.start, 256);
379 * This should (hopefully) be the correct way to identify
380 * all the different GVP SCSI controllers (except for the
384 static struct zorro_device_id gvp11_zorro_tbl[] = {
385 { ZORRO_PROD_GVP_COMBO_030_R3_SCSI, ~0x00ffffff },
386 { ZORRO_PROD_GVP_SERIES_II, ~0x00ffffff },
387 { ZORRO_PROD_GVP_GFORCE_030_SCSI, ~0x01ffffff },
388 { ZORRO_PROD_GVP_A530_SCSI, ~0x01ffffff },
389 { ZORRO_PROD_GVP_COMBO_030_R4_SCSI, ~0x01ffffff },
390 { ZORRO_PROD_GVP_A1291, ~0x07ffffff },
391 { ZORRO_PROD_GVP_GFORCE_040_SCSI_1, ~0x07ffffff },
394 MODULE_DEVICE_TABLE(zorro, gvp11_zorro_tbl);
396 static struct zorro_driver gvp11_driver = {
398 .id_table = gvp11_zorro_tbl,
399 .probe = gvp11_probe,
400 .remove = gvp11_remove,
403 static int __init gvp11_init(void)
405 return zorro_register_driver(&gvp11_driver);
407 module_init(gvp11_init);
409 static void __exit gvp11_exit(void)
411 zorro_unregister_driver(&gvp11_driver);
413 module_exit(gvp11_exit);
415 MODULE_DESCRIPTION("GVP Series II SCSI");
416 MODULE_LICENSE("GPL");