18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (C) 1998-2000 Andreas S. Krebs (akrebs@altavista.net), Maintainer
48c2ecf20Sopenharmony_ci *  Copyright (C) 1998-2002 Andre Hedrick <andre@linux-ide.org>, Integrator
58c2ecf20Sopenharmony_ci *  Copyright (C) 2007-2011 Bartlomiej Zolnierkiewicz
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * CYPRESS CY82C693 chipset IDE controller
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * The CY82C693 chipset is used on Digital's PC-Alpha 164SX boards.
108c2ecf20Sopenharmony_ci */
118c2ecf20Sopenharmony_ci
128c2ecf20Sopenharmony_ci#include <linux/module.h>
138c2ecf20Sopenharmony_ci#include <linux/types.h>
148c2ecf20Sopenharmony_ci#include <linux/pci.h>
158c2ecf20Sopenharmony_ci#include <linux/ide.h>
168c2ecf20Sopenharmony_ci#include <linux/init.h>
178c2ecf20Sopenharmony_ci
188c2ecf20Sopenharmony_ci#include <asm/io.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define DRV_NAME "cy82c693"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/*
238c2ecf20Sopenharmony_ci *	NOTE: the value for busmaster timeout is tricky and I got it by
248c2ecf20Sopenharmony_ci *	trial and error!  By using a to low value will cause DMA timeouts
258c2ecf20Sopenharmony_ci *	and drop IDE performance, and by using a to high value will cause
268c2ecf20Sopenharmony_ci *	audio playback to scatter.
278c2ecf20Sopenharmony_ci *	If you know a better value or how to calc it, please let me know.
288c2ecf20Sopenharmony_ci */
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_ci/* twice the value written in cy82c693ub datasheet */
318c2ecf20Sopenharmony_ci#define BUSMASTER_TIMEOUT	0x50
328c2ecf20Sopenharmony_ci/*
338c2ecf20Sopenharmony_ci * the value above was tested on my machine and it seems to work okay
348c2ecf20Sopenharmony_ci */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci/* here are the offset definitions for the registers */
378c2ecf20Sopenharmony_ci#define CY82_IDE_CMDREG		0x04
388c2ecf20Sopenharmony_ci#define CY82_IDE_ADDRSETUP	0x48
398c2ecf20Sopenharmony_ci#define CY82_IDE_MASTER_IOR	0x4C
408c2ecf20Sopenharmony_ci#define CY82_IDE_MASTER_IOW	0x4D
418c2ecf20Sopenharmony_ci#define CY82_IDE_SLAVE_IOR	0x4E
428c2ecf20Sopenharmony_ci#define CY82_IDE_SLAVE_IOW	0x4F
438c2ecf20Sopenharmony_ci#define CY82_IDE_MASTER_8BIT	0x50
448c2ecf20Sopenharmony_ci#define CY82_IDE_SLAVE_8BIT	0x51
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define CY82_INDEX_PORT		0x22
478c2ecf20Sopenharmony_ci#define CY82_DATA_PORT		0x23
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ci#define CY82_INDEX_CHANNEL0	0x30
508c2ecf20Sopenharmony_ci#define CY82_INDEX_CHANNEL1	0x31
518c2ecf20Sopenharmony_ci#define CY82_INDEX_TIMEOUT	0x32
528c2ecf20Sopenharmony_ci
538c2ecf20Sopenharmony_ci/*
548c2ecf20Sopenharmony_ci * set DMA mode a specific channel for CY82C693
558c2ecf20Sopenharmony_ci */
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic void cy82c693_set_dma_mode(ide_hwif_t *hwif, ide_drive_t *drive)
588c2ecf20Sopenharmony_ci{
598c2ecf20Sopenharmony_ci	const u8 mode = drive->dma_mode;
608c2ecf20Sopenharmony_ci	u8 single = (mode & 0x10) >> 4, index = 0, data = 0;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	index = hwif->channel ? CY82_INDEX_CHANNEL1 : CY82_INDEX_CHANNEL0;
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci	data = (mode & 3) | (single << 2);
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_ci	outb(index, CY82_INDEX_PORT);
678c2ecf20Sopenharmony_ci	outb(data, CY82_DATA_PORT);
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci	/*
708c2ecf20Sopenharmony_ci	 * note: below we set the value for Bus Master IDE TimeOut Register
718c2ecf20Sopenharmony_ci	 * I'm not absolutely sure what this does, but it solved my problem
728c2ecf20Sopenharmony_ci	 * with IDE DMA and sound, so I now can play sound and work with
738c2ecf20Sopenharmony_ci	 * my IDE driver at the same time :-)
748c2ecf20Sopenharmony_ci	 *
758c2ecf20Sopenharmony_ci	 * If you know the correct (best) value for this register please
768c2ecf20Sopenharmony_ci	 * let me know - ASK
778c2ecf20Sopenharmony_ci	 */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci	data = BUSMASTER_TIMEOUT;
808c2ecf20Sopenharmony_ci	outb(CY82_INDEX_TIMEOUT, CY82_INDEX_PORT);
818c2ecf20Sopenharmony_ci	outb(data, CY82_DATA_PORT);
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic void cy82c693_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	struct pci_dev *dev = to_pci_dev(hwif->dev);
878c2ecf20Sopenharmony_ci	int bus_speed = ide_pci_clk ? ide_pci_clk : 33;
888c2ecf20Sopenharmony_ci	const unsigned long T = 1000000 / bus_speed;
898c2ecf20Sopenharmony_ci	unsigned int addrCtrl;
908c2ecf20Sopenharmony_ci	struct ide_timing t;
918c2ecf20Sopenharmony_ci	u8 time_16, time_8;
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_ci	/* select primary or secondary channel */
948c2ecf20Sopenharmony_ci	if (drive->dn > 1) {  /* drive is on the secondary channel */
958c2ecf20Sopenharmony_ci		dev = pci_get_slot(dev->bus, dev->devfn+1);
968c2ecf20Sopenharmony_ci		if (!dev) {
978c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: tune_drive: "
988c2ecf20Sopenharmony_ci				"Cannot find secondary interface!\n",
998c2ecf20Sopenharmony_ci				drive->name);
1008c2ecf20Sopenharmony_ci			return;
1018c2ecf20Sopenharmony_ci		}
1028c2ecf20Sopenharmony_ci	}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci	ide_timing_compute(drive, drive->pio_mode, &t, T, 1);
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_ci	time_16 = clamp_val(t.recover - 1, 0, 15) |
1078c2ecf20Sopenharmony_ci		  (clamp_val(t.active - 1, 0, 15) << 4);
1088c2ecf20Sopenharmony_ci	time_8 = clamp_val(t.act8b - 1, 0, 15) |
1098c2ecf20Sopenharmony_ci		 (clamp_val(t.rec8b - 1, 0, 15) << 4);
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_ci	/* now let's write  the clocks registers */
1128c2ecf20Sopenharmony_ci	if ((drive->dn & 1) == 0) {
1138c2ecf20Sopenharmony_ci		/*
1148c2ecf20Sopenharmony_ci		 * set master drive
1158c2ecf20Sopenharmony_ci		 * address setup control register
1168c2ecf20Sopenharmony_ci		 * is 32 bit !!!
1178c2ecf20Sopenharmony_ci		 */
1188c2ecf20Sopenharmony_ci		pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
1198c2ecf20Sopenharmony_ci
1208c2ecf20Sopenharmony_ci		addrCtrl &= (~0xF);
1218c2ecf20Sopenharmony_ci		addrCtrl |= clamp_val(t.setup - 1, 0, 15);
1228c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci		/* now let's set the remaining registers */
1258c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, CY82_IDE_MASTER_IOR, time_16);
1268c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, CY82_IDE_MASTER_IOW, time_16);
1278c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, CY82_IDE_MASTER_8BIT, time_8);
1288c2ecf20Sopenharmony_ci	} else {
1298c2ecf20Sopenharmony_ci		/*
1308c2ecf20Sopenharmony_ci		 * set slave drive
1318c2ecf20Sopenharmony_ci		 * address setup control register
1328c2ecf20Sopenharmony_ci		 * is 32 bit !!!
1338c2ecf20Sopenharmony_ci		 */
1348c2ecf20Sopenharmony_ci		pci_read_config_dword(dev, CY82_IDE_ADDRSETUP, &addrCtrl);
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci		addrCtrl &= (~0xF0);
1378c2ecf20Sopenharmony_ci		addrCtrl |= (clamp_val(t.setup - 1, 0, 15) << 4);
1388c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, CY82_IDE_ADDRSETUP, addrCtrl);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci		/* now let's set the remaining registers */
1418c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, CY82_IDE_SLAVE_IOR, time_16);
1428c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, CY82_IDE_SLAVE_IOW, time_16);
1438c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, CY82_IDE_SLAVE_8BIT, time_8);
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	if (drive->dn > 1)
1468c2ecf20Sopenharmony_ci		pci_dev_put(dev);
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic void init_iops_cy82c693(ide_hwif_t *hwif)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	static ide_hwif_t *primary;
1528c2ecf20Sopenharmony_ci	struct pci_dev *dev = to_pci_dev(hwif->dev);
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci	if (PCI_FUNC(dev->devfn) == 1)
1558c2ecf20Sopenharmony_ci		primary = hwif;
1568c2ecf20Sopenharmony_ci	else {
1578c2ecf20Sopenharmony_ci		hwif->mate = primary;
1588c2ecf20Sopenharmony_ci		hwif->channel = 1;
1598c2ecf20Sopenharmony_ci	}
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_cistatic const struct ide_port_ops cy82c693_port_ops = {
1638c2ecf20Sopenharmony_ci	.set_pio_mode		= cy82c693_set_pio_mode,
1648c2ecf20Sopenharmony_ci	.set_dma_mode		= cy82c693_set_dma_mode,
1658c2ecf20Sopenharmony_ci};
1668c2ecf20Sopenharmony_ci
1678c2ecf20Sopenharmony_cistatic const struct ide_port_info cy82c693_chipset = {
1688c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
1698c2ecf20Sopenharmony_ci	.init_iops	= init_iops_cy82c693,
1708c2ecf20Sopenharmony_ci	.port_ops	= &cy82c693_port_ops,
1718c2ecf20Sopenharmony_ci	.host_flags	= IDE_HFLAG_SINGLE,
1728c2ecf20Sopenharmony_ci	.pio_mask	= ATA_PIO4,
1738c2ecf20Sopenharmony_ci	.swdma_mask	= ATA_SWDMA2,
1748c2ecf20Sopenharmony_ci	.mwdma_mask	= ATA_MWDMA2,
1758c2ecf20Sopenharmony_ci};
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic int cy82c693_init_one(struct pci_dev *dev,
1788c2ecf20Sopenharmony_ci			     const struct pci_device_id *id)
1798c2ecf20Sopenharmony_ci{
1808c2ecf20Sopenharmony_ci	struct pci_dev *dev2;
1818c2ecf20Sopenharmony_ci	int ret = -ENODEV;
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci	/* CY82C693 is more than only a IDE controller.
1848c2ecf20Sopenharmony_ci	   Function 1 is primary IDE channel, function 2 - secondary. */
1858c2ecf20Sopenharmony_ci	if ((dev->class >> 8) == PCI_CLASS_STORAGE_IDE &&
1868c2ecf20Sopenharmony_ci	    PCI_FUNC(dev->devfn) == 1) {
1878c2ecf20Sopenharmony_ci		dev2 = pci_get_slot(dev->bus, dev->devfn + 1);
1888c2ecf20Sopenharmony_ci		ret = ide_pci_init_two(dev, dev2, &cy82c693_chipset, NULL);
1898c2ecf20Sopenharmony_ci		if (ret)
1908c2ecf20Sopenharmony_ci			pci_dev_put(dev2);
1918c2ecf20Sopenharmony_ci	}
1928c2ecf20Sopenharmony_ci	return ret;
1938c2ecf20Sopenharmony_ci}
1948c2ecf20Sopenharmony_ci
1958c2ecf20Sopenharmony_cistatic void cy82c693_remove(struct pci_dev *dev)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	struct ide_host *host = pci_get_drvdata(dev);
1988c2ecf20Sopenharmony_ci	struct pci_dev *dev2 = host->dev[1] ? to_pci_dev(host->dev[1]) : NULL;
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	ide_pci_remove(dev);
2018c2ecf20Sopenharmony_ci	pci_dev_put(dev2);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_cistatic const struct pci_device_id cy82c693_pci_tbl[] = {
2058c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(CONTAQ, PCI_DEVICE_ID_CONTAQ_82C693), 0 },
2068c2ecf20Sopenharmony_ci	{ 0, },
2078c2ecf20Sopenharmony_ci};
2088c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, cy82c693_pci_tbl);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_cistatic struct pci_driver cy82c693_pci_driver = {
2118c2ecf20Sopenharmony_ci	.name		= "Cypress_IDE",
2128c2ecf20Sopenharmony_ci	.id_table	= cy82c693_pci_tbl,
2138c2ecf20Sopenharmony_ci	.probe		= cy82c693_init_one,
2148c2ecf20Sopenharmony_ci	.remove		= cy82c693_remove,
2158c2ecf20Sopenharmony_ci	.suspend	= ide_pci_suspend,
2168c2ecf20Sopenharmony_ci	.resume		= ide_pci_resume,
2178c2ecf20Sopenharmony_ci};
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic int __init cy82c693_ide_init(void)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	return ide_pci_register_driver(&cy82c693_pci_driver);
2228c2ecf20Sopenharmony_ci}
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic void __exit cy82c693_ide_exit(void)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	pci_unregister_driver(&cy82c693_pci_driver);
2278c2ecf20Sopenharmony_ci}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_cimodule_init(cy82c693_ide_init);
2308c2ecf20Sopenharmony_cimodule_exit(cy82c693_ide_exit);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ciMODULE_AUTHOR("Andreas Krebs, Andre Hedrick, Bartlomiej Zolnierkiewicz");
2338c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PCI driver module for the Cypress CY82C693 IDE");
2348c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
235