ahci: implement ata_save/restore_initial_config()
[powerpc.git] / drivers / ata / pata_pdc2027x.c
index 76dd1c9..8261f4f 100644 (file)
 #include <scsi/scsi_host.h>
 #include <scsi/scsi_cmnd.h>
 #include <linux/libata.h>
-#include <asm/io.h>
 
 #define DRV_NAME       "pata_pdc2027x"
-#define DRV_VERSION    "0.74-ac5"
+#define DRV_VERSION    "0.9"
 #undef PDC_DEBUG
 
 #ifdef PDC_DEBUG
@@ -46,6 +45,8 @@
 #endif
 
 enum {
+       PDC_MMIO_BAR            = 5,
+
        PDC_UDMA_100            = 0,
        PDC_UDMA_133            = 1,
 
@@ -62,12 +63,13 @@ enum {
 };
 
 static int pdc2027x_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
-static void pdc2027x_remove_one(struct pci_dev *pdev);
 static void pdc2027x_error_handler(struct ata_port *ap);
 static void pdc2027x_set_piomode(struct ata_port *ap, struct ata_device *adev);
 static void pdc2027x_set_dmamode(struct ata_port *ap, struct ata_device *adev);
-static void pdc2027x_post_set_mode(struct ata_port *ap);
 static int pdc2027x_check_atapi_dma(struct ata_queued_cmd *qc);
+static unsigned long pdc2027x_mode_filter(struct ata_device *adev, unsigned long mask);
+static int pdc2027x_cable_detect(struct ata_port *ap);
+static int pdc2027x_set_mode(struct ata_port *ap, struct ata_device **r_failed);
 
 /*
  * ATA Timing Tables based on 133MHz controller clock.
@@ -123,7 +125,7 @@ static struct pci_driver pdc2027x_pci_driver = {
        .name                   = DRV_NAME,
        .id_table               = pdc2027x_pci_tbl,
        .probe                  = pdc2027x_init_one,
-       .remove                 = __devexit_p(pdc2027x_remove_one),
+       .remove                 = ata_pci_remove_one,
 };
 
 static struct scsi_host_template pdc2027x_sht = {
@@ -146,6 +148,7 @@ static struct scsi_host_template pdc2027x_sht = {
 
 static struct ata_port_operations pdc2027x_pata100_ops = {
        .port_disable           = ata_port_disable,
+       .mode_filter            = ata_pci_default_filter,
 
        .tf_load                = ata_tf_load,
        .tf_read                = ata_tf_read,
@@ -160,26 +163,28 @@ static struct ata_port_operations pdc2027x_pata100_ops = {
        .bmdma_status           = ata_bmdma_status,
        .qc_prep                = ata_qc_prep,
        .qc_issue               = ata_qc_issue_prot,
-       .data_xfer              = ata_mmio_data_xfer,
+       .data_xfer              = ata_data_xfer,
 
        .freeze                 = ata_bmdma_freeze,
        .thaw                   = ata_bmdma_thaw,
        .error_handler          = pdc2027x_error_handler,
        .post_internal_cmd      = ata_bmdma_post_internal_cmd,
+       .cable_detect           = pdc2027x_cable_detect,
 
        .irq_handler            = ata_interrupt,
        .irq_clear              = ata_bmdma_irq_clear,
+       .irq_on                 = ata_irq_on,
+       .irq_ack                = ata_irq_ack,
 
        .port_start             = ata_port_start,
-       .port_stop              = ata_port_stop,
-       .host_stop              = ata_pci_host_stop,
 };
 
 static struct ata_port_operations pdc2027x_pata133_ops = {
        .port_disable           = ata_port_disable,
        .set_piomode            = pdc2027x_set_piomode,
        .set_dmamode            = pdc2027x_set_dmamode,
-       .post_set_mode          = pdc2027x_post_set_mode,
+       .set_mode               = pdc2027x_set_mode,
+       .mode_filter            = pdc2027x_mode_filter,
 
        .tf_load                = ata_tf_load,
        .tf_read                = ata_tf_read,
@@ -194,19 +199,20 @@ static struct ata_port_operations pdc2027x_pata133_ops = {
        .bmdma_status           = ata_bmdma_status,
        .qc_prep                = ata_qc_prep,
        .qc_issue               = ata_qc_issue_prot,
-       .data_xfer              = ata_mmio_data_xfer,
+       .data_xfer              = ata_data_xfer,
 
        .freeze                 = ata_bmdma_freeze,
        .thaw                   = ata_bmdma_thaw,
        .error_handler          = pdc2027x_error_handler,
        .post_internal_cmd      = ata_bmdma_post_internal_cmd,
+       .cable_detect           = pdc2027x_cable_detect,
 
        .irq_handler            = ata_interrupt,
        .irq_clear              = ata_bmdma_irq_clear,
+       .irq_on                 = ata_irq_on,
+       .irq_ack                = ata_irq_ack,
 
        .port_start             = ata_port_start,
-       .port_stop              = ata_port_stop,
-       .host_stop              = ata_pci_host_stop,
 };
 
 static struct ata_port_info pdc2027x_port_info[] = {
@@ -245,7 +251,7 @@ MODULE_DEVICE_TABLE(pci, pdc2027x_pci_tbl);
  */
 static inline void __iomem *port_mmio(struct ata_port *ap, unsigned int offset)
 {
-       return ap->host->mmio_base + ap->port_no * 0x100 + offset;
+       return ap->host->iomap[PDC_MMIO_BAR] + ap->port_no * 0x100 + offset;
 }
 
 /**
@@ -261,7 +267,7 @@ static inline void __iomem *dev_mmio(struct ata_port *ap, struct ata_device *ade
 }
 
 /**
- *     pdc2027x_pata_cbl_detect - Probe host controller cable detect info
+ *     pdc2027x_pata_cable_detect - Probe host controller cable detect info
  *     @ap: Port for which cable detect info is desired
  *
  *     Read 80c cable indicator from Promise extended register.
@@ -270,7 +276,7 @@ static inline void __iomem *dev_mmio(struct ata_port *ap, struct ata_device *ade
  *     LOCKING:
  *     None (inherited from caller).
  */
-static void pdc2027x_cbl_detect(struct ata_port *ap)
+static int pdc2027x_cable_detect(struct ata_port *ap)
 {
        u32 cgcr;
 
@@ -281,13 +287,10 @@ static void pdc2027x_cbl_detect(struct ata_port *ap)
 
        PDPRINTK("No cable or 80-conductor cable on port %d\n", ap->port_no);
 
-       ap->cbl = ATA_CBL_PATA80;
-       return;
-
+       return ATA_CBL_PATA80;
 cbl40:
        printk(KERN_INFO DRV_NAME ": 40-conductor cable detected on port %d\n", ap->port_no);
-       ap->cbl = ATA_CBL_PATA40;
-       ap->udma_mask &= ATA_UDMA_MASK_40C;
+       return ATA_CBL_PATA40;
 }
 
 /**
@@ -314,7 +317,6 @@ static int pdc2027x_prereset(struct ata_port *ap)
        /* Check whether port enabled */
        if (!pdc2027x_port_enabled(ap))
                return -ENOENT;
-       pdc2027x_cbl_detect(ap);
        return ata_std_prereset(ap);
 }
 
@@ -333,6 +335,32 @@ static void pdc2027x_error_handler(struct ata_port *ap)
        ata_bmdma_drive_eh(ap, pdc2027x_prereset, ata_std_softreset, NULL, ata_std_postreset);
 }
 
+/**
+ *     pdc2720x_mode_filter    -       mode selection filter
+ *     @adev: ATA device
+ *     @mask: list of modes proposed
+ *
+ *     Block UDMA on devices that cause trouble with this controller.
+ */
+
+static unsigned long pdc2027x_mode_filter(struct ata_device *adev, unsigned long mask)
+{
+       unsigned char model_num[ATA_ID_PROD_LEN + 1];
+       struct ata_device *pair = ata_dev_pair(adev);
+
+       if (adev->class != ATA_DEV_ATA || adev->devno == 0 || pair == NULL)
+               return ata_pci_default_filter(adev, mask);
+
+       /* Check for slave of a Maxtor at UDMA6 */
+       ata_id_c_string(pair->id, model_num, ATA_ID_PROD,
+                         ATA_ID_PROD_LEN + 1);
+       /* If the master is a maxtor in UDMA6 then the slave should not use UDMA 6 */
+       if(strstr(model_num, "Maxtor") == 0 && pair->dma_mode == XFER_UDMA_6)
+               mask &= ~ (1 << (6 + ATA_SHIFT_UDMA));
+
+       return ata_pci_default_filter(adev, mask);
+}
+
 /**
  *     pdc2027x_set_piomode - Initialize host controller PATA PIO timings
  *     @ap: Port to configure
@@ -444,17 +472,22 @@ static void pdc2027x_set_dmamode(struct ata_port *ap, struct ata_device *adev)
 }
 
 /**
- *     pdc2027x_post_set_mode - Set the timing registers back to correct values.
+ *     pdc2027x_set_mode - Set the timing registers back to correct values.
  *     @ap: Port to configure
+ *     @r_failed: Returned device for failure
  *
  *     The pdc2027x hardware will look at "SET FEATURES" and change the timing registers
  *     automatically. The values set by the hardware might be incorrect, under 133Mhz PLL.
  *     This function overwrites the possibly incorrect values set by the hardware to be correct.
  */
-static void pdc2027x_post_set_mode(struct ata_port *ap)
+static int pdc2027x_set_mode(struct ata_port *ap, struct ata_device **r_failed)
 {
        int i;
 
+       i = ata_do_set_mode(ap, r_failed);
+       if (i < 0)
+               return i;
+
        for (i = 0; i < ATA_MAX_DEVICES; i++) {
                struct ata_device *dev = &ap->device[i];
 
@@ -476,6 +509,7 @@ static void pdc2027x_post_set_mode(struct ata_port *ap)
                        }
                }
        }
+       return 0;
 }
 
 /**
@@ -526,18 +560,19 @@ static int pdc2027x_check_atapi_dma(struct ata_queued_cmd *qc)
 
 static long pdc_read_counter(struct ata_probe_ent *probe_ent)
 {
+       void __iomem *mmio_base = probe_ent->iomap[PDC_MMIO_BAR];
        long counter;
        int retry = 1;
        u32 bccrl, bccrh, bccrlv, bccrhv;
 
 retry:
-       bccrl = readl(probe_ent->mmio_base + PDC_BYTE_COUNT) & 0xffff;
-       bccrh = readl(probe_ent->mmio_base + PDC_BYTE_COUNT + 0x100) & 0xffff;
+       bccrl = readl(mmio_base + PDC_BYTE_COUNT) & 0xffff;
+       bccrh = readl(mmio_base + PDC_BYTE_COUNT + 0x100) & 0xffff;
        rmb();
 
        /* Read the counter values again for verification */
-       bccrlv = readl(probe_ent->mmio_base + PDC_BYTE_COUNT) & 0xffff;
-       bccrhv = readl(probe_ent->mmio_base + PDC_BYTE_COUNT + 0x100) & 0xffff;
+       bccrlv = readl(mmio_base + PDC_BYTE_COUNT) & 0xffff;
+       bccrhv = readl(mmio_base + PDC_BYTE_COUNT + 0x100) & 0xffff;
        rmb();
 
        counter = (bccrh << 15) | bccrl;
@@ -568,7 +603,7 @@ retry:
  */
 static void pdc_adjust_pll(struct ata_probe_ent *probe_ent, long pll_clock, unsigned int board_idx)
 {
-
+       void __iomem *mmio_base = probe_ent->iomap[PDC_MMIO_BAR];
        u16 pll_ctl;
        long pll_clock_khz = pll_clock / 1000;
        long pout_required = board_idx? PDC_133_MHZ:PDC_100_MHZ;
@@ -587,7 +622,7 @@ static void pdc_adjust_pll(struct ata_probe_ent *probe_ent, long pll_clock, unsi
        /* Show the current clock value of PLL control register
         * (maybe already configured by the firmware)
         */
-       pll_ctl = readw(probe_ent->mmio_base + PDC_PLL_CTL);
+       pll_ctl = readw(mmio_base + PDC_PLL_CTL);
 
        PDPRINTK("pll_ctl[%X]\n", pll_ctl);
 #endif
@@ -627,8 +662,8 @@ static void pdc_adjust_pll(struct ata_probe_ent *probe_ent, long pll_clock, unsi
 
        PDPRINTK("Writing pll_ctl[%X]\n", pll_ctl);
 
-       writew(pll_ctl, probe_ent->mmio_base + PDC_PLL_CTL);
-       readw(probe_ent->mmio_base + PDC_PLL_CTL); /* flush */
+       writew(pll_ctl, mmio_base + PDC_PLL_CTL);
+       readw(mmio_base + PDC_PLL_CTL); /* flush */
 
        /* Wait the PLL circuit to be stable */
        mdelay(30);
@@ -638,7 +673,7 @@ static void pdc_adjust_pll(struct ata_probe_ent *probe_ent, long pll_clock, unsi
         *  Show the current clock value of PLL control register
         * (maybe configured by the firmware)
         */
-       pll_ctl = readw(probe_ent->mmio_base + PDC_PLL_CTL);
+       pll_ctl = readw(mmio_base + PDC_PLL_CTL);
 
        PDPRINTK("pll_ctl[%X]\n", pll_ctl);
 #endif
@@ -654,6 +689,7 @@ static void pdc_adjust_pll(struct ata_probe_ent *probe_ent, long pll_clock, unsi
  */
 static long pdc_detect_pll_input_clock(struct ata_probe_ent *probe_ent)
 {
+       void __iomem *mmio_base = probe_ent->iomap[PDC_MMIO_BAR];
        u32 scr;
        long start_count, end_count;
        long pll_clock;
@@ -662,10 +698,10 @@ static long pdc_detect_pll_input_clock(struct ata_probe_ent *probe_ent)
        start_count = pdc_read_counter(probe_ent);
 
        /* Start the test mode */
-       scr = readl(probe_ent->mmio_base + PDC_SYS_CTL);
+       scr = readl(mmio_base + PDC_SYS_CTL);
        PDPRINTK("scr[%X]\n", scr);
-       writel(scr | (0x01 << 14), probe_ent->mmio_base + PDC_SYS_CTL);
-       readl(probe_ent->mmio_base + PDC_SYS_CTL); /* flush */
+       writel(scr | (0x01 << 14), mmio_base + PDC_SYS_CTL);
+       readl(mmio_base + PDC_SYS_CTL); /* flush */
 
        /* Let the counter run for 100 ms. */
        mdelay(100);
@@ -674,10 +710,10 @@ static long pdc_detect_pll_input_clock(struct ata_probe_ent *probe_ent)
        end_count = pdc_read_counter(probe_ent);
 
        /* Stop the test mode */
-       scr = readl(probe_ent->mmio_base + PDC_SYS_CTL);
+       scr = readl(mmio_base + PDC_SYS_CTL);
        PDPRINTK("scr[%X]\n", scr);
-       writel(scr & ~(0x01 << 14), probe_ent->mmio_base + PDC_SYS_CTL);
-       readl(probe_ent->mmio_base + PDC_SYS_CTL); /* flush */
+       writel(scr & ~(0x01 << 14), mmio_base + PDC_SYS_CTL);
+       readl(mmio_base + PDC_SYS_CTL); /* flush */
 
        /* calculate the input clock in Hz */
        pll_clock = (start_count - end_count) * 10;
@@ -722,7 +758,7 @@ static int pdc_hardware_init(struct pci_dev *pdev, struct ata_probe_ent *pe, uns
  * @port: ata ioports to setup
  * @base: base address
  */
-static void pdc_ata_setup_port(struct ata_ioports *port, unsigned long base)
+static void pdc_ata_setup_port(struct ata_ioports *port, void __iomem *base)
 {
        port->cmd_addr          =
        port->data_addr         = base;
@@ -755,48 +791,37 @@ static int __devinit pdc2027x_init_one(struct pci_dev *pdev, const struct pci_de
        static int printed_version;
        unsigned int board_idx = (unsigned int) ent->driver_data;
 
-       struct ata_probe_ent *probe_ent = NULL;
-       unsigned long base;
+       struct ata_probe_ent *probe_ent;
        void __iomem *mmio_base;
        int rc;
 
        if (!printed_version++)
                dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
 
-       rc = pci_enable_device(pdev);
+       rc = pcim_enable_device(pdev);
        if (rc)
                return rc;
 
-       rc = pci_request_regions(pdev, DRV_NAME);
+       rc = pcim_iomap_regions(pdev, 1 << PDC_MMIO_BAR, DRV_NAME);
        if (rc)
-               goto err_out;
+               return rc;
 
        rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
        if (rc)
-               goto err_out_regions;
+               return rc;
 
        rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
        if (rc)
-               goto err_out_regions;
+               return rc;
 
        /* Prepare the probe entry */
-       probe_ent = kzalloc(sizeof(*probe_ent), GFP_KERNEL);
-       if (probe_ent == NULL) {
-               rc = -ENOMEM;
-               goto err_out_regions;
-       }
+       probe_ent = devm_kzalloc(&pdev->dev, sizeof(*probe_ent), GFP_KERNEL);
+       if (probe_ent == NULL)
+               return -ENOMEM;
 
        probe_ent->dev = pci_dev_to_dev(pdev);
        INIT_LIST_HEAD(&probe_ent->node);
 
-       mmio_base = pci_iomap(pdev, 5, 0);
-       if (!mmio_base) {
-               rc = -ENOMEM;
-               goto err_out_free_ent;
-       }
-
-       base = (unsigned long) mmio_base;
-
        probe_ent->sht          = pdc2027x_port_info[board_idx].sht;
        probe_ent->port_flags   = pdc2027x_port_info[board_idx].flags;
        probe_ent->pio_mask     = pdc2027x_port_info[board_idx].pio_mask;
@@ -805,13 +830,15 @@ static int __devinit pdc2027x_init_one(struct pci_dev *pdev, const struct pci_de
        probe_ent->port_ops     = pdc2027x_port_info[board_idx].port_ops;
 
                probe_ent->irq = pdev->irq;
-               probe_ent->irq_flags = SA_SHIRQ;
-       probe_ent->mmio_base = mmio_base;
+               probe_ent->irq_flags = IRQF_SHARED;
+       probe_ent->iomap = pcim_iomap_table(pdev);
+
+       mmio_base = probe_ent->iomap[PDC_MMIO_BAR];
 
-       pdc_ata_setup_port(&probe_ent->port[0], base + 0x17c0);
-       probe_ent->port[0].bmdma_addr = base + 0x1000;
-       pdc_ata_setup_port(&probe_ent->port[1], base + 0x15c0);
-       probe_ent->port[1].bmdma_addr = base + 0x1008;
+       pdc_ata_setup_port(&probe_ent->port[0], mmio_base + 0x17c0);
+       probe_ent->port[0].bmdma_addr = mmio_base + 0x1000;
+       pdc_ata_setup_port(&probe_ent->port[1], mmio_base + 0x15c0);
+       probe_ent->port[1].bmdma_addr = mmio_base + 0x1008;
 
        probe_ent->n_ports = 2;
 
@@ -820,32 +847,13 @@ static int __devinit pdc2027x_init_one(struct pci_dev *pdev, const struct pci_de
 
        /* initialize adapter */
        if (pdc_hardware_init(pdev, probe_ent, board_idx) != 0)
-               goto err_out_free_ent;
+               return -EIO;
 
-       ata_device_add(probe_ent);
-       kfree(probe_ent);
+       if (!ata_device_add(probe_ent))
+               return -ENODEV;
 
+       devm_kfree(&pdev->dev, probe_ent);
        return 0;
-
-err_out_free_ent:
-       kfree(probe_ent);
-err_out_regions:
-       pci_release_regions(pdev);
-err_out:
-       pci_disable_device(pdev);
-       return rc;
-}
-
-/**
- * pdc2027x_remove_one - Called to remove a single instance of the
- * adapter.
- *
- * @dev: The PCI device to remove.
- * FIXME: module load/unload not working yet
- */
-static void __devexit pdc2027x_remove_one(struct pci_dev *pdev)
-{
-       ata_pci_remove_one(pdev);
 }
 
 /**