sata_sil.c 18.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7
/*
 *  sata_sil.c - Silicon Image SATA
 *
 *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
 *  		    Please ALWAYS copy linux-ide@vger.kernel.org
 *		    on emails.
 *
8
 *  Copyright 2003-2005 Red Hat, Inc.
L
Linus Torvalds 已提交
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.*
L
Linus Torvalds 已提交
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.
 *
L
Linus Torvalds 已提交
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>
L
Linus Torvalds 已提交
45 46 47 48
#include <scsi/scsi_host.h>
#include <linux/libata.h>

#define DRV_NAME	"sata_sil"
J
Jeff Garzik 已提交
49
#define DRV_VERSION	"2.3"
L
Linus Torvalds 已提交
50 51

enum {
T
Tejun Heo 已提交
52 53
	SIL_MMIO_BAR		= 5,

54 55 56
	/*
	 * host flags
	 */
57
	SIL_FLAG_NO_SATA_IRQ	= (1 << 28),
58
	SIL_FLAG_RERR_ON_DMA_ACT = (1 << 29),
59
	SIL_FLAG_MOD15WRITE	= (1 << 30),
60

J
Jeff Garzik 已提交
61
	SIL_DFL_PORT_FLAGS	= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
62
				  ATA_FLAG_MMIO,
63

64 65 66
	/*
	 * Controller IDs
	 */
L
Linus Torvalds 已提交
67
	sil_3112		= 0,
68 69 70
	sil_3112_no_sata_irq	= 1,
	sil_3512		= 2,
	sil_3114		= 3,
L
Linus Torvalds 已提交
71

72 73 74
	/*
	 * Register offsets
	 */
L
Linus Torvalds 已提交
75
	SIL_SYSCFG		= 0x48,
76 77 78 79 80

	/*
	 * Register bits
	 */
	/* SYSCFG */
L
Linus Torvalds 已提交
81 82 83 84 85 86 87 88
	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,

89
	/* BMDMA/BMDMA2 */
L
Linus Torvalds 已提交
90
	SIL_INTR_STEERING	= (1 << 1),
91

92 93 94 95 96 97 98 99 100 101 102 103 104 105
	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 */

106 107 108
	/*
	 * Others
	 */
L
Linus Torvalds 已提交
109 110 111 112
	SIL_QUIRK_MOD15WRITE	= (1 << 0),
	SIL_QUIRK_UDMA5MAX	= (1 << 1),
};

113
static int sil_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
114
#ifdef CONFIG_PM
115
static int sil_pci_device_resume(struct pci_dev *pdev);
116
#endif
117
static void sil_dev_config(struct ata_device *dev);
T
Tejun Heo 已提交
118 119
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);
120
static int sil_set_mode(struct ata_link *link, struct ata_device **r_failed);
T
Tejun Heo 已提交
121 122
static void sil_freeze(struct ata_port *ap);
static void sil_thaw(struct ata_port *ap);
L
Linus Torvalds 已提交
123

124

125
static const struct pci_device_id sil_pci_tbl[] = {
126 127 128 129 130 131 132 133
	{ 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 },

L
Linus Torvalds 已提交
134 135 136 137 138 139
	{ }	/* terminate list */
};


/* TODO firmware versions should be added - eric */
static const struct sil_drivelist {
140
	const char *product;
L
Linus Torvalds 已提交
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
	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 },
	{ "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,
163
#ifdef CONFIG_PM
164 165
	.suspend		= ata_pci_device_suspend,
	.resume			= sil_pci_device_resume,
166
#endif
L
Linus Torvalds 已提交
167 168
};

169
static struct scsi_host_template sil_sht = {
170
	ATA_BMDMA_SHT(DRV_NAME),
L
Linus Torvalds 已提交
171 172
};

173 174
static struct ata_port_operations sil_ops = {
	.inherits		= &ata_bmdma_port_ops,
L
Linus Torvalds 已提交
175
	.dev_config		= sil_dev_config,
176
	.set_mode		= sil_set_mode,
T
Tejun Heo 已提交
177 178
	.freeze			= sil_freeze,
	.thaw			= sil_thaw,
L
Linus Torvalds 已提交
179 180 181 182
	.scr_read		= sil_scr_read,
	.scr_write		= sil_scr_write,
};

183
static const struct ata_port_info sil_port_info[] = {
L
Linus Torvalds 已提交
184
	/* sil_3112 */
185
	{
J
Jeff Garzik 已提交
186
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_MOD15WRITE,
187 188
		.pio_mask	= 0x1f,			/* pio0-4 */
		.mwdma_mask	= 0x07,			/* mwdma0-2 */
189
		.udma_mask	= ATA_UDMA5,
190
		.port_ops	= &sil_ops,
191
	},
192 193
	/* sil_3112_no_sata_irq */
	{
J
Jeff Garzik 已提交
194
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_MOD15WRITE |
195 196 197
				  SIL_FLAG_NO_SATA_IRQ,
		.pio_mask	= 0x1f,			/* pio0-4 */
		.mwdma_mask	= 0x07,			/* mwdma0-2 */
198
		.udma_mask	= ATA_UDMA5,
199 200
		.port_ops	= &sil_ops,
	},
201
	/* sil_3512 */
L
Linus Torvalds 已提交
202
	{
J
Jeff Garzik 已提交
203
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_RERR_ON_DMA_ACT,
204 205
		.pio_mask	= 0x1f,			/* pio0-4 */
		.mwdma_mask	= 0x07,			/* mwdma0-2 */
206
		.udma_mask	= ATA_UDMA5,
207 208 209
		.port_ops	= &sil_ops,
	},
	/* sil_3114 */
L
Linus Torvalds 已提交
210
	{
J
Jeff Garzik 已提交
211
		.flags		= SIL_DFL_PORT_FLAGS | SIL_FLAG_RERR_ON_DMA_ACT,
L
Linus Torvalds 已提交
212 213
		.pio_mask	= 0x1f,			/* pio0-4 */
		.mwdma_mask	= 0x07,			/* mwdma0-2 */
214
		.udma_mask	= ATA_UDMA5,
L
Linus Torvalds 已提交
215 216 217 218 219 220 221 222 223 224
		.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 */
225
	unsigned long bmdma2;	/* DMA register block #2 */
226
	unsigned long fifo_cfg;	/* FIFO Valid Byte Count and Control */
L
Linus Torvalds 已提交
227 228 229
	unsigned long scr;	/* SATA control register block */
	unsigned long sien;	/* SATA Interrupt Enable register */
	unsigned long xfer_mode;/* data transfer mode register */
230
	unsigned long sfis_cfg;	/* SATA FIS reception config register */
L
Linus Torvalds 已提交
231 232
} sil_port[] = {
	/* port 0 ... */
233 234 235
	/*   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 },
236 237
	{ 0x280, 0x28A, 0x200, 0x210, 0x240, 0x300, 0x348, 0x2b4, 0x34c },
	{ 0x2C0, 0x2CA, 0x208, 0x218, 0x244, 0x380, 0x3c8, 0x2f4, 0x3cc },
L
Linus Torvalds 已提交
238 239 240 241 242 243 244 245 246
	/* ... 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);

247
static int slow_down;
248 249 250
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)");

251

L
Linus Torvalds 已提交
252 253 254 255 256 257 258
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;
}

259 260
/**
 *	sil_set_mode		-	wrap set_mode functions
261
 *	@link: link to set up
262 263 264 265 266 267
 *	@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
 */

268
static int sil_set_mode(struct ata_link *link, struct ata_device **r_failed)
L
Linus Torvalds 已提交
269
{
270 271
	struct ata_port *ap = link->ap;
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
T
Tejun Heo 已提交
272
	void __iomem *addr = mmio_base + sil_port[ap->port_no].xfer_mode;
273
	struct ata_device *dev;
274
	u32 tmp, dev_mode[2] = { };
275
	int rc;
J
Jeff Garzik 已提交
276

277
	rc = ata_do_set_mode(link, r_failed);
278 279
	if (rc)
		return rc;
L
Linus Torvalds 已提交
280

281
	ata_link_for_each_dev(dev, link) {
282
		if (!ata_dev_enabled(dev))
283
			dev_mode[dev->devno] = 0;	/* PIO0/1/2 */
L
Linus Torvalds 已提交
284
		else if (dev->flags & ATA_DFLAG_PIO)
285
			dev_mode[dev->devno] = 1;	/* PIO3/4 */
L
Linus Torvalds 已提交
286
		else
287
			dev_mode[dev->devno] = 3;	/* UDMA */
L
Linus Torvalds 已提交
288 289 290 291 292 293 294 295 296
		/* 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 */
297
	return 0;
L
Linus Torvalds 已提交
298 299
}

300 301
static inline void __iomem *sil_scr_addr(struct ata_port *ap,
					 unsigned int sc_reg)
L
Linus Torvalds 已提交
302
{
T
Tejun Heo 已提交
303
	void __iomem *offset = ap->ioaddr.scr_addr;
L
Linus Torvalds 已提交
304 305 306 307 308 309 310 311 312 313 314 315 316

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

R
Randy Dunlap 已提交
317
	return NULL;
L
Linus Torvalds 已提交
318 319
}

T
Tejun Heo 已提交
320
static int sil_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
L
Linus Torvalds 已提交
321
{
T
Tejun Heo 已提交
322
	void __iomem *mmio = sil_scr_addr(link->ap, sc_reg);
323 324 325 326 327 328

	if (mmio) {
		*val = readl(mmio);
		return 0;
	}
	return -EINVAL;
L
Linus Torvalds 已提交
329 330
}

T
Tejun Heo 已提交
331
static int sil_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
L
Linus Torvalds 已提交
332
{
T
Tejun Heo 已提交
333
	void __iomem *mmio = sil_scr_addr(link->ap, sc_reg);
334 335

	if (mmio) {
L
Linus Torvalds 已提交
336
		writel(val, mmio);
337 338 339
		return 0;
	}
	return -EINVAL;
L
Linus Torvalds 已提交
340 341
}

342 343
static void sil_host_intr(struct ata_port *ap, u32 bmdma2)
{
T
Tejun Heo 已提交
344 345
	struct ata_eh_info *ehi = &ap->link.eh_info;
	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
346 347
	u8 status;

348
	if (unlikely(bmdma2 & SIL_DMA_SATA_IRQ)) {
349 350 351 352 353 354
		u32 serror;

		/* 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.
		 */
T
Tejun Heo 已提交
355 356
		sil_scr_read(&ap->link, SCR_ERROR, &serror);
		sil_scr_write(&ap->link, SCR_ERROR, serror);
357

T
Tejun Heo 已提交
358 359
		/* Sometimes spurious interrupts occur, double check
		 * it's PHYRDY CHG.
360
		 */
T
Tejun Heo 已提交
361
		if (serror & SERR_PHYRDY_CHG) {
362
			ap->link.eh_info.serror |= serror;
T
Tejun Heo 已提交
363
			goto freeze;
364 365
		}

T
Tejun Heo 已提交
366 367
		if (!(bmdma2 & SIL_DMA_COMPLETE))
			return;
368 369
	}

T
Tejun Heo 已提交
370
	if (unlikely(!qc || (qc->tf.flags & ATA_TFLAG_POLLING))) {
371
		/* this sometimes happens, just clear IRQ */
T
Tejun Heo 已提交
372
		ap->ops->sff_check_status(ap);
373 374 375
		return;
	}

376 377 378 379 380 381 382 383
	/* 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.
T
Tejun Heo 已提交
384 385
		 * The flag was turned on only for atapi devices.  No
		 * need to check ata_is_atapi(qc->tf.protocol) again.
386 387 388 389 390
		 */
		if (!(qc->dev->flags & ATA_DFLAG_CDB_INTR))
			goto err_hsm;
		break;
	case HSM_ST_LAST:
T
Tejun Heo 已提交
391
		if (ata_is_dma(qc->tf.protocol)) {
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
			/* 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 */
T
Tejun Heo 已提交
408
	status = ap->ops->sff_check_status(ap);
409 410 411 412
	if (unlikely(status & ATA_BUSY))
		goto err_hsm;

	/* ack bmdma irq events */
T
Tejun Heo 已提交
413
	ata_sff_irq_clear(ap);
414 415

	/* kick HSM in the ass */
T
Tejun Heo 已提交
416
	ata_sff_hsm_move(ap, qc, status, 0);
417

T
Tejun Heo 已提交
418
	if (unlikely(qc->err_mask) && ata_is_dma(qc->tf.protocol))
419 420
		ata_ehi_push_desc(ehi, "BMDMA2 stat 0x%x", bmdma2);

421 422 423 424 425 426 427 428
	return;

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

429
static irqreturn_t sil_interrupt(int irq, void *dev_instance)
430
{
J
Jeff Garzik 已提交
431
	struct ata_host *host = dev_instance;
T
Tejun Heo 已提交
432
	void __iomem *mmio_base = host->iomap[SIL_MMIO_BAR];
433 434 435
	int handled = 0;
	int i;

J
Jeff Garzik 已提交
436
	spin_lock(&host->lock);
437

J
Jeff Garzik 已提交
438 439
	for (i = 0; i < host->n_ports; i++) {
		struct ata_port *ap = host->ports[i];
440 441 442 443 444
		u32 bmdma2 = readl(mmio_base + sil_port[ap->port_no].bmdma2);

		if (unlikely(!ap || ap->flags & ATA_FLAG_DISABLED))
			continue;

445 446 447 448
		/* turn off SATA_IRQ if not supported */
		if (ap->flags & SIL_FLAG_NO_SATA_IRQ)
			bmdma2 &= ~SIL_DMA_SATA_IRQ;

449 450
		if (bmdma2 == 0xffffffff ||
		    !(bmdma2 & (SIL_DMA_COMPLETE | SIL_DMA_SATA_IRQ)))
451 452 453 454 455 456
			continue;

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

J
Jeff Garzik 已提交
457
	spin_unlock(&host->lock);
458 459 460 461

	return IRQ_RETVAL(handled);
}

T
Tejun Heo 已提交
462 463
static void sil_freeze(struct ata_port *ap)
{
T
Tejun Heo 已提交
464
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
T
Tejun Heo 已提交
465 466
	u32 tmp;

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

T
Tejun Heo 已提交
470 471 472 473 474 475 476 477 478
	/* 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 */
}

static void sil_thaw(struct ata_port *ap)
{
T
Tejun Heo 已提交
479
	void __iomem *mmio_base = ap->host->iomap[SIL_MMIO_BAR];
T
Tejun Heo 已提交
480 481 482
	u32 tmp;

	/* clear IRQ */
T
Tejun Heo 已提交
483
	ap->ops->sff_check_status(ap);
T
Tejun Heo 已提交
484
	ata_sff_irq_clear(ap);
T
Tejun Heo 已提交
485

486 487 488
	/* 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);
489

T
Tejun Heo 已提交
490 491 492 493 494 495
	/* turn on IRQ */
	tmp = readl(mmio_base + SIL_SYSCFG);
	tmp &= ~(SIL_MASK_IDE0_INT << ap->port_no);
	writel(tmp, mmio_base + SIL_SYSCFG);
}

L
Linus Torvalds 已提交
496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523
/**
 *	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
 */
524
static void sil_dev_config(struct ata_device *dev)
L
Linus Torvalds 已提交
525
{
T
Tejun Heo 已提交
526 527
	struct ata_port *ap = dev->link->ap;
	int print_info = ap->link.eh_context.i.flags & ATA_EHI_PRINTINFO;
L
Linus Torvalds 已提交
528
	unsigned int n, quirks = 0;
529
	unsigned char model_num[ATA_ID_PROD_LEN + 1];
L
Linus Torvalds 已提交
530

531
	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
L
Linus Torvalds 已提交
532

J
Jeff Garzik 已提交
533
	for (n = 0; sil_blacklist[n].product; n++)
534
		if (!strcmp(sil_blacklist[n].product, model_num)) {
L
Linus Torvalds 已提交
535 536 537
			quirks = sil_blacklist[n].quirk;
			break;
		}
J
Jeff Garzik 已提交
538

L
Linus Torvalds 已提交
539
	/* limit requests to 15 sectors */
540 541 542
	if (slow_down ||
	    ((ap->flags & SIL_FLAG_MOD15WRITE) &&
	     (quirks & SIL_QUIRK_MOD15WRITE))) {
543 544 545
		if (print_info)
			ata_dev_printk(dev, KERN_INFO, "applying Seagate "
				       "errata fix (mod15write workaround)\n");
546
		dev->max_sectors = 15;
L
Linus Torvalds 已提交
547 548 549 550 551
		return;
	}

	/* limit to udma5 */
	if (quirks & SIL_QUIRK_UDMA5MAX) {
552 553 554
		if (print_info)
			ata_dev_printk(dev, KERN_INFO, "applying Maxtor "
				       "errata fix %s\n", model_num);
555
		dev->udma_mask &= ATA_UDMA5;
L
Linus Torvalds 已提交
556 557 558 559
		return;
	}
}

560
static void sil_init_controller(struct ata_host *host)
561
{
562 563
	struct pci_dev *pdev = to_pci_dev(host->dev);
	void __iomem *mmio_base = host->iomap[SIL_MMIO_BAR];
564 565 566 567 568 569 570 571 572
	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 */
573
		for (i = 0; i < host->n_ports; i++)
574 575 576 577 578 579 580
			writew(cls << 8 | cls,
			       mmio_base + sil_port[i].fifo_cfg);
	} else
		dev_printk(KERN_WARNING, &pdev->dev,
			   "cache line size not set.  Driver may not function\n");

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

584
		for (i = 0, cnt = 0; i < host->n_ports; i++) {
585 586 587 588 589 590 591 592 593 594 595 596
			tmp = readl(mmio_base + sil_port[i].sfis_cfg);
			if ((tmp & 0x3) != 0x01)
				continue;
			if (!cnt)
				dev_printk(KERN_INFO, &pdev->dev,
					   "Applying R_ERR on DMA activate "
					   "FIS errata fix\n");
			writel(tmp & ~0x3, mmio_base + sil_port[i].sfis_cfg);
			cnt++;
		}
	}

597
	if (host->n_ports == 4) {
598 599 600 601 602 603 604 605
		/* 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);
	}
}

606
static int sil_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
L
Linus Torvalds 已提交
607 608
{
	static int printed_version;
609 610 611
	int board_id = ent->driver_data;
	const struct ata_port_info *ppi[] = { &sil_port_info[board_id], NULL };
	struct ata_host *host;
612
	void __iomem *mmio_base;
613
	int n_ports, rc;
L
Linus Torvalds 已提交
614 615 616
	unsigned int i;

	if (!printed_version++)
617
		dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
L
Linus Torvalds 已提交
618

619 620 621 622 623 624 625 626 627 628
	/* allocate host */
	n_ports = 2;
	if (board_id == sil_3114)
		n_ports = 4;

	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
	if (!host)
		return -ENOMEM;

	/* acquire resources and fill host */
629
	rc = pcim_enable_device(pdev);
L
Linus Torvalds 已提交
630 631 632
	if (rc)
		return rc;

T
Tejun Heo 已提交
633 634
	rc = pcim_iomap_regions(pdev, 1 << SIL_MMIO_BAR, DRV_NAME);
	if (rc == -EBUSY)
635
		pcim_pin_device(pdev);
T
Tejun Heo 已提交
636
	if (rc)
637
		return rc;
638
	host->iomap = pcim_iomap_table(pdev);
L
Linus Torvalds 已提交
639 640 641

	rc = pci_set_dma_mask(pdev, ATA_DMA_MASK);
	if (rc)
642
		return rc;
L
Linus Torvalds 已提交
643 644
	rc = pci_set_consistent_dma_mask(pdev, ATA_DMA_MASK);
	if (rc)
645
		return rc;
L
Linus Torvalds 已提交
646

647
	mmio_base = host->iomap[SIL_MMIO_BAR];
L
Linus Torvalds 已提交
648

649
	for (i = 0; i < host->n_ports; i++) {
650 651
		struct ata_port *ap = host->ports[i];
		struct ata_ioports *ioaddr = &ap->ioaddr;
652 653 654 655 656 657

		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;
T
Tejun Heo 已提交
658
		ata_sff_std_ports(ioaddr);
659 660 661

		ata_port_pbar_desc(ap, SIL_MMIO_BAR, -1, "mmio");
		ata_port_pbar_desc(ap, SIL_MMIO_BAR, sil_port[i].tf, "tf");
L
Linus Torvalds 已提交
662 663
	}

664 665
	/* initialize and activate */
	sil_init_controller(host);
L
Linus Torvalds 已提交
666 667

	pci_set_master(pdev);
668 669
	return ata_host_activate(host, pdev->irq, sil_interrupt, IRQF_SHARED,
				 &sil_sht);
L
Linus Torvalds 已提交
670 671
}

672
#ifdef CONFIG_PM
673 674
static int sil_pci_device_resume(struct pci_dev *pdev)
{
J
Jeff Garzik 已提交
675
	struct ata_host *host = dev_get_drvdata(&pdev->dev);
676 677 678 679 680
	int rc;

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

682
	sil_init_controller(host);
J
Jeff Garzik 已提交
683
	ata_host_resume(host);
684 685 686

	return 0;
}
687
#endif
688

L
Linus Torvalds 已提交
689 690
static int __init sil_init(void)
{
691
	return pci_register_driver(&sil_pci_driver);
L
Linus Torvalds 已提交
692 693 694 695 696 697 698 699 700 701
}

static void __exit sil_exit(void)
{
	pci_unregister_driver(&sil_pci_driver);
}


module_init(sil_init);
module_exit(sil_exit);