18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 2000-2002 Andre Hedrick <andre@linux-ide.org>
48c2ecf20Sopenharmony_ci *  Copyright (C) 2006-2007 MontaVista Software, Inc. <source@mvista.com>
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * This is a look-alike variation of the ICH0 PIIX4 Ultra-66,
78c2ecf20Sopenharmony_ci * but this keeps the ISA-Bridge and slots alive.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci */
108c2ecf20Sopenharmony_ci
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/ide.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#define DRV_NAME "slc90e66"
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_cistatic DEFINE_SPINLOCK(slc90e66_lock);
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_cistatic void slc90e66_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
238c2ecf20Sopenharmony_ci{
248c2ecf20Sopenharmony_ci	struct pci_dev *dev	= to_pci_dev(hwif->dev);
258c2ecf20Sopenharmony_ci	int is_slave		= drive->dn & 1;
268c2ecf20Sopenharmony_ci	int master_port		= hwif->channel ? 0x42 : 0x40;
278c2ecf20Sopenharmony_ci	int slave_port		= 0x44;
288c2ecf20Sopenharmony_ci	unsigned long flags;
298c2ecf20Sopenharmony_ci	u16 master_data;
308c2ecf20Sopenharmony_ci	u8 slave_data;
318c2ecf20Sopenharmony_ci	int control = 0;
328c2ecf20Sopenharmony_ci	const u8 pio = drive->pio_mode - XFER_PIO_0;
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci				     /* ISP  RTC */
358c2ecf20Sopenharmony_ci	static const u8 timings[][2] = {
368c2ecf20Sopenharmony_ci					{ 0, 0 },
378c2ecf20Sopenharmony_ci					{ 0, 0 },
388c2ecf20Sopenharmony_ci					{ 1, 0 },
398c2ecf20Sopenharmony_ci					{ 2, 1 },
408c2ecf20Sopenharmony_ci					{ 2, 3 }, };
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci	spin_lock_irqsave(&slc90e66_lock, flags);
438c2ecf20Sopenharmony_ci	pci_read_config_word(dev, master_port, &master_data);
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci	if (pio > 1)
468c2ecf20Sopenharmony_ci		control |= 1;	/* Programmable timing on */
478c2ecf20Sopenharmony_ci	if (drive->media == ide_disk)
488c2ecf20Sopenharmony_ci		control |= 4;	/* Prefetch, post write */
498c2ecf20Sopenharmony_ci	if (ide_pio_need_iordy(drive, pio))
508c2ecf20Sopenharmony_ci		control |= 2;	/* IORDY */
518c2ecf20Sopenharmony_ci	if (is_slave) {
528c2ecf20Sopenharmony_ci		master_data |=  0x4000;
538c2ecf20Sopenharmony_ci		master_data &= ~0x0070;
548c2ecf20Sopenharmony_ci		if (pio > 1) {
558c2ecf20Sopenharmony_ci			/* Set PPE, IE and TIME */
568c2ecf20Sopenharmony_ci			master_data |= control << 4;
578c2ecf20Sopenharmony_ci		}
588c2ecf20Sopenharmony_ci		pci_read_config_byte(dev, slave_port, &slave_data);
598c2ecf20Sopenharmony_ci		slave_data &= hwif->channel ? 0x0f : 0xf0;
608c2ecf20Sopenharmony_ci		slave_data |= ((timings[pio][0] << 2) | timings[pio][1]) <<
618c2ecf20Sopenharmony_ci			       (hwif->channel ? 4 : 0);
628c2ecf20Sopenharmony_ci	} else {
638c2ecf20Sopenharmony_ci		master_data &= ~0x3307;
648c2ecf20Sopenharmony_ci		if (pio > 1) {
658c2ecf20Sopenharmony_ci			/* enable PPE, IE and TIME */
668c2ecf20Sopenharmony_ci			master_data |= control;
678c2ecf20Sopenharmony_ci		}
688c2ecf20Sopenharmony_ci		master_data |= (timings[pio][0] << 12) | (timings[pio][1] << 8);
698c2ecf20Sopenharmony_ci	}
708c2ecf20Sopenharmony_ci	pci_write_config_word(dev, master_port, master_data);
718c2ecf20Sopenharmony_ci	if (is_slave)
728c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, slave_port, slave_data);
738c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&slc90e66_lock, flags);
748c2ecf20Sopenharmony_ci}
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_cistatic void slc90e66_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
778c2ecf20Sopenharmony_ci{
788c2ecf20Sopenharmony_ci	struct pci_dev *dev	= to_pci_dev(hwif->dev);
798c2ecf20Sopenharmony_ci	u8 maslave		= hwif->channel ? 0x42 : 0x40;
808c2ecf20Sopenharmony_ci	int sitre = 0, a_speed	= 7 << (drive->dn * 4);
818c2ecf20Sopenharmony_ci	int u_speed = 0, u_flag = 1 << drive->dn;
828c2ecf20Sopenharmony_ci	u16			reg4042, reg44, reg48, reg4a;
838c2ecf20Sopenharmony_ci	const u8 speed		= drive->dma_mode;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	pci_read_config_word(dev, maslave, &reg4042);
868c2ecf20Sopenharmony_ci	sitre = (reg4042 & 0x4000) ? 1 : 0;
878c2ecf20Sopenharmony_ci	pci_read_config_word(dev, 0x44, &reg44);
888c2ecf20Sopenharmony_ci	pci_read_config_word(dev, 0x48, &reg48);
898c2ecf20Sopenharmony_ci	pci_read_config_word(dev, 0x4a, &reg4a);
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci	if (speed >= XFER_UDMA_0) {
928c2ecf20Sopenharmony_ci		u_speed = (speed - XFER_UDMA_0) << (drive->dn * 4);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		if (!(reg48 & u_flag))
958c2ecf20Sopenharmony_ci			pci_write_config_word(dev, 0x48, reg48|u_flag);
968c2ecf20Sopenharmony_ci		if ((reg4a & a_speed) != u_speed) {
978c2ecf20Sopenharmony_ci			pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
988c2ecf20Sopenharmony_ci			pci_read_config_word(dev, 0x4a, &reg4a);
998c2ecf20Sopenharmony_ci			pci_write_config_word(dev, 0x4a, reg4a|u_speed);
1008c2ecf20Sopenharmony_ci		}
1018c2ecf20Sopenharmony_ci	} else {
1028c2ecf20Sopenharmony_ci		const u8 mwdma_to_pio[] = { 0, 3, 4 };
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		if (reg48 & u_flag)
1058c2ecf20Sopenharmony_ci			pci_write_config_word(dev, 0x48, reg48 & ~u_flag);
1068c2ecf20Sopenharmony_ci		if (reg4a & a_speed)
1078c2ecf20Sopenharmony_ci			pci_write_config_word(dev, 0x4a, reg4a & ~a_speed);
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci		if (speed >= XFER_MW_DMA_0)
1108c2ecf20Sopenharmony_ci			drive->pio_mode =
1118c2ecf20Sopenharmony_ci				mwdma_to_pio[speed - XFER_MW_DMA_0] + XFER_PIO_0;
1128c2ecf20Sopenharmony_ci		else
1138c2ecf20Sopenharmony_ci			drive->pio_mode = XFER_PIO_2; /* for SWDMA2 */
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci		slc90e66_set_pio_mode(hwif, drive);
1168c2ecf20Sopenharmony_ci	}
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic u8 slc90e66_cable_detect(ide_hwif_t *hwif)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct pci_dev *dev = to_pci_dev(hwif->dev);
1228c2ecf20Sopenharmony_ci	u8 reg47 = 0, mask = hwif->channel ? 0x01 : 0x02;
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, 0x47, &reg47);
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	/* bit[0(1)]: 0:80, 1:40 */
1278c2ecf20Sopenharmony_ci	return (reg47 & mask) ? ATA_CBL_PATA40 : ATA_CBL_PATA80;
1288c2ecf20Sopenharmony_ci}
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_cistatic const struct ide_port_ops slc90e66_port_ops = {
1318c2ecf20Sopenharmony_ci	.set_pio_mode		= slc90e66_set_pio_mode,
1328c2ecf20Sopenharmony_ci	.set_dma_mode		= slc90e66_set_dma_mode,
1338c2ecf20Sopenharmony_ci	.cable_detect		= slc90e66_cable_detect,
1348c2ecf20Sopenharmony_ci};
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic const struct ide_port_info slc90e66_chipset = {
1378c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
1388c2ecf20Sopenharmony_ci	.enablebits	= { {0x41, 0x80, 0x80}, {0x43, 0x80, 0x80} },
1398c2ecf20Sopenharmony_ci	.port_ops	= &slc90e66_port_ops,
1408c2ecf20Sopenharmony_ci	.pio_mask	= ATA_PIO4,
1418c2ecf20Sopenharmony_ci	.swdma_mask	= ATA_SWDMA2_ONLY,
1428c2ecf20Sopenharmony_ci	.mwdma_mask	= ATA_MWDMA12_ONLY,
1438c2ecf20Sopenharmony_ci	.udma_mask	= ATA_UDMA4,
1448c2ecf20Sopenharmony_ci};
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistatic int slc90e66_init_one(struct pci_dev *dev,
1478c2ecf20Sopenharmony_ci			     const struct pci_device_id *id)
1488c2ecf20Sopenharmony_ci{
1498c2ecf20Sopenharmony_ci	return ide_pci_init_one(dev, &slc90e66_chipset, NULL);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_cistatic const struct pci_device_id slc90e66_pci_tbl[] = {
1538c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(EFAR, PCI_DEVICE_ID_EFAR_SLC90E66_1), 0 },
1548c2ecf20Sopenharmony_ci	{ 0, },
1558c2ecf20Sopenharmony_ci};
1568c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, slc90e66_pci_tbl);
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_cistatic struct pci_driver slc90e66_pci_driver = {
1598c2ecf20Sopenharmony_ci	.name		= "SLC90e66_IDE",
1608c2ecf20Sopenharmony_ci	.id_table	= slc90e66_pci_tbl,
1618c2ecf20Sopenharmony_ci	.probe		= slc90e66_init_one,
1628c2ecf20Sopenharmony_ci	.remove		= ide_pci_remove,
1638c2ecf20Sopenharmony_ci	.suspend	= ide_pci_suspend,
1648c2ecf20Sopenharmony_ci	.resume		= ide_pci_resume,
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic int __init slc90e66_ide_init(void)
1688c2ecf20Sopenharmony_ci{
1698c2ecf20Sopenharmony_ci	return ide_pci_register_driver(&slc90e66_pci_driver);
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_cistatic void __exit slc90e66_ide_exit(void)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	pci_unregister_driver(&slc90e66_pci_driver);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cimodule_init(slc90e66_ide_init);
1788c2ecf20Sopenharmony_cimodule_exit(slc90e66_ide_exit);
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andre Hedrick");
1818c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PCI driver module for SLC90E66 IDE");
1828c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
183