18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *    pata_oldpiix.c - Intel PATA/SATA controllers
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *	(C) 2005 Red Hat
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *    Some parts based on ata_piix.c by Jeff Garzik and others.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *    Early PIIX differs significantly from the later PIIX as it lacks
108c2ecf20Sopenharmony_ci *    SITRE and the slave timing registers. This means that you have to
118c2ecf20Sopenharmony_ci *    set timing per channel, or be clever. Libata tells us whenever it
128c2ecf20Sopenharmony_ci *    does drive selection and we use this to reload the timings.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci *    Because of these behaviour differences PIIX gets its own driver module.
158c2ecf20Sopenharmony_ci */
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <linux/kernel.h>
188c2ecf20Sopenharmony_ci#include <linux/module.h>
198c2ecf20Sopenharmony_ci#include <linux/pci.h>
208c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
218c2ecf20Sopenharmony_ci#include <linux/delay.h>
228c2ecf20Sopenharmony_ci#include <linux/device.h>
238c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
248c2ecf20Sopenharmony_ci#include <linux/libata.h>
258c2ecf20Sopenharmony_ci#include <linux/ata.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define DRV_NAME	"pata_oldpiix"
288c2ecf20Sopenharmony_ci#define DRV_VERSION	"0.5.5"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/**
318c2ecf20Sopenharmony_ci *	oldpiix_pre_reset		-	probe begin
328c2ecf20Sopenharmony_ci *	@link: ATA link
338c2ecf20Sopenharmony_ci *	@deadline: deadline jiffies for the operation
348c2ecf20Sopenharmony_ci *
358c2ecf20Sopenharmony_ci *	Set up cable type and use generic probe init
368c2ecf20Sopenharmony_ci */
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistatic int oldpiix_pre_reset(struct ata_link *link, unsigned long deadline)
398c2ecf20Sopenharmony_ci{
408c2ecf20Sopenharmony_ci	struct ata_port *ap = link->ap;
418c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
428c2ecf20Sopenharmony_ci	static const struct pci_bits oldpiix_enable_bits[] = {
438c2ecf20Sopenharmony_ci		{ 0x41U, 1U, 0x80UL, 0x80UL },	/* port 0 */
448c2ecf20Sopenharmony_ci		{ 0x43U, 1U, 0x80UL, 0x80UL },	/* port 1 */
458c2ecf20Sopenharmony_ci	};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ci	if (!pci_test_config_bits(pdev, &oldpiix_enable_bits[ap->port_no]))
488c2ecf20Sopenharmony_ci		return -ENOENT;
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci	return ata_sff_prereset(link, deadline);
518c2ecf20Sopenharmony_ci}
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/**
548c2ecf20Sopenharmony_ci *	oldpiix_set_piomode - Initialize host controller PATA PIO timings
558c2ecf20Sopenharmony_ci *	@ap: Port whose timings we are configuring
568c2ecf20Sopenharmony_ci *	@adev: Device whose timings we are configuring
578c2ecf20Sopenharmony_ci *
588c2ecf20Sopenharmony_ci *	Set PIO mode for device, in host controller PCI config space.
598c2ecf20Sopenharmony_ci *
608c2ecf20Sopenharmony_ci *	LOCKING:
618c2ecf20Sopenharmony_ci *	None (inherited from caller).
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic void oldpiix_set_piomode (struct ata_port *ap, struct ata_device *adev)
658c2ecf20Sopenharmony_ci{
668c2ecf20Sopenharmony_ci	unsigned int pio	= adev->pio_mode - XFER_PIO_0;
678c2ecf20Sopenharmony_ci	struct pci_dev *dev	= to_pci_dev(ap->host->dev);
688c2ecf20Sopenharmony_ci	unsigned int idetm_port= ap->port_no ? 0x42 : 0x40;
698c2ecf20Sopenharmony_ci	u16 idetm_data;
708c2ecf20Sopenharmony_ci	int control = 0;
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci	/*
738c2ecf20Sopenharmony_ci	 *	See Intel Document 298600-004 for the timing programing rules
748c2ecf20Sopenharmony_ci	 *	for PIIX/ICH. Note that the early PIIX does not have the slave
758c2ecf20Sopenharmony_ci	 *	timing port at 0x44.
768c2ecf20Sopenharmony_ci	 */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	static const	 /* ISP  RTC */
798c2ecf20Sopenharmony_ci	u8 timings[][2]	= { { 0, 0 },
808c2ecf20Sopenharmony_ci			    { 0, 0 },
818c2ecf20Sopenharmony_ci			    { 1, 0 },
828c2ecf20Sopenharmony_ci			    { 2, 1 },
838c2ecf20Sopenharmony_ci			    { 2, 3 }, };
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	if (pio > 1)
868c2ecf20Sopenharmony_ci		control |= 1;	/* TIME */
878c2ecf20Sopenharmony_ci	if (ata_pio_need_iordy(adev))
888c2ecf20Sopenharmony_ci		control |= 2;	/* IE */
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci	/* Intel specifies that the prefetch/posting is for disk only */
918c2ecf20Sopenharmony_ci	if (adev->class == ATA_DEV_ATA)
928c2ecf20Sopenharmony_ci		control |= 4;	/* PPE */
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	pci_read_config_word(dev, idetm_port, &idetm_data);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	/*
978c2ecf20Sopenharmony_ci	 * Set PPE, IE and TIME as appropriate.
988c2ecf20Sopenharmony_ci	 * Clear the other drive's timing bits.
998c2ecf20Sopenharmony_ci	 */
1008c2ecf20Sopenharmony_ci	if (adev->devno == 0) {
1018c2ecf20Sopenharmony_ci		idetm_data &= 0xCCE0;
1028c2ecf20Sopenharmony_ci		idetm_data |= control;
1038c2ecf20Sopenharmony_ci	} else {
1048c2ecf20Sopenharmony_ci		idetm_data &= 0xCC0E;
1058c2ecf20Sopenharmony_ci		idetm_data |= (control << 4);
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci	idetm_data |= (timings[pio][0] << 12) |
1088c2ecf20Sopenharmony_ci			(timings[pio][1] << 8);
1098c2ecf20Sopenharmony_ci	pci_write_config_word(dev, idetm_port, idetm_data);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* Track which port is configured */
1128c2ecf20Sopenharmony_ci	ap->private_data = adev;
1138c2ecf20Sopenharmony_ci}
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/**
1168c2ecf20Sopenharmony_ci *	oldpiix_set_dmamode - Initialize host controller PATA DMA timings
1178c2ecf20Sopenharmony_ci *	@ap: Port whose timings we are configuring
1188c2ecf20Sopenharmony_ci *	@adev: Device to program
1198c2ecf20Sopenharmony_ci *
1208c2ecf20Sopenharmony_ci *	Set MWDMA mode for device, in host controller PCI config space.
1218c2ecf20Sopenharmony_ci *
1228c2ecf20Sopenharmony_ci *	LOCKING:
1238c2ecf20Sopenharmony_ci *	None (inherited from caller).
1248c2ecf20Sopenharmony_ci */
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_cistatic void oldpiix_set_dmamode (struct ata_port *ap, struct ata_device *adev)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	struct pci_dev *dev	= to_pci_dev(ap->host->dev);
1298c2ecf20Sopenharmony_ci	u8 idetm_port		= ap->port_no ? 0x42 : 0x40;
1308c2ecf20Sopenharmony_ci	u16 idetm_data;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	static const	 /* ISP  RTC */
1338c2ecf20Sopenharmony_ci	u8 timings[][2]	= { { 0, 0 },
1348c2ecf20Sopenharmony_ci			    { 0, 0 },
1358c2ecf20Sopenharmony_ci			    { 1, 0 },
1368c2ecf20Sopenharmony_ci			    { 2, 1 },
1378c2ecf20Sopenharmony_ci			    { 2, 3 }, };
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	/*
1408c2ecf20Sopenharmony_ci	 * MWDMA is driven by the PIO timings. We must also enable
1418c2ecf20Sopenharmony_ci	 * IORDY unconditionally along with TIME1. PPE has already
1428c2ecf20Sopenharmony_ci	 * been set when the PIO timing was set.
1438c2ecf20Sopenharmony_ci	 */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	unsigned int mwdma	= adev->dma_mode - XFER_MW_DMA_0;
1468c2ecf20Sopenharmony_ci	unsigned int control;
1478c2ecf20Sopenharmony_ci	const unsigned int needed_pio[3] = {
1488c2ecf20Sopenharmony_ci		XFER_PIO_0, XFER_PIO_3, XFER_PIO_4
1498c2ecf20Sopenharmony_ci	};
1508c2ecf20Sopenharmony_ci	int pio = needed_pio[mwdma] - XFER_PIO_0;
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	pci_read_config_word(dev, idetm_port, &idetm_data);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	control = 3;	/* IORDY|TIME0 */
1558c2ecf20Sopenharmony_ci	/* Intel specifies that the PPE functionality is for disk only */
1568c2ecf20Sopenharmony_ci	if (adev->class == ATA_DEV_ATA)
1578c2ecf20Sopenharmony_ci		control |= 4;	/* PPE enable */
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	/* If the drive MWDMA is faster than it can do PIO then
1608c2ecf20Sopenharmony_ci	   we must force PIO into PIO0 */
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (adev->pio_mode < needed_pio[mwdma])
1638c2ecf20Sopenharmony_ci		/* Enable DMA timing only */
1648c2ecf20Sopenharmony_ci		control |= 8;	/* PIO cycles in PIO0 */
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	/* Mask out the relevant control and timing bits we will load. Also
1678c2ecf20Sopenharmony_ci	   clear the other drive TIME register as a precaution */
1688c2ecf20Sopenharmony_ci	if (adev->devno == 0) {
1698c2ecf20Sopenharmony_ci		idetm_data &= 0xCCE0;
1708c2ecf20Sopenharmony_ci		idetm_data |= control;
1718c2ecf20Sopenharmony_ci	} else {
1728c2ecf20Sopenharmony_ci		idetm_data &= 0xCC0E;
1738c2ecf20Sopenharmony_ci		idetm_data |= (control << 4);
1748c2ecf20Sopenharmony_ci	}
1758c2ecf20Sopenharmony_ci	idetm_data |= (timings[pio][0] << 12) | (timings[pio][1] << 8);
1768c2ecf20Sopenharmony_ci	pci_write_config_word(dev, idetm_port, idetm_data);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	/* Track which port is configured */
1798c2ecf20Sopenharmony_ci	ap->private_data = adev;
1808c2ecf20Sopenharmony_ci}
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci/**
1838c2ecf20Sopenharmony_ci *	oldpiix_qc_issue	-	command issue
1848c2ecf20Sopenharmony_ci *	@qc: command pending
1858c2ecf20Sopenharmony_ci *
1868c2ecf20Sopenharmony_ci *	Called when the libata layer is about to issue a command. We wrap
1878c2ecf20Sopenharmony_ci *	this interface so that we can load the correct ATA timings if
1888c2ecf20Sopenharmony_ci *	necessary. Our logic also clears TIME0/TIME1 for the other device so
1898c2ecf20Sopenharmony_ci *	that, even if we get this wrong, cycles to the other device will
1908c2ecf20Sopenharmony_ci *	be made PIO0.
1918c2ecf20Sopenharmony_ci */
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic unsigned int oldpiix_qc_issue(struct ata_queued_cmd *qc)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	struct ata_port *ap = qc->ap;
1968c2ecf20Sopenharmony_ci	struct ata_device *adev = qc->dev;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (adev != ap->private_data) {
1998c2ecf20Sopenharmony_ci		oldpiix_set_piomode(ap, adev);
2008c2ecf20Sopenharmony_ci		if (ata_dma_enabled(adev))
2018c2ecf20Sopenharmony_ci			oldpiix_set_dmamode(ap, adev);
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci	return ata_bmdma_qc_issue(qc);
2048c2ecf20Sopenharmony_ci}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_cistatic struct scsi_host_template oldpiix_sht = {
2088c2ecf20Sopenharmony_ci	ATA_BMDMA_SHT(DRV_NAME),
2098c2ecf20Sopenharmony_ci};
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_cistatic struct ata_port_operations oldpiix_pata_ops = {
2128c2ecf20Sopenharmony_ci	.inherits		= &ata_bmdma_port_ops,
2138c2ecf20Sopenharmony_ci	.qc_issue		= oldpiix_qc_issue,
2148c2ecf20Sopenharmony_ci	.cable_detect		= ata_cable_40wire,
2158c2ecf20Sopenharmony_ci	.set_piomode		= oldpiix_set_piomode,
2168c2ecf20Sopenharmony_ci	.set_dmamode		= oldpiix_set_dmamode,
2178c2ecf20Sopenharmony_ci	.prereset		= oldpiix_pre_reset,
2188c2ecf20Sopenharmony_ci};
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci/**
2228c2ecf20Sopenharmony_ci *	oldpiix_init_one - Register PIIX ATA PCI device with kernel services
2238c2ecf20Sopenharmony_ci *	@pdev: PCI device to register
2248c2ecf20Sopenharmony_ci *	@ent: Entry in oldpiix_pci_tbl matching with @pdev
2258c2ecf20Sopenharmony_ci *
2268c2ecf20Sopenharmony_ci *	Called from kernel PCI layer.  We probe for combined mode (sigh),
2278c2ecf20Sopenharmony_ci *	and then hand over control to libata, for it to do the rest.
2288c2ecf20Sopenharmony_ci *
2298c2ecf20Sopenharmony_ci *	LOCKING:
2308c2ecf20Sopenharmony_ci *	Inherited from PCI layer (may sleep).
2318c2ecf20Sopenharmony_ci *
2328c2ecf20Sopenharmony_ci *	RETURNS:
2338c2ecf20Sopenharmony_ci *	Zero on success, or -ERRNO value.
2348c2ecf20Sopenharmony_ci */
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic int oldpiix_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
2378c2ecf20Sopenharmony_ci{
2388c2ecf20Sopenharmony_ci	static const struct ata_port_info info = {
2398c2ecf20Sopenharmony_ci		.flags		= ATA_FLAG_SLAVE_POSS,
2408c2ecf20Sopenharmony_ci		.pio_mask	= ATA_PIO4,
2418c2ecf20Sopenharmony_ci		.mwdma_mask	= ATA_MWDMA12_ONLY,
2428c2ecf20Sopenharmony_ci		.port_ops	= &oldpiix_pata_ops,
2438c2ecf20Sopenharmony_ci	};
2448c2ecf20Sopenharmony_ci	const struct ata_port_info *ppi[] = { &info, NULL };
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	ata_print_version_once(&pdev->dev, DRV_VERSION);
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	return ata_pci_bmdma_init_one(pdev, ppi, &oldpiix_sht, NULL, 0);
2498c2ecf20Sopenharmony_ci}
2508c2ecf20Sopenharmony_ci
2518c2ecf20Sopenharmony_cistatic const struct pci_device_id oldpiix_pci_tbl[] = {
2528c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(INTEL, 0x1230), },
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	{ }	/* terminate list */
2558c2ecf20Sopenharmony_ci};
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_cistatic struct pci_driver oldpiix_pci_driver = {
2588c2ecf20Sopenharmony_ci	.name			= DRV_NAME,
2598c2ecf20Sopenharmony_ci	.id_table		= oldpiix_pci_tbl,
2608c2ecf20Sopenharmony_ci	.probe			= oldpiix_init_one,
2618c2ecf20Sopenharmony_ci	.remove			= ata_pci_remove_one,
2628c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2638c2ecf20Sopenharmony_ci	.suspend		= ata_pci_device_suspend,
2648c2ecf20Sopenharmony_ci	.resume			= ata_pci_device_resume,
2658c2ecf20Sopenharmony_ci#endif
2668c2ecf20Sopenharmony_ci};
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cimodule_pci_driver(oldpiix_pci_driver);
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alan Cox");
2718c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("SCSI low-level driver for early PIIX series controllers");
2728c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2738c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, oldpiix_pci_tbl);
2748c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
275