blob: b7e8e825a8691c4fc8e735adee9d5849cb291d06 [file] [log] [blame]
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -05001/*
2 * ixp4xx PATA/Compact Flash driver
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -04003 * Copyright (C) 2006-07 Tower Technologies
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -05004 * 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 Zummo5d4c51f2007-05-26 19:26:55 -04009 * 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 Zummo0df0d0a2006-11-14 13:43:21 -050012 *
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 Zummo5d4c51f2007-05-26 19:26:55 -040027#define DRV_VERSION "0.2"
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050028
Tejun Heo02607312007-08-06 18:36:23 +090029static int ixp4xx_set_mode(struct ata_link *link, struct ata_device **error)
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050030{
Tejun Heof58229f2007-08-06 18:36:23 +090031 struct ata_device *dev;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050032
Tejun Heo02607312007-08-06 18:36:23 +090033 ata_link_for_each_dev(dev, link) {
Tejun Heo9666f402007-05-04 21:27:47 +020034 if (ata_dev_enabled(dev)) {
Alan3ddcc592007-02-20 17:45:55 +000035 ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n");
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050036 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 }
Alanb229a7b2007-01-24 11:47:07 +000042 return 0;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050043}
44
Tejun Heo55dba312007-12-05 16:43:07 +090045static unsigned int ixp4xx_mmio_data_xfer(struct ata_device *dev,
46 unsigned char *buf, unsigned int buflen, int rw)
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050047{
48 unsigned int i;
49 unsigned int words = buflen >> 1;
50 u16 *buf16 = (u16 *) buf;
Tejun Heo55dba312007-12-05 16:43:07 +090051 struct ata_port *ap = dev->link->ap;
Jeff Garzik59f99882007-05-28 07:07:20 -040052 void __iomem *mmio = ap->ioaddr.data_addr;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050053 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 Heo55dba312007-12-05 16:43:07 +090062 if (rw == READ)
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050063 for (i = 0; i < words; i++)
64 buf16[i] = readw(mmio);
Tejun Heo55dba312007-12-05 16:43:07 +090065 else
66 for (i = 0; i < words; i++)
67 writew(buf16[i], mmio);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050068
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 Heo55dba312007-12-05 16:43:07 +090074 if (rw == READ) {
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050075 align_buf[0] = readw(mmio);
76 memcpy(trailing_buf, align_buf, 1);
Tejun Heo55dba312007-12-05 16:43:07 +090077 } else {
78 memcpy(align_buf, trailing_buf, 1);
79 writew(align_buf[0], mmio);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050080 }
Tejun Heo55dba312007-12-05 16:43:07 +090081 words++;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050082 }
83
84 udelay(100);
85 *data->cs0_cfg |= 0x01;
Tejun Heo55dba312007-12-05 16:43:07 +090086
87 return words << 1;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050088}
89
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050090static struct scsi_host_template ixp4xx_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +090091 ATA_PIO_SHT(DRV_NAME),
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050092};
93
94static struct ata_port_operations ixp4xx_port_ops = {
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -040095 .set_mode = ixp4xx_set_mode,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050096
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -040097 .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,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500102
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400103 .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,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500107
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400108 .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,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500112
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400113 .irq_handler = ata_interrupt,
Tejun Heo358f9a72008-03-25 12:22:47 +0900114 .irq_clear = ata_noop_irq_clear,
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400115 .irq_on = ata_irq_on,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500116
Tejun Heo6bd99b42008-03-25 12:22:48 +0900117 .port_start = ata_sff_port_start,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500118};
119
Rod Whitbyaf183742008-01-06 10:05:28 +0900120static void ixp4xx_setup_port(struct ata_port *ap,
Tejun Heocbcdd872007-08-18 13:14:55 +0900121 struct ixp4xx_pata_data *data,
122 unsigned long raw_cs0, unsigned long raw_cs1)
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500123{
Rod Whitbyaf183742008-01-06 10:05:28 +0900124 struct ata_ioports *ioaddr = &ap->ioaddr;
Tejun Heocbcdd872007-08-18 13:14:55 +0900125 unsigned long raw_cmd = raw_cs0;
126 unsigned long raw_ctl = raw_cs1 + 0x06;
127
Tejun Heo0d5ff562007-02-01 15:06:36 +0900128 ioaddr->cmd_addr = data->cs0;
129 ioaddr->altstatus_addr = data->cs1 + 0x06;
130 ioaddr->ctl_addr = data->cs1 + 0x06;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500131
132 ata_std_ports(ioaddr);
133
134#ifndef __ARMEB__
135
136 /* adjust the addresses to handle the address swizzling of the
137 * ixp4xx in little endian mode.
138 */
139
Tejun Heo0d5ff562007-02-01 15:06:36 +0900140 *(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;
Tejun Heocbcdd872007-08-18 13:14:55 +0900153
154 raw_cmd ^= 0x03;
155 raw_ctl ^= 0x03;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500156#endif
Tejun Heocbcdd872007-08-18 13:14:55 +0900157
158 ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500159}
160
161static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
162{
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500163 unsigned int irq;
164 struct resource *cs0, *cs1;
Tejun Heo5d7288242007-04-17 23:44:08 +0900165 struct ata_host *host;
166 struct ata_port *ap;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500167 struct ixp4xx_pata_data *data = pdev->dev.platform_data;
168
169 cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
170 cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
171
172 if (!cs0 || !cs1)
173 return -EINVAL;
174
Tejun Heo5d7288242007-04-17 23:44:08 +0900175 /* allocate host */
176 host = ata_host_alloc(&pdev->dev, 1);
177 if (!host)
178 return -ENOMEM;
179
180 /* acquire resources and fill host */
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500181 pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
182
Tejun Heo24dc5f32007-01-20 16:00:28 +0900183 data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
184 data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500185
Scott Thompson991bf522007-10-02 13:53:01 -0700186 if (!data->cs0 || !data->cs1)
187 return -ENOMEM;
188
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500189 irq = platform_get_irq(pdev, 0);
190 if (irq)
Alessandro Zummo282c6b92007-03-18 15:23:33 +0100191 set_irq_type(irq, IRQT_RISING);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500192
193 /* Setup expansion bus chip selects */
194 *data->cs0_cfg = data->cs0_bits;
195 *data->cs1_cfg = data->cs1_bits;
196
Tejun Heo5d7288242007-04-17 23:44:08 +0900197 ap = host->ports[0];
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500198
Tejun Heo5d7288242007-04-17 23:44:08 +0900199 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;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500202
Tejun Heocbcdd872007-08-18 13:14:55 +0900203 ixp4xx_setup_port(ap, data, cs0->start, cs1->start);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500204
205 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
206
Tejun Heo5d7288242007-04-17 23:44:08 +0900207 /* activate host */
208 return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500209}
210
211static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
212{
213 struct ata_host *host = platform_get_drvdata(dev);
214
Tejun Heo24dc5f32007-01-20 16:00:28 +0900215 ata_host_detach(host);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500216
217 return 0;
218}
219
220static struct platform_driver ixp4xx_pata_platform_driver = {
221 .driver = {
222 .name = DRV_NAME,
223 .owner = THIS_MODULE,
224 },
225 .probe = ixp4xx_pata_probe,
226 .remove = __devexit_p(ixp4xx_pata_remove),
227};
228
229static int __init ixp4xx_pata_init(void)
230{
231 return platform_driver_register(&ixp4xx_pata_platform_driver);
232}
233
234static void __exit ixp4xx_pata_exit(void)
235{
236 platform_driver_unregister(&ixp4xx_pata_platform_driver);
237}
238
239MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
240MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
241MODULE_LICENSE("GPL");
242MODULE_VERSION(DRV_VERSION);
243
244module_init(ixp4xx_pata_init);
245module_exit(ixp4xx_pata_exit);