162306a36Sopenharmony_ci/*
262306a36Sopenharmony_ci *  ata_generic.c - Generic PATA/SATA controller driver.
362306a36Sopenharmony_ci *  Copyright 2005 Red Hat Inc, all rights reserved.
462306a36Sopenharmony_ci *
562306a36Sopenharmony_ci *  Elements from ide/pci/generic.c
662306a36Sopenharmony_ci *	    Copyright (C) 2001-2002	Andre Hedrick <andre@linux-ide.org>
762306a36Sopenharmony_ci *	    Portions (C) Copyright 2002  Red Hat Inc <alan@redhat.com>
862306a36Sopenharmony_ci *
962306a36Sopenharmony_ci *  May be copied or modified under the terms of the GNU General Public License
1062306a36Sopenharmony_ci *
1162306a36Sopenharmony_ci *  Driver for PCI IDE interfaces implementing the standard bus mastering
1262306a36Sopenharmony_ci *  interface functionality. This assumes the BIOS did the drive set up and
1362306a36Sopenharmony_ci *  tuning for us. By default we do not grab all IDE class devices as they
1462306a36Sopenharmony_ci *  may have other drivers or need fixups to avoid problems. Instead we keep
1562306a36Sopenharmony_ci *  a default list of stuff without documentation/driver that appears to
1662306a36Sopenharmony_ci *  work.
1762306a36Sopenharmony_ci */
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include <linux/kernel.h>
2062306a36Sopenharmony_ci#include <linux/module.h>
2162306a36Sopenharmony_ci#include <linux/pci.h>
2262306a36Sopenharmony_ci#include <linux/blkdev.h>
2362306a36Sopenharmony_ci#include <linux/delay.h>
2462306a36Sopenharmony_ci#include <scsi/scsi_host.h>
2562306a36Sopenharmony_ci#include <linux/libata.h>
2662306a36Sopenharmony_ci
2762306a36Sopenharmony_ci#define DRV_NAME "ata_generic"
2862306a36Sopenharmony_ci#define DRV_VERSION "0.2.15"
2962306a36Sopenharmony_ci
3062306a36Sopenharmony_ci/*
3162306a36Sopenharmony_ci *	A generic parallel ATA driver using libata
3262306a36Sopenharmony_ci */
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_cienum {
3562306a36Sopenharmony_ci	ATA_GEN_CLASS_MATCH		= (1 << 0),
3662306a36Sopenharmony_ci	ATA_GEN_FORCE_DMA		= (1 << 1),
3762306a36Sopenharmony_ci	ATA_GEN_INTEL_IDER		= (1 << 2),
3862306a36Sopenharmony_ci};
3962306a36Sopenharmony_ci
4062306a36Sopenharmony_ci/**
4162306a36Sopenharmony_ci *	generic_set_mode	-	mode setting
4262306a36Sopenharmony_ci *	@link: link to set up
4362306a36Sopenharmony_ci *	@unused: returned device on error
4462306a36Sopenharmony_ci *
4562306a36Sopenharmony_ci *	Use a non standard set_mode function. We don't want to be tuned.
4662306a36Sopenharmony_ci *	The BIOS configured everything. Our job is not to fiddle. We
4762306a36Sopenharmony_ci *	read the dma enabled bits from the PCI configuration of the device
4862306a36Sopenharmony_ci *	and respect them.
4962306a36Sopenharmony_ci */
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_cistatic int generic_set_mode(struct ata_link *link, struct ata_device **unused)
5262306a36Sopenharmony_ci{
5362306a36Sopenharmony_ci	struct ata_port *ap = link->ap;
5462306a36Sopenharmony_ci	const struct pci_device_id *id = ap->host->private_data;
5562306a36Sopenharmony_ci	int dma_enabled = 0;
5662306a36Sopenharmony_ci	struct ata_device *dev;
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci	if (id->driver_data & ATA_GEN_FORCE_DMA) {
5962306a36Sopenharmony_ci		dma_enabled = 0xff;
6062306a36Sopenharmony_ci	} else if (ap->ioaddr.bmdma_addr) {
6162306a36Sopenharmony_ci		/* Bits 5 and 6 indicate if DMA is active on master/slave */
6262306a36Sopenharmony_ci		dma_enabled = ioread8(ap->ioaddr.bmdma_addr + ATA_DMA_STATUS);
6362306a36Sopenharmony_ci	}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_ci	ata_for_each_dev(dev, link, ENABLED) {
6662306a36Sopenharmony_ci		/* We don't really care */
6762306a36Sopenharmony_ci		dev->pio_mode = XFER_PIO_0;
6862306a36Sopenharmony_ci		dev->dma_mode = XFER_MW_DMA_0;
6962306a36Sopenharmony_ci		/* We do need the right mode information for DMA or PIO
7062306a36Sopenharmony_ci		   and this comes from the current configuration flags */
7162306a36Sopenharmony_ci		if (dma_enabled & (1 << (5 + dev->devno))) {
7262306a36Sopenharmony_ci			unsigned int xfer_mask = ata_id_xfermask(dev->id);
7362306a36Sopenharmony_ci			const char *name;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci			if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
7662306a36Sopenharmony_ci				name = ata_mode_string(xfer_mask);
7762306a36Sopenharmony_ci			else {
7862306a36Sopenharmony_ci				/* SWDMA perhaps? */
7962306a36Sopenharmony_ci				name = "DMA";
8062306a36Sopenharmony_ci				xfer_mask |= ata_xfer_mode2mask(XFER_MW_DMA_0);
8162306a36Sopenharmony_ci			}
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci			ata_dev_info(dev, "configured for %s\n", name);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci			dev->xfer_mode = ata_xfer_mask2mode(xfer_mask);
8662306a36Sopenharmony_ci			dev->xfer_shift = ata_xfer_mode2shift(dev->xfer_mode);
8762306a36Sopenharmony_ci			dev->flags &= ~ATA_DFLAG_PIO;
8862306a36Sopenharmony_ci		} else {
8962306a36Sopenharmony_ci			ata_dev_info(dev, "configured for PIO\n");
9062306a36Sopenharmony_ci			dev->xfer_mode = XFER_PIO_0;
9162306a36Sopenharmony_ci			dev->xfer_shift = ATA_SHIFT_PIO;
9262306a36Sopenharmony_ci			dev->flags |= ATA_DFLAG_PIO;
9362306a36Sopenharmony_ci		}
9462306a36Sopenharmony_ci	}
9562306a36Sopenharmony_ci	return 0;
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic const struct scsi_host_template generic_sht = {
9962306a36Sopenharmony_ci	ATA_BMDMA_SHT(DRV_NAME),
10062306a36Sopenharmony_ci};
10162306a36Sopenharmony_ci
10262306a36Sopenharmony_cistatic struct ata_port_operations generic_port_ops = {
10362306a36Sopenharmony_ci	.inherits	= &ata_bmdma_port_ops,
10462306a36Sopenharmony_ci	.cable_detect	= ata_cable_unknown,
10562306a36Sopenharmony_ci	.set_mode	= generic_set_mode,
10662306a36Sopenharmony_ci};
10762306a36Sopenharmony_ci
10862306a36Sopenharmony_cistatic int all_generic_ide;		/* Set to claim all devices */
10962306a36Sopenharmony_ci
11062306a36Sopenharmony_ci/**
11162306a36Sopenharmony_ci *	is_intel_ider		-	identify intel IDE-R devices
11262306a36Sopenharmony_ci *	@dev: PCI device
11362306a36Sopenharmony_ci *
11462306a36Sopenharmony_ci *	Distinguish Intel IDE-R controller devices from other Intel IDE
11562306a36Sopenharmony_ci *	devices. IDE-R devices have no timing registers and are in
11662306a36Sopenharmony_ci *	most respects virtual. They should be driven by the ata_generic
11762306a36Sopenharmony_ci *	driver.
11862306a36Sopenharmony_ci *
11962306a36Sopenharmony_ci *	IDE-R devices have PCI offset 0xF8.L as zero, later Intel ATA has
12062306a36Sopenharmony_ci *	it non zero. All Intel ATA has 0x40 writable (timing), but it is
12162306a36Sopenharmony_ci *	not writable on IDE-R devices (this is guaranteed).
12262306a36Sopenharmony_ci */
12362306a36Sopenharmony_ci
12462306a36Sopenharmony_cistatic int is_intel_ider(struct pci_dev *dev)
12562306a36Sopenharmony_ci{
12662306a36Sopenharmony_ci	/* For Intel IDE the value at 0xF8 is only zero on IDE-R
12762306a36Sopenharmony_ci	   interfaces */
12862306a36Sopenharmony_ci	u32 r;
12962306a36Sopenharmony_ci	u16 t;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	/* Check the manufacturing ID, it will be zero for IDE-R */
13262306a36Sopenharmony_ci	pci_read_config_dword(dev, 0xF8, &r);
13362306a36Sopenharmony_ci	/* Not IDE-R: punt so that ata_(old)piix gets it */
13462306a36Sopenharmony_ci	if (r != 0)
13562306a36Sopenharmony_ci		return 0;
13662306a36Sopenharmony_ci	/* 0xF8 will also be zero on some early Intel IDE devices
13762306a36Sopenharmony_ci	   but they will have a sane timing register */
13862306a36Sopenharmony_ci	pci_read_config_word(dev, 0x40, &t);
13962306a36Sopenharmony_ci	if (t != 0)
14062306a36Sopenharmony_ci		return 0;
14162306a36Sopenharmony_ci	/* Finally check if the timing register is writable so that
14262306a36Sopenharmony_ci	   we eliminate any early devices hot-docked in a docking
14362306a36Sopenharmony_ci	   station */
14462306a36Sopenharmony_ci	pci_write_config_word(dev, 0x40, 1);
14562306a36Sopenharmony_ci	pci_read_config_word(dev, 0x40, &t);
14662306a36Sopenharmony_ci	if (t) {
14762306a36Sopenharmony_ci		pci_write_config_word(dev, 0x40, 0);
14862306a36Sopenharmony_ci		return 0;
14962306a36Sopenharmony_ci	}
15062306a36Sopenharmony_ci	return 1;
15162306a36Sopenharmony_ci}
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci/**
15462306a36Sopenharmony_ci *	ata_generic_init_one		-	attach generic IDE
15562306a36Sopenharmony_ci *	@dev: PCI device found
15662306a36Sopenharmony_ci *	@id: match entry
15762306a36Sopenharmony_ci *
15862306a36Sopenharmony_ci *	Called each time a matching IDE interface is found. We check if the
15962306a36Sopenharmony_ci *	interface is one we wish to claim and if so we perform any chip
16062306a36Sopenharmony_ci *	specific hacks then let the ATA layer do the heavy lifting.
16162306a36Sopenharmony_ci */
16262306a36Sopenharmony_ci
16362306a36Sopenharmony_cistatic int ata_generic_init_one(struct pci_dev *dev, const struct pci_device_id *id)
16462306a36Sopenharmony_ci{
16562306a36Sopenharmony_ci	u16 command;
16662306a36Sopenharmony_ci	static const struct ata_port_info info = {
16762306a36Sopenharmony_ci		.flags = ATA_FLAG_SLAVE_POSS,
16862306a36Sopenharmony_ci		.pio_mask = ATA_PIO4,
16962306a36Sopenharmony_ci		.mwdma_mask = ATA_MWDMA2,
17062306a36Sopenharmony_ci		.udma_mask = ATA_UDMA5,
17162306a36Sopenharmony_ci		.port_ops = &generic_port_ops
17262306a36Sopenharmony_ci	};
17362306a36Sopenharmony_ci	const struct ata_port_info *ppi[] = { &info, NULL };
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	/* Don't use the generic entry unless instructed to do so */
17662306a36Sopenharmony_ci	if ((id->driver_data & ATA_GEN_CLASS_MATCH) && all_generic_ide == 0)
17762306a36Sopenharmony_ci		return -ENODEV;
17862306a36Sopenharmony_ci
17962306a36Sopenharmony_ci	if ((id->driver_data & ATA_GEN_INTEL_IDER) && !all_generic_ide)
18062306a36Sopenharmony_ci		if (!is_intel_ider(dev))
18162306a36Sopenharmony_ci			return -ENODEV;
18262306a36Sopenharmony_ci
18362306a36Sopenharmony_ci	/* Devices that need care */
18462306a36Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_UMC &&
18562306a36Sopenharmony_ci	    dev->device == PCI_DEVICE_ID_UMC_UM8886A &&
18662306a36Sopenharmony_ci	    (!(PCI_FUNC(dev->devfn) & 1)))
18762306a36Sopenharmony_ci		return -ENODEV;
18862306a36Sopenharmony_ci
18962306a36Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_OPTI &&
19062306a36Sopenharmony_ci	    dev->device == PCI_DEVICE_ID_OPTI_82C558 &&
19162306a36Sopenharmony_ci	    (!(PCI_FUNC(dev->devfn) & 1)))
19262306a36Sopenharmony_ci		return -ENODEV;
19362306a36Sopenharmony_ci
19462306a36Sopenharmony_ci	/* Don't re-enable devices in generic mode or we will break some
19562306a36Sopenharmony_ci	   motherboards with disabled and unused IDE controllers */
19662306a36Sopenharmony_ci	pci_read_config_word(dev, PCI_COMMAND, &command);
19762306a36Sopenharmony_ci	if (!(command & PCI_COMMAND_IO))
19862306a36Sopenharmony_ci		return -ENODEV;
19962306a36Sopenharmony_ci
20062306a36Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_AL)
20162306a36Sopenharmony_ci		ata_pci_bmdma_clear_simplex(dev);
20262306a36Sopenharmony_ci
20362306a36Sopenharmony_ci	if (dev->vendor == PCI_VENDOR_ID_ATI) {
20462306a36Sopenharmony_ci		int rc = pcim_enable_device(dev);
20562306a36Sopenharmony_ci		if (rc < 0)
20662306a36Sopenharmony_ci			return rc;
20762306a36Sopenharmony_ci		pcim_pin_device(dev);
20862306a36Sopenharmony_ci	}
20962306a36Sopenharmony_ci	return ata_pci_bmdma_init_one(dev, ppi, &generic_sht, (void *)id, 0);
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic struct pci_device_id ata_generic[] = {
21362306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_PCTECH, PCI_DEVICE_ID_PCTECH_SAMURAI_IDE), },
21462306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HOLTEK, PCI_DEVICE_ID_HOLTEK_6565), },
21562306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8673F), },
21662306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8886A), },
21762306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_UMC,    PCI_DEVICE_ID_UMC_UM8886BF), },
21862306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_HINT,   PCI_DEVICE_ID_HINT_VXPROII_IDE), },
21962306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_VIA,    PCI_DEVICE_ID_VIA_82C561), },
22062306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_OPTI,   PCI_DEVICE_ID_OPTI_82C558), },
22162306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_CENATEK,PCI_DEVICE_ID_CENATEK_IDE),
22262306a36Sopenharmony_ci	  .driver_data = ATA_GEN_FORCE_DMA },
22362306a36Sopenharmony_ci#if !defined(CONFIG_PATA_TOSHIBA) && !defined(CONFIG_PATA_TOSHIBA_MODULE)
22462306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_1), },
22562306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_2),  },
22662306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_3),  },
22762306a36Sopenharmony_ci	{ PCI_DEVICE(PCI_VENDOR_ID_TOSHIBA,PCI_DEVICE_ID_TOSHIBA_PICCOLO_5),  },
22862306a36Sopenharmony_ci#endif
22962306a36Sopenharmony_ci	/* Intel, IDE class device */
23062306a36Sopenharmony_ci	{ PCI_VENDOR_ID_INTEL, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
23162306a36Sopenharmony_ci	  PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL,
23262306a36Sopenharmony_ci	  .driver_data = ATA_GEN_INTEL_IDER },
23362306a36Sopenharmony_ci	/* Must come last. If you add entries adjust this table appropriately */
23462306a36Sopenharmony_ci	{ PCI_DEVICE_CLASS(PCI_CLASS_STORAGE_IDE << 8, 0xFFFFFF00UL),
23562306a36Sopenharmony_ci	  .driver_data = ATA_GEN_CLASS_MATCH },
23662306a36Sopenharmony_ci	{ 0, },
23762306a36Sopenharmony_ci};
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_cistatic struct pci_driver ata_generic_pci_driver = {
24062306a36Sopenharmony_ci	.name 		= DRV_NAME,
24162306a36Sopenharmony_ci	.id_table	= ata_generic,
24262306a36Sopenharmony_ci	.probe 		= ata_generic_init_one,
24362306a36Sopenharmony_ci	.remove		= ata_pci_remove_one,
24462306a36Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
24562306a36Sopenharmony_ci	.suspend	= ata_pci_device_suspend,
24662306a36Sopenharmony_ci	.resume		= ata_pci_device_resume,
24762306a36Sopenharmony_ci#endif
24862306a36Sopenharmony_ci};
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_cimodule_pci_driver(ata_generic_pci_driver);
25162306a36Sopenharmony_ci
25262306a36Sopenharmony_ciMODULE_AUTHOR("Alan Cox");
25362306a36Sopenharmony_ciMODULE_DESCRIPTION("low-level driver for generic ATA");
25462306a36Sopenharmony_ciMODULE_LICENSE("GPL");
25562306a36Sopenharmony_ciMODULE_DEVICE_TABLE(pci, ata_generic);
25662306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
25762306a36Sopenharmony_ci
25862306a36Sopenharmony_cimodule_param(all_generic_ide, int, 0);
259