18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * VIA IDE driver for Linux. Supported southbridges:
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci *   vt82c576, vt82c586, vt82c586a, vt82c586b, vt82c596a, vt82c596b,
68c2ecf20Sopenharmony_ci *   vt82c686, vt82c686a, vt82c686b, vt8231, vt8233, vt8233c, vt8233a,
78c2ecf20Sopenharmony_ci *   vt8235, vt8237, vt8237a
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Copyright (c) 2000-2002 Vojtech Pavlik
108c2ecf20Sopenharmony_ci * Copyright (c) 2007-2010 Bartlomiej Zolnierkiewicz
118c2ecf20Sopenharmony_ci *
128c2ecf20Sopenharmony_ci * Based on the work of:
138c2ecf20Sopenharmony_ci *	Michel Aubry
148c2ecf20Sopenharmony_ci *	Jeff Garzik
158c2ecf20Sopenharmony_ci *	Andre Hedrick
168c2ecf20Sopenharmony_ci *
178c2ecf20Sopenharmony_ci * Documentation:
188c2ecf20Sopenharmony_ci *	Obsolete device documentation publicly available from via.com.tw
198c2ecf20Sopenharmony_ci *	Current device documentation available under NDA only
208c2ecf20Sopenharmony_ci */
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ci#include <linux/module.h>
248c2ecf20Sopenharmony_ci#include <linux/kernel.h>
258c2ecf20Sopenharmony_ci#include <linux/slab.h>
268c2ecf20Sopenharmony_ci#include <linux/pci.h>
278c2ecf20Sopenharmony_ci#include <linux/init.h>
288c2ecf20Sopenharmony_ci#include <linux/ide.h>
298c2ecf20Sopenharmony_ci#include <linux/dmi.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#ifdef CONFIG_PPC_CHRP
328c2ecf20Sopenharmony_ci#include <asm/processor.h>
338c2ecf20Sopenharmony_ci#endif
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define DRV_NAME "via82cxxx"
368c2ecf20Sopenharmony_ci
378c2ecf20Sopenharmony_ci#define VIA_IDE_ENABLE		0x40
388c2ecf20Sopenharmony_ci#define VIA_IDE_CONFIG		0x41
398c2ecf20Sopenharmony_ci#define VIA_FIFO_CONFIG		0x43
408c2ecf20Sopenharmony_ci#define VIA_MISC_1		0x44
418c2ecf20Sopenharmony_ci#define VIA_MISC_2		0x45
428c2ecf20Sopenharmony_ci#define VIA_MISC_3		0x46
438c2ecf20Sopenharmony_ci#define VIA_DRIVE_TIMING	0x48
448c2ecf20Sopenharmony_ci#define VIA_8BIT_TIMING		0x4e
458c2ecf20Sopenharmony_ci#define VIA_ADDRESS_SETUP	0x4c
468c2ecf20Sopenharmony_ci#define VIA_UDMA_TIMING		0x50
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define VIA_BAD_PREQ		0x01 /* Crashes if PREQ# till DDACK# set */
498c2ecf20Sopenharmony_ci#define VIA_BAD_CLK66		0x02 /* 66 MHz clock doesn't work correctly */
508c2ecf20Sopenharmony_ci#define VIA_SET_FIFO		0x04 /* Needs to have FIFO split set */
518c2ecf20Sopenharmony_ci#define VIA_NO_UNMASK		0x08 /* Doesn't work with IRQ unmasking on */
528c2ecf20Sopenharmony_ci#define VIA_BAD_ID		0x10 /* Has wrong vendor ID (0x1107) */
538c2ecf20Sopenharmony_ci#define VIA_BAD_AST		0x20 /* Don't touch Address Setup Timing */
548c2ecf20Sopenharmony_ci#define VIA_SATA_PATA		0x80 /* SATA/PATA combined configuration */
558c2ecf20Sopenharmony_ci
568c2ecf20Sopenharmony_cienum {
578c2ecf20Sopenharmony_ci	VIA_IDFLAG_SINGLE = (1 << 1), /* single channel controller */
588c2ecf20Sopenharmony_ci};
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/*
618c2ecf20Sopenharmony_ci * VIA SouthBridge chips.
628c2ecf20Sopenharmony_ci */
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_cistatic struct via_isa_bridge {
658c2ecf20Sopenharmony_ci	char *name;
668c2ecf20Sopenharmony_ci	u16 id;
678c2ecf20Sopenharmony_ci	u8 rev_min;
688c2ecf20Sopenharmony_ci	u8 rev_max;
698c2ecf20Sopenharmony_ci	u8 udma_mask;
708c2ecf20Sopenharmony_ci	u8 flags;
718c2ecf20Sopenharmony_ci} via_isa_bridges[] = {
728c2ecf20Sopenharmony_ci	{ "vx855",	PCI_DEVICE_ID_VIA_VX855,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
738c2ecf20Sopenharmony_ci	{ "vx800",	PCI_DEVICE_ID_VIA_VX800,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
748c2ecf20Sopenharmony_ci	{ "cx700",	PCI_DEVICE_ID_VIA_CX700,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
758c2ecf20Sopenharmony_ci	{ "vt8261",	PCI_DEVICE_ID_VIA_8261,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
768c2ecf20Sopenharmony_ci	{ "vt8237s",	PCI_DEVICE_ID_VIA_8237S,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
778c2ecf20Sopenharmony_ci	{ "vt6410",	PCI_DEVICE_ID_VIA_6410,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
788c2ecf20Sopenharmony_ci	{ "vt6415",	PCI_DEVICE_ID_VIA_6415,     0x00, 0xff, ATA_UDMA6, VIA_BAD_AST },
798c2ecf20Sopenharmony_ci	{ "vt8251",	PCI_DEVICE_ID_VIA_8251,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
808c2ecf20Sopenharmony_ci	{ "vt8237",	PCI_DEVICE_ID_VIA_8237,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
818c2ecf20Sopenharmony_ci	{ "vt8237a",	PCI_DEVICE_ID_VIA_8237A,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
828c2ecf20Sopenharmony_ci	{ "vt8235",	PCI_DEVICE_ID_VIA_8235,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
838c2ecf20Sopenharmony_ci	{ "vt8233a",	PCI_DEVICE_ID_VIA_8233A,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
848c2ecf20Sopenharmony_ci	{ "vt8233c",	PCI_DEVICE_ID_VIA_8233C_0,  0x00, 0x2f, ATA_UDMA5, },
858c2ecf20Sopenharmony_ci	{ "vt8233",	PCI_DEVICE_ID_VIA_8233_0,   0x00, 0x2f, ATA_UDMA5, },
868c2ecf20Sopenharmony_ci	{ "vt8231",	PCI_DEVICE_ID_VIA_8231,     0x00, 0x2f, ATA_UDMA5, },
878c2ecf20Sopenharmony_ci	{ "vt82c686b",	PCI_DEVICE_ID_VIA_82C686,   0x40, 0x4f, ATA_UDMA5, },
888c2ecf20Sopenharmony_ci	{ "vt82c686a",	PCI_DEVICE_ID_VIA_82C686,   0x10, 0x2f, ATA_UDMA4, },
898c2ecf20Sopenharmony_ci	{ "vt82c686",	PCI_DEVICE_ID_VIA_82C686,   0x00, 0x0f, ATA_UDMA2, VIA_BAD_CLK66 },
908c2ecf20Sopenharmony_ci	{ "vt82c596b",	PCI_DEVICE_ID_VIA_82C596,   0x10, 0x2f, ATA_UDMA4, },
918c2ecf20Sopenharmony_ci	{ "vt82c596a",	PCI_DEVICE_ID_VIA_82C596,   0x00, 0x0f, ATA_UDMA2, VIA_BAD_CLK66 },
928c2ecf20Sopenharmony_ci	{ "vt82c586b",	PCI_DEVICE_ID_VIA_82C586_0, 0x47, 0x4f, ATA_UDMA2, VIA_SET_FIFO },
938c2ecf20Sopenharmony_ci	{ "vt82c586b",	PCI_DEVICE_ID_VIA_82C586_0, 0x40, 0x46, ATA_UDMA2, VIA_SET_FIFO | VIA_BAD_PREQ },
948c2ecf20Sopenharmony_ci	{ "vt82c586b",	PCI_DEVICE_ID_VIA_82C586_0, 0x30, 0x3f, ATA_UDMA2, VIA_SET_FIFO },
958c2ecf20Sopenharmony_ci	{ "vt82c586a",	PCI_DEVICE_ID_VIA_82C586_0, 0x20, 0x2f, ATA_UDMA2, VIA_SET_FIFO },
968c2ecf20Sopenharmony_ci	{ "vt82c586",	PCI_DEVICE_ID_VIA_82C586_0, 0x00, 0x0f,      0x00, VIA_SET_FIFO },
978c2ecf20Sopenharmony_ci	{ "vt82c576",	PCI_DEVICE_ID_VIA_82C576,   0x00, 0x2f,      0x00, VIA_SET_FIFO | VIA_NO_UNMASK },
988c2ecf20Sopenharmony_ci	{ "vt82c576",	PCI_DEVICE_ID_VIA_82C576,   0x00, 0x2f,      0x00, VIA_SET_FIFO | VIA_NO_UNMASK | VIA_BAD_ID },
998c2ecf20Sopenharmony_ci	{ "vtxxxx",	PCI_DEVICE_ID_VIA_ANON,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
1008c2ecf20Sopenharmony_ci	{ NULL }
1018c2ecf20Sopenharmony_ci};
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic unsigned int via_clock;
1048c2ecf20Sopenharmony_cistatic char *via_dma[] = { "16", "25", "33", "44", "66", "100", "133" };
1058c2ecf20Sopenharmony_ci
1068c2ecf20Sopenharmony_cistruct via82cxxx_dev
1078c2ecf20Sopenharmony_ci{
1088c2ecf20Sopenharmony_ci	struct via_isa_bridge *via_config;
1098c2ecf20Sopenharmony_ci	unsigned int via_80w;
1108c2ecf20Sopenharmony_ci};
1118c2ecf20Sopenharmony_ci
1128c2ecf20Sopenharmony_ci/**
1138c2ecf20Sopenharmony_ci *	via_set_speed			-	write timing registers
1148c2ecf20Sopenharmony_ci *	@dev: PCI device
1158c2ecf20Sopenharmony_ci *	@dn: device
1168c2ecf20Sopenharmony_ci *	@timing: IDE timing data to use
1178c2ecf20Sopenharmony_ci *
1188c2ecf20Sopenharmony_ci *	via_set_speed writes timing values to the chipset registers
1198c2ecf20Sopenharmony_ci */
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_cistatic void via_set_speed(ide_hwif_t *hwif, u8 dn, struct ide_timing *timing)
1228c2ecf20Sopenharmony_ci{
1238c2ecf20Sopenharmony_ci	struct pci_dev *dev = to_pci_dev(hwif->dev);
1248c2ecf20Sopenharmony_ci	struct ide_host *host = pci_get_drvdata(dev);
1258c2ecf20Sopenharmony_ci	struct via82cxxx_dev *vdev = host->host_priv;
1268c2ecf20Sopenharmony_ci	u8 t;
1278c2ecf20Sopenharmony_ci
1288c2ecf20Sopenharmony_ci	if (~vdev->via_config->flags & VIA_BAD_AST) {
1298c2ecf20Sopenharmony_ci		pci_read_config_byte(dev, VIA_ADDRESS_SETUP, &t);
1308c2ecf20Sopenharmony_ci		t = (t & ~(3 << ((3 - dn) << 1))) | ((clamp_val(timing->setup, 1, 4) - 1) << ((3 - dn) << 1));
1318c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, VIA_ADDRESS_SETUP, t);
1328c2ecf20Sopenharmony_ci	}
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci	pci_write_config_byte(dev, VIA_8BIT_TIMING + (1 - (dn >> 1)),
1358c2ecf20Sopenharmony_ci		((clamp_val(timing->act8b, 1, 16) - 1) << 4) | (clamp_val(timing->rec8b, 1, 16) - 1));
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_ci	pci_write_config_byte(dev, VIA_DRIVE_TIMING + (3 - dn),
1388c2ecf20Sopenharmony_ci		((clamp_val(timing->active, 1, 16) - 1) << 4) | (clamp_val(timing->recover, 1, 16) - 1));
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	switch (vdev->via_config->udma_mask) {
1418c2ecf20Sopenharmony_ci	case ATA_UDMA2: t = timing->udma ? (0xe0 | (clamp_val(timing->udma, 2, 5) - 2)) : 0x03; break;
1428c2ecf20Sopenharmony_ci	case ATA_UDMA4: t = timing->udma ? (0xe8 | (clamp_val(timing->udma, 2, 9) - 2)) : 0x0f; break;
1438c2ecf20Sopenharmony_ci	case ATA_UDMA5: t = timing->udma ? (0xe0 | (clamp_val(timing->udma, 2, 9) - 2)) : 0x07; break;
1448c2ecf20Sopenharmony_ci	case ATA_UDMA6: t = timing->udma ? (0xe0 | (clamp_val(timing->udma, 2, 9) - 2)) : 0x07; break;
1458c2ecf20Sopenharmony_ci	}
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	/* Set UDMA unless device is not UDMA capable */
1488c2ecf20Sopenharmony_ci	if (vdev->via_config->udma_mask) {
1498c2ecf20Sopenharmony_ci		u8 udma_etc;
1508c2ecf20Sopenharmony_ci
1518c2ecf20Sopenharmony_ci		pci_read_config_byte(dev, VIA_UDMA_TIMING + 3 - dn, &udma_etc);
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci		/* clear transfer mode bit */
1548c2ecf20Sopenharmony_ci		udma_etc &= ~0x20;
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci		if (timing->udma) {
1578c2ecf20Sopenharmony_ci			/* preserve 80-wire cable detection bit */
1588c2ecf20Sopenharmony_ci			udma_etc &= 0x10;
1598c2ecf20Sopenharmony_ci			udma_etc |= t;
1608c2ecf20Sopenharmony_ci		}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci		pci_write_config_byte(dev, VIA_UDMA_TIMING + 3 - dn, udma_etc);
1638c2ecf20Sopenharmony_ci	}
1648c2ecf20Sopenharmony_ci}
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci/**
1678c2ecf20Sopenharmony_ci *	via_set_drive		-	configure transfer mode
1688c2ecf20Sopenharmony_ci *	@hwif: port
1698c2ecf20Sopenharmony_ci *	@drive: Drive to set up
1708c2ecf20Sopenharmony_ci *
1718c2ecf20Sopenharmony_ci *	via_set_drive() computes timing values configures the chipset to
1728c2ecf20Sopenharmony_ci *	a desired transfer mode.  It also can be called by upper layers.
1738c2ecf20Sopenharmony_ci */
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistatic void via_set_drive(ide_hwif_t *hwif, ide_drive_t *drive)
1768c2ecf20Sopenharmony_ci{
1778c2ecf20Sopenharmony_ci	ide_drive_t *peer = ide_get_pair_dev(drive);
1788c2ecf20Sopenharmony_ci	struct ide_host *host = dev_get_drvdata(hwif->dev);
1798c2ecf20Sopenharmony_ci	struct via82cxxx_dev *vdev = host->host_priv;
1808c2ecf20Sopenharmony_ci	struct ide_timing t, p;
1818c2ecf20Sopenharmony_ci	unsigned int T, UT;
1828c2ecf20Sopenharmony_ci	const u8 speed = drive->dma_mode;
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	T = 1000000000 / via_clock;
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	switch (vdev->via_config->udma_mask) {
1878c2ecf20Sopenharmony_ci	case ATA_UDMA2: UT = T;   break;
1888c2ecf20Sopenharmony_ci	case ATA_UDMA4: UT = T/2; break;
1898c2ecf20Sopenharmony_ci	case ATA_UDMA5: UT = T/3; break;
1908c2ecf20Sopenharmony_ci	case ATA_UDMA6: UT = T/4; break;
1918c2ecf20Sopenharmony_ci	default:	UT = T;
1928c2ecf20Sopenharmony_ci	}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_ci	ide_timing_compute(drive, speed, &t, T, UT);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (peer) {
1978c2ecf20Sopenharmony_ci		ide_timing_compute(peer, peer->pio_mode, &p, T, UT);
1988c2ecf20Sopenharmony_ci		ide_timing_merge(&p, &t, &t, IDE_TIMING_8BIT);
1998c2ecf20Sopenharmony_ci	}
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	via_set_speed(hwif, drive->dn, &t);
2028c2ecf20Sopenharmony_ci}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci/**
2058c2ecf20Sopenharmony_ci *	via_set_pio_mode	-	set host controller for PIO mode
2068c2ecf20Sopenharmony_ci *	@hwif: port
2078c2ecf20Sopenharmony_ci *	@drive: drive
2088c2ecf20Sopenharmony_ci *
2098c2ecf20Sopenharmony_ci *	A callback from the upper layers for PIO-only tuning.
2108c2ecf20Sopenharmony_ci */
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic void via_set_pio_mode(ide_hwif_t *hwif, ide_drive_t *drive)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	drive->dma_mode = drive->pio_mode;
2158c2ecf20Sopenharmony_ci	via_set_drive(hwif, drive);
2168c2ecf20Sopenharmony_ci}
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_cistatic struct via_isa_bridge *via_config_find(struct pci_dev **isa)
2198c2ecf20Sopenharmony_ci{
2208c2ecf20Sopenharmony_ci	struct via_isa_bridge *via_config;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	for (via_config = via_isa_bridges;
2238c2ecf20Sopenharmony_ci	     via_config->id != PCI_DEVICE_ID_VIA_ANON; via_config++)
2248c2ecf20Sopenharmony_ci		if ((*isa = pci_get_device(PCI_VENDOR_ID_VIA +
2258c2ecf20Sopenharmony_ci			!!(via_config->flags & VIA_BAD_ID),
2268c2ecf20Sopenharmony_ci			via_config->id, NULL))) {
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci			if ((*isa)->revision >= via_config->rev_min &&
2298c2ecf20Sopenharmony_ci			    (*isa)->revision <= via_config->rev_max)
2308c2ecf20Sopenharmony_ci				break;
2318c2ecf20Sopenharmony_ci			pci_dev_put(*isa);
2328c2ecf20Sopenharmony_ci		}
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	return via_config;
2358c2ecf20Sopenharmony_ci}
2368c2ecf20Sopenharmony_ci
2378c2ecf20Sopenharmony_ci/*
2388c2ecf20Sopenharmony_ci * Check and handle 80-wire cable presence
2398c2ecf20Sopenharmony_ci */
2408c2ecf20Sopenharmony_cistatic void via_cable_detect(struct via82cxxx_dev *vdev, u32 u)
2418c2ecf20Sopenharmony_ci{
2428c2ecf20Sopenharmony_ci	int i;
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	switch (vdev->via_config->udma_mask) {
2458c2ecf20Sopenharmony_ci		case ATA_UDMA4:
2468c2ecf20Sopenharmony_ci			for (i = 24; i >= 0; i -= 8)
2478c2ecf20Sopenharmony_ci				if (((u >> (i & 16)) & 8) &&
2488c2ecf20Sopenharmony_ci				    ((u >> i) & 0x20) &&
2498c2ecf20Sopenharmony_ci				     (((u >> i) & 7) < 2)) {
2508c2ecf20Sopenharmony_ci					/*
2518c2ecf20Sopenharmony_ci					 * 2x PCI clock and
2528c2ecf20Sopenharmony_ci					 * UDMA w/ < 3T/cycle
2538c2ecf20Sopenharmony_ci					 */
2548c2ecf20Sopenharmony_ci					vdev->via_80w |= (1 << (1 - (i >> 4)));
2558c2ecf20Sopenharmony_ci				}
2568c2ecf20Sopenharmony_ci			break;
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_ci		case ATA_UDMA5:
2598c2ecf20Sopenharmony_ci			for (i = 24; i >= 0; i -= 8)
2608c2ecf20Sopenharmony_ci				if (((u >> i) & 0x10) ||
2618c2ecf20Sopenharmony_ci				    (((u >> i) & 0x20) &&
2628c2ecf20Sopenharmony_ci				     (((u >> i) & 7) < 4))) {
2638c2ecf20Sopenharmony_ci					/* BIOS 80-wire bit or
2648c2ecf20Sopenharmony_ci					 * UDMA w/ < 60ns/cycle
2658c2ecf20Sopenharmony_ci					 */
2668c2ecf20Sopenharmony_ci					vdev->via_80w |= (1 << (1 - (i >> 4)));
2678c2ecf20Sopenharmony_ci				}
2688c2ecf20Sopenharmony_ci			break;
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_ci		case ATA_UDMA6:
2718c2ecf20Sopenharmony_ci			for (i = 24; i >= 0; i -= 8)
2728c2ecf20Sopenharmony_ci				if (((u >> i) & 0x10) ||
2738c2ecf20Sopenharmony_ci				    (((u >> i) & 0x20) &&
2748c2ecf20Sopenharmony_ci				     (((u >> i) & 7) < 6))) {
2758c2ecf20Sopenharmony_ci					/* BIOS 80-wire bit or
2768c2ecf20Sopenharmony_ci					 * UDMA w/ < 60ns/cycle
2778c2ecf20Sopenharmony_ci					 */
2788c2ecf20Sopenharmony_ci					vdev->via_80w |= (1 << (1 - (i >> 4)));
2798c2ecf20Sopenharmony_ci				}
2808c2ecf20Sopenharmony_ci			break;
2818c2ecf20Sopenharmony_ci	}
2828c2ecf20Sopenharmony_ci}
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci/**
2858c2ecf20Sopenharmony_ci *	init_chipset_via82cxxx	-	initialization handler
2868c2ecf20Sopenharmony_ci *	@dev: PCI device
2878c2ecf20Sopenharmony_ci *
2888c2ecf20Sopenharmony_ci *	The initialization callback. Here we determine the IDE chip type
2898c2ecf20Sopenharmony_ci *	and initialize its drive independent registers.
2908c2ecf20Sopenharmony_ci */
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_cistatic int init_chipset_via82cxxx(struct pci_dev *dev)
2938c2ecf20Sopenharmony_ci{
2948c2ecf20Sopenharmony_ci	struct ide_host *host = pci_get_drvdata(dev);
2958c2ecf20Sopenharmony_ci	struct via82cxxx_dev *vdev = host->host_priv;
2968c2ecf20Sopenharmony_ci	struct via_isa_bridge *via_config = vdev->via_config;
2978c2ecf20Sopenharmony_ci	u8 t, v;
2988c2ecf20Sopenharmony_ci	u32 u;
2998c2ecf20Sopenharmony_ci
3008c2ecf20Sopenharmony_ci	/*
3018c2ecf20Sopenharmony_ci	 * Detect cable and configure Clk66
3028c2ecf20Sopenharmony_ci	 */
3038c2ecf20Sopenharmony_ci	pci_read_config_dword(dev, VIA_UDMA_TIMING, &u);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	via_cable_detect(vdev, u);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci	if (via_config->udma_mask == ATA_UDMA4) {
3088c2ecf20Sopenharmony_ci		/* Enable Clk66 */
3098c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, VIA_UDMA_TIMING, u|0x80008);
3108c2ecf20Sopenharmony_ci	} else if (via_config->flags & VIA_BAD_CLK66) {
3118c2ecf20Sopenharmony_ci		/* Would cause trouble on 596a and 686 */
3128c2ecf20Sopenharmony_ci		pci_write_config_dword(dev, VIA_UDMA_TIMING, u & ~0x80008);
3138c2ecf20Sopenharmony_ci	}
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci	/*
3168c2ecf20Sopenharmony_ci	 * Check whether interfaces are enabled.
3178c2ecf20Sopenharmony_ci	 */
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, VIA_IDE_ENABLE, &v);
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci	/*
3228c2ecf20Sopenharmony_ci	 * Set up FIFO sizes and thresholds.
3238c2ecf20Sopenharmony_ci	 */
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	pci_read_config_byte(dev, VIA_FIFO_CONFIG, &t);
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	/* Disable PREQ# till DDACK# */
3288c2ecf20Sopenharmony_ci	if (via_config->flags & VIA_BAD_PREQ) {
3298c2ecf20Sopenharmony_ci		/* Would crash on 586b rev 41 */
3308c2ecf20Sopenharmony_ci		t &= 0x7f;
3318c2ecf20Sopenharmony_ci	}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	/* Fix FIFO split between channels */
3348c2ecf20Sopenharmony_ci	if (via_config->flags & VIA_SET_FIFO) {
3358c2ecf20Sopenharmony_ci		t &= (t & 0x9f);
3368c2ecf20Sopenharmony_ci		switch (v & 3) {
3378c2ecf20Sopenharmony_ci			case 2: t |= 0x00; break;	/* 16 on primary */
3388c2ecf20Sopenharmony_ci			case 1: t |= 0x60; break;	/* 16 on secondary */
3398c2ecf20Sopenharmony_ci			case 3: t |= 0x20; break;	/* 8 pri 8 sec */
3408c2ecf20Sopenharmony_ci		}
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	pci_write_config_byte(dev, VIA_FIFO_CONFIG, t);
3448c2ecf20Sopenharmony_ci
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci/*
3498c2ecf20Sopenharmony_ci *	Cable special cases
3508c2ecf20Sopenharmony_ci */
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic const struct dmi_system_id cable_dmi_table[] = {
3538c2ecf20Sopenharmony_ci	{
3548c2ecf20Sopenharmony_ci		.ident = "Acer Ferrari 3400",
3558c2ecf20Sopenharmony_ci		.matches = {
3568c2ecf20Sopenharmony_ci			DMI_MATCH(DMI_BOARD_VENDOR, "Acer,Inc."),
3578c2ecf20Sopenharmony_ci			DMI_MATCH(DMI_BOARD_NAME, "Ferrari 3400"),
3588c2ecf20Sopenharmony_ci		},
3598c2ecf20Sopenharmony_ci	},
3608c2ecf20Sopenharmony_ci	{ }
3618c2ecf20Sopenharmony_ci};
3628c2ecf20Sopenharmony_ci
3638c2ecf20Sopenharmony_cistatic int via_cable_override(struct pci_dev *pdev)
3648c2ecf20Sopenharmony_ci{
3658c2ecf20Sopenharmony_ci	/* Systems by DMI */
3668c2ecf20Sopenharmony_ci	if (dmi_check_system(cable_dmi_table))
3678c2ecf20Sopenharmony_ci		return 1;
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	/* Arima W730-K8/Targa Visionary 811/... */
3708c2ecf20Sopenharmony_ci	if (pdev->subsystem_vendor == 0x161F &&
3718c2ecf20Sopenharmony_ci	    pdev->subsystem_device == 0x2032)
3728c2ecf20Sopenharmony_ci		return 1;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	return 0;
3758c2ecf20Sopenharmony_ci}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_cistatic u8 via82cxxx_cable_detect(ide_hwif_t *hwif)
3788c2ecf20Sopenharmony_ci{
3798c2ecf20Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(hwif->dev);
3808c2ecf20Sopenharmony_ci	struct ide_host *host = pci_get_drvdata(pdev);
3818c2ecf20Sopenharmony_ci	struct via82cxxx_dev *vdev = host->host_priv;
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_ci	if (via_cable_override(pdev))
3848c2ecf20Sopenharmony_ci		return ATA_CBL_PATA40_SHORT;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	if ((vdev->via_config->flags & VIA_SATA_PATA) && hwif->channel == 0)
3878c2ecf20Sopenharmony_ci		return ATA_CBL_SATA;
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci	if ((vdev->via_80w >> hwif->channel) & 1)
3908c2ecf20Sopenharmony_ci		return ATA_CBL_PATA80;
3918c2ecf20Sopenharmony_ci	else
3928c2ecf20Sopenharmony_ci		return ATA_CBL_PATA40;
3938c2ecf20Sopenharmony_ci}
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_cistatic const struct ide_port_ops via_port_ops = {
3968c2ecf20Sopenharmony_ci	.set_pio_mode		= via_set_pio_mode,
3978c2ecf20Sopenharmony_ci	.set_dma_mode		= via_set_drive,
3988c2ecf20Sopenharmony_ci	.cable_detect		= via82cxxx_cable_detect,
3998c2ecf20Sopenharmony_ci};
4008c2ecf20Sopenharmony_ci
4018c2ecf20Sopenharmony_cistatic const struct ide_port_info via82cxxx_chipset = {
4028c2ecf20Sopenharmony_ci	.name		= DRV_NAME,
4038c2ecf20Sopenharmony_ci	.init_chipset	= init_chipset_via82cxxx,
4048c2ecf20Sopenharmony_ci	.enablebits	= { { 0x40, 0x02, 0x02 }, { 0x40, 0x01, 0x01 } },
4058c2ecf20Sopenharmony_ci	.port_ops	= &via_port_ops,
4068c2ecf20Sopenharmony_ci	.host_flags	= IDE_HFLAG_PIO_NO_BLACKLIST |
4078c2ecf20Sopenharmony_ci			  IDE_HFLAG_POST_SET_MODE |
4088c2ecf20Sopenharmony_ci			  IDE_HFLAG_IO_32BIT,
4098c2ecf20Sopenharmony_ci	.pio_mask	= ATA_PIO5,
4108c2ecf20Sopenharmony_ci	.swdma_mask	= ATA_SWDMA2,
4118c2ecf20Sopenharmony_ci	.mwdma_mask	= ATA_MWDMA2,
4128c2ecf20Sopenharmony_ci};
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic int via_init_one(struct pci_dev *dev, const struct pci_device_id *id)
4158c2ecf20Sopenharmony_ci{
4168c2ecf20Sopenharmony_ci	struct pci_dev *isa = NULL;
4178c2ecf20Sopenharmony_ci	struct via_isa_bridge *via_config;
4188c2ecf20Sopenharmony_ci	struct via82cxxx_dev *vdev;
4198c2ecf20Sopenharmony_ci	int rc;
4208c2ecf20Sopenharmony_ci	u8 idx = id->driver_data;
4218c2ecf20Sopenharmony_ci	struct ide_port_info d;
4228c2ecf20Sopenharmony_ci
4238c2ecf20Sopenharmony_ci	d = via82cxxx_chipset;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci	/*
4268c2ecf20Sopenharmony_ci	 * Find the ISA bridge and check we know what it is.
4278c2ecf20Sopenharmony_ci	 */
4288c2ecf20Sopenharmony_ci	via_config = via_config_find(&isa);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci	/*
4318c2ecf20Sopenharmony_ci	 * Print the boot message.
4328c2ecf20Sopenharmony_ci	 */
4338c2ecf20Sopenharmony_ci	printk(KERN_INFO DRV_NAME " %s: VIA %s (rev %02x) IDE %sDMA%s\n",
4348c2ecf20Sopenharmony_ci		pci_name(dev), via_config->name, isa->revision,
4358c2ecf20Sopenharmony_ci		via_config->udma_mask ? "U" : "MW",
4368c2ecf20Sopenharmony_ci		via_dma[via_config->udma_mask ?
4378c2ecf20Sopenharmony_ci			(fls(via_config->udma_mask) - 1) : 0]);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci	pci_dev_put(isa);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	/*
4428c2ecf20Sopenharmony_ci	 * Determine system bus clock.
4438c2ecf20Sopenharmony_ci	 */
4448c2ecf20Sopenharmony_ci	via_clock = (ide_pci_clk ? ide_pci_clk : 33) * 1000;
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_ci	switch (via_clock) {
4478c2ecf20Sopenharmony_ci	case 33000: via_clock = 33333; break;
4488c2ecf20Sopenharmony_ci	case 37000: via_clock = 37500; break;
4498c2ecf20Sopenharmony_ci	case 41000: via_clock = 41666; break;
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci
4528c2ecf20Sopenharmony_ci	if (via_clock < 20000 || via_clock > 50000) {
4538c2ecf20Sopenharmony_ci		printk(KERN_WARNING DRV_NAME ": User given PCI clock speed "
4548c2ecf20Sopenharmony_ci			"impossible (%d), using 33 MHz instead.\n", via_clock);
4558c2ecf20Sopenharmony_ci		via_clock = 33333;
4568c2ecf20Sopenharmony_ci	}
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	if (idx == 1)
4598c2ecf20Sopenharmony_ci		d.enablebits[1].reg = d.enablebits[0].reg = 0;
4608c2ecf20Sopenharmony_ci	else
4618c2ecf20Sopenharmony_ci		d.host_flags |= IDE_HFLAG_NO_AUTODMA;
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	if (idx == VIA_IDFLAG_SINGLE)
4648c2ecf20Sopenharmony_ci		d.host_flags |= IDE_HFLAG_SINGLE;
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	if ((via_config->flags & VIA_NO_UNMASK) == 0)
4678c2ecf20Sopenharmony_ci		d.host_flags |= IDE_HFLAG_UNMASK_IRQS;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	d.udma_mask = via_config->udma_mask;
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	vdev = kzalloc(sizeof(*vdev), GFP_KERNEL);
4728c2ecf20Sopenharmony_ci	if (!vdev) {
4738c2ecf20Sopenharmony_ci		printk(KERN_ERR DRV_NAME " %s: out of memory :(\n",
4748c2ecf20Sopenharmony_ci			pci_name(dev));
4758c2ecf20Sopenharmony_ci		return -ENOMEM;
4768c2ecf20Sopenharmony_ci	}
4778c2ecf20Sopenharmony_ci
4788c2ecf20Sopenharmony_ci	vdev->via_config = via_config;
4798c2ecf20Sopenharmony_ci
4808c2ecf20Sopenharmony_ci	rc = ide_pci_init_one(dev, &d, vdev);
4818c2ecf20Sopenharmony_ci	if (rc)
4828c2ecf20Sopenharmony_ci		kfree(vdev);
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_ci	return rc;
4858c2ecf20Sopenharmony_ci}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_cistatic void via_remove(struct pci_dev *dev)
4888c2ecf20Sopenharmony_ci{
4898c2ecf20Sopenharmony_ci	struct ide_host *host = pci_get_drvdata(dev);
4908c2ecf20Sopenharmony_ci	struct via82cxxx_dev *vdev = host->host_priv;
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci	ide_pci_remove(dev);
4938c2ecf20Sopenharmony_ci	kfree(vdev);
4948c2ecf20Sopenharmony_ci}
4958c2ecf20Sopenharmony_ci
4968c2ecf20Sopenharmony_cistatic const struct pci_device_id via_pci_tbl[] = {
4978c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C576_1),  0 },
4988c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_82C586_1),  0 },
4998c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_CX700_IDE), 0 },
5008c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_VX855_IDE), VIA_IDFLAG_SINGLE },
5018c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_6410),      1 },
5028c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_6415),      1 },
5038c2ecf20Sopenharmony_ci	{ PCI_VDEVICE(VIA, PCI_DEVICE_ID_VIA_SATA_EIDE), 1 },
5048c2ecf20Sopenharmony_ci	{ 0, },
5058c2ecf20Sopenharmony_ci};
5068c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(pci, via_pci_tbl);
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_cistatic struct pci_driver via_pci_driver = {
5098c2ecf20Sopenharmony_ci	.name 		= "VIA_IDE",
5108c2ecf20Sopenharmony_ci	.id_table 	= via_pci_tbl,
5118c2ecf20Sopenharmony_ci	.probe 		= via_init_one,
5128c2ecf20Sopenharmony_ci	.remove		= via_remove,
5138c2ecf20Sopenharmony_ci	.suspend	= ide_pci_suspend,
5148c2ecf20Sopenharmony_ci	.resume		= ide_pci_resume,
5158c2ecf20Sopenharmony_ci};
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_cistatic int __init via_ide_init(void)
5188c2ecf20Sopenharmony_ci{
5198c2ecf20Sopenharmony_ci	return ide_pci_register_driver(&via_pci_driver);
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic void __exit via_ide_exit(void)
5238c2ecf20Sopenharmony_ci{
5248c2ecf20Sopenharmony_ci	pci_unregister_driver(&via_pci_driver);
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cimodule_init(via_ide_init);
5288c2ecf20Sopenharmony_cimodule_exit(via_ide_exit);
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vojtech Pavlik, Bartlomiej Zolnierkiewicz, Michel Aubry, Jeff Garzik, Andre Hedrick");
5318c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("PCI driver module for VIA IDE");
5328c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
533