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 | |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 45 | static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf, |
| 46 | unsigned int buflen, int write_data) |
| 47 | { |
| 48 | unsigned int i; |
| 49 | unsigned int words = buflen >> 1; |
| 50 | u16 *buf16 = (u16 *) buf; |
Tejun Heo | 9af5c9c | 2007-08-06 18:36:22 +0900 | [diff] [blame] | 51 | struct ata_port *ap = adev->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 */ |
| 62 | if (write_data) { |
| 63 | for (i = 0; i < words; i++) |
| 64 | writew(buf16[i], mmio); |
| 65 | } else { |
| 66 | for (i = 0; i < words; i++) |
| 67 | buf16[i] = readw(mmio); |
| 68 | } |
| 69 | |
| 70 | /* Transfer trailing 1 byte, if any. */ |
| 71 | if (unlikely(buflen & 0x01)) { |
| 72 | u16 align_buf[1] = { 0 }; |
| 73 | unsigned char *trailing_buf = buf + buflen - 1; |
| 74 | |
| 75 | if (write_data) { |
| 76 | memcpy(align_buf, trailing_buf, 1); |
| 77 | writew(align_buf[0], mmio); |
| 78 | } else { |
| 79 | align_buf[0] = readw(mmio); |
| 80 | memcpy(trailing_buf, align_buf, 1); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | udelay(100); |
| 85 | *data->cs0_cfg |= 0x01; |
| 86 | } |
| 87 | |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 88 | static struct scsi_host_template ixp4xx_sht = { |
| 89 | .module = THIS_MODULE, |
| 90 | .name = DRV_NAME, |
| 91 | .ioctl = ata_scsi_ioctl, |
| 92 | .queuecommand = ata_scsi_queuecmd, |
| 93 | .can_queue = ATA_DEF_QUEUE, |
| 94 | .this_id = ATA_SHT_THIS_ID, |
| 95 | .sg_tablesize = LIBATA_MAX_PRD, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 96 | .cmd_per_lun = ATA_SHT_CMD_PER_LUN, |
| 97 | .emulated = ATA_SHT_EMULATED, |
| 98 | .use_clustering = ATA_SHT_USE_CLUSTERING, |
| 99 | .proc_name = DRV_NAME, |
| 100 | .dma_boundary = ATA_DMA_BOUNDARY, |
| 101 | .slave_configure = ata_scsi_slave_config, |
Tejun Heo | c972b60 | 2006-11-29 12:10:46 +0900 | [diff] [blame] | 102 | .slave_destroy = ata_scsi_slave_destroy, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 103 | .bios_param = ata_std_bios_param, |
| 104 | }; |
| 105 | |
| 106 | static struct ata_port_operations ixp4xx_port_ops = { |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 107 | .set_mode = ixp4xx_set_mode, |
| 108 | .mode_filter = ata_pci_default_filter, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 109 | |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 110 | .tf_load = ata_tf_load, |
| 111 | .tf_read = ata_tf_read, |
| 112 | .exec_command = ata_exec_command, |
| 113 | .check_status = ata_check_status, |
| 114 | .dev_select = ata_std_dev_select, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 115 | |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 116 | .freeze = ata_bmdma_freeze, |
| 117 | .thaw = ata_bmdma_thaw, |
| 118 | .error_handler = ata_bmdma_error_handler, |
| 119 | .post_internal_cmd = ata_bmdma_post_internal_cmd, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 120 | |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 121 | .qc_prep = ata_qc_prep, |
| 122 | .qc_issue = ata_qc_issue_prot, |
| 123 | .data_xfer = ixp4xx_mmio_data_xfer, |
| 124 | .cable_detect = ata_cable_40wire, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 125 | |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 126 | .irq_handler = ata_interrupt, |
| 127 | .irq_clear = ata_bmdma_irq_clear, |
| 128 | .irq_on = ata_irq_on, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 129 | |
Alessandro Zummo | 5d4c51f | 2007-05-26 19:26:55 -0400 | [diff] [blame] | 130 | .port_start = ata_port_start, |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 131 | }; |
| 132 | |
| 133 | static void ixp4xx_setup_port(struct ata_ioports *ioaddr, |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 134 | struct ixp4xx_pata_data *data, |
| 135 | unsigned long raw_cs0, unsigned long raw_cs1) |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 136 | { |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 137 | unsigned long raw_cmd = raw_cs0; |
| 138 | unsigned long raw_ctl = raw_cs1 + 0x06; |
| 139 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 140 | ioaddr->cmd_addr = data->cs0; |
| 141 | ioaddr->altstatus_addr = data->cs1 + 0x06; |
| 142 | ioaddr->ctl_addr = data->cs1 + 0x06; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 143 | |
| 144 | ata_std_ports(ioaddr); |
| 145 | |
| 146 | #ifndef __ARMEB__ |
| 147 | |
| 148 | /* adjust the addresses to handle the address swizzling of the |
| 149 | * ixp4xx in little endian mode. |
| 150 | */ |
| 151 | |
Tejun Heo | 0d5ff56 | 2007-02-01 15:06:36 +0900 | [diff] [blame] | 152 | *(unsigned long *)&ioaddr->data_addr ^= 0x02; |
| 153 | *(unsigned long *)&ioaddr->cmd_addr ^= 0x03; |
| 154 | *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03; |
| 155 | *(unsigned long *)&ioaddr->ctl_addr ^= 0x03; |
| 156 | *(unsigned long *)&ioaddr->error_addr ^= 0x03; |
| 157 | *(unsigned long *)&ioaddr->feature_addr ^= 0x03; |
| 158 | *(unsigned long *)&ioaddr->nsect_addr ^= 0x03; |
| 159 | *(unsigned long *)&ioaddr->lbal_addr ^= 0x03; |
| 160 | *(unsigned long *)&ioaddr->lbam_addr ^= 0x03; |
| 161 | *(unsigned long *)&ioaddr->lbah_addr ^= 0x03; |
| 162 | *(unsigned long *)&ioaddr->device_addr ^= 0x03; |
| 163 | *(unsigned long *)&ioaddr->status_addr ^= 0x03; |
| 164 | *(unsigned long *)&ioaddr->command_addr ^= 0x03; |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 165 | |
| 166 | raw_cmd ^= 0x03; |
| 167 | raw_ctl ^= 0x03; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 168 | #endif |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 169 | |
| 170 | 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] | 171 | } |
| 172 | |
| 173 | static __devinit int ixp4xx_pata_probe(struct platform_device *pdev) |
| 174 | { |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 175 | unsigned int irq; |
| 176 | struct resource *cs0, *cs1; |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 177 | struct ata_host *host; |
| 178 | struct ata_port *ap; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 179 | struct ixp4xx_pata_data *data = pdev->dev.platform_data; |
| 180 | |
| 181 | cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
| 182 | cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1); |
| 183 | |
| 184 | if (!cs0 || !cs1) |
| 185 | return -EINVAL; |
| 186 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 187 | /* allocate host */ |
| 188 | host = ata_host_alloc(&pdev->dev, 1); |
| 189 | if (!host) |
| 190 | return -ENOMEM; |
| 191 | |
| 192 | /* acquire resources and fill host */ |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 193 | pdev->dev.coherent_dma_mask = DMA_32BIT_MASK; |
| 194 | |
Tejun Heo | 24dc5f3 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 195 | data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000); |
| 196 | data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 197 | |
Scott Thompson | 991bf52 | 2007-10-02 13:53:01 -0700 | [diff] [blame] | 198 | if (!data->cs0 || !data->cs1) |
| 199 | return -ENOMEM; |
| 200 | |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 201 | irq = platform_get_irq(pdev, 0); |
| 202 | if (irq) |
Alessandro Zummo | 282c6b9 | 2007-03-18 15:23:33 +0100 | [diff] [blame] | 203 | set_irq_type(irq, IRQT_RISING); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 204 | |
| 205 | /* Setup expansion bus chip selects */ |
| 206 | *data->cs0_cfg = data->cs0_bits; |
| 207 | *data->cs1_cfg = data->cs1_bits; |
| 208 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 209 | ap = host->ports[0]; |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 210 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 211 | ap->ops = &ixp4xx_port_ops; |
| 212 | ap->pio_mask = 0x1f; /* PIO4 */ |
| 213 | 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] | 214 | |
Tejun Heo | cbcdd87 | 2007-08-18 13:14:55 +0900 | [diff] [blame] | 215 | ixp4xx_setup_port(ap, data, cs0->start, cs1->start); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 216 | |
| 217 | dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n"); |
| 218 | |
Tejun Heo | 5d72882 | 2007-04-17 23:44:08 +0900 | [diff] [blame] | 219 | /* activate host */ |
| 220 | return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | static __devexit int ixp4xx_pata_remove(struct platform_device *dev) |
| 224 | { |
| 225 | struct ata_host *host = platform_get_drvdata(dev); |
| 226 | |
Tejun Heo | 24dc5f3 | 2007-01-20 16:00:28 +0900 | [diff] [blame] | 227 | ata_host_detach(host); |
Alessandro Zummo | 0df0d0a | 2006-11-14 13:43:21 -0500 | [diff] [blame] | 228 | |
| 229 | return 0; |
| 230 | } |
| 231 | |
| 232 | static struct platform_driver ixp4xx_pata_platform_driver = { |
| 233 | .driver = { |
| 234 | .name = DRV_NAME, |
| 235 | .owner = THIS_MODULE, |
| 236 | }, |
| 237 | .probe = ixp4xx_pata_probe, |
| 238 | .remove = __devexit_p(ixp4xx_pata_remove), |
| 239 | }; |
| 240 | |
| 241 | static int __init ixp4xx_pata_init(void) |
| 242 | { |
| 243 | return platform_driver_register(&ixp4xx_pata_platform_driver); |
| 244 | } |
| 245 | |
| 246 | static void __exit ixp4xx_pata_exit(void) |
| 247 | { |
| 248 | platform_driver_unregister(&ixp4xx_pata_platform_driver); |
| 249 | } |
| 250 | |
| 251 | MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>"); |
| 252 | MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA"); |
| 253 | MODULE_LICENSE("GPL"); |
| 254 | MODULE_VERSION(DRV_VERSION); |
| 255 | |
| 256 | module_init(ixp4xx_pata_init); |
| 257 | module_exit(ixp4xx_pata_exit); |