2 * ixp4xx PATA/Compact Flash driver
3 * Copyright (C) 2006-07 Tower Technologies
4 * Author: Alessandro Zummo <a.zummo@towertech.it>
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
9 * on the ixp4xx. In the irq is not available, you might
10 * want to modify both this driver and libata to run in
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.
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>
26 #define DRV_NAME "pata_ixp4xx_cf"
27 #define DRV_VERSION "0.2"
29 static int ixp4xx_set_mode(struct ata_link *link, struct ata_device **error)
31 struct ata_device *dev;
33 ata_link_for_each_dev(dev, link) {
34 if (ata_dev_enabled(dev)) {
35 ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
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;
45 static unsigned int ixp4xx_mmio_data_xfer(struct ata_device *dev,
46 unsigned char *buf, unsigned int buflen, int rw)
49 unsigned int words = buflen >> 1;
50 u16 *buf16 = (u16 *) buf;
51 struct ata_port *ap = dev->link->ap;
52 void __iomem *mmio = ap->ioaddr.data_addr;
53 struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
55 /* set the expansion bus in 16bit mode and restore
56 * 8 bit mode after the transaction.
58 *data->cs0_cfg &= ~(0x01);
61 /* Transfer multiple of 2 bytes */
63 for (i = 0; i < words; i++)
64 buf16[i] = readw(mmio);
66 for (i = 0; i < words; i++)
67 writew(buf16[i], mmio);
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;
75 align_buf[0] = readw(mmio);
76 memcpy(trailing_buf, align_buf, 1);
78 memcpy(align_buf, trailing_buf, 1);
79 writew(align_buf[0], mmio);
85 *data->cs0_cfg |= 0x01;
90 static struct scsi_host_template ixp4xx_sht = {
91 ATA_PIO_SHT(DRV_NAME),
94 static struct ata_port_operations ixp4xx_port_ops = {
95 .set_mode = ixp4xx_set_mode,
97 .tf_load = ata_tf_load,
98 .tf_read = ata_tf_read,
99 .exec_command = ata_exec_command,
100 .check_status = ata_check_status,
101 .dev_select = ata_std_dev_select,
103 .freeze = ata_bmdma_freeze,
104 .thaw = ata_bmdma_thaw,
105 .error_handler = ata_bmdma_error_handler,
106 .post_internal_cmd = ata_bmdma_post_internal_cmd,
108 .qc_prep = ata_qc_prep,
109 .qc_issue = ata_qc_issue_prot,
110 .data_xfer = ixp4xx_mmio_data_xfer,
111 .cable_detect = ata_cable_40wire,
113 .irq_handler = ata_interrupt,
114 .irq_clear = ata_noop_irq_clear,
115 .irq_on = ata_irq_on,
117 .port_start = ata_sff_port_start,
120 static void ixp4xx_setup_port(struct ata_port *ap,
121 struct ixp4xx_pata_data *data,
122 unsigned long raw_cs0, unsigned long raw_cs1)
124 struct ata_ioports *ioaddr = &ap->ioaddr;
125 unsigned long raw_cmd = raw_cs0;
126 unsigned long raw_ctl = raw_cs1 + 0x06;
128 ioaddr->cmd_addr = data->cs0;
129 ioaddr->altstatus_addr = data->cs1 + 0x06;
130 ioaddr->ctl_addr = data->cs1 + 0x06;
132 ata_std_ports(ioaddr);
136 /* adjust the addresses to handle the address swizzling of the
137 * ixp4xx in little endian mode.
140 *(unsigned long *)&ioaddr->data_addr ^= 0x02;
141 *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
142 *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
143 *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
144 *(unsigned long *)&ioaddr->error_addr ^= 0x03;
145 *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
146 *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
147 *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
148 *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
149 *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
150 *(unsigned long *)&ioaddr->device_addr ^= 0x03;
151 *(unsigned long *)&ioaddr->status_addr ^= 0x03;
152 *(unsigned long *)&ioaddr->command_addr ^= 0x03;
158 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl);
161 static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
164 struct resource *cs0, *cs1;
165 struct ata_host *host;
167 struct ixp4xx_pata_data *data = pdev->dev.platform_data;
169 cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
176 host = ata_host_alloc(&pdev->dev, 1);
180 /* acquire resources and fill host */
181 pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
183 data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
184 data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
186 if (!data->cs0 || !data->cs1)
189 irq = platform_get_irq(pdev, 0);
191 set_irq_type(irq, IRQT_RISING);
193 /* Setup expansion bus chip selects */
194 *data->cs0_cfg = data->cs0_bits;
195 *data->cs1_cfg = data->cs1_bits;
199 ap->ops = &ixp4xx_port_ops;
200 ap->pio_mask = 0x1f; /* PIO4 */
201 ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
203 ixp4xx_setup_port(ap, data, cs0->start, cs1->start);
205 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
208 return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht);
211 static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
213 struct ata_host *host = platform_get_drvdata(dev);
215 ata_host_detach(host);
220 static struct platform_driver ixp4xx_pata_platform_driver = {
223 .owner = THIS_MODULE,
225 .probe = ixp4xx_pata_probe,
226 .remove = __devexit_p(ixp4xx_pata_remove),
229 static int __init ixp4xx_pata_init(void)
231 return platform_driver_register(&ixp4xx_pata_platform_driver);
234 static void __exit ixp4xx_pata_exit(void)
236 platform_driver_unregister(&ixp4xx_pata_platform_driver);
239 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
240 MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
241 MODULE_LICENSE("GPL");
242 MODULE_VERSION(DRV_VERSION);
244 module_init(ixp4xx_pata_init);
245 module_exit(ixp4xx_pata_exit);