1 #include <linux/types.h>
2 #include <linux/init.h>
3 #include <linux/interrupt.h>
5 #include <linux/slab.h>
6 #include <linux/spinlock.h>
7 #include <linux/zorro.h>
8 #include <linux/module.h>
11 #include <asm/pgtable.h>
12 #include <asm/amigaints.h>
13 #include <asm/amigahw.h>
22 struct gvp11_hostdata {
23 struct WD33C93_hostdata wh;
24 struct gvp11_scsiregs *regs;
27 static irqreturn_t gvp11_intr(int irq, void *data)
29 struct Scsi_Host *instance = data;
30 struct gvp11_hostdata *hdata = shost_priv(instance);
31 unsigned int status = hdata->regs->CNTR;
34 if (!(status & GVP11_DMAC_INT_PENDING))
37 spin_lock_irqsave(instance->host_lock, flags);
38 wd33c93_intr(instance);
39 spin_unlock_irqrestore(instance->host_lock, flags);
43 static int gvp11_xfer_mask = 0;
45 void gvp11_setup(char *str, int *ints)
47 gvp11_xfer_mask = ints[1];
50 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
52 struct Scsi_Host *instance = cmd->device->host;
53 struct gvp11_hostdata *hdata = shost_priv(instance);
54 struct WD33C93_hostdata *wh = &hdata->wh;
55 struct gvp11_scsiregs *regs = hdata->regs;
56 unsigned short cntr = GVP11_DMAC_INT_ENABLE;
57 unsigned long addr = virt_to_bus(cmd->SCp.ptr);
59 static int scsi_alloc_out_of_range = 0;
61 /* use bounce buffer if the physical address is bad */
62 if (addr & wh->dma_xfer_mask) {
63 wh->dma_bounce_len = (cmd->SCp.this_residual + 511) & ~0x1ff;
65 if (!scsi_alloc_out_of_range) {
66 wh->dma_bounce_buffer =
67 kmalloc(wh->dma_bounce_len, GFP_KERNEL);
68 wh->dma_buffer_pool = BUF_SCSI_ALLOCED;
71 if (scsi_alloc_out_of_range ||
72 !wh->dma_bounce_buffer) {
73 wh->dma_bounce_buffer =
74 amiga_chip_alloc(wh->dma_bounce_len,
75 "GVP II SCSI Bounce Buffer");
77 if (!wh->dma_bounce_buffer) {
78 wh->dma_bounce_len = 0;
82 wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
85 /* check if the address of the bounce buffer is OK */
86 addr = virt_to_bus(wh->dma_bounce_buffer);
88 if (addr & wh->dma_xfer_mask) {
89 /* fall back to Chip RAM if address out of range */
90 if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED) {
91 kfree(wh->dma_bounce_buffer);
92 scsi_alloc_out_of_range = 1;
94 amiga_chip_free(wh->dma_bounce_buffer);
97 wh->dma_bounce_buffer =
98 amiga_chip_alloc(wh->dma_bounce_len,
99 "GVP II SCSI Bounce Buffer");
101 if (!wh->dma_bounce_buffer) {
102 wh->dma_bounce_len = 0;
106 addr = virt_to_bus(wh->dma_bounce_buffer);
107 wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
111 /* copy to bounce buffer for a write */
112 memcpy(wh->dma_bounce_buffer, cmd->SCp.ptr,
113 cmd->SCp.this_residual);
117 /* setup dma direction */
119 cntr |= GVP11_DMAC_DIR_WRITE;
121 wh->dma_dir = dir_in;
124 /* setup DMA *physical* address */
128 /* invalidate any cache */
129 cache_clear(addr, cmd->SCp.this_residual);
131 /* push any dirty cache */
132 cache_push(addr, cmd->SCp.this_residual);
135 bank_mask = (~wh->dma_xfer_mask >> 18) & 0x01c0;
137 regs->BANK = bank_mask & (addr >> 18);
146 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
149 struct gvp11_hostdata *hdata = shost_priv(instance);
150 struct WD33C93_hostdata *wh = &hdata->wh;
151 struct gvp11_scsiregs *regs = hdata->regs;
155 /* remove write bit from CONTROL bits */
156 regs->CNTR = GVP11_DMAC_INT_ENABLE;
158 /* copy from a bounce buffer, if necessary */
159 if (status && wh->dma_bounce_buffer) {
160 if (wh->dma_dir && SCpnt)
161 memcpy(SCpnt->SCp.ptr, wh->dma_bounce_buffer,
162 SCpnt->SCp.this_residual);
164 if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED)
165 kfree(wh->dma_bounce_buffer);
167 amiga_chip_free(wh->dma_bounce_buffer);
169 wh->dma_bounce_buffer = NULL;
170 wh->dma_bounce_len = 0;
174 static int gvp11_bus_reset(struct scsi_cmnd *cmd)
176 struct Scsi_Host *instance = cmd->device->host;
178 /* FIXME perform bus-specific reset */
180 /* FIXME 2: shouldn't we no-op this function (return
181 FAILED), and fall back to host reset function,
182 wd33c93_host_reset ? */
184 spin_lock_irq(instance->host_lock);
185 wd33c93_host_reset(cmd);
186 spin_unlock_irq(instance->host_lock);
191 static struct scsi_host_template gvp11_scsi_template = {
192 .module = THIS_MODULE,
193 .name = "GVP Series II SCSI",
194 .show_info = wd33c93_show_info,
195 .write_info = wd33c93_write_info,
196 .proc_name = "GVP11",
197 .queuecommand = wd33c93_queuecommand,
198 .eh_abort_handler = wd33c93_abort,
199 .eh_bus_reset_handler = gvp11_bus_reset,
200 .eh_host_reset_handler = wd33c93_host_reset,
201 .can_queue = CAN_QUEUE,
203 .sg_tablesize = SG_ALL,
204 .cmd_per_lun = CMD_PER_LUN,
205 .use_clustering = DISABLE_CLUSTERING
208 static int check_wd33c93(struct gvp11_scsiregs *regs)
211 volatile unsigned char *sasr_3393, *scmd_3393;
212 unsigned char save_sasr;
216 * These darn GVP boards are a problem - it can be tough to tell
217 * whether or not they include a SCSI controller. This is the
218 * ultimate Yet-Another-GVP-Detection-Hack in that it actually
219 * probes for a WD33c93 chip: If we find one, it's extremely
220 * likely that this card supports SCSI, regardless of Product_
221 * Code, Board_Size, etc.
224 /* Get pointers to the presumed register locations and save contents */
226 sasr_3393 = ®s->SASR;
227 scmd_3393 = ®s->SCMD;
228 save_sasr = *sasr_3393;
230 /* First test the AuxStatus Reg */
232 q = *sasr_3393; /* read it */
233 if (q & 0x08) /* bit 3 should always be clear */
235 *sasr_3393 = WD_AUXILIARY_STATUS; /* setup indirect address */
236 if (*sasr_3393 == WD_AUXILIARY_STATUS) { /* shouldn't retain the write */
237 *sasr_3393 = save_sasr; /* Oops - restore this byte */
240 if (*sasr_3393 != q) { /* should still read the same */
241 *sasr_3393 = save_sasr; /* Oops - restore this byte */
244 if (*scmd_3393 != q) /* and so should the image at 0x1f */
248 * Ok, we probably have a wd33c93, but let's check a few other places
249 * for good measure. Make sure that this works for both 'A and 'B
253 *sasr_3393 = WD_SCSI_STATUS;
255 *sasr_3393 = WD_SCSI_STATUS;
257 *sasr_3393 = WD_SCSI_STATUS;
259 *sasr_3393 = WD_SCSI_STATUS;
261 if (qq != q) /* should be read only */
263 *sasr_3393 = 0x1e; /* this register is unimplemented */
271 if (qq != q || qq != 0xff) /* should be read only, all 1's */
273 *sasr_3393 = WD_TIMEOUT_PERIOD;
275 *sasr_3393 = WD_TIMEOUT_PERIOD;
277 *sasr_3393 = WD_TIMEOUT_PERIOD;
279 *sasr_3393 = WD_TIMEOUT_PERIOD;
281 if (qq != (~q & 0xff)) /* should be read/write */
283 #endif /* CHECK_WD33C93 */
288 static int gvp11_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
290 struct Scsi_Host *instance;
291 unsigned long address;
294 unsigned int default_dma_xfer_mask;
295 struct gvp11_hostdata *hdata;
296 struct gvp11_scsiregs *regs;
299 default_dma_xfer_mask = ent->driver_data;
302 * Rumors state that some GVP ram boards use the same product
303 * code as the SCSI controllers. Therefore if the board-size
304 * is not 64KB we assume it is a ram board and bail out.
306 if (zorro_resource_len(z) != 0x10000)
309 address = z->resource.start;
310 if (!request_mem_region(address, 256, "wd33c93"))
313 regs = (struct gvp11_scsiregs *)(ZTWO_VADDR(address));
315 error = check_wd33c93(regs);
317 goto fail_check_or_alloc;
319 instance = scsi_host_alloc(&gvp11_scsi_template,
320 sizeof(struct gvp11_hostdata));
323 goto fail_check_or_alloc;
326 instance->irq = IRQ_AMIGA_PORTS;
327 instance->unique_id = z->slotaddr;
332 while (regs->CNTR & GVP11_DMAC_BUSY)
337 wdregs.SASR = ®s->SASR;
338 wdregs.SCMD = ®s->SCMD;
340 hdata = shost_priv(instance);
342 hdata->wh.dma_xfer_mask = gvp11_xfer_mask;
344 hdata->wh.dma_xfer_mask = default_dma_xfer_mask;
346 hdata->wh.no_sync = 0xff;
348 hdata->wh.dma_mode = CTRL_DMA;
352 * Check for 14MHz SCSI clock
354 epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
355 wd33c93_init(instance, wdregs, dma_setup, dma_stop,
356 (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
359 error = request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
360 "GVP11 SCSI", instance);
364 regs->CNTR = GVP11_DMAC_INT_ENABLE;
366 error = scsi_add_host(instance, NULL);
370 zorro_set_drvdata(z, instance);
371 scsi_scan_host(instance);
375 free_irq(IRQ_AMIGA_PORTS, instance);
377 scsi_host_put(instance);
379 release_mem_region(address, 256);
383 static void gvp11_remove(struct zorro_dev *z)
385 struct Scsi_Host *instance = zorro_get_drvdata(z);
386 struct gvp11_hostdata *hdata = shost_priv(instance);
388 hdata->regs->CNTR = 0;
389 scsi_remove_host(instance);
390 free_irq(IRQ_AMIGA_PORTS, instance);
391 scsi_host_put(instance);
392 release_mem_region(z->resource.start, 256);
396 * This should (hopefully) be the correct way to identify
397 * all the different GVP SCSI controllers (except for the
401 static struct zorro_device_id gvp11_zorro_tbl[] = {
402 { ZORRO_PROD_GVP_COMBO_030_R3_SCSI, ~0x00ffffff },
403 { ZORRO_PROD_GVP_SERIES_II, ~0x00ffffff },
404 { ZORRO_PROD_GVP_GFORCE_030_SCSI, ~0x01ffffff },
405 { ZORRO_PROD_GVP_A530_SCSI, ~0x01ffffff },
406 { ZORRO_PROD_GVP_COMBO_030_R4_SCSI, ~0x01ffffff },
407 { ZORRO_PROD_GVP_A1291, ~0x07ffffff },
408 { ZORRO_PROD_GVP_GFORCE_040_SCSI_1, ~0x07ffffff },
411 MODULE_DEVICE_TABLE(zorro, gvp11_zorro_tbl);
413 static struct zorro_driver gvp11_driver = {
415 .id_table = gvp11_zorro_tbl,
416 .probe = gvp11_probe,
417 .remove = gvp11_remove,
420 static int __init gvp11_init(void)
422 return zorro_register_driver(&gvp11_driver);
424 module_init(gvp11_init);
426 static void __exit gvp11_exit(void)
428 zorro_unregister_driver(&gvp11_driver);
430 module_exit(gvp11_exit);
432 MODULE_DESCRIPTION("GVP Series II SCSI");
433 MODULE_LICENSE("GPL");