xref: /openbmc/linux/drivers/ata/sata_mv.c (revision 8930ff254a3a80d4477c3391ade07d6dd2a036c7)
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>
63f351b2d6SSaeed Bishara #include <linux/platform_device.h>
64f351b2d6SSaeed Bishara #include <linux/ata_platform.h>
6515a32632SLennert Buytenhek #include <linux/mbus.h>
66c46938ccSMark Lord #include <linux/bitops.h>
675a0e3ad6STejun Heo #include <linux/gfp.h>
68c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
69c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
706c08772eSJeff Garzik #include <scsi/scsi_device.h>
71c6fd2807SJeff Garzik #include <linux/libata.h>
72c6fd2807SJeff Garzik 
73c6fd2807SJeff Garzik #define DRV_NAME	"sata_mv"
74cae5a29dSMark Lord #define DRV_VERSION	"1.28"
75c6fd2807SJeff Garzik 
7640f21b11SMark Lord /*
7740f21b11SMark Lord  * module options
7840f21b11SMark Lord  */
7940f21b11SMark Lord 
8040f21b11SMark Lord static int msi;
8140f21b11SMark Lord #ifdef CONFIG_PCI
8240f21b11SMark Lord module_param(msi, int, S_IRUGO);
8340f21b11SMark Lord MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
8440f21b11SMark Lord #endif
8540f21b11SMark Lord 
862b748a0aSMark Lord static int irq_coalescing_io_count;
872b748a0aSMark Lord module_param(irq_coalescing_io_count, int, S_IRUGO);
882b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_io_count,
892b748a0aSMark Lord 		 "IRQ coalescing I/O count threshold (0..255)");
902b748a0aSMark Lord 
912b748a0aSMark Lord static int irq_coalescing_usecs;
922b748a0aSMark Lord module_param(irq_coalescing_usecs, int, S_IRUGO);
932b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_usecs,
942b748a0aSMark Lord 		 "IRQ coalescing time threshold in usecs");
952b748a0aSMark Lord 
96c6fd2807SJeff Garzik enum {
97c6fd2807SJeff Garzik 	/* BAR's are enumerated in terms of pci_resource_start() terms */
98c6fd2807SJeff Garzik 	MV_PRIMARY_BAR		= 0,	/* offset 0x10: memory space */
99c6fd2807SJeff Garzik 	MV_IO_BAR		= 2,	/* offset 0x18: IO space */
100c6fd2807SJeff Garzik 	MV_MISC_BAR		= 3,	/* offset 0x1c: FLASH, NVRAM, SRAM */
101c6fd2807SJeff Garzik 
102c6fd2807SJeff Garzik 	MV_MAJOR_REG_AREA_SZ	= 0x10000,	/* 64KB */
103c6fd2807SJeff Garzik 	MV_MINOR_REG_AREA_SZ	= 0x2000,	/* 8KB */
104c6fd2807SJeff Garzik 
1052b748a0aSMark Lord 	/* For use with both IRQ coalescing methods ("all ports" or "per-HC" */
1062b748a0aSMark Lord 	COAL_CLOCKS_PER_USEC	= 150,		/* for calculating COAL_TIMEs */
1072b748a0aSMark Lord 	MAX_COAL_TIME_THRESHOLD	= ((1 << 24) - 1), /* internal clocks count */
1082b748a0aSMark Lord 	MAX_COAL_IO_COUNT	= 255,		/* completed I/O count */
1092b748a0aSMark Lord 
110c6fd2807SJeff Garzik 	MV_PCI_REG_BASE		= 0,
111c6fd2807SJeff Garzik 
1122b748a0aSMark Lord 	/*
1132b748a0aSMark Lord 	 * Per-chip ("all ports") interrupt coalescing feature.
1142b748a0aSMark Lord 	 * This is only for GEN_II / GEN_IIE hardware.
1152b748a0aSMark Lord 	 *
1162b748a0aSMark Lord 	 * Coalescing defers the interrupt until either the IO_THRESHOLD
1172b748a0aSMark Lord 	 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
1182b748a0aSMark Lord 	 */
119cae5a29dSMark Lord 	COAL_REG_BASE		= 0x18000,
120cae5a29dSMark Lord 	IRQ_COAL_CAUSE		= (COAL_REG_BASE + 0x08),
1212b748a0aSMark Lord 	ALL_PORTS_COAL_IRQ	= (1 << 4),	/* all ports irq event */
1222b748a0aSMark Lord 
123cae5a29dSMark Lord 	IRQ_COAL_IO_THRESHOLD   = (COAL_REG_BASE + 0xcc),
124cae5a29dSMark Lord 	IRQ_COAL_TIME_THRESHOLD = (COAL_REG_BASE + 0xd0),
1252b748a0aSMark Lord 
1262b748a0aSMark Lord 	/*
1272b748a0aSMark Lord 	 * Registers for the (unused here) transaction coalescing feature:
1282b748a0aSMark Lord 	 */
129cae5a29dSMark Lord 	TRAN_COAL_CAUSE_LO	= (COAL_REG_BASE + 0x88),
130cae5a29dSMark Lord 	TRAN_COAL_CAUSE_HI	= (COAL_REG_BASE + 0x8c),
1312b748a0aSMark Lord 
132cae5a29dSMark Lord 	SATAHC0_REG_BASE	= 0x20000,
133cae5a29dSMark Lord 	FLASH_CTL		= 0x1046c,
134cae5a29dSMark Lord 	GPIO_PORT_CTL		= 0x104f0,
135cae5a29dSMark Lord 	RESET_CFG		= 0x180d8,
136c6fd2807SJeff Garzik 
137c6fd2807SJeff Garzik 	MV_PCI_REG_SZ		= MV_MAJOR_REG_AREA_SZ,
138c6fd2807SJeff Garzik 	MV_SATAHC_REG_SZ	= MV_MAJOR_REG_AREA_SZ,
139c6fd2807SJeff Garzik 	MV_SATAHC_ARBTR_REG_SZ	= MV_MINOR_REG_AREA_SZ,		/* arbiter */
140c6fd2807SJeff Garzik 	MV_PORT_REG_SZ		= MV_MINOR_REG_AREA_SZ,
141c6fd2807SJeff Garzik 
142c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH		= 32,
143c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH_MASK	= MV_MAX_Q_DEPTH - 1,
144c6fd2807SJeff Garzik 
145c6fd2807SJeff Garzik 	/* CRQB needs alignment on a 1KB boundary. Size == 1KB
146c6fd2807SJeff Garzik 	 * CRPB needs alignment on a 256B boundary. Size == 256B
147c6fd2807SJeff Garzik 	 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
148c6fd2807SJeff Garzik 	 */
149c6fd2807SJeff Garzik 	MV_CRQB_Q_SZ		= (32 * MV_MAX_Q_DEPTH),
150c6fd2807SJeff Garzik 	MV_CRPB_Q_SZ		= (8 * MV_MAX_Q_DEPTH),
151da2fa9baSMark Lord 	MV_MAX_SG_CT		= 256,
152c6fd2807SJeff Garzik 	MV_SG_TBL_SZ		= (16 * MV_MAX_SG_CT),
153c6fd2807SJeff Garzik 
154352fab70SMark Lord 	/* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
155c6fd2807SJeff Garzik 	MV_PORT_HC_SHIFT	= 2,
156352fab70SMark Lord 	MV_PORTS_PER_HC		= (1 << MV_PORT_HC_SHIFT), /* 4 */
157352fab70SMark Lord 	/* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
158352fab70SMark Lord 	MV_PORT_MASK		= (MV_PORTS_PER_HC - 1),   /* 3 */
159c6fd2807SJeff Garzik 
160c6fd2807SJeff Garzik 	/* Host Flags */
161c6fd2807SJeff Garzik 	MV_FLAG_DUAL_HC		= (1 << 30),  /* two SATA Host Controllers */
1627bb3c529SSaeed Bishara 
163c5d3e45aSJeff Garzik 	MV_COMMON_FLAGS		= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
16491b1a84cSMark Lord 				  ATA_FLAG_MMIO | ATA_FLAG_PIO_POLLING,
165ad3aef51SMark Lord 
16691b1a84cSMark Lord 	MV_GEN_I_FLAGS		= MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
167c6fd2807SJeff Garzik 
16840f21b11SMark Lord 	MV_GEN_II_FLAGS		= MV_COMMON_FLAGS | ATA_FLAG_NCQ |
16940f21b11SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
17091b1a84cSMark Lord 
17191b1a84cSMark Lord 	MV_GEN_IIE_FLAGS	= MV_GEN_II_FLAGS | ATA_FLAG_AN,
172ad3aef51SMark Lord 
173c6fd2807SJeff Garzik 	CRQB_FLAG_READ		= (1 << 0),
174c6fd2807SJeff Garzik 	CRQB_TAG_SHIFT		= 1,
175c5d3e45aSJeff Garzik 	CRQB_IOID_SHIFT		= 6,	/* CRQB Gen-II/IIE IO Id shift */
176e12bef50SMark Lord 	CRQB_PMP_SHIFT		= 12,	/* CRQB Gen-II/IIE PMP shift */
177c5d3e45aSJeff Garzik 	CRQB_HOSTQ_SHIFT	= 17,	/* CRQB Gen-II/IIE HostQueTag shift */
178c6fd2807SJeff Garzik 	CRQB_CMD_ADDR_SHIFT	= 8,
179c6fd2807SJeff Garzik 	CRQB_CMD_CS		= (0x2 << 11),
180c6fd2807SJeff Garzik 	CRQB_CMD_LAST		= (1 << 15),
181c6fd2807SJeff Garzik 
182c6fd2807SJeff Garzik 	CRPB_FLAG_STATUS_SHIFT	= 8,
183c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_6	= 5,	/* CRPB Gen-II IO Id shift */
184c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_7	= 7,	/* CRPB Gen-IIE IO Id shift */
185c6fd2807SJeff Garzik 
186c6fd2807SJeff Garzik 	EPRD_FLAG_END_OF_TBL	= (1 << 31),
187c6fd2807SJeff Garzik 
188c6fd2807SJeff Garzik 	/* PCI interface registers */
189c6fd2807SJeff Garzik 
190cae5a29dSMark Lord 	MV_PCI_COMMAND		= 0xc00,
191cae5a29dSMark Lord 	MV_PCI_COMMAND_MWRCOM	= (1 << 4),	/* PCI Master Write Combining */
192cae5a29dSMark Lord 	MV_PCI_COMMAND_MRDTRIG	= (1 << 7),	/* PCI Master Read Trigger */
193c6fd2807SJeff Garzik 
194cae5a29dSMark Lord 	PCI_MAIN_CMD_STS	= 0xd30,
195c6fd2807SJeff Garzik 	STOP_PCI_MASTER		= (1 << 2),
196c6fd2807SJeff Garzik 	PCI_MASTER_EMPTY	= (1 << 3),
197c6fd2807SJeff Garzik 	GLOB_SFT_RST		= (1 << 4),
198c6fd2807SJeff Garzik 
199cae5a29dSMark Lord 	MV_PCI_MODE		= 0xd00,
2008e7decdbSMark Lord 	MV_PCI_MODE_MASK	= 0x30,
2018e7decdbSMark Lord 
202c6fd2807SJeff Garzik 	MV_PCI_EXP_ROM_BAR_CTL	= 0xd2c,
203c6fd2807SJeff Garzik 	MV_PCI_DISC_TIMER	= 0xd04,
204c6fd2807SJeff Garzik 	MV_PCI_MSI_TRIGGER	= 0xc38,
205c6fd2807SJeff Garzik 	MV_PCI_SERR_MASK	= 0xc28,
206cae5a29dSMark Lord 	MV_PCI_XBAR_TMOUT	= 0x1d04,
207c6fd2807SJeff Garzik 	MV_PCI_ERR_LOW_ADDRESS	= 0x1d40,
208c6fd2807SJeff Garzik 	MV_PCI_ERR_HIGH_ADDRESS	= 0x1d44,
209c6fd2807SJeff Garzik 	MV_PCI_ERR_ATTRIBUTE	= 0x1d48,
210c6fd2807SJeff Garzik 	MV_PCI_ERR_COMMAND	= 0x1d50,
211c6fd2807SJeff Garzik 
212cae5a29dSMark Lord 	PCI_IRQ_CAUSE		= 0x1d58,
213cae5a29dSMark Lord 	PCI_IRQ_MASK		= 0x1d5c,
214c6fd2807SJeff Garzik 	PCI_UNMASK_ALL_IRQS	= 0x7fffff,	/* bits 22-0 */
215c6fd2807SJeff Garzik 
216cae5a29dSMark Lord 	PCIE_IRQ_CAUSE		= 0x1900,
217cae5a29dSMark Lord 	PCIE_IRQ_MASK		= 0x1910,
218646a4da5SMark Lord 	PCIE_UNMASK_ALL_IRQS	= 0x40a,	/* assorted bits */
21902a121daSMark Lord 
2207368f919SMark Lord 	/* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
221cae5a29dSMark Lord 	PCI_HC_MAIN_IRQ_CAUSE	= 0x1d60,
222cae5a29dSMark Lord 	PCI_HC_MAIN_IRQ_MASK	= 0x1d64,
223cae5a29dSMark Lord 	SOC_HC_MAIN_IRQ_CAUSE	= 0x20020,
224cae5a29dSMark Lord 	SOC_HC_MAIN_IRQ_MASK	= 0x20024,
22540f21b11SMark Lord 	ERR_IRQ			= (1 << 0),	/* shift by (2 * port #) */
22640f21b11SMark Lord 	DONE_IRQ		= (1 << 1),	/* shift by (2 * port #) */
227c6fd2807SJeff Garzik 	HC0_IRQ_PEND		= 0x1ff,	/* bits 0-8 = HC0's ports */
228c6fd2807SJeff Garzik 	HC_SHIFT		= 9,		/* bits 9-17 = HC1's ports */
2292b748a0aSMark Lord 	DONE_IRQ_0_3		= 0x000000aa,	/* DONE_IRQ ports 0,1,2,3 */
2302b748a0aSMark Lord 	DONE_IRQ_4_7		= (DONE_IRQ_0_3 << HC_SHIFT),  /* 4,5,6,7 */
231c6fd2807SJeff Garzik 	PCI_ERR			= (1 << 18),
23240f21b11SMark Lord 	TRAN_COAL_LO_DONE	= (1 << 19),	/* transaction coalescing */
23340f21b11SMark Lord 	TRAN_COAL_HI_DONE	= (1 << 20),	/* transaction coalescing */
23440f21b11SMark Lord 	PORTS_0_3_COAL_DONE	= (1 << 8),	/* HC0 IRQ coalescing */
23540f21b11SMark Lord 	PORTS_4_7_COAL_DONE	= (1 << 17),	/* HC1 IRQ coalescing */
23640f21b11SMark Lord 	ALL_PORTS_COAL_DONE	= (1 << 21),	/* GEN_II(E) IRQ coalescing */
237c6fd2807SJeff Garzik 	GPIO_INT		= (1 << 22),
238c6fd2807SJeff Garzik 	SELF_INT		= (1 << 23),
239c6fd2807SJeff Garzik 	TWSI_INT		= (1 << 24),
240c6fd2807SJeff Garzik 	HC_MAIN_RSVD		= (0x7f << 25),	/* bits 31-25 */
241fb621e2fSJeff Garzik 	HC_MAIN_RSVD_5		= (0x1fff << 19), /* bits 31-19 */
242f351b2d6SSaeed Bishara 	HC_MAIN_RSVD_SOC	= (0x3fffffb << 6),     /* bits 31-9, 7-6 */
243c6fd2807SJeff Garzik 
244c6fd2807SJeff Garzik 	/* SATAHC registers */
245cae5a29dSMark Lord 	HC_CFG			= 0x00,
246c6fd2807SJeff Garzik 
247cae5a29dSMark Lord 	HC_IRQ_CAUSE		= 0x14,
248352fab70SMark Lord 	DMA_IRQ			= (1 << 0),	/* shift by port # */
249352fab70SMark Lord 	HC_COAL_IRQ		= (1 << 4),	/* IRQ coalescing */
250c6fd2807SJeff Garzik 	DEV_IRQ			= (1 << 8),	/* shift by port # */
251c6fd2807SJeff Garzik 
2522b748a0aSMark Lord 	/*
2532b748a0aSMark Lord 	 * Per-HC (Host-Controller) interrupt coalescing feature.
2542b748a0aSMark Lord 	 * This is present on all chip generations.
2552b748a0aSMark Lord 	 *
2562b748a0aSMark Lord 	 * Coalescing defers the interrupt until either the IO_THRESHOLD
2572b748a0aSMark Lord 	 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
2582b748a0aSMark Lord 	 */
259cae5a29dSMark Lord 	HC_IRQ_COAL_IO_THRESHOLD	= 0x000c,
260cae5a29dSMark Lord 	HC_IRQ_COAL_TIME_THRESHOLD	= 0x0010,
2612b748a0aSMark Lord 
262cae5a29dSMark Lord 	SOC_LED_CTRL		= 0x2c,
263000b344fSMark Lord 	SOC_LED_CTRL_BLINK	= (1 << 0),	/* Active LED blink */
264000b344fSMark Lord 	SOC_LED_CTRL_ACT_PRESENCE = (1 << 2),	/* Multiplex dev presence */
265000b344fSMark Lord 						/*  with dev activity LED */
266000b344fSMark Lord 
267c6fd2807SJeff Garzik 	/* Shadow block registers */
268cae5a29dSMark Lord 	SHD_BLK			= 0x100,
269cae5a29dSMark Lord 	SHD_CTL_AST		= 0x20,		/* ofs from SHD_BLK */
270c6fd2807SJeff Garzik 
271c6fd2807SJeff Garzik 	/* SATA registers */
272cae5a29dSMark Lord 	SATA_STATUS		= 0x300,  /* ctrl, err regs follow status */
273cae5a29dSMark Lord 	SATA_ACTIVE		= 0x350,
274cae5a29dSMark Lord 	FIS_IRQ_CAUSE		= 0x364,
275cae5a29dSMark Lord 	FIS_IRQ_CAUSE_AN	= (1 << 9),	/* async notification */
27617c5aab5SMark Lord 
277cae5a29dSMark Lord 	LTMODE			= 0x30c,	/* requires read-after-write */
27817c5aab5SMark Lord 	LTMODE_BIT8		= (1 << 8),	/* unknown, but necessary */
27917c5aab5SMark Lord 
280cae5a29dSMark Lord 	PHY_MODE2		= 0x330,
281c6fd2807SJeff Garzik 	PHY_MODE3		= 0x310,
282cae5a29dSMark Lord 
283cae5a29dSMark Lord 	PHY_MODE4		= 0x314,	/* requires read-after-write */
284ba069e37SMark Lord 	PHY_MODE4_CFG_MASK	= 0x00000003,	/* phy internal config field */
285ba069e37SMark Lord 	PHY_MODE4_CFG_VALUE	= 0x00000001,	/* phy internal config field */
286ba069e37SMark Lord 	PHY_MODE4_RSVD_ZEROS	= 0x5de3fffa,	/* Gen2e always write zeros */
287ba069e37SMark Lord 	PHY_MODE4_RSVD_ONES	= 0x00000005,	/* Gen2e always write ones */
288ba069e37SMark Lord 
289cae5a29dSMark Lord 	SATA_IFCTL		= 0x344,
290cae5a29dSMark Lord 	SATA_TESTCTL		= 0x348,
291cae5a29dSMark Lord 	SATA_IFSTAT		= 0x34c,
292cae5a29dSMark Lord 	VENDOR_UNIQUE_FIS	= 0x35c,
29317c5aab5SMark Lord 
294cae5a29dSMark Lord 	FISCFG			= 0x360,
2958e7decdbSMark Lord 	FISCFG_WAIT_DEV_ERR	= (1 << 8),	/* wait for host on DevErr */
2968e7decdbSMark Lord 	FISCFG_SINGLE_SYNC	= (1 << 16),	/* SYNC on DMA activation */
29717c5aab5SMark Lord 
29829b7e43cSMartin Michlmayr 	PHY_MODE9_GEN2		= 0x398,
29929b7e43cSMartin Michlmayr 	PHY_MODE9_GEN1		= 0x39c,
30029b7e43cSMartin Michlmayr 	PHYCFG_OFS		= 0x3a0,	/* only in 65n devices */
30129b7e43cSMartin Michlmayr 
302c6fd2807SJeff Garzik 	MV5_PHY_MODE		= 0x74,
303cae5a29dSMark Lord 	MV5_LTMODE		= 0x30,
304cae5a29dSMark Lord 	MV5_PHY_CTL		= 0x0C,
305cae5a29dSMark Lord 	SATA_IFCFG		= 0x050,
306c6fd2807SJeff Garzik 
307c6fd2807SJeff Garzik 	MV_M2_PREAMP_MASK	= 0x7e0,
308c6fd2807SJeff Garzik 
309c6fd2807SJeff Garzik 	/* Port registers */
310cae5a29dSMark Lord 	EDMA_CFG		= 0,
3110c58912eSMark Lord 	EDMA_CFG_Q_DEPTH	= 0x1f,		/* max device queue depth */
3120c58912eSMark Lord 	EDMA_CFG_NCQ		= (1 << 5),	/* for R/W FPDMA queued */
313c6fd2807SJeff Garzik 	EDMA_CFG_NCQ_GO_ON_ERR	= (1 << 14),	/* continue on error */
314c6fd2807SJeff Garzik 	EDMA_CFG_RD_BRST_EXT	= (1 << 11),	/* read burst 512B */
315c6fd2807SJeff Garzik 	EDMA_CFG_WR_BUFF_LEN	= (1 << 13),	/* write buffer 512B */
316e12bef50SMark Lord 	EDMA_CFG_EDMA_FBS	= (1 << 16),	/* EDMA FIS-Based Switching */
317e12bef50SMark Lord 	EDMA_CFG_FBS		= (1 << 26),	/* FIS-Based Switching */
318c6fd2807SJeff Garzik 
319cae5a29dSMark Lord 	EDMA_ERR_IRQ_CAUSE	= 0x8,
320cae5a29dSMark Lord 	EDMA_ERR_IRQ_MASK	= 0xc,
3216c1153e0SJeff Garzik 	EDMA_ERR_D_PAR		= (1 << 0),	/* UDMA data parity err */
3226c1153e0SJeff Garzik 	EDMA_ERR_PRD_PAR	= (1 << 1),	/* UDMA PRD parity err */
3236c1153e0SJeff Garzik 	EDMA_ERR_DEV		= (1 << 2),	/* device error */
3246c1153e0SJeff Garzik 	EDMA_ERR_DEV_DCON	= (1 << 3),	/* device disconnect */
3256c1153e0SJeff Garzik 	EDMA_ERR_DEV_CON	= (1 << 4),	/* device connected */
3266c1153e0SJeff Garzik 	EDMA_ERR_SERR		= (1 << 5),	/* SError bits [WBDST] raised */
327c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS	= (1 << 7),	/* Gen II/IIE self-disable */
328c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS_5	= (1 << 8),	/* Gen I self-disable */
3296c1153e0SJeff Garzik 	EDMA_ERR_BIST_ASYNC	= (1 << 8),	/* BIST FIS or Async Notify */
330c5d3e45aSJeff Garzik 	EDMA_ERR_TRANS_IRQ_7	= (1 << 8),	/* Gen IIE transprt layer irq */
3316c1153e0SJeff Garzik 	EDMA_ERR_CRQB_PAR	= (1 << 9),	/* CRQB parity error */
3326c1153e0SJeff Garzik 	EDMA_ERR_CRPB_PAR	= (1 << 10),	/* CRPB parity error */
3336c1153e0SJeff Garzik 	EDMA_ERR_INTRL_PAR	= (1 << 11),	/* internal parity error */
3346c1153e0SJeff Garzik 	EDMA_ERR_IORDY		= (1 << 12),	/* IORdy timeout */
335646a4da5SMark Lord 
3366c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_RX	= (0xf << 13),	/* link ctrl rx error */
337646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_0	= (1 << 13),	/* transient: CRC err */
338646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_1	= (1 << 14),	/* transient: FIFO err */
339646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_2	= (1 << 15),	/* fatal: caught SYNC */
340646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_3	= (1 << 16),	/* transient: FIS rx err */
341646a4da5SMark Lord 
3426c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_RX	= (0xf << 17),	/* link data rx error */
343646a4da5SMark Lord 
3446c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_TX	= (0x1f << 21),	/* link ctrl tx error */
345646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_0	= (1 << 21),	/* transient: CRC err */
346646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_1	= (1 << 22),	/* transient: FIFO err */
347646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_2	= (1 << 23),	/* transient: caught SYNC */
348646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_3	= (1 << 24),	/* transient: caught DMAT */
349646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_4	= (1 << 25),	/* transient: FIS collision */
350646a4da5SMark Lord 
3516c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_TX	= (0x1f << 26),	/* link data tx error */
352646a4da5SMark Lord 
3536c1153e0SJeff Garzik 	EDMA_ERR_TRANS_PROTO	= (1 << 31),	/* transport protocol error */
354c5d3e45aSJeff Garzik 	EDMA_ERR_OVERRUN_5	= (1 << 5),
355c5d3e45aSJeff Garzik 	EDMA_ERR_UNDERRUN_5	= (1 << 6),
356646a4da5SMark Lord 
357646a4da5SMark Lord 	EDMA_ERR_IRQ_TRANSIENT  = EDMA_ERR_LNK_CTRL_RX_0 |
358646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_1 |
359646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_3 |
36085afb934SMark Lord 				  EDMA_ERR_LNK_CTRL_TX,
361646a4da5SMark Lord 
362bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE		= EDMA_ERR_D_PAR |
363bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
364bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
365bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
366bdd4dddeSJeff Garzik 				  EDMA_ERR_SERR |
367bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS |
3686c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
369bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
370bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
371bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY |
372bdd4dddeSJeff Garzik 				  EDMA_ERR_LNK_CTRL_RX_2 |
373c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_RX |
374c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_TX |
375bdd4dddeSJeff Garzik 				  EDMA_ERR_TRANS_PROTO,
376e12bef50SMark Lord 
377bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE_5	= EDMA_ERR_D_PAR |
378bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
379bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
380bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
381bdd4dddeSJeff Garzik 				  EDMA_ERR_OVERRUN_5 |
382bdd4dddeSJeff Garzik 				  EDMA_ERR_UNDERRUN_5 |
383bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS_5 |
3846c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
385bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
386bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
387bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY,
388c6fd2807SJeff Garzik 
389cae5a29dSMark Lord 	EDMA_REQ_Q_BASE_HI	= 0x10,
390cae5a29dSMark Lord 	EDMA_REQ_Q_IN_PTR	= 0x14,		/* also contains BASE_LO */
391c6fd2807SJeff Garzik 
392cae5a29dSMark Lord 	EDMA_REQ_Q_OUT_PTR	= 0x18,
393c6fd2807SJeff Garzik 	EDMA_REQ_Q_PTR_SHIFT	= 5,
394c6fd2807SJeff Garzik 
395cae5a29dSMark Lord 	EDMA_RSP_Q_BASE_HI	= 0x1c,
396cae5a29dSMark Lord 	EDMA_RSP_Q_IN_PTR	= 0x20,
397cae5a29dSMark Lord 	EDMA_RSP_Q_OUT_PTR	= 0x24,		/* also contains BASE_LO */
398c6fd2807SJeff Garzik 	EDMA_RSP_Q_PTR_SHIFT	= 3,
399c6fd2807SJeff Garzik 
400cae5a29dSMark Lord 	EDMA_CMD		= 0x28,		/* EDMA command register */
4010ea9e179SJeff Garzik 	EDMA_EN			= (1 << 0),	/* enable EDMA */
4020ea9e179SJeff Garzik 	EDMA_DS			= (1 << 1),	/* disable EDMA; self-negated */
4038e7decdbSMark Lord 	EDMA_RESET		= (1 << 2),	/* reset eng/trans/link/phy */
404c6fd2807SJeff Garzik 
405cae5a29dSMark Lord 	EDMA_STATUS		= 0x30,		/* EDMA engine status */
4068e7decdbSMark Lord 	EDMA_STATUS_CACHE_EMPTY	= (1 << 6),	/* GenIIe command cache empty */
4078e7decdbSMark Lord 	EDMA_STATUS_IDLE	= (1 << 7),	/* GenIIe EDMA enabled/idle */
4088e7decdbSMark Lord 
409cae5a29dSMark Lord 	EDMA_IORDY_TMOUT	= 0x34,
410cae5a29dSMark Lord 	EDMA_ARB_CFG		= 0x38,
4118e7decdbSMark Lord 
412cae5a29dSMark Lord 	EDMA_HALTCOND		= 0x60,		/* GenIIe halt conditions */
413cae5a29dSMark Lord 	EDMA_UNKNOWN_RSVD	= 0x6C,		/* GenIIe unknown/reserved */
414da14265eSMark Lord 
415cae5a29dSMark Lord 	BMDMA_CMD		= 0x224,	/* bmdma command register */
416cae5a29dSMark Lord 	BMDMA_STATUS		= 0x228,	/* bmdma status register */
417cae5a29dSMark Lord 	BMDMA_PRD_LOW		= 0x22c,	/* bmdma PRD addr 31:0 */
418cae5a29dSMark Lord 	BMDMA_PRD_HIGH		= 0x230,	/* bmdma PRD addr 63:32 */
419da14265eSMark Lord 
420c6fd2807SJeff Garzik 	/* Host private flags (hp_flags) */
421c6fd2807SJeff Garzik 	MV_HP_FLAG_MSI		= (1 << 0),
422c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB0	= (1 << 1),
423c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB2	= (1 << 2),
424c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1B2	= (1 << 3),
425c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1C0	= (1 << 4),
4260ea9e179SJeff Garzik 	MV_HP_GEN_I		= (1 << 6),	/* Generation I: 50xx */
4270ea9e179SJeff Garzik 	MV_HP_GEN_II		= (1 << 7),	/* Generation II: 60xx */
4280ea9e179SJeff Garzik 	MV_HP_GEN_IIE		= (1 << 8),	/* Generation IIE: 6042/7042 */
42902a121daSMark Lord 	MV_HP_PCIE		= (1 << 9),	/* PCIe bus/regs: 7042 */
430616d4a98SMark Lord 	MV_HP_CUT_THROUGH	= (1 << 10),	/* can use EDMA cut-through */
4311f398472SMark Lord 	MV_HP_FLAG_SOC		= (1 << 11),	/* SystemOnChip, no PCI */
432000b344fSMark Lord 	MV_HP_QUIRK_LED_BLINK_EN = (1 << 12),	/* is led blinking enabled? */
433c6fd2807SJeff Garzik 
434c6fd2807SJeff Garzik 	/* Port private flags (pp_flags) */
4350ea9e179SJeff Garzik 	MV_PP_FLAG_EDMA_EN	= (1 << 0),	/* is EDMA engine enabled? */
43672109168SMark Lord 	MV_PP_FLAG_NCQ_EN	= (1 << 1),	/* is EDMA set up for NCQ? */
43700f42eabSMark Lord 	MV_PP_FLAG_FBS_EN	= (1 << 2),	/* is EDMA set up for FBS? */
43829d187bbSMark Lord 	MV_PP_FLAG_DELAYED_EH	= (1 << 3),	/* delayed dev err handling */
439d16ab3f6SMark Lord 	MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4),	/* ignore initial ATA_DRDY */
440c6fd2807SJeff Garzik };
441c6fd2807SJeff Garzik 
442ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
443ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
444c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
4458e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
4461f398472SMark Lord #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
447c6fd2807SJeff Garzik 
44815a32632SLennert Buytenhek #define WINDOW_CTRL(i)		(0x20030 + ((i) << 4))
44915a32632SLennert Buytenhek #define WINDOW_BASE(i)		(0x20034 + ((i) << 4))
45015a32632SLennert Buytenhek 
451c6fd2807SJeff Garzik enum {
452baf14aa1SJeff Garzik 	/* DMA boundary 0xffff is required by the s/g splitting
453baf14aa1SJeff Garzik 	 * we need on /length/ in mv_fill-sg().
454baf14aa1SJeff Garzik 	 */
455baf14aa1SJeff Garzik 	MV_DMA_BOUNDARY		= 0xffffU,
456c6fd2807SJeff Garzik 
4570ea9e179SJeff Garzik 	/* mask of register bits containing lower 32 bits
4580ea9e179SJeff Garzik 	 * of EDMA request queue DMA address
4590ea9e179SJeff Garzik 	 */
460c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_LO_MASK	= 0xfffffc00U,
461c6fd2807SJeff Garzik 
4620ea9e179SJeff Garzik 	/* ditto, for response queue */
463c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_LO_MASK	= 0xffffff00U,
464c6fd2807SJeff Garzik };
465c6fd2807SJeff Garzik 
466c6fd2807SJeff Garzik enum chip_type {
467c6fd2807SJeff Garzik 	chip_504x,
468c6fd2807SJeff Garzik 	chip_508x,
469c6fd2807SJeff Garzik 	chip_5080,
470c6fd2807SJeff Garzik 	chip_604x,
471c6fd2807SJeff Garzik 	chip_608x,
472c6fd2807SJeff Garzik 	chip_6042,
473c6fd2807SJeff Garzik 	chip_7042,
474f351b2d6SSaeed Bishara 	chip_soc,
475c6fd2807SJeff Garzik };
476c6fd2807SJeff Garzik 
477c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
478c6fd2807SJeff Garzik struct mv_crqb {
479c6fd2807SJeff Garzik 	__le32			sg_addr;
480c6fd2807SJeff Garzik 	__le32			sg_addr_hi;
481c6fd2807SJeff Garzik 	__le16			ctrl_flags;
482c6fd2807SJeff Garzik 	__le16			ata_cmd[11];
483c6fd2807SJeff Garzik };
484c6fd2807SJeff Garzik 
485c6fd2807SJeff Garzik struct mv_crqb_iie {
486c6fd2807SJeff Garzik 	__le32			addr;
487c6fd2807SJeff Garzik 	__le32			addr_hi;
488c6fd2807SJeff Garzik 	__le32			flags;
489c6fd2807SJeff Garzik 	__le32			len;
490c6fd2807SJeff Garzik 	__le32			ata_cmd[4];
491c6fd2807SJeff Garzik };
492c6fd2807SJeff Garzik 
493c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
494c6fd2807SJeff Garzik struct mv_crpb {
495c6fd2807SJeff Garzik 	__le16			id;
496c6fd2807SJeff Garzik 	__le16			flags;
497c6fd2807SJeff Garzik 	__le32			tmstmp;
498c6fd2807SJeff Garzik };
499c6fd2807SJeff Garzik 
500c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
501c6fd2807SJeff Garzik struct mv_sg {
502c6fd2807SJeff Garzik 	__le32			addr;
503c6fd2807SJeff Garzik 	__le32			flags_size;
504c6fd2807SJeff Garzik 	__le32			addr_hi;
505c6fd2807SJeff Garzik 	__le32			reserved;
506c6fd2807SJeff Garzik };
507c6fd2807SJeff Garzik 
50808da1759SMark Lord /*
50908da1759SMark Lord  * We keep a local cache of a few frequently accessed port
51008da1759SMark Lord  * registers here, to avoid having to read them (very slow)
51108da1759SMark Lord  * when switching between EDMA and non-EDMA modes.
51208da1759SMark Lord  */
51308da1759SMark Lord struct mv_cached_regs {
51408da1759SMark Lord 	u32			fiscfg;
51508da1759SMark Lord 	u32			ltmode;
51608da1759SMark Lord 	u32			haltcond;
517c01e8a23SMark Lord 	u32			unknown_rsvd;
51808da1759SMark Lord };
51908da1759SMark Lord 
520c6fd2807SJeff Garzik struct mv_port_priv {
521c6fd2807SJeff Garzik 	struct mv_crqb		*crqb;
522c6fd2807SJeff Garzik 	dma_addr_t		crqb_dma;
523c6fd2807SJeff Garzik 	struct mv_crpb		*crpb;
524c6fd2807SJeff Garzik 	dma_addr_t		crpb_dma;
525eb73d558SMark Lord 	struct mv_sg		*sg_tbl[MV_MAX_Q_DEPTH];
526eb73d558SMark Lord 	dma_addr_t		sg_tbl_dma[MV_MAX_Q_DEPTH];
527bdd4dddeSJeff Garzik 
528bdd4dddeSJeff Garzik 	unsigned int		req_idx;
529bdd4dddeSJeff Garzik 	unsigned int		resp_idx;
530bdd4dddeSJeff Garzik 
531c6fd2807SJeff Garzik 	u32			pp_flags;
53208da1759SMark Lord 	struct mv_cached_regs	cached;
53329d187bbSMark Lord 	unsigned int		delayed_eh_pmp_map;
534c6fd2807SJeff Garzik };
535c6fd2807SJeff Garzik 
536c6fd2807SJeff Garzik struct mv_port_signal {
537c6fd2807SJeff Garzik 	u32			amps;
538c6fd2807SJeff Garzik 	u32			pre;
539c6fd2807SJeff Garzik };
540c6fd2807SJeff Garzik 
54102a121daSMark Lord struct mv_host_priv {
54202a121daSMark Lord 	u32			hp_flags;
5431bfeff03SSaeed Bishara 	unsigned int 		board_idx;
54496e2c487SMark Lord 	u32			main_irq_mask;
54502a121daSMark Lord 	struct mv_port_signal	signal[8];
54602a121daSMark Lord 	const struct mv_hw_ops	*ops;
547f351b2d6SSaeed Bishara 	int			n_ports;
548f351b2d6SSaeed Bishara 	void __iomem		*base;
5497368f919SMark Lord 	void __iomem		*main_irq_cause_addr;
5507368f919SMark Lord 	void __iomem		*main_irq_mask_addr;
551cae5a29dSMark Lord 	u32			irq_cause_offset;
552cae5a29dSMark Lord 	u32			irq_mask_offset;
55302a121daSMark Lord 	u32			unmask_all_irqs;
554c77a2f4eSSaeed Bishara 
555c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
556c77a2f4eSSaeed Bishara 	struct clk		*clk;
557c77a2f4eSSaeed Bishara #endif
558da2fa9baSMark Lord 	/*
559da2fa9baSMark Lord 	 * These consistent DMA memory pools give us guaranteed
560da2fa9baSMark Lord 	 * alignment for hardware-accessed data structures,
561da2fa9baSMark Lord 	 * and less memory waste in accomplishing the alignment.
562da2fa9baSMark Lord 	 */
563da2fa9baSMark Lord 	struct dma_pool		*crqb_pool;
564da2fa9baSMark Lord 	struct dma_pool		*crpb_pool;
565da2fa9baSMark Lord 	struct dma_pool		*sg_tbl_pool;
56602a121daSMark Lord };
56702a121daSMark Lord 
568c6fd2807SJeff Garzik struct mv_hw_ops {
569c6fd2807SJeff Garzik 	void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
570c6fd2807SJeff Garzik 			   unsigned int port);
571c6fd2807SJeff Garzik 	void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
572c6fd2807SJeff Garzik 	void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
573c6fd2807SJeff Garzik 			   void __iomem *mmio);
574c6fd2807SJeff Garzik 	int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
575c6fd2807SJeff Garzik 			unsigned int n_hc);
576c6fd2807SJeff Garzik 	void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
5777bb3c529SSaeed Bishara 	void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
578c6fd2807SJeff Garzik };
579c6fd2807SJeff Garzik 
58082ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
58182ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
58282ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
58382ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
584c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap);
585c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap);
5863e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc);
587c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc);
588c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
589c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
590a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
591a1efdabaSTejun Heo 			unsigned long deadline);
592bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap);
593bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap);
594f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev);
595c6fd2807SJeff Garzik 
596c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
597c6fd2807SJeff Garzik 			   unsigned int port);
598c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
599c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
600c6fd2807SJeff Garzik 			   void __iomem *mmio);
601c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
602c6fd2807SJeff Garzik 			unsigned int n_hc);
603c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
6047bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
605c6fd2807SJeff Garzik 
606c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
607c6fd2807SJeff Garzik 			   unsigned int port);
608c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
609c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
610c6fd2807SJeff Garzik 			   void __iomem *mmio);
611c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
612c6fd2807SJeff Garzik 			unsigned int n_hc);
613c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
614f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
615f351b2d6SSaeed Bishara 				      void __iomem *mmio);
616f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
617f351b2d6SSaeed Bishara 				      void __iomem *mmio);
618f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
619f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc);
620f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
621f351b2d6SSaeed Bishara 				      void __iomem *mmio);
622f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
62329b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
62429b7e43cSMartin Michlmayr 				  void __iomem *mmio, unsigned int port);
6257bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
626e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
627c6fd2807SJeff Garzik 			     unsigned int port_no);
628e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap);
629b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio);
63000b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
631c6fd2807SJeff Garzik 
632e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp);
633e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
634e49856d8SMark Lord 				unsigned long deadline);
635e49856d8SMark Lord static int  mv_softreset(struct ata_link *link, unsigned int *class,
636e49856d8SMark Lord 				unsigned long deadline);
63729d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap);
6384c299ca3SMark Lord static void mv_process_crpb_entries(struct ata_port *ap,
6394c299ca3SMark Lord 					struct mv_port_priv *pp);
640c6fd2807SJeff Garzik 
641da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap);
642da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
643da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc);
644da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc);
645da14265eSMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc);
646da14265eSMark Lord static u8   mv_bmdma_status(struct ata_port *ap);
647d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap);
648da14265eSMark Lord 
649eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
650eb73d558SMark Lord  * because we have to allow room for worst case splitting of
651eb73d558SMark Lord  * PRDs for 64K boundaries in mv_fill_sg().
652eb73d558SMark Lord  */
653c5d3e45aSJeff Garzik static struct scsi_host_template mv5_sht = {
65468d1d07bSTejun Heo 	ATA_BASE_SHT(DRV_NAME),
655baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
656c5d3e45aSJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
657c5d3e45aSJeff Garzik };
658c5d3e45aSJeff Garzik 
659c5d3e45aSJeff Garzik static struct scsi_host_template mv6_sht = {
66068d1d07bSTejun Heo 	ATA_NCQ_SHT(DRV_NAME),
661138bfdd0SMark Lord 	.can_queue		= MV_MAX_Q_DEPTH - 1,
662baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
663c6fd2807SJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
664c6fd2807SJeff Garzik };
665c6fd2807SJeff Garzik 
666029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = {
667029cfd6bSTejun Heo 	.inherits		= &ata_sff_port_ops,
668c6fd2807SJeff Garzik 
669c96f1732SAlan Cox 	.lost_interrupt		= ATA_OP_NULL,
670c96f1732SAlan Cox 
6713e4a1391SMark Lord 	.qc_defer		= mv_qc_defer,
672c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep,
673c6fd2807SJeff Garzik 	.qc_issue		= mv_qc_issue,
674c6fd2807SJeff Garzik 
675bdd4dddeSJeff Garzik 	.freeze			= mv_eh_freeze,
676bdd4dddeSJeff Garzik 	.thaw			= mv_eh_thaw,
677a1efdabaSTejun Heo 	.hardreset		= mv_hardreset,
678a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler, /* avoid SFF EH */
679029cfd6bSTejun Heo 	.post_internal_cmd	= ATA_OP_NULL,
680bdd4dddeSJeff Garzik 
681c6fd2807SJeff Garzik 	.scr_read		= mv5_scr_read,
682c6fd2807SJeff Garzik 	.scr_write		= mv5_scr_write,
683c6fd2807SJeff Garzik 
684c6fd2807SJeff Garzik 	.port_start		= mv_port_start,
685c6fd2807SJeff Garzik 	.port_stop		= mv_port_stop,
686c6fd2807SJeff Garzik };
687c6fd2807SJeff Garzik 
688029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
689*8930ff25STejun Heo 	.inherits		= &ata_bmdma_port_ops,
690c6fd2807SJeff Garzik 
691*8930ff25STejun Heo 	.lost_interrupt		= ATA_OP_NULL,
692*8930ff25STejun Heo 
693*8930ff25STejun Heo 	.qc_defer		= mv_qc_defer,
694*8930ff25STejun Heo 	.qc_prep		= mv_qc_prep,
695*8930ff25STejun Heo 	.qc_issue		= mv_qc_issue,
696*8930ff25STejun Heo 
697*8930ff25STejun Heo 	.dev_config             = mv6_dev_config,
698*8930ff25STejun Heo 
699*8930ff25STejun Heo 	.freeze			= mv_eh_freeze,
700*8930ff25STejun Heo 	.thaw			= mv_eh_thaw,
701*8930ff25STejun Heo 	.hardreset		= mv_hardreset,
702*8930ff25STejun Heo 	.softreset		= mv_softreset,
703e49856d8SMark Lord 	.pmp_hardreset		= mv_pmp_hardreset,
704e49856d8SMark Lord 	.pmp_softreset		= mv_softreset,
70529d187bbSMark Lord 	.error_handler		= mv_pmp_error_handler,
706da14265eSMark Lord 
707*8930ff25STejun Heo 	.scr_read		= mv_scr_read,
708*8930ff25STejun Heo 	.scr_write		= mv_scr_write,
709*8930ff25STejun Heo 
710d16ab3f6SMark Lord 	.sff_check_status	= mv_sff_check_status,
711da14265eSMark Lord 	.sff_irq_clear		= mv_sff_irq_clear,
712da14265eSMark Lord 	.check_atapi_dma	= mv_check_atapi_dma,
713da14265eSMark Lord 	.bmdma_setup		= mv_bmdma_setup,
714da14265eSMark Lord 	.bmdma_start		= mv_bmdma_start,
715da14265eSMark Lord 	.bmdma_stop		= mv_bmdma_stop,
716da14265eSMark Lord 	.bmdma_status		= mv_bmdma_status,
717*8930ff25STejun Heo 
718*8930ff25STejun Heo 	.port_start		= mv_port_start,
719*8930ff25STejun Heo 	.port_stop		= mv_port_stop,
720*8930ff25STejun Heo 
721*8930ff25STejun Heo 	.mode_filter            = ATA_OP_NULL,  /* will be removed soon */
722c6fd2807SJeff Garzik };
723c6fd2807SJeff Garzik 
724029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
725029cfd6bSTejun Heo 	.inherits		= &mv6_ops,
726029cfd6bSTejun Heo 	.dev_config		= ATA_OP_NULL,
727c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep_iie,
728c6fd2807SJeff Garzik };
729c6fd2807SJeff Garzik 
730c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
731c6fd2807SJeff Garzik 	{  /* chip_504x */
73291b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS,
733c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
734bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
735c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
736c6fd2807SJeff Garzik 	},
737c6fd2807SJeff Garzik 	{  /* chip_508x */
73891b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
739c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
740bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
741c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
742c6fd2807SJeff Garzik 	},
743c6fd2807SJeff Garzik 	{  /* chip_5080 */
74491b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
745c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
746bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
747c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
748c6fd2807SJeff Garzik 	},
749c6fd2807SJeff Garzik 	{  /* chip_604x */
75091b1a84cSMark Lord 		.flags		= MV_GEN_II_FLAGS,
751c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
752bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
753c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
754c6fd2807SJeff Garzik 	},
755c6fd2807SJeff Garzik 	{  /* chip_608x */
75691b1a84cSMark Lord 		.flags		= MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
757c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
758bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
759c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
760c6fd2807SJeff Garzik 	},
761c6fd2807SJeff Garzik 	{  /* chip_6042 */
76291b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
763c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
764bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
765c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
766c6fd2807SJeff Garzik 	},
767c6fd2807SJeff Garzik 	{  /* chip_7042 */
76891b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
769c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
770bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
771c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
772c6fd2807SJeff Garzik 	},
773f351b2d6SSaeed Bishara 	{  /* chip_soc */
77491b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
775c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
776f351b2d6SSaeed Bishara 		.udma_mask	= ATA_UDMA6,
777f351b2d6SSaeed Bishara 		.port_ops	= &mv_iie_ops,
778f351b2d6SSaeed Bishara 	},
779c6fd2807SJeff Garzik };
780c6fd2807SJeff Garzik 
781c6fd2807SJeff Garzik static const struct pci_device_id mv_pci_tbl[] = {
7822d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
7832d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
7842d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
7852d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
78646c5784cSMark Lord 	/* RocketRAID 1720/174x have different identifiers */
78746c5784cSMark Lord 	{ PCI_VDEVICE(TTI, 0x1720), chip_6042 },
7884462254aSMark Lord 	{ PCI_VDEVICE(TTI, 0x1740), chip_6042 },
7894462254aSMark Lord 	{ PCI_VDEVICE(TTI, 0x1742), chip_6042 },
790c6fd2807SJeff Garzik 
7912d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
7922d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
7932d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
7942d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
7952d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
796c6fd2807SJeff Garzik 
7972d2744fcSJeff Garzik 	{ PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
7982d2744fcSJeff Garzik 
799d9f9c6bcSFlorian Attenberger 	/* Adaptec 1430SA */
800d9f9c6bcSFlorian Attenberger 	{ PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
801d9f9c6bcSFlorian Attenberger 
80202a121daSMark Lord 	/* Marvell 7042 support */
8036a3d586dSMorrison, Tom 	{ PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
8046a3d586dSMorrison, Tom 
80502a121daSMark Lord 	/* Highpoint RocketRAID PCIe series */
80602a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2300), chip_7042 },
80702a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2310), chip_7042 },
80802a121daSMark Lord 
809c6fd2807SJeff Garzik 	{ }			/* terminate list */
810c6fd2807SJeff Garzik };
811c6fd2807SJeff Garzik 
812c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
813c6fd2807SJeff Garzik 	.phy_errata		= mv5_phy_errata,
814c6fd2807SJeff Garzik 	.enable_leds		= mv5_enable_leds,
815c6fd2807SJeff Garzik 	.read_preamp		= mv5_read_preamp,
816c6fd2807SJeff Garzik 	.reset_hc		= mv5_reset_hc,
817c6fd2807SJeff Garzik 	.reset_flash		= mv5_reset_flash,
818c6fd2807SJeff Garzik 	.reset_bus		= mv5_reset_bus,
819c6fd2807SJeff Garzik };
820c6fd2807SJeff Garzik 
821c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
822c6fd2807SJeff Garzik 	.phy_errata		= mv6_phy_errata,
823c6fd2807SJeff Garzik 	.enable_leds		= mv6_enable_leds,
824c6fd2807SJeff Garzik 	.read_preamp		= mv6_read_preamp,
825c6fd2807SJeff Garzik 	.reset_hc		= mv6_reset_hc,
826c6fd2807SJeff Garzik 	.reset_flash		= mv6_reset_flash,
827c6fd2807SJeff Garzik 	.reset_bus		= mv_reset_pci_bus,
828c6fd2807SJeff Garzik };
829c6fd2807SJeff Garzik 
830f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
831f351b2d6SSaeed Bishara 	.phy_errata		= mv6_phy_errata,
832f351b2d6SSaeed Bishara 	.enable_leds		= mv_soc_enable_leds,
833f351b2d6SSaeed Bishara 	.read_preamp		= mv_soc_read_preamp,
834f351b2d6SSaeed Bishara 	.reset_hc		= mv_soc_reset_hc,
835f351b2d6SSaeed Bishara 	.reset_flash		= mv_soc_reset_flash,
836f351b2d6SSaeed Bishara 	.reset_bus		= mv_soc_reset_bus,
837f351b2d6SSaeed Bishara };
838f351b2d6SSaeed Bishara 
83929b7e43cSMartin Michlmayr static const struct mv_hw_ops mv_soc_65n_ops = {
84029b7e43cSMartin Michlmayr 	.phy_errata		= mv_soc_65n_phy_errata,
84129b7e43cSMartin Michlmayr 	.enable_leds		= mv_soc_enable_leds,
84229b7e43cSMartin Michlmayr 	.reset_hc		= mv_soc_reset_hc,
84329b7e43cSMartin Michlmayr 	.reset_flash		= mv_soc_reset_flash,
84429b7e43cSMartin Michlmayr 	.reset_bus		= mv_soc_reset_bus,
84529b7e43cSMartin Michlmayr };
84629b7e43cSMartin Michlmayr 
847c6fd2807SJeff Garzik /*
848c6fd2807SJeff Garzik  * Functions
849c6fd2807SJeff Garzik  */
850c6fd2807SJeff Garzik 
851c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
852c6fd2807SJeff Garzik {
853c6fd2807SJeff Garzik 	writel(data, addr);
854c6fd2807SJeff Garzik 	(void) readl(addr);	/* flush to avoid PCI posted write */
855c6fd2807SJeff Garzik }
856c6fd2807SJeff Garzik 
857c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
858c6fd2807SJeff Garzik {
859c6fd2807SJeff Garzik 	return port >> MV_PORT_HC_SHIFT;
860c6fd2807SJeff Garzik }
861c6fd2807SJeff Garzik 
862c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
863c6fd2807SJeff Garzik {
864c6fd2807SJeff Garzik 	return port & MV_PORT_MASK;
865c6fd2807SJeff Garzik }
866c6fd2807SJeff Garzik 
8671cfd19aeSMark Lord /*
8681cfd19aeSMark Lord  * Consolidate some rather tricky bit shift calculations.
8691cfd19aeSMark Lord  * This is hot-path stuff, so not a function.
8701cfd19aeSMark Lord  * Simple code, with two return values, so macro rather than inline.
8711cfd19aeSMark Lord  *
8721cfd19aeSMark Lord  * port is the sole input, in range 0..7.
8737368f919SMark Lord  * shift is one output, for use with main_irq_cause / main_irq_mask registers.
8747368f919SMark Lord  * hardport is the other output, in range 0..3.
8751cfd19aeSMark Lord  *
8761cfd19aeSMark Lord  * Note that port and hardport may be the same variable in some cases.
8771cfd19aeSMark Lord  */
8781cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport)	\
8791cfd19aeSMark Lord {								\
8801cfd19aeSMark Lord 	shift    = mv_hc_from_port(port) * HC_SHIFT;		\
8811cfd19aeSMark Lord 	hardport = mv_hardport_from_port(port);			\
8821cfd19aeSMark Lord 	shift   += hardport * 2;				\
8831cfd19aeSMark Lord }
8841cfd19aeSMark Lord 
885352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
886352fab70SMark Lord {
887cae5a29dSMark Lord 	return (base + SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
888352fab70SMark Lord }
889352fab70SMark Lord 
890c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
891c6fd2807SJeff Garzik 						 unsigned int port)
892c6fd2807SJeff Garzik {
893c6fd2807SJeff Garzik 	return mv_hc_base(base, mv_hc_from_port(port));
894c6fd2807SJeff Garzik }
895c6fd2807SJeff Garzik 
896c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
897c6fd2807SJeff Garzik {
898c6fd2807SJeff Garzik 	return  mv_hc_base_from_port(base, port) +
899c6fd2807SJeff Garzik 		MV_SATAHC_ARBTR_REG_SZ +
900c6fd2807SJeff Garzik 		(mv_hardport_from_port(port) * MV_PORT_REG_SZ);
901c6fd2807SJeff Garzik }
902c6fd2807SJeff Garzik 
903e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
904e12bef50SMark Lord {
905e12bef50SMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
906e12bef50SMark Lord 	unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
907e12bef50SMark Lord 
908e12bef50SMark Lord 	return hc_mmio + ofs;
909e12bef50SMark Lord }
910e12bef50SMark Lord 
911f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
912f351b2d6SSaeed Bishara {
913f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
914f351b2d6SSaeed Bishara 	return hpriv->base;
915f351b2d6SSaeed Bishara }
916f351b2d6SSaeed Bishara 
917c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
918c6fd2807SJeff Garzik {
919f351b2d6SSaeed Bishara 	return mv_port_base(mv_host_base(ap->host), ap->port_no);
920c6fd2807SJeff Garzik }
921c6fd2807SJeff Garzik 
922cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
923c6fd2807SJeff Garzik {
924cca3974eSJeff Garzik 	return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
925c6fd2807SJeff Garzik }
926c6fd2807SJeff Garzik 
92708da1759SMark Lord /**
92808da1759SMark Lord  *      mv_save_cached_regs - (re-)initialize cached port registers
92908da1759SMark Lord  *      @ap: the port whose registers we are caching
93008da1759SMark Lord  *
93108da1759SMark Lord  *	Initialize the local cache of port registers,
93208da1759SMark Lord  *	so that reading them over and over again can
93308da1759SMark Lord  *	be avoided on the hotter paths of this driver.
93408da1759SMark Lord  *	This saves a few microseconds each time we switch
93508da1759SMark Lord  *	to/from EDMA mode to perform (eg.) a drive cache flush.
93608da1759SMark Lord  */
93708da1759SMark Lord static void mv_save_cached_regs(struct ata_port *ap)
93808da1759SMark Lord {
93908da1759SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
94008da1759SMark Lord 	struct mv_port_priv *pp = ap->private_data;
94108da1759SMark Lord 
942cae5a29dSMark Lord 	pp->cached.fiscfg = readl(port_mmio + FISCFG);
943cae5a29dSMark Lord 	pp->cached.ltmode = readl(port_mmio + LTMODE);
944cae5a29dSMark Lord 	pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND);
945cae5a29dSMark Lord 	pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD);
94608da1759SMark Lord }
94708da1759SMark Lord 
94808da1759SMark Lord /**
94908da1759SMark Lord  *      mv_write_cached_reg - write to a cached port register
95008da1759SMark Lord  *      @addr: hardware address of the register
95108da1759SMark Lord  *      @old: pointer to cached value of the register
95208da1759SMark Lord  *      @new: new value for the register
95308da1759SMark Lord  *
95408da1759SMark Lord  *	Write a new value to a cached register,
95508da1759SMark Lord  *	but only if the value is different from before.
95608da1759SMark Lord  */
95708da1759SMark Lord static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
95808da1759SMark Lord {
95908da1759SMark Lord 	if (new != *old) {
96012f3b6d7SMark Lord 		unsigned long laddr;
96108da1759SMark Lord 		*old = new;
96212f3b6d7SMark Lord 		/*
96312f3b6d7SMark Lord 		 * Workaround for 88SX60x1-B2 FEr SATA#13:
96412f3b6d7SMark Lord 		 * Read-after-write is needed to prevent generating 64-bit
96512f3b6d7SMark Lord 		 * write cycles on the PCI bus for SATA interface registers
96612f3b6d7SMark Lord 		 * at offsets ending in 0x4 or 0xc.
96712f3b6d7SMark Lord 		 *
96812f3b6d7SMark Lord 		 * Looks like a lot of fuss, but it avoids an unnecessary
96912f3b6d7SMark Lord 		 * +1 usec read-after-write delay for unaffected registers.
97012f3b6d7SMark Lord 		 */
97112f3b6d7SMark Lord 		laddr = (long)addr & 0xffff;
97212f3b6d7SMark Lord 		if (laddr >= 0x300 && laddr <= 0x33c) {
97312f3b6d7SMark Lord 			laddr &= 0x000f;
97412f3b6d7SMark Lord 			if (laddr == 0x4 || laddr == 0xc) {
97512f3b6d7SMark Lord 				writelfl(new, addr); /* read after write */
97612f3b6d7SMark Lord 				return;
97712f3b6d7SMark Lord 			}
97812f3b6d7SMark Lord 		}
97912f3b6d7SMark Lord 		writel(new, addr); /* unaffected by the errata */
98008da1759SMark Lord 	}
98108da1759SMark Lord }
98208da1759SMark Lord 
983c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
984c5d3e45aSJeff Garzik 			     struct mv_host_priv *hpriv,
985c5d3e45aSJeff Garzik 			     struct mv_port_priv *pp)
986c5d3e45aSJeff Garzik {
987bdd4dddeSJeff Garzik 	u32 index;
988bdd4dddeSJeff Garzik 
989c5d3e45aSJeff Garzik 	/*
990c5d3e45aSJeff Garzik 	 * initialize request queue
991c5d3e45aSJeff Garzik 	 */
992fcfb1f77SMark Lord 	pp->req_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
993fcfb1f77SMark Lord 	index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
994bdd4dddeSJeff Garzik 
995c5d3e45aSJeff Garzik 	WARN_ON(pp->crqb_dma & 0x3ff);
996cae5a29dSMark Lord 	writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI);
997bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
998cae5a29dSMark Lord 		 port_mmio + EDMA_REQ_Q_IN_PTR);
999cae5a29dSMark Lord 	writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR);
1000c5d3e45aSJeff Garzik 
1001c5d3e45aSJeff Garzik 	/*
1002c5d3e45aSJeff Garzik 	 * initialize response queue
1003c5d3e45aSJeff Garzik 	 */
1004fcfb1f77SMark Lord 	pp->resp_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
1005fcfb1f77SMark Lord 	index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
1006bdd4dddeSJeff Garzik 
1007c5d3e45aSJeff Garzik 	WARN_ON(pp->crpb_dma & 0xff);
1008cae5a29dSMark Lord 	writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI);
1009cae5a29dSMark Lord 	writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR);
1010bdd4dddeSJeff Garzik 	writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
1011cae5a29dSMark Lord 		 port_mmio + EDMA_RSP_Q_OUT_PTR);
1012c5d3e45aSJeff Garzik }
1013c5d3e45aSJeff Garzik 
10142b748a0aSMark Lord static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
10152b748a0aSMark Lord {
10162b748a0aSMark Lord 	/*
10172b748a0aSMark Lord 	 * When writing to the main_irq_mask in hardware,
10182b748a0aSMark Lord 	 * we must ensure exclusivity between the interrupt coalescing bits
10192b748a0aSMark Lord 	 * and the corresponding individual port DONE_IRQ bits.
10202b748a0aSMark Lord 	 *
10212b748a0aSMark Lord 	 * Note that this register is really an "IRQ enable" register,
10222b748a0aSMark Lord 	 * not an "IRQ mask" register as Marvell's naming might suggest.
10232b748a0aSMark Lord 	 */
10242b748a0aSMark Lord 	if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
10252b748a0aSMark Lord 		mask &= ~DONE_IRQ_0_3;
10262b748a0aSMark Lord 	if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
10272b748a0aSMark Lord 		mask &= ~DONE_IRQ_4_7;
10282b748a0aSMark Lord 	writelfl(mask, hpriv->main_irq_mask_addr);
10292b748a0aSMark Lord }
10302b748a0aSMark Lord 
1031c4de573bSMark Lord static void mv_set_main_irq_mask(struct ata_host *host,
1032c4de573bSMark Lord 				 u32 disable_bits, u32 enable_bits)
1033c4de573bSMark Lord {
1034c4de573bSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1035c4de573bSMark Lord 	u32 old_mask, new_mask;
1036c4de573bSMark Lord 
103796e2c487SMark Lord 	old_mask = hpriv->main_irq_mask;
1038c4de573bSMark Lord 	new_mask = (old_mask & ~disable_bits) | enable_bits;
103996e2c487SMark Lord 	if (new_mask != old_mask) {
104096e2c487SMark Lord 		hpriv->main_irq_mask = new_mask;
10412b748a0aSMark Lord 		mv_write_main_irq_mask(new_mask, hpriv);
1042c4de573bSMark Lord 	}
104396e2c487SMark Lord }
1044c4de573bSMark Lord 
1045c4de573bSMark Lord static void mv_enable_port_irqs(struct ata_port *ap,
1046c4de573bSMark Lord 				     unsigned int port_bits)
1047c4de573bSMark Lord {
1048c4de573bSMark Lord 	unsigned int shift, hardport, port = ap->port_no;
1049c4de573bSMark Lord 	u32 disable_bits, enable_bits;
1050c4de573bSMark Lord 
1051c4de573bSMark Lord 	MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
1052c4de573bSMark Lord 
1053c4de573bSMark Lord 	disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
1054c4de573bSMark Lord 	enable_bits  = port_bits << shift;
1055c4de573bSMark Lord 	mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
1056c4de573bSMark Lord }
1057c4de573bSMark Lord 
105800b81235SMark Lord static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
105900b81235SMark Lord 					  void __iomem *port_mmio,
106000b81235SMark Lord 					  unsigned int port_irqs)
1061c6fd2807SJeff Garzik {
10620c58912eSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1063352fab70SMark Lord 	int hardport = mv_hardport_from_port(ap->port_no);
10640c58912eSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(
1065b0bccb18SMark Lord 				mv_host_base(ap->host), ap->port_no);
1066cae6edc3SMark Lord 	u32 hc_irq_cause;
10670c58912eSMark Lord 
1068bdd4dddeSJeff Garzik 	/* clear EDMA event indicators, if any */
1069cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
1070bdd4dddeSJeff Garzik 
1071cae6edc3SMark Lord 	/* clear pending irq events */
1072cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1073cae5a29dSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
10740c58912eSMark Lord 
10750c58912eSMark Lord 	/* clear FIS IRQ Cause */
1076e4006077SMark Lord 	if (IS_GEN_IIE(hpriv))
1077cae5a29dSMark Lord 		writelfl(0, port_mmio + FIS_IRQ_CAUSE);
10780c58912eSMark Lord 
107900b81235SMark Lord 	mv_enable_port_irqs(ap, port_irqs);
108000b81235SMark Lord }
108100b81235SMark Lord 
10822b748a0aSMark Lord static void mv_set_irq_coalescing(struct ata_host *host,
10832b748a0aSMark Lord 				  unsigned int count, unsigned int usecs)
10842b748a0aSMark Lord {
10852b748a0aSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
10862b748a0aSMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
10872b748a0aSMark Lord 	u32 coal_enable = 0;
10882b748a0aSMark Lord 	unsigned long flags;
10896abf4678SMark Lord 	unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
10902b748a0aSMark Lord 	const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
10912b748a0aSMark Lord 							ALL_PORTS_COAL_DONE;
10922b748a0aSMark Lord 
10932b748a0aSMark Lord 	/* Disable IRQ coalescing if either threshold is zero */
10942b748a0aSMark Lord 	if (!usecs || !count) {
10952b748a0aSMark Lord 		clks = count = 0;
10962b748a0aSMark Lord 	} else {
10972b748a0aSMark Lord 		/* Respect maximum limits of the hardware */
10982b748a0aSMark Lord 		clks = usecs * COAL_CLOCKS_PER_USEC;
10992b748a0aSMark Lord 		if (clks > MAX_COAL_TIME_THRESHOLD)
11002b748a0aSMark Lord 			clks = MAX_COAL_TIME_THRESHOLD;
11012b748a0aSMark Lord 		if (count > MAX_COAL_IO_COUNT)
11022b748a0aSMark Lord 			count = MAX_COAL_IO_COUNT;
11032b748a0aSMark Lord 	}
11042b748a0aSMark Lord 
11052b748a0aSMark Lord 	spin_lock_irqsave(&host->lock, flags);
11066abf4678SMark Lord 	mv_set_main_irq_mask(host, coal_disable, 0);
11072b748a0aSMark Lord 
11086abf4678SMark Lord 	if (is_dual_hc && !IS_GEN_I(hpriv)) {
11092b748a0aSMark Lord 		/*
11106abf4678SMark Lord 		 * GEN_II/GEN_IIE with dual host controllers:
11116abf4678SMark Lord 		 * one set of global thresholds for the entire chip.
11122b748a0aSMark Lord 		 */
1113cae5a29dSMark Lord 		writel(clks,  mmio + IRQ_COAL_TIME_THRESHOLD);
1114cae5a29dSMark Lord 		writel(count, mmio + IRQ_COAL_IO_THRESHOLD);
11152b748a0aSMark Lord 		/* clear leftover coal IRQ bit */
1116cae5a29dSMark Lord 		writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
11176abf4678SMark Lord 		if (count)
11182b748a0aSMark Lord 			coal_enable = ALL_PORTS_COAL_DONE;
11196abf4678SMark Lord 		clks = count = 0; /* force clearing of regular regs below */
11202b748a0aSMark Lord 	}
11216abf4678SMark Lord 
11222b748a0aSMark Lord 	/*
11232b748a0aSMark Lord 	 * All chips: independent thresholds for each HC on the chip.
11242b748a0aSMark Lord 	 */
11252b748a0aSMark Lord 	hc_mmio = mv_hc_base_from_port(mmio, 0);
1126cae5a29dSMark Lord 	writel(clks,  hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1127cae5a29dSMark Lord 	writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1128cae5a29dSMark Lord 	writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11296abf4678SMark Lord 	if (count)
11302b748a0aSMark Lord 		coal_enable |= PORTS_0_3_COAL_DONE;
11316abf4678SMark Lord 	if (is_dual_hc) {
11322b748a0aSMark Lord 		hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
1133cae5a29dSMark Lord 		writel(clks,  hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1134cae5a29dSMark Lord 		writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1135cae5a29dSMark Lord 		writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11366abf4678SMark Lord 		if (count)
11372b748a0aSMark Lord 			coal_enable |= PORTS_4_7_COAL_DONE;
11382b748a0aSMark Lord 	}
11392b748a0aSMark Lord 
11406abf4678SMark Lord 	mv_set_main_irq_mask(host, 0, coal_enable);
11412b748a0aSMark Lord 	spin_unlock_irqrestore(&host->lock, flags);
11422b748a0aSMark Lord }
11432b748a0aSMark Lord 
114400b81235SMark Lord /**
114500b81235SMark Lord  *      mv_start_edma - Enable eDMA engine
114600b81235SMark Lord  *      @base: port base address
114700b81235SMark Lord  *      @pp: port private data
114800b81235SMark Lord  *
114900b81235SMark Lord  *      Verify the local cache of the eDMA state is accurate with a
115000b81235SMark Lord  *      WARN_ON.
115100b81235SMark Lord  *
115200b81235SMark Lord  *      LOCKING:
115300b81235SMark Lord  *      Inherited from caller.
115400b81235SMark Lord  */
115500b81235SMark Lord static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
115600b81235SMark Lord 			 struct mv_port_priv *pp, u8 protocol)
115700b81235SMark Lord {
115800b81235SMark Lord 	int want_ncq = (protocol == ATA_PROT_NCQ);
115900b81235SMark Lord 
116000b81235SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
116100b81235SMark Lord 		int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
116200b81235SMark Lord 		if (want_ncq != using_ncq)
116300b81235SMark Lord 			mv_stop_edma(ap);
116400b81235SMark Lord 	}
116500b81235SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
116600b81235SMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
116700b81235SMark Lord 
116800b81235SMark Lord 		mv_edma_cfg(ap, want_ncq, 1);
116900b81235SMark Lord 
1170f630d562SMark Lord 		mv_set_edma_ptrs(port_mmio, hpriv, pp);
117100b81235SMark Lord 		mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
1172bdd4dddeSJeff Garzik 
1173cae5a29dSMark Lord 		writelfl(EDMA_EN, port_mmio + EDMA_CMD);
1174c6fd2807SJeff Garzik 		pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
1175c6fd2807SJeff Garzik 	}
1176c6fd2807SJeff Garzik }
1177c6fd2807SJeff Garzik 
11789b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
11799b2c4e0bSMark Lord {
11809b2c4e0bSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
11819b2c4e0bSMark Lord 	const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
11829b2c4e0bSMark Lord 	const int per_loop = 5, timeout = (15 * 1000 / per_loop);
11839b2c4e0bSMark Lord 	int i;
11849b2c4e0bSMark Lord 
11859b2c4e0bSMark Lord 	/*
11869b2c4e0bSMark Lord 	 * Wait for the EDMA engine to finish transactions in progress.
1187c46938ccSMark Lord 	 * No idea what a good "timeout" value might be, but measurements
1188c46938ccSMark Lord 	 * indicate that it often requires hundreds of microseconds
1189c46938ccSMark Lord 	 * with two drives in-use.  So we use the 15msec value above
1190c46938ccSMark Lord 	 * as a rough guess at what even more drives might require.
11919b2c4e0bSMark Lord 	 */
11929b2c4e0bSMark Lord 	for (i = 0; i < timeout; ++i) {
1193cae5a29dSMark Lord 		u32 edma_stat = readl(port_mmio + EDMA_STATUS);
11949b2c4e0bSMark Lord 		if ((edma_stat & empty_idle) == empty_idle)
11959b2c4e0bSMark Lord 			break;
11969b2c4e0bSMark Lord 		udelay(per_loop);
11979b2c4e0bSMark Lord 	}
11989b2c4e0bSMark Lord 	/* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
11999b2c4e0bSMark Lord }
12009b2c4e0bSMark Lord 
1201c6fd2807SJeff Garzik /**
1202e12bef50SMark Lord  *      mv_stop_edma_engine - Disable eDMA engine
1203b562468cSMark Lord  *      @port_mmio: io base address
1204c6fd2807SJeff Garzik  *
1205c6fd2807SJeff Garzik  *      LOCKING:
1206c6fd2807SJeff Garzik  *      Inherited from caller.
1207c6fd2807SJeff Garzik  */
1208b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
1209c6fd2807SJeff Garzik {
1210b562468cSMark Lord 	int i;
1211c6fd2807SJeff Garzik 
1212b562468cSMark Lord 	/* Disable eDMA.  The disable bit auto clears. */
1213cae5a29dSMark Lord 	writelfl(EDMA_DS, port_mmio + EDMA_CMD);
1214c6fd2807SJeff Garzik 
1215b562468cSMark Lord 	/* Wait for the chip to confirm eDMA is off. */
1216b562468cSMark Lord 	for (i = 10000; i > 0; i--) {
1217cae5a29dSMark Lord 		u32 reg = readl(port_mmio + EDMA_CMD);
12184537deb5SJeff Garzik 		if (!(reg & EDMA_EN))
1219b562468cSMark Lord 			return 0;
1220b562468cSMark Lord 		udelay(10);
1221c6fd2807SJeff Garzik 	}
1222b562468cSMark Lord 	return -EIO;
1223c6fd2807SJeff Garzik }
1224c6fd2807SJeff Garzik 
1225e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
1226c6fd2807SJeff Garzik {
1227c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1228c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
122966e57a2cSMark Lord 	int err = 0;
1230c6fd2807SJeff Garzik 
1231b562468cSMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
1232b562468cSMark Lord 		return 0;
1233c6fd2807SJeff Garzik 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
12349b2c4e0bSMark Lord 	mv_wait_for_edma_empty_idle(ap);
1235b562468cSMark Lord 	if (mv_stop_edma_engine(port_mmio)) {
1236c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
123766e57a2cSMark Lord 		err = -EIO;
1238c6fd2807SJeff Garzik 	}
123966e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
124066e57a2cSMark Lord 	return err;
12410ea9e179SJeff Garzik }
12420ea9e179SJeff Garzik 
1243c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1244c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes)
1245c6fd2807SJeff Garzik {
1246c6fd2807SJeff Garzik 	int b, w;
1247c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
1248c6fd2807SJeff Garzik 		DPRINTK("%p: ", start + b);
1249c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
1250c6fd2807SJeff Garzik 			printk("%08x ", readl(start + b));
1251c6fd2807SJeff Garzik 			b += sizeof(u32);
1252c6fd2807SJeff Garzik 		}
1253c6fd2807SJeff Garzik 		printk("\n");
1254c6fd2807SJeff Garzik 	}
1255c6fd2807SJeff Garzik }
1256c6fd2807SJeff Garzik #endif
1257c6fd2807SJeff Garzik 
1258c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
1259c6fd2807SJeff Garzik {
1260c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1261c6fd2807SJeff Garzik 	int b, w;
1262c6fd2807SJeff Garzik 	u32 dw;
1263c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
1264c6fd2807SJeff Garzik 		DPRINTK("%02x: ", b);
1265c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
1266c6fd2807SJeff Garzik 			(void) pci_read_config_dword(pdev, b, &dw);
1267c6fd2807SJeff Garzik 			printk("%08x ", dw);
1268c6fd2807SJeff Garzik 			b += sizeof(u32);
1269c6fd2807SJeff Garzik 		}
1270c6fd2807SJeff Garzik 		printk("\n");
1271c6fd2807SJeff Garzik 	}
1272c6fd2807SJeff Garzik #endif
1273c6fd2807SJeff Garzik }
1274c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port,
1275c6fd2807SJeff Garzik 			     struct pci_dev *pdev)
1276c6fd2807SJeff Garzik {
1277c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1278c6fd2807SJeff Garzik 	void __iomem *hc_base = mv_hc_base(mmio_base,
1279c6fd2807SJeff Garzik 					   port >> MV_PORT_HC_SHIFT);
1280c6fd2807SJeff Garzik 	void __iomem *port_base;
1281c6fd2807SJeff Garzik 	int start_port, num_ports, p, start_hc, num_hcs, hc;
1282c6fd2807SJeff Garzik 
1283c6fd2807SJeff Garzik 	if (0 > port) {
1284c6fd2807SJeff Garzik 		start_hc = start_port = 0;
1285c6fd2807SJeff Garzik 		num_ports = 8;		/* shld be benign for 4 port devs */
1286c6fd2807SJeff Garzik 		num_hcs = 2;
1287c6fd2807SJeff Garzik 	} else {
1288c6fd2807SJeff Garzik 		start_hc = port >> MV_PORT_HC_SHIFT;
1289c6fd2807SJeff Garzik 		start_port = port;
1290c6fd2807SJeff Garzik 		num_ports = num_hcs = 1;
1291c6fd2807SJeff Garzik 	}
1292c6fd2807SJeff Garzik 	DPRINTK("All registers for port(s) %u-%u:\n", start_port,
1293c6fd2807SJeff Garzik 		num_ports > 1 ? num_ports - 1 : start_port);
1294c6fd2807SJeff Garzik 
1295c6fd2807SJeff Garzik 	if (NULL != pdev) {
1296c6fd2807SJeff Garzik 		DPRINTK("PCI config space regs:\n");
1297c6fd2807SJeff Garzik 		mv_dump_pci_cfg(pdev, 0x68);
1298c6fd2807SJeff Garzik 	}
1299c6fd2807SJeff Garzik 	DPRINTK("PCI regs:\n");
1300c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xc00, 0x3c);
1301c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xd00, 0x34);
1302c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xf00, 0x4);
1303c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0x1d00, 0x6c);
1304c6fd2807SJeff Garzik 	for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
1305c6fd2807SJeff Garzik 		hc_base = mv_hc_base(mmio_base, hc);
1306c6fd2807SJeff Garzik 		DPRINTK("HC regs (HC %i):\n", hc);
1307c6fd2807SJeff Garzik 		mv_dump_mem(hc_base, 0x1c);
1308c6fd2807SJeff Garzik 	}
1309c6fd2807SJeff Garzik 	for (p = start_port; p < start_port + num_ports; p++) {
1310c6fd2807SJeff Garzik 		port_base = mv_port_base(mmio_base, p);
1311c6fd2807SJeff Garzik 		DPRINTK("EDMA regs (port %i):\n", p);
1312c6fd2807SJeff Garzik 		mv_dump_mem(port_base, 0x54);
1313c6fd2807SJeff Garzik 		DPRINTK("SATA regs (port %i):\n", p);
1314c6fd2807SJeff Garzik 		mv_dump_mem(port_base+0x300, 0x60);
1315c6fd2807SJeff Garzik 	}
1316c6fd2807SJeff Garzik #endif
1317c6fd2807SJeff Garzik }
1318c6fd2807SJeff Garzik 
1319c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1320c6fd2807SJeff Garzik {
1321c6fd2807SJeff Garzik 	unsigned int ofs;
1322c6fd2807SJeff Garzik 
1323c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1324c6fd2807SJeff Garzik 	case SCR_STATUS:
1325c6fd2807SJeff Garzik 	case SCR_CONTROL:
1326c6fd2807SJeff Garzik 	case SCR_ERROR:
1327cae5a29dSMark Lord 		ofs = SATA_STATUS + (sc_reg_in * sizeof(u32));
1328c6fd2807SJeff Garzik 		break;
1329c6fd2807SJeff Garzik 	case SCR_ACTIVE:
1330cae5a29dSMark Lord 		ofs = SATA_ACTIVE;   /* active is not with the others */
1331c6fd2807SJeff Garzik 		break;
1332c6fd2807SJeff Garzik 	default:
1333c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1334c6fd2807SJeff Garzik 		break;
1335c6fd2807SJeff Garzik 	}
1336c6fd2807SJeff Garzik 	return ofs;
1337c6fd2807SJeff Garzik }
1338c6fd2807SJeff Garzik 
133982ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
1340c6fd2807SJeff Garzik {
1341c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1342c6fd2807SJeff Garzik 
1343da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
134482ef04fbSTejun Heo 		*val = readl(mv_ap_base(link->ap) + ofs);
1345da3dbb17STejun Heo 		return 0;
1346da3dbb17STejun Heo 	} else
1347da3dbb17STejun Heo 		return -EINVAL;
1348c6fd2807SJeff Garzik }
1349c6fd2807SJeff Garzik 
135082ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
1351c6fd2807SJeff Garzik {
1352c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1353c6fd2807SJeff Garzik 
1354da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
135520091773SMark Lord 		void __iomem *addr = mv_ap_base(link->ap) + ofs;
135620091773SMark Lord 		if (sc_reg_in == SCR_CONTROL) {
135720091773SMark Lord 			/*
135820091773SMark Lord 			 * Workaround for 88SX60x1 FEr SATA#26:
135920091773SMark Lord 			 *
136020091773SMark Lord 			 * COMRESETs have to take care not to accidently
136120091773SMark Lord 			 * put the drive to sleep when writing SCR_CONTROL.
136220091773SMark Lord 			 * Setting bits 12..15 prevents this problem.
136320091773SMark Lord 			 *
136420091773SMark Lord 			 * So if we see an outbound COMMRESET, set those bits.
136520091773SMark Lord 			 * Ditto for the followup write that clears the reset.
136620091773SMark Lord 			 *
136720091773SMark Lord 			 * The proprietary driver does this for
136820091773SMark Lord 			 * all chip versions, and so do we.
136920091773SMark Lord 			 */
137020091773SMark Lord 			if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1)
137120091773SMark Lord 				val |= 0xf000;
137220091773SMark Lord 		}
137320091773SMark Lord 		writelfl(val, addr);
1374da3dbb17STejun Heo 		return 0;
1375da3dbb17STejun Heo 	} else
1376da3dbb17STejun Heo 		return -EINVAL;
1377c6fd2807SJeff Garzik }
1378c6fd2807SJeff Garzik 
1379f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1380f273827eSMark Lord {
1381f273827eSMark Lord 	/*
1382e49856d8SMark Lord 	 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1383e49856d8SMark Lord 	 *
1384e49856d8SMark Lord 	 * Gen-II does not support NCQ over a port multiplier
1385e49856d8SMark Lord 	 *  (no FIS-based switching).
1386f273827eSMark Lord 	 */
1387e49856d8SMark Lord 	if (adev->flags & ATA_DFLAG_NCQ) {
1388352fab70SMark Lord 		if (sata_pmp_attached(adev->link->ap)) {
1389e49856d8SMark Lord 			adev->flags &= ~ATA_DFLAG_NCQ;
1390352fab70SMark Lord 			ata_dev_printk(adev, KERN_INFO,
1391352fab70SMark Lord 				"NCQ disabled for command-based switching\n");
1392352fab70SMark Lord 		}
1393f273827eSMark Lord 	}
1394e49856d8SMark Lord }
1395f273827eSMark Lord 
13963e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc)
13973e4a1391SMark Lord {
13983e4a1391SMark Lord 	struct ata_link *link = qc->dev->link;
13993e4a1391SMark Lord 	struct ata_port *ap = link->ap;
14003e4a1391SMark Lord 	struct mv_port_priv *pp = ap->private_data;
14013e4a1391SMark Lord 
14023e4a1391SMark Lord 	/*
140329d187bbSMark Lord 	 * Don't allow new commands if we're in a delayed EH state
140429d187bbSMark Lord 	 * for NCQ and/or FIS-based switching.
140529d187bbSMark Lord 	 */
140629d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
140729d187bbSMark Lord 		return ATA_DEFER_PORT;
1408159a7ff7SGwendal Grignou 
1409159a7ff7SGwendal Grignou 	/* PIO commands need exclusive link: no other commands [DMA or PIO]
1410159a7ff7SGwendal Grignou 	 * can run concurrently.
1411159a7ff7SGwendal Grignou 	 * set excl_link when we want to send a PIO command in DMA mode
1412159a7ff7SGwendal Grignou 	 * or a non-NCQ command in NCQ mode.
1413159a7ff7SGwendal Grignou 	 * When we receive a command from that link, and there are no
1414159a7ff7SGwendal Grignou 	 * outstanding commands, mark a flag to clear excl_link and let
1415159a7ff7SGwendal Grignou 	 * the command go through.
1416159a7ff7SGwendal Grignou 	 */
1417159a7ff7SGwendal Grignou 	if (unlikely(ap->excl_link)) {
1418159a7ff7SGwendal Grignou 		if (link == ap->excl_link) {
1419159a7ff7SGwendal Grignou 			if (ap->nr_active_links)
1420159a7ff7SGwendal Grignou 				return ATA_DEFER_PORT;
1421159a7ff7SGwendal Grignou 			qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
1422159a7ff7SGwendal Grignou 			return 0;
1423159a7ff7SGwendal Grignou 		} else
1424159a7ff7SGwendal Grignou 			return ATA_DEFER_PORT;
1425159a7ff7SGwendal Grignou 	}
1426159a7ff7SGwendal Grignou 
142729d187bbSMark Lord 	/*
14283e4a1391SMark Lord 	 * If the port is completely idle, then allow the new qc.
14293e4a1391SMark Lord 	 */
14303e4a1391SMark Lord 	if (ap->nr_active_links == 0)
14313e4a1391SMark Lord 		return 0;
14323e4a1391SMark Lord 
14333e4a1391SMark Lord 	/*
14344bdee6c5STejun Heo 	 * The port is operating in host queuing mode (EDMA) with NCQ
14354bdee6c5STejun Heo 	 * enabled, allow multiple NCQ commands.  EDMA also allows
14364bdee6c5STejun Heo 	 * queueing multiple DMA commands but libata core currently
14374bdee6c5STejun Heo 	 * doesn't allow it.
14383e4a1391SMark Lord 	 */
14394bdee6c5STejun Heo 	if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
1440159a7ff7SGwendal Grignou 	    (pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
1441159a7ff7SGwendal Grignou 		if (ata_is_ncq(qc->tf.protocol))
14423e4a1391SMark Lord 			return 0;
1443159a7ff7SGwendal Grignou 		else {
1444159a7ff7SGwendal Grignou 			ap->excl_link = link;
1445159a7ff7SGwendal Grignou 			return ATA_DEFER_PORT;
1446159a7ff7SGwendal Grignou 		}
1447159a7ff7SGwendal Grignou 	}
14484bdee6c5STejun Heo 
14493e4a1391SMark Lord 	return ATA_DEFER_PORT;
14503e4a1391SMark Lord }
14513e4a1391SMark Lord 
145208da1759SMark Lord static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
1453e49856d8SMark Lord {
145408da1759SMark Lord 	struct mv_port_priv *pp = ap->private_data;
145508da1759SMark Lord 	void __iomem *port_mmio;
145600f42eabSMark Lord 
145708da1759SMark Lord 	u32 fiscfg,   *old_fiscfg   = &pp->cached.fiscfg;
145808da1759SMark Lord 	u32 ltmode,   *old_ltmode   = &pp->cached.ltmode;
145908da1759SMark Lord 	u32 haltcond, *old_haltcond = &pp->cached.haltcond;
146000f42eabSMark Lord 
146108da1759SMark Lord 	ltmode   = *old_ltmode & ~LTMODE_BIT8;
146208da1759SMark Lord 	haltcond = *old_haltcond | EDMA_ERR_DEV;
146300f42eabSMark Lord 
146400f42eabSMark Lord 	if (want_fbs) {
146508da1759SMark Lord 		fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
146608da1759SMark Lord 		ltmode = *old_ltmode | LTMODE_BIT8;
14674c299ca3SMark Lord 		if (want_ncq)
146808da1759SMark Lord 			haltcond &= ~EDMA_ERR_DEV;
14694c299ca3SMark Lord 		else
147008da1759SMark Lord 			fiscfg |=  FISCFG_WAIT_DEV_ERR;
147108da1759SMark Lord 	} else {
147208da1759SMark Lord 		fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
1473e49856d8SMark Lord 	}
147400f42eabSMark Lord 
147508da1759SMark Lord 	port_mmio = mv_ap_base(ap);
1476cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + FISCFG, old_fiscfg, fiscfg);
1477cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + LTMODE, old_ltmode, ltmode);
1478cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + EDMA_HALTCOND, old_haltcond, haltcond);
1479e49856d8SMark Lord }
1480c6fd2807SJeff Garzik 
1481dd2890f6SMark Lord static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1482dd2890f6SMark Lord {
1483dd2890f6SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1484dd2890f6SMark Lord 	u32 old, new;
1485dd2890f6SMark Lord 
1486dd2890f6SMark Lord 	/* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1487cae5a29dSMark Lord 	old = readl(hpriv->base + GPIO_PORT_CTL);
1488dd2890f6SMark Lord 	if (want_ncq)
1489dd2890f6SMark Lord 		new = old | (1 << 22);
1490dd2890f6SMark Lord 	else
1491dd2890f6SMark Lord 		new = old & ~(1 << 22);
1492dd2890f6SMark Lord 	if (new != old)
1493cae5a29dSMark Lord 		writel(new, hpriv->base + GPIO_PORT_CTL);
1494dd2890f6SMark Lord }
1495dd2890f6SMark Lord 
1496c01e8a23SMark Lord /**
1497c01e8a23SMark Lord  *	mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
1498c01e8a23SMark Lord  *	@ap: Port being initialized
1499c01e8a23SMark Lord  *
1500c01e8a23SMark Lord  *	There are two DMA modes on these chips:  basic DMA, and EDMA.
1501c01e8a23SMark Lord  *
1502c01e8a23SMark Lord  *	Bit-0 of the "EDMA RESERVED" register enables/disables use
1503c01e8a23SMark Lord  *	of basic DMA on the GEN_IIE versions of the chips.
1504c01e8a23SMark Lord  *
1505c01e8a23SMark Lord  *	This bit survives EDMA resets, and must be set for basic DMA
1506c01e8a23SMark Lord  *	to function, and should be cleared when EDMA is active.
1507c01e8a23SMark Lord  */
1508c01e8a23SMark Lord static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
1509c01e8a23SMark Lord {
1510c01e8a23SMark Lord 	struct mv_port_priv *pp = ap->private_data;
1511c01e8a23SMark Lord 	u32 new, *old = &pp->cached.unknown_rsvd;
1512c01e8a23SMark Lord 
1513c01e8a23SMark Lord 	if (enable_bmdma)
1514c01e8a23SMark Lord 		new = *old | 1;
1515c01e8a23SMark Lord 	else
1516c01e8a23SMark Lord 		new = *old & ~1;
1517cae5a29dSMark Lord 	mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD, old, new);
1518c01e8a23SMark Lord }
1519c01e8a23SMark Lord 
1520000b344fSMark Lord /*
1521000b344fSMark Lord  * SOC chips have an issue whereby the HDD LEDs don't always blink
1522000b344fSMark Lord  * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
1523000b344fSMark Lord  * of the SOC takes care of it, generating a steady blink rate when
1524000b344fSMark Lord  * any drive on the chip is active.
1525000b344fSMark Lord  *
1526000b344fSMark Lord  * Unfortunately, the blink mode is a global hardware setting for the SOC,
1527000b344fSMark Lord  * so we must use it whenever at least one port on the SOC has NCQ enabled.
1528000b344fSMark Lord  *
1529000b344fSMark Lord  * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
1530000b344fSMark Lord  * LED operation works then, and provides better (more accurate) feedback.
1531000b344fSMark Lord  *
1532000b344fSMark Lord  * Note that this code assumes that an SOC never has more than one HC onboard.
1533000b344fSMark Lord  */
1534000b344fSMark Lord static void mv_soc_led_blink_enable(struct ata_port *ap)
1535000b344fSMark Lord {
1536000b344fSMark Lord 	struct ata_host *host = ap->host;
1537000b344fSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1538000b344fSMark Lord 	void __iomem *hc_mmio;
1539000b344fSMark Lord 	u32 led_ctrl;
1540000b344fSMark Lord 
1541000b344fSMark Lord 	if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
1542000b344fSMark Lord 		return;
1543000b344fSMark Lord 	hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN;
1544000b344fSMark Lord 	hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1545cae5a29dSMark Lord 	led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1546cae5a29dSMark Lord 	writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1547000b344fSMark Lord }
1548000b344fSMark Lord 
1549000b344fSMark Lord static void mv_soc_led_blink_disable(struct ata_port *ap)
1550000b344fSMark Lord {
1551000b344fSMark Lord 	struct ata_host *host = ap->host;
1552000b344fSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1553000b344fSMark Lord 	void __iomem *hc_mmio;
1554000b344fSMark Lord 	u32 led_ctrl;
1555000b344fSMark Lord 	unsigned int port;
1556000b344fSMark Lord 
1557000b344fSMark Lord 	if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
1558000b344fSMark Lord 		return;
1559000b344fSMark Lord 
1560000b344fSMark Lord 	/* disable led-blink only if no ports are using NCQ */
1561000b344fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
1562000b344fSMark Lord 		struct ata_port *this_ap = host->ports[port];
1563000b344fSMark Lord 		struct mv_port_priv *pp = this_ap->private_data;
1564000b344fSMark Lord 
1565000b344fSMark Lord 		if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1566000b344fSMark Lord 			return;
1567000b344fSMark Lord 	}
1568000b344fSMark Lord 
1569000b344fSMark Lord 	hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
1570000b344fSMark Lord 	hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1571cae5a29dSMark Lord 	led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1572cae5a29dSMark Lord 	writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1573000b344fSMark Lord }
1574000b344fSMark Lord 
157500b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
1576c6fd2807SJeff Garzik {
1577c6fd2807SJeff Garzik 	u32 cfg;
1578e12bef50SMark Lord 	struct mv_port_priv *pp    = ap->private_data;
1579e12bef50SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1580e12bef50SMark Lord 	void __iomem *port_mmio    = mv_ap_base(ap);
1581c6fd2807SJeff Garzik 
1582c6fd2807SJeff Garzik 	/* set up non-NCQ EDMA configuration */
1583c6fd2807SJeff Garzik 	cfg = EDMA_CFG_Q_DEPTH;		/* always 0x1f for *all* chips */
1584d16ab3f6SMark Lord 	pp->pp_flags &=
1585d16ab3f6SMark Lord 	  ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
1586c6fd2807SJeff Garzik 
1587c6fd2807SJeff Garzik 	if (IS_GEN_I(hpriv))
1588c6fd2807SJeff Garzik 		cfg |= (1 << 8);	/* enab config burst size mask */
1589c6fd2807SJeff Garzik 
1590dd2890f6SMark Lord 	else if (IS_GEN_II(hpriv)) {
1591c6fd2807SJeff Garzik 		cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1592dd2890f6SMark Lord 		mv_60x1_errata_sata25(ap, want_ncq);
1593c6fd2807SJeff Garzik 
1594dd2890f6SMark Lord 	} else if (IS_GEN_IIE(hpriv)) {
159500f42eabSMark Lord 		int want_fbs = sata_pmp_attached(ap);
159600f42eabSMark Lord 		/*
159700f42eabSMark Lord 		 * Possible future enhancement:
159800f42eabSMark Lord 		 *
159900f42eabSMark Lord 		 * The chip can use FBS with non-NCQ, if we allow it,
160000f42eabSMark Lord 		 * But first we need to have the error handling in place
160100f42eabSMark Lord 		 * for this mode (datasheet section 7.3.15.4.2.3).
160200f42eabSMark Lord 		 * So disallow non-NCQ FBS for now.
160300f42eabSMark Lord 		 */
160400f42eabSMark Lord 		want_fbs &= want_ncq;
160500f42eabSMark Lord 
160608da1759SMark Lord 		mv_config_fbs(ap, want_ncq, want_fbs);
160700f42eabSMark Lord 
160800f42eabSMark Lord 		if (want_fbs) {
160900f42eabSMark Lord 			pp->pp_flags |= MV_PP_FLAG_FBS_EN;
161000f42eabSMark Lord 			cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
161100f42eabSMark Lord 		}
161200f42eabSMark Lord 
1613e728eabeSJeff Garzik 		cfg |= (1 << 23);	/* do not mask PM field in rx'd FIS */
161400b81235SMark Lord 		if (want_edma) {
1615e728eabeSJeff Garzik 			cfg |= (1 << 22); /* enab 4-entry host queue cache */
16161f398472SMark Lord 			if (!IS_SOC(hpriv))
1617c6fd2807SJeff Garzik 				cfg |= (1 << 18); /* enab early completion */
161800b81235SMark Lord 		}
1619616d4a98SMark Lord 		if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1620616d4a98SMark Lord 			cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
1621c01e8a23SMark Lord 		mv_bmdma_enable_iie(ap, !want_edma);
1622000b344fSMark Lord 
1623000b344fSMark Lord 		if (IS_SOC(hpriv)) {
1624000b344fSMark Lord 			if (want_ncq)
1625000b344fSMark Lord 				mv_soc_led_blink_enable(ap);
1626000b344fSMark Lord 			else
1627000b344fSMark Lord 				mv_soc_led_blink_disable(ap);
1628000b344fSMark Lord 		}
1629c6fd2807SJeff Garzik 	}
1630c6fd2807SJeff Garzik 
163172109168SMark Lord 	if (want_ncq) {
163272109168SMark Lord 		cfg |= EDMA_CFG_NCQ;
163372109168SMark Lord 		pp->pp_flags |=  MV_PP_FLAG_NCQ_EN;
163400b81235SMark Lord 	}
163572109168SMark Lord 
1636cae5a29dSMark Lord 	writelfl(cfg, port_mmio + EDMA_CFG);
1637c6fd2807SJeff Garzik }
1638c6fd2807SJeff Garzik 
1639da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1640da2fa9baSMark Lord {
1641da2fa9baSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1642da2fa9baSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1643eb73d558SMark Lord 	int tag;
1644da2fa9baSMark Lord 
1645da2fa9baSMark Lord 	if (pp->crqb) {
1646da2fa9baSMark Lord 		dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1647da2fa9baSMark Lord 		pp->crqb = NULL;
1648da2fa9baSMark Lord 	}
1649da2fa9baSMark Lord 	if (pp->crpb) {
1650da2fa9baSMark Lord 		dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1651da2fa9baSMark Lord 		pp->crpb = NULL;
1652da2fa9baSMark Lord 	}
1653eb73d558SMark Lord 	/*
1654eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1655eb73d558SMark Lord 	 * For later hardware, we have one unique sg_tbl per NCQ tag.
1656eb73d558SMark Lord 	 */
1657eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1658eb73d558SMark Lord 		if (pp->sg_tbl[tag]) {
1659eb73d558SMark Lord 			if (tag == 0 || !IS_GEN_I(hpriv))
1660eb73d558SMark Lord 				dma_pool_free(hpriv->sg_tbl_pool,
1661eb73d558SMark Lord 					      pp->sg_tbl[tag],
1662eb73d558SMark Lord 					      pp->sg_tbl_dma[tag]);
1663eb73d558SMark Lord 			pp->sg_tbl[tag] = NULL;
1664eb73d558SMark Lord 		}
1665da2fa9baSMark Lord 	}
1666da2fa9baSMark Lord }
1667da2fa9baSMark Lord 
1668c6fd2807SJeff Garzik /**
1669c6fd2807SJeff Garzik  *      mv_port_start - Port specific init/start routine.
1670c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1671c6fd2807SJeff Garzik  *
1672c6fd2807SJeff Garzik  *      Allocate and point to DMA memory, init port private memory,
1673c6fd2807SJeff Garzik  *      zero indices.
1674c6fd2807SJeff Garzik  *
1675c6fd2807SJeff Garzik  *      LOCKING:
1676c6fd2807SJeff Garzik  *      Inherited from caller.
1677c6fd2807SJeff Garzik  */
1678c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1679c6fd2807SJeff Garzik {
1680cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1681cca3974eSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1682c6fd2807SJeff Garzik 	struct mv_port_priv *pp;
1683933cb8e5SMark Lord 	unsigned long flags;
1684dde20207SJames Bottomley 	int tag;
1685c6fd2807SJeff Garzik 
168624dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1687c6fd2807SJeff Garzik 	if (!pp)
168824dc5f33STejun Heo 		return -ENOMEM;
1689da2fa9baSMark Lord 	ap->private_data = pp;
1690c6fd2807SJeff Garzik 
1691da2fa9baSMark Lord 	pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1692da2fa9baSMark Lord 	if (!pp->crqb)
1693da2fa9baSMark Lord 		return -ENOMEM;
1694da2fa9baSMark Lord 	memset(pp->crqb, 0, MV_CRQB_Q_SZ);
1695c6fd2807SJeff Garzik 
1696da2fa9baSMark Lord 	pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1697da2fa9baSMark Lord 	if (!pp->crpb)
1698da2fa9baSMark Lord 		goto out_port_free_dma_mem;
1699da2fa9baSMark Lord 	memset(pp->crpb, 0, MV_CRPB_Q_SZ);
1700c6fd2807SJeff Garzik 
17013bd0a70eSMark Lord 	/* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
17023bd0a70eSMark Lord 	if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
17033bd0a70eSMark Lord 		ap->flags |= ATA_FLAG_AN;
1704eb73d558SMark Lord 	/*
1705eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1706eb73d558SMark Lord 	 * For later hardware, we need one unique sg_tbl per NCQ tag.
1707eb73d558SMark Lord 	 */
1708eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1709eb73d558SMark Lord 		if (tag == 0 || !IS_GEN_I(hpriv)) {
1710eb73d558SMark Lord 			pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1711eb73d558SMark Lord 					      GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1712eb73d558SMark Lord 			if (!pp->sg_tbl[tag])
1713da2fa9baSMark Lord 				goto out_port_free_dma_mem;
1714eb73d558SMark Lord 		} else {
1715eb73d558SMark Lord 			pp->sg_tbl[tag]     = pp->sg_tbl[0];
1716eb73d558SMark Lord 			pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1717eb73d558SMark Lord 		}
1718eb73d558SMark Lord 	}
1719933cb8e5SMark Lord 
1720933cb8e5SMark Lord 	spin_lock_irqsave(ap->lock, flags);
172108da1759SMark Lord 	mv_save_cached_regs(ap);
172266e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
1723933cb8e5SMark Lord 	spin_unlock_irqrestore(ap->lock, flags);
1724933cb8e5SMark Lord 
1725c6fd2807SJeff Garzik 	return 0;
1726da2fa9baSMark Lord 
1727da2fa9baSMark Lord out_port_free_dma_mem:
1728da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1729da2fa9baSMark Lord 	return -ENOMEM;
1730c6fd2807SJeff Garzik }
1731c6fd2807SJeff Garzik 
1732c6fd2807SJeff Garzik /**
1733c6fd2807SJeff Garzik  *      mv_port_stop - Port specific cleanup/stop routine.
1734c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1735c6fd2807SJeff Garzik  *
1736c6fd2807SJeff Garzik  *      Stop DMA, cleanup port memory.
1737c6fd2807SJeff Garzik  *
1738c6fd2807SJeff Garzik  *      LOCKING:
1739cca3974eSJeff Garzik  *      This routine uses the host lock to protect the DMA stop.
1740c6fd2807SJeff Garzik  */
1741c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1742c6fd2807SJeff Garzik {
1743933cb8e5SMark Lord 	unsigned long flags;
1744933cb8e5SMark Lord 
1745933cb8e5SMark Lord 	spin_lock_irqsave(ap->lock, flags);
1746e12bef50SMark Lord 	mv_stop_edma(ap);
174788e675e1SMark Lord 	mv_enable_port_irqs(ap, 0);
1748933cb8e5SMark Lord 	spin_unlock_irqrestore(ap->lock, flags);
1749da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1750c6fd2807SJeff Garzik }
1751c6fd2807SJeff Garzik 
1752c6fd2807SJeff Garzik /**
1753c6fd2807SJeff Garzik  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1754c6fd2807SJeff Garzik  *      @qc: queued command whose SG list to source from
1755c6fd2807SJeff Garzik  *
1756c6fd2807SJeff Garzik  *      Populate the SG list and mark the last entry.
1757c6fd2807SJeff Garzik  *
1758c6fd2807SJeff Garzik  *      LOCKING:
1759c6fd2807SJeff Garzik  *      Inherited from caller.
1760c6fd2807SJeff Garzik  */
17616c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1762c6fd2807SJeff Garzik {
1763c6fd2807SJeff Garzik 	struct mv_port_priv *pp = qc->ap->private_data;
1764c6fd2807SJeff Garzik 	struct scatterlist *sg;
17653be6cbd7SJeff Garzik 	struct mv_sg *mv_sg, *last_sg = NULL;
1766ff2aeb1eSTejun Heo 	unsigned int si;
1767c6fd2807SJeff Garzik 
1768eb73d558SMark Lord 	mv_sg = pp->sg_tbl[qc->tag];
1769ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1770d88184fbSJeff Garzik 		dma_addr_t addr = sg_dma_address(sg);
1771d88184fbSJeff Garzik 		u32 sg_len = sg_dma_len(sg);
1772c6fd2807SJeff Garzik 
17734007b493SOlof Johansson 		while (sg_len) {
17744007b493SOlof Johansson 			u32 offset = addr & 0xffff;
17754007b493SOlof Johansson 			u32 len = sg_len;
17764007b493SOlof Johansson 
177732cd11a6SMark Lord 			if (offset + len > 0x10000)
17784007b493SOlof Johansson 				len = 0x10000 - offset;
17794007b493SOlof Johansson 
1780d88184fbSJeff Garzik 			mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1781d88184fbSJeff Garzik 			mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
17826c08772eSJeff Garzik 			mv_sg->flags_size = cpu_to_le32(len & 0xffff);
178332cd11a6SMark Lord 			mv_sg->reserved = 0;
1784c6fd2807SJeff Garzik 
17854007b493SOlof Johansson 			sg_len -= len;
17864007b493SOlof Johansson 			addr += len;
17874007b493SOlof Johansson 
17883be6cbd7SJeff Garzik 			last_sg = mv_sg;
1789d88184fbSJeff Garzik 			mv_sg++;
1790c6fd2807SJeff Garzik 		}
17914007b493SOlof Johansson 	}
17923be6cbd7SJeff Garzik 
17933be6cbd7SJeff Garzik 	if (likely(last_sg))
17943be6cbd7SJeff Garzik 		last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
179532cd11a6SMark Lord 	mb(); /* ensure data structure is visible to the chipset */
1796c6fd2807SJeff Garzik }
1797c6fd2807SJeff Garzik 
17985796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1799c6fd2807SJeff Garzik {
1800c6fd2807SJeff Garzik 	u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1801c6fd2807SJeff Garzik 		(last ? CRQB_CMD_LAST : 0);
1802c6fd2807SJeff Garzik 	*cmdw = cpu_to_le16(tmp);
1803c6fd2807SJeff Garzik }
1804c6fd2807SJeff Garzik 
1805c6fd2807SJeff Garzik /**
1806da14265eSMark Lord  *	mv_sff_irq_clear - Clear hardware interrupt after DMA.
1807da14265eSMark Lord  *	@ap: Port associated with this ATA transaction.
1808da14265eSMark Lord  *
1809da14265eSMark Lord  *	We need this only for ATAPI bmdma transactions,
1810da14265eSMark Lord  *	as otherwise we experience spurious interrupts
1811da14265eSMark Lord  *	after libata-sff handles the bmdma interrupts.
1812da14265eSMark Lord  */
1813da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap)
1814da14265eSMark Lord {
1815da14265eSMark Lord 	mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
1816da14265eSMark Lord }
1817da14265eSMark Lord 
1818da14265eSMark Lord /**
1819da14265eSMark Lord  *	mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
1820da14265eSMark Lord  *	@qc: queued command to check for chipset/DMA compatibility.
1821da14265eSMark Lord  *
1822da14265eSMark Lord  *	The bmdma engines cannot handle speculative data sizes
1823da14265eSMark Lord  *	(bytecount under/over flow).  So only allow DMA for
1824da14265eSMark Lord  *	data transfer commands with known data sizes.
1825da14265eSMark Lord  *
1826da14265eSMark Lord  *	LOCKING:
1827da14265eSMark Lord  *	Inherited from caller.
1828da14265eSMark Lord  */
1829da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
1830da14265eSMark Lord {
1831da14265eSMark Lord 	struct scsi_cmnd *scmd = qc->scsicmd;
1832da14265eSMark Lord 
1833da14265eSMark Lord 	if (scmd) {
1834da14265eSMark Lord 		switch (scmd->cmnd[0]) {
1835da14265eSMark Lord 		case READ_6:
1836da14265eSMark Lord 		case READ_10:
1837da14265eSMark Lord 		case READ_12:
1838da14265eSMark Lord 		case WRITE_6:
1839da14265eSMark Lord 		case WRITE_10:
1840da14265eSMark Lord 		case WRITE_12:
1841da14265eSMark Lord 		case GPCMD_READ_CD:
1842da14265eSMark Lord 		case GPCMD_SEND_DVD_STRUCTURE:
1843da14265eSMark Lord 		case GPCMD_SEND_CUE_SHEET:
1844da14265eSMark Lord 			return 0; /* DMA is safe */
1845da14265eSMark Lord 		}
1846da14265eSMark Lord 	}
1847da14265eSMark Lord 	return -EOPNOTSUPP; /* use PIO instead */
1848da14265eSMark Lord }
1849da14265eSMark Lord 
1850da14265eSMark Lord /**
1851da14265eSMark Lord  *	mv_bmdma_setup - Set up BMDMA transaction
1852da14265eSMark Lord  *	@qc: queued command to prepare DMA for.
1853da14265eSMark Lord  *
1854da14265eSMark Lord  *	LOCKING:
1855da14265eSMark Lord  *	Inherited from caller.
1856da14265eSMark Lord  */
1857da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc)
1858da14265eSMark Lord {
1859da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1860da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1861da14265eSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1862da14265eSMark Lord 
1863da14265eSMark Lord 	mv_fill_sg(qc);
1864da14265eSMark Lord 
1865da14265eSMark Lord 	/* clear all DMA cmd bits */
1866cae5a29dSMark Lord 	writel(0, port_mmio + BMDMA_CMD);
1867da14265eSMark Lord 
1868da14265eSMark Lord 	/* load PRD table addr. */
1869da14265eSMark Lord 	writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
1870cae5a29dSMark Lord 		port_mmio + BMDMA_PRD_HIGH);
1871da14265eSMark Lord 	writelfl(pp->sg_tbl_dma[qc->tag],
1872cae5a29dSMark Lord 		port_mmio + BMDMA_PRD_LOW);
1873da14265eSMark Lord 
1874da14265eSMark Lord 	/* issue r/w command */
1875da14265eSMark Lord 	ap->ops->sff_exec_command(ap, &qc->tf);
1876da14265eSMark Lord }
1877da14265eSMark Lord 
1878da14265eSMark Lord /**
1879da14265eSMark Lord  *	mv_bmdma_start - Start a BMDMA transaction
1880da14265eSMark Lord  *	@qc: queued command to start DMA on.
1881da14265eSMark Lord  *
1882da14265eSMark Lord  *	LOCKING:
1883da14265eSMark Lord  *	Inherited from caller.
1884da14265eSMark Lord  */
1885da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc)
1886da14265eSMark Lord {
1887da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1888da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1889da14265eSMark Lord 	unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
1890da14265eSMark Lord 	u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
1891da14265eSMark Lord 
1892da14265eSMark Lord 	/* start host DMA transaction */
1893cae5a29dSMark Lord 	writelfl(cmd, port_mmio + BMDMA_CMD);
1894da14265eSMark Lord }
1895da14265eSMark Lord 
1896da14265eSMark Lord /**
1897da14265eSMark Lord  *	mv_bmdma_stop - Stop BMDMA transfer
1898da14265eSMark Lord  *	@qc: queued command to stop DMA on.
1899da14265eSMark Lord  *
1900da14265eSMark Lord  *	Clears the ATA_DMA_START flag in the bmdma control register
1901da14265eSMark Lord  *
1902da14265eSMark Lord  *	LOCKING:
1903da14265eSMark Lord  *	Inherited from caller.
1904da14265eSMark Lord  */
1905da14265eSMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc)
1906da14265eSMark Lord {
1907da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1908da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1909da14265eSMark Lord 	u32 cmd;
1910da14265eSMark Lord 
1911da14265eSMark Lord 	/* clear start/stop bit */
1912cae5a29dSMark Lord 	cmd = readl(port_mmio + BMDMA_CMD);
1913da14265eSMark Lord 	cmd &= ~ATA_DMA_START;
1914cae5a29dSMark Lord 	writelfl(cmd, port_mmio + BMDMA_CMD);
1915da14265eSMark Lord 
1916da14265eSMark Lord 	/* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
1917da14265eSMark Lord 	ata_sff_dma_pause(ap);
1918da14265eSMark Lord }
1919da14265eSMark Lord 
1920da14265eSMark Lord /**
1921da14265eSMark Lord  *	mv_bmdma_status - Read BMDMA status
1922da14265eSMark Lord  *	@ap: port for which to retrieve DMA status.
1923da14265eSMark Lord  *
1924da14265eSMark Lord  *	Read and return equivalent of the sff BMDMA status register.
1925da14265eSMark Lord  *
1926da14265eSMark Lord  *	LOCKING:
1927da14265eSMark Lord  *	Inherited from caller.
1928da14265eSMark Lord  */
1929da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap)
1930da14265eSMark Lord {
1931da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1932da14265eSMark Lord 	u32 reg, status;
1933da14265eSMark Lord 
1934da14265eSMark Lord 	/*
1935da14265eSMark Lord 	 * Other bits are valid only if ATA_DMA_ACTIVE==0,
1936da14265eSMark Lord 	 * and the ATA_DMA_INTR bit doesn't exist.
1937da14265eSMark Lord 	 */
1938cae5a29dSMark Lord 	reg = readl(port_mmio + BMDMA_STATUS);
1939da14265eSMark Lord 	if (reg & ATA_DMA_ACTIVE)
1940da14265eSMark Lord 		status = ATA_DMA_ACTIVE;
1941da14265eSMark Lord 	else
1942da14265eSMark Lord 		status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
1943da14265eSMark Lord 	return status;
1944da14265eSMark Lord }
1945da14265eSMark Lord 
1946299b3f8dSMark Lord static void mv_rw_multi_errata_sata24(struct ata_queued_cmd *qc)
1947299b3f8dSMark Lord {
1948299b3f8dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
1949299b3f8dSMark Lord 	/*
1950299b3f8dSMark Lord 	 * Workaround for 88SX60x1 FEr SATA#24.
1951299b3f8dSMark Lord 	 *
1952299b3f8dSMark Lord 	 * Chip may corrupt WRITEs if multi_count >= 4kB.
1953299b3f8dSMark Lord 	 * Note that READs are unaffected.
1954299b3f8dSMark Lord 	 *
1955299b3f8dSMark Lord 	 * It's not clear if this errata really means "4K bytes",
1956299b3f8dSMark Lord 	 * or if it always happens for multi_count > 7
1957299b3f8dSMark Lord 	 * regardless of device sector_size.
1958299b3f8dSMark Lord 	 *
1959299b3f8dSMark Lord 	 * So, for safety, any write with multi_count > 7
1960299b3f8dSMark Lord 	 * gets converted here into a regular PIO write instead:
1961299b3f8dSMark Lord 	 */
1962299b3f8dSMark Lord 	if ((tf->flags & ATA_TFLAG_WRITE) && is_multi_taskfile(tf)) {
1963299b3f8dSMark Lord 		if (qc->dev->multi_count > 7) {
1964299b3f8dSMark Lord 			switch (tf->command) {
1965299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI:
1966299b3f8dSMark Lord 				tf->command = ATA_CMD_PIO_WRITE;
1967299b3f8dSMark Lord 				break;
1968299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI_FUA_EXT:
1969299b3f8dSMark Lord 				tf->flags &= ~ATA_TFLAG_FUA; /* ugh */
1970299b3f8dSMark Lord 				/* fall through */
1971299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI_EXT:
1972299b3f8dSMark Lord 				tf->command = ATA_CMD_PIO_WRITE_EXT;
1973299b3f8dSMark Lord 				break;
1974299b3f8dSMark Lord 			}
1975299b3f8dSMark Lord 		}
1976299b3f8dSMark Lord 	}
1977299b3f8dSMark Lord }
1978299b3f8dSMark Lord 
1979da14265eSMark Lord /**
1980c6fd2807SJeff Garzik  *      mv_qc_prep - Host specific command preparation.
1981c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1982c6fd2807SJeff Garzik  *
1983c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1984c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1985c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1986c6fd2807SJeff Garzik  *      the SG load routine.
1987c6fd2807SJeff Garzik  *
1988c6fd2807SJeff Garzik  *      LOCKING:
1989c6fd2807SJeff Garzik  *      Inherited from caller.
1990c6fd2807SJeff Garzik  */
1991c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc)
1992c6fd2807SJeff Garzik {
1993c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1994c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1995c6fd2807SJeff Garzik 	__le16 *cw;
19968d2b450dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
1997c6fd2807SJeff Garzik 	u16 flags = 0;
1998c6fd2807SJeff Garzik 	unsigned in_index;
1999c6fd2807SJeff Garzik 
2000299b3f8dSMark Lord 	switch (tf->protocol) {
2001299b3f8dSMark Lord 	case ATA_PROT_DMA:
2002299b3f8dSMark Lord 	case ATA_PROT_NCQ:
2003299b3f8dSMark Lord 		break;	/* continue below */
2004299b3f8dSMark Lord 	case ATA_PROT_PIO:
2005299b3f8dSMark Lord 		mv_rw_multi_errata_sata24(qc);
2006c6fd2807SJeff Garzik 		return;
2007299b3f8dSMark Lord 	default:
2008299b3f8dSMark Lord 		return;
2009299b3f8dSMark Lord 	}
2010c6fd2807SJeff Garzik 
2011c6fd2807SJeff Garzik 	/* Fill in command request block
2012c6fd2807SJeff Garzik 	 */
20138d2b450dSMark Lord 	if (!(tf->flags & ATA_TFLAG_WRITE))
2014c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
2015c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
2016c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
2017e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2018c6fd2807SJeff Garzik 
2019bdd4dddeSJeff Garzik 	/* get current queue index from software */
2020fcfb1f77SMark Lord 	in_index = pp->req_idx;
2021c6fd2807SJeff Garzik 
2022c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr =
2023eb73d558SMark Lord 		cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
2024c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr_hi =
2025eb73d558SMark Lord 		cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
2026c6fd2807SJeff Garzik 	pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
2027c6fd2807SJeff Garzik 
2028c6fd2807SJeff Garzik 	cw = &pp->crqb[in_index].ata_cmd[0];
2029c6fd2807SJeff Garzik 
2030c6fd2807SJeff Garzik 	/* Sadly, the CRQB cannot accomodate all registers--there are
2031c6fd2807SJeff Garzik 	 * only 11 bytes...so we must pick and choose required
2032c6fd2807SJeff Garzik 	 * registers based on the command.  So, we drop feature and
2033c6fd2807SJeff Garzik 	 * hob_feature for [RW] DMA commands, but they are needed for
2034cd12e1f7SMark Lord 	 * NCQ.  NCQ will drop hob_nsect, which is not needed there
2035cd12e1f7SMark Lord 	 * (nsect is used only for the tag; feat/hob_feat hold true nsect).
2036c6fd2807SJeff Garzik 	 */
2037c6fd2807SJeff Garzik 	switch (tf->command) {
2038c6fd2807SJeff Garzik 	case ATA_CMD_READ:
2039c6fd2807SJeff Garzik 	case ATA_CMD_READ_EXT:
2040c6fd2807SJeff Garzik 	case ATA_CMD_WRITE:
2041c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_EXT:
2042c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_FUA_EXT:
2043c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
2044c6fd2807SJeff Garzik 		break;
2045c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_READ:
2046c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_WRITE:
2047c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
2048c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
2049c6fd2807SJeff Garzik 		break;
2050c6fd2807SJeff Garzik 	default:
2051c6fd2807SJeff Garzik 		/* The only other commands EDMA supports in non-queued and
2052c6fd2807SJeff Garzik 		 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
2053c6fd2807SJeff Garzik 		 * of which are defined/used by Linux.  If we get here, this
2054c6fd2807SJeff Garzik 		 * driver needs work.
2055c6fd2807SJeff Garzik 		 *
2056c6fd2807SJeff Garzik 		 * FIXME: modify libata to give qc_prep a return value and
2057c6fd2807SJeff Garzik 		 * return error here.
2058c6fd2807SJeff Garzik 		 */
2059c6fd2807SJeff Garzik 		BUG_ON(tf->command);
2060c6fd2807SJeff Garzik 		break;
2061c6fd2807SJeff Garzik 	}
2062c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
2063c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
2064c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
2065c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
2066c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
2067c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
2068c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
2069c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
2070c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);	/* last */
2071c6fd2807SJeff Garzik 
2072c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2073c6fd2807SJeff Garzik 		return;
2074c6fd2807SJeff Garzik 	mv_fill_sg(qc);
2075c6fd2807SJeff Garzik }
2076c6fd2807SJeff Garzik 
2077c6fd2807SJeff Garzik /**
2078c6fd2807SJeff Garzik  *      mv_qc_prep_iie - Host specific command preparation.
2079c6fd2807SJeff Garzik  *      @qc: queued command to prepare
2080c6fd2807SJeff Garzik  *
2081c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2082c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
2083c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
2084c6fd2807SJeff Garzik  *      the SG load routine.
2085c6fd2807SJeff Garzik  *
2086c6fd2807SJeff Garzik  *      LOCKING:
2087c6fd2807SJeff Garzik  *      Inherited from caller.
2088c6fd2807SJeff Garzik  */
2089c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
2090c6fd2807SJeff Garzik {
2091c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
2092c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2093c6fd2807SJeff Garzik 	struct mv_crqb_iie *crqb;
20948d2b450dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
2095c6fd2807SJeff Garzik 	unsigned in_index;
2096c6fd2807SJeff Garzik 	u32 flags = 0;
2097c6fd2807SJeff Garzik 
20988d2b450dSMark Lord 	if ((tf->protocol != ATA_PROT_DMA) &&
20998d2b450dSMark Lord 	    (tf->protocol != ATA_PROT_NCQ))
2100c6fd2807SJeff Garzik 		return;
2101c6fd2807SJeff Garzik 
2102e12bef50SMark Lord 	/* Fill in Gen IIE command request block */
21038d2b450dSMark Lord 	if (!(tf->flags & ATA_TFLAG_WRITE))
2104c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
2105c6fd2807SJeff Garzik 
2106c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
2107c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
21088c0aeb4aSMark Lord 	flags |= qc->tag << CRQB_HOSTQ_SHIFT;
2109e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2110c6fd2807SJeff Garzik 
2111bdd4dddeSJeff Garzik 	/* get current queue index from software */
2112fcfb1f77SMark Lord 	in_index = pp->req_idx;
2113c6fd2807SJeff Garzik 
2114c6fd2807SJeff Garzik 	crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
2115eb73d558SMark Lord 	crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
2116eb73d558SMark Lord 	crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
2117c6fd2807SJeff Garzik 	crqb->flags = cpu_to_le32(flags);
2118c6fd2807SJeff Garzik 
2119c6fd2807SJeff Garzik 	crqb->ata_cmd[0] = cpu_to_le32(
2120c6fd2807SJeff Garzik 			(tf->command << 16) |
2121c6fd2807SJeff Garzik 			(tf->feature << 24)
2122c6fd2807SJeff Garzik 		);
2123c6fd2807SJeff Garzik 	crqb->ata_cmd[1] = cpu_to_le32(
2124c6fd2807SJeff Garzik 			(tf->lbal << 0) |
2125c6fd2807SJeff Garzik 			(tf->lbam << 8) |
2126c6fd2807SJeff Garzik 			(tf->lbah << 16) |
2127c6fd2807SJeff Garzik 			(tf->device << 24)
2128c6fd2807SJeff Garzik 		);
2129c6fd2807SJeff Garzik 	crqb->ata_cmd[2] = cpu_to_le32(
2130c6fd2807SJeff Garzik 			(tf->hob_lbal << 0) |
2131c6fd2807SJeff Garzik 			(tf->hob_lbam << 8) |
2132c6fd2807SJeff Garzik 			(tf->hob_lbah << 16) |
2133c6fd2807SJeff Garzik 			(tf->hob_feature << 24)
2134c6fd2807SJeff Garzik 		);
2135c6fd2807SJeff Garzik 	crqb->ata_cmd[3] = cpu_to_le32(
2136c6fd2807SJeff Garzik 			(tf->nsect << 0) |
2137c6fd2807SJeff Garzik 			(tf->hob_nsect << 8)
2138c6fd2807SJeff Garzik 		);
2139c6fd2807SJeff Garzik 
2140c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2141c6fd2807SJeff Garzik 		return;
2142c6fd2807SJeff Garzik 	mv_fill_sg(qc);
2143c6fd2807SJeff Garzik }
2144c6fd2807SJeff Garzik 
2145c6fd2807SJeff Garzik /**
2146d16ab3f6SMark Lord  *	mv_sff_check_status - fetch device status, if valid
2147d16ab3f6SMark Lord  *	@ap: ATA port to fetch status from
2148d16ab3f6SMark Lord  *
2149d16ab3f6SMark Lord  *	When using command issue via mv_qc_issue_fis(),
2150d16ab3f6SMark Lord  *	the initial ATA_BUSY state does not show up in the
2151d16ab3f6SMark Lord  *	ATA status (shadow) register.  This can confuse libata!
2152d16ab3f6SMark Lord  *
2153d16ab3f6SMark Lord  *	So we have a hook here to fake ATA_BUSY for that situation,
2154d16ab3f6SMark Lord  *	until the first time a BUSY, DRQ, or ERR bit is seen.
2155d16ab3f6SMark Lord  *
2156d16ab3f6SMark Lord  *	The rest of the time, it simply returns the ATA status register.
2157d16ab3f6SMark Lord  */
2158d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap)
2159d16ab3f6SMark Lord {
2160d16ab3f6SMark Lord 	u8 stat = ioread8(ap->ioaddr.status_addr);
2161d16ab3f6SMark Lord 	struct mv_port_priv *pp = ap->private_data;
2162d16ab3f6SMark Lord 
2163d16ab3f6SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
2164d16ab3f6SMark Lord 		if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
2165d16ab3f6SMark Lord 			pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
2166d16ab3f6SMark Lord 		else
2167d16ab3f6SMark Lord 			stat = ATA_BUSY;
2168d16ab3f6SMark Lord 	}
2169d16ab3f6SMark Lord 	return stat;
2170d16ab3f6SMark Lord }
2171d16ab3f6SMark Lord 
2172d16ab3f6SMark Lord /**
217370f8b79cSMark Lord  *	mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
217470f8b79cSMark Lord  *	@fis: fis to be sent
217570f8b79cSMark Lord  *	@nwords: number of 32-bit words in the fis
217670f8b79cSMark Lord  */
217770f8b79cSMark Lord static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
217870f8b79cSMark Lord {
217970f8b79cSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
218070f8b79cSMark Lord 	u32 ifctl, old_ifctl, ifstat;
218170f8b79cSMark Lord 	int i, timeout = 200, final_word = nwords - 1;
218270f8b79cSMark Lord 
218370f8b79cSMark Lord 	/* Initiate FIS transmission mode */
2184cae5a29dSMark Lord 	old_ifctl = readl(port_mmio + SATA_IFCTL);
218570f8b79cSMark Lord 	ifctl = 0x100 | (old_ifctl & 0xf);
2186cae5a29dSMark Lord 	writelfl(ifctl, port_mmio + SATA_IFCTL);
218770f8b79cSMark Lord 
218870f8b79cSMark Lord 	/* Send all words of the FIS except for the final word */
218970f8b79cSMark Lord 	for (i = 0; i < final_word; ++i)
2190cae5a29dSMark Lord 		writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS);
219170f8b79cSMark Lord 
219270f8b79cSMark Lord 	/* Flag end-of-transmission, and then send the final word */
2193cae5a29dSMark Lord 	writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL);
2194cae5a29dSMark Lord 	writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS);
219570f8b79cSMark Lord 
219670f8b79cSMark Lord 	/*
219770f8b79cSMark Lord 	 * Wait for FIS transmission to complete.
219870f8b79cSMark Lord 	 * This typically takes just a single iteration.
219970f8b79cSMark Lord 	 */
220070f8b79cSMark Lord 	do {
2201cae5a29dSMark Lord 		ifstat = readl(port_mmio + SATA_IFSTAT);
220270f8b79cSMark Lord 	} while (!(ifstat & 0x1000) && --timeout);
220370f8b79cSMark Lord 
220470f8b79cSMark Lord 	/* Restore original port configuration */
2205cae5a29dSMark Lord 	writelfl(old_ifctl, port_mmio + SATA_IFCTL);
220670f8b79cSMark Lord 
220770f8b79cSMark Lord 	/* See if it worked */
220870f8b79cSMark Lord 	if ((ifstat & 0x3000) != 0x1000) {
220970f8b79cSMark Lord 		ata_port_printk(ap, KERN_WARNING,
221070f8b79cSMark Lord 				"%s transmission error, ifstat=%08x\n",
221170f8b79cSMark Lord 				__func__, ifstat);
221270f8b79cSMark Lord 		return AC_ERR_OTHER;
221370f8b79cSMark Lord 	}
221470f8b79cSMark Lord 	return 0;
221570f8b79cSMark Lord }
221670f8b79cSMark Lord 
221770f8b79cSMark Lord /**
221870f8b79cSMark Lord  *	mv_qc_issue_fis - Issue a command directly as a FIS
221970f8b79cSMark Lord  *	@qc: queued command to start
222070f8b79cSMark Lord  *
222170f8b79cSMark Lord  *	Note that the ATA shadow registers are not updated
222270f8b79cSMark Lord  *	after command issue, so the device will appear "READY"
222370f8b79cSMark Lord  *	if polled, even while it is BUSY processing the command.
222470f8b79cSMark Lord  *
222570f8b79cSMark Lord  *	So we use a status hook to fake ATA_BUSY until the drive changes state.
222670f8b79cSMark Lord  *
222770f8b79cSMark Lord  *	Note: we don't get updated shadow regs on *completion*
222870f8b79cSMark Lord  *	of non-data commands. So avoid sending them via this function,
222970f8b79cSMark Lord  *	as they will appear to have completed immediately.
223070f8b79cSMark Lord  *
223170f8b79cSMark Lord  *	GEN_IIE has special registers that we could get the result tf from,
223270f8b79cSMark Lord  *	but earlier chipsets do not.  For now, we ignore those registers.
223370f8b79cSMark Lord  */
223470f8b79cSMark Lord static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
223570f8b79cSMark Lord {
223670f8b79cSMark Lord 	struct ata_port *ap = qc->ap;
223770f8b79cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
223870f8b79cSMark Lord 	struct ata_link *link = qc->dev->link;
223970f8b79cSMark Lord 	u32 fis[5];
224070f8b79cSMark Lord 	int err = 0;
224170f8b79cSMark Lord 
224270f8b79cSMark Lord 	ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
22434c4a90fdSThiago Farina 	err = mv_send_fis(ap, fis, ARRAY_SIZE(fis));
224470f8b79cSMark Lord 	if (err)
224570f8b79cSMark Lord 		return err;
224670f8b79cSMark Lord 
224770f8b79cSMark Lord 	switch (qc->tf.protocol) {
224870f8b79cSMark Lord 	case ATAPI_PROT_PIO:
224970f8b79cSMark Lord 		pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
225070f8b79cSMark Lord 		/* fall through */
225170f8b79cSMark Lord 	case ATAPI_PROT_NODATA:
225270f8b79cSMark Lord 		ap->hsm_task_state = HSM_ST_FIRST;
225370f8b79cSMark Lord 		break;
225470f8b79cSMark Lord 	case ATA_PROT_PIO:
225570f8b79cSMark Lord 		pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
225670f8b79cSMark Lord 		if (qc->tf.flags & ATA_TFLAG_WRITE)
225770f8b79cSMark Lord 			ap->hsm_task_state = HSM_ST_FIRST;
225870f8b79cSMark Lord 		else
225970f8b79cSMark Lord 			ap->hsm_task_state = HSM_ST;
226070f8b79cSMark Lord 		break;
226170f8b79cSMark Lord 	default:
226270f8b79cSMark Lord 		ap->hsm_task_state = HSM_ST_LAST;
226370f8b79cSMark Lord 		break;
226470f8b79cSMark Lord 	}
226570f8b79cSMark Lord 
226670f8b79cSMark Lord 	if (qc->tf.flags & ATA_TFLAG_POLLING)
226770f8b79cSMark Lord 		ata_pio_queue_task(ap, qc, 0);
226870f8b79cSMark Lord 	return 0;
226970f8b79cSMark Lord }
227070f8b79cSMark Lord 
227170f8b79cSMark Lord /**
2272c6fd2807SJeff Garzik  *      mv_qc_issue - Initiate a command to the host
2273c6fd2807SJeff Garzik  *      @qc: queued command to start
2274c6fd2807SJeff Garzik  *
2275c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2276c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it sanity checks our local
2277c6fd2807SJeff Garzik  *      caches of the request producer/consumer indices then enables
2278c6fd2807SJeff Garzik  *      DMA and bumps the request producer index.
2279c6fd2807SJeff Garzik  *
2280c6fd2807SJeff Garzik  *      LOCKING:
2281c6fd2807SJeff Garzik  *      Inherited from caller.
2282c6fd2807SJeff Garzik  */
2283c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
2284c6fd2807SJeff Garzik {
2285f48765ccSMark Lord 	static int limit_warnings = 10;
2286c5d3e45aSJeff Garzik 	struct ata_port *ap = qc->ap;
2287c5d3e45aSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2288c5d3e45aSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2289bdd4dddeSJeff Garzik 	u32 in_index;
229042ed893dSMark Lord 	unsigned int port_irqs;
2291c6fd2807SJeff Garzik 
2292d16ab3f6SMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
2293d16ab3f6SMark Lord 
2294f48765ccSMark Lord 	switch (qc->tf.protocol) {
2295f48765ccSMark Lord 	case ATA_PROT_DMA:
2296f48765ccSMark Lord 	case ATA_PROT_NCQ:
2297f48765ccSMark Lord 		mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
2298f48765ccSMark Lord 		pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2299f48765ccSMark Lord 		in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
2300f48765ccSMark Lord 
2301f48765ccSMark Lord 		/* Write the request in pointer to kick the EDMA to life */
2302f48765ccSMark Lord 		writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
2303cae5a29dSMark Lord 					port_mmio + EDMA_REQ_Q_IN_PTR);
2304f48765ccSMark Lord 		return 0;
2305f48765ccSMark Lord 
2306f48765ccSMark Lord 	case ATA_PROT_PIO:
2307c6112bd8SMark Lord 		/*
2308c6112bd8SMark Lord 		 * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
2309c6112bd8SMark Lord 		 *
2310c6112bd8SMark Lord 		 * Someday, we might implement special polling workarounds
2311c6112bd8SMark Lord 		 * for these, but it all seems rather unnecessary since we
2312c6112bd8SMark Lord 		 * normally use only DMA for commands which transfer more
2313c6112bd8SMark Lord 		 * than a single block of data.
2314c6112bd8SMark Lord 		 *
2315c6112bd8SMark Lord 		 * Much of the time, this could just work regardless.
2316c6112bd8SMark Lord 		 * So for now, just log the incident, and allow the attempt.
2317c6112bd8SMark Lord 		 */
2318c7843e8fSMark Lord 		if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
2319c6112bd8SMark Lord 			--limit_warnings;
2320c6112bd8SMark Lord 			ata_link_printk(qc->dev->link, KERN_WARNING, DRV_NAME
2321c6112bd8SMark Lord 					": attempting PIO w/multiple DRQ: "
2322c6112bd8SMark Lord 					"this may fail due to h/w errata\n");
2323c6112bd8SMark Lord 		}
2324f48765ccSMark Lord 		/* drop through */
232542ed893dSMark Lord 	case ATA_PROT_NODATA:
2326f48765ccSMark Lord 	case ATAPI_PROT_PIO:
232742ed893dSMark Lord 	case ATAPI_PROT_NODATA:
232842ed893dSMark Lord 		if (ap->flags & ATA_FLAG_PIO_POLLING)
232942ed893dSMark Lord 			qc->tf.flags |= ATA_TFLAG_POLLING;
233042ed893dSMark Lord 		break;
233142ed893dSMark Lord 	}
233242ed893dSMark Lord 
233342ed893dSMark Lord 	if (qc->tf.flags & ATA_TFLAG_POLLING)
233442ed893dSMark Lord 		port_irqs = ERR_IRQ;	/* mask device interrupt when polling */
233542ed893dSMark Lord 	else
233642ed893dSMark Lord 		port_irqs = ERR_IRQ | DONE_IRQ;	/* unmask all interrupts */
233742ed893dSMark Lord 
233817c5aab5SMark Lord 	/*
233917c5aab5SMark Lord 	 * We're about to send a non-EDMA capable command to the
2340c6fd2807SJeff Garzik 	 * port.  Turn off EDMA so there won't be problems accessing
2341c6fd2807SJeff Garzik 	 * shadow block, etc registers.
2342c6fd2807SJeff Garzik 	 */
2343b562468cSMark Lord 	mv_stop_edma(ap);
2344f48765ccSMark Lord 	mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
2345e49856d8SMark Lord 	mv_pmp_select(ap, qc->dev->link->pmp);
234670f8b79cSMark Lord 
234770f8b79cSMark Lord 	if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
234870f8b79cSMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
234970f8b79cSMark Lord 		/*
235070f8b79cSMark Lord 		 * Workaround for 88SX60x1 FEr SATA#25 (part 2).
235170f8b79cSMark Lord 		 *
235270f8b79cSMark Lord 		 * After any NCQ error, the READ_LOG_EXT command
235370f8b79cSMark Lord 		 * from libata-eh *must* use mv_qc_issue_fis().
235470f8b79cSMark Lord 		 * Otherwise it might fail, due to chip errata.
235570f8b79cSMark Lord 		 *
235670f8b79cSMark Lord 		 * Rather than special-case it, we'll just *always*
235770f8b79cSMark Lord 		 * use this method here for READ_LOG_EXT, making for
235870f8b79cSMark Lord 		 * easier testing.
235970f8b79cSMark Lord 		 */
236070f8b79cSMark Lord 		if (IS_GEN_II(hpriv))
236170f8b79cSMark Lord 			return mv_qc_issue_fis(qc);
236270f8b79cSMark Lord 	}
23639363c382STejun Heo 	return ata_sff_qc_issue(qc);
2364c6fd2807SJeff Garzik }
2365c6fd2807SJeff Garzik 
23668f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
23678f767f8aSMark Lord {
23688f767f8aSMark Lord 	struct mv_port_priv *pp = ap->private_data;
23698f767f8aSMark Lord 	struct ata_queued_cmd *qc;
23708f767f8aSMark Lord 
23718f767f8aSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
23728f767f8aSMark Lord 		return NULL;
23738f767f8aSMark Lord 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
23743e4ec344STejun Heo 	if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING))
23758f767f8aSMark Lord 		return qc;
23763e4ec344STejun Heo 	return NULL;
23778f767f8aSMark Lord }
23788f767f8aSMark Lord 
237929d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap)
238029d187bbSMark Lord {
238129d187bbSMark Lord 	unsigned int pmp, pmp_map;
238229d187bbSMark Lord 	struct mv_port_priv *pp = ap->private_data;
238329d187bbSMark Lord 
238429d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
238529d187bbSMark Lord 		/*
238629d187bbSMark Lord 		 * Perform NCQ error analysis on failed PMPs
238729d187bbSMark Lord 		 * before we freeze the port entirely.
238829d187bbSMark Lord 		 *
238929d187bbSMark Lord 		 * The failed PMPs are marked earlier by mv_pmp_eh_prep().
239029d187bbSMark Lord 		 */
239129d187bbSMark Lord 		pmp_map = pp->delayed_eh_pmp_map;
239229d187bbSMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
239329d187bbSMark Lord 		for (pmp = 0; pmp_map != 0; pmp++) {
239429d187bbSMark Lord 			unsigned int this_pmp = (1 << pmp);
239529d187bbSMark Lord 			if (pmp_map & this_pmp) {
239629d187bbSMark Lord 				struct ata_link *link = &ap->pmp_link[pmp];
239729d187bbSMark Lord 				pmp_map &= ~this_pmp;
239829d187bbSMark Lord 				ata_eh_analyze_ncq_error(link);
239929d187bbSMark Lord 			}
240029d187bbSMark Lord 		}
240129d187bbSMark Lord 		ata_port_freeze(ap);
240229d187bbSMark Lord 	}
240329d187bbSMark Lord 	sata_pmp_error_handler(ap);
240429d187bbSMark Lord }
240529d187bbSMark Lord 
24064c299ca3SMark Lord static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
24074c299ca3SMark Lord {
24084c299ca3SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
24094c299ca3SMark Lord 
2410cae5a29dSMark Lord 	return readl(port_mmio + SATA_TESTCTL) >> 16;
24114c299ca3SMark Lord }
24124c299ca3SMark Lord 
24134c299ca3SMark Lord static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
24144c299ca3SMark Lord {
24154c299ca3SMark Lord 	struct ata_eh_info *ehi;
24164c299ca3SMark Lord 	unsigned int pmp;
24174c299ca3SMark Lord 
24184c299ca3SMark Lord 	/*
24194c299ca3SMark Lord 	 * Initialize EH info for PMPs which saw device errors
24204c299ca3SMark Lord 	 */
24214c299ca3SMark Lord 	ehi = &ap->link.eh_info;
24224c299ca3SMark Lord 	for (pmp = 0; pmp_map != 0; pmp++) {
24234c299ca3SMark Lord 		unsigned int this_pmp = (1 << pmp);
24244c299ca3SMark Lord 		if (pmp_map & this_pmp) {
24254c299ca3SMark Lord 			struct ata_link *link = &ap->pmp_link[pmp];
24264c299ca3SMark Lord 
24274c299ca3SMark Lord 			pmp_map &= ~this_pmp;
24284c299ca3SMark Lord 			ehi = &link->eh_info;
24294c299ca3SMark Lord 			ata_ehi_clear_desc(ehi);
24304c299ca3SMark Lord 			ata_ehi_push_desc(ehi, "dev err");
24314c299ca3SMark Lord 			ehi->err_mask |= AC_ERR_DEV;
24324c299ca3SMark Lord 			ehi->action |= ATA_EH_RESET;
24334c299ca3SMark Lord 			ata_link_abort(link);
24344c299ca3SMark Lord 		}
24354c299ca3SMark Lord 	}
24364c299ca3SMark Lord }
24374c299ca3SMark Lord 
243806aaca3fSMark Lord static int mv_req_q_empty(struct ata_port *ap)
243906aaca3fSMark Lord {
244006aaca3fSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
244106aaca3fSMark Lord 	u32 in_ptr, out_ptr;
244206aaca3fSMark Lord 
2443cae5a29dSMark Lord 	in_ptr  = (readl(port_mmio + EDMA_REQ_Q_IN_PTR)
244406aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2445cae5a29dSMark Lord 	out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR)
244606aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
244706aaca3fSMark Lord 	return (in_ptr == out_ptr);	/* 1 == queue_is_empty */
244806aaca3fSMark Lord }
244906aaca3fSMark Lord 
24504c299ca3SMark Lord static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
24514c299ca3SMark Lord {
24524c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
24534c299ca3SMark Lord 	int failed_links;
24544c299ca3SMark Lord 	unsigned int old_map, new_map;
24554c299ca3SMark Lord 
24564c299ca3SMark Lord 	/*
24574c299ca3SMark Lord 	 * Device error during FBS+NCQ operation:
24584c299ca3SMark Lord 	 *
24594c299ca3SMark Lord 	 * Set a port flag to prevent further I/O being enqueued.
24604c299ca3SMark Lord 	 * Leave the EDMA running to drain outstanding commands from this port.
24614c299ca3SMark Lord 	 * Perform the post-mortem/EH only when all responses are complete.
24624c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
24634c299ca3SMark Lord 	 */
24644c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
24654c299ca3SMark Lord 		pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
24664c299ca3SMark Lord 		pp->delayed_eh_pmp_map = 0;
24674c299ca3SMark Lord 	}
24684c299ca3SMark Lord 	old_map = pp->delayed_eh_pmp_map;
24694c299ca3SMark Lord 	new_map = old_map | mv_get_err_pmp_map(ap);
24704c299ca3SMark Lord 
24714c299ca3SMark Lord 	if (old_map != new_map) {
24724c299ca3SMark Lord 		pp->delayed_eh_pmp_map = new_map;
24734c299ca3SMark Lord 		mv_pmp_eh_prep(ap, new_map & ~old_map);
24744c299ca3SMark Lord 	}
2475c46938ccSMark Lord 	failed_links = hweight16(new_map);
24764c299ca3SMark Lord 
24774c299ca3SMark Lord 	ata_port_printk(ap, KERN_INFO, "%s: pmp_map=%04x qc_map=%04x "
24784c299ca3SMark Lord 			"failed_links=%d nr_active_links=%d\n",
24794c299ca3SMark Lord 			__func__, pp->delayed_eh_pmp_map,
24804c299ca3SMark Lord 			ap->qc_active, failed_links,
24814c299ca3SMark Lord 			ap->nr_active_links);
24824c299ca3SMark Lord 
248306aaca3fSMark Lord 	if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
24844c299ca3SMark Lord 		mv_process_crpb_entries(ap, pp);
24854c299ca3SMark Lord 		mv_stop_edma(ap);
24864c299ca3SMark Lord 		mv_eh_freeze(ap);
24874c299ca3SMark Lord 		ata_port_printk(ap, KERN_INFO, "%s: done\n", __func__);
24884c299ca3SMark Lord 		return 1;	/* handled */
24894c299ca3SMark Lord 	}
24904c299ca3SMark Lord 	ata_port_printk(ap, KERN_INFO, "%s: waiting\n", __func__);
24914c299ca3SMark Lord 	return 1;	/* handled */
24924c299ca3SMark Lord }
24934c299ca3SMark Lord 
24944c299ca3SMark Lord static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
24954c299ca3SMark Lord {
24964c299ca3SMark Lord 	/*
24974c299ca3SMark Lord 	 * Possible future enhancement:
24984c299ca3SMark Lord 	 *
24994c299ca3SMark Lord 	 * FBS+non-NCQ operation is not yet implemented.
25004c299ca3SMark Lord 	 * See related notes in mv_edma_cfg().
25014c299ca3SMark Lord 	 *
25024c299ca3SMark Lord 	 * Device error during FBS+non-NCQ operation:
25034c299ca3SMark Lord 	 *
25044c299ca3SMark Lord 	 * We need to snapshot the shadow registers for each failed command.
25054c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
25064c299ca3SMark Lord 	 */
25074c299ca3SMark Lord 	return 0;	/* not handled */
25084c299ca3SMark Lord }
25094c299ca3SMark Lord 
25104c299ca3SMark Lord static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
25114c299ca3SMark Lord {
25124c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
25134c299ca3SMark Lord 
25144c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
25154c299ca3SMark Lord 		return 0;	/* EDMA was not active: not handled */
25164c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
25174c299ca3SMark Lord 		return 0;	/* FBS was not active: not handled */
25184c299ca3SMark Lord 
25194c299ca3SMark Lord 	if (!(edma_err_cause & EDMA_ERR_DEV))
25204c299ca3SMark Lord 		return 0;	/* non DEV error: not handled */
25214c299ca3SMark Lord 	edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
25224c299ca3SMark Lord 	if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
25234c299ca3SMark Lord 		return 0;	/* other problems: not handled */
25244c299ca3SMark Lord 
25254c299ca3SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
25264c299ca3SMark Lord 		/*
25274c299ca3SMark Lord 		 * EDMA should NOT have self-disabled for this case.
25284c299ca3SMark Lord 		 * If it did, then something is wrong elsewhere,
25294c299ca3SMark Lord 		 * and we cannot handle it here.
25304c299ca3SMark Lord 		 */
25314c299ca3SMark Lord 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
25324c299ca3SMark Lord 			ata_port_printk(ap, KERN_WARNING,
25334c299ca3SMark Lord 				"%s: err_cause=0x%x pp_flags=0x%x\n",
25344c299ca3SMark Lord 				__func__, edma_err_cause, pp->pp_flags);
25354c299ca3SMark Lord 			return 0; /* not handled */
25364c299ca3SMark Lord 		}
25374c299ca3SMark Lord 		return mv_handle_fbs_ncq_dev_err(ap);
25384c299ca3SMark Lord 	} else {
25394c299ca3SMark Lord 		/*
25404c299ca3SMark Lord 		 * EDMA should have self-disabled for this case.
25414c299ca3SMark Lord 		 * If it did not, then something is wrong elsewhere,
25424c299ca3SMark Lord 		 * and we cannot handle it here.
25434c299ca3SMark Lord 		 */
25444c299ca3SMark Lord 		if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
25454c299ca3SMark Lord 			ata_port_printk(ap, KERN_WARNING,
25464c299ca3SMark Lord 				"%s: err_cause=0x%x pp_flags=0x%x\n",
25474c299ca3SMark Lord 				__func__, edma_err_cause, pp->pp_flags);
25484c299ca3SMark Lord 			return 0; /* not handled */
25494c299ca3SMark Lord 		}
25504c299ca3SMark Lord 		return mv_handle_fbs_non_ncq_dev_err(ap);
25514c299ca3SMark Lord 	}
25524c299ca3SMark Lord 	return 0;	/* not handled */
25534c299ca3SMark Lord }
25544c299ca3SMark Lord 
2555a9010329SMark Lord static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
25568f767f8aSMark Lord {
25578f767f8aSMark Lord 	struct ata_eh_info *ehi = &ap->link.eh_info;
2558a9010329SMark Lord 	char *when = "idle";
25598f767f8aSMark Lord 
25608f767f8aSMark Lord 	ata_ehi_clear_desc(ehi);
25613e4ec344STejun Heo 	if (edma_was_enabled) {
2562a9010329SMark Lord 		when = "EDMA enabled";
25638f767f8aSMark Lord 	} else {
25648f767f8aSMark Lord 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
25658f767f8aSMark Lord 		if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
2566a9010329SMark Lord 			when = "polling";
25678f767f8aSMark Lord 	}
2568a9010329SMark Lord 	ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
25698f767f8aSMark Lord 	ehi->err_mask |= AC_ERR_OTHER;
25708f767f8aSMark Lord 	ehi->action   |= ATA_EH_RESET;
25718f767f8aSMark Lord 	ata_port_freeze(ap);
25728f767f8aSMark Lord }
25738f767f8aSMark Lord 
2574c6fd2807SJeff Garzik /**
2575c6fd2807SJeff Garzik  *      mv_err_intr - Handle error interrupts on the port
2576c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
2577c6fd2807SJeff Garzik  *
25788d07379dSMark Lord  *      Most cases require a full reset of the chip's state machine,
25798d07379dSMark Lord  *      which also performs a COMRESET.
25808d07379dSMark Lord  *      Also, if the port disabled DMA, update our cached copy to match.
2581c6fd2807SJeff Garzik  *
2582c6fd2807SJeff Garzik  *      LOCKING:
2583c6fd2807SJeff Garzik  *      Inherited from caller.
2584c6fd2807SJeff Garzik  */
258537b9046aSMark Lord static void mv_err_intr(struct ata_port *ap)
2586c6fd2807SJeff Garzik {
2587c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2588bdd4dddeSJeff Garzik 	u32 edma_err_cause, eh_freeze_mask, serr = 0;
2589e4006077SMark Lord 	u32 fis_cause = 0;
2590bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2591bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2592bdd4dddeSJeff Garzik 	unsigned int action = 0, err_mask = 0;
25939af5c9c9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
259437b9046aSMark Lord 	struct ata_queued_cmd *qc;
259537b9046aSMark Lord 	int abort = 0;
2596c6fd2807SJeff Garzik 
25978d07379dSMark Lord 	/*
259837b9046aSMark Lord 	 * Read and clear the SError and err_cause bits.
2599e4006077SMark Lord 	 * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
2600e4006077SMark Lord 	 * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
2601bdd4dddeSJeff Garzik 	 */
260237b9046aSMark Lord 	sata_scr_read(&ap->link, SCR_ERROR, &serr);
260337b9046aSMark Lord 	sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
260437b9046aSMark Lord 
2605cae5a29dSMark Lord 	edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE);
2606e4006077SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2607cae5a29dSMark Lord 		fis_cause = readl(port_mmio + FIS_IRQ_CAUSE);
2608cae5a29dSMark Lord 		writelfl(~fis_cause, port_mmio + FIS_IRQ_CAUSE);
2609e4006077SMark Lord 	}
2610cae5a29dSMark Lord 	writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE);
2611bdd4dddeSJeff Garzik 
26124c299ca3SMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
26134c299ca3SMark Lord 		/*
26144c299ca3SMark Lord 		 * Device errors during FIS-based switching operation
26154c299ca3SMark Lord 		 * require special handling.
26164c299ca3SMark Lord 		 */
26174c299ca3SMark Lord 		if (mv_handle_dev_err(ap, edma_err_cause))
26184c299ca3SMark Lord 			return;
26194c299ca3SMark Lord 	}
26204c299ca3SMark Lord 
262137b9046aSMark Lord 	qc = mv_get_active_qc(ap);
262237b9046aSMark Lord 	ata_ehi_clear_desc(ehi);
262337b9046aSMark Lord 	ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
262437b9046aSMark Lord 			  edma_err_cause, pp->pp_flags);
2625e4006077SMark Lord 
2626c443c500SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2627e4006077SMark Lord 		ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
2628cae5a29dSMark Lord 		if (fis_cause & FIS_IRQ_CAUSE_AN) {
2629c443c500SMark Lord 			u32 ec = edma_err_cause &
2630c443c500SMark Lord 			       ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
2631c443c500SMark Lord 			sata_async_notification(ap);
2632c443c500SMark Lord 			if (!ec)
2633c443c500SMark Lord 				return; /* Just an AN; no need for the nukes */
2634c443c500SMark Lord 			ata_ehi_push_desc(ehi, "SDB notify");
2635c443c500SMark Lord 		}
2636c443c500SMark Lord 	}
2637bdd4dddeSJeff Garzik 	/*
2638352fab70SMark Lord 	 * All generations share these EDMA error cause bits:
2639bdd4dddeSJeff Garzik 	 */
264037b9046aSMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
2641bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_DEV;
264237b9046aSMark Lord 		action |= ATA_EH_RESET;
264337b9046aSMark Lord 		ata_ehi_push_desc(ehi, "dev error");
264437b9046aSMark Lord 	}
2645bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
26466c1153e0SJeff Garzik 			EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
2647bdd4dddeSJeff Garzik 			EDMA_ERR_INTRL_PAR)) {
2648bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
2649cf480626STejun Heo 		action |= ATA_EH_RESET;
2650b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "parity error");
2651bdd4dddeSJeff Garzik 	}
2652bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
2653bdd4dddeSJeff Garzik 		ata_ehi_hotplugged(ehi);
2654bdd4dddeSJeff Garzik 		ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
2655b64bbc39STejun Heo 			"dev disconnect" : "dev connect");
2656cf480626STejun Heo 		action |= ATA_EH_RESET;
2657bdd4dddeSJeff Garzik 	}
2658bdd4dddeSJeff Garzik 
2659352fab70SMark Lord 	/*
2660352fab70SMark Lord 	 * Gen-I has a different SELF_DIS bit,
2661352fab70SMark Lord 	 * different FREEZE bits, and no SERR bit:
2662352fab70SMark Lord 	 */
2663ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv)) {
2664bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE_5;
2665bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
2666c6fd2807SJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2667b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
2668c6fd2807SJeff Garzik 		}
2669bdd4dddeSJeff Garzik 	} else {
2670bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE;
2671bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2672bdd4dddeSJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2673b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
2674bdd4dddeSJeff Garzik 		}
2675bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SERR) {
26768d07379dSMark Lord 			ata_ehi_push_desc(ehi, "SError=%08x", serr);
26778d07379dSMark Lord 			err_mask |= AC_ERR_ATA_BUS;
2678cf480626STejun Heo 			action |= ATA_EH_RESET;
2679bdd4dddeSJeff Garzik 		}
2680bdd4dddeSJeff Garzik 	}
2681c6fd2807SJeff Garzik 
2682bdd4dddeSJeff Garzik 	if (!err_mask) {
2683bdd4dddeSJeff Garzik 		err_mask = AC_ERR_OTHER;
2684cf480626STejun Heo 		action |= ATA_EH_RESET;
2685bdd4dddeSJeff Garzik 	}
2686bdd4dddeSJeff Garzik 
2687bdd4dddeSJeff Garzik 	ehi->serror |= serr;
2688bdd4dddeSJeff Garzik 	ehi->action |= action;
2689bdd4dddeSJeff Garzik 
2690bdd4dddeSJeff Garzik 	if (qc)
2691bdd4dddeSJeff Garzik 		qc->err_mask |= err_mask;
2692bdd4dddeSJeff Garzik 	else
2693bdd4dddeSJeff Garzik 		ehi->err_mask |= err_mask;
2694bdd4dddeSJeff Garzik 
269537b9046aSMark Lord 	if (err_mask == AC_ERR_DEV) {
269637b9046aSMark Lord 		/*
269737b9046aSMark Lord 		 * Cannot do ata_port_freeze() here,
269837b9046aSMark Lord 		 * because it would kill PIO access,
269937b9046aSMark Lord 		 * which is needed for further diagnosis.
270037b9046aSMark Lord 		 */
270137b9046aSMark Lord 		mv_eh_freeze(ap);
270237b9046aSMark Lord 		abort = 1;
270337b9046aSMark Lord 	} else if (edma_err_cause & eh_freeze_mask) {
270437b9046aSMark Lord 		/*
270537b9046aSMark Lord 		 * Note to self: ata_port_freeze() calls ata_port_abort()
270637b9046aSMark Lord 		 */
2707bdd4dddeSJeff Garzik 		ata_port_freeze(ap);
270837b9046aSMark Lord 	} else {
270937b9046aSMark Lord 		abort = 1;
271037b9046aSMark Lord 	}
271137b9046aSMark Lord 
271237b9046aSMark Lord 	if (abort) {
271337b9046aSMark Lord 		if (qc)
271437b9046aSMark Lord 			ata_link_abort(qc->dev->link);
2715bdd4dddeSJeff Garzik 		else
2716bdd4dddeSJeff Garzik 			ata_port_abort(ap);
2717bdd4dddeSJeff Garzik 	}
271837b9046aSMark Lord }
2719bdd4dddeSJeff Garzik 
2720fcfb1f77SMark Lord static void mv_process_crpb_response(struct ata_port *ap,
2721fcfb1f77SMark Lord 		struct mv_crpb *response, unsigned int tag, int ncq_enabled)
2722fcfb1f77SMark Lord {
2723fcfb1f77SMark Lord 	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
2724fcfb1f77SMark Lord 
2725fcfb1f77SMark Lord 	if (qc) {
2726fcfb1f77SMark Lord 		u8 ata_status;
2727fcfb1f77SMark Lord 		u16 edma_status = le16_to_cpu(response->flags);
2728fcfb1f77SMark Lord 		/*
2729fcfb1f77SMark Lord 		 * edma_status from a response queue entry:
2730cae5a29dSMark Lord 		 *   LSB is from EDMA_ERR_IRQ_CAUSE (non-NCQ only).
2731fcfb1f77SMark Lord 		 *   MSB is saved ATA status from command completion.
2732fcfb1f77SMark Lord 		 */
2733fcfb1f77SMark Lord 		if (!ncq_enabled) {
2734fcfb1f77SMark Lord 			u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
2735fcfb1f77SMark Lord 			if (err_cause) {
2736fcfb1f77SMark Lord 				/*
2737fcfb1f77SMark Lord 				 * Error will be seen/handled by mv_err_intr().
2738fcfb1f77SMark Lord 				 * So do nothing at all here.
2739fcfb1f77SMark Lord 				 */
2740fcfb1f77SMark Lord 				return;
2741fcfb1f77SMark Lord 			}
2742fcfb1f77SMark Lord 		}
2743fcfb1f77SMark Lord 		ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
274437b9046aSMark Lord 		if (!ac_err_mask(ata_status))
2745fcfb1f77SMark Lord 			ata_qc_complete(qc);
274637b9046aSMark Lord 		/* else: leave it for mv_err_intr() */
2747fcfb1f77SMark Lord 	} else {
2748fcfb1f77SMark Lord 		ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
2749fcfb1f77SMark Lord 				__func__, tag);
2750fcfb1f77SMark Lord 	}
2751fcfb1f77SMark Lord }
2752fcfb1f77SMark Lord 
2753fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
2754bdd4dddeSJeff Garzik {
2755bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2756bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2757fcfb1f77SMark Lord 	u32 in_index;
2758bdd4dddeSJeff Garzik 	bool work_done = false;
2759fcfb1f77SMark Lord 	int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
2760bdd4dddeSJeff Garzik 
2761fcfb1f77SMark Lord 	/* Get the hardware queue position index */
2762cae5a29dSMark Lord 	in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR)
2763bdd4dddeSJeff Garzik 			>> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2764bdd4dddeSJeff Garzik 
2765fcfb1f77SMark Lord 	/* Process new responses from since the last time we looked */
2766fcfb1f77SMark Lord 	while (in_index != pp->resp_idx) {
27676c1153e0SJeff Garzik 		unsigned int tag;
2768fcfb1f77SMark Lord 		struct mv_crpb *response = &pp->crpb[pp->resp_idx];
2769bdd4dddeSJeff Garzik 
2770fcfb1f77SMark Lord 		pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2771bdd4dddeSJeff Garzik 
2772fcfb1f77SMark Lord 		if (IS_GEN_I(hpriv)) {
2773fcfb1f77SMark Lord 			/* 50xx: no NCQ, only one command active at a time */
27749af5c9c9STejun Heo 			tag = ap->link.active_tag;
2775fcfb1f77SMark Lord 		} else {
2776fcfb1f77SMark Lord 			/* Gen II/IIE: get command tag from CRPB entry */
2777fcfb1f77SMark Lord 			tag = le16_to_cpu(response->id) & 0x1f;
2778bdd4dddeSJeff Garzik 		}
2779fcfb1f77SMark Lord 		mv_process_crpb_response(ap, response, tag, ncq_enabled);
2780bdd4dddeSJeff Garzik 		work_done = true;
2781bdd4dddeSJeff Garzik 	}
2782bdd4dddeSJeff Garzik 
2783352fab70SMark Lord 	/* Update the software queue position index in hardware */
2784bdd4dddeSJeff Garzik 	if (work_done)
2785bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
2786fcfb1f77SMark Lord 			 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
2787cae5a29dSMark Lord 			 port_mmio + EDMA_RSP_Q_OUT_PTR);
2788c6fd2807SJeff Garzik }
2789c6fd2807SJeff Garzik 
2790a9010329SMark Lord static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2791a9010329SMark Lord {
2792a9010329SMark Lord 	struct mv_port_priv *pp;
2793a9010329SMark Lord 	int edma_was_enabled;
2794a9010329SMark Lord 
2795a9010329SMark Lord 	/*
2796a9010329SMark Lord 	 * Grab a snapshot of the EDMA_EN flag setting,
2797a9010329SMark Lord 	 * so that we have a consistent view for this port,
2798a9010329SMark Lord 	 * even if something we call of our routines changes it.
2799a9010329SMark Lord 	 */
2800a9010329SMark Lord 	pp = ap->private_data;
2801a9010329SMark Lord 	edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2802a9010329SMark Lord 	/*
2803a9010329SMark Lord 	 * Process completed CRPB response(s) before other events.
2804a9010329SMark Lord 	 */
2805a9010329SMark Lord 	if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2806a9010329SMark Lord 		mv_process_crpb_entries(ap, pp);
28074c299ca3SMark Lord 		if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
28084c299ca3SMark Lord 			mv_handle_fbs_ncq_dev_err(ap);
2809a9010329SMark Lord 	}
2810a9010329SMark Lord 	/*
2811a9010329SMark Lord 	 * Handle chip-reported errors, or continue on to handle PIO.
2812a9010329SMark Lord 	 */
2813a9010329SMark Lord 	if (unlikely(port_cause & ERR_IRQ)) {
2814a9010329SMark Lord 		mv_err_intr(ap);
2815a9010329SMark Lord 	} else if (!edma_was_enabled) {
2816a9010329SMark Lord 		struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2817a9010329SMark Lord 		if (qc)
2818a9010329SMark Lord 			ata_sff_host_intr(ap, qc);
2819a9010329SMark Lord 		else
2820a9010329SMark Lord 			mv_unexpected_intr(ap, edma_was_enabled);
2821a9010329SMark Lord 	}
2822a9010329SMark Lord }
2823a9010329SMark Lord 
2824c6fd2807SJeff Garzik /**
2825c6fd2807SJeff Garzik  *      mv_host_intr - Handle all interrupts on the given host controller
2826cca3974eSJeff Garzik  *      @host: host specific structure
28277368f919SMark Lord  *      @main_irq_cause: Main interrupt cause register for the chip.
2828c6fd2807SJeff Garzik  *
2829c6fd2807SJeff Garzik  *      LOCKING:
2830c6fd2807SJeff Garzik  *      Inherited from caller.
2831c6fd2807SJeff Garzik  */
28327368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
2833c6fd2807SJeff Garzik {
2834f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2835eabd5eb1SMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
2836a3718c1fSMark Lord 	unsigned int handled = 0, port;
2837c6fd2807SJeff Garzik 
28382b748a0aSMark Lord 	/* If asserted, clear the "all ports" IRQ coalescing bit */
28392b748a0aSMark Lord 	if (main_irq_cause & ALL_PORTS_COAL_DONE)
2840cae5a29dSMark Lord 		writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
28412b748a0aSMark Lord 
2842a3718c1fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
2843cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[port];
2844eabd5eb1SMark Lord 		unsigned int p, shift, hardport, port_cause;
2845eabd5eb1SMark Lord 
2846a3718c1fSMark Lord 		MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2847a3718c1fSMark Lord 		/*
2848eabd5eb1SMark Lord 		 * Each hc within the host has its own hc_irq_cause register,
2849eabd5eb1SMark Lord 		 * where the interrupting ports bits get ack'd.
2850a3718c1fSMark Lord 		 */
2851eabd5eb1SMark Lord 		if (hardport == 0) {	/* first port on this hc ? */
2852eabd5eb1SMark Lord 			u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2853eabd5eb1SMark Lord 			u32 port_mask, ack_irqs;
2854eabd5eb1SMark Lord 			/*
2855eabd5eb1SMark Lord 			 * Skip this entire hc if nothing pending for any ports
2856eabd5eb1SMark Lord 			 */
2857eabd5eb1SMark Lord 			if (!hc_cause) {
2858eabd5eb1SMark Lord 				port += MV_PORTS_PER_HC - 1;
2859eabd5eb1SMark Lord 				continue;
2860eabd5eb1SMark Lord 			}
2861eabd5eb1SMark Lord 			/*
2862eabd5eb1SMark Lord 			 * We don't need/want to read the hc_irq_cause register,
2863eabd5eb1SMark Lord 			 * because doing so hurts performance, and
2864eabd5eb1SMark Lord 			 * main_irq_cause already gives us everything we need.
2865eabd5eb1SMark Lord 			 *
2866eabd5eb1SMark Lord 			 * But we do have to *write* to the hc_irq_cause to ack
2867eabd5eb1SMark Lord 			 * the ports that we are handling this time through.
2868eabd5eb1SMark Lord 			 *
2869eabd5eb1SMark Lord 			 * This requires that we create a bitmap for those
2870eabd5eb1SMark Lord 			 * ports which interrupted us, and use that bitmap
2871eabd5eb1SMark Lord 			 * to ack (only) those ports via hc_irq_cause.
2872eabd5eb1SMark Lord 			 */
2873eabd5eb1SMark Lord 			ack_irqs = 0;
28742b748a0aSMark Lord 			if (hc_cause & PORTS_0_3_COAL_DONE)
28752b748a0aSMark Lord 				ack_irqs = HC_COAL_IRQ;
2876eabd5eb1SMark Lord 			for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2877eabd5eb1SMark Lord 				if ((port + p) >= hpriv->n_ports)
2878eabd5eb1SMark Lord 					break;
2879eabd5eb1SMark Lord 				port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2880eabd5eb1SMark Lord 				if (hc_cause & port_mask)
2881eabd5eb1SMark Lord 					ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2882eabd5eb1SMark Lord 			}
2883a3718c1fSMark Lord 			hc_mmio = mv_hc_base_from_port(mmio, port);
2884cae5a29dSMark Lord 			writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE);
2885a3718c1fSMark Lord 			handled = 1;
2886a3718c1fSMark Lord 		}
2887a9010329SMark Lord 		/*
2888a9010329SMark Lord 		 * Handle interrupts signalled for this port:
2889a9010329SMark Lord 		 */
2890eabd5eb1SMark Lord 		port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
2891a9010329SMark Lord 		if (port_cause)
2892a9010329SMark Lord 			mv_port_intr(ap, port_cause);
2893eabd5eb1SMark Lord 	}
2894a3718c1fSMark Lord 	return handled;
2895c6fd2807SJeff Garzik }
2896c6fd2807SJeff Garzik 
2897a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
2898bdd4dddeSJeff Garzik {
289902a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2900bdd4dddeSJeff Garzik 	struct ata_port *ap;
2901bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
2902bdd4dddeSJeff Garzik 	struct ata_eh_info *ehi;
2903bdd4dddeSJeff Garzik 	unsigned int i, err_mask, printed = 0;
2904bdd4dddeSJeff Garzik 	u32 err_cause;
2905bdd4dddeSJeff Garzik 
2906cae5a29dSMark Lord 	err_cause = readl(mmio + hpriv->irq_cause_offset);
2907bdd4dddeSJeff Garzik 
2908bdd4dddeSJeff Garzik 	dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
2909bdd4dddeSJeff Garzik 		   err_cause);
2910bdd4dddeSJeff Garzik 
2911bdd4dddeSJeff Garzik 	DPRINTK("All regs @ PCI error\n");
2912bdd4dddeSJeff Garzik 	mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
2913bdd4dddeSJeff Garzik 
2914cae5a29dSMark Lord 	writelfl(0, mmio + hpriv->irq_cause_offset);
2915bdd4dddeSJeff Garzik 
2916bdd4dddeSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
2917bdd4dddeSJeff Garzik 		ap = host->ports[i];
2918936fd732STejun Heo 		if (!ata_link_offline(&ap->link)) {
29199af5c9c9STejun Heo 			ehi = &ap->link.eh_info;
2920bdd4dddeSJeff Garzik 			ata_ehi_clear_desc(ehi);
2921bdd4dddeSJeff Garzik 			if (!printed++)
2922bdd4dddeSJeff Garzik 				ata_ehi_push_desc(ehi,
2923bdd4dddeSJeff Garzik 					"PCI err cause 0x%08x", err_cause);
2924bdd4dddeSJeff Garzik 			err_mask = AC_ERR_HOST_BUS;
2925cf480626STejun Heo 			ehi->action = ATA_EH_RESET;
29269af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
2927bdd4dddeSJeff Garzik 			if (qc)
2928bdd4dddeSJeff Garzik 				qc->err_mask |= err_mask;
2929bdd4dddeSJeff Garzik 			else
2930bdd4dddeSJeff Garzik 				ehi->err_mask |= err_mask;
2931bdd4dddeSJeff Garzik 
2932bdd4dddeSJeff Garzik 			ata_port_freeze(ap);
2933bdd4dddeSJeff Garzik 		}
2934bdd4dddeSJeff Garzik 	}
2935a3718c1fSMark Lord 	return 1;	/* handled */
2936bdd4dddeSJeff Garzik }
2937bdd4dddeSJeff Garzik 
2938c6fd2807SJeff Garzik /**
2939c5d3e45aSJeff Garzik  *      mv_interrupt - Main interrupt event handler
2940c6fd2807SJeff Garzik  *      @irq: unused
2941c6fd2807SJeff Garzik  *      @dev_instance: private data; in this case the host structure
2942c6fd2807SJeff Garzik  *
2943c6fd2807SJeff Garzik  *      Read the read only register to determine if any host
2944c6fd2807SJeff Garzik  *      controllers have pending interrupts.  If so, call lower level
2945c6fd2807SJeff Garzik  *      routine to handle.  Also check for PCI errors which are only
2946c6fd2807SJeff Garzik  *      reported here.
2947c6fd2807SJeff Garzik  *
2948c6fd2807SJeff Garzik  *      LOCKING:
2949cca3974eSJeff Garzik  *      This routine holds the host lock while processing pending
2950c6fd2807SJeff Garzik  *      interrupts.
2951c6fd2807SJeff Garzik  */
29527d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
2953c6fd2807SJeff Garzik {
2954cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
2955f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2956a3718c1fSMark Lord 	unsigned int handled = 0;
29576d3c30efSMark Lord 	int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
295896e2c487SMark Lord 	u32 main_irq_cause, pending_irqs;
2959c6fd2807SJeff Garzik 
2960646a4da5SMark Lord 	spin_lock(&host->lock);
29616d3c30efSMark Lord 
29626d3c30efSMark Lord 	/* for MSI:  block new interrupts while in here */
29636d3c30efSMark Lord 	if (using_msi)
29642b748a0aSMark Lord 		mv_write_main_irq_mask(0, hpriv);
29656d3c30efSMark Lord 
29667368f919SMark Lord 	main_irq_cause = readl(hpriv->main_irq_cause_addr);
296796e2c487SMark Lord 	pending_irqs   = main_irq_cause & hpriv->main_irq_mask;
2968352fab70SMark Lord 	/*
2969352fab70SMark Lord 	 * Deal with cases where we either have nothing pending, or have read
2970352fab70SMark Lord 	 * a bogus register value which can indicate HW removal or PCI fault.
2971c6fd2807SJeff Garzik 	 */
2972a44253d2SMark Lord 	if (pending_irqs && main_irq_cause != 0xffffffffU) {
29731f398472SMark Lord 		if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
2974a3718c1fSMark Lord 			handled = mv_pci_error(host, hpriv->base);
2975a3718c1fSMark Lord 		else
2976a44253d2SMark Lord 			handled = mv_host_intr(host, pending_irqs);
2977bdd4dddeSJeff Garzik 	}
29786d3c30efSMark Lord 
29796d3c30efSMark Lord 	/* for MSI: unmask; interrupt cause bits will retrigger now */
29806d3c30efSMark Lord 	if (using_msi)
29812b748a0aSMark Lord 		mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
29826d3c30efSMark Lord 
29839d51af7bSMark Lord 	spin_unlock(&host->lock);
29849d51af7bSMark Lord 
2985c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
2986c6fd2807SJeff Garzik }
2987c6fd2807SJeff Garzik 
2988c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
2989c6fd2807SJeff Garzik {
2990c6fd2807SJeff Garzik 	unsigned int ofs;
2991c6fd2807SJeff Garzik 
2992c6fd2807SJeff Garzik 	switch (sc_reg_in) {
2993c6fd2807SJeff Garzik 	case SCR_STATUS:
2994c6fd2807SJeff Garzik 	case SCR_ERROR:
2995c6fd2807SJeff Garzik 	case SCR_CONTROL:
2996c6fd2807SJeff Garzik 		ofs = sc_reg_in * sizeof(u32);
2997c6fd2807SJeff Garzik 		break;
2998c6fd2807SJeff Garzik 	default:
2999c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
3000c6fd2807SJeff Garzik 		break;
3001c6fd2807SJeff Garzik 	}
3002c6fd2807SJeff Garzik 	return ofs;
3003c6fd2807SJeff Garzik }
3004c6fd2807SJeff Garzik 
300582ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
3006c6fd2807SJeff Garzik {
300782ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
3008f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
300982ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3010c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
3011c6fd2807SJeff Garzik 
3012da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
3013da3dbb17STejun Heo 		*val = readl(addr + ofs);
3014da3dbb17STejun Heo 		return 0;
3015da3dbb17STejun Heo 	} else
3016da3dbb17STejun Heo 		return -EINVAL;
3017c6fd2807SJeff Garzik }
3018c6fd2807SJeff Garzik 
301982ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
3020c6fd2807SJeff Garzik {
302182ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
3022f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
302382ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3024c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
3025c6fd2807SJeff Garzik 
3026da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
30270d5ff566STejun Heo 		writelfl(val, addr + ofs);
3028da3dbb17STejun Heo 		return 0;
3029da3dbb17STejun Heo 	} else
3030da3dbb17STejun Heo 		return -EINVAL;
3031c6fd2807SJeff Garzik }
3032c6fd2807SJeff Garzik 
30337bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
3034c6fd2807SJeff Garzik {
30357bb3c529SSaeed Bishara 	struct pci_dev *pdev = to_pci_dev(host->dev);
3036c6fd2807SJeff Garzik 	int early_5080;
3037c6fd2807SJeff Garzik 
303844c10138SAuke Kok 	early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
3039c6fd2807SJeff Garzik 
3040c6fd2807SJeff Garzik 	if (!early_5080) {
3041c6fd2807SJeff Garzik 		u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3042c6fd2807SJeff Garzik 		tmp |= (1 << 0);
3043c6fd2807SJeff Garzik 		writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3044c6fd2807SJeff Garzik 	}
3045c6fd2807SJeff Garzik 
30467bb3c529SSaeed Bishara 	mv_reset_pci_bus(host, mmio);
3047c6fd2807SJeff Garzik }
3048c6fd2807SJeff Garzik 
3049c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3050c6fd2807SJeff Garzik {
3051cae5a29dSMark Lord 	writel(0x0fcfffff, mmio + FLASH_CTL);
3052c6fd2807SJeff Garzik }
3053c6fd2807SJeff Garzik 
3054c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
3055c6fd2807SJeff Garzik 			   void __iomem *mmio)
3056c6fd2807SJeff Garzik {
3057c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
3058c6fd2807SJeff Garzik 	u32 tmp;
3059c6fd2807SJeff Garzik 
3060c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
3061c6fd2807SJeff Garzik 
3062c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0x1800;	/* bits 12:11 */
3063c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0xe0;	/* bits 7:5 */
3064c6fd2807SJeff Garzik }
3065c6fd2807SJeff Garzik 
3066c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3067c6fd2807SJeff Garzik {
3068c6fd2807SJeff Garzik 	u32 tmp;
3069c6fd2807SJeff Garzik 
3070cae5a29dSMark Lord 	writel(0, mmio + GPIO_PORT_CTL);
3071c6fd2807SJeff Garzik 
3072c6fd2807SJeff Garzik 	/* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
3073c6fd2807SJeff Garzik 
3074c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3075c6fd2807SJeff Garzik 	tmp |= ~(1 << 0);
3076c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3077c6fd2807SJeff Garzik }
3078c6fd2807SJeff Garzik 
3079c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3080c6fd2807SJeff Garzik 			   unsigned int port)
3081c6fd2807SJeff Garzik {
3082c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, port);
3083c6fd2807SJeff Garzik 	const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
3084c6fd2807SJeff Garzik 	u32 tmp;
3085c6fd2807SJeff Garzik 	int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
3086c6fd2807SJeff Garzik 
3087c6fd2807SJeff Garzik 	if (fix_apm_sq) {
3088cae5a29dSMark Lord 		tmp = readl(phy_mmio + MV5_LTMODE);
3089c6fd2807SJeff Garzik 		tmp |= (1 << 19);
3090cae5a29dSMark Lord 		writel(tmp, phy_mmio + MV5_LTMODE);
3091c6fd2807SJeff Garzik 
3092cae5a29dSMark Lord 		tmp = readl(phy_mmio + MV5_PHY_CTL);
3093c6fd2807SJeff Garzik 		tmp &= ~0x3;
3094c6fd2807SJeff Garzik 		tmp |= 0x1;
3095cae5a29dSMark Lord 		writel(tmp, phy_mmio + MV5_PHY_CTL);
3096c6fd2807SJeff Garzik 	}
3097c6fd2807SJeff Garzik 
3098c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
3099c6fd2807SJeff Garzik 	tmp &= ~mask;
3100c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].pre;
3101c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].amps;
3102c6fd2807SJeff Garzik 	writel(tmp, phy_mmio + MV5_PHY_MODE);
3103c6fd2807SJeff Garzik }
3104c6fd2807SJeff Garzik 
3105c6fd2807SJeff Garzik 
3106c6fd2807SJeff Garzik #undef ZERO
3107c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
3108c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
3109c6fd2807SJeff Garzik 			     unsigned int port)
3110c6fd2807SJeff Garzik {
3111c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
3112c6fd2807SJeff Garzik 
3113e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
3114c6fd2807SJeff Garzik 
3115c6fd2807SJeff Garzik 	ZERO(0x028);	/* command */
3116cae5a29dSMark Lord 	writel(0x11f, port_mmio + EDMA_CFG);
3117c6fd2807SJeff Garzik 	ZERO(0x004);	/* timer */
3118c6fd2807SJeff Garzik 	ZERO(0x008);	/* irq err cause */
3119c6fd2807SJeff Garzik 	ZERO(0x00c);	/* irq err mask */
3120c6fd2807SJeff Garzik 	ZERO(0x010);	/* rq bah */
3121c6fd2807SJeff Garzik 	ZERO(0x014);	/* rq inp */
3122c6fd2807SJeff Garzik 	ZERO(0x018);	/* rq outp */
3123c6fd2807SJeff Garzik 	ZERO(0x01c);	/* respq bah */
3124c6fd2807SJeff Garzik 	ZERO(0x024);	/* respq outp */
3125c6fd2807SJeff Garzik 	ZERO(0x020);	/* respq inp */
3126c6fd2807SJeff Garzik 	ZERO(0x02c);	/* test control */
3127cae5a29dSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
3128c6fd2807SJeff Garzik }
3129c6fd2807SJeff Garzik #undef ZERO
3130c6fd2807SJeff Garzik 
3131c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
3132c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3133c6fd2807SJeff Garzik 			unsigned int hc)
3134c6fd2807SJeff Garzik {
3135c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3136c6fd2807SJeff Garzik 	u32 tmp;
3137c6fd2807SJeff Garzik 
3138c6fd2807SJeff Garzik 	ZERO(0x00c);
3139c6fd2807SJeff Garzik 	ZERO(0x010);
3140c6fd2807SJeff Garzik 	ZERO(0x014);
3141c6fd2807SJeff Garzik 	ZERO(0x018);
3142c6fd2807SJeff Garzik 
3143c6fd2807SJeff Garzik 	tmp = readl(hc_mmio + 0x20);
3144c6fd2807SJeff Garzik 	tmp &= 0x1c1c1c1c;
3145c6fd2807SJeff Garzik 	tmp |= 0x03030303;
3146c6fd2807SJeff Garzik 	writel(tmp, hc_mmio + 0x20);
3147c6fd2807SJeff Garzik }
3148c6fd2807SJeff Garzik #undef ZERO
3149c6fd2807SJeff Garzik 
3150c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3151c6fd2807SJeff Garzik 			unsigned int n_hc)
3152c6fd2807SJeff Garzik {
3153c6fd2807SJeff Garzik 	unsigned int hc, port;
3154c6fd2807SJeff Garzik 
3155c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3156c6fd2807SJeff Garzik 		for (port = 0; port < MV_PORTS_PER_HC; port++)
3157c6fd2807SJeff Garzik 			mv5_reset_hc_port(hpriv, mmio,
3158c6fd2807SJeff Garzik 					  (hc * MV_PORTS_PER_HC) + port);
3159c6fd2807SJeff Garzik 
3160c6fd2807SJeff Garzik 		mv5_reset_one_hc(hpriv, mmio, hc);
3161c6fd2807SJeff Garzik 	}
3162c6fd2807SJeff Garzik 
3163c6fd2807SJeff Garzik 	return 0;
3164c6fd2807SJeff Garzik }
3165c6fd2807SJeff Garzik 
3166c6fd2807SJeff Garzik #undef ZERO
3167c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
31687bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
3169c6fd2807SJeff Garzik {
317002a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3171c6fd2807SJeff Garzik 	u32 tmp;
3172c6fd2807SJeff Garzik 
3173cae5a29dSMark Lord 	tmp = readl(mmio + MV_PCI_MODE);
3174c6fd2807SJeff Garzik 	tmp &= 0xff00ffff;
3175cae5a29dSMark Lord 	writel(tmp, mmio + MV_PCI_MODE);
3176c6fd2807SJeff Garzik 
3177c6fd2807SJeff Garzik 	ZERO(MV_PCI_DISC_TIMER);
3178c6fd2807SJeff Garzik 	ZERO(MV_PCI_MSI_TRIGGER);
3179cae5a29dSMark Lord 	writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
3180c6fd2807SJeff Garzik 	ZERO(MV_PCI_SERR_MASK);
3181cae5a29dSMark Lord 	ZERO(hpriv->irq_cause_offset);
3182cae5a29dSMark Lord 	ZERO(hpriv->irq_mask_offset);
3183c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_LOW_ADDRESS);
3184c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_HIGH_ADDRESS);
3185c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_ATTRIBUTE);
3186c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_COMMAND);
3187c6fd2807SJeff Garzik }
3188c6fd2807SJeff Garzik #undef ZERO
3189c6fd2807SJeff Garzik 
3190c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3191c6fd2807SJeff Garzik {
3192c6fd2807SJeff Garzik 	u32 tmp;
3193c6fd2807SJeff Garzik 
3194c6fd2807SJeff Garzik 	mv5_reset_flash(hpriv, mmio);
3195c6fd2807SJeff Garzik 
3196cae5a29dSMark Lord 	tmp = readl(mmio + GPIO_PORT_CTL);
3197c6fd2807SJeff Garzik 	tmp &= 0x3;
3198c6fd2807SJeff Garzik 	tmp |= (1 << 5) | (1 << 6);
3199cae5a29dSMark Lord 	writel(tmp, mmio + GPIO_PORT_CTL);
3200c6fd2807SJeff Garzik }
3201c6fd2807SJeff Garzik 
3202c6fd2807SJeff Garzik /**
3203c6fd2807SJeff Garzik  *      mv6_reset_hc - Perform the 6xxx global soft reset
3204c6fd2807SJeff Garzik  *      @mmio: base address of the HBA
3205c6fd2807SJeff Garzik  *
3206c6fd2807SJeff Garzik  *      This routine only applies to 6xxx parts.
3207c6fd2807SJeff Garzik  *
3208c6fd2807SJeff Garzik  *      LOCKING:
3209c6fd2807SJeff Garzik  *      Inherited from caller.
3210c6fd2807SJeff Garzik  */
3211c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3212c6fd2807SJeff Garzik 			unsigned int n_hc)
3213c6fd2807SJeff Garzik {
3214cae5a29dSMark Lord 	void __iomem *reg = mmio + PCI_MAIN_CMD_STS;
3215c6fd2807SJeff Garzik 	int i, rc = 0;
3216c6fd2807SJeff Garzik 	u32 t;
3217c6fd2807SJeff Garzik 
3218c6fd2807SJeff Garzik 	/* Following procedure defined in PCI "main command and status
3219c6fd2807SJeff Garzik 	 * register" table.
3220c6fd2807SJeff Garzik 	 */
3221c6fd2807SJeff Garzik 	t = readl(reg);
3222c6fd2807SJeff Garzik 	writel(t | STOP_PCI_MASTER, reg);
3223c6fd2807SJeff Garzik 
3224c6fd2807SJeff Garzik 	for (i = 0; i < 1000; i++) {
3225c6fd2807SJeff Garzik 		udelay(1);
3226c6fd2807SJeff Garzik 		t = readl(reg);
32272dcb407eSJeff Garzik 		if (PCI_MASTER_EMPTY & t)
3228c6fd2807SJeff Garzik 			break;
3229c6fd2807SJeff Garzik 	}
3230c6fd2807SJeff Garzik 	if (!(PCI_MASTER_EMPTY & t)) {
3231c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
3232c6fd2807SJeff Garzik 		rc = 1;
3233c6fd2807SJeff Garzik 		goto done;
3234c6fd2807SJeff Garzik 	}
3235c6fd2807SJeff Garzik 
3236c6fd2807SJeff Garzik 	/* set reset */
3237c6fd2807SJeff Garzik 	i = 5;
3238c6fd2807SJeff Garzik 	do {
3239c6fd2807SJeff Garzik 		writel(t | GLOB_SFT_RST, reg);
3240c6fd2807SJeff Garzik 		t = readl(reg);
3241c6fd2807SJeff Garzik 		udelay(1);
3242c6fd2807SJeff Garzik 	} while (!(GLOB_SFT_RST & t) && (i-- > 0));
3243c6fd2807SJeff Garzik 
3244c6fd2807SJeff Garzik 	if (!(GLOB_SFT_RST & t)) {
3245c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't set global reset\n");
3246c6fd2807SJeff Garzik 		rc = 1;
3247c6fd2807SJeff Garzik 		goto done;
3248c6fd2807SJeff Garzik 	}
3249c6fd2807SJeff Garzik 
3250c6fd2807SJeff Garzik 	/* clear reset and *reenable the PCI master* (not mentioned in spec) */
3251c6fd2807SJeff Garzik 	i = 5;
3252c6fd2807SJeff Garzik 	do {
3253c6fd2807SJeff Garzik 		writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
3254c6fd2807SJeff Garzik 		t = readl(reg);
3255c6fd2807SJeff Garzik 		udelay(1);
3256c6fd2807SJeff Garzik 	} while ((GLOB_SFT_RST & t) && (i-- > 0));
3257c6fd2807SJeff Garzik 
3258c6fd2807SJeff Garzik 	if (GLOB_SFT_RST & t) {
3259c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
3260c6fd2807SJeff Garzik 		rc = 1;
3261c6fd2807SJeff Garzik 	}
3262c6fd2807SJeff Garzik done:
3263c6fd2807SJeff Garzik 	return rc;
3264c6fd2807SJeff Garzik }
3265c6fd2807SJeff Garzik 
3266c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
3267c6fd2807SJeff Garzik 			   void __iomem *mmio)
3268c6fd2807SJeff Garzik {
3269c6fd2807SJeff Garzik 	void __iomem *port_mmio;
3270c6fd2807SJeff Garzik 	u32 tmp;
3271c6fd2807SJeff Garzik 
3272cae5a29dSMark Lord 	tmp = readl(mmio + RESET_CFG);
3273c6fd2807SJeff Garzik 	if ((tmp & (1 << 0)) == 0) {
3274c6fd2807SJeff Garzik 		hpriv->signal[idx].amps = 0x7 << 8;
3275c6fd2807SJeff Garzik 		hpriv->signal[idx].pre = 0x1 << 5;
3276c6fd2807SJeff Garzik 		return;
3277c6fd2807SJeff Garzik 	}
3278c6fd2807SJeff Garzik 
3279c6fd2807SJeff Garzik 	port_mmio = mv_port_base(mmio, idx);
3280c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE2);
3281c6fd2807SJeff Garzik 
3282c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
3283c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
3284c6fd2807SJeff Garzik }
3285c6fd2807SJeff Garzik 
3286c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3287c6fd2807SJeff Garzik {
3288cae5a29dSMark Lord 	writel(0x00000060, mmio + GPIO_PORT_CTL);
3289c6fd2807SJeff Garzik }
3290c6fd2807SJeff Garzik 
3291c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3292c6fd2807SJeff Garzik 			   unsigned int port)
3293c6fd2807SJeff Garzik {
3294c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
3295c6fd2807SJeff Garzik 
3296c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
3297c6fd2807SJeff Garzik 	int fix_phy_mode2 =
3298c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
3299c6fd2807SJeff Garzik 	int fix_phy_mode4 =
3300c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
33018c30a8b9SMark Lord 	u32 m2, m3;
3302c6fd2807SJeff Garzik 
3303c6fd2807SJeff Garzik 	if (fix_phy_mode2) {
3304c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
3305c6fd2807SJeff Garzik 		m2 &= ~(1 << 16);
3306c6fd2807SJeff Garzik 		m2 |= (1 << 31);
3307c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
3308c6fd2807SJeff Garzik 
3309c6fd2807SJeff Garzik 		udelay(200);
3310c6fd2807SJeff Garzik 
3311c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
3312c6fd2807SJeff Garzik 		m2 &= ~((1 << 16) | (1 << 31));
3313c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
3314c6fd2807SJeff Garzik 
3315c6fd2807SJeff Garzik 		udelay(200);
3316c6fd2807SJeff Garzik 	}
3317c6fd2807SJeff Garzik 
33188c30a8b9SMark Lord 	/*
33198c30a8b9SMark Lord 	 * Gen-II/IIe PHY_MODE3 errata RM#2:
33208c30a8b9SMark Lord 	 * Achieves better receiver noise performance than the h/w default:
33218c30a8b9SMark Lord 	 */
33228c30a8b9SMark Lord 	m3 = readl(port_mmio + PHY_MODE3);
33238c30a8b9SMark Lord 	m3 = (m3 & 0x1f) | (0x5555601 << 5);
3324c6fd2807SJeff Garzik 
33250388a8c0SMark Lord 	/* Guideline 88F5182 (GL# SATA-S11) */
33260388a8c0SMark Lord 	if (IS_SOC(hpriv))
33270388a8c0SMark Lord 		m3 &= ~0x1c;
33280388a8c0SMark Lord 
3329c6fd2807SJeff Garzik 	if (fix_phy_mode4) {
3330ba069e37SMark Lord 		u32 m4 = readl(port_mmio + PHY_MODE4);
3331ba069e37SMark Lord 		/*
3332ba069e37SMark Lord 		 * Enforce reserved-bit restrictions on GenIIe devices only.
3333ba069e37SMark Lord 		 * For earlier chipsets, force only the internal config field
3334ba069e37SMark Lord 		 *  (workaround for errata FEr SATA#10 part 1).
3335ba069e37SMark Lord 		 */
33368c30a8b9SMark Lord 		if (IS_GEN_IIE(hpriv))
3337ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
3338ba069e37SMark Lord 		else
3339ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
33408c30a8b9SMark Lord 		writel(m4, port_mmio + PHY_MODE4);
3341c6fd2807SJeff Garzik 	}
3342b406c7a6SMark Lord 	/*
3343b406c7a6SMark Lord 	 * Workaround for 60x1-B2 errata SATA#13:
3344b406c7a6SMark Lord 	 * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
3345b406c7a6SMark Lord 	 * so we must always rewrite PHY_MODE3 after PHY_MODE4.
3346ba68460bSMark Lord 	 * Or ensure we use writelfl() when writing PHY_MODE4.
3347b406c7a6SMark Lord 	 */
3348b406c7a6SMark Lord 	writel(m3, port_mmio + PHY_MODE3);
3349c6fd2807SJeff Garzik 
3350c6fd2807SJeff Garzik 	/* Revert values of pre-emphasis and signal amps to the saved ones */
3351c6fd2807SJeff Garzik 	m2 = readl(port_mmio + PHY_MODE2);
3352c6fd2807SJeff Garzik 
3353c6fd2807SJeff Garzik 	m2 &= ~MV_M2_PREAMP_MASK;
3354c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].amps;
3355c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].pre;
3356c6fd2807SJeff Garzik 	m2 &= ~(1 << 16);
3357c6fd2807SJeff Garzik 
3358c6fd2807SJeff Garzik 	/* according to mvSata 3.6.1, some IIE values are fixed */
3359c6fd2807SJeff Garzik 	if (IS_GEN_IIE(hpriv)) {
3360c6fd2807SJeff Garzik 		m2 &= ~0xC30FF01F;
3361c6fd2807SJeff Garzik 		m2 |= 0x0000900F;
3362c6fd2807SJeff Garzik 	}
3363c6fd2807SJeff Garzik 
3364c6fd2807SJeff Garzik 	writel(m2, port_mmio + PHY_MODE2);
3365c6fd2807SJeff Garzik }
3366c6fd2807SJeff Garzik 
3367f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
3368f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
3369f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
3370f351b2d6SSaeed Bishara 				      void __iomem *mmio)
3371f351b2d6SSaeed Bishara {
3372f351b2d6SSaeed Bishara 	return;
3373f351b2d6SSaeed Bishara }
3374f351b2d6SSaeed Bishara 
3375f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
3376f351b2d6SSaeed Bishara 			   void __iomem *mmio)
3377f351b2d6SSaeed Bishara {
3378f351b2d6SSaeed Bishara 	void __iomem *port_mmio;
3379f351b2d6SSaeed Bishara 	u32 tmp;
3380f351b2d6SSaeed Bishara 
3381f351b2d6SSaeed Bishara 	port_mmio = mv_port_base(mmio, idx);
3382f351b2d6SSaeed Bishara 	tmp = readl(port_mmio + PHY_MODE2);
3383f351b2d6SSaeed Bishara 
3384f351b2d6SSaeed Bishara 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
3385f351b2d6SSaeed Bishara 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
3386f351b2d6SSaeed Bishara }
3387f351b2d6SSaeed Bishara 
3388f351b2d6SSaeed Bishara #undef ZERO
3389f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
3390f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
3391f351b2d6SSaeed Bishara 					void __iomem *mmio, unsigned int port)
3392f351b2d6SSaeed Bishara {
3393f351b2d6SSaeed Bishara 	void __iomem *port_mmio = mv_port_base(mmio, port);
3394f351b2d6SSaeed Bishara 
3395e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
3396f351b2d6SSaeed Bishara 
3397f351b2d6SSaeed Bishara 	ZERO(0x028);		/* command */
3398cae5a29dSMark Lord 	writel(0x101f, port_mmio + EDMA_CFG);
3399f351b2d6SSaeed Bishara 	ZERO(0x004);		/* timer */
3400f351b2d6SSaeed Bishara 	ZERO(0x008);		/* irq err cause */
3401f351b2d6SSaeed Bishara 	ZERO(0x00c);		/* irq err mask */
3402f351b2d6SSaeed Bishara 	ZERO(0x010);		/* rq bah */
3403f351b2d6SSaeed Bishara 	ZERO(0x014);		/* rq inp */
3404f351b2d6SSaeed Bishara 	ZERO(0x018);		/* rq outp */
3405f351b2d6SSaeed Bishara 	ZERO(0x01c);		/* respq bah */
3406f351b2d6SSaeed Bishara 	ZERO(0x024);		/* respq outp */
3407f351b2d6SSaeed Bishara 	ZERO(0x020);		/* respq inp */
3408f351b2d6SSaeed Bishara 	ZERO(0x02c);		/* test control */
3409d7b0c143SSaeed Bishara 	writel(0x800, port_mmio + EDMA_IORDY_TMOUT);
3410f351b2d6SSaeed Bishara }
3411f351b2d6SSaeed Bishara 
3412f351b2d6SSaeed Bishara #undef ZERO
3413f351b2d6SSaeed Bishara 
3414f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
3415f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
3416f351b2d6SSaeed Bishara 				       void __iomem *mmio)
3417f351b2d6SSaeed Bishara {
3418f351b2d6SSaeed Bishara 	void __iomem *hc_mmio = mv_hc_base(mmio, 0);
3419f351b2d6SSaeed Bishara 
3420f351b2d6SSaeed Bishara 	ZERO(0x00c);
3421f351b2d6SSaeed Bishara 	ZERO(0x010);
3422f351b2d6SSaeed Bishara 	ZERO(0x014);
3423f351b2d6SSaeed Bishara 
3424f351b2d6SSaeed Bishara }
3425f351b2d6SSaeed Bishara 
3426f351b2d6SSaeed Bishara #undef ZERO
3427f351b2d6SSaeed Bishara 
3428f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
3429f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc)
3430f351b2d6SSaeed Bishara {
3431f351b2d6SSaeed Bishara 	unsigned int port;
3432f351b2d6SSaeed Bishara 
3433f351b2d6SSaeed Bishara 	for (port = 0; port < hpriv->n_ports; port++)
3434f351b2d6SSaeed Bishara 		mv_soc_reset_hc_port(hpriv, mmio, port);
3435f351b2d6SSaeed Bishara 
3436f351b2d6SSaeed Bishara 	mv_soc_reset_one_hc(hpriv, mmio);
3437f351b2d6SSaeed Bishara 
3438f351b2d6SSaeed Bishara 	return 0;
3439f351b2d6SSaeed Bishara }
3440f351b2d6SSaeed Bishara 
3441f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
3442f351b2d6SSaeed Bishara 				      void __iomem *mmio)
3443f351b2d6SSaeed Bishara {
3444f351b2d6SSaeed Bishara 	return;
3445f351b2d6SSaeed Bishara }
3446f351b2d6SSaeed Bishara 
3447f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
3448f351b2d6SSaeed Bishara {
3449f351b2d6SSaeed Bishara 	return;
3450f351b2d6SSaeed Bishara }
3451f351b2d6SSaeed Bishara 
345229b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
345329b7e43cSMartin Michlmayr 				  void __iomem *mmio, unsigned int port)
345429b7e43cSMartin Michlmayr {
345529b7e43cSMartin Michlmayr 	void __iomem *port_mmio = mv_port_base(mmio, port);
345629b7e43cSMartin Michlmayr 	u32	reg;
345729b7e43cSMartin Michlmayr 
345829b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE3);
345929b7e43cSMartin Michlmayr 	reg &= ~(0x3 << 27);	/* SELMUPF (bits 28:27) to 1 */
346029b7e43cSMartin Michlmayr 	reg |= (0x1 << 27);
346129b7e43cSMartin Michlmayr 	reg &= ~(0x3 << 29);	/* SELMUPI (bits 30:29) to 1 */
346229b7e43cSMartin Michlmayr 	reg |= (0x1 << 29);
346329b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE3);
346429b7e43cSMartin Michlmayr 
346529b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE4);
346629b7e43cSMartin Michlmayr 	reg &= ~0x1;	/* SATU_OD8 (bit 0) to 0, reserved bit 16 must be set */
346729b7e43cSMartin Michlmayr 	reg |= (0x1 << 16);
346829b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE4);
346929b7e43cSMartin Michlmayr 
347029b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE9_GEN2);
347129b7e43cSMartin Michlmayr 	reg &= ~0xf;	/* TXAMP[3:0] (bits 3:0) to 8 */
347229b7e43cSMartin Michlmayr 	reg |= 0x8;
347329b7e43cSMartin Michlmayr 	reg &= ~(0x1 << 14);	/* TXAMP[4] (bit 14) to 0 */
347429b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE9_GEN2);
347529b7e43cSMartin Michlmayr 
347629b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE9_GEN1);
347729b7e43cSMartin Michlmayr 	reg &= ~0xf;	/* TXAMP[3:0] (bits 3:0) to 8 */
347829b7e43cSMartin Michlmayr 	reg |= 0x8;
347929b7e43cSMartin Michlmayr 	reg &= ~(0x1 << 14);	/* TXAMP[4] (bit 14) to 0 */
348029b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE9_GEN1);
348129b7e43cSMartin Michlmayr }
348229b7e43cSMartin Michlmayr 
348329b7e43cSMartin Michlmayr /**
348429b7e43cSMartin Michlmayr  *	soc_is_65 - check if the soc is 65 nano device
348529b7e43cSMartin Michlmayr  *
348629b7e43cSMartin Michlmayr  *	Detect the type of the SoC, this is done by reading the PHYCFG_OFS
348729b7e43cSMartin Michlmayr  *	register, this register should contain non-zero value and it exists only
348829b7e43cSMartin Michlmayr  *	in the 65 nano devices, when reading it from older devices we get 0.
348929b7e43cSMartin Michlmayr  */
349029b7e43cSMartin Michlmayr static bool soc_is_65n(struct mv_host_priv *hpriv)
349129b7e43cSMartin Michlmayr {
349229b7e43cSMartin Michlmayr 	void __iomem *port0_mmio = mv_port_base(hpriv->base, 0);
349329b7e43cSMartin Michlmayr 
349429b7e43cSMartin Michlmayr 	if (readl(port0_mmio + PHYCFG_OFS))
349529b7e43cSMartin Michlmayr 		return true;
349629b7e43cSMartin Michlmayr 	return false;
349729b7e43cSMartin Michlmayr }
349829b7e43cSMartin Michlmayr 
34998e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
3500b67a1064SMark Lord {
3501cae5a29dSMark Lord 	u32 ifcfg = readl(port_mmio + SATA_IFCFG);
3502b67a1064SMark Lord 
35038e7decdbSMark Lord 	ifcfg = (ifcfg & 0xf7f) | 0x9b1000;	/* from chip spec */
3504b67a1064SMark Lord 	if (want_gen2i)
35058e7decdbSMark Lord 		ifcfg |= (1 << 7);		/* enable gen2i speed */
3506cae5a29dSMark Lord 	writelfl(ifcfg, port_mmio + SATA_IFCFG);
3507b67a1064SMark Lord }
3508b67a1064SMark Lord 
3509e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
3510c6fd2807SJeff Garzik 			     unsigned int port_no)
3511c6fd2807SJeff Garzik {
3512c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port_no);
3513c6fd2807SJeff Garzik 
35148e7decdbSMark Lord 	/*
35158e7decdbSMark Lord 	 * The datasheet warns against setting EDMA_RESET when EDMA is active
35168e7decdbSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
35178e7decdbSMark Lord 	 * to disable the EDMA engine before doing the EDMA_RESET operation.
35188e7decdbSMark Lord 	 */
35190d8be5cbSMark Lord 	mv_stop_edma_engine(port_mmio);
3520cae5a29dSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3521c6fd2807SJeff Garzik 
3522b67a1064SMark Lord 	if (!IS_GEN_I(hpriv)) {
35238e7decdbSMark Lord 		/* Enable 3.0gb/s link speed: this survives EDMA_RESET */
35248e7decdbSMark Lord 		mv_setup_ifcfg(port_mmio, 1);
3525c6fd2807SJeff Garzik 	}
3526b67a1064SMark Lord 	/*
35278e7decdbSMark Lord 	 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
3528b67a1064SMark Lord 	 * link, and physical layers.  It resets all SATA interface registers
3529cae5a29dSMark Lord 	 * (except for SATA_IFCFG), and issues a COMRESET to the dev.
3530c6fd2807SJeff Garzik 	 */
3531cae5a29dSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3532b67a1064SMark Lord 	udelay(25);	/* allow reset propagation */
3533cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_CMD);
3534c6fd2807SJeff Garzik 
3535c6fd2807SJeff Garzik 	hpriv->ops->phy_errata(hpriv, mmio, port_no);
3536c6fd2807SJeff Garzik 
3537ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv))
3538c6fd2807SJeff Garzik 		mdelay(1);
3539c6fd2807SJeff Garzik }
3540c6fd2807SJeff Garzik 
3541e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
3542e49856d8SMark Lord {
3543e49856d8SMark Lord 	if (sata_pmp_supported(ap)) {
3544e49856d8SMark Lord 		void __iomem *port_mmio = mv_ap_base(ap);
3545cae5a29dSMark Lord 		u32 reg = readl(port_mmio + SATA_IFCTL);
3546e49856d8SMark Lord 		int old = reg & 0xf;
3547e49856d8SMark Lord 
3548e49856d8SMark Lord 		if (old != pmp) {
3549e49856d8SMark Lord 			reg = (reg & ~0xf) | pmp;
3550cae5a29dSMark Lord 			writelfl(reg, port_mmio + SATA_IFCTL);
3551e49856d8SMark Lord 		}
3552e49856d8SMark Lord 	}
3553e49856d8SMark Lord }
3554e49856d8SMark Lord 
3555e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
3556bdd4dddeSJeff Garzik 				unsigned long deadline)
3557c6fd2807SJeff Garzik {
3558e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
3559e49856d8SMark Lord 	return sata_std_hardreset(link, class, deadline);
3560e49856d8SMark Lord }
3561c6fd2807SJeff Garzik 
3562e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
3563e49856d8SMark Lord 				unsigned long deadline)
3564da3dbb17STejun Heo {
3565e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
3566e49856d8SMark Lord 	return ata_sff_softreset(link, class, deadline);
3567bdd4dddeSJeff Garzik }
3568bdd4dddeSJeff Garzik 
3569cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
3570bdd4dddeSJeff Garzik 			unsigned long deadline)
3571bdd4dddeSJeff Garzik {
3572cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3573bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
3574b562468cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
3575f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
35760d8be5cbSMark Lord 	int rc, attempts = 0, extra = 0;
35770d8be5cbSMark Lord 	u32 sstatus;
35780d8be5cbSMark Lord 	bool online;
3579bdd4dddeSJeff Garzik 
3580e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, ap->port_no);
3581b562468cSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
3582d16ab3f6SMark Lord 	pp->pp_flags &=
3583d16ab3f6SMark Lord 	  ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
3584bdd4dddeSJeff Garzik 
35850d8be5cbSMark Lord 	/* Workaround for errata FEr SATA#10 (part 2) */
35860d8be5cbSMark Lord 	do {
358717c5aab5SMark Lord 		const unsigned long *timing =
358817c5aab5SMark Lord 				sata_ehc_deb_timing(&link->eh_context);
3589bdd4dddeSJeff Garzik 
359017c5aab5SMark Lord 		rc = sata_link_hardreset(link, timing, deadline + extra,
359117c5aab5SMark Lord 					 &online, NULL);
35929dcffd99SMark Lord 		rc = online ? -EAGAIN : rc;
359317c5aab5SMark Lord 		if (rc)
35940d8be5cbSMark Lord 			return rc;
35950d8be5cbSMark Lord 		sata_scr_read(link, SCR_STATUS, &sstatus);
35960d8be5cbSMark Lord 		if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
35970d8be5cbSMark Lord 			/* Force 1.5gb/s link speed and try again */
35988e7decdbSMark Lord 			mv_setup_ifcfg(mv_ap_base(ap), 0);
35990d8be5cbSMark Lord 			if (time_after(jiffies + HZ, deadline))
36000d8be5cbSMark Lord 				extra = HZ; /* only extend it once, max */
3601bdd4dddeSJeff Garzik 		}
36020d8be5cbSMark Lord 	} while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
360308da1759SMark Lord 	mv_save_cached_regs(ap);
360466e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
3605bdd4dddeSJeff Garzik 
360617c5aab5SMark Lord 	return rc;
3607bdd4dddeSJeff Garzik }
3608bdd4dddeSJeff Garzik 
3609bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
3610c6fd2807SJeff Garzik {
36111cfd19aeSMark Lord 	mv_stop_edma(ap);
3612c4de573bSMark Lord 	mv_enable_port_irqs(ap, 0);
3613c6fd2807SJeff Garzik }
3614bdd4dddeSJeff Garzik 
3615bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
3616bdd4dddeSJeff Garzik {
3617f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
3618c4de573bSMark Lord 	unsigned int port = ap->port_no;
3619c4de573bSMark Lord 	unsigned int hardport = mv_hardport_from_port(port);
36201cfd19aeSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
3621bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
3622c4de573bSMark Lord 	u32 hc_irq_cause;
3623bdd4dddeSJeff Garzik 
3624bdd4dddeSJeff Garzik 	/* clear EDMA errors on this port */
3625cae5a29dSMark Lord 	writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3626bdd4dddeSJeff Garzik 
3627bdd4dddeSJeff Garzik 	/* clear pending irq events */
3628cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
3629cae5a29dSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
3630bdd4dddeSJeff Garzik 
363188e675e1SMark Lord 	mv_enable_port_irqs(ap, ERR_IRQ);
3632c6fd2807SJeff Garzik }
3633c6fd2807SJeff Garzik 
3634c6fd2807SJeff Garzik /**
3635c6fd2807SJeff Garzik  *      mv_port_init - Perform some early initialization on a single port.
3636c6fd2807SJeff Garzik  *      @port: libata data structure storing shadow register addresses
3637c6fd2807SJeff Garzik  *      @port_mmio: base address of the port
3638c6fd2807SJeff Garzik  *
3639c6fd2807SJeff Garzik  *      Initialize shadow register mmio addresses, clear outstanding
3640c6fd2807SJeff Garzik  *      interrupts on the port, and unmask interrupts for the future
3641c6fd2807SJeff Garzik  *      start of the port.
3642c6fd2807SJeff Garzik  *
3643c6fd2807SJeff Garzik  *      LOCKING:
3644c6fd2807SJeff Garzik  *      Inherited from caller.
3645c6fd2807SJeff Garzik  */
3646c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
3647c6fd2807SJeff Garzik {
3648cae5a29dSMark Lord 	void __iomem *serr, *shd_base = port_mmio + SHD_BLK;
3649c6fd2807SJeff Garzik 
3650c6fd2807SJeff Garzik 	/* PIO related setup
3651c6fd2807SJeff Garzik 	 */
3652c6fd2807SJeff Garzik 	port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
3653c6fd2807SJeff Garzik 	port->error_addr =
3654c6fd2807SJeff Garzik 		port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
3655c6fd2807SJeff Garzik 	port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
3656c6fd2807SJeff Garzik 	port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
3657c6fd2807SJeff Garzik 	port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
3658c6fd2807SJeff Garzik 	port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
3659c6fd2807SJeff Garzik 	port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
3660c6fd2807SJeff Garzik 	port->status_addr =
3661c6fd2807SJeff Garzik 		port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
3662c6fd2807SJeff Garzik 	/* special case: control/altstatus doesn't have ATA_REG_ address */
3663cae5a29dSMark Lord 	port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST;
3664c6fd2807SJeff Garzik 
3665c6fd2807SJeff Garzik 	/* Clear any currently outstanding port interrupt conditions */
3666cae5a29dSMark Lord 	serr = port_mmio + mv_scr_offset(SCR_ERROR);
3667cae5a29dSMark Lord 	writelfl(readl(serr), serr);
3668cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3669c6fd2807SJeff Garzik 
3670646a4da5SMark Lord 	/* unmask all non-transient EDMA error interrupts */
3671cae5a29dSMark Lord 	writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK);
3672c6fd2807SJeff Garzik 
3673c6fd2807SJeff Garzik 	VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
3674cae5a29dSMark Lord 		readl(port_mmio + EDMA_CFG),
3675cae5a29dSMark Lord 		readl(port_mmio + EDMA_ERR_IRQ_CAUSE),
3676cae5a29dSMark Lord 		readl(port_mmio + EDMA_ERR_IRQ_MASK));
3677c6fd2807SJeff Garzik }
3678c6fd2807SJeff Garzik 
3679616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host)
3680616d4a98SMark Lord {
3681616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3682616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
3683616d4a98SMark Lord 	u32 reg;
3684616d4a98SMark Lord 
36851f398472SMark Lord 	if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
3686616d4a98SMark Lord 		return 0;	/* not PCI-X capable */
3687cae5a29dSMark Lord 	reg = readl(mmio + MV_PCI_MODE);
3688616d4a98SMark Lord 	if ((reg & MV_PCI_MODE_MASK) == 0)
3689616d4a98SMark Lord 		return 0;	/* conventional PCI mode */
3690616d4a98SMark Lord 	return 1;	/* chip is in PCI-X mode */
3691616d4a98SMark Lord }
3692616d4a98SMark Lord 
3693616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host)
3694616d4a98SMark Lord {
3695616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3696616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
3697616d4a98SMark Lord 	u32 reg;
3698616d4a98SMark Lord 
3699616d4a98SMark Lord 	if (!mv_in_pcix_mode(host)) {
3700cae5a29dSMark Lord 		reg = readl(mmio + MV_PCI_COMMAND);
3701cae5a29dSMark Lord 		if (reg & MV_PCI_COMMAND_MRDTRIG)
3702616d4a98SMark Lord 			return 0; /* not okay */
3703616d4a98SMark Lord 	}
3704616d4a98SMark Lord 	return 1; /* okay */
3705616d4a98SMark Lord }
3706616d4a98SMark Lord 
370765ad7fefSMark Lord static void mv_60x1b2_errata_pci7(struct ata_host *host)
370865ad7fefSMark Lord {
370965ad7fefSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
371065ad7fefSMark Lord 	void __iomem *mmio = hpriv->base;
371165ad7fefSMark Lord 
371265ad7fefSMark Lord 	/* workaround for 60x1-B2 errata PCI#7 */
371365ad7fefSMark Lord 	if (mv_in_pcix_mode(host)) {
3714cae5a29dSMark Lord 		u32 reg = readl(mmio + MV_PCI_COMMAND);
3715cae5a29dSMark Lord 		writelfl(reg & ~MV_PCI_COMMAND_MWRCOM, mmio + MV_PCI_COMMAND);
371665ad7fefSMark Lord 	}
371765ad7fefSMark Lord }
371865ad7fefSMark Lord 
37194447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
3720c6fd2807SJeff Garzik {
37214447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
37224447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3723c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
3724c6fd2807SJeff Garzik 
3725c6fd2807SJeff Garzik 	switch (board_idx) {
3726c6fd2807SJeff Garzik 	case chip_5080:
3727c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
3728ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
3729c6fd2807SJeff Garzik 
373044c10138SAuke Kok 		switch (pdev->revision) {
3731c6fd2807SJeff Garzik 		case 0x1:
3732c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
3733c6fd2807SJeff Garzik 			break;
3734c6fd2807SJeff Garzik 		case 0x3:
3735c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3736c6fd2807SJeff Garzik 			break;
3737c6fd2807SJeff Garzik 		default:
3738c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
3739c6fd2807SJeff Garzik 			   "Applying 50XXB2 workarounds to unknown rev\n");
3740c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3741c6fd2807SJeff Garzik 			break;
3742c6fd2807SJeff Garzik 		}
3743c6fd2807SJeff Garzik 		break;
3744c6fd2807SJeff Garzik 
3745c6fd2807SJeff Garzik 	case chip_504x:
3746c6fd2807SJeff Garzik 	case chip_508x:
3747c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
3748ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
3749c6fd2807SJeff Garzik 
375044c10138SAuke Kok 		switch (pdev->revision) {
3751c6fd2807SJeff Garzik 		case 0x0:
3752c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
3753c6fd2807SJeff Garzik 			break;
3754c6fd2807SJeff Garzik 		case 0x3:
3755c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3756c6fd2807SJeff Garzik 			break;
3757c6fd2807SJeff Garzik 		default:
3758c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
3759c6fd2807SJeff Garzik 			   "Applying B2 workarounds to unknown rev\n");
3760c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3761c6fd2807SJeff Garzik 			break;
3762c6fd2807SJeff Garzik 		}
3763c6fd2807SJeff Garzik 		break;
3764c6fd2807SJeff Garzik 
3765c6fd2807SJeff Garzik 	case chip_604x:
3766c6fd2807SJeff Garzik 	case chip_608x:
3767c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
3768ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_II;
3769c6fd2807SJeff Garzik 
377044c10138SAuke Kok 		switch (pdev->revision) {
3771c6fd2807SJeff Garzik 		case 0x7:
377265ad7fefSMark Lord 			mv_60x1b2_errata_pci7(host);
3773c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
3774c6fd2807SJeff Garzik 			break;
3775c6fd2807SJeff Garzik 		case 0x9:
3776c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3777c6fd2807SJeff Garzik 			break;
3778c6fd2807SJeff Garzik 		default:
3779c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
3780c6fd2807SJeff Garzik 				   "Applying B2 workarounds to unknown rev\n");
3781c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
3782c6fd2807SJeff Garzik 			break;
3783c6fd2807SJeff Garzik 		}
3784c6fd2807SJeff Garzik 		break;
3785c6fd2807SJeff Garzik 
3786c6fd2807SJeff Garzik 	case chip_7042:
3787616d4a98SMark Lord 		hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
3788306b30f7SMark Lord 		if (pdev->vendor == PCI_VENDOR_ID_TTI &&
3789306b30f7SMark Lord 		    (pdev->device == 0x2300 || pdev->device == 0x2310))
3790306b30f7SMark Lord 		{
37914e520033SMark Lord 			/*
37924e520033SMark Lord 			 * Highpoint RocketRAID PCIe 23xx series cards:
37934e520033SMark Lord 			 *
37944e520033SMark Lord 			 * Unconfigured drives are treated as "Legacy"
37954e520033SMark Lord 			 * by the BIOS, and it overwrites sector 8 with
37964e520033SMark Lord 			 * a "Lgcy" metadata block prior to Linux boot.
37974e520033SMark Lord 			 *
37984e520033SMark Lord 			 * Configured drives (RAID or JBOD) leave sector 8
37994e520033SMark Lord 			 * alone, but instead overwrite a high numbered
38004e520033SMark Lord 			 * sector for the RAID metadata.  This sector can
38014e520033SMark Lord 			 * be determined exactly, by truncating the physical
38024e520033SMark Lord 			 * drive capacity to a nice even GB value.
38034e520033SMark Lord 			 *
38044e520033SMark Lord 			 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
38054e520033SMark Lord 			 *
38064e520033SMark Lord 			 * Warn the user, lest they think we're just buggy.
38074e520033SMark Lord 			 */
38084e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
38094e520033SMark Lord 				" BIOS CORRUPTS DATA on all attached drives,"
38104e520033SMark Lord 				" regardless of if/how they are configured."
38114e520033SMark Lord 				" BEWARE!\n");
38124e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": For data safety, do not"
38134e520033SMark Lord 				" use sectors 8-9 on \"Legacy\" drives,"
38144e520033SMark Lord 				" and avoid the final two gigabytes on"
38154e520033SMark Lord 				" all RocketRAID BIOS initialized drives.\n");
3816306b30f7SMark Lord 		}
38178e7decdbSMark Lord 		/* drop through */
3818c6fd2807SJeff Garzik 	case chip_6042:
3819c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
3820c6fd2807SJeff Garzik 		hp_flags |= MV_HP_GEN_IIE;
3821616d4a98SMark Lord 		if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
3822616d4a98SMark Lord 			hp_flags |= MV_HP_CUT_THROUGH;
3823c6fd2807SJeff Garzik 
382444c10138SAuke Kok 		switch (pdev->revision) {
38255cf73bfbSMark Lord 		case 0x2: /* Rev.B0: the first/only public release */
3826c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3827c6fd2807SJeff Garzik 			break;
3828c6fd2807SJeff Garzik 		default:
3829c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
3830c6fd2807SJeff Garzik 			   "Applying 60X1C0 workarounds to unknown rev\n");
3831c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3832c6fd2807SJeff Garzik 			break;
3833c6fd2807SJeff Garzik 		}
3834c6fd2807SJeff Garzik 		break;
3835f351b2d6SSaeed Bishara 	case chip_soc:
383629b7e43cSMartin Michlmayr 		if (soc_is_65n(hpriv))
383729b7e43cSMartin Michlmayr 			hpriv->ops = &mv_soc_65n_ops;
383829b7e43cSMartin Michlmayr 		else
3839f351b2d6SSaeed Bishara 			hpriv->ops = &mv_soc_ops;
3840eb3a55a9SSaeed Bishara 		hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3841eb3a55a9SSaeed Bishara 			MV_HP_ERRATA_60X1C0;
3842f351b2d6SSaeed Bishara 		break;
3843c6fd2807SJeff Garzik 
3844c6fd2807SJeff Garzik 	default:
3845f351b2d6SSaeed Bishara 		dev_printk(KERN_ERR, host->dev,
38465796d1c4SJeff Garzik 			   "BUG: invalid board index %u\n", board_idx);
3847c6fd2807SJeff Garzik 		return 1;
3848c6fd2807SJeff Garzik 	}
3849c6fd2807SJeff Garzik 
3850c6fd2807SJeff Garzik 	hpriv->hp_flags = hp_flags;
385102a121daSMark Lord 	if (hp_flags & MV_HP_PCIE) {
3852cae5a29dSMark Lord 		hpriv->irq_cause_offset	= PCIE_IRQ_CAUSE;
3853cae5a29dSMark Lord 		hpriv->irq_mask_offset	= PCIE_IRQ_MASK;
385402a121daSMark Lord 		hpriv->unmask_all_irqs	= PCIE_UNMASK_ALL_IRQS;
385502a121daSMark Lord 	} else {
3856cae5a29dSMark Lord 		hpriv->irq_cause_offset	= PCI_IRQ_CAUSE;
3857cae5a29dSMark Lord 		hpriv->irq_mask_offset	= PCI_IRQ_MASK;
385802a121daSMark Lord 		hpriv->unmask_all_irqs	= PCI_UNMASK_ALL_IRQS;
385902a121daSMark Lord 	}
3860c6fd2807SJeff Garzik 
3861c6fd2807SJeff Garzik 	return 0;
3862c6fd2807SJeff Garzik }
3863c6fd2807SJeff Garzik 
3864c6fd2807SJeff Garzik /**
3865c6fd2807SJeff Garzik  *      mv_init_host - Perform some early initialization of the host.
38664447d351STejun Heo  *	@host: ATA host to initialize
3867c6fd2807SJeff Garzik  *
3868c6fd2807SJeff Garzik  *      If possible, do an early global reset of the host.  Then do
3869c6fd2807SJeff Garzik  *      our port init and clear/unmask all/relevant host interrupts.
3870c6fd2807SJeff Garzik  *
3871c6fd2807SJeff Garzik  *      LOCKING:
3872c6fd2807SJeff Garzik  *      Inherited from caller.
3873c6fd2807SJeff Garzik  */
38741bfeff03SSaeed Bishara static int mv_init_host(struct ata_host *host)
3875c6fd2807SJeff Garzik {
3876c6fd2807SJeff Garzik 	int rc = 0, n_hc, port, hc;
38774447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3878f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
3879c6fd2807SJeff Garzik 
38801bfeff03SSaeed Bishara 	rc = mv_chip_id(host, hpriv->board_idx);
3881c6fd2807SJeff Garzik 	if (rc)
3882c6fd2807SJeff Garzik 		goto done;
3883c6fd2807SJeff Garzik 
38841f398472SMark Lord 	if (IS_SOC(hpriv)) {
3885cae5a29dSMark Lord 		hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE;
3886cae5a29dSMark Lord 		hpriv->main_irq_mask_addr  = mmio + SOC_HC_MAIN_IRQ_MASK;
38871f398472SMark Lord 	} else {
3888cae5a29dSMark Lord 		hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE;
3889cae5a29dSMark Lord 		hpriv->main_irq_mask_addr  = mmio + PCI_HC_MAIN_IRQ_MASK;
3890f351b2d6SSaeed Bishara 	}
3891352fab70SMark Lord 
38925d0fb2e7SThomas Reitmayr 	/* initialize shadow irq mask with register's value */
38935d0fb2e7SThomas Reitmayr 	hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
38945d0fb2e7SThomas Reitmayr 
3895352fab70SMark Lord 	/* global interrupt mask: 0 == mask everything */
3896c4de573bSMark Lord 	mv_set_main_irq_mask(host, ~0, 0);
3897f351b2d6SSaeed Bishara 
38984447d351STejun Heo 	n_hc = mv_get_hc_count(host->ports[0]->flags);
3899c6fd2807SJeff Garzik 
39004447d351STejun Heo 	for (port = 0; port < host->n_ports; port++)
390129b7e43cSMartin Michlmayr 		if (hpriv->ops->read_preamp)
3902c6fd2807SJeff Garzik 			hpriv->ops->read_preamp(hpriv, port, mmio);
3903c6fd2807SJeff Garzik 
3904c6fd2807SJeff Garzik 	rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
3905c6fd2807SJeff Garzik 	if (rc)
3906c6fd2807SJeff Garzik 		goto done;
3907c6fd2807SJeff Garzik 
3908c6fd2807SJeff Garzik 	hpriv->ops->reset_flash(hpriv, mmio);
39097bb3c529SSaeed Bishara 	hpriv->ops->reset_bus(host, mmio);
3910c6fd2807SJeff Garzik 	hpriv->ops->enable_leds(hpriv, mmio);
3911c6fd2807SJeff Garzik 
39124447d351STejun Heo 	for (port = 0; port < host->n_ports; port++) {
3913cbcdd875STejun Heo 		struct ata_port *ap = host->ports[port];
3914c6fd2807SJeff Garzik 		void __iomem *port_mmio = mv_port_base(mmio, port);
3915cbcdd875STejun Heo 
3916cbcdd875STejun Heo 		mv_port_init(&ap->ioaddr, port_mmio);
3917c6fd2807SJeff Garzik 	}
3918c6fd2807SJeff Garzik 
3919c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3920c6fd2807SJeff Garzik 		void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3921c6fd2807SJeff Garzik 
3922c6fd2807SJeff Garzik 		VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
3923c6fd2807SJeff Garzik 			"(before clear)=0x%08x\n", hc,
3924cae5a29dSMark Lord 			readl(hc_mmio + HC_CFG),
3925cae5a29dSMark Lord 			readl(hc_mmio + HC_IRQ_CAUSE));
3926c6fd2807SJeff Garzik 
3927c6fd2807SJeff Garzik 		/* Clear any currently outstanding hc interrupt conditions */
3928cae5a29dSMark Lord 		writelfl(0, hc_mmio + HC_IRQ_CAUSE);
3929c6fd2807SJeff Garzik 	}
3930c6fd2807SJeff Garzik 
393144c65d16SMark Lord 	if (!IS_SOC(hpriv)) {
3932c6fd2807SJeff Garzik 		/* Clear any currently outstanding host interrupt conditions */
3933cae5a29dSMark Lord 		writelfl(0, mmio + hpriv->irq_cause_offset);
3934c6fd2807SJeff Garzik 
3935c6fd2807SJeff Garzik 		/* and unmask interrupt generation for host regs */
3936cae5a29dSMark Lord 		writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_offset);
393744c65d16SMark Lord 	}
3938c6fd2807SJeff Garzik 
393951de32d2SMark Lord 	/*
394051de32d2SMark Lord 	 * enable only global host interrupts for now.
394151de32d2SMark Lord 	 * The per-port interrupts get done later as ports are set up.
394251de32d2SMark Lord 	 */
3943c4de573bSMark Lord 	mv_set_main_irq_mask(host, 0, PCI_ERR);
39442b748a0aSMark Lord 	mv_set_irq_coalescing(host, irq_coalescing_io_count,
39452b748a0aSMark Lord 				    irq_coalescing_usecs);
3946c6fd2807SJeff Garzik done:
3947c6fd2807SJeff Garzik 	return rc;
3948c6fd2807SJeff Garzik }
3949c6fd2807SJeff Garzik 
3950fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
3951fbf14e2fSByron Bradley {
3952fbf14e2fSByron Bradley 	hpriv->crqb_pool   = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
3953fbf14e2fSByron Bradley 							     MV_CRQB_Q_SZ, 0);
3954fbf14e2fSByron Bradley 	if (!hpriv->crqb_pool)
3955fbf14e2fSByron Bradley 		return -ENOMEM;
3956fbf14e2fSByron Bradley 
3957fbf14e2fSByron Bradley 	hpriv->crpb_pool   = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
3958fbf14e2fSByron Bradley 							     MV_CRPB_Q_SZ, 0);
3959fbf14e2fSByron Bradley 	if (!hpriv->crpb_pool)
3960fbf14e2fSByron Bradley 		return -ENOMEM;
3961fbf14e2fSByron Bradley 
3962fbf14e2fSByron Bradley 	hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
3963fbf14e2fSByron Bradley 							     MV_SG_TBL_SZ, 0);
3964fbf14e2fSByron Bradley 	if (!hpriv->sg_tbl_pool)
3965fbf14e2fSByron Bradley 		return -ENOMEM;
3966fbf14e2fSByron Bradley 
3967fbf14e2fSByron Bradley 	return 0;
3968fbf14e2fSByron Bradley }
3969fbf14e2fSByron Bradley 
397015a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
397115a32632SLennert Buytenhek 				 struct mbus_dram_target_info *dram)
397215a32632SLennert Buytenhek {
397315a32632SLennert Buytenhek 	int i;
397415a32632SLennert Buytenhek 
397515a32632SLennert Buytenhek 	for (i = 0; i < 4; i++) {
397615a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_CTRL(i));
397715a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_BASE(i));
397815a32632SLennert Buytenhek 	}
397915a32632SLennert Buytenhek 
398015a32632SLennert Buytenhek 	for (i = 0; i < dram->num_cs; i++) {
398115a32632SLennert Buytenhek 		struct mbus_dram_window *cs = dram->cs + i;
398215a32632SLennert Buytenhek 
398315a32632SLennert Buytenhek 		writel(((cs->size - 1) & 0xffff0000) |
398415a32632SLennert Buytenhek 			(cs->mbus_attr << 8) |
398515a32632SLennert Buytenhek 			(dram->mbus_dram_target_id << 4) | 1,
398615a32632SLennert Buytenhek 			hpriv->base + WINDOW_CTRL(i));
398715a32632SLennert Buytenhek 		writel(cs->base, hpriv->base + WINDOW_BASE(i));
398815a32632SLennert Buytenhek 	}
398915a32632SLennert Buytenhek }
399015a32632SLennert Buytenhek 
3991f351b2d6SSaeed Bishara /**
3992f351b2d6SSaeed Bishara  *      mv_platform_probe - handle a positive probe of an soc Marvell
3993f351b2d6SSaeed Bishara  *      host
3994f351b2d6SSaeed Bishara  *      @pdev: platform device found
3995f351b2d6SSaeed Bishara  *
3996f351b2d6SSaeed Bishara  *      LOCKING:
3997f351b2d6SSaeed Bishara  *      Inherited from caller.
3998f351b2d6SSaeed Bishara  */
3999f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
4000f351b2d6SSaeed Bishara {
4001f351b2d6SSaeed Bishara 	static int printed_version;
4002f351b2d6SSaeed Bishara 	const struct mv_sata_platform_data *mv_platform_data;
4003f351b2d6SSaeed Bishara 	const struct ata_port_info *ppi[] =
4004f351b2d6SSaeed Bishara 	    { &mv_port_info[chip_soc], NULL };
4005f351b2d6SSaeed Bishara 	struct ata_host *host;
4006f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv;
4007f351b2d6SSaeed Bishara 	struct resource *res;
4008f351b2d6SSaeed Bishara 	int n_ports, rc;
4009f351b2d6SSaeed Bishara 
4010f351b2d6SSaeed Bishara 	if (!printed_version++)
4011f351b2d6SSaeed Bishara 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
4012f351b2d6SSaeed Bishara 
4013f351b2d6SSaeed Bishara 	/*
4014f351b2d6SSaeed Bishara 	 * Simple resource validation ..
4015f351b2d6SSaeed Bishara 	 */
4016f351b2d6SSaeed Bishara 	if (unlikely(pdev->num_resources != 2)) {
4017f351b2d6SSaeed Bishara 		dev_err(&pdev->dev, "invalid number of resources\n");
4018f351b2d6SSaeed Bishara 		return -EINVAL;
4019f351b2d6SSaeed Bishara 	}
4020f351b2d6SSaeed Bishara 
4021f351b2d6SSaeed Bishara 	/*
4022f351b2d6SSaeed Bishara 	 * Get the register base first
4023f351b2d6SSaeed Bishara 	 */
4024f351b2d6SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4025f351b2d6SSaeed Bishara 	if (res == NULL)
4026f351b2d6SSaeed Bishara 		return -EINVAL;
4027f351b2d6SSaeed Bishara 
4028f351b2d6SSaeed Bishara 	/* allocate host */
4029f351b2d6SSaeed Bishara 	mv_platform_data = pdev->dev.platform_data;
4030f351b2d6SSaeed Bishara 	n_ports = mv_platform_data->n_ports;
4031f351b2d6SSaeed Bishara 
4032f351b2d6SSaeed Bishara 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
4033f351b2d6SSaeed Bishara 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
4034f351b2d6SSaeed Bishara 
4035f351b2d6SSaeed Bishara 	if (!host || !hpriv)
4036f351b2d6SSaeed Bishara 		return -ENOMEM;
4037f351b2d6SSaeed Bishara 	host->private_data = hpriv;
4038f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
40391bfeff03SSaeed Bishara 	hpriv->board_idx = chip_soc;
4040f351b2d6SSaeed Bishara 
4041f351b2d6SSaeed Bishara 	host->iomap = NULL;
4042f1cb0ea1SSaeed Bishara 	hpriv->base = devm_ioremap(&pdev->dev, res->start,
4043041b5eacSJulia Lawall 				   resource_size(res));
4044cae5a29dSMark Lord 	hpriv->base -= SATAHC0_REG_BASE;
4045f351b2d6SSaeed Bishara 
4046c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4047c77a2f4eSSaeed Bishara 	hpriv->clk = clk_get(&pdev->dev, NULL);
4048c77a2f4eSSaeed Bishara 	if (IS_ERR(hpriv->clk))
4049c77a2f4eSSaeed Bishara 		dev_notice(&pdev->dev, "cannot get clkdev\n");
4050c77a2f4eSSaeed Bishara 	else
4051c77a2f4eSSaeed Bishara 		clk_enable(hpriv->clk);
4052c77a2f4eSSaeed Bishara #endif
4053c77a2f4eSSaeed Bishara 
405415a32632SLennert Buytenhek 	/*
405515a32632SLennert Buytenhek 	 * (Re-)program MBUS remapping windows if we are asked to.
405615a32632SLennert Buytenhek 	 */
405715a32632SLennert Buytenhek 	if (mv_platform_data->dram != NULL)
405815a32632SLennert Buytenhek 		mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
405915a32632SLennert Buytenhek 
4060fbf14e2fSByron Bradley 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
4061fbf14e2fSByron Bradley 	if (rc)
4062c77a2f4eSSaeed Bishara 		goto err;
4063fbf14e2fSByron Bradley 
4064f351b2d6SSaeed Bishara 	/* initialize adapter */
40651bfeff03SSaeed Bishara 	rc = mv_init_host(host);
4066f351b2d6SSaeed Bishara 	if (rc)
4067c77a2f4eSSaeed Bishara 		goto err;
4068f351b2d6SSaeed Bishara 
4069f351b2d6SSaeed Bishara 	dev_printk(KERN_INFO, &pdev->dev,
4070f351b2d6SSaeed Bishara 		   "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
4071f351b2d6SSaeed Bishara 		   host->n_ports);
4072f351b2d6SSaeed Bishara 
4073f351b2d6SSaeed Bishara 	return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
4074f351b2d6SSaeed Bishara 				 IRQF_SHARED, &mv6_sht);
4075c77a2f4eSSaeed Bishara err:
4076c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4077c77a2f4eSSaeed Bishara 	if (!IS_ERR(hpriv->clk)) {
4078c77a2f4eSSaeed Bishara 		clk_disable(hpriv->clk);
4079c77a2f4eSSaeed Bishara 		clk_put(hpriv->clk);
4080c77a2f4eSSaeed Bishara 	}
4081c77a2f4eSSaeed Bishara #endif
4082c77a2f4eSSaeed Bishara 
4083c77a2f4eSSaeed Bishara 	return rc;
4084f351b2d6SSaeed Bishara }
4085f351b2d6SSaeed Bishara 
4086f351b2d6SSaeed Bishara /*
4087f351b2d6SSaeed Bishara  *
4088f351b2d6SSaeed Bishara  *      mv_platform_remove    -       unplug a platform interface
4089f351b2d6SSaeed Bishara  *      @pdev: platform device
4090f351b2d6SSaeed Bishara  *
4091f351b2d6SSaeed Bishara  *      A platform bus SATA device has been unplugged. Perform the needed
4092f351b2d6SSaeed Bishara  *      cleanup. Also called on module unload for any active devices.
4093f351b2d6SSaeed Bishara  */
4094f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev)
4095f351b2d6SSaeed Bishara {
4096f351b2d6SSaeed Bishara 	struct device *dev = &pdev->dev;
4097f351b2d6SSaeed Bishara 	struct ata_host *host = dev_get_drvdata(dev);
4098c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4099c77a2f4eSSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
4100c77a2f4eSSaeed Bishara #endif
4101f351b2d6SSaeed Bishara 	ata_host_detach(host);
4102c77a2f4eSSaeed Bishara 
4103c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4104c77a2f4eSSaeed Bishara 	if (!IS_ERR(hpriv->clk)) {
4105c77a2f4eSSaeed Bishara 		clk_disable(hpriv->clk);
4106c77a2f4eSSaeed Bishara 		clk_put(hpriv->clk);
4107c77a2f4eSSaeed Bishara 	}
4108c77a2f4eSSaeed Bishara #endif
4109f351b2d6SSaeed Bishara 	return 0;
4110f351b2d6SSaeed Bishara }
4111f351b2d6SSaeed Bishara 
41126481f2b5SSaeed Bishara #ifdef CONFIG_PM
41136481f2b5SSaeed Bishara static int mv_platform_suspend(struct platform_device *pdev, pm_message_t state)
41146481f2b5SSaeed Bishara {
41156481f2b5SSaeed Bishara 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
41166481f2b5SSaeed Bishara 	if (host)
41176481f2b5SSaeed Bishara 		return ata_host_suspend(host, state);
41186481f2b5SSaeed Bishara 	else
41196481f2b5SSaeed Bishara 		return 0;
41206481f2b5SSaeed Bishara }
41216481f2b5SSaeed Bishara 
41226481f2b5SSaeed Bishara static int mv_platform_resume(struct platform_device *pdev)
41236481f2b5SSaeed Bishara {
41246481f2b5SSaeed Bishara 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
41256481f2b5SSaeed Bishara 	int ret;
41266481f2b5SSaeed Bishara 
41276481f2b5SSaeed Bishara 	if (host) {
41286481f2b5SSaeed Bishara 		struct mv_host_priv *hpriv = host->private_data;
41296481f2b5SSaeed Bishara 		const struct mv_sata_platform_data *mv_platform_data = \
41306481f2b5SSaeed Bishara 			pdev->dev.platform_data;
41316481f2b5SSaeed Bishara 		/*
41326481f2b5SSaeed Bishara 		 * (Re-)program MBUS remapping windows if we are asked to.
41336481f2b5SSaeed Bishara 		 */
41346481f2b5SSaeed Bishara 		if (mv_platform_data->dram != NULL)
41356481f2b5SSaeed Bishara 			mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
41366481f2b5SSaeed Bishara 
41376481f2b5SSaeed Bishara 		/* initialize adapter */
41381bfeff03SSaeed Bishara 		ret = mv_init_host(host);
41396481f2b5SSaeed Bishara 		if (ret) {
41406481f2b5SSaeed Bishara 			printk(KERN_ERR DRV_NAME ": Error during HW init\n");
41416481f2b5SSaeed Bishara 			return ret;
41426481f2b5SSaeed Bishara 		}
41436481f2b5SSaeed Bishara 		ata_host_resume(host);
41446481f2b5SSaeed Bishara 	}
41456481f2b5SSaeed Bishara 
41466481f2b5SSaeed Bishara 	return 0;
41476481f2b5SSaeed Bishara }
41486481f2b5SSaeed Bishara #else
41496481f2b5SSaeed Bishara #define mv_platform_suspend NULL
41506481f2b5SSaeed Bishara #define mv_platform_resume NULL
41516481f2b5SSaeed Bishara #endif
41526481f2b5SSaeed Bishara 
4153f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
4154f351b2d6SSaeed Bishara 	.probe			= mv_platform_probe,
4155f351b2d6SSaeed Bishara 	.remove			= __devexit_p(mv_platform_remove),
41566481f2b5SSaeed Bishara 	.suspend		= mv_platform_suspend,
41576481f2b5SSaeed Bishara 	.resume			= mv_platform_resume,
4158f351b2d6SSaeed Bishara 	.driver			= {
4159f351b2d6SSaeed Bishara 				   .name = DRV_NAME,
4160f351b2d6SSaeed Bishara 				   .owner = THIS_MODULE,
4161f351b2d6SSaeed Bishara 				  },
4162f351b2d6SSaeed Bishara };
4163f351b2d6SSaeed Bishara 
4164f351b2d6SSaeed Bishara 
41657bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4166f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4167f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent);
4168b2dec48cSSaeed Bishara #ifdef CONFIG_PM
4169b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev);
4170b2dec48cSSaeed Bishara #endif
4171f351b2d6SSaeed Bishara 
41727bb3c529SSaeed Bishara 
41737bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
41747bb3c529SSaeed Bishara 	.name			= DRV_NAME,
41757bb3c529SSaeed Bishara 	.id_table		= mv_pci_tbl,
4176f351b2d6SSaeed Bishara 	.probe			= mv_pci_init_one,
41777bb3c529SSaeed Bishara 	.remove			= ata_pci_remove_one,
4178b2dec48cSSaeed Bishara #ifdef CONFIG_PM
4179b2dec48cSSaeed Bishara 	.suspend		= ata_pci_device_suspend,
4180b2dec48cSSaeed Bishara 	.resume			= mv_pci_device_resume,
4181b2dec48cSSaeed Bishara #endif
4182b2dec48cSSaeed Bishara 
41837bb3c529SSaeed Bishara };
41847bb3c529SSaeed Bishara 
41857bb3c529SSaeed Bishara /* move to PCI layer or libata core? */
41867bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev)
41877bb3c529SSaeed Bishara {
41887bb3c529SSaeed Bishara 	int rc;
41897bb3c529SSaeed Bishara 
41906a35528aSYang Hongyang 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
41916a35528aSYang Hongyang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
41927bb3c529SSaeed Bishara 		if (rc) {
4193284901a9SYang Hongyang 			rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
41947bb3c529SSaeed Bishara 			if (rc) {
41957bb3c529SSaeed Bishara 				dev_printk(KERN_ERR, &pdev->dev,
41967bb3c529SSaeed Bishara 					   "64-bit DMA enable failed\n");
41977bb3c529SSaeed Bishara 				return rc;
41987bb3c529SSaeed Bishara 			}
41997bb3c529SSaeed Bishara 		}
42007bb3c529SSaeed Bishara 	} else {
4201284901a9SYang Hongyang 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
42027bb3c529SSaeed Bishara 		if (rc) {
42037bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
42047bb3c529SSaeed Bishara 				   "32-bit DMA enable failed\n");
42057bb3c529SSaeed Bishara 			return rc;
42067bb3c529SSaeed Bishara 		}
4207284901a9SYang Hongyang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
42087bb3c529SSaeed Bishara 		if (rc) {
42097bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
42107bb3c529SSaeed Bishara 				   "32-bit consistent DMA enable failed\n");
42117bb3c529SSaeed Bishara 			return rc;
42127bb3c529SSaeed Bishara 		}
42137bb3c529SSaeed Bishara 	}
42147bb3c529SSaeed Bishara 
42157bb3c529SSaeed Bishara 	return rc;
42167bb3c529SSaeed Bishara }
42177bb3c529SSaeed Bishara 
4218c6fd2807SJeff Garzik /**
4219c6fd2807SJeff Garzik  *      mv_print_info - Dump key info to kernel log for perusal.
42204447d351STejun Heo  *      @host: ATA host to print info about
4221c6fd2807SJeff Garzik  *
4222c6fd2807SJeff Garzik  *      FIXME: complete this.
4223c6fd2807SJeff Garzik  *
4224c6fd2807SJeff Garzik  *      LOCKING:
4225c6fd2807SJeff Garzik  *      Inherited from caller.
4226c6fd2807SJeff Garzik  */
42274447d351STejun Heo static void mv_print_info(struct ata_host *host)
4228c6fd2807SJeff Garzik {
42294447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
42304447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
423144c10138SAuke Kok 	u8 scc;
4232c1e4fe71SJeff Garzik 	const char *scc_s, *gen;
4233c6fd2807SJeff Garzik 
4234c6fd2807SJeff Garzik 	/* Use this to determine the HW stepping of the chip so we know
4235c6fd2807SJeff Garzik 	 * what errata to workaround
4236c6fd2807SJeff Garzik 	 */
4237c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
4238c6fd2807SJeff Garzik 	if (scc == 0)
4239c6fd2807SJeff Garzik 		scc_s = "SCSI";
4240c6fd2807SJeff Garzik 	else if (scc == 0x01)
4241c6fd2807SJeff Garzik 		scc_s = "RAID";
4242c6fd2807SJeff Garzik 	else
4243c1e4fe71SJeff Garzik 		scc_s = "?";
4244c1e4fe71SJeff Garzik 
4245c1e4fe71SJeff Garzik 	if (IS_GEN_I(hpriv))
4246c1e4fe71SJeff Garzik 		gen = "I";
4247c1e4fe71SJeff Garzik 	else if (IS_GEN_II(hpriv))
4248c1e4fe71SJeff Garzik 		gen = "II";
4249c1e4fe71SJeff Garzik 	else if (IS_GEN_IIE(hpriv))
4250c1e4fe71SJeff Garzik 		gen = "IIE";
4251c1e4fe71SJeff Garzik 	else
4252c1e4fe71SJeff Garzik 		gen = "?";
4253c6fd2807SJeff Garzik 
4254c6fd2807SJeff Garzik 	dev_printk(KERN_INFO, &pdev->dev,
4255c1e4fe71SJeff Garzik 	       "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
4256c1e4fe71SJeff Garzik 	       gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
4257c6fd2807SJeff Garzik 	       scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
4258c6fd2807SJeff Garzik }
4259c6fd2807SJeff Garzik 
4260c6fd2807SJeff Garzik /**
4261f351b2d6SSaeed Bishara  *      mv_pci_init_one - handle a positive probe of a PCI Marvell host
4262c6fd2807SJeff Garzik  *      @pdev: PCI device found
4263c6fd2807SJeff Garzik  *      @ent: PCI device ID entry for the matched host
4264c6fd2807SJeff Garzik  *
4265c6fd2807SJeff Garzik  *      LOCKING:
4266c6fd2807SJeff Garzik  *      Inherited from caller.
4267c6fd2807SJeff Garzik  */
4268f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4269f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent)
4270c6fd2807SJeff Garzik {
42712dcb407eSJeff Garzik 	static int printed_version;
4272c6fd2807SJeff Garzik 	unsigned int board_idx = (unsigned int)ent->driver_data;
42734447d351STejun Heo 	const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
42744447d351STejun Heo 	struct ata_host *host;
42754447d351STejun Heo 	struct mv_host_priv *hpriv;
4276c4bc7d73SSaeed Bishara 	int n_ports, port, rc;
4277c6fd2807SJeff Garzik 
4278c6fd2807SJeff Garzik 	if (!printed_version++)
4279c6fd2807SJeff Garzik 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
4280c6fd2807SJeff Garzik 
42814447d351STejun Heo 	/* allocate host */
42824447d351STejun Heo 	n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
42834447d351STejun Heo 
42844447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
42854447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
42864447d351STejun Heo 	if (!host || !hpriv)
42874447d351STejun Heo 		return -ENOMEM;
42884447d351STejun Heo 	host->private_data = hpriv;
4289f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
42901bfeff03SSaeed Bishara 	hpriv->board_idx = board_idx;
42914447d351STejun Heo 
42924447d351STejun Heo 	/* acquire resources */
429324dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
429424dc5f33STejun Heo 	if (rc)
4295c6fd2807SJeff Garzik 		return rc;
4296c6fd2807SJeff Garzik 
42970d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
42980d5ff566STejun Heo 	if (rc == -EBUSY)
429924dc5f33STejun Heo 		pcim_pin_device(pdev);
43000d5ff566STejun Heo 	if (rc)
430124dc5f33STejun Heo 		return rc;
43024447d351STejun Heo 	host->iomap = pcim_iomap_table(pdev);
4303f351b2d6SSaeed Bishara 	hpriv->base = host->iomap[MV_PRIMARY_BAR];
4304c6fd2807SJeff Garzik 
4305d88184fbSJeff Garzik 	rc = pci_go_64(pdev);
4306d88184fbSJeff Garzik 	if (rc)
4307d88184fbSJeff Garzik 		return rc;
4308d88184fbSJeff Garzik 
4309da2fa9baSMark Lord 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
4310da2fa9baSMark Lord 	if (rc)
4311da2fa9baSMark Lord 		return rc;
4312da2fa9baSMark Lord 
4313c4bc7d73SSaeed Bishara 	for (port = 0; port < host->n_ports; port++) {
4314c4bc7d73SSaeed Bishara 		struct ata_port *ap = host->ports[port];
4315c4bc7d73SSaeed Bishara 		void __iomem *port_mmio = mv_port_base(hpriv->base, port);
4316c4bc7d73SSaeed Bishara 		unsigned int offset = port_mmio - hpriv->base;
4317c4bc7d73SSaeed Bishara 
4318c4bc7d73SSaeed Bishara 		ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
4319c4bc7d73SSaeed Bishara 		ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
4320c4bc7d73SSaeed Bishara 	}
4321c4bc7d73SSaeed Bishara 
4322c6fd2807SJeff Garzik 	/* initialize adapter */
43231bfeff03SSaeed Bishara 	rc = mv_init_host(host);
432424dc5f33STejun Heo 	if (rc)
432524dc5f33STejun Heo 		return rc;
4326c6fd2807SJeff Garzik 
43276d3c30efSMark Lord 	/* Enable message-switched interrupts, if requested */
43286d3c30efSMark Lord 	if (msi && pci_enable_msi(pdev) == 0)
43296d3c30efSMark Lord 		hpriv->hp_flags |= MV_HP_FLAG_MSI;
4330c6fd2807SJeff Garzik 
4331c6fd2807SJeff Garzik 	mv_dump_pci_cfg(pdev, 0x68);
43324447d351STejun Heo 	mv_print_info(host);
4333c6fd2807SJeff Garzik 
43344447d351STejun Heo 	pci_set_master(pdev);
4335ea8b4db9SJeff Garzik 	pci_try_set_mwi(pdev);
43364447d351STejun Heo 	return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
4337c5d3e45aSJeff Garzik 				 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
4338c6fd2807SJeff Garzik }
4339b2dec48cSSaeed Bishara 
4340b2dec48cSSaeed Bishara #ifdef CONFIG_PM
4341b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev)
4342b2dec48cSSaeed Bishara {
4343b2dec48cSSaeed Bishara 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
4344b2dec48cSSaeed Bishara 	int rc;
4345b2dec48cSSaeed Bishara 
4346b2dec48cSSaeed Bishara 	rc = ata_pci_device_do_resume(pdev);
4347b2dec48cSSaeed Bishara 	if (rc)
4348b2dec48cSSaeed Bishara 		return rc;
4349b2dec48cSSaeed Bishara 
4350b2dec48cSSaeed Bishara 	/* initialize adapter */
4351b2dec48cSSaeed Bishara 	rc = mv_init_host(host);
4352b2dec48cSSaeed Bishara 	if (rc)
4353b2dec48cSSaeed Bishara 		return rc;
4354b2dec48cSSaeed Bishara 
4355b2dec48cSSaeed Bishara 	ata_host_resume(host);
4356b2dec48cSSaeed Bishara 
4357b2dec48cSSaeed Bishara 	return 0;
4358b2dec48cSSaeed Bishara }
4359b2dec48cSSaeed Bishara #endif
43607bb3c529SSaeed Bishara #endif
4361c6fd2807SJeff Garzik 
4362f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev);
4363f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev);
4364f351b2d6SSaeed Bishara 
4365c6fd2807SJeff Garzik static int __init mv_init(void)
4366c6fd2807SJeff Garzik {
43677bb3c529SSaeed Bishara 	int rc = -ENODEV;
43687bb3c529SSaeed Bishara #ifdef CONFIG_PCI
43697bb3c529SSaeed Bishara 	rc = pci_register_driver(&mv_pci_driver);
4370f351b2d6SSaeed Bishara 	if (rc < 0)
4371f351b2d6SSaeed Bishara 		return rc;
4372f351b2d6SSaeed Bishara #endif
4373f351b2d6SSaeed Bishara 	rc = platform_driver_register(&mv_platform_driver);
4374f351b2d6SSaeed Bishara 
4375f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
4376f351b2d6SSaeed Bishara 	if (rc < 0)
4377f351b2d6SSaeed Bishara 		pci_unregister_driver(&mv_pci_driver);
43787bb3c529SSaeed Bishara #endif
43797bb3c529SSaeed Bishara 	return rc;
4380c6fd2807SJeff Garzik }
4381c6fd2807SJeff Garzik 
4382c6fd2807SJeff Garzik static void __exit mv_exit(void)
4383c6fd2807SJeff Garzik {
43847bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4385c6fd2807SJeff Garzik 	pci_unregister_driver(&mv_pci_driver);
43867bb3c529SSaeed Bishara #endif
4387f351b2d6SSaeed Bishara 	platform_driver_unregister(&mv_platform_driver);
4388c6fd2807SJeff Garzik }
4389c6fd2807SJeff Garzik 
4390c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
4391c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
4392c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
4393c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
4394c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
439517c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
4396c6fd2807SJeff Garzik 
4397c6fd2807SJeff Garzik module_init(mv_init);
4398c6fd2807SJeff Garzik module_exit(mv_exit);
4399