sis5513.c 31.4 KB
Newer Older
1
/*
Alan Cox's avatar
Alan Cox committed
2
 * linux/drivers/ide/pci/sis5513.c		Version 0.14ac	Sept 11, 2002
3 4 5 6 7 8 9 10 11 12
 *
 * Copyright (C) 1999-2000	Andre Hedrick <andre@linux-ide.org>
 * Copyright (C) 2002		Lionel Bouton <Lionel.Bouton@inet6.fr>, Maintainer
 * May be copied or modified under the terms of the GNU General Public License
 *
 *
 * Thanks :
 *
 * SiS Taiwan		: for direct support and hardware.
 * Daniela Engert	: for initial ATA100 advices and numerous others.
Alan Cox's avatar
Alan Cox committed
13
 * John Fremlin, Manfred Spraul, Dave Morgan, Peter Kjellerstedt	:
14 15 16 17 18
 *			  for checking code correctness, providing patches.
 *
 *
 * Original tests and design on the SiS620/5513 chipset.
 * ATA100 tests and design on the SiS735/5513 chipset.
Jens Axboe's avatar
Jens Axboe committed
19 20
 * ATA16/33 support from specs
 * ATA133 support for SiS961/962 by L.C. Chang <lcchang@sis.com.tw>
Alan Cox's avatar
Alan Cox committed
21 22 23 24
 *
 * Documentation:
 *	SiS chipset documentation available under NDA to companies not
 *	individuals only.
25 26 27
 */

/*
Alan Cox's avatar
Alan Cox committed
28 29 30 31 32 33 34 35 36 37 38 39 40 41
 * Notes/Special cases:
 * - SiS5513 derivatives usually have the same PCI IDE register layout when
 *  supporting the same UDMA modes.
 * - There are exceptions :
 *  . SiS730 and SiS550 use the same layout than ATA_66 chipsets but support
 *   ATA_100
 *  . ATA_133 capable chipsets mark a shift in SiS chipset designs : previously
 *   south and northbridge were integrated, making IDE (a southbridge function)
 *   capabilities easily deduced from the northbridge PCI id. With ATA_133,
 *   chipsets started to be split in the usual north/south bridges chips
 *   -> the driver needs to detect the correct southbridge when faced to newest
 *   northbridges.
 *  . On ATA133 capable chipsets when bit 30 of dword at 0x54 is 1 the
 *   configuration space is moved from 0x40 to 0x70.
42 43 44 45
 */

#include <linux/config.h>
#include <linux/types.h>
46
#include <linux/module.h>
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/timer.h>
#include <linux/mm.h>
#include <linux/ioport.h>
#include <linux/blkdev.h>
#include <linux/hdreg.h>

#include <linux/interrupt.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/ide.h>

#include <asm/io.h>
#include <asm/irq.h>

#include "ide_modes.h"
Jens Axboe's avatar
Jens Axboe committed
64
#include "sis5513.h"
65 66

/* When DEBUG is defined it outputs initial PCI config register
Jens Axboe's avatar
Jens Axboe committed
67
   values and changes made to them by the driver */
68 69 70 71 72
// #define DEBUG
/* When BROKEN_LEVEL is defined it limits the DMA mode
   at boot time to its value */
// #define BROKEN_LEVEL XFER_SW_DMA_0

Alan Cox's avatar
Alan Cox committed
73
/* Miscellaneous flags */
74 75
#define SIS5513_LATENCY		0x01

Alan Cox's avatar
Alan Cox committed
76
/* registers layout and init values are chipset family dependant */
77 78 79 80 81 82 83
/* 1/ define families */
#define ATA_00		0x00
#define ATA_16		0x01
#define ATA_33		0x02
#define ATA_66		0x03
#define ATA_100a	0x04 // SiS730 is ATA100 with ATA66 layout
#define ATA_100		0x05
Jens Axboe's avatar
Jens Axboe committed
84 85
#define ATA_133a	0x06 // SiS961b with 133 support
#define ATA_133		0x07 // SiS962
86
/* 2/ variable holding the controller chipset family value */
Jens Axboe's avatar
Jens Axboe committed
87
static u8 chipset_family;
88 89 90 91 92 93


/*
 * Debug code: following IDE config registers' changes
 */
#ifdef DEBUG
Jens Axboe's avatar
Jens Axboe committed
94 95 96 97 98
/* Copy of IDE Config registers fewer will be used
 * Some odd chipsets hang if unused registers are accessed
 * -> We only access them in #DEBUG code (then we'll see if SiS did
 * it right from day one) */
static u8 ide_regs_copy[0xff];
99 100 101 102

/* Read config registers, print differences from previous read */
static void sis5513_load_verify_registers(struct pci_dev* dev, char* info) {
	int i;
Jens Axboe's avatar
Jens Axboe committed
103 104
	u8 reg_val;
	u8 changed=0;
105 106

	printk("SIS5513: %s, changed registers:\n", info);
Jens Axboe's avatar
Jens Axboe committed
107
	for(i=0; i<=0xff; i++) {
108 109
		pci_read_config_byte(dev, i, &reg_val);
		if (reg_val != ide_regs_copy[i]) {
Jens Axboe's avatar
Jens Axboe committed
110
			printk("%02x: %02x -> %02x\n",
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
			       i, ide_regs_copy[i], reg_val);
			ide_regs_copy[i]=reg_val;
			changed=1;
		}
	}

	if (!changed) {
		printk("none\n");
	}
}

/* Load config registers, no printing */
static void sis5513_load_registers(struct pci_dev* dev) {
	int i;

Jens Axboe's avatar
Jens Axboe committed
126
	for(i=0; i<=0xff; i++) {
127 128 129 130
		pci_read_config_byte(dev, i, &(ide_regs_copy[i]));
	}
}

Jens Axboe's avatar
Jens Axboe committed
131
/* Print config space registers a la "lspci -vxxx" */
132
static void sis5513_print_registers(struct pci_dev* dev, char* marker) {
Jens Axboe's avatar
Jens Axboe committed
133
	int i,j;
134 135 136 137

	sis5513_load_registers(dev);
	printk("SIS5513 %s\n", marker);

Jens Axboe's avatar
Jens Axboe committed
138 139 140 141 142 143
	for(i=0; i<=0xf; i++) {
		printk("SIS5513 dump: %d" "0:", i);
		for(j=0; j<=0xf; j++) {
			printk(" %02x", ide_regs_copy[(i<<16)+j]);
		}
		printk("\n");
144 145 146 147 148 149 150 151 152 153
	}
}
#endif


/*
 * Devices supported
 */
static const struct {
	const char *name;
Jens Axboe's avatar
Jens Axboe committed
154 155 156
	u16 host_id;
	u8 chipset_family;
	u8 flags;
157
} SiSHostChipInfo[] = {
Jens Axboe's avatar
Jens Axboe committed
158 159 160 161 162 163
	{ "SiS752",	PCI_DEVICE_ID_SI_752,	ATA_133,	0 },
	{ "SiS751",	PCI_DEVICE_ID_SI_751,	ATA_133,	0 },
	{ "SiS750",	PCI_DEVICE_ID_SI_750,	ATA_133,	0 },
	{ "SiS748",	PCI_DEVICE_ID_SI_748,	ATA_133,	0 },
	{ "SiS746",	PCI_DEVICE_ID_SI_746,	ATA_133,	0 },
	{ "SiS745",	PCI_DEVICE_ID_SI_745,	ATA_133,	0 },
164
	{ "SiS740",	PCI_DEVICE_ID_SI_740,	ATA_133,	0 },
165 166
	{ "SiS735",	PCI_DEVICE_ID_SI_735,	ATA_100,	SIS5513_LATENCY },
	{ "SiS730",	PCI_DEVICE_ID_SI_730,	ATA_100a,	SIS5513_LATENCY },
167
	{ "SiS655",	PCI_DEVICE_ID_SI_655,	ATA_133,	0 },
Jens Axboe's avatar
Jens Axboe committed
168 169 170 171 172 173
	{ "SiS652",	PCI_DEVICE_ID_SI_652,	ATA_133,	0 },
	{ "SiS651",	PCI_DEVICE_ID_SI_651,	ATA_133,	0 },
	{ "SiS650",	PCI_DEVICE_ID_SI_650,	ATA_133,	0 },
	{ "SiS648",	PCI_DEVICE_ID_SI_648,	ATA_133,	0 },
	{ "SiS646",	PCI_DEVICE_ID_SI_646,	ATA_133,	0 },
	{ "SiS645",	PCI_DEVICE_ID_SI_645,	ATA_133,	0 },
174 175 176 177
	{ "SiS635",	PCI_DEVICE_ID_SI_635,	ATA_100,	SIS5513_LATENCY },
	{ "SiS640",	PCI_DEVICE_ID_SI_640,	ATA_66,		SIS5513_LATENCY },
	{ "SiS630",	PCI_DEVICE_ID_SI_630,	ATA_66,		SIS5513_LATENCY },
	{ "SiS620",	PCI_DEVICE_ID_SI_620,	ATA_66,		SIS5513_LATENCY },
Jens Axboe's avatar
Jens Axboe committed
178
	{ "SiS550",	PCI_DEVICE_ID_SI_550,	ATA_100a,	0},
179 180 181 182 183 184 185 186 187 188
	{ "SiS540",	PCI_DEVICE_ID_SI_540,	ATA_66,		0},
	{ "SiS530",	PCI_DEVICE_ID_SI_530,	ATA_66,		0},
	{ "SiS5600",	PCI_DEVICE_ID_SI_5600,	ATA_33,		0},
	{ "SiS5598",	PCI_DEVICE_ID_SI_5598,	ATA_33,		0},
	{ "SiS5597",	PCI_DEVICE_ID_SI_5597,	ATA_33,		0},
	{ "SiS5591",	PCI_DEVICE_ID_SI_5591,	ATA_33,		0},
	{ "SiS5513",	PCI_DEVICE_ID_SI_5513,	ATA_16,		0},
	{ "SiS5511",	PCI_DEVICE_ID_SI_5511,	ATA_16,		0},
};

Alan Cox's avatar
Alan Cox committed
189
/* Cycle time bits and values vary across chip dma capabilities
190 191
   These three arrays hold the register layout and the values to set.
   Indexed by chipset_family and (dma_mode - XFER_UDMA_0) */
Jens Axboe's avatar
Jens Axboe committed
192 193 194 195 196 197 198 199 200 201 202 203 204 205

/* {ATA_00, ATA_16, ATA_33, ATA_66, ATA_100a, ATA_100, ATA_133} */
static u8 cycle_time_offset[] = {0,0,5,4,4,0,0};
static u8 cycle_time_range[] = {0,0,2,3,3,4,4};
static u8 cycle_time_value[][XFER_UDMA_6 - XFER_UDMA_0 + 1] = {
	{0,0,0,0,0,0,0}, /* no udma */
	{0,0,0,0,0,0,0}, /* no udma */
	{3,2,1,0,0,0,0}, /* ATA_33 */
	{7,5,3,2,1,0,0}, /* ATA_66 */
	{7,5,3,2,1,0,0}, /* ATA_100a (730 specific), differences are on cycle_time range and offset */
	{11,7,5,4,2,1,0}, /* ATA_100 */
	{15,10,7,5,3,2,1}, /* ATA_133a (earliest 691 southbridges) */
	{15,10,7,5,3,2,1}, /* ATA_133 */
};
Alan Cox's avatar
Alan Cox committed
206
/* CRC Valid Setup Time vary across IDE clock setting 33/66/100/133
Jens Axboe's avatar
Jens Axboe committed
207 208 209 210 211 212 213 214 215 216 217
   See SiS962 data sheet for more detail */
static u8 cvs_time_value[][XFER_UDMA_6 - XFER_UDMA_0 + 1] = {
	{0,0,0,0,0,0,0}, /* no udma */
	{0,0,0,0,0,0,0}, /* no udma */
	{2,1,1,0,0,0,0},
	{4,3,2,1,0,0,0},
	{4,3,2,1,0,0,0},
	{6,4,3,1,1,1,0},
	{9,6,4,2,2,2,2},
	{9,6,4,2,2,2,2},
};
Alan Cox's avatar
Alan Cox committed
218
/* Initialize time, Active time, Recovery time vary across
Jens Axboe's avatar
Jens Axboe committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
   IDE clock settings. These 3 arrays hold the register value
   for PIO0/1/2/3/4 and DMA0/1/2 mode in order */
static u8 ini_time_value[][8] = {
	{0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0},
	{2,1,0,0,0,1,0,0},
	{4,3,1,1,1,3,1,1},
	{4,3,1,1,1,3,1,1},
	{6,4,2,2,2,4,2,2},
	{9,6,3,3,3,6,3,3},
	{9,6,3,3,3,6,3,3},
};
static u8 act_time_value[][8] = {
	{0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0},
	{9,9,9,2,2,7,2,2},
	{19,19,19,5,4,14,5,4},
	{19,19,19,5,4,14,5,4},
	{28,28,28,7,6,21,7,6},
	{38,38,38,10,9,28,10,9},
	{38,38,38,10,9,28,10,9},
};
static u8 rco_time_value[][8] = {
	{0,0,0,0,0,0,0,0},
	{0,0,0,0,0,0,0,0},
	{9,2,0,2,0,7,1,1},
	{19,5,1,5,2,16,3,2},
	{19,5,1,5,2,16,3,2},
	{30,9,3,9,4,25,6,4},
	{40,12,4,12,5,34,12,5},
	{40,12,4,12,5,34,12,5},
250 251 252 253 254 255 256
};

static struct pci_dev *host_dev = NULL;

/*
 * Printing configuration
 */
257 258 259 260
/* Used for chipset type printing at boot time */
static char* chipset_capability[] = {
	"ATA", "ATA 16",
	"ATA 33", "ATA 66",
261 262
	"ATA 100 (1st gen)", "ATA 100 (2nd gen)",
	"ATA 133 (1st gen)", "ATA 133 (2nd gen)"
263 264
};

265 266 267 268
#if defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS)
#include <linux/stat.h>
#include <linux/proc_fs.h>

Jens Axboe's avatar
Jens Axboe committed
269 270
static u8 sis_proc = 0;

271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
static struct pci_dev *bmide_dev;

static char* cable_type[] = {
	"80 pins",
	"40 pins"
};

static char* recovery_time[] ={
	"12 PCICLK", "1 PCICLK",
	"2 PCICLK", "3 PCICLK",
	"4 PCICLK", "5 PCICLCK",
	"6 PCICLK", "7 PCICLCK",
	"8 PCICLK", "9 PCICLCK",
	"10 PCICLK", "11 PCICLK",
	"13 PCICLK", "14 PCICLK",
	"15 PCICLK", "15 PCICLK"
};

static char* active_time[] = {
	"8 PCICLK", "1 PCICLCK",
	"2 PCICLK", "3 PCICLK",
	"4 PCICLK", "5 PCICLK",
	"6 PCICLK", "12 PCICLK"
};

static char* cycle_time[] = {
	"Reserved", "2 CLK",
	"3 CLK", "4 CLK",
	"5 CLK", "6 CLK",
	"7 CLK", "8 CLK",
	"9 CLK", "10 CLK",
	"11 CLK", "12 CLK",
Jens Axboe's avatar
Jens Axboe committed
303 304 305 306
	"13 CLK", "14 CLK",
	"15 CLK", "16 CLK"
};

307
/* Generic add master or slave info function */
Jens Axboe's avatar
Jens Axboe committed
308
static char* get_drives_info (char *buffer, u8 pos)
309
{
Jens Axboe's avatar
Jens Axboe committed
310
	u8 reg00, reg01, reg10, reg11; /* timing registers */
Alan Cox's avatar
Alan Cox committed
311
	u32 regdw0, regdw1;
312 313 314
	char* p = buffer;

/* Postwrite/Prefetch */
Jens Axboe's avatar
Jens Axboe committed
315 316 317 318 319 320 321 322 323 324 325 326
	if (chipset_family < ATA_133) {
		pci_read_config_byte(bmide_dev, 0x4b, &reg00);
		p += sprintf(p, "Drive %d:        Postwrite %s \t \t Postwrite %s\n",
			     pos, (reg00 & (0x10 << pos)) ? "Enabled" : "Disabled",
			     (reg00 & (0x40 << pos)) ? "Enabled" : "Disabled");
		p += sprintf(p, "                Prefetch  %s \t \t Prefetch  %s\n",
			     (reg00 & (0x01 << pos)) ? "Enabled" : "Disabled",
			     (reg00 & (0x04 << pos)) ? "Enabled" : "Disabled");
		pci_read_config_byte(bmide_dev, 0x40+2*pos, &reg00);
		pci_read_config_byte(bmide_dev, 0x41+2*pos, &reg01);
		pci_read_config_byte(bmide_dev, 0x44+2*pos, &reg10);
		pci_read_config_byte(bmide_dev, 0x45+2*pos, &reg11);
Alan Cox's avatar
Alan Cox committed
327 328 329 330 331 332 333 334
	} else {
		u32 reg54h;
		u8 drive_pci = 0x40;
		pci_read_config_dword(bmide_dev, 0x54, &reg54h);
		if (reg54h & 0x40000000) {
			// Configuration space remapped to 0x70
			drive_pci = 0x70;
		}
335 336
		pci_read_config_dword(bmide_dev, (unsigned long)drive_pci+4*pos, &regdw0);
		pci_read_config_dword(bmide_dev, (unsigned long)drive_pci+4*pos+8, &regdw1);
Alan Cox's avatar
Alan Cox committed
337
		p += sprintf(p, "Drive %d:\n", pos);
Jens Axboe's avatar
Jens Axboe committed
338 339
	}

340 341

/* UDMA */
Alan Cox's avatar
Alan Cox committed
342 343 344 345 346 347 348 349
	if (chipset_family >= ATA_133) {
		p += sprintf(p, "                UDMA %s \t \t \t UDMA %s\n",
			     (regdw0 & 0x04) ? "Enabled" : "Disabled",
			     (regdw1 & 0x04) ? "Enabled" : "Disabled");
		p += sprintf(p, "                UDMA Cycle Time    %s \t UDMA Cycle Time    %s\n",
			     cycle_time[(regdw0 & 0xF0) >> 4],
			     cycle_time[(regdw1 & 0xF0) >> 4]);
	} else if (chipset_family >= ATA_33) {
350
		p += sprintf(p, "                UDMA %s \t \t \t UDMA %s\n",
Alan Cox's avatar
Alan Cox committed
351
			     (reg01 & 0x80) ? "Enabled" : "Disabled",
352 353 354 355 356 357 358
			     (reg11 & 0x80) ? "Enabled" : "Disabled");

		p += sprintf(p, "                UDMA Cycle Time    ");
		switch(chipset_family) {
			case ATA_33:	p += sprintf(p, cycle_time[(reg01 & 0x60) >> 5]); break;
			case ATA_66:
			case ATA_100a:	p += sprintf(p, cycle_time[(reg01 & 0x70) >> 4]); break;
Jens Axboe's avatar
Jens Axboe committed
359 360
			case ATA_100:
			case ATA_133a:	p += sprintf(p, cycle_time[reg01 & 0x0F]); break;
361
			default:	p += sprintf(p, "?"); break;
362 363 364 365 366 367
		}
		p += sprintf(p, " \t UDMA Cycle Time    ");
		switch(chipset_family) {
			case ATA_33:	p += sprintf(p, cycle_time[(reg11 & 0x60) >> 5]); break;
			case ATA_66:
			case ATA_100a:	p += sprintf(p, cycle_time[(reg11 & 0x70) >> 4]); break;
Jens Axboe's avatar
Jens Axboe committed
368 369
			case ATA_100:
			case ATA_133a:  p += sprintf(p, cycle_time[reg11 & 0x0F]); break;
370
			default:	p += sprintf(p, "?"); break;
371 372 373 374
		}
		p += sprintf(p, "\n");
	}

375
	if (chipset_family < ATA_133) { /* else case TODO */
376
/* Data Active */
377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399
		p += sprintf(p, "                Data Active Time   ");
		switch(chipset_family) {
			case ATA_00:
			case ATA_16: /* confirmed */
			case ATA_33:
			case ATA_66:
			case ATA_100a: p += sprintf(p, active_time[reg01 & 0x07]); break;
			case ATA_100:
			case ATA_133a: p += sprintf(p, active_time[(reg00 & 0x70) >> 4]); break;
			default: p += sprintf(p, "?"); break;
		}
		p += sprintf(p, " \t Data Active Time   ");
		switch(chipset_family) {
			case ATA_00:
			case ATA_16:
			case ATA_33:
			case ATA_66:
			case ATA_100a: p += sprintf(p, active_time[reg11 & 0x07]); break;
			case ATA_100:
			case ATA_133a: p += sprintf(p, active_time[(reg10 & 0x70) >> 4]); break;
			default: p += sprintf(p, "?"); break;
		}
		p += sprintf(p, "\n");
400 401 402

/* Data Recovery */
	/* warning: may need (reg&0x07) for pre ATA66 chips */
Jens Axboe's avatar
Jens Axboe committed
403 404 405
		p += sprintf(p, "                Data Recovery Time %s \t Data Recovery Time %s\n",
			     recovery_time[reg00 & 0x0f], recovery_time[reg10 & 0x0f]);
	}
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423

	return p;
}

static char* get_masters_info(char* buffer)
{
	return get_drives_info(buffer, 0);
}

static char* get_slaves_info(char* buffer)
{
	return get_drives_info(buffer, 1);
}

/* Main get_info, called on /proc/ide/sis reads */
static int sis_get_info (char *buffer, char **addr, off_t offset, int count)
{
	char *p = buffer;
424
	int len;
Jens Axboe's avatar
Jens Axboe committed
425
	u8 reg;
426 427 428 429 430 431 432 433 434
	u16 reg2, reg3;

	p += sprintf(p, "\nSiS 5513 ");
	switch(chipset_family) {
		case ATA_16: p += sprintf(p, "DMA 16"); break;
		case ATA_33: p += sprintf(p, "Ultra 33"); break;
		case ATA_66: p += sprintf(p, "Ultra 66"); break;
		case ATA_100a:
		case ATA_100: p += sprintf(p, "Ultra 100"); break;
Jens Axboe's avatar
Jens Axboe committed
435 436 437
		case ATA_133a:
		case ATA_133: p += sprintf(p, "Ultra 133"); break;
		default: p+= sprintf(p, "Unknown???"); break;
438 439 440
	}
	p += sprintf(p, " chipset\n");
	p += sprintf(p, "--------------- Primary Channel "
Jens Axboe's avatar
Jens Axboe committed
441 442
		     "---------------- Secondary Channel "
		     "-------------\n");
443 444 445

/* Status */
	pci_read_config_byte(bmide_dev, 0x4a, &reg);
Jens Axboe's avatar
Jens Axboe committed
446 447 448 449
	if (chipset_family == ATA_133) {
		pci_read_config_word(bmide_dev, 0x50, &reg2);
		pci_read_config_word(bmide_dev, 0x52, &reg3);
	}
450 451 452 453 454
	p += sprintf(p, "Channel Status: ");
	if (chipset_family < ATA_66) {
		p += sprintf(p, "%s \t \t \t \t %s\n",
			     (reg & 0x04) ? "On" : "Off",
			     (reg & 0x02) ? "On" : "Off");
Jens Axboe's avatar
Jens Axboe committed
455
	} else if (chipset_family < ATA_133) {
456 457 458
		p += sprintf(p, "%s \t \t \t \t %s \n",
			     (reg & 0x02) ? "On" : "Off",
			     (reg & 0x04) ? "On" : "Off");
Jens Axboe's avatar
Jens Axboe committed
459 460 461 462
	} else { /* ATA_133 */
		p += sprintf(p, "%s \t \t \t \t %s \n",
			     (reg2 & 0x02) ? "On" : "Off",
			     (reg3 & 0x02) ? "On" : "Off");
463 464 465 466 467 468 469 470 471
	}

/* Operation Mode */
	pci_read_config_byte(bmide_dev, 0x09, &reg);
	p += sprintf(p, "Operation Mode: %s \t \t \t %s \n",
		     (reg & 0x01) ? "Native" : "Compatible",
		     (reg & 0x04) ? "Native" : "Compatible");

/* 80-pin cable ? */
Jens Axboe's avatar
Jens Axboe committed
472 473 474 475 476
	if (chipset_family >= ATA_133) {
		p += sprintf(p, "Cable Type:     %s \t \t \t %s\n",
			     (reg2 & 0x01) ? cable_type[1] : cable_type[0],
			     (reg3 & 0x01) ? cable_type[1] : cable_type[0]);
	} else if (chipset_family > ATA_33) {
477 478 479 480 481 482 483
		pci_read_config_byte(bmide_dev, 0x48, &reg);
		p += sprintf(p, "Cable Type:     %s \t \t \t %s\n",
			     (reg & 0x10) ? cable_type[1] : cable_type[0],
			     (reg & 0x20) ? cable_type[1] : cable_type[0]);
	}

/* Prefetch Count */
Jens Axboe's avatar
Jens Axboe committed
484 485 486 487 488 489
	if (chipset_family < ATA_133) {
		pci_read_config_word(bmide_dev, 0x4c, &reg2);
		pci_read_config_word(bmide_dev, 0x4e, &reg3);
		p += sprintf(p, "Prefetch Count: %d \t \t \t \t %d\n",
			     reg2, reg3);
	}
490 491 492 493

	p = get_masters_info(p);
	p = get_slaves_info(p);

494 495 496 497
	len = (p - buffer) - offset;
	*addr = buffer + offset;
	
	return len > count ? count : len;
498 499 500
}
#endif /* defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS) */

Jens Axboe's avatar
Jens Axboe committed
501
static u8 sis5513_ratemask (ide_drive_t *drive)
502
{
Jens Axboe's avatar
Jens Axboe committed
503 504 505 506 507
#if 0
	u8 rates[] = { 0, 0, 1, 2, 3, 3, 4, 4 };
	u8 mode = rates[chipset_family];
#else
	u8 mode;
508 509

	switch(chipset_family) {
Jens Axboe's avatar
Jens Axboe committed
510 511 512 513
		case ATA_133:
		case ATA_133a:
			mode = 4;
			break;
514
		case ATA_100:
Jens Axboe's avatar
Jens Axboe committed
515 516 517 518 519 520 521 522
		case ATA_100a:
			mode = 3;
			break;
		case ATA_66:
			mode = 2;
			break;
		case ATA_33:
			return 1;
523 524 525
		case ATA_16:
                case ATA_00:	
		default:
Jens Axboe's avatar
Jens Axboe committed
526
			return 0;
527
	}
Jens Axboe's avatar
Jens Axboe committed
528 529 530 531
#endif
	if (!eighty_ninty_three(drive))
		mode = min(mode, (u8)1);
	return mode;
532 533 534 535 536 537 538 539 540 541 542
}

/*
 * Configuration functions
 */
/* Enables per-drive prefetch and postwrite */
static void config_drive_art_rwp (ide_drive_t *drive)
{
	ide_hwif_t *hwif	= HWIF(drive);
	struct pci_dev *dev	= hwif->pci_dev;

Jens Axboe's avatar
Jens Axboe committed
543 544
	u8 reg4bh		= 0;
	u8 rw_prefetch		= (0x11 << drive->dn);
545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563

#ifdef DEBUG
	printk("SIS5513: config_drive_art_rwp, drive %d\n", drive->dn);
	sis5513_load_verify_registers(dev, "config_drive_art_rwp start");
#endif

	if (drive->media != ide_disk)
		return;
	pci_read_config_byte(dev, 0x4b, &reg4bh);

	if ((reg4bh & rw_prefetch) != rw_prefetch)
		pci_write_config_byte(dev, 0x4b, reg4bh|rw_prefetch);
#ifdef DEBUG
	sis5513_load_verify_registers(dev, "config_drive_art_rwp end");
#endif
}


/* Set per-drive active and recovery time */
Jens Axboe's avatar
Jens Axboe committed
564
static void config_art_rwp_pio (ide_drive_t *drive, u8 pio)
565 566 567 568
{
	ide_hwif_t *hwif	= HWIF(drive);
	struct pci_dev *dev	= hwif->pci_dev;

Jens Axboe's avatar
Jens Axboe committed
569
	u8			timing, drive_pci, test1, test2;
570

Jens Axboe's avatar
Jens Axboe committed
571 572
	u16 eide_pio_timing[6] = {600, 390, 240, 180, 120, 90};
	u16 xfer_pio = drive->id->eide_pio_modes;
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597

#ifdef DEBUG
	sis5513_load_verify_registers(dev, "config_drive_art_rwp_pio start");
#endif

	config_drive_art_rwp(drive);
	pio = ide_get_best_pio_mode(drive, 255, pio, NULL);

	if (xfer_pio> 4)
		xfer_pio = 0;

	if (drive->id->eide_pio_iordy > 0) {
		for (xfer_pio = 5;
			(xfer_pio > 0) &&
			(drive->id->eide_pio_iordy > eide_pio_timing[xfer_pio]);
			xfer_pio--);
	} else {
		xfer_pio = (drive->id->eide_pio_modes & 4) ? 0x05 :
			   (drive->id->eide_pio_modes & 2) ? 0x04 :
			   (drive->id->eide_pio_modes & 1) ? 0x03 : xfer_pio;
	}

	timing = (xfer_pio >= pio) ? xfer_pio : pio;

#ifdef DEBUG
Jens Axboe's avatar
Jens Axboe committed
598 599
	printk("SIS5513: config_drive_art_rwp_pio, "
		"drive %d, pio %d, timing %d\n",
600 601 602
	       drive->dn, pio, timing);
#endif

Jens Axboe's avatar
Jens Axboe committed
603 604 605 606 607 608 609 610 611 612
	/* In pre ATA_133 case, drives sit at 0x40 + 4*drive->dn */
	drive_pci = 0x40;
	/* In SiS962 case drives sit at (0x40 or 0x70) + 8*drive->dn) */
	if (chipset_family >= ATA_133) {
		u32 reg54h;
		pci_read_config_dword(dev, 0x54, &reg54h);
		if (reg54h & 0x40000000) drive_pci = 0x70;
		drive_pci += ((drive->dn)*0x4);
	} else {
		drive_pci += ((drive->dn)*0x2);
613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632
	}

	/* register layout changed with newer ATA100 chips */
	if (chipset_family < ATA_100) {
		pci_read_config_byte(dev, drive_pci, &test1);
		pci_read_config_byte(dev, drive_pci+1, &test2);

		/* Clear active and recovery timings */
		test1 &= ~0x0F;
		test2 &= ~0x07;

		switch(timing) {
			case 4:		test1 |= 0x01; test2 |= 0x03; break;
			case 3:		test1 |= 0x03; test2 |= 0x03; break;
			case 2:		test1 |= 0x04; test2 |= 0x04; break;
			case 1:		test1 |= 0x07; test2 |= 0x06; break;
			default:	break;
		}
		pci_write_config_byte(dev, drive_pci, test1);
		pci_write_config_byte(dev, drive_pci+1, test2);
Jens Axboe's avatar
Jens Axboe committed
633 634 635
	} else if (chipset_family < ATA_133) {
		switch(timing) { /*		active  recovery
						  v     v */
636 637 638 639 640 641 642
			case 4:		test1 = 0x30|0x01; break;
			case 3:		test1 = 0x30|0x03; break;
			case 2:		test1 = 0x40|0x04; break;
			case 1:		test1 = 0x60|0x07; break;
			default:	break;
		}
		pci_write_config_byte(dev, drive_pci, test1);
Jens Axboe's avatar
Jens Axboe committed
643 644 645 646 647 648 649 650 651 652 653 654 655 656
	} else { /* ATA_133 */
		u32 test3;
		pci_read_config_dword(dev, drive_pci, &test3);
		test3 &= 0xc0c00fff;
		if (test3 & 0x08) {
			test3 |= (unsigned long)ini_time_value[ATA_133-ATA_00][timing] << 12;
			test3 |= (unsigned long)act_time_value[ATA_133-ATA_00][timing] << 16;
			test3 |= (unsigned long)rco_time_value[ATA_133-ATA_00][timing] << 24;
		} else {
			test3 |= (unsigned long)ini_time_value[ATA_100-ATA_00][timing] << 12;
			test3 |= (unsigned long)act_time_value[ATA_100-ATA_00][timing] << 16;
			test3 |= (unsigned long)rco_time_value[ATA_100-ATA_00][timing] << 24;
		}
		pci_write_config_dword(dev, drive_pci, test3);
657 658 659 660 661 662 663
	}

#ifdef DEBUG
	sis5513_load_verify_registers(dev, "config_drive_art_rwp_pio start");
#endif
}

Jens Axboe's avatar
Jens Axboe committed
664
static int config_chipset_for_pio (ide_drive_t *drive, u8 pio)
665
{
Jens Axboe's avatar
Jens Axboe committed
666 667 668 669 670
#if 0
	config_art_rwp_pio(drive, pio);
	return ide_config_drive_speed(drive, (XFER_PIO_0 + pio));
#else
	u8 speed;
671 672 673 674 675 676 677 678 679 680

	switch(pio) {
		case 4:		speed = XFER_PIO_4; break;
		case 3:		speed = XFER_PIO_3; break;
		case 2:		speed = XFER_PIO_2; break;
		case 1:		speed = XFER_PIO_1; break;
		default:	speed = XFER_PIO_0; break;
	}

	config_art_rwp_pio(drive, pio);
Jens Axboe's avatar
Jens Axboe committed
681 682
	return ide_config_drive_speed(drive, speed);
#endif
683 684
}

Jens Axboe's avatar
Jens Axboe committed
685
static int sis5513_tune_chipset (ide_drive_t *drive, u8 xferspeed)
686 687 688
{
	ide_hwif_t *hwif	= HWIF(drive);
	struct pci_dev *dev	= hwif->pci_dev;
Jens Axboe's avatar
Jens Axboe committed
689

Alan Cox's avatar
Alan Cox committed
690
	u8 drive_pci, reg, speed;
Jens Axboe's avatar
Jens Axboe committed
691
	u32 regdw;
692 693 694 695 696 697 698

#ifdef DEBUG
	sis5513_load_verify_registers(dev, "sis5513_tune_chipset start");
#endif

#ifdef BROKEN_LEVEL
#ifdef DEBUG
Jens Axboe's avatar
Jens Axboe committed
699
	printk("SIS5513: BROKEN_LEVEL activated, speed=%d -> speed=%d\n", xferspeed, BROKEN_LEVEL);
700
#endif
Jens Axboe's avatar
Jens Axboe committed
701
	if (xferspeed > BROKEN_LEVEL) xferspeed = BROKEN_LEVEL;
702 703
#endif

Alan Cox's avatar
Alan Cox committed
704 705 706 707 708 709
	speed = ide_rate_filter(sis5513_ratemask(drive), xferspeed);

#ifdef DEBUG
	printk("SIS5513: sis5513_tune_chipset, drive %d, speed %d\n",
	       drive->dn, xferspeed);
#endif
Jens Axboe's avatar
Jens Axboe committed
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732

	/* See config_art_rwp_pio for drive pci config registers */
	drive_pci = 0x40;
	if (chipset_family >= ATA_133) {
		u32 reg54h;
		pci_read_config_dword(dev, 0x54, &reg54h);
		if (reg54h & 0x40000000) drive_pci = 0x70;
		drive_pci += ((drive->dn)*0x4);
		pci_read_config_dword(dev, (unsigned long)drive_pci, &regdw);
		/* Disable UDMA bit for non UDMA modes on UDMA chips */
		if (speed < XFER_UDMA_0) {
			regdw &= 0xfffffffb;
			pci_write_config_dword(dev, (unsigned long)drive_pci, regdw);
		}
	
	} else {
		drive_pci += ((drive->dn)*0x2);
		pci_read_config_byte(dev, drive_pci+1, &reg);
		/* Disable UDMA bit for non UDMA modes on UDMA chips */
		if ((speed < XFER_UDMA_0) && (chipset_family > ATA_16)) {
			reg &= 0x7F;
			pci_write_config_byte(dev, drive_pci+1, reg);
		}
733 734 735 736
	}

	/* Config chip for mode */
	switch(speed) {
Jens Axboe's avatar
Jens Axboe committed
737
		case XFER_UDMA_6:
738 739 740 741 742 743
		case XFER_UDMA_5:
		case XFER_UDMA_4:
		case XFER_UDMA_3:
		case XFER_UDMA_2:
		case XFER_UDMA_1:
		case XFER_UDMA_0:
Jens Axboe's avatar
Jens Axboe committed
744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
			if (chipset_family >= ATA_133) {
				regdw |= 0x04;
				regdw &= 0xfffff00f;
				/* check if ATA133 enable */
				if (regdw & 0x08) {
					regdw |= (unsigned long)cycle_time_value[ATA_133-ATA_00][speed-XFER_UDMA_0] << 4;
					regdw |= (unsigned long)cvs_time_value[ATA_133-ATA_00][speed-XFER_UDMA_0] << 8;
				} else {
				/* if ATA133 disable, we should not set speed above UDMA5 */
					if (speed > XFER_UDMA_5)
						speed = XFER_UDMA_5;
					regdw |= (unsigned long)cycle_time_value[ATA_100-ATA_00][speed-XFER_UDMA_0] << 4;
					regdw |= (unsigned long)cvs_time_value[ATA_100-ATA_00][speed-XFER_UDMA_0] << 8;
				}
				pci_write_config_dword(dev, (unsigned long)drive_pci, regdw);
			} else {
				/* Force the UDMA bit on if we want to use UDMA */
				reg |= 0x80;
				/* clean reg cycle time bits */
				reg &= ~((0xFF >> (8 - cycle_time_range[chipset_family]))
					 << cycle_time_offset[chipset_family]);
				/* set reg cycle time bits */
				reg |= cycle_time_value[chipset_family-ATA_00][speed-XFER_UDMA_0]
					<< cycle_time_offset[chipset_family];
				pci_write_config_byte(dev, drive_pci+1, reg);
			}
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
			break;
		case XFER_MW_DMA_2:
		case XFER_MW_DMA_1:
		case XFER_MW_DMA_0:
		case XFER_SW_DMA_2:
		case XFER_SW_DMA_1:
		case XFER_SW_DMA_0:
			break;
		case XFER_PIO_4: return((int) config_chipset_for_pio(drive, 4));
		case XFER_PIO_3: return((int) config_chipset_for_pio(drive, 3));
		case XFER_PIO_2: return((int) config_chipset_for_pio(drive, 2));
		case XFER_PIO_1: return((int) config_chipset_for_pio(drive, 1));
		case XFER_PIO_0:
		default:	 return((int) config_chipset_for_pio(drive, 0));	
	}
#ifdef DEBUG
	sis5513_load_verify_registers(dev, "sis5513_tune_chipset end");
#endif
	return ((int) ide_config_drive_speed(drive, speed));
}

Jens Axboe's avatar
Jens Axboe committed
791
static void sis5513_tune_drive (ide_drive_t *drive, u8 pio)
792 793 794 795 796 797 798 799 800
{
	(void) config_chipset_for_pio(drive, pio);
}

/*
 * ((id->hw_config & 0x4000|0x2000) && (HWIF(drive)->udma_four))
 */
static int config_chipset_for_dma (ide_drive_t *drive)
{
Jens Axboe's avatar
Jens Axboe committed
801
	u8 speed	= ide_dma_speed(drive, sis5513_ratemask(drive));
802 803

#ifdef DEBUG
Alan Cox's avatar
Alan Cox committed
804
	printk("SIS5513: config_chipset_for_dma, drive %d, ultra %x\n",
Jens Axboe's avatar
Jens Axboe committed
805
	       drive->dn, drive->id->dma_ultra);
806 807
#endif

Jens Axboe's avatar
Jens Axboe committed
808 809 810
	if (!(speed))
		return 0;

811
	sis5513_tune_chipset(drive, speed);
Jens Axboe's avatar
Jens Axboe committed
812
	return ide_dma_enable(drive);
813 814
}

Jens Axboe's avatar
Jens Axboe committed
815
static int sis5513_config_drive_xfer_rate (ide_drive_t *drive)
816
{
Jens Axboe's avatar
Jens Axboe committed
817 818
	ide_hwif_t *hwif	= HWIF(drive);
	struct hd_driveid *id	= drive->id;
819 820 821

	drive->init_speed = 0;

Jens Axboe's avatar
Jens Axboe committed
822
	if (id && (id->capability & 1) && drive->autodma) {
823
		/* Consult the list of known "bad" drives */
Jens Axboe's avatar
Jens Axboe committed
824
		if (hwif->ide_dma_bad_drive(drive))
825 826
			goto fast_ata_pio;
		if (id->field_valid & 4) {
Jens Axboe's avatar
Jens Axboe committed
827
			if (id->dma_ultra & hwif->ultra_mask) {
828
				/* Force if Capable UltraDMA */
Jens Axboe's avatar
Jens Axboe committed
829 830
				int dma = config_chipset_for_dma(drive);
				if ((id->field_valid & 2) && !dma)
831 832 833 834
					goto try_dma_modes;
			}
		} else if (id->field_valid & 2) {
try_dma_modes:
Jens Axboe's avatar
Jens Axboe committed
835 836
			if ((id->dma_mword & hwif->mwdma_mask) ||
			    (id->dma_1word & hwif->swdma_mask)) {
837
				/* Force if Capable regular DMA modes */
Jens Axboe's avatar
Jens Axboe committed
838
				if (!config_chipset_for_dma(drive))
839 840
					goto no_dma_set;
			}
Jens Axboe's avatar
Jens Axboe committed
841 842
		} else if (hwif->ide_dma_good_drive(drive) &&
			   (id->eide_dma_time < 150)) {
843
			/* Consult the list of known "good" drives */
Jens Axboe's avatar
Jens Axboe committed
844
			if (!config_chipset_for_dma(drive))
845 846 847 848 849 850 851 852
				goto no_dma_set;
		} else {
			goto fast_ata_pio;
		}
	} else if ((id->capability & 8) || (id->field_valid & 2)) {
fast_ata_pio:
no_dma_set:
		sis5513_tune_drive(drive, 5);
Jens Axboe's avatar
Jens Axboe committed
853
		return hwif->ide_dma_off_quietly(drive);
854
	}
Jens Axboe's avatar
Jens Axboe committed
855
	return hwif->ide_dma_on(drive);
856 857 858
}

/* initiates/aborts (U)DMA read/write operations on a drive. */
Jens Axboe's avatar
Jens Axboe committed
859
static int sis5513_config_xfer_rate (ide_drive_t *drive)
860
{
Jens Axboe's avatar
Jens Axboe committed
861 862 863
	config_drive_art_rwp(drive);
	config_art_rwp_pio(drive, 5);
	return sis5513_config_drive_xfer_rate(drive);
864
}
Jens Axboe's avatar
Jens Axboe committed
865

866 867 868 869 870 871 872 873 874 875 876 877 878
/* Helper function used at init time
 * returns a PCI device revision ID
 * (used to detect different IDE controller versions)
 */
static u8 __init devfn_rev(int device, int function)
{
	u8 revision;
	/* Find device */
	struct pci_dev* dev = pci_find_slot(0,PCI_DEVFN(device,function));
	pci_read_config_byte(dev, PCI_REVISION_ID, &revision);
	return revision;
}

879
/* Chip detection and general config */
Jens Axboe's avatar
Jens Axboe committed
880
static unsigned int __init init_chipset_sis5513 (struct pci_dev *dev, const char *name)
881 882 883 884 885 886 887 888 889 890 891 892 893 894
{
	struct pci_dev *host;
	int i = 0;

	/* Find the chip */
	for (i = 0; i < ARRAY_SIZE(SiSHostChipInfo) && !host_dev; i++) {
		host = pci_find_device (PCI_VENDOR_ID_SI,
					SiSHostChipInfo[i].host_id,
					NULL);
		if (!host)
			continue;

		host_dev = host;
		chipset_family = SiSHostChipInfo[i].chipset_family;
Jens Axboe's avatar
Jens Axboe committed
895 896 897 898
	
		/* check 100/133 chipset family */
		if (chipset_family == ATA_133) {
			u32 reg54h;
899
			u16 devid;
Jens Axboe's avatar
Jens Axboe committed
900
			pci_read_config_dword(dev, 0x54, &reg54h);
901
			/* SiS962 and above report 0x5518 dev id if high bit is cleared */
Jens Axboe's avatar
Jens Axboe committed
902
			pci_write_config_dword(dev, 0x54, (reg54h & 0x7fffffff));
903 904
			pci_read_config_word(dev, 0x02, &devid);
			/* restore register 0x54 */
Jens Axboe's avatar
Jens Axboe committed
905
			pci_write_config_dword(dev, 0x54, reg54h);
906

Jens Axboe's avatar
Jens Axboe committed
907
			/* devid 5518 here means SiS962 or later
908 909 910 911
			   which supports ATA133.
			   These are refered by chipset_family = ATA133
			*/
			if (devid != 0x5518) {
Jens Axboe's avatar
Jens Axboe committed
912 913 914
				u8 reg49h;
				/* SiS961 family */
				pci_read_config_byte(dev, 0x49, &reg49h);
915 916
				/* check isa bridge device rev id */
				if (((devfn_rev(2,0) & 0xff) == 0x10) && (reg49h & 0x80))
Jens Axboe's avatar
Jens Axboe committed
917 918 919 920 921
					chipset_family = ATA_133a;
				else
					chipset_family = ATA_100;
			}
		}
922
		printk(SiSHostChipInfo[i].name);
Jens Axboe's avatar
Jens Axboe committed
923
		printk("    %s controller", chipset_capability[chipset_family]);
924 925 926 927 928 929 930
		printk("\n");

#ifdef DEBUG
		sis5513_print_registers(dev, "pci_init_sis5513 start");
#endif

		if (SiSHostChipInfo[i].flags & SIS5513_LATENCY) {
Jens Axboe's avatar
Jens Axboe committed
931
			u8 latency = (chipset_family == ATA_100)? 0x80 : 0x10; /* Lacking specs */
932 933
			pci_write_config_byte(dev, PCI_LATENCY_TIMER, latency);
		}
934 935 936 937 938 939 940 941

		/* Special case for SiS630 : 630S/ET is ATA_100a */
		if (SiSHostChipInfo[i].host_id == PCI_DEVICE_ID_SI_630) {
			/* check host device rev id */
			if (devfn_rev(0,0) >= 0x30) {
				chipset_family = ATA_100a;
			}
		}
942 943 944
	}

	/* Make general config ops here
Alan Cox's avatar
Alan Cox committed
945
	   1/ tell IDE channels to operate in Compatibility mode only
946 947
	   2/ tell old chips to allow per drive IDE timings */
	if (host_dev) {
Jens Axboe's avatar
Jens Axboe committed
948 949
		u8 reg;
		u16 regw;
950 951
		switch(chipset_family) {
			case ATA_133:
Jens Axboe's avatar
Jens Axboe committed
952 953 954 955 956 957 958 959 960
				/* SiS962 operation mode */
				pci_read_config_word(dev, 0x50, &regw);
				if (regw & 0x08)
					pci_write_config_word(dev, 0x50, regw&0xfff7);
				pci_read_config_word(dev, 0x52, &regw);
				if (regw & 0x08)
					pci_write_config_word(dev, 0x52, regw&0xfff7);
				break;
			case ATA_133a:
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997
			case ATA_100:
				/* Set compatibility bit */
				pci_read_config_byte(dev, 0x49, &reg);
				if (!(reg & 0x01)) {
					pci_write_config_byte(dev, 0x49, reg|0x01);
				}
				break;
			case ATA_100a:
			case ATA_66:
				/* On ATA_66 chips the bit was elsewhere */
				pci_read_config_byte(dev, 0x52, &reg);
				if (!(reg & 0x04)) {
					pci_write_config_byte(dev, 0x52, reg|0x04);
				}
				break;
			case ATA_33:
				/* On ATA_33 we didn't have a single bit to set */
				pci_read_config_byte(dev, 0x09, &reg);
				if ((reg & 0x0f) != 0x00) {
					pci_write_config_byte(dev, 0x09, reg&0xf0);
				}
			case ATA_16:
				/* force per drive recovery and active timings
				   needed on ATA_33 and below chips */
				pci_read_config_byte(dev, 0x52, &reg);
				if (!(reg & 0x08)) {
					pci_write_config_byte(dev, 0x52, reg|0x08);
				}
				break;
			case ATA_00:
			default: break;
		}

#if defined(DISPLAY_SIS_TIMINGS) && defined(CONFIG_PROC_FS)
		if (!sis_proc) {
			sis_proc = 1;
			bmide_dev = dev;
Jens Axboe's avatar
Jens Axboe committed
998
			ide_pci_register_host_proc(&sis_procs[0]);
999 1000 1001 1002 1003 1004 1005 1006 1007
		}
#endif
	}
#ifdef DEBUG
	sis5513_load_verify_registers(dev, "pci_init_sis5513 end");
#endif
	return 0;
}

Jens Axboe's avatar
Jens Axboe committed
1008
static unsigned int __init ata66_sis5513 (ide_hwif_t *hwif)
1009
{
Jens Axboe's avatar
Jens Axboe committed
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020
	u8 ata66 = 0;

	if (chipset_family >= ATA_133) {
		u16 regw = 0;
		u16 reg_addr = hwif->channel ? 0x52: 0x50;
		pci_read_config_word(hwif->pci_dev, reg_addr, &regw);
		ata66 = (regw & 0x8000) ? 0 : 1;
	} else if (chipset_family >= ATA_66) {
		u8 reg48h = 0;
		u8 mask = hwif->channel ? 0x20 : 0x10;
		pci_read_config_byte(hwif->pci_dev, 0x48, &reg48h);
1021 1022 1023 1024 1025
		ata66 = (reg48h & mask) ? 0 : 1;
	}
        return ata66;
}

Jens Axboe's avatar
Jens Axboe committed
1026
static void __init init_hwif_sis5513 (ide_hwif_t *hwif)
1027
{
Jens Axboe's avatar
Jens Axboe committed
1028
	hwif->autodma = 0;
1029

Jens Axboe's avatar
Jens Axboe committed
1030 1031
	if (!hwif->irq)
		hwif->irq = hwif->channel ? 15 : 14;
1032 1033 1034 1035

	hwif->tuneproc = &sis5513_tune_drive;
	hwif->speedproc = &sis5513_tune_chipset;

Jens Axboe's avatar
Jens Axboe committed
1036 1037 1038
	if (!(hwif->dma_base)) {
		hwif->drives[0].autotune = 1;
		hwif->drives[1].autotune = 1;
1039
		return;
Jens Axboe's avatar
Jens Axboe committed
1040 1041 1042 1043 1044 1045
	}

	hwif->atapi_dma = 1;
	hwif->ultra_mask = 0x7f;
	hwif->mwdma_mask = 0x07;
	hwif->swdma_mask = 0x07;
1046

Jens Axboe's avatar
Jens Axboe committed
1047 1048 1049 1050 1051 1052 1053 1054 1055 1056
	if (!host_dev)
		return;

	if (!(hwif->udma_four))
		hwif->udma_four = ata66_sis5513(hwif);

	if (chipset_family > ATA_16) {
		hwif->ide_dma_check = &sis5513_config_xfer_rate;
		if (!noautodma)
			hwif->autodma = 1;
1057
	}
Jens Axboe's avatar
Jens Axboe committed
1058 1059
	hwif->drives[0].autodma = hwif->autodma;
	hwif->drives[1].autodma = hwif->autodma;
1060 1061 1062
	return;
}

Jens Axboe's avatar
Jens Axboe committed
1063
static void __init init_dma_sis5513 (ide_hwif_t *hwif, unsigned long dmabase)
1064
{
Jens Axboe's avatar
Jens Axboe committed
1065 1066 1067 1068
	ide_setup_dma(hwif, dmabase, 8);
}

extern void ide_setup_pci_device(struct pci_dev *, ide_pci_device_t *);
1069

1070 1071

static int __devinit sis5513_init_one(struct pci_dev *dev, const struct pci_device_id *id)
Jens Axboe's avatar
Jens Axboe committed
1072
{
1073 1074 1075
	ide_pci_device_t *d = &sis5513_chipsets[id->driver_data];
	if (dev->device != d->device)
		BUG();
1076
	ide_setup_pci_device(dev, d);
1077
	MOD_INC_USE_COUNT;
1078
	return 0;
1079
}
Jens Axboe's avatar
Jens Axboe committed
1080

1081 1082 1083 1084
static struct pci_device_id sis5513_pci_tbl[] __devinitdata = {
	{ PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_5513, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
	{ 0, },
};
Jens Axboe's avatar
Jens Axboe committed
1085

1086
static struct pci_driver driver = {
1087 1088 1089
	.name		= "SIS IDE",
	.id_table	= sis5513_pci_tbl,
	.probe		= sis5513_init_one,
1090 1091 1092 1093 1094
};

static int sis5513_ide_init(void)
{
	return ide_pci_register_driver(&driver);
Jens Axboe's avatar
Jens Axboe committed
1095 1096
}

1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
static void sis5513_ide_exit(void)
{
	ide_pci_unregister_driver(&driver);
}

module_init(sis5513_ide_init);
module_exit(sis5513_ide_exit);

MODULE_AUTHOR("Lionel Bouton, L C Chang, Andre Hedrick");
MODULE_DESCRIPTION("PCI driver module for SIS IDE");
MODULE_LICENSE("GPL");

Alan Cox's avatar
Alan Cox committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120
/*
 * TODO:
 *	- Get ridden of SisHostChipInfo[] completness dependancy.
 *	- Study drivers/ide/ide-timing.h.
 *	- Are there pre-ATA_16 SiS5513 chips ? -> tune init code for them
 *	  or remove ATA_00 define
 *	- More checks in the config registers (force values instead of
 *	  relying on the BIOS setting them correctly).
 *	- Further optimisations ?
 *	  . for example ATA66+ regs 0x48 & 0x4A
 */