Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/jikos/hid
[sfrench/cifs-2.6.git] / drivers / ata / pata_ixp4xx_cf.c
1 /*
2  * ixp4xx PATA/Compact Flash driver
3  * Copyright (c) 2006 Tower Technologies
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
9  * on the ixp4xx. The interrupt line is optional, if not
10  * specified the driver will run in polling mode.
11  *
12  * This program is free software; you can redistribute it and/or modify
13  * it under the terms of the GNU General Public License version 2 as
14  * published by the Free Software Foundation.
15  *
16  */
17
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/libata.h>
21 #include <linux/irq.h>
22 #include <linux/platform_device.h>
23 #include <scsi/scsi_host.h>
24
25 #define DRV_NAME        "pata_ixp4xx_cf"
26 #define DRV_VERSION     "0.1.2"
27
28 static int ixp4xx_set_mode(struct ata_port *ap, struct ata_device **error)
29 {
30         int i;
31
32         for (i = 0; i < ATA_MAX_DEVICES; i++) {
33                 struct ata_device *dev = &ap->device[i];
34                 if (ata_dev_ready(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;
40                 }
41         }
42         return 0;
43 }
44
45 static void ixp4xx_phy_reset(struct ata_port *ap)
46 {
47         ap->cbl = ATA_CBL_PATA40;
48         ata_port_probe(ap);
49         ata_bus_reset(ap);
50 }
51
52 static void ixp4xx_mmio_data_xfer(struct ata_device *adev, unsigned char *buf,
53                                 unsigned int buflen, int write_data)
54 {
55         unsigned int i;
56         unsigned int words = buflen >> 1;
57         u16 *buf16 = (u16 *) buf;
58         struct ata_port *ap = adev->ap;
59         void __iomem *mmio = (void __iomem *)ap->ioaddr.data_addr;
60         struct ixp4xx_pata_data *data = ap->host->dev->platform_data;
61
62         /* set the expansion bus in 16bit mode and restore
63          * 8 bit mode after the transaction.
64          */
65         *data->cs0_cfg &= ~(0x01);
66         udelay(100);
67
68         /* Transfer multiple of 2 bytes */
69         if (write_data) {
70                 for (i = 0; i < words; i++)
71                         writew(buf16[i], mmio);
72         } else {
73                 for (i = 0; i < words; i++)
74                         buf16[i] = readw(mmio);
75         }
76
77         /* Transfer trailing 1 byte, if any. */
78         if (unlikely(buflen & 0x01)) {
79                 u16 align_buf[1] = { 0 };
80                 unsigned char *trailing_buf = buf + buflen - 1;
81
82                 if (write_data) {
83                         memcpy(align_buf, trailing_buf, 1);
84                         writew(align_buf[0], mmio);
85                 } else {
86                         align_buf[0] = readw(mmio);
87                         memcpy(trailing_buf, align_buf, 1);
88                 }
89         }
90
91         udelay(100);
92         *data->cs0_cfg |= 0x01;
93 }
94
95 static void ixp4xx_irq_clear(struct ata_port *ap)
96 {
97 }
98
99 static struct scsi_host_template ixp4xx_sht = {
100         .module                 = THIS_MODULE,
101         .name                   = DRV_NAME,
102         .ioctl                  = ata_scsi_ioctl,
103         .queuecommand           = ata_scsi_queuecmd,
104         .can_queue              = ATA_DEF_QUEUE,
105         .this_id                = ATA_SHT_THIS_ID,
106         .sg_tablesize           = LIBATA_MAX_PRD,
107         .cmd_per_lun            = ATA_SHT_CMD_PER_LUN,
108         .emulated               = ATA_SHT_EMULATED,
109         .use_clustering         = ATA_SHT_USE_CLUSTERING,
110         .proc_name              = DRV_NAME,
111         .dma_boundary           = ATA_DMA_BOUNDARY,
112         .slave_configure        = ata_scsi_slave_config,
113         .slave_destroy          = ata_scsi_slave_destroy,
114         .bios_param             = ata_std_bios_param,
115 };
116
117 static struct ata_port_operations ixp4xx_port_ops = {
118         .set_mode       = ixp4xx_set_mode,
119         .mode_filter    = ata_pci_default_filter,
120
121         .port_disable   = ata_port_disable,
122         .tf_load        = ata_tf_load,
123         .tf_read        = ata_tf_read,
124         .check_status   = ata_check_status,
125         .exec_command   = ata_exec_command,
126         .dev_select     = ata_std_dev_select,
127
128         .qc_prep        = ata_qc_prep,
129         .qc_issue       = ata_qc_issue_prot,
130         .eng_timeout    = ata_eng_timeout,
131         .data_xfer      = ixp4xx_mmio_data_xfer,
132         .cable_detect   = ata_cable_40wire,
133
134         .irq_clear      = ixp4xx_irq_clear,
135         .irq_on         = ata_irq_on,
136         .irq_ack        = ata_irq_ack,
137
138         .port_start     = ata_port_start,
139
140         .phy_reset      = ixp4xx_phy_reset,
141 };
142
143 static void ixp4xx_setup_port(struct ata_ioports *ioaddr,
144                                 struct ixp4xx_pata_data *data)
145 {
146         ioaddr->cmd_addr        = data->cs0;
147         ioaddr->altstatus_addr  = data->cs1 + 0x06;
148         ioaddr->ctl_addr        = data->cs1 + 0x06;
149
150         ata_std_ports(ioaddr);
151
152 #ifndef __ARMEB__
153
154         /* adjust the addresses to handle the address swizzling of the
155          * ixp4xx in little endian mode.
156          */
157
158         *(unsigned long *)&ioaddr->data_addr            ^= 0x02;
159         *(unsigned long *)&ioaddr->cmd_addr             ^= 0x03;
160         *(unsigned long *)&ioaddr->altstatus_addr       ^= 0x03;
161         *(unsigned long *)&ioaddr->ctl_addr             ^= 0x03;
162         *(unsigned long *)&ioaddr->error_addr           ^= 0x03;
163         *(unsigned long *)&ioaddr->feature_addr         ^= 0x03;
164         *(unsigned long *)&ioaddr->nsect_addr           ^= 0x03;
165         *(unsigned long *)&ioaddr->lbal_addr            ^= 0x03;
166         *(unsigned long *)&ioaddr->lbam_addr            ^= 0x03;
167         *(unsigned long *)&ioaddr->lbah_addr            ^= 0x03;
168         *(unsigned long *)&ioaddr->device_addr          ^= 0x03;
169         *(unsigned long *)&ioaddr->status_addr          ^= 0x03;
170         *(unsigned long *)&ioaddr->command_addr         ^= 0x03;
171 #endif
172 }
173
174 static __devinit int ixp4xx_pata_probe(struct platform_device *pdev)
175 {
176         unsigned int irq;
177         struct resource *cs0, *cs1;
178         struct ata_host *host;
179         struct ata_port *ap;
180         struct ixp4xx_pata_data *data = pdev->dev.platform_data;
181         int rc;
182
183         cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0);
184         cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1);
185
186         if (!cs0 || !cs1)
187                 return -EINVAL;
188
189         /* allocate host */
190         host = ata_host_alloc(&pdev->dev, 1);
191         if (!host)
192                 return -ENOMEM;
193
194         /* acquire resources and fill host */
195         pdev->dev.coherent_dma_mask = DMA_32BIT_MASK;
196
197         data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000);
198         data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000);
199
200         irq = platform_get_irq(pdev, 0);
201         if (irq)
202                 set_irq_type(irq, IRQT_RISING);
203
204         /* Setup expansion bus chip selects */
205         *data->cs0_cfg = data->cs0_bits;
206         *data->cs1_cfg = data->cs1_bits;
207
208         ap = host->ports[0];
209
210         ap->ops = &ixp4xx_port_ops;
211         ap->pio_mask = 0x1f; /* PIO4 */
212         ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI;
213
214         /* run in polling mode if no irq has been assigned */
215         if (!irq)
216                 ap->flags |= ATA_FLAG_PIO_POLLING;
217
218         ixp4xx_setup_port(&ap->ioaddr, data);
219
220         dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
221
222         /* activate host */
223         return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht);
224 }
225
226 static __devexit int ixp4xx_pata_remove(struct platform_device *dev)
227 {
228         struct ata_host *host = platform_get_drvdata(dev);
229
230         ata_host_detach(host);
231
232         return 0;
233 }
234
235 static struct platform_driver ixp4xx_pata_platform_driver = {
236         .driver  = {
237                 .name   = DRV_NAME,
238                 .owner  = THIS_MODULE,
239         },
240         .probe          = ixp4xx_pata_probe,
241         .remove         = __devexit_p(ixp4xx_pata_remove),
242 };
243
244 static int __init ixp4xx_pata_init(void)
245 {
246         return platform_driver_register(&ixp4xx_pata_platform_driver);
247 }
248
249 static void __exit ixp4xx_pata_exit(void)
250 {
251         platform_driver_unregister(&ixp4xx_pata_platform_driver);
252 }
253
254 MODULE_AUTHOR("Alessandro Zummo <a.zummo@towertech.it>");
255 MODULE_DESCRIPTION("low-level driver for ixp4xx Compact Flash PATA");
256 MODULE_LICENSE("GPL");
257 MODULE_VERSION(DRV_VERSION);
258
259 module_init(ixp4xx_pata_init);
260 module_exit(ixp4xx_pata_exit);