Back to home page

OSCL-LXR

 
 

    


0001 /*
0002  * Generic platform device PATA driver
0003  *
0004  * Copyright (C) 2006 - 2007  Paul Mundt
0005  *
0006  * Based on pata_pcmcia:
0007  *
0008  *   Copyright 2005-2006 Red Hat Inc, all rights reserved.
0009  *
0010  * This file is subject to the terms and conditions of the GNU General Public
0011  * License.  See the file "COPYING" in the main directory of this archive
0012  * for more details.
0013  */
0014 #include <linux/kernel.h>
0015 #include <linux/module.h>
0016 #include <linux/blkdev.h>
0017 #include <scsi/scsi_host.h>
0018 #include <linux/ata.h>
0019 #include <linux/libata.h>
0020 #include <linux/platform_device.h>
0021 #include <linux/ata_platform.h>
0022 
0023 #define DRV_NAME "pata_platform"
0024 #define DRV_VERSION "1.2"
0025 
0026 static int pio_mask = 1;
0027 module_param(pio_mask, int, 0);
0028 MODULE_PARM_DESC(pio_mask, "PIO modes supported, mode 0 only by default");
0029 
0030 /*
0031  * Provide our own set_mode() as we don't want to change anything that has
0032  * already been configured..
0033  */
0034 static int pata_platform_set_mode(struct ata_link *link, struct ata_device **unused)
0035 {
0036     struct ata_device *dev;
0037 
0038     ata_for_each_dev(dev, link, ENABLED) {
0039         /* We don't really care */
0040         dev->pio_mode = dev->xfer_mode = XFER_PIO_0;
0041         dev->xfer_shift = ATA_SHIFT_PIO;
0042         dev->flags |= ATA_DFLAG_PIO;
0043         ata_dev_info(dev, "configured for PIO\n");
0044     }
0045     return 0;
0046 }
0047 
0048 static struct scsi_host_template pata_platform_sht = {
0049     ATA_PIO_SHT(DRV_NAME),
0050 };
0051 
0052 static void pata_platform_setup_port(struct ata_ioports *ioaddr,
0053                      unsigned int shift)
0054 {
0055     /* Fixup the port shift for platforms that need it */
0056     ioaddr->data_addr   = ioaddr->cmd_addr + (ATA_REG_DATA    << shift);
0057     ioaddr->error_addr  = ioaddr->cmd_addr + (ATA_REG_ERR     << shift);
0058     ioaddr->feature_addr    = ioaddr->cmd_addr + (ATA_REG_FEATURE << shift);
0059     ioaddr->nsect_addr  = ioaddr->cmd_addr + (ATA_REG_NSECT   << shift);
0060     ioaddr->lbal_addr   = ioaddr->cmd_addr + (ATA_REG_LBAL    << shift);
0061     ioaddr->lbam_addr   = ioaddr->cmd_addr + (ATA_REG_LBAM    << shift);
0062     ioaddr->lbah_addr   = ioaddr->cmd_addr + (ATA_REG_LBAH    << shift);
0063     ioaddr->device_addr = ioaddr->cmd_addr + (ATA_REG_DEVICE  << shift);
0064     ioaddr->status_addr = ioaddr->cmd_addr + (ATA_REG_STATUS  << shift);
0065     ioaddr->command_addr    = ioaddr->cmd_addr + (ATA_REG_CMD     << shift);
0066 }
0067 
0068 /**
0069  *  __pata_platform_probe       -   attach a platform interface
0070  *  @dev: device
0071  *  @io_res: Resource representing I/O base
0072  *  @ctl_res: Resource representing CTL base
0073  *  @irq_res: Resource representing IRQ and its flags
0074  *  @ioport_shift: I/O port shift
0075  *  @__pio_mask: PIO mask
0076  *  @sht: scsi_host_template to use when registering
0077  *  @use16bit: Flag to indicate 16-bit IO instead of 32-bit
0078  *
0079  *  Register a platform bus IDE interface. Such interfaces are PIO and we
0080  *  assume do not support IRQ sharing.
0081  *
0082  *  Platform devices are expected to contain at least 2 resources per port:
0083  *
0084  *      - I/O Base (IORESOURCE_IO or IORESOURCE_MEM)
0085  *      - CTL Base (IORESOURCE_IO or IORESOURCE_MEM)
0086  *
0087  *  and optionally:
0088  *
0089  *      - IRQ      (IORESOURCE_IRQ)
0090  *
0091  *  If the base resources are both mem types, the ioremap() is handled
0092  *  here. For IORESOURCE_IO, it's assumed that there's no remapping
0093  *  necessary.
0094  *
0095  *  If no IRQ resource is present, PIO polling mode is used instead.
0096  */
0097 int __pata_platform_probe(struct device *dev, struct resource *io_res,
0098               struct resource *ctl_res, struct resource *irq_res,
0099               unsigned int ioport_shift, int __pio_mask,
0100               struct scsi_host_template *sht, bool use16bit)
0101 {
0102     struct ata_host *host;
0103     struct ata_port *ap;
0104     unsigned int mmio;
0105     int irq = 0;
0106     int irq_flags = 0;
0107 
0108     /*
0109      * Check for MMIO
0110      */
0111     mmio = (( io_res->flags == IORESOURCE_MEM) &&
0112         (ctl_res->flags == IORESOURCE_MEM));
0113 
0114     /*
0115      * And the IRQ
0116      */
0117     if (irq_res && irq_res->start > 0) {
0118         irq = irq_res->start;
0119         irq_flags = (irq_res->flags & IRQF_TRIGGER_MASK) | IRQF_SHARED;
0120     }
0121 
0122     /*
0123      * Now that that's out of the way, wire up the port..
0124      */
0125     host = ata_host_alloc(dev, 1);
0126     if (!host)
0127         return -ENOMEM;
0128     ap = host->ports[0];
0129 
0130     ap->ops = devm_kzalloc(dev, sizeof(*ap->ops), GFP_KERNEL);
0131     if (!ap->ops)
0132         return -ENOMEM;
0133     ap->ops->inherits = &ata_sff_port_ops;
0134     ap->ops->cable_detect = ata_cable_unknown;
0135     ap->ops->set_mode = pata_platform_set_mode;
0136     if (use16bit)
0137         ap->ops->sff_data_xfer = ata_sff_data_xfer;
0138     else
0139         ap->ops->sff_data_xfer = ata_sff_data_xfer32;
0140 
0141     ap->pio_mask = __pio_mask;
0142     ap->flags |= ATA_FLAG_SLAVE_POSS;
0143 
0144     /*
0145      * Use polling mode if there's no IRQ
0146      */
0147     if (!irq) {
0148         ap->flags |= ATA_FLAG_PIO_POLLING;
0149         ata_port_desc(ap, "no IRQ, using PIO polling");
0150     }
0151 
0152     /*
0153      * Handle the MMIO case
0154      */
0155     if (mmio) {
0156         ap->ioaddr.cmd_addr = devm_ioremap(dev, io_res->start,
0157                 resource_size(io_res));
0158         ap->ioaddr.ctl_addr = devm_ioremap(dev, ctl_res->start,
0159                 resource_size(ctl_res));
0160     } else {
0161         ap->ioaddr.cmd_addr = devm_ioport_map(dev, io_res->start,
0162                 resource_size(io_res));
0163         ap->ioaddr.ctl_addr = devm_ioport_map(dev, ctl_res->start,
0164                 resource_size(ctl_res));
0165     }
0166     if (!ap->ioaddr.cmd_addr || !ap->ioaddr.ctl_addr) {
0167         dev_err(dev, "failed to map IO/CTL base\n");
0168         return -ENOMEM;
0169     }
0170 
0171     ap->ioaddr.altstatus_addr = ap->ioaddr.ctl_addr;
0172 
0173     pata_platform_setup_port(&ap->ioaddr, ioport_shift);
0174 
0175     ata_port_desc(ap, "%s cmd 0x%llx ctl 0x%llx", mmio ? "mmio" : "ioport",
0176               (unsigned long long)io_res->start,
0177               (unsigned long long)ctl_res->start);
0178 
0179     /* activate */
0180     return ata_host_activate(host, irq, irq ? ata_sff_interrupt : NULL,
0181                  irq_flags, sht);
0182 }
0183 EXPORT_SYMBOL_GPL(__pata_platform_probe);
0184 
0185 static int pata_platform_probe(struct platform_device *pdev)
0186 {
0187     struct resource *io_res;
0188     struct resource *ctl_res;
0189     struct resource *irq_res;
0190     struct pata_platform_info *pp_info = dev_get_platdata(&pdev->dev);
0191 
0192     /*
0193      * Simple resource validation ..
0194      */
0195     if ((pdev->num_resources != 3) && (pdev->num_resources != 2)) {
0196         dev_err(&pdev->dev, "invalid number of resources\n");
0197         return -EINVAL;
0198     }
0199 
0200     /*
0201      * Get the I/O base first
0202      */
0203     io_res = platform_get_mem_or_io(pdev, 0);
0204     if (!io_res)
0205         return -EINVAL;
0206 
0207     /*
0208      * Then the CTL base
0209      */
0210     ctl_res = platform_get_mem_or_io(pdev, 1);
0211     if (!ctl_res)
0212         return -EINVAL;
0213 
0214     /*
0215      * And the IRQ
0216      */
0217     irq_res = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
0218 
0219     return __pata_platform_probe(&pdev->dev, io_res, ctl_res, irq_res,
0220                      pp_info ? pp_info->ioport_shift : 0,
0221                      pio_mask, &pata_platform_sht, false);
0222 }
0223 
0224 static struct platform_driver pata_platform_driver = {
0225     .probe      = pata_platform_probe,
0226     .remove     = ata_platform_remove_one,
0227     .driver = {
0228         .name       = DRV_NAME,
0229     },
0230 };
0231 
0232 module_platform_driver(pata_platform_driver);
0233 
0234 MODULE_AUTHOR("Paul Mundt");
0235 MODULE_DESCRIPTION("low-level driver for platform device ATA");
0236 MODULE_LICENSE("GPL");
0237 MODULE_VERSION(DRV_VERSION);
0238 MODULE_ALIAS("platform:" DRV_NAME);