Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 1 | /* |
| 2 | * ixp4xx PATA/Compact Flash driver |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 3 | * Copyright (C) 2006-07 Tower Technologies |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 4 | * Author: Alessandro Zummo <a.zummo@towertech.it> |
| 5 | * |
| 6 | * An ATA driver to handle a Compact Flash connected |
| 7 | * to the ixp4xx expansion bus in TrueIDE mode. The CF |
| 8 | * must have it chip selects connected to two CS lines |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 9 | * on the ixp4xx. In the irq is not available, you might |
| 10 | * want to modify both this driver and libata to run in |
| 11 | * polling mode. |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 12 | * |
| 13 | * This program is free software; you can redistribute it and/or modify |
| 14 | * it under the terms of the GNU General Public License version 2 as |
| 15 | * published by the Free Software Foundation. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include <linux/kernel.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/libata.h> |
| 22 | #include <linux/irq.h> |
| 23 | #include <linux/platform_device.h> |
| 24 | #include <scsi/scsi_host.h> |
| 25 | |
| 26 | #define DRV_NAME "pata_ixp4xx_cf" |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 27 | #define DRV_VERSION "0.2" |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 28 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 29 | static int ixp4xx_set_mode(struct ata_link *link, struct ata_device **error) |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 30 | { |
Tejun Heo | f58229f | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 31 | struct ata_device *dev; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 32 | |
Tejun Heo | 0260731 | 2007-08-06 18:36:23 +0900 | [diff] [blame] | 33 | ata_link_for_each_dev(dev, link) { |
Tejun Heo | 9666f40 | 2007-05-04 21:27:47 +0200 | [diff] [blame] | 34 | if (ata_dev_enabled(dev)) { |
Alan | 3ddcc59 | 2007-02-20 17:45:55 +0000 | [diff] [blame] | 35 | ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n"); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 36 | dev->pio_mode = XFER_PIO_0; |
| 37 | dev->xfer_mode = XFER_PIO_0; |
| 38 | dev->xfer_shift = ATA_SHIFT_PIO; |
| 39 | dev->flags |= ATA_DFLAG_PIO; |
| 40 | } |
| 41 | } |
Alan | b229a7b | 2007-01-24 11:47:07 +0000 | [diff] [blame] | 42 | return 0; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 43 | } |
| 44 | |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 45 | static unsigned int ixp4xx_mmio_data_xfer(struct ata_device *dev, |
| 46 | unsigned char *buf, unsigned int buflen, int rw) |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 47 | { |
| 48 | unsigned int i; |
| 49 | unsigned int words = buflen >> 1; |
| 50 | u16 *buf16 = (u16 *) buf; |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 51 | struct ata_port *ap = dev->link->ap; |
Jeff Garzik | 59f9988 | 2007-05-28 07:07:20 -0400 | [diff] [blame] | 52 | void __iomem *mmio = ap->ioaddr.data_addr; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 53 | struct ixp4xx_pata_data *data = ap->host->dev->platform_data; |
| 54 | |
| 55 | /* set the expansion bus in 16bit mode and restore |
| 56 | * 8 bit mode after the transaction. |
| 57 | */ |
| 58 | *data->cs0_cfg &= ~(0x01); |
| 59 | udelay(100); |
| 60 | |
| 61 | /* Transfer multiple of 2 bytes */ |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 62 | if (rw == READ) |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 63 | for (i = 0; i < words; i++) |
| 64 | buf16[i] = readw(mmio); |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 65 | else |
| 66 | for (i = 0; i < words; i++) |
| 67 | writew(buf16[i], mmio); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 68 | |
| 69 | /* Transfer trailing 1 byte, if any. */ |
| 70 | if (unlikely(buflen & 0x01)) { |
| 71 | u16 align_buf[1] = { 0 }; |
| 72 | unsigned char *trailing_buf = buf + buflen - 1; |
| 73 | |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 74 | if (rw == READ) { |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 75 | align_buf[0] = readw(mmio); |
| 76 | memcpy(trailing_buf, align_buf, 1); |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 77 | } else { |
| 78 | memcpy(align_buf, trailing_buf, 1); |
| 79 | writew(align_buf[0], mmio); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 80 | } |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 81 | words++; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | udelay(100); |
| 85 | *data->cs0_cfg |= 0x01; |
Tejun Heo | 55dba31 | 2007-12-05 16:43:07 +0900 | [diff] [blame] | 86 | |
| 87 | return words << 1; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 88 | } |
| 89 | |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 90 | static struct scsi_host_template ixp4xx_sht = { |
Tejun Heo | 68d1d07 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 91 | ATA_PIO_SHT(DRV_NAME), |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | static struct ata_port_operations ixp4xx_port_ops = { |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 95 | .inherits = &ata_sff_port_ops, |
Tejun Heo | 5682ed3 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 96 | .sff_data_xfer = ixp4xx_mmio_data_xfer, |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 97 | .cable_detect = ata_cable_40wire, |
Tejun Heo | 029cfd6 | 2008-03-25 12:22:49 +0900 | [diff] [blame] | 98 | .set_mode = ixp4xx_set_mode, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 99 | }; |
| 100 | |
Rod Whitby | af18374 | 2008-01-06 10:05:28 +0900 | [diff] [blame] | 101 | static void ixp4xx_setup_port(struct ata_port *ap, |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 102 | struct ixp4xx_pata_data *data, |
| 103 | unsigned long raw_cs0, unsigned long raw_cs1) |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 104 | { |
Rod Whitby | af18374 | 2008-01-06 10:05:28 +0900 | [diff] [blame] | 105 | struct ata_ioports *ioaddr = &ap->ioaddr; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 106 | unsigned long raw_cmd = raw_cs0; |
| 107 | unsigned long raw_ctl = raw_cs1 + 0x06; |
| 108 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 109 | ioaddr->cmd_addr = data->cs0; |
| 110 | ioaddr->altstatus_addr = data->cs1 + 0x06; |
| 111 | ioaddr->ctl_addr = data->cs1 + 0x06; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 112 | |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 113 | ata_sff_std_ports(ioaddr); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 114 | |
| 115 | #ifndef __ARMEB__ |
| 116 | |
| 117 | /* adjust the addresses to handle the address swizzling of the |
| 118 | * ixp4xx in little endian mode. |
| 119 | */ |
| 120 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 121 | *(unsigned long *)&ioaddr->data_addr ^= 0x02; |
| 122 | *(unsigned long *)&ioaddr->cmd_addr ^= 0x03; |
| 123 | *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03; |
| 124 | *(unsigned long *)&ioaddr->ctl_addr ^= 0x03; |
| 125 | *(unsigned long *)&ioaddr->error_addr ^= 0x03; |
| 126 | *(unsigned long *)&ioaddr->feature_addr ^= 0x03; |
| 127 | *(unsigned long *)&ioaddr->nsect_addr ^= 0x03; |
| 128 | *(unsigned long *)&ioaddr->lbal_addr ^= 0x03; |
| 129 | *(unsigned long *)&ioaddr->lbam_addr ^= 0x03; |
| 130 | *(unsigned long *)&ioaddr->lbah_addr ^= 0x03; |
| 131 | *(unsigned long *)&ioaddr->device_addr ^= 0x03; |
| 132 | *(unsigned long *)&ioaddr->status_addr ^= 0x03; |
| 133 | *(unsigned long *)&ioaddr->command_addr ^= 0x03; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 134 | |
| 135 | raw_cmd ^= 0x03; |
| 136 | raw_ctl ^= 0x03; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 137 | #endif |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 138 | |
| 139 | ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | static __devinit int ixp4xx_pata_probe(struct platform_device *pdev) |
| 143 | { |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 144 | unsigned int irq; |
| 145 | struct resource *cs0, *cs1; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 146 | struct ata_host *host; |
| 147 | struct ata_port *ap; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 148 | struct ixp4xx_pata_data *data = pdev->dev.platform_data; |
| 149 | |
| 150 | cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 151 | cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 152 | |
| 153 | if (!cs0 || !cs1) |
| 154 | return -EINVAL; |
| 155 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 156 | /* allocate host */ |
| 157 | host = ata_host_alloc(&pdev->dev, 1); |
| 158 | if (!host) |
| 159 | return -ENOMEM; |
| 160 | |
| 161 | /* acquire resources and fill host */ |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 162 | pdev->dev.coherent_dma_mask = DMA_32BIT_MASK; |
| 163 | |
Tejun Heo | 24dc5f3 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 164 | data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000); |
| 165 | data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 166 | |
Scott Thompson | 991bf52 | 2007-10-02 13:53:01 -0700 | [diff] [blame] | 167 | if (!data->cs0 || !data->cs1) |
| 168 | return -ENOMEM; |
| 169 | |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 170 | irq = platform_get_irq(pdev, 0); |
| 171 | if (irq) |
Alessandro Zummo | 282c6b9 | 2007-03-18 15:23:33 +0100 | [diff] [blame] | 172 | set_irq_type(irq, IRQT_RISING); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 173 | |
| 174 | /* Setup expansion bus chip selects */ |
| 175 | *data->cs0_cfg = data->cs0_bits; |
| 176 | *data->cs1_cfg = data->cs1_bits; |
| 177 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 178 | ap = host->ports[0]; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 179 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 180 | ap->ops = &ixp4xx_port_ops; |
| 181 | ap->pio_mask = 0x1f; /* PIO4 */ |
| 182 | ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 183 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 184 | ixp4xx_setup_port(ap, data, cs0->start, cs1->start); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 185 | |
| 186 | dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n"); |
| 187 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 188 | /* activate host */ |
Tejun Heo | 9363c38 | 2008-04-07 22:47:16 +0900 | [diff] [blame] | 189 | return ata_host_activate(host, irq, ata_sff_interrupt, 0, &ixp4xx_sht); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | static __devexit int ixp4xx_pata_remove(struct platform_device *dev) |
| 193 | { |
| 194 | struct ata_host *host = platform_get_drvdata(dev); |
| 195 | |
Tejun Heo | 24dc5f3 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 196 | ata_host_detach(host); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 197 | |
| 198 | return 0; |
| 199 | } |
| 200 | |
| 201 | static struct platform_driver ixp4xx_pata_platform_driver = { |
| 202 | .driver = { |
| 203 | .name = DRV_NAME, |
| 204 | .owner = THIS_MODULE, |
| 205 | }, |
| 206 | .probe = ixp4xx_pata_probe, |
| 207 | .remove = __devexit_p(ixp4xx_pata_remove), |
| 208 | }; |
| 209 | |
| 210 | static int __init ixp4xx_pata_init(void) |
| 211 | { |
| 212 | return platform_driver_register(&ixp4xx_pata_platform_driver); |
| 213 | } |
| 214 | |
| 215 | static void __exit ixp4xx_pata_exit(void) |
| 216 | { |
| 217 | platform_driver_unregister(&ixp4xx_pata_platform_driver); |
| 218 | } |
| 219 | |
| 220 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 221 | MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA"); |
| 222 | MODULE_LICENSE("GPL"); |
| 223 | MODULE_VERSION(DRV_VERSION); |
Kay Sievers | 458622f | 2008-04-18 13:41:57 -0700 | [diff] [blame] | 224 | MODULE_ALIAS("platform:" DRV_NAME); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 225 | |
| 226 | module_init(ixp4xx_pata_init); |
| 227 | module_exit(ixp4xx_pata_exit); |