xref: /openbmc/linux/drivers/ata/sata_inic162x.c (revision f356b08205f6668248960093faf9326c7852a38d)
11fd7a697STejun Heo /*
21fd7a697STejun Heo  * sata_inic162x.c - Driver for Initio 162x SATA controllers
31fd7a697STejun Heo  *
41fd7a697STejun Heo  * Copyright 2006  SUSE Linux Products GmbH
51fd7a697STejun Heo  * Copyright 2006  Tejun Heo <teheo@novell.com>
61fd7a697STejun Heo  *
71fd7a697STejun Heo  * This file is released under GPL v2.
81fd7a697STejun Heo  *
9bb969619STejun Heo  * **** WARNING ****
10bb969619STejun Heo  *
11bb969619STejun Heo  * This driver never worked properly and unfortunately data corruption is
12bb969619STejun Heo  * relatively common.  There isn't anyone working on the driver and there's
13bb969619STejun Heo  * no support from the vendor.  Do not use this driver in any production
14bb969619STejun Heo  * environment.
15bb969619STejun Heo  *
16bb969619STejun Heo  * http://thread.gmane.org/gmane.linux.debian.devel.bugs.rc/378525/focus=54491
17bb969619STejun Heo  * https://bugzilla.kernel.org/show_bug.cgi?id=60565
18bb969619STejun Heo  *
19bb969619STejun Heo  * *****************
20bb969619STejun Heo  *
211fd7a697STejun Heo  * This controller is eccentric and easily locks up if something isn't
221fd7a697STejun Heo  * right.  Documentation is available at initio's website but it only
231fd7a697STejun Heo  * documents registers (not programming model).
241fd7a697STejun Heo  *
2522bfc6d5STejun Heo  * This driver has interesting history.  The first version was written
2622bfc6d5STejun Heo  * from the documentation and a 2.4 IDE driver posted on a Taiwan
2722bfc6d5STejun Heo  * company, which didn't use any IDMA features and couldn't handle
2822bfc6d5STejun Heo  * LBA48.  The resulting driver couldn't handle LBA48 devices either
2922bfc6d5STejun Heo  * making it pretty useless.
3022bfc6d5STejun Heo  *
3122bfc6d5STejun Heo  * After a while, initio picked the driver up, renamed it to
3222bfc6d5STejun Heo  * sata_initio162x, updated it to use IDMA for ATA DMA commands and
3322bfc6d5STejun Heo  * posted it on their website.  It only used ATA_PROT_DMA for IDMA and
3422bfc6d5STejun Heo  * attaching both devices and issuing IDMA and !IDMA commands
3522bfc6d5STejun Heo  * simultaneously broke it due to PIRQ masking interaction but it did
3622bfc6d5STejun Heo  * show how to use the IDMA (ADMA + some initio specific twists)
3722bfc6d5STejun Heo  * engine.
3822bfc6d5STejun Heo  *
3922bfc6d5STejun Heo  * Then, I picked up their changes again and here's the usable driver
4022bfc6d5STejun Heo  * which uses IDMA for everything.  Everything works now including
4122bfc6d5STejun Heo  * LBA48, CD/DVD burning, suspend/resume and hotplug.  There are some
4222bfc6d5STejun Heo  * issues tho.  Result Tf is not resported properly, NCQ isn't
4322bfc6d5STejun Heo  * supported yet and CD/DVD writing works with DMA assisted PIO
4422bfc6d5STejun Heo  * protocol (which, for native SATA devices, shouldn't cause any
4522bfc6d5STejun Heo  * noticeable difference).
4622bfc6d5STejun Heo  *
4722bfc6d5STejun Heo  * Anyways, so, here's finally a working driver for inic162x.  Enjoy!
4822bfc6d5STejun Heo  *
4922bfc6d5STejun Heo  * initio: If you guys wanna improve the driver regarding result TF
5022bfc6d5STejun Heo  * access and other stuff, please feel free to contact me.  I'll be
5122bfc6d5STejun Heo  * happy to assist.
521fd7a697STejun Heo  */
531fd7a697STejun Heo 
545a0e3ad6STejun Heo #include <linux/gfp.h>
551fd7a697STejun Heo #include <linux/kernel.h>
561fd7a697STejun Heo #include <linux/module.h>
571fd7a697STejun Heo #include <linux/pci.h>
581fd7a697STejun Heo #include <scsi/scsi_host.h>
591fd7a697STejun Heo #include <linux/libata.h>
601fd7a697STejun Heo #include <linux/blkdev.h>
611fd7a697STejun Heo #include <scsi/scsi_device.h>
621fd7a697STejun Heo 
631fd7a697STejun Heo #define DRV_NAME	"sata_inic162x"
6422bfc6d5STejun Heo #define DRV_VERSION	"0.4"
651fd7a697STejun Heo 
661fd7a697STejun Heo enum {
67ba66b242STejun Heo 	MMIO_BAR_PCI		= 5,
68ba66b242STejun Heo 	MMIO_BAR_CARDBUS	= 1,
691fd7a697STejun Heo 
701fd7a697STejun Heo 	NR_PORTS		= 2,
711fd7a697STejun Heo 
723ad400a9STejun Heo 	IDMA_CPB_TBL_SIZE	= 4 * 32,
733ad400a9STejun Heo 
743ad400a9STejun Heo 	INIC_DMA_BOUNDARY	= 0xffffff,
753ad400a9STejun Heo 
76b0dd9b8eSTejun Heo 	HOST_ACTRL		= 0x08,
771fd7a697STejun Heo 	HOST_CTL		= 0x7c,
781fd7a697STejun Heo 	HOST_STAT		= 0x7e,
791fd7a697STejun Heo 	HOST_IRQ_STAT		= 0xbc,
801fd7a697STejun Heo 	HOST_IRQ_MASK		= 0xbe,
811fd7a697STejun Heo 
821fd7a697STejun Heo 	PORT_SIZE		= 0x40,
831fd7a697STejun Heo 
841fd7a697STejun Heo 	/* registers for ATA TF operation */
85b0dd9b8eSTejun Heo 	PORT_TF_DATA		= 0x00,
86b0dd9b8eSTejun Heo 	PORT_TF_FEATURE		= 0x01,
87b0dd9b8eSTejun Heo 	PORT_TF_NSECT		= 0x02,
88b0dd9b8eSTejun Heo 	PORT_TF_LBAL		= 0x03,
89b0dd9b8eSTejun Heo 	PORT_TF_LBAM		= 0x04,
90b0dd9b8eSTejun Heo 	PORT_TF_LBAH		= 0x05,
91b0dd9b8eSTejun Heo 	PORT_TF_DEVICE		= 0x06,
92b0dd9b8eSTejun Heo 	PORT_TF_COMMAND		= 0x07,
93b0dd9b8eSTejun Heo 	PORT_TF_ALT_STAT	= 0x08,
941fd7a697STejun Heo 	PORT_IRQ_STAT		= 0x09,
951fd7a697STejun Heo 	PORT_IRQ_MASK		= 0x0a,
961fd7a697STejun Heo 	PORT_PRD_CTL		= 0x0b,
971fd7a697STejun Heo 	PORT_PRD_ADDR		= 0x0c,
981fd7a697STejun Heo 	PORT_PRD_XFERLEN	= 0x10,
99b0dd9b8eSTejun Heo 	PORT_CPB_CPBLAR		= 0x18,
100b0dd9b8eSTejun Heo 	PORT_CPB_PTQFIFO	= 0x1c,
1011fd7a697STejun Heo 
1021fd7a697STejun Heo 	/* IDMA register */
1031fd7a697STejun Heo 	PORT_IDMA_CTL		= 0x14,
104b0dd9b8eSTejun Heo 	PORT_IDMA_STAT		= 0x16,
105b0dd9b8eSTejun Heo 
106b0dd9b8eSTejun Heo 	PORT_RPQ_FIFO		= 0x1e,
107b0dd9b8eSTejun Heo 	PORT_RPQ_CNT		= 0x1f,
1081fd7a697STejun Heo 
1091fd7a697STejun Heo 	PORT_SCR		= 0x20,
1101fd7a697STejun Heo 
1111fd7a697STejun Heo 	/* HOST_CTL bits */
11299580664SBob Stewart 	HCTL_LEDEN		= (1 << 3),  /* enable LED operation */
1131fd7a697STejun Heo 	HCTL_IRQOFF		= (1 << 8),  /* global IRQ off */
114b0dd9b8eSTejun Heo 	HCTL_FTHD0		= (1 << 10), /* fifo threshold 0 */
115b0dd9b8eSTejun Heo 	HCTL_FTHD1		= (1 << 11), /* fifo threshold 1*/
116b0dd9b8eSTejun Heo 	HCTL_PWRDWN		= (1 << 12), /* power down PHYs */
1171fd7a697STejun Heo 	HCTL_SOFTRST		= (1 << 13), /* global reset (no phy reset) */
1181fd7a697STejun Heo 	HCTL_RPGSEL		= (1 << 15), /* register page select */
1191fd7a697STejun Heo 
1201fd7a697STejun Heo 	HCTL_KNOWN_BITS		= HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
1211fd7a697STejun Heo 				  HCTL_RPGSEL,
1221fd7a697STejun Heo 
1231fd7a697STejun Heo 	/* HOST_IRQ_(STAT|MASK) bits */
1241fd7a697STejun Heo 	HIRQ_PORT0		= (1 << 0),
1251fd7a697STejun Heo 	HIRQ_PORT1		= (1 << 1),
1261fd7a697STejun Heo 	HIRQ_SOFT		= (1 << 14),
1271fd7a697STejun Heo 	HIRQ_GLOBAL		= (1 << 15), /* STAT only */
1281fd7a697STejun Heo 
1291fd7a697STejun Heo 	/* PORT_IRQ_(STAT|MASK) bits */
1301fd7a697STejun Heo 	PIRQ_OFFLINE		= (1 << 0),  /* device unplugged */
1311fd7a697STejun Heo 	PIRQ_ONLINE		= (1 << 1),  /* device plugged */
1321fd7a697STejun Heo 	PIRQ_COMPLETE		= (1 << 2),  /* completion interrupt */
1331fd7a697STejun Heo 	PIRQ_FATAL		= (1 << 3),  /* fatal error */
1341fd7a697STejun Heo 	PIRQ_ATA		= (1 << 4),  /* ATA interrupt */
1351fd7a697STejun Heo 	PIRQ_REPLY		= (1 << 5),  /* reply FIFO not empty */
1361fd7a697STejun Heo 	PIRQ_PENDING		= (1 << 7),  /* port IRQ pending (STAT only) */
1371fd7a697STejun Heo 
1381fd7a697STejun Heo 	PIRQ_ERR		= PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
139f8b0685aSTejun Heo 	PIRQ_MASK_DEFAULT	= PIRQ_REPLY | PIRQ_ATA,
1401fd7a697STejun Heo 	PIRQ_MASK_FREEZE	= 0xff,
1411fd7a697STejun Heo 
1421fd7a697STejun Heo 	/* PORT_PRD_CTL bits */
1431fd7a697STejun Heo 	PRD_CTL_START		= (1 << 0),
1441fd7a697STejun Heo 	PRD_CTL_WR		= (1 << 3),
1451fd7a697STejun Heo 	PRD_CTL_DMAEN		= (1 << 7),  /* DMA enable */
1461fd7a697STejun Heo 
1471fd7a697STejun Heo 	/* PORT_IDMA_CTL bits */
1481fd7a697STejun Heo 	IDMA_CTL_RST_ATA	= (1 << 2),  /* hardreset ATA bus */
1491fd7a697STejun Heo 	IDMA_CTL_RST_IDMA	= (1 << 5),  /* reset IDMA machinary */
1501fd7a697STejun Heo 	IDMA_CTL_GO		= (1 << 7),  /* IDMA mode go */
1511fd7a697STejun Heo 	IDMA_CTL_ATA_NIEN	= (1 << 8),  /* ATA IRQ disable */
152b0dd9b8eSTejun Heo 
153b0dd9b8eSTejun Heo 	/* PORT_IDMA_STAT bits */
154b0dd9b8eSTejun Heo 	IDMA_STAT_PERR		= (1 << 0),  /* PCI ERROR MODE */
155b0dd9b8eSTejun Heo 	IDMA_STAT_CPBERR	= (1 << 1),  /* ADMA CPB error */
156b0dd9b8eSTejun Heo 	IDMA_STAT_LGCY		= (1 << 3),  /* ADMA legacy */
157b0dd9b8eSTejun Heo 	IDMA_STAT_UIRQ		= (1 << 4),  /* ADMA unsolicited irq */
158b0dd9b8eSTejun Heo 	IDMA_STAT_STPD		= (1 << 5),  /* ADMA stopped */
159b0dd9b8eSTejun Heo 	IDMA_STAT_PSD		= (1 << 6),  /* ADMA pause */
160b0dd9b8eSTejun Heo 	IDMA_STAT_DONE		= (1 << 7),  /* ADMA done */
161b0dd9b8eSTejun Heo 
162b0dd9b8eSTejun Heo 	IDMA_STAT_ERR		= IDMA_STAT_PERR | IDMA_STAT_CPBERR,
163b0dd9b8eSTejun Heo 
164b0dd9b8eSTejun Heo 	/* CPB Control Flags*/
165b0dd9b8eSTejun Heo 	CPB_CTL_VALID		= (1 << 0),  /* CPB valid */
166b0dd9b8eSTejun Heo 	CPB_CTL_QUEUED		= (1 << 1),  /* queued command */
167b0dd9b8eSTejun Heo 	CPB_CTL_DATA		= (1 << 2),  /* data, rsvd in datasheet */
168b0dd9b8eSTejun Heo 	CPB_CTL_IEN		= (1 << 3),  /* PCI interrupt enable */
169b0dd9b8eSTejun Heo 	CPB_CTL_DEVDIR		= (1 << 4),  /* device direction control */
170b0dd9b8eSTejun Heo 
171b0dd9b8eSTejun Heo 	/* CPB Response Flags */
172b0dd9b8eSTejun Heo 	CPB_RESP_DONE		= (1 << 0),  /* ATA command complete */
173b0dd9b8eSTejun Heo 	CPB_RESP_REL		= (1 << 1),  /* ATA release */
174b0dd9b8eSTejun Heo 	CPB_RESP_IGNORED	= (1 << 2),  /* CPB ignored */
175b0dd9b8eSTejun Heo 	CPB_RESP_ATA_ERR	= (1 << 3),  /* ATA command error */
176b0dd9b8eSTejun Heo 	CPB_RESP_SPURIOUS	= (1 << 4),  /* ATA spurious interrupt error */
177b0dd9b8eSTejun Heo 	CPB_RESP_UNDERFLOW	= (1 << 5),  /* APRD deficiency length error */
178b0dd9b8eSTejun Heo 	CPB_RESP_OVERFLOW	= (1 << 6),  /* APRD exccess length error */
179b0dd9b8eSTejun Heo 	CPB_RESP_CPB_ERR	= (1 << 7),  /* CPB error flag */
180b0dd9b8eSTejun Heo 
181b0dd9b8eSTejun Heo 	/* PRD Control Flags */
182b0dd9b8eSTejun Heo 	PRD_DRAIN		= (1 << 1),  /* ignore data excess */
183b0dd9b8eSTejun Heo 	PRD_CDB			= (1 << 2),  /* atapi packet command pointer */
184b0dd9b8eSTejun Heo 	PRD_DIRECT_INTR		= (1 << 3),  /* direct interrupt */
185b0dd9b8eSTejun Heo 	PRD_DMA			= (1 << 4),  /* data transfer method */
186b0dd9b8eSTejun Heo 	PRD_WRITE		= (1 << 5),  /* data dir, rsvd in datasheet */
187b0dd9b8eSTejun Heo 	PRD_IOM			= (1 << 6),  /* io/memory transfer */
188b0dd9b8eSTejun Heo 	PRD_END			= (1 << 7),  /* APRD chain end */
1891fd7a697STejun Heo };
1901fd7a697STejun Heo 
1913ad400a9STejun Heo /* Comman Parameter Block */
1923ad400a9STejun Heo struct inic_cpb {
1933ad400a9STejun Heo 	u8		resp_flags;	/* Response Flags */
1943ad400a9STejun Heo 	u8		error;		/* ATA Error */
1953ad400a9STejun Heo 	u8		status;		/* ATA Status */
1963ad400a9STejun Heo 	u8		ctl_flags;	/* Control Flags */
1973ad400a9STejun Heo 	__le32		len;		/* Total Transfer Length */
1983ad400a9STejun Heo 	__le32		prd;		/* First PRD pointer */
1993ad400a9STejun Heo 	u8		rsvd[4];
2003ad400a9STejun Heo 	/* 16 bytes */
2013ad400a9STejun Heo 	u8		feature;	/* ATA Feature */
2023ad400a9STejun Heo 	u8		hob_feature;	/* ATA Ex. Feature */
2033ad400a9STejun Heo 	u8		device;		/* ATA Device/Head */
2043ad400a9STejun Heo 	u8		mirctl;		/* Mirror Control */
2053ad400a9STejun Heo 	u8		nsect;		/* ATA Sector Count */
2063ad400a9STejun Heo 	u8		hob_nsect;	/* ATA Ex. Sector Count */
2073ad400a9STejun Heo 	u8		lbal;		/* ATA Sector Number */
2083ad400a9STejun Heo 	u8		hob_lbal;	/* ATA Ex. Sector Number */
2093ad400a9STejun Heo 	u8		lbam;		/* ATA Cylinder Low */
2103ad400a9STejun Heo 	u8		hob_lbam;	/* ATA Ex. Cylinder Low */
2113ad400a9STejun Heo 	u8		lbah;		/* ATA Cylinder High */
2123ad400a9STejun Heo 	u8		hob_lbah;	/* ATA Ex. Cylinder High */
2133ad400a9STejun Heo 	u8		command;	/* ATA Command */
2143ad400a9STejun Heo 	u8		ctl;		/* ATA Control */
2153ad400a9STejun Heo 	u8		slave_error;	/* Slave ATA Error */
2163ad400a9STejun Heo 	u8		slave_status;	/* Slave ATA Status */
2173ad400a9STejun Heo 	/* 32 bytes */
2183ad400a9STejun Heo } __packed;
2193ad400a9STejun Heo 
2203ad400a9STejun Heo /* Physical Region Descriptor */
2213ad400a9STejun Heo struct inic_prd {
2223ad400a9STejun Heo 	__le32		mad;		/* Physical Memory Address */
2233ad400a9STejun Heo 	__le16		len;		/* Transfer Length */
2243ad400a9STejun Heo 	u8		rsvd;
2253ad400a9STejun Heo 	u8		flags;		/* Control Flags */
2263ad400a9STejun Heo } __packed;
2273ad400a9STejun Heo 
2283ad400a9STejun Heo struct inic_pkt {
2293ad400a9STejun Heo 	struct inic_cpb	cpb;
230b3f677e5STejun Heo 	struct inic_prd	prd[LIBATA_MAX_PRD + 1];	/* + 1 for cdb */
231b3f677e5STejun Heo 	u8		cdb[ATAPI_CDB_LEN];
2323ad400a9STejun Heo } __packed;
2333ad400a9STejun Heo 
2341fd7a697STejun Heo struct inic_host_priv {
235ba66b242STejun Heo 	void __iomem	*mmio_base;
2361fd7a697STejun Heo 	u16		cached_hctl;
2371fd7a697STejun Heo };
2381fd7a697STejun Heo 
2391fd7a697STejun Heo struct inic_port_priv {
2403ad400a9STejun Heo 	struct inic_pkt	*pkt;
2413ad400a9STejun Heo 	dma_addr_t	pkt_dma;
2423ad400a9STejun Heo 	u32		*cpb_tbl;
2433ad400a9STejun Heo 	dma_addr_t	cpb_tbl_dma;
2441fd7a697STejun Heo };
2451fd7a697STejun Heo 
2461fd7a697STejun Heo static struct scsi_host_template inic_sht = {
247ab5b0235STejun Heo 	ATA_BASE_SHT(DRV_NAME),
248ab5b0235STejun Heo 	.sg_tablesize	= LIBATA_MAX_PRD,	/* maybe it can be larger? */
2493ad400a9STejun Heo 	.dma_boundary	= INIC_DMA_BOUNDARY,
2501fd7a697STejun Heo };
2511fd7a697STejun Heo 
2521fd7a697STejun Heo static const int scr_map[] = {
2531fd7a697STejun Heo 	[SCR_STATUS]	= 0,
2541fd7a697STejun Heo 	[SCR_ERROR]	= 1,
2551fd7a697STejun Heo 	[SCR_CONTROL]	= 2,
2561fd7a697STejun Heo };
2571fd7a697STejun Heo 
2581fd7a697STejun Heo static void __iomem *inic_port_base(struct ata_port *ap)
2591fd7a697STejun Heo {
260ba66b242STejun Heo 	struct inic_host_priv *hpriv = ap->host->private_data;
261ba66b242STejun Heo 
262ba66b242STejun Heo 	return hpriv->mmio_base + ap->port_no * PORT_SIZE;
2631fd7a697STejun Heo }
2641fd7a697STejun Heo 
2651fd7a697STejun Heo static void inic_reset_port(void __iomem *port_base)
2661fd7a697STejun Heo {
2671fd7a697STejun Heo 	void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
2681fd7a697STejun Heo 
269f8b0685aSTejun Heo 	/* stop IDMA engine */
270f8b0685aSTejun Heo 	readw(idma_ctl); /* flush */
271f8b0685aSTejun Heo 	msleep(1);
2721fd7a697STejun Heo 
2731fd7a697STejun Heo 	/* mask IRQ and assert reset */
274f8b0685aSTejun Heo 	writew(IDMA_CTL_RST_IDMA, idma_ctl);
2751fd7a697STejun Heo 	readw(idma_ctl); /* flush */
2761fd7a697STejun Heo 	msleep(1);
2771fd7a697STejun Heo 
2781fd7a697STejun Heo 	/* release reset */
279f8b0685aSTejun Heo 	writew(0, idma_ctl);
2801fd7a697STejun Heo 
2811fd7a697STejun Heo 	/* clear irq */
2821fd7a697STejun Heo 	writeb(0xff, port_base + PORT_IRQ_STAT);
2831fd7a697STejun Heo }
2841fd7a697STejun Heo 
28582ef04fbSTejun Heo static int inic_scr_read(struct ata_link *link, unsigned sc_reg, u32 *val)
2861fd7a697STejun Heo {
28782ef04fbSTejun Heo 	void __iomem *scr_addr = inic_port_base(link->ap) + PORT_SCR;
2881fd7a697STejun Heo 
2891fd7a697STejun Heo 	if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
290da3dbb17STejun Heo 		return -EINVAL;
2911fd7a697STejun Heo 
292da3dbb17STejun Heo 	*val = readl(scr_addr + scr_map[sc_reg] * 4);
2931fd7a697STejun Heo 
2941fd7a697STejun Heo 	/* this controller has stuck DIAG.N, ignore it */
2951fd7a697STejun Heo 	if (sc_reg == SCR_ERROR)
296da3dbb17STejun Heo 		*val &= ~SERR_PHYRDY_CHG;
297da3dbb17STejun Heo 	return 0;
2981fd7a697STejun Heo }
2991fd7a697STejun Heo 
30082ef04fbSTejun Heo static int inic_scr_write(struct ata_link *link, unsigned sc_reg, u32 val)
3011fd7a697STejun Heo {
30282ef04fbSTejun Heo 	void __iomem *scr_addr = inic_port_base(link->ap) + PORT_SCR;
3031fd7a697STejun Heo 
3041fd7a697STejun Heo 	if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
305da3dbb17STejun Heo 		return -EINVAL;
3061fd7a697STejun Heo 
3071fd7a697STejun Heo 	writel(val, scr_addr + scr_map[sc_reg] * 4);
308da3dbb17STejun Heo 	return 0;
3091fd7a697STejun Heo }
3101fd7a697STejun Heo 
3113ad400a9STejun Heo static void inic_stop_idma(struct ata_port *ap)
3123ad400a9STejun Heo {
3133ad400a9STejun Heo 	void __iomem *port_base = inic_port_base(ap);
3143ad400a9STejun Heo 
3153ad400a9STejun Heo 	readb(port_base + PORT_RPQ_FIFO);
3163ad400a9STejun Heo 	readb(port_base + PORT_RPQ_CNT);
3173ad400a9STejun Heo 	writew(0, port_base + PORT_IDMA_CTL);
3183ad400a9STejun Heo }
3193ad400a9STejun Heo 
3203ad400a9STejun Heo static void inic_host_err_intr(struct ata_port *ap, u8 irq_stat, u16 idma_stat)
3213ad400a9STejun Heo {
3223ad400a9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
3233ad400a9STejun Heo 	struct inic_port_priv *pp = ap->private_data;
3243ad400a9STejun Heo 	struct inic_cpb *cpb = &pp->pkt->cpb;
3253ad400a9STejun Heo 	bool freeze = false;
3263ad400a9STejun Heo 
3273ad400a9STejun Heo 	ata_ehi_clear_desc(ehi);
3283ad400a9STejun Heo 	ata_ehi_push_desc(ehi, "irq_stat=0x%x idma_stat=0x%x",
3293ad400a9STejun Heo 			  irq_stat, idma_stat);
3303ad400a9STejun Heo 
3313ad400a9STejun Heo 	inic_stop_idma(ap);
3323ad400a9STejun Heo 
3333ad400a9STejun Heo 	if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
3343ad400a9STejun Heo 		ata_ehi_push_desc(ehi, "hotplug");
3353ad400a9STejun Heo 		ata_ehi_hotplugged(ehi);
3363ad400a9STejun Heo 		freeze = true;
3373ad400a9STejun Heo 	}
3383ad400a9STejun Heo 
3393ad400a9STejun Heo 	if (idma_stat & IDMA_STAT_PERR) {
3403ad400a9STejun Heo 		ata_ehi_push_desc(ehi, "PCI error");
3413ad400a9STejun Heo 		freeze = true;
3423ad400a9STejun Heo 	}
3433ad400a9STejun Heo 
3443ad400a9STejun Heo 	if (idma_stat & IDMA_STAT_CPBERR) {
3453ad400a9STejun Heo 		ata_ehi_push_desc(ehi, "CPB error");
3463ad400a9STejun Heo 
3473ad400a9STejun Heo 		if (cpb->resp_flags & CPB_RESP_IGNORED) {
3483ad400a9STejun Heo 			__ata_ehi_push_desc(ehi, " ignored");
3493ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_INVALID;
3503ad400a9STejun Heo 			freeze = true;
3513ad400a9STejun Heo 		}
3523ad400a9STejun Heo 
3533ad400a9STejun Heo 		if (cpb->resp_flags & CPB_RESP_ATA_ERR)
3543ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_DEV;
3553ad400a9STejun Heo 
3563ad400a9STejun Heo 		if (cpb->resp_flags & CPB_RESP_SPURIOUS) {
3573ad400a9STejun Heo 			__ata_ehi_push_desc(ehi, " spurious-intr");
3583ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_HSM;
3593ad400a9STejun Heo 			freeze = true;
3603ad400a9STejun Heo 		}
3613ad400a9STejun Heo 
3623ad400a9STejun Heo 		if (cpb->resp_flags &
3633ad400a9STejun Heo 		    (CPB_RESP_UNDERFLOW | CPB_RESP_OVERFLOW)) {
3643ad400a9STejun Heo 			__ata_ehi_push_desc(ehi, " data-over/underflow");
3653ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_HSM;
3663ad400a9STejun Heo 			freeze = true;
3673ad400a9STejun Heo 		}
3683ad400a9STejun Heo 	}
3693ad400a9STejun Heo 
3703ad400a9STejun Heo 	if (freeze)
3713ad400a9STejun Heo 		ata_port_freeze(ap);
3723ad400a9STejun Heo 	else
3733ad400a9STejun Heo 		ata_port_abort(ap);
3743ad400a9STejun Heo }
3753ad400a9STejun Heo 
3761fd7a697STejun Heo static void inic_host_intr(struct ata_port *ap)
3771fd7a697STejun Heo {
3781fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
3793ad400a9STejun Heo 	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
3801fd7a697STejun Heo 	u8 irq_stat;
3813ad400a9STejun Heo 	u16 idma_stat;
3821fd7a697STejun Heo 
3833ad400a9STejun Heo 	/* read and clear IRQ status */
3841fd7a697STejun Heo 	irq_stat = readb(port_base + PORT_IRQ_STAT);
3851fd7a697STejun Heo 	writeb(irq_stat, port_base + PORT_IRQ_STAT);
3863ad400a9STejun Heo 	idma_stat = readw(port_base + PORT_IDMA_STAT);
3871fd7a697STejun Heo 
3883ad400a9STejun Heo 	if (unlikely((irq_stat & PIRQ_ERR) || (idma_stat & IDMA_STAT_ERR)))
3893ad400a9STejun Heo 		inic_host_err_intr(ap, irq_stat, idma_stat);
3901fd7a697STejun Heo 
391f8b0685aSTejun Heo 	if (unlikely(!qc))
3923ad400a9STejun Heo 		goto spurious;
3931fd7a697STejun Heo 
3943ad400a9STejun Heo 	if (likely(idma_stat & IDMA_STAT_DONE)) {
3953ad400a9STejun Heo 		inic_stop_idma(ap);
3963ad400a9STejun Heo 
3973ad400a9STejun Heo 		/* Depending on circumstances, device error
3983ad400a9STejun Heo 		 * isn't reported by IDMA, check it explicitly.
3993ad400a9STejun Heo 		 */
4003ad400a9STejun Heo 		if (unlikely(readb(port_base + PORT_TF_COMMAND) &
4013ad400a9STejun Heo 			     (ATA_DF | ATA_ERR)))
4023ad400a9STejun Heo 			qc->err_mask |= AC_ERR_DEV;
4033ad400a9STejun Heo 
4043ad400a9STejun Heo 		ata_qc_complete(qc);
4053ad400a9STejun Heo 		return;
4063ad400a9STejun Heo 	}
4071fd7a697STejun Heo 
4083ad400a9STejun Heo  spurious:
409a9a79dfeSJoe Perches 	ata_port_warn(ap, "unhandled interrupt: cmd=0x%x irq_stat=0x%x idma_stat=0x%x\n",
410f8b0685aSTejun Heo 		      qc ? qc->tf.command : 0xff, irq_stat, idma_stat);
4111fd7a697STejun Heo }
4121fd7a697STejun Heo 
4131fd7a697STejun Heo static irqreturn_t inic_interrupt(int irq, void *dev_instance)
4141fd7a697STejun Heo {
4151fd7a697STejun Heo 	struct ata_host *host = dev_instance;
416ba66b242STejun Heo 	struct inic_host_priv *hpriv = host->private_data;
4171fd7a697STejun Heo 	u16 host_irq_stat;
41887c8b22bSJoe Perches 	int i, handled = 0;
4191fd7a697STejun Heo 
420ba66b242STejun Heo 	host_irq_stat = readw(hpriv->mmio_base + HOST_IRQ_STAT);
4211fd7a697STejun Heo 
4221fd7a697STejun Heo 	if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
4231fd7a697STejun Heo 		goto out;
4241fd7a697STejun Heo 
4251fd7a697STejun Heo 	spin_lock(&host->lock);
4261fd7a697STejun Heo 
4273e4ec344STejun Heo 	for (i = 0; i < NR_PORTS; i++)
4283e4ec344STejun Heo 		if (host_irq_stat & (HIRQ_PORT0 << i)) {
4293e4ec344STejun Heo 			inic_host_intr(host->ports[i]);
4301fd7a697STejun Heo 			handled++;
4311fd7a697STejun Heo 		}
4321fd7a697STejun Heo 
4331fd7a697STejun Heo 	spin_unlock(&host->lock);
4341fd7a697STejun Heo 
4351fd7a697STejun Heo  out:
4361fd7a697STejun Heo 	return IRQ_RETVAL(handled);
4371fd7a697STejun Heo }
4381fd7a697STejun Heo 
439b3f677e5STejun Heo static int inic_check_atapi_dma(struct ata_queued_cmd *qc)
440b3f677e5STejun Heo {
441b3f677e5STejun Heo 	/* For some reason ATAPI_PROT_DMA doesn't work for some
442b3f677e5STejun Heo 	 * commands including writes and other misc ops.  Use PIO
443b3f677e5STejun Heo 	 * protocol instead, which BTW is driven by the DMA engine
444b3f677e5STejun Heo 	 * anyway, so it shouldn't make much difference for native
445b3f677e5STejun Heo 	 * SATA devices.
446b3f677e5STejun Heo 	 */
447b3f677e5STejun Heo 	if (atapi_cmd_type(qc->cdb[0]) == READ)
448b3f677e5STejun Heo 		return 0;
449b3f677e5STejun Heo 	return 1;
450b3f677e5STejun Heo }
451b3f677e5STejun Heo 
4523ad400a9STejun Heo static void inic_fill_sg(struct inic_prd *prd, struct ata_queued_cmd *qc)
4533ad400a9STejun Heo {
4543ad400a9STejun Heo 	struct scatterlist *sg;
4553ad400a9STejun Heo 	unsigned int si;
456049e8e04STejun Heo 	u8 flags = 0;
4573ad400a9STejun Heo 
4583ad400a9STejun Heo 	if (qc->tf.flags & ATA_TFLAG_WRITE)
4593ad400a9STejun Heo 		flags |= PRD_WRITE;
4603ad400a9STejun Heo 
461049e8e04STejun Heo 	if (ata_is_dma(qc->tf.protocol))
462049e8e04STejun Heo 		flags |= PRD_DMA;
463049e8e04STejun Heo 
4643ad400a9STejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
4653ad400a9STejun Heo 		prd->mad = cpu_to_le32(sg_dma_address(sg));
4663ad400a9STejun Heo 		prd->len = cpu_to_le16(sg_dma_len(sg));
4673ad400a9STejun Heo 		prd->flags = flags;
4683ad400a9STejun Heo 		prd++;
4693ad400a9STejun Heo 	}
4703ad400a9STejun Heo 
4713ad400a9STejun Heo 	WARN_ON(!si);
4723ad400a9STejun Heo 	prd[-1].flags |= PRD_END;
4733ad400a9STejun Heo }
4743ad400a9STejun Heo 
4753ad400a9STejun Heo static void inic_qc_prep(struct ata_queued_cmd *qc)
4763ad400a9STejun Heo {
4773ad400a9STejun Heo 	struct inic_port_priv *pp = qc->ap->private_data;
4783ad400a9STejun Heo 	struct inic_pkt *pkt = pp->pkt;
4793ad400a9STejun Heo 	struct inic_cpb *cpb = &pkt->cpb;
4803ad400a9STejun Heo 	struct inic_prd *prd = pkt->prd;
481049e8e04STejun Heo 	bool is_atapi = ata_is_atapi(qc->tf.protocol);
482049e8e04STejun Heo 	bool is_data = ata_is_data(qc->tf.protocol);
483b3f677e5STejun Heo 	unsigned int cdb_len = 0;
4843ad400a9STejun Heo 
4853ad400a9STejun Heo 	VPRINTK("ENTER\n");
4863ad400a9STejun Heo 
487049e8e04STejun Heo 	if (is_atapi)
488b3f677e5STejun Heo 		cdb_len = qc->dev->cdb_len;
4893ad400a9STejun Heo 
4903ad400a9STejun Heo 	/* prepare packet, based on initio driver */
4913ad400a9STejun Heo 	memset(pkt, 0, sizeof(struct inic_pkt));
4923ad400a9STejun Heo 
493049e8e04STejun Heo 	cpb->ctl_flags = CPB_CTL_VALID | CPB_CTL_IEN;
494b3f677e5STejun Heo 	if (is_atapi || is_data)
495049e8e04STejun Heo 		cpb->ctl_flags |= CPB_CTL_DATA;
4963ad400a9STejun Heo 
497b3f677e5STejun Heo 	cpb->len = cpu_to_le32(qc->nbytes + cdb_len);
4983ad400a9STejun Heo 	cpb->prd = cpu_to_le32(pp->pkt_dma + offsetof(struct inic_pkt, prd));
4993ad400a9STejun Heo 
5003ad400a9STejun Heo 	cpb->device = qc->tf.device;
5013ad400a9STejun Heo 	cpb->feature = qc->tf.feature;
5023ad400a9STejun Heo 	cpb->nsect = qc->tf.nsect;
5033ad400a9STejun Heo 	cpb->lbal = qc->tf.lbal;
5043ad400a9STejun Heo 	cpb->lbam = qc->tf.lbam;
5053ad400a9STejun Heo 	cpb->lbah = qc->tf.lbah;
5063ad400a9STejun Heo 
5073ad400a9STejun Heo 	if (qc->tf.flags & ATA_TFLAG_LBA48) {
5083ad400a9STejun Heo 		cpb->hob_feature = qc->tf.hob_feature;
5093ad400a9STejun Heo 		cpb->hob_nsect = qc->tf.hob_nsect;
5103ad400a9STejun Heo 		cpb->hob_lbal = qc->tf.hob_lbal;
5113ad400a9STejun Heo 		cpb->hob_lbam = qc->tf.hob_lbam;
5123ad400a9STejun Heo 		cpb->hob_lbah = qc->tf.hob_lbah;
5133ad400a9STejun Heo 	}
5143ad400a9STejun Heo 
5153ad400a9STejun Heo 	cpb->command = qc->tf.command;
5163ad400a9STejun Heo 	/* don't load ctl - dunno why.  it's like that in the initio driver */
5173ad400a9STejun Heo 
518b3f677e5STejun Heo 	/* setup PRD for CDB */
519b3f677e5STejun Heo 	if (is_atapi) {
520b3f677e5STejun Heo 		memcpy(pkt->cdb, qc->cdb, ATAPI_CDB_LEN);
521b3f677e5STejun Heo 		prd->mad = cpu_to_le32(pp->pkt_dma +
522b3f677e5STejun Heo 				       offsetof(struct inic_pkt, cdb));
523b3f677e5STejun Heo 		prd->len = cpu_to_le16(cdb_len);
524b3f677e5STejun Heo 		prd->flags = PRD_CDB | PRD_WRITE;
525b3f677e5STejun Heo 		if (!is_data)
526b3f677e5STejun Heo 			prd->flags |= PRD_END;
527b3f677e5STejun Heo 		prd++;
528b3f677e5STejun Heo 	}
529b3f677e5STejun Heo 
5303ad400a9STejun Heo 	/* setup sg table */
531049e8e04STejun Heo 	if (is_data)
5323ad400a9STejun Heo 		inic_fill_sg(prd, qc);
5333ad400a9STejun Heo 
5343ad400a9STejun Heo 	pp->cpb_tbl[0] = pp->pkt_dma;
5353ad400a9STejun Heo }
5363ad400a9STejun Heo 
5371fd7a697STejun Heo static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
5381fd7a697STejun Heo {
5391fd7a697STejun Heo 	struct ata_port *ap = qc->ap;
5403ad400a9STejun Heo 	void __iomem *port_base = inic_port_base(ap);
5411fd7a697STejun Heo 
5423ad400a9STejun Heo 	/* fire up the ADMA engine */
54399580664SBob Stewart 	writew(HCTL_FTHD0 | HCTL_LEDEN, port_base + HOST_CTL);
5443ad400a9STejun Heo 	writew(IDMA_CTL_GO, port_base + PORT_IDMA_CTL);
5453ad400a9STejun Heo 	writeb(0, port_base + PORT_CPB_PTQFIFO);
5463ad400a9STejun Heo 
5473ad400a9STejun Heo 	return 0;
5483ad400a9STejun Heo }
5491fd7a697STejun Heo 
550364fac0eSTejun Heo static void inic_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
551364fac0eSTejun Heo {
552364fac0eSTejun Heo 	void __iomem *port_base = inic_port_base(ap);
553364fac0eSTejun Heo 
554364fac0eSTejun Heo 	tf->feature	= readb(port_base + PORT_TF_FEATURE);
555364fac0eSTejun Heo 	tf->nsect	= readb(port_base + PORT_TF_NSECT);
556364fac0eSTejun Heo 	tf->lbal	= readb(port_base + PORT_TF_LBAL);
557364fac0eSTejun Heo 	tf->lbam	= readb(port_base + PORT_TF_LBAM);
558364fac0eSTejun Heo 	tf->lbah	= readb(port_base + PORT_TF_LBAH);
559364fac0eSTejun Heo 	tf->device	= readb(port_base + PORT_TF_DEVICE);
560364fac0eSTejun Heo 	tf->command	= readb(port_base + PORT_TF_COMMAND);
561364fac0eSTejun Heo }
562364fac0eSTejun Heo 
563364fac0eSTejun Heo static bool inic_qc_fill_rtf(struct ata_queued_cmd *qc)
564364fac0eSTejun Heo {
565364fac0eSTejun Heo 	struct ata_taskfile *rtf = &qc->result_tf;
566364fac0eSTejun Heo 	struct ata_taskfile tf;
567364fac0eSTejun Heo 
568364fac0eSTejun Heo 	/* FIXME: Except for status and error, result TF access
569364fac0eSTejun Heo 	 * doesn't work.  I tried reading from BAR0/2, CPB and BAR5.
570364fac0eSTejun Heo 	 * None works regardless of which command interface is used.
571364fac0eSTejun Heo 	 * For now return true iff status indicates device error.
572364fac0eSTejun Heo 	 * This means that we're reporting bogus sector for RW
573364fac0eSTejun Heo 	 * failures.  Eeekk....
574364fac0eSTejun Heo 	 */
575364fac0eSTejun Heo 	inic_tf_read(qc->ap, &tf);
576364fac0eSTejun Heo 
577364fac0eSTejun Heo 	if (!(tf.command & ATA_ERR))
578364fac0eSTejun Heo 		return false;
579364fac0eSTejun Heo 
580364fac0eSTejun Heo 	rtf->command = tf.command;
581364fac0eSTejun Heo 	rtf->feature = tf.feature;
582364fac0eSTejun Heo 	return true;
583364fac0eSTejun Heo }
584364fac0eSTejun Heo 
5851fd7a697STejun Heo static void inic_freeze(struct ata_port *ap)
5861fd7a697STejun Heo {
5871fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
5881fd7a697STejun Heo 
589ab5b0235STejun Heo 	writeb(PIRQ_MASK_FREEZE, port_base + PORT_IRQ_MASK);
5901fd7a697STejun Heo 	writeb(0xff, port_base + PORT_IRQ_STAT);
5911fd7a697STejun Heo }
5921fd7a697STejun Heo 
5931fd7a697STejun Heo static void inic_thaw(struct ata_port *ap)
5941fd7a697STejun Heo {
5951fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
5961fd7a697STejun Heo 
5971fd7a697STejun Heo 	writeb(0xff, port_base + PORT_IRQ_STAT);
598ab5b0235STejun Heo 	writeb(PIRQ_MASK_DEFAULT, port_base + PORT_IRQ_MASK);
5991fd7a697STejun Heo }
6001fd7a697STejun Heo 
601364fac0eSTejun Heo static int inic_check_ready(struct ata_link *link)
602364fac0eSTejun Heo {
603364fac0eSTejun Heo 	void __iomem *port_base = inic_port_base(link->ap);
604364fac0eSTejun Heo 
605364fac0eSTejun Heo 	return ata_check_ready(readb(port_base + PORT_TF_COMMAND));
606364fac0eSTejun Heo }
607364fac0eSTejun Heo 
6081fd7a697STejun Heo /*
6091fd7a697STejun Heo  * SRST and SControl hardreset don't give valid signature on this
6101fd7a697STejun Heo  * controller.  Only controller specific hardreset mechanism works.
6111fd7a697STejun Heo  */
612cc0680a5STejun Heo static int inic_hardreset(struct ata_link *link, unsigned int *class,
613d4b2bab4STejun Heo 			  unsigned long deadline)
6141fd7a697STejun Heo {
615cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
6161fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6171fd7a697STejun Heo 	void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
618cc0680a5STejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
6191fd7a697STejun Heo 	int rc;
6201fd7a697STejun Heo 
6211fd7a697STejun Heo 	/* hammer it into sane state */
6221fd7a697STejun Heo 	inic_reset_port(port_base);
6231fd7a697STejun Heo 
624f8b0685aSTejun Heo 	writew(IDMA_CTL_RST_ATA, idma_ctl);
6251fd7a697STejun Heo 	readw(idma_ctl);	/* flush */
62697750cebSTejun Heo 	ata_msleep(ap, 1);
627f8b0685aSTejun Heo 	writew(0, idma_ctl);
6281fd7a697STejun Heo 
629cc0680a5STejun Heo 	rc = sata_link_resume(link, timing, deadline);
6301fd7a697STejun Heo 	if (rc) {
631a9a79dfeSJoe Perches 		ata_link_warn(link,
632a9a79dfeSJoe Perches 			      "failed to resume link after reset (errno=%d)\n",
633a9a79dfeSJoe Perches 			      rc);
6341fd7a697STejun Heo 		return rc;
6351fd7a697STejun Heo 	}
6361fd7a697STejun Heo 
6371fd7a697STejun Heo 	*class = ATA_DEV_NONE;
638cc0680a5STejun Heo 	if (ata_link_online(link)) {
6391fd7a697STejun Heo 		struct ata_taskfile tf;
6401fd7a697STejun Heo 
641705e76beSTejun Heo 		/* wait for link to become ready */
642364fac0eSTejun Heo 		rc = ata_wait_after_reset(link, deadline, inic_check_ready);
6439b89391cSTejun Heo 		/* link occupied, -ENODEV too is an error */
6449b89391cSTejun Heo 		if (rc) {
645a9a79dfeSJoe Perches 			ata_link_warn(link,
646a9a79dfeSJoe Perches 				      "device not ready after hardreset (errno=%d)\n",
647a9a79dfeSJoe Perches 				      rc);
648d4b2bab4STejun Heo 			return rc;
6491fd7a697STejun Heo 		}
6501fd7a697STejun Heo 
651364fac0eSTejun Heo 		inic_tf_read(ap, &tf);
6521fd7a697STejun Heo 		*class = ata_dev_classify(&tf);
6531fd7a697STejun Heo 	}
6541fd7a697STejun Heo 
6551fd7a697STejun Heo 	return 0;
6561fd7a697STejun Heo }
6571fd7a697STejun Heo 
6581fd7a697STejun Heo static void inic_error_handler(struct ata_port *ap)
6591fd7a697STejun Heo {
6601fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6611fd7a697STejun Heo 
6621fd7a697STejun Heo 	inic_reset_port(port_base);
663a1efdabaSTejun Heo 	ata_std_error_handler(ap);
6641fd7a697STejun Heo }
6651fd7a697STejun Heo 
6661fd7a697STejun Heo static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
6671fd7a697STejun Heo {
6681fd7a697STejun Heo 	/* make DMA engine forget about the failed command */
669a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED)
6701fd7a697STejun Heo 		inic_reset_port(inic_port_base(qc->ap));
6711fd7a697STejun Heo }
6721fd7a697STejun Heo 
6731fd7a697STejun Heo static void init_port(struct ata_port *ap)
6741fd7a697STejun Heo {
6751fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6763ad400a9STejun Heo 	struct inic_port_priv *pp = ap->private_data;
6771fd7a697STejun Heo 
6783ad400a9STejun Heo 	/* clear packet and CPB table */
6793ad400a9STejun Heo 	memset(pp->pkt, 0, sizeof(struct inic_pkt));
6803ad400a9STejun Heo 	memset(pp->cpb_tbl, 0, IDMA_CPB_TBL_SIZE);
6813ad400a9STejun Heo 
6826bc0d390STejun Heo 	/* setup CPB lookup table addresses */
6833ad400a9STejun Heo 	writel(pp->cpb_tbl_dma, port_base + PORT_CPB_CPBLAR);
6841fd7a697STejun Heo }
6851fd7a697STejun Heo 
6861fd7a697STejun Heo static int inic_port_resume(struct ata_port *ap)
6871fd7a697STejun Heo {
6881fd7a697STejun Heo 	init_port(ap);
6891fd7a697STejun Heo 	return 0;
6901fd7a697STejun Heo }
6911fd7a697STejun Heo 
6921fd7a697STejun Heo static int inic_port_start(struct ata_port *ap)
6931fd7a697STejun Heo {
6943ad400a9STejun Heo 	struct device *dev = ap->host->dev;
6951fd7a697STejun Heo 	struct inic_port_priv *pp;
6961fd7a697STejun Heo 
6971fd7a697STejun Heo 	/* alloc and initialize private data */
6983ad400a9STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
6991fd7a697STejun Heo 	if (!pp)
7001fd7a697STejun Heo 		return -ENOMEM;
7011fd7a697STejun Heo 	ap->private_data = pp;
7021fd7a697STejun Heo 
7031fd7a697STejun Heo 	/* Alloc resources */
7043ad400a9STejun Heo 	pp->pkt = dmam_alloc_coherent(dev, sizeof(struct inic_pkt),
7053ad400a9STejun Heo 				      &pp->pkt_dma, GFP_KERNEL);
7063ad400a9STejun Heo 	if (!pp->pkt)
7073ad400a9STejun Heo 		return -ENOMEM;
7083ad400a9STejun Heo 
7093ad400a9STejun Heo 	pp->cpb_tbl = dmam_alloc_coherent(dev, IDMA_CPB_TBL_SIZE,
7103ad400a9STejun Heo 					  &pp->cpb_tbl_dma, GFP_KERNEL);
7113ad400a9STejun Heo 	if (!pp->cpb_tbl)
7123ad400a9STejun Heo 		return -ENOMEM;
7133ad400a9STejun Heo 
7141fd7a697STejun Heo 	init_port(ap);
7151fd7a697STejun Heo 
7161fd7a697STejun Heo 	return 0;
7171fd7a697STejun Heo }
7181fd7a697STejun Heo 
7191fd7a697STejun Heo static struct ata_port_operations inic_port_ops = {
720f8b0685aSTejun Heo 	.inherits		= &sata_port_ops,
7211fd7a697STejun Heo 
722b3f677e5STejun Heo 	.check_atapi_dma	= inic_check_atapi_dma,
7233ad400a9STejun Heo 	.qc_prep		= inic_qc_prep,
7241fd7a697STejun Heo 	.qc_issue		= inic_qc_issue,
725364fac0eSTejun Heo 	.qc_fill_rtf		= inic_qc_fill_rtf,
7261fd7a697STejun Heo 
7271fd7a697STejun Heo 	.freeze			= inic_freeze,
7281fd7a697STejun Heo 	.thaw			= inic_thaw,
729a1efdabaSTejun Heo 	.hardreset		= inic_hardreset,
7301fd7a697STejun Heo 	.error_handler		= inic_error_handler,
7311fd7a697STejun Heo 	.post_internal_cmd	= inic_post_internal_cmd,
7321fd7a697STejun Heo 
733029cfd6bSTejun Heo 	.scr_read		= inic_scr_read,
734029cfd6bSTejun Heo 	.scr_write		= inic_scr_write,
7351fd7a697STejun Heo 
736029cfd6bSTejun Heo 	.port_resume		= inic_port_resume,
7371fd7a697STejun Heo 	.port_start		= inic_port_start,
7381fd7a697STejun Heo };
7391fd7a697STejun Heo 
740*f356b082SBhumika Goyal static const struct ata_port_info inic_port_info = {
7411fd7a697STejun Heo 	.flags			= ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
74214bdef98SErik Inge Bolsø 	.pio_mask		= ATA_PIO4,
74314bdef98SErik Inge Bolsø 	.mwdma_mask		= ATA_MWDMA2,
744bf6263a8SJeff Garzik 	.udma_mask		= ATA_UDMA6,
7451fd7a697STejun Heo 	.port_ops		= &inic_port_ops
7461fd7a697STejun Heo };
7471fd7a697STejun Heo 
7481fd7a697STejun Heo static int init_controller(void __iomem *mmio_base, u16 hctl)
7491fd7a697STejun Heo {
7501fd7a697STejun Heo 	int i;
7511fd7a697STejun Heo 	u16 val;
7521fd7a697STejun Heo 
7531fd7a697STejun Heo 	hctl &= ~HCTL_KNOWN_BITS;
7541fd7a697STejun Heo 
7551fd7a697STejun Heo 	/* Soft reset whole controller.  Spec says reset duration is 3
7561fd7a697STejun Heo 	 * PCI clocks, be generous and give it 10ms.
7571fd7a697STejun Heo 	 */
7581fd7a697STejun Heo 	writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
7591fd7a697STejun Heo 	readw(mmio_base + HOST_CTL); /* flush */
7601fd7a697STejun Heo 
7611fd7a697STejun Heo 	for (i = 0; i < 10; i++) {
7621fd7a697STejun Heo 		msleep(1);
7631fd7a697STejun Heo 		val = readw(mmio_base + HOST_CTL);
7641fd7a697STejun Heo 		if (!(val & HCTL_SOFTRST))
7651fd7a697STejun Heo 			break;
7661fd7a697STejun Heo 	}
7671fd7a697STejun Heo 
7681fd7a697STejun Heo 	if (val & HCTL_SOFTRST)
7691fd7a697STejun Heo 		return -EIO;
7701fd7a697STejun Heo 
7711fd7a697STejun Heo 	/* mask all interrupts and reset ports */
7721fd7a697STejun Heo 	for (i = 0; i < NR_PORTS; i++) {
7731fd7a697STejun Heo 		void __iomem *port_base = mmio_base + i * PORT_SIZE;
7741fd7a697STejun Heo 
7751fd7a697STejun Heo 		writeb(0xff, port_base + PORT_IRQ_MASK);
7761fd7a697STejun Heo 		inic_reset_port(port_base);
7771fd7a697STejun Heo 	}
7781fd7a697STejun Heo 
7791fd7a697STejun Heo 	/* port IRQ is masked now, unmask global IRQ */
7801fd7a697STejun Heo 	writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
7811fd7a697STejun Heo 	val = readw(mmio_base + HOST_IRQ_MASK);
7821fd7a697STejun Heo 	val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
7831fd7a697STejun Heo 	writew(val, mmio_base + HOST_IRQ_MASK);
7841fd7a697STejun Heo 
7851fd7a697STejun Heo 	return 0;
7861fd7a697STejun Heo }
7871fd7a697STejun Heo 
78858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
7891fd7a697STejun Heo static int inic_pci_device_resume(struct pci_dev *pdev)
7901fd7a697STejun Heo {
7910a86e1c8SJingoo Han 	struct ata_host *host = pci_get_drvdata(pdev);
7921fd7a697STejun Heo 	struct inic_host_priv *hpriv = host->private_data;
7931fd7a697STejun Heo 	int rc;
7941fd7a697STejun Heo 
7955aea408dSDmitriy Monakhov 	rc = ata_pci_device_do_resume(pdev);
7965aea408dSDmitriy Monakhov 	if (rc)
7975aea408dSDmitriy Monakhov 		return rc;
7981fd7a697STejun Heo 
7991fd7a697STejun Heo 	if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
800ba66b242STejun Heo 		rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl);
8011fd7a697STejun Heo 		if (rc)
8021fd7a697STejun Heo 			return rc;
8031fd7a697STejun Heo 	}
8041fd7a697STejun Heo 
8051fd7a697STejun Heo 	ata_host_resume(host);
8061fd7a697STejun Heo 
8071fd7a697STejun Heo 	return 0;
8081fd7a697STejun Heo }
809438ac6d5STejun Heo #endif
8101fd7a697STejun Heo 
8111fd7a697STejun Heo static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8121fd7a697STejun Heo {
8134447d351STejun Heo 	const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
8144447d351STejun Heo 	struct ata_host *host;
8151fd7a697STejun Heo 	struct inic_host_priv *hpriv;
8160d5ff566STejun Heo 	void __iomem * const *iomap;
817ba66b242STejun Heo 	int mmio_bar;
8181fd7a697STejun Heo 	int i, rc;
8191fd7a697STejun Heo 
82006296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
8211fd7a697STejun Heo 
822bb969619STejun Heo 	dev_alert(&pdev->dev, "inic162x support is broken with common data corruption issues and will be disabled by default, contact linux-ide@vger.kernel.org if in production use\n");
823bb969619STejun Heo 
8244447d351STejun Heo 	/* alloc host */
8254447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
8264447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
8274447d351STejun Heo 	if (!host || !hpriv)
8284447d351STejun Heo 		return -ENOMEM;
8294447d351STejun Heo 
8304447d351STejun Heo 	host->private_data = hpriv;
8314447d351STejun Heo 
832ba66b242STejun Heo 	/* Acquire resources and fill host.  Note that PCI and cardbus
833ba66b242STejun Heo 	 * use different BARs.
834ba66b242STejun Heo 	 */
83524dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
8361fd7a697STejun Heo 	if (rc)
8371fd7a697STejun Heo 		return rc;
8381fd7a697STejun Heo 
839ba66b242STejun Heo 	if (pci_resource_flags(pdev, MMIO_BAR_PCI) & IORESOURCE_MEM)
840ba66b242STejun Heo 		mmio_bar = MMIO_BAR_PCI;
841ba66b242STejun Heo 	else
842ba66b242STejun Heo 		mmio_bar = MMIO_BAR_CARDBUS;
843ba66b242STejun Heo 
844ba66b242STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << mmio_bar, DRV_NAME);
8450d5ff566STejun Heo 	if (rc)
8460d5ff566STejun Heo 		return rc;
8474447d351STejun Heo 	host->iomap = iomap = pcim_iomap_table(pdev);
848ba66b242STejun Heo 	hpriv->mmio_base = iomap[mmio_bar];
849ba66b242STejun Heo 	hpriv->cached_hctl = readw(hpriv->mmio_base + HOST_CTL);
8504447d351STejun Heo 
8514447d351STejun Heo 	for (i = 0; i < NR_PORTS; i++) {
852cbcdd875STejun Heo 		struct ata_port *ap = host->ports[i];
853cbcdd875STejun Heo 
854ba66b242STejun Heo 		ata_port_pbar_desc(ap, mmio_bar, -1, "mmio");
855ba66b242STejun Heo 		ata_port_pbar_desc(ap, mmio_bar, i * PORT_SIZE, "port");
8564447d351STejun Heo 	}
8574447d351STejun Heo 
8581fd7a697STejun Heo 	/* Set dma_mask.  This devices doesn't support 64bit addressing. */
859c54c719bSQuentin Lambert 	rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
8601fd7a697STejun Heo 	if (rc) {
861a44fec1fSJoe Perches 		dev_err(&pdev->dev, "32-bit DMA enable failed\n");
86224dc5f33STejun Heo 		return rc;
8631fd7a697STejun Heo 	}
8641fd7a697STejun Heo 
865c54c719bSQuentin Lambert 	rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
8661fd7a697STejun Heo 	if (rc) {
867a44fec1fSJoe Perches 		dev_err(&pdev->dev, "32-bit consistent DMA enable failed\n");
86824dc5f33STejun Heo 		return rc;
8691fd7a697STejun Heo 	}
8701fd7a697STejun Heo 
871b7d8629fSFUJITA Tomonori 	/*
872b7d8629fSFUJITA Tomonori 	 * This controller is braindamaged.  dma_boundary is 0xffff
873b7d8629fSFUJITA Tomonori 	 * like others but it will lock up the whole machine HARD if
874b7d8629fSFUJITA Tomonori 	 * 65536 byte PRD entry is fed. Reduce maximum segment size.
875b7d8629fSFUJITA Tomonori 	 */
876b7d8629fSFUJITA Tomonori 	rc = pci_set_dma_max_seg_size(pdev, 65536 - 512);
877b7d8629fSFUJITA Tomonori 	if (rc) {
878a44fec1fSJoe Perches 		dev_err(&pdev->dev, "failed to set the maximum segment size\n");
879b7d8629fSFUJITA Tomonori 		return rc;
880b7d8629fSFUJITA Tomonori 	}
881b7d8629fSFUJITA Tomonori 
882ba66b242STejun Heo 	rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl);
8831fd7a697STejun Heo 	if (rc) {
884a44fec1fSJoe Perches 		dev_err(&pdev->dev, "failed to initialize controller\n");
88524dc5f33STejun Heo 		return rc;
8861fd7a697STejun Heo 	}
8871fd7a697STejun Heo 
8881fd7a697STejun Heo 	pci_set_master(pdev);
8894447d351STejun Heo 	return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
8904447d351STejun Heo 				 &inic_sht);
8911fd7a697STejun Heo }
8921fd7a697STejun Heo 
8931fd7a697STejun Heo static const struct pci_device_id inic_pci_tbl[] = {
8941fd7a697STejun Heo 	{ PCI_VDEVICE(INIT, 0x1622), },
8951fd7a697STejun Heo 	{ },
8961fd7a697STejun Heo };
8971fd7a697STejun Heo 
8981fd7a697STejun Heo static struct pci_driver inic_pci_driver = {
8991fd7a697STejun Heo 	.name 		= DRV_NAME,
9001fd7a697STejun Heo 	.id_table	= inic_pci_tbl,
90158eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
9021fd7a697STejun Heo 	.suspend	= ata_pci_device_suspend,
9031fd7a697STejun Heo 	.resume		= inic_pci_device_resume,
904438ac6d5STejun Heo #endif
9051fd7a697STejun Heo 	.probe 		= inic_init_one,
9061fd7a697STejun Heo 	.remove		= ata_pci_remove_one,
9071fd7a697STejun Heo };
9081fd7a697STejun Heo 
9092fc75da0SAxel Lin module_pci_driver(inic_pci_driver);
9101fd7a697STejun Heo 
9111fd7a697STejun Heo MODULE_AUTHOR("Tejun Heo");
9121fd7a697STejun Heo MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
9131fd7a697STejun Heo MODULE_LICENSE("GPL v2");
9141fd7a697STejun Heo MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
9151fd7a697STejun Heo MODULE_VERSION(DRV_VERSION);
916