xref: /openbmc/linux/drivers/ata/sata_mv.c (revision 6ec76070f17993aa9ac0344330b971783d10c9c2)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  * sata_mv.c - Marvell SATA support
3c6fd2807SJeff Garzik  *
440f21b11SMark Lord  * Copyright 2008-2009: Marvell Corporation, all rights reserved.
5c6fd2807SJeff Garzik  * Copyright 2005: EMC Corporation, all rights reserved.
6c6fd2807SJeff Garzik  * Copyright 2005 Red Hat, Inc.  All rights reserved.
7c6fd2807SJeff Garzik  *
840f21b11SMark Lord  * Originally written by Brett Russ.
940f21b11SMark Lord  * Extensive overhaul and enhancement by Mark Lord <mlord@pobox.com>.
1040f21b11SMark Lord  *
11c6fd2807SJeff Garzik  * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
12c6fd2807SJeff Garzik  *
13c6fd2807SJeff Garzik  * This program is free software; you can redistribute it and/or modify
14c6fd2807SJeff Garzik  * it under the terms of the GNU General Public License as published by
15c6fd2807SJeff Garzik  * the Free Software Foundation; version 2 of the License.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  * This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  * GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  * You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  * along with this program; if not, write to the Free Software
24c6fd2807SJeff Garzik  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  */
27c6fd2807SJeff Garzik 
284a05e209SJeff Garzik /*
2985afb934SMark Lord  * sata_mv TODO list:
3085afb934SMark Lord  *
3185afb934SMark Lord  * --> Develop a low-power-consumption strategy, and implement it.
3285afb934SMark Lord  *
332b748a0aSMark Lord  * --> Add sysfs attributes for per-chip / per-HC IRQ coalescing thresholds.
3485afb934SMark Lord  *
3585afb934SMark Lord  * --> [Experiment, Marvell value added] Is it possible to use target
3685afb934SMark Lord  *       mode to cross-connect two Linux boxes with Marvell cards?  If so,
3785afb934SMark Lord  *       creating LibATA target mode support would be very interesting.
3885afb934SMark Lord  *
3985afb934SMark Lord  *       Target mode, for those without docs, is the ability to directly
4085afb934SMark Lord  *       connect two SATA ports.
414a05e209SJeff Garzik  */
424a05e209SJeff Garzik 
4365ad7fefSMark Lord /*
4465ad7fefSMark Lord  * 80x1-B2 errata PCI#11:
4565ad7fefSMark Lord  *
4665ad7fefSMark Lord  * Users of the 6041/6081 Rev.B2 chips (current is C0)
4765ad7fefSMark Lord  * should be careful to insert those cards only onto PCI-X bus #0,
4865ad7fefSMark Lord  * and only in device slots 0..7, not higher.  The chips may not
4965ad7fefSMark Lord  * work correctly otherwise  (note: this is a pretty rare condition).
5065ad7fefSMark Lord  */
5165ad7fefSMark Lord 
52c6fd2807SJeff Garzik #include <linux/kernel.h>
53c6fd2807SJeff Garzik #include <linux/module.h>
54c6fd2807SJeff Garzik #include <linux/pci.h>
55c6fd2807SJeff Garzik #include <linux/init.h>
56c6fd2807SJeff Garzik #include <linux/blkdev.h>
57c6fd2807SJeff Garzik #include <linux/delay.h>
58c6fd2807SJeff Garzik #include <linux/interrupt.h>
598d8b6004SAndrew Morton #include <linux/dmapool.h>
60c6fd2807SJeff Garzik #include <linux/dma-mapping.h>
61c6fd2807SJeff Garzik #include <linux/device.h>
62c77a2f4eSSaeed Bishara #include <linux/clk.h>
63b7db4f2eSAndrew Lunn #include <linux/phy/phy.h>
64f351b2d6SSaeed Bishara #include <linux/platform_device.h>
65f351b2d6SSaeed Bishara #include <linux/ata_platform.h>
6615a32632SLennert Buytenhek #include <linux/mbus.h>
67c46938ccSMark Lord #include <linux/bitops.h>
685a0e3ad6STejun Heo #include <linux/gfp.h>
6997b414e1SAndrew Lunn #include <linux/of.h>
7097b414e1SAndrew Lunn #include <linux/of_irq.h>
71c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
72c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
736c08772eSJeff Garzik #include <scsi/scsi_device.h>
74c6fd2807SJeff Garzik #include <linux/libata.h>
75c6fd2807SJeff Garzik 
76c6fd2807SJeff Garzik #define DRV_NAME	"sata_mv"
77cae5a29dSMark Lord #define DRV_VERSION	"1.28"
78c6fd2807SJeff Garzik 
7940f21b11SMark Lord /*
8040f21b11SMark Lord  * module options
8140f21b11SMark Lord  */
8240f21b11SMark Lord 
8340f21b11SMark Lord #ifdef CONFIG_PCI
8413b74085SAndrew Lunn static int msi;
8540f21b11SMark Lord module_param(msi, int, S_IRUGO);
8640f21b11SMark Lord MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
8740f21b11SMark Lord #endif
8840f21b11SMark Lord 
892b748a0aSMark Lord static int irq_coalescing_io_count;
902b748a0aSMark Lord module_param(irq_coalescing_io_count, int, S_IRUGO);
912b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_io_count,
922b748a0aSMark Lord 		 "IRQ coalescing I/O count threshold (0..255)");
932b748a0aSMark Lord 
942b748a0aSMark Lord static int irq_coalescing_usecs;
952b748a0aSMark Lord module_param(irq_coalescing_usecs, int, S_IRUGO);
962b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_usecs,
972b748a0aSMark Lord 		 "IRQ coalescing time threshold in usecs");
982b748a0aSMark Lord 
99c6fd2807SJeff Garzik enum {
100c6fd2807SJeff Garzik 	/* BAR's are enumerated in terms of pci_resource_start() terms */
101c6fd2807SJeff Garzik 	MV_PRIMARY_BAR		= 0,	/* offset 0x10: memory space */
102c6fd2807SJeff Garzik 	MV_IO_BAR		= 2,	/* offset 0x18: IO space */
103c6fd2807SJeff Garzik 	MV_MISC_BAR		= 3,	/* offset 0x1c: FLASH, NVRAM, SRAM */
104c6fd2807SJeff Garzik 
105c6fd2807SJeff Garzik 	MV_MAJOR_REG_AREA_SZ	= 0x10000,	/* 64KB */
106c6fd2807SJeff Garzik 	MV_MINOR_REG_AREA_SZ	= 0x2000,	/* 8KB */
107c6fd2807SJeff Garzik 
1082b748a0aSMark Lord 	/* For use with both IRQ coalescing methods ("all ports" or "per-HC" */
1092b748a0aSMark Lord 	COAL_CLOCKS_PER_USEC	= 150,		/* for calculating COAL_TIMEs */
1102b748a0aSMark Lord 	MAX_COAL_TIME_THRESHOLD	= ((1 << 24) - 1), /* internal clocks count */
1112b748a0aSMark Lord 	MAX_COAL_IO_COUNT	= 255,		/* completed I/O count */
1122b748a0aSMark Lord 
113c6fd2807SJeff Garzik 	MV_PCI_REG_BASE		= 0,
114c6fd2807SJeff Garzik 
1152b748a0aSMark Lord 	/*
1162b748a0aSMark Lord 	 * Per-chip ("all ports") interrupt coalescing feature.
1172b748a0aSMark Lord 	 * This is only for GEN_II / GEN_IIE hardware.
1182b748a0aSMark Lord 	 *
1192b748a0aSMark Lord 	 * Coalescing defers the interrupt until either the IO_THRESHOLD
1202b748a0aSMark Lord 	 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
1212b748a0aSMark Lord 	 */
122cae5a29dSMark Lord 	COAL_REG_BASE		= 0x18000,
123cae5a29dSMark Lord 	IRQ_COAL_CAUSE		= (COAL_REG_BASE + 0x08),
1242b748a0aSMark Lord 	ALL_PORTS_COAL_IRQ	= (1 << 4),	/* all ports irq event */
1252b748a0aSMark Lord 
126cae5a29dSMark Lord 	IRQ_COAL_IO_THRESHOLD   = (COAL_REG_BASE + 0xcc),
127cae5a29dSMark Lord 	IRQ_COAL_TIME_THRESHOLD = (COAL_REG_BASE + 0xd0),
1282b748a0aSMark Lord 
1292b748a0aSMark Lord 	/*
1302b748a0aSMark Lord 	 * Registers for the (unused here) transaction coalescing feature:
1312b748a0aSMark Lord 	 */
132cae5a29dSMark Lord 	TRAN_COAL_CAUSE_LO	= (COAL_REG_BASE + 0x88),
133cae5a29dSMark Lord 	TRAN_COAL_CAUSE_HI	= (COAL_REG_BASE + 0x8c),
1342b748a0aSMark Lord 
135cae5a29dSMark Lord 	SATAHC0_REG_BASE	= 0x20000,
136cae5a29dSMark Lord 	FLASH_CTL		= 0x1046c,
137cae5a29dSMark Lord 	GPIO_PORT_CTL		= 0x104f0,
138cae5a29dSMark Lord 	RESET_CFG		= 0x180d8,
139c6fd2807SJeff Garzik 
140c6fd2807SJeff Garzik 	MV_PCI_REG_SZ		= MV_MAJOR_REG_AREA_SZ,
141c6fd2807SJeff Garzik 	MV_SATAHC_REG_SZ	= MV_MAJOR_REG_AREA_SZ,
142c6fd2807SJeff Garzik 	MV_SATAHC_ARBTR_REG_SZ	= MV_MINOR_REG_AREA_SZ,		/* arbiter */
143c6fd2807SJeff Garzik 	MV_PORT_REG_SZ		= MV_MINOR_REG_AREA_SZ,
144c6fd2807SJeff Garzik 
145c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH		= 32,
146c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH_MASK	= MV_MAX_Q_DEPTH - 1,
147c6fd2807SJeff Garzik 
148c6fd2807SJeff Garzik 	/* CRQB needs alignment on a 1KB boundary. Size == 1KB
149c6fd2807SJeff Garzik 	 * CRPB needs alignment on a 256B boundary. Size == 256B
150c6fd2807SJeff Garzik 	 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
151c6fd2807SJeff Garzik 	 */
152c6fd2807SJeff Garzik 	MV_CRQB_Q_SZ		= (32 * MV_MAX_Q_DEPTH),
153c6fd2807SJeff Garzik 	MV_CRPB_Q_SZ		= (8 * MV_MAX_Q_DEPTH),
154da2fa9baSMark Lord 	MV_MAX_SG_CT		= 256,
155c6fd2807SJeff Garzik 	MV_SG_TBL_SZ		= (16 * MV_MAX_SG_CT),
156c6fd2807SJeff Garzik 
157352fab70SMark Lord 	/* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
158c6fd2807SJeff Garzik 	MV_PORT_HC_SHIFT	= 2,
159352fab70SMark Lord 	MV_PORTS_PER_HC		= (1 << MV_PORT_HC_SHIFT), /* 4 */
160352fab70SMark Lord 	/* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
161352fab70SMark Lord 	MV_PORT_MASK		= (MV_PORTS_PER_HC - 1),   /* 3 */
162c6fd2807SJeff Garzik 
163c6fd2807SJeff Garzik 	/* Host Flags */
164c6fd2807SJeff Garzik 	MV_FLAG_DUAL_HC		= (1 << 30),  /* two SATA Host Controllers */
1657bb3c529SSaeed Bishara 
1669cbe056fSSergei Shtylyov 	MV_COMMON_FLAGS		= ATA_FLAG_SATA | ATA_FLAG_PIO_POLLING,
167ad3aef51SMark Lord 
16891b1a84cSMark Lord 	MV_GEN_I_FLAGS		= MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
169c6fd2807SJeff Garzik 
17040f21b11SMark Lord 	MV_GEN_II_FLAGS		= MV_COMMON_FLAGS | ATA_FLAG_NCQ |
17140f21b11SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
17291b1a84cSMark Lord 
17391b1a84cSMark Lord 	MV_GEN_IIE_FLAGS	= MV_GEN_II_FLAGS | ATA_FLAG_AN,
174ad3aef51SMark Lord 
175c6fd2807SJeff Garzik 	CRQB_FLAG_READ		= (1 << 0),
176c6fd2807SJeff Garzik 	CRQB_TAG_SHIFT		= 1,
177c5d3e45aSJeff Garzik 	CRQB_IOID_SHIFT		= 6,	/* CRQB Gen-II/IIE IO Id shift */
178e12bef50SMark Lord 	CRQB_PMP_SHIFT		= 12,	/* CRQB Gen-II/IIE PMP shift */
179c5d3e45aSJeff Garzik 	CRQB_HOSTQ_SHIFT	= 17,	/* CRQB Gen-II/IIE HostQueTag shift */
180c6fd2807SJeff Garzik 	CRQB_CMD_ADDR_SHIFT	= 8,
181c6fd2807SJeff Garzik 	CRQB_CMD_CS		= (0x2 << 11),
182c6fd2807SJeff Garzik 	CRQB_CMD_LAST		= (1 << 15),
183c6fd2807SJeff Garzik 
184c6fd2807SJeff Garzik 	CRPB_FLAG_STATUS_SHIFT	= 8,
185c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_6	= 5,	/* CRPB Gen-II IO Id shift */
186c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_7	= 7,	/* CRPB Gen-IIE IO Id shift */
187c6fd2807SJeff Garzik 
188c6fd2807SJeff Garzik 	EPRD_FLAG_END_OF_TBL	= (1 << 31),
189c6fd2807SJeff Garzik 
190c6fd2807SJeff Garzik 	/* PCI interface registers */
191c6fd2807SJeff Garzik 
192cae5a29dSMark Lord 	MV_PCI_COMMAND		= 0xc00,
193cae5a29dSMark Lord 	MV_PCI_COMMAND_MWRCOM	= (1 << 4),	/* PCI Master Write Combining */
194cae5a29dSMark Lord 	MV_PCI_COMMAND_MRDTRIG	= (1 << 7),	/* PCI Master Read Trigger */
195c6fd2807SJeff Garzik 
196cae5a29dSMark Lord 	PCI_MAIN_CMD_STS	= 0xd30,
197c6fd2807SJeff Garzik 	STOP_PCI_MASTER		= (1 << 2),
198c6fd2807SJeff Garzik 	PCI_MASTER_EMPTY	= (1 << 3),
199c6fd2807SJeff Garzik 	GLOB_SFT_RST		= (1 << 4),
200c6fd2807SJeff Garzik 
201cae5a29dSMark Lord 	MV_PCI_MODE		= 0xd00,
2028e7decdbSMark Lord 	MV_PCI_MODE_MASK	= 0x30,
2038e7decdbSMark Lord 
204c6fd2807SJeff Garzik 	MV_PCI_EXP_ROM_BAR_CTL	= 0xd2c,
205c6fd2807SJeff Garzik 	MV_PCI_DISC_TIMER	= 0xd04,
206c6fd2807SJeff Garzik 	MV_PCI_MSI_TRIGGER	= 0xc38,
207c6fd2807SJeff Garzik 	MV_PCI_SERR_MASK	= 0xc28,
208cae5a29dSMark Lord 	MV_PCI_XBAR_TMOUT	= 0x1d04,
209c6fd2807SJeff Garzik 	MV_PCI_ERR_LOW_ADDRESS	= 0x1d40,
210c6fd2807SJeff Garzik 	MV_PCI_ERR_HIGH_ADDRESS	= 0x1d44,
211c6fd2807SJeff Garzik 	MV_PCI_ERR_ATTRIBUTE	= 0x1d48,
212c6fd2807SJeff Garzik 	MV_PCI_ERR_COMMAND	= 0x1d50,
213c6fd2807SJeff Garzik 
214cae5a29dSMark Lord 	PCI_IRQ_CAUSE		= 0x1d58,
215cae5a29dSMark Lord 	PCI_IRQ_MASK		= 0x1d5c,
216c6fd2807SJeff Garzik 	PCI_UNMASK_ALL_IRQS	= 0x7fffff,	/* bits 22-0 */
217c6fd2807SJeff Garzik 
218cae5a29dSMark Lord 	PCIE_IRQ_CAUSE		= 0x1900,
219cae5a29dSMark Lord 	PCIE_IRQ_MASK		= 0x1910,
220646a4da5SMark Lord 	PCIE_UNMASK_ALL_IRQS	= 0x40a,	/* assorted bits */
22102a121daSMark Lord 
2227368f919SMark Lord 	/* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
223cae5a29dSMark Lord 	PCI_HC_MAIN_IRQ_CAUSE	= 0x1d60,
224cae5a29dSMark Lord 	PCI_HC_MAIN_IRQ_MASK	= 0x1d64,
225cae5a29dSMark Lord 	SOC_HC_MAIN_IRQ_CAUSE	= 0x20020,
226cae5a29dSMark Lord 	SOC_HC_MAIN_IRQ_MASK	= 0x20024,
22740f21b11SMark Lord 	ERR_IRQ			= (1 << 0),	/* shift by (2 * port #) */
22840f21b11SMark Lord 	DONE_IRQ		= (1 << 1),	/* shift by (2 * port #) */
229c6fd2807SJeff Garzik 	HC0_IRQ_PEND		= 0x1ff,	/* bits 0-8 = HC0's ports */
230c6fd2807SJeff Garzik 	HC_SHIFT		= 9,		/* bits 9-17 = HC1's ports */
2312b748a0aSMark Lord 	DONE_IRQ_0_3		= 0x000000aa,	/* DONE_IRQ ports 0,1,2,3 */
2322b748a0aSMark Lord 	DONE_IRQ_4_7		= (DONE_IRQ_0_3 << HC_SHIFT),  /* 4,5,6,7 */
233c6fd2807SJeff Garzik 	PCI_ERR			= (1 << 18),
23440f21b11SMark Lord 	TRAN_COAL_LO_DONE	= (1 << 19),	/* transaction coalescing */
23540f21b11SMark Lord 	TRAN_COAL_HI_DONE	= (1 << 20),	/* transaction coalescing */
23640f21b11SMark Lord 	PORTS_0_3_COAL_DONE	= (1 << 8),	/* HC0 IRQ coalescing */
23740f21b11SMark Lord 	PORTS_4_7_COAL_DONE	= (1 << 17),	/* HC1 IRQ coalescing */
23840f21b11SMark Lord 	ALL_PORTS_COAL_DONE	= (1 << 21),	/* GEN_II(E) IRQ coalescing */
239c6fd2807SJeff Garzik 	GPIO_INT		= (1 << 22),
240c6fd2807SJeff Garzik 	SELF_INT		= (1 << 23),
241c6fd2807SJeff Garzik 	TWSI_INT		= (1 << 24),
242c6fd2807SJeff Garzik 	HC_MAIN_RSVD		= (0x7f << 25),	/* bits 31-25 */
243fb621e2fSJeff Garzik 	HC_MAIN_RSVD_5		= (0x1fff << 19), /* bits 31-19 */
244f351b2d6SSaeed Bishara 	HC_MAIN_RSVD_SOC	= (0x3fffffb << 6),     /* bits 31-9, 7-6 */
245c6fd2807SJeff Garzik 
246c6fd2807SJeff Garzik 	/* SATAHC registers */
247cae5a29dSMark Lord 	HC_CFG			= 0x00,
248c6fd2807SJeff Garzik 
249cae5a29dSMark Lord 	HC_IRQ_CAUSE		= 0x14,
250352fab70SMark Lord 	DMA_IRQ			= (1 << 0),	/* shift by port # */
251352fab70SMark Lord 	HC_COAL_IRQ		= (1 << 4),	/* IRQ coalescing */
252c6fd2807SJeff Garzik 	DEV_IRQ			= (1 << 8),	/* shift by port # */
253c6fd2807SJeff Garzik 
2542b748a0aSMark Lord 	/*
2552b748a0aSMark Lord 	 * Per-HC (Host-Controller) interrupt coalescing feature.
2562b748a0aSMark Lord 	 * This is present on all chip generations.
2572b748a0aSMark Lord 	 *
2582b748a0aSMark Lord 	 * Coalescing defers the interrupt until either the IO_THRESHOLD
2592b748a0aSMark Lord 	 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
2602b748a0aSMark Lord 	 */
261cae5a29dSMark Lord 	HC_IRQ_COAL_IO_THRESHOLD	= 0x000c,
262cae5a29dSMark Lord 	HC_IRQ_COAL_TIME_THRESHOLD	= 0x0010,
2632b748a0aSMark Lord 
264cae5a29dSMark Lord 	SOC_LED_CTRL		= 0x2c,
265000b344fSMark Lord 	SOC_LED_CTRL_BLINK	= (1 << 0),	/* Active LED blink */
266000b344fSMark Lord 	SOC_LED_CTRL_ACT_PRESENCE = (1 << 2),	/* Multiplex dev presence */
267000b344fSMark Lord 						/*  with dev activity LED */
268000b344fSMark Lord 
269c6fd2807SJeff Garzik 	/* Shadow block registers */
270cae5a29dSMark Lord 	SHD_BLK			= 0x100,
271cae5a29dSMark Lord 	SHD_CTL_AST		= 0x20,		/* ofs from SHD_BLK */
272c6fd2807SJeff Garzik 
273c6fd2807SJeff Garzik 	/* SATA registers */
274cae5a29dSMark Lord 	SATA_STATUS		= 0x300,  /* ctrl, err regs follow status */
275cae5a29dSMark Lord 	SATA_ACTIVE		= 0x350,
276cae5a29dSMark Lord 	FIS_IRQ_CAUSE		= 0x364,
277cae5a29dSMark Lord 	FIS_IRQ_CAUSE_AN	= (1 << 9),	/* async notification */
27817c5aab5SMark Lord 
279cae5a29dSMark Lord 	LTMODE			= 0x30c,	/* requires read-after-write */
28017c5aab5SMark Lord 	LTMODE_BIT8		= (1 << 8),	/* unknown, but necessary */
28117c5aab5SMark Lord 
282cae5a29dSMark Lord 	PHY_MODE2		= 0x330,
283c6fd2807SJeff Garzik 	PHY_MODE3		= 0x310,
284cae5a29dSMark Lord 
285cae5a29dSMark Lord 	PHY_MODE4		= 0x314,	/* requires read-after-write */
286ba069e37SMark Lord 	PHY_MODE4_CFG_MASK	= 0x00000003,	/* phy internal config field */
287ba069e37SMark Lord 	PHY_MODE4_CFG_VALUE	= 0x00000001,	/* phy internal config field */
288ba069e37SMark Lord 	PHY_MODE4_RSVD_ZEROS	= 0x5de3fffa,	/* Gen2e always write zeros */
289ba069e37SMark Lord 	PHY_MODE4_RSVD_ONES	= 0x00000005,	/* Gen2e always write ones */
290ba069e37SMark Lord 
291cae5a29dSMark Lord 	SATA_IFCTL		= 0x344,
292cae5a29dSMark Lord 	SATA_TESTCTL		= 0x348,
293cae5a29dSMark Lord 	SATA_IFSTAT		= 0x34c,
294cae5a29dSMark Lord 	VENDOR_UNIQUE_FIS	= 0x35c,
29517c5aab5SMark Lord 
296cae5a29dSMark Lord 	FISCFG			= 0x360,
2978e7decdbSMark Lord 	FISCFG_WAIT_DEV_ERR	= (1 << 8),	/* wait for host on DevErr */
2988e7decdbSMark Lord 	FISCFG_SINGLE_SYNC	= (1 << 16),	/* SYNC on DMA activation */
29917c5aab5SMark Lord 
30029b7e43cSMartin Michlmayr 	PHY_MODE9_GEN2		= 0x398,
30129b7e43cSMartin Michlmayr 	PHY_MODE9_GEN1		= 0x39c,
30229b7e43cSMartin Michlmayr 	PHYCFG_OFS		= 0x3a0,	/* only in 65n devices */
30329b7e43cSMartin Michlmayr 
304c6fd2807SJeff Garzik 	MV5_PHY_MODE		= 0x74,
305cae5a29dSMark Lord 	MV5_LTMODE		= 0x30,
306cae5a29dSMark Lord 	MV5_PHY_CTL		= 0x0C,
307cae5a29dSMark Lord 	SATA_IFCFG		= 0x050,
3089013d64eSLior Amsalem 	LP_PHY_CTL		= 0x058,
3093661aa99SThomas Petazzoni 	LP_PHY_CTL_PIN_PU_PLL   = (1 << 0),
3103661aa99SThomas Petazzoni 	LP_PHY_CTL_PIN_PU_RX    = (1 << 1),
3113661aa99SThomas Petazzoni 	LP_PHY_CTL_PIN_PU_TX    = (1 << 2),
3123661aa99SThomas Petazzoni 	LP_PHY_CTL_GEN_TX_3G    = (1 << 5),
3133661aa99SThomas Petazzoni 	LP_PHY_CTL_GEN_RX_3G    = (1 << 9),
314c6fd2807SJeff Garzik 
315c6fd2807SJeff Garzik 	MV_M2_PREAMP_MASK	= 0x7e0,
316c6fd2807SJeff Garzik 
317c6fd2807SJeff Garzik 	/* Port registers */
318cae5a29dSMark Lord 	EDMA_CFG		= 0,
3190c58912eSMark Lord 	EDMA_CFG_Q_DEPTH	= 0x1f,		/* max device queue depth */
3200c58912eSMark Lord 	EDMA_CFG_NCQ		= (1 << 5),	/* for R/W FPDMA queued */
321c6fd2807SJeff Garzik 	EDMA_CFG_NCQ_GO_ON_ERR	= (1 << 14),	/* continue on error */
322c6fd2807SJeff Garzik 	EDMA_CFG_RD_BRST_EXT	= (1 << 11),	/* read burst 512B */
323c6fd2807SJeff Garzik 	EDMA_CFG_WR_BUFF_LEN	= (1 << 13),	/* write buffer 512B */
324e12bef50SMark Lord 	EDMA_CFG_EDMA_FBS	= (1 << 16),	/* EDMA FIS-Based Switching */
325e12bef50SMark Lord 	EDMA_CFG_FBS		= (1 << 26),	/* FIS-Based Switching */
326c6fd2807SJeff Garzik 
327cae5a29dSMark Lord 	EDMA_ERR_IRQ_CAUSE	= 0x8,
328cae5a29dSMark Lord 	EDMA_ERR_IRQ_MASK	= 0xc,
3296c1153e0SJeff Garzik 	EDMA_ERR_D_PAR		= (1 << 0),	/* UDMA data parity err */
3306c1153e0SJeff Garzik 	EDMA_ERR_PRD_PAR	= (1 << 1),	/* UDMA PRD parity err */
3316c1153e0SJeff Garzik 	EDMA_ERR_DEV		= (1 << 2),	/* device error */
3326c1153e0SJeff Garzik 	EDMA_ERR_DEV_DCON	= (1 << 3),	/* device disconnect */
3336c1153e0SJeff Garzik 	EDMA_ERR_DEV_CON	= (1 << 4),	/* device connected */
3346c1153e0SJeff Garzik 	EDMA_ERR_SERR		= (1 << 5),	/* SError bits [WBDST] raised */
335c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS	= (1 << 7),	/* Gen II/IIE self-disable */
336c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS_5	= (1 << 8),	/* Gen I self-disable */
3376c1153e0SJeff Garzik 	EDMA_ERR_BIST_ASYNC	= (1 << 8),	/* BIST FIS or Async Notify */
338c5d3e45aSJeff Garzik 	EDMA_ERR_TRANS_IRQ_7	= (1 << 8),	/* Gen IIE transprt layer irq */
3396c1153e0SJeff Garzik 	EDMA_ERR_CRQB_PAR	= (1 << 9),	/* CRQB parity error */
3406c1153e0SJeff Garzik 	EDMA_ERR_CRPB_PAR	= (1 << 10),	/* CRPB parity error */
3416c1153e0SJeff Garzik 	EDMA_ERR_INTRL_PAR	= (1 << 11),	/* internal parity error */
3426c1153e0SJeff Garzik 	EDMA_ERR_IORDY		= (1 << 12),	/* IORdy timeout */
343646a4da5SMark Lord 
3446c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_RX	= (0xf << 13),	/* link ctrl rx error */
345646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_0	= (1 << 13),	/* transient: CRC err */
346646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_1	= (1 << 14),	/* transient: FIFO err */
347646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_2	= (1 << 15),	/* fatal: caught SYNC */
348646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_3	= (1 << 16),	/* transient: FIS rx err */
349646a4da5SMark Lord 
3506c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_RX	= (0xf << 17),	/* link data rx error */
351646a4da5SMark Lord 
3526c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_TX	= (0x1f << 21),	/* link ctrl tx error */
353646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_0	= (1 << 21),	/* transient: CRC err */
354646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_1	= (1 << 22),	/* transient: FIFO err */
355646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_2	= (1 << 23),	/* transient: caught SYNC */
356646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_3	= (1 << 24),	/* transient: caught DMAT */
357646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_4	= (1 << 25),	/* transient: FIS collision */
358646a4da5SMark Lord 
3596c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_TX	= (0x1f << 26),	/* link data tx error */
360646a4da5SMark Lord 
3616c1153e0SJeff Garzik 	EDMA_ERR_TRANS_PROTO	= (1 << 31),	/* transport protocol error */
362c5d3e45aSJeff Garzik 	EDMA_ERR_OVERRUN_5	= (1 << 5),
363c5d3e45aSJeff Garzik 	EDMA_ERR_UNDERRUN_5	= (1 << 6),
364646a4da5SMark Lord 
365646a4da5SMark Lord 	EDMA_ERR_IRQ_TRANSIENT  = EDMA_ERR_LNK_CTRL_RX_0 |
366646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_1 |
367646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_3 |
36885afb934SMark Lord 				  EDMA_ERR_LNK_CTRL_TX,
369646a4da5SMark Lord 
370bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE		= EDMA_ERR_D_PAR |
371bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
372bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
373bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
374bdd4dddeSJeff Garzik 				  EDMA_ERR_SERR |
375bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS |
3766c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
377bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
378bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
379bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY |
380bdd4dddeSJeff Garzik 				  EDMA_ERR_LNK_CTRL_RX_2 |
381c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_RX |
382c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_TX |
383bdd4dddeSJeff Garzik 				  EDMA_ERR_TRANS_PROTO,
384e12bef50SMark Lord 
385bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE_5	= EDMA_ERR_D_PAR |
386bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
387bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
388bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
389bdd4dddeSJeff Garzik 				  EDMA_ERR_OVERRUN_5 |
390bdd4dddeSJeff Garzik 				  EDMA_ERR_UNDERRUN_5 |
391bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS_5 |
3926c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
393bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
394bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
395bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY,
396c6fd2807SJeff Garzik 
397cae5a29dSMark Lord 	EDMA_REQ_Q_BASE_HI	= 0x10,
398cae5a29dSMark Lord 	EDMA_REQ_Q_IN_PTR	= 0x14,		/* also contains BASE_LO */
399c6fd2807SJeff Garzik 
400cae5a29dSMark Lord 	EDMA_REQ_Q_OUT_PTR	= 0x18,
401c6fd2807SJeff Garzik 	EDMA_REQ_Q_PTR_SHIFT	= 5,
402c6fd2807SJeff Garzik 
403cae5a29dSMark Lord 	EDMA_RSP_Q_BASE_HI	= 0x1c,
404cae5a29dSMark Lord 	EDMA_RSP_Q_IN_PTR	= 0x20,
405cae5a29dSMark Lord 	EDMA_RSP_Q_OUT_PTR	= 0x24,		/* also contains BASE_LO */
406c6fd2807SJeff Garzik 	EDMA_RSP_Q_PTR_SHIFT	= 3,
407c6fd2807SJeff Garzik 
408cae5a29dSMark Lord 	EDMA_CMD		= 0x28,		/* EDMA command register */
4090ea9e179SJeff Garzik 	EDMA_EN			= (1 << 0),	/* enable EDMA */
4100ea9e179SJeff Garzik 	EDMA_DS			= (1 << 1),	/* disable EDMA; self-negated */
4118e7decdbSMark Lord 	EDMA_RESET		= (1 << 2),	/* reset eng/trans/link/phy */
412c6fd2807SJeff Garzik 
413cae5a29dSMark Lord 	EDMA_STATUS		= 0x30,		/* EDMA engine status */
4148e7decdbSMark Lord 	EDMA_STATUS_CACHE_EMPTY	= (1 << 6),	/* GenIIe command cache empty */
4158e7decdbSMark Lord 	EDMA_STATUS_IDLE	= (1 << 7),	/* GenIIe EDMA enabled/idle */
4168e7decdbSMark Lord 
417cae5a29dSMark Lord 	EDMA_IORDY_TMOUT	= 0x34,
418cae5a29dSMark Lord 	EDMA_ARB_CFG		= 0x38,
4198e7decdbSMark Lord 
420cae5a29dSMark Lord 	EDMA_HALTCOND		= 0x60,		/* GenIIe halt conditions */
421cae5a29dSMark Lord 	EDMA_UNKNOWN_RSVD	= 0x6C,		/* GenIIe unknown/reserved */
422da14265eSMark Lord 
423cae5a29dSMark Lord 	BMDMA_CMD		= 0x224,	/* bmdma command register */
424cae5a29dSMark Lord 	BMDMA_STATUS		= 0x228,	/* bmdma status register */
425cae5a29dSMark Lord 	BMDMA_PRD_LOW		= 0x22c,	/* bmdma PRD addr 31:0 */
426cae5a29dSMark Lord 	BMDMA_PRD_HIGH		= 0x230,	/* bmdma PRD addr 63:32 */
427da14265eSMark Lord 
428c6fd2807SJeff Garzik 	/* Host private flags (hp_flags) */
429c6fd2807SJeff Garzik 	MV_HP_FLAG_MSI		= (1 << 0),
430c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB0	= (1 << 1),
431c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB2	= (1 << 2),
432c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1B2	= (1 << 3),
433c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1C0	= (1 << 4),
4340ea9e179SJeff Garzik 	MV_HP_GEN_I		= (1 << 6),	/* Generation I: 50xx */
4350ea9e179SJeff Garzik 	MV_HP_GEN_II		= (1 << 7),	/* Generation II: 60xx */
4360ea9e179SJeff Garzik 	MV_HP_GEN_IIE		= (1 << 8),	/* Generation IIE: 6042/7042 */
43702a121daSMark Lord 	MV_HP_PCIE		= (1 << 9),	/* PCIe bus/regs: 7042 */
438616d4a98SMark Lord 	MV_HP_CUT_THROUGH	= (1 << 10),	/* can use EDMA cut-through */
4391f398472SMark Lord 	MV_HP_FLAG_SOC		= (1 << 11),	/* SystemOnChip, no PCI */
440000b344fSMark Lord 	MV_HP_QUIRK_LED_BLINK_EN = (1 << 12),	/* is led blinking enabled? */
4419013d64eSLior Amsalem 	MV_HP_FIX_LP_PHY_CTL	= (1 << 13),	/* fix speed in LP_PHY_CTL ? */
442c6fd2807SJeff Garzik 
443c6fd2807SJeff Garzik 	/* Port private flags (pp_flags) */
4440ea9e179SJeff Garzik 	MV_PP_FLAG_EDMA_EN	= (1 << 0),	/* is EDMA engine enabled? */
44572109168SMark Lord 	MV_PP_FLAG_NCQ_EN	= (1 << 1),	/* is EDMA set up for NCQ? */
44600f42eabSMark Lord 	MV_PP_FLAG_FBS_EN	= (1 << 2),	/* is EDMA set up for FBS? */
44729d187bbSMark Lord 	MV_PP_FLAG_DELAYED_EH	= (1 << 3),	/* delayed dev err handling */
448d16ab3f6SMark Lord 	MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4),	/* ignore initial ATA_DRDY */
449c6fd2807SJeff Garzik };
450c6fd2807SJeff Garzik 
451ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
452ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
453c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
4548e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
4551f398472SMark Lord #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
456c6fd2807SJeff Garzik 
45715a32632SLennert Buytenhek #define WINDOW_CTRL(i)		(0x20030 + ((i) << 4))
45815a32632SLennert Buytenhek #define WINDOW_BASE(i)		(0x20034 + ((i) << 4))
45915a32632SLennert Buytenhek 
460c6fd2807SJeff Garzik enum {
461baf14aa1SJeff Garzik 	/* DMA boundary 0xffff is required by the s/g splitting
462baf14aa1SJeff Garzik 	 * we need on /length/ in mv_fill-sg().
463baf14aa1SJeff Garzik 	 */
464baf14aa1SJeff Garzik 	MV_DMA_BOUNDARY		= 0xffffU,
465c6fd2807SJeff Garzik 
4660ea9e179SJeff Garzik 	/* mask of register bits containing lower 32 bits
4670ea9e179SJeff Garzik 	 * of EDMA request queue DMA address
4680ea9e179SJeff Garzik 	 */
469c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_LO_MASK	= 0xfffffc00U,
470c6fd2807SJeff Garzik 
4710ea9e179SJeff Garzik 	/* ditto, for response queue */
472c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_LO_MASK	= 0xffffff00U,
473c6fd2807SJeff Garzik };
474c6fd2807SJeff Garzik 
475c6fd2807SJeff Garzik enum chip_type {
476c6fd2807SJeff Garzik 	chip_504x,
477c6fd2807SJeff Garzik 	chip_508x,
478c6fd2807SJeff Garzik 	chip_5080,
479c6fd2807SJeff Garzik 	chip_604x,
480c6fd2807SJeff Garzik 	chip_608x,
481c6fd2807SJeff Garzik 	chip_6042,
482c6fd2807SJeff Garzik 	chip_7042,
483f351b2d6SSaeed Bishara 	chip_soc,
484c6fd2807SJeff Garzik };
485c6fd2807SJeff Garzik 
486c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
487c6fd2807SJeff Garzik struct mv_crqb {
488c6fd2807SJeff Garzik 	__le32			sg_addr;
489c6fd2807SJeff Garzik 	__le32			sg_addr_hi;
490c6fd2807SJeff Garzik 	__le16			ctrl_flags;
491c6fd2807SJeff Garzik 	__le16			ata_cmd[11];
492c6fd2807SJeff Garzik };
493c6fd2807SJeff Garzik 
494c6fd2807SJeff Garzik struct mv_crqb_iie {
495c6fd2807SJeff Garzik 	__le32			addr;
496c6fd2807SJeff Garzik 	__le32			addr_hi;
497c6fd2807SJeff Garzik 	__le32			flags;
498c6fd2807SJeff Garzik 	__le32			len;
499c6fd2807SJeff Garzik 	__le32			ata_cmd[4];
500c6fd2807SJeff Garzik };
501c6fd2807SJeff Garzik 
502c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
503c6fd2807SJeff Garzik struct mv_crpb {
504c6fd2807SJeff Garzik 	__le16			id;
505c6fd2807SJeff Garzik 	__le16			flags;
506c6fd2807SJeff Garzik 	__le32			tmstmp;
507c6fd2807SJeff Garzik };
508c6fd2807SJeff Garzik 
509c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
510c6fd2807SJeff Garzik struct mv_sg {
511c6fd2807SJeff Garzik 	__le32			addr;
512c6fd2807SJeff Garzik 	__le32			flags_size;
513c6fd2807SJeff Garzik 	__le32			addr_hi;
514c6fd2807SJeff Garzik 	__le32			reserved;
515c6fd2807SJeff Garzik };
516c6fd2807SJeff Garzik 
51708da1759SMark Lord /*
51808da1759SMark Lord  * We keep a local cache of a few frequently accessed port
51908da1759SMark Lord  * registers here, to avoid having to read them (very slow)
52008da1759SMark Lord  * when switching between EDMA and non-EDMA modes.
52108da1759SMark Lord  */
52208da1759SMark Lord struct mv_cached_regs {
52308da1759SMark Lord 	u32			fiscfg;
52408da1759SMark Lord 	u32			ltmode;
52508da1759SMark Lord 	u32			haltcond;
526c01e8a23SMark Lord 	u32			unknown_rsvd;
52708da1759SMark Lord };
52808da1759SMark Lord 
529c6fd2807SJeff Garzik struct mv_port_priv {
530c6fd2807SJeff Garzik 	struct mv_crqb		*crqb;
531c6fd2807SJeff Garzik 	dma_addr_t		crqb_dma;
532c6fd2807SJeff Garzik 	struct mv_crpb		*crpb;
533c6fd2807SJeff Garzik 	dma_addr_t		crpb_dma;
534eb73d558SMark Lord 	struct mv_sg		*sg_tbl[MV_MAX_Q_DEPTH];
535eb73d558SMark Lord 	dma_addr_t		sg_tbl_dma[MV_MAX_Q_DEPTH];
536bdd4dddeSJeff Garzik 
537bdd4dddeSJeff Garzik 	unsigned int		req_idx;
538bdd4dddeSJeff Garzik 	unsigned int		resp_idx;
539bdd4dddeSJeff Garzik 
540c6fd2807SJeff Garzik 	u32			pp_flags;
54108da1759SMark Lord 	struct mv_cached_regs	cached;
54229d187bbSMark Lord 	unsigned int		delayed_eh_pmp_map;
543c6fd2807SJeff Garzik };
544c6fd2807SJeff Garzik 
545c6fd2807SJeff Garzik struct mv_port_signal {
546c6fd2807SJeff Garzik 	u32			amps;
547c6fd2807SJeff Garzik 	u32			pre;
548c6fd2807SJeff Garzik };
549c6fd2807SJeff Garzik 
55002a121daSMark Lord struct mv_host_priv {
55102a121daSMark Lord 	u32			hp_flags;
5521bfeff03SSaeed Bishara 	unsigned int 		board_idx;
55396e2c487SMark Lord 	u32			main_irq_mask;
55402a121daSMark Lord 	struct mv_port_signal	signal[8];
55502a121daSMark Lord 	const struct mv_hw_ops	*ops;
556f351b2d6SSaeed Bishara 	int			n_ports;
557f351b2d6SSaeed Bishara 	void __iomem		*base;
5587368f919SMark Lord 	void __iomem		*main_irq_cause_addr;
5597368f919SMark Lord 	void __iomem		*main_irq_mask_addr;
560cae5a29dSMark Lord 	u32			irq_cause_offset;
561cae5a29dSMark Lord 	u32			irq_mask_offset;
56202a121daSMark Lord 	u32			unmask_all_irqs;
563c77a2f4eSSaeed Bishara 
564e0067f0bSEzequiel Garcia 	/*
565e0067f0bSEzequiel Garcia 	 * Needed on some devices that require their clocks to be enabled.
566e0067f0bSEzequiel Garcia 	 * These are optional: if the platform device does not have any
567e0067f0bSEzequiel Garcia 	 * clocks, they won't be used.  Also, if the underlying hardware
568e0067f0bSEzequiel Garcia 	 * does not support the common clock framework (CONFIG_HAVE_CLK=n),
569e0067f0bSEzequiel Garcia 	 * all the clock operations become no-ops (see clk.h).
570e0067f0bSEzequiel Garcia 	 */
571c77a2f4eSSaeed Bishara 	struct clk		*clk;
572eee98990SAndrew Lunn 	struct clk              **port_clks;
573da2fa9baSMark Lord 	/*
574b7db4f2eSAndrew Lunn 	 * Some devices have a SATA PHY which can be enabled/disabled
575b7db4f2eSAndrew Lunn 	 * in order to save power. These are optional: if the platform
576b7db4f2eSAndrew Lunn 	 * devices does not have any phy, they won't be used.
577b7db4f2eSAndrew Lunn 	 */
578b7db4f2eSAndrew Lunn 	struct phy		**port_phys;
579b7db4f2eSAndrew Lunn 	/*
580da2fa9baSMark Lord 	 * These consistent DMA memory pools give us guaranteed
581da2fa9baSMark Lord 	 * alignment for hardware-accessed data structures,
582da2fa9baSMark Lord 	 * and less memory waste in accomplishing the alignment.
583da2fa9baSMark Lord 	 */
584da2fa9baSMark Lord 	struct dma_pool		*crqb_pool;
585da2fa9baSMark Lord 	struct dma_pool		*crpb_pool;
586da2fa9baSMark Lord 	struct dma_pool		*sg_tbl_pool;
58702a121daSMark Lord };
58802a121daSMark Lord 
589c6fd2807SJeff Garzik struct mv_hw_ops {
590c6fd2807SJeff Garzik 	void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
591c6fd2807SJeff Garzik 			   unsigned int port);
592c6fd2807SJeff Garzik 	void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
593c6fd2807SJeff Garzik 	void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
594c6fd2807SJeff Garzik 			   void __iomem *mmio);
595c6fd2807SJeff Garzik 	int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
596c6fd2807SJeff Garzik 			unsigned int n_hc);
597c6fd2807SJeff Garzik 	void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
5987bb3c529SSaeed Bishara 	void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
599c6fd2807SJeff Garzik };
600c6fd2807SJeff Garzik 
60182ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
60282ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
60382ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
60482ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
605c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap);
606c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap);
6073e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc);
608c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc);
609c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
610c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
611a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
612a1efdabaSTejun Heo 			unsigned long deadline);
613bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap);
614bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap);
615f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev);
616c6fd2807SJeff Garzik 
617c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
618c6fd2807SJeff Garzik 			   unsigned int port);
619c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
620c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
621c6fd2807SJeff Garzik 			   void __iomem *mmio);
622c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
623c6fd2807SJeff Garzik 			unsigned int n_hc);
624c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
6257bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
626c6fd2807SJeff Garzik 
627c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
628c6fd2807SJeff Garzik 			   unsigned int port);
629c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
630c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
631c6fd2807SJeff Garzik 			   void __iomem *mmio);
632c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
633c6fd2807SJeff Garzik 			unsigned int n_hc);
634c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
635f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
636f351b2d6SSaeed Bishara 				      void __iomem *mmio);
637f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
638f351b2d6SSaeed Bishara 				      void __iomem *mmio);
639f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
640f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc);
641f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
642f351b2d6SSaeed Bishara 				      void __iomem *mmio);
643f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
64429b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
64529b7e43cSMartin Michlmayr 				  void __iomem *mmio, unsigned int port);
6467bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
647e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
648c6fd2807SJeff Garzik 			     unsigned int port_no);
649e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap);
650b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio);
65100b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
652c6fd2807SJeff Garzik 
653e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp);
654e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
655e49856d8SMark Lord 				unsigned long deadline);
656e49856d8SMark Lord static int  mv_softreset(struct ata_link *link, unsigned int *class,
657e49856d8SMark Lord 				unsigned long deadline);
65829d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap);
6594c299ca3SMark Lord static void mv_process_crpb_entries(struct ata_port *ap,
6604c299ca3SMark Lord 					struct mv_port_priv *pp);
661c6fd2807SJeff Garzik 
662da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap);
663da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
664da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc);
665da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc);
666da14265eSMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc);
667da14265eSMark Lord static u8   mv_bmdma_status(struct ata_port *ap);
668d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap);
669da14265eSMark Lord 
670eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
671eb73d558SMark Lord  * because we have to allow room for worst case splitting of
672eb73d558SMark Lord  * PRDs for 64K boundaries in mv_fill_sg().
673eb73d558SMark Lord  */
67413b74085SAndrew Lunn #ifdef CONFIG_PCI
675c5d3e45aSJeff Garzik static struct scsi_host_template mv5_sht = {
67668d1d07bSTejun Heo 	ATA_BASE_SHT(DRV_NAME),
677baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
678c5d3e45aSJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
679c5d3e45aSJeff Garzik };
68013b74085SAndrew Lunn #endif
681c5d3e45aSJeff Garzik static struct scsi_host_template mv6_sht = {
68268d1d07bSTejun Heo 	ATA_NCQ_SHT(DRV_NAME),
683138bfdd0SMark Lord 	.can_queue		= MV_MAX_Q_DEPTH - 1,
684baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
685c6fd2807SJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
686c6fd2807SJeff Garzik };
687c6fd2807SJeff Garzik 
688029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = {
689029cfd6bSTejun Heo 	.inherits		= &ata_sff_port_ops,
690c6fd2807SJeff Garzik 
691c96f1732SAlan Cox 	.lost_interrupt		= ATA_OP_NULL,
692c96f1732SAlan Cox 
6933e4a1391SMark Lord 	.qc_defer		= mv_qc_defer,
694c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep,
695c6fd2807SJeff Garzik 	.qc_issue		= mv_qc_issue,
696c6fd2807SJeff Garzik 
697bdd4dddeSJeff Garzik 	.freeze			= mv_eh_freeze,
698bdd4dddeSJeff Garzik 	.thaw			= mv_eh_thaw,
699a1efdabaSTejun Heo 	.hardreset		= mv_hardreset,
700bdd4dddeSJeff Garzik 
701c6fd2807SJeff Garzik 	.scr_read		= mv5_scr_read,
702c6fd2807SJeff Garzik 	.scr_write		= mv5_scr_write,
703c6fd2807SJeff Garzik 
704c6fd2807SJeff Garzik 	.port_start		= mv_port_start,
705c6fd2807SJeff Garzik 	.port_stop		= mv_port_stop,
706c6fd2807SJeff Garzik };
707c6fd2807SJeff Garzik 
708029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
7098930ff25STejun Heo 	.inherits		= &ata_bmdma_port_ops,
710c6fd2807SJeff Garzik 
7118930ff25STejun Heo 	.lost_interrupt		= ATA_OP_NULL,
7128930ff25STejun Heo 
7138930ff25STejun Heo 	.qc_defer		= mv_qc_defer,
7148930ff25STejun Heo 	.qc_prep		= mv_qc_prep,
7158930ff25STejun Heo 	.qc_issue		= mv_qc_issue,
7168930ff25STejun Heo 
7178930ff25STejun Heo 	.dev_config             = mv6_dev_config,
7188930ff25STejun Heo 
7198930ff25STejun Heo 	.freeze			= mv_eh_freeze,
7208930ff25STejun Heo 	.thaw			= mv_eh_thaw,
7218930ff25STejun Heo 	.hardreset		= mv_hardreset,
7228930ff25STejun Heo 	.softreset		= mv_softreset,
723e49856d8SMark Lord 	.pmp_hardreset		= mv_pmp_hardreset,
724e49856d8SMark Lord 	.pmp_softreset		= mv_softreset,
72529d187bbSMark Lord 	.error_handler		= mv_pmp_error_handler,
726da14265eSMark Lord 
7278930ff25STejun Heo 	.scr_read		= mv_scr_read,
7288930ff25STejun Heo 	.scr_write		= mv_scr_write,
7298930ff25STejun Heo 
730d16ab3f6SMark Lord 	.sff_check_status	= mv_sff_check_status,
731da14265eSMark Lord 	.sff_irq_clear		= mv_sff_irq_clear,
732da14265eSMark Lord 	.check_atapi_dma	= mv_check_atapi_dma,
733da14265eSMark Lord 	.bmdma_setup		= mv_bmdma_setup,
734da14265eSMark Lord 	.bmdma_start		= mv_bmdma_start,
735da14265eSMark Lord 	.bmdma_stop		= mv_bmdma_stop,
736da14265eSMark Lord 	.bmdma_status		= mv_bmdma_status,
7378930ff25STejun Heo 
7388930ff25STejun Heo 	.port_start		= mv_port_start,
7398930ff25STejun Heo 	.port_stop		= mv_port_stop,
740c6fd2807SJeff Garzik };
741c6fd2807SJeff Garzik 
742029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
743029cfd6bSTejun Heo 	.inherits		= &mv6_ops,
744029cfd6bSTejun Heo 	.dev_config		= ATA_OP_NULL,
745c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep_iie,
746c6fd2807SJeff Garzik };
747c6fd2807SJeff Garzik 
748c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
749c6fd2807SJeff Garzik 	{  /* chip_504x */
75091b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS,
751c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
752bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
753c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
754c6fd2807SJeff Garzik 	},
755c6fd2807SJeff Garzik 	{  /* chip_508x */
75691b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
757c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
758bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
759c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
760c6fd2807SJeff Garzik 	},
761c6fd2807SJeff Garzik 	{  /* chip_5080 */
76291b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
763c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
764bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
765c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
766c6fd2807SJeff Garzik 	},
767c6fd2807SJeff Garzik 	{  /* chip_604x */
76891b1a84cSMark Lord 		.flags		= MV_GEN_II_FLAGS,
769c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
770bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
771c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
772c6fd2807SJeff Garzik 	},
773c6fd2807SJeff Garzik 	{  /* chip_608x */
77491b1a84cSMark Lord 		.flags		= MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
775c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
776bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
777c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
778c6fd2807SJeff Garzik 	},
779c6fd2807SJeff Garzik 	{  /* chip_6042 */
78091b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
781c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
782bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
783c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
784c6fd2807SJeff Garzik 	},
785c6fd2807SJeff Garzik 	{  /* chip_7042 */
78691b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
787c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
788bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
789c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
790c6fd2807SJeff Garzik 	},
791f351b2d6SSaeed Bishara 	{  /* chip_soc */
79291b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
793c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
794f351b2d6SSaeed Bishara 		.udma_mask	= ATA_UDMA6,
795f351b2d6SSaeed Bishara 		.port_ops	= &mv_iie_ops,
796f351b2d6SSaeed Bishara 	},
797c6fd2807SJeff Garzik };
798c6fd2807SJeff Garzik 
799c6fd2807SJeff Garzik static const struct pci_device_id mv_pci_tbl[] = {
8002d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
8012d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
8022d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
8032d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
80446c5784cSMark Lord 	/* RocketRAID 1720/174x have different identifiers */
80546c5784cSMark Lord 	{ PCI_VDEVICE(TTI, 0x1720), chip_6042 },
8064462254aSMark Lord 	{ PCI_VDEVICE(TTI, 0x1740), chip_6042 },
8074462254aSMark Lord 	{ PCI_VDEVICE(TTI, 0x1742), chip_6042 },
808c6fd2807SJeff Garzik 
8092d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
8102d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
8112d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
8122d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
8132d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
814c6fd2807SJeff Garzik 
8152d2744fcSJeff Garzik 	{ PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
8162d2744fcSJeff Garzik 
817d9f9c6bcSFlorian Attenberger 	/* Adaptec 1430SA */
818d9f9c6bcSFlorian Attenberger 	{ PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
819d9f9c6bcSFlorian Attenberger 
82002a121daSMark Lord 	/* Marvell 7042 support */
8216a3d586dSMorrison, Tom 	{ PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
8226a3d586dSMorrison, Tom 
82302a121daSMark Lord 	/* Highpoint RocketRAID PCIe series */
82402a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2300), chip_7042 },
82502a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2310), chip_7042 },
82602a121daSMark Lord 
827c6fd2807SJeff Garzik 	{ }			/* terminate list */
828c6fd2807SJeff Garzik };
829c6fd2807SJeff Garzik 
830c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
831c6fd2807SJeff Garzik 	.phy_errata		= mv5_phy_errata,
832c6fd2807SJeff Garzik 	.enable_leds		= mv5_enable_leds,
833c6fd2807SJeff Garzik 	.read_preamp		= mv5_read_preamp,
834c6fd2807SJeff Garzik 	.reset_hc		= mv5_reset_hc,
835c6fd2807SJeff Garzik 	.reset_flash		= mv5_reset_flash,
836c6fd2807SJeff Garzik 	.reset_bus		= mv5_reset_bus,
837c6fd2807SJeff Garzik };
838c6fd2807SJeff Garzik 
839c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
840c6fd2807SJeff Garzik 	.phy_errata		= mv6_phy_errata,
841c6fd2807SJeff Garzik 	.enable_leds		= mv6_enable_leds,
842c6fd2807SJeff Garzik 	.read_preamp		= mv6_read_preamp,
843c6fd2807SJeff Garzik 	.reset_hc		= mv6_reset_hc,
844c6fd2807SJeff Garzik 	.reset_flash		= mv6_reset_flash,
845c6fd2807SJeff Garzik 	.reset_bus		= mv_reset_pci_bus,
846c6fd2807SJeff Garzik };
847c6fd2807SJeff Garzik 
848f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
849f351b2d6SSaeed Bishara 	.phy_errata		= mv6_phy_errata,
850f351b2d6SSaeed Bishara 	.enable_leds		= mv_soc_enable_leds,
851f351b2d6SSaeed Bishara 	.read_preamp		= mv_soc_read_preamp,
852f351b2d6SSaeed Bishara 	.reset_hc		= mv_soc_reset_hc,
853f351b2d6SSaeed Bishara 	.reset_flash		= mv_soc_reset_flash,
854f351b2d6SSaeed Bishara 	.reset_bus		= mv_soc_reset_bus,
855f351b2d6SSaeed Bishara };
856f351b2d6SSaeed Bishara 
85729b7e43cSMartin Michlmayr static const struct mv_hw_ops mv_soc_65n_ops = {
85829b7e43cSMartin Michlmayr 	.phy_errata		= mv_soc_65n_phy_errata,
85929b7e43cSMartin Michlmayr 	.enable_leds		= mv_soc_enable_leds,
86029b7e43cSMartin Michlmayr 	.reset_hc		= mv_soc_reset_hc,
86129b7e43cSMartin Michlmayr 	.reset_flash		= mv_soc_reset_flash,
86229b7e43cSMartin Michlmayr 	.reset_bus		= mv_soc_reset_bus,
86329b7e43cSMartin Michlmayr };
86429b7e43cSMartin Michlmayr 
865c6fd2807SJeff Garzik /*
866c6fd2807SJeff Garzik  * Functions
867c6fd2807SJeff Garzik  */
868c6fd2807SJeff Garzik 
869c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
870c6fd2807SJeff Garzik {
871c6fd2807SJeff Garzik 	writel(data, addr);
872c6fd2807SJeff Garzik 	(void) readl(addr);	/* flush to avoid PCI posted write */
873c6fd2807SJeff Garzik }
874c6fd2807SJeff Garzik 
875c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
876c6fd2807SJeff Garzik {
877c6fd2807SJeff Garzik 	return port >> MV_PORT_HC_SHIFT;
878c6fd2807SJeff Garzik }
879c6fd2807SJeff Garzik 
880c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
881c6fd2807SJeff Garzik {
882c6fd2807SJeff Garzik 	return port & MV_PORT_MASK;
883c6fd2807SJeff Garzik }
884c6fd2807SJeff Garzik 
8851cfd19aeSMark Lord /*
8861cfd19aeSMark Lord  * Consolidate some rather tricky bit shift calculations.
8871cfd19aeSMark Lord  * This is hot-path stuff, so not a function.
8881cfd19aeSMark Lord  * Simple code, with two return values, so macro rather than inline.
8891cfd19aeSMark Lord  *
8901cfd19aeSMark Lord  * port is the sole input, in range 0..7.
8917368f919SMark Lord  * shift is one output, for use with main_irq_cause / main_irq_mask registers.
8927368f919SMark Lord  * hardport is the other output, in range 0..3.
8931cfd19aeSMark Lord  *
8941cfd19aeSMark Lord  * Note that port and hardport may be the same variable in some cases.
8951cfd19aeSMark Lord  */
8961cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport)	\
8971cfd19aeSMark Lord {								\
8981cfd19aeSMark Lord 	shift    = mv_hc_from_port(port) * HC_SHIFT;		\
8991cfd19aeSMark Lord 	hardport = mv_hardport_from_port(port);			\
9001cfd19aeSMark Lord 	shift   += hardport * 2;				\
9011cfd19aeSMark Lord }
9021cfd19aeSMark Lord 
903352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
904352fab70SMark Lord {
905cae5a29dSMark Lord 	return (base + SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
906352fab70SMark Lord }
907352fab70SMark Lord 
908c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
909c6fd2807SJeff Garzik 						 unsigned int port)
910c6fd2807SJeff Garzik {
911c6fd2807SJeff Garzik 	return mv_hc_base(base, mv_hc_from_port(port));
912c6fd2807SJeff Garzik }
913c6fd2807SJeff Garzik 
914c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
915c6fd2807SJeff Garzik {
916c6fd2807SJeff Garzik 	return  mv_hc_base_from_port(base, port) +
917c6fd2807SJeff Garzik 		MV_SATAHC_ARBTR_REG_SZ +
918c6fd2807SJeff Garzik 		(mv_hardport_from_port(port) * MV_PORT_REG_SZ);
919c6fd2807SJeff Garzik }
920c6fd2807SJeff Garzik 
921e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
922e12bef50SMark Lord {
923e12bef50SMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
924e12bef50SMark Lord 	unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
925e12bef50SMark Lord 
926e12bef50SMark Lord 	return hc_mmio + ofs;
927e12bef50SMark Lord }
928e12bef50SMark Lord 
929f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
930f351b2d6SSaeed Bishara {
931f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
932f351b2d6SSaeed Bishara 	return hpriv->base;
933f351b2d6SSaeed Bishara }
934f351b2d6SSaeed Bishara 
935c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
936c6fd2807SJeff Garzik {
937f351b2d6SSaeed Bishara 	return mv_port_base(mv_host_base(ap->host), ap->port_no);
938c6fd2807SJeff Garzik }
939c6fd2807SJeff Garzik 
940cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
941c6fd2807SJeff Garzik {
942cca3974eSJeff Garzik 	return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
943c6fd2807SJeff Garzik }
944c6fd2807SJeff Garzik 
94508da1759SMark Lord /**
94608da1759SMark Lord  *      mv_save_cached_regs - (re-)initialize cached port registers
94708da1759SMark Lord  *      @ap: the port whose registers we are caching
94808da1759SMark Lord  *
94908da1759SMark Lord  *	Initialize the local cache of port registers,
95008da1759SMark Lord  *	so that reading them over and over again can
95108da1759SMark Lord  *	be avoided on the hotter paths of this driver.
95208da1759SMark Lord  *	This saves a few microseconds each time we switch
95308da1759SMark Lord  *	to/from EDMA mode to perform (eg.) a drive cache flush.
95408da1759SMark Lord  */
95508da1759SMark Lord static void mv_save_cached_regs(struct ata_port *ap)
95608da1759SMark Lord {
95708da1759SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
95808da1759SMark Lord 	struct mv_port_priv *pp = ap->private_data;
95908da1759SMark Lord 
960cae5a29dSMark Lord 	pp->cached.fiscfg = readl(port_mmio + FISCFG);
961cae5a29dSMark Lord 	pp->cached.ltmode = readl(port_mmio + LTMODE);
962cae5a29dSMark Lord 	pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND);
963cae5a29dSMark Lord 	pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD);
96408da1759SMark Lord }
96508da1759SMark Lord 
96608da1759SMark Lord /**
96708da1759SMark Lord  *      mv_write_cached_reg - write to a cached port register
96808da1759SMark Lord  *      @addr: hardware address of the register
96908da1759SMark Lord  *      @old: pointer to cached value of the register
97008da1759SMark Lord  *      @new: new value for the register
97108da1759SMark Lord  *
97208da1759SMark Lord  *	Write a new value to a cached register,
97308da1759SMark Lord  *	but only if the value is different from before.
97408da1759SMark Lord  */
97508da1759SMark Lord static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
97608da1759SMark Lord {
97708da1759SMark Lord 	if (new != *old) {
97812f3b6d7SMark Lord 		unsigned long laddr;
97908da1759SMark Lord 		*old = new;
98012f3b6d7SMark Lord 		/*
98112f3b6d7SMark Lord 		 * Workaround for 88SX60x1-B2 FEr SATA#13:
98212f3b6d7SMark Lord 		 * Read-after-write is needed to prevent generating 64-bit
98312f3b6d7SMark Lord 		 * write cycles on the PCI bus for SATA interface registers
98412f3b6d7SMark Lord 		 * at offsets ending in 0x4 or 0xc.
98512f3b6d7SMark Lord 		 *
98612f3b6d7SMark Lord 		 * Looks like a lot of fuss, but it avoids an unnecessary
98712f3b6d7SMark Lord 		 * +1 usec read-after-write delay for unaffected registers.
98812f3b6d7SMark Lord 		 */
98976bf3441SBen Dooks 		laddr = (unsigned long)addr & 0xffff;
99012f3b6d7SMark Lord 		if (laddr >= 0x300 && laddr <= 0x33c) {
99112f3b6d7SMark Lord 			laddr &= 0x000f;
99212f3b6d7SMark Lord 			if (laddr == 0x4 || laddr == 0xc) {
99312f3b6d7SMark Lord 				writelfl(new, addr); /* read after write */
99412f3b6d7SMark Lord 				return;
99512f3b6d7SMark Lord 			}
99612f3b6d7SMark Lord 		}
99712f3b6d7SMark Lord 		writel(new, addr); /* unaffected by the errata */
99808da1759SMark Lord 	}
99908da1759SMark Lord }
100008da1759SMark Lord 
1001c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
1002c5d3e45aSJeff Garzik 			     struct mv_host_priv *hpriv,
1003c5d3e45aSJeff Garzik 			     struct mv_port_priv *pp)
1004c5d3e45aSJeff Garzik {
1005bdd4dddeSJeff Garzik 	u32 index;
1006bdd4dddeSJeff Garzik 
1007c5d3e45aSJeff Garzik 	/*
1008c5d3e45aSJeff Garzik 	 * initialize request queue
1009c5d3e45aSJeff Garzik 	 */
1010fcfb1f77SMark Lord 	pp->req_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
1011fcfb1f77SMark Lord 	index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
1012bdd4dddeSJeff Garzik 
1013c5d3e45aSJeff Garzik 	WARN_ON(pp->crqb_dma & 0x3ff);
1014cae5a29dSMark Lord 	writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI);
1015bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
1016cae5a29dSMark Lord 		 port_mmio + EDMA_REQ_Q_IN_PTR);
1017cae5a29dSMark Lord 	writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR);
1018c5d3e45aSJeff Garzik 
1019c5d3e45aSJeff Garzik 	/*
1020c5d3e45aSJeff Garzik 	 * initialize response queue
1021c5d3e45aSJeff Garzik 	 */
1022fcfb1f77SMark Lord 	pp->resp_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
1023fcfb1f77SMark Lord 	index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
1024bdd4dddeSJeff Garzik 
1025c5d3e45aSJeff Garzik 	WARN_ON(pp->crpb_dma & 0xff);
1026cae5a29dSMark Lord 	writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI);
1027cae5a29dSMark Lord 	writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR);
1028bdd4dddeSJeff Garzik 	writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
1029cae5a29dSMark Lord 		 port_mmio + EDMA_RSP_Q_OUT_PTR);
1030c5d3e45aSJeff Garzik }
1031c5d3e45aSJeff Garzik 
10322b748a0aSMark Lord static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
10332b748a0aSMark Lord {
10342b748a0aSMark Lord 	/*
10352b748a0aSMark Lord 	 * When writing to the main_irq_mask in hardware,
10362b748a0aSMark Lord 	 * we must ensure exclusivity between the interrupt coalescing bits
10372b748a0aSMark Lord 	 * and the corresponding individual port DONE_IRQ bits.
10382b748a0aSMark Lord 	 *
10392b748a0aSMark Lord 	 * Note that this register is really an "IRQ enable" register,
10402b748a0aSMark Lord 	 * not an "IRQ mask" register as Marvell's naming might suggest.
10412b748a0aSMark Lord 	 */
10422b748a0aSMark Lord 	if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
10432b748a0aSMark Lord 		mask &= ~DONE_IRQ_0_3;
10442b748a0aSMark Lord 	if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
10452b748a0aSMark Lord 		mask &= ~DONE_IRQ_4_7;
10462b748a0aSMark Lord 	writelfl(mask, hpriv->main_irq_mask_addr);
10472b748a0aSMark Lord }
10482b748a0aSMark Lord 
1049c4de573bSMark Lord static void mv_set_main_irq_mask(struct ata_host *host,
1050c4de573bSMark Lord 				 u32 disable_bits, u32 enable_bits)
1051c4de573bSMark Lord {
1052c4de573bSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1053c4de573bSMark Lord 	u32 old_mask, new_mask;
1054c4de573bSMark Lord 
105596e2c487SMark Lord 	old_mask = hpriv->main_irq_mask;
1056c4de573bSMark Lord 	new_mask = (old_mask & ~disable_bits) | enable_bits;
105796e2c487SMark Lord 	if (new_mask != old_mask) {
105896e2c487SMark Lord 		hpriv->main_irq_mask = new_mask;
10592b748a0aSMark Lord 		mv_write_main_irq_mask(new_mask, hpriv);
1060c4de573bSMark Lord 	}
106196e2c487SMark Lord }
1062c4de573bSMark Lord 
1063c4de573bSMark Lord static void mv_enable_port_irqs(struct ata_port *ap,
1064c4de573bSMark Lord 				     unsigned int port_bits)
1065c4de573bSMark Lord {
1066c4de573bSMark Lord 	unsigned int shift, hardport, port = ap->port_no;
1067c4de573bSMark Lord 	u32 disable_bits, enable_bits;
1068c4de573bSMark Lord 
1069c4de573bSMark Lord 	MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
1070c4de573bSMark Lord 
1071c4de573bSMark Lord 	disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
1072c4de573bSMark Lord 	enable_bits  = port_bits << shift;
1073c4de573bSMark Lord 	mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
1074c4de573bSMark Lord }
1075c4de573bSMark Lord 
107600b81235SMark Lord static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
107700b81235SMark Lord 					  void __iomem *port_mmio,
107800b81235SMark Lord 					  unsigned int port_irqs)
1079c6fd2807SJeff Garzik {
10800c58912eSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1081352fab70SMark Lord 	int hardport = mv_hardport_from_port(ap->port_no);
10820c58912eSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(
1083b0bccb18SMark Lord 				mv_host_base(ap->host), ap->port_no);
1084cae6edc3SMark Lord 	u32 hc_irq_cause;
10850c58912eSMark Lord 
1086bdd4dddeSJeff Garzik 	/* clear EDMA event indicators, if any */
1087cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
1088bdd4dddeSJeff Garzik 
1089cae6edc3SMark Lord 	/* clear pending irq events */
1090cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1091cae5a29dSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
10920c58912eSMark Lord 
10930c58912eSMark Lord 	/* clear FIS IRQ Cause */
1094e4006077SMark Lord 	if (IS_GEN_IIE(hpriv))
1095cae5a29dSMark Lord 		writelfl(0, port_mmio + FIS_IRQ_CAUSE);
10960c58912eSMark Lord 
109700b81235SMark Lord 	mv_enable_port_irqs(ap, port_irqs);
109800b81235SMark Lord }
109900b81235SMark Lord 
11002b748a0aSMark Lord static void mv_set_irq_coalescing(struct ata_host *host,
11012b748a0aSMark Lord 				  unsigned int count, unsigned int usecs)
11022b748a0aSMark Lord {
11032b748a0aSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
11042b748a0aSMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
11052b748a0aSMark Lord 	u32 coal_enable = 0;
11062b748a0aSMark Lord 	unsigned long flags;
11076abf4678SMark Lord 	unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
11082b748a0aSMark Lord 	const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
11092b748a0aSMark Lord 							ALL_PORTS_COAL_DONE;
11102b748a0aSMark Lord 
11112b748a0aSMark Lord 	/* Disable IRQ coalescing if either threshold is zero */
11122b748a0aSMark Lord 	if (!usecs || !count) {
11132b748a0aSMark Lord 		clks = count = 0;
11142b748a0aSMark Lord 	} else {
11152b748a0aSMark Lord 		/* Respect maximum limits of the hardware */
11162b748a0aSMark Lord 		clks = usecs * COAL_CLOCKS_PER_USEC;
11172b748a0aSMark Lord 		if (clks > MAX_COAL_TIME_THRESHOLD)
11182b748a0aSMark Lord 			clks = MAX_COAL_TIME_THRESHOLD;
11192b748a0aSMark Lord 		if (count > MAX_COAL_IO_COUNT)
11202b748a0aSMark Lord 			count = MAX_COAL_IO_COUNT;
11212b748a0aSMark Lord 	}
11222b748a0aSMark Lord 
11232b748a0aSMark Lord 	spin_lock_irqsave(&host->lock, flags);
11246abf4678SMark Lord 	mv_set_main_irq_mask(host, coal_disable, 0);
11252b748a0aSMark Lord 
11266abf4678SMark Lord 	if (is_dual_hc && !IS_GEN_I(hpriv)) {
11272b748a0aSMark Lord 		/*
11286abf4678SMark Lord 		 * GEN_II/GEN_IIE with dual host controllers:
11296abf4678SMark Lord 		 * one set of global thresholds for the entire chip.
11302b748a0aSMark Lord 		 */
1131cae5a29dSMark Lord 		writel(clks,  mmio + IRQ_COAL_TIME_THRESHOLD);
1132cae5a29dSMark Lord 		writel(count, mmio + IRQ_COAL_IO_THRESHOLD);
11332b748a0aSMark Lord 		/* clear leftover coal IRQ bit */
1134cae5a29dSMark Lord 		writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
11356abf4678SMark Lord 		if (count)
11362b748a0aSMark Lord 			coal_enable = ALL_PORTS_COAL_DONE;
11376abf4678SMark Lord 		clks = count = 0; /* force clearing of regular regs below */
11382b748a0aSMark Lord 	}
11396abf4678SMark Lord 
11402b748a0aSMark Lord 	/*
11412b748a0aSMark Lord 	 * All chips: independent thresholds for each HC on the chip.
11422b748a0aSMark Lord 	 */
11432b748a0aSMark Lord 	hc_mmio = mv_hc_base_from_port(mmio, 0);
1144cae5a29dSMark Lord 	writel(clks,  hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1145cae5a29dSMark Lord 	writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1146cae5a29dSMark Lord 	writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11476abf4678SMark Lord 	if (count)
11482b748a0aSMark Lord 		coal_enable |= PORTS_0_3_COAL_DONE;
11496abf4678SMark Lord 	if (is_dual_hc) {
11502b748a0aSMark Lord 		hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
1151cae5a29dSMark Lord 		writel(clks,  hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1152cae5a29dSMark Lord 		writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1153cae5a29dSMark Lord 		writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11546abf4678SMark Lord 		if (count)
11552b748a0aSMark Lord 			coal_enable |= PORTS_4_7_COAL_DONE;
11562b748a0aSMark Lord 	}
11572b748a0aSMark Lord 
11586abf4678SMark Lord 	mv_set_main_irq_mask(host, 0, coal_enable);
11592b748a0aSMark Lord 	spin_unlock_irqrestore(&host->lock, flags);
11602b748a0aSMark Lord }
11612b748a0aSMark Lord 
116200b81235SMark Lord /**
116300b81235SMark Lord  *      mv_start_edma - Enable eDMA engine
116400b81235SMark Lord  *      @base: port base address
116500b81235SMark Lord  *      @pp: port private data
116600b81235SMark Lord  *
116700b81235SMark Lord  *      Verify the local cache of the eDMA state is accurate with a
116800b81235SMark Lord  *      WARN_ON.
116900b81235SMark Lord  *
117000b81235SMark Lord  *      LOCKING:
117100b81235SMark Lord  *      Inherited from caller.
117200b81235SMark Lord  */
117300b81235SMark Lord static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
117400b81235SMark Lord 			 struct mv_port_priv *pp, u8 protocol)
117500b81235SMark Lord {
117600b81235SMark Lord 	int want_ncq = (protocol == ATA_PROT_NCQ);
117700b81235SMark Lord 
117800b81235SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
117900b81235SMark Lord 		int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
118000b81235SMark Lord 		if (want_ncq != using_ncq)
118100b81235SMark Lord 			mv_stop_edma(ap);
118200b81235SMark Lord 	}
118300b81235SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
118400b81235SMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
118500b81235SMark Lord 
118600b81235SMark Lord 		mv_edma_cfg(ap, want_ncq, 1);
118700b81235SMark Lord 
1188f630d562SMark Lord 		mv_set_edma_ptrs(port_mmio, hpriv, pp);
118900b81235SMark Lord 		mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
1190bdd4dddeSJeff Garzik 
1191cae5a29dSMark Lord 		writelfl(EDMA_EN, port_mmio + EDMA_CMD);
1192c6fd2807SJeff Garzik 		pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
1193c6fd2807SJeff Garzik 	}
1194c6fd2807SJeff Garzik }
1195c6fd2807SJeff Garzik 
11969b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
11979b2c4e0bSMark Lord {
11989b2c4e0bSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
11999b2c4e0bSMark Lord 	const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
12009b2c4e0bSMark Lord 	const int per_loop = 5, timeout = (15 * 1000 / per_loop);
12019b2c4e0bSMark Lord 	int i;
12029b2c4e0bSMark Lord 
12039b2c4e0bSMark Lord 	/*
12049b2c4e0bSMark Lord 	 * Wait for the EDMA engine to finish transactions in progress.
1205c46938ccSMark Lord 	 * No idea what a good "timeout" value might be, but measurements
1206c46938ccSMark Lord 	 * indicate that it often requires hundreds of microseconds
1207c46938ccSMark Lord 	 * with two drives in-use.  So we use the 15msec value above
1208c46938ccSMark Lord 	 * as a rough guess at what even more drives might require.
12099b2c4e0bSMark Lord 	 */
12109b2c4e0bSMark Lord 	for (i = 0; i < timeout; ++i) {
1211cae5a29dSMark Lord 		u32 edma_stat = readl(port_mmio + EDMA_STATUS);
12129b2c4e0bSMark Lord 		if ((edma_stat & empty_idle) == empty_idle)
12139b2c4e0bSMark Lord 			break;
12149b2c4e0bSMark Lord 		udelay(per_loop);
12159b2c4e0bSMark Lord 	}
1216a9a79dfeSJoe Perches 	/* ata_port_info(ap, "%s: %u+ usecs\n", __func__, i); */
12179b2c4e0bSMark Lord }
12189b2c4e0bSMark Lord 
1219c6fd2807SJeff Garzik /**
1220e12bef50SMark Lord  *      mv_stop_edma_engine - Disable eDMA engine
1221b562468cSMark Lord  *      @port_mmio: io base address
1222c6fd2807SJeff Garzik  *
1223c6fd2807SJeff Garzik  *      LOCKING:
1224c6fd2807SJeff Garzik  *      Inherited from caller.
1225c6fd2807SJeff Garzik  */
1226b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
1227c6fd2807SJeff Garzik {
1228b562468cSMark Lord 	int i;
1229c6fd2807SJeff Garzik 
1230b562468cSMark Lord 	/* Disable eDMA.  The disable bit auto clears. */
1231cae5a29dSMark Lord 	writelfl(EDMA_DS, port_mmio + EDMA_CMD);
1232c6fd2807SJeff Garzik 
1233b562468cSMark Lord 	/* Wait for the chip to confirm eDMA is off. */
1234b562468cSMark Lord 	for (i = 10000; i > 0; i--) {
1235cae5a29dSMark Lord 		u32 reg = readl(port_mmio + EDMA_CMD);
12364537deb5SJeff Garzik 		if (!(reg & EDMA_EN))
1237b562468cSMark Lord 			return 0;
1238b562468cSMark Lord 		udelay(10);
1239c6fd2807SJeff Garzik 	}
1240b562468cSMark Lord 	return -EIO;
1241c6fd2807SJeff Garzik }
1242c6fd2807SJeff Garzik 
1243e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
1244c6fd2807SJeff Garzik {
1245c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1246c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
124766e57a2cSMark Lord 	int err = 0;
1248c6fd2807SJeff Garzik 
1249b562468cSMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
1250b562468cSMark Lord 		return 0;
1251c6fd2807SJeff Garzik 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
12529b2c4e0bSMark Lord 	mv_wait_for_edma_empty_idle(ap);
1253b562468cSMark Lord 	if (mv_stop_edma_engine(port_mmio)) {
1254a9a79dfeSJoe Perches 		ata_port_err(ap, "Unable to stop eDMA\n");
125566e57a2cSMark Lord 		err = -EIO;
1256c6fd2807SJeff Garzik 	}
125766e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
125866e57a2cSMark Lord 	return err;
12590ea9e179SJeff Garzik }
12600ea9e179SJeff Garzik 
1261c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1262c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes)
1263c6fd2807SJeff Garzik {
1264c6fd2807SJeff Garzik 	int b, w;
1265c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
1266c6fd2807SJeff Garzik 		DPRINTK("%p: ", start + b);
1267c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
1268c6fd2807SJeff Garzik 			printk("%08x ", readl(start + b));
1269c6fd2807SJeff Garzik 			b += sizeof(u32);
1270c6fd2807SJeff Garzik 		}
1271c6fd2807SJeff Garzik 		printk("\n");
1272c6fd2807SJeff Garzik 	}
1273c6fd2807SJeff Garzik }
1274c6fd2807SJeff Garzik #endif
127513b74085SAndrew Lunn #if defined(ATA_DEBUG) || defined(CONFIG_PCI)
1276c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
1277c6fd2807SJeff Garzik {
1278c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1279c6fd2807SJeff Garzik 	int b, w;
1280c6fd2807SJeff Garzik 	u32 dw;
1281c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
1282c6fd2807SJeff Garzik 		DPRINTK("%02x: ", b);
1283c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
1284c6fd2807SJeff Garzik 			(void) pci_read_config_dword(pdev, b, &dw);
1285c6fd2807SJeff Garzik 			printk("%08x ", dw);
1286c6fd2807SJeff Garzik 			b += sizeof(u32);
1287c6fd2807SJeff Garzik 		}
1288c6fd2807SJeff Garzik 		printk("\n");
1289c6fd2807SJeff Garzik 	}
1290c6fd2807SJeff Garzik #endif
1291c6fd2807SJeff Garzik }
129213b74085SAndrew Lunn #endif
1293c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port,
1294c6fd2807SJeff Garzik 			     struct pci_dev *pdev)
1295c6fd2807SJeff Garzik {
1296c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1297c6fd2807SJeff Garzik 	void __iomem *hc_base = mv_hc_base(mmio_base,
1298c6fd2807SJeff Garzik 					   port >> MV_PORT_HC_SHIFT);
1299c6fd2807SJeff Garzik 	void __iomem *port_base;
1300c6fd2807SJeff Garzik 	int start_port, num_ports, p, start_hc, num_hcs, hc;
1301c6fd2807SJeff Garzik 
1302c6fd2807SJeff Garzik 	if (0 > port) {
1303c6fd2807SJeff Garzik 		start_hc = start_port = 0;
1304c6fd2807SJeff Garzik 		num_ports = 8;		/* shld be benign for 4 port devs */
1305c6fd2807SJeff Garzik 		num_hcs = 2;
1306c6fd2807SJeff Garzik 	} else {
1307c6fd2807SJeff Garzik 		start_hc = port >> MV_PORT_HC_SHIFT;
1308c6fd2807SJeff Garzik 		start_port = port;
1309c6fd2807SJeff Garzik 		num_ports = num_hcs = 1;
1310c6fd2807SJeff Garzik 	}
1311c6fd2807SJeff Garzik 	DPRINTK("All registers for port(s) %u-%u:\n", start_port,
1312c6fd2807SJeff Garzik 		num_ports > 1 ? num_ports - 1 : start_port);
1313c6fd2807SJeff Garzik 
1314c6fd2807SJeff Garzik 	if (NULL != pdev) {
1315c6fd2807SJeff Garzik 		DPRINTK("PCI config space regs:\n");
1316c6fd2807SJeff Garzik 		mv_dump_pci_cfg(pdev, 0x68);
1317c6fd2807SJeff Garzik 	}
1318c6fd2807SJeff Garzik 	DPRINTK("PCI regs:\n");
1319c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xc00, 0x3c);
1320c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xd00, 0x34);
1321c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xf00, 0x4);
1322c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0x1d00, 0x6c);
1323c6fd2807SJeff Garzik 	for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
1324c6fd2807SJeff Garzik 		hc_base = mv_hc_base(mmio_base, hc);
1325c6fd2807SJeff Garzik 		DPRINTK("HC regs (HC %i):\n", hc);
1326c6fd2807SJeff Garzik 		mv_dump_mem(hc_base, 0x1c);
1327c6fd2807SJeff Garzik 	}
1328c6fd2807SJeff Garzik 	for (p = start_port; p < start_port + num_ports; p++) {
1329c6fd2807SJeff Garzik 		port_base = mv_port_base(mmio_base, p);
1330c6fd2807SJeff Garzik 		DPRINTK("EDMA regs (port %i):\n", p);
1331c6fd2807SJeff Garzik 		mv_dump_mem(port_base, 0x54);
1332c6fd2807SJeff Garzik 		DPRINTK("SATA regs (port %i):\n", p);
1333c6fd2807SJeff Garzik 		mv_dump_mem(port_base+0x300, 0x60);
1334c6fd2807SJeff Garzik 	}
1335c6fd2807SJeff Garzik #endif
1336c6fd2807SJeff Garzik }
1337c6fd2807SJeff Garzik 
1338c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1339c6fd2807SJeff Garzik {
1340c6fd2807SJeff Garzik 	unsigned int ofs;
1341c6fd2807SJeff Garzik 
1342c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1343c6fd2807SJeff Garzik 	case SCR_STATUS:
1344c6fd2807SJeff Garzik 	case SCR_CONTROL:
1345c6fd2807SJeff Garzik 	case SCR_ERROR:
1346cae5a29dSMark Lord 		ofs = SATA_STATUS + (sc_reg_in * sizeof(u32));
1347c6fd2807SJeff Garzik 		break;
1348c6fd2807SJeff Garzik 	case SCR_ACTIVE:
1349cae5a29dSMark Lord 		ofs = SATA_ACTIVE;   /* active is not with the others */
1350c6fd2807SJeff Garzik 		break;
1351c6fd2807SJeff Garzik 	default:
1352c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1353c6fd2807SJeff Garzik 		break;
1354c6fd2807SJeff Garzik 	}
1355c6fd2807SJeff Garzik 	return ofs;
1356c6fd2807SJeff Garzik }
1357c6fd2807SJeff Garzik 
135882ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
1359c6fd2807SJeff Garzik {
1360c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1361c6fd2807SJeff Garzik 
1362da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
136382ef04fbSTejun Heo 		*val = readl(mv_ap_base(link->ap) + ofs);
1364da3dbb17STejun Heo 		return 0;
1365da3dbb17STejun Heo 	} else
1366da3dbb17STejun Heo 		return -EINVAL;
1367c6fd2807SJeff Garzik }
1368c6fd2807SJeff Garzik 
136982ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
1370c6fd2807SJeff Garzik {
1371c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1372c6fd2807SJeff Garzik 
1373da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
137420091773SMark Lord 		void __iomem *addr = mv_ap_base(link->ap) + ofs;
13759013d64eSLior Amsalem 		struct mv_host_priv *hpriv = link->ap->host->private_data;
137620091773SMark Lord 		if (sc_reg_in == SCR_CONTROL) {
137720091773SMark Lord 			/*
137820091773SMark Lord 			 * Workaround for 88SX60x1 FEr SATA#26:
137920091773SMark Lord 			 *
138025985edcSLucas De Marchi 			 * COMRESETs have to take care not to accidentally
138120091773SMark Lord 			 * put the drive to sleep when writing SCR_CONTROL.
138220091773SMark Lord 			 * Setting bits 12..15 prevents this problem.
138320091773SMark Lord 			 *
138420091773SMark Lord 			 * So if we see an outbound COMMRESET, set those bits.
138520091773SMark Lord 			 * Ditto for the followup write that clears the reset.
138620091773SMark Lord 			 *
138720091773SMark Lord 			 * The proprietary driver does this for
138820091773SMark Lord 			 * all chip versions, and so do we.
138920091773SMark Lord 			 */
139020091773SMark Lord 			if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1)
139120091773SMark Lord 				val |= 0xf000;
13929013d64eSLior Amsalem 
13939013d64eSLior Amsalem 			if (hpriv->hp_flags & MV_HP_FIX_LP_PHY_CTL) {
13949013d64eSLior Amsalem 				void __iomem *lp_phy_addr =
13959013d64eSLior Amsalem 					mv_ap_base(link->ap) + LP_PHY_CTL;
13969013d64eSLior Amsalem 				/*
13979013d64eSLior Amsalem 				 * Set PHY speed according to SControl speed.
13989013d64eSLior Amsalem 				 */
13993661aa99SThomas Petazzoni 				u32 lp_phy_val =
14003661aa99SThomas Petazzoni 					LP_PHY_CTL_PIN_PU_PLL |
14013661aa99SThomas Petazzoni 					LP_PHY_CTL_PIN_PU_RX  |
14023661aa99SThomas Petazzoni 					LP_PHY_CTL_PIN_PU_TX;
14033661aa99SThomas Petazzoni 
14043661aa99SThomas Petazzoni 				if ((val & 0xf0) != 0x10)
14053661aa99SThomas Petazzoni 					lp_phy_val |=
14063661aa99SThomas Petazzoni 						LP_PHY_CTL_GEN_TX_3G |
14073661aa99SThomas Petazzoni 						LP_PHY_CTL_GEN_RX_3G;
14083661aa99SThomas Petazzoni 
14093661aa99SThomas Petazzoni 				writelfl(lp_phy_val, lp_phy_addr);
14109013d64eSLior Amsalem 			}
141120091773SMark Lord 		}
141220091773SMark Lord 		writelfl(val, addr);
1413da3dbb17STejun Heo 		return 0;
1414da3dbb17STejun Heo 	} else
1415da3dbb17STejun Heo 		return -EINVAL;
1416c6fd2807SJeff Garzik }
1417c6fd2807SJeff Garzik 
1418f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1419f273827eSMark Lord {
1420f273827eSMark Lord 	/*
1421e49856d8SMark Lord 	 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1422e49856d8SMark Lord 	 *
1423e49856d8SMark Lord 	 * Gen-II does not support NCQ over a port multiplier
1424e49856d8SMark Lord 	 *  (no FIS-based switching).
1425f273827eSMark Lord 	 */
1426e49856d8SMark Lord 	if (adev->flags & ATA_DFLAG_NCQ) {
1427352fab70SMark Lord 		if (sata_pmp_attached(adev->link->ap)) {
1428e49856d8SMark Lord 			adev->flags &= ~ATA_DFLAG_NCQ;
1429a9a79dfeSJoe Perches 			ata_dev_info(adev,
1430352fab70SMark Lord 				"NCQ disabled for command-based switching\n");
1431352fab70SMark Lord 		}
1432f273827eSMark Lord 	}
1433e49856d8SMark Lord }
1434f273827eSMark Lord 
14353e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc)
14363e4a1391SMark Lord {
14373e4a1391SMark Lord 	struct ata_link *link = qc->dev->link;
14383e4a1391SMark Lord 	struct ata_port *ap = link->ap;
14393e4a1391SMark Lord 	struct mv_port_priv *pp = ap->private_data;
14403e4a1391SMark Lord 
14413e4a1391SMark Lord 	/*
144229d187bbSMark Lord 	 * Don't allow new commands if we're in a delayed EH state
144329d187bbSMark Lord 	 * for NCQ and/or FIS-based switching.
144429d187bbSMark Lord 	 */
144529d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
144629d187bbSMark Lord 		return ATA_DEFER_PORT;
1447159a7ff7SGwendal Grignou 
1448159a7ff7SGwendal Grignou 	/* PIO commands need exclusive link: no other commands [DMA or PIO]
1449159a7ff7SGwendal Grignou 	 * can run concurrently.
1450159a7ff7SGwendal Grignou 	 * set excl_link when we want to send a PIO command in DMA mode
1451159a7ff7SGwendal Grignou 	 * or a non-NCQ command in NCQ mode.
1452159a7ff7SGwendal Grignou 	 * When we receive a command from that link, and there are no
1453159a7ff7SGwendal Grignou 	 * outstanding commands, mark a flag to clear excl_link and let
1454159a7ff7SGwendal Grignou 	 * the command go through.
1455159a7ff7SGwendal Grignou 	 */
1456159a7ff7SGwendal Grignou 	if (unlikely(ap->excl_link)) {
1457159a7ff7SGwendal Grignou 		if (link == ap->excl_link) {
1458159a7ff7SGwendal Grignou 			if (ap->nr_active_links)
1459159a7ff7SGwendal Grignou 				return ATA_DEFER_PORT;
1460159a7ff7SGwendal Grignou 			qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
1461159a7ff7SGwendal Grignou 			return 0;
1462159a7ff7SGwendal Grignou 		} else
1463159a7ff7SGwendal Grignou 			return ATA_DEFER_PORT;
1464159a7ff7SGwendal Grignou 	}
1465159a7ff7SGwendal Grignou 
146629d187bbSMark Lord 	/*
14673e4a1391SMark Lord 	 * If the port is completely idle, then allow the new qc.
14683e4a1391SMark Lord 	 */
14693e4a1391SMark Lord 	if (ap->nr_active_links == 0)
14703e4a1391SMark Lord 		return 0;
14713e4a1391SMark Lord 
14723e4a1391SMark Lord 	/*
14734bdee6c5STejun Heo 	 * The port is operating in host queuing mode (EDMA) with NCQ
14744bdee6c5STejun Heo 	 * enabled, allow multiple NCQ commands.  EDMA also allows
14754bdee6c5STejun Heo 	 * queueing multiple DMA commands but libata core currently
14764bdee6c5STejun Heo 	 * doesn't allow it.
14773e4a1391SMark Lord 	 */
14784bdee6c5STejun Heo 	if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
1479159a7ff7SGwendal Grignou 	    (pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
1480159a7ff7SGwendal Grignou 		if (ata_is_ncq(qc->tf.protocol))
14813e4a1391SMark Lord 			return 0;
1482159a7ff7SGwendal Grignou 		else {
1483159a7ff7SGwendal Grignou 			ap->excl_link = link;
1484159a7ff7SGwendal Grignou 			return ATA_DEFER_PORT;
1485159a7ff7SGwendal Grignou 		}
1486159a7ff7SGwendal Grignou 	}
14874bdee6c5STejun Heo 
14883e4a1391SMark Lord 	return ATA_DEFER_PORT;
14893e4a1391SMark Lord }
14903e4a1391SMark Lord 
149108da1759SMark Lord static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
1492e49856d8SMark Lord {
149308da1759SMark Lord 	struct mv_port_priv *pp = ap->private_data;
149408da1759SMark Lord 	void __iomem *port_mmio;
149500f42eabSMark Lord 
149608da1759SMark Lord 	u32 fiscfg,   *old_fiscfg   = &pp->cached.fiscfg;
149708da1759SMark Lord 	u32 ltmode,   *old_ltmode   = &pp->cached.ltmode;
149808da1759SMark Lord 	u32 haltcond, *old_haltcond = &pp->cached.haltcond;
149900f42eabSMark Lord 
150008da1759SMark Lord 	ltmode   = *old_ltmode & ~LTMODE_BIT8;
150108da1759SMark Lord 	haltcond = *old_haltcond | EDMA_ERR_DEV;
150200f42eabSMark Lord 
150300f42eabSMark Lord 	if (want_fbs) {
150408da1759SMark Lord 		fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
150508da1759SMark Lord 		ltmode = *old_ltmode | LTMODE_BIT8;
15064c299ca3SMark Lord 		if (want_ncq)
150708da1759SMark Lord 			haltcond &= ~EDMA_ERR_DEV;
15084c299ca3SMark Lord 		else
150908da1759SMark Lord 			fiscfg |=  FISCFG_WAIT_DEV_ERR;
151008da1759SMark Lord 	} else {
151108da1759SMark Lord 		fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
1512e49856d8SMark Lord 	}
151300f42eabSMark Lord 
151408da1759SMark Lord 	port_mmio = mv_ap_base(ap);
1515cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + FISCFG, old_fiscfg, fiscfg);
1516cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + LTMODE, old_ltmode, ltmode);
1517cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + EDMA_HALTCOND, old_haltcond, haltcond);
1518e49856d8SMark Lord }
1519c6fd2807SJeff Garzik 
1520dd2890f6SMark Lord static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1521dd2890f6SMark Lord {
1522dd2890f6SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1523dd2890f6SMark Lord 	u32 old, new;
1524dd2890f6SMark Lord 
1525dd2890f6SMark Lord 	/* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1526cae5a29dSMark Lord 	old = readl(hpriv->base + GPIO_PORT_CTL);
1527dd2890f6SMark Lord 	if (want_ncq)
1528dd2890f6SMark Lord 		new = old | (1 << 22);
1529dd2890f6SMark Lord 	else
1530dd2890f6SMark Lord 		new = old & ~(1 << 22);
1531dd2890f6SMark Lord 	if (new != old)
1532cae5a29dSMark Lord 		writel(new, hpriv->base + GPIO_PORT_CTL);
1533dd2890f6SMark Lord }
1534dd2890f6SMark Lord 
1535c01e8a23SMark Lord /**
1536c01e8a23SMark Lord  *	mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
1537c01e8a23SMark Lord  *	@ap: Port being initialized
1538c01e8a23SMark Lord  *
1539c01e8a23SMark Lord  *	There are two DMA modes on these chips:  basic DMA, and EDMA.
1540c01e8a23SMark Lord  *
1541c01e8a23SMark Lord  *	Bit-0 of the "EDMA RESERVED" register enables/disables use
1542c01e8a23SMark Lord  *	of basic DMA on the GEN_IIE versions of the chips.
1543c01e8a23SMark Lord  *
1544c01e8a23SMark Lord  *	This bit survives EDMA resets, and must be set for basic DMA
1545c01e8a23SMark Lord  *	to function, and should be cleared when EDMA is active.
1546c01e8a23SMark Lord  */
1547c01e8a23SMark Lord static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
1548c01e8a23SMark Lord {
1549c01e8a23SMark Lord 	struct mv_port_priv *pp = ap->private_data;
1550c01e8a23SMark Lord 	u32 new, *old = &pp->cached.unknown_rsvd;
1551c01e8a23SMark Lord 
1552c01e8a23SMark Lord 	if (enable_bmdma)
1553c01e8a23SMark Lord 		new = *old | 1;
1554c01e8a23SMark Lord 	else
1555c01e8a23SMark Lord 		new = *old & ~1;
1556cae5a29dSMark Lord 	mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD, old, new);
1557c01e8a23SMark Lord }
1558c01e8a23SMark Lord 
1559000b344fSMark Lord /*
1560000b344fSMark Lord  * SOC chips have an issue whereby the HDD LEDs don't always blink
1561000b344fSMark Lord  * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
1562000b344fSMark Lord  * of the SOC takes care of it, generating a steady blink rate when
1563000b344fSMark Lord  * any drive on the chip is active.
1564000b344fSMark Lord  *
1565000b344fSMark Lord  * Unfortunately, the blink mode is a global hardware setting for the SOC,
1566000b344fSMark Lord  * so we must use it whenever at least one port on the SOC has NCQ enabled.
1567000b344fSMark Lord  *
1568000b344fSMark Lord  * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
1569000b344fSMark Lord  * LED operation works then, and provides better (more accurate) feedback.
1570000b344fSMark Lord  *
1571000b344fSMark Lord  * Note that this code assumes that an SOC never has more than one HC onboard.
1572000b344fSMark Lord  */
1573000b344fSMark Lord static void mv_soc_led_blink_enable(struct ata_port *ap)
1574000b344fSMark Lord {
1575000b344fSMark Lord 	struct ata_host *host = ap->host;
1576000b344fSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1577000b344fSMark Lord 	void __iomem *hc_mmio;
1578000b344fSMark Lord 	u32 led_ctrl;
1579000b344fSMark Lord 
1580000b344fSMark Lord 	if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
1581000b344fSMark Lord 		return;
1582000b344fSMark Lord 	hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN;
1583000b344fSMark Lord 	hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1584cae5a29dSMark Lord 	led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1585cae5a29dSMark Lord 	writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1586000b344fSMark Lord }
1587000b344fSMark Lord 
1588000b344fSMark Lord static void mv_soc_led_blink_disable(struct ata_port *ap)
1589000b344fSMark Lord {
1590000b344fSMark Lord 	struct ata_host *host = ap->host;
1591000b344fSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1592000b344fSMark Lord 	void __iomem *hc_mmio;
1593000b344fSMark Lord 	u32 led_ctrl;
1594000b344fSMark Lord 	unsigned int port;
1595000b344fSMark Lord 
1596000b344fSMark Lord 	if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
1597000b344fSMark Lord 		return;
1598000b344fSMark Lord 
1599000b344fSMark Lord 	/* disable led-blink only if no ports are using NCQ */
1600000b344fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
1601000b344fSMark Lord 		struct ata_port *this_ap = host->ports[port];
1602000b344fSMark Lord 		struct mv_port_priv *pp = this_ap->private_data;
1603000b344fSMark Lord 
1604000b344fSMark Lord 		if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1605000b344fSMark Lord 			return;
1606000b344fSMark Lord 	}
1607000b344fSMark Lord 
1608000b344fSMark Lord 	hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
1609000b344fSMark Lord 	hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1610cae5a29dSMark Lord 	led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1611cae5a29dSMark Lord 	writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1612000b344fSMark Lord }
1613000b344fSMark Lord 
161400b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
1615c6fd2807SJeff Garzik {
1616c6fd2807SJeff Garzik 	u32 cfg;
1617e12bef50SMark Lord 	struct mv_port_priv *pp    = ap->private_data;
1618e12bef50SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1619e12bef50SMark Lord 	void __iomem *port_mmio    = mv_ap_base(ap);
1620c6fd2807SJeff Garzik 
1621c6fd2807SJeff Garzik 	/* set up non-NCQ EDMA configuration */
1622c6fd2807SJeff Garzik 	cfg = EDMA_CFG_Q_DEPTH;		/* always 0x1f for *all* chips */
1623d16ab3f6SMark Lord 	pp->pp_flags &=
1624d16ab3f6SMark Lord 	  ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
1625c6fd2807SJeff Garzik 
1626c6fd2807SJeff Garzik 	if (IS_GEN_I(hpriv))
1627c6fd2807SJeff Garzik 		cfg |= (1 << 8);	/* enab config burst size mask */
1628c6fd2807SJeff Garzik 
1629dd2890f6SMark Lord 	else if (IS_GEN_II(hpriv)) {
1630c6fd2807SJeff Garzik 		cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1631dd2890f6SMark Lord 		mv_60x1_errata_sata25(ap, want_ncq);
1632c6fd2807SJeff Garzik 
1633dd2890f6SMark Lord 	} else if (IS_GEN_IIE(hpriv)) {
163400f42eabSMark Lord 		int want_fbs = sata_pmp_attached(ap);
163500f42eabSMark Lord 		/*
163600f42eabSMark Lord 		 * Possible future enhancement:
163700f42eabSMark Lord 		 *
163800f42eabSMark Lord 		 * The chip can use FBS with non-NCQ, if we allow it,
163900f42eabSMark Lord 		 * But first we need to have the error handling in place
164000f42eabSMark Lord 		 * for this mode (datasheet section 7.3.15.4.2.3).
164100f42eabSMark Lord 		 * So disallow non-NCQ FBS for now.
164200f42eabSMark Lord 		 */
164300f42eabSMark Lord 		want_fbs &= want_ncq;
164400f42eabSMark Lord 
164508da1759SMark Lord 		mv_config_fbs(ap, want_ncq, want_fbs);
164600f42eabSMark Lord 
164700f42eabSMark Lord 		if (want_fbs) {
164800f42eabSMark Lord 			pp->pp_flags |= MV_PP_FLAG_FBS_EN;
164900f42eabSMark Lord 			cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
165000f42eabSMark Lord 		}
165100f42eabSMark Lord 
1652e728eabeSJeff Garzik 		cfg |= (1 << 23);	/* do not mask PM field in rx'd FIS */
165300b81235SMark Lord 		if (want_edma) {
1654e728eabeSJeff Garzik 			cfg |= (1 << 22); /* enab 4-entry host queue cache */
16551f398472SMark Lord 			if (!IS_SOC(hpriv))
1656c6fd2807SJeff Garzik 				cfg |= (1 << 18); /* enab early completion */
165700b81235SMark Lord 		}
1658616d4a98SMark Lord 		if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1659616d4a98SMark Lord 			cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
1660c01e8a23SMark Lord 		mv_bmdma_enable_iie(ap, !want_edma);
1661000b344fSMark Lord 
1662000b344fSMark Lord 		if (IS_SOC(hpriv)) {
1663000b344fSMark Lord 			if (want_ncq)
1664000b344fSMark Lord 				mv_soc_led_blink_enable(ap);
1665000b344fSMark Lord 			else
1666000b344fSMark Lord 				mv_soc_led_blink_disable(ap);
1667000b344fSMark Lord 		}
1668c6fd2807SJeff Garzik 	}
1669c6fd2807SJeff Garzik 
167072109168SMark Lord 	if (want_ncq) {
167172109168SMark Lord 		cfg |= EDMA_CFG_NCQ;
167272109168SMark Lord 		pp->pp_flags |=  MV_PP_FLAG_NCQ_EN;
167300b81235SMark Lord 	}
167472109168SMark Lord 
1675cae5a29dSMark Lord 	writelfl(cfg, port_mmio + EDMA_CFG);
1676c6fd2807SJeff Garzik }
1677c6fd2807SJeff Garzik 
1678da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1679da2fa9baSMark Lord {
1680da2fa9baSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1681da2fa9baSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1682eb73d558SMark Lord 	int tag;
1683da2fa9baSMark Lord 
1684da2fa9baSMark Lord 	if (pp->crqb) {
1685da2fa9baSMark Lord 		dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1686da2fa9baSMark Lord 		pp->crqb = NULL;
1687da2fa9baSMark Lord 	}
1688da2fa9baSMark Lord 	if (pp->crpb) {
1689da2fa9baSMark Lord 		dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1690da2fa9baSMark Lord 		pp->crpb = NULL;
1691da2fa9baSMark Lord 	}
1692eb73d558SMark Lord 	/*
1693eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1694eb73d558SMark Lord 	 * For later hardware, we have one unique sg_tbl per NCQ tag.
1695eb73d558SMark Lord 	 */
1696eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1697eb73d558SMark Lord 		if (pp->sg_tbl[tag]) {
1698eb73d558SMark Lord 			if (tag == 0 || !IS_GEN_I(hpriv))
1699eb73d558SMark Lord 				dma_pool_free(hpriv->sg_tbl_pool,
1700eb73d558SMark Lord 					      pp->sg_tbl[tag],
1701eb73d558SMark Lord 					      pp->sg_tbl_dma[tag]);
1702eb73d558SMark Lord 			pp->sg_tbl[tag] = NULL;
1703eb73d558SMark Lord 		}
1704da2fa9baSMark Lord 	}
1705da2fa9baSMark Lord }
1706da2fa9baSMark Lord 
1707c6fd2807SJeff Garzik /**
1708c6fd2807SJeff Garzik  *      mv_port_start - Port specific init/start routine.
1709c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1710c6fd2807SJeff Garzik  *
1711c6fd2807SJeff Garzik  *      Allocate and point to DMA memory, init port private memory,
1712c6fd2807SJeff Garzik  *      zero indices.
1713c6fd2807SJeff Garzik  *
1714c6fd2807SJeff Garzik  *      LOCKING:
1715c6fd2807SJeff Garzik  *      Inherited from caller.
1716c6fd2807SJeff Garzik  */
1717c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1718c6fd2807SJeff Garzik {
1719cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1720cca3974eSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1721c6fd2807SJeff Garzik 	struct mv_port_priv *pp;
1722933cb8e5SMark Lord 	unsigned long flags;
1723dde20207SJames Bottomley 	int tag;
1724c6fd2807SJeff Garzik 
172524dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1726c6fd2807SJeff Garzik 	if (!pp)
172724dc5f33STejun Heo 		return -ENOMEM;
1728da2fa9baSMark Lord 	ap->private_data = pp;
1729c6fd2807SJeff Garzik 
1730*6ec76070SHarman Kalra 	pp->crqb = dma_pool_zalloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1731da2fa9baSMark Lord 	if (!pp->crqb)
1732da2fa9baSMark Lord 		return -ENOMEM;
1733c6fd2807SJeff Garzik 
1734*6ec76070SHarman Kalra 	pp->crpb = dma_pool_zalloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1735da2fa9baSMark Lord 	if (!pp->crpb)
1736da2fa9baSMark Lord 		goto out_port_free_dma_mem;
1737c6fd2807SJeff Garzik 
17383bd0a70eSMark Lord 	/* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
17393bd0a70eSMark Lord 	if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
17403bd0a70eSMark Lord 		ap->flags |= ATA_FLAG_AN;
1741eb73d558SMark Lord 	/*
1742eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1743eb73d558SMark Lord 	 * For later hardware, we need one unique sg_tbl per NCQ tag.
1744eb73d558SMark Lord 	 */
1745eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1746eb73d558SMark Lord 		if (tag == 0 || !IS_GEN_I(hpriv)) {
1747eb73d558SMark Lord 			pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1748eb73d558SMark Lord 					      GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1749eb73d558SMark Lord 			if (!pp->sg_tbl[tag])
1750da2fa9baSMark Lord 				goto out_port_free_dma_mem;
1751eb73d558SMark Lord 		} else {
1752eb73d558SMark Lord 			pp->sg_tbl[tag]     = pp->sg_tbl[0];
1753eb73d558SMark Lord 			pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1754eb73d558SMark Lord 		}
1755eb73d558SMark Lord 	}
1756933cb8e5SMark Lord 
1757933cb8e5SMark Lord 	spin_lock_irqsave(ap->lock, flags);
175808da1759SMark Lord 	mv_save_cached_regs(ap);
175966e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
1760933cb8e5SMark Lord 	spin_unlock_irqrestore(ap->lock, flags);
1761933cb8e5SMark Lord 
1762c6fd2807SJeff Garzik 	return 0;
1763da2fa9baSMark Lord 
1764da2fa9baSMark Lord out_port_free_dma_mem:
1765da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1766da2fa9baSMark Lord 	return -ENOMEM;
1767c6fd2807SJeff Garzik }
1768c6fd2807SJeff Garzik 
1769c6fd2807SJeff Garzik /**
1770c6fd2807SJeff Garzik  *      mv_port_stop - Port specific cleanup/stop routine.
1771c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1772c6fd2807SJeff Garzik  *
1773c6fd2807SJeff Garzik  *      Stop DMA, cleanup port memory.
1774c6fd2807SJeff Garzik  *
1775c6fd2807SJeff Garzik  *      LOCKING:
1776cca3974eSJeff Garzik  *      This routine uses the host lock to protect the DMA stop.
1777c6fd2807SJeff Garzik  */
1778c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1779c6fd2807SJeff Garzik {
1780933cb8e5SMark Lord 	unsigned long flags;
1781933cb8e5SMark Lord 
1782933cb8e5SMark Lord 	spin_lock_irqsave(ap->lock, flags);
1783e12bef50SMark Lord 	mv_stop_edma(ap);
178488e675e1SMark Lord 	mv_enable_port_irqs(ap, 0);
1785933cb8e5SMark Lord 	spin_unlock_irqrestore(ap->lock, flags);
1786da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1787c6fd2807SJeff Garzik }
1788c6fd2807SJeff Garzik 
1789c6fd2807SJeff Garzik /**
1790c6fd2807SJeff Garzik  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1791c6fd2807SJeff Garzik  *      @qc: queued command whose SG list to source from
1792c6fd2807SJeff Garzik  *
1793c6fd2807SJeff Garzik  *      Populate the SG list and mark the last entry.
1794c6fd2807SJeff Garzik  *
1795c6fd2807SJeff Garzik  *      LOCKING:
1796c6fd2807SJeff Garzik  *      Inherited from caller.
1797c6fd2807SJeff Garzik  */
17986c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1799c6fd2807SJeff Garzik {
1800c6fd2807SJeff Garzik 	struct mv_port_priv *pp = qc->ap->private_data;
1801c6fd2807SJeff Garzik 	struct scatterlist *sg;
18023be6cbd7SJeff Garzik 	struct mv_sg *mv_sg, *last_sg = NULL;
1803ff2aeb1eSTejun Heo 	unsigned int si;
1804c6fd2807SJeff Garzik 
1805eb73d558SMark Lord 	mv_sg = pp->sg_tbl[qc->tag];
1806ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1807d88184fbSJeff Garzik 		dma_addr_t addr = sg_dma_address(sg);
1808d88184fbSJeff Garzik 		u32 sg_len = sg_dma_len(sg);
1809c6fd2807SJeff Garzik 
18104007b493SOlof Johansson 		while (sg_len) {
18114007b493SOlof Johansson 			u32 offset = addr & 0xffff;
18124007b493SOlof Johansson 			u32 len = sg_len;
18134007b493SOlof Johansson 
181432cd11a6SMark Lord 			if (offset + len > 0x10000)
18154007b493SOlof Johansson 				len = 0x10000 - offset;
18164007b493SOlof Johansson 
1817d88184fbSJeff Garzik 			mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1818d88184fbSJeff Garzik 			mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
18196c08772eSJeff Garzik 			mv_sg->flags_size = cpu_to_le32(len & 0xffff);
182032cd11a6SMark Lord 			mv_sg->reserved = 0;
1821c6fd2807SJeff Garzik 
18224007b493SOlof Johansson 			sg_len -= len;
18234007b493SOlof Johansson 			addr += len;
18244007b493SOlof Johansson 
18253be6cbd7SJeff Garzik 			last_sg = mv_sg;
1826d88184fbSJeff Garzik 			mv_sg++;
1827c6fd2807SJeff Garzik 		}
18284007b493SOlof Johansson 	}
18293be6cbd7SJeff Garzik 
18303be6cbd7SJeff Garzik 	if (likely(last_sg))
18313be6cbd7SJeff Garzik 		last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
183232cd11a6SMark Lord 	mb(); /* ensure data structure is visible to the chipset */
1833c6fd2807SJeff Garzik }
1834c6fd2807SJeff Garzik 
18355796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1836c6fd2807SJeff Garzik {
1837c6fd2807SJeff Garzik 	u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1838c6fd2807SJeff Garzik 		(last ? CRQB_CMD_LAST : 0);
1839c6fd2807SJeff Garzik 	*cmdw = cpu_to_le16(tmp);
1840c6fd2807SJeff Garzik }
1841c6fd2807SJeff Garzik 
1842c6fd2807SJeff Garzik /**
1843da14265eSMark Lord  *	mv_sff_irq_clear - Clear hardware interrupt after DMA.
1844da14265eSMark Lord  *	@ap: Port associated with this ATA transaction.
1845da14265eSMark Lord  *
1846da14265eSMark Lord  *	We need this only for ATAPI bmdma transactions,
1847da14265eSMark Lord  *	as otherwise we experience spurious interrupts
1848da14265eSMark Lord  *	after libata-sff handles the bmdma interrupts.
1849da14265eSMark Lord  */
1850da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap)
1851da14265eSMark Lord {
1852da14265eSMark Lord 	mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
1853da14265eSMark Lord }
1854da14265eSMark Lord 
1855da14265eSMark Lord /**
1856da14265eSMark Lord  *	mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
1857da14265eSMark Lord  *	@qc: queued command to check for chipset/DMA compatibility.
1858da14265eSMark Lord  *
1859da14265eSMark Lord  *	The bmdma engines cannot handle speculative data sizes
1860da14265eSMark Lord  *	(bytecount under/over flow).  So only allow DMA for
1861da14265eSMark Lord  *	data transfer commands with known data sizes.
1862da14265eSMark Lord  *
1863da14265eSMark Lord  *	LOCKING:
1864da14265eSMark Lord  *	Inherited from caller.
1865da14265eSMark Lord  */
1866da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
1867da14265eSMark Lord {
1868da14265eSMark Lord 	struct scsi_cmnd *scmd = qc->scsicmd;
1869da14265eSMark Lord 
1870da14265eSMark Lord 	if (scmd) {
1871da14265eSMark Lord 		switch (scmd->cmnd[0]) {
1872da14265eSMark Lord 		case READ_6:
1873da14265eSMark Lord 		case READ_10:
1874da14265eSMark Lord 		case READ_12:
1875da14265eSMark Lord 		case WRITE_6:
1876da14265eSMark Lord 		case WRITE_10:
1877da14265eSMark Lord 		case WRITE_12:
1878da14265eSMark Lord 		case GPCMD_READ_CD:
1879da14265eSMark Lord 		case GPCMD_SEND_DVD_STRUCTURE:
1880da14265eSMark Lord 		case GPCMD_SEND_CUE_SHEET:
1881da14265eSMark Lord 			return 0; /* DMA is safe */
1882da14265eSMark Lord 		}
1883da14265eSMark Lord 	}
1884da14265eSMark Lord 	return -EOPNOTSUPP; /* use PIO instead */
1885da14265eSMark Lord }
1886da14265eSMark Lord 
1887da14265eSMark Lord /**
1888da14265eSMark Lord  *	mv_bmdma_setup - Set up BMDMA transaction
1889da14265eSMark Lord  *	@qc: queued command to prepare DMA for.
1890da14265eSMark Lord  *
1891da14265eSMark Lord  *	LOCKING:
1892da14265eSMark Lord  *	Inherited from caller.
1893da14265eSMark Lord  */
1894da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc)
1895da14265eSMark Lord {
1896da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1897da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1898da14265eSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1899da14265eSMark Lord 
1900da14265eSMark Lord 	mv_fill_sg(qc);
1901da14265eSMark Lord 
1902da14265eSMark Lord 	/* clear all DMA cmd bits */
1903cae5a29dSMark Lord 	writel(0, port_mmio + BMDMA_CMD);
1904da14265eSMark Lord 
1905da14265eSMark Lord 	/* load PRD table addr. */
1906da14265eSMark Lord 	writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
1907cae5a29dSMark Lord 		port_mmio + BMDMA_PRD_HIGH);
1908da14265eSMark Lord 	writelfl(pp->sg_tbl_dma[qc->tag],
1909cae5a29dSMark Lord 		port_mmio + BMDMA_PRD_LOW);
1910da14265eSMark Lord 
1911da14265eSMark Lord 	/* issue r/w command */
1912da14265eSMark Lord 	ap->ops->sff_exec_command(ap, &qc->tf);
1913da14265eSMark Lord }
1914da14265eSMark Lord 
1915da14265eSMark Lord /**
1916da14265eSMark Lord  *	mv_bmdma_start - Start a BMDMA transaction
1917da14265eSMark Lord  *	@qc: queued command to start DMA on.
1918da14265eSMark Lord  *
1919da14265eSMark Lord  *	LOCKING:
1920da14265eSMark Lord  *	Inherited from caller.
1921da14265eSMark Lord  */
1922da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc)
1923da14265eSMark Lord {
1924da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1925da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1926da14265eSMark Lord 	unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
1927da14265eSMark Lord 	u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
1928da14265eSMark Lord 
1929da14265eSMark Lord 	/* start host DMA transaction */
1930cae5a29dSMark Lord 	writelfl(cmd, port_mmio + BMDMA_CMD);
1931da14265eSMark Lord }
1932da14265eSMark Lord 
1933da14265eSMark Lord /**
1934da14265eSMark Lord  *	mv_bmdma_stop - Stop BMDMA transfer
1935da14265eSMark Lord  *	@qc: queued command to stop DMA on.
1936da14265eSMark Lord  *
1937da14265eSMark Lord  *	Clears the ATA_DMA_START flag in the bmdma control register
1938da14265eSMark Lord  *
1939da14265eSMark Lord  *	LOCKING:
1940da14265eSMark Lord  *	Inherited from caller.
1941da14265eSMark Lord  */
194244b73380SMark Lord static void mv_bmdma_stop_ap(struct ata_port *ap)
1943da14265eSMark Lord {
1944da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1945da14265eSMark Lord 	u32 cmd;
1946da14265eSMark Lord 
1947da14265eSMark Lord 	/* clear start/stop bit */
1948cae5a29dSMark Lord 	cmd = readl(port_mmio + BMDMA_CMD);
194944b73380SMark Lord 	if (cmd & ATA_DMA_START) {
1950da14265eSMark Lord 		cmd &= ~ATA_DMA_START;
1951cae5a29dSMark Lord 		writelfl(cmd, port_mmio + BMDMA_CMD);
1952da14265eSMark Lord 
1953da14265eSMark Lord 		/* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
1954da14265eSMark Lord 		ata_sff_dma_pause(ap);
1955da14265eSMark Lord 	}
195644b73380SMark Lord }
195744b73380SMark Lord 
195844b73380SMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc)
195944b73380SMark Lord {
196044b73380SMark Lord 	mv_bmdma_stop_ap(qc->ap);
196144b73380SMark Lord }
1962da14265eSMark Lord 
1963da14265eSMark Lord /**
1964da14265eSMark Lord  *	mv_bmdma_status - Read BMDMA status
1965da14265eSMark Lord  *	@ap: port for which to retrieve DMA status.
1966da14265eSMark Lord  *
1967da14265eSMark Lord  *	Read and return equivalent of the sff BMDMA status register.
1968da14265eSMark Lord  *
1969da14265eSMark Lord  *	LOCKING:
1970da14265eSMark Lord  *	Inherited from caller.
1971da14265eSMark Lord  */
1972da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap)
1973da14265eSMark Lord {
1974da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1975da14265eSMark Lord 	u32 reg, status;
1976da14265eSMark Lord 
1977da14265eSMark Lord 	/*
1978da14265eSMark Lord 	 * Other bits are valid only if ATA_DMA_ACTIVE==0,
1979da14265eSMark Lord 	 * and the ATA_DMA_INTR bit doesn't exist.
1980da14265eSMark Lord 	 */
1981cae5a29dSMark Lord 	reg = readl(port_mmio + BMDMA_STATUS);
1982da14265eSMark Lord 	if (reg & ATA_DMA_ACTIVE)
1983da14265eSMark Lord 		status = ATA_DMA_ACTIVE;
198444b73380SMark Lord 	else if (reg & ATA_DMA_ERR)
1985da14265eSMark Lord 		status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
198644b73380SMark Lord 	else {
198744b73380SMark Lord 		/*
198844b73380SMark Lord 		 * Just because DMA_ACTIVE is 0 (DMA completed),
198944b73380SMark Lord 		 * this does _not_ mean the device is "done".
199044b73380SMark Lord 		 * So we should not yet be signalling ATA_DMA_INTR
199144b73380SMark Lord 		 * in some cases.  Eg. DSM/TRIM, and perhaps others.
199244b73380SMark Lord 		 */
199344b73380SMark Lord 		mv_bmdma_stop_ap(ap);
199444b73380SMark Lord 		if (ioread8(ap->ioaddr.altstatus_addr) & ATA_BUSY)
199544b73380SMark Lord 			status = 0;
199644b73380SMark Lord 		else
199744b73380SMark Lord 			status = ATA_DMA_INTR;
199844b73380SMark Lord 	}
1999da14265eSMark Lord 	return status;
2000da14265eSMark Lord }
2001da14265eSMark Lord 
2002299b3f8dSMark Lord static void mv_rw_multi_errata_sata24(struct ata_queued_cmd *qc)
2003299b3f8dSMark Lord {
2004299b3f8dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
2005299b3f8dSMark Lord 	/*
2006299b3f8dSMark Lord 	 * Workaround for 88SX60x1 FEr SATA#24.
2007299b3f8dSMark Lord 	 *
2008299b3f8dSMark Lord 	 * Chip may corrupt WRITEs if multi_count >= 4kB.
2009299b3f8dSMark Lord 	 * Note that READs are unaffected.
2010299b3f8dSMark Lord 	 *
2011299b3f8dSMark Lord 	 * It's not clear if this errata really means "4K bytes",
2012299b3f8dSMark Lord 	 * or if it always happens for multi_count > 7
2013299b3f8dSMark Lord 	 * regardless of device sector_size.
2014299b3f8dSMark Lord 	 *
2015299b3f8dSMark Lord 	 * So, for safety, any write with multi_count > 7
2016299b3f8dSMark Lord 	 * gets converted here into a regular PIO write instead:
2017299b3f8dSMark Lord 	 */
2018299b3f8dSMark Lord 	if ((tf->flags & ATA_TFLAG_WRITE) && is_multi_taskfile(tf)) {
2019299b3f8dSMark Lord 		if (qc->dev->multi_count > 7) {
2020299b3f8dSMark Lord 			switch (tf->command) {
2021299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI:
2022299b3f8dSMark Lord 				tf->command = ATA_CMD_PIO_WRITE;
2023299b3f8dSMark Lord 				break;
2024299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI_FUA_EXT:
2025299b3f8dSMark Lord 				tf->flags &= ~ATA_TFLAG_FUA; /* ugh */
2026299b3f8dSMark Lord 				/* fall through */
2027299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI_EXT:
2028299b3f8dSMark Lord 				tf->command = ATA_CMD_PIO_WRITE_EXT;
2029299b3f8dSMark Lord 				break;
2030299b3f8dSMark Lord 			}
2031299b3f8dSMark Lord 		}
2032299b3f8dSMark Lord 	}
2033299b3f8dSMark Lord }
2034299b3f8dSMark Lord 
2035da14265eSMark Lord /**
2036c6fd2807SJeff Garzik  *      mv_qc_prep - Host specific command preparation.
2037c6fd2807SJeff Garzik  *      @qc: queued command to prepare
2038c6fd2807SJeff Garzik  *
2039c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2040c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
2041c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
2042c6fd2807SJeff Garzik  *      the SG load routine.
2043c6fd2807SJeff Garzik  *
2044c6fd2807SJeff Garzik  *      LOCKING:
2045c6fd2807SJeff Garzik  *      Inherited from caller.
2046c6fd2807SJeff Garzik  */
2047c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc)
2048c6fd2807SJeff Garzik {
2049c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
2050c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2051c6fd2807SJeff Garzik 	__le16 *cw;
20528d2b450dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
2053c6fd2807SJeff Garzik 	u16 flags = 0;
2054c6fd2807SJeff Garzik 	unsigned in_index;
2055c6fd2807SJeff Garzik 
2056299b3f8dSMark Lord 	switch (tf->protocol) {
2057299b3f8dSMark Lord 	case ATA_PROT_DMA:
205844b73380SMark Lord 		if (tf->command == ATA_CMD_DSM)
205944b73380SMark Lord 			return;
206044b73380SMark Lord 		/* fall-thru */
2061299b3f8dSMark Lord 	case ATA_PROT_NCQ:
2062299b3f8dSMark Lord 		break;	/* continue below */
2063299b3f8dSMark Lord 	case ATA_PROT_PIO:
2064299b3f8dSMark Lord 		mv_rw_multi_errata_sata24(qc);
2065c6fd2807SJeff Garzik 		return;
2066299b3f8dSMark Lord 	default:
2067299b3f8dSMark Lord 		return;
2068299b3f8dSMark Lord 	}
2069c6fd2807SJeff Garzik 
2070c6fd2807SJeff Garzik 	/* Fill in command request block
2071c6fd2807SJeff Garzik 	 */
20728d2b450dSMark Lord 	if (!(tf->flags & ATA_TFLAG_WRITE))
2073c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
2074c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
2075c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
2076e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2077c6fd2807SJeff Garzik 
2078bdd4dddeSJeff Garzik 	/* get current queue index from software */
2079fcfb1f77SMark Lord 	in_index = pp->req_idx;
2080c6fd2807SJeff Garzik 
2081c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr =
2082eb73d558SMark Lord 		cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
2083c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr_hi =
2084eb73d558SMark Lord 		cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
2085c6fd2807SJeff Garzik 	pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
2086c6fd2807SJeff Garzik 
2087c6fd2807SJeff Garzik 	cw = &pp->crqb[in_index].ata_cmd[0];
2088c6fd2807SJeff Garzik 
208925985edcSLucas De Marchi 	/* Sadly, the CRQB cannot accommodate all registers--there are
2090c6fd2807SJeff Garzik 	 * only 11 bytes...so we must pick and choose required
2091c6fd2807SJeff Garzik 	 * registers based on the command.  So, we drop feature and
2092c6fd2807SJeff Garzik 	 * hob_feature for [RW] DMA commands, but they are needed for
2093cd12e1f7SMark Lord 	 * NCQ.  NCQ will drop hob_nsect, which is not needed there
2094cd12e1f7SMark Lord 	 * (nsect is used only for the tag; feat/hob_feat hold true nsect).
2095c6fd2807SJeff Garzik 	 */
2096c6fd2807SJeff Garzik 	switch (tf->command) {
2097c6fd2807SJeff Garzik 	case ATA_CMD_READ:
2098c6fd2807SJeff Garzik 	case ATA_CMD_READ_EXT:
2099c6fd2807SJeff Garzik 	case ATA_CMD_WRITE:
2100c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_EXT:
2101c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_FUA_EXT:
2102c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
2103c6fd2807SJeff Garzik 		break;
2104c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_READ:
2105c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_WRITE:
2106c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
2107c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
2108c6fd2807SJeff Garzik 		break;
2109c6fd2807SJeff Garzik 	default:
2110c6fd2807SJeff Garzik 		/* The only other commands EDMA supports in non-queued and
2111c6fd2807SJeff Garzik 		 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
2112c6fd2807SJeff Garzik 		 * of which are defined/used by Linux.  If we get here, this
2113c6fd2807SJeff Garzik 		 * driver needs work.
2114c6fd2807SJeff Garzik 		 *
2115c6fd2807SJeff Garzik 		 * FIXME: modify libata to give qc_prep a return value and
2116c6fd2807SJeff Garzik 		 * return error here.
2117c6fd2807SJeff Garzik 		 */
2118c6fd2807SJeff Garzik 		BUG_ON(tf->command);
2119c6fd2807SJeff Garzik 		break;
2120c6fd2807SJeff Garzik 	}
2121c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
2122c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
2123c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
2124c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
2125c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
2126c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
2127c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
2128c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
2129c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);	/* last */
2130c6fd2807SJeff Garzik 
2131c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2132c6fd2807SJeff Garzik 		return;
2133c6fd2807SJeff Garzik 	mv_fill_sg(qc);
2134c6fd2807SJeff Garzik }
2135c6fd2807SJeff Garzik 
2136c6fd2807SJeff Garzik /**
2137c6fd2807SJeff Garzik  *      mv_qc_prep_iie - Host specific command preparation.
2138c6fd2807SJeff Garzik  *      @qc: queued command to prepare
2139c6fd2807SJeff Garzik  *
2140c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2141c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
2142c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
2143c6fd2807SJeff Garzik  *      the SG load routine.
2144c6fd2807SJeff Garzik  *
2145c6fd2807SJeff Garzik  *      LOCKING:
2146c6fd2807SJeff Garzik  *      Inherited from caller.
2147c6fd2807SJeff Garzik  */
2148c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
2149c6fd2807SJeff Garzik {
2150c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
2151c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2152c6fd2807SJeff Garzik 	struct mv_crqb_iie *crqb;
21538d2b450dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
2154c6fd2807SJeff Garzik 	unsigned in_index;
2155c6fd2807SJeff Garzik 	u32 flags = 0;
2156c6fd2807SJeff Garzik 
21578d2b450dSMark Lord 	if ((tf->protocol != ATA_PROT_DMA) &&
21588d2b450dSMark Lord 	    (tf->protocol != ATA_PROT_NCQ))
2159c6fd2807SJeff Garzik 		return;
216044b73380SMark Lord 	if (tf->command == ATA_CMD_DSM)
216144b73380SMark Lord 		return;  /* use bmdma for this */
2162c6fd2807SJeff Garzik 
2163e12bef50SMark Lord 	/* Fill in Gen IIE command request block */
21648d2b450dSMark Lord 	if (!(tf->flags & ATA_TFLAG_WRITE))
2165c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
2166c6fd2807SJeff Garzik 
2167c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
2168c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
21698c0aeb4aSMark Lord 	flags |= qc->tag << CRQB_HOSTQ_SHIFT;
2170e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2171c6fd2807SJeff Garzik 
2172bdd4dddeSJeff Garzik 	/* get current queue index from software */
2173fcfb1f77SMark Lord 	in_index = pp->req_idx;
2174c6fd2807SJeff Garzik 
2175c6fd2807SJeff Garzik 	crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
2176eb73d558SMark Lord 	crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
2177eb73d558SMark Lord 	crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
2178c6fd2807SJeff Garzik 	crqb->flags = cpu_to_le32(flags);
2179c6fd2807SJeff Garzik 
2180c6fd2807SJeff Garzik 	crqb->ata_cmd[0] = cpu_to_le32(
2181c6fd2807SJeff Garzik 			(tf->command << 16) |
2182c6fd2807SJeff Garzik 			(tf->feature << 24)
2183c6fd2807SJeff Garzik 		);
2184c6fd2807SJeff Garzik 	crqb->ata_cmd[1] = cpu_to_le32(
2185c6fd2807SJeff Garzik 			(tf->lbal << 0) |
2186c6fd2807SJeff Garzik 			(tf->lbam << 8) |
2187c6fd2807SJeff Garzik 			(tf->lbah << 16) |
2188c6fd2807SJeff Garzik 			(tf->device << 24)
2189c6fd2807SJeff Garzik 		);
2190c6fd2807SJeff Garzik 	crqb->ata_cmd[2] = cpu_to_le32(
2191c6fd2807SJeff Garzik 			(tf->hob_lbal << 0) |
2192c6fd2807SJeff Garzik 			(tf->hob_lbam << 8) |
2193c6fd2807SJeff Garzik 			(tf->hob_lbah << 16) |
2194c6fd2807SJeff Garzik 			(tf->hob_feature << 24)
2195c6fd2807SJeff Garzik 		);
2196c6fd2807SJeff Garzik 	crqb->ata_cmd[3] = cpu_to_le32(
2197c6fd2807SJeff Garzik 			(tf->nsect << 0) |
2198c6fd2807SJeff Garzik 			(tf->hob_nsect << 8)
2199c6fd2807SJeff Garzik 		);
2200c6fd2807SJeff Garzik 
2201c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2202c6fd2807SJeff Garzik 		return;
2203c6fd2807SJeff Garzik 	mv_fill_sg(qc);
2204c6fd2807SJeff Garzik }
2205c6fd2807SJeff Garzik 
2206c6fd2807SJeff Garzik /**
2207d16ab3f6SMark Lord  *	mv_sff_check_status - fetch device status, if valid
2208d16ab3f6SMark Lord  *	@ap: ATA port to fetch status from
2209d16ab3f6SMark Lord  *
2210d16ab3f6SMark Lord  *	When using command issue via mv_qc_issue_fis(),
2211d16ab3f6SMark Lord  *	the initial ATA_BUSY state does not show up in the
2212d16ab3f6SMark Lord  *	ATA status (shadow) register.  This can confuse libata!
2213d16ab3f6SMark Lord  *
2214d16ab3f6SMark Lord  *	So we have a hook here to fake ATA_BUSY for that situation,
2215d16ab3f6SMark Lord  *	until the first time a BUSY, DRQ, or ERR bit is seen.
2216d16ab3f6SMark Lord  *
2217d16ab3f6SMark Lord  *	The rest of the time, it simply returns the ATA status register.
2218d16ab3f6SMark Lord  */
2219d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap)
2220d16ab3f6SMark Lord {
2221d16ab3f6SMark Lord 	u8 stat = ioread8(ap->ioaddr.status_addr);
2222d16ab3f6SMark Lord 	struct mv_port_priv *pp = ap->private_data;
2223d16ab3f6SMark Lord 
2224d16ab3f6SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
2225d16ab3f6SMark Lord 		if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
2226d16ab3f6SMark Lord 			pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
2227d16ab3f6SMark Lord 		else
2228d16ab3f6SMark Lord 			stat = ATA_BUSY;
2229d16ab3f6SMark Lord 	}
2230d16ab3f6SMark Lord 	return stat;
2231d16ab3f6SMark Lord }
2232d16ab3f6SMark Lord 
2233d16ab3f6SMark Lord /**
223470f8b79cSMark Lord  *	mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
223570f8b79cSMark Lord  *	@fis: fis to be sent
223670f8b79cSMark Lord  *	@nwords: number of 32-bit words in the fis
223770f8b79cSMark Lord  */
223870f8b79cSMark Lord static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
223970f8b79cSMark Lord {
224070f8b79cSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
224170f8b79cSMark Lord 	u32 ifctl, old_ifctl, ifstat;
224270f8b79cSMark Lord 	int i, timeout = 200, final_word = nwords - 1;
224370f8b79cSMark Lord 
224470f8b79cSMark Lord 	/* Initiate FIS transmission mode */
2245cae5a29dSMark Lord 	old_ifctl = readl(port_mmio + SATA_IFCTL);
224670f8b79cSMark Lord 	ifctl = 0x100 | (old_ifctl & 0xf);
2247cae5a29dSMark Lord 	writelfl(ifctl, port_mmio + SATA_IFCTL);
224870f8b79cSMark Lord 
224970f8b79cSMark Lord 	/* Send all words of the FIS except for the final word */
225070f8b79cSMark Lord 	for (i = 0; i < final_word; ++i)
2251cae5a29dSMark Lord 		writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS);
225270f8b79cSMark Lord 
225370f8b79cSMark Lord 	/* Flag end-of-transmission, and then send the final word */
2254cae5a29dSMark Lord 	writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL);
2255cae5a29dSMark Lord 	writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS);
225670f8b79cSMark Lord 
225770f8b79cSMark Lord 	/*
225870f8b79cSMark Lord 	 * Wait for FIS transmission to complete.
225970f8b79cSMark Lord 	 * This typically takes just a single iteration.
226070f8b79cSMark Lord 	 */
226170f8b79cSMark Lord 	do {
2262cae5a29dSMark Lord 		ifstat = readl(port_mmio + SATA_IFSTAT);
226370f8b79cSMark Lord 	} while (!(ifstat & 0x1000) && --timeout);
226470f8b79cSMark Lord 
226570f8b79cSMark Lord 	/* Restore original port configuration */
2266cae5a29dSMark Lord 	writelfl(old_ifctl, port_mmio + SATA_IFCTL);
226770f8b79cSMark Lord 
226870f8b79cSMark Lord 	/* See if it worked */
226970f8b79cSMark Lord 	if ((ifstat & 0x3000) != 0x1000) {
2270a9a79dfeSJoe Perches 		ata_port_warn(ap, "%s transmission error, ifstat=%08x\n",
227170f8b79cSMark Lord 			      __func__, ifstat);
227270f8b79cSMark Lord 		return AC_ERR_OTHER;
227370f8b79cSMark Lord 	}
227470f8b79cSMark Lord 	return 0;
227570f8b79cSMark Lord }
227670f8b79cSMark Lord 
227770f8b79cSMark Lord /**
227870f8b79cSMark Lord  *	mv_qc_issue_fis - Issue a command directly as a FIS
227970f8b79cSMark Lord  *	@qc: queued command to start
228070f8b79cSMark Lord  *
228170f8b79cSMark Lord  *	Note that the ATA shadow registers are not updated
228270f8b79cSMark Lord  *	after command issue, so the device will appear "READY"
228370f8b79cSMark Lord  *	if polled, even while it is BUSY processing the command.
228470f8b79cSMark Lord  *
228570f8b79cSMark Lord  *	So we use a status hook to fake ATA_BUSY until the drive changes state.
228670f8b79cSMark Lord  *
228770f8b79cSMark Lord  *	Note: we don't get updated shadow regs on *completion*
228870f8b79cSMark Lord  *	of non-data commands. So avoid sending them via this function,
228970f8b79cSMark Lord  *	as they will appear to have completed immediately.
229070f8b79cSMark Lord  *
229170f8b79cSMark Lord  *	GEN_IIE has special registers that we could get the result tf from,
229270f8b79cSMark Lord  *	but earlier chipsets do not.  For now, we ignore those registers.
229370f8b79cSMark Lord  */
229470f8b79cSMark Lord static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
229570f8b79cSMark Lord {
229670f8b79cSMark Lord 	struct ata_port *ap = qc->ap;
229770f8b79cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
229870f8b79cSMark Lord 	struct ata_link *link = qc->dev->link;
229970f8b79cSMark Lord 	u32 fis[5];
230070f8b79cSMark Lord 	int err = 0;
230170f8b79cSMark Lord 
230270f8b79cSMark Lord 	ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
23034c4a90fdSThiago Farina 	err = mv_send_fis(ap, fis, ARRAY_SIZE(fis));
230470f8b79cSMark Lord 	if (err)
230570f8b79cSMark Lord 		return err;
230670f8b79cSMark Lord 
230770f8b79cSMark Lord 	switch (qc->tf.protocol) {
230870f8b79cSMark Lord 	case ATAPI_PROT_PIO:
230970f8b79cSMark Lord 		pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
231070f8b79cSMark Lord 		/* fall through */
231170f8b79cSMark Lord 	case ATAPI_PROT_NODATA:
231270f8b79cSMark Lord 		ap->hsm_task_state = HSM_ST_FIRST;
231370f8b79cSMark Lord 		break;
231470f8b79cSMark Lord 	case ATA_PROT_PIO:
231570f8b79cSMark Lord 		pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
231670f8b79cSMark Lord 		if (qc->tf.flags & ATA_TFLAG_WRITE)
231770f8b79cSMark Lord 			ap->hsm_task_state = HSM_ST_FIRST;
231870f8b79cSMark Lord 		else
231970f8b79cSMark Lord 			ap->hsm_task_state = HSM_ST;
232070f8b79cSMark Lord 		break;
232170f8b79cSMark Lord 	default:
232270f8b79cSMark Lord 		ap->hsm_task_state = HSM_ST_LAST;
232370f8b79cSMark Lord 		break;
232470f8b79cSMark Lord 	}
232570f8b79cSMark Lord 
232670f8b79cSMark Lord 	if (qc->tf.flags & ATA_TFLAG_POLLING)
2327ea3c6450SGwendal Grignou 		ata_sff_queue_pio_task(link, 0);
232870f8b79cSMark Lord 	return 0;
232970f8b79cSMark Lord }
233070f8b79cSMark Lord 
233170f8b79cSMark Lord /**
2332c6fd2807SJeff Garzik  *      mv_qc_issue - Initiate a command to the host
2333c6fd2807SJeff Garzik  *      @qc: queued command to start
2334c6fd2807SJeff Garzik  *
2335c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2336c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it sanity checks our local
2337c6fd2807SJeff Garzik  *      caches of the request producer/consumer indices then enables
2338c6fd2807SJeff Garzik  *      DMA and bumps the request producer index.
2339c6fd2807SJeff Garzik  *
2340c6fd2807SJeff Garzik  *      LOCKING:
2341c6fd2807SJeff Garzik  *      Inherited from caller.
2342c6fd2807SJeff Garzik  */
2343c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
2344c6fd2807SJeff Garzik {
2345f48765ccSMark Lord 	static int limit_warnings = 10;
2346c5d3e45aSJeff Garzik 	struct ata_port *ap = qc->ap;
2347c5d3e45aSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2348c5d3e45aSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2349bdd4dddeSJeff Garzik 	u32 in_index;
235042ed893dSMark Lord 	unsigned int port_irqs;
2351c6fd2807SJeff Garzik 
2352d16ab3f6SMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
2353d16ab3f6SMark Lord 
2354f48765ccSMark Lord 	switch (qc->tf.protocol) {
2355f48765ccSMark Lord 	case ATA_PROT_DMA:
235644b73380SMark Lord 		if (qc->tf.command == ATA_CMD_DSM) {
235744b73380SMark Lord 			if (!ap->ops->bmdma_setup)  /* no bmdma on GEN_I */
235844b73380SMark Lord 				return AC_ERR_OTHER;
235944b73380SMark Lord 			break;  /* use bmdma for this */
236044b73380SMark Lord 		}
236144b73380SMark Lord 		/* fall thru */
2362f48765ccSMark Lord 	case ATA_PROT_NCQ:
2363f48765ccSMark Lord 		mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
2364f48765ccSMark Lord 		pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2365f48765ccSMark Lord 		in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
2366f48765ccSMark Lord 
2367f48765ccSMark Lord 		/* Write the request in pointer to kick the EDMA to life */
2368f48765ccSMark Lord 		writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
2369cae5a29dSMark Lord 					port_mmio + EDMA_REQ_Q_IN_PTR);
2370f48765ccSMark Lord 		return 0;
2371f48765ccSMark Lord 
2372f48765ccSMark Lord 	case ATA_PROT_PIO:
2373c6112bd8SMark Lord 		/*
2374c6112bd8SMark Lord 		 * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
2375c6112bd8SMark Lord 		 *
2376c6112bd8SMark Lord 		 * Someday, we might implement special polling workarounds
2377c6112bd8SMark Lord 		 * for these, but it all seems rather unnecessary since we
2378c6112bd8SMark Lord 		 * normally use only DMA for commands which transfer more
2379c6112bd8SMark Lord 		 * than a single block of data.
2380c6112bd8SMark Lord 		 *
2381c6112bd8SMark Lord 		 * Much of the time, this could just work regardless.
2382c6112bd8SMark Lord 		 * So for now, just log the incident, and allow the attempt.
2383c6112bd8SMark Lord 		 */
2384c7843e8fSMark Lord 		if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
2385c6112bd8SMark Lord 			--limit_warnings;
2386a9a79dfeSJoe Perches 			ata_link_warn(qc->dev->link, DRV_NAME
2387c6112bd8SMark Lord 				      ": attempting PIO w/multiple DRQ: "
2388c6112bd8SMark Lord 				      "this may fail due to h/w errata\n");
2389c6112bd8SMark Lord 		}
2390f48765ccSMark Lord 		/* drop through */
239142ed893dSMark Lord 	case ATA_PROT_NODATA:
2392f48765ccSMark Lord 	case ATAPI_PROT_PIO:
239342ed893dSMark Lord 	case ATAPI_PROT_NODATA:
239442ed893dSMark Lord 		if (ap->flags & ATA_FLAG_PIO_POLLING)
239542ed893dSMark Lord 			qc->tf.flags |= ATA_TFLAG_POLLING;
239642ed893dSMark Lord 		break;
239742ed893dSMark Lord 	}
239842ed893dSMark Lord 
239942ed893dSMark Lord 	if (qc->tf.flags & ATA_TFLAG_POLLING)
240042ed893dSMark Lord 		port_irqs = ERR_IRQ;	/* mask device interrupt when polling */
240142ed893dSMark Lord 	else
240242ed893dSMark Lord 		port_irqs = ERR_IRQ | DONE_IRQ;	/* unmask all interrupts */
240342ed893dSMark Lord 
240417c5aab5SMark Lord 	/*
240517c5aab5SMark Lord 	 * We're about to send a non-EDMA capable command to the
2406c6fd2807SJeff Garzik 	 * port.  Turn off EDMA so there won't be problems accessing
2407c6fd2807SJeff Garzik 	 * shadow block, etc registers.
2408c6fd2807SJeff Garzik 	 */
2409b562468cSMark Lord 	mv_stop_edma(ap);
2410f48765ccSMark Lord 	mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
2411e49856d8SMark Lord 	mv_pmp_select(ap, qc->dev->link->pmp);
241270f8b79cSMark Lord 
241370f8b79cSMark Lord 	if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
241470f8b79cSMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
241570f8b79cSMark Lord 		/*
241670f8b79cSMark Lord 		 * Workaround for 88SX60x1 FEr SATA#25 (part 2).
241770f8b79cSMark Lord 		 *
241870f8b79cSMark Lord 		 * After any NCQ error, the READ_LOG_EXT command
241970f8b79cSMark Lord 		 * from libata-eh *must* use mv_qc_issue_fis().
242070f8b79cSMark Lord 		 * Otherwise it might fail, due to chip errata.
242170f8b79cSMark Lord 		 *
242270f8b79cSMark Lord 		 * Rather than special-case it, we'll just *always*
242370f8b79cSMark Lord 		 * use this method here for READ_LOG_EXT, making for
242470f8b79cSMark Lord 		 * easier testing.
242570f8b79cSMark Lord 		 */
242670f8b79cSMark Lord 		if (IS_GEN_II(hpriv))
242770f8b79cSMark Lord 			return mv_qc_issue_fis(qc);
242870f8b79cSMark Lord 	}
2429360ff783STejun Heo 	return ata_bmdma_qc_issue(qc);
2430c6fd2807SJeff Garzik }
2431c6fd2807SJeff Garzik 
24328f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
24338f767f8aSMark Lord {
24348f767f8aSMark Lord 	struct mv_port_priv *pp = ap->private_data;
24358f767f8aSMark Lord 	struct ata_queued_cmd *qc;
24368f767f8aSMark Lord 
24378f767f8aSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
24388f767f8aSMark Lord 		return NULL;
24398f767f8aSMark Lord 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
24403e4ec344STejun Heo 	if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING))
24418f767f8aSMark Lord 		return qc;
24423e4ec344STejun Heo 	return NULL;
24438f767f8aSMark Lord }
24448f767f8aSMark Lord 
244529d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap)
244629d187bbSMark Lord {
244729d187bbSMark Lord 	unsigned int pmp, pmp_map;
244829d187bbSMark Lord 	struct mv_port_priv *pp = ap->private_data;
244929d187bbSMark Lord 
245029d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
245129d187bbSMark Lord 		/*
245229d187bbSMark Lord 		 * Perform NCQ error analysis on failed PMPs
245329d187bbSMark Lord 		 * before we freeze the port entirely.
245429d187bbSMark Lord 		 *
245529d187bbSMark Lord 		 * The failed PMPs are marked earlier by mv_pmp_eh_prep().
245629d187bbSMark Lord 		 */
245729d187bbSMark Lord 		pmp_map = pp->delayed_eh_pmp_map;
245829d187bbSMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
245929d187bbSMark Lord 		for (pmp = 0; pmp_map != 0; pmp++) {
246029d187bbSMark Lord 			unsigned int this_pmp = (1 << pmp);
246129d187bbSMark Lord 			if (pmp_map & this_pmp) {
246229d187bbSMark Lord 				struct ata_link *link = &ap->pmp_link[pmp];
246329d187bbSMark Lord 				pmp_map &= ~this_pmp;
246429d187bbSMark Lord 				ata_eh_analyze_ncq_error(link);
246529d187bbSMark Lord 			}
246629d187bbSMark Lord 		}
246729d187bbSMark Lord 		ata_port_freeze(ap);
246829d187bbSMark Lord 	}
246929d187bbSMark Lord 	sata_pmp_error_handler(ap);
247029d187bbSMark Lord }
247129d187bbSMark Lord 
24724c299ca3SMark Lord static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
24734c299ca3SMark Lord {
24744c299ca3SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
24754c299ca3SMark Lord 
2476cae5a29dSMark Lord 	return readl(port_mmio + SATA_TESTCTL) >> 16;
24774c299ca3SMark Lord }
24784c299ca3SMark Lord 
24794c299ca3SMark Lord static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
24804c299ca3SMark Lord {
24814c299ca3SMark Lord 	struct ata_eh_info *ehi;
24824c299ca3SMark Lord 	unsigned int pmp;
24834c299ca3SMark Lord 
24844c299ca3SMark Lord 	/*
24854c299ca3SMark Lord 	 * Initialize EH info for PMPs which saw device errors
24864c299ca3SMark Lord 	 */
24874c299ca3SMark Lord 	ehi = &ap->link.eh_info;
24884c299ca3SMark Lord 	for (pmp = 0; pmp_map != 0; pmp++) {
24894c299ca3SMark Lord 		unsigned int this_pmp = (1 << pmp);
24904c299ca3SMark Lord 		if (pmp_map & this_pmp) {
24914c299ca3SMark Lord 			struct ata_link *link = &ap->pmp_link[pmp];
24924c299ca3SMark Lord 
24934c299ca3SMark Lord 			pmp_map &= ~this_pmp;
24944c299ca3SMark Lord 			ehi = &link->eh_info;
24954c299ca3SMark Lord 			ata_ehi_clear_desc(ehi);
24964c299ca3SMark Lord 			ata_ehi_push_desc(ehi, "dev err");
24974c299ca3SMark Lord 			ehi->err_mask |= AC_ERR_DEV;
24984c299ca3SMark Lord 			ehi->action |= ATA_EH_RESET;
24994c299ca3SMark Lord 			ata_link_abort(link);
25004c299ca3SMark Lord 		}
25014c299ca3SMark Lord 	}
25024c299ca3SMark Lord }
25034c299ca3SMark Lord 
250406aaca3fSMark Lord static int mv_req_q_empty(struct ata_port *ap)
250506aaca3fSMark Lord {
250606aaca3fSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
250706aaca3fSMark Lord 	u32 in_ptr, out_ptr;
250806aaca3fSMark Lord 
2509cae5a29dSMark Lord 	in_ptr  = (readl(port_mmio + EDMA_REQ_Q_IN_PTR)
251006aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2511cae5a29dSMark Lord 	out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR)
251206aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
251306aaca3fSMark Lord 	return (in_ptr == out_ptr);	/* 1 == queue_is_empty */
251406aaca3fSMark Lord }
251506aaca3fSMark Lord 
25164c299ca3SMark Lord static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
25174c299ca3SMark Lord {
25184c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
25194c299ca3SMark Lord 	int failed_links;
25204c299ca3SMark Lord 	unsigned int old_map, new_map;
25214c299ca3SMark Lord 
25224c299ca3SMark Lord 	/*
25234c299ca3SMark Lord 	 * Device error during FBS+NCQ operation:
25244c299ca3SMark Lord 	 *
25254c299ca3SMark Lord 	 * Set a port flag to prevent further I/O being enqueued.
25264c299ca3SMark Lord 	 * Leave the EDMA running to drain outstanding commands from this port.
25274c299ca3SMark Lord 	 * Perform the post-mortem/EH only when all responses are complete.
25284c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
25294c299ca3SMark Lord 	 */
25304c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
25314c299ca3SMark Lord 		pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
25324c299ca3SMark Lord 		pp->delayed_eh_pmp_map = 0;
25334c299ca3SMark Lord 	}
25344c299ca3SMark Lord 	old_map = pp->delayed_eh_pmp_map;
25354c299ca3SMark Lord 	new_map = old_map | mv_get_err_pmp_map(ap);
25364c299ca3SMark Lord 
25374c299ca3SMark Lord 	if (old_map != new_map) {
25384c299ca3SMark Lord 		pp->delayed_eh_pmp_map = new_map;
25394c299ca3SMark Lord 		mv_pmp_eh_prep(ap, new_map & ~old_map);
25404c299ca3SMark Lord 	}
2541c46938ccSMark Lord 	failed_links = hweight16(new_map);
25424c299ca3SMark Lord 
2543a9a79dfeSJoe Perches 	ata_port_info(ap,
2544a9a79dfeSJoe Perches 		      "%s: pmp_map=%04x qc_map=%04x failed_links=%d nr_active_links=%d\n",
25454c299ca3SMark Lord 		      __func__, pp->delayed_eh_pmp_map,
25464c299ca3SMark Lord 		      ap->qc_active, failed_links,
25474c299ca3SMark Lord 		      ap->nr_active_links);
25484c299ca3SMark Lord 
254906aaca3fSMark Lord 	if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
25504c299ca3SMark Lord 		mv_process_crpb_entries(ap, pp);
25514c299ca3SMark Lord 		mv_stop_edma(ap);
25524c299ca3SMark Lord 		mv_eh_freeze(ap);
2553a9a79dfeSJoe Perches 		ata_port_info(ap, "%s: done\n", __func__);
25544c299ca3SMark Lord 		return 1;	/* handled */
25554c299ca3SMark Lord 	}
2556a9a79dfeSJoe Perches 	ata_port_info(ap, "%s: waiting\n", __func__);
25574c299ca3SMark Lord 	return 1;	/* handled */
25584c299ca3SMark Lord }
25594c299ca3SMark Lord 
25604c299ca3SMark Lord static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
25614c299ca3SMark Lord {
25624c299ca3SMark Lord 	/*
25634c299ca3SMark Lord 	 * Possible future enhancement:
25644c299ca3SMark Lord 	 *
25654c299ca3SMark Lord 	 * FBS+non-NCQ operation is not yet implemented.
25664c299ca3SMark Lord 	 * See related notes in mv_edma_cfg().
25674c299ca3SMark Lord 	 *
25684c299ca3SMark Lord 	 * Device error during FBS+non-NCQ operation:
25694c299ca3SMark Lord 	 *
25704c299ca3SMark Lord 	 * We need to snapshot the shadow registers for each failed command.
25714c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
25724c299ca3SMark Lord 	 */
25734c299ca3SMark Lord 	return 0;	/* not handled */
25744c299ca3SMark Lord }
25754c299ca3SMark Lord 
25764c299ca3SMark Lord static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
25774c299ca3SMark Lord {
25784c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
25794c299ca3SMark Lord 
25804c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
25814c299ca3SMark Lord 		return 0;	/* EDMA was not active: not handled */
25824c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
25834c299ca3SMark Lord 		return 0;	/* FBS was not active: not handled */
25844c299ca3SMark Lord 
25854c299ca3SMark Lord 	if (!(edma_err_cause & EDMA_ERR_DEV))
25864c299ca3SMark Lord 		return 0;	/* non DEV error: not handled */
25874c299ca3SMark Lord 	edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
25884c299ca3SMark Lord 	if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
25894c299ca3SMark Lord 		return 0;	/* other problems: not handled */
25904c299ca3SMark Lord 
25914c299ca3SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
25924c299ca3SMark Lord 		/*
25934c299ca3SMark Lord 		 * EDMA should NOT have self-disabled for this case.
25944c299ca3SMark Lord 		 * If it did, then something is wrong elsewhere,
25954c299ca3SMark Lord 		 * and we cannot handle it here.
25964c299ca3SMark Lord 		 */
25974c299ca3SMark Lord 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2598a9a79dfeSJoe Perches 			ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
25994c299ca3SMark Lord 				      __func__, edma_err_cause, pp->pp_flags);
26004c299ca3SMark Lord 			return 0; /* not handled */
26014c299ca3SMark Lord 		}
26024c299ca3SMark Lord 		return mv_handle_fbs_ncq_dev_err(ap);
26034c299ca3SMark Lord 	} else {
26044c299ca3SMark Lord 		/*
26054c299ca3SMark Lord 		 * EDMA should have self-disabled for this case.
26064c299ca3SMark Lord 		 * If it did not, then something is wrong elsewhere,
26074c299ca3SMark Lord 		 * and we cannot handle it here.
26084c299ca3SMark Lord 		 */
26094c299ca3SMark Lord 		if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
2610a9a79dfeSJoe Perches 			ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
26114c299ca3SMark Lord 				      __func__, edma_err_cause, pp->pp_flags);
26124c299ca3SMark Lord 			return 0; /* not handled */
26134c299ca3SMark Lord 		}
26144c299ca3SMark Lord 		return mv_handle_fbs_non_ncq_dev_err(ap);
26154c299ca3SMark Lord 	}
26164c299ca3SMark Lord 	return 0;	/* not handled */
26174c299ca3SMark Lord }
26184c299ca3SMark Lord 
2619a9010329SMark Lord static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
26208f767f8aSMark Lord {
26218f767f8aSMark Lord 	struct ata_eh_info *ehi = &ap->link.eh_info;
2622a9010329SMark Lord 	char *when = "idle";
26238f767f8aSMark Lord 
26248f767f8aSMark Lord 	ata_ehi_clear_desc(ehi);
26253e4ec344STejun Heo 	if (edma_was_enabled) {
2626a9010329SMark Lord 		when = "EDMA enabled";
26278f767f8aSMark Lord 	} else {
26288f767f8aSMark Lord 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
26298f767f8aSMark Lord 		if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
2630a9010329SMark Lord 			when = "polling";
26318f767f8aSMark Lord 	}
2632a9010329SMark Lord 	ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
26338f767f8aSMark Lord 	ehi->err_mask |= AC_ERR_OTHER;
26348f767f8aSMark Lord 	ehi->action   |= ATA_EH_RESET;
26358f767f8aSMark Lord 	ata_port_freeze(ap);
26368f767f8aSMark Lord }
26378f767f8aSMark Lord 
2638c6fd2807SJeff Garzik /**
2639c6fd2807SJeff Garzik  *      mv_err_intr - Handle error interrupts on the port
2640c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
2641c6fd2807SJeff Garzik  *
26428d07379dSMark Lord  *      Most cases require a full reset of the chip's state machine,
26438d07379dSMark Lord  *      which also performs a COMRESET.
26448d07379dSMark Lord  *      Also, if the port disabled DMA, update our cached copy to match.
2645c6fd2807SJeff Garzik  *
2646c6fd2807SJeff Garzik  *      LOCKING:
2647c6fd2807SJeff Garzik  *      Inherited from caller.
2648c6fd2807SJeff Garzik  */
264937b9046aSMark Lord static void mv_err_intr(struct ata_port *ap)
2650c6fd2807SJeff Garzik {
2651c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2652bdd4dddeSJeff Garzik 	u32 edma_err_cause, eh_freeze_mask, serr = 0;
2653e4006077SMark Lord 	u32 fis_cause = 0;
2654bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2655bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2656bdd4dddeSJeff Garzik 	unsigned int action = 0, err_mask = 0;
26579af5c9c9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
265837b9046aSMark Lord 	struct ata_queued_cmd *qc;
265937b9046aSMark Lord 	int abort = 0;
2660c6fd2807SJeff Garzik 
26618d07379dSMark Lord 	/*
266237b9046aSMark Lord 	 * Read and clear the SError and err_cause bits.
2663e4006077SMark Lord 	 * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
2664e4006077SMark Lord 	 * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
2665bdd4dddeSJeff Garzik 	 */
266637b9046aSMark Lord 	sata_scr_read(&ap->link, SCR_ERROR, &serr);
266737b9046aSMark Lord 	sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
266837b9046aSMark Lord 
2669cae5a29dSMark Lord 	edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE);
2670e4006077SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2671cae5a29dSMark Lord 		fis_cause = readl(port_mmio + FIS_IRQ_CAUSE);
2672cae5a29dSMark Lord 		writelfl(~fis_cause, port_mmio + FIS_IRQ_CAUSE);
2673e4006077SMark Lord 	}
2674cae5a29dSMark Lord 	writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE);
2675bdd4dddeSJeff Garzik 
26764c299ca3SMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
26774c299ca3SMark Lord 		/*
26784c299ca3SMark Lord 		 * Device errors during FIS-based switching operation
26794c299ca3SMark Lord 		 * require special handling.
26804c299ca3SMark Lord 		 */
26814c299ca3SMark Lord 		if (mv_handle_dev_err(ap, edma_err_cause))
26824c299ca3SMark Lord 			return;
26834c299ca3SMark Lord 	}
26844c299ca3SMark Lord 
268537b9046aSMark Lord 	qc = mv_get_active_qc(ap);
268637b9046aSMark Lord 	ata_ehi_clear_desc(ehi);
268737b9046aSMark Lord 	ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
268837b9046aSMark Lord 			  edma_err_cause, pp->pp_flags);
2689e4006077SMark Lord 
2690c443c500SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2691e4006077SMark Lord 		ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
2692cae5a29dSMark Lord 		if (fis_cause & FIS_IRQ_CAUSE_AN) {
2693c443c500SMark Lord 			u32 ec = edma_err_cause &
2694c443c500SMark Lord 			       ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
2695c443c500SMark Lord 			sata_async_notification(ap);
2696c443c500SMark Lord 			if (!ec)
2697c443c500SMark Lord 				return; /* Just an AN; no need for the nukes */
2698c443c500SMark Lord 			ata_ehi_push_desc(ehi, "SDB notify");
2699c443c500SMark Lord 		}
2700c443c500SMark Lord 	}
2701bdd4dddeSJeff Garzik 	/*
2702352fab70SMark Lord 	 * All generations share these EDMA error cause bits:
2703bdd4dddeSJeff Garzik 	 */
270437b9046aSMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
2705bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_DEV;
270637b9046aSMark Lord 		action |= ATA_EH_RESET;
270737b9046aSMark Lord 		ata_ehi_push_desc(ehi, "dev error");
270837b9046aSMark Lord 	}
2709bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
27106c1153e0SJeff Garzik 			EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
2711bdd4dddeSJeff Garzik 			EDMA_ERR_INTRL_PAR)) {
2712bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
2713cf480626STejun Heo 		action |= ATA_EH_RESET;
2714b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "parity error");
2715bdd4dddeSJeff Garzik 	}
2716bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
2717bdd4dddeSJeff Garzik 		ata_ehi_hotplugged(ehi);
2718bdd4dddeSJeff Garzik 		ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
2719b64bbc39STejun Heo 			"dev disconnect" : "dev connect");
2720cf480626STejun Heo 		action |= ATA_EH_RESET;
2721bdd4dddeSJeff Garzik 	}
2722bdd4dddeSJeff Garzik 
2723352fab70SMark Lord 	/*
2724352fab70SMark Lord 	 * Gen-I has a different SELF_DIS bit,
2725352fab70SMark Lord 	 * different FREEZE bits, and no SERR bit:
2726352fab70SMark Lord 	 */
2727ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv)) {
2728bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE_5;
2729bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
2730c6fd2807SJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2731b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
2732c6fd2807SJeff Garzik 		}
2733bdd4dddeSJeff Garzik 	} else {
2734bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE;
2735bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2736bdd4dddeSJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2737b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
2738bdd4dddeSJeff Garzik 		}
2739bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SERR) {
27408d07379dSMark Lord 			ata_ehi_push_desc(ehi, "SError=%08x", serr);
27418d07379dSMark Lord 			err_mask |= AC_ERR_ATA_BUS;
2742cf480626STejun Heo 			action |= ATA_EH_RESET;
2743bdd4dddeSJeff Garzik 		}
2744bdd4dddeSJeff Garzik 	}
2745c6fd2807SJeff Garzik 
2746bdd4dddeSJeff Garzik 	if (!err_mask) {
2747bdd4dddeSJeff Garzik 		err_mask = AC_ERR_OTHER;
2748cf480626STejun Heo 		action |= ATA_EH_RESET;
2749bdd4dddeSJeff Garzik 	}
2750bdd4dddeSJeff Garzik 
2751bdd4dddeSJeff Garzik 	ehi->serror |= serr;
2752bdd4dddeSJeff Garzik 	ehi->action |= action;
2753bdd4dddeSJeff Garzik 
2754bdd4dddeSJeff Garzik 	if (qc)
2755bdd4dddeSJeff Garzik 		qc->err_mask |= err_mask;
2756bdd4dddeSJeff Garzik 	else
2757bdd4dddeSJeff Garzik 		ehi->err_mask |= err_mask;
2758bdd4dddeSJeff Garzik 
275937b9046aSMark Lord 	if (err_mask == AC_ERR_DEV) {
276037b9046aSMark Lord 		/*
276137b9046aSMark Lord 		 * Cannot do ata_port_freeze() here,
276237b9046aSMark Lord 		 * because it would kill PIO access,
276337b9046aSMark Lord 		 * which is needed for further diagnosis.
276437b9046aSMark Lord 		 */
276537b9046aSMark Lord 		mv_eh_freeze(ap);
276637b9046aSMark Lord 		abort = 1;
276737b9046aSMark Lord 	} else if (edma_err_cause & eh_freeze_mask) {
276837b9046aSMark Lord 		/*
276937b9046aSMark Lord 		 * Note to self: ata_port_freeze() calls ata_port_abort()
277037b9046aSMark Lord 		 */
2771bdd4dddeSJeff Garzik 		ata_port_freeze(ap);
277237b9046aSMark Lord 	} else {
277337b9046aSMark Lord 		abort = 1;
277437b9046aSMark Lord 	}
277537b9046aSMark Lord 
277637b9046aSMark Lord 	if (abort) {
277737b9046aSMark Lord 		if (qc)
277837b9046aSMark Lord 			ata_link_abort(qc->dev->link);
2779bdd4dddeSJeff Garzik 		else
2780bdd4dddeSJeff Garzik 			ata_port_abort(ap);
2781bdd4dddeSJeff Garzik 	}
278237b9046aSMark Lord }
2783bdd4dddeSJeff Garzik 
27841aadf5c3STejun Heo static bool mv_process_crpb_response(struct ata_port *ap,
2785fcfb1f77SMark Lord 		struct mv_crpb *response, unsigned int tag, int ncq_enabled)
2786fcfb1f77SMark Lord {
2787fcfb1f77SMark Lord 	u8 ata_status;
2788fcfb1f77SMark Lord 	u16 edma_status = le16_to_cpu(response->flags);
2789752e386cSTejun Heo 
2790fcfb1f77SMark Lord 	/*
2791fcfb1f77SMark Lord 	 * edma_status from a response queue entry:
2792cae5a29dSMark Lord 	 *   LSB is from EDMA_ERR_IRQ_CAUSE (non-NCQ only).
2793fcfb1f77SMark Lord 	 *   MSB is saved ATA status from command completion.
2794fcfb1f77SMark Lord 	 */
2795fcfb1f77SMark Lord 	if (!ncq_enabled) {
2796fcfb1f77SMark Lord 		u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
2797fcfb1f77SMark Lord 		if (err_cause) {
2798fcfb1f77SMark Lord 			/*
2799752e386cSTejun Heo 			 * Error will be seen/handled by
2800752e386cSTejun Heo 			 * mv_err_intr().  So do nothing at all here.
2801fcfb1f77SMark Lord 			 */
28021aadf5c3STejun Heo 			return false;
2803fcfb1f77SMark Lord 		}
2804fcfb1f77SMark Lord 	}
2805fcfb1f77SMark Lord 	ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
280637b9046aSMark Lord 	if (!ac_err_mask(ata_status))
28071aadf5c3STejun Heo 		return true;
280837b9046aSMark Lord 	/* else: leave it for mv_err_intr() */
28091aadf5c3STejun Heo 	return false;
2810fcfb1f77SMark Lord }
2811fcfb1f77SMark Lord 
2812fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
2813bdd4dddeSJeff Garzik {
2814bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2815bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2816fcfb1f77SMark Lord 	u32 in_index;
2817bdd4dddeSJeff Garzik 	bool work_done = false;
28181aadf5c3STejun Heo 	u32 done_mask = 0;
2819fcfb1f77SMark Lord 	int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
2820bdd4dddeSJeff Garzik 
2821fcfb1f77SMark Lord 	/* Get the hardware queue position index */
2822cae5a29dSMark Lord 	in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR)
2823bdd4dddeSJeff Garzik 			>> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2824bdd4dddeSJeff Garzik 
2825fcfb1f77SMark Lord 	/* Process new responses from since the last time we looked */
2826fcfb1f77SMark Lord 	while (in_index != pp->resp_idx) {
28276c1153e0SJeff Garzik 		unsigned int tag;
2828fcfb1f77SMark Lord 		struct mv_crpb *response = &pp->crpb[pp->resp_idx];
2829bdd4dddeSJeff Garzik 
2830fcfb1f77SMark Lord 		pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2831bdd4dddeSJeff Garzik 
2832fcfb1f77SMark Lord 		if (IS_GEN_I(hpriv)) {
2833fcfb1f77SMark Lord 			/* 50xx: no NCQ, only one command active at a time */
28349af5c9c9STejun Heo 			tag = ap->link.active_tag;
2835fcfb1f77SMark Lord 		} else {
2836fcfb1f77SMark Lord 			/* Gen II/IIE: get command tag from CRPB entry */
2837fcfb1f77SMark Lord 			tag = le16_to_cpu(response->id) & 0x1f;
2838bdd4dddeSJeff Garzik 		}
28391aadf5c3STejun Heo 		if (mv_process_crpb_response(ap, response, tag, ncq_enabled))
28401aadf5c3STejun Heo 			done_mask |= 1 << tag;
2841bdd4dddeSJeff Garzik 		work_done = true;
2842bdd4dddeSJeff Garzik 	}
2843bdd4dddeSJeff Garzik 
28441aadf5c3STejun Heo 	if (work_done) {
28451aadf5c3STejun Heo 		ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
28461aadf5c3STejun Heo 
2847352fab70SMark Lord 		/* Update the software queue position index in hardware */
2848bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
2849fcfb1f77SMark Lord 			 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
2850cae5a29dSMark Lord 			 port_mmio + EDMA_RSP_Q_OUT_PTR);
2851c6fd2807SJeff Garzik 	}
28521aadf5c3STejun Heo }
2853c6fd2807SJeff Garzik 
2854a9010329SMark Lord static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2855a9010329SMark Lord {
2856a9010329SMark Lord 	struct mv_port_priv *pp;
2857a9010329SMark Lord 	int edma_was_enabled;
2858a9010329SMark Lord 
2859a9010329SMark Lord 	/*
2860a9010329SMark Lord 	 * Grab a snapshot of the EDMA_EN flag setting,
2861a9010329SMark Lord 	 * so that we have a consistent view for this port,
2862a9010329SMark Lord 	 * even if something we call of our routines changes it.
2863a9010329SMark Lord 	 */
2864a9010329SMark Lord 	pp = ap->private_data;
2865a9010329SMark Lord 	edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2866a9010329SMark Lord 	/*
2867a9010329SMark Lord 	 * Process completed CRPB response(s) before other events.
2868a9010329SMark Lord 	 */
2869a9010329SMark Lord 	if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2870a9010329SMark Lord 		mv_process_crpb_entries(ap, pp);
28714c299ca3SMark Lord 		if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
28724c299ca3SMark Lord 			mv_handle_fbs_ncq_dev_err(ap);
2873a9010329SMark Lord 	}
2874a9010329SMark Lord 	/*
2875a9010329SMark Lord 	 * Handle chip-reported errors, or continue on to handle PIO.
2876a9010329SMark Lord 	 */
2877a9010329SMark Lord 	if (unlikely(port_cause & ERR_IRQ)) {
2878a9010329SMark Lord 		mv_err_intr(ap);
2879a9010329SMark Lord 	} else if (!edma_was_enabled) {
2880a9010329SMark Lord 		struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2881a9010329SMark Lord 		if (qc)
2882c3b28894STejun Heo 			ata_bmdma_port_intr(ap, qc);
2883a9010329SMark Lord 		else
2884a9010329SMark Lord 			mv_unexpected_intr(ap, edma_was_enabled);
2885a9010329SMark Lord 	}
2886a9010329SMark Lord }
2887a9010329SMark Lord 
2888c6fd2807SJeff Garzik /**
2889c6fd2807SJeff Garzik  *      mv_host_intr - Handle all interrupts on the given host controller
2890cca3974eSJeff Garzik  *      @host: host specific structure
28917368f919SMark Lord  *      @main_irq_cause: Main interrupt cause register for the chip.
2892c6fd2807SJeff Garzik  *
2893c6fd2807SJeff Garzik  *      LOCKING:
2894c6fd2807SJeff Garzik  *      Inherited from caller.
2895c6fd2807SJeff Garzik  */
28967368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
2897c6fd2807SJeff Garzik {
2898f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2899eabd5eb1SMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
2900a3718c1fSMark Lord 	unsigned int handled = 0, port;
2901c6fd2807SJeff Garzik 
29022b748a0aSMark Lord 	/* If asserted, clear the "all ports" IRQ coalescing bit */
29032b748a0aSMark Lord 	if (main_irq_cause & ALL_PORTS_COAL_DONE)
2904cae5a29dSMark Lord 		writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
29052b748a0aSMark Lord 
2906a3718c1fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
2907cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[port];
2908eabd5eb1SMark Lord 		unsigned int p, shift, hardport, port_cause;
2909eabd5eb1SMark Lord 
2910a3718c1fSMark Lord 		MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2911a3718c1fSMark Lord 		/*
2912eabd5eb1SMark Lord 		 * Each hc within the host has its own hc_irq_cause register,
2913eabd5eb1SMark Lord 		 * where the interrupting ports bits get ack'd.
2914a3718c1fSMark Lord 		 */
2915eabd5eb1SMark Lord 		if (hardport == 0) {	/* first port on this hc ? */
2916eabd5eb1SMark Lord 			u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2917eabd5eb1SMark Lord 			u32 port_mask, ack_irqs;
2918eabd5eb1SMark Lord 			/*
2919eabd5eb1SMark Lord 			 * Skip this entire hc if nothing pending for any ports
2920eabd5eb1SMark Lord 			 */
2921eabd5eb1SMark Lord 			if (!hc_cause) {
2922eabd5eb1SMark Lord 				port += MV_PORTS_PER_HC - 1;
2923eabd5eb1SMark Lord 				continue;
2924eabd5eb1SMark Lord 			}
2925eabd5eb1SMark Lord 			/*
2926eabd5eb1SMark Lord 			 * We don't need/want to read the hc_irq_cause register,
2927eabd5eb1SMark Lord 			 * because doing so hurts performance, and
2928eabd5eb1SMark Lord 			 * main_irq_cause already gives us everything we need.
2929eabd5eb1SMark Lord 			 *
2930eabd5eb1SMark Lord 			 * But we do have to *write* to the hc_irq_cause to ack
2931eabd5eb1SMark Lord 			 * the ports that we are handling this time through.
2932eabd5eb1SMark Lord 			 *
2933eabd5eb1SMark Lord 			 * This requires that we create a bitmap for those
2934eabd5eb1SMark Lord 			 * ports which interrupted us, and use that bitmap
2935eabd5eb1SMark Lord 			 * to ack (only) those ports via hc_irq_cause.
2936eabd5eb1SMark Lord 			 */
2937eabd5eb1SMark Lord 			ack_irqs = 0;
29382b748a0aSMark Lord 			if (hc_cause & PORTS_0_3_COAL_DONE)
29392b748a0aSMark Lord 				ack_irqs = HC_COAL_IRQ;
2940eabd5eb1SMark Lord 			for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2941eabd5eb1SMark Lord 				if ((port + p) >= hpriv->n_ports)
2942eabd5eb1SMark Lord 					break;
2943eabd5eb1SMark Lord 				port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2944eabd5eb1SMark Lord 				if (hc_cause & port_mask)
2945eabd5eb1SMark Lord 					ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2946eabd5eb1SMark Lord 			}
2947a3718c1fSMark Lord 			hc_mmio = mv_hc_base_from_port(mmio, port);
2948cae5a29dSMark Lord 			writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE);
2949a3718c1fSMark Lord 			handled = 1;
2950a3718c1fSMark Lord 		}
2951a9010329SMark Lord 		/*
2952a9010329SMark Lord 		 * Handle interrupts signalled for this port:
2953a9010329SMark Lord 		 */
2954eabd5eb1SMark Lord 		port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
2955a9010329SMark Lord 		if (port_cause)
2956a9010329SMark Lord 			mv_port_intr(ap, port_cause);
2957eabd5eb1SMark Lord 	}
2958a3718c1fSMark Lord 	return handled;
2959c6fd2807SJeff Garzik }
2960c6fd2807SJeff Garzik 
2961a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
2962bdd4dddeSJeff Garzik {
296302a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2964bdd4dddeSJeff Garzik 	struct ata_port *ap;
2965bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
2966bdd4dddeSJeff Garzik 	struct ata_eh_info *ehi;
2967bdd4dddeSJeff Garzik 	unsigned int i, err_mask, printed = 0;
2968bdd4dddeSJeff Garzik 	u32 err_cause;
2969bdd4dddeSJeff Garzik 
2970cae5a29dSMark Lord 	err_cause = readl(mmio + hpriv->irq_cause_offset);
2971bdd4dddeSJeff Garzik 
2972a44fec1fSJoe Perches 	dev_err(host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n", err_cause);
2973bdd4dddeSJeff Garzik 
2974bdd4dddeSJeff Garzik 	DPRINTK("All regs @ PCI error\n");
2975bdd4dddeSJeff Garzik 	mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
2976bdd4dddeSJeff Garzik 
2977cae5a29dSMark Lord 	writelfl(0, mmio + hpriv->irq_cause_offset);
2978bdd4dddeSJeff Garzik 
2979bdd4dddeSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
2980bdd4dddeSJeff Garzik 		ap = host->ports[i];
2981936fd732STejun Heo 		if (!ata_link_offline(&ap->link)) {
29829af5c9c9STejun Heo 			ehi = &ap->link.eh_info;
2983bdd4dddeSJeff Garzik 			ata_ehi_clear_desc(ehi);
2984bdd4dddeSJeff Garzik 			if (!printed++)
2985bdd4dddeSJeff Garzik 				ata_ehi_push_desc(ehi,
2986bdd4dddeSJeff Garzik 					"PCI err cause 0x%08x", err_cause);
2987bdd4dddeSJeff Garzik 			err_mask = AC_ERR_HOST_BUS;
2988cf480626STejun Heo 			ehi->action = ATA_EH_RESET;
29899af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
2990bdd4dddeSJeff Garzik 			if (qc)
2991bdd4dddeSJeff Garzik 				qc->err_mask |= err_mask;
2992bdd4dddeSJeff Garzik 			else
2993bdd4dddeSJeff Garzik 				ehi->err_mask |= err_mask;
2994bdd4dddeSJeff Garzik 
2995bdd4dddeSJeff Garzik 			ata_port_freeze(ap);
2996bdd4dddeSJeff Garzik 		}
2997bdd4dddeSJeff Garzik 	}
2998a3718c1fSMark Lord 	return 1;	/* handled */
2999bdd4dddeSJeff Garzik }
3000bdd4dddeSJeff Garzik 
3001c6fd2807SJeff Garzik /**
3002c5d3e45aSJeff Garzik  *      mv_interrupt - Main interrupt event handler
3003c6fd2807SJeff Garzik  *      @irq: unused
3004c6fd2807SJeff Garzik  *      @dev_instance: private data; in this case the host structure
3005c6fd2807SJeff Garzik  *
3006c6fd2807SJeff Garzik  *      Read the read only register to determine if any host
3007c6fd2807SJeff Garzik  *      controllers have pending interrupts.  If so, call lower level
3008c6fd2807SJeff Garzik  *      routine to handle.  Also check for PCI errors which are only
3009c6fd2807SJeff Garzik  *      reported here.
3010c6fd2807SJeff Garzik  *
3011c6fd2807SJeff Garzik  *      LOCKING:
3012cca3974eSJeff Garzik  *      This routine holds the host lock while processing pending
3013c6fd2807SJeff Garzik  *      interrupts.
3014c6fd2807SJeff Garzik  */
30157d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
3016c6fd2807SJeff Garzik {
3017cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
3018f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
3019a3718c1fSMark Lord 	unsigned int handled = 0;
30206d3c30efSMark Lord 	int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
302196e2c487SMark Lord 	u32 main_irq_cause, pending_irqs;
3022c6fd2807SJeff Garzik 
3023646a4da5SMark Lord 	spin_lock(&host->lock);
30246d3c30efSMark Lord 
30256d3c30efSMark Lord 	/* for MSI:  block new interrupts while in here */
30266d3c30efSMark Lord 	if (using_msi)
30272b748a0aSMark Lord 		mv_write_main_irq_mask(0, hpriv);
30286d3c30efSMark Lord 
30297368f919SMark Lord 	main_irq_cause = readl(hpriv->main_irq_cause_addr);
303096e2c487SMark Lord 	pending_irqs   = main_irq_cause & hpriv->main_irq_mask;
3031352fab70SMark Lord 	/*
3032352fab70SMark Lord 	 * Deal with cases where we either have nothing pending, or have read
3033352fab70SMark Lord 	 * a bogus register value which can indicate HW removal or PCI fault.
3034c6fd2807SJeff Garzik 	 */
3035a44253d2SMark Lord 	if (pending_irqs && main_irq_cause != 0xffffffffU) {
30361f398472SMark Lord 		if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
3037a3718c1fSMark Lord 			handled = mv_pci_error(host, hpriv->base);
3038a3718c1fSMark Lord 		else
3039a44253d2SMark Lord 			handled = mv_host_intr(host, pending_irqs);
3040bdd4dddeSJeff Garzik 	}
30416d3c30efSMark Lord 
30426d3c30efSMark Lord 	/* for MSI: unmask; interrupt cause bits will retrigger now */
30436d3c30efSMark Lord 	if (using_msi)
30442b748a0aSMark Lord 		mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
30456d3c30efSMark Lord 
30469d51af7bSMark Lord 	spin_unlock(&host->lock);
30479d51af7bSMark Lord 
3048c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
3049c6fd2807SJeff Garzik }
3050c6fd2807SJeff Garzik 
3051c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
3052c6fd2807SJeff Garzik {
3053c6fd2807SJeff Garzik 	unsigned int ofs;
3054c6fd2807SJeff Garzik 
3055c6fd2807SJeff Garzik 	switch (sc_reg_in) {
3056c6fd2807SJeff Garzik 	case SCR_STATUS:
3057c6fd2807SJeff Garzik 	case SCR_ERROR:
3058c6fd2807SJeff Garzik 	case SCR_CONTROL:
3059c6fd2807SJeff Garzik 		ofs = sc_reg_in * sizeof(u32);
3060c6fd2807SJeff Garzik 		break;
3061c6fd2807SJeff Garzik 	default:
3062c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
3063c6fd2807SJeff Garzik 		break;
3064c6fd2807SJeff Garzik 	}
3065c6fd2807SJeff Garzik 	return ofs;
3066c6fd2807SJeff Garzik }
3067c6fd2807SJeff Garzik 
306882ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
3069c6fd2807SJeff Garzik {
307082ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
3071f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
307282ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3073c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
3074c6fd2807SJeff Garzik 
3075da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
3076da3dbb17STejun Heo 		*val = readl(addr + ofs);
3077da3dbb17STejun Heo 		return 0;
3078da3dbb17STejun Heo 	} else
3079da3dbb17STejun Heo 		return -EINVAL;
3080c6fd2807SJeff Garzik }
3081c6fd2807SJeff Garzik 
308282ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
3083c6fd2807SJeff Garzik {
308482ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
3085f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
308682ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3087c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
3088c6fd2807SJeff Garzik 
3089da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
30900d5ff566STejun Heo 		writelfl(val, addr + ofs);
3091da3dbb17STejun Heo 		return 0;
3092da3dbb17STejun Heo 	} else
3093da3dbb17STejun Heo 		return -EINVAL;
3094c6fd2807SJeff Garzik }
3095c6fd2807SJeff Garzik 
30967bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
3097c6fd2807SJeff Garzik {
30987bb3c529SSaeed Bishara 	struct pci_dev *pdev = to_pci_dev(host->dev);
3099c6fd2807SJeff Garzik 	int early_5080;
3100c6fd2807SJeff Garzik 
310144c10138SAuke Kok 	early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
3102c6fd2807SJeff Garzik 
3103c6fd2807SJeff Garzik 	if (!early_5080) {
3104c6fd2807SJeff Garzik 		u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3105c6fd2807SJeff Garzik 		tmp |= (1 << 0);
3106c6fd2807SJeff Garzik 		writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3107c6fd2807SJeff Garzik 	}
3108c6fd2807SJeff Garzik 
31097bb3c529SSaeed Bishara 	mv_reset_pci_bus(host, mmio);
3110c6fd2807SJeff Garzik }
3111c6fd2807SJeff Garzik 
3112c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3113c6fd2807SJeff Garzik {
3114cae5a29dSMark Lord 	writel(0x0fcfffff, mmio + FLASH_CTL);
3115c6fd2807SJeff Garzik }
3116c6fd2807SJeff Garzik 
3117c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
3118c6fd2807SJeff Garzik 			   void __iomem *mmio)
3119c6fd2807SJeff Garzik {
3120c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
3121c6fd2807SJeff Garzik 	u32 tmp;
3122c6fd2807SJeff Garzik 
3123c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
3124c6fd2807SJeff Garzik 
3125c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0x1800;	/* bits 12:11 */
3126c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0xe0;	/* bits 7:5 */
3127c6fd2807SJeff Garzik }
3128c6fd2807SJeff Garzik 
3129c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3130c6fd2807SJeff Garzik {
3131c6fd2807SJeff Garzik 	u32 tmp;
3132c6fd2807SJeff Garzik 
3133cae5a29dSMark Lord 	writel(0, mmio + GPIO_PORT_CTL);
3134c6fd2807SJeff Garzik 
3135c6fd2807SJeff Garzik 	/* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
3136c6fd2807SJeff Garzik 
3137c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3138c6fd2807SJeff Garzik 	tmp |= ~(1 << 0);
3139c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3140c6fd2807SJeff Garzik }
3141c6fd2807SJeff Garzik 
3142c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3143c6fd2807SJeff Garzik 			   unsigned int port)
3144c6fd2807SJeff Garzik {
3145c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, port);
3146c6fd2807SJeff Garzik 	const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
3147c6fd2807SJeff Garzik 	u32 tmp;
3148c6fd2807SJeff Garzik 	int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
3149c6fd2807SJeff Garzik 
3150c6fd2807SJeff Garzik 	if (fix_apm_sq) {
3151cae5a29dSMark Lord 		tmp = readl(phy_mmio + MV5_LTMODE);
3152c6fd2807SJeff Garzik 		tmp |= (1 << 19);
3153cae5a29dSMark Lord 		writel(tmp, phy_mmio + MV5_LTMODE);
3154c6fd2807SJeff Garzik 
3155cae5a29dSMark Lord 		tmp = readl(phy_mmio + MV5_PHY_CTL);
3156c6fd2807SJeff Garzik 		tmp &= ~0x3;
3157c6fd2807SJeff Garzik 		tmp |= 0x1;
3158cae5a29dSMark Lord 		writel(tmp, phy_mmio + MV5_PHY_CTL);
3159c6fd2807SJeff Garzik 	}
3160c6fd2807SJeff Garzik 
3161c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
3162c6fd2807SJeff Garzik 	tmp &= ~mask;
3163c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].pre;
3164c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].amps;
3165c6fd2807SJeff Garzik 	writel(tmp, phy_mmio + MV5_PHY_MODE);
3166c6fd2807SJeff Garzik }
3167c6fd2807SJeff Garzik 
3168c6fd2807SJeff Garzik 
3169c6fd2807SJeff Garzik #undef ZERO
3170c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
3171c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
3172c6fd2807SJeff Garzik 			     unsigned int port)
3173c6fd2807SJeff Garzik {
3174c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
3175c6fd2807SJeff Garzik 
3176e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
3177c6fd2807SJeff Garzik 
3178c6fd2807SJeff Garzik 	ZERO(0x028);	/* command */
3179cae5a29dSMark Lord 	writel(0x11f, port_mmio + EDMA_CFG);
3180c6fd2807SJeff Garzik 	ZERO(0x004);	/* timer */
3181c6fd2807SJeff Garzik 	ZERO(0x008);	/* irq err cause */
3182c6fd2807SJeff Garzik 	ZERO(0x00c);	/* irq err mask */
3183c6fd2807SJeff Garzik 	ZERO(0x010);	/* rq bah */
3184c6fd2807SJeff Garzik 	ZERO(0x014);	/* rq inp */
3185c6fd2807SJeff Garzik 	ZERO(0x018);	/* rq outp */
3186c6fd2807SJeff Garzik 	ZERO(0x01c);	/* respq bah */
3187c6fd2807SJeff Garzik 	ZERO(0x024);	/* respq outp */
3188c6fd2807SJeff Garzik 	ZERO(0x020);	/* respq inp */
3189c6fd2807SJeff Garzik 	ZERO(0x02c);	/* test control */
3190cae5a29dSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
3191c6fd2807SJeff Garzik }
3192c6fd2807SJeff Garzik #undef ZERO
3193c6fd2807SJeff Garzik 
3194c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
3195c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3196c6fd2807SJeff Garzik 			unsigned int hc)
3197c6fd2807SJeff Garzik {
3198c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3199c6fd2807SJeff Garzik 	u32 tmp;
3200c6fd2807SJeff Garzik 
3201c6fd2807SJeff Garzik 	ZERO(0x00c);
3202c6fd2807SJeff Garzik 	ZERO(0x010);
3203c6fd2807SJeff Garzik 	ZERO(0x014);
3204c6fd2807SJeff Garzik 	ZERO(0x018);
3205c6fd2807SJeff Garzik 
3206c6fd2807SJeff Garzik 	tmp = readl(hc_mmio + 0x20);
3207c6fd2807SJeff Garzik 	tmp &= 0x1c1c1c1c;
3208c6fd2807SJeff Garzik 	tmp |= 0x03030303;
3209c6fd2807SJeff Garzik 	writel(tmp, hc_mmio + 0x20);
3210c6fd2807SJeff Garzik }
3211c6fd2807SJeff Garzik #undef ZERO
3212c6fd2807SJeff Garzik 
3213c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3214c6fd2807SJeff Garzik 			unsigned int n_hc)
3215c6fd2807SJeff Garzik {
3216c6fd2807SJeff Garzik 	unsigned int hc, port;
3217c6fd2807SJeff Garzik 
3218c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3219c6fd2807SJeff Garzik 		for (port = 0; port < MV_PORTS_PER_HC; port++)
3220c6fd2807SJeff Garzik 			mv5_reset_hc_port(hpriv, mmio,
3221c6fd2807SJeff Garzik 					  (hc * MV_PORTS_PER_HC) + port);
3222c6fd2807SJeff Garzik 
3223c6fd2807SJeff Garzik 		mv5_reset_one_hc(hpriv, mmio, hc);
3224c6fd2807SJeff Garzik 	}
3225c6fd2807SJeff Garzik 
3226c6fd2807SJeff Garzik 	return 0;
3227c6fd2807SJeff Garzik }
3228c6fd2807SJeff Garzik 
3229c6fd2807SJeff Garzik #undef ZERO
3230c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
32317bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
3232c6fd2807SJeff Garzik {
323302a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3234c6fd2807SJeff Garzik 	u32 tmp;
3235c6fd2807SJeff Garzik 
3236cae5a29dSMark Lord 	tmp = readl(mmio + MV_PCI_MODE);
3237c6fd2807SJeff Garzik 	tmp &= 0xff00ffff;
3238cae5a29dSMark Lord 	writel(tmp, mmio + MV_PCI_MODE);
3239c6fd2807SJeff Garzik 
3240c6fd2807SJeff Garzik 	ZERO(MV_PCI_DISC_TIMER);
3241c6fd2807SJeff Garzik 	ZERO(MV_PCI_MSI_TRIGGER);
3242cae5a29dSMark Lord 	writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
3243c6fd2807SJeff Garzik 	ZERO(MV_PCI_SERR_MASK);
3244cae5a29dSMark Lord 	ZERO(hpriv->irq_cause_offset);
3245cae5a29dSMark Lord 	ZERO(hpriv->irq_mask_offset);
3246c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_LOW_ADDRESS);
3247c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_HIGH_ADDRESS);
3248c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_ATTRIBUTE);
3249c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_COMMAND);
3250c6fd2807SJeff Garzik }
3251c6fd2807SJeff Garzik #undef ZERO
3252c6fd2807SJeff Garzik 
3253c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3254c6fd2807SJeff Garzik {
3255c6fd2807SJeff Garzik 	u32 tmp;
3256c6fd2807SJeff Garzik 
3257c6fd2807SJeff Garzik 	mv5_reset_flash(hpriv, mmio);
3258c6fd2807SJeff Garzik 
3259cae5a29dSMark Lord 	tmp = readl(mmio + GPIO_PORT_CTL);
3260c6fd2807SJeff Garzik 	tmp &= 0x3;
3261c6fd2807SJeff Garzik 	tmp |= (1 << 5) | (1 << 6);
3262cae5a29dSMark Lord 	writel(tmp, mmio + GPIO_PORT_CTL);
3263c6fd2807SJeff Garzik }
3264c6fd2807SJeff Garzik 
3265c6fd2807SJeff Garzik /**
3266c6fd2807SJeff Garzik  *      mv6_reset_hc - Perform the 6xxx global soft reset
3267c6fd2807SJeff Garzik  *      @mmio: base address of the HBA
3268c6fd2807SJeff Garzik  *
3269c6fd2807SJeff Garzik  *      This routine only applies to 6xxx parts.
3270c6fd2807SJeff Garzik  *
3271c6fd2807SJeff Garzik  *      LOCKING:
3272c6fd2807SJeff Garzik  *      Inherited from caller.
3273c6fd2807SJeff Garzik  */
3274c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3275c6fd2807SJeff Garzik 			unsigned int n_hc)
3276c6fd2807SJeff Garzik {
3277cae5a29dSMark Lord 	void __iomem *reg = mmio + PCI_MAIN_CMD_STS;
3278c6fd2807SJeff Garzik 	int i, rc = 0;
3279c6fd2807SJeff Garzik 	u32 t;
3280c6fd2807SJeff Garzik 
3281c6fd2807SJeff Garzik 	/* Following procedure defined in PCI "main command and status
3282c6fd2807SJeff Garzik 	 * register" table.
3283c6fd2807SJeff Garzik 	 */
3284c6fd2807SJeff Garzik 	t = readl(reg);
3285c6fd2807SJeff Garzik 	writel(t | STOP_PCI_MASTER, reg);
3286c6fd2807SJeff Garzik 
3287c6fd2807SJeff Garzik 	for (i = 0; i < 1000; i++) {
3288c6fd2807SJeff Garzik 		udelay(1);
3289c6fd2807SJeff Garzik 		t = readl(reg);
32902dcb407eSJeff Garzik 		if (PCI_MASTER_EMPTY & t)
3291c6fd2807SJeff Garzik 			break;
3292c6fd2807SJeff Garzik 	}
3293c6fd2807SJeff Garzik 	if (!(PCI_MASTER_EMPTY & t)) {
3294c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
3295c6fd2807SJeff Garzik 		rc = 1;
3296c6fd2807SJeff Garzik 		goto done;
3297c6fd2807SJeff Garzik 	}
3298c6fd2807SJeff Garzik 
3299c6fd2807SJeff Garzik 	/* set reset */
3300c6fd2807SJeff Garzik 	i = 5;
3301c6fd2807SJeff Garzik 	do {
3302c6fd2807SJeff Garzik 		writel(t | GLOB_SFT_RST, reg);
3303c6fd2807SJeff Garzik 		t = readl(reg);
3304c6fd2807SJeff Garzik 		udelay(1);
3305c6fd2807SJeff Garzik 	} while (!(GLOB_SFT_RST & t) && (i-- > 0));
3306c6fd2807SJeff Garzik 
3307c6fd2807SJeff Garzik 	if (!(GLOB_SFT_RST & t)) {
3308c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't set global reset\n");
3309c6fd2807SJeff Garzik 		rc = 1;
3310c6fd2807SJeff Garzik 		goto done;
3311c6fd2807SJeff Garzik 	}
3312c6fd2807SJeff Garzik 
3313c6fd2807SJeff Garzik 	/* clear reset and *reenable the PCI master* (not mentioned in spec) */
3314c6fd2807SJeff Garzik 	i = 5;
3315c6fd2807SJeff Garzik 	do {
3316c6fd2807SJeff Garzik 		writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
3317c6fd2807SJeff Garzik 		t = readl(reg);
3318c6fd2807SJeff Garzik 		udelay(1);
3319c6fd2807SJeff Garzik 	} while ((GLOB_SFT_RST & t) && (i-- > 0));
3320c6fd2807SJeff Garzik 
3321c6fd2807SJeff Garzik 	if (GLOB_SFT_RST & t) {
3322c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
3323c6fd2807SJeff Garzik 		rc = 1;
3324c6fd2807SJeff Garzik 	}
3325c6fd2807SJeff Garzik done:
3326c6fd2807SJeff Garzik 	return rc;
3327c6fd2807SJeff Garzik }
3328c6fd2807SJeff Garzik 
3329c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
3330c6fd2807SJeff Garzik 			   void __iomem *mmio)
3331c6fd2807SJeff Garzik {
3332c6fd2807SJeff Garzik 	void __iomem *port_mmio;
3333c6fd2807SJeff Garzik 	u32 tmp;
3334c6fd2807SJeff Garzik 
3335cae5a29dSMark Lord 	tmp = readl(mmio + RESET_CFG);
3336c6fd2807SJeff Garzik 	if ((tmp & (1 << 0)) == 0) {
3337c6fd2807SJeff Garzik 		hpriv->signal[idx].amps = 0x7 << 8;
3338c6fd2807SJeff Garzik 		hpriv->signal[idx].pre = 0x1 << 5;
3339c6fd2807SJeff Garzik 		return;
3340c6fd2807SJeff Garzik 	}
3341c6fd2807SJeff Garzik 
3342c6fd2807SJeff Garzik 	port_mmio = mv_port_base(mmio, idx);
3343c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE2);
3344c6fd2807SJeff Garzik 
3345c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
3346c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
3347c6fd2807SJeff Garzik }
3348c6fd2807SJeff Garzik 
3349c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3350c6fd2807SJeff Garzik {
3351cae5a29dSMark Lord 	writel(0x00000060, mmio + GPIO_PORT_CTL);
3352c6fd2807SJeff Garzik }
3353c6fd2807SJeff Garzik 
3354c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3355c6fd2807SJeff Garzik 			   unsigned int port)
3356c6fd2807SJeff Garzik {
3357c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
3358c6fd2807SJeff Garzik 
3359c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
3360c6fd2807SJeff Garzik 	int fix_phy_mode2 =
3361c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
3362c6fd2807SJeff Garzik 	int fix_phy_mode4 =
3363c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
33648c30a8b9SMark Lord 	u32 m2, m3;
3365c6fd2807SJeff Garzik 
3366c6fd2807SJeff Garzik 	if (fix_phy_mode2) {
3367c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
3368c6fd2807SJeff Garzik 		m2 &= ~(1 << 16);
3369c6fd2807SJeff Garzik 		m2 |= (1 << 31);
3370c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
3371c6fd2807SJeff Garzik 
3372c6fd2807SJeff Garzik 		udelay(200);
3373c6fd2807SJeff Garzik 
3374c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
3375c6fd2807SJeff Garzik 		m2 &= ~((1 << 16) | (1 << 31));
3376c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
3377c6fd2807SJeff Garzik 
3378c6fd2807SJeff Garzik 		udelay(200);
3379c6fd2807SJeff Garzik 	}
3380c6fd2807SJeff Garzik 
33818c30a8b9SMark Lord 	/*
33828c30a8b9SMark Lord 	 * Gen-II/IIe PHY_MODE3 errata RM#2:
33838c30a8b9SMark Lord 	 * Achieves better receiver noise performance than the h/w default:
33848c30a8b9SMark Lord 	 */
33858c30a8b9SMark Lord 	m3 = readl(port_mmio + PHY_MODE3);
33868c30a8b9SMark Lord 	m3 = (m3 & 0x1f) | (0x5555601 << 5);
3387c6fd2807SJeff Garzik 
33880388a8c0SMark Lord 	/* Guideline 88F5182 (GL# SATA-S11) */
33890388a8c0SMark Lord 	if (IS_SOC(hpriv))
33900388a8c0SMark Lord 		m3 &= ~0x1c;
33910388a8c0SMark Lord 
3392c6fd2807SJeff Garzik 	if (fix_phy_mode4) {
3393ba069e37SMark Lord 		u32 m4 = readl(port_mmio + PHY_MODE4);
3394ba069e37SMark Lord 		/*
3395ba069e37SMark Lord 		 * Enforce reserved-bit restrictions on GenIIe devices only.
3396ba069e37SMark Lord 		 * For earlier chipsets, force only the internal config field
3397ba069e37SMark Lord 		 *  (workaround for errata FEr SATA#10 part 1).
3398ba069e37SMark Lord 		 */
33998c30a8b9SMark Lord 		if (IS_GEN_IIE(hpriv))
3400ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
3401ba069e37SMark Lord 		else
3402ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
34038c30a8b9SMark Lord 		writel(m4, port_mmio + PHY_MODE4);
3404c6fd2807SJeff Garzik 	}
3405b406c7a6SMark Lord 	/*
3406b406c7a6SMark Lord 	 * Workaround for 60x1-B2 errata SATA#13:
3407b406c7a6SMark Lord 	 * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
3408b406c7a6SMark Lord 	 * so we must always rewrite PHY_MODE3 after PHY_MODE4.
3409ba68460bSMark Lord 	 * Or ensure we use writelfl() when writing PHY_MODE4.
3410b406c7a6SMark Lord 	 */
3411b406c7a6SMark Lord 	writel(m3, port_mmio + PHY_MODE3);
3412c6fd2807SJeff Garzik 
3413c6fd2807SJeff Garzik 	/* Revert values of pre-emphasis and signal amps to the saved ones */
3414c6fd2807SJeff Garzik 	m2 = readl(port_mmio + PHY_MODE2);
3415c6fd2807SJeff Garzik 
3416c6fd2807SJeff Garzik 	m2 &= ~MV_M2_PREAMP_MASK;
3417c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].amps;
3418c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].pre;
3419c6fd2807SJeff Garzik 	m2 &= ~(1 << 16);
3420c6fd2807SJeff Garzik 
3421c6fd2807SJeff Garzik 	/* according to mvSata 3.6.1, some IIE values are fixed */
3422c6fd2807SJeff Garzik 	if (IS_GEN_IIE(hpriv)) {
3423c6fd2807SJeff Garzik 		m2 &= ~0xC30FF01F;
3424c6fd2807SJeff Garzik 		m2 |= 0x0000900F;
3425c6fd2807SJeff Garzik 	}
3426c6fd2807SJeff Garzik 
3427c6fd2807SJeff Garzik 	writel(m2, port_mmio + PHY_MODE2);
3428c6fd2807SJeff Garzik }
3429c6fd2807SJeff Garzik 
3430f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
3431f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
3432f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
3433f351b2d6SSaeed Bishara 				      void __iomem *mmio)
3434f351b2d6SSaeed Bishara {
3435f351b2d6SSaeed Bishara 	return;
3436f351b2d6SSaeed Bishara }
3437f351b2d6SSaeed Bishara 
3438f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
3439f351b2d6SSaeed Bishara 			   void __iomem *mmio)
3440f351b2d6SSaeed Bishara {
3441f351b2d6SSaeed Bishara 	void __iomem *port_mmio;
3442f351b2d6SSaeed Bishara 	u32 tmp;
3443f351b2d6SSaeed Bishara 
3444f351b2d6SSaeed Bishara 	port_mmio = mv_port_base(mmio, idx);
3445f351b2d6SSaeed Bishara 	tmp = readl(port_mmio + PHY_MODE2);
3446f351b2d6SSaeed Bishara 
3447f351b2d6SSaeed Bishara 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
3448f351b2d6SSaeed Bishara 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
3449f351b2d6SSaeed Bishara }
3450f351b2d6SSaeed Bishara 
3451f351b2d6SSaeed Bishara #undef ZERO
3452f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
3453f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
3454f351b2d6SSaeed Bishara 					void __iomem *mmio, unsigned int port)
3455f351b2d6SSaeed Bishara {
3456f351b2d6SSaeed Bishara 	void __iomem *port_mmio = mv_port_base(mmio, port);
3457f351b2d6SSaeed Bishara 
3458e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
3459f351b2d6SSaeed Bishara 
3460f351b2d6SSaeed Bishara 	ZERO(0x028);		/* command */
3461cae5a29dSMark Lord 	writel(0x101f, port_mmio + EDMA_CFG);
3462f351b2d6SSaeed Bishara 	ZERO(0x004);		/* timer */
3463f351b2d6SSaeed Bishara 	ZERO(0x008);		/* irq err cause */
3464f351b2d6SSaeed Bishara 	ZERO(0x00c);		/* irq err mask */
3465f351b2d6SSaeed Bishara 	ZERO(0x010);		/* rq bah */
3466f351b2d6SSaeed Bishara 	ZERO(0x014);		/* rq inp */
3467f351b2d6SSaeed Bishara 	ZERO(0x018);		/* rq outp */
3468f351b2d6SSaeed Bishara 	ZERO(0x01c);		/* respq bah */
3469f351b2d6SSaeed Bishara 	ZERO(0x024);		/* respq outp */
3470f351b2d6SSaeed Bishara 	ZERO(0x020);		/* respq inp */
3471f351b2d6SSaeed Bishara 	ZERO(0x02c);		/* test control */
3472d7b0c143SSaeed Bishara 	writel(0x800, port_mmio + EDMA_IORDY_TMOUT);
3473f351b2d6SSaeed Bishara }
3474f351b2d6SSaeed Bishara 
3475f351b2d6SSaeed Bishara #undef ZERO
3476f351b2d6SSaeed Bishara 
3477f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
3478f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
3479f351b2d6SSaeed Bishara 				       void __iomem *mmio)
3480f351b2d6SSaeed Bishara {
3481f351b2d6SSaeed Bishara 	void __iomem *hc_mmio = mv_hc_base(mmio, 0);
3482f351b2d6SSaeed Bishara 
3483f351b2d6SSaeed Bishara 	ZERO(0x00c);
3484f351b2d6SSaeed Bishara 	ZERO(0x010);
3485f351b2d6SSaeed Bishara 	ZERO(0x014);
3486f351b2d6SSaeed Bishara 
3487f351b2d6SSaeed Bishara }
3488f351b2d6SSaeed Bishara 
3489f351b2d6SSaeed Bishara #undef ZERO
3490f351b2d6SSaeed Bishara 
3491f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
3492f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc)
3493f351b2d6SSaeed Bishara {
3494f351b2d6SSaeed Bishara 	unsigned int port;
3495f351b2d6SSaeed Bishara 
3496f351b2d6SSaeed Bishara 	for (port = 0; port < hpriv->n_ports; port++)
3497f351b2d6SSaeed Bishara 		mv_soc_reset_hc_port(hpriv, mmio, port);
3498f351b2d6SSaeed Bishara 
3499f351b2d6SSaeed Bishara 	mv_soc_reset_one_hc(hpriv, mmio);
3500f351b2d6SSaeed Bishara 
3501f351b2d6SSaeed Bishara 	return 0;
3502f351b2d6SSaeed Bishara }
3503f351b2d6SSaeed Bishara 
3504f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
3505f351b2d6SSaeed Bishara 				      void __iomem *mmio)
3506f351b2d6SSaeed Bishara {
3507f351b2d6SSaeed Bishara 	return;
3508f351b2d6SSaeed Bishara }
3509f351b2d6SSaeed Bishara 
3510f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
3511f351b2d6SSaeed Bishara {
3512f351b2d6SSaeed Bishara 	return;
3513f351b2d6SSaeed Bishara }
3514f351b2d6SSaeed Bishara 
351529b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
351629b7e43cSMartin Michlmayr 				  void __iomem *mmio, unsigned int port)
351729b7e43cSMartin Michlmayr {
351829b7e43cSMartin Michlmayr 	void __iomem *port_mmio = mv_port_base(mmio, port);
351929b7e43cSMartin Michlmayr 	u32	reg;
352029b7e43cSMartin Michlmayr 
352129b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE3);
352229b7e43cSMartin Michlmayr 	reg &= ~(0x3 << 27);	/* SELMUPF (bits 28:27) to 1 */
352329b7e43cSMartin Michlmayr 	reg |= (0x1 << 27);
352429b7e43cSMartin Michlmayr 	reg &= ~(0x3 << 29);	/* SELMUPI (bits 30:29) to 1 */
352529b7e43cSMartin Michlmayr 	reg |= (0x1 << 29);
352629b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE3);
352729b7e43cSMartin Michlmayr 
352829b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE4);
352929b7e43cSMartin Michlmayr 	reg &= ~0x1;	/* SATU_OD8 (bit 0) to 0, reserved bit 16 must be set */
353029b7e43cSMartin Michlmayr 	reg |= (0x1 << 16);
353129b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE4);
353229b7e43cSMartin Michlmayr 
353329b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE9_GEN2);
353429b7e43cSMartin Michlmayr 	reg &= ~0xf;	/* TXAMP[3:0] (bits 3:0) to 8 */
353529b7e43cSMartin Michlmayr 	reg |= 0x8;
353629b7e43cSMartin Michlmayr 	reg &= ~(0x1 << 14);	/* TXAMP[4] (bit 14) to 0 */
353729b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE9_GEN2);
353829b7e43cSMartin Michlmayr 
353929b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE9_GEN1);
354029b7e43cSMartin Michlmayr 	reg &= ~0xf;	/* TXAMP[3:0] (bits 3:0) to 8 */
354129b7e43cSMartin Michlmayr 	reg |= 0x8;
354229b7e43cSMartin Michlmayr 	reg &= ~(0x1 << 14);	/* TXAMP[4] (bit 14) to 0 */
354329b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE9_GEN1);
354429b7e43cSMartin Michlmayr }
354529b7e43cSMartin Michlmayr 
354629b7e43cSMartin Michlmayr /**
354729b7e43cSMartin Michlmayr  *	soc_is_65 - check if the soc is 65 nano device
354829b7e43cSMartin Michlmayr  *
354929b7e43cSMartin Michlmayr  *	Detect the type of the SoC, this is done by reading the PHYCFG_OFS
355029b7e43cSMartin Michlmayr  *	register, this register should contain non-zero value and it exists only
355129b7e43cSMartin Michlmayr  *	in the 65 nano devices, when reading it from older devices we get 0.
355229b7e43cSMartin Michlmayr  */
355329b7e43cSMartin Michlmayr static bool soc_is_65n(struct mv_host_priv *hpriv)
355429b7e43cSMartin Michlmayr {
355529b7e43cSMartin Michlmayr 	void __iomem *port0_mmio = mv_port_base(hpriv->base, 0);
355629b7e43cSMartin Michlmayr 
355729b7e43cSMartin Michlmayr 	if (readl(port0_mmio + PHYCFG_OFS))
355829b7e43cSMartin Michlmayr 		return true;
355929b7e43cSMartin Michlmayr 	return false;
356029b7e43cSMartin Michlmayr }
356129b7e43cSMartin Michlmayr 
35628e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
3563b67a1064SMark Lord {
3564cae5a29dSMark Lord 	u32 ifcfg = readl(port_mmio + SATA_IFCFG);
3565b67a1064SMark Lord 
35668e7decdbSMark Lord 	ifcfg = (ifcfg & 0xf7f) | 0x9b1000;	/* from chip spec */
3567b67a1064SMark Lord 	if (want_gen2i)
35688e7decdbSMark Lord 		ifcfg |= (1 << 7);		/* enable gen2i speed */
3569cae5a29dSMark Lord 	writelfl(ifcfg, port_mmio + SATA_IFCFG);
3570b67a1064SMark Lord }
3571b67a1064SMark Lord 
3572e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
3573c6fd2807SJeff Garzik 			     unsigned int port_no)
3574c6fd2807SJeff Garzik {
3575c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port_no);
3576c6fd2807SJeff Garzik 
35778e7decdbSMark Lord 	/*
35788e7decdbSMark Lord 	 * The datasheet warns against setting EDMA_RESET when EDMA is active
35798e7decdbSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
35808e7decdbSMark Lord 	 * to disable the EDMA engine before doing the EDMA_RESET operation.
35818e7decdbSMark Lord 	 */
35820d8be5cbSMark Lord 	mv_stop_edma_engine(port_mmio);
3583cae5a29dSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3584c6fd2807SJeff Garzik 
3585b67a1064SMark Lord 	if (!IS_GEN_I(hpriv)) {
35868e7decdbSMark Lord 		/* Enable 3.0gb/s link speed: this survives EDMA_RESET */
35878e7decdbSMark Lord 		mv_setup_ifcfg(port_mmio, 1);
3588c6fd2807SJeff Garzik 	}
3589b67a1064SMark Lord 	/*
35908e7decdbSMark Lord 	 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
3591b67a1064SMark Lord 	 * link, and physical layers.  It resets all SATA interface registers
3592cae5a29dSMark Lord 	 * (except for SATA_IFCFG), and issues a COMRESET to the dev.
3593c6fd2807SJeff Garzik 	 */
3594cae5a29dSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3595b67a1064SMark Lord 	udelay(25);	/* allow reset propagation */
3596cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_CMD);
3597c6fd2807SJeff Garzik 
3598c6fd2807SJeff Garzik 	hpriv->ops->phy_errata(hpriv, mmio, port_no);
3599c6fd2807SJeff Garzik 
3600ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv))
3601c6fd2807SJeff Garzik 		mdelay(1);
3602c6fd2807SJeff Garzik }
3603c6fd2807SJeff Garzik 
3604e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
3605e49856d8SMark Lord {
3606e49856d8SMark Lord 	if (sata_pmp_supported(ap)) {
3607e49856d8SMark Lord 		void __iomem *port_mmio = mv_ap_base(ap);
3608cae5a29dSMark Lord 		u32 reg = readl(port_mmio + SATA_IFCTL);
3609e49856d8SMark Lord 		int old = reg & 0xf;
3610e49856d8SMark Lord 
3611e49856d8SMark Lord 		if (old != pmp) {
3612e49856d8SMark Lord 			reg = (reg & ~0xf) | pmp;
3613cae5a29dSMark Lord 			writelfl(reg, port_mmio + SATA_IFCTL);
3614e49856d8SMark Lord 		}
3615e49856d8SMark Lord 	}
3616e49856d8SMark Lord }
3617e49856d8SMark Lord 
3618e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
3619bdd4dddeSJeff Garzik 				unsigned long deadline)
3620c6fd2807SJeff Garzik {
3621e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
3622e49856d8SMark Lord 	return sata_std_hardreset(link, class, deadline);
3623e49856d8SMark Lord }
3624c6fd2807SJeff Garzik 
3625e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
3626e49856d8SMark Lord 				unsigned long deadline)
3627da3dbb17STejun Heo {
3628e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
3629e49856d8SMark Lord 	return ata_sff_softreset(link, class, deadline);
3630bdd4dddeSJeff Garzik }
3631bdd4dddeSJeff Garzik 
3632cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
3633bdd4dddeSJeff Garzik 			unsigned long deadline)
3634bdd4dddeSJeff Garzik {
3635cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3636bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
3637b562468cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
3638f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
36390d8be5cbSMark Lord 	int rc, attempts = 0, extra = 0;
36400d8be5cbSMark Lord 	u32 sstatus;
36410d8be5cbSMark Lord 	bool online;
3642bdd4dddeSJeff Garzik 
3643e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, ap->port_no);
3644b562468cSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
3645d16ab3f6SMark Lord 	pp->pp_flags &=
3646d16ab3f6SMark Lord 	  ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
3647bdd4dddeSJeff Garzik 
36480d8be5cbSMark Lord 	/* Workaround for errata FEr SATA#10 (part 2) */
36490d8be5cbSMark Lord 	do {
365017c5aab5SMark Lord 		const unsigned long *timing =
365117c5aab5SMark Lord 				sata_ehc_deb_timing(&link->eh_context);
3652bdd4dddeSJeff Garzik 
365317c5aab5SMark Lord 		rc = sata_link_hardreset(link, timing, deadline + extra,
365417c5aab5SMark Lord 					 &online, NULL);
36559dcffd99SMark Lord 		rc = online ? -EAGAIN : rc;
365617c5aab5SMark Lord 		if (rc)
36570d8be5cbSMark Lord 			return rc;
36580d8be5cbSMark Lord 		sata_scr_read(link, SCR_STATUS, &sstatus);
36590d8be5cbSMark Lord 		if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
36600d8be5cbSMark Lord 			/* Force 1.5gb/s link speed and try again */
36618e7decdbSMark Lord 			mv_setup_ifcfg(mv_ap_base(ap), 0);
36620d8be5cbSMark Lord 			if (time_after(jiffies + HZ, deadline))
36630d8be5cbSMark Lord 				extra = HZ; /* only extend it once, max */
3664bdd4dddeSJeff Garzik 		}
36650d8be5cbSMark Lord 	} while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
366608da1759SMark Lord 	mv_save_cached_regs(ap);
366766e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
3668bdd4dddeSJeff Garzik 
366917c5aab5SMark Lord 	return rc;
3670bdd4dddeSJeff Garzik }
3671bdd4dddeSJeff Garzik 
3672bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
3673c6fd2807SJeff Garzik {
36741cfd19aeSMark Lord 	mv_stop_edma(ap);
3675c4de573bSMark Lord 	mv_enable_port_irqs(ap, 0);
3676c6fd2807SJeff Garzik }
3677bdd4dddeSJeff Garzik 
3678bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
3679bdd4dddeSJeff Garzik {
3680f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
3681c4de573bSMark Lord 	unsigned int port = ap->port_no;
3682c4de573bSMark Lord 	unsigned int hardport = mv_hardport_from_port(port);
36831cfd19aeSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
3684bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
3685c4de573bSMark Lord 	u32 hc_irq_cause;
3686bdd4dddeSJeff Garzik 
3687bdd4dddeSJeff Garzik 	/* clear EDMA errors on this port */
3688cae5a29dSMark Lord 	writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3689bdd4dddeSJeff Garzik 
3690bdd4dddeSJeff Garzik 	/* clear pending irq events */
3691cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
3692cae5a29dSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
3693bdd4dddeSJeff Garzik 
369488e675e1SMark Lord 	mv_enable_port_irqs(ap, ERR_IRQ);
3695c6fd2807SJeff Garzik }
3696c6fd2807SJeff Garzik 
3697c6fd2807SJeff Garzik /**
3698c6fd2807SJeff Garzik  *      mv_port_init - Perform some early initialization on a single port.
3699c6fd2807SJeff Garzik  *      @port: libata data structure storing shadow register addresses
3700c6fd2807SJeff Garzik  *      @port_mmio: base address of the port
3701c6fd2807SJeff Garzik  *
3702c6fd2807SJeff Garzik  *      Initialize shadow register mmio addresses, clear outstanding
3703c6fd2807SJeff Garzik  *      interrupts on the port, and unmask interrupts for the future
3704c6fd2807SJeff Garzik  *      start of the port.
3705c6fd2807SJeff Garzik  *
3706c6fd2807SJeff Garzik  *      LOCKING:
3707c6fd2807SJeff Garzik  *      Inherited from caller.
3708c6fd2807SJeff Garzik  */
3709c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
3710c6fd2807SJeff Garzik {
3711cae5a29dSMark Lord 	void __iomem *serr, *shd_base = port_mmio + SHD_BLK;
3712c6fd2807SJeff Garzik 
3713c6fd2807SJeff Garzik 	/* PIO related setup
3714c6fd2807SJeff Garzik 	 */
3715c6fd2807SJeff Garzik 	port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
3716c6fd2807SJeff Garzik 	port->error_addr =
3717c6fd2807SJeff Garzik 		port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
3718c6fd2807SJeff Garzik 	port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
3719c6fd2807SJeff Garzik 	port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
3720c6fd2807SJeff Garzik 	port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
3721c6fd2807SJeff Garzik 	port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
3722c6fd2807SJeff Garzik 	port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
3723c6fd2807SJeff Garzik 	port->status_addr =
3724c6fd2807SJeff Garzik 		port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
3725c6fd2807SJeff Garzik 	/* special case: control/altstatus doesn't have ATA_REG_ address */
3726cae5a29dSMark Lord 	port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST;
3727c6fd2807SJeff Garzik 
3728c6fd2807SJeff Garzik 	/* Clear any currently outstanding port interrupt conditions */
3729cae5a29dSMark Lord 	serr = port_mmio + mv_scr_offset(SCR_ERROR);
3730cae5a29dSMark Lord 	writelfl(readl(serr), serr);
3731cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3732c6fd2807SJeff Garzik 
3733646a4da5SMark Lord 	/* unmask all non-transient EDMA error interrupts */
3734cae5a29dSMark Lord 	writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK);
3735c6fd2807SJeff Garzik 
3736c6fd2807SJeff Garzik 	VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
3737cae5a29dSMark Lord 		readl(port_mmio + EDMA_CFG),
3738cae5a29dSMark Lord 		readl(port_mmio + EDMA_ERR_IRQ_CAUSE),
3739cae5a29dSMark Lord 		readl(port_mmio + EDMA_ERR_IRQ_MASK));
3740c6fd2807SJeff Garzik }
3741c6fd2807SJeff Garzik 
3742616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host)
3743616d4a98SMark Lord {
3744616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3745616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
3746616d4a98SMark Lord 	u32 reg;
3747616d4a98SMark Lord 
37481f398472SMark Lord 	if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
3749616d4a98SMark Lord 		return 0;	/* not PCI-X capable */
3750cae5a29dSMark Lord 	reg = readl(mmio + MV_PCI_MODE);
3751616d4a98SMark Lord 	if ((reg & MV_PCI_MODE_MASK) == 0)
3752616d4a98SMark Lord 		return 0;	/* conventional PCI mode */
3753616d4a98SMark Lord 	return 1;	/* chip is in PCI-X mode */
3754616d4a98SMark Lord }
3755616d4a98SMark Lord 
3756616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host)
3757616d4a98SMark Lord {
3758616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3759616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
3760616d4a98SMark Lord 	u32 reg;
3761616d4a98SMark Lord 
3762616d4a98SMark Lord 	if (!mv_in_pcix_mode(host)) {
3763cae5a29dSMark Lord 		reg = readl(mmio + MV_PCI_COMMAND);
3764cae5a29dSMark Lord 		if (reg & MV_PCI_COMMAND_MRDTRIG)
3765616d4a98SMark Lord 			return 0; /* not okay */
3766616d4a98SMark Lord 	}
3767616d4a98SMark Lord 	return 1; /* okay */
3768616d4a98SMark Lord }
3769616d4a98SMark Lord 
377065ad7fefSMark Lord static void mv_60x1b2_errata_pci7(struct ata_host *host)
377165ad7fefSMark Lord {
377265ad7fefSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
377365ad7fefSMark Lord 	void __iomem *mmio = hpriv->base;
377465ad7fefSMark Lord 
377565ad7fefSMark Lord 	/* workaround for 60x1-B2 errata PCI#7 */
377665ad7fefSMark Lord 	if (mv_in_pcix_mode(host)) {
3777cae5a29dSMark Lord 		u32 reg = readl(mmio + MV_PCI_COMMAND);
3778cae5a29dSMark Lord 		writelfl(reg & ~MV_PCI_COMMAND_MWRCOM, mmio + MV_PCI_COMMAND);
377965ad7fefSMark Lord 	}
378065ad7fefSMark Lord }
378165ad7fefSMark Lord 
37824447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
3783c6fd2807SJeff Garzik {
37844447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
37854447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3786c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
3787c6fd2807SJeff Garzik 
3788c6fd2807SJeff Garzik 	switch (board_idx) {
3789c6fd2807SJeff Garzik 	case chip_5080:
3790c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
3791ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
3792c6fd2807SJeff Garzik 
379344c10138SAuke Kok 		switch (pdev->revision) {
3794c6fd2807SJeff Garzik 		case 0x1:
3795c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
3796c6fd2807SJeff Garzik 			break;
3797c6fd2807SJeff Garzik 		case 0x3:
3798c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3799c6fd2807SJeff Garzik 			break;
3800c6fd2807SJeff Garzik 		default:
3801a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3802c6fd2807SJeff Garzik 				 "Applying 50XXB2 workarounds to unknown rev\n");
3803c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3804c6fd2807SJeff Garzik 			break;
3805c6fd2807SJeff Garzik 		}
3806c6fd2807SJeff Garzik 		break;
3807c6fd2807SJeff Garzik 
3808c6fd2807SJeff Garzik 	case chip_504x:
3809c6fd2807SJeff Garzik 	case chip_508x:
3810c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
3811ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
3812c6fd2807SJeff Garzik 
381344c10138SAuke Kok 		switch (pdev->revision) {
3814c6fd2807SJeff Garzik 		case 0x0:
3815c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
3816c6fd2807SJeff Garzik 			break;
3817c6fd2807SJeff Garzik 		case 0x3:
3818c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3819c6fd2807SJeff Garzik 			break;
3820c6fd2807SJeff Garzik 		default:
3821a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3822c6fd2807SJeff Garzik 				 "Applying B2 workarounds to unknown rev\n");
3823c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3824c6fd2807SJeff Garzik 			break;
3825c6fd2807SJeff Garzik 		}
3826c6fd2807SJeff Garzik 		break;
3827c6fd2807SJeff Garzik 
3828c6fd2807SJeff Garzik 	case chip_604x:
3829c6fd2807SJeff Garzik 	case chip_608x:
3830c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
3831ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_II;
3832c6fd2807SJeff Garzik 
383344c10138SAuke Kok 		switch (pdev->revision) {
3834c6fd2807SJeff Garzik 		case 0x7:
383565ad7fefSMark Lord 			mv_60x1b2_errata_pci7(host);
3836c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
3837c6fd2807SJeff Garzik 			break;
3838c6fd2807SJeff Garzik 		case 0x9:
3839c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3840c6fd2807SJeff Garzik 			break;
3841c6fd2807SJeff Garzik 		default:
3842a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3843c6fd2807SJeff Garzik 				 "Applying B2 workarounds to unknown rev\n");
3844c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
3845c6fd2807SJeff Garzik 			break;
3846c6fd2807SJeff Garzik 		}
3847c6fd2807SJeff Garzik 		break;
3848c6fd2807SJeff Garzik 
3849c6fd2807SJeff Garzik 	case chip_7042:
3850616d4a98SMark Lord 		hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
3851306b30f7SMark Lord 		if (pdev->vendor == PCI_VENDOR_ID_TTI &&
3852306b30f7SMark Lord 		    (pdev->device == 0x2300 || pdev->device == 0x2310))
3853306b30f7SMark Lord 		{
38544e520033SMark Lord 			/*
38554e520033SMark Lord 			 * Highpoint RocketRAID PCIe 23xx series cards:
38564e520033SMark Lord 			 *
38574e520033SMark Lord 			 * Unconfigured drives are treated as "Legacy"
38584e520033SMark Lord 			 * by the BIOS, and it overwrites sector 8 with
38594e520033SMark Lord 			 * a "Lgcy" metadata block prior to Linux boot.
38604e520033SMark Lord 			 *
38614e520033SMark Lord 			 * Configured drives (RAID or JBOD) leave sector 8
38624e520033SMark Lord 			 * alone, but instead overwrite a high numbered
38634e520033SMark Lord 			 * sector for the RAID metadata.  This sector can
38644e520033SMark Lord 			 * be determined exactly, by truncating the physical
38654e520033SMark Lord 			 * drive capacity to a nice even GB value.
38664e520033SMark Lord 			 *
38674e520033SMark Lord 			 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
38684e520033SMark Lord 			 *
38694e520033SMark Lord 			 * Warn the user, lest they think we're just buggy.
38704e520033SMark Lord 			 */
38714e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
38724e520033SMark Lord 				" BIOS CORRUPTS DATA on all attached drives,"
38734e520033SMark Lord 				" regardless of if/how they are configured."
38744e520033SMark Lord 				" BEWARE!\n");
38754e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": For data safety, do not"
38764e520033SMark Lord 				" use sectors 8-9 on \"Legacy\" drives,"
38774e520033SMark Lord 				" and avoid the final two gigabytes on"
38784e520033SMark Lord 				" all RocketRAID BIOS initialized drives.\n");
3879306b30f7SMark Lord 		}
38808e7decdbSMark Lord 		/* drop through */
3881c6fd2807SJeff Garzik 	case chip_6042:
3882c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
3883c6fd2807SJeff Garzik 		hp_flags |= MV_HP_GEN_IIE;
3884616d4a98SMark Lord 		if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
3885616d4a98SMark Lord 			hp_flags |= MV_HP_CUT_THROUGH;
3886c6fd2807SJeff Garzik 
388744c10138SAuke Kok 		switch (pdev->revision) {
38885cf73bfbSMark Lord 		case 0x2: /* Rev.B0: the first/only public release */
3889c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3890c6fd2807SJeff Garzik 			break;
3891c6fd2807SJeff Garzik 		default:
3892a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3893c6fd2807SJeff Garzik 				 "Applying 60X1C0 workarounds to unknown rev\n");
3894c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3895c6fd2807SJeff Garzik 			break;
3896c6fd2807SJeff Garzik 		}
3897c6fd2807SJeff Garzik 		break;
3898f351b2d6SSaeed Bishara 	case chip_soc:
389929b7e43cSMartin Michlmayr 		if (soc_is_65n(hpriv))
390029b7e43cSMartin Michlmayr 			hpriv->ops = &mv_soc_65n_ops;
390129b7e43cSMartin Michlmayr 		else
3902f351b2d6SSaeed Bishara 			hpriv->ops = &mv_soc_ops;
3903eb3a55a9SSaeed Bishara 		hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3904eb3a55a9SSaeed Bishara 			MV_HP_ERRATA_60X1C0;
3905f351b2d6SSaeed Bishara 		break;
3906c6fd2807SJeff Garzik 
3907c6fd2807SJeff Garzik 	default:
3908a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: invalid board index %u\n", board_idx);
3909c6fd2807SJeff Garzik 		return 1;
3910c6fd2807SJeff Garzik 	}
3911c6fd2807SJeff Garzik 
3912c6fd2807SJeff Garzik 	hpriv->hp_flags = hp_flags;
391302a121daSMark Lord 	if (hp_flags & MV_HP_PCIE) {
3914cae5a29dSMark Lord 		hpriv->irq_cause_offset	= PCIE_IRQ_CAUSE;
3915cae5a29dSMark Lord 		hpriv->irq_mask_offset	= PCIE_IRQ_MASK;
391602a121daSMark Lord 		hpriv->unmask_all_irqs	= PCIE_UNMASK_ALL_IRQS;
391702a121daSMark Lord 	} else {
3918cae5a29dSMark Lord 		hpriv->irq_cause_offset	= PCI_IRQ_CAUSE;
3919cae5a29dSMark Lord 		hpriv->irq_mask_offset	= PCI_IRQ_MASK;
392002a121daSMark Lord 		hpriv->unmask_all_irqs	= PCI_UNMASK_ALL_IRQS;
392102a121daSMark Lord 	}
3922c6fd2807SJeff Garzik 
3923c6fd2807SJeff Garzik 	return 0;
3924c6fd2807SJeff Garzik }
3925c6fd2807SJeff Garzik 
3926c6fd2807SJeff Garzik /**
3927c6fd2807SJeff Garzik  *      mv_init_host - Perform some early initialization of the host.
39284447d351STejun Heo  *	@host: ATA host to initialize
3929c6fd2807SJeff Garzik  *
3930c6fd2807SJeff Garzik  *      If possible, do an early global reset of the host.  Then do
3931c6fd2807SJeff Garzik  *      our port init and clear/unmask all/relevant host interrupts.
3932c6fd2807SJeff Garzik  *
3933c6fd2807SJeff Garzik  *      LOCKING:
3934c6fd2807SJeff Garzik  *      Inherited from caller.
3935c6fd2807SJeff Garzik  */
39361bfeff03SSaeed Bishara static int mv_init_host(struct ata_host *host)
3937c6fd2807SJeff Garzik {
3938c6fd2807SJeff Garzik 	int rc = 0, n_hc, port, hc;
39394447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3940f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
3941c6fd2807SJeff Garzik 
39421bfeff03SSaeed Bishara 	rc = mv_chip_id(host, hpriv->board_idx);
3943c6fd2807SJeff Garzik 	if (rc)
3944c6fd2807SJeff Garzik 		goto done;
3945c6fd2807SJeff Garzik 
39461f398472SMark Lord 	if (IS_SOC(hpriv)) {
3947cae5a29dSMark Lord 		hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE;
3948cae5a29dSMark Lord 		hpriv->main_irq_mask_addr  = mmio + SOC_HC_MAIN_IRQ_MASK;
39491f398472SMark Lord 	} else {
3950cae5a29dSMark Lord 		hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE;
3951cae5a29dSMark Lord 		hpriv->main_irq_mask_addr  = mmio + PCI_HC_MAIN_IRQ_MASK;
3952f351b2d6SSaeed Bishara 	}
3953352fab70SMark Lord 
39545d0fb2e7SThomas Reitmayr 	/* initialize shadow irq mask with register's value */
39555d0fb2e7SThomas Reitmayr 	hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
39565d0fb2e7SThomas Reitmayr 
3957352fab70SMark Lord 	/* global interrupt mask: 0 == mask everything */
3958c4de573bSMark Lord 	mv_set_main_irq_mask(host, ~0, 0);
3959f351b2d6SSaeed Bishara 
39604447d351STejun Heo 	n_hc = mv_get_hc_count(host->ports[0]->flags);
3961c6fd2807SJeff Garzik 
39624447d351STejun Heo 	for (port = 0; port < host->n_ports; port++)
396329b7e43cSMartin Michlmayr 		if (hpriv->ops->read_preamp)
3964c6fd2807SJeff Garzik 			hpriv->ops->read_preamp(hpriv, port, mmio);
3965c6fd2807SJeff Garzik 
3966c6fd2807SJeff Garzik 	rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
3967c6fd2807SJeff Garzik 	if (rc)
3968c6fd2807SJeff Garzik 		goto done;
3969c6fd2807SJeff Garzik 
3970c6fd2807SJeff Garzik 	hpriv->ops->reset_flash(hpriv, mmio);
39717bb3c529SSaeed Bishara 	hpriv->ops->reset_bus(host, mmio);
3972c6fd2807SJeff Garzik 	hpriv->ops->enable_leds(hpriv, mmio);
3973c6fd2807SJeff Garzik 
39744447d351STejun Heo 	for (port = 0; port < host->n_ports; port++) {
3975cbcdd875STejun Heo 		struct ata_port *ap = host->ports[port];
3976c6fd2807SJeff Garzik 		void __iomem *port_mmio = mv_port_base(mmio, port);
3977cbcdd875STejun Heo 
3978cbcdd875STejun Heo 		mv_port_init(&ap->ioaddr, port_mmio);
3979c6fd2807SJeff Garzik 	}
3980c6fd2807SJeff Garzik 
3981c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3982c6fd2807SJeff Garzik 		void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3983c6fd2807SJeff Garzik 
3984c6fd2807SJeff Garzik 		VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
3985c6fd2807SJeff Garzik 			"(before clear)=0x%08x\n", hc,
3986cae5a29dSMark Lord 			readl(hc_mmio + HC_CFG),
3987cae5a29dSMark Lord 			readl(hc_mmio + HC_IRQ_CAUSE));
3988c6fd2807SJeff Garzik 
3989c6fd2807SJeff Garzik 		/* Clear any currently outstanding hc interrupt conditions */
3990cae5a29dSMark Lord 		writelfl(0, hc_mmio + HC_IRQ_CAUSE);
3991c6fd2807SJeff Garzik 	}
3992c6fd2807SJeff Garzik 
399344c65d16SMark Lord 	if (!IS_SOC(hpriv)) {
3994c6fd2807SJeff Garzik 		/* Clear any currently outstanding host interrupt conditions */
3995cae5a29dSMark Lord 		writelfl(0, mmio + hpriv->irq_cause_offset);
3996c6fd2807SJeff Garzik 
3997c6fd2807SJeff Garzik 		/* and unmask interrupt generation for host regs */
3998cae5a29dSMark Lord 		writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_offset);
399944c65d16SMark Lord 	}
4000c6fd2807SJeff Garzik 
400151de32d2SMark Lord 	/*
400251de32d2SMark Lord 	 * enable only global host interrupts for now.
400351de32d2SMark Lord 	 * The per-port interrupts get done later as ports are set up.
400451de32d2SMark Lord 	 */
4005c4de573bSMark Lord 	mv_set_main_irq_mask(host, 0, PCI_ERR);
40062b748a0aSMark Lord 	mv_set_irq_coalescing(host, irq_coalescing_io_count,
40072b748a0aSMark Lord 				    irq_coalescing_usecs);
4008c6fd2807SJeff Garzik done:
4009c6fd2807SJeff Garzik 	return rc;
4010c6fd2807SJeff Garzik }
4011c6fd2807SJeff Garzik 
4012fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
4013fbf14e2fSByron Bradley {
4014fbf14e2fSByron Bradley 	hpriv->crqb_pool   = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
4015fbf14e2fSByron Bradley 							     MV_CRQB_Q_SZ, 0);
4016fbf14e2fSByron Bradley 	if (!hpriv->crqb_pool)
4017fbf14e2fSByron Bradley 		return -ENOMEM;
4018fbf14e2fSByron Bradley 
4019fbf14e2fSByron Bradley 	hpriv->crpb_pool   = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
4020fbf14e2fSByron Bradley 							     MV_CRPB_Q_SZ, 0);
4021fbf14e2fSByron Bradley 	if (!hpriv->crpb_pool)
4022fbf14e2fSByron Bradley 		return -ENOMEM;
4023fbf14e2fSByron Bradley 
4024fbf14e2fSByron Bradley 	hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
4025fbf14e2fSByron Bradley 							     MV_SG_TBL_SZ, 0);
4026fbf14e2fSByron Bradley 	if (!hpriv->sg_tbl_pool)
4027fbf14e2fSByron Bradley 		return -ENOMEM;
4028fbf14e2fSByron Bradley 
4029fbf14e2fSByron Bradley 	return 0;
4030fbf14e2fSByron Bradley }
4031fbf14e2fSByron Bradley 
403215a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
403363a9332bSAndrew Lunn 				 const struct mbus_dram_target_info *dram)
403415a32632SLennert Buytenhek {
403515a32632SLennert Buytenhek 	int i;
403615a32632SLennert Buytenhek 
403715a32632SLennert Buytenhek 	for (i = 0; i < 4; i++) {
403815a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_CTRL(i));
403915a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_BASE(i));
404015a32632SLennert Buytenhek 	}
404115a32632SLennert Buytenhek 
404215a32632SLennert Buytenhek 	for (i = 0; i < dram->num_cs; i++) {
404363a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
404415a32632SLennert Buytenhek 
404515a32632SLennert Buytenhek 		writel(((cs->size - 1) & 0xffff0000) |
404615a32632SLennert Buytenhek 			(cs->mbus_attr << 8) |
404715a32632SLennert Buytenhek 			(dram->mbus_dram_target_id << 4) | 1,
404815a32632SLennert Buytenhek 			hpriv->base + WINDOW_CTRL(i));
404915a32632SLennert Buytenhek 		writel(cs->base, hpriv->base + WINDOW_BASE(i));
405015a32632SLennert Buytenhek 	}
405115a32632SLennert Buytenhek }
405215a32632SLennert Buytenhek 
4053f351b2d6SSaeed Bishara /**
4054f351b2d6SSaeed Bishara  *      mv_platform_probe - handle a positive probe of an soc Marvell
4055f351b2d6SSaeed Bishara  *      host
4056f351b2d6SSaeed Bishara  *      @pdev: platform device found
4057f351b2d6SSaeed Bishara  *
4058f351b2d6SSaeed Bishara  *      LOCKING:
4059f351b2d6SSaeed Bishara  *      Inherited from caller.
4060f351b2d6SSaeed Bishara  */
4061f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
4062f351b2d6SSaeed Bishara {
4063f351b2d6SSaeed Bishara 	const struct mv_sata_platform_data *mv_platform_data;
406463a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
4065f351b2d6SSaeed Bishara 	const struct ata_port_info *ppi[] =
4066f351b2d6SSaeed Bishara 	    { &mv_port_info[chip_soc], NULL };
4067f351b2d6SSaeed Bishara 	struct ata_host *host;
4068f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv;
4069f351b2d6SSaeed Bishara 	struct resource *res;
407097b414e1SAndrew Lunn 	int n_ports = 0, irq = 0;
407199b80e97SDan Carpenter 	int rc;
4072eee98990SAndrew Lunn 	int port;
4073f351b2d6SSaeed Bishara 
407406296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
4075f351b2d6SSaeed Bishara 
4076f351b2d6SSaeed Bishara 	/*
4077f351b2d6SSaeed Bishara 	 * Simple resource validation ..
4078f351b2d6SSaeed Bishara 	 */
4079f351b2d6SSaeed Bishara 	if (unlikely(pdev->num_resources != 2)) {
4080f351b2d6SSaeed Bishara 		dev_err(&pdev->dev, "invalid number of resources\n");
4081f351b2d6SSaeed Bishara 		return -EINVAL;
4082f351b2d6SSaeed Bishara 	}
4083f351b2d6SSaeed Bishara 
4084f351b2d6SSaeed Bishara 	/*
4085f351b2d6SSaeed Bishara 	 * Get the register base first
4086f351b2d6SSaeed Bishara 	 */
4087f351b2d6SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4088f351b2d6SSaeed Bishara 	if (res == NULL)
4089f351b2d6SSaeed Bishara 		return -EINVAL;
4090f351b2d6SSaeed Bishara 
4091f351b2d6SSaeed Bishara 	/* allocate host */
409297b414e1SAndrew Lunn 	if (pdev->dev.of_node) {
409397b414e1SAndrew Lunn 		of_property_read_u32(pdev->dev.of_node, "nr-ports", &n_ports);
409497b414e1SAndrew Lunn 		irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
409597b414e1SAndrew Lunn 	} else {
409661b8c345SJingoo Han 		mv_platform_data = dev_get_platdata(&pdev->dev);
4097f351b2d6SSaeed Bishara 		n_ports = mv_platform_data->n_ports;
409897b414e1SAndrew Lunn 		irq = platform_get_irq(pdev, 0);
409997b414e1SAndrew Lunn 	}
4100f351b2d6SSaeed Bishara 
4101f351b2d6SSaeed Bishara 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
4102f351b2d6SSaeed Bishara 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
4103f351b2d6SSaeed Bishara 
4104f351b2d6SSaeed Bishara 	if (!host || !hpriv)
4105f351b2d6SSaeed Bishara 		return -ENOMEM;
4106eee98990SAndrew Lunn 	hpriv->port_clks = devm_kzalloc(&pdev->dev,
4107eee98990SAndrew Lunn 					sizeof(struct clk *) * n_ports,
4108eee98990SAndrew Lunn 					GFP_KERNEL);
4109eee98990SAndrew Lunn 	if (!hpriv->port_clks)
4110eee98990SAndrew Lunn 		return -ENOMEM;
4111b7db4f2eSAndrew Lunn 	hpriv->port_phys = devm_kzalloc(&pdev->dev,
4112b7db4f2eSAndrew Lunn 					sizeof(struct phy *) * n_ports,
4113b7db4f2eSAndrew Lunn 					GFP_KERNEL);
4114b7db4f2eSAndrew Lunn 	if (!hpriv->port_phys)
4115b7db4f2eSAndrew Lunn 		return -ENOMEM;
4116f351b2d6SSaeed Bishara 	host->private_data = hpriv;
41171bfeff03SSaeed Bishara 	hpriv->board_idx = chip_soc;
4118f351b2d6SSaeed Bishara 
4119f351b2d6SSaeed Bishara 	host->iomap = NULL;
4120f1cb0ea1SSaeed Bishara 	hpriv->base = devm_ioremap(&pdev->dev, res->start,
4121041b5eacSJulia Lawall 				   resource_size(res));
4122cae5a29dSMark Lord 	hpriv->base -= SATAHC0_REG_BASE;
4123f351b2d6SSaeed Bishara 
4124c77a2f4eSSaeed Bishara 	hpriv->clk = clk_get(&pdev->dev, NULL);
4125c77a2f4eSSaeed Bishara 	if (IS_ERR(hpriv->clk))
4126eee98990SAndrew Lunn 		dev_notice(&pdev->dev, "cannot get optional clkdev\n");
4127c77a2f4eSSaeed Bishara 	else
4128eee98990SAndrew Lunn 		clk_prepare_enable(hpriv->clk);
4129eee98990SAndrew Lunn 
4130eee98990SAndrew Lunn 	for (port = 0; port < n_ports; port++) {
4131eee98990SAndrew Lunn 		char port_number[16];
4132eee98990SAndrew Lunn 		sprintf(port_number, "%d", port);
4133eee98990SAndrew Lunn 		hpriv->port_clks[port] = clk_get(&pdev->dev, port_number);
4134eee98990SAndrew Lunn 		if (!IS_ERR(hpriv->port_clks[port]))
4135eee98990SAndrew Lunn 			clk_prepare_enable(hpriv->port_clks[port]);
4136b7db4f2eSAndrew Lunn 
4137b7db4f2eSAndrew Lunn 		sprintf(port_number, "port%d", port);
413890aa2997SAndrew Lunn 		hpriv->port_phys[port] = devm_phy_optional_get(&pdev->dev,
413990aa2997SAndrew Lunn 							       port_number);
4140b7db4f2eSAndrew Lunn 		if (IS_ERR(hpriv->port_phys[port])) {
4141b7db4f2eSAndrew Lunn 			rc = PTR_ERR(hpriv->port_phys[port]);
4142b7db4f2eSAndrew Lunn 			hpriv->port_phys[port] = NULL;
414390aa2997SAndrew Lunn 			if (rc != -EPROBE_DEFER)
414454dfffdeSLinus Torvalds 				dev_warn(&pdev->dev, "error getting phy %d", rc);
41458ad116e6SEzequiel Garcia 
41468ad116e6SEzequiel Garcia 			/* Cleanup only the initialized ports */
41478ad116e6SEzequiel Garcia 			hpriv->n_ports = port;
4148b7db4f2eSAndrew Lunn 			goto err;
4149b7db4f2eSAndrew Lunn 		} else
4150b7db4f2eSAndrew Lunn 			phy_power_on(hpriv->port_phys[port]);
4151eee98990SAndrew Lunn 	}
4152c77a2f4eSSaeed Bishara 
41538ad116e6SEzequiel Garcia 	/* All the ports have been initialized */
41548ad116e6SEzequiel Garcia 	hpriv->n_ports = n_ports;
41558ad116e6SEzequiel Garcia 
415615a32632SLennert Buytenhek 	/*
415715a32632SLennert Buytenhek 	 * (Re-)program MBUS remapping windows if we are asked to.
415815a32632SLennert Buytenhek 	 */
415963a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
416063a9332bSAndrew Lunn 	if (dram)
416163a9332bSAndrew Lunn 		mv_conf_mbus_windows(hpriv, dram);
416215a32632SLennert Buytenhek 
4163fbf14e2fSByron Bradley 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
4164fbf14e2fSByron Bradley 	if (rc)
4165c77a2f4eSSaeed Bishara 		goto err;
4166fbf14e2fSByron Bradley 
41679013d64eSLior Amsalem 	/*
41689013d64eSLior Amsalem 	 * To allow disk hotplug on Armada 370/XP SoCs, the PHY speed must be
41699013d64eSLior Amsalem 	 * updated in the LP_PHY_CTL register.
41709013d64eSLior Amsalem 	 */
41719013d64eSLior Amsalem 	if (pdev->dev.of_node &&
41729013d64eSLior Amsalem 		of_device_is_compatible(pdev->dev.of_node,
41739013d64eSLior Amsalem 					"marvell,armada-370-sata"))
41749013d64eSLior Amsalem 		hpriv->hp_flags |= MV_HP_FIX_LP_PHY_CTL;
41759013d64eSLior Amsalem 
4176f351b2d6SSaeed Bishara 	/* initialize adapter */
41771bfeff03SSaeed Bishara 	rc = mv_init_host(host);
4178f351b2d6SSaeed Bishara 	if (rc)
4179c77a2f4eSSaeed Bishara 		goto err;
4180f351b2d6SSaeed Bishara 
4181a44fec1fSJoe Perches 	dev_info(&pdev->dev, "slots %u ports %d\n",
4182a44fec1fSJoe Perches 		 (unsigned)MV_MAX_Q_DEPTH, host->n_ports);
4183f351b2d6SSaeed Bishara 
418497b414e1SAndrew Lunn 	rc = ata_host_activate(host, irq, mv_interrupt, IRQF_SHARED, &mv6_sht);
4185c00a4c9dSSergei Shtylyov 	if (!rc)
4186c00a4c9dSSergei Shtylyov 		return 0;
4187c00a4c9dSSergei Shtylyov 
4188c77a2f4eSSaeed Bishara err:
4189c77a2f4eSSaeed Bishara 	if (!IS_ERR(hpriv->clk)) {
4190eee98990SAndrew Lunn 		clk_disable_unprepare(hpriv->clk);
4191c77a2f4eSSaeed Bishara 		clk_put(hpriv->clk);
4192c77a2f4eSSaeed Bishara 	}
41938ad116e6SEzequiel Garcia 	for (port = 0; port < hpriv->n_ports; port++) {
4194eee98990SAndrew Lunn 		if (!IS_ERR(hpriv->port_clks[port])) {
4195eee98990SAndrew Lunn 			clk_disable_unprepare(hpriv->port_clks[port]);
4196eee98990SAndrew Lunn 			clk_put(hpriv->port_clks[port]);
4197eee98990SAndrew Lunn 		}
4198b7db4f2eSAndrew Lunn 		phy_power_off(hpriv->port_phys[port]);
4199eee98990SAndrew Lunn 	}
4200c77a2f4eSSaeed Bishara 
4201c77a2f4eSSaeed Bishara 	return rc;
4202f351b2d6SSaeed Bishara }
4203f351b2d6SSaeed Bishara 
4204f351b2d6SSaeed Bishara /*
4205f351b2d6SSaeed Bishara  *
4206f351b2d6SSaeed Bishara  *      mv_platform_remove    -       unplug a platform interface
4207f351b2d6SSaeed Bishara  *      @pdev: platform device
4208f351b2d6SSaeed Bishara  *
4209f351b2d6SSaeed Bishara  *      A platform bus SATA device has been unplugged. Perform the needed
4210f351b2d6SSaeed Bishara  *      cleanup. Also called on module unload for any active devices.
4211f351b2d6SSaeed Bishara  */
42120ec24914SGreg Kroah-Hartman static int mv_platform_remove(struct platform_device *pdev)
4213f351b2d6SSaeed Bishara {
4214d8661921SSergei Shtylyov 	struct ata_host *host = platform_get_drvdata(pdev);
4215c77a2f4eSSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
4216eee98990SAndrew Lunn 	int port;
4217f351b2d6SSaeed Bishara 	ata_host_detach(host);
4218c77a2f4eSSaeed Bishara 
4219c77a2f4eSSaeed Bishara 	if (!IS_ERR(hpriv->clk)) {
4220eee98990SAndrew Lunn 		clk_disable_unprepare(hpriv->clk);
4221c77a2f4eSSaeed Bishara 		clk_put(hpriv->clk);
4222c77a2f4eSSaeed Bishara 	}
4223eee98990SAndrew Lunn 	for (port = 0; port < host->n_ports; port++) {
4224eee98990SAndrew Lunn 		if (!IS_ERR(hpriv->port_clks[port])) {
4225eee98990SAndrew Lunn 			clk_disable_unprepare(hpriv->port_clks[port]);
4226eee98990SAndrew Lunn 			clk_put(hpriv->port_clks[port]);
4227eee98990SAndrew Lunn 		}
4228b7db4f2eSAndrew Lunn 		phy_power_off(hpriv->port_phys[port]);
4229eee98990SAndrew Lunn 	}
4230f351b2d6SSaeed Bishara 	return 0;
4231f351b2d6SSaeed Bishara }
4232f351b2d6SSaeed Bishara 
423358eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
42346481f2b5SSaeed Bishara static int mv_platform_suspend(struct platform_device *pdev, pm_message_t state)
42356481f2b5SSaeed Bishara {
4236d8661921SSergei Shtylyov 	struct ata_host *host = platform_get_drvdata(pdev);
42376481f2b5SSaeed Bishara 	if (host)
42386481f2b5SSaeed Bishara 		return ata_host_suspend(host, state);
42396481f2b5SSaeed Bishara 	else
42406481f2b5SSaeed Bishara 		return 0;
42416481f2b5SSaeed Bishara }
42426481f2b5SSaeed Bishara 
42436481f2b5SSaeed Bishara static int mv_platform_resume(struct platform_device *pdev)
42446481f2b5SSaeed Bishara {
4245d8661921SSergei Shtylyov 	struct ata_host *host = platform_get_drvdata(pdev);
424663a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
42476481f2b5SSaeed Bishara 	int ret;
42486481f2b5SSaeed Bishara 
42496481f2b5SSaeed Bishara 	if (host) {
42506481f2b5SSaeed Bishara 		struct mv_host_priv *hpriv = host->private_data;
425163a9332bSAndrew Lunn 
42526481f2b5SSaeed Bishara 		/*
42536481f2b5SSaeed Bishara 		 * (Re-)program MBUS remapping windows if we are asked to.
42546481f2b5SSaeed Bishara 		 */
425563a9332bSAndrew Lunn 		dram = mv_mbus_dram_info();
425663a9332bSAndrew Lunn 		if (dram)
425763a9332bSAndrew Lunn 			mv_conf_mbus_windows(hpriv, dram);
42586481f2b5SSaeed Bishara 
42596481f2b5SSaeed Bishara 		/* initialize adapter */
42601bfeff03SSaeed Bishara 		ret = mv_init_host(host);
42616481f2b5SSaeed Bishara 		if (ret) {
42626481f2b5SSaeed Bishara 			printk(KERN_ERR DRV_NAME ": Error during HW init\n");
42636481f2b5SSaeed Bishara 			return ret;
42646481f2b5SSaeed Bishara 		}
42656481f2b5SSaeed Bishara 		ata_host_resume(host);
42666481f2b5SSaeed Bishara 	}
42676481f2b5SSaeed Bishara 
42686481f2b5SSaeed Bishara 	return 0;
42696481f2b5SSaeed Bishara }
42706481f2b5SSaeed Bishara #else
42716481f2b5SSaeed Bishara #define mv_platform_suspend NULL
42726481f2b5SSaeed Bishara #define mv_platform_resume NULL
42736481f2b5SSaeed Bishara #endif
42746481f2b5SSaeed Bishara 
427597b414e1SAndrew Lunn #ifdef CONFIG_OF
42760ec24914SGreg Kroah-Hartman static struct of_device_id mv_sata_dt_ids[] = {
4277b1f5c73bSSimon Guinot 	{ .compatible = "marvell,armada-370-sata", },
427897b414e1SAndrew Lunn 	{ .compatible = "marvell,orion-sata", },
427997b414e1SAndrew Lunn 	{},
428097b414e1SAndrew Lunn };
428197b414e1SAndrew Lunn MODULE_DEVICE_TABLE(of, mv_sata_dt_ids);
428297b414e1SAndrew Lunn #endif
428397b414e1SAndrew Lunn 
4284f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
4285f351b2d6SSaeed Bishara 	.probe		= mv_platform_probe,
42860ec24914SGreg Kroah-Hartman 	.remove		= mv_platform_remove,
42876481f2b5SSaeed Bishara 	.suspend	= mv_platform_suspend,
42886481f2b5SSaeed Bishara 	.resume		= mv_platform_resume,
4289f351b2d6SSaeed Bishara 	.driver		= {
4290f351b2d6SSaeed Bishara 		.name = DRV_NAME,
429197b414e1SAndrew Lunn 		.of_match_table = of_match_ptr(mv_sata_dt_ids),
4292f351b2d6SSaeed Bishara 	},
4293f351b2d6SSaeed Bishara };
4294f351b2d6SSaeed Bishara 
4295f351b2d6SSaeed Bishara 
42967bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4297f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4298f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent);
429958eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
4300b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev);
4301b2dec48cSSaeed Bishara #endif
4302f351b2d6SSaeed Bishara 
43037bb3c529SSaeed Bishara 
43047bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
43057bb3c529SSaeed Bishara 	.name			= DRV_NAME,
43067bb3c529SSaeed Bishara 	.id_table		= mv_pci_tbl,
4307f351b2d6SSaeed Bishara 	.probe			= mv_pci_init_one,
43087bb3c529SSaeed Bishara 	.remove			= ata_pci_remove_one,
430958eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
4310b2dec48cSSaeed Bishara 	.suspend		= ata_pci_device_suspend,
4311b2dec48cSSaeed Bishara 	.resume			= mv_pci_device_resume,
4312b2dec48cSSaeed Bishara #endif
4313b2dec48cSSaeed Bishara 
43147bb3c529SSaeed Bishara };
43157bb3c529SSaeed Bishara 
43167bb3c529SSaeed Bishara /* move to PCI layer or libata core? */
43177bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev)
43187bb3c529SSaeed Bishara {
43197bb3c529SSaeed Bishara 	int rc;
43207bb3c529SSaeed Bishara 
4321c54c719bSQuentin Lambert 	if (!dma_set_mask(&pdev->dev, DMA_BIT_MASK(64))) {
4322c54c719bSQuentin Lambert 		rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(64));
43237bb3c529SSaeed Bishara 		if (rc) {
4324c54c719bSQuentin Lambert 			rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
43257bb3c529SSaeed Bishara 			if (rc) {
4326a44fec1fSJoe Perches 				dev_err(&pdev->dev,
43277bb3c529SSaeed Bishara 					"64-bit DMA enable failed\n");
43287bb3c529SSaeed Bishara 				return rc;
43297bb3c529SSaeed Bishara 			}
43307bb3c529SSaeed Bishara 		}
43317bb3c529SSaeed Bishara 	} else {
4332c54c719bSQuentin Lambert 		rc = dma_set_mask(&pdev->dev, DMA_BIT_MASK(32));
43337bb3c529SSaeed Bishara 		if (rc) {
4334a44fec1fSJoe Perches 			dev_err(&pdev->dev, "32-bit DMA enable failed\n");
43357bb3c529SSaeed Bishara 			return rc;
43367bb3c529SSaeed Bishara 		}
4337c54c719bSQuentin Lambert 		rc = dma_set_coherent_mask(&pdev->dev, DMA_BIT_MASK(32));
43387bb3c529SSaeed Bishara 		if (rc) {
4339a44fec1fSJoe Perches 			dev_err(&pdev->dev,
43407bb3c529SSaeed Bishara 				"32-bit consistent DMA enable failed\n");
43417bb3c529SSaeed Bishara 			return rc;
43427bb3c529SSaeed Bishara 		}
43437bb3c529SSaeed Bishara 	}
43447bb3c529SSaeed Bishara 
43457bb3c529SSaeed Bishara 	return rc;
43467bb3c529SSaeed Bishara }
43477bb3c529SSaeed Bishara 
4348c6fd2807SJeff Garzik /**
4349c6fd2807SJeff Garzik  *      mv_print_info - Dump key info to kernel log for perusal.
43504447d351STejun Heo  *      @host: ATA host to print info about
4351c6fd2807SJeff Garzik  *
4352c6fd2807SJeff Garzik  *      FIXME: complete this.
4353c6fd2807SJeff Garzik  *
4354c6fd2807SJeff Garzik  *      LOCKING:
4355c6fd2807SJeff Garzik  *      Inherited from caller.
4356c6fd2807SJeff Garzik  */
43574447d351STejun Heo static void mv_print_info(struct ata_host *host)
4358c6fd2807SJeff Garzik {
43594447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
43604447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
436144c10138SAuke Kok 	u8 scc;
4362c1e4fe71SJeff Garzik 	const char *scc_s, *gen;
4363c6fd2807SJeff Garzik 
4364c6fd2807SJeff Garzik 	/* Use this to determine the HW stepping of the chip so we know
4365c6fd2807SJeff Garzik 	 * what errata to workaround
4366c6fd2807SJeff Garzik 	 */
4367c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
4368c6fd2807SJeff Garzik 	if (scc == 0)
4369c6fd2807SJeff Garzik 		scc_s = "SCSI";
4370c6fd2807SJeff Garzik 	else if (scc == 0x01)
4371c6fd2807SJeff Garzik 		scc_s = "RAID";
4372c6fd2807SJeff Garzik 	else
4373c1e4fe71SJeff Garzik 		scc_s = "?";
4374c1e4fe71SJeff Garzik 
4375c1e4fe71SJeff Garzik 	if (IS_GEN_I(hpriv))
4376c1e4fe71SJeff Garzik 		gen = "I";
4377c1e4fe71SJeff Garzik 	else if (IS_GEN_II(hpriv))
4378c1e4fe71SJeff Garzik 		gen = "II";
4379c1e4fe71SJeff Garzik 	else if (IS_GEN_IIE(hpriv))
4380c1e4fe71SJeff Garzik 		gen = "IIE";
4381c1e4fe71SJeff Garzik 	else
4382c1e4fe71SJeff Garzik 		gen = "?";
4383c6fd2807SJeff Garzik 
4384a44fec1fSJoe Perches 	dev_info(&pdev->dev, "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
4385c1e4fe71SJeff Garzik 		 gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
4386c6fd2807SJeff Garzik 		 scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
4387c6fd2807SJeff Garzik }
4388c6fd2807SJeff Garzik 
4389c6fd2807SJeff Garzik /**
4390f351b2d6SSaeed Bishara  *      mv_pci_init_one - handle a positive probe of a PCI Marvell host
4391c6fd2807SJeff Garzik  *      @pdev: PCI device found
4392c6fd2807SJeff Garzik  *      @ent: PCI device ID entry for the matched host
4393c6fd2807SJeff Garzik  *
4394c6fd2807SJeff Garzik  *      LOCKING:
4395c6fd2807SJeff Garzik  *      Inherited from caller.
4396c6fd2807SJeff Garzik  */
4397f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4398f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent)
4399c6fd2807SJeff Garzik {
4400c6fd2807SJeff Garzik 	unsigned int board_idx = (unsigned int)ent->driver_data;
44014447d351STejun Heo 	const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
44024447d351STejun Heo 	struct ata_host *host;
44034447d351STejun Heo 	struct mv_host_priv *hpriv;
4404c4bc7d73SSaeed Bishara 	int n_ports, port, rc;
4405c6fd2807SJeff Garzik 
440606296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
4407c6fd2807SJeff Garzik 
44084447d351STejun Heo 	/* allocate host */
44094447d351STejun Heo 	n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
44104447d351STejun Heo 
44114447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
44124447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
44134447d351STejun Heo 	if (!host || !hpriv)
44144447d351STejun Heo 		return -ENOMEM;
44154447d351STejun Heo 	host->private_data = hpriv;
4416f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
44171bfeff03SSaeed Bishara 	hpriv->board_idx = board_idx;
44184447d351STejun Heo 
44194447d351STejun Heo 	/* acquire resources */
442024dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
442124dc5f33STejun Heo 	if (rc)
4422c6fd2807SJeff Garzik 		return rc;
4423c6fd2807SJeff Garzik 
44240d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
44250d5ff566STejun Heo 	if (rc == -EBUSY)
442624dc5f33STejun Heo 		pcim_pin_device(pdev);
44270d5ff566STejun Heo 	if (rc)
442824dc5f33STejun Heo 		return rc;
44294447d351STejun Heo 	host->iomap = pcim_iomap_table(pdev);
4430f351b2d6SSaeed Bishara 	hpriv->base = host->iomap[MV_PRIMARY_BAR];
4431c6fd2807SJeff Garzik 
4432d88184fbSJeff Garzik 	rc = pci_go_64(pdev);
4433d88184fbSJeff Garzik 	if (rc)
4434d88184fbSJeff Garzik 		return rc;
4435d88184fbSJeff Garzik 
4436da2fa9baSMark Lord 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
4437da2fa9baSMark Lord 	if (rc)
4438da2fa9baSMark Lord 		return rc;
4439da2fa9baSMark Lord 
4440c4bc7d73SSaeed Bishara 	for (port = 0; port < host->n_ports; port++) {
4441c4bc7d73SSaeed Bishara 		struct ata_port *ap = host->ports[port];
4442c4bc7d73SSaeed Bishara 		void __iomem *port_mmio = mv_port_base(hpriv->base, port);
4443c4bc7d73SSaeed Bishara 		unsigned int offset = port_mmio - hpriv->base;
4444c4bc7d73SSaeed Bishara 
4445c4bc7d73SSaeed Bishara 		ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
4446c4bc7d73SSaeed Bishara 		ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
4447c4bc7d73SSaeed Bishara 	}
4448c4bc7d73SSaeed Bishara 
4449c6fd2807SJeff Garzik 	/* initialize adapter */
44501bfeff03SSaeed Bishara 	rc = mv_init_host(host);
445124dc5f33STejun Heo 	if (rc)
445224dc5f33STejun Heo 		return rc;
4453c6fd2807SJeff Garzik 
44546d3c30efSMark Lord 	/* Enable message-switched interrupts, if requested */
44556d3c30efSMark Lord 	if (msi && pci_enable_msi(pdev) == 0)
44566d3c30efSMark Lord 		hpriv->hp_flags |= MV_HP_FLAG_MSI;
4457c6fd2807SJeff Garzik 
4458c6fd2807SJeff Garzik 	mv_dump_pci_cfg(pdev, 0x68);
44594447d351STejun Heo 	mv_print_info(host);
4460c6fd2807SJeff Garzik 
44614447d351STejun Heo 	pci_set_master(pdev);
4462ea8b4db9SJeff Garzik 	pci_try_set_mwi(pdev);
44634447d351STejun Heo 	return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
4464c5d3e45aSJeff Garzik 				 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
4465c6fd2807SJeff Garzik }
4466b2dec48cSSaeed Bishara 
446758eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
4468b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev)
4469b2dec48cSSaeed Bishara {
4470d8661921SSergei Shtylyov 	struct ata_host *host = pci_get_drvdata(pdev);
4471b2dec48cSSaeed Bishara 	int rc;
4472b2dec48cSSaeed Bishara 
4473b2dec48cSSaeed Bishara 	rc = ata_pci_device_do_resume(pdev);
4474b2dec48cSSaeed Bishara 	if (rc)
4475b2dec48cSSaeed Bishara 		return rc;
4476b2dec48cSSaeed Bishara 
4477b2dec48cSSaeed Bishara 	/* initialize adapter */
4478b2dec48cSSaeed Bishara 	rc = mv_init_host(host);
4479b2dec48cSSaeed Bishara 	if (rc)
4480b2dec48cSSaeed Bishara 		return rc;
4481b2dec48cSSaeed Bishara 
4482b2dec48cSSaeed Bishara 	ata_host_resume(host);
4483b2dec48cSSaeed Bishara 
4484b2dec48cSSaeed Bishara 	return 0;
4485b2dec48cSSaeed Bishara }
4486b2dec48cSSaeed Bishara #endif
44877bb3c529SSaeed Bishara #endif
4488c6fd2807SJeff Garzik 
4489c6fd2807SJeff Garzik static int __init mv_init(void)
4490c6fd2807SJeff Garzik {
44917bb3c529SSaeed Bishara 	int rc = -ENODEV;
44927bb3c529SSaeed Bishara #ifdef CONFIG_PCI
44937bb3c529SSaeed Bishara 	rc = pci_register_driver(&mv_pci_driver);
4494f351b2d6SSaeed Bishara 	if (rc < 0)
4495f351b2d6SSaeed Bishara 		return rc;
4496f351b2d6SSaeed Bishara #endif
4497f351b2d6SSaeed Bishara 	rc = platform_driver_register(&mv_platform_driver);
4498f351b2d6SSaeed Bishara 
4499f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
4500f351b2d6SSaeed Bishara 	if (rc < 0)
4501f351b2d6SSaeed Bishara 		pci_unregister_driver(&mv_pci_driver);
45027bb3c529SSaeed Bishara #endif
45037bb3c529SSaeed Bishara 	return rc;
4504c6fd2807SJeff Garzik }
4505c6fd2807SJeff Garzik 
4506c6fd2807SJeff Garzik static void __exit mv_exit(void)
4507c6fd2807SJeff Garzik {
45087bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4509c6fd2807SJeff Garzik 	pci_unregister_driver(&mv_pci_driver);
45107bb3c529SSaeed Bishara #endif
4511f351b2d6SSaeed Bishara 	platform_driver_unregister(&mv_platform_driver);
4512c6fd2807SJeff Garzik }
4513c6fd2807SJeff Garzik 
4514c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
4515c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
4516c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
4517c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
4518c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
451917c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
4520c6fd2807SJeff Garzik 
4521c6fd2807SJeff Garzik module_init(mv_init);
4522c6fd2807SJeff Garzik module_exit(mv_exit);
4523