109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2669a5db4SJeff Garzik /*
3669a5db4SJeff Garzik * pata_mpiix.c - Intel MPIIX PATA for new ATA layer
4669a5db4SJeff Garzik * (C) 2005-2006 Red Hat Inc
5ab771630SAlan Cox * Alan Cox <alan@lxorguk.ukuu.org.uk>
6669a5db4SJeff Garzik *
7669a5db4SJeff Garzik * The MPIIX is different enough to the PIIX4 and friends that we give it
8669a5db4SJeff Garzik * a separate driver. The old ide/pci code handles this by just not tuning
9669a5db4SJeff Garzik * MPIIX at all.
10669a5db4SJeff Garzik *
11669a5db4SJeff Garzik * The MPIIX also differs in another important way from the majority of PIIX
12669a5db4SJeff Garzik * devices. The chip is a bridge (pardon the pun) between the old world of
13669a5db4SJeff Garzik * ISA IDE and PCI IDE. Although the ATA timings are PCI configured the actual
14669a5db4SJeff Garzik * IDE controller is not decoded in PCI space and the chip does not claim to
15669a5db4SJeff Garzik * be IDE class PCI. This requires slightly non-standard probe logic compared
16669a5db4SJeff Garzik * with PCI IDE and also that we do not disable the device when our driver is
17669a5db4SJeff Garzik * unloaded (as it has many other functions).
18669a5db4SJeff Garzik *
1925985edcSLucas De Marchi * The driver consciously keeps this logic internally to avoid pushing quirky
20669a5db4SJeff Garzik * PATA history into the clean libata layer.
21669a5db4SJeff Garzik *
22c961922bSAlan Cox * Thinkpad specific note: If you boot an MPIIX using a thinkpad with a PCMCIA
23669a5db4SJeff Garzik * hard disk present this driver will not detect it. This is not a bug. In this
24669a5db4SJeff Garzik * configuration the secondary port of the MPIIX is disabled and the addresses
25669a5db4SJeff Garzik * are decoded by the PCMCIA bridge and therefore are for a generic IDE driver
26669a5db4SJeff Garzik * to operate.
27669a5db4SJeff Garzik */
28669a5db4SJeff Garzik
29669a5db4SJeff Garzik #include <linux/kernel.h>
30669a5db4SJeff Garzik #include <linux/module.h>
31669a5db4SJeff Garzik #include <linux/pci.h>
32669a5db4SJeff Garzik #include <linux/blkdev.h>
33669a5db4SJeff Garzik #include <linux/delay.h>
34669a5db4SJeff Garzik #include <scsi/scsi_host.h>
35669a5db4SJeff Garzik #include <linux/libata.h>
36669a5db4SJeff Garzik
37669a5db4SJeff Garzik #define DRV_NAME "pata_mpiix"
38871af121SAlan Cox #define DRV_VERSION "0.7.7"
39669a5db4SJeff Garzik
40669a5db4SJeff Garzik enum {
41669a5db4SJeff Garzik IDETIM = 0x6C, /* IDE control register */
42669a5db4SJeff Garzik IORDY = (1 << 1),
43669a5db4SJeff Garzik PPE = (1 << 2),
44669a5db4SJeff Garzik FTIM = (1 << 0),
45669a5db4SJeff Garzik ENABLED = (1 << 15),
46669a5db4SJeff Garzik SECONDARY = (1 << 14)
47669a5db4SJeff Garzik };
48669a5db4SJeff Garzik
mpiix_pre_reset(struct ata_link * link,unsigned long deadline)49cc0680a5STejun Heo static int mpiix_pre_reset(struct ata_link *link, unsigned long deadline)
50669a5db4SJeff Garzik {
51cc0680a5STejun Heo struct ata_port *ap = link->ap;
52669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev);
5392ae7849SSergei Shtylyov static const struct pci_bits mpiix_enable_bits = { 0x6D, 1, 0x80, 0x80 };
54669a5db4SJeff Garzik
5592ae7849SSergei Shtylyov if (!pci_test_config_bits(pdev, &mpiix_enable_bits))
56c961922bSAlan Cox return -ENOENT;
57d4b2bab4STejun Heo
589363c382STejun Heo return ata_sff_prereset(link, deadline);
59669a5db4SJeff Garzik }
60669a5db4SJeff Garzik
61669a5db4SJeff Garzik /**
62669a5db4SJeff Garzik * mpiix_set_piomode - set initial PIO mode data
63669a5db4SJeff Garzik * @ap: ATA interface
64669a5db4SJeff Garzik * @adev: ATA device
65669a5db4SJeff Garzik *
66669a5db4SJeff Garzik * Called to do the PIO mode setup. The MPIIX allows us to program the
677b4f1a13SSergei Shtylyov * IORDY sample point (2-5 clocks), recovery (1-4 clocks) and whether
687b4f1a13SSergei Shtylyov * prefetching or IORDY are used.
69669a5db4SJeff Garzik *
70669a5db4SJeff Garzik * This would get very ugly because we can only program timing for one
71669a5db4SJeff Garzik * device at a time, the other gets PIO0. Fortunately libata calls
729363c382STejun Heo * our qc_issue command before a command is issued so we can flip the
739363c382STejun Heo * timings back and forth to reduce the pain.
74669a5db4SJeff Garzik */
75669a5db4SJeff Garzik
mpiix_set_piomode(struct ata_port * ap,struct ata_device * adev)76669a5db4SJeff Garzik static void mpiix_set_piomode(struct ata_port *ap, struct ata_device *adev)
77669a5db4SJeff Garzik {
78669a5db4SJeff Garzik int control = 0;
79669a5db4SJeff Garzik int pio = adev->pio_mode - XFER_PIO_0;
80669a5db4SJeff Garzik struct pci_dev *pdev = to_pci_dev(ap->host->dev);
81669a5db4SJeff Garzik u16 idetim;
82669a5db4SJeff Garzik static const /* ISP RTC */
83669a5db4SJeff Garzik u8 timings[][2] = { { 0, 0 },
84669a5db4SJeff Garzik { 0, 0 },
85669a5db4SJeff Garzik { 1, 0 },
86669a5db4SJeff Garzik { 2, 1 },
87669a5db4SJeff Garzik { 2, 3 }, };
88669a5db4SJeff Garzik
89669a5db4SJeff Garzik pci_read_config_word(pdev, IDETIM, &idetim);
907b4f1a13SSergei Shtylyov
917b4f1a13SSergei Shtylyov /* Mask the IORDY/TIME/PPE for this device */
92669a5db4SJeff Garzik if (adev->class == ATA_DEV_ATA)
937b4f1a13SSergei Shtylyov control |= PPE; /* Enable prefetch/posting for disk */
94669a5db4SJeff Garzik if (ata_pio_need_iordy(adev))
957b4f1a13SSergei Shtylyov control |= IORDY;
967b4f1a13SSergei Shtylyov if (pio > 1)
97669a5db4SJeff Garzik control |= FTIM; /* This drive is on the fast timing bank */
98669a5db4SJeff Garzik
99669a5db4SJeff Garzik /* Mask out timing and clear both TIME bank selects */
100669a5db4SJeff Garzik idetim &= 0xCCEE;
1017b4f1a13SSergei Shtylyov idetim &= ~(0x07 << (4 * adev->devno));
1027b4f1a13SSergei Shtylyov idetim |= control << (4 * adev->devno);
103669a5db4SJeff Garzik
104669a5db4SJeff Garzik idetim |= (timings[pio][0] << 12) | (timings[pio][1] << 8);
105669a5db4SJeff Garzik pci_write_config_word(pdev, IDETIM, idetim);
106669a5db4SJeff Garzik
107669a5db4SJeff Garzik /* We use ap->private_data as a pointer to the device currently
108669a5db4SJeff Garzik loaded for timing */
109669a5db4SJeff Garzik ap->private_data = adev;
110669a5db4SJeff Garzik }
111669a5db4SJeff Garzik
112669a5db4SJeff Garzik /**
1139363c382STejun Heo * mpiix_qc_issue - command issue
114669a5db4SJeff Garzik * @qc: command pending
115669a5db4SJeff Garzik *
116669a5db4SJeff Garzik * Called when the libata layer is about to issue a command. We wrap
117669a5db4SJeff Garzik * this interface so that we can load the correct ATA timings if
1183a4fa0a2SRobert P. J. Day * necessary. Our logic also clears TIME0/TIME1 for the other device so
119669a5db4SJeff Garzik * that, even if we get this wrong, cycles to the other device will
120669a5db4SJeff Garzik * be made PIO0.
121669a5db4SJeff Garzik */
122669a5db4SJeff Garzik
mpiix_qc_issue(struct ata_queued_cmd * qc)1239363c382STejun Heo static unsigned int mpiix_qc_issue(struct ata_queued_cmd *qc)
124669a5db4SJeff Garzik {
125669a5db4SJeff Garzik struct ata_port *ap = qc->ap;
126669a5db4SJeff Garzik struct ata_device *adev = qc->dev;
127669a5db4SJeff Garzik
128669a5db4SJeff Garzik /* If modes have been configured and the channel data is not loaded
129669a5db4SJeff Garzik then load it. We have to check if pio_mode is set as the core code
130669a5db4SJeff Garzik does not set adev->pio_mode to XFER_PIO_0 while probing as would be
131669a5db4SJeff Garzik logical */
132669a5db4SJeff Garzik
133669a5db4SJeff Garzik if (adev->pio_mode && adev != ap->private_data)
134669a5db4SJeff Garzik mpiix_set_piomode(ap, adev);
135669a5db4SJeff Garzik
1369363c382STejun Heo return ata_sff_qc_issue(qc);
137669a5db4SJeff Garzik }
138669a5db4SJeff Garzik
139*25df73d9SBart Van Assche static const struct scsi_host_template mpiix_sht = {
14068d1d07bSTejun Heo ATA_PIO_SHT(DRV_NAME),
141669a5db4SJeff Garzik };
142669a5db4SJeff Garzik
143669a5db4SJeff Garzik static struct ata_port_operations mpiix_port_ops = {
144029cfd6bSTejun Heo .inherits = &ata_sff_port_ops,
1459363c382STejun Heo .qc_issue = mpiix_qc_issue,
146029cfd6bSTejun Heo .cable_detect = ata_cable_40wire,
147029cfd6bSTejun Heo .set_piomode = mpiix_set_piomode,
148a1efdabaSTejun Heo .prereset = mpiix_pre_reset,
149871af121SAlan Cox .sff_data_xfer = ata_sff_data_xfer32,
150669a5db4SJeff Garzik };
151669a5db4SJeff Garzik
mpiix_init_one(struct pci_dev * dev,const struct pci_device_id * id)152669a5db4SJeff Garzik static int mpiix_init_one(struct pci_dev *dev, const struct pci_device_id *id)
153669a5db4SJeff Garzik {
154669a5db4SJeff Garzik /* Single threaded by the PCI probe logic */
1555d728824STejun Heo struct ata_host *host;
1565d728824STejun Heo struct ata_port *ap;
1570d5ff566STejun Heo void __iomem *cmd_addr, *ctl_addr;
158669a5db4SJeff Garzik u16 idetim;
159cbcdd875STejun Heo int cmd, ctl, irq;
160669a5db4SJeff Garzik
16106296a1eSJoe Perches ata_print_version_once(&dev->dev, DRV_VERSION);
162669a5db4SJeff Garzik
1635d728824STejun Heo host = ata_host_alloc(&dev->dev, 1);
1645d728824STejun Heo if (!host)
1655d728824STejun Heo return -ENOMEM;
166cbcdd875STejun Heo ap = host->ports[0];
1675d728824STejun Heo
168669a5db4SJeff Garzik /* MPIIX has many functions which can be turned on or off according
169669a5db4SJeff Garzik to other devices present. Make sure IDE is enabled before we try
170669a5db4SJeff Garzik and use it */
171669a5db4SJeff Garzik
172669a5db4SJeff Garzik pci_read_config_word(dev, IDETIM, &idetim);
173669a5db4SJeff Garzik if (!(idetim & ENABLED))
174669a5db4SJeff Garzik return -ENODEV;
175669a5db4SJeff Garzik
17692ae7849SSergei Shtylyov /* See if it's primary or secondary channel... */
1770d5ff566STejun Heo if (!(idetim & SECONDARY)) {
178cbcdd875STejun Heo cmd = 0x1F0;
179cbcdd875STejun Heo ctl = 0x3F6;
1800d5ff566STejun Heo irq = 14;
1810d5ff566STejun Heo } else {
182cbcdd875STejun Heo cmd = 0x170;
183cbcdd875STejun Heo ctl = 0x376;
1840d5ff566STejun Heo irq = 15;
1850d5ff566STejun Heo }
1860d5ff566STejun Heo
187cbcdd875STejun Heo cmd_addr = devm_ioport_map(&dev->dev, cmd, 8);
188cbcdd875STejun Heo ctl_addr = devm_ioport_map(&dev->dev, ctl, 1);
1890d5ff566STejun Heo if (!cmd_addr || !ctl_addr)
1900d5ff566STejun Heo return -ENOMEM;
1910d5ff566STejun Heo
192cbcdd875STejun Heo ata_port_desc(ap, "cmd 0x%x ctl 0x%x", cmd, ctl);
193cbcdd875STejun Heo
194669a5db4SJeff Garzik /* We do our own plumbing to avoid leaking special cases for whacko
195669a5db4SJeff Garzik ancient hardware into the core code. There are two issues to
196669a5db4SJeff Garzik worry about. #1 The chip is a bridge so if in legacy mode and
197669a5db4SJeff Garzik without BARs set fools the setup. #2 If you pci_disable_device
198669a5db4SJeff Garzik the MPIIX your box goes castors up */
199669a5db4SJeff Garzik
2005d728824STejun Heo ap->ops = &mpiix_port_ops;
20114bdef98SErik Inge Bolsø ap->pio_mask = ATA_PIO4;
2025d728824STejun Heo ap->flags |= ATA_FLAG_SLAVE_POSS;
20392ae7849SSergei Shtylyov
2045d728824STejun Heo ap->ioaddr.cmd_addr = cmd_addr;
2055d728824STejun Heo ap->ioaddr.ctl_addr = ctl_addr;
2065d728824STejun Heo ap->ioaddr.altstatus_addr = ctl_addr;
207669a5db4SJeff Garzik
208669a5db4SJeff Garzik /* Let libata fill in the port details */
2099363c382STejun Heo ata_sff_std_ports(&ap->ioaddr);
210669a5db4SJeff Garzik
2115d728824STejun Heo /* activate host */
2129363c382STejun Heo return ata_host_activate(host, irq, ata_sff_interrupt, IRQF_SHARED,
2135d728824STejun Heo &mpiix_sht);
214669a5db4SJeff Garzik }
215669a5db4SJeff Garzik
216669a5db4SJeff Garzik static const struct pci_device_id mpiix[] = {
2172d2744fcSJeff Garzik { PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_82371MX), },
2182d2744fcSJeff Garzik
2192d2744fcSJeff Garzik { },
220669a5db4SJeff Garzik };
221669a5db4SJeff Garzik
222669a5db4SJeff Garzik static struct pci_driver mpiix_pci_driver = {
223669a5db4SJeff Garzik .name = DRV_NAME,
224669a5db4SJeff Garzik .id_table = mpiix,
225669a5db4SJeff Garzik .probe = mpiix_init_one,
22624dc5f33STejun Heo .remove = ata_pci_remove_one,
22758eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
22830ced0f0SAlan .suspend = ata_pci_device_suspend,
22930ced0f0SAlan .resume = ata_pci_device_resume,
230438ac6d5STejun Heo #endif
231669a5db4SJeff Garzik };
232669a5db4SJeff Garzik
2332fc75da0SAxel Lin module_pci_driver(mpiix_pci_driver);
234669a5db4SJeff Garzik
235669a5db4SJeff Garzik MODULE_AUTHOR("Alan Cox");
236669a5db4SJeff Garzik MODULE_DESCRIPTION("low-level driver for Intel MPIIX");
237669a5db4SJeff Garzik MODULE_LICENSE("GPL");
238669a5db4SJeff Garzik MODULE_DEVICE_TABLE(pci, mpiix);
239669a5db4SJeff Garzik MODULE_VERSION(DRV_VERSION);
240