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