pata_via.c 19.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * pata_via.c 	- VIA PATA for new ATA layer
 *			  (C) 2005-2006 Red Hat Inc
 *
 *  Documentation
 *	Most chipset documentation available under NDA only
 *
 *  VIA version guide
 *	VIA VT82C561	-	early design, uses ata_generic currently
 *	VIA VT82C576	-	MWDMA, 33Mhz
 *	VIA VT82C586	-	MWDMA, 33Mhz
 *	VIA VT82C586a	-	Added UDMA to 33Mhz
 *	VIA VT82C586b	-	UDMA33
 *	VIA VT82C596a	-	Nonfunctional UDMA66
 *	VIA VT82C596b	-	Working UDMA66
 *	VIA VT82C686	-	Nonfunctional UDMA66
 *	VIA VT82C686a	-	Working UDMA66
 *	VIA VT82C686b	-	Updated to UDMA100
 *	VIA VT8231	-	UDMA100
 *	VIA VT8233	-	UDMA100
 *	VIA VT8233a	-	UDMA133
 *	VIA VT8233c	-	UDMA100
 *	VIA VT8235	-	UDMA133
 *	VIA VT8237	-	UDMA133
25
 *	VIA VT8237A	-	UDMA133
Alan's avatar
Alan committed
26
 *	VIA VT8237S	-	UDMA133
27
 *	VIA VT8251	-	UDMA133
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
 *
 *	Most registers remain compatible across chips. Others start reserved
 *	and acquire sensible semantics if set to 1 (eg cable detect). A few
 *	exceptions exist, notably around the FIFO settings.
 *
 *	One additional quirk of the VIA design is that like ALi they use few
 *	PCI IDs for a lot of chips.
 *
 *	Based heavily on:
 *
 * Version 3.38
 *
 * VIA IDE driver for Linux. Supported southbridges:
 *
 *   vt82c576, vt82c586, vt82c586a, vt82c586b, vt82c596a, vt82c596b,
 *   vt82c686, vt82c686a, vt82c686b, vt8231, vt8233, vt8233c, vt8233a,
 *   vt8235, vt8237
 *
 * Copyright (c) 2000-2002 Vojtech Pavlik
 *
 * Based on the work of:
 *	Michel Aubry
 *	Jeff Garzik
 *	Andre Hedrick

 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
61
#include <linux/gfp.h>
62 63
#include <scsi/scsi_host.h>
#include <linux/libata.h>
Alan Cox's avatar
Alan Cox committed
64
#include <linux/dmi.h>
65 66

#define DRV_NAME "pata_via"
67
#define DRV_VERSION "0.3.4"
68 69

enum {
70 71 72 73 74 75 76 77
	VIA_BAD_PREQ	= 0x01, /* Crashes if PREQ# till DDACK# set */
	VIA_BAD_CLK66	= 0x02, /* 66 MHz clock doesn't work correctly */
	VIA_SET_FIFO	= 0x04, /* Needs to have FIFO split set */
	VIA_NO_UNMASK	= 0x08, /* Doesn't work with IRQ unmasking on */
	VIA_BAD_ID	= 0x10, /* Has wrong vendor ID (0x1107) */
	VIA_BAD_AST	= 0x20, /* Don't touch Address Setup Timing */
	VIA_NO_ENABLES	= 0x40, /* Has no enablebits */
	VIA_SATA_PATA	= 0x80, /* SATA/PATA combined configuration */
78 79
};

80 81 82 83
enum {
	VIA_IDFLAG_SINGLE = (1 << 0), /* single channel controller) */
};

84 85 86 87 88 89 90 91 92
/*
 * VIA SouthBridge chips.
 */

static const struct via_isa_bridge {
	const char *name;
	u16 id;
	u8 rev_min;
	u8 rev_max;
93 94
	u8 udma_mask;
	u8 flags;
95
} via_isa_bridges[] = {
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
	{ "vx855",	PCI_DEVICE_ID_VIA_VX855,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
	{ "vx800",	PCI_DEVICE_ID_VIA_VX800,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
	{ "vt8261",	PCI_DEVICE_ID_VIA_8261,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "vt8237s",	PCI_DEVICE_ID_VIA_8237S,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "vt8251",	PCI_DEVICE_ID_VIA_8251,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "cx700",	PCI_DEVICE_ID_VIA_CX700,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_SATA_PATA },
	{ "vt6410",	PCI_DEVICE_ID_VIA_6410,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST | VIA_NO_ENABLES },
	{ "vt6415",	PCI_DEVICE_ID_VIA_6415,     0x00, 0xff, ATA_UDMA6, VIA_BAD_AST | VIA_NO_ENABLES },
	{ "vt8237a",	PCI_DEVICE_ID_VIA_8237A,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "vt8237",	PCI_DEVICE_ID_VIA_8237,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "vt8235",	PCI_DEVICE_ID_VIA_8235,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "vt8233a",	PCI_DEVICE_ID_VIA_8233A,    0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
	{ "vt8233c",	PCI_DEVICE_ID_VIA_8233C_0,  0x00, 0x2f, ATA_UDMA5, },
	{ "vt8233",	PCI_DEVICE_ID_VIA_8233_0,   0x00, 0x2f, ATA_UDMA5, },
	{ "vt8231",	PCI_DEVICE_ID_VIA_8231,     0x00, 0x2f, ATA_UDMA5, },
	{ "vt82c686b",	PCI_DEVICE_ID_VIA_82C686,   0x40, 0x4f, ATA_UDMA5, },
	{ "vt82c686a",	PCI_DEVICE_ID_VIA_82C686,   0x10, 0x2f, ATA_UDMA4, },
	{ "vt82c686",	PCI_DEVICE_ID_VIA_82C686,   0x00, 0x0f, ATA_UDMA2, VIA_BAD_CLK66 },
	{ "vt82c596b",	PCI_DEVICE_ID_VIA_82C596,   0x10, 0x2f, ATA_UDMA4, },
	{ "vt82c596a",	PCI_DEVICE_ID_VIA_82C596,   0x00, 0x0f, ATA_UDMA2, VIA_BAD_CLK66 },
	{ "vt82c586b",	PCI_DEVICE_ID_VIA_82C586_0, 0x47, 0x4f, ATA_UDMA2, VIA_SET_FIFO },
	{ "vt82c586b",	PCI_DEVICE_ID_VIA_82C586_0, 0x40, 0x46, ATA_UDMA2, VIA_SET_FIFO | VIA_BAD_PREQ },
	{ "vt82c586b",	PCI_DEVICE_ID_VIA_82C586_0, 0x30, 0x3f, ATA_UDMA2, VIA_SET_FIFO },
	{ "vt82c586a",	PCI_DEVICE_ID_VIA_82C586_0, 0x20, 0x2f, ATA_UDMA2, VIA_SET_FIFO },
	{ "vt82c586",	PCI_DEVICE_ID_VIA_82C586_0, 0x00, 0x0f,      0x00, VIA_SET_FIFO },
	{ "vt82c576",	PCI_DEVICE_ID_VIA_82C576,   0x00, 0x2f,      0x00, VIA_SET_FIFO | VIA_NO_UNMASK },
	{ "vt82c576",	PCI_DEVICE_ID_VIA_82C576,   0x00, 0x2f,      0x00, VIA_SET_FIFO | VIA_NO_UNMASK | VIA_BAD_ID },
	{ "vtxxxx",	PCI_DEVICE_ID_VIA_ANON,     0x00, 0x2f, ATA_UDMA6, VIA_BAD_AST },
124 125 126
	{ NULL }
};

127 128 129
struct via_port {
	u8 cached_device;
};
Alan Cox's avatar
Alan Cox committed
130 131 132 133 134

/*
 *	Cable special cases
 */

135
static const struct dmi_system_id cable_dmi_table[] = {
Alan Cox's avatar
Alan Cox committed
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
	{
		.ident = "Acer Ferrari 3400",
		.matches = {
			DMI_MATCH(DMI_BOARD_VENDOR, "Acer,Inc."),
			DMI_MATCH(DMI_BOARD_NAME, "Ferrari 3400"),
		},
	},
	{ }
};

static int via_cable_override(struct pci_dev *pdev)
{
	/* Systems by DMI */
	if (dmi_check_system(cable_dmi_table))
		return 1;
151 152 153
	/* Arima W730-K8/Targa Visionary 811/... */
	if (pdev->subsystem_vendor == 0x161F && pdev->subsystem_device == 0x2032)
		return 1;
Alan Cox's avatar
Alan Cox committed
154 155 156 157
	return 0;
}


158 159 160 161 162 163 164 165 166 167 168 169 170
/**
 *	via_cable_detect	-	cable detection
 *	@ap: ATA port
 *
 *	Perform cable detection. Actually for the VIA case the BIOS
 *	already did this for us. We read the values provided by the
 *	BIOS. If you are using an 8235 in a non-PC configuration you
 *	may need to update this code.
 *
 *	Hotplug also impacts on this.
 */

static int via_cable_detect(struct ata_port *ap) {
Alan Cox's avatar
Alan Cox committed
171
	const struct via_isa_bridge *config = ap->host->private_data;
172 173 174
	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
	u32 ata66;

Alan Cox's avatar
Alan Cox committed
175 176 177
	if (via_cable_override(pdev))
		return ATA_CBL_PATA40_SHORT;

178 179 180
	if ((config->flags & VIA_SATA_PATA) && ap->port_no == 0)
		return ATA_CBL_SATA;

Alan Cox's avatar
Alan Cox committed
181
	/* Early chips are 40 wire */
182
	if (config->udma_mask < ATA_UDMA4)
Alan Cox's avatar
Alan Cox committed
183 184
		return ATA_CBL_PATA40;
	/* UDMA 66 chips have only drive side logic */
185
	else if (config->udma_mask < ATA_UDMA5)
Alan Cox's avatar
Alan Cox committed
186 187
		return ATA_CBL_PATA_UNK;
	/* UDMA 100 or later */
188 189 190 191 192
	pci_read_config_dword(pdev, 0x50, &ata66);
	/* Check both the drive cable reporting bits, we might not have
	   two drives */
	if (ata66 & (0x10100000 >> (16 * ap->port_no)))
		return ATA_CBL_PATA80;
193
	/* Check with ACPI so we can spot BIOS reported SATA bridges */
194 195
	if (ata_acpi_init_gtm(ap) &&
	    ata_acpi_cbl_80wire(ap, ata_acpi_init_gtm(ap)))
196
		return ATA_CBL_PATA80;
Alan Cox's avatar
Alan Cox committed
197
	return ATA_CBL_PATA40;
198 199
}

Tejun Heo's avatar
Tejun Heo committed
200
static int via_pre_reset(struct ata_link *link, unsigned long deadline)
201
{
Tejun Heo's avatar
Tejun Heo committed
202
	struct ata_port *ap = link->ap;
203 204 205 206 207 208 209 210
	const struct via_isa_bridge *config = ap->host->private_data;

	if (!(config->flags & VIA_NO_ENABLES)) {
		static const struct pci_bits via_enable_bits[] = {
			{ 0x40, 1, 0x02, 0x02 },
			{ 0x40, 1, 0x01, 0x01 }
		};
		struct pci_dev *pdev = to_pci_dev(ap->host->dev);
211 212
		if (!pci_test_config_bits(pdev, &via_enable_bits[ap->port_no]))
			return -ENOENT;
213
	}
214

Tejun Heo's avatar
Tejun Heo committed
215
	return ata_sff_prereset(link, deadline);
216 217 218 219
}


/**
220
 *	via_do_set_mode	-	set transfer mode data
221 222 223 224 225 226 227 228 229 230 231 232 233
 *	@ap: ATA interface
 *	@adev: ATA device
 *	@mode: ATA mode being programmed
 *	@set_ast: Set to program address setup
 *	@udma_type: UDMA mode/format of registers
 *
 *	Program the VIA registers for DMA and PIO modes. Uses the ata timing
 *	support in order to compute modes.
 *
 *	FIXME: Hotplug will require we serialize multiple mode changes
 *	on the two channels.
 */

234 235
static void via_do_set_mode(struct ata_port *ap, struct ata_device *adev,
			    int mode, int set_ast, int udma_type)
236 237 238 239
{
	struct pci_dev *pdev = to_pci_dev(ap->host->dev);
	struct ata_device *peer = ata_dev_pair(adev);
	struct ata_timing t, p;
240
	static int via_clock = 33333;	/* Bus clock in kHZ */
241
	unsigned long T =  1000000000 / via_clock;
242
	unsigned long UT = T;
243 244 245
	int ut;
	int offset = 3 - (2*ap->port_no) - adev->devno;

246 247 248 249 250 251 252 253 254
	switch (udma_type) {
	case ATA_UDMA4:
		UT = T / 2; break;
	case ATA_UDMA5:
		UT = T / 3; break;
	case ATA_UDMA6:
		UT = T / 4; break;
	}

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
	/* Calculate the timing values we require */
	ata_timing_compute(adev, mode, &t, T, UT);

	/* We share 8bit timing so we must merge the constraints */
	if (peer) {
		if (peer->pio_mode) {
			ata_timing_compute(peer, peer->pio_mode, &p, T, UT);
			ata_timing_merge(&p, &t, &t, ATA_TIMING_8BIT);
		}
	}

	/* Address setup is programmable but breaks on UDMA133 setups */
	if (set_ast) {
		u8 setup;	/* 2 bits per drive */
		int shift = 2 * offset;

		pci_read_config_byte(pdev, 0x4C, &setup);
		setup &= ~(3 << shift);
273
		setup |= (clamp_val(t.setup, 1, 4) - 1) << shift;
274 275 276 277 278
		pci_write_config_byte(pdev, 0x4C, setup);
	}

	/* Load the PIO mode bits */
	pci_write_config_byte(pdev, 0x4F - ap->port_no,
Harvey Harrison's avatar
Harvey Harrison committed
279
		((clamp_val(t.act8b, 1, 16) - 1) << 4) | (clamp_val(t.rec8b, 1, 16) - 1));
280
	pci_write_config_byte(pdev, 0x48 + offset,
Harvey Harrison's avatar
Harvey Harrison committed
281
		((clamp_val(t.active, 1, 16) - 1) << 4) | (clamp_val(t.recover, 1, 16) - 1));
282 283

	/* Load the UDMA bits according to type */
284 285 286 287 288 289 290 291 292 293 294 295 296 297
	switch (udma_type) {
	case ATA_UDMA2:
	default:
		ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 5) - 2)) : 0x03;
		break;
	case ATA_UDMA4:
		ut = t.udma ? (0xe8 | (clamp_val(t.udma, 2, 9) - 2)) : 0x0f;
		break;
	case ATA_UDMA5:
		ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 9) - 2)) : 0x07;
		break;
	case ATA_UDMA6:
		ut = t.udma ? (0xe0 | (clamp_val(t.udma, 2, 9) - 2)) : 0x07;
		break;
298
	}
299

300
	/* Set UDMA unless device is not UDMA capable */
301 302
	if (udma_type) {
		u8 udma_etc;
303

304
		pci_read_config_byte(pdev, 0x50 + offset, &udma_etc);
305

306 307 308 309 310 311 312 313 314 315
		/* clear transfer mode bit */
		udma_etc &= ~0x20;

		if (t.udma) {
			/* preserve 80-wire cable detection bit */
			udma_etc &= 0x10;
			udma_etc |= ut;
		}

		pci_write_config_byte(pdev, 0x50 + offset, udma_etc);
316
	}
317 318 319 320 321 322 323
}

static void via_set_piomode(struct ata_port *ap, struct ata_device *adev)
{
	const struct via_isa_bridge *config = ap->host->private_data;
	int set_ast = (config->flags & VIA_BAD_AST) ? 0 : 1;

324
	via_do_set_mode(ap, adev, adev->pio_mode, set_ast, config->udma_mask);
325 326 327 328 329 330 331
}

static void via_set_dmamode(struct ata_port *ap, struct ata_device *adev)
{
	const struct via_isa_bridge *config = ap->host->private_data;
	int set_ast = (config->flags & VIA_BAD_AST) ? 0 : 1;

332
	via_do_set_mode(ap, adev, adev->dma_mode, set_ast, config->udma_mask);
333 334
}

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
/**
 *	via_mode_filter		-	filter buggy device/mode pairs
 *	@dev: ATA device
 *	@mask: Mode bitmask
 *
 *	We need to apply some minimal filtering for old controllers and at least
 *	one breed of Transcend SSD. Return the updated mask.
 */

static unsigned long via_mode_filter(struct ata_device *dev, unsigned long mask)
{
	struct ata_host *host = dev->link->ap->host;
	const struct via_isa_bridge *config = host->private_data;
	unsigned char model_num[ATA_ID_PROD_LEN + 1];

	if (config->id == PCI_DEVICE_ID_VIA_82C586_0) {
		ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
		if (strcmp(model_num, "TS64GSSD25-M") == 0) {
			ata_dev_printk(dev, KERN_WARNING,
	"disabling UDMA mode due to reported lockups with this device.\n");
			mask &= ~ ATA_MASK_UDMA;
		}
	}
358
	return mask;
359 360
}

361
/**
362
 *	via_tf_load - send taskfile registers to host controller
363 364 365 366 367 368
 *	@ap: Port to which output is sent
 *	@tf: ATA taskfile register set
 *
 *	Outputs ATA taskfile to standard ATA host controller.
 *
 *	Note: This is to fix the internal bug of via chipsets, which
369 370
 *	will reset the device register after changing the IEN bit on
 *	ctl register
371
 */
372
static void via_tf_load(struct ata_port *ap, const struct ata_taskfile *tf)
373
{
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405
	struct ata_ioports *ioaddr = &ap->ioaddr;
	struct via_port *vp = ap->private_data;
	unsigned int is_addr = tf->flags & ATA_TFLAG_ISADDR;
	int newctl = 0;

	if (tf->ctl != ap->last_ctl) {
		iowrite8(tf->ctl, ioaddr->ctl_addr);
		ap->last_ctl = tf->ctl;
		ata_wait_idle(ap);
		newctl = 1;
	}

	if (tf->flags & ATA_TFLAG_DEVICE) {
		iowrite8(tf->device, ioaddr->device_addr);
		vp->cached_device = tf->device;
	} else if (newctl)
		iowrite8(vp->cached_device, ioaddr->device_addr);

	if (is_addr && (tf->flags & ATA_TFLAG_LBA48)) {
		WARN_ON_ONCE(!ioaddr->ctl_addr);
		iowrite8(tf->hob_feature, ioaddr->feature_addr);
		iowrite8(tf->hob_nsect, ioaddr->nsect_addr);
		iowrite8(tf->hob_lbal, ioaddr->lbal_addr);
		iowrite8(tf->hob_lbam, ioaddr->lbam_addr);
		iowrite8(tf->hob_lbah, ioaddr->lbah_addr);
		VPRINTK("hob: feat 0x%X nsect 0x%X, lba 0x%X 0x%X 0x%X\n",
			tf->hob_feature,
			tf->hob_nsect,
			tf->hob_lbal,
			tf->hob_lbam,
			tf->hob_lbah);
	}
406

407 408 409 410 411 412 413 414 415 416 417 418
	if (is_addr) {
		iowrite8(tf->feature, ioaddr->feature_addr);
		iowrite8(tf->nsect, ioaddr->nsect_addr);
		iowrite8(tf->lbal, ioaddr->lbal_addr);
		iowrite8(tf->lbam, ioaddr->lbam_addr);
		iowrite8(tf->lbah, ioaddr->lbah_addr);
		VPRINTK("feat 0x%X nsect 0x%X lba 0x%X 0x%X 0x%X\n",
			tf->feature,
			tf->nsect,
			tf->lbal,
			tf->lbam,
			tf->lbah);
419
	}
420 421 422 423 424 425 426
}

static int via_port_start(struct ata_port *ap)
{
	struct via_port *vp;
	struct pci_dev *pdev = to_pci_dev(ap->host->dev);

427
	int ret = ata_bmdma_port_start(ap);
428 429 430 431 432 433 434 435
	if (ret < 0)
		return ret;

	vp = devm_kzalloc(&pdev->dev, sizeof(struct via_port), GFP_KERNEL);
	if (vp == NULL)
		return -ENOMEM;
	ap->private_data = vp;
	return 0;
436 437
}

438
static struct scsi_host_template via_sht = {
439
	ATA_BMDMA_SHT(DRV_NAME),
440 441 442
};

static struct ata_port_operations via_port_ops = {
443 444
	.inherits	= &ata_bmdma_port_ops,
	.cable_detect	= via_cable_detect,
445 446
	.set_piomode	= via_set_piomode,
	.set_dmamode	= via_set_dmamode,
447
	.prereset	= via_pre_reset,
448
	.sff_tf_load	= via_tf_load,
449
	.port_start	= via_port_start,
450
	.mode_filter	= via_mode_filter,
451 452 453
};

static struct ata_port_operations via_port_ops_noirq = {
454
	.inherits	= &via_port_ops,
Tejun Heo's avatar
Tejun Heo committed
455
	.sff_data_xfer	= ata_sff_data_xfer_noirq,
456 457
};

458 459 460 461 462
/**
 *	via_config_fifo		-	set up the FIFO
 *	@pdev: PCI device
 *	@flags: configuration flags
 *
463
 *	Set the FIFO properties for this device if necessary. Used both on
464 465 466 467 468 469
 *	set up and on and the resume path
 */

static void via_config_fifo(struct pci_dev *pdev, unsigned int flags)
{
	u8 enable;
470

471 472 473
	/* 0x40 low bits indicate enabled channels */
	pci_read_config_byte(pdev, 0x40 , &enable);
	enable &= 3;
474

475
	if (flags & VIA_SET_FIFO) {
476
		static const u8 fifo_setting[4] = {0x00, 0x60, 0x00, 0x20};
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
		u8 fifo;

		pci_read_config_byte(pdev, 0x43, &fifo);

		/* Clear PREQ# until DDACK# for errata */
		if (flags & VIA_BAD_PREQ)
			fifo &= 0x7F;
		else
			fifo &= 0x9f;
		/* Turn on FIFO for enabled channels */
		fifo |= fifo_setting[enable];
		pci_write_config_byte(pdev, 0x43, fifo);
	}
}

492 493
/**
 *	via_init_one		-	discovery callback
494
 *	@pdev: PCI device
495 496 497 498 499 500 501 502 503
 *	@id: PCI table info
 *
 *	A VIA IDE interface has been discovered. Figure out what revision
 *	and perform configuration work before handing it to the ATA layer
 */

static int via_init_one(struct pci_dev *pdev, const struct pci_device_id *id)
{
	/* Early VIA without UDMA support */
Tejun Heo's avatar
Tejun Heo committed
504
	static const struct ata_port_info via_mwdma_info = {
505
		.flags = ATA_FLAG_SLAVE_POSS,
506 507
		.pio_mask = ATA_PIO4,
		.mwdma_mask = ATA_MWDMA2,
508 509 510
		.port_ops = &via_port_ops
	};
	/* Ditto with IRQ masking required */
Tejun Heo's avatar
Tejun Heo committed
511
	static const struct ata_port_info via_mwdma_info_borked = {
512
		.flags = ATA_FLAG_SLAVE_POSS,
513 514
		.pio_mask = ATA_PIO4,
		.mwdma_mask = ATA_MWDMA2,
515 516 517
		.port_ops = &via_port_ops_noirq,
	};
	/* VIA UDMA 33 devices (and borked 66) */
Tejun Heo's avatar
Tejun Heo committed
518
	static const struct ata_port_info via_udma33_info = {
519
		.flags = ATA_FLAG_SLAVE_POSS,
520 521
		.pio_mask = ATA_PIO4,
		.mwdma_mask = ATA_MWDMA2,
522
		.udma_mask = ATA_UDMA2,
523 524 525
		.port_ops = &via_port_ops
	};
	/* VIA UDMA 66 devices */
Tejun Heo's avatar
Tejun Heo committed
526
	static const struct ata_port_info via_udma66_info = {
527
		.flags = ATA_FLAG_SLAVE_POSS,
528 529
		.pio_mask = ATA_PIO4,
		.mwdma_mask = ATA_MWDMA2,
530
		.udma_mask = ATA_UDMA4,
531 532 533
		.port_ops = &via_port_ops
	};
	/* VIA UDMA 100 devices */
Tejun Heo's avatar
Tejun Heo committed
534
	static const struct ata_port_info via_udma100_info = {
535
		.flags = ATA_FLAG_SLAVE_POSS,
536 537
		.pio_mask = ATA_PIO4,
		.mwdma_mask = ATA_MWDMA2,
538
		.udma_mask = ATA_UDMA5,
539 540 541
		.port_ops = &via_port_ops
	};
	/* UDMA133 with bad AST (All current 133) */
Tejun Heo's avatar
Tejun Heo committed
542
	static const struct ata_port_info via_udma133_info = {
543
		.flags = ATA_FLAG_SLAVE_POSS,
544 545
		.pio_mask = ATA_PIO4,
		.mwdma_mask = ATA_MWDMA2,
546
		.udma_mask = ATA_UDMA6,	/* FIXME: should check north bridge */
547 548
		.port_ops = &via_port_ops
	};
549
	const struct ata_port_info *ppi[] = { NULL, NULL };
550
	struct pci_dev *isa;
551 552 553 554
	const struct via_isa_bridge *config;
	static int printed_version;
	u8 enable;
	u32 timing;
555
	unsigned long flags = id->driver_data;
556
	int rc;
557 558 559 560

	if (!printed_version++)
		dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");

561 562 563 564
	rc = pcim_enable_device(pdev);
	if (rc)
		return rc;

565 566 567
	if (flags & VIA_IDFLAG_SINGLE)
		ppi[1] = &ata_dummy_port_info;

568 569
	/* To find out how the IDE will behave and what features we
	   actually have to look at the bridge not the IDE controller */
570 571
	for (config = via_isa_bridges; config->id != PCI_DEVICE_ID_VIA_ANON;
	     config++)
572 573 574
		if ((isa = pci_get_device(PCI_VENDOR_ID_VIA +
			!!(config->flags & VIA_BAD_ID),
			config->id, NULL))) {
575 576
			u8 rev = isa->revision;
			pci_dev_put(isa);
577

578 579 580 581
			if ((id->device == 0x0415 || id->device == 0x3164) &&
			    (config->id != id->device))
				continue;

582
			if (rev >= config->rev_min && rev <= config->rev_max)
583 584 585
				break;
		}

Alan Cox's avatar
Alan Cox committed
586 587 588 589 590 591
	if (!(config->flags & VIA_NO_ENABLES)) {
		/* 0x40 low bits indicate enabled channels */
		pci_read_config_byte(pdev, 0x40 , &enable);
		enable &= 3;
		if (enable == 0)
			return -ENODEV;
592 593 594
	}

	/* Initialise the FIFO for the enabled channels. */
595
	via_config_fifo(pdev, config->flags);
596

597
	/* Clock set up */
598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	switch (config->udma_mask) {
	case 0x00:
		if (config->flags & VIA_NO_UNMASK)
			ppi[0] = &via_mwdma_info_borked;
		else
			ppi[0] = &via_mwdma_info;
		break;
	case ATA_UDMA2:
		ppi[0] = &via_udma33_info;
		break;
	case ATA_UDMA4:
		ppi[0] = &via_udma66_info;
		break;
	case ATA_UDMA5:
		ppi[0] = &via_udma100_info;
		break;
	case ATA_UDMA6:
		ppi[0] = &via_udma133_info;
		break;
	default:
		WARN_ON(1);
		return -ENODEV;
 	}
621 622 623 624 625 626 627 628 629

	if (config->flags & VIA_BAD_CLK66) {
		/* Disable the 66MHz clock on problem devices */
		pci_read_config_dword(pdev, 0x50, &timing);
		timing &= ~0x80008;
		pci_write_config_dword(pdev, 0x50, timing);
	}

	/* We have established the device type, now fire it up */
630
	return ata_pci_bmdma_init_one(pdev, ppi, &via_sht, (void *)config, 0);
631 632
}

633
#ifdef CONFIG_PM
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648
/**
 *	via_reinit_one		-	reinit after resume
 *	@pdev; PCI device
 *
 *	Called when the VIA PATA device is resumed. We must then
 *	reconfigure the fifo and other setup we may have altered. In
 *	addition the kernel needs to have the resume methods on PCI
 *	quirk supported.
 */

static int via_reinit_one(struct pci_dev *pdev)
{
	u32 timing;
	struct ata_host *host = dev_get_drvdata(&pdev->dev);
	const struct via_isa_bridge *config = host->private_data;
649 650 651 652 653
	int rc;

	rc = ata_pci_device_do_resume(pdev);
	if (rc)
		return rc;
654

655 656
	via_config_fifo(pdev, config->flags);

657
	if (config->udma_mask == ATA_UDMA4) {
658 659 660 661 662 663 664 665 666 667 668
		/* The 66 MHz devices require we enable the clock */
		pci_read_config_dword(pdev, 0x50, &timing);
		timing |= 0x80008;
		pci_write_config_dword(pdev, 0x50, timing);
	}
	if (config->flags & VIA_BAD_CLK66) {
		/* Disable the 66MHz clock on problem devices */
		pci_read_config_dword(pdev, 0x50, &timing);
		timing &= ~0x80008;
		pci_write_config_dword(pdev, 0x50, timing);
	}
669 670 671

	ata_host_resume(host);
	return 0;
672
}
673
#endif
674

675
static const struct pci_device_id via[] = {
676
	{ PCI_VDEVICE(VIA, 0x0415), },
677 678 679 680 681
	{ PCI_VDEVICE(VIA, 0x0571), },
	{ PCI_VDEVICE(VIA, 0x0581), },
	{ PCI_VDEVICE(VIA, 0x1571), },
	{ PCI_VDEVICE(VIA, 0x3164), },
	{ PCI_VDEVICE(VIA, 0x5324), },
682
	{ PCI_VDEVICE(VIA, 0xC409), VIA_IDFLAG_SINGLE },
683
	{ PCI_VDEVICE(VIA, 0x9001), VIA_IDFLAG_SINGLE },
684 685

	{ },
686 687 688
};

static struct pci_driver via_pci_driver = {
689
	.name 		= DRV_NAME,
690 691
	.id_table	= via,
	.probe 		= via_init_one,
692
	.remove		= ata_pci_remove_one,
693
#ifdef CONFIG_PM
694 695
	.suspend	= ata_pci_device_suspend,
	.resume		= via_reinit_one,
696
#endif
697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
};

static int __init via_init(void)
{
	return pci_register_driver(&via_pci_driver);
}

static void __exit via_exit(void)
{
	pci_unregister_driver(&via_pci_driver);
}

MODULE_AUTHOR("Alan Cox");
MODULE_DESCRIPTION("low-level driver for VIA PATA");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(pci, via);
MODULE_VERSION(DRV_VERSION);

module_init(via_init);
module_exit(via_exit);