Commit | Line | Data |
---|---|---|
0df0d0a0 AZ |
1 | /* |
2 | * ixp4xx PATA/Compact Flash driver | |
5d4c51f6 | 3 | * Copyright (C) 2006-07 Tower Technologies |
0df0d0a0 AZ |
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 | |
5d4c51f6 AZ |
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. | |
0df0d0a0 AZ |
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" | |
5d4c51f6 | 27 | #define DRV_VERSION "0.2" |
0df0d0a0 | 28 | |
0260731f | 29 | static int ixp4xx_set_mode(struct ata_link *link, struct ata_device **error) |
0df0d0a0 | 30 | { |
f58229f8 | 31 | struct ata_device *dev; |
0df0d0a0 | 32 | |
0260731f | 33 | ata_link_for_each_dev(dev, link) { |
9666f400 | 34 | if (ata_dev_enabled(dev)) { |
3ddcc591 | 35 | ata_dev_printk(dev, KERN_INFO, "configured for PIO0\n"); |
0df0d0a0 AZ |
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 | } | |
b229a7b0 | 42 | return 0; |
0df0d0a0 AZ |
43 | } |
44 | ||
55dba312 TH |
45 | static unsigned int ixp4xx_mmio_data_xfer(struct ata_device *dev, |
46 | unsigned char *buf, unsigned int buflen, int rw) | |
0df0d0a0 AZ |
47 | { |
48 | unsigned int i; | |
49 | unsigned int words = buflen >> 1; | |
50 | u16 *buf16 = (u16 *) buf; | |
55dba312 | 51 | struct ata_port *ap = dev->link->ap; |
59f99880 | 52 | void __iomem *mmio = ap->ioaddr.data_addr; |
0df0d0a0 AZ |
53 | 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 */ | |
55dba312 | 62 | if (rw == READ) |
0df0d0a0 AZ |
63 | for (i = 0; i < words; i++) |
64 | buf16[i] = readw(mmio); | |
55dba312 TH |
65 | else |
66 | for (i = 0; i < words; i++) | |
67 | writew(buf16[i], mmio); | |
0df0d0a0 AZ |
68 | |
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 | ||
55dba312 | 74 | if (rw == READ) { |
0df0d0a0 AZ |
75 | align_buf[0] = readw(mmio); |
76 | memcpy(trailing_buf, align_buf, 1); | |
55dba312 TH |
77 | } else { |
78 | memcpy(align_buf, trailing_buf, 1); | |
79 | writew(align_buf[0], mmio); | |
0df0d0a0 | 80 | } |
55dba312 | 81 | words++; |
0df0d0a0 AZ |
82 | } |
83 | ||
84 | udelay(100); | |
85 | *data->cs0_cfg |= 0x01; | |
55dba312 TH |
86 | |
87 | return words << 1; | |
0df0d0a0 AZ |
88 | } |
89 | ||
0df0d0a0 AZ |
90 | static struct scsi_host_template ixp4xx_sht = { |
91 | .module = THIS_MODULE, | |
92 | .name = DRV_NAME, | |
93 | .ioctl = ata_scsi_ioctl, | |
94 | .queuecommand = ata_scsi_queuecmd, | |
95 | .can_queue = ATA_DEF_QUEUE, | |
96 | .this_id = ATA_SHT_THIS_ID, | |
97 | .sg_tablesize = LIBATA_MAX_PRD, | |
0df0d0a0 AZ |
98 | .cmd_per_lun = ATA_SHT_CMD_PER_LUN, |
99 | .emulated = ATA_SHT_EMULATED, | |
100 | .use_clustering = ATA_SHT_USE_CLUSTERING, | |
101 | .proc_name = DRV_NAME, | |
102 | .dma_boundary = ATA_DMA_BOUNDARY, | |
103 | .slave_configure = ata_scsi_slave_config, | |
c972b60b | 104 | .slave_destroy = ata_scsi_slave_destroy, |
0df0d0a0 AZ |
105 | .bios_param = ata_std_bios_param, |
106 | }; | |
107 | ||
108 | static struct ata_port_operations ixp4xx_port_ops = { | |
5d4c51f6 AZ |
109 | .set_mode = ixp4xx_set_mode, |
110 | .mode_filter = ata_pci_default_filter, | |
111 | ||
5d4c51f6 AZ |
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, | |
117 | ||
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, | |
122 | ||
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, | |
127 | ||
128 | .irq_handler = ata_interrupt, | |
129 | .irq_clear = ata_bmdma_irq_clear, | |
130 | .irq_on = ata_irq_on, | |
5d4c51f6 AZ |
131 | |
132 | .port_start = ata_port_start, | |
0df0d0a0 AZ |
133 | }; |
134 | ||
af183748 | 135 | static void ixp4xx_setup_port(struct ata_port *ap, |
cbcdd875 TH |
136 | struct ixp4xx_pata_data *data, |
137 | unsigned long raw_cs0, unsigned long raw_cs1) | |
0df0d0a0 | 138 | { |
af183748 | 139 | struct ata_ioports *ioaddr = &ap->ioaddr; |
cbcdd875 TH |
140 | unsigned long raw_cmd = raw_cs0; |
141 | unsigned long raw_ctl = raw_cs1 + 0x06; | |
142 | ||
0d5ff566 TH |
143 | ioaddr->cmd_addr = data->cs0; |
144 | ioaddr->altstatus_addr = data->cs1 + 0x06; | |
145 | ioaddr->ctl_addr = data->cs1 + 0x06; | |
0df0d0a0 AZ |
146 | |
147 | ata_std_ports(ioaddr); | |
148 | ||
149 | #ifndef __ARMEB__ | |
150 | ||
151 | /* adjust the addresses to handle the address swizzling of the | |
152 | * ixp4xx in little endian mode. | |
153 | */ | |
154 | ||
0d5ff566 TH |
155 | *(unsigned long *)&ioaddr->data_addr ^= 0x02; |
156 | *(unsigned long *)&ioaddr->cmd_addr ^= 0x03; | |
157 | *(unsigned long *)&ioaddr->altstatus_addr ^= 0x03; | |
158 | *(unsigned long *)&ioaddr->ctl_addr ^= 0x03; | |
159 | *(unsigned long *)&ioaddr->error_addr ^= 0x03; | |
160 | *(unsigned long *)&ioaddr->feature_addr ^= 0x03; | |
161 | *(unsigned long *)&ioaddr->nsect_addr ^= 0x03; | |
162 | *(unsigned long *)&ioaddr->lbal_addr ^= 0x03; | |
163 | *(unsigned long *)&ioaddr->lbam_addr ^= 0x03; | |
164 | *(unsigned long *)&ioaddr->lbah_addr ^= 0x03; | |
165 | *(unsigned long *)&ioaddr->device_addr ^= 0x03; | |
166 | *(unsigned long *)&ioaddr->status_addr ^= 0x03; | |
167 | *(unsigned long *)&ioaddr->command_addr ^= 0x03; | |
cbcdd875 TH |
168 | |
169 | raw_cmd ^= 0x03; | |
170 | raw_ctl ^= 0x03; | |
0df0d0a0 | 171 | #endif |
cbcdd875 TH |
172 | |
173 | ata_port_desc(ap, "cmd 0x%lx ctl 0x%lx", raw_cmd, raw_ctl); | |
0df0d0a0 AZ |
174 | } |
175 | ||
176 | static __devinit int ixp4xx_pata_probe(struct platform_device *pdev) | |
177 | { | |
0df0d0a0 AZ |
178 | unsigned int irq; |
179 | struct resource *cs0, *cs1; | |
5d728824 TH |
180 | struct ata_host *host; |
181 | struct ata_port *ap; | |
0df0d0a0 AZ |
182 | struct ixp4xx_pata_data *data = pdev->dev.platform_data; |
183 | ||
184 | cs0 = platform_get_resource(pdev, IORESOURCE_MEM, 0); | |
185 | cs1 = platform_get_resource(pdev, IORESOURCE_MEM, 1); | |
186 | ||
187 | if (!cs0 || !cs1) | |
188 | return -EINVAL; | |
189 | ||
5d728824 TH |
190 | /* allocate host */ |
191 | host = ata_host_alloc(&pdev->dev, 1); | |
192 | if (!host) | |
193 | return -ENOMEM; | |
194 | ||
195 | /* acquire resources and fill host */ | |
0df0d0a0 AZ |
196 | pdev->dev.coherent_dma_mask = DMA_32BIT_MASK; |
197 | ||
24dc5f33 TH |
198 | data->cs0 = devm_ioremap(&pdev->dev, cs0->start, 0x1000); |
199 | data->cs1 = devm_ioremap(&pdev->dev, cs1->start, 0x1000); | |
0df0d0a0 | 200 | |
991bf528 ST |
201 | if (!data->cs0 || !data->cs1) |
202 | return -ENOMEM; | |
203 | ||
0df0d0a0 AZ |
204 | irq = platform_get_irq(pdev, 0); |
205 | if (irq) | |
282c6b9c | 206 | set_irq_type(irq, IRQT_RISING); |
0df0d0a0 AZ |
207 | |
208 | /* Setup expansion bus chip selects */ | |
209 | *data->cs0_cfg = data->cs0_bits; | |
210 | *data->cs1_cfg = data->cs1_bits; | |
211 | ||
5d728824 | 212 | ap = host->ports[0]; |
0df0d0a0 | 213 | |
5d728824 TH |
214 | ap->ops = &ixp4xx_port_ops; |
215 | ap->pio_mask = 0x1f; /* PIO4 */ | |
216 | ap->flags |= ATA_FLAG_MMIO | ATA_FLAG_NO_LEGACY | ATA_FLAG_NO_ATAPI; | |
0df0d0a0 | 217 | |
cbcdd875 | 218 | ixp4xx_setup_port(ap, data, cs0->start, cs1->start); |
0df0d0a0 AZ |
219 | |
220 | dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n"); | |
221 | ||
5d728824 TH |
222 | /* activate host */ |
223 | return ata_host_activate(host, irq, ata_interrupt, 0, &ixp4xx_sht); | |
0df0d0a0 AZ |
224 | } |
225 | ||
226 | static __devexit int ixp4xx_pata_remove(struct platform_device *dev) | |
227 | { | |
228 | struct ata_host *host = platform_get_drvdata(dev); | |
229 | ||
24dc5f33 | 230 | ata_host_detach(host); |
0df0d0a0 AZ |
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); |