xref: /openbmc/linux/drivers/ata/sata_inic162x.c (revision c900529f3d9161bfde5cca0754f83b4d3c3e0220)
159bd9dedSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
21fd7a697STejun Heo /*
31fd7a697STejun Heo  * sata_inic162x.c - Driver for Initio 162x SATA controllers
41fd7a697STejun Heo  *
51fd7a697STejun Heo  * Copyright 2006  SUSE Linux Products GmbH
61fd7a697STejun Heo  * Copyright 2006  Tejun Heo <teheo@novell.com>
71fd7a697STejun Heo  *
8bb969619STejun Heo  * **** WARNING ****
9bb969619STejun Heo  *
10bb969619STejun Heo  * This driver never worked properly and unfortunately data corruption is
11bb969619STejun Heo  * relatively common.  There isn't anyone working on the driver and there's
12bb969619STejun Heo  * no support from the vendor.  Do not use this driver in any production
13bb969619STejun Heo  * environment.
14bb969619STejun Heo  *
15bb969619STejun Heo  * http://thread.gmane.org/gmane.linux.debian.devel.bugs.rc/378525/focus=54491
16bb969619STejun Heo  * https://bugzilla.kernel.org/show_bug.cgi?id=60565
17bb969619STejun Heo  *
18bb969619STejun Heo  * *****************
19bb969619STejun Heo  *
201fd7a697STejun Heo  * This controller is eccentric and easily locks up if something isn't
211fd7a697STejun Heo  * right.  Documentation is available at initio's website but it only
221fd7a697STejun Heo  * documents registers (not programming model).
231fd7a697STejun Heo  *
2422bfc6d5STejun Heo  * This driver has interesting history.  The first version was written
2522bfc6d5STejun Heo  * from the documentation and a 2.4 IDE driver posted on a Taiwan
2622bfc6d5STejun Heo  * company, which didn't use any IDMA features and couldn't handle
2722bfc6d5STejun Heo  * LBA48.  The resulting driver couldn't handle LBA48 devices either
2822bfc6d5STejun Heo  * making it pretty useless.
2922bfc6d5STejun Heo  *
3022bfc6d5STejun Heo  * After a while, initio picked the driver up, renamed it to
3122bfc6d5STejun Heo  * sata_initio162x, updated it to use IDMA for ATA DMA commands and
3222bfc6d5STejun Heo  * posted it on their website.  It only used ATA_PROT_DMA for IDMA and
3322bfc6d5STejun Heo  * attaching both devices and issuing IDMA and !IDMA commands
3422bfc6d5STejun Heo  * simultaneously broke it due to PIRQ masking interaction but it did
3522bfc6d5STejun Heo  * show how to use the IDMA (ADMA + some initio specific twists)
3622bfc6d5STejun Heo  * engine.
3722bfc6d5STejun Heo  *
3822bfc6d5STejun Heo  * Then, I picked up their changes again and here's the usable driver
3922bfc6d5STejun Heo  * which uses IDMA for everything.  Everything works now including
4022bfc6d5STejun Heo  * LBA48, CD/DVD burning, suspend/resume and hotplug.  There are some
4122bfc6d5STejun Heo  * issues tho.  Result Tf is not resported properly, NCQ isn't
4222bfc6d5STejun Heo  * supported yet and CD/DVD writing works with DMA assisted PIO
4322bfc6d5STejun Heo  * protocol (which, for native SATA devices, shouldn't cause any
4422bfc6d5STejun Heo  * noticeable difference).
4522bfc6d5STejun Heo  *
4622bfc6d5STejun Heo  * Anyways, so, here's finally a working driver for inic162x.  Enjoy!
4722bfc6d5STejun Heo  *
4822bfc6d5STejun Heo  * initio: If you guys wanna improve the driver regarding result TF
4922bfc6d5STejun Heo  * access and other stuff, please feel free to contact me.  I'll be
5022bfc6d5STejun Heo  * happy to assist.
511fd7a697STejun Heo  */
521fd7a697STejun Heo 
535a0e3ad6STejun Heo #include <linux/gfp.h>
541fd7a697STejun Heo #include <linux/kernel.h>
551fd7a697STejun Heo #include <linux/module.h>
561fd7a697STejun Heo #include <linux/pci.h>
571fd7a697STejun Heo #include <scsi/scsi_host.h>
581fd7a697STejun Heo #include <linux/libata.h>
591fd7a697STejun Heo #include <linux/blkdev.h>
601fd7a697STejun Heo #include <scsi/scsi_device.h>
611fd7a697STejun Heo 
621fd7a697STejun Heo #define DRV_NAME	"sata_inic162x"
6322bfc6d5STejun Heo #define DRV_VERSION	"0.4"
641fd7a697STejun Heo 
651fd7a697STejun Heo enum {
66ba66b242STejun Heo 	MMIO_BAR_PCI		= 5,
67ba66b242STejun Heo 	MMIO_BAR_CARDBUS	= 1,
681fd7a697STejun Heo 
691fd7a697STejun Heo 	NR_PORTS		= 2,
701fd7a697STejun Heo 
713ad400a9STejun Heo 	IDMA_CPB_TBL_SIZE	= 4 * 32,
723ad400a9STejun Heo 
733ad400a9STejun Heo 	INIC_DMA_BOUNDARY	= 0xffffff,
743ad400a9STejun Heo 
75b0dd9b8eSTejun Heo 	HOST_ACTRL		= 0x08,
761fd7a697STejun Heo 	HOST_CTL		= 0x7c,
771fd7a697STejun Heo 	HOST_STAT		= 0x7e,
781fd7a697STejun Heo 	HOST_IRQ_STAT		= 0xbc,
791fd7a697STejun Heo 	HOST_IRQ_MASK		= 0xbe,
801fd7a697STejun Heo 
811fd7a697STejun Heo 	PORT_SIZE		= 0x40,
821fd7a697STejun Heo 
831fd7a697STejun Heo 	/* registers for ATA TF operation */
84b0dd9b8eSTejun Heo 	PORT_TF_DATA		= 0x00,
85b0dd9b8eSTejun Heo 	PORT_TF_FEATURE		= 0x01,
86b0dd9b8eSTejun Heo 	PORT_TF_NSECT		= 0x02,
87b0dd9b8eSTejun Heo 	PORT_TF_LBAL		= 0x03,
88b0dd9b8eSTejun Heo 	PORT_TF_LBAM		= 0x04,
89b0dd9b8eSTejun Heo 	PORT_TF_LBAH		= 0x05,
90b0dd9b8eSTejun Heo 	PORT_TF_DEVICE		= 0x06,
91b0dd9b8eSTejun Heo 	PORT_TF_COMMAND		= 0x07,
92b0dd9b8eSTejun Heo 	PORT_TF_ALT_STAT	= 0x08,
931fd7a697STejun Heo 	PORT_IRQ_STAT		= 0x09,
941fd7a697STejun Heo 	PORT_IRQ_MASK		= 0x0a,
951fd7a697STejun Heo 	PORT_PRD_CTL		= 0x0b,
961fd7a697STejun Heo 	PORT_PRD_ADDR		= 0x0c,
971fd7a697STejun Heo 	PORT_PRD_XFERLEN	= 0x10,
98b0dd9b8eSTejun Heo 	PORT_CPB_CPBLAR		= 0x18,
99b0dd9b8eSTejun Heo 	PORT_CPB_PTQFIFO	= 0x1c,
1001fd7a697STejun Heo 
1011fd7a697STejun Heo 	/* IDMA register */
1021fd7a697STejun Heo 	PORT_IDMA_CTL		= 0x14,
103b0dd9b8eSTejun Heo 	PORT_IDMA_STAT		= 0x16,
104b0dd9b8eSTejun Heo 
105b0dd9b8eSTejun Heo 	PORT_RPQ_FIFO		= 0x1e,
106b0dd9b8eSTejun Heo 	PORT_RPQ_CNT		= 0x1f,
1071fd7a697STejun Heo 
1081fd7a697STejun Heo 	PORT_SCR		= 0x20,
1091fd7a697STejun Heo 
1101fd7a697STejun Heo 	/* HOST_CTL bits */
11199580664SBob Stewart 	HCTL_LEDEN		= (1 << 3),  /* enable LED operation */
1121fd7a697STejun Heo 	HCTL_IRQOFF		= (1 << 8),  /* global IRQ off */
113b0dd9b8eSTejun Heo 	HCTL_FTHD0		= (1 << 10), /* fifo threshold 0 */
114b0dd9b8eSTejun Heo 	HCTL_FTHD1		= (1 << 11), /* fifo threshold 1*/
115b0dd9b8eSTejun Heo 	HCTL_PWRDWN		= (1 << 12), /* power down PHYs */
1161fd7a697STejun Heo 	HCTL_SOFTRST		= (1 << 13), /* global reset (no phy reset) */
1171fd7a697STejun Heo 	HCTL_RPGSEL		= (1 << 15), /* register page select */
1181fd7a697STejun Heo 
1191fd7a697STejun Heo 	HCTL_KNOWN_BITS		= HCTL_IRQOFF | HCTL_PWRDWN | HCTL_SOFTRST |
1201fd7a697STejun Heo 				  HCTL_RPGSEL,
1211fd7a697STejun Heo 
1221fd7a697STejun Heo 	/* HOST_IRQ_(STAT|MASK) bits */
1231fd7a697STejun Heo 	HIRQ_PORT0		= (1 << 0),
1241fd7a697STejun Heo 	HIRQ_PORT1		= (1 << 1),
1251fd7a697STejun Heo 	HIRQ_SOFT		= (1 << 14),
1261fd7a697STejun Heo 	HIRQ_GLOBAL		= (1 << 15), /* STAT only */
1271fd7a697STejun Heo 
1281fd7a697STejun Heo 	/* PORT_IRQ_(STAT|MASK) bits */
1291fd7a697STejun Heo 	PIRQ_OFFLINE		= (1 << 0),  /* device unplugged */
1301fd7a697STejun Heo 	PIRQ_ONLINE		= (1 << 1),  /* device plugged */
1311fd7a697STejun Heo 	PIRQ_COMPLETE		= (1 << 2),  /* completion interrupt */
1321fd7a697STejun Heo 	PIRQ_FATAL		= (1 << 3),  /* fatal error */
1331fd7a697STejun Heo 	PIRQ_ATA		= (1 << 4),  /* ATA interrupt */
1341fd7a697STejun Heo 	PIRQ_REPLY		= (1 << 5),  /* reply FIFO not empty */
1351fd7a697STejun Heo 	PIRQ_PENDING		= (1 << 7),  /* port IRQ pending (STAT only) */
1361fd7a697STejun Heo 
1371fd7a697STejun Heo 	PIRQ_ERR		= PIRQ_OFFLINE | PIRQ_ONLINE | PIRQ_FATAL,
138f8b0685aSTejun Heo 	PIRQ_MASK_DEFAULT	= PIRQ_REPLY | PIRQ_ATA,
1391fd7a697STejun Heo 	PIRQ_MASK_FREEZE	= 0xff,
1401fd7a697STejun Heo 
1411fd7a697STejun Heo 	/* PORT_PRD_CTL bits */
1421fd7a697STejun Heo 	PRD_CTL_START		= (1 << 0),
1431fd7a697STejun Heo 	PRD_CTL_WR		= (1 << 3),
1441fd7a697STejun Heo 	PRD_CTL_DMAEN		= (1 << 7),  /* DMA enable */
1451fd7a697STejun Heo 
1461fd7a697STejun Heo 	/* PORT_IDMA_CTL bits */
1471fd7a697STejun Heo 	IDMA_CTL_RST_ATA	= (1 << 2),  /* hardreset ATA bus */
14889e7c850SJohn Oldman 	IDMA_CTL_RST_IDMA	= (1 << 5),  /* reset IDMA machinery */
1491fd7a697STejun Heo 	IDMA_CTL_GO		= (1 << 7),  /* IDMA mode go */
1501fd7a697STejun Heo 	IDMA_CTL_ATA_NIEN	= (1 << 8),  /* ATA IRQ disable */
151b0dd9b8eSTejun Heo 
152b0dd9b8eSTejun Heo 	/* PORT_IDMA_STAT bits */
153b0dd9b8eSTejun Heo 	IDMA_STAT_PERR		= (1 << 0),  /* PCI ERROR MODE */
154b0dd9b8eSTejun Heo 	IDMA_STAT_CPBERR	= (1 << 1),  /* ADMA CPB error */
155b0dd9b8eSTejun Heo 	IDMA_STAT_LGCY		= (1 << 3),  /* ADMA legacy */
156b0dd9b8eSTejun Heo 	IDMA_STAT_UIRQ		= (1 << 4),  /* ADMA unsolicited irq */
157b0dd9b8eSTejun Heo 	IDMA_STAT_STPD		= (1 << 5),  /* ADMA stopped */
158b0dd9b8eSTejun Heo 	IDMA_STAT_PSD		= (1 << 6),  /* ADMA pause */
159b0dd9b8eSTejun Heo 	IDMA_STAT_DONE		= (1 << 7),  /* ADMA done */
160b0dd9b8eSTejun Heo 
161b0dd9b8eSTejun Heo 	IDMA_STAT_ERR		= IDMA_STAT_PERR | IDMA_STAT_CPBERR,
162b0dd9b8eSTejun Heo 
163b0dd9b8eSTejun Heo 	/* CPB Control Flags*/
164b0dd9b8eSTejun Heo 	CPB_CTL_VALID		= (1 << 0),  /* CPB valid */
165b0dd9b8eSTejun Heo 	CPB_CTL_QUEUED		= (1 << 1),  /* queued command */
166b0dd9b8eSTejun Heo 	CPB_CTL_DATA		= (1 << 2),  /* data, rsvd in datasheet */
167b0dd9b8eSTejun Heo 	CPB_CTL_IEN		= (1 << 3),  /* PCI interrupt enable */
168b0dd9b8eSTejun Heo 	CPB_CTL_DEVDIR		= (1 << 4),  /* device direction control */
169b0dd9b8eSTejun Heo 
170b0dd9b8eSTejun Heo 	/* CPB Response Flags */
171b0dd9b8eSTejun Heo 	CPB_RESP_DONE		= (1 << 0),  /* ATA command complete */
172b0dd9b8eSTejun Heo 	CPB_RESP_REL		= (1 << 1),  /* ATA release */
173b0dd9b8eSTejun Heo 	CPB_RESP_IGNORED	= (1 << 2),  /* CPB ignored */
174b0dd9b8eSTejun Heo 	CPB_RESP_ATA_ERR	= (1 << 3),  /* ATA command error */
175b0dd9b8eSTejun Heo 	CPB_RESP_SPURIOUS	= (1 << 4),  /* ATA spurious interrupt error */
176b0dd9b8eSTejun Heo 	CPB_RESP_UNDERFLOW	= (1 << 5),  /* APRD deficiency length error */
177b0dd9b8eSTejun Heo 	CPB_RESP_OVERFLOW	= (1 << 6),  /* APRD exccess length error */
178b0dd9b8eSTejun Heo 	CPB_RESP_CPB_ERR	= (1 << 7),  /* CPB error flag */
179b0dd9b8eSTejun Heo 
180b0dd9b8eSTejun Heo 	/* PRD Control Flags */
181b0dd9b8eSTejun Heo 	PRD_DRAIN		= (1 << 1),  /* ignore data excess */
182b0dd9b8eSTejun Heo 	PRD_CDB			= (1 << 2),  /* atapi packet command pointer */
183b0dd9b8eSTejun Heo 	PRD_DIRECT_INTR		= (1 << 3),  /* direct interrupt */
184b0dd9b8eSTejun Heo 	PRD_DMA			= (1 << 4),  /* data transfer method */
185b0dd9b8eSTejun Heo 	PRD_WRITE		= (1 << 5),  /* data dir, rsvd in datasheet */
186b0dd9b8eSTejun Heo 	PRD_IOM			= (1 << 6),  /* io/memory transfer */
187b0dd9b8eSTejun Heo 	PRD_END			= (1 << 7),  /* APRD chain end */
1881fd7a697STejun Heo };
1891fd7a697STejun Heo 
1903ad400a9STejun Heo /* Comman Parameter Block */
1913ad400a9STejun Heo struct inic_cpb {
1923ad400a9STejun Heo 	u8		resp_flags;	/* Response Flags */
1933ad400a9STejun Heo 	u8		error;		/* ATA Error */
1943ad400a9STejun Heo 	u8		status;		/* ATA Status */
1953ad400a9STejun Heo 	u8		ctl_flags;	/* Control Flags */
1963ad400a9STejun Heo 	__le32		len;		/* Total Transfer Length */
1973ad400a9STejun Heo 	__le32		prd;		/* First PRD pointer */
1983ad400a9STejun Heo 	u8		rsvd[4];
1993ad400a9STejun Heo 	/* 16 bytes */
2003ad400a9STejun Heo 	u8		feature;	/* ATA Feature */
2013ad400a9STejun Heo 	u8		hob_feature;	/* ATA Ex. Feature */
2023ad400a9STejun Heo 	u8		device;		/* ATA Device/Head */
2033ad400a9STejun Heo 	u8		mirctl;		/* Mirror Control */
2043ad400a9STejun Heo 	u8		nsect;		/* ATA Sector Count */
2053ad400a9STejun Heo 	u8		hob_nsect;	/* ATA Ex. Sector Count */
2063ad400a9STejun Heo 	u8		lbal;		/* ATA Sector Number */
2073ad400a9STejun Heo 	u8		hob_lbal;	/* ATA Ex. Sector Number */
2083ad400a9STejun Heo 	u8		lbam;		/* ATA Cylinder Low */
2093ad400a9STejun Heo 	u8		hob_lbam;	/* ATA Ex. Cylinder Low */
2103ad400a9STejun Heo 	u8		lbah;		/* ATA Cylinder High */
2113ad400a9STejun Heo 	u8		hob_lbah;	/* ATA Ex. Cylinder High */
2123ad400a9STejun Heo 	u8		command;	/* ATA Command */
2133ad400a9STejun Heo 	u8		ctl;		/* ATA Control */
2143ad400a9STejun Heo 	u8		slave_error;	/* Slave ATA Error */
2153ad400a9STejun Heo 	u8		slave_status;	/* Slave ATA Status */
2163ad400a9STejun Heo 	/* 32 bytes */
2173ad400a9STejun Heo } __packed;
2183ad400a9STejun Heo 
2193ad400a9STejun Heo /* Physical Region Descriptor */
2203ad400a9STejun Heo struct inic_prd {
2213ad400a9STejun Heo 	__le32		mad;		/* Physical Memory Address */
2223ad400a9STejun Heo 	__le16		len;		/* Transfer Length */
2233ad400a9STejun Heo 	u8		rsvd;
2243ad400a9STejun Heo 	u8		flags;		/* Control Flags */
2253ad400a9STejun Heo } __packed;
2263ad400a9STejun Heo 
2273ad400a9STejun Heo struct inic_pkt {
2283ad400a9STejun Heo 	struct inic_cpb	cpb;
229b3f677e5STejun Heo 	struct inic_prd	prd[LIBATA_MAX_PRD + 1];	/* + 1 for cdb */
230b3f677e5STejun Heo 	u8		cdb[ATAPI_CDB_LEN];
2313ad400a9STejun Heo } __packed;
2323ad400a9STejun Heo 
2331fd7a697STejun Heo struct inic_host_priv {
234ba66b242STejun Heo 	void __iomem	*mmio_base;
2351fd7a697STejun Heo 	u16		cached_hctl;
2361fd7a697STejun Heo };
2371fd7a697STejun Heo 
2381fd7a697STejun Heo struct inic_port_priv {
2393ad400a9STejun Heo 	struct inic_pkt	*pkt;
2403ad400a9STejun Heo 	dma_addr_t	pkt_dma;
2413ad400a9STejun Heo 	u32		*cpb_tbl;
2423ad400a9STejun Heo 	dma_addr_t	cpb_tbl_dma;
2431fd7a697STejun Heo };
2441fd7a697STejun Heo 
24525df73d9SBart Van Assche static const struct scsi_host_template inic_sht = {
246ab5b0235STejun Heo 	ATA_BASE_SHT(DRV_NAME),
247ab5b0235STejun Heo 	.sg_tablesize		= LIBATA_MAX_PRD, /* maybe it can be larger? */
248a8cf59a6SChristoph Hellwig 
249a8cf59a6SChristoph Hellwig 	/*
250a8cf59a6SChristoph Hellwig 	 * This controller is braindamaged.  dma_boundary is 0xffff like others
251a8cf59a6SChristoph Hellwig 	 * but it will lock up the whole machine HARD if 65536 byte PRD entry
252a8cf59a6SChristoph Hellwig 	 * is fed.  Reduce maximum segment size.
253a8cf59a6SChristoph Hellwig 	 */
2543ad400a9STejun Heo 	.dma_boundary		= INIC_DMA_BOUNDARY,
255a8cf59a6SChristoph Hellwig 	.max_segment_size	= 65536 - 512,
2561fd7a697STejun Heo };
2571fd7a697STejun Heo 
2581fd7a697STejun Heo static const int scr_map[] = {
2591fd7a697STejun Heo 	[SCR_STATUS]	= 0,
2601fd7a697STejun Heo 	[SCR_ERROR]	= 1,
2611fd7a697STejun Heo 	[SCR_CONTROL]	= 2,
2621fd7a697STejun Heo };
2631fd7a697STejun Heo 
inic_port_base(struct ata_port * ap)2641fd7a697STejun Heo static void __iomem *inic_port_base(struct ata_port *ap)
2651fd7a697STejun Heo {
266ba66b242STejun Heo 	struct inic_host_priv *hpriv = ap->host->private_data;
267ba66b242STejun Heo 
268ba66b242STejun Heo 	return hpriv->mmio_base + ap->port_no * PORT_SIZE;
2691fd7a697STejun Heo }
2701fd7a697STejun Heo 
inic_reset_port(void __iomem * port_base)2711fd7a697STejun Heo static void inic_reset_port(void __iomem *port_base)
2721fd7a697STejun Heo {
2731fd7a697STejun Heo 	void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
2741fd7a697STejun Heo 
275f8b0685aSTejun Heo 	/* stop IDMA engine */
276f8b0685aSTejun Heo 	readw(idma_ctl); /* flush */
277f8b0685aSTejun Heo 	msleep(1);
2781fd7a697STejun Heo 
2791fd7a697STejun Heo 	/* mask IRQ and assert reset */
280f8b0685aSTejun Heo 	writew(IDMA_CTL_RST_IDMA, idma_ctl);
2811fd7a697STejun Heo 	readw(idma_ctl); /* flush */
2821fd7a697STejun Heo 	msleep(1);
2831fd7a697STejun Heo 
2841fd7a697STejun Heo 	/* release reset */
285f8b0685aSTejun Heo 	writew(0, idma_ctl);
2861fd7a697STejun Heo 
2871fd7a697STejun Heo 	/* clear irq */
2881fd7a697STejun Heo 	writeb(0xff, port_base + PORT_IRQ_STAT);
2891fd7a697STejun Heo }
2901fd7a697STejun Heo 
inic_scr_read(struct ata_link * link,unsigned sc_reg,u32 * val)29182ef04fbSTejun Heo static int inic_scr_read(struct ata_link *link, unsigned sc_reg, u32 *val)
2921fd7a697STejun Heo {
29382ef04fbSTejun Heo 	void __iomem *scr_addr = inic_port_base(link->ap) + PORT_SCR;
2941fd7a697STejun Heo 
2951fd7a697STejun Heo 	if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
296da3dbb17STejun Heo 		return -EINVAL;
2971fd7a697STejun Heo 
298da3dbb17STejun Heo 	*val = readl(scr_addr + scr_map[sc_reg] * 4);
2991fd7a697STejun Heo 
3001fd7a697STejun Heo 	/* this controller has stuck DIAG.N, ignore it */
3011fd7a697STejun Heo 	if (sc_reg == SCR_ERROR)
302da3dbb17STejun Heo 		*val &= ~SERR_PHYRDY_CHG;
303da3dbb17STejun Heo 	return 0;
3041fd7a697STejun Heo }
3051fd7a697STejun Heo 
inic_scr_write(struct ata_link * link,unsigned sc_reg,u32 val)30682ef04fbSTejun Heo static int inic_scr_write(struct ata_link *link, unsigned sc_reg, u32 val)
3071fd7a697STejun Heo {
30882ef04fbSTejun Heo 	void __iomem *scr_addr = inic_port_base(link->ap) + PORT_SCR;
3091fd7a697STejun Heo 
3101fd7a697STejun Heo 	if (unlikely(sc_reg >= ARRAY_SIZE(scr_map)))
311da3dbb17STejun Heo 		return -EINVAL;
3121fd7a697STejun Heo 
3131fd7a697STejun Heo 	writel(val, scr_addr + scr_map[sc_reg] * 4);
314da3dbb17STejun Heo 	return 0;
3151fd7a697STejun Heo }
3161fd7a697STejun Heo 
inic_stop_idma(struct ata_port * ap)3173ad400a9STejun Heo static void inic_stop_idma(struct ata_port *ap)
3183ad400a9STejun Heo {
3193ad400a9STejun Heo 	void __iomem *port_base = inic_port_base(ap);
3203ad400a9STejun Heo 
3213ad400a9STejun Heo 	readb(port_base + PORT_RPQ_FIFO);
3223ad400a9STejun Heo 	readb(port_base + PORT_RPQ_CNT);
3233ad400a9STejun Heo 	writew(0, port_base + PORT_IDMA_CTL);
3243ad400a9STejun Heo }
3253ad400a9STejun Heo 
inic_host_err_intr(struct ata_port * ap,u8 irq_stat,u16 idma_stat)3263ad400a9STejun Heo static void inic_host_err_intr(struct ata_port *ap, u8 irq_stat, u16 idma_stat)
3273ad400a9STejun Heo {
3283ad400a9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
3293ad400a9STejun Heo 	struct inic_port_priv *pp = ap->private_data;
3303ad400a9STejun Heo 	struct inic_cpb *cpb = &pp->pkt->cpb;
3313ad400a9STejun Heo 	bool freeze = false;
3323ad400a9STejun Heo 
3333ad400a9STejun Heo 	ata_ehi_clear_desc(ehi);
3343ad400a9STejun Heo 	ata_ehi_push_desc(ehi, "irq_stat=0x%x idma_stat=0x%x",
3353ad400a9STejun Heo 			  irq_stat, idma_stat);
3363ad400a9STejun Heo 
3373ad400a9STejun Heo 	inic_stop_idma(ap);
3383ad400a9STejun Heo 
3393ad400a9STejun Heo 	if (irq_stat & (PIRQ_OFFLINE | PIRQ_ONLINE)) {
3403ad400a9STejun Heo 		ata_ehi_push_desc(ehi, "hotplug");
3413ad400a9STejun Heo 		ata_ehi_hotplugged(ehi);
3423ad400a9STejun Heo 		freeze = true;
3433ad400a9STejun Heo 	}
3443ad400a9STejun Heo 
3453ad400a9STejun Heo 	if (idma_stat & IDMA_STAT_PERR) {
3463ad400a9STejun Heo 		ata_ehi_push_desc(ehi, "PCI error");
3473ad400a9STejun Heo 		freeze = true;
3483ad400a9STejun Heo 	}
3493ad400a9STejun Heo 
3503ad400a9STejun Heo 	if (idma_stat & IDMA_STAT_CPBERR) {
3513ad400a9STejun Heo 		ata_ehi_push_desc(ehi, "CPB error");
3523ad400a9STejun Heo 
3533ad400a9STejun Heo 		if (cpb->resp_flags & CPB_RESP_IGNORED) {
3543ad400a9STejun Heo 			__ata_ehi_push_desc(ehi, " ignored");
3553ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_INVALID;
3563ad400a9STejun Heo 			freeze = true;
3573ad400a9STejun Heo 		}
3583ad400a9STejun Heo 
3593ad400a9STejun Heo 		if (cpb->resp_flags & CPB_RESP_ATA_ERR)
3603ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_DEV;
3613ad400a9STejun Heo 
3623ad400a9STejun Heo 		if (cpb->resp_flags & CPB_RESP_SPURIOUS) {
3633ad400a9STejun Heo 			__ata_ehi_push_desc(ehi, " spurious-intr");
3643ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_HSM;
3653ad400a9STejun Heo 			freeze = true;
3663ad400a9STejun Heo 		}
3673ad400a9STejun Heo 
3683ad400a9STejun Heo 		if (cpb->resp_flags &
3693ad400a9STejun Heo 		    (CPB_RESP_UNDERFLOW | CPB_RESP_OVERFLOW)) {
3703ad400a9STejun Heo 			__ata_ehi_push_desc(ehi, " data-over/underflow");
3713ad400a9STejun Heo 			ehi->err_mask |= AC_ERR_HSM;
3723ad400a9STejun Heo 			freeze = true;
3733ad400a9STejun Heo 		}
3743ad400a9STejun Heo 	}
3753ad400a9STejun Heo 
3763ad400a9STejun Heo 	if (freeze)
3773ad400a9STejun Heo 		ata_port_freeze(ap);
3783ad400a9STejun Heo 	else
3793ad400a9STejun Heo 		ata_port_abort(ap);
3803ad400a9STejun Heo }
3813ad400a9STejun Heo 
inic_host_intr(struct ata_port * ap)3821fd7a697STejun Heo static void inic_host_intr(struct ata_port *ap)
3831fd7a697STejun Heo {
3841fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
3853ad400a9STejun Heo 	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
3861fd7a697STejun Heo 	u8 irq_stat;
3873ad400a9STejun Heo 	u16 idma_stat;
3881fd7a697STejun Heo 
3893ad400a9STejun Heo 	/* read and clear IRQ status */
3901fd7a697STejun Heo 	irq_stat = readb(port_base + PORT_IRQ_STAT);
3911fd7a697STejun Heo 	writeb(irq_stat, port_base + PORT_IRQ_STAT);
3923ad400a9STejun Heo 	idma_stat = readw(port_base + PORT_IDMA_STAT);
3931fd7a697STejun Heo 
3943ad400a9STejun Heo 	if (unlikely((irq_stat & PIRQ_ERR) || (idma_stat & IDMA_STAT_ERR)))
3953ad400a9STejun Heo 		inic_host_err_intr(ap, irq_stat, idma_stat);
3961fd7a697STejun Heo 
397f8b0685aSTejun Heo 	if (unlikely(!qc))
3983ad400a9STejun Heo 		goto spurious;
3991fd7a697STejun Heo 
4003ad400a9STejun Heo 	if (likely(idma_stat & IDMA_STAT_DONE)) {
4013ad400a9STejun Heo 		inic_stop_idma(ap);
4023ad400a9STejun Heo 
4033ad400a9STejun Heo 		/* Depending on circumstances, device error
4043ad400a9STejun Heo 		 * isn't reported by IDMA, check it explicitly.
4053ad400a9STejun Heo 		 */
4063ad400a9STejun Heo 		if (unlikely(readb(port_base + PORT_TF_COMMAND) &
4073ad400a9STejun Heo 			     (ATA_DF | ATA_ERR)))
4083ad400a9STejun Heo 			qc->err_mask |= AC_ERR_DEV;
4093ad400a9STejun Heo 
4103ad400a9STejun Heo 		ata_qc_complete(qc);
4113ad400a9STejun Heo 		return;
4123ad400a9STejun Heo 	}
4131fd7a697STejun Heo 
4143ad400a9STejun Heo  spurious:
415a9a79dfeSJoe Perches 	ata_port_warn(ap, "unhandled interrupt: cmd=0x%x irq_stat=0x%x idma_stat=0x%x\n",
416f8b0685aSTejun Heo 		      qc ? qc->tf.command : 0xff, irq_stat, idma_stat);
4171fd7a697STejun Heo }
4181fd7a697STejun Heo 
inic_interrupt(int irq,void * dev_instance)4191fd7a697STejun Heo static irqreturn_t inic_interrupt(int irq, void *dev_instance)
4201fd7a697STejun Heo {
4211fd7a697STejun Heo 	struct ata_host *host = dev_instance;
422ba66b242STejun Heo 	struct inic_host_priv *hpriv = host->private_data;
4231fd7a697STejun Heo 	u16 host_irq_stat;
42487c8b22bSJoe Perches 	int i, handled = 0;
4251fd7a697STejun Heo 
426ba66b242STejun Heo 	host_irq_stat = readw(hpriv->mmio_base + HOST_IRQ_STAT);
4271fd7a697STejun Heo 
4281fd7a697STejun Heo 	if (unlikely(!(host_irq_stat & HIRQ_GLOBAL)))
4291fd7a697STejun Heo 		goto out;
4301fd7a697STejun Heo 
4311fd7a697STejun Heo 	spin_lock(&host->lock);
4321fd7a697STejun Heo 
4333e4ec344STejun Heo 	for (i = 0; i < NR_PORTS; i++)
4343e4ec344STejun Heo 		if (host_irq_stat & (HIRQ_PORT0 << i)) {
4353e4ec344STejun Heo 			inic_host_intr(host->ports[i]);
4361fd7a697STejun Heo 			handled++;
4371fd7a697STejun Heo 		}
4381fd7a697STejun Heo 
4391fd7a697STejun Heo 	spin_unlock(&host->lock);
4401fd7a697STejun Heo 
4411fd7a697STejun Heo  out:
4421fd7a697STejun Heo 	return IRQ_RETVAL(handled);
4431fd7a697STejun Heo }
4441fd7a697STejun Heo 
inic_check_atapi_dma(struct ata_queued_cmd * qc)445b3f677e5STejun Heo static int inic_check_atapi_dma(struct ata_queued_cmd *qc)
446b3f677e5STejun Heo {
447b3f677e5STejun Heo 	/* For some reason ATAPI_PROT_DMA doesn't work for some
448b3f677e5STejun Heo 	 * commands including writes and other misc ops.  Use PIO
449b3f677e5STejun Heo 	 * protocol instead, which BTW is driven by the DMA engine
450b3f677e5STejun Heo 	 * anyway, so it shouldn't make much difference for native
451b3f677e5STejun Heo 	 * SATA devices.
452b3f677e5STejun Heo 	 */
453b3f677e5STejun Heo 	if (atapi_cmd_type(qc->cdb[0]) == READ)
454b3f677e5STejun Heo 		return 0;
455b3f677e5STejun Heo 	return 1;
456b3f677e5STejun Heo }
457b3f677e5STejun Heo 
inic_fill_sg(struct inic_prd * prd,struct ata_queued_cmd * qc)4583ad400a9STejun Heo static void inic_fill_sg(struct inic_prd *prd, struct ata_queued_cmd *qc)
4593ad400a9STejun Heo {
4603ad400a9STejun Heo 	struct scatterlist *sg;
4613ad400a9STejun Heo 	unsigned int si;
462049e8e04STejun Heo 	u8 flags = 0;
4633ad400a9STejun Heo 
4643ad400a9STejun Heo 	if (qc->tf.flags & ATA_TFLAG_WRITE)
4653ad400a9STejun Heo 		flags |= PRD_WRITE;
4663ad400a9STejun Heo 
467049e8e04STejun Heo 	if (ata_is_dma(qc->tf.protocol))
468049e8e04STejun Heo 		flags |= PRD_DMA;
469049e8e04STejun Heo 
4703ad400a9STejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
4713ad400a9STejun Heo 		prd->mad = cpu_to_le32(sg_dma_address(sg));
4723ad400a9STejun Heo 		prd->len = cpu_to_le16(sg_dma_len(sg));
4733ad400a9STejun Heo 		prd->flags = flags;
4743ad400a9STejun Heo 		prd++;
4753ad400a9STejun Heo 	}
4763ad400a9STejun Heo 
4773ad400a9STejun Heo 	WARN_ON(!si);
4783ad400a9STejun Heo 	prd[-1].flags |= PRD_END;
4793ad400a9STejun Heo }
4803ad400a9STejun Heo 
inic_qc_prep(struct ata_queued_cmd * qc)48195364f36SJiri Slaby static enum ata_completion_errors inic_qc_prep(struct ata_queued_cmd *qc)
4823ad400a9STejun Heo {
4833ad400a9STejun Heo 	struct inic_port_priv *pp = qc->ap->private_data;
4843ad400a9STejun Heo 	struct inic_pkt *pkt = pp->pkt;
4853ad400a9STejun Heo 	struct inic_cpb *cpb = &pkt->cpb;
4863ad400a9STejun Heo 	struct inic_prd *prd = pkt->prd;
487049e8e04STejun Heo 	bool is_atapi = ata_is_atapi(qc->tf.protocol);
488049e8e04STejun Heo 	bool is_data = ata_is_data(qc->tf.protocol);
489b3f677e5STejun Heo 	unsigned int cdb_len = 0;
4903ad400a9STejun Heo 
491049e8e04STejun Heo 	if (is_atapi)
492b3f677e5STejun Heo 		cdb_len = qc->dev->cdb_len;
4933ad400a9STejun Heo 
4943ad400a9STejun Heo 	/* prepare packet, based on initio driver */
4953ad400a9STejun Heo 	memset(pkt, 0, sizeof(struct inic_pkt));
4963ad400a9STejun Heo 
497049e8e04STejun Heo 	cpb->ctl_flags = CPB_CTL_VALID | CPB_CTL_IEN;
498b3f677e5STejun Heo 	if (is_atapi || is_data)
499049e8e04STejun Heo 		cpb->ctl_flags |= CPB_CTL_DATA;
5003ad400a9STejun Heo 
501b3f677e5STejun Heo 	cpb->len = cpu_to_le32(qc->nbytes + cdb_len);
5023ad400a9STejun Heo 	cpb->prd = cpu_to_le32(pp->pkt_dma + offsetof(struct inic_pkt, prd));
5033ad400a9STejun Heo 
5043ad400a9STejun Heo 	cpb->device = qc->tf.device;
5053ad400a9STejun Heo 	cpb->feature = qc->tf.feature;
5063ad400a9STejun Heo 	cpb->nsect = qc->tf.nsect;
5073ad400a9STejun Heo 	cpb->lbal = qc->tf.lbal;
5083ad400a9STejun Heo 	cpb->lbam = qc->tf.lbam;
5093ad400a9STejun Heo 	cpb->lbah = qc->tf.lbah;
5103ad400a9STejun Heo 
5113ad400a9STejun Heo 	if (qc->tf.flags & ATA_TFLAG_LBA48) {
5123ad400a9STejun Heo 		cpb->hob_feature = qc->tf.hob_feature;
5133ad400a9STejun Heo 		cpb->hob_nsect = qc->tf.hob_nsect;
5143ad400a9STejun Heo 		cpb->hob_lbal = qc->tf.hob_lbal;
5153ad400a9STejun Heo 		cpb->hob_lbam = qc->tf.hob_lbam;
5163ad400a9STejun Heo 		cpb->hob_lbah = qc->tf.hob_lbah;
5173ad400a9STejun Heo 	}
5183ad400a9STejun Heo 
5193ad400a9STejun Heo 	cpb->command = qc->tf.command;
5203ad400a9STejun Heo 	/* don't load ctl - dunno why.  it's like that in the initio driver */
5213ad400a9STejun Heo 
522b3f677e5STejun Heo 	/* setup PRD for CDB */
523b3f677e5STejun Heo 	if (is_atapi) {
524b3f677e5STejun Heo 		memcpy(pkt->cdb, qc->cdb, ATAPI_CDB_LEN);
525b3f677e5STejun Heo 		prd->mad = cpu_to_le32(pp->pkt_dma +
526b3f677e5STejun Heo 				       offsetof(struct inic_pkt, cdb));
527b3f677e5STejun Heo 		prd->len = cpu_to_le16(cdb_len);
528b3f677e5STejun Heo 		prd->flags = PRD_CDB | PRD_WRITE;
529b3f677e5STejun Heo 		if (!is_data)
530b3f677e5STejun Heo 			prd->flags |= PRD_END;
531b3f677e5STejun Heo 		prd++;
532b3f677e5STejun Heo 	}
533b3f677e5STejun Heo 
5343ad400a9STejun Heo 	/* setup sg table */
535049e8e04STejun Heo 	if (is_data)
5363ad400a9STejun Heo 		inic_fill_sg(prd, qc);
5373ad400a9STejun Heo 
5383ad400a9STejun Heo 	pp->cpb_tbl[0] = pp->pkt_dma;
53995364f36SJiri Slaby 
54095364f36SJiri Slaby 	return AC_ERR_OK;
5413ad400a9STejun Heo }
5423ad400a9STejun Heo 
inic_qc_issue(struct ata_queued_cmd * qc)5431fd7a697STejun Heo static unsigned int inic_qc_issue(struct ata_queued_cmd *qc)
5441fd7a697STejun Heo {
5451fd7a697STejun Heo 	struct ata_port *ap = qc->ap;
5463ad400a9STejun Heo 	void __iomem *port_base = inic_port_base(ap);
5471fd7a697STejun Heo 
5483ad400a9STejun Heo 	/* fire up the ADMA engine */
54999580664SBob Stewart 	writew(HCTL_FTHD0 | HCTL_LEDEN, port_base + HOST_CTL);
5503ad400a9STejun Heo 	writew(IDMA_CTL_GO, port_base + PORT_IDMA_CTL);
5513ad400a9STejun Heo 	writeb(0, port_base + PORT_CPB_PTQFIFO);
5523ad400a9STejun Heo 
5533ad400a9STejun Heo 	return 0;
5543ad400a9STejun Heo }
5551fd7a697STejun Heo 
inic_tf_read(struct ata_port * ap,struct ata_taskfile * tf)556364fac0eSTejun Heo static void inic_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
557364fac0eSTejun Heo {
558364fac0eSTejun Heo 	void __iomem *port_base = inic_port_base(ap);
559364fac0eSTejun Heo 
560efcef265SSergey Shtylyov 	tf->error	= readb(port_base + PORT_TF_FEATURE);
561364fac0eSTejun Heo 	tf->nsect	= readb(port_base + PORT_TF_NSECT);
562364fac0eSTejun Heo 	tf->lbal	= readb(port_base + PORT_TF_LBAL);
563364fac0eSTejun Heo 	tf->lbam	= readb(port_base + PORT_TF_LBAM);
564364fac0eSTejun Heo 	tf->lbah	= readb(port_base + PORT_TF_LBAH);
565364fac0eSTejun Heo 	tf->device	= readb(port_base + PORT_TF_DEVICE);
566efcef265SSergey Shtylyov 	tf->status	= readb(port_base + PORT_TF_COMMAND);
567364fac0eSTejun Heo }
568364fac0eSTejun Heo 
inic_qc_fill_rtf(struct ata_queued_cmd * qc)569931139afSDamien Le Moal static void inic_qc_fill_rtf(struct ata_queued_cmd *qc)
570364fac0eSTejun Heo {
571364fac0eSTejun Heo 	struct ata_taskfile *rtf = &qc->result_tf;
572364fac0eSTejun Heo 	struct ata_taskfile tf;
573364fac0eSTejun Heo 
574364fac0eSTejun Heo 	/* FIXME: Except for status and error, result TF access
575364fac0eSTejun Heo 	 * doesn't work.  I tried reading from BAR0/2, CPB and BAR5.
576364fac0eSTejun Heo 	 * None works regardless of which command interface is used.
577364fac0eSTejun Heo 	 * For now return true iff status indicates device error.
578364fac0eSTejun Heo 	 * This means that we're reporting bogus sector for RW
579364fac0eSTejun Heo 	 * failures.  Eeekk....
580364fac0eSTejun Heo 	 */
581364fac0eSTejun Heo 	inic_tf_read(qc->ap, &tf);
582364fac0eSTejun Heo 
583931139afSDamien Le Moal 	if (tf.status & ATA_ERR) {
584efcef265SSergey Shtylyov 		rtf->status = tf.status;
585efcef265SSergey Shtylyov 		rtf->error = tf.error;
586931139afSDamien Le Moal 	}
587364fac0eSTejun Heo }
588364fac0eSTejun Heo 
inic_freeze(struct ata_port * ap)5891fd7a697STejun Heo static void inic_freeze(struct ata_port *ap)
5901fd7a697STejun Heo {
5911fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
5921fd7a697STejun Heo 
593ab5b0235STejun Heo 	writeb(PIRQ_MASK_FREEZE, port_base + PORT_IRQ_MASK);
5941fd7a697STejun Heo 	writeb(0xff, port_base + PORT_IRQ_STAT);
5951fd7a697STejun Heo }
5961fd7a697STejun Heo 
inic_thaw(struct ata_port * ap)5971fd7a697STejun Heo static void inic_thaw(struct ata_port *ap)
5981fd7a697STejun Heo {
5991fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6001fd7a697STejun Heo 
6011fd7a697STejun Heo 	writeb(0xff, port_base + PORT_IRQ_STAT);
602ab5b0235STejun Heo 	writeb(PIRQ_MASK_DEFAULT, port_base + PORT_IRQ_MASK);
6031fd7a697STejun Heo }
6041fd7a697STejun Heo 
inic_check_ready(struct ata_link * link)605364fac0eSTejun Heo static int inic_check_ready(struct ata_link *link)
606364fac0eSTejun Heo {
607364fac0eSTejun Heo 	void __iomem *port_base = inic_port_base(link->ap);
608364fac0eSTejun Heo 
609364fac0eSTejun Heo 	return ata_check_ready(readb(port_base + PORT_TF_COMMAND));
610364fac0eSTejun Heo }
611364fac0eSTejun Heo 
6121fd7a697STejun Heo /*
6131fd7a697STejun Heo  * SRST and SControl hardreset don't give valid signature on this
6141fd7a697STejun Heo  * controller.  Only controller specific hardreset mechanism works.
6151fd7a697STejun Heo  */
inic_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)616cc0680a5STejun Heo static int inic_hardreset(struct ata_link *link, unsigned int *class,
617d4b2bab4STejun Heo 			  unsigned long deadline)
6181fd7a697STejun Heo {
619cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
6201fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6211fd7a697STejun Heo 	void __iomem *idma_ctl = port_base + PORT_IDMA_CTL;
622*d14d41ccSSergey Shtylyov 	const unsigned int *timing = sata_ehc_deb_timing(&link->eh_context);
6231fd7a697STejun Heo 	int rc;
6241fd7a697STejun Heo 
6251fd7a697STejun Heo 	/* hammer it into sane state */
6261fd7a697STejun Heo 	inic_reset_port(port_base);
6271fd7a697STejun Heo 
628f8b0685aSTejun Heo 	writew(IDMA_CTL_RST_ATA, idma_ctl);
6291fd7a697STejun Heo 	readw(idma_ctl);	/* flush */
63097750cebSTejun Heo 	ata_msleep(ap, 1);
631f8b0685aSTejun Heo 	writew(0, idma_ctl);
6321fd7a697STejun Heo 
633cc0680a5STejun Heo 	rc = sata_link_resume(link, timing, deadline);
6341fd7a697STejun Heo 	if (rc) {
635a9a79dfeSJoe Perches 		ata_link_warn(link,
636a9a79dfeSJoe Perches 			      "failed to resume link after reset (errno=%d)\n",
637a9a79dfeSJoe Perches 			      rc);
6381fd7a697STejun Heo 		return rc;
6391fd7a697STejun Heo 	}
6401fd7a697STejun Heo 
6411fd7a697STejun Heo 	*class = ATA_DEV_NONE;
642cc0680a5STejun Heo 	if (ata_link_online(link)) {
6431fd7a697STejun Heo 		struct ata_taskfile tf;
6441fd7a697STejun Heo 
645705e76beSTejun Heo 		/* wait for link to become ready */
646364fac0eSTejun Heo 		rc = ata_wait_after_reset(link, deadline, inic_check_ready);
6479b89391cSTejun Heo 		/* link occupied, -ENODEV too is an error */
6489b89391cSTejun Heo 		if (rc) {
649a9a79dfeSJoe Perches 			ata_link_warn(link,
650a9a79dfeSJoe Perches 				      "device not ready after hardreset (errno=%d)\n",
651a9a79dfeSJoe Perches 				      rc);
652d4b2bab4STejun Heo 			return rc;
6531fd7a697STejun Heo 		}
6541fd7a697STejun Heo 
655364fac0eSTejun Heo 		inic_tf_read(ap, &tf);
6566c952a0dSHannes Reinecke 		*class = ata_port_classify(ap, &tf);
6571fd7a697STejun Heo 	}
6581fd7a697STejun Heo 
6591fd7a697STejun Heo 	return 0;
6601fd7a697STejun Heo }
6611fd7a697STejun Heo 
inic_error_handler(struct ata_port * ap)6621fd7a697STejun Heo static void inic_error_handler(struct ata_port *ap)
6631fd7a697STejun Heo {
6641fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6651fd7a697STejun Heo 
6661fd7a697STejun Heo 	inic_reset_port(port_base);
667a1efdabaSTejun Heo 	ata_std_error_handler(ap);
6681fd7a697STejun Heo }
6691fd7a697STejun Heo 
inic_post_internal_cmd(struct ata_queued_cmd * qc)6701fd7a697STejun Heo static void inic_post_internal_cmd(struct ata_queued_cmd *qc)
6711fd7a697STejun Heo {
6721fd7a697STejun Heo 	/* make DMA engine forget about the failed command */
67387629312SNiklas Cassel 	if (qc->flags & ATA_QCFLAG_EH)
6741fd7a697STejun Heo 		inic_reset_port(inic_port_base(qc->ap));
6751fd7a697STejun Heo }
6761fd7a697STejun Heo 
init_port(struct ata_port * ap)6771fd7a697STejun Heo static void init_port(struct ata_port *ap)
6781fd7a697STejun Heo {
6791fd7a697STejun Heo 	void __iomem *port_base = inic_port_base(ap);
6803ad400a9STejun Heo 	struct inic_port_priv *pp = ap->private_data;
6811fd7a697STejun Heo 
6823ad400a9STejun Heo 	/* clear packet and CPB table */
6833ad400a9STejun Heo 	memset(pp->pkt, 0, sizeof(struct inic_pkt));
6843ad400a9STejun Heo 	memset(pp->cpb_tbl, 0, IDMA_CPB_TBL_SIZE);
6853ad400a9STejun Heo 
6866bc0d390STejun Heo 	/* setup CPB lookup table addresses */
6873ad400a9STejun Heo 	writel(pp->cpb_tbl_dma, port_base + PORT_CPB_CPBLAR);
6881fd7a697STejun Heo }
6891fd7a697STejun Heo 
inic_port_resume(struct ata_port * ap)6901fd7a697STejun Heo static int inic_port_resume(struct ata_port *ap)
6911fd7a697STejun Heo {
6921fd7a697STejun Heo 	init_port(ap);
6931fd7a697STejun Heo 	return 0;
6941fd7a697STejun Heo }
6951fd7a697STejun Heo 
inic_port_start(struct ata_port * ap)6961fd7a697STejun Heo static int inic_port_start(struct ata_port *ap)
6971fd7a697STejun Heo {
6983ad400a9STejun Heo 	struct device *dev = ap->host->dev;
6991fd7a697STejun Heo 	struct inic_port_priv *pp;
7001fd7a697STejun Heo 
7011fd7a697STejun Heo 	/* alloc and initialize private data */
7023ad400a9STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
7031fd7a697STejun Heo 	if (!pp)
7041fd7a697STejun Heo 		return -ENOMEM;
7051fd7a697STejun Heo 	ap->private_data = pp;
7061fd7a697STejun Heo 
7071fd7a697STejun Heo 	/* Alloc resources */
7083ad400a9STejun Heo 	pp->pkt = dmam_alloc_coherent(dev, sizeof(struct inic_pkt),
7093ad400a9STejun Heo 				      &pp->pkt_dma, GFP_KERNEL);
7103ad400a9STejun Heo 	if (!pp->pkt)
7113ad400a9STejun Heo 		return -ENOMEM;
7123ad400a9STejun Heo 
7133ad400a9STejun Heo 	pp->cpb_tbl = dmam_alloc_coherent(dev, IDMA_CPB_TBL_SIZE,
7143ad400a9STejun Heo 					  &pp->cpb_tbl_dma, GFP_KERNEL);
7153ad400a9STejun Heo 	if (!pp->cpb_tbl)
7163ad400a9STejun Heo 		return -ENOMEM;
7173ad400a9STejun Heo 
7181fd7a697STejun Heo 	init_port(ap);
7191fd7a697STejun Heo 
7201fd7a697STejun Heo 	return 0;
7211fd7a697STejun Heo }
7221fd7a697STejun Heo 
7231fd7a697STejun Heo static struct ata_port_operations inic_port_ops = {
724f8b0685aSTejun Heo 	.inherits		= &sata_port_ops,
7251fd7a697STejun Heo 
726b3f677e5STejun Heo 	.check_atapi_dma	= inic_check_atapi_dma,
7273ad400a9STejun Heo 	.qc_prep		= inic_qc_prep,
7281fd7a697STejun Heo 	.qc_issue		= inic_qc_issue,
729364fac0eSTejun Heo 	.qc_fill_rtf		= inic_qc_fill_rtf,
7301fd7a697STejun Heo 
7311fd7a697STejun Heo 	.freeze			= inic_freeze,
7321fd7a697STejun Heo 	.thaw			= inic_thaw,
733a1efdabaSTejun Heo 	.hardreset		= inic_hardreset,
7341fd7a697STejun Heo 	.error_handler		= inic_error_handler,
7351fd7a697STejun Heo 	.post_internal_cmd	= inic_post_internal_cmd,
7361fd7a697STejun Heo 
737029cfd6bSTejun Heo 	.scr_read		= inic_scr_read,
738029cfd6bSTejun Heo 	.scr_write		= inic_scr_write,
7391fd7a697STejun Heo 
740029cfd6bSTejun Heo 	.port_resume		= inic_port_resume,
7411fd7a697STejun Heo 	.port_start		= inic_port_start,
7421fd7a697STejun Heo };
7431fd7a697STejun Heo 
744f356b082SBhumika Goyal static const struct ata_port_info inic_port_info = {
7451fd7a697STejun Heo 	.flags			= ATA_FLAG_SATA | ATA_FLAG_PIO_DMA,
74614bdef98SErik Inge Bolsø 	.pio_mask		= ATA_PIO4,
74714bdef98SErik Inge Bolsø 	.mwdma_mask		= ATA_MWDMA2,
748bf6263a8SJeff Garzik 	.udma_mask		= ATA_UDMA6,
7491fd7a697STejun Heo 	.port_ops		= &inic_port_ops
7501fd7a697STejun Heo };
7511fd7a697STejun Heo 
init_controller(void __iomem * mmio_base,u16 hctl)7521fd7a697STejun Heo static int init_controller(void __iomem *mmio_base, u16 hctl)
7531fd7a697STejun Heo {
7541fd7a697STejun Heo 	int i;
7551fd7a697STejun Heo 	u16 val;
7561fd7a697STejun Heo 
7571fd7a697STejun Heo 	hctl &= ~HCTL_KNOWN_BITS;
7581fd7a697STejun Heo 
7591fd7a697STejun Heo 	/* Soft reset whole controller.  Spec says reset duration is 3
7601fd7a697STejun Heo 	 * PCI clocks, be generous and give it 10ms.
7611fd7a697STejun Heo 	 */
7621fd7a697STejun Heo 	writew(hctl | HCTL_SOFTRST, mmio_base + HOST_CTL);
7631fd7a697STejun Heo 	readw(mmio_base + HOST_CTL); /* flush */
7641fd7a697STejun Heo 
7651fd7a697STejun Heo 	for (i = 0; i < 10; i++) {
7661fd7a697STejun Heo 		msleep(1);
7671fd7a697STejun Heo 		val = readw(mmio_base + HOST_CTL);
7681fd7a697STejun Heo 		if (!(val & HCTL_SOFTRST))
7691fd7a697STejun Heo 			break;
7701fd7a697STejun Heo 	}
7711fd7a697STejun Heo 
7721fd7a697STejun Heo 	if (val & HCTL_SOFTRST)
7731fd7a697STejun Heo 		return -EIO;
7741fd7a697STejun Heo 
7751fd7a697STejun Heo 	/* mask all interrupts and reset ports */
7761fd7a697STejun Heo 	for (i = 0; i < NR_PORTS; i++) {
7771fd7a697STejun Heo 		void __iomem *port_base = mmio_base + i * PORT_SIZE;
7781fd7a697STejun Heo 
7791fd7a697STejun Heo 		writeb(0xff, port_base + PORT_IRQ_MASK);
7801fd7a697STejun Heo 		inic_reset_port(port_base);
7811fd7a697STejun Heo 	}
7821fd7a697STejun Heo 
7831fd7a697STejun Heo 	/* port IRQ is masked now, unmask global IRQ */
7841fd7a697STejun Heo 	writew(hctl & ~HCTL_IRQOFF, mmio_base + HOST_CTL);
7851fd7a697STejun Heo 	val = readw(mmio_base + HOST_IRQ_MASK);
7861fd7a697STejun Heo 	val &= ~(HIRQ_PORT0 | HIRQ_PORT1);
7871fd7a697STejun Heo 	writew(val, mmio_base + HOST_IRQ_MASK);
7881fd7a697STejun Heo 
7891fd7a697STejun Heo 	return 0;
7901fd7a697STejun Heo }
7911fd7a697STejun Heo 
79258eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
inic_pci_device_resume(struct pci_dev * pdev)7931fd7a697STejun Heo static int inic_pci_device_resume(struct pci_dev *pdev)
7941fd7a697STejun Heo {
7950a86e1c8SJingoo Han 	struct ata_host *host = pci_get_drvdata(pdev);
7961fd7a697STejun Heo 	struct inic_host_priv *hpriv = host->private_data;
7971fd7a697STejun Heo 	int rc;
7981fd7a697STejun Heo 
7995aea408dSDmitriy Monakhov 	rc = ata_pci_device_do_resume(pdev);
8005aea408dSDmitriy Monakhov 	if (rc)
8015aea408dSDmitriy Monakhov 		return rc;
8021fd7a697STejun Heo 
8031fd7a697STejun Heo 	if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
804ba66b242STejun Heo 		rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl);
8051fd7a697STejun Heo 		if (rc)
8061fd7a697STejun Heo 			return rc;
8071fd7a697STejun Heo 	}
8081fd7a697STejun Heo 
8091fd7a697STejun Heo 	ata_host_resume(host);
8101fd7a697STejun Heo 
8111fd7a697STejun Heo 	return 0;
8121fd7a697STejun Heo }
813438ac6d5STejun Heo #endif
8141fd7a697STejun Heo 
inic_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)8151fd7a697STejun Heo static int inic_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
8161fd7a697STejun Heo {
8174447d351STejun Heo 	const struct ata_port_info *ppi[] = { &inic_port_info, NULL };
8184447d351STejun Heo 	struct ata_host *host;
8191fd7a697STejun Heo 	struct inic_host_priv *hpriv;
8200d5ff566STejun Heo 	void __iomem * const *iomap;
821ba66b242STejun Heo 	int mmio_bar;
8221fd7a697STejun Heo 	int i, rc;
8231fd7a697STejun Heo 
82406296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
8251fd7a697STejun Heo 
826bb969619STejun 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");
827bb969619STejun Heo 
8284447d351STejun Heo 	/* alloc host */
8294447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, NR_PORTS);
8304447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
8314447d351STejun Heo 	if (!host || !hpriv)
8324447d351STejun Heo 		return -ENOMEM;
8334447d351STejun Heo 
8344447d351STejun Heo 	host->private_data = hpriv;
8354447d351STejun Heo 
836ba66b242STejun Heo 	/* Acquire resources and fill host.  Note that PCI and cardbus
837ba66b242STejun Heo 	 * use different BARs.
838ba66b242STejun Heo 	 */
83924dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
8401fd7a697STejun Heo 	if (rc)
8411fd7a697STejun Heo 		return rc;
8421fd7a697STejun Heo 
843ba66b242STejun Heo 	if (pci_resource_flags(pdev, MMIO_BAR_PCI) & IORESOURCE_MEM)
844ba66b242STejun Heo 		mmio_bar = MMIO_BAR_PCI;
845ba66b242STejun Heo 	else
846ba66b242STejun Heo 		mmio_bar = MMIO_BAR_CARDBUS;
847ba66b242STejun Heo 
848ba66b242STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << mmio_bar, DRV_NAME);
8490d5ff566STejun Heo 	if (rc)
8500d5ff566STejun Heo 		return rc;
8514447d351STejun Heo 	host->iomap = iomap = pcim_iomap_table(pdev);
852ba66b242STejun Heo 	hpriv->mmio_base = iomap[mmio_bar];
853ba66b242STejun Heo 	hpriv->cached_hctl = readw(hpriv->mmio_base + HOST_CTL);
8544447d351STejun Heo 
8554447d351STejun Heo 	for (i = 0; i < NR_PORTS; i++) {
856cbcdd875STejun Heo 		struct ata_port *ap = host->ports[i];
857cbcdd875STejun Heo 
858ba66b242STejun Heo 		ata_port_pbar_desc(ap, mmio_bar, -1, "mmio");
859ba66b242STejun Heo 		ata_port_pbar_desc(ap, mmio_bar, i * PORT_SIZE, "port");
8604447d351STejun Heo 	}
8614447d351STejun Heo 
8621fd7a697STejun Heo 	/* Set dma_mask.  This devices doesn't support 64bit addressing. */
863b5e55556SChristoph Hellwig 	rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
8641fd7a697STejun Heo 	if (rc) {
865a44fec1fSJoe Perches 		dev_err(&pdev->dev, "32-bit DMA enable failed\n");
86624dc5f33STejun Heo 		return rc;
8671fd7a697STejun Heo 	}
8681fd7a697STejun Heo 
869ba66b242STejun Heo 	rc = init_controller(hpriv->mmio_base, hpriv->cached_hctl);
8701fd7a697STejun Heo 	if (rc) {
871a44fec1fSJoe Perches 		dev_err(&pdev->dev, "failed to initialize controller\n");
87224dc5f33STejun Heo 		return rc;
8731fd7a697STejun Heo 	}
8741fd7a697STejun Heo 
8751fd7a697STejun Heo 	pci_set_master(pdev);
8764447d351STejun Heo 	return ata_host_activate(host, pdev->irq, inic_interrupt, IRQF_SHARED,
8774447d351STejun Heo 				 &inic_sht);
8781fd7a697STejun Heo }
8791fd7a697STejun Heo 
8801fd7a697STejun Heo static const struct pci_device_id inic_pci_tbl[] = {
8811fd7a697STejun Heo 	{ PCI_VDEVICE(INIT, 0x1622), },
8821fd7a697STejun Heo 	{ },
8831fd7a697STejun Heo };
8841fd7a697STejun Heo 
8851fd7a697STejun Heo static struct pci_driver inic_pci_driver = {
8861fd7a697STejun Heo 	.name 		= DRV_NAME,
8871fd7a697STejun Heo 	.id_table	= inic_pci_tbl,
88858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
8891fd7a697STejun Heo 	.suspend	= ata_pci_device_suspend,
8901fd7a697STejun Heo 	.resume		= inic_pci_device_resume,
891438ac6d5STejun Heo #endif
8921fd7a697STejun Heo 	.probe 		= inic_init_one,
8931fd7a697STejun Heo 	.remove		= ata_pci_remove_one,
8941fd7a697STejun Heo };
8951fd7a697STejun Heo 
8962fc75da0SAxel Lin module_pci_driver(inic_pci_driver);
8971fd7a697STejun Heo 
8981fd7a697STejun Heo MODULE_AUTHOR("Tejun Heo");
8991fd7a697STejun Heo MODULE_DESCRIPTION("low-level driver for Initio 162x SATA");
9001fd7a697STejun Heo MODULE_LICENSE("GPL v2");
9011fd7a697STejun Heo MODULE_DEVICE_TABLE(pci, inic_pci_tbl);
9021fd7a697STejun Heo MODULE_VERSION(DRV_VERSION);
903