siimage.c 27.9 KB
Newer Older
1
/*
2
 * linux/drivers/ide/pci/siimage.c		Version 1.07	Nov 30, 2003
3 4
 *
 * Copyright (C) 2001-2002	Andre Hedrick <andre@linux-ide.org>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * Copyright (C) 2003		Red Hat <alan@redhat.com>
 *
 *  May be copied or modified under the terms of the GNU General Public License
 *
 *  Documentation available under NDA only
 *
 *
 *  FAQ Items:
 *	If you are using Marvell SATA-IDE adapters with Maxtor drives
 *	ensure the system is set up for ATA100/UDMA5 not UDMA6.
 *
 *	If you are using WD drives with SATA bridges you must set the
 *	drive to "Single". "Master" will hang
 *
 *	If you have strange problems with nVidia chipset systems please
 *	see the SI support documentation and update your system BIOS
 *	if neccessary
22 23
 */

24
#include <linux/config.h>
25
#include <linux/types.h>
26
#include <linux/module.h>
27 28 29 30 31 32 33 34
#include <linux/pci.h>
#include <linux/delay.h>
#include <linux/hdreg.h>
#include <linux/ide.h>
#include <linux/init.h>

#include <asm/io.h>

35 36
#undef SIIMAGE_VIRTUAL_DMAPIO
#undef SIIMAGE_LARGE_DMA
37

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
/**
 *	pdev_is_sata		-	check if device is SATA
 *	@pdev:	PCI device to check
 *	
 *	Returns true if this is a SATA controller
 */
 
static int pdev_is_sata(struct pci_dev *pdev)
{
	switch(pdev->device)
	{
		case PCI_DEVICE_ID_SII_3112:
		case PCI_DEVICE_ID_SII_1210SA:
			return 1;
		case PCI_DEVICE_ID_SII_680:
			return 0;
	}
	BUG();
	return 0;
}
 
/**
 *	is_sata			-	check if hwif is SATA
 *	@hwif:	interface to check
 *	
 *	Returns true if this is a SATA controller
 */
 
static inline int is_sata(ide_hwif_t *hwif)
{
	return pdev_is_sata(hwif->pci_dev);
}

/**
 *	siimage_selreg		-	return register base
 *	@hwif: interface
 *	@r: config offset
 *
 *	Turn a config register offset into the right address in either
 *	PCI space or MMIO space to access the control register in question
 *	Thankfully this is a configuration operation so isnt performance
 *	criticial. 
 */
 
static unsigned long siimage_selreg(ide_hwif_t *hwif, int r)
{
	unsigned long base = (unsigned long)hwif->hwif_data;
	base += 0xA0 + r;
	if(hwif->mmio)
		base += (hwif->channel << 6);
	else
		base += (hwif->channel << 4);
	return base;
}
	
/**
 *	siimage_seldev		-	return register base
 *	@hwif: interface
 *	@r: config offset
 *
 *	Turn a config register offset into the right address in either
 *	PCI space or MMIO space to access the control register in question
 *	including accounting for the unit shift.
 */
 
static inline unsigned long siimage_seldev(ide_drive_t *drive, int r)
{
	ide_hwif_t *hwif	= HWIF(drive);
	unsigned long base = (unsigned long)hwif->hwif_data;
	base += 0xA0 + r;
	if(hwif->mmio)
		base += (hwif->channel << 6);
	else
		base += (hwif->channel << 4);
	base |= drive->select.b.unit << drive->select.b.unit;
	return base;
}
115

116 117 118 119 120 121 122 123 124 125
/**
 *	siimage_ratemask	-	Compute available modes
 *	@drive: IDE drive
 *
 *	Compute the available speeds for the devices on the interface.
 *	For the CMD680 this depends on the clocking mode (scsc), for the
 *	SI3312 SATA controller life is a bit simpler. Enforce UDMA33
 *	as a limit if there is no 80pin cable present.
 */
 
126 127 128 129
static byte siimage_ratemask (ide_drive_t *drive)
{
	ide_hwif_t *hwif	= HWIF(drive);
	u8 mode	= 0, scsc = 0;
130
	unsigned long base = (unsigned long) hwif->hwif_data;
131 132

	if (hwif->mmio)
133
		scsc = hwif->INB(base + 0x4A);
134 135 136
	else
		pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);

137 138 139 140 141
	if(is_sata(hwif))
	{
		if(strstr(drive->id->model, "Maxtor"))
			return 3;
		return 4;
142
	}
143 144 145 146 147 148 149 150 151 152
	
	if ((scsc & 0x30) == 0x10)	/* 133 */
		mode = 4;
	else if ((scsc & 0x30) == 0x20)	/* 2xPCI */
		mode = 4;
	else if ((scsc & 0x30) == 0x00)	/* 100 */
		mode = 3;
	else 	/* Disabled ? */
		BUG();

153 154 155 156 157
	if (!eighty_ninty_three(drive))
		mode = min(mode, (u8)1);
	return mode;
}

158 159 160 161 162 163 164 165
/**
 *	siimage_taskfile_timing	-	turn timing data to a mode
 *	@hwif: interface to query
 *
 *	Read the timing data for the interface and return the 
 *	mode that is being used.
 */
 
166 167 168
static byte siimage_taskfile_timing (ide_hwif_t *hwif)
{
	u16 timing	= 0x328a;
169
	unsigned long addr = siimage_selreg(hwif, 2);
170 171

	if (hwif->mmio)
172
		timing = hwif->INW(addr);
173
	else
174
		pci_read_config_word(hwif->pci_dev, addr, &timing);
175 176 177 178

	switch (timing) {
		case 0x10c1:	return 4;
		case 0x10c3:	return 3;
179
		case 0x1104:
180 181 182 183 184 185 186
		case 0x1281:	return 2;
		case 0x2283:	return 1;
		case 0x328a:
		default:	return 0;
	}
}

187 188 189 190 191 192 193 194 195 196
/**
 *	simmage_tuneproc	-	tune a drive
 *	@drive: drive to tune
 *	@mode_wanted: the target operating mode
 *
 *	Load the timing settings for this device mode into the
 *	controller. If we are in PIO mode 3 or 4 turn on IORDY
 *	monitoring (bit 9). The TF timing is bits 31:16
 */
 
197 198 199
static void siimage_tuneproc (ide_drive_t *drive, byte mode_wanted)
{
	ide_hwif_t *hwif	= HWIF(drive);
200 201 202 203 204
	u32 speedt		= 0;
	u16 speedp		= 0;
	unsigned long addr	= siimage_seldev(drive, 0x04);
	unsigned long tfaddr	= siimage_selreg(hwif, 0x02);
	
205 206
	/* cheat for now and use the docs */
	switch(mode_wanted) {
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222
		case 4:	
			speedp = 0x10c1; 
			speedt = 0x10c1;
			break;
		case 3:	
			speedp = 0x10C3; 
			speedt = 0x10C3;
			break;
		case 2:	
			speedp = 0x1104; 
			speedt = 0x1281;
			break;
		case 1:		
			speedp = 0x2283; 
			speedt = 0x1281;
			break;
223
		case 0:
224 225 226 227
		default:
			speedp = 0x328A; 
			speedt = 0x328A;
			break;
228 229
	}
	if (hwif->mmio)
230 231 232 233 234 235 236 237 238
	{
		hwif->OUTW(speedt, addr);
		hwif->OUTW(speedp, tfaddr);
		/* Now set up IORDY */
		if(mode_wanted == 3 || mode_wanted == 4)
			hwif->OUTW(hwif->INW(tfaddr-2)|0x200, tfaddr-2);
		else
			hwif->OUTW(hwif->INW(tfaddr-2)&~0x200, tfaddr-2);
	}
239
	else
240 241 242 243 244 245 246 247 248 249
	{
		pci_write_config_word(hwif->pci_dev, addr, speedp);
		pci_write_config_word(hwif->pci_dev, tfaddr, speedt);
		pci_read_config_word(hwif->pci_dev, tfaddr-2, &speedp);
		speedp &= ~0x200;
		/* Set IORDY for mode 3 or 4 */
		if(mode_wanted == 3 || mode_wanted == 4)
			speedp |= 0x200;
		pci_write_config_word(hwif->pci_dev, tfaddr-2, speedp);
	}
250 251
}

252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/**
 *	config_siimage_chipset_for_pio	-	set drive timings
 *	@drive: drive to tune
 *	@speed we want
 *
 *	Compute the best pio mode we can for a given device. Also honour
 *	the timings for the driver when dealing with mixed devices. Some
 *	of this is ugly but its all wrapped up here
 *
 *	The SI680 can also do VDMA - we need to start using that
 *
 *	FIXME: we use the BIOS channel timings to avoid driving the task
 *	files too fast at the disk. We need to compute the master/slave
 *	drive PIO mode properly so that we can up the speed on a hotplug
 *	system.
 */
 
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
static void config_siimage_chipset_for_pio (ide_drive_t *drive, byte set_speed)
{
	u8 channel_timings	= siimage_taskfile_timing(HWIF(drive));
	u8 speed = 0, set_pio	= ide_get_best_pio_mode(drive, 4, 5, NULL);

	/* WARNING PIO timing mess is going to happen b/w devices, argh */
	if ((channel_timings != set_pio) && (set_pio > channel_timings))
		set_pio = channel_timings;

	siimage_tuneproc(drive, set_pio);
	speed = XFER_PIO_0 + set_pio;
	if (set_speed)
		(void) ide_config_drive_speed(drive, speed);
}

static void config_chipset_for_pio (ide_drive_t *drive, byte set_speed)
{
	config_siimage_chipset_for_pio(drive, set_speed);
}

289 290 291 292 293 294 295 296 297 298
/**
 *	siimage_tune_chipset	-	set controller timings
 *	@drive: Drive to set up
 *	@xferspeed: speed we want to achieve
 *
 *	Tune the SII chipset for the desired mode. If we can't achieve
 *	the desired mode then tune for a lower one, but ultimately
 *	make the thing work.
 */
 
299 300 301 302 303 304 305 306 307
static int siimage_tune_chipset (ide_drive_t *drive, byte xferspeed)
{
	u8 ultra6[]		= { 0x0F, 0x0B, 0x07, 0x05, 0x03, 0x02, 0x01 };
	u8 ultra5[]		= { 0x0C, 0x07, 0x05, 0x04, 0x02, 0x01 };
	u16 dma[]		= { 0x2208, 0x10C2, 0x10C1 };

	ide_hwif_t *hwif	= HWIF(drive);
	u16 ultra = 0, multi	= 0;
	u8 mode = 0, unit	= drive->select.b.unit;
308 309
	u8 speed		= ide_rate_filter(siimage_ratemask(drive), xferspeed);
	unsigned long base	= (unsigned long)hwif->hwif_data;
310 311 312
	u8 scsc = 0, addr_mask	= ((hwif->channel) ?
				    ((hwif->mmio) ? 0xF4 : 0x84) :
				    ((hwif->mmio) ? 0xB4 : 0x80));
313 314 315
				    
	unsigned long ma	= siimage_seldev(drive, 0x08);
	unsigned long ua	= siimage_seldev(drive, 0x0C);
316 317

	if (hwif->mmio) {
318 319 320 321
		scsc = hwif->INB(base + 0x4A);
		mode = hwif->INB(base + addr_mask);
		multi = hwif->INW(ma);
		ultra = hwif->INW(ua);
322
	} else {
323
		pci_read_config_byte(hwif->pci_dev, 0x8A, &scsc);
324
		pci_read_config_byte(hwif->pci_dev, addr_mask, &mode);
325 326
		pci_read_config_word(hwif->pci_dev, ma, &multi);
		pci_read_config_word(hwif->pci_dev, ua, &ultra);
327 328 329 330 331 332
	}

	mode &= ~((unit) ? 0x30 : 0x03);
	ultra &= ~0x3F;
	scsc = ((scsc & 0x30) == 0x00) ? 0 : 1;

333
	scsc = is_sata(hwif) ? 1 : scsc;
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358

	switch(speed) {
		case XFER_PIO_4:
		case XFER_PIO_3:
		case XFER_PIO_2:
		case XFER_PIO_1:
		case XFER_PIO_0:
			siimage_tuneproc(drive, (speed - XFER_PIO_0));
			mode |= ((unit) ? 0x10 : 0x01);
			break;
		case XFER_MW_DMA_2:
		case XFER_MW_DMA_1:
		case XFER_MW_DMA_0:
			multi = dma[speed - XFER_MW_DMA_0];
			mode |= ((unit) ? 0x20 : 0x02);
			config_siimage_chipset_for_pio(drive, 0);
			break;
		case XFER_UDMA_6:
		case XFER_UDMA_5:
		case XFER_UDMA_4:
		case XFER_UDMA_3:
		case XFER_UDMA_2:
		case XFER_UDMA_1:
		case XFER_UDMA_0:
			multi = dma[2];
359 360
			ultra |= ((scsc) ? (ultra6[speed - XFER_UDMA_0]) :
					   (ultra5[speed - XFER_UDMA_0]));
361 362 363 364 365 366 367 368
			mode |= ((unit) ? 0x30 : 0x03);
			config_siimage_chipset_for_pio(drive, 0);
			break;
		default:
			return 1;
	}

	if (hwif->mmio) {
369 370 371
		hwif->OUTB(mode, base + addr_mask);
		hwif->OUTW(multi, ma);
		hwif->OUTW(ultra, ua);
372 373
	} else {
		pci_write_config_byte(hwif->pci_dev, addr_mask, mode);
374 375
		pci_write_config_word(hwif->pci_dev, ma, multi);
		pci_write_config_word(hwif->pci_dev, ua, ultra);
376 377 378 379
	}
	return (ide_config_drive_speed(drive, speed));
}

380 381 382 383 384 385 386 387 388
/**
 *	config_chipset_for_dma	-	configure for DMA
 *	@drive: drive to configure
 *
 *	Called by the IDE layer when it wants the timings set up.
 *	For the CMD680 we also need to set up the PIO timings and
 *	enable DMA.
 */
 
389 390 391 392
static int config_chipset_for_dma (ide_drive_t *drive)
{
	u8 speed	= ide_dma_speed(drive, siimage_ratemask(drive));

393
	config_chipset_for_pio(drive, !speed);
394

395
	if (!speed)
396 397
		return 0;

398
	if (ide_set_xfer_rate(drive, speed))
399 400 401 402 403 404 405 406
		return 0;

	if (!drive->init_speed)
		drive->init_speed = speed;

	return ide_dma_enable(drive);
}

407 408 409 410 411 412 413 414 415 416
/**
 *	siimage_configure_drive_for_dma	-	set up for DMA transfers
 *	@drive: drive we are going to set up
 *
 *	Set up the drive for DMA, tune the controller and drive as 
 *	required. If the drive isn't suitable for DMA or we hit
 *	other problems then we will drop down to PIO and set up
 *	PIO appropriately
 */
 
417 418 419 420 421
static int siimage_config_drive_for_dma (ide_drive_t *drive)
{
	ide_hwif_t *hwif	= HWIF(drive);
	struct hd_driveid *id	= drive->id;

422
	if ((id->capability & 1) != 0 && drive->autodma) {
423 424 425 426

		if (ide_use_dma(drive)) {
			if (config_chipset_for_dma(drive))
				return hwif->ide_dma_on(drive);
427
		}
428 429 430

		goto fast_ata_pio;

431 432 433 434 435
	} else if ((id->capability & 8) || (id->field_valid & 2)) {
fast_ata_pio:
		config_chipset_for_pio(drive, 1);
		return hwif->ide_dma_off_quietly(drive);
	}
436 437
	/* IORDY not supported */
	return 0;
438 439 440 441 442 443 444
}

/* returns 1 if dma irq issued, 0 otherwise */
static int siimage_io_ide_dma_test_irq (ide_drive_t *drive)
{
	ide_hwif_t *hwif	= HWIF(drive);
	u8 dma_altstat		= 0;
445
	unsigned long addr	= siimage_selreg(hwif, 1);
446 447 448 449 450 451

	/* return 1 if INTR asserted */
	if ((hwif->INB(hwif->dma_status) & 4) == 4)
		return 1;

	/* return 1 if Device INTR asserted */
452
	pci_read_config_byte(hwif->pci_dev, addr, &dma_altstat);
453
	if (dma_altstat & 8)
454 455 456 457
		return 0;	//return 1;
	return 0;
}

458
#if 0
459 460 461 462 463 464 465 466 467
/**
 *	siimage_mmio_ide_dma_count	-	DMA bytes done
 *	@drive
 *
 *	If we are doing VDMA the CMD680 requires a little bit
 *	of more careful handling and we have to read the counts
 *	off ourselves. For non VDMA life is normal.
 */
 
468 469 470 471 472 473 474
static int siimage_mmio_ide_dma_count (ide_drive_t *drive)
{
#ifdef SIIMAGE_VIRTUAL_DMAPIO
	struct request *rq	= HWGROUP(drive)->rq;
	ide_hwif_t *hwif	= HWIF(drive);
	u32 count		= (rq->nr_sectors * SECTOR_SIZE);
	u32 rcount		= 0;
475
	unsigned long addr	= siimage_selreg(hwif, 0x1C);
476

477 478
	hwif->OUTL(count, addr);
	rcount = hwif->INL(addr);
479 480 481 482 483 484 485

	printk("\n%s: count = %d, rcount = %d, nr_sectors = %lu\n",
		drive->name, count, rcount, rq->nr_sectors);

#endif /* SIIMAGE_VIRTUAL_DMAPIO */
	return __ide_dma_count(drive);
}
486
#endif
487

488 489 490 491 492 493 494 495
/**
 *	siimage_mmio_ide_dma_test_irq	-	check we caused an IRQ
 *	@drive: drive we are testing
 *
 *	Check if we caused an IDE DMA interrupt. We may also have caused
 *	SATA status interrupts, if so we clean them up and continue.
 */
 
496 497 498
static int siimage_mmio_ide_dma_test_irq (ide_drive_t *drive)
{
	ide_hwif_t *hwif	= HWIF(drive);
499 500
	unsigned long base	= (unsigned long)hwif->hwif_data;
	unsigned long addr	= siimage_selreg(hwif, 0x1);
501 502

	if (SATA_ERROR_REG) {
503
		u32 ext_stat = hwif->INL(base + 0x10);
504 505 506 507 508 509
		u8 watchdog = 0;
		if (ext_stat & ((hwif->channel) ? 0x40 : 0x10)) {
			u32 sata_error = hwif->INL(SATA_ERROR_REG);
			hwif->OUTL(sata_error, SATA_ERROR_REG);
			watchdog = (sata_error & 0x00680000) ? 1 : 0;
#if 1
510
			printk(KERN_WARNING "%s: sata_error = 0x%08x, "
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
				"watchdog = %d, %s\n",
				drive->name, sata_error, watchdog,
				__FUNCTION__);
#endif

		} else {
			watchdog = (ext_stat & 0x8000) ? 1 : 0;
		}
		ext_stat >>= 16;

		if (!(ext_stat & 0x0404) && !watchdog)
			return 0;
	}

	/* return 1 if INTR asserted */
	if ((hwif->INB(hwif->dma_status) & 0x04) == 0x04)
		return 1;

	/* return 1 if Device INTR asserted */
530
	if ((hwif->INB(addr) & 8) == 8)
531 532 533 534 535
		return 0;	//return 1;

	return 0;
}

536 537 538 539 540 541 542 543 544 545
/**
 *	siimage_busproc		-	bus isolation ioctl
 *	@drive: drive to isolate/restore
 *	@state: bus state to set
 *
 *	Used by the SII3112 to handle bus isolation. As this is a 
 *	SATA controller the work required is quite limited, we 
 *	just have to clean up the statistics
 */
 
546 547 548 549
static int siimage_busproc (ide_drive_t * drive, int state)
{
	ide_hwif_t *hwif	= HWIF(drive);
	u32 stat_config		= 0;
550
	unsigned long addr	= siimage_selreg(hwif, 0);
551 552

	if (hwif->mmio) {
553
		stat_config = hwif->INL(addr);
554
	} else
555
		pci_read_config_dword(hwif->pci_dev, addr, &stat_config);
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570

	switch (state) {
		case BUSSTATE_ON:
			hwif->drives[0].failures = 0;
			hwif->drives[1].failures = 0;
			break;
		case BUSSTATE_OFF:
			hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
			hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
			break;
		case BUSSTATE_TRISTATE:
			hwif->drives[0].failures = hwif->drives[0].max_failures + 1;
			hwif->drives[1].failures = hwif->drives[1].max_failures + 1;
			break;
		default:
571
			return -EINVAL;
572 573 574 575 576
	}
	hwif->bus_state = state;
	return 0;
}

577 578 579 580 581 582 583 584
/**
 *	siimage_reset_poll	-	wait for sata reset
 *	@drive: drive we are resetting
 *
 *	Poll the SATA phy and see whether it has come back from the dead
 *	yet.
 */
 
585 586 587 588 589 590
static int siimage_reset_poll (ide_drive_t *drive)
{
	if (SATA_STATUS_REG) {
		ide_hwif_t *hwif	= HWIF(drive);

		if ((hwif->INL(SATA_STATUS_REG) & 0x03) != 0x03) {
591
			printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
592
				hwif->name, hwif->INL(SATA_STATUS_REG));
Tejun Heo's avatar
Tejun Heo committed
593
			HWGROUP(drive)->polling = 0;
594 595 596 597 598 599 600 601
			return ide_started;
		}
		return 0;
	} else {
		return 0;
	}
}

602 603 604 605 606 607 608 609
/**
 *	siimage_pre_reset	-	reset hook
 *	@drive: IDE device being reset
 *
 *	For the SATA devices we need to handle recalibration/geometry
 *	differently
 */
 
610 611 612 613 614
static void siimage_pre_reset (ide_drive_t *drive)
{
	if (drive->media != ide_disk)
		return;

615 616
	if (is_sata(HWIF(drive)))
	{
617 618 619 620 621
		drive->special.b.set_geometry = 0;
		drive->special.b.recalibrate = 0;
	}
}

622 623 624 625 626 627 628 629
/**
 *	siimage_reset	-	reset a device on an siimage controller
 *	@drive: drive to reset
 *
 *	Perform a controller level reset fo the device. For
 *	SATA we must also check the PHY.
 */
 
630 631 632 633
static void siimage_reset (ide_drive_t *drive)
{
	ide_hwif_t *hwif	= HWIF(drive);
	u8 reset		= 0;
634
	unsigned long addr	= siimage_selreg(hwif, 0);
635 636

	if (hwif->mmio) {
637 638 639
		reset = hwif->INB(addr);
		hwif->OUTB((reset|0x03), addr);
		/* FIXME:posting */
640
		udelay(25);
641 642
		hwif->OUTB(reset, addr);
		(void) hwif->INB(addr);
643
	} else {
644 645
		pci_read_config_byte(hwif->pci_dev, addr, &reset);
		pci_write_config_byte(hwif->pci_dev, addr, reset|0x03);
646
		udelay(25);
647 648
		pci_write_config_byte(hwif->pci_dev, addr, reset);
		pci_read_config_byte(hwif->pci_dev, addr, &reset);
649 650 651 652
	}

	if (SATA_STATUS_REG) {
		u32 sata_stat = hwif->INL(SATA_STATUS_REG);
653
		printk(KERN_WARNING "%s: reset phy, status=0x%08x, %s\n",
654 655
			hwif->name, sata_stat, __FUNCTION__);
		if (!(sata_stat)) {
656
			printk(KERN_WARNING "%s: reset phy dead, status=0x%08x\n",
657 658 659 660 661 662 663
				hwif->name, sata_stat);
			drive->failures++;
		}
	}

}

664 665 666 667 668 669 670 671 672 673
/**
 *	proc_reports_siimage		-	add siimage controller to proc
 *	@dev: PCI device
 *	@clocking: SCSC value
 *	@name: controller name
 *
 *	Report the clocking mode of the controller and add it to
 *	the /proc interface layer
 */
 
674 675
static void proc_reports_siimage (struct pci_dev *dev, u8 clocking, const char *name)
{
676 677 678 679 680 681 682 683 684
	if (!pdev_is_sata(dev)) {
		printk(KERN_INFO "%s: BASE CLOCK ", name);
		clocking &= 0x03;
		switch (clocking) {
			case 0x03: printk("DISABLED!\n"); break;
			case 0x02: printk("== 2X PCI\n"); break;
			case 0x01: printk("== 133\n"); break;
			case 0x00: printk("== 100\n"); break;
		}
685 686 687
	}
}

688 689 690 691 692 693 694 695 696 697
/**
 *	setup_mmio_siimage	-	switch an SI controller into MMIO
 *	@dev: PCI device we are configuring
 *	@name: device name
 *
 *	Attempt to put the device into mmio mode. There are some slight
 *	complications here with certain systems where the mmio bar isnt
 *	mapped so we have to be sure we can fall back to I/O.
 */
 
698 699
static unsigned int setup_mmio_siimage (struct pci_dev *dev, const char *name)
{
700
	unsigned long bar5	= pci_resource_start(dev, 5);
701
	unsigned long barsize	= pci_resource_len(dev, 5);
702
	u8 tmpbyte	= 0;
703
	void __iomem *ioaddr;
704

705 706 707 708 709 710 711 712 713 714 715 716 717
	/*
	 *	Drop back to PIO if we can't map the mmio. Some
	 *	systems seem to get terminally confused in the PCI
	 *	spaces.
	 */
	 
	if(!request_mem_region(bar5, barsize, name))
	{
		printk(KERN_WARNING "siimage: IDE controller MMIO ports not available.\n");
		return 0;
	}
		
	ioaddr = ioremap(bar5, barsize);
718 719

	if (ioaddr == NULL)
720 721
	{
		release_mem_region(bar5, barsize);
722
		return 0;
723
	}
724 725

	pci_set_master(dev);
726
	pci_set_drvdata(dev, (void *) ioaddr);
727

728
	if (pdev_is_sata(dev)) {
729 730
		writel(0, ioaddr + 0x148);
		writel(0, ioaddr + 0x1C8);
731 732
	}

733 734 735
	writeb(0, ioaddr + 0xB4);
	writeb(0, ioaddr + 0xF4);
	tmpbyte = readb(ioaddr + 0x4A);
736

737 738 739
	switch(tmpbyte & 0x30) {
		case 0x00:
			/* In 100 MHz clocking, try and switch to 133 */
740
			writeb(tmpbyte|0x10, ioaddr + 0x4A);
741
			break;
742 743 744 745 746 747 748 749 750
		case 0x10:
			/* On 133Mhz clocking */
			break;
		case 0x20:
			/* On PCIx2 clocking */
			break;
		case 0x30:
			/* Clocking is disabled */
			/* 133 clock attempt to force it on */
751
			writeb(tmpbyte & ~0x20, ioaddr + 0x4A);
752 753 754
			break;
	}
	
755 756 757 758 759 760 761 762 763 764
	writeb(      0x72, ioaddr + 0xA1);
	writew(    0x328A, ioaddr + 0xA2);
	writel(0x62DD62DD, ioaddr + 0xA4);
	writel(0x43924392, ioaddr + 0xA8);
	writel(0x40094009, ioaddr + 0xAC);
	writeb(      0x72, ioaddr + 0xE1);
	writew(    0x328A, ioaddr + 0xE2);
	writel(0x62DD62DD, ioaddr + 0xE4);
	writel(0x43924392, ioaddr + 0xE8);
	writel(0x40094009, ioaddr + 0xEC);
765 766

	if (pdev_is_sata(dev)) {
767 768 769 770
		writel(0xFFFF0000, ioaddr + 0x108);
		writel(0xFFFF0000, ioaddr + 0x188);
		writel(0x00680000, ioaddr + 0x148);
		writel(0x00680000, ioaddr + 0x1C8);
771 772
	}

773
	tmpbyte = readb(ioaddr + 0x4A);
774

775
	proc_reports_siimage(dev, (tmpbyte>>4), name);
776 777 778
	return 1;
}

779 780 781 782 783 784 785 786 787
/**
 *	init_chipset_siimage	-	set up an SI device
 *	@dev: PCI device
 *	@name: device name
 *
 *	Perform the initial PCI set up for this device. Attempt to switch
 *	to 133MHz clocking if the system isn't already set up to do it.
 */

788
static unsigned int __devinit init_chipset_siimage(struct pci_dev *dev, const char *name)
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
{
	u32 class_rev	= 0;
	u8 tmpbyte	= 0;
	u8 BA5_EN	= 0;

        pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
        class_rev &= 0xff;
	pci_write_config_byte(dev, PCI_CACHE_LINE_SIZE, (class_rev) ? 1 : 255);	

	pci_read_config_byte(dev, 0x8A, &BA5_EN);
	if ((BA5_EN & 0x01) || (pci_resource_start(dev, 5))) {
		if (setup_mmio_siimage(dev, name)) {
			return 0;
		}
	}

	pci_write_config_byte(dev, 0x80, 0x00);
	pci_write_config_byte(dev, 0x84, 0x00);
	pci_read_config_byte(dev, 0x8A, &tmpbyte);
808
	switch(tmpbyte & 0x30) {
809 810 811 812 813 814 815 816
		case 0x00:
			/* 133 clock attempt to force it on */
			pci_write_config_byte(dev, 0x8A, tmpbyte|0x10);
		case 0x30:
			/* if clocking is disabled */
			/* 133 clock attempt to force it on */
			pci_write_config_byte(dev, 0x8A, tmpbyte & ~0x20);
		case 0x10:
817
			/* 133 already */
818
			break;
819 820
		case 0x20:
			/* BIOS set PCI x2 clocking */
821 822 823
			break;
	}

824 825 826 827
	pci_read_config_byte(dev,   0x8A, &tmpbyte);

	pci_write_config_byte(dev,  0xA1, 0x72);
	pci_write_config_word(dev,  0xA2, 0x328A);
828 829 830
	pci_write_config_dword(dev, 0xA4, 0x62DD62DD);
	pci_write_config_dword(dev, 0xA8, 0x43924392);
	pci_write_config_dword(dev, 0xAC, 0x40094009);
831 832
	pci_write_config_byte(dev,  0xB1, 0x72);
	pci_write_config_word(dev,  0xB2, 0x328A);
833 834 835 836
	pci_write_config_dword(dev, 0xB4, 0x62DD62DD);
	pci_write_config_dword(dev, 0xB8, 0x43924392);
	pci_write_config_dword(dev, 0xBC, 0x40094009);

837
	proc_reports_siimage(dev, (tmpbyte>>4), name);
838 839 840
	return 0;
}

841 842 843 844 845 846 847 848 849 850 851 852
/**
 *	init_mmio_iops_siimage	-	set up the iops for MMIO
 *	@hwif: interface to set up
 *
 *	The basic setup here is fairly simple, we can use standard MMIO
 *	operations. However we do have to set the taskfile register offsets
 *	by hand as there isnt a standard defined layout for them this
 *	time.
 *
 *	The hardware supports buffered taskfiles and also some rather nice
 *	extended PRD tables. Unfortunately right now we don't.
 */
853 854

static void __devinit init_mmio_iops_siimage(ide_hwif_t *hwif)
855 856
{
	struct pci_dev *dev	= hwif->pci_dev;
857
	void *addr		= pci_get_drvdata(dev);
858
	u8 ch			= hwif->channel;
859 860 861 862 863 864
	hw_regs_t		hw;
	unsigned long		base;

	/*
	 *	Fill in the basic HWIF bits
	 */
865

866
	default_hwif_mmiops(hwif);
867 868 869 870 871 872 873
	hwif->hwif_data			= addr;

	/*
	 *	Now set up the hw. We have to do this ourselves as
	 *	the MMIO layout isnt the same as the the standard port
	 *	based I/O
	 */
874

875 876
	memset(&hw, 0, sizeof(hw_regs_t));

877 878
	base = (unsigned long)addr;
	if (ch)
879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899
		base += 0xC0;
	else
		base += 0x80;

	/*
	 *	The buffered task file doesn't have status/control
	 *	so we can't currently use it sanely since we want to
	 *	use LBA48 mode.
	 */	
//	base += 0x10;
//	hwif->no_lba48 = 1;

	hw.io_ports[IDE_DATA_OFFSET]	= base;
	hw.io_ports[IDE_ERROR_OFFSET]	= base + 1;
	hw.io_ports[IDE_NSECTOR_OFFSET]	= base + 2;
	hw.io_ports[IDE_SECTOR_OFFSET]	= base + 3;
	hw.io_ports[IDE_LCYL_OFFSET]	= base + 4;
	hw.io_ports[IDE_HCYL_OFFSET]	= base + 5;
	hw.io_ports[IDE_SELECT_OFFSET]	= base + 6;
	hw.io_ports[IDE_STATUS_OFFSET]	= base + 7;
	hw.io_ports[IDE_CONTROL_OFFSET]	= base + 10;
900 901 902

	hw.io_ports[IDE_IRQ_OFFSET]	= 0;

903 904 905 906 907 908 909 910 911 912
	if (pdev_is_sata(dev)) {
		base = (unsigned long)addr;
		if (ch)
			base += 0x80;
		hwif->sata_scr[SATA_STATUS_OFFSET]	= base + 0x104;
		hwif->sata_scr[SATA_ERROR_OFFSET]	= base + 0x108;
		hwif->sata_scr[SATA_CONTROL_OFFSET]	= base + 0x100;
		hwif->sata_misc[SATA_MISC_OFFSET]	= base + 0x140;
		hwif->sata_misc[SATA_PHY_OFFSET]	= base + 0x144;
		hwif->sata_misc[SATA_IEN_OFFSET]	= base + 0x148;
913 914 915 916 917 918 919 920
	}

	hw.irq				= hwif->pci_dev->irq;

	memcpy(&hwif->hw, &hw, sizeof(hw));
	memcpy(hwif->io_ports, hwif->hw.io_ports, sizeof(hwif->hw.io_ports));

	hwif->irq			= hw.irq;
921 922

       	base = (unsigned long) addr;
923 924

#ifdef SIIMAGE_LARGE_DMA
925 926 927 928
/* Watch the brackets - even Ken and Dennis get some language design wrong */
	hwif->dma_base			= base + (ch ? 0x18 : 0x10);
	hwif->dma_base2			= base + (ch ? 0x08 : 0x00);
	hwif->dma_prdtable		= hwif->dma_base2 + 4;
929
#else /* ! SIIMAGE_LARGE_DMA */
930 931
	hwif->dma_base			= base + (ch ? 0x08 : 0x00);
	hwif->dma_base2			= base + (ch ? 0x18 : 0x10);
932
#endif /* SIIMAGE_LARGE_DMA */
933
	hwif->mmio			= 2;
934 935
}

936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
static int is_dev_seagate_sata(ide_drive_t *drive)
{
	const char *s = &drive->id->model[0];
	unsigned len;

	if (!drive->present)
		return 0;

	len = strnlen(s, sizeof(drive->id->model));

	if ((len > 4) && (!memcmp(s, "ST", 2))) {
		if ((!memcmp(s + len - 2, "AS", 2)) ||
		    (!memcmp(s + len - 3, "ASL", 3))) {
			printk(KERN_INFO "%s: applying pessimistic Seagate "
					 "errata fix\n", drive->name);
			return 1;
		}
	}
	return 0;
}

957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
/**
 *	siimage_fixup		-	post probe fixups
 *	@hwif: interface to fix up
 *
 *	Called after drive probe we use this to decide whether the
 *	Seagate fixup must be applied. This used to be in init_iops but
 *	that can occur before we know what drives are present.
 */

static void __devinit siimage_fixup(ide_hwif_t *hwif)
{
	/* Try and raise the rqsize */
	if (!is_sata(hwif) || !is_dev_seagate_sata(&hwif->drives[0]))
		hwif->rqsize = 128;
}

973 974 975 976 977 978 979 980 981
/**
 *	init_iops_siimage	-	set up iops
 *	@hwif: interface to set up
 *
 *	Do the basic setup for the SIIMAGE hardware interface
 *	and then do the MMIO setup if we can. This is the first
 *	look in we get for setting up the hwif so that we
 *	can get the iops right before using them.
 */
982 983

static void __devinit init_iops_siimage(ide_hwif_t *hwif)
984 985 986 987 988 989
{
	struct pci_dev *dev	= hwif->pci_dev;
	u32 class_rev		= 0;

	pci_read_config_dword(dev, PCI_CLASS_REVISION, &class_rev);
	class_rev &= 0xff;
990
	
991
	hwif->hwif_data = NULL;
992

993 994
	/* Pessimal until we finish probing */
	hwif->rqsize = 15;
995

996
	if (pci_get_drvdata(dev) == NULL)
997 998 999 1000
		return;
	init_mmio_iops_siimage(hwif);
}

1001 1002 1003 1004 1005 1006 1007
/**
 *	ata66_siimage	-	check for 80 pin cable
 *	@hwif: interface to check
 *
 *	Check for the presence of an ATA66 capable cable on the
 *	interface.
 */
1008 1009

static unsigned int __devinit ata66_siimage(ide_hwif_t *hwif)
1010
{
1011
	unsigned long addr = siimage_selreg(hwif, 0);
1012
	if (pci_get_drvdata(hwif->pci_dev) == NULL) {
1013
		u8 ata66 = 0;
1014
		pci_read_config_byte(hwif->pci_dev, addr, &ata66);
1015 1016 1017
		return (ata66 & 0x01) ? 1 : 0;
	}

1018
	return (hwif->INB(addr) & 0x01) ? 1 : 0;
1019 1020
}

1021 1022 1023 1024 1025 1026 1027 1028
/**
 *	init_hwif_siimage	-	set up hwif structs
 *	@hwif: interface to set up
 *
 *	We do the basic set up of the interface structure. The SIIMAGE
 *	requires several custom handlers so we override the default
 *	ide DMA handlers appropriately
 */
1029 1030

static void __devinit init_hwif_siimage(ide_hwif_t *hwif)
1031 1032
{
	hwif->autodma = 0;
1033
	
1034 1035 1036 1037 1038 1039
	hwif->resetproc = &siimage_reset;
	hwif->speedproc = &siimage_tune_chipset;
	hwif->tuneproc	= &siimage_tuneproc;
	hwif->reset_poll = &siimage_reset_poll;
	hwif->pre_reset = &siimage_pre_reset;

1040 1041 1042
	if(is_sata(hwif))
		hwif->busproc   = &siimage_busproc;

1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
	if (!hwif->dma_base) {
		hwif->drives[0].autotune = 1;
		hwif->drives[1].autotune = 1;
		return;
	}

	hwif->ultra_mask = 0x7f;
	hwif->mwdma_mask = 0x07;
	hwif->swdma_mask = 0x07;

1053
	if (!is_sata(hwif))
1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
		hwif->atapi_dma = 1;

	hwif->ide_dma_check = &siimage_config_drive_for_dma;
	if (!(hwif->udma_four))
		hwif->udma_four = ata66_siimage(hwif);

	if (hwif->mmio) {
		hwif->ide_dma_test_irq = &siimage_mmio_ide_dma_test_irq;
	} else {
		hwif->ide_dma_test_irq = & siimage_io_ide_dma_test_irq;
	}
1065 1066 1067 1068 1069 1070 1071
	
	/*
	 *	The BIOS often doesn't set up DMA on this controller
	 *	so we always do it.
	 */

	hwif->autodma = 1;
1072 1073 1074 1075
	hwif->drives[0].autodma = hwif->autodma;
	hwif->drives[1].autodma = hwif->autodma;
}

1076 1077 1078 1079 1080 1081
#define DECLARE_SII_DEV(name_str)			\
	{						\
		.name		= name_str,		\
		.init_chipset	= init_chipset_siimage,	\
		.init_iops	= init_iops_siimage,	\
		.init_hwif	= init_hwif_siimage,	\
1082
		.fixup		= siimage_fixup,	\
1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
		.channels	= 2,			\
		.autodma	= AUTODMA,		\
		.bootable	= ON_BOARD,		\
	}

static ide_pci_device_t siimage_chipsets[] __devinitdata = {
	/* 0 */ DECLARE_SII_DEV("SiI680"),
	/* 1 */ DECLARE_SII_DEV("SiI3112 Serial ATA"),
	/* 2 */ DECLARE_SII_DEV("Adaptec AAR-1210SA")
};

1094 1095 1096 1097 1098 1099 1100 1101 1102
/**
 *	siimage_init_one	-	pci layer discovery entry
 *	@dev: PCI device
 *	@id: ident table entry
 *
 *	Called by the PCI code when it finds an SI680 or SI3112 controller.
 *	We then use the IDE PCI generic helper to do most of the work.
 */
 
1103
static int __devinit siimage_init_one(struct pci_dev *dev, const struct pci_device_id *id)
1104
{
1105
	return ide_setup_pci_device(dev, &siimage_chipsets[id->driver_data]);
1106 1107
}

1108
static struct pci_device_id siimage_pci_tbl[] = {
1109
	{ PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_680,  PCI_ANY_ID, PCI_ANY_ID, 0, 0, 0},
1110
#ifdef CONFIG_BLK_DEV_IDE_SATA
1111
	{ PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_3112, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 1},
1112
	{ PCI_VENDOR_ID_CMD, PCI_DEVICE_ID_SII_1210SA, PCI_ANY_ID, PCI_ANY_ID, 0, 0, 2},
1113
#endif
1114 1115
	{ 0, },
};
1116
MODULE_DEVICE_TABLE(pci, siimage_pci_tbl);
1117

1118
static struct pci_driver driver = {
1119
	.name		= "SiI_IDE",
1120 1121
	.id_table	= siimage_pci_tbl,
	.probe		= siimage_init_one,
1122 1123 1124 1125 1126 1127 1128 1129 1130
};

static int siimage_ide_init(void)
{
	return ide_pci_register_driver(&driver);
}

module_init(siimage_ide_init);

1131
MODULE_AUTHOR("Andre Hedrick, Alan Cox");
1132 1133
MODULE_DESCRIPTION("PCI driver module for SiI IDE");
MODULE_LICENSE("GPL");