3 ata_piix.c - Intel PATA/SATA controllers
5 Maintained by: Jeff Garzik <jgarzik@pobox.com>
6 Please ALWAYS copy linux-ide@vger.kernel.org
10 Copyright 2003-2004 Red Hat Inc
11 Copyright 2003-2004 Jeff Garzik
14 Copyright header from piix.c:
16 Copyright (C) 1998-1999 Andrzej Krzysztofowicz, Author and Maintainer
17 Copyright (C) 1998-2000 Andre Hedrick <andre@linux-ide.org>
18 Copyright (C) 2003 Red Hat Inc <alan@redhat.com>
20 May be copied or modified under the terms of the GNU General Public License
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>
31 #include <scsi/scsi_host.h>
32 #include <linux/libata.h>
34 #define DRV_NAME "ata_piix"
35 #define DRV_VERSION "1.03"
38 PIIX_IOCFG = 0x54, /* IDE I/O configuration register */
39 ICH5_PMR = 0x90, /* port mapping register */
40 ICH5_PCS = 0x92, /* port control and status */
42 PIIX_FLAG_AHCI = (1 << 28), /* AHCI possible */
43 PIIX_FLAG_CHECKINTR = (1 << 29), /* make sure PCI INTx enabled */
44 PIIX_FLAG_COMBINED = (1 << 30), /* combined mode possible */
46 /* combined mode. if set, PATA is channel 0.
47 * if clear, PATA is channel 1.
49 PIIX_COMB_PATA_P0 = (1 << 1),
50 PIIX_COMB = (1 << 2), /* combined mode enabled? */
52 PIIX_PORT_PRESENT = (1 << 0),
53 PIIX_PORT_ENABLED = (1 << 4),
55 PIIX_80C_PRI = (1 << 5) | (1 << 4),
56 PIIX_80C_SEC = (1 << 7) | (1 << 6),
67 static int piix_init_one (struct pci_dev *pdev,
68 const struct pci_device_id *ent);
70 static void piix_pata_phy_reset(struct ata_port *ap);
71 static void piix_sata_phy_reset(struct ata_port *ap);
72 static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev);
73 static void piix_set_dmamode (struct ata_port *ap, struct ata_device *adev);
75 static unsigned int in_module_init = 1;
77 static struct pci_device_id piix_pci_tbl[] = {
78 #ifdef ATA_ENABLE_PATA
79 { 0x8086, 0x7111, PCI_ANY_ID, PCI_ANY_ID, 0, 0, piix4_pata },
80 { 0x8086, 0x24db, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
81 { 0x8086, 0x25a2, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_pata },
84 /* NOTE: The following PCI ids must be kept in sync with the
85 * list in drivers/pci/quirks.c.
88 { 0x8086, 0x24d1, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
89 { 0x8086, 0x24df, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
90 { 0x8086, 0x25a3, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
91 { 0x8086, 0x25b0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich5_sata },
92 { 0x8086, 0x2651, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata },
93 { 0x8086, 0x2652, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata_rm },
94 { 0x8086, 0x2653, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich6_sata_rm },
95 { 0x8086, 0x27c0, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich7_sata },
96 { 0x8086, 0x27c4, PCI_ANY_ID, PCI_ANY_ID, 0, 0, ich7_sata },
97 { 0x8086, 0x2680, PCI_ANY_ID, PCI_ANY_ID, 0, 0, esb2_sata },
99 { } /* terminate list */
102 static struct pci_driver piix_pci_driver = {
104 .id_table = piix_pci_tbl,
105 .probe = piix_init_one,
106 .remove = ata_pci_remove_one,
109 static Scsi_Host_Template piix_sht = {
110 .module = THIS_MODULE,
112 .ioctl = ata_scsi_ioctl,
113 .queuecommand = ata_scsi_queuecmd,
114 .eh_strategy_handler = ata_scsi_error,
115 .can_queue = ATA_DEF_QUEUE,
116 .this_id = ATA_SHT_THIS_ID,
117 .sg_tablesize = LIBATA_MAX_PRD,
118 .max_sectors = ATA_MAX_SECTORS,
119 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
120 .emulated = ATA_SHT_EMULATED,
121 .use_clustering = ATA_SHT_USE_CLUSTERING,
122 .proc_name = DRV_NAME,
123 .dma_boundary = ATA_DMA_BOUNDARY,
124 .slave_configure = ata_scsi_slave_config,
125 .bios_param = ata_std_bios_param,
129 static struct ata_port_operations piix_pata_ops = {
130 .port_disable = ata_port_disable,
131 .set_piomode = piix_set_piomode,
132 .set_dmamode = piix_set_dmamode,
134 .tf_load = ata_tf_load,
135 .tf_read = ata_tf_read,
136 .check_status = ata_check_status,
137 .exec_command = ata_exec_command,
138 .dev_select = ata_std_dev_select,
140 .phy_reset = piix_pata_phy_reset,
142 .bmdma_setup = ata_bmdma_setup,
143 .bmdma_start = ata_bmdma_start,
144 .bmdma_stop = ata_bmdma_stop,
145 .bmdma_status = ata_bmdma_status,
146 .qc_prep = ata_qc_prep,
147 .qc_issue = ata_qc_issue_prot,
149 .eng_timeout = ata_eng_timeout,
151 .irq_handler = ata_interrupt,
152 .irq_clear = ata_bmdma_irq_clear,
154 .port_start = ata_port_start,
155 .port_stop = ata_port_stop,
156 .host_stop = ata_host_stop,
159 static struct ata_port_operations piix_sata_ops = {
160 .port_disable = ata_port_disable,
162 .tf_load = ata_tf_load,
163 .tf_read = ata_tf_read,
164 .check_status = ata_check_status,
165 .exec_command = ata_exec_command,
166 .dev_select = ata_std_dev_select,
168 .phy_reset = piix_sata_phy_reset,
170 .bmdma_setup = ata_bmdma_setup,
171 .bmdma_start = ata_bmdma_start,
172 .bmdma_stop = ata_bmdma_stop,
173 .bmdma_status = ata_bmdma_status,
174 .qc_prep = ata_qc_prep,
175 .qc_issue = ata_qc_issue_prot,
177 .eng_timeout = ata_eng_timeout,
179 .irq_handler = ata_interrupt,
180 .irq_clear = ata_bmdma_irq_clear,
182 .port_start = ata_port_start,
183 .port_stop = ata_port_stop,
184 .host_stop = ata_host_stop,
187 static struct ata_port_info piix_port_info[] = {
191 .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST |
193 .pio_mask = 0x1f, /* pio0-4 */
195 .mwdma_mask = 0x06, /* mwdma1-2 */
197 .mwdma_mask = 0x00, /* mwdma broken */
199 .udma_mask = 0x3f, /* udma0-5 */
200 .port_ops = &piix_pata_ops,
206 .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
207 PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR,
208 .pio_mask = 0x1f, /* pio0-4 */
209 .mwdma_mask = 0x07, /* mwdma0-2 */
210 .udma_mask = 0x7f, /* udma0-6 */
211 .port_ops = &piix_sata_ops,
217 .host_flags = ATA_FLAG_SLAVE_POSS | ATA_FLAG_SRST,
218 .pio_mask = 0x1f, /* pio0-4 */
220 .mwdma_mask = 0x06, /* mwdma1-2 */
222 .mwdma_mask = 0x00, /* mwdma broken */
224 .udma_mask = ATA_UDMA_MASK_40C,
225 .port_ops = &piix_pata_ops,
231 .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
232 PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
234 .pio_mask = 0x1f, /* pio0-4 */
235 .mwdma_mask = 0x07, /* mwdma0-2 */
236 .udma_mask = 0x7f, /* udma0-6 */
237 .port_ops = &piix_sata_ops,
243 .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
244 PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
245 ATA_FLAG_SLAVE_POSS | PIIX_FLAG_AHCI,
246 .pio_mask = 0x1f, /* pio0-4 */
247 .mwdma_mask = 0x07, /* mwdma0-2 */
248 .udma_mask = 0x7f, /* udma0-6 */
249 .port_ops = &piix_sata_ops,
255 .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
256 PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
257 ATA_FLAG_SLAVE_POSS | PIIX_FLAG_AHCI,
258 .pio_mask = 0x1f, /* pio0-4 */
259 .mwdma_mask = 0x07, /* mwdma0-2 */
260 .udma_mask = 0x7f, /* udma0-6 */
261 .port_ops = &piix_sata_ops,
267 .host_flags = ATA_FLAG_SATA | ATA_FLAG_SRST |
268 PIIX_FLAG_COMBINED | PIIX_FLAG_CHECKINTR |
269 ATA_FLAG_SLAVE_POSS | PIIX_FLAG_AHCI,
270 .pio_mask = 0x1f, /* pio0-4 */
271 .mwdma_mask = 0x07, /* mwdma0-2 */
272 .udma_mask = 0x7f, /* udma0-6 */
273 .port_ops = &piix_sata_ops,
277 static struct pci_bits piix_enable_bits[] = {
278 { 0x41U, 1U, 0x80UL, 0x80UL }, /* port 0 */
279 { 0x43U, 1U, 0x80UL, 0x80UL }, /* port 1 */
282 MODULE_AUTHOR("Andre Hedrick, Alan Cox, Andrzej Krzysztofowicz, Jeff Garzik");
283 MODULE_DESCRIPTION("SCSI low-level driver for Intel PIIX/ICH ATA controllers");
284 MODULE_LICENSE("GPL");
285 MODULE_DEVICE_TABLE(pci, piix_pci_tbl);
286 MODULE_VERSION(DRV_VERSION);
289 * piix_pata_cbl_detect - Probe host controller cable detect info
290 * @ap: Port for which cable detect info is desired
292 * Read 80c cable indicator from ATA PCI device's PCI config
293 * register. This register is normally set by firmware (BIOS).
296 * None (inherited from caller).
298 static void piix_pata_cbl_detect(struct ata_port *ap)
300 struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
303 /* no 80c support in host controller? */
304 if ((ap->udma_mask & ~ATA_UDMA_MASK_40C) == 0)
307 /* check BIOS cable detect results */
308 mask = ap->hard_port_no == 0 ? PIIX_80C_PRI : PIIX_80C_SEC;
309 pci_read_config_byte(pdev, PIIX_IOCFG, &tmp);
310 if ((tmp & mask) == 0)
313 ap->cbl = ATA_CBL_PATA80;
317 ap->cbl = ATA_CBL_PATA40;
318 ap->udma_mask &= ATA_UDMA_MASK_40C;
322 * piix_pata_phy_reset - Probe specified port on PATA host controller
328 * None (inherited from caller).
331 static void piix_pata_phy_reset(struct ata_port *ap)
333 struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
335 if (!pci_test_config_bits(pdev, &piix_enable_bits[ap->hard_port_no])) {
336 ata_port_disable(ap);
337 printk(KERN_INFO "ata%u: port disabled. ignoring.\n", ap->id);
341 piix_pata_cbl_detect(ap);
349 * piix_sata_probe - Probe PCI device for present SATA devices
350 * @ap: Port associated with the PCI device we wish to probe
352 * Reads SATA PCI device's PCI config register Port Configuration
353 * and Status (PCS) to determine port and device availability.
356 * None (inherited from caller).
359 * Non-zero if device detected, zero otherwise.
361 static int piix_sata_probe (struct ata_port *ap)
363 struct pci_dev *pdev = to_pci_dev(ap->host_set->dev);
364 int combined = (ap->flags & ATA_FLAG_SLAVE_POSS);
365 int orig_mask, mask, i;
368 mask = (PIIX_PORT_PRESENT << ap->hard_port_no) |
369 (PIIX_PORT_ENABLED << ap->hard_port_no);
371 pci_read_config_byte(pdev, ICH5_PCS, &pcs);
372 orig_mask = (int) pcs & 0xff;
374 /* TODO: this is vaguely wrong for ICH6 combined mode,
375 * where only two of the four SATA ports are mapped
376 * onto a single ATA channel. It is also vaguely inaccurate
377 * for ICH5, which has only two ports. However, this is ok,
378 * as further device presence detection code will handle
379 * any false positives produced here.
382 for (i = 0; i < 4; i++) {
383 mask = (PIIX_PORT_PRESENT << i) | (PIIX_PORT_ENABLED << i);
385 if ((orig_mask & mask) == mask)
386 if (combined || (i == ap->hard_port_no))
394 * piix_sata_phy_reset - Probe specified port on SATA host controller
400 * None (inherited from caller).
403 static void piix_sata_phy_reset(struct ata_port *ap)
405 if (!piix_sata_probe(ap)) {
406 ata_port_disable(ap);
407 printk(KERN_INFO "ata%u: SATA port has no device.\n", ap->id);
411 ap->cbl = ATA_CBL_SATA;
419 * piix_set_piomode - Initialize host controller PATA PIO timings
420 * @ap: Port whose timings we are configuring
422 * @pio: PIO mode, 0 - 4
424 * Set PIO mode for device, in host controller PCI config space.
427 * None (inherited from caller).
430 static void piix_set_piomode (struct ata_port *ap, struct ata_device *adev)
432 unsigned int pio = adev->pio_mode - XFER_PIO_0;
433 struct pci_dev *dev = to_pci_dev(ap->host_set->dev);
434 unsigned int is_slave = (adev->devno != 0);
435 unsigned int master_port= ap->hard_port_no ? 0x42 : 0x40;
436 unsigned int slave_port = 0x44;
440 static const /* ISP RTC */
441 u8 timings[][2] = { { 0, 0 },
447 pci_read_config_word(dev, master_port, &master_data);
449 master_data |= 0x4000;
450 /* enable PPE, IE and TIME */
451 master_data |= 0x0070;
452 pci_read_config_byte(dev, slave_port, &slave_data);
453 slave_data &= (ap->hard_port_no ? 0x0f : 0xf0);
455 (timings[pio][0] << 2) |
456 (timings[pio][1] << (ap->hard_port_no ? 4 : 0));
458 master_data &= 0xccf8;
459 /* enable PPE, IE and TIME */
460 master_data |= 0x0007;
462 (timings[pio][0] << 12) |
463 (timings[pio][1] << 8);
465 pci_write_config_word(dev, master_port, master_data);
467 pci_write_config_byte(dev, slave_port, slave_data);
471 * piix_set_dmamode - Initialize host controller PATA PIO timings
472 * @ap: Port whose timings we are configuring
474 * @udma: udma mode, 0 - 6
476 * Set UDMA mode for device, in host controller PCI config space.
479 * None (inherited from caller).
482 static void piix_set_dmamode (struct ata_port *ap, struct ata_device *adev)
484 unsigned int udma = adev->dma_mode; /* FIXME: MWDMA too */
485 struct pci_dev *dev = to_pci_dev(ap->host_set->dev);
486 u8 maslave = ap->hard_port_no ? 0x42 : 0x40;
488 unsigned int drive_dn = (ap->hard_port_no ? 2 : 0) + adev->devno;
489 int a_speed = 3 << (drive_dn * 4);
490 int u_flag = 1 << drive_dn;
491 int v_flag = 0x01 << drive_dn;
492 int w_flag = 0x10 << drive_dn;
496 u8 reg48, reg54, reg55;
498 pci_read_config_word(dev, maslave, ®4042);
499 DPRINTK("reg4042 = 0x%04x\n", reg4042);
500 sitre = (reg4042 & 0x4000) ? 1 : 0;
501 pci_read_config_byte(dev, 0x48, ®48);
502 pci_read_config_word(dev, 0x4a, ®4a);
503 pci_read_config_byte(dev, 0x54, ®54);
504 pci_read_config_byte(dev, 0x55, ®55);
508 case XFER_UDMA_2: u_speed = 2 << (drive_dn * 4); break;
512 case XFER_UDMA_1: u_speed = 1 << (drive_dn * 4); break;
513 case XFER_UDMA_0: u_speed = 0 << (drive_dn * 4); break;
515 case XFER_MW_DMA_1: break;
521 if (speed >= XFER_UDMA_0) {
522 if (!(reg48 & u_flag))
523 pci_write_config_byte(dev, 0x48, reg48 | u_flag);
524 if (speed == XFER_UDMA_5) {
525 pci_write_config_byte(dev, 0x55, (u8) reg55|w_flag);
527 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
529 if ((reg4a & a_speed) != u_speed)
530 pci_write_config_word(dev, 0x4a, (reg4a & ~a_speed) | u_speed);
531 if (speed > XFER_UDMA_2) {
532 if (!(reg54 & v_flag))
533 pci_write_config_byte(dev, 0x54, reg54 | v_flag);
535 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
538 pci_write_config_byte(dev, 0x48, reg48 & ~u_flag);
540 pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
542 pci_write_config_byte(dev, 0x54, reg54 & ~v_flag);
544 pci_write_config_byte(dev, 0x55, (u8) reg55 & ~w_flag);
548 /* move to PCI layer, integrate w/ MSI stuff */
549 static void pci_enable_intx(struct pci_dev *pdev)
553 pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
554 if (pci_command & PCI_COMMAND_INTX_DISABLE) {
555 pci_command &= ~PCI_COMMAND_INTX_DISABLE;
556 pci_write_config_word(pdev, PCI_COMMAND, pci_command);
560 #define AHCI_PCI_BAR 5
561 #define AHCI_GLOBAL_CTL 0x04
562 #define AHCI_ENABLE (1 << 31)
563 static int piix_disable_ahci(struct pci_dev *pdev)
570 /* BUG: pci_enable_device has not yet been called. This
571 * works because this device is usually set up by BIOS.
574 addr = pci_resource_start(pdev, AHCI_PCI_BAR);
575 if (!addr || !pci_resource_len(pdev, AHCI_PCI_BAR))
578 mmio = ioremap(addr, 64);
582 tmp = readl(mmio + AHCI_GLOBAL_CTL);
583 if (tmp & AHCI_ENABLE) {
585 writel(tmp, mmio + AHCI_GLOBAL_CTL);
587 tmp = readl(mmio + AHCI_GLOBAL_CTL);
588 if (tmp & AHCI_ENABLE)
597 * piix_init_one - Register PIIX ATA PCI device with kernel services
598 * @pdev: PCI device to register
599 * @ent: Entry in piix_pci_tbl matching with @pdev
601 * Called from kernel PCI layer. We probe for combined mode (sigh),
602 * and then hand over control to libata, for it to do the rest.
605 * Inherited from PCI layer (may sleep).
608 * Zero on success, or -ERRNO value.
611 static int piix_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
613 static int printed_version;
614 struct ata_port_info *port_info[2];
615 unsigned int combined = 0, n_ports = 1;
616 unsigned int pata_chan = 0, sata_chan = 0;
618 if (!printed_version++)
619 printk(KERN_DEBUG DRV_NAME " version " DRV_VERSION "\n");
621 /* no hotplugging support (FIXME) */
625 port_info[0] = &piix_port_info[ent->driver_data];
628 if (port_info[0]->host_flags & PIIX_FLAG_AHCI) {
629 int rc = piix_disable_ahci(pdev);
634 if (port_info[0]->host_flags & PIIX_FLAG_COMBINED) {
636 pci_read_config_byte(pdev, ICH5_PMR, &tmp);
638 if (tmp & PIIX_COMB) {
640 if (tmp & PIIX_COMB_PATA_P0)
647 /* On ICH5, some BIOSen disable the interrupt using the
648 * PCI_COMMAND_INTX_DISABLE bit added in PCI 2.3.
649 * On ICH6, this bit has the same effect, but only when
650 * MSI is disabled (and it is disabled, as we don't use
651 * message-signalled interrupts currently).
653 if (port_info[0]->host_flags & PIIX_FLAG_CHECKINTR)
654 pci_enable_intx(pdev);
657 port_info[sata_chan] = &piix_port_info[ent->driver_data];
658 port_info[sata_chan]->host_flags |= ATA_FLAG_SLAVE_POSS;
659 port_info[pata_chan] = &piix_port_info[ich5_pata];
662 printk(KERN_WARNING DRV_NAME ": combined mode detected\n");
665 return ata_pci_init_one(pdev, port_info, n_ports);
677 static int __init piix_init(void)
681 DPRINTK("pci_module_init\n");
682 rc = pci_module_init(&piix_pci_driver);
699 static void __exit piix_exit(void)
701 pci_unregister_driver(&piix_pci_driver);
704 module_init(piix_init);
705 module_exit(piix_exit);