18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci *  ata_generic.c - Generic PATA/SATA controller driver.
38c2ecf20Sopenharmony_ci *  Copyright 2005 Red Hat Inc, all rights reserved.
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *  Elements from ide/pci/generic.c
68c2ecf20Sopenharmony_ci *	    Copyright (C) 2001-2002	Andre Hedrick <andre@linux-ide.org>
78c2ecf20Sopenharmony_ci *	    Portions (C) Copyright 2002  Red Hat Inc <alan@redhat.com>
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  May be copied or modified under the terms of the GNU General Public License
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci *  Driver for PCI IDE interfaces implementing the standard bus mastering
128c2ecf20Sopenharmony_ci *  interface functionality. This assumes the BIOS did the drive set up and
138c2ecf20Sopenharmony_ci *  tuning for us. By default we do not grab all IDE class devices as they
148c2ecf20Sopenharmony_ci *  may have other drivers or need fixups to avoid problems. Instead we keep
158c2ecf20Sopenharmony_ci *  a default list of stuff without documentation/driver that appears to
168c2ecf20Sopenharmony_ci *  work.
178c2ecf20Sopenharmony_ci */
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci#include <linux/kernel.h>
208c2ecf20Sopenharmony_ci#include <linux/module.h>
218c2ecf20Sopenharmony_ci#include <linux/pci.h>
228c2ecf20Sopenharmony_ci#include <linux/blkdev.h>
238c2ecf20Sopenharmony_ci#include <linux/delay.h>
248c2ecf20Sopenharmony_ci#include <scsi/scsi_host.h>
258c2ecf20Sopenharmony_ci#include <linux/libata.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#define DRV_NAME "ata_generic"
288c2ecf20Sopenharmony_ci#define DRV_VERSION "0.2.15"
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/*
318c2ecf20Sopenharmony_ci *	A generic parallel ATA driver using libata
328c2ecf20Sopenharmony_ci */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_cienum {
358c2ecf20Sopenharmony_ci	ATA_GEN_CLASS_MATCH		= (1 << 0),
368c2ecf20Sopenharmony_ci	ATA_GEN_FORCE_DMA		= (1 << 1),
378c2ecf20Sopenharmony_ci	ATA_GEN_INTEL_IDER		= (1 << 2),
388c2ecf20Sopenharmony_ci};
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci/**
418c2ecf20Sopenharmony_ci *	generic_set_mode	-	mode setting
428c2ecf20Sopenharmony_ci *	@link: link to set up
438c2ecf20Sopenharmony_ci *	@unused: returned device on error
448c2ecf20Sopenharmony_ci *
458c2ecf20Sopenharmony_ci *	Use a non standard set_mode function. We don't want to be tuned.
468c2ecf20Sopenharmony_ci *	The BIOS configured everything. Our job is not to fiddle. We
478c2ecf20Sopenharmony_ci *	read the dma enabled bits from the PCI configuration of the device
488c2ecf20Sopenharmony_ci *	and respect them.
498c2ecf20Sopenharmony_ci */
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistatic int generic_set_mode(struct ata_link *link, struct ata_device **unused)
528c2ecf20Sopenharmony_ci{
538c2ecf20Sopenharmony_ci	struct ata_port *ap = link->ap;
548c2ecf20Sopenharmony_ci	const struct pci_device_id *id = ap->host->private_data;
558c2ecf20Sopenharmony_ci	int dma_enabled = 0;
568c2ecf20Sopenharmony_ci	struct ata_device *dev;
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci	if (id->driver_data & ATA_GEN_FORCE_DMA) {
598c2ecf20Sopenharmony_ci		dma_enabled = 0xff;
608c2ecf20Sopenharmony_ci	} else if (ap->ioaddr.bmdma_addr) {
618c2ecf20Sopenharmony_ci		/* Bits 5 and 6 indicate if DMA is active on master/slave */
628c2ecf20Sopenharmony_ci		dma_enabled = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
638c2ecf20Sopenharmony_ci	}
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci	ata_for_each_dev(dev, link, ENABLED) {
668c2ecf20Sopenharmony_ci		/* We don't really care */
678c2ecf20Sopenharmony_ci		dev->pio_mode = XFER_PIO_0;
688c2ecf20Sopenharmony_ci		dev->dma_mode = XFER_MW_DMA_0;
698c2ecf20Sopenharmony_ci		/* We do need the right mode information for DMA or PIO
708c2ecf20Sopenharmony_ci		   and this comes from the current configuration flags */
718c2ecf20Sopenharmony_ci		if (dma_enabled & (1 << (5 + dev->devno))) {
728c2ecf20Sopenharmony_ci			unsigned int xfer_mask = ata_id_xfermask(dev->id);
738c2ecf20Sopenharmony_ci			const char *name;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci			if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
768c2ecf20Sopenharmony_ci				name = ata_mode_string(xfer_mask);
778c2ecf20Sopenharmony_ci			else {
788c2ecf20Sopenharmony_ci				/* SWDMA perhaps? */
798c2ecf20Sopenharmony_ci				name = "DMA";
808c2ecf20Sopenharmony_ci				xfer_mask |= ata_xfer_mode2mask(XFER_MW_DMA_0);
818c2ecf20Sopenharmony_ci			}
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci			ata_dev_info(dev, "configured for %s\n", name);
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci			dev->xfer_mode = ata_xfer_mask2mode(xfer_mask);
868c2ecf20Sopenharmony_ci			dev->xfer_shift = ata_xfer_mode2shift(dev->xfer_mode);
878c2ecf20Sopenharmony_ci			dev->flags &= ~ATA_DFLAG_PIO;
888c2ecf20Sopenharmony_ci		} else {
898c2ecf20Sopenharmony_ci			ata_dev_info(dev, "configured for PIO\n");
908c2ecf20Sopenharmony_ci			dev->xfer_mode = XFER_PIO_0;
918c2ecf20Sopenharmony_ci			dev->xfer_shift = ATA_SHIFT_PIO;
928c2ecf20Sopenharmony_ci			dev->flags |= ATA_DFLAG_PIO;
938c2ecf20Sopenharmony_ci		}
948c2ecf20Sopenharmony_ci	}
958c2ecf20Sopenharmony_ci	return 0;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic struct scsi_host_template generic_sht = {
998c2ecf20Sopenharmony_ci	ATA_BMDMA_SHT(DRV_NAME),
1008c2ecf20Sopenharmony_ci};
1018c2ecf20Sopenharmony_ci
1028c2ecf20Sopenharmony_cistatic struct ata_port_operations generic_port_ops = {
1038c2ecf20Sopenharmony_ci	.inherits	= &ata_bmdma_port_ops,
1048c2ecf20Sopenharmony_ci	.cable_detect	= ata_cable_unknown,
1058c2ecf20Sopenharmony_ci	.set_mode	= generic_set_mode,
1068c2ecf20Sopenharmony_ci};
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int all_generic_ide;		/* Set to claim all devices */
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci/**
1118c2ecf20Sopenharmony_ci *	is_intel_ider		-	identify intel IDE-R devices
1128c2ecf20Sopenharmony_ci *	@dev: PCI device
1138c2ecf20Sopenharmony_ci *
1148c2ecf20Sopenharmony_ci *	Distinguish Intel IDE-R controller devices from other Intel IDE
1158c2ecf20Sopenharmony_ci *	devices. IDE-R devices have no timing registers and are in
1168c2ecf20Sopenharmony_ci *	most respects virtual. They should be driven by the ata_generic
1178c2ecf20Sopenharmony_ci *	driver.
1188c2ecf20Sopenharmony_ci *
1198c2ecf20Sopenharmony_ci *	IDE-R devices have PCI offset 0xF8.L as zero, later Intel ATA has
1208c2ecf20Sopenharmony_ci *	it non zero. All Intel ATA has 0x40 writable (timing), but it is
1218c2ecf20Sopenharmony_ci *	not writable on IDE-R devices (this is guaranteed).
1228c2ecf20Sopenharmony_ci */
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_cistatic int is_intel_ider(struct pci_dev *dev)
1258c2ecf20Sopenharmony_ci{
1268c2ecf20Sopenharmony_ci	/* For Intel IDE the value at 0xF8 is only zero on IDE-R
1278c2ecf20Sopenharmony_ci	   interfaces */
1288c2ecf20Sopenharmony_ci	u32 r;
1298c2ecf20Sopenharmony_ci	u16 t;
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	/* Check the manufacturing ID, it will be zero for IDE-R */
1328c2ecf20Sopenharmony_ci	pci_read_config_dword(dev, 0xF8, &r);
1338c2ecf20Sopenharmony_ci	/* Not IDE-R: punt so that ata_(old)piix gets it */
1348c2ecf20Sopenharmony_ci	if (r != 0)
1358c2ecf20Sopenharmony_ci		return 0;
1368c2ecf20Sopenharmony_ci	/* 0xF8 will also be zero on some early Intel IDE devices
1378c2ecf20Sopenharmony_ci	   but they will have a sane timing register */
1388c2ecf20Sopenharmony_ci	pci_read_config_word(dev, 0x40, &t);
1398c2ecf20Sopenharmony_ci	if (t != 0)
1408c2ecf20Sopenharmony_ci		return 0;
1418c2ecf20Sopenharmony_ci	/* Finally check if the timing register is writable so that
1428c2ecf20Sopenharmony_ci	   we eliminate any early devices hot-docked in a docking
1438c2ecf20Sopenharmony_ci	   station */
1448c2ecf20Sopenharmony_ci	pci_write_config_word(dev, 0x40, 1);
1458c2ecf20Sopenharmony_ci	pci_read_config_word(dev, 0x40, &t);
1468c2ecf20Sopenharmony_ci	if (t) {
1478c2ecf20Sopenharmony_ci		pci_write_config_word(dev, 0x40, 0);
1488c2ecf20Sopenharmony_ci		return 0;
1498c2ecf20Sopenharmony_ci	}
1508c2ecf20Sopenharmony_ci	return 1;
1518c2ecf20Sopenharmony_ci}
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci/**
1548c2ecf20Sopenharmony_ci *	ata_generic_init		-	attach generic IDE
1558c2ecf20Sopenharmony_ci *	@dev: PCI device found
1568c2ecf20Sopenharmony_ci *	@id: match entry
1578c2ecf20Sopenharmony_ci *
1588c2ecf20Sopenharmony_ci *	Called each time a matching IDE interface is found. We check if the
1598c2ecf20Sopenharmony_ci *	interface is one we wish to claim and if so we perform any chip
1608c2ecf20Sopenharmony_ci *	specific hacks then let the ATA layer do the heavy lifting.
1618c2ecf20Sopenharmony_ci */
1628c2ecf20Sopenharmony_ci
1638c2ecf20Sopenharmony_cistatic int ata_generic_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1648c2ecf20Sopenharmony_ci{
1658c2ecf20Sopenharmony_ci	u16 command;
1668c2ecf20Sopenharmony_ci	static const struct ata_port_info info = {
1678c2ecf20Sopenharmony_ci		.flags = ATA_FLAG_SLAVE_POSS,
1688c2ecf20Sopenharmony_ci		.pio_mask = ATA_PIO4,
1698c2ecf20Sopenharmony_ci		.mwdma_mask = ATA_MWDMA2,
1708c2ecf20Sopenharmony_ci		.udma_mask = ATA_UDMA5,
1718c2ecf20Sopenharmony_ci		.port_ops = &generic_port_ops
1728c2ecf20Sopenharmony_ci	};
1738c2ecf20Sopenharmony_ci	const struct ata_port_info *ppi[] = { &info, NULL };
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Don't use the generic entry unless instructed to do so */
1768c2ecf20Sopenharmony_ci	if ((id->driver_data & ATA_GEN_CLASS_MATCH) && all_generic_ide == 0)
1778c2ecf20Sopenharmony_ci		return -ENODEV;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	if ((id->driver_data & ATA_GEN_INTEL_IDER) && !all_generic_ide)
1808c2ecf20Sopenharmony_ci		if (!is_intel_ider(dev))
1818c2ecf20Sopenharmony_ci			return -ENODEV;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/* Devices that need care */
1848c2ecf20Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_UMC &&
1858c2ecf20Sopenharmony_ci	    dev->device == PCI_DEVICE_ID_UMC_UM8886A &&
1868c2ecf20Sopenharmony_ci	    (!(PCI_FUNC(dev->devfn) & 1)))
1878c2ecf20Sopenharmony_ci		return -ENODEV;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_OPTI &&
1908c2ecf20Sopenharmony_ci	    dev->device == PCI_DEVICE_ID_OPTI_82C558 &&
1918c2ecf20Sopenharmony_ci	    (!(PCI_FUNC(dev->devfn) & 1)))
1928c2ecf20Sopenharmony_ci		return -ENODEV;
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	/* Don't re-enable devices in generic mode or we will break some
1958c2ecf20Sopenharmony_ci	   motherboards with disabled and unused IDE controllers */
1968c2ecf20Sopenharmony_ci	pci_read_config_word(dev, PCI_COMMAND, &command);
1978c2ecf20Sopenharmony_ci	if (!(command & PCI_COMMAND_IO))
1988c2ecf20Sopenharmony_ci		return -ENODEV;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_AL)
2018c2ecf20Sopenharmony_ci		ata_pci_bmdma_clear_simplex(dev);
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_ATI) {
2048c2ecf20Sopenharmony_ci		int rc = pcim_enable_device(dev);
2058c2ecf20Sopenharmony_ci		if (rc < 0)
2068c2ecf20Sopenharmony_ci			return rc;
2078c2ecf20Sopenharmony_ci		pcim_pin_device(dev);
2088c2ecf20Sopenharmony_ci	}
2098c2ecf20Sopenharmony_ci	return ata_pci_bmdma_init_one(dev, ppi, &generic_sht, (void *)id, 0);
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic struct pci_device_id ata_generic[] = {
2138c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_SAMURAI_IDE), },
2148c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HOLTEK, PCI_DEVICE_ID_HOLTEK_6565), },
2158c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8673F), },
2168c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8886A), },
2178c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8886BF), },
2188c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HINT,   PCI_DEVICE_ID_HINT_VXPROII_IDE), },
2198c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_VIA,    PCI_DEVICE_ID_VIA_82C561), },
2208c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_OPTI,   PCI_DEVICE_ID_OPTI_82C558), },
2218c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_CENATEK,PCI_DEVICE_ID_CENATEK_IDE),
2228c2ecf20Sopenharmony_ci	  .driver_data = ATA_GEN_FORCE_DMA },
2238c2ecf20Sopenharmony_ci#if !defined(CONFIG_PATA_TOSHIBA) && !defined(CONFIG_PATA_TOSHIBA_MODULE)
2248c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_1), },
2258c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_2),  },
2268c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_3),  },
2278c2ecf20Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_5),  },
2288c2ecf20Sopenharmony_ci#endif
2298c2ecf20Sopenharmony_ci	/* Intel, IDE class device */
2308c2ecf20Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
2318c2ecf20Sopenharmony_ci	  PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL,
2328c2ecf20Sopenharmony_ci	  .driver_data = ATA_GEN_INTEL_IDER },
2338c2ecf20Sopenharmony_ci	/* Must come last. If you add entries adjust this table appropriately */
2348c2ecf20Sopenharmony_ci	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL),
2358c2ecf20Sopenharmony_ci	  .driver_data = ATA_GEN_CLASS_MATCH },
2368c2ecf20Sopenharmony_ci	{ 0, },
2378c2ecf20Sopenharmony_ci};
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_cistatic struct pci_driver ata_generic_pci_driver = {
2408c2ecf20Sopenharmony_ci	.name 		= DRV_NAME,
2418c2ecf20Sopenharmony_ci	.id_table	= ata_generic,
2428c2ecf20Sopenharmony_ci	.probe 		= ata_generic_init_one,
2438c2ecf20Sopenharmony_ci	.remove		= ata_pci_remove_one,
2448c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
2458c2ecf20Sopenharmony_ci	.suspend	= ata_pci_device_suspend,
2468c2ecf20Sopenharmony_ci	.resume		= ata_pci_device_resume,
2478c2ecf20Sopenharmony_ci#endif
2488c2ecf20Sopenharmony_ci};
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_cimodule_pci_driver(ata_generic_pci_driver);
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ciMODULE_AUTHOR("Alan Cox");
2538c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("low-level driver for generic ATA");
2548c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
2558c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, ata_generic);
2568c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cimodule_param(all_generic_ide, int, 0);
259