18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * pata_mpiix.c 	- Intel MPIIX PATA for new ATA layer
48c2ecf20Sopenharmony_ci *			  (C) 2005-2006 Red Hat Inc
58c2ecf20Sopenharmony_ci *			  Alan Cox <alan@lxorguk.ukuu.org.uk>
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * The MPIIX is different enough to the PIIX4 and friends that we give it
88c2ecf20Sopenharmony_ci * a separate driver. The old ide/pci code handles this by just not tuning
98c2ecf20Sopenharmony_ci * MPIIX at all.
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The MPIIX also differs in another important way from the majority of PIIX
128c2ecf20Sopenharmony_ci * devices. The chip is a bridge (pardon the pun) between the old world of
138c2ecf20Sopenharmony_ci * ISA IDE and PCI IDE. Although the ATA timings are PCI configured the actual
148c2ecf20Sopenharmony_ci * IDE controller is not decoded in PCI space and the chip does not claim to
158c2ecf20Sopenharmony_ci * be IDE class PCI. This requires slightly non-standard probe logic compared
168c2ecf20Sopenharmony_ci * with PCI IDE and also that we do not disable the device when our driver is
178c2ecf20Sopenharmony_ci * unloaded (as it has many other functions).
188c2ecf20Sopenharmony_ci *
198c2ecf20Sopenharmony_ci * The driver consciously keeps this logic internally to avoid pushing quirky
208c2ecf20Sopenharmony_ci * PATA history into the clean libata layer.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Thinkpad specific note: If you boot an MPIIX using a thinkpad with a PCMCIA
238c2ecf20Sopenharmony_ci * hard disk present this driver will not detect it. This is not a bug. In this
248c2ecf20Sopenharmony_ci * configuration the secondary port of the MPIIX is disabled and the addresses
258c2ecf20Sopenharmony_ci * are decoded by the PCMCIA bridge and therefore are for a generic IDE driver
268c2ecf20Sopenharmony_ci * to operate.
278c2ecf20Sopenharmony_ci */
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#include <linux/kernel.h>
308c2ecf20Sopenharmony_ci#include <linux/module.h>
318c2ecf20Sopenharmony_ci#include <linux/pci.h>
328c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
338c2ecf20Sopenharmony_ci#include <linux/delay.h>
348c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
358c2ecf20Sopenharmony_ci#include <linux/libata.h>
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define DRV_NAME "pata_mpiix"
388c2ecf20Sopenharmony_ci#define DRV_VERSION "0.7.7"
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_cienum {
418c2ecf20Sopenharmony_ci	IDETIM = 0x6C,		/* IDE control register */
428c2ecf20Sopenharmony_ci	IORDY = (1 << 1),
438c2ecf20Sopenharmony_ci	PPE = (1 << 2),
448c2ecf20Sopenharmony_ci	FTIM = (1 << 0),
458c2ecf20Sopenharmony_ci	ENABLED = (1 << 15),
468c2ecf20Sopenharmony_ci	SECONDARY = (1 << 14)
478c2ecf20Sopenharmony_ci};
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_cistatic int mpiix_pre_reset(struct ata_link *link, unsigned long deadline)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	struct ata_port *ap = link->ap;
528c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
538c2ecf20Sopenharmony_ci	static const struct pci_bits mpiix_enable_bits = { 0x6D, 1, 0x80, 0x80 };
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (!pci_test_config_bits(pdev, &mpiix_enable_bits))
568c2ecf20Sopenharmony_ci		return -ENOENT;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	return ata_sff_prereset(link, deadline);
598c2ecf20Sopenharmony_ci}
608c2ecf20Sopenharmony_ci
618c2ecf20Sopenharmony_ci/**
628c2ecf20Sopenharmony_ci *	mpiix_set_piomode	-	set initial PIO mode data
638c2ecf20Sopenharmony_ci *	@ap: ATA interface
648c2ecf20Sopenharmony_ci *	@adev: ATA device
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci *	Called to do the PIO mode setup. The MPIIX allows us to program the
678c2ecf20Sopenharmony_ci *	IORDY sample point (2-5 clocks), recovery (1-4 clocks) and whether
688c2ecf20Sopenharmony_ci *	prefetching or IORDY are used.
698c2ecf20Sopenharmony_ci *
708c2ecf20Sopenharmony_ci *	This would get very ugly because we can only program timing for one
718c2ecf20Sopenharmony_ci *	device at a time, the other gets PIO0. Fortunately libata calls
728c2ecf20Sopenharmony_ci *	our qc_issue command before a command is issued so we can flip the
738c2ecf20Sopenharmony_ci *	timings back and forth to reduce the pain.
748c2ecf20Sopenharmony_ci */
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic void mpiix_set_piomode(struct ata_port *ap, struct ata_device *adev)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	int control = 0;
798c2ecf20Sopenharmony_ci	int pio = adev->pio_mode - XFER_PIO_0;
808c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
818c2ecf20Sopenharmony_ci	u16 idetim;
828c2ecf20Sopenharmony_ci	static const	 /* ISP  RTC */
838c2ecf20Sopenharmony_ci	u8 timings[][2]	= { { 0, 0 },
848c2ecf20Sopenharmony_ci			    { 0, 0 },
858c2ecf20Sopenharmony_ci			    { 1, 0 },
868c2ecf20Sopenharmony_ci			    { 2, 1 },
878c2ecf20Sopenharmony_ci			    { 2, 3 }, };
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	pci_read_config_word(pdev, IDETIM, &idetim);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	/* Mask the IORDY/TIME/PPE for this device */
928c2ecf20Sopenharmony_ci	if (adev->class == ATA_DEV_ATA)
938c2ecf20Sopenharmony_ci		control |= PPE;		/* Enable prefetch/posting for disk */
948c2ecf20Sopenharmony_ci	if (ata_pio_need_iordy(adev))
958c2ecf20Sopenharmony_ci		control |= IORDY;
968c2ecf20Sopenharmony_ci	if (pio > 1)
978c2ecf20Sopenharmony_ci		control |= FTIM;	/* This drive is on the fast timing bank */
988c2ecf20Sopenharmony_ci
998c2ecf20Sopenharmony_ci	/* Mask out timing and clear both TIME bank selects */
1008c2ecf20Sopenharmony_ci	idetim &= 0xCCEE;
1018c2ecf20Sopenharmony_ci	idetim &= ~(0x07  << (4 * adev->devno));
1028c2ecf20Sopenharmony_ci	idetim |= control << (4 * adev->devno);
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	idetim |= (timings[pio][0] << 12) | (timings[pio][1] << 8);
1058c2ecf20Sopenharmony_ci	pci_write_config_word(pdev, IDETIM, idetim);
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci	/* We use ap->private_data as a pointer to the device currently
1088c2ecf20Sopenharmony_ci	   loaded for timing */
1098c2ecf20Sopenharmony_ci	ap->private_data = adev;
1108c2ecf20Sopenharmony_ci}
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/**
1138c2ecf20Sopenharmony_ci *	mpiix_qc_issue		-	command issue
1148c2ecf20Sopenharmony_ci *	@qc: command pending
1158c2ecf20Sopenharmony_ci *
1168c2ecf20Sopenharmony_ci *	Called when the libata layer is about to issue a command. We wrap
1178c2ecf20Sopenharmony_ci *	this interface so that we can load the correct ATA timings if
1188c2ecf20Sopenharmony_ci *	necessary. Our logic also clears TIME0/TIME1 for the other device so
1198c2ecf20Sopenharmony_ci *	that, even if we get this wrong, cycles to the other device will
1208c2ecf20Sopenharmony_ci *	be made PIO0.
1218c2ecf20Sopenharmony_ci */
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_cistatic unsigned int mpiix_qc_issue(struct ata_queued_cmd *qc)
1248c2ecf20Sopenharmony_ci{
1258c2ecf20Sopenharmony_ci	struct ata_port *ap = qc->ap;
1268c2ecf20Sopenharmony_ci	struct ata_device *adev = qc->dev;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	/* If modes have been configured and the channel data is not loaded
1298c2ecf20Sopenharmony_ci	   then load it. We have to check if pio_mode is set as the core code
1308c2ecf20Sopenharmony_ci	   does not set adev->pio_mode to XFER_PIO_0 while probing as would be
1318c2ecf20Sopenharmony_ci	   logical */
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	if (adev->pio_mode && adev != ap->private_data)
1348c2ecf20Sopenharmony_ci		mpiix_set_piomode(ap, adev);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	return ata_sff_qc_issue(qc);
1378c2ecf20Sopenharmony_ci}
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_cistatic struct scsi_host_template mpiix_sht = {
1408c2ecf20Sopenharmony_ci	ATA_PIO_SHT(DRV_NAME),
1418c2ecf20Sopenharmony_ci};
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic struct ata_port_operations mpiix_port_ops = {
1448c2ecf20Sopenharmony_ci	.inherits	= &ata_sff_port_ops,
1458c2ecf20Sopenharmony_ci	.qc_issue	= mpiix_qc_issue,
1468c2ecf20Sopenharmony_ci	.cable_detect	= ata_cable_40wire,
1478c2ecf20Sopenharmony_ci	.set_piomode	= mpiix_set_piomode,
1488c2ecf20Sopenharmony_ci	.prereset	= mpiix_pre_reset,
1498c2ecf20Sopenharmony_ci	.sff_data_xfer	= ata_sff_data_xfer32,
1508c2ecf20Sopenharmony_ci};
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic int mpiix_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	/* Single threaded by the PCI probe logic */
1558c2ecf20Sopenharmony_ci	struct ata_host *host;
1568c2ecf20Sopenharmony_ci	struct ata_port *ap;
1578c2ecf20Sopenharmony_ci	void __iomem *cmd_addr, *ctl_addr;
1588c2ecf20Sopenharmony_ci	u16 idetim;
1598c2ecf20Sopenharmony_ci	int cmd, ctl, irq;
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_ci	ata_print_version_once(&dev->dev, DRV_VERSION);
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_ci	host = ata_host_alloc(&dev->dev, 1);
1648c2ecf20Sopenharmony_ci	if (!host)
1658c2ecf20Sopenharmony_ci		return -ENOMEM;
1668c2ecf20Sopenharmony_ci	ap = host->ports[0];
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_ci	/* MPIIX has many functions which can be turned on or off according
1698c2ecf20Sopenharmony_ci	   to other devices present. Make sure IDE is enabled before we try
1708c2ecf20Sopenharmony_ci	   and use it */
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ci	pci_read_config_word(dev, IDETIM, &idetim);
1738c2ecf20Sopenharmony_ci	if (!(idetim & ENABLED))
1748c2ecf20Sopenharmony_ci		return -ENODEV;
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_ci	/* See if it's primary or secondary channel... */
1778c2ecf20Sopenharmony_ci	if (!(idetim & SECONDARY)) {
1788c2ecf20Sopenharmony_ci		cmd = 0x1F0;
1798c2ecf20Sopenharmony_ci		ctl = 0x3F6;
1808c2ecf20Sopenharmony_ci		irq = 14;
1818c2ecf20Sopenharmony_ci	} else {
1828c2ecf20Sopenharmony_ci		cmd = 0x170;
1838c2ecf20Sopenharmony_ci		ctl = 0x376;
1848c2ecf20Sopenharmony_ci		irq = 15;
1858c2ecf20Sopenharmony_ci	}
1868c2ecf20Sopenharmony_ci
1878c2ecf20Sopenharmony_ci	cmd_addr = devm_ioport_map(&dev->dev, cmd, 8);
1888c2ecf20Sopenharmony_ci	ctl_addr = devm_ioport_map(&dev->dev, ctl, 1);
1898c2ecf20Sopenharmony_ci	if (!cmd_addr || !ctl_addr)
1908c2ecf20Sopenharmony_ci		return -ENOMEM;
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	ata_port_desc(ap, "cmd 0x%x ctl 0x%x", cmd, ctl);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* We do our own plumbing to avoid leaking special cases for whacko
1958c2ecf20Sopenharmony_ci	   ancient hardware into the core code. There are two issues to
1968c2ecf20Sopenharmony_ci	   worry about.  #1 The chip is a bridge so if in legacy mode and
1978c2ecf20Sopenharmony_ci	   without BARs set fools the setup.  #2 If you pci_disable_device
1988c2ecf20Sopenharmony_ci	   the MPIIX your box goes castors up */
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	ap->ops = &mpiix_port_ops;
2018c2ecf20Sopenharmony_ci	ap->pio_mask = ATA_PIO4;
2028c2ecf20Sopenharmony_ci	ap->flags |= ATA_FLAG_SLAVE_POSS;
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	ap->ioaddr.cmd_addr = cmd_addr;
2058c2ecf20Sopenharmony_ci	ap->ioaddr.ctl_addr = ctl_addr;
2068c2ecf20Sopenharmony_ci	ap->ioaddr.altstatus_addr = ctl_addr;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	/* Let libata fill in the port details */
2098c2ecf20Sopenharmony_ci	ata_sff_std_ports(&ap->ioaddr);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ci	/* activate host */
2128c2ecf20Sopenharmony_ci	return ata_host_activate(host, irq, ata_sff_interrupt, IRQF_SHARED,
2138c2ecf20Sopenharmony_ci				 &mpiix_sht);
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic const struct pci_device_id mpiix[] = {
2178c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(INTEL, PCI_DEVICE_ID_INTEL_82371MX), },
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	{ },
2208c2ecf20Sopenharmony_ci};
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_cistatic struct pci_driver mpiix_pci_driver = {
2238c2ecf20Sopenharmony_ci	.name 		= DRV_NAME,
2248c2ecf20Sopenharmony_ci	.id_table	= mpiix,
2258c2ecf20Sopenharmony_ci	.probe 		= mpiix_init_one,
2268c2ecf20Sopenharmony_ci	.remove		= ata_pci_remove_one,
2278c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2288c2ecf20Sopenharmony_ci	.suspend	= ata_pci_device_suspend,
2298c2ecf20Sopenharmony_ci	.resume		= ata_pci_device_resume,
2308c2ecf20Sopenharmony_ci#endif
2318c2ecf20Sopenharmony_ci};
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_cimodule_pci_driver(mpiix_pci_driver);
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alan Cox");
2368c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("low-level driver for Intel MPIIX");
2378c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2388c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, mpiix);
2398c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
240