amd74xx.c 10.0 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5
/*
 * AMD 755/756/766/8111 and nVidia nForce/2/2s/3/3s/CK804/MCP04
 * IDE driver for Linux.
 *
 * Copyright (c) 2000-2002 Vojtech Pavlik
6
 * Copyright (c) 2007-2010 Bartlomiej Zolnierkiewicz
L
Linus Torvalds 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 *
 * Based on the work of:
 *      Andre Hedrick
 */

/*
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 as published by
 * the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/ide.h>

24 25
#define DRV_NAME "amd74xx"

26 27 28 29 30 31 32
enum {
	AMD_IDE_CONFIG		= 0x41,
	AMD_CABLE_DETECT	= 0x42,
	AMD_DRIVE_TIMING	= 0x48,
	AMD_8BIT_TIMING		= 0x4e,
	AMD_ADDRESS_SETUP	= 0x4c,
	AMD_UDMA_TIMING		= 0x50,
L
Linus Torvalds 已提交
33 34 35 36 37
};

static unsigned int amd_80w;
static unsigned int amd_clock;

38
static char *amd_dma[] = { "16", "25", "33", "44", "66", "100", "133" };
L
Linus Torvalds 已提交
39 40
static unsigned char amd_cyc2udma[] = { 6, 6, 5, 4, 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 7 };

41 42 43 44 45
static inline u8 amd_offset(struct pci_dev *dev)
{
	return (dev->vendor == PCI_VENDOR_ID_NVIDIA) ? 0x10 : 0;
}

L
Linus Torvalds 已提交
46 47 48 49
/*
 * amd_set_speed() writes timing values to the chipset registers
 */

50 51
static void amd_set_speed(struct pci_dev *dev, u8 dn, u8 udma_mask,
			  struct ide_timing *timing)
L
Linus Torvalds 已提交
52
{
53
	u8 t = 0, offset = amd_offset(dev);
L
Linus Torvalds 已提交
54

55
	pci_read_config_byte(dev, AMD_ADDRESS_SETUP + offset, &t);
H
Harvey Harrison 已提交
56
	t = (t & ~(3 << ((3 - dn) << 1))) | ((clamp_val(timing->setup, 1, 4) - 1) << ((3 - dn) << 1));
57
	pci_write_config_byte(dev, AMD_ADDRESS_SETUP + offset, t);
L
Linus Torvalds 已提交
58

59
	pci_write_config_byte(dev, AMD_8BIT_TIMING + offset + (1 - (dn >> 1)),
H
Harvey Harrison 已提交
60
		((clamp_val(timing->act8b, 1, 16) - 1) << 4) | (clamp_val(timing->rec8b, 1, 16) - 1));
L
Linus Torvalds 已提交
61

62
	pci_write_config_byte(dev, AMD_DRIVE_TIMING + offset + (3 - dn),
H
Harvey Harrison 已提交
63
		((clamp_val(timing->active, 1, 16) - 1) << 4) | (clamp_val(timing->recover, 1, 16) - 1));
L
Linus Torvalds 已提交
64

65
	switch (udma_mask) {
H
Harvey Harrison 已提交
66 67 68 69
	case ATA_UDMA2: t = timing->udma ? (0xc0 | (clamp_val(timing->udma, 2, 5) - 2)) : 0x03; break;
	case ATA_UDMA4: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 2, 10)]) : 0x03; break;
	case ATA_UDMA5: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 1, 10)]) : 0x03; break;
	case ATA_UDMA6: t = timing->udma ? (0xc0 | amd_cyc2udma[clamp_val(timing->udma, 1, 15)]) : 0x03; break;
70
	default: return;
L
Linus Torvalds 已提交
71 72
	}

73 74
	if (timing->udma)
		pci_write_config_byte(dev, AMD_UDMA_TIMING + offset + 3 - dn, t);
L
Linus Torvalds 已提交
75 76 77
}

/*
78 79
 * amd_set_drive() computes timing values and configures the chipset
 * to a desired transfer mode.  It also can be called by upper layers.
L
Linus Torvalds 已提交
80 81
 */

82
static void amd_set_drive(ide_drive_t *drive, const u8 speed)
L
Linus Torvalds 已提交
83
{
84
	ide_hwif_t *hwif = drive->hwif;
85
	struct pci_dev *dev = to_pci_dev(hwif->dev);
86
	ide_drive_t *peer = ide_get_pair_dev(drive);
L
Linus Torvalds 已提交
87 88
	struct ide_timing t, p;
	int T, UT;
89
	u8 udma_mask = hwif->ultra_mask;
L
Linus Torvalds 已提交
90 91

	T = 1000000000 / amd_clock;
92
	UT = (udma_mask == ATA_UDMA2) ? T : (T / 2);
L
Linus Torvalds 已提交
93 94 95

	ide_timing_compute(drive, speed, &t, T, UT);

96
	if (peer) {
L
Linus Torvalds 已提交
97 98 99 100 101 102 103
		ide_timing_compute(peer, peer->current_speed, &p, T, UT);
		ide_timing_merge(&p, &t, &t, IDE_TIMING_8BIT);
	}

	if (speed == XFER_UDMA_5 && amd_clock <= 33333) t.udma = 1;
	if (speed == XFER_UDMA_6 && amd_clock <= 33333) t.udma = 15;

104
	amd_set_speed(dev, drive->dn, udma_mask, &t);
L
Linus Torvalds 已提交
105 106 107
}

/*
108
 * amd_set_pio_mode() is a callback from upper layers for PIO-only tuning.
L
Linus Torvalds 已提交
109 110
 */

111
static void amd_set_pio_mode(ide_drive_t *drive, const u8 pio)
L
Linus Torvalds 已提交
112
{
113
	amd_set_drive(drive, XFER_PIO_0 + pio);
L
Linus Torvalds 已提交
114 115
}

116
static void amd7409_cable_detect(struct pci_dev *dev)
117 118 119 120
{
	/* no host side cable detection */
	amd_80w = 0x03;
}
L
Linus Torvalds 已提交
121

122
static void amd7411_cable_detect(struct pci_dev *dev)
L
Linus Torvalds 已提交
123 124
{
	int i;
125 126 127 128 129 130 131 132
	u32 u = 0;
	u8 t = 0, offset = amd_offset(dev);

	pci_read_config_byte(dev, AMD_CABLE_DETECT + offset, &t);
	pci_read_config_dword(dev, AMD_UDMA_TIMING + offset, &u);
	amd_80w = ((t & 0x3) ? 1 : 0) | ((t & 0xc) ? 2 : 0);
	for (i = 24; i >= 0; i -= 8)
		if (((u >> i) & 4) && !(amd_80w & (1 << (1 - (i >> 4))))) {
133 134 135
			printk(KERN_WARNING DRV_NAME " %s: BIOS didn't set "
				"cable bits correctly. Enabling workaround.\n",
				pci_name(dev));
136 137 138
			amd_80w |= (1 << (1 - (i >> 4)));
		}
}
L
Linus Torvalds 已提交
139 140

/*
141
 * The initialization callback.  Initialize drive independent registers.
L
Linus Torvalds 已提交
142 143
 */

144
static int init_chipset_amd74xx(struct pci_dev *dev)
145 146
{
	u8 t = 0, offset = amd_offset(dev);
L
Linus Torvalds 已提交
147 148 149 150 151

/*
 * Check 80-wire cable presence.
 */

152 153 154 155 156
	if (dev->vendor == PCI_VENDOR_ID_AMD &&
	    dev->device == PCI_DEVICE_ID_AMD_COBRA_7401)
		; /* no UDMA > 2 */
	else if (dev->vendor == PCI_VENDOR_ID_AMD &&
		 dev->device == PCI_DEVICE_ID_AMD_VIPER_7409)
157
		amd7409_cable_detect(dev);
158
	else
159
		amd7411_cable_detect(dev);
L
Linus Torvalds 已提交
160 161 162 163 164

/*
 * Take care of prefetch & postwrite.
 */

165 166 167 168 169
	pci_read_config_byte(dev, AMD_IDE_CONFIG + offset, &t);
	/*
	 * Check for broken FIFO support.
	 */
	if (dev->vendor == PCI_VENDOR_ID_AMD &&
R
Roel Kluin 已提交
170
	    dev->device == PCI_DEVICE_ID_AMD_VIPER_7411)
171 172 173 174
		t &= 0x0f;
	else
		t |= 0xf0;
	pci_write_config_byte(dev, AMD_IDE_CONFIG + offset, t);
L
Linus Torvalds 已提交
175

176
	return 0;
L
Linus Torvalds 已提交
177 178
}

179
static u8 amd_cable_detect(ide_hwif_t *hwif)
180 181 182 183 184 185 186
{
	if ((amd_80w >> hwif->channel) & 1)
		return ATA_CBL_PATA80;
	else
		return ATA_CBL_PATA40;
}

187 188 189 190 191 192
static const struct ide_port_ops amd_port_ops = {
	.set_pio_mode		= amd_set_pio_mode,
	.set_dma_mode		= amd_set_drive,
	.cable_detect		= amd_cable_detect,
};

193 194 195 196
#define IDE_HFLAGS_AMD \
	(IDE_HFLAG_PIO_NO_BLACKLIST | \
	 IDE_HFLAG_POST_SET_MODE | \
	 IDE_HFLAG_IO_32BIT | \
197
	 IDE_HFLAG_UNMASK_IRQS)
198

199
#define DECLARE_AMD_DEV(swdma, udma)				\
L
Linus Torvalds 已提交
200
	{								\
201
		.name		= DRV_NAME,				\
L
Linus Torvalds 已提交
202 203
		.init_chipset	= init_chipset_amd74xx,			\
		.enablebits	= {{0x40,0x02,0x02}, {0x40,0x01,0x01}},	\
204
		.port_ops	= &amd_port_ops,			\
205
		.host_flags	= IDE_HFLAGS_AMD,			\
B
Bartlomiej Zolnierkiewicz 已提交
206
		.pio_mask	= ATA_PIO5,				\
207
		.swdma_mask	= swdma,				\
208
		.mwdma_mask	= ATA_MWDMA2,				\
209
		.udma_mask	= udma,					\
L
Linus Torvalds 已提交
210 211
	}

212
#define DECLARE_NV_DEV(udma)					\
L
Linus Torvalds 已提交
213
	{								\
214
		.name		= DRV_NAME,				\
L
Linus Torvalds 已提交
215 216
		.init_chipset	= init_chipset_amd74xx,			\
		.enablebits	= {{0x50,0x02,0x02}, {0x50,0x01,0x01}},	\
217
		.port_ops	= &amd_port_ops,			\
218
		.host_flags	= IDE_HFLAGS_AMD,			\
B
Bartlomiej Zolnierkiewicz 已提交
219
		.pio_mask	= ATA_PIO5,				\
220 221
		.swdma_mask	= ATA_SWDMA2,				\
		.mwdma_mask	= ATA_MWDMA2,				\
222
		.udma_mask	= udma,					\
L
Linus Torvalds 已提交
223 224
	}

225
static const struct ide_port_info amd74xx_chipsets[] __devinitdata = {
226 227 228 229 230 231 232 233 234
	/* 0: AMD7401 */	DECLARE_AMD_DEV(0x00, ATA_UDMA2),
	/* 1: AMD7409 */	DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA4),
	/* 2: AMD7411/7441 */	DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
	/* 3: AMD8111 */	DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA6),

	/* 4: NFORCE */		DECLARE_NV_DEV(ATA_UDMA5),
	/* 5: >= NFORCE2 */	DECLARE_NV_DEV(ATA_UDMA6),

	/* 6: AMD5536 */	DECLARE_AMD_DEV(ATA_SWDMA2, ATA_UDMA5),
L
Linus Torvalds 已提交
235 236 237 238
};

static int __devinit amd74xx_probe(struct pci_dev *dev, const struct pci_device_id *id)
{
239 240 241 242 243 244 245 246 247 248 249
	struct ide_port_info d;
	u8 idx = id->driver_data;

	d = amd74xx_chipsets[idx];

	/*
	 * Check for bad SWDMA and incorrectly wired Serenade mainboards.
	 */
	if (idx == 1) {
		if (dev->revision <= 7)
			d.swdma_mask = 0;
250
		d.host_flags |= IDE_HFLAG_CLEAR_SIMPLEX;
251
	} else if (idx == 3) {
252 253 254
		if (dev->subsystem_vendor == PCI_VENDOR_ID_AMD &&
		    dev->subsystem_device == PCI_DEVICE_ID_AMD_SERENADE)
			d.udma_mask = ATA_UDMA5;
L
Linus Torvalds 已提交
255
	}
256

257 258 259 260 261 262 263 264 265
	/*
	 * It seems that on some nVidia controllers using AltStatus
	 * register can be unreliable so default to Status register
	 * if the device is in Compatibility Mode.
	 */
	if (dev->vendor == PCI_VENDOR_ID_NVIDIA &&
	    ide_pci_is_in_compatibility_mode(dev))
		d.host_flags |= IDE_HFLAG_BROKEN_ALTSTATUS;

266 267
	printk(KERN_INFO "%s %s: UDMA%s controller\n",
		d.name, pci_name(dev), amd_dma[fls(d.udma_mask) - 1]);
268

269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
	/*
	* Determine the system bus clock.
	*/
	amd_clock = (ide_pci_clk ? ide_pci_clk : 33) * 1000;

	switch (amd_clock) {
	case 33000: amd_clock = 33333; break;
	case 37000: amd_clock = 37500; break;
	case 41000: amd_clock = 41666; break;
	}

	if (amd_clock < 20000 || amd_clock > 50000) {
		printk(KERN_WARNING "%s: User given PCI clock speed impossible"
				    " (%d), using 33 MHz instead.\n",
				    d.name, amd_clock);
		amd_clock = 33333;
	}

287
	return ide_pci_init_one(dev, &d, NULL);
L
Linus Torvalds 已提交
288 289
}

290 291 292 293
static const struct pci_device_id amd74xx_pci_tbl[] = {
	{ PCI_VDEVICE(AMD,	PCI_DEVICE_ID_AMD_COBRA_7401),		 0 },
	{ PCI_VDEVICE(AMD,	PCI_DEVICE_ID_AMD_VIPER_7409),		 1 },
	{ PCI_VDEVICE(AMD,	PCI_DEVICE_ID_AMD_VIPER_7411),		 2 },
294 295 296 297 298
	{ PCI_VDEVICE(AMD,	PCI_DEVICE_ID_AMD_OPUS_7441),		 2 },
	{ PCI_VDEVICE(AMD,	PCI_DEVICE_ID_AMD_8111_IDE),		 3 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_IDE),	 4 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE2_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE2S_IDE),	 5 },
L
Linus Torvalds 已提交
299
#ifdef CONFIG_BLK_DEV_IDE_SATA
300
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE2S_SATA),	 5 },
L
Linus Torvalds 已提交
301
#endif
302 303
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE3_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE3S_IDE),	 5 },
L
Linus Torvalds 已提交
304
#ifdef CONFIG_BLK_DEV_IDE_SATA
305 306
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE3S_SATA2),	 5 },
L
Linus Torvalds 已提交
307
#endif
308 309 310 311 312 313 314 315 316 317
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_CK804_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP04_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP51_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP55_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP61_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP65_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP67_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP73_IDE),	 5 },
	{ PCI_VDEVICE(NVIDIA,	PCI_DEVICE_ID_NVIDIA_NFORCE_MCP77_IDE),	 5 },
	{ PCI_VDEVICE(AMD,	PCI_DEVICE_ID_AMD_CS5536_IDE),		 6 },
L
Linus Torvalds 已提交
318 319 320 321
	{ 0, },
};
MODULE_DEVICE_TABLE(pci, amd74xx_pci_tbl);

322
static struct pci_driver amd74xx_pci_driver = {
L
Linus Torvalds 已提交
323 324 325
	.name		= "AMD_IDE",
	.id_table	= amd74xx_pci_tbl,
	.probe		= amd74xx_probe,
326
	.remove		= ide_pci_remove,
327 328
	.suspend	= ide_pci_suspend,
	.resume		= ide_pci_resume,
L
Linus Torvalds 已提交
329 330
};

331
static int __init amd74xx_ide_init(void)
L
Linus Torvalds 已提交
332
{
333
	return ide_pci_register_driver(&amd74xx_pci_driver);
L
Linus Torvalds 已提交
334 335
}

336 337
static void __exit amd74xx_ide_exit(void)
{
338
	pci_unregister_driver(&amd74xx_pci_driver);
339 340
}

L
Linus Torvalds 已提交
341
module_init(amd74xx_ide_init);
342
module_exit(amd74xx_ide_exit);
L
Linus Torvalds 已提交
343

344
MODULE_AUTHOR("Vojtech Pavlik, Bartlomiej Zolnierkiewicz");
L
Linus Torvalds 已提交
345 346
MODULE_DESCRIPTION("AMD PCI IDE driver");
MODULE_LICENSE("GPL");