Merge branch 'for-2.6.35' of git://git.kernel.org/pub/scm/linux/kernel/git/broonie...
[safe/jmp/linux-2.6] / drivers / ata / pata_legacy.c
index 2474068..9df1ff7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *   pata-legacy.c - Legacy port PATA/SATA controller driver.
- *   Copyright 2005/2006 Red Hat <alan@redhat.com>, all rights reserved.
+ *   Copyright 2005/2006 Red Hat, all rights reserved.
  *
  *  This program is free software; you can redistribute it and/or modify
  *  it under the terms of the GNU General Public License as published by
  *             http://www.ryston.cz/petr/vlb/pdc20230b.html
  *             http://www.ryston.cz/petr/vlb/pdc20230c.html
  *             http://www.ryston.cz/petr/vlb/pdc20630.html
+ *     QDI65x0:
+ *             http://www.ryston.cz/petr/vlb/qd6500.html
+ *             http://www.ryston.cz/petr/vlb/qd6580.html
+ *
+ *     QDI65x0 probe code based on drivers/ide/legacy/qd65xx.c
+ *     Rewritten from the work of Colten Edwards <pje120@cs.usask.ca> by
+ *     Samuel Thibault <samuel.thibault@ens-lyon.org>
  *
  *  Unsupported but docs exist:
  *     Appian/Adaptec AIC25VL01/Cirrus Logic PD7220
@@ -35,7 +42,7 @@
  *  the MPIIX where the tuning is PCI side but the IDE is "ISA side".
  *
  *  Specific support is included for the ht6560a/ht6560b/opti82c611a/
- *  opti82c465mv/promise 20230c/20630/winbond83759A
+ *  opti82c465mv/promise 20230c/20630/qdi65x0/winbond83759A
  *
  *  Use the autospeed and pio_mask options with:
  *     Appian ADI/2 aka CLPD7220 or AIC25VL01.
@@ -48,6 +55,7 @@
  *
  */
 
+#include <linux/async.h>
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/pci.h>
@@ -108,6 +116,7 @@ struct legacy_controller {
        struct ata_port_operations *ops;
        unsigned int pio_mask;
        unsigned int flags;
+       unsigned int pflags;
        int (*setup)(struct platform_device *, struct legacy_probe *probe,
                struct legacy_data *data);
 };
@@ -129,7 +138,7 @@ static int qdi;                     /* Set to probe QDI controllers */
 static int winbond;            /* Set to probe Winbond controllers,
                                        give I/O port if non standard */
 static int autospeed;          /* Chip present which snoops speed changes */
-static int pio_mask = 0x1F;    /* PIO range for autospeed devices */
+static int pio_mask = ATA_PIO4;        /* PIO range for autospeed devices */
 static int iordy_mask = 0xFFFFFFFF;    /* Use iordy if available */
 
 /**
@@ -194,15 +203,12 @@ static int legacy_set_mode(struct ata_link *link, struct ata_device **unused)
 {
        struct ata_device *dev;
 
-       ata_link_for_each_dev(dev, link) {
-               if (ata_dev_enabled(dev)) {
-                       ata_dev_printk(dev, KERN_INFO,
-                                               "configured for PIO\n");
-                       dev->pio_mode = XFER_PIO_0;
-                       dev->xfer_mode = XFER_PIO_0;
-                       dev->xfer_shift = ATA_SHIFT_PIO;
-                       dev->flags |= ATA_DFLAG_PIO;
-               }
+       ata_for_each_dev(dev, link, ENABLED) {
+               ata_dev_printk(dev, KERN_INFO, "configured for PIO\n");
+               dev->pio_mode = XFER_PIO_0;
+               dev->xfer_mode = XFER_PIO_0;
+               dev->xfer_shift = ATA_SHIFT_PIO;
+               dev->flags |= ATA_DFLAG_PIO;
        }
        return 0;
 }
@@ -226,12 +232,12 @@ static const struct ata_port_operations legacy_base_port_ops = {
 
 static struct ata_port_operations simple_port_ops = {
        .inherits       = &legacy_base_port_ops,
-       .data_xfer      = ata_data_xfer_noirq,
+       .sff_data_xfer  = ata_sff_data_xfer_noirq,
 };
 
 static struct ata_port_operations legacy_port_ops = {
        .inherits       = &legacy_base_port_ops,
-       .data_xfer      = ata_data_xfer_noirq,
+       .sff_data_xfer  = ata_sff_data_xfer_noirq,
        .set_mode       = legacy_set_mode,
 };
 
@@ -286,9 +292,12 @@ static void pdc20230_set_piomode(struct ata_port *ap, struct ata_device *adev)
 static unsigned int pdc_data_xfer_vlb(struct ata_device *dev,
                        unsigned char *buf, unsigned int buflen, int rw)
 {
-       if (ata_id_has_dword_io(dev->id)) {
-               struct ata_port *ap = dev->link->ap;
-               int slop = buflen & 3;
+       int slop = buflen & 3;
+       struct ata_port *ap = dev->link->ap;
+
+       /* 32bit I/O capable *and* we need to write a whole number of dwords */
+       if (ata_id_has_dword_io(dev->id) && (slop == 0 || slop == 3)
+                                       && (ap->pflags & ATA_PFLAG_PIO32)) {
                unsigned long flags;
 
                local_irq_save(flags);
@@ -305,7 +314,7 @@ static unsigned int pdc_data_xfer_vlb(struct ata_device *dev,
                        iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
 
                if (unlikely(slop)) {
-                       u32 pad;
+                       __le32 pad;
                        if (rw == READ) {
                                pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
                                memcpy(buf + buflen - slop, &pad, slop);
@@ -317,7 +326,7 @@ static unsigned int pdc_data_xfer_vlb(struct ata_device *dev,
                }
                local_irq_restore(flags);
        } else
-               buflen = ata_data_xfer_noirq(dev, buf, buflen, rw);
+               buflen = ata_sff_data_xfer_noirq(dev, buf, buflen, rw);
 
        return buflen;
 }
@@ -325,7 +334,7 @@ static unsigned int pdc_data_xfer_vlb(struct ata_device *dev,
 static struct ata_port_operations pdc20230_port_ops = {
        .inherits       = &legacy_base_port_ops,
        .set_piomode    = pdc20230_set_piomode,
-       .data_xfer      = pdc_data_xfer_vlb,
+       .sff_data_xfer  = pdc_data_xfer_vlb,
 };
 
 /*
@@ -343,8 +352,8 @@ static void ht6560a_set_piomode(struct ata_port *ap, struct ata_device *adev)
        /* Get the timing data in cycles. For now play safe at 50Mhz */
        ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
 
-       active = FIT(t.active, 2, 15);
-       recover = FIT(t.recover, 4, 15);
+       active = clamp_val(t.active, 2, 15);
+       recover = clamp_val(t.recover, 4, 15);
 
        inb(0x3E6);
        inb(0x3E6);
@@ -377,8 +386,8 @@ static void ht6560b_set_piomode(struct ata_port *ap, struct ata_device *adev)
        /* Get the timing data in cycles. For now play safe at 50Mhz */
        ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
 
-       active = FIT(t.active, 2, 15);
-       recover = FIT(t.recover, 2, 16);
+       active = clamp_val(t.active, 2, 15);
+       recover = clamp_val(t.recover, 2, 16);
        recover &= 0x15;
 
        inb(0x3E6);
@@ -462,9 +471,9 @@ static void opti82c611a_set_piomode(struct ata_port *ap,
                ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
        }
 
-       active = FIT(t.active, 2, 17) - 2;
-       recover = FIT(t.recover, 1, 16) - 1;
-       setup = FIT(t.setup, 1, 4) - 1;
+       active = clamp_val(t.active, 2, 17) - 2;
+       recover = clamp_val(t.recover, 1, 16) - 1;
+       setup = clamp_val(t.setup, 1, 4) - 1;
 
        /* Select the right timing bank for write timing */
        rc = ioread8(ap->ioaddr.lbal_addr);
@@ -541,9 +550,9 @@ static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
                ata_timing_merge(&t, &tp, &t, ATA_TIMING_SETUP);
        }
 
-       active = FIT(t.active, 2, 17) - 2;
-       recover = FIT(t.recover, 1, 16) - 1;
-       setup = FIT(t.setup, 1, 4) - 1;
+       active = clamp_val(t.active, 2, 17) - 2;
+       recover = clamp_val(t.recover, 1, 16) - 1;
+       setup = clamp_val(t.setup, 1, 4) - 1;
 
        /* Select the right timing bank for write timing */
        rc = ioread8(ap->ioaddr.lbal_addr);
@@ -579,7 +588,7 @@ static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
 }
 
 /**
- *     opt82c465mv_qc_issue_prot       -       command issue
+ *     opt82c465mv_qc_issue            -       command issue
  *     @qc: command pending
  *
  *     Called when the libata layer is about to issue a command. We wrap
@@ -593,7 +602,7 @@ static void opti82c46x_set_piomode(struct ata_port *ap, struct ata_device *adev)
  *     FIXME: dual channel needs ->serialize support
  */
 
-static unsigned int opti82c46x_qc_issue_prot(struct ata_queued_cmd *qc)
+static unsigned int opti82c46x_qc_issue(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct ata_device *adev = qc->dev;
@@ -604,13 +613,13 @@ static unsigned int opti82c46x_qc_issue_prot(struct ata_queued_cmd *qc)
            && ap->host->private_data != NULL)
                opti82c46x_set_piomode(ap, adev);
 
-       return ata_qc_issue_prot(qc);
+       return ata_sff_qc_issue(qc);
 }
 
 static struct ata_port_operations opti82c46x_port_ops = {
        .inherits       = &legacy_base_port_ops,
        .set_piomode    = opti82c46x_set_piomode,
-       .qc_issue       = opti82c46x_qc_issue_prot,
+       .qc_issue       = opti82c46x_qc_issue,
 };
 
 static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
@@ -624,11 +633,11 @@ static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
        ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
 
        if (ld_qdi->fast) {
-               active = 8 - FIT(t.active, 1, 8);
-               recovery = 18 - FIT(t.recover, 3, 18);
+               active = 8 - clamp_val(t.active, 1, 8);
+               recovery = 18 - clamp_val(t.recover, 3, 18);
        } else {
-               active = 9 - FIT(t.active, 2, 9);
-               recovery = 15 - FIT(t.recover, 0, 15);
+               active = 9 - clamp_val(t.active, 2, 9);
+               recovery = 15 - clamp_val(t.recover, 0, 15);
        }
        timing = (recovery << 4) | active | 0x08;
 
@@ -641,10 +650,9 @@ static void qdi6500_set_piomode(struct ata_port *ap, struct ata_device *adev)
  *     qdi6580dp_set_piomode           -       PIO setup for dual channel
  *     @ap: Port
  *     @adev: Device
- *     @irq: interrupt line
  *
  *     In dual channel mode the 6580 has one clock per channel and we have
- *     to software clockswitch in qc_issue_prot.
+ *     to software clockswitch in qc_issue.
  */
 
 static void qdi6580dp_set_piomode(struct ata_port *ap, struct ata_device *adev)
@@ -658,11 +666,11 @@ static void qdi6580dp_set_piomode(struct ata_port *ap, struct ata_device *adev)
        ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
 
        if (ld_qdi->fast) {
-               active = 8 - FIT(t.active, 1, 8);
-               recovery = 18 - FIT(t.recover, 3, 18);
+               active = 8 - clamp_val(t.active, 1, 8);
+               recovery = 18 - clamp_val(t.recover, 3, 18);
        } else {
-               active = 9 - FIT(t.active, 2, 9);
-               recovery = 15 - FIT(t.recover, 0, 15);
+               active = 9 - clamp_val(t.active, 2, 9);
+               recovery = 15 - clamp_val(t.recover, 0, 15);
        }
        timing = (recovery << 4) | active | 0x08;
 
@@ -671,7 +679,7 @@ static void qdi6580dp_set_piomode(struct ata_port *ap, struct ata_device *adev)
        outb(timing, ld_qdi->timing + 2 * ap->port_no);
        /* Clear the FIFO */
        if (adev->class != ATA_DEV_ATA)
-               outb(0x5F, ld_qdi->timing + 3);
+               outb(0x5F, (ld_qdi->timing & 0xFFF0) + 3);
 }
 
 /**
@@ -695,29 +703,29 @@ static void qdi6580_set_piomode(struct ata_port *ap, struct ata_device *adev)
        ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
 
        if (ld_qdi->fast) {
-               active = 8 - FIT(t.active, 1, 8);
-               recovery = 18 - FIT(t.recover, 3, 18);
+               active = 8 - clamp_val(t.active, 1, 8);
+               recovery = 18 - clamp_val(t.recover, 3, 18);
        } else {
-               active = 9 - FIT(t.active, 2, 9);
-               recovery = 15 - FIT(t.recover, 0, 15);
+               active = 9 - clamp_val(t.active, 2, 9);
+               recovery = 15 - clamp_val(t.recover, 0, 15);
        }
        timing = (recovery << 4) | active | 0x08;
        ld_qdi->clock[adev->devno] = timing;
        outb(timing, ld_qdi->timing + 2 * adev->devno);
        /* Clear the FIFO */
        if (adev->class != ATA_DEV_ATA)
-               outb(0x5F, ld_qdi->timing + 3);
+               outb(0x5F, (ld_qdi->timing & 0xFFF0) + 3);
 }
 
 /**
- *     qdi_qc_issue_prot       -       command issue
+ *     qdi_qc_issue            -       command issue
  *     @qc: command pending
  *
  *     Called when the libata layer is about to issue a command. We wrap
  *     this interface so that we can load the correct ATA timings.
  */
 
-static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
+static unsigned int qdi_qc_issue(struct ata_queued_cmd *qc)
 {
        struct ata_port *ap = qc->ap;
        struct ata_device *adev = qc->dev;
@@ -730,7 +738,7 @@ static unsigned int qdi_qc_issue_prot(struct ata_queued_cmd *qc)
                                                        2 * ap->port_no);
                }
        }
-       return ata_qc_issue_prot(qc);
+       return ata_sff_qc_issue(qc);
 }
 
 static unsigned int vlb32_data_xfer(struct ata_device *adev, unsigned char *buf,
@@ -739,27 +747,26 @@ static unsigned int vlb32_data_xfer(struct ata_device *adev, unsigned char *buf,
        struct ata_port *ap = adev->link->ap;
        int slop = buflen & 3;
 
-       if (ata_id_has_dword_io(adev->id)) {
+       if (ata_id_has_dword_io(adev->id) && (slop == 0 || slop == 3)
+                                       && (ap->pflags & ATA_PFLAG_PIO32)) {
                if (rw == WRITE)
                        iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
                else
                        ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
 
                if (unlikely(slop)) {
-                       u32 pad;
+                       __le32 pad;
                        if (rw == WRITE) {
                                memcpy(&pad, buf + buflen - slop, slop);
-                               pad = le32_to_cpu(pad);
-                               iowrite32(pad, ap->ioaddr.data_addr);
+                               iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
                        } else {
-                               pad = ioread32(ap->ioaddr.data_addr);
-                               pad = cpu_to_le32(pad);
+                               pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
                                memcpy(buf + buflen - slop, &pad, slop);
                        }
                }
                return (buflen + 3) & ~3;
        } else
-               return ata_data_xfer(adev, buf, buflen, rw);
+               return ata_sff_data_xfer(adev, buf, buflen, rw);
 }
 
 static int qdi_port(struct platform_device *dev,
@@ -774,20 +781,21 @@ static int qdi_port(struct platform_device *dev,
 static struct ata_port_operations qdi6500_port_ops = {
        .inherits       = &legacy_base_port_ops,
        .set_piomode    = qdi6500_set_piomode,
-       .qc_issue       = qdi_qc_issue_prot,
-       .data_xfer      = vlb32_data_xfer,
+       .qc_issue       = qdi_qc_issue,
+       .sff_data_xfer  = vlb32_data_xfer,
 };
 
 static struct ata_port_operations qdi6580_port_ops = {
        .inherits       = &legacy_base_port_ops,
        .set_piomode    = qdi6580_set_piomode,
-       .data_xfer      = vlb32_data_xfer,
+       .sff_data_xfer  = vlb32_data_xfer,
 };
 
 static struct ata_port_operations qdi6580dp_port_ops = {
        .inherits       = &legacy_base_port_ops,
        .set_piomode    = qdi6580dp_set_piomode,
-       .data_xfer      = vlb32_data_xfer,
+       .qc_issue       = qdi_qc_issue,
+       .sff_data_xfer  = vlb32_data_xfer,
 };
 
 static DEFINE_SPINLOCK(winbond_lock);
@@ -830,8 +838,8 @@ static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev)
        else
                ata_timing_compute(adev, adev->pio_mode, &t, 30303, 1000);
 
-       active = (FIT(t.active, 3, 17) - 1) & 0x0F;
-       recovery = (FIT(t.recover, 1, 15) + 1) & 0x0F;
+       active = (clamp_val(t.active, 3, 17) - 1) & 0x0F;
+       recovery = (clamp_val(t.recover, 1, 15) + 1) & 0x0F;
        timing = (active << 4) | recovery;
        winbond_writecfg(ld_winbond->timing, timing, reg);
 
@@ -842,7 +850,7 @@ static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev)
                reg |= 0x08;    /* FIFO off */
        if (!ata_pio_need_iordy(adev))
                reg |= 0x02;    /* IORDY off */
-       reg |= (FIT(t.setup, 0, 3) << 6);
+       reg |= (clamp_val(t.setup, 0, 3) << 6);
        winbond_writecfg(ld_winbond->timing, timing + 1, reg);
 }
 
@@ -858,32 +866,35 @@ static int winbond_port(struct platform_device *dev,
 static struct ata_port_operations winbond_port_ops = {
        .inherits       = &legacy_base_port_ops,
        .set_piomode    = winbond_set_piomode,
-       .data_xfer      = vlb32_data_xfer,
+       .sff_data_xfer  = vlb32_data_xfer,
 };
 
 static struct legacy_controller controllers[] = {
        {"BIOS",        &legacy_port_ops,       0x1F,
-                                               ATA_FLAG_NO_IORDY,      NULL },
+                       ATA_FLAG_NO_IORDY,      0,                      NULL },
        {"Snooping",    &simple_port_ops,       0x1F,
-                                               0              ,        NULL },
+                       0,                      0,                      NULL },
        {"PDC20230",    &pdc20230_port_ops,     0x7,
-                                               ATA_FLAG_NO_IORDY,      NULL },
+                       ATA_FLAG_NO_IORDY,
+                       ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE,        NULL },
        {"HT6560A",     &ht6560a_port_ops,      0x07,
-                                               ATA_FLAG_NO_IORDY,      NULL },
+                       ATA_FLAG_NO_IORDY,      0,                      NULL },
        {"HT6560B",     &ht6560b_port_ops,      0x1F,
-                                               ATA_FLAG_NO_IORDY,      NULL },
+                       ATA_FLAG_NO_IORDY,      0,                      NULL },
        {"OPTI82C611A", &opti82c611a_port_ops,  0x0F,
-                                               0              ,        NULL },
+                       0,                      0,                      NULL },
        {"OPTI82C46X",  &opti82c46x_port_ops,   0x0F,
-                                               0              ,        NULL },
+                       0,                      0,                      NULL },
        {"QDI6500",     &qdi6500_port_ops,      0x07,
-                                       ATA_FLAG_NO_IORDY,      qdi_port },
+                       ATA_FLAG_NO_IORDY,
+                       ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE,    qdi_port },
        {"QDI6580",     &qdi6580_port_ops,      0x1F,
-                                       0              ,        qdi_port },
+                       0, ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE, qdi_port },
        {"QDI6580DP",   &qdi6580dp_port_ops,    0x1F,
-                                       0              ,        qdi_port },
+                       0, ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE, qdi_port },
        {"W83759A",     &winbond_port_ops,      0x1F,
-                                       0              ,        winbond_port }
+                       0, ATA_PFLAG_PIO32 | ATA_PFLAG_PIO32CHANGE,
+                                                               winbond_port }
 };
 
 /**
@@ -1013,30 +1024,33 @@ static __init int legacy_init_one(struct legacy_probe *probe)
        ap->ops = ops;
        ap->pio_mask = pio_modes;
        ap->flags |= ATA_FLAG_SLAVE_POSS | iordy;
+       ap->pflags |= controller->pflags;
        ap->ioaddr.cmd_addr = io_addr;
        ap->ioaddr.altstatus_addr = ctrl_addr;
        ap->ioaddr.ctl_addr = ctrl_addr;
-       ata_std_ports(&ap->ioaddr);
+       ata_sff_std_ports(&ap->ioaddr);
        ap->host->private_data = ld;
 
        ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", io, io + 0x0206);
 
-       ret = ata_host_activate(host, probe->irq, ata_interrupt, 0,
-                                                               &legacy_sht);
+       ret = ata_host_activate(host, probe->irq, ata_sff_interrupt, 0,
+                               &legacy_sht);
        if (ret)
                goto fail;
+       async_synchronize_full();
        ld->platform_dev = pdev;
 
        /* Nothing found means we drop the port as its probably not there */
 
        ret = -ENODEV;
-       ata_link_for_each_dev(dev, &ap->link) {
+       ata_for_each_dev(dev, &ap->link, ALL) {
                if (!ata_dev_absent(dev)) {
                        legacy_host[probe->slot] = host;
                        ld->platform_dev = pdev;
                        return 0;
                }
        }
+       ata_host_detach(host);
 fail:
        platform_device_unregister(pdev);
        return ret;