blob: 7db315a9baaddfe7edceb03e9c65bb14255c4bf8 [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
Alan3ddcc592007-02-20 17:45:55 +000029static int ixp4xx_set_mode(struct ata_port *ap, 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 Heof58229f2007-08-06 18:36:23 +090033 ata_link_for_each_dev(dev, &ap->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
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -050045static 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 Heo9af5c9c2007-08-06 18:36:22 +090051 struct ata_port *ap = adev->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 */
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 Zummo0df0d0a2006-11-14 13:43:21 -050088static 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 Zummo0df0d0a2006-11-14 13:43:21 -050096 .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 Heoc972b602006-11-29 12:10:46 +0900102 .slave_destroy = ata_scsi_slave_destroy,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500103 .bios_param = ata_std_bios_param,
104};
105
106static struct ata_port_operations ixp4xx_port_ops = {
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400107 .set_mode = ixp4xx_set_mode,
108 .mode_filter = ata_pci_default_filter,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500109
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400110 .port_disable = ata_port_disable,
111 .tf_load = ata_tf_load,
112 .tf_read = ata_tf_read,
113 .exec_command = ata_exec_command,
114 .check_status = ata_check_status,
115 .dev_select = ata_std_dev_select,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500116
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400117 .freeze = ata_bmdma_freeze,
118 .thaw = ata_bmdma_thaw,
119 .error_handler = ata_bmdma_error_handler,
120 .post_internal_cmd = ata_bmdma_post_internal_cmd,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500121
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400122 .qc_prep = ata_qc_prep,
123 .qc_issue = ata_qc_issue_prot,
124 .data_xfer = ixp4xx_mmio_data_xfer,
125 .cable_detect = ata_cable_40wire,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500126
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400127 .irq_handler = ata_interrupt,
128 .irq_clear = ata_bmdma_irq_clear,
129 .irq_on = ata_irq_on,
130 .irq_ack = ata_dummy_irq_ack,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500131
Alessandro Zummo5d4c51f2007-05-26 19:26:55 -0400132 .port_start = ata_port_start,
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500133};
134
135static void ixp4xx_setup_port(struct ata_ioports *ioaddr,
136 struct ixp4xx_pata_data *data)
137{
Tejun Heo0d5ff562007-02-01 15:06:36 +0900138 ioaddr->cmd_addr = data->cs0;
139 ioaddr->altstatus_addr = data->cs1 + 0x06;
140 ioaddr->ctl_addr = data->cs1 + 0x06;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500141
142 ata_std_ports(ioaddr);
143
144#ifndef __ARMEB__
145
146 /* adjust the addresses to handle the address swizzling of the
147 * ixp4xx in little endian mode.
148 */
149
Tejun Heo0d5ff562007-02-01 15:06:36 +0900150 *(unsigned long *)&ioaddr->data_addr ^= 0x02;
151 *(unsigned long *)&ioaddr->cmd_addr ^= 0x03;
152 *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03;
153 *(unsigned long *)&ioaddr->ctl_addr ^= 0x03;
154 *(unsigned long *)&ioaddr->error_addr ^= 0x03;
155 *(unsigned long *)&ioaddr->feature_addr ^= 0x03;
156 *(unsigned long *)&ioaddr->nsect_addr ^= 0x03;
157 *(unsigned long *)&ioaddr->lbal_addr ^= 0x03;
158 *(unsigned long *)&ioaddr->lbam_addr ^= 0x03;
159 *(unsigned long *)&ioaddr->lbah_addr ^= 0x03;
160 *(unsigned long *)&ioaddr->device_addr ^= 0x03;
161 *(unsigned long *)&ioaddr->status_addr ^= 0x03;
162 *(unsigned long *)&ioaddr->command_addr ^= 0x03;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500163#endif
164}
165
166static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
167{
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500168 unsigned int irq;
169 struct resource *cs0, *cs1;
Tejun Heo5d728822007-04-17 23:44:08 +0900170 struct ata_host *host;
171 struct ata_port *ap;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500172 struct ixp4xx_pata_data *data = pdev->dev.platform_data;
173
174 cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
175 cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
176
177 if (!cs0 || !cs1)
178 return -EINVAL;
179
Tejun Heo5d728822007-04-17 23:44:08 +0900180 /* allocate host */
181 host = ata_host_alloc(&pdev->dev, 1);
182 if (!host)
183 return -ENOMEM;
184
185 /* acquire resources and fill host */
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500186 pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
187
Tejun Heo24dc5f32007-01-20 16:00:28 +0900188 data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
189 data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500190
Scott Thompson991bf522007-10-02 13:53:01 -0700191 if (!data->cs0 || !data->cs1)
192 return -ENOMEM;
193
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500194 irq = platform_get_irq(pdev, 0);
195 if (irq)
Alessandro Zummo282c6b92007-03-18 15:23:33 +0100196 set_irq_type(irq, IRQT_RISING);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500197
198 /* Setup expansion bus chip selects */
199 *data->cs0_cfg = data->cs0_bits;
200 *data->cs1_cfg = data->cs1_bits;
201
Tejun Heo5d728822007-04-17 23:44:08 +0900202 ap = host->ports[0];
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500203
Tejun Heo5d728822007-04-17 23:44:08 +0900204 ap->ops = &ixp4xx_port_ops;
205 ap->pio_mask = 0x1f; /* PIO4 */
206 ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500207
Tejun Heo5d728822007-04-17 23:44:08 +0900208 ixp4xx_setup_port(&ap->ioaddr, data);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500209
210 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
211
Tejun Heo5d728822007-04-17 23:44:08 +0900212 /* activate host */
213 return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500214}
215
216static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
217{
218 struct ata_host *host = platform_get_drvdata(dev);
219
Tejun Heo24dc5f32007-01-20 16:00:28 +0900220 ata_host_detach(host);
Alessandro Zummo0df0d0a2006-11-14 13:43:21 -0500221
222 return 0;
223}
224
225static struct platform_driver ixp4xx_pata_platform_driver = {
226 .driver = {
227 .name = DRV_NAME,
228 .owner = THIS_MODULE,
229 },
230 .probe = ixp4xx_pata_probe,
231 .remove = __devexit_p(ixp4xx_pata_remove),
232};
233
234static int __init ixp4xx_pata_init(void)
235{
236 return platform_driver_register(&ixp4xx_pata_platform_driver);
237}
238
239static void __exit ixp4xx_pata_exit(void)
240{
241 platform_driver_unregister(&ixp4xx_pata_platform_driver);
242}
243
244MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
245MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
246MODULE_LICENSE("GPL");
247MODULE_VERSION(DRV_VERSION);
248
249module_init(ixp4xx_pata_init);
250module_exit(ixp4xx_pata_exit);