sata_sil.c 21.9 KB
Newer Older
Linus Torvalds's avatar
Linus Torvalds committed
1 2 3
/*
 *  sata_sil.c - Silicon Image SATA
 *
4
 *  Maintained by:  Tejun Heo <tj@kernel.org>
Linus Torvalds's avatar
Linus Torvalds committed
5 6 7
 *  		    Please ALWAYS copy linux-ide@vger.kernel.org
 *		    on emails.
 *
8
 *  Copyright 2003-2005 Red Hat, Inc.
Linus Torvalds's avatar
Linus Torvalds committed
9 10
 *  Copyright 2003 Benjamin Herrenschmidt
 *
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *  libata documentation is available via 'make {ps|pdf}docs',
 *  as Documentation/DocBook/libata.*
Linus Torvalds's avatar
Linus Torvalds committed
29
 *
30 31 32 33 34
 *  Documentation for SiI 3112:
 *  http://gkernel.sourceforge.net/specs/sii/3112A_SiI-DS-0095-B2.pdf.bz2
 *
 *  Other errata and documentation available under NDA.
 *
Linus Torvalds's avatar
Linus Torvalds committed
35 36 37 38 39 40 41 42 43
 */

#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/pci.h>
#include <linux/init.h>
#include <linux/blkdev.h>
#include <linux/delay.h>
#include <linux/interrupt.h>
44
#include <linux/device.h>
Linus Torvalds's avatar
Linus Torvalds committed
45 46
#include <scsi/scsi_host.h>
#include <linux/libata.h>
47
#include <linux/dmi.h>
Linus Torvalds's avatar
Linus Torvalds committed
48 49

#define DRV_NAME	"sata_sil"
50 51 52
#define DRV_VERSION	"2.4"

#define SIL_DMA_BOUNDARY	0x7fffffffUL
Linus Torvalds's avatar
Linus Torvalds committed
53 54

enum {
Tejun Heo's avatar
Tejun Heo committed
55 56
	SIL_MMIO_BAR		= 5,

57 58 59
	/*
	 * host flags
	 */
60
	SIL_FLAG_NO_SATA_IRQ	= (1 << 28),
61
	SIL_FLAG_RERR_ON_DMA_ACT = (1 << 29),
62
	SIL_FLAG_MOD15WRITE	= (1 << 30),
63

64
	SIL_DFL_PORT_FLAGS	= ATA_FLAG_SATA,
65

66 67 68
	/*
	 * Controller IDs
	 */
Linus Torvalds's avatar
Linus Torvalds committed
69
	sil_3112		= 0,
70 71 72
	sil_3112_no_sata_irq	= 1,
	sil_3512		= 2,
	sil_3114		= 3,
Linus Torvalds's avatar
Linus Torvalds committed
73

74 75 76
	/*
	 * Register offsets
	 */
Linus Torvalds's avatar
Linus Torvalds committed
77
	SIL_SYSCFG		= 0x48,
78 79 80 81 82

	/*
	 * Register bits
	 */
	/* SYSCFG */
Linus Torvalds's avatar
Linus Torvalds committed
83 84 85 86 87 88 89 90
	SIL_MASK_IDE0_INT	= (1 << 22),
	SIL_MASK_IDE1_INT	= (1 << 23),
	SIL_MASK_IDE2_INT	= (1 << 24),
	SIL_MASK_IDE3_INT	= (1 << 25),
	SIL_MASK_2PORT		= SIL_MASK_IDE0_INT | SIL_MASK_IDE1_INT,
	SIL_MASK_4PORT		= SIL_MASK_2PORT |
				  SIL_MASK_IDE2_INT | SIL_MASK_IDE3_INT,

91
	/* BMDMA/BMDMA2 */
Linus Torvalds's avatar
Linus Torvalds committed
92
	SIL_INTR_STEERING	= (1 << 1),
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107
	SIL_DMA_ENABLE		= (1 << 0),  /* DMA run switch */
	SIL_DMA_RDWR		= (1 << 3),  /* DMA Rd-Wr */
	SIL_DMA_SATA_IRQ	= (1 << 4),  /* OR of all SATA IRQs */
	SIL_DMA_ACTIVE		= (1 << 16), /* DMA running */
	SIL_DMA_ERROR		= (1 << 17), /* PCI bus error */
	SIL_DMA_COMPLETE	= (1 << 18), /* cmd complete / IRQ pending */
	SIL_DMA_N_SATA_IRQ	= (1 << 6),  /* SATA_IRQ for the next channel */
	SIL_DMA_N_ACTIVE	= (1 << 24), /* ACTIVE for the next channel */
	SIL_DMA_N_ERROR		= (1 << 25), /* ERROR for the next channel */
	SIL_DMA_N_COMPLETE	= (1 << 26), /* COMPLETE for the next channel */

	/* SIEN */
	SIL_SIEN_N		= (1 << 16), /* triggered by SError.N */

108 109 110
	/*
	 * Others
	 */
Linus Torvalds's avatar
Linus Torvalds committed
111 112 113 114
	SIL_QUIRK_MOD15WRITE	= (1 << 0),
	SIL_QUIRK_UDMA5MAX	= (1 << 1),
};

115
static int sil_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
116
#ifdef CONFIG_PM
117
static int sil_pci_device_resume(struct pci_dev *pdev);
118
#endif
119
static void sil_dev_config(struct ata_device *dev);
120 121
static int sil_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
static int sil_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
122
static int sil_set_mode(struct ata_link *link, struct ata_device **r_failed);
123 124 125 126
static void sil_qc_prep(struct ata_queued_cmd *qc);
static void sil_bmdma_setup(struct ata_queued_cmd *qc);
static void sil_bmdma_start(struct ata_queued_cmd *qc);
static void sil_bmdma_stop(struct ata_queued_cmd *qc);
127 128
static void sil_freeze(struct ata_port *ap);
static void sil_thaw(struct ata_port *ap);
Linus Torvalds's avatar
Linus Torvalds committed
129

130

131
static const struct pci_device_id sil_pci_tbl[] = {
132 133 134 135 136 137 138 139
	{ PCI_VDEVICE(CMD, 0x3112), sil_3112 },
	{ PCI_VDEVICE(CMD, 0x0240), sil_3112 },
	{ PCI_VDEVICE(CMD, 0x3512), sil_3512 },
	{ PCI_VDEVICE(CMD, 0x3114), sil_3114 },
	{ PCI_VDEVICE(ATI, 0x436e), sil_3112 },
	{ PCI_VDEVICE(ATI, 0x4379), sil_3112_no_sata_irq },
	{ PCI_VDEVICE(ATI, 0x437a), sil_3112_no_sata_irq },

Linus Torvalds's avatar
Linus Torvalds committed
140 141 142 143 144 145
	{ }	/* terminate list */
};


/* TODO firmware versions should be added - eric */
static const struct sil_drivelist {
146
	const char *product;
Linus Torvalds's avatar
Linus Torvalds committed
147 148 149 150 151 152 153 154 155 156 157 158 159
	unsigned int quirk;
} sil_blacklist [] = {
	{ "ST320012AS",		SIL_QUIRK_MOD15WRITE },
	{ "ST330013AS",		SIL_QUIRK_MOD15WRITE },
	{ "ST340017AS",		SIL_QUIRK_MOD15WRITE },
	{ "ST360015AS",		SIL_QUIRK_MOD15WRITE },
	{ "ST380023AS",		SIL_QUIRK_MOD15WRITE },
	{ "ST3120023AS",	SIL_QUIRK_MOD15WRITE },
	{ "ST340014ASL",	SIL_QUIRK_MOD15WRITE },
	{ "ST360014ASL",	SIL_QUIRK_MOD15WRITE },
	{ "ST380011ASL",	SIL_QUIRK_MOD15WRITE },
	{ "ST3120022ASL",	SIL_QUIRK_MOD15WRITE },
	{ "ST3160021ASL",	SIL_QUIRK_MOD15WRITE },
160
	{ "TOSHIBA MK2561GSYN",	SIL_QUIRK_MOD15WRITE },
Linus Torvalds's avatar
Linus Torvalds committed
161 162 163 164 165 166 167 168 169
	{ "Maxtor 4D060H3",	SIL_QUIRK_UDMA5MAX },
	{ }
};

static struct pci_driver sil_pci_driver = {
	.name			= DRV_NAME,
	.id_table		= sil_pci_tbl,
	.probe			= sil_init_one,
	.remove			= ata_pci_remove_one,
170
#ifdef CONFIG_PM
171 172
	.suspend		= ata_pci_device_suspend,
	.resume			= sil_pci_device_resume,
173
#endif
Linus Torvalds's avatar
Linus Torvalds committed
174 175
};

176
static struct scsi_host_template sil_sht = {
177 178 179 180 181 182
	ATA_BASE_SHT(DRV_NAME),
	/** These controllers support Large Block Transfer which allows
	    transfer chunks up to 2GB and which cross 64KB boundaries,
	    therefore the DMA limits are more relaxed than standard ATA SFF. */
	.dma_boundary		= SIL_DMA_BOUNDARY,
	.sg_tablesize		= ATA_MAX_PRD
Linus Torvalds's avatar
Linus Torvalds committed
183 184
};

185
static struct ata_port_operations sil_ops = {
186
	.inherits		= &ata_bmdma32_port_ops,
Linus Torvalds's avatar
Linus Torvalds committed
187
	.dev_config		= sil_dev_config,
188
	.set_mode		= sil_set_mode,
189 190 191 192
	.bmdma_setup            = sil_bmdma_setup,
	.bmdma_start            = sil_bmdma_start,
	.bmdma_stop		= sil_bmdma_stop,
	.qc_prep		= sil_qc_prep,
193 194
	.freeze			= sil_freeze,
	.thaw			= sil_thaw,
Linus Torvalds's avatar
Linus Torvalds committed
195 196 197 198
	.scr_read		= sil_scr_read,
	.scr_write		= sil_scr_write,
};

199
static const struct ata_port_info sil_port_info[] = {
Linus Torvalds's avatar
Linus Torvalds committed
200
	/* sil_3112 */
201
	{
Jeff Garzik's avatar
Jeff Garzik committed
202
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_MOD15WRITE,
203 204
		.pio_mask	= ATA_PIO4,
		.mwdma_mask	= ATA_MWDMA2,
205
		.udma_mask	= ATA_UDMA5,
206
		.port_ops	= &sil_ops,
207
	},
208 209
	/* sil_3112_no_sata_irq */
	{
Jeff Garzik's avatar
Jeff Garzik committed
210
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_MOD15WRITE |
211
				  SIL_FLAG_NO_SATA_IRQ,
212 213
		.pio_mask	= ATA_PIO4,
		.mwdma_mask	= ATA_MWDMA2,
214
		.udma_mask	= ATA_UDMA5,
215 216
		.port_ops	= &sil_ops,
	},
217
	/* sil_3512 */
Linus Torvalds's avatar
Linus Torvalds committed
218
	{
Jeff Garzik's avatar
Jeff Garzik committed
219
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_RERR_ON_DMA_ACT,
220 221
		.pio_mask	= ATA_PIO4,
		.mwdma_mask	= ATA_MWDMA2,
222
		.udma_mask	= ATA_UDMA5,
223 224 225
		.port_ops	= &sil_ops,
	},
	/* sil_3114 */
Linus Torvalds's avatar
Linus Torvalds committed
226
	{
Jeff Garzik's avatar
Jeff Garzik committed
227
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_RERR_ON_DMA_ACT,
228 229
		.pio_mask	= ATA_PIO4,
		.mwdma_mask	= ATA_MWDMA2,
230
		.udma_mask	= ATA_UDMA5,
Linus Torvalds's avatar
Linus Torvalds committed
231 232 233 234 235 236 237 238 239 240
		.port_ops	= &sil_ops,
	},
};

/* per-port register offsets */
/* TODO: we can probably calculate rather than use a table */
static const struct {
	unsigned long tf;	/* ATA taskfile register block */
	unsigned long ctl;	/* ATA control/altstatus register block */
	unsigned long bmdma;	/* DMA register block */
241
	unsigned long bmdma2;	/* DMA register block #2 */
242
	unsigned long fifo_cfg;	/* FIFO Valid Byte Count and Control */
Linus Torvalds's avatar
Linus Torvalds committed
243 244 245
	unsigned long scr;	/* SATA control register block */
	unsigned long sien;	/* SATA Interrupt Enable register */
	unsigned long xfer_mode;/* data transfer mode register */
246
	unsigned long sfis_cfg;	/* SATA FIS reception config register */
Linus Torvalds's avatar
Linus Torvalds committed
247 248
} sil_port[] = {
	/* port 0 ... */
249 250 251
	/*   tf    ctl  bmdma  bmdma2  fifo    scr   sien   mode   sfis */
	{  0x80,  0x8A,   0x0,  0x10,  0x40, 0x100, 0x148,  0xb4, 0x14c },
	{  0xC0,  0xCA,   0x8,  0x18,  0x44, 0x180, 0x1c8,  0xf4, 0x1cc },
252 253
	{ 0x280, 0x28A, 0x200, 0x210, 0x240, 0x300, 0x348, 0x2b4, 0x34c },
	{ 0x2C0, 0x2CA, 0x208, 0x218, 0x244, 0x380, 0x3c8, 0x2f4, 0x3cc },
Linus Torvalds's avatar
Linus Torvalds committed
254 255 256 257 258 259 260 261 262
	/* ... port 3 */
};

MODULE_AUTHOR("Jeff Garzik");
MODULE_DESCRIPTION("low-level driver for Silicon Image SATA controller");
MODULE_LICENSE("GPL");
MODULE_DEVICE_TABLE(pci, sil_pci_tbl);
MODULE_VERSION(DRV_VERSION);

263
static int slow_down;
264 265 266
module_param(slow_down, int, 0444);
MODULE_PARM_DESC(slow_down, "Sledgehammer used to work around random problems, by limiting commands to 15 sectors (0=off, 1=on)");

267

268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
static void sil_bmdma_stop(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
	void __iomem *bmdma2 = mmio_base + sil_port[ap->port_no].bmdma2;

	/* clear start/stop bit - can safely always write 0 */
	iowrite8(0, bmdma2);

	/* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
	ata_sff_dma_pause(ap);
}

static void sil_bmdma_setup(struct ata_queued_cmd *qc)
{
	struct ata_port *ap = qc->ap;
	void __iomem *bmdma = ap->ioaddr.bmdma_addr;

	/* load PRD table addr. */
287
	iowrite32(ap->bmdma_prd_dma, bmdma + ATA_DMA_TABLE_OFS);
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

	/* issue r/w command */
	ap->ops->sff_exec_command(ap, &qc->tf);
}

static void sil_bmdma_start(struct ata_queued_cmd *qc)
{
	unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
	struct ata_port *ap = qc->ap;
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
	void __iomem *bmdma2 = mmio_base + sil_port[ap->port_no].bmdma2;
	u8 dmactl = ATA_DMA_START;

	/* set transfer direction, start host DMA transaction
	   Note: For Large Block Transfer to work, the DMA must be started
	   using the bmdma2 register. */
	if (!rw)
		dmactl |= ATA_DMA_WR;
	iowrite8(dmactl, bmdma2);
}

/* The way God intended PCI IDE scatter/gather lists to look and behave... */
static void sil_fill_sg(struct ata_queued_cmd *qc)
{
	struct scatterlist *sg;
	struct ata_port *ap = qc->ap;
314
	struct ata_bmdma_prd *prd, *last_prd = NULL;
315 316
	unsigned int si;

317
	prd = &ap->bmdma_prd[0];
318 319 320 321 322 323 324 325 326
	for_each_sg(qc->sg, sg, qc->n_elem, si) {
		/* Note h/w doesn't support 64-bit, so we unconditionally
		 * truncate dma_addr_t to u32.
		 */
		u32 addr = (u32) sg_dma_address(sg);
		u32 sg_len = sg_dma_len(sg);

		prd->addr = cpu_to_le32(addr);
		prd->flags_len = cpu_to_le32(sg_len);
327
		VPRINTK("PRD[%u] = (0x%X, 0x%X)\n", si, addr, sg_len);
328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344

		last_prd = prd;
		prd++;
	}

	if (likely(last_prd))
		last_prd->flags_len |= cpu_to_le32(ATA_PRD_EOT);
}

static void sil_qc_prep(struct ata_queued_cmd *qc)
{
	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
		return;

	sil_fill_sg(qc);
}

Linus Torvalds's avatar
Linus Torvalds committed
345 346 347 348 349 350 351
static unsigned char sil_get_device_cache_line(struct pci_dev *pdev)
{
	u8 cache_line = 0;
	pci_read_config_byte(pdev, PCI_CACHE_LINE_SIZE, &cache_line);
	return cache_line;
}

352 353
/**
 *	sil_set_mode		-	wrap set_mode functions
354
 *	@link: link to set up
355 356 357 358 359 360
 *	@r_failed: returned device when we fail
 *
 *	Wrap the libata method for device setup as after the setup we need
 *	to inspect the results and do some configuration work
 */

361
static int sil_set_mode(struct ata_link *link, struct ata_device **r_failed)
Linus Torvalds's avatar
Linus Torvalds committed
362
{
363 364
	struct ata_port *ap = link->ap;
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
Tejun Heo's avatar
Tejun Heo committed
365
	void __iomem *addr = mmio_base + sil_port[ap->port_no].xfer_mode;
366
	struct ata_device *dev;
367
	u32 tmp, dev_mode[2] = { };
368
	int rc;
369

370
	rc = ata_do_set_mode(link, r_failed);
371 372
	if (rc)
		return rc;
Linus Torvalds's avatar
Linus Torvalds committed
373

Tejun Heo's avatar
Tejun Heo committed
374
	ata_for_each_dev(dev, link, ALL) {
375
		if (!ata_dev_enabled(dev))
376
			dev_mode[dev->devno] = 0;	/* PIO0/1/2 */
Linus Torvalds's avatar
Linus Torvalds committed
377
		else if (dev->flags & ATA_DFLAG_PIO)
378
			dev_mode[dev->devno] = 1;	/* PIO3/4 */
Linus Torvalds's avatar
Linus Torvalds committed
379
		else
380
			dev_mode[dev->devno] = 3;	/* UDMA */
Linus Torvalds's avatar
Linus Torvalds committed
381 382 383 384 385 386 387 388 389
		/* value 2 indicates MDMA */
	}

	tmp = readl(addr);
	tmp &= ~((1<<5) | (1<<4) | (1<<1) | (1<<0));
	tmp |= dev_mode[0];
	tmp |= (dev_mode[1] << 4);
	writel(tmp, addr);
	readl(addr);	/* flush */
390
	return 0;
Linus Torvalds's avatar
Linus Torvalds committed
391 392
}

393 394
static inline void __iomem *sil_scr_addr(struct ata_port *ap,
					 unsigned int sc_reg)
Linus Torvalds's avatar
Linus Torvalds committed
395
{
Tejun Heo's avatar
Tejun Heo committed
396
	void __iomem *offset = ap->ioaddr.scr_addr;
Linus Torvalds's avatar
Linus Torvalds committed
397 398 399 400 401 402 403 404 405 406 407 408 409

	switch (sc_reg) {
	case SCR_STATUS:
		return offset + 4;
	case SCR_ERROR:
		return offset + 8;
	case SCR_CONTROL:
		return offset;
	default:
		/* do nothing */
		break;
	}

Randy Dunlap's avatar
Randy Dunlap committed
410
	return NULL;
Linus Torvalds's avatar
Linus Torvalds committed
411 412
}

413
static int sil_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
Linus Torvalds's avatar
Linus Torvalds committed
414
{
415
	void __iomem *mmio = sil_scr_addr(link->ap, sc_reg);
416 417 418 419 420 421

	if (mmio) {
		*val = readl(mmio);
		return 0;
	}
	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
422 423
}

424
static int sil_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
Linus Torvalds's avatar
Linus Torvalds committed
425
{
426
	void __iomem *mmio = sil_scr_addr(link->ap, sc_reg);
427 428

	if (mmio) {
Linus Torvalds's avatar
Linus Torvalds committed
429
		writel(val, mmio);
430 431 432
		return 0;
	}
	return -EINVAL;
Linus Torvalds's avatar
Linus Torvalds committed
433 434
}

435 436
static void sil_host_intr(struct ata_port *ap, u32 bmdma2)
{
437 438
	struct ata_eh_info *ehi = &ap->link.eh_info;
	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
439 440
	u8 status;

441
	if (unlikely(bmdma2 & SIL_DMA_SATA_IRQ)) {
442
		u32 serror = 0xffffffff;
443 444 445 446 447

		/* SIEN doesn't mask SATA IRQs on some 3112s.  Those
		 * controllers continue to assert IRQ as long as
		 * SError bits are pending.  Clear SError immediately.
		 */
448 449
		sil_scr_read(&ap->link, SCR_ERROR, &serror);
		sil_scr_write(&ap->link, SCR_ERROR, serror);
450

451 452
		/* Sometimes spurious interrupts occur, double check
		 * it's PHYRDY CHG.
453
		 */
454
		if (serror & SERR_PHYRDY_CHG) {
455
			ap->link.eh_info.serror |= serror;
456
			goto freeze;
457 458
		}

459 460
		if (!(bmdma2 & SIL_DMA_COMPLETE))
			return;
461 462
	}

463
	if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
464
		/* this sometimes happens, just clear IRQ */
Tejun Heo's avatar
Tejun Heo committed
465
		ap->ops->sff_check_status(ap);
466 467 468
		return;
	}

469 470 471 472 473 474 475 476
	/* Check whether we are expecting interrupt in this state */
	switch (ap->hsm_task_state) {
	case HSM_ST_FIRST:
		/* Some pre-ATAPI-4 devices assert INTRQ
		 * at this state when ready to receive CDB.
		 */

		/* Check the ATA_DFLAG_CDB_INTR flag is enough here.
477 478
		 * The flag was turned on only for atapi devices.  No
		 * need to check ata_is_atapi(qc->tf.protocol) again.
479 480 481 482 483
		 */
		if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
			goto err_hsm;
		break;
	case HSM_ST_LAST:
484
		if (ata_is_dma(qc->tf.protocol)) {
485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
			/* clear DMA-Start bit */
			ap->ops->bmdma_stop(qc);

			if (bmdma2 & SIL_DMA_ERROR) {
				qc->err_mask |= AC_ERR_HOST_BUS;
				ap->hsm_task_state = HSM_ST_ERR;
			}
		}
		break;
	case HSM_ST:
		break;
	default:
		goto err_hsm;
	}

	/* check main status, clearing INTRQ */
Tejun Heo's avatar
Tejun Heo committed
501
	status = ap->ops->sff_check_status(ap);
502 503 504 505
	if (unlikely(status & ATA_BUSY))
		goto err_hsm;

	/* ack bmdma irq events */
506
	ata_bmdma_irq_clear(ap);
507 508

	/* kick HSM in the ass */
Tejun Heo's avatar
Tejun Heo committed
509
	ata_sff_hsm_move(ap, qc, status, 0);
510

511
	if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
512 513
		ata_ehi_push_desc(ehi, "BMDMA2 stat 0x%x", bmdma2);

514 515 516 517 518 519 520 521
	return;

 err_hsm:
	qc->err_mask |= AC_ERR_HSM;
 freeze:
	ata_port_freeze(ap);
}

522
static irqreturn_t sil_interrupt(int irq, void *dev_instance)
523
{
Jeff Garzik's avatar
Jeff Garzik committed
524
	struct ata_host *host = dev_instance;
Tejun Heo's avatar
Tejun Heo committed
525
	void __iomem *mmio_base = host->iomap[SIL_MMIO_BAR];
526 527 528
	int handled = 0;
	int i;

Jeff Garzik's avatar
Jeff Garzik committed
529
	spin_lock(&host->lock);
530

Jeff Garzik's avatar
Jeff Garzik committed
531 532
	for (i = 0; i < host->n_ports; i++) {
		struct ata_port *ap = host->ports[i];
533 534
		u32 bmdma2 = readl(mmio_base + sil_port[ap->port_no].bmdma2);

535 536 537 538
		/* turn off SATA_IRQ if not supported */
		if (ap->flags & SIL_FLAG_NO_SATA_IRQ)
			bmdma2 &= ~SIL_DMA_SATA_IRQ;

539 540
		if (bmdma2 == 0xffffffff ||
		    !(bmdma2 & (SIL_DMA_COMPLETE | SIL_DMA_SATA_IRQ)))
541 542 543 544 545 546
			continue;

		sil_host_intr(ap, bmdma2);
		handled = 1;
	}

Jeff Garzik's avatar
Jeff Garzik committed
547
	spin_unlock(&host->lock);
548 549 550 551

	return IRQ_RETVAL(handled);
}

552 553
static void sil_freeze(struct ata_port *ap)
{
Tejun Heo's avatar
Tejun Heo committed
554
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
555 556
	u32 tmp;

557 558 559
	/* global IRQ mask doesn't block SATA IRQ, turn off explicitly */
	writel(0, mmio_base + sil_port[ap->port_no].sien);

560 561 562 563 564
	/* plug IRQ */
	tmp = readl(mmio_base + SIL_SYSCFG);
	tmp |= SIL_MASK_IDE0_INT << ap->port_no;
	writel(tmp, mmio_base + SIL_SYSCFG);
	readl(mmio_base + SIL_SYSCFG);	/* flush */
565 566 567 568 569 570 571 572 573 574 575 576 577

	/* Ensure DMA_ENABLE is off.
	 *
	 * This is because the controller will not give us access to the
	 * taskfile registers while a DMA is in progress
	 */
	iowrite8(ioread8(ap->ioaddr.bmdma_addr) & ~SIL_DMA_ENABLE,
		 ap->ioaddr.bmdma_addr);

	/* According to ata_bmdma_stop, an HDMA transition requires
	 * on PIO cycle. But we can't read a taskfile register.
	 */
	ioread8(ap->ioaddr.bmdma_addr);
578 579 580 581
}

static void sil_thaw(struct ata_port *ap)
{
Tejun Heo's avatar
Tejun Heo committed
582
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
583 584 585
	u32 tmp;

	/* clear IRQ */
Tejun Heo's avatar
Tejun Heo committed
586
	ap->ops->sff_check_status(ap);
587
	ata_bmdma_irq_clear(ap);
588

589 590 591
	/* turn on SATA IRQ if supported */
	if (!(ap->flags & SIL_FLAG_NO_SATA_IRQ))
		writel(SIL_SIEN_N, mmio_base + sil_port[ap->port_no].sien);
592

593 594 595 596 597 598
	/* turn on IRQ */
	tmp = readl(mmio_base + SIL_SYSCFG);
	tmp &= ~(SIL_MASK_IDE0_INT << ap->port_no);
	writel(tmp, mmio_base + SIL_SYSCFG);
}

Linus Torvalds's avatar
Linus Torvalds committed
599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626
/**
 *	sil_dev_config - Apply device/host-specific errata fixups
 *	@dev: Device to be examined
 *
 *	After the IDENTIFY [PACKET] DEVICE step is complete, and a
 *	device is known to be present, this function is called.
 *	We apply two errata fixups which are specific to Silicon Image,
 *	a Seagate and a Maxtor fixup.
 *
 *	For certain Seagate devices, we must limit the maximum sectors
 *	to under 8K.
 *
 *	For certain Maxtor devices, we must not program the drive
 *	beyond udma5.
 *
 *	Both fixups are unfairly pessimistic.  As soon as I get more
 *	information on these errata, I will create a more exhaustive
 *	list, and apply the fixups to only the specific
 *	devices/hosts/firmwares that need it.
 *
 *	20040111 - Seagate drives affected by the Mod15Write bug are blacklisted
 *	The Maxtor quirk is in the blacklist, but I'm keeping the original
 *	pessimistic fix for the following reasons...
 *	- There seems to be less info on it, only one device gleaned off the
 *	Windows	driver, maybe only one is affected.  More info would be greatly
 *	appreciated.
 *	- But then again UDMA5 is hardly anything to complain about
 */
627
static void sil_dev_config(struct ata_device *dev)
Linus Torvalds's avatar
Linus Torvalds committed
628
{
629 630
	struct ata_port *ap = dev->link->ap;
	int print_info = ap->link.eh_context.i.flags & ATA_EHI_PRINTINFO;
Linus Torvalds's avatar
Linus Torvalds committed
631
	unsigned int n, quirks = 0;
632
	unsigned char model_num[ATA_ID_PROD_LEN + 1];
Linus Torvalds's avatar
Linus Torvalds committed
633

634
	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
Linus Torvalds's avatar
Linus Torvalds committed
635

636
	for (n = 0; sil_blacklist[n].product; n++)
637
		if (!strcmp(sil_blacklist[n].product, model_num)) {
Linus Torvalds's avatar
Linus Torvalds committed
638 639 640
			quirks = sil_blacklist[n].quirk;
			break;
		}
641

Linus Torvalds's avatar
Linus Torvalds committed
642
	/* limit requests to 15 sectors */
643 644 645
	if (slow_down ||
	    ((ap->flags & SIL_FLAG_MOD15WRITE) &&
	     (quirks & SIL_QUIRK_MOD15WRITE))) {
646
		if (print_info)
647 648
			ata_dev_info(dev,
		"applying Seagate errata fix (mod15write workaround)\n");
649
		dev->max_sectors = 15;
Linus Torvalds's avatar
Linus Torvalds committed
650 651 652 653 654
		return;
	}

	/* limit to udma5 */
	if (quirks & SIL_QUIRK_UDMA5MAX) {
655
		if (print_info)
656 657
			ata_dev_info(dev, "applying Maxtor errata fix %s\n",
				     model_num);
658
		dev->udma_mask &= ATA_UDMA5;
Linus Torvalds's avatar
Linus Torvalds committed
659 660 661 662
		return;
	}
}

663
static void sil_init_controller(struct ata_host *host)
664
{
665 666
	struct pci_dev *pdev = to_pci_dev(host->dev);
	void __iomem *mmio_base = host->iomap[SIL_MMIO_BAR];
667 668 669 670 671 672 673 674 675
	u8 cls;
	u32 tmp;
	int i;

	/* Initialize FIFO PCI bus arbitration */
	cls = sil_get_device_cache_line(pdev);
	if (cls) {
		cls >>= 3;
		cls++;  /* cls = (line_size/8)+1 */
676
		for (i = 0; i < host->n_ports; i++)
677 678 679
			writew(cls << 8 | cls,
			       mmio_base + sil_port[i].fifo_cfg);
	} else
680 681
		dev_warn(&pdev->dev,
			 "cache line size not set.  Driver may not function\n");
682 683

	/* Apply R_ERR on DMA activate FIS errata workaround */
684
	if (host->ports[0]->flags & SIL_FLAG_RERR_ON_DMA_ACT) {
685 686
		int cnt;

687
		for (i = 0, cnt = 0; i < host->n_ports; i++) {
688 689 690 691
			tmp = readl(mmio_base + sil_port[i].sfis_cfg);
			if ((tmp & 0x3) != 0x01)
				continue;
			if (!cnt)
692 693
				dev_info(&pdev->dev,
					 "Applying R_ERR on DMA activate FIS errata fix\n");
694 695 696 697 698
			writel(tmp & ~0x3, mmio_base + sil_port[i].sfis_cfg);
			cnt++;
		}
	}

699
	if (host->n_ports == 4) {
700 701 702 703 704 705 706 707
		/* flip the magic "make 4 ports work" bit */
		tmp = readl(mmio_base + sil_port[2].bmdma);
		if ((tmp & SIL_INTR_STEERING) == 0)
			writel(tmp | SIL_INTR_STEERING,
			       mmio_base + sil_port[2].bmdma);
	}
}

708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733
static bool sil_broken_system_poweroff(struct pci_dev *pdev)
{
	static const struct dmi_system_id broken_systems[] = {
		{
			.ident = "HP Compaq nx6325",
			.matches = {
				DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
				DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6325"),
			},
			/* PCI slot number of the controller */
			.driver_data = (void *)0x12UL,
		},

		{ }	/* terminate list */
	};
	const struct dmi_system_id *dmi = dmi_first_match(broken_systems);

	if (dmi) {
		unsigned long slot = (unsigned long)dmi->driver_data;
		/* apply the quirk only to on-board controllers */
		return slot == PCI_SLOT(pdev->devfn);
	}

	return false;
}

734
static int sil_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds's avatar
Linus Torvalds committed
735
{
736
	int board_id = ent->driver_data;
737 738
	struct ata_port_info pi = sil_port_info[board_id];
	const struct ata_port_info *ppi[] = { &pi, NULL };
739
	struct ata_host *host;
740
	void __iomem *mmio_base;
741
	int n_ports, rc;
Linus Torvalds's avatar
Linus Torvalds committed
742 743
	unsigned int i;

744
	ata_print_version_once(&pdev->dev, DRV_VERSION);
Linus Torvalds's avatar
Linus Torvalds committed
745

746 747 748 749 750
	/* allocate host */
	n_ports = 2;
	if (board_id == sil_3114)
		n_ports = 4;

751 752 753 754 755 756 757
	if (sil_broken_system_poweroff(pdev)) {
		pi.flags |= ATA_FLAG_NO_POWEROFF_SPINDOWN |
					ATA_FLAG_NO_HIBERNATE_SPINDOWN;
		dev_info(&pdev->dev, "quirky BIOS, skipping spindown "
				"on poweroff and hibernation\n");
	}

758 759 760 761 762
	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
	if (!host)
		return -ENOMEM;

	/* acquire resources and fill host */
763
	rc = pcim_enable_device(pdev);
Linus Torvalds's avatar
Linus Torvalds committed
764 765 766
	if (rc)
		return rc;

Tejun Heo's avatar
Tejun Heo committed
767 768
	rc = pcim_iomap_regions(pdev, 1 << SIL_MMIO_BAR, DRV_NAME);
	if (rc == -EBUSY)
769
		pcim_pin_device(pdev);
Tejun Heo's avatar
Tejun Heo committed
770
	if (rc)
771
		return rc;
772
	host->iomap = pcim_iomap_table(pdev);
Linus Torvalds's avatar
Linus Torvalds committed
773 774 775

	rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
	if (rc)
776
		return rc;
Linus Torvalds's avatar
Linus Torvalds committed
777 778
	rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
	if (rc)
779
		return rc;
Linus Torvalds's avatar
Linus Torvalds committed
780

781
	mmio_base = host->iomap[SIL_MMIO_BAR];
Linus Torvalds's avatar
Linus Torvalds committed
782

783
	for (i = 0; i < host->n_ports; i++) {
784 785
		struct ata_port *ap = host->ports[i];
		struct ata_ioports *ioaddr = &ap->ioaddr;
786 787 788 789 790 791

		ioaddr->cmd_addr = mmio_base + sil_port[i].tf;
		ioaddr->altstatus_addr =
		ioaddr->ctl_addr = mmio_base + sil_port[i].ctl;
		ioaddr->bmdma_addr = mmio_base + sil_port[i].bmdma;
		ioaddr->scr_addr = mmio_base + sil_port[i].scr;
Tejun Heo's avatar
Tejun Heo committed
792
		ata_sff_std_ports(ioaddr);
793 794 795

		ata_port_pbar_desc(ap, SIL_MMIO_BAR, -1, "mmio");
		ata_port_pbar_desc(ap, SIL_MMIO_BAR, sil_port[i].tf, "tf");
Linus Torvalds's avatar
Linus Torvalds committed
796 797
	}

798 799
	/* initialize and activate */
	sil_init_controller(host);
Linus Torvalds's avatar
Linus Torvalds committed
800 801

	pci_set_master(pdev);
802 803
	return ata_host_activate(host, pdev->irq, sil_interrupt, IRQF_SHARED,
				 &sil_sht);
Linus Torvalds's avatar
Linus Torvalds committed
804 805
}

806
#ifdef CONFIG_PM
807 808
static int sil_pci_device_resume(struct pci_dev *pdev)
{
Jeff Garzik's avatar
Jeff Garzik committed
809
	struct ata_host *host = dev_get_drvdata(&pdev->dev);
810 811 812 813 814
	int rc;

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

816
	sil_init_controller(host);
Jeff Garzik's avatar
Jeff Garzik committed
817
	ata_host_resume(host);
818 819 820

	return 0;
}
821
#endif
822

Axel Lin's avatar
Axel Lin committed
823
module_pci_driver(sil_pci_driver);