1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19#include <linux/config.h>
20#include <linux/kernel.h>
21#include <linux/module.h>
22#include <linux/pci.h>
23#include <linux/init.h>
24#include <linux/blkdev.h>
25#include <linux/delay.h>
26#include "scsi.h"
27#include "hosts.h"
28#include <linux/libata.h>
29
30#define DRV_NAME "ata_piix"
31#define DRV_VERSION "0.95"
32
33enum {
34 PIIX_IOCFG = 0x54,
35 ICH5_PCS = 0x92,
36
37 PIIX_FLAG_COMBINED = (1 << 30),
38
39 PIIX_COMB_PRI = (1 << 0),
40 PIIX_COMB_SEC = (1 << 1),
41
42 PIIX_80C_PRI = (1 << 5) | (1 << 4),
43 PIIX_80C_SEC = (1 << 7) | (1 << 6),
44
45 ich5_pata = 0,
46 ich5_sata = 1,
47 piix4_pata = 2,
48};
49
50static int piix_init_one (struct pci_dev *pdev,
51 const struct pci_device_id *ent);
52
53static void piix_pata_phy_reset(struct ata_port *ap);
54static void piix_sata_phy_reset(struct ata_port *ap);
55static void piix_sata_port_disable(struct ata_port *ap);
56static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev,
57 unsigned int pio);
58static void piix_set_udmamode (struct ata_port *ap, struct ata_device *adev,
59 unsigned int udma);
60
61static unsigned int in_module_init = 1;
62
63static struct pci_device_id piix_pci_tbl[] = {
64#ifdef ATA_ENABLE_PATA
65 { 0x8086, 0x7111, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix4_pata },
66 { 0x8086, 0x24db, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
67 { 0x8086, 0x25a2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
68#endif
69
70 { 0x8086, 0x24d1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
71 { 0x8086, 0x24df, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
72 { 0x8086, 0x25a3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
73 { 0x8086, 0x25b0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
74
75 { }
76};
77
78static struct pci_driver piix_pci_driver = {
79 .name = DRV_NAME,
80 .id_table = piix_pci_tbl,
81 .probe = piix_init_one,
82 .remove = ata_pci_remove_one,
83};
84
85static Scsi_Host_Template piix_sht = {
86 .module = THIS_MODULE,
87 .name = DRV_NAME,
88 .queuecommand = ata_scsi_queuecmd,
89 .eh_strategy_handler = ata_scsi_error,
90 .can_queue = ATA_DEF_QUEUE,
91 .this_id = ATA_SHT_THIS_ID,
92 .sg_tablesize = ATA_MAX_PRD,
93 .max_sectors = ATA_MAX_SECTORS,
94 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
95 .emulated = ATA_SHT_EMULATED,
96 .use_clustering = ATA_SHT_USE_CLUSTERING,
97 .proc_name = DRV_NAME,
98 .dma_boundary = ATA_DMA_BOUNDARY,
99 .slave_configure = ata_scsi_slave_config,
100};
101
102static struct ata_port_operations piix_pata_ops = {
103 .port_disable = ata_port_disable,
104 .set_piomode = piix_set_piomode,
105 .set_udmamode = piix_set_udmamode,
106
107 .tf_load = ata_tf_load_pio,
108 .tf_read = ata_tf_read_pio,
109 .check_status = ata_check_status_pio,
110 .exec_command = ata_exec_command_pio,
111
112 .phy_reset = piix_pata_phy_reset,
113 .phy_config = pata_phy_config,
114
115 .bmdma_start = ata_bmdma_start_pio,
116 .fill_sg = ata_fill_sg,
117 .eng_timeout = ata_eng_timeout,
118
119 .irq_handler = ata_interrupt,
120
121 .port_start = ata_port_start,
122 .port_stop = ata_port_stop,
123};
124
125static struct ata_port_operations piix_sata_ops = {
126 .port_disable = piix_sata_port_disable,
127 .set_piomode = piix_set_piomode,
128 .set_udmamode = piix_set_udmamode,
129
130 .tf_load = ata_tf_load_pio,
131 .tf_read = ata_tf_read_pio,
132 .check_status = ata_check_status_pio,
133 .exec_command = ata_exec_command_pio,
134
135 .phy_reset = piix_sata_phy_reset,
136 .phy_config = pata_phy_config,
137
138 .bmdma_start = ata_bmdma_start_pio,
139 .fill_sg = ata_fill_sg,
140 .eng_timeout = ata_eng_timeout,
141
142 .irq_handler = ata_interrupt,
143
144 .port_start = ata_port_start,
145 .port_stop = ata_port_stop,
146};
147
148static struct ata_port_info piix_port_info[] = {
149
150 {
151 .sht = &piix_sht,
152 .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
153 .pio_mask = 0x03,
154 .udma_mask = ATA_UDMA_MASK_40C,
155 .port_ops = &piix_pata_ops,
156 },
157
158
159 {
160 .sht = &piix_sht,
161 .host_flags = ATA_FLAG_SATA | PIIX_FLAG_COMBINED |
162 ATA_FLAG_SRST,
163 .pio_mask = 0x03,
164 .udma_mask = 0x7f,
165 .port_ops = &piix_sata_ops,
166 },
167
168
169 {
170 .sht = &piix_sht,
171 .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
172 .pio_mask = 0x03,
173 .udma_mask = ATA_UDMA_MASK_40C,
174 .port_ops = &piix_pata_ops,
175 },
176};
177
178static struct pci_bits piix_enable_bits[] = {
179 { 0x41U, 1U, 0x80UL, 0x80UL },
180 { 0x43U, 1U, 0x80UL, 0x80UL },
181};
182
183MODULE_AUTHOR("Andre Hedrick, Alan Cox, Andrzej Krzysztofowicz, Jeff Garzik");
184MODULE_DESCRIPTION("SCSI low-level driver for Intel PIIX/ICH ATA controllers");
185MODULE_LICENSE("GPL");
186MODULE_DEVICE_TABLE(pci, piix_pci_tbl);
187
188
189
190
191
192
193
194
195
196
197
198static void piix_pata_cbl_detect(struct ata_port *ap)
199{
200 struct pci_dev *pdev = ap->host_set->pdev;
201 u8 tmp, mask;
202
203
204 if ((ap->udma_mask & ~ATA_UDMA_MASK_40C) == 0)
205 goto cbl40;
206
207
208 mask = ap->port_no == 0 ? PIIX_80C_PRI : PIIX_80C_SEC;
209 pci_read_config_byte(pdev, PIIX_IOCFG, &tmp);
210 if ((tmp & mask) == 0)
211 goto cbl40;
212
213 ap->cbl = ATA_CBL_PATA80;
214 return;
215
216cbl40:
217 ap->cbl = ATA_CBL_PATA40;
218 ap->udma_mask &= ATA_UDMA_MASK_40C;
219}
220
221
222
223
224
225
226
227
228
229
230
231static void piix_pata_phy_reset(struct ata_port *ap)
232{
233 if (!pci_test_config_bits(ap->host_set->pdev,
234 &piix_enable_bits[ap->port_no])) {
235 ata_port_disable(ap);
236 printk(KERN_INFO "ata%u: port disabled. ignoring.\n", ap->id);
237 return;
238 }
239
240 piix_pata_cbl_detect(ap);
241
242 ata_port_probe(ap);
243
244 ata_bus_reset(ap);
245}
246
247
248
249
250
251
252
253
254
255
256
257
258
259static void piix_pcs_probe (struct ata_port *ap, unsigned int *have_port,
260 unsigned int *have_device)
261{
262 struct pci_dev *pdev = ap->host_set->pdev;
263 u16 pcs;
264
265 pci_read_config_word(pdev, ICH5_PCS, &pcs);
266
267
268 if (pcs & (1 << ap->port_no)) {
269 *have_port = 1;
270
271 if (pcs & (1 << (ap->port_no + 4)))
272 *have_device = 1;
273 }
274}
275
276
277
278
279
280
281
282
283
284
285static void piix_pcs_disable (struct ata_port *ap)
286{
287 struct pci_dev *pdev = ap->host_set->pdev;
288 u16 pcs;
289
290 pci_read_config_word(pdev, ICH5_PCS, &pcs);
291
292 if (pcs & (1 << ap->port_no)) {
293 pcs &= ~(1 << ap->port_no);
294 pci_write_config_word(pdev, ICH5_PCS, pcs);
295 }
296}
297
298
299
300
301
302
303
304
305
306
307
308static void piix_sata_phy_reset(struct ata_port *ap)
309{
310 unsigned int have_port = 0, have_dev = 0;
311
312 if (!pci_test_config_bits(ap->host_set->pdev,
313 &piix_enable_bits[ap->port_no])) {
314 ata_port_disable(ap);
315 printk(KERN_INFO "ata%u: port disabled. ignoring.\n", ap->id);
316 return;
317 }
318
319 piix_pcs_probe(ap, &have_port, &have_dev);
320
321
322 if (!have_port) {
323 ata_port_disable(ap);
324 printk(KERN_INFO "ata%u: SATA port disabled. ignoring.\n",
325 ap->id);
326 return;
327 }
328
329
330 if (!have_dev) {
331 piix_sata_port_disable(ap);
332 printk(KERN_INFO "ata%u: SATA port has no device. disabling.\n",
333 ap->id);
334 return;
335 }
336
337 ap->cbl = ATA_CBL_SATA;
338
339 ata_port_probe(ap);
340
341 ata_bus_reset(ap);
342}
343
344
345
346
347
348
349
350
351
352
353
354static void piix_sata_port_disable(struct ata_port *ap)
355{
356 ata_port_disable(ap);
357 piix_pcs_disable(ap);
358}
359
360
361
362
363
364
365
366
367
368
369
370
371
372static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev,
373 unsigned int pio)
374{
375 struct pci_dev *dev = ap->host_set->pdev;
376 unsigned int is_slave = (adev->flags & ATA_DFLAG_MASTER) ? 0 : 1;
377 unsigned int master_port= ap->port_no ? 0x42 : 0x40;
378 unsigned int slave_port = 0x44;
379 u16 master_data;
380 u8 slave_data;
381
382 static const
383 u8 timings[][2] = { { 0, 0 },
384 { 0, 0 },
385 { 1, 0 },
386 { 2, 1 },
387 { 2, 3 }, };
388
389 pci_read_config_word(dev, master_port, &master_data);
390 if (is_slave) {
391 master_data |= 0x4000;
392
393 master_data |= 0x0070;
394 pci_read_config_byte(dev, slave_port, &slave_data);
395 slave_data &= (ap->port_no ? 0x0f : 0xf0);
396 slave_data |=
397 (timings[pio][0] << 2) |
398 (timings[pio][1] << (ap->port_no ? 4 : 0));
399 } else {
400 master_data &= 0xccf8;
401
402 master_data |= 0x0007;
403 master_data |=
404 (timings[pio][0] << 12) |
405 (timings[pio][1] << 8);
406 }
407 pci_write_config_word(dev, master_port, master_data);
408 if (is_slave)
409 pci_write_config_byte(dev, slave_port, slave_data);
410}
411
412
413
414
415
416
417
418
419
420
421
422
423
424static void piix_set_udmamode (struct ata_port *ap, struct ata_device *adev,
425 unsigned int udma)
426{
427 struct pci_dev *dev = ap->host_set->pdev;
428 u8 maslave = ap->port_no ? 0x42 : 0x40;
429 u8 speed = udma;
430 unsigned int drive_dn = (ap->port_no ? 2 : 0) + adev->devno;
431 int a_speed = 3 << (drive_dn * 4);
432 int u_flag = 1 << drive_dn;
433 int v_flag = 0x01 << drive_dn;
434 int w_flag = 0x10 << drive_dn;
435 int u_speed = 0;
436 int sitre;
437 u16 reg4042, reg44, reg48, reg4a, reg54;
438 u8 reg55;
439
440 pci_read_config_word(dev, maslave, ®4042);
441 DPRINTK("reg4042 = 0x%04x\n", reg4042);
442 sitre = (reg4042 & 0x4000) ? 1 : 0;
443 pci_read_config_word(dev, 0x44, ®44);
444 pci_read_config_word(dev, 0x48, ®48);
445 pci_read_config_word(dev, 0x4a, ®4a);
446 pci_read_config_word(dev, 0x54, ®54);
447 pci_read_config_byte(dev, 0x55, ®55);
448
449 switch(speed) {
450 case XFER_UDMA_4:
451 case XFER_UDMA_2: u_speed = 2 << (drive_dn * 4); break;
452 case XFER_UDMA_6:
453 case XFER_UDMA_5:
454 case XFER_UDMA_3:
455 case XFER_UDMA_1: u_speed = 1 << (drive_dn * 4); break;
456 case XFER_UDMA_0: u_speed = 0 << (drive_dn * 4); break;
457 default:
458 BUG();
459 return;
460 }
461
462 if (!(reg48 & u_flag))
463 pci_write_config_word(dev, 0x48, reg48|u_flag);
464 if (speed == XFER_UDMA_5) {
465 pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
466 } else {
467 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
468 }
469 if (!(reg4a & u_speed)) {
470 pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
471 pci_write_config_word(dev, 0x4a, reg4a|u_speed);
472 }
473 if (speed > XFER_UDMA_2) {
474 if (!(reg54 & v_flag)) {
475 pci_write_config_word(dev, 0x54, reg54|v_flag);
476 }
477 } else {
478 pci_write_config_word(dev, 0x54, reg54 & ~v_flag);
479 }
480}
481
482
483
484
485
486
487
488
489
490
491
492
493static void piix_probe_combined (struct pci_dev *pdev, unsigned int *mask)
494{
495 u8 tmp;
496
497 pci_read_config_byte(pdev, 0x90, &tmp);
498 tmp &= 0x6;
499
500
501 if (tmp == 0x4)
502 *mask |= PIIX_COMB_SEC;
503 if (tmp == 0x6)
504 *mask |= PIIX_COMB_PRI;
505}
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522static int piix_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
523{
524 static int printed_version;
525 struct ata_port_info *port_info[2];
526 unsigned int combined = 0, n_ports = 1;
527 unsigned int pata_comb = 0, sata_comb = 0;
528
529 if (!printed_version++)
530 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
531
532
533 if (!in_module_init)
534 return -ENODEV;
535
536 port_info[0] = &piix_port_info[ent->driver_data];
537 port_info[1] = NULL;
538 if (port_info[0]->host_flags & PIIX_FLAG_COMBINED)
539 piix_probe_combined(pdev, &combined);
540
541 if (combined & PIIX_COMB_PRI)
542 sata_comb = 1;
543 else if (combined & PIIX_COMB_SEC)
544 pata_comb = 1;
545
546 if (pata_comb || sata_comb) {
547 port_info[sata_comb] = &piix_port_info[ent->driver_data];
548 port_info[sata_comb]->host_flags |= ATA_FLAG_SLAVE_POSS;
549 port_info[pata_comb] = &piix_port_info[ich5_pata];
550 n_ports++;
551
552 printk(KERN_WARNING DRV_NAME ": combined mode detected\n");
553 }
554
555 return ata_pci_init_one(pdev, port_info, n_ports);
556}
557
558
559
560
561
562
563
564
565
566
567static int __init piix_init(void)
568{
569 int rc;
570
571 DPRINTK("pci_module_init\n");
572 rc = pci_module_init(&piix_pci_driver);
573 if (rc)
574 return rc;
575
576 in_module_init = 0;
577
578 DPRINTK("done\n");
579 return 0;
580}
581
582
583
584
585
586
587
588
589static void __exit piix_exit(void)
590{
591 pci_unregister_driver(&piix_pci_driver);
592}
593
594module_init(piix_init);
595module_exit(piix_exit);
596
597