1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/pci.h>
27#include <linux/init.h>
28#include <linux/blkdev.h>
29#include <linux/delay.h>
30#include <linux/device.h>
31#include <scsi/scsi_host.h>
32#include <linux/libata.h>
33#include <linux/dmi.h>
34
35#define DRV_NAME "pata_rdc"
36#define DRV_VERSION "0.01"
37
38struct rdc_host_priv {
39 u32 saved_iocfg;
40};
41
42
43
44
45
46
47
48
49
50
51
52
53static int rdc_pata_cable_detect(struct ata_port *ap)
54{
55 struct rdc_host_priv *hpriv = ap->host->private_data;
56 u8 mask;
57
58
59 mask = 0x30 << (2 * ap->port_no);
60 if ((hpriv->saved_iocfg & mask) == 0)
61 return ATA_CBL_PATA40;
62 return ATA_CBL_PATA80;
63}
64
65
66
67
68
69
70
71
72
73static int rdc_pata_prereset(struct ata_link *link, unsigned long deadline)
74{
75 struct ata_port *ap = link->ap;
76 struct pci_dev *pdev = to_pci_dev(ap->host->dev);
77
78 static const struct pci_bits rdc_enable_bits[] = {
79 { 0x41U, 1U, 0x80UL, 0x80UL },
80 { 0x43U, 1U, 0x80UL, 0x80UL },
81 };
82
83 if (!pci_test_config_bits(pdev, &rdc_enable_bits[ap->port_no]))
84 return -ENOENT;
85 return ata_sff_prereset(link, deadline);
86}
87
88
89
90
91
92
93
94
95
96
97
98
99static void rdc_set_piomode(struct ata_port *ap, struct ata_device *adev)
100{
101 unsigned int pio = adev->pio_mode - XFER_PIO_0;
102 struct pci_dev *dev = to_pci_dev(ap->host->dev);
103 unsigned int is_slave = (adev->devno != 0);
104 unsigned int master_port= ap->port_no ? 0x42 : 0x40;
105 unsigned int slave_port = 0x44;
106 u16 master_data;
107 u8 slave_data;
108 u8 udma_enable;
109 int control = 0;
110
111 static const
112 u8 timings[][2] = { { 0, 0 },
113 { 0, 0 },
114 { 1, 0 },
115 { 2, 1 },
116 { 2, 3 }, };
117
118 if (pio >= 2)
119 control |= 1;
120 if (ata_pio_need_iordy(adev))
121 control |= 2;
122
123 if (adev->class == ATA_DEV_ATA)
124 control |= 4;
125
126
127
128
129
130 pci_read_config_word(dev, master_port, &master_data);
131 if (is_slave) {
132
133 master_data &= 0xff0f;
134
135 master_data |= 0x4000;
136
137 master_data |= (control << 4);
138 pci_read_config_byte(dev, slave_port, &slave_data);
139 slave_data &= (ap->port_no ? 0x0f : 0xf0);
140
141 slave_data |= ((timings[pio][0] << 2) | timings[pio][1])
142 << (ap->port_no ? 4 : 0);
143 } else {
144
145 master_data &= 0xccf0;
146
147 master_data |= control;
148
149 master_data |=
150 (timings[pio][0] << 12) |
151 (timings[pio][1] << 8);
152 }
153 pci_write_config_word(dev, master_port, master_data);
154 if (is_slave)
155 pci_write_config_byte(dev, slave_port, slave_data);
156
157
158
159
160 pci_read_config_byte(dev, 0x48, &udma_enable);
161 udma_enable &= ~(1 << (2 * ap->port_no + adev->devno));
162 pci_write_config_byte(dev, 0x48, udma_enable);
163}
164
165
166
167
168
169
170
171
172
173
174
175
176static void rdc_set_dmamode(struct ata_port *ap, struct ata_device *adev)
177{
178 struct pci_dev *dev = to_pci_dev(ap->host->dev);
179 u8 master_port = ap->port_no ? 0x42 : 0x40;
180 u16 master_data;
181 u8 speed = adev->dma_mode;
182 int devid = adev->devno + 2 * ap->port_no;
183 u8 udma_enable = 0;
184
185 static const
186 u8 timings[][2] = { { 0, 0 },
187 { 0, 0 },
188 { 1, 0 },
189 { 2, 1 },
190 { 2, 3 }, };
191
192 pci_read_config_word(dev, master_port, &master_data);
193 pci_read_config_byte(dev, 0x48, &udma_enable);
194
195 if (speed >= XFER_UDMA_0) {
196 unsigned int udma = adev->dma_mode - XFER_UDMA_0;
197 u16 udma_timing;
198 u16 ideconf;
199 int u_clock, u_speed;
200
201
202
203
204
205
206
207
208 u_speed = min(2 - (udma & 1), udma);
209 if (udma == 5)
210 u_clock = 0x1000;
211 else if (udma > 2)
212 u_clock = 1;
213 else
214 u_clock = 0;
215
216 udma_enable |= (1 << devid);
217
218
219 pci_read_config_word(dev, 0x4A, &udma_timing);
220 udma_timing &= ~(3 << (4 * devid));
221 udma_timing |= u_speed << (4 * devid);
222 pci_write_config_word(dev, 0x4A, udma_timing);
223
224
225 pci_read_config_word(dev, 0x54, &ideconf);
226 ideconf &= ~(0x1001 << devid);
227 ideconf |= u_clock << devid;
228 pci_write_config_word(dev, 0x54, ideconf);
229 } else {
230
231
232
233
234
235 unsigned int mwdma = adev->dma_mode - XFER_MW_DMA_0;
236 unsigned int control;
237 u8 slave_data;
238 const unsigned int needed_pio[3] = {
239 XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
240 };
241 int pio = needed_pio[mwdma] - XFER_PIO_0;
242
243 control = 3;
244
245
246
247
248 if (adev->pio_mode < needed_pio[mwdma])
249
250 control |= 8;
251
252 if (adev->devno) {
253 master_data &= 0xFF4F;
254 master_data |= control << 4;
255 pci_read_config_byte(dev, 0x44, &slave_data);
256 slave_data &= (ap->port_no ? 0x0f : 0xf0);
257
258 slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) << (ap->port_no ? 4 : 0);
259 pci_write_config_byte(dev, 0x44, slave_data);
260 } else {
261 master_data &= 0xCCF4;
262
263 master_data |= control;
264 master_data |=
265 (timings[pio][0] << 12) |
266 (timings[pio][1] << 8);
267 }
268
269 udma_enable &= ~(1 << devid);
270 pci_write_config_word(dev, master_port, master_data);
271 }
272 pci_write_config_byte(dev, 0x48, udma_enable);
273}
274
275static struct ata_port_operations rdc_pata_ops = {
276 .inherits = &ata_bmdma32_port_ops,
277 .cable_detect = rdc_pata_cable_detect,
278 .set_piomode = rdc_set_piomode,
279 .set_dmamode = rdc_set_dmamode,
280 .prereset = rdc_pata_prereset,
281};
282
283static struct ata_port_info rdc_port_info = {
284
285 .flags = ATA_FLAG_SLAVE_POSS,
286 .pio_mask = ATA_PIO4,
287 .mwdma_mask = ATA_MWDMA2,
288 .udma_mask = ATA_UDMA5,
289 .port_ops = &rdc_pata_ops,
290};
291
292static struct scsi_host_template rdc_sht = {
293 ATA_BMDMA_SHT(DRV_NAME),
294};
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311static int __devinit rdc_init_one(struct pci_dev *pdev,
312 const struct pci_device_id *ent)
313{
314 static int printed_version;
315 struct device *dev = &pdev->dev;
316 struct ata_port_info port_info[2];
317 const struct ata_port_info *ppi[] = { &port_info[0], &port_info[1] };
318 unsigned long port_flags;
319 struct ata_host *host;
320 struct rdc_host_priv *hpriv;
321 int rc;
322
323 if (!printed_version++)
324 dev_printk(KERN_DEBUG, &pdev->dev,
325 "version " DRV_VERSION "\n");
326
327 port_info[0] = rdc_port_info;
328 port_info[1] = rdc_port_info;
329
330 port_flags = port_info[0].flags;
331
332
333 rc = pcim_enable_device(pdev);
334 if (rc)
335 return rc;
336
337 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
338 if (!hpriv)
339 return -ENOMEM;
340
341
342
343
344 pci_read_config_dword(pdev, 0x54, &hpriv->saved_iocfg);
345
346 rc = ata_pci_sff_prepare_host(pdev, ppi, &host);
347 if (rc)
348 return rc;
349 host->private_data = hpriv;
350
351 pci_intx(pdev, 1);
352
353 host->flags |= ATA_HOST_PARALLEL_SCAN;
354
355 pci_set_master(pdev);
356 return ata_pci_sff_activate_host(host, ata_sff_interrupt, &rdc_sht);
357}
358
359static void rdc_remove_one(struct pci_dev *pdev)
360{
361 struct ata_host *host = dev_get_drvdata(&pdev->dev);
362 struct rdc_host_priv *hpriv = host->private_data;
363
364 pci_write_config_dword(pdev, 0x54, hpriv->saved_iocfg);
365
366 ata_pci_remove_one(pdev);
367}
368
369static const struct pci_device_id rdc_pci_tbl[] = {
370 { PCI_DEVICE(0x17F3, 0x1011), },
371 { PCI_DEVICE(0x17F3, 0x1012), },
372 { }
373};
374
375static struct pci_driver rdc_pci_driver = {
376 .name = DRV_NAME,
377 .id_table = rdc_pci_tbl,
378 .probe = rdc_init_one,
379 .remove = rdc_remove_one,
380};
381
382
383static int __init rdc_init(void)
384{
385 return pci_register_driver(&rdc_pci_driver);
386}
387
388static void __exit rdc_exit(void)
389{
390 pci_unregister_driver(&rdc_pci_driver);
391}
392
393module_init(rdc_init);
394module_exit(rdc_exit);
395
396MODULE_AUTHOR("Alan Cox (based on ata_piix)");
397MODULE_DESCRIPTION("SCSI low-level driver for RDC PATA controllers");
398MODULE_LICENSE("GPL");
399MODULE_DEVICE_TABLE(pci, rdc_pci_tbl);
400MODULE_VERSION(DRV_VERSION);
401