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