Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 #include <linux/types.h>
0003 #include <linux/init.h>
0004 #include <linux/interrupt.h>
0005 #include <linux/mm.h>
0006 #include <linux/slab.h>
0007 #include <linux/spinlock.h>
0008 #include <linux/zorro.h>
0009 #include <linux/module.h>
0010 
0011 #include <asm/page.h>
0012 #include <asm/amigaints.h>
0013 #include <asm/amigahw.h>
0014 
0015 #include <scsi/scsi.h>
0016 #include <scsi/scsi_cmnd.h>
0017 #include <scsi/scsi_device.h>
0018 #include <scsi/scsi_eh.h>
0019 #include <scsi/scsi_tcq.h>
0020 #include "wd33c93.h"
0021 #include "gvp11.h"
0022 
0023 
0024 #define CHECK_WD33C93
0025 
0026 struct gvp11_hostdata {
0027     struct WD33C93_hostdata wh;
0028     struct gvp11_scsiregs *regs;
0029     struct device *dev;
0030 };
0031 
0032 #define DMA_DIR(d)   ((d == DATA_OUT_DIR) ? DMA_TO_DEVICE : DMA_FROM_DEVICE)
0033 #define TO_DMA_MASK(m)  (~((unsigned long long)m & 0xffffffff))
0034 
0035 static irqreturn_t gvp11_intr(int irq, void *data)
0036 {
0037     struct Scsi_Host *instance = data;
0038     struct gvp11_hostdata *hdata = shost_priv(instance);
0039     unsigned int status = hdata->regs->CNTR;
0040     unsigned long flags;
0041 
0042     if (!(status & GVP11_DMAC_INT_PENDING))
0043         return IRQ_NONE;
0044 
0045     spin_lock_irqsave(instance->host_lock, flags);
0046     wd33c93_intr(instance);
0047     spin_unlock_irqrestore(instance->host_lock, flags);
0048     return IRQ_HANDLED;
0049 }
0050 
0051 static int gvp11_xfer_mask = 0;
0052 
0053 void gvp11_setup(char *str, int *ints)
0054 {
0055     gvp11_xfer_mask = ints[1];
0056 }
0057 
0058 static int dma_setup(struct scsi_cmnd *cmd, int dir_in)
0059 {
0060     struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(cmd);
0061     unsigned long len = scsi_pointer->this_residual;
0062     struct Scsi_Host *instance = cmd->device->host;
0063     struct gvp11_hostdata *hdata = shost_priv(instance);
0064     struct WD33C93_hostdata *wh = &hdata->wh;
0065     struct gvp11_scsiregs *regs = hdata->regs;
0066     unsigned short cntr = GVP11_DMAC_INT_ENABLE;
0067     dma_addr_t addr;
0068     int bank_mask;
0069     static int scsi_alloc_out_of_range = 0;
0070 
0071     addr = dma_map_single(hdata->dev, scsi_pointer->ptr,
0072                   len, DMA_DIR(dir_in));
0073     if (dma_mapping_error(hdata->dev, addr)) {
0074         dev_warn(hdata->dev, "cannot map SCSI data block %p\n",
0075              scsi_pointer->ptr);
0076         return 1;
0077     }
0078     scsi_pointer->dma_handle = addr;
0079 
0080     /* use bounce buffer if the physical address is bad */
0081     if (addr & wh->dma_xfer_mask) {
0082         /* drop useless mapping */
0083         dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
0084                  scsi_pointer->this_residual,
0085                  DMA_DIR(dir_in));
0086         scsi_pointer->dma_handle = (dma_addr_t) NULL;
0087 
0088         wh->dma_bounce_len = (scsi_pointer->this_residual + 511) & ~0x1ff;
0089 
0090         if (!scsi_alloc_out_of_range) {
0091             wh->dma_bounce_buffer =
0092                 kmalloc(wh->dma_bounce_len, GFP_KERNEL);
0093             wh->dma_buffer_pool = BUF_SCSI_ALLOCED;
0094         }
0095 
0096         if (scsi_alloc_out_of_range ||
0097             !wh->dma_bounce_buffer) {
0098             wh->dma_bounce_buffer =
0099                 amiga_chip_alloc(wh->dma_bounce_len,
0100                          "GVP II SCSI Bounce Buffer");
0101 
0102             if (!wh->dma_bounce_buffer) {
0103                 wh->dma_bounce_len = 0;
0104                 return 1;
0105             }
0106 
0107             wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
0108         }
0109 
0110         if (!dir_in) {
0111             /* copy to bounce buffer for a write */
0112             memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
0113                    scsi_pointer->this_residual);
0114         }
0115 
0116         if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED) {
0117         /* will flush/invalidate cache for us */
0118             addr = dma_map_single(hdata->dev,
0119                           wh->dma_bounce_buffer,
0120                           wh->dma_bounce_len,
0121                           DMA_DIR(dir_in));
0122             /* can't map buffer; use PIO */
0123             if (dma_mapping_error(hdata->dev, addr)) {
0124                 dev_warn(hdata->dev,
0125                      "cannot map bounce buffer %p\n",
0126                      wh->dma_bounce_buffer);
0127                 return 1;
0128             }
0129         }
0130 
0131         if (addr & wh->dma_xfer_mask) {
0132             /* drop useless mapping */
0133             dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
0134                      scsi_pointer->this_residual,
0135                      DMA_DIR(dir_in));
0136             /* fall back to Chip RAM if address out of range */
0137             if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED) {
0138                 kfree(wh->dma_bounce_buffer);
0139                 scsi_alloc_out_of_range = 1;
0140             } else {
0141                 amiga_chip_free(wh->dma_bounce_buffer);
0142             }
0143 
0144             wh->dma_bounce_buffer =
0145                 amiga_chip_alloc(wh->dma_bounce_len,
0146                          "GVP II SCSI Bounce Buffer");
0147 
0148             if (!wh->dma_bounce_buffer) {
0149                 wh->dma_bounce_len = 0;
0150                 return 1;
0151             }
0152 
0153             if (!dir_in) {
0154                 /* copy to bounce buffer for a write */
0155                 memcpy(wh->dma_bounce_buffer, scsi_pointer->ptr,
0156                        scsi_pointer->this_residual);
0157             }
0158             /* chip RAM can be mapped to phys. address directly */
0159             addr = virt_to_phys(wh->dma_bounce_buffer);
0160             /* no need to flush/invalidate cache */
0161             wh->dma_buffer_pool = BUF_CHIP_ALLOCED;
0162         }
0163         /* finally, have OK mapping (punted for PIO else) */
0164         scsi_pointer->dma_handle = addr;
0165 
0166     }
0167 
0168     /* setup dma direction */
0169     if (!dir_in)
0170         cntr |= GVP11_DMAC_DIR_WRITE;
0171 
0172     wh->dma_dir = dir_in;
0173     regs->CNTR = cntr;
0174 
0175     /* setup DMA *physical* address */
0176     regs->ACR = addr;
0177 
0178     /* no more cache flush here - dma_map_single() takes care */
0179 
0180     bank_mask = (~wh->dma_xfer_mask >> 18) & 0x01c0;
0181     if (bank_mask)
0182         regs->BANK = bank_mask & (addr >> 18);
0183 
0184     /* start DMA */
0185     regs->ST_DMA = 1;
0186 
0187     /* return success */
0188     return 0;
0189 }
0190 
0191 static void dma_stop(struct Scsi_Host *instance, struct scsi_cmnd *SCpnt,
0192              int status)
0193 {
0194     struct scsi_pointer *scsi_pointer = WD33C93_scsi_pointer(SCpnt);
0195     struct gvp11_hostdata *hdata = shost_priv(instance);
0196     struct WD33C93_hostdata *wh = &hdata->wh;
0197     struct gvp11_scsiregs *regs = hdata->regs;
0198 
0199     /* stop DMA */
0200     regs->SP_DMA = 1;
0201     /* remove write bit from CONTROL bits */
0202     regs->CNTR = GVP11_DMAC_INT_ENABLE;
0203 
0204     if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED)
0205         dma_unmap_single(hdata->dev, scsi_pointer->dma_handle,
0206                  scsi_pointer->this_residual,
0207                  DMA_DIR(wh->dma_dir));
0208 
0209     /* copy from a bounce buffer, if necessary */
0210     if (status && wh->dma_bounce_buffer) {
0211         if (wh->dma_dir && SCpnt)
0212             memcpy(scsi_pointer->ptr, wh->dma_bounce_buffer,
0213                    scsi_pointer->this_residual);
0214 
0215         if (wh->dma_buffer_pool == BUF_SCSI_ALLOCED)
0216             kfree(wh->dma_bounce_buffer);
0217         else
0218             amiga_chip_free(wh->dma_bounce_buffer);
0219 
0220         wh->dma_bounce_buffer = NULL;
0221         wh->dma_bounce_len = 0;
0222     }
0223 }
0224 
0225 static struct scsi_host_template gvp11_scsi_template = {
0226     .module         = THIS_MODULE,
0227     .name           = "GVP Series II SCSI",
0228     .show_info      = wd33c93_show_info,
0229     .write_info     = wd33c93_write_info,
0230     .proc_name      = "GVP11",
0231     .queuecommand       = wd33c93_queuecommand,
0232     .eh_abort_handler   = wd33c93_abort,
0233     .eh_host_reset_handler  = wd33c93_host_reset,
0234     .can_queue      = CAN_QUEUE,
0235     .this_id        = 7,
0236     .sg_tablesize       = SG_ALL,
0237     .cmd_per_lun        = CMD_PER_LUN,
0238     .dma_boundary       = PAGE_SIZE - 1,
0239     .cmd_size       = sizeof(struct scsi_pointer),
0240 };
0241 
0242 static int check_wd33c93(struct gvp11_scsiregs *regs)
0243 {
0244 #ifdef CHECK_WD33C93
0245     volatile unsigned char *sasr_3393, *scmd_3393;
0246     unsigned char save_sasr;
0247     unsigned char q, qq;
0248 
0249     /*
0250      * These darn GVP boards are a problem - it can be tough to tell
0251      * whether or not they include a SCSI controller. This is the
0252      * ultimate Yet-Another-GVP-Detection-Hack in that it actually
0253      * probes for a WD33c93 chip: If we find one, it's extremely
0254      * likely that this card supports SCSI, regardless of Product_
0255      * Code, Board_Size, etc.
0256      */
0257 
0258     /* Get pointers to the presumed register locations and save contents */
0259 
0260     sasr_3393 = &regs->SASR;
0261     scmd_3393 = &regs->SCMD;
0262     save_sasr = *sasr_3393;
0263 
0264     /* First test the AuxStatus Reg */
0265 
0266     q = *sasr_3393; /* read it */
0267     if (q & 0x08)   /* bit 3 should always be clear */
0268         return -ENODEV;
0269     *sasr_3393 = WD_AUXILIARY_STATUS;   /* setup indirect address */
0270     if (*sasr_3393 == WD_AUXILIARY_STATUS) {    /* shouldn't retain the write */
0271         *sasr_3393 = save_sasr; /* Oops - restore this byte */
0272         return -ENODEV;
0273     }
0274     if (*sasr_3393 != q) {  /* should still read the same */
0275         *sasr_3393 = save_sasr; /* Oops - restore this byte */
0276         return -ENODEV;
0277     }
0278     if (*scmd_3393 != q)    /* and so should the image at 0x1f */
0279         return -ENODEV;
0280 
0281     /*
0282      * Ok, we probably have a wd33c93, but let's check a few other places
0283      * for good measure. Make sure that this works for both 'A and 'B
0284      * chip versions.
0285      */
0286 
0287     *sasr_3393 = WD_SCSI_STATUS;
0288     q = *scmd_3393;
0289     *sasr_3393 = WD_SCSI_STATUS;
0290     *scmd_3393 = ~q;
0291     *sasr_3393 = WD_SCSI_STATUS;
0292     qq = *scmd_3393;
0293     *sasr_3393 = WD_SCSI_STATUS;
0294     *scmd_3393 = q;
0295     if (qq != q)    /* should be read only */
0296         return -ENODEV;
0297     *sasr_3393 = 0x1e;  /* this register is unimplemented */
0298     q = *scmd_3393;
0299     *sasr_3393 = 0x1e;
0300     *scmd_3393 = ~q;
0301     *sasr_3393 = 0x1e;
0302     qq = *scmd_3393;
0303     *sasr_3393 = 0x1e;
0304     *scmd_3393 = q;
0305     if (qq != q || qq != 0xff)  /* should be read only, all 1's */
0306         return -ENODEV;
0307     *sasr_3393 = WD_TIMEOUT_PERIOD;
0308     q = *scmd_3393;
0309     *sasr_3393 = WD_TIMEOUT_PERIOD;
0310     *scmd_3393 = ~q;
0311     *sasr_3393 = WD_TIMEOUT_PERIOD;
0312     qq = *scmd_3393;
0313     *sasr_3393 = WD_TIMEOUT_PERIOD;
0314     *scmd_3393 = q;
0315     if (qq != (~q & 0xff))  /* should be read/write */
0316         return -ENODEV;
0317 #endif /* CHECK_WD33C93 */
0318 
0319     return 0;
0320 }
0321 
0322 static int gvp11_probe(struct zorro_dev *z, const struct zorro_device_id *ent)
0323 {
0324     struct Scsi_Host *instance;
0325     unsigned long address;
0326     int error;
0327     unsigned int epc;
0328     unsigned int default_dma_xfer_mask;
0329     struct gvp11_hostdata *hdata;
0330     struct gvp11_scsiregs *regs;
0331     wd33c93_regs wdregs;
0332 
0333     default_dma_xfer_mask = ent->driver_data;
0334 
0335     if (dma_set_mask_and_coherent(&z->dev,
0336         TO_DMA_MASK(default_dma_xfer_mask))) {
0337         dev_warn(&z->dev, "cannot use DMA mask %llx\n",
0338              TO_DMA_MASK(default_dma_xfer_mask));
0339         return -ENODEV;
0340     }
0341 
0342     /*
0343      * Rumors state that some GVP ram boards use the same product
0344      * code as the SCSI controllers. Therefore if the board-size
0345      * is not 64KB we assume it is a ram board and bail out.
0346      */
0347     if (zorro_resource_len(z) != 0x10000)
0348         return -ENODEV;
0349 
0350     address = z->resource.start;
0351     if (!request_mem_region(address, 256, "wd33c93"))
0352         return -EBUSY;
0353 
0354     regs = ZTWO_VADDR(address);
0355 
0356     error = check_wd33c93(regs);
0357     if (error)
0358         goto fail_check_or_alloc;
0359 
0360     instance = scsi_host_alloc(&gvp11_scsi_template,
0361                    sizeof(struct gvp11_hostdata));
0362     if (!instance) {
0363         error = -ENOMEM;
0364         goto fail_check_or_alloc;
0365     }
0366 
0367     instance->irq = IRQ_AMIGA_PORTS;
0368     instance->unique_id = z->slotaddr;
0369 
0370     regs->secret2 = 1;
0371     regs->secret1 = 0;
0372     regs->secret3 = 15;
0373     while (regs->CNTR & GVP11_DMAC_BUSY)
0374         ;
0375     regs->CNTR = 0;
0376     regs->BANK = 0;
0377 
0378     wdregs.SASR = &regs->SASR;
0379     wdregs.SCMD = &regs->SCMD;
0380 
0381     hdata = shost_priv(instance);
0382     if (gvp11_xfer_mask) {
0383         hdata->wh.dma_xfer_mask = gvp11_xfer_mask;
0384         if (dma_set_mask_and_coherent(&z->dev,
0385             TO_DMA_MASK(gvp11_xfer_mask))) {
0386             dev_warn(&z->dev, "cannot use DMA mask %llx\n",
0387                  TO_DMA_MASK(gvp11_xfer_mask));
0388             error = -ENODEV;
0389             goto fail_check_or_alloc;
0390         }
0391     } else
0392         hdata->wh.dma_xfer_mask = default_dma_xfer_mask;
0393 
0394     hdata->wh.no_sync = 0xff;
0395     hdata->wh.fast = 0;
0396     hdata->wh.dma_mode = CTRL_DMA;
0397     hdata->regs = regs;
0398 
0399     /*
0400      * Check for 14MHz SCSI clock
0401      */
0402     epc = *(unsigned short *)(ZTWO_VADDR(address) + 0x8000);
0403     wd33c93_init(instance, wdregs, dma_setup, dma_stop,
0404              (epc & GVP_SCSICLKMASK) ? WD33C93_FS_8_10
0405                          : WD33C93_FS_12_15);
0406 
0407     error = request_irq(IRQ_AMIGA_PORTS, gvp11_intr, IRQF_SHARED,
0408                 "GVP11 SCSI", instance);
0409     if (error)
0410         goto fail_irq;
0411 
0412     regs->CNTR = GVP11_DMAC_INT_ENABLE;
0413 
0414     error = scsi_add_host(instance, NULL);
0415     if (error)
0416         goto fail_host;
0417 
0418     zorro_set_drvdata(z, instance);
0419     scsi_scan_host(instance);
0420     return 0;
0421 
0422 fail_host:
0423     free_irq(IRQ_AMIGA_PORTS, instance);
0424 fail_irq:
0425     scsi_host_put(instance);
0426 fail_check_or_alloc:
0427     release_mem_region(address, 256);
0428     return error;
0429 }
0430 
0431 static void gvp11_remove(struct zorro_dev *z)
0432 {
0433     struct Scsi_Host *instance = zorro_get_drvdata(z);
0434     struct gvp11_hostdata *hdata = shost_priv(instance);
0435 
0436     hdata->regs->CNTR = 0;
0437     scsi_remove_host(instance);
0438     free_irq(IRQ_AMIGA_PORTS, instance);
0439     scsi_host_put(instance);
0440     release_mem_region(z->resource.start, 256);
0441 }
0442 
0443     /*
0444      * This should (hopefully) be the correct way to identify
0445      * all the different GVP SCSI controllers (except for the
0446      * SERIES I though).
0447      */
0448 
0449 static struct zorro_device_id gvp11_zorro_tbl[] = {
0450     { ZORRO_PROD_GVP_COMBO_030_R3_SCSI, ~0x00ffffff },
0451     { ZORRO_PROD_GVP_SERIES_II,     ~0x00ffffff },
0452     { ZORRO_PROD_GVP_GFORCE_030_SCSI,   ~0x01ffffff },
0453     { ZORRO_PROD_GVP_A530_SCSI,     ~0x01ffffff },
0454     { ZORRO_PROD_GVP_COMBO_030_R4_SCSI, ~0x01ffffff },
0455     { ZORRO_PROD_GVP_A1291,         ~0x07ffffff },
0456     { ZORRO_PROD_GVP_GFORCE_040_SCSI_1, ~0x07ffffff },
0457     { 0 }
0458 };
0459 MODULE_DEVICE_TABLE(zorro, gvp11_zorro_tbl);
0460 
0461 static struct zorro_driver gvp11_driver = {
0462     .name       = "gvp11",
0463     .id_table   = gvp11_zorro_tbl,
0464     .probe      = gvp11_probe,
0465     .remove     = gvp11_remove,
0466 };
0467 
0468 static int __init gvp11_init(void)
0469 {
0470     return zorro_register_driver(&gvp11_driver);
0471 }
0472 module_init(gvp11_init);
0473 
0474 static void __exit gvp11_exit(void)
0475 {
0476     zorro_unregister_driver(&gvp11_driver);
0477 }
0478 module_exit(gvp11_exit);
0479 
0480 MODULE_DESCRIPTION("GVP Series II SCSI");
0481 MODULE_LICENSE("GPL");