Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  *  linux/drivers/acorn/scsi/powertec.c
0004  *
0005  *  Copyright (C) 1997-2005 Russell King
0006  */
0007 #include <linux/module.h>
0008 #include <linux/blkdev.h>
0009 #include <linux/kernel.h>
0010 #include <linux/string.h>
0011 #include <linux/ioport.h>
0012 #include <linux/proc_fs.h>
0013 #include <linux/delay.h>
0014 #include <linux/interrupt.h>
0015 #include <linux/init.h>
0016 #include <linux/dma-mapping.h>
0017 #include <linux/pgtable.h>
0018 
0019 #include <asm/dma.h>
0020 #include <asm/ecard.h>
0021 #include <asm/io.h>
0022 
0023 #include <scsi/scsi.h>
0024 #include <scsi/scsi_cmnd.h>
0025 #include <scsi/scsi_device.h>
0026 #include <scsi/scsi_eh.h>
0027 #include <scsi/scsi_host.h>
0028 #include <scsi/scsi_tcq.h>
0029 #include "fas216.h"
0030 #include "arm_scsi.h"
0031 
0032 #include <scsi/scsicam.h>
0033 
0034 #define POWERTEC_FAS216_OFFSET  0x3000
0035 #define POWERTEC_FAS216_SHIFT   6
0036 
0037 #define POWERTEC_INTR_STATUS    0x2000
0038 #define POWERTEC_INTR_BIT   0x80
0039 
0040 #define POWERTEC_RESET_CONTROL  0x1018
0041 #define POWERTEC_RESET_BIT  1
0042 
0043 #define POWERTEC_TERM_CONTROL   0x2018
0044 #define POWERTEC_TERM_ENABLE    1
0045 
0046 #define POWERTEC_INTR_CONTROL   0x101c
0047 #define POWERTEC_INTR_ENABLE    1
0048 #define POWERTEC_INTR_DISABLE   0
0049 
0050 #define VERSION "1.10 (19/01/2003 2.5.59)"
0051 
0052 /*
0053  * Use term=0,1,0,0,0 to turn terminators on/off.
0054  * One entry per slot.
0055  */
0056 static int term[MAX_ECARDS] = { 1, 1, 1, 1, 1, 1, 1, 1 };
0057 
0058 #define NR_SG   256
0059 
0060 struct powertec_info {
0061     FAS216_Info     info;
0062     struct expansion_card   *ec;
0063     void __iomem        *base;
0064     unsigned int        term_ctl;
0065     struct scatterlist  sg[NR_SG];
0066 };
0067 
0068 /* Prototype: void powertecscsi_irqenable(ec, irqnr)
0069  * Purpose  : Enable interrupts on Powertec SCSI card
0070  * Params   : ec    - expansion card structure
0071  *          : irqnr - interrupt number
0072  */
0073 static void
0074 powertecscsi_irqenable(struct expansion_card *ec, int irqnr)
0075 {
0076     struct powertec_info *info = ec->irq_data;
0077     writeb(POWERTEC_INTR_ENABLE, info->base + POWERTEC_INTR_CONTROL);
0078 }
0079 
0080 /* Prototype: void powertecscsi_irqdisable(ec, irqnr)
0081  * Purpose  : Disable interrupts on Powertec SCSI card
0082  * Params   : ec    - expansion card structure
0083  *          : irqnr - interrupt number
0084  */
0085 static void
0086 powertecscsi_irqdisable(struct expansion_card *ec, int irqnr)
0087 {
0088     struct powertec_info *info = ec->irq_data;
0089     writeb(POWERTEC_INTR_DISABLE, info->base + POWERTEC_INTR_CONTROL);
0090 }
0091 
0092 static const expansioncard_ops_t powertecscsi_ops = {
0093     .irqenable  = powertecscsi_irqenable,
0094     .irqdisable = powertecscsi_irqdisable,
0095 };
0096 
0097 /* Prototype: void powertecscsi_terminator_ctl(host, on_off)
0098  * Purpose  : Turn the Powertec SCSI terminators on or off
0099  * Params   : host   - card to turn on/off
0100  *          : on_off - !0 to turn on, 0 to turn off
0101  */
0102 static void
0103 powertecscsi_terminator_ctl(struct Scsi_Host *host, int on_off)
0104 {
0105     struct powertec_info *info = (struct powertec_info *)host->hostdata;
0106 
0107     info->term_ctl = on_off ? POWERTEC_TERM_ENABLE : 0;
0108     writeb(info->term_ctl, info->base + POWERTEC_TERM_CONTROL);
0109 }
0110 
0111 /* Prototype: void powertecscsi_intr(irq, *dev_id, *regs)
0112  * Purpose  : handle interrupts from Powertec SCSI card
0113  * Params   : irq    - interrupt number
0114  *        dev_id - user-defined (Scsi_Host structure)
0115  */
0116 static irqreturn_t powertecscsi_intr(int irq, void *dev_id)
0117 {
0118     struct powertec_info *info = dev_id;
0119 
0120     return fas216_intr(&info->info);
0121 }
0122 
0123 /* Prototype: fasdmatype_t powertecscsi_dma_setup(host, SCpnt, direction, min_type)
0124  * Purpose  : initialises DMA/PIO
0125  * Params   : host      - host
0126  *        SCpnt     - command
0127  *        direction - DMA on to/off of card
0128  *        min_type  - minimum DMA support that we must have for this transfer
0129  * Returns  : type of transfer to be performed
0130  */
0131 static fasdmatype_t
0132 powertecscsi_dma_setup(struct Scsi_Host *host, struct scsi_pointer *SCp,
0133                fasdmadir_t direction, fasdmatype_t min_type)
0134 {
0135     struct powertec_info *info = (struct powertec_info *)host->hostdata;
0136     struct device *dev = scsi_get_device(host);
0137     int dmach = info->info.scsi.dma;
0138 
0139     if (info->info.ifcfg.capabilities & FASCAP_DMA &&
0140         min_type == fasdma_real_all) {
0141         int bufs, map_dir, dma_dir;
0142 
0143         bufs = copy_SCp_to_sg(&info->sg[0], SCp, NR_SG);
0144 
0145         if (direction == DMA_OUT) {
0146             map_dir = DMA_TO_DEVICE;
0147             dma_dir = DMA_MODE_WRITE;
0148         } else {
0149             map_dir = DMA_FROM_DEVICE;
0150             dma_dir = DMA_MODE_READ;
0151         }
0152 
0153         dma_map_sg(dev, info->sg, bufs, map_dir);
0154 
0155         disable_dma(dmach);
0156         set_dma_sg(dmach, info->sg, bufs);
0157         set_dma_mode(dmach, dma_dir);
0158         enable_dma(dmach);
0159         return fasdma_real_all;
0160     }
0161 
0162     /*
0163      * If we're not doing DMA,
0164      *  we'll do slow PIO
0165      */
0166     return fasdma_pio;
0167 }
0168 
0169 /* Prototype: int powertecscsi_dma_stop(host, SCpnt)
0170  * Purpose  : stops DMA/PIO
0171  * Params   : host  - host
0172  *        SCpnt - command
0173  */
0174 static void
0175 powertecscsi_dma_stop(struct Scsi_Host *host, struct scsi_pointer *SCp)
0176 {
0177     struct powertec_info *info = (struct powertec_info *)host->hostdata;
0178     if (info->info.scsi.dma != NO_DMA)
0179         disable_dma(info->info.scsi.dma);
0180 }
0181 
0182 /* Prototype: const char *powertecscsi_info(struct Scsi_Host * host)
0183  * Purpose  : returns a descriptive string about this interface,
0184  * Params   : host - driver host structure to return info for.
0185  * Returns  : pointer to a static buffer containing null terminated string.
0186  */
0187 const char *powertecscsi_info(struct Scsi_Host *host)
0188 {
0189     struct powertec_info *info = (struct powertec_info *)host->hostdata;
0190     static char string[150];
0191 
0192     sprintf(string, "%s (%s) in slot %d v%s terminators o%s",
0193         host->hostt->name, info->info.scsi.type, info->ec->slot_no,
0194         VERSION, info->term_ctl ? "n" : "ff");
0195 
0196     return string;
0197 }
0198 
0199 /* Prototype: int powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
0200  * Purpose  : Set a driver specific function
0201  * Params   : host   - host to setup
0202  *          : buffer - buffer containing string describing operation
0203  *          : length - length of string
0204  * Returns  : -EINVAL, or 0
0205  */
0206 static int
0207 powertecscsi_set_proc_info(struct Scsi_Host *host, char *buffer, int length)
0208 {
0209     int ret = length;
0210 
0211     if (length >= 12 && strncmp(buffer, "POWERTECSCSI", 12) == 0) {
0212         buffer += 12;
0213         length -= 12;
0214 
0215         if (length >= 5 && strncmp(buffer, "term=", 5) == 0) {
0216             if (buffer[5] == '1')
0217                 powertecscsi_terminator_ctl(host, 1);
0218             else if (buffer[5] == '0')
0219                 powertecscsi_terminator_ctl(host, 0);
0220             else
0221                 ret = -EINVAL;
0222         } else
0223             ret = -EINVAL;
0224     } else
0225         ret = -EINVAL;
0226 
0227     return ret;
0228 }
0229 
0230 /* Prototype: int powertecscsi_proc_info(char *buffer, char **start, off_t offset,
0231  *                  int length, int host_no, int inout)
0232  * Purpose  : Return information about the driver to a user process accessing
0233  *        the /proc filesystem.
0234  * Params   : buffer  - a buffer to write information to
0235  *        start   - a pointer into this buffer set by this routine to the start
0236  *              of the required information.
0237  *        offset  - offset into information that we have read up to.
0238  *        length  - length of buffer
0239  *        inout   - 0 for reading, 1 for writing.
0240  * Returns  : length of data written to buffer.
0241  */
0242 static int powertecscsi_show_info(struct seq_file *m, struct Scsi_Host *host)
0243 {
0244     struct powertec_info *info;
0245 
0246     info = (struct powertec_info *)host->hostdata;
0247 
0248     seq_printf(m, "PowerTec SCSI driver v%s\n", VERSION);
0249     fas216_print_host(&info->info, m);
0250     seq_printf(m, "Term    : o%s\n",
0251             info->term_ctl ? "n" : "ff");
0252 
0253     fas216_print_stats(&info->info, m);
0254     fas216_print_devices(&info->info, m);
0255     return 0;
0256 }
0257 
0258 static ssize_t powertecscsi_show_term(struct device *dev, struct device_attribute *attr, char *buf)
0259 {
0260     struct expansion_card *ec = ECARD_DEV(dev);
0261     struct Scsi_Host *host = ecard_get_drvdata(ec);
0262     struct powertec_info *info = (struct powertec_info *)host->hostdata;
0263 
0264     return sprintf(buf, "%d\n", info->term_ctl ? 1 : 0);
0265 }
0266 
0267 static ssize_t
0268 powertecscsi_store_term(struct device *dev, struct device_attribute *attr, const char *buf, size_t len)
0269 {
0270     struct expansion_card *ec = ECARD_DEV(dev);
0271     struct Scsi_Host *host = ecard_get_drvdata(ec);
0272 
0273     if (len > 1)
0274         powertecscsi_terminator_ctl(host, buf[0] != '0');
0275 
0276     return len;
0277 }
0278 
0279 static DEVICE_ATTR(bus_term, S_IRUGO | S_IWUSR,
0280            powertecscsi_show_term, powertecscsi_store_term);
0281 
0282 static struct scsi_host_template powertecscsi_template = {
0283     .module             = THIS_MODULE,
0284     .show_info          = powertecscsi_show_info,
0285     .write_info         = powertecscsi_set_proc_info,
0286     .name               = "PowerTec SCSI",
0287     .info               = powertecscsi_info,
0288     .queuecommand           = fas216_queue_command,
0289     .eh_host_reset_handler      = fas216_eh_host_reset,
0290     .eh_bus_reset_handler       = fas216_eh_bus_reset,
0291     .eh_device_reset_handler    = fas216_eh_device_reset,
0292     .eh_abort_handler       = fas216_eh_abort,
0293     .cmd_size           = sizeof(struct fas216_cmd_priv),
0294     .can_queue          = 8,
0295     .this_id            = 7,
0296     .sg_tablesize           = SG_MAX_SEGMENTS,
0297     .dma_boundary           = IOMD_DMA_BOUNDARY,
0298     .cmd_per_lun            = 2,
0299     .proc_name          = "powertec",
0300 };
0301 
0302 static int powertecscsi_probe(struct expansion_card *ec,
0303                   const struct ecard_id *id)
0304 {
0305     struct Scsi_Host *host;
0306     struct powertec_info *info;
0307     void __iomem *base;
0308     int ret;
0309 
0310     ret = ecard_request_resources(ec);
0311     if (ret)
0312         goto out;
0313 
0314     base = ecardm_iomap(ec, ECARD_RES_IOCFAST, 0, 0);
0315     if (!base) {
0316         ret = -ENOMEM;
0317         goto out_region;
0318     }
0319 
0320     host = scsi_host_alloc(&powertecscsi_template,
0321                    sizeof (struct powertec_info));
0322     if (!host) {
0323         ret = -ENOMEM;
0324         goto out_region;
0325     }
0326 
0327     ecard_set_drvdata(ec, host);
0328 
0329     info = (struct powertec_info *)host->hostdata;
0330     info->base = base;
0331     powertecscsi_terminator_ctl(host, term[ec->slot_no]);
0332 
0333     info->ec = ec;
0334     info->info.scsi.io_base     = base + POWERTEC_FAS216_OFFSET;
0335     info->info.scsi.io_shift    = POWERTEC_FAS216_SHIFT;
0336     info->info.scsi.irq     = ec->irq;
0337     info->info.scsi.dma     = ec->dma;
0338     info->info.ifcfg.clockrate  = 40; /* MHz */
0339     info->info.ifcfg.select_timeout = 255;
0340     info->info.ifcfg.asyncperiod    = 200; /* ns */
0341     info->info.ifcfg.sync_max_depth = 7;
0342     info->info.ifcfg.cntl3      = CNTL3_BS8 | CNTL3_FASTSCSI | CNTL3_FASTCLK;
0343     info->info.ifcfg.disconnect_ok  = 1;
0344     info->info.ifcfg.wide_max_size  = 0;
0345     info->info.ifcfg.capabilities   = 0;
0346     info->info.dma.setup        = powertecscsi_dma_setup;
0347     info->info.dma.pseudo       = NULL;
0348     info->info.dma.stop     = powertecscsi_dma_stop;
0349 
0350     ec->irqaddr = base + POWERTEC_INTR_STATUS;
0351     ec->irqmask = POWERTEC_INTR_BIT;
0352 
0353     ecard_setirq(ec, &powertecscsi_ops, info);
0354 
0355     device_create_file(&ec->dev, &dev_attr_bus_term);
0356 
0357     ret = fas216_init(host);
0358     if (ret)
0359         goto out_free;
0360 
0361     ret = request_irq(ec->irq, powertecscsi_intr,
0362               0, "powertec", info);
0363     if (ret) {
0364         printk("scsi%d: IRQ%d not free: %d\n",
0365                host->host_no, ec->irq, ret);
0366         goto out_release;
0367     }
0368 
0369     if (info->info.scsi.dma != NO_DMA) {
0370         if (request_dma(info->info.scsi.dma, "powertec")) {
0371             printk("scsi%d: DMA%d not free, using PIO\n",
0372                    host->host_no, info->info.scsi.dma);
0373             info->info.scsi.dma = NO_DMA;
0374         } else {
0375             set_dma_speed(info->info.scsi.dma, 180);
0376             info->info.ifcfg.capabilities |= FASCAP_DMA;
0377         }
0378     }
0379 
0380     ret = fas216_add(host, &ec->dev);
0381     if (ret == 0)
0382         goto out;
0383 
0384     if (info->info.scsi.dma != NO_DMA)
0385         free_dma(info->info.scsi.dma);
0386     free_irq(ec->irq, info);
0387 
0388  out_release:
0389     fas216_release(host);
0390 
0391  out_free:
0392     device_remove_file(&ec->dev, &dev_attr_bus_term);
0393     scsi_host_put(host);
0394 
0395  out_region:
0396     ecard_release_resources(ec);
0397 
0398  out:
0399     return ret;
0400 }
0401 
0402 static void powertecscsi_remove(struct expansion_card *ec)
0403 {
0404     struct Scsi_Host *host = ecard_get_drvdata(ec);
0405     struct powertec_info *info = (struct powertec_info *)host->hostdata;
0406 
0407     ecard_set_drvdata(ec, NULL);
0408     fas216_remove(host);
0409 
0410     device_remove_file(&ec->dev, &dev_attr_bus_term);
0411 
0412     if (info->info.scsi.dma != NO_DMA)
0413         free_dma(info->info.scsi.dma);
0414     free_irq(ec->irq, info);
0415 
0416     fas216_release(host);
0417     scsi_host_put(host);
0418     ecard_release_resources(ec);
0419 }
0420 
0421 static const struct ecard_id powertecscsi_cids[] = {
0422     { MANU_ALSYSTEMS, PROD_ALSYS_SCSIATAPI },
0423     { 0xffff, 0xffff },
0424 };
0425 
0426 static struct ecard_driver powertecscsi_driver = {
0427     .probe      = powertecscsi_probe,
0428     .remove     = powertecscsi_remove,
0429     .id_table   = powertecscsi_cids,
0430     .drv = {
0431         .name       = "powertecscsi",
0432     },
0433 };
0434 
0435 static int __init powertecscsi_init(void)
0436 {
0437     return ecard_register_driver(&powertecscsi_driver);
0438 }
0439 
0440 static void __exit powertecscsi_exit(void)
0441 {
0442     ecard_remove_driver(&powertecscsi_driver);
0443 }
0444 
0445 module_init(powertecscsi_init);
0446 module_exit(powertecscsi_exit);
0447 
0448 MODULE_AUTHOR("Russell King");
0449 MODULE_DESCRIPTION("Powertec SCSI driver");
0450 module_param_array(term, int, NULL, 0);
0451 MODULE_PARM_DESC(term, "SCSI bus termination");
0452 MODULE_LICENSE("GPL");