pata_hpt37x: add proper cable detection methods
[safe/jmp/linux-2.6] / drivers / ata / pata_winbond.c
index 3ea345c..6d8619b 100644 (file)
@@ -1,14 +1,13 @@
 /*
  *    pata_winbond.c - Winbond VLB ATA controllers
- *     (C) 2006 Red Hat <alan@redhat.com>
+ *     (C) 2006 Red Hat
  *
  *    Support for the Winbond 83759A when operating in advanced mode.
  *    Multichip mode is not currently supported.
  */
+
 #include <linux/kernel.h>
 #include <linux/module.h>
-#include <linux/pci.h>
 #include <linux/init.h>
 #include <linux/blkdev.h>
 #include <linux/delay.h>
@@ -17,7 +16,7 @@
 #include <linux/platform_device.h>
 
 #define DRV_NAME "pata_winbond"
-#define DRV_VERSION "0.0.1"
+#define DRV_VERSION "0.0.3"
 
 #define NR_HOST 4      /* Two winbond controllers, two channels each */
 
@@ -36,7 +35,7 @@ static int probe_winbond = 1;
 static int probe_winbond;
 #endif
 
-static spinlock_t winbond_lock = SPIN_LOCK_UNLOCKED;
+static DEFINE_SPINLOCK(winbond_lock);
 
 static void winbond_writecfg(unsigned long port, u8 reg, u8 val)
 {
@@ -69,99 +68,68 @@ static void winbond_set_piomode(struct ata_port *ap, struct ata_device *adev)
        int timing = 0x88 + (ap->port_no * 4) + (adev->devno * 2);
 
        reg = winbond_readcfg(winbond->config, 0x81);
-       
+
        /* Get the timing data in cycles */
        if (reg & 0x40)         /* Fast VLB bus, assume 50MHz */
                ata_timing_compute(adev, adev->pio_mode, &t, 20000, 1000);
        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(winbond->config, timing, reg);
-       
+
        /* Load the setup timing */
-       
+
        reg = 0x35;
        if (adev->class != ATA_DEV_ATA)
                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(winbond->config, timing + 1, reg);
 }
 
 
-static void winbond_data_xfer(struct ata_device *adev, unsigned char *buf, unsigned int buflen, int write_data)
+static unsigned int winbond_data_xfer(struct ata_device *dev,
+                       unsigned char *buf, unsigned int buflen, int rw)
 {
-       struct ata_port *ap = adev->ap;
+       struct ata_port *ap = dev->link->ap;
        int slop = buflen & 3;
 
-       if (ata_id_has_dword_io(adev->id)) {
-               if (write_data)
-                       outsl(ap->ioaddr.data_addr, buf, buflen >> 2);
+       if (ata_id_has_dword_io(dev->id)) {
+               if (rw == READ)
+                       ioread32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
                else
-                       insl(ap->ioaddr.data_addr, buf, buflen >> 2);
+                       iowrite32_rep(ap->ioaddr.data_addr, buf, buflen >> 2);
 
                if (unlikely(slop)) {
-                       u32 pad;
-                       if (write_data) {
-                               memcpy(&pad, buf + buflen - slop, slop);
-                               outl(le32_to_cpu(pad), ap->ioaddr.data_addr);
-                       } else {
-                               pad = cpu_to_le16(inl(ap->ioaddr.data_addr));
+                       __le32 pad;
+                       if (rw == READ) {
+                               pad = cpu_to_le32(ioread32(ap->ioaddr.data_addr));
                                memcpy(buf + buflen - slop, &pad, slop);
+                       } else {
+                               memcpy(&pad, buf + buflen - slop, slop);
+                               iowrite32(le32_to_cpu(pad), ap->ioaddr.data_addr);
                        }
+                       buflen += 4 - slop;
                }
        } else
-               ata_pio_data_xfer(adev, buf, buflen, write_data);
+               buflen = ata_sff_data_xfer(dev, buf, buflen, rw);
+
+       return buflen;
 }
 
 static struct scsi_host_template winbond_sht = {
-       .module                 = THIS_MODULE,
-       .name                   = DRV_NAME,
-       .ioctl                  = ata_scsi_ioctl,
-       .queuecommand           = ata_scsi_queuecmd,
-       .can_queue              = ATA_DEF_QUEUE,
-       .this_id                = ATA_SHT_THIS_ID,
-       .sg_tablesize           = LIBATA_MAX_PRD,
-       .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
-       .emulated               = ATA_SHT_EMULATED,
-       .use_clustering         = ATA_SHT_USE_CLUSTERING,
-       .proc_name              = DRV_NAME,
-       .dma_boundary           = ATA_DMA_BOUNDARY,
-       .slave_configure        = ata_scsi_slave_config,
-       .slave_destroy          = ata_scsi_slave_destroy,
-       .bios_param             = ata_std_bios_param,
+       ATA_PIO_SHT(DRV_NAME),
 };
 
 static struct ata_port_operations winbond_port_ops = {
-       .port_disable   = ata_port_disable,
+       .inherits       = &ata_sff_port_ops,
+       .sff_data_xfer  = winbond_data_xfer,
+       .cable_detect   = ata_cable_40wire,
        .set_piomode    = winbond_set_piomode,
-
-       .tf_load        = ata_tf_load,
-       .tf_read        = ata_tf_read,
-       .check_status   = ata_check_status,
-       .exec_command   = ata_exec_command,
-       .dev_select     = ata_std_dev_select,
-
-       .freeze         = ata_bmdma_freeze,
-       .thaw           = ata_bmdma_thaw,
-       .error_handler  = ata_bmdma_error_handler,
-       .post_internal_cmd = ata_bmdma_post_internal_cmd,
-
-       .qc_prep        = ata_qc_prep,
-       .qc_issue       = ata_qc_issue_prot,
-
-       .data_xfer      = winbond_data_xfer,
-
-       .irq_handler    = ata_interrupt,
-       .irq_clear      = ata_bmdma_irq_clear,
-
-       .port_start     = ata_port_start,
-       .port_stop      = ata_port_stop,
-       .host_stop      = ata_host_stop
 };
 
 /**
@@ -177,11 +145,9 @@ static struct ata_port_operations winbond_port_ops = {
 
 static __init int winbond_init_one(unsigned long port)
 {
-       struct ata_probe_ent ae;
        struct platform_device *pdev;
-       int ret;
        u8 reg;
-       int i;
+       int i, rc;
 
        reg = winbond_readcfg(port, 0x81);
        reg |= 0x80;    /* jumpered mode off */
@@ -194,55 +160,65 @@ static __init int winbond_init_one(unsigned long port)
        winbond_writecfg(port, 0x85, reg);
 
        reg = winbond_readcfg(port, 0x81);
-       
+
        if (!(reg & 0x03))              /* Disabled */
-               return 0;
+               return -ENODEV;
 
        for (i = 0; i < 2 ; i ++) {
-
-               if (reg & (1 << i)) {           
-                       /*
-                        *      Fill in a probe structure first of all
-                        */
-
-                       pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
-                       if (pdev == NULL)
-                               return -ENOMEM;
-
-                       memset(&ae, 0, sizeof(struct ata_probe_ent));
-                       INIT_LIST_HEAD(&ae.node);
-                       ae.dev = &pdev->dev;
-
-                       ae.port_ops = &winbond_port_ops;
-                       ae.pio_mask = 0x1F;
-
-                       ae.sht = &winbond_sht;
-       
-                       ae.n_ports = 1;
-                       ae.irq = 14 + i;
-                       ae.irq_flags = 0;
-                       ae.port_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST;
-                       ae.port[0].cmd_addr = 0x1F0 - (0x80 * i);
-                       ae.port[0].altstatus_addr = ae.port[0].cmd_addr + 0x0206;
-                       ae.port[0].ctl_addr = ae.port[0].altstatus_addr;
-                       ata_std_ports(&ae.port[0]);
-                       /*
-                        *      Hook in a private data structure per channel
-                        */
-                       ae.private_data = &winbond_data[nr_winbond_host];
-                       winbond_data[nr_winbond_host].config = port;
-                       winbond_data[nr_winbond_host].platform_dev = pdev;
-
-                       ret = ata_device_add(&ae);
-                       if (ret == 0) {
-                               platform_device_unregister(pdev);
-                               return -ENODEV;
-                       }
-                       winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
-               }
+               unsigned long cmd_port = 0x1F0 - (0x80 * i);
+               unsigned long ctl_port = cmd_port + 0x206;
+               struct ata_host *host;
+               struct ata_port *ap;
+               void __iomem *cmd_addr, *ctl_addr;
+
+               if (!(reg & (1 << i)))
+                       continue;
+
+               pdev = platform_device_register_simple(DRV_NAME, nr_winbond_host, NULL, 0);
+               if (IS_ERR(pdev))
+                       return PTR_ERR(pdev);
+
+               rc = -ENOMEM;
+               host = ata_host_alloc(&pdev->dev, 1);
+               if (!host)
+                       goto err_unregister;
+               ap = host->ports[0];
+
+               rc = -ENOMEM;
+               cmd_addr = devm_ioport_map(&pdev->dev, cmd_port, 8);
+               ctl_addr = devm_ioport_map(&pdev->dev, ctl_port, 1);
+               if (!cmd_addr || !ctl_addr)
+                       goto err_unregister;
+
+               ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", cmd_port, ctl_port);
+
+               ap->ops = &winbond_port_ops;
+               ap->pio_mask = ATA_PIO4;
+               ap->flags |= ATA_FLAG_SLAVE_POSS;
+               ap->ioaddr.cmd_addr = cmd_addr;
+               ap->ioaddr.altstatus_addr = ctl_addr;
+               ap->ioaddr.ctl_addr = ctl_addr;
+               ata_sff_std_ports(&ap->ioaddr);
+
+               /* hook in a private data structure per channel */
+               host->private_data = &winbond_data[nr_winbond_host];
+               winbond_data[nr_winbond_host].config = port;
+               winbond_data[nr_winbond_host].platform_dev = pdev;
+
+               /* activate */
+               rc = ata_host_activate(host, 14 + i, ata_sff_interrupt, 0,
+                                      &winbond_sht);
+               if (rc)
+                       goto err_unregister;
+
+               winbond_host[nr_winbond_host++] = dev_get_drvdata(&pdev->dev);
        }
 
        return 0;
+
+ err_unregister:
+       platform_device_unregister(pdev);
+       return rc;
 }
 
 /**
@@ -257,7 +233,7 @@ static __init int winbond_init(void)
 
        int ct = 0;
        int i;
-       
+
        if (probe_winbond == 0)
                return -ENODEV;
 
@@ -272,7 +248,7 @@ static __init int winbond_init(void)
 
                        if (request_region(port, 2, "pata_winbond")) {
                                ret = winbond_init_one(port);
-                               if(ret <= 0)
+                               if (ret <= 0)
                                        release_region(port, 2);
                                else ct+= ret;
                        }
@@ -288,7 +264,7 @@ static __exit void winbond_exit(void)
        int i;
 
        for (i = 0; i < nr_winbond_host; i++) {
-               ata_host_remove(winbond_host[i]);
+               ata_host_detach(winbond_host[i]);
                release_region(winbond_data[i].config, 2);
                platform_device_unregister(winbond_data[i].platform_dev);
        }