xref: /openbmc/linux/drivers/ata/sata_mv.c (revision eee989902aab45f0ca2739727ef615420802649c)
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 
1639cbe056fSSergei Shtylyov 	MV_COMMON_FLAGS		= ATA_FLAG_SATA | ATA_FLAG_PIO_POLLING,
164ad3aef51SMark Lord 
16591b1a84cSMark Lord 	MV_GEN_I_FLAGS		= MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
166c6fd2807SJeff Garzik 
16740f21b11SMark Lord 	MV_GEN_II_FLAGS		= MV_COMMON_FLAGS | ATA_FLAG_NCQ |
16840f21b11SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
16991b1a84cSMark Lord 
17091b1a84cSMark Lord 	MV_GEN_IIE_FLAGS	= MV_GEN_II_FLAGS | ATA_FLAG_AN,
171ad3aef51SMark Lord 
172c6fd2807SJeff Garzik 	CRQB_FLAG_READ		= (1 << 0),
173c6fd2807SJeff Garzik 	CRQB_TAG_SHIFT		= 1,
174c5d3e45aSJeff Garzik 	CRQB_IOID_SHIFT		= 6,	/* CRQB Gen-II/IIE IO Id shift */
175e12bef50SMark Lord 	CRQB_PMP_SHIFT		= 12,	/* CRQB Gen-II/IIE PMP shift */
176c5d3e45aSJeff Garzik 	CRQB_HOSTQ_SHIFT	= 17,	/* CRQB Gen-II/IIE HostQueTag shift */
177c6fd2807SJeff Garzik 	CRQB_CMD_ADDR_SHIFT	= 8,
178c6fd2807SJeff Garzik 	CRQB_CMD_CS		= (0x2 << 11),
179c6fd2807SJeff Garzik 	CRQB_CMD_LAST		= (1 << 15),
180c6fd2807SJeff Garzik 
181c6fd2807SJeff Garzik 	CRPB_FLAG_STATUS_SHIFT	= 8,
182c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_6	= 5,	/* CRPB Gen-II IO Id shift */
183c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_7	= 7,	/* CRPB Gen-IIE IO Id shift */
184c6fd2807SJeff Garzik 
185c6fd2807SJeff Garzik 	EPRD_FLAG_END_OF_TBL	= (1 << 31),
186c6fd2807SJeff Garzik 
187c6fd2807SJeff Garzik 	/* PCI interface registers */
188c6fd2807SJeff Garzik 
189cae5a29dSMark Lord 	MV_PCI_COMMAND		= 0xc00,
190cae5a29dSMark Lord 	MV_PCI_COMMAND_MWRCOM	= (1 << 4),	/* PCI Master Write Combining */
191cae5a29dSMark Lord 	MV_PCI_COMMAND_MRDTRIG	= (1 << 7),	/* PCI Master Read Trigger */
192c6fd2807SJeff Garzik 
193cae5a29dSMark Lord 	PCI_MAIN_CMD_STS	= 0xd30,
194c6fd2807SJeff Garzik 	STOP_PCI_MASTER		= (1 << 2),
195c6fd2807SJeff Garzik 	PCI_MASTER_EMPTY	= (1 << 3),
196c6fd2807SJeff Garzik 	GLOB_SFT_RST		= (1 << 4),
197c6fd2807SJeff Garzik 
198cae5a29dSMark Lord 	MV_PCI_MODE		= 0xd00,
1998e7decdbSMark Lord 	MV_PCI_MODE_MASK	= 0x30,
2008e7decdbSMark Lord 
201c6fd2807SJeff Garzik 	MV_PCI_EXP_ROM_BAR_CTL	= 0xd2c,
202c6fd2807SJeff Garzik 	MV_PCI_DISC_TIMER	= 0xd04,
203c6fd2807SJeff Garzik 	MV_PCI_MSI_TRIGGER	= 0xc38,
204c6fd2807SJeff Garzik 	MV_PCI_SERR_MASK	= 0xc28,
205cae5a29dSMark Lord 	MV_PCI_XBAR_TMOUT	= 0x1d04,
206c6fd2807SJeff Garzik 	MV_PCI_ERR_LOW_ADDRESS	= 0x1d40,
207c6fd2807SJeff Garzik 	MV_PCI_ERR_HIGH_ADDRESS	= 0x1d44,
208c6fd2807SJeff Garzik 	MV_PCI_ERR_ATTRIBUTE	= 0x1d48,
209c6fd2807SJeff Garzik 	MV_PCI_ERR_COMMAND	= 0x1d50,
210c6fd2807SJeff Garzik 
211cae5a29dSMark Lord 	PCI_IRQ_CAUSE		= 0x1d58,
212cae5a29dSMark Lord 	PCI_IRQ_MASK		= 0x1d5c,
213c6fd2807SJeff Garzik 	PCI_UNMASK_ALL_IRQS	= 0x7fffff,	/* bits 22-0 */
214c6fd2807SJeff Garzik 
215cae5a29dSMark Lord 	PCIE_IRQ_CAUSE		= 0x1900,
216cae5a29dSMark Lord 	PCIE_IRQ_MASK		= 0x1910,
217646a4da5SMark Lord 	PCIE_UNMASK_ALL_IRQS	= 0x40a,	/* assorted bits */
21802a121daSMark Lord 
2197368f919SMark Lord 	/* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
220cae5a29dSMark Lord 	PCI_HC_MAIN_IRQ_CAUSE	= 0x1d60,
221cae5a29dSMark Lord 	PCI_HC_MAIN_IRQ_MASK	= 0x1d64,
222cae5a29dSMark Lord 	SOC_HC_MAIN_IRQ_CAUSE	= 0x20020,
223cae5a29dSMark Lord 	SOC_HC_MAIN_IRQ_MASK	= 0x20024,
22440f21b11SMark Lord 	ERR_IRQ			= (1 << 0),	/* shift by (2 * port #) */
22540f21b11SMark Lord 	DONE_IRQ		= (1 << 1),	/* shift by (2 * port #) */
226c6fd2807SJeff Garzik 	HC0_IRQ_PEND		= 0x1ff,	/* bits 0-8 = HC0's ports */
227c6fd2807SJeff Garzik 	HC_SHIFT		= 9,		/* bits 9-17 = HC1's ports */
2282b748a0aSMark Lord 	DONE_IRQ_0_3		= 0x000000aa,	/* DONE_IRQ ports 0,1,2,3 */
2292b748a0aSMark Lord 	DONE_IRQ_4_7		= (DONE_IRQ_0_3 << HC_SHIFT),  /* 4,5,6,7 */
230c6fd2807SJeff Garzik 	PCI_ERR			= (1 << 18),
23140f21b11SMark Lord 	TRAN_COAL_LO_DONE	= (1 << 19),	/* transaction coalescing */
23240f21b11SMark Lord 	TRAN_COAL_HI_DONE	= (1 << 20),	/* transaction coalescing */
23340f21b11SMark Lord 	PORTS_0_3_COAL_DONE	= (1 << 8),	/* HC0 IRQ coalescing */
23440f21b11SMark Lord 	PORTS_4_7_COAL_DONE	= (1 << 17),	/* HC1 IRQ coalescing */
23540f21b11SMark Lord 	ALL_PORTS_COAL_DONE	= (1 << 21),	/* GEN_II(E) IRQ coalescing */
236c6fd2807SJeff Garzik 	GPIO_INT		= (1 << 22),
237c6fd2807SJeff Garzik 	SELF_INT		= (1 << 23),
238c6fd2807SJeff Garzik 	TWSI_INT		= (1 << 24),
239c6fd2807SJeff Garzik 	HC_MAIN_RSVD		= (0x7f << 25),	/* bits 31-25 */
240fb621e2fSJeff Garzik 	HC_MAIN_RSVD_5		= (0x1fff << 19), /* bits 31-19 */
241f351b2d6SSaeed Bishara 	HC_MAIN_RSVD_SOC	= (0x3fffffb << 6),     /* bits 31-9, 7-6 */
242c6fd2807SJeff Garzik 
243c6fd2807SJeff Garzik 	/* SATAHC registers */
244cae5a29dSMark Lord 	HC_CFG			= 0x00,
245c6fd2807SJeff Garzik 
246cae5a29dSMark Lord 	HC_IRQ_CAUSE		= 0x14,
247352fab70SMark Lord 	DMA_IRQ			= (1 << 0),	/* shift by port # */
248352fab70SMark Lord 	HC_COAL_IRQ		= (1 << 4),	/* IRQ coalescing */
249c6fd2807SJeff Garzik 	DEV_IRQ			= (1 << 8),	/* shift by port # */
250c6fd2807SJeff Garzik 
2512b748a0aSMark Lord 	/*
2522b748a0aSMark Lord 	 * Per-HC (Host-Controller) interrupt coalescing feature.
2532b748a0aSMark Lord 	 * This is present on all chip generations.
2542b748a0aSMark Lord 	 *
2552b748a0aSMark Lord 	 * Coalescing defers the interrupt until either the IO_THRESHOLD
2562b748a0aSMark Lord 	 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
2572b748a0aSMark Lord 	 */
258cae5a29dSMark Lord 	HC_IRQ_COAL_IO_THRESHOLD	= 0x000c,
259cae5a29dSMark Lord 	HC_IRQ_COAL_TIME_THRESHOLD	= 0x0010,
2602b748a0aSMark Lord 
261cae5a29dSMark Lord 	SOC_LED_CTRL		= 0x2c,
262000b344fSMark Lord 	SOC_LED_CTRL_BLINK	= (1 << 0),	/* Active LED blink */
263000b344fSMark Lord 	SOC_LED_CTRL_ACT_PRESENCE = (1 << 2),	/* Multiplex dev presence */
264000b344fSMark Lord 						/*  with dev activity LED */
265000b344fSMark Lord 
266c6fd2807SJeff Garzik 	/* Shadow block registers */
267cae5a29dSMark Lord 	SHD_BLK			= 0x100,
268cae5a29dSMark Lord 	SHD_CTL_AST		= 0x20,		/* ofs from SHD_BLK */
269c6fd2807SJeff Garzik 
270c6fd2807SJeff Garzik 	/* SATA registers */
271cae5a29dSMark Lord 	SATA_STATUS		= 0x300,  /* ctrl, err regs follow status */
272cae5a29dSMark Lord 	SATA_ACTIVE		= 0x350,
273cae5a29dSMark Lord 	FIS_IRQ_CAUSE		= 0x364,
274cae5a29dSMark Lord 	FIS_IRQ_CAUSE_AN	= (1 << 9),	/* async notification */
27517c5aab5SMark Lord 
276cae5a29dSMark Lord 	LTMODE			= 0x30c,	/* requires read-after-write */
27717c5aab5SMark Lord 	LTMODE_BIT8		= (1 << 8),	/* unknown, but necessary */
27817c5aab5SMark Lord 
279cae5a29dSMark Lord 	PHY_MODE2		= 0x330,
280c6fd2807SJeff Garzik 	PHY_MODE3		= 0x310,
281cae5a29dSMark Lord 
282cae5a29dSMark Lord 	PHY_MODE4		= 0x314,	/* requires read-after-write */
283ba069e37SMark Lord 	PHY_MODE4_CFG_MASK	= 0x00000003,	/* phy internal config field */
284ba069e37SMark Lord 	PHY_MODE4_CFG_VALUE	= 0x00000001,	/* phy internal config field */
285ba069e37SMark Lord 	PHY_MODE4_RSVD_ZEROS	= 0x5de3fffa,	/* Gen2e always write zeros */
286ba069e37SMark Lord 	PHY_MODE4_RSVD_ONES	= 0x00000005,	/* Gen2e always write ones */
287ba069e37SMark Lord 
288cae5a29dSMark Lord 	SATA_IFCTL		= 0x344,
289cae5a29dSMark Lord 	SATA_TESTCTL		= 0x348,
290cae5a29dSMark Lord 	SATA_IFSTAT		= 0x34c,
291cae5a29dSMark Lord 	VENDOR_UNIQUE_FIS	= 0x35c,
29217c5aab5SMark Lord 
293cae5a29dSMark Lord 	FISCFG			= 0x360,
2948e7decdbSMark Lord 	FISCFG_WAIT_DEV_ERR	= (1 << 8),	/* wait for host on DevErr */
2958e7decdbSMark Lord 	FISCFG_SINGLE_SYNC	= (1 << 16),	/* SYNC on DMA activation */
29617c5aab5SMark Lord 
29729b7e43cSMartin Michlmayr 	PHY_MODE9_GEN2		= 0x398,
29829b7e43cSMartin Michlmayr 	PHY_MODE9_GEN1		= 0x39c,
29929b7e43cSMartin Michlmayr 	PHYCFG_OFS		= 0x3a0,	/* only in 65n devices */
30029b7e43cSMartin Michlmayr 
301c6fd2807SJeff Garzik 	MV5_PHY_MODE		= 0x74,
302cae5a29dSMark Lord 	MV5_LTMODE		= 0x30,
303cae5a29dSMark Lord 	MV5_PHY_CTL		= 0x0C,
304cae5a29dSMark Lord 	SATA_IFCFG		= 0x050,
305c6fd2807SJeff Garzik 
306c6fd2807SJeff Garzik 	MV_M2_PREAMP_MASK	= 0x7e0,
307c6fd2807SJeff Garzik 
308c6fd2807SJeff Garzik 	/* Port registers */
309cae5a29dSMark Lord 	EDMA_CFG		= 0,
3100c58912eSMark Lord 	EDMA_CFG_Q_DEPTH	= 0x1f,		/* max device queue depth */
3110c58912eSMark Lord 	EDMA_CFG_NCQ		= (1 << 5),	/* for R/W FPDMA queued */
312c6fd2807SJeff Garzik 	EDMA_CFG_NCQ_GO_ON_ERR	= (1 << 14),	/* continue on error */
313c6fd2807SJeff Garzik 	EDMA_CFG_RD_BRST_EXT	= (1 << 11),	/* read burst 512B */
314c6fd2807SJeff Garzik 	EDMA_CFG_WR_BUFF_LEN	= (1 << 13),	/* write buffer 512B */
315e12bef50SMark Lord 	EDMA_CFG_EDMA_FBS	= (1 << 16),	/* EDMA FIS-Based Switching */
316e12bef50SMark Lord 	EDMA_CFG_FBS		= (1 << 26),	/* FIS-Based Switching */
317c6fd2807SJeff Garzik 
318cae5a29dSMark Lord 	EDMA_ERR_IRQ_CAUSE	= 0x8,
319cae5a29dSMark Lord 	EDMA_ERR_IRQ_MASK	= 0xc,
3206c1153e0SJeff Garzik 	EDMA_ERR_D_PAR		= (1 << 0),	/* UDMA data parity err */
3216c1153e0SJeff Garzik 	EDMA_ERR_PRD_PAR	= (1 << 1),	/* UDMA PRD parity err */
3226c1153e0SJeff Garzik 	EDMA_ERR_DEV		= (1 << 2),	/* device error */
3236c1153e0SJeff Garzik 	EDMA_ERR_DEV_DCON	= (1 << 3),	/* device disconnect */
3246c1153e0SJeff Garzik 	EDMA_ERR_DEV_CON	= (1 << 4),	/* device connected */
3256c1153e0SJeff Garzik 	EDMA_ERR_SERR		= (1 << 5),	/* SError bits [WBDST] raised */
326c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS	= (1 << 7),	/* Gen II/IIE self-disable */
327c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS_5	= (1 << 8),	/* Gen I self-disable */
3286c1153e0SJeff Garzik 	EDMA_ERR_BIST_ASYNC	= (1 << 8),	/* BIST FIS or Async Notify */
329c5d3e45aSJeff Garzik 	EDMA_ERR_TRANS_IRQ_7	= (1 << 8),	/* Gen IIE transprt layer irq */
3306c1153e0SJeff Garzik 	EDMA_ERR_CRQB_PAR	= (1 << 9),	/* CRQB parity error */
3316c1153e0SJeff Garzik 	EDMA_ERR_CRPB_PAR	= (1 << 10),	/* CRPB parity error */
3326c1153e0SJeff Garzik 	EDMA_ERR_INTRL_PAR	= (1 << 11),	/* internal parity error */
3336c1153e0SJeff Garzik 	EDMA_ERR_IORDY		= (1 << 12),	/* IORdy timeout */
334646a4da5SMark Lord 
3356c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_RX	= (0xf << 13),	/* link ctrl rx error */
336646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_0	= (1 << 13),	/* transient: CRC err */
337646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_1	= (1 << 14),	/* transient: FIFO err */
338646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_2	= (1 << 15),	/* fatal: caught SYNC */
339646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_3	= (1 << 16),	/* transient: FIS rx err */
340646a4da5SMark Lord 
3416c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_RX	= (0xf << 17),	/* link data rx error */
342646a4da5SMark Lord 
3436c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_TX	= (0x1f << 21),	/* link ctrl tx error */
344646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_0	= (1 << 21),	/* transient: CRC err */
345646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_1	= (1 << 22),	/* transient: FIFO err */
346646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_2	= (1 << 23),	/* transient: caught SYNC */
347646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_3	= (1 << 24),	/* transient: caught DMAT */
348646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_4	= (1 << 25),	/* transient: FIS collision */
349646a4da5SMark Lord 
3506c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_TX	= (0x1f << 26),	/* link data tx error */
351646a4da5SMark Lord 
3526c1153e0SJeff Garzik 	EDMA_ERR_TRANS_PROTO	= (1 << 31),	/* transport protocol error */
353c5d3e45aSJeff Garzik 	EDMA_ERR_OVERRUN_5	= (1 << 5),
354c5d3e45aSJeff Garzik 	EDMA_ERR_UNDERRUN_5	= (1 << 6),
355646a4da5SMark Lord 
356646a4da5SMark Lord 	EDMA_ERR_IRQ_TRANSIENT  = EDMA_ERR_LNK_CTRL_RX_0 |
357646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_1 |
358646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_3 |
35985afb934SMark Lord 				  EDMA_ERR_LNK_CTRL_TX,
360646a4da5SMark Lord 
361bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE		= EDMA_ERR_D_PAR |
362bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
363bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
364bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
365bdd4dddeSJeff Garzik 				  EDMA_ERR_SERR |
366bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS |
3676c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
368bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
369bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
370bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY |
371bdd4dddeSJeff Garzik 				  EDMA_ERR_LNK_CTRL_RX_2 |
372c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_RX |
373c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_TX |
374bdd4dddeSJeff Garzik 				  EDMA_ERR_TRANS_PROTO,
375e12bef50SMark Lord 
376bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE_5	= EDMA_ERR_D_PAR |
377bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
378bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
379bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
380bdd4dddeSJeff Garzik 				  EDMA_ERR_OVERRUN_5 |
381bdd4dddeSJeff Garzik 				  EDMA_ERR_UNDERRUN_5 |
382bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS_5 |
3836c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
384bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
385bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
386bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY,
387c6fd2807SJeff Garzik 
388cae5a29dSMark Lord 	EDMA_REQ_Q_BASE_HI	= 0x10,
389cae5a29dSMark Lord 	EDMA_REQ_Q_IN_PTR	= 0x14,		/* also contains BASE_LO */
390c6fd2807SJeff Garzik 
391cae5a29dSMark Lord 	EDMA_REQ_Q_OUT_PTR	= 0x18,
392c6fd2807SJeff Garzik 	EDMA_REQ_Q_PTR_SHIFT	= 5,
393c6fd2807SJeff Garzik 
394cae5a29dSMark Lord 	EDMA_RSP_Q_BASE_HI	= 0x1c,
395cae5a29dSMark Lord 	EDMA_RSP_Q_IN_PTR	= 0x20,
396cae5a29dSMark Lord 	EDMA_RSP_Q_OUT_PTR	= 0x24,		/* also contains BASE_LO */
397c6fd2807SJeff Garzik 	EDMA_RSP_Q_PTR_SHIFT	= 3,
398c6fd2807SJeff Garzik 
399cae5a29dSMark Lord 	EDMA_CMD		= 0x28,		/* EDMA command register */
4000ea9e179SJeff Garzik 	EDMA_EN			= (1 << 0),	/* enable EDMA */
4010ea9e179SJeff Garzik 	EDMA_DS			= (1 << 1),	/* disable EDMA; self-negated */
4028e7decdbSMark Lord 	EDMA_RESET		= (1 << 2),	/* reset eng/trans/link/phy */
403c6fd2807SJeff Garzik 
404cae5a29dSMark Lord 	EDMA_STATUS		= 0x30,		/* EDMA engine status */
4058e7decdbSMark Lord 	EDMA_STATUS_CACHE_EMPTY	= (1 << 6),	/* GenIIe command cache empty */
4068e7decdbSMark Lord 	EDMA_STATUS_IDLE	= (1 << 7),	/* GenIIe EDMA enabled/idle */
4078e7decdbSMark Lord 
408cae5a29dSMark Lord 	EDMA_IORDY_TMOUT	= 0x34,
409cae5a29dSMark Lord 	EDMA_ARB_CFG		= 0x38,
4108e7decdbSMark Lord 
411cae5a29dSMark Lord 	EDMA_HALTCOND		= 0x60,		/* GenIIe halt conditions */
412cae5a29dSMark Lord 	EDMA_UNKNOWN_RSVD	= 0x6C,		/* GenIIe unknown/reserved */
413da14265eSMark Lord 
414cae5a29dSMark Lord 	BMDMA_CMD		= 0x224,	/* bmdma command register */
415cae5a29dSMark Lord 	BMDMA_STATUS		= 0x228,	/* bmdma status register */
416cae5a29dSMark Lord 	BMDMA_PRD_LOW		= 0x22c,	/* bmdma PRD addr 31:0 */
417cae5a29dSMark Lord 	BMDMA_PRD_HIGH		= 0x230,	/* bmdma PRD addr 63:32 */
418da14265eSMark Lord 
419c6fd2807SJeff Garzik 	/* Host private flags (hp_flags) */
420c6fd2807SJeff Garzik 	MV_HP_FLAG_MSI		= (1 << 0),
421c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB0	= (1 << 1),
422c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB2	= (1 << 2),
423c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1B2	= (1 << 3),
424c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1C0	= (1 << 4),
4250ea9e179SJeff Garzik 	MV_HP_GEN_I		= (1 << 6),	/* Generation I: 50xx */
4260ea9e179SJeff Garzik 	MV_HP_GEN_II		= (1 << 7),	/* Generation II: 60xx */
4270ea9e179SJeff Garzik 	MV_HP_GEN_IIE		= (1 << 8),	/* Generation IIE: 6042/7042 */
42802a121daSMark Lord 	MV_HP_PCIE		= (1 << 9),	/* PCIe bus/regs: 7042 */
429616d4a98SMark Lord 	MV_HP_CUT_THROUGH	= (1 << 10),	/* can use EDMA cut-through */
4301f398472SMark Lord 	MV_HP_FLAG_SOC		= (1 << 11),	/* SystemOnChip, no PCI */
431000b344fSMark Lord 	MV_HP_QUIRK_LED_BLINK_EN = (1 << 12),	/* is led blinking enabled? */
432c6fd2807SJeff Garzik 
433c6fd2807SJeff Garzik 	/* Port private flags (pp_flags) */
4340ea9e179SJeff Garzik 	MV_PP_FLAG_EDMA_EN	= (1 << 0),	/* is EDMA engine enabled? */
43572109168SMark Lord 	MV_PP_FLAG_NCQ_EN	= (1 << 1),	/* is EDMA set up for NCQ? */
43600f42eabSMark Lord 	MV_PP_FLAG_FBS_EN	= (1 << 2),	/* is EDMA set up for FBS? */
43729d187bbSMark Lord 	MV_PP_FLAG_DELAYED_EH	= (1 << 3),	/* delayed dev err handling */
438d16ab3f6SMark Lord 	MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4),	/* ignore initial ATA_DRDY */
439c6fd2807SJeff Garzik };
440c6fd2807SJeff Garzik 
441ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
442ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
443c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
4448e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
4451f398472SMark Lord #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
446c6fd2807SJeff Garzik 
44715a32632SLennert Buytenhek #define WINDOW_CTRL(i)		(0x20030 + ((i) << 4))
44815a32632SLennert Buytenhek #define WINDOW_BASE(i)		(0x20034 + ((i) << 4))
44915a32632SLennert Buytenhek 
450c6fd2807SJeff Garzik enum {
451baf14aa1SJeff Garzik 	/* DMA boundary 0xffff is required by the s/g splitting
452baf14aa1SJeff Garzik 	 * we need on /length/ in mv_fill-sg().
453baf14aa1SJeff Garzik 	 */
454baf14aa1SJeff Garzik 	MV_DMA_BOUNDARY		= 0xffffU,
455c6fd2807SJeff Garzik 
4560ea9e179SJeff Garzik 	/* mask of register bits containing lower 32 bits
4570ea9e179SJeff Garzik 	 * of EDMA request queue DMA address
4580ea9e179SJeff Garzik 	 */
459c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_LO_MASK	= 0xfffffc00U,
460c6fd2807SJeff Garzik 
4610ea9e179SJeff Garzik 	/* ditto, for response queue */
462c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_LO_MASK	= 0xffffff00U,
463c6fd2807SJeff Garzik };
464c6fd2807SJeff Garzik 
465c6fd2807SJeff Garzik enum chip_type {
466c6fd2807SJeff Garzik 	chip_504x,
467c6fd2807SJeff Garzik 	chip_508x,
468c6fd2807SJeff Garzik 	chip_5080,
469c6fd2807SJeff Garzik 	chip_604x,
470c6fd2807SJeff Garzik 	chip_608x,
471c6fd2807SJeff Garzik 	chip_6042,
472c6fd2807SJeff Garzik 	chip_7042,
473f351b2d6SSaeed Bishara 	chip_soc,
474c6fd2807SJeff Garzik };
475c6fd2807SJeff Garzik 
476c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
477c6fd2807SJeff Garzik struct mv_crqb {
478c6fd2807SJeff Garzik 	__le32			sg_addr;
479c6fd2807SJeff Garzik 	__le32			sg_addr_hi;
480c6fd2807SJeff Garzik 	__le16			ctrl_flags;
481c6fd2807SJeff Garzik 	__le16			ata_cmd[11];
482c6fd2807SJeff Garzik };
483c6fd2807SJeff Garzik 
484c6fd2807SJeff Garzik struct mv_crqb_iie {
485c6fd2807SJeff Garzik 	__le32			addr;
486c6fd2807SJeff Garzik 	__le32			addr_hi;
487c6fd2807SJeff Garzik 	__le32			flags;
488c6fd2807SJeff Garzik 	__le32			len;
489c6fd2807SJeff Garzik 	__le32			ata_cmd[4];
490c6fd2807SJeff Garzik };
491c6fd2807SJeff Garzik 
492c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
493c6fd2807SJeff Garzik struct mv_crpb {
494c6fd2807SJeff Garzik 	__le16			id;
495c6fd2807SJeff Garzik 	__le16			flags;
496c6fd2807SJeff Garzik 	__le32			tmstmp;
497c6fd2807SJeff Garzik };
498c6fd2807SJeff Garzik 
499c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
500c6fd2807SJeff Garzik struct mv_sg {
501c6fd2807SJeff Garzik 	__le32			addr;
502c6fd2807SJeff Garzik 	__le32			flags_size;
503c6fd2807SJeff Garzik 	__le32			addr_hi;
504c6fd2807SJeff Garzik 	__le32			reserved;
505c6fd2807SJeff Garzik };
506c6fd2807SJeff Garzik 
50708da1759SMark Lord /*
50808da1759SMark Lord  * We keep a local cache of a few frequently accessed port
50908da1759SMark Lord  * registers here, to avoid having to read them (very slow)
51008da1759SMark Lord  * when switching between EDMA and non-EDMA modes.
51108da1759SMark Lord  */
51208da1759SMark Lord struct mv_cached_regs {
51308da1759SMark Lord 	u32			fiscfg;
51408da1759SMark Lord 	u32			ltmode;
51508da1759SMark Lord 	u32			haltcond;
516c01e8a23SMark Lord 	u32			unknown_rsvd;
51708da1759SMark Lord };
51808da1759SMark Lord 
519c6fd2807SJeff Garzik struct mv_port_priv {
520c6fd2807SJeff Garzik 	struct mv_crqb		*crqb;
521c6fd2807SJeff Garzik 	dma_addr_t		crqb_dma;
522c6fd2807SJeff Garzik 	struct mv_crpb		*crpb;
523c6fd2807SJeff Garzik 	dma_addr_t		crpb_dma;
524eb73d558SMark Lord 	struct mv_sg		*sg_tbl[MV_MAX_Q_DEPTH];
525eb73d558SMark Lord 	dma_addr_t		sg_tbl_dma[MV_MAX_Q_DEPTH];
526bdd4dddeSJeff Garzik 
527bdd4dddeSJeff Garzik 	unsigned int		req_idx;
528bdd4dddeSJeff Garzik 	unsigned int		resp_idx;
529bdd4dddeSJeff Garzik 
530c6fd2807SJeff Garzik 	u32			pp_flags;
53108da1759SMark Lord 	struct mv_cached_regs	cached;
53229d187bbSMark Lord 	unsigned int		delayed_eh_pmp_map;
533c6fd2807SJeff Garzik };
534c6fd2807SJeff Garzik 
535c6fd2807SJeff Garzik struct mv_port_signal {
536c6fd2807SJeff Garzik 	u32			amps;
537c6fd2807SJeff Garzik 	u32			pre;
538c6fd2807SJeff Garzik };
539c6fd2807SJeff Garzik 
54002a121daSMark Lord struct mv_host_priv {
54102a121daSMark Lord 	u32			hp_flags;
5421bfeff03SSaeed Bishara 	unsigned int 		board_idx;
54396e2c487SMark Lord 	u32			main_irq_mask;
54402a121daSMark Lord 	struct mv_port_signal	signal[8];
54502a121daSMark Lord 	const struct mv_hw_ops	*ops;
546f351b2d6SSaeed Bishara 	int			n_ports;
547f351b2d6SSaeed Bishara 	void __iomem		*base;
5487368f919SMark Lord 	void __iomem		*main_irq_cause_addr;
5497368f919SMark Lord 	void __iomem		*main_irq_mask_addr;
550cae5a29dSMark Lord 	u32			irq_cause_offset;
551cae5a29dSMark Lord 	u32			irq_mask_offset;
55202a121daSMark Lord 	u32			unmask_all_irqs;
553c77a2f4eSSaeed Bishara 
554c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
555c77a2f4eSSaeed Bishara 	struct clk		*clk;
556*eee98990SAndrew Lunn 	struct clk              **port_clks;
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,
678bdd4dddeSJeff Garzik 
679c6fd2807SJeff Garzik 	.scr_read		= mv5_scr_read,
680c6fd2807SJeff Garzik 	.scr_write		= mv5_scr_write,
681c6fd2807SJeff Garzik 
682c6fd2807SJeff Garzik 	.port_start		= mv_port_start,
683c6fd2807SJeff Garzik 	.port_stop		= mv_port_stop,
684c6fd2807SJeff Garzik };
685c6fd2807SJeff Garzik 
686029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
6878930ff25STejun Heo 	.inherits		= &ata_bmdma_port_ops,
688c6fd2807SJeff Garzik 
6898930ff25STejun Heo 	.lost_interrupt		= ATA_OP_NULL,
6908930ff25STejun Heo 
6918930ff25STejun Heo 	.qc_defer		= mv_qc_defer,
6928930ff25STejun Heo 	.qc_prep		= mv_qc_prep,
6938930ff25STejun Heo 	.qc_issue		= mv_qc_issue,
6948930ff25STejun Heo 
6958930ff25STejun Heo 	.dev_config             = mv6_dev_config,
6968930ff25STejun Heo 
6978930ff25STejun Heo 	.freeze			= mv_eh_freeze,
6988930ff25STejun Heo 	.thaw			= mv_eh_thaw,
6998930ff25STejun Heo 	.hardreset		= mv_hardreset,
7008930ff25STejun Heo 	.softreset		= mv_softreset,
701e49856d8SMark Lord 	.pmp_hardreset		= mv_pmp_hardreset,
702e49856d8SMark Lord 	.pmp_softreset		= mv_softreset,
70329d187bbSMark Lord 	.error_handler		= mv_pmp_error_handler,
704da14265eSMark Lord 
7058930ff25STejun Heo 	.scr_read		= mv_scr_read,
7068930ff25STejun Heo 	.scr_write		= mv_scr_write,
7078930ff25STejun Heo 
708d16ab3f6SMark Lord 	.sff_check_status	= mv_sff_check_status,
709da14265eSMark Lord 	.sff_irq_clear		= mv_sff_irq_clear,
710da14265eSMark Lord 	.check_atapi_dma	= mv_check_atapi_dma,
711da14265eSMark Lord 	.bmdma_setup		= mv_bmdma_setup,
712da14265eSMark Lord 	.bmdma_start		= mv_bmdma_start,
713da14265eSMark Lord 	.bmdma_stop		= mv_bmdma_stop,
714da14265eSMark Lord 	.bmdma_status		= mv_bmdma_status,
7158930ff25STejun Heo 
7168930ff25STejun Heo 	.port_start		= mv_port_start,
7178930ff25STejun Heo 	.port_stop		= mv_port_stop,
718c6fd2807SJeff Garzik };
719c6fd2807SJeff Garzik 
720029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
721029cfd6bSTejun Heo 	.inherits		= &mv6_ops,
722029cfd6bSTejun Heo 	.dev_config		= ATA_OP_NULL,
723c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep_iie,
724c6fd2807SJeff Garzik };
725c6fd2807SJeff Garzik 
726c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
727c6fd2807SJeff Garzik 	{  /* chip_504x */
72891b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS,
729c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
730bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
731c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
732c6fd2807SJeff Garzik 	},
733c6fd2807SJeff Garzik 	{  /* chip_508x */
73491b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
735c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
736bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
737c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
738c6fd2807SJeff Garzik 	},
739c6fd2807SJeff Garzik 	{  /* chip_5080 */
74091b1a84cSMark Lord 		.flags		= MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
741c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
742bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
743c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
744c6fd2807SJeff Garzik 	},
745c6fd2807SJeff Garzik 	{  /* chip_604x */
74691b1a84cSMark Lord 		.flags		= MV_GEN_II_FLAGS,
747c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
748bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
749c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
750c6fd2807SJeff Garzik 	},
751c6fd2807SJeff Garzik 	{  /* chip_608x */
75291b1a84cSMark Lord 		.flags		= MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
753c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
754bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
755c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
756c6fd2807SJeff Garzik 	},
757c6fd2807SJeff Garzik 	{  /* chip_6042 */
75891b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
759c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
760bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
761c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
762c6fd2807SJeff Garzik 	},
763c6fd2807SJeff Garzik 	{  /* chip_7042 */
76491b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
765c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
766bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
767c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
768c6fd2807SJeff Garzik 	},
769f351b2d6SSaeed Bishara 	{  /* chip_soc */
77091b1a84cSMark Lord 		.flags		= MV_GEN_IIE_FLAGS,
771c361acbcSMark Lord 		.pio_mask	= ATA_PIO4,
772f351b2d6SSaeed Bishara 		.udma_mask	= ATA_UDMA6,
773f351b2d6SSaeed Bishara 		.port_ops	= &mv_iie_ops,
774f351b2d6SSaeed Bishara 	},
775c6fd2807SJeff Garzik };
776c6fd2807SJeff Garzik 
777c6fd2807SJeff Garzik static const struct pci_device_id mv_pci_tbl[] = {
7782d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
7792d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
7802d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
7812d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
78246c5784cSMark Lord 	/* RocketRAID 1720/174x have different identifiers */
78346c5784cSMark Lord 	{ PCI_VDEVICE(TTI, 0x1720), chip_6042 },
7844462254aSMark Lord 	{ PCI_VDEVICE(TTI, 0x1740), chip_6042 },
7854462254aSMark Lord 	{ PCI_VDEVICE(TTI, 0x1742), chip_6042 },
786c6fd2807SJeff Garzik 
7872d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
7882d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
7892d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
7902d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
7912d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
792c6fd2807SJeff Garzik 
7932d2744fcSJeff Garzik 	{ PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
7942d2744fcSJeff Garzik 
795d9f9c6bcSFlorian Attenberger 	/* Adaptec 1430SA */
796d9f9c6bcSFlorian Attenberger 	{ PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
797d9f9c6bcSFlorian Attenberger 
79802a121daSMark Lord 	/* Marvell 7042 support */
7996a3d586dSMorrison, Tom 	{ PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
8006a3d586dSMorrison, Tom 
80102a121daSMark Lord 	/* Highpoint RocketRAID PCIe series */
80202a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2300), chip_7042 },
80302a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2310), chip_7042 },
80402a121daSMark Lord 
805c6fd2807SJeff Garzik 	{ }			/* terminate list */
806c6fd2807SJeff Garzik };
807c6fd2807SJeff Garzik 
808c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
809c6fd2807SJeff Garzik 	.phy_errata		= mv5_phy_errata,
810c6fd2807SJeff Garzik 	.enable_leds		= mv5_enable_leds,
811c6fd2807SJeff Garzik 	.read_preamp		= mv5_read_preamp,
812c6fd2807SJeff Garzik 	.reset_hc		= mv5_reset_hc,
813c6fd2807SJeff Garzik 	.reset_flash		= mv5_reset_flash,
814c6fd2807SJeff Garzik 	.reset_bus		= mv5_reset_bus,
815c6fd2807SJeff Garzik };
816c6fd2807SJeff Garzik 
817c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
818c6fd2807SJeff Garzik 	.phy_errata		= mv6_phy_errata,
819c6fd2807SJeff Garzik 	.enable_leds		= mv6_enable_leds,
820c6fd2807SJeff Garzik 	.read_preamp		= mv6_read_preamp,
821c6fd2807SJeff Garzik 	.reset_hc		= mv6_reset_hc,
822c6fd2807SJeff Garzik 	.reset_flash		= mv6_reset_flash,
823c6fd2807SJeff Garzik 	.reset_bus		= mv_reset_pci_bus,
824c6fd2807SJeff Garzik };
825c6fd2807SJeff Garzik 
826f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
827f351b2d6SSaeed Bishara 	.phy_errata		= mv6_phy_errata,
828f351b2d6SSaeed Bishara 	.enable_leds		= mv_soc_enable_leds,
829f351b2d6SSaeed Bishara 	.read_preamp		= mv_soc_read_preamp,
830f351b2d6SSaeed Bishara 	.reset_hc		= mv_soc_reset_hc,
831f351b2d6SSaeed Bishara 	.reset_flash		= mv_soc_reset_flash,
832f351b2d6SSaeed Bishara 	.reset_bus		= mv_soc_reset_bus,
833f351b2d6SSaeed Bishara };
834f351b2d6SSaeed Bishara 
83529b7e43cSMartin Michlmayr static const struct mv_hw_ops mv_soc_65n_ops = {
83629b7e43cSMartin Michlmayr 	.phy_errata		= mv_soc_65n_phy_errata,
83729b7e43cSMartin Michlmayr 	.enable_leds		= mv_soc_enable_leds,
83829b7e43cSMartin Michlmayr 	.reset_hc		= mv_soc_reset_hc,
83929b7e43cSMartin Michlmayr 	.reset_flash		= mv_soc_reset_flash,
84029b7e43cSMartin Michlmayr 	.reset_bus		= mv_soc_reset_bus,
84129b7e43cSMartin Michlmayr };
84229b7e43cSMartin Michlmayr 
843c6fd2807SJeff Garzik /*
844c6fd2807SJeff Garzik  * Functions
845c6fd2807SJeff Garzik  */
846c6fd2807SJeff Garzik 
847c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
848c6fd2807SJeff Garzik {
849c6fd2807SJeff Garzik 	writel(data, addr);
850c6fd2807SJeff Garzik 	(void) readl(addr);	/* flush to avoid PCI posted write */
851c6fd2807SJeff Garzik }
852c6fd2807SJeff Garzik 
853c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
854c6fd2807SJeff Garzik {
855c6fd2807SJeff Garzik 	return port >> MV_PORT_HC_SHIFT;
856c6fd2807SJeff Garzik }
857c6fd2807SJeff Garzik 
858c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
859c6fd2807SJeff Garzik {
860c6fd2807SJeff Garzik 	return port & MV_PORT_MASK;
861c6fd2807SJeff Garzik }
862c6fd2807SJeff Garzik 
8631cfd19aeSMark Lord /*
8641cfd19aeSMark Lord  * Consolidate some rather tricky bit shift calculations.
8651cfd19aeSMark Lord  * This is hot-path stuff, so not a function.
8661cfd19aeSMark Lord  * Simple code, with two return values, so macro rather than inline.
8671cfd19aeSMark Lord  *
8681cfd19aeSMark Lord  * port is the sole input, in range 0..7.
8697368f919SMark Lord  * shift is one output, for use with main_irq_cause / main_irq_mask registers.
8707368f919SMark Lord  * hardport is the other output, in range 0..3.
8711cfd19aeSMark Lord  *
8721cfd19aeSMark Lord  * Note that port and hardport may be the same variable in some cases.
8731cfd19aeSMark Lord  */
8741cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport)	\
8751cfd19aeSMark Lord {								\
8761cfd19aeSMark Lord 	shift    = mv_hc_from_port(port) * HC_SHIFT;		\
8771cfd19aeSMark Lord 	hardport = mv_hardport_from_port(port);			\
8781cfd19aeSMark Lord 	shift   += hardport * 2;				\
8791cfd19aeSMark Lord }
8801cfd19aeSMark Lord 
881352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
882352fab70SMark Lord {
883cae5a29dSMark Lord 	return (base + SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
884352fab70SMark Lord }
885352fab70SMark Lord 
886c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
887c6fd2807SJeff Garzik 						 unsigned int port)
888c6fd2807SJeff Garzik {
889c6fd2807SJeff Garzik 	return mv_hc_base(base, mv_hc_from_port(port));
890c6fd2807SJeff Garzik }
891c6fd2807SJeff Garzik 
892c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
893c6fd2807SJeff Garzik {
894c6fd2807SJeff Garzik 	return  mv_hc_base_from_port(base, port) +
895c6fd2807SJeff Garzik 		MV_SATAHC_ARBTR_REG_SZ +
896c6fd2807SJeff Garzik 		(mv_hardport_from_port(port) * MV_PORT_REG_SZ);
897c6fd2807SJeff Garzik }
898c6fd2807SJeff Garzik 
899e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
900e12bef50SMark Lord {
901e12bef50SMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
902e12bef50SMark Lord 	unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
903e12bef50SMark Lord 
904e12bef50SMark Lord 	return hc_mmio + ofs;
905e12bef50SMark Lord }
906e12bef50SMark Lord 
907f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
908f351b2d6SSaeed Bishara {
909f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
910f351b2d6SSaeed Bishara 	return hpriv->base;
911f351b2d6SSaeed Bishara }
912f351b2d6SSaeed Bishara 
913c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
914c6fd2807SJeff Garzik {
915f351b2d6SSaeed Bishara 	return mv_port_base(mv_host_base(ap->host), ap->port_no);
916c6fd2807SJeff Garzik }
917c6fd2807SJeff Garzik 
918cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
919c6fd2807SJeff Garzik {
920cca3974eSJeff Garzik 	return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
921c6fd2807SJeff Garzik }
922c6fd2807SJeff Garzik 
92308da1759SMark Lord /**
92408da1759SMark Lord  *      mv_save_cached_regs - (re-)initialize cached port registers
92508da1759SMark Lord  *      @ap: the port whose registers we are caching
92608da1759SMark Lord  *
92708da1759SMark Lord  *	Initialize the local cache of port registers,
92808da1759SMark Lord  *	so that reading them over and over again can
92908da1759SMark Lord  *	be avoided on the hotter paths of this driver.
93008da1759SMark Lord  *	This saves a few microseconds each time we switch
93108da1759SMark Lord  *	to/from EDMA mode to perform (eg.) a drive cache flush.
93208da1759SMark Lord  */
93308da1759SMark Lord static void mv_save_cached_regs(struct ata_port *ap)
93408da1759SMark Lord {
93508da1759SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
93608da1759SMark Lord 	struct mv_port_priv *pp = ap->private_data;
93708da1759SMark Lord 
938cae5a29dSMark Lord 	pp->cached.fiscfg = readl(port_mmio + FISCFG);
939cae5a29dSMark Lord 	pp->cached.ltmode = readl(port_mmio + LTMODE);
940cae5a29dSMark Lord 	pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND);
941cae5a29dSMark Lord 	pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD);
94208da1759SMark Lord }
94308da1759SMark Lord 
94408da1759SMark Lord /**
94508da1759SMark Lord  *      mv_write_cached_reg - write to a cached port register
94608da1759SMark Lord  *      @addr: hardware address of the register
94708da1759SMark Lord  *      @old: pointer to cached value of the register
94808da1759SMark Lord  *      @new: new value for the register
94908da1759SMark Lord  *
95008da1759SMark Lord  *	Write a new value to a cached register,
95108da1759SMark Lord  *	but only if the value is different from before.
95208da1759SMark Lord  */
95308da1759SMark Lord static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
95408da1759SMark Lord {
95508da1759SMark Lord 	if (new != *old) {
95612f3b6d7SMark Lord 		unsigned long laddr;
95708da1759SMark Lord 		*old = new;
95812f3b6d7SMark Lord 		/*
95912f3b6d7SMark Lord 		 * Workaround for 88SX60x1-B2 FEr SATA#13:
96012f3b6d7SMark Lord 		 * Read-after-write is needed to prevent generating 64-bit
96112f3b6d7SMark Lord 		 * write cycles on the PCI bus for SATA interface registers
96212f3b6d7SMark Lord 		 * at offsets ending in 0x4 or 0xc.
96312f3b6d7SMark Lord 		 *
96412f3b6d7SMark Lord 		 * Looks like a lot of fuss, but it avoids an unnecessary
96512f3b6d7SMark Lord 		 * +1 usec read-after-write delay for unaffected registers.
96612f3b6d7SMark Lord 		 */
96712f3b6d7SMark Lord 		laddr = (long)addr & 0xffff;
96812f3b6d7SMark Lord 		if (laddr >= 0x300 && laddr <= 0x33c) {
96912f3b6d7SMark Lord 			laddr &= 0x000f;
97012f3b6d7SMark Lord 			if (laddr == 0x4 || laddr == 0xc) {
97112f3b6d7SMark Lord 				writelfl(new, addr); /* read after write */
97212f3b6d7SMark Lord 				return;
97312f3b6d7SMark Lord 			}
97412f3b6d7SMark Lord 		}
97512f3b6d7SMark Lord 		writel(new, addr); /* unaffected by the errata */
97608da1759SMark Lord 	}
97708da1759SMark Lord }
97808da1759SMark Lord 
979c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
980c5d3e45aSJeff Garzik 			     struct mv_host_priv *hpriv,
981c5d3e45aSJeff Garzik 			     struct mv_port_priv *pp)
982c5d3e45aSJeff Garzik {
983bdd4dddeSJeff Garzik 	u32 index;
984bdd4dddeSJeff Garzik 
985c5d3e45aSJeff Garzik 	/*
986c5d3e45aSJeff Garzik 	 * initialize request queue
987c5d3e45aSJeff Garzik 	 */
988fcfb1f77SMark Lord 	pp->req_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
989fcfb1f77SMark Lord 	index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
990bdd4dddeSJeff Garzik 
991c5d3e45aSJeff Garzik 	WARN_ON(pp->crqb_dma & 0x3ff);
992cae5a29dSMark Lord 	writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI);
993bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
994cae5a29dSMark Lord 		 port_mmio + EDMA_REQ_Q_IN_PTR);
995cae5a29dSMark Lord 	writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR);
996c5d3e45aSJeff Garzik 
997c5d3e45aSJeff Garzik 	/*
998c5d3e45aSJeff Garzik 	 * initialize response queue
999c5d3e45aSJeff Garzik 	 */
1000fcfb1f77SMark Lord 	pp->resp_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
1001fcfb1f77SMark Lord 	index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
1002bdd4dddeSJeff Garzik 
1003c5d3e45aSJeff Garzik 	WARN_ON(pp->crpb_dma & 0xff);
1004cae5a29dSMark Lord 	writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI);
1005cae5a29dSMark Lord 	writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR);
1006bdd4dddeSJeff Garzik 	writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
1007cae5a29dSMark Lord 		 port_mmio + EDMA_RSP_Q_OUT_PTR);
1008c5d3e45aSJeff Garzik }
1009c5d3e45aSJeff Garzik 
10102b748a0aSMark Lord static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
10112b748a0aSMark Lord {
10122b748a0aSMark Lord 	/*
10132b748a0aSMark Lord 	 * When writing to the main_irq_mask in hardware,
10142b748a0aSMark Lord 	 * we must ensure exclusivity between the interrupt coalescing bits
10152b748a0aSMark Lord 	 * and the corresponding individual port DONE_IRQ bits.
10162b748a0aSMark Lord 	 *
10172b748a0aSMark Lord 	 * Note that this register is really an "IRQ enable" register,
10182b748a0aSMark Lord 	 * not an "IRQ mask" register as Marvell's naming might suggest.
10192b748a0aSMark Lord 	 */
10202b748a0aSMark Lord 	if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
10212b748a0aSMark Lord 		mask &= ~DONE_IRQ_0_3;
10222b748a0aSMark Lord 	if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
10232b748a0aSMark Lord 		mask &= ~DONE_IRQ_4_7;
10242b748a0aSMark Lord 	writelfl(mask, hpriv->main_irq_mask_addr);
10252b748a0aSMark Lord }
10262b748a0aSMark Lord 
1027c4de573bSMark Lord static void mv_set_main_irq_mask(struct ata_host *host,
1028c4de573bSMark Lord 				 u32 disable_bits, u32 enable_bits)
1029c4de573bSMark Lord {
1030c4de573bSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1031c4de573bSMark Lord 	u32 old_mask, new_mask;
1032c4de573bSMark Lord 
103396e2c487SMark Lord 	old_mask = hpriv->main_irq_mask;
1034c4de573bSMark Lord 	new_mask = (old_mask & ~disable_bits) | enable_bits;
103596e2c487SMark Lord 	if (new_mask != old_mask) {
103696e2c487SMark Lord 		hpriv->main_irq_mask = new_mask;
10372b748a0aSMark Lord 		mv_write_main_irq_mask(new_mask, hpriv);
1038c4de573bSMark Lord 	}
103996e2c487SMark Lord }
1040c4de573bSMark Lord 
1041c4de573bSMark Lord static void mv_enable_port_irqs(struct ata_port *ap,
1042c4de573bSMark Lord 				     unsigned int port_bits)
1043c4de573bSMark Lord {
1044c4de573bSMark Lord 	unsigned int shift, hardport, port = ap->port_no;
1045c4de573bSMark Lord 	u32 disable_bits, enable_bits;
1046c4de573bSMark Lord 
1047c4de573bSMark Lord 	MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
1048c4de573bSMark Lord 
1049c4de573bSMark Lord 	disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
1050c4de573bSMark Lord 	enable_bits  = port_bits << shift;
1051c4de573bSMark Lord 	mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
1052c4de573bSMark Lord }
1053c4de573bSMark Lord 
105400b81235SMark Lord static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
105500b81235SMark Lord 					  void __iomem *port_mmio,
105600b81235SMark Lord 					  unsigned int port_irqs)
1057c6fd2807SJeff Garzik {
10580c58912eSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1059352fab70SMark Lord 	int hardport = mv_hardport_from_port(ap->port_no);
10600c58912eSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(
1061b0bccb18SMark Lord 				mv_host_base(ap->host), ap->port_no);
1062cae6edc3SMark Lord 	u32 hc_irq_cause;
10630c58912eSMark Lord 
1064bdd4dddeSJeff Garzik 	/* clear EDMA event indicators, if any */
1065cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
1066bdd4dddeSJeff Garzik 
1067cae6edc3SMark Lord 	/* clear pending irq events */
1068cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1069cae5a29dSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
10700c58912eSMark Lord 
10710c58912eSMark Lord 	/* clear FIS IRQ Cause */
1072e4006077SMark Lord 	if (IS_GEN_IIE(hpriv))
1073cae5a29dSMark Lord 		writelfl(0, port_mmio + FIS_IRQ_CAUSE);
10740c58912eSMark Lord 
107500b81235SMark Lord 	mv_enable_port_irqs(ap, port_irqs);
107600b81235SMark Lord }
107700b81235SMark Lord 
10782b748a0aSMark Lord static void mv_set_irq_coalescing(struct ata_host *host,
10792b748a0aSMark Lord 				  unsigned int count, unsigned int usecs)
10802b748a0aSMark Lord {
10812b748a0aSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
10822b748a0aSMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
10832b748a0aSMark Lord 	u32 coal_enable = 0;
10842b748a0aSMark Lord 	unsigned long flags;
10856abf4678SMark Lord 	unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
10862b748a0aSMark Lord 	const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
10872b748a0aSMark Lord 							ALL_PORTS_COAL_DONE;
10882b748a0aSMark Lord 
10892b748a0aSMark Lord 	/* Disable IRQ coalescing if either threshold is zero */
10902b748a0aSMark Lord 	if (!usecs || !count) {
10912b748a0aSMark Lord 		clks = count = 0;
10922b748a0aSMark Lord 	} else {
10932b748a0aSMark Lord 		/* Respect maximum limits of the hardware */
10942b748a0aSMark Lord 		clks = usecs * COAL_CLOCKS_PER_USEC;
10952b748a0aSMark Lord 		if (clks > MAX_COAL_TIME_THRESHOLD)
10962b748a0aSMark Lord 			clks = MAX_COAL_TIME_THRESHOLD;
10972b748a0aSMark Lord 		if (count > MAX_COAL_IO_COUNT)
10982b748a0aSMark Lord 			count = MAX_COAL_IO_COUNT;
10992b748a0aSMark Lord 	}
11002b748a0aSMark Lord 
11012b748a0aSMark Lord 	spin_lock_irqsave(&host->lock, flags);
11026abf4678SMark Lord 	mv_set_main_irq_mask(host, coal_disable, 0);
11032b748a0aSMark Lord 
11046abf4678SMark Lord 	if (is_dual_hc && !IS_GEN_I(hpriv)) {
11052b748a0aSMark Lord 		/*
11066abf4678SMark Lord 		 * GEN_II/GEN_IIE with dual host controllers:
11076abf4678SMark Lord 		 * one set of global thresholds for the entire chip.
11082b748a0aSMark Lord 		 */
1109cae5a29dSMark Lord 		writel(clks,  mmio + IRQ_COAL_TIME_THRESHOLD);
1110cae5a29dSMark Lord 		writel(count, mmio + IRQ_COAL_IO_THRESHOLD);
11112b748a0aSMark Lord 		/* clear leftover coal IRQ bit */
1112cae5a29dSMark Lord 		writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
11136abf4678SMark Lord 		if (count)
11142b748a0aSMark Lord 			coal_enable = ALL_PORTS_COAL_DONE;
11156abf4678SMark Lord 		clks = count = 0; /* force clearing of regular regs below */
11162b748a0aSMark Lord 	}
11176abf4678SMark Lord 
11182b748a0aSMark Lord 	/*
11192b748a0aSMark Lord 	 * All chips: independent thresholds for each HC on the chip.
11202b748a0aSMark Lord 	 */
11212b748a0aSMark Lord 	hc_mmio = mv_hc_base_from_port(mmio, 0);
1122cae5a29dSMark Lord 	writel(clks,  hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1123cae5a29dSMark Lord 	writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1124cae5a29dSMark Lord 	writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11256abf4678SMark Lord 	if (count)
11262b748a0aSMark Lord 		coal_enable |= PORTS_0_3_COAL_DONE;
11276abf4678SMark Lord 	if (is_dual_hc) {
11282b748a0aSMark Lord 		hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
1129cae5a29dSMark Lord 		writel(clks,  hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1130cae5a29dSMark Lord 		writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1131cae5a29dSMark Lord 		writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11326abf4678SMark Lord 		if (count)
11332b748a0aSMark Lord 			coal_enable |= PORTS_4_7_COAL_DONE;
11342b748a0aSMark Lord 	}
11352b748a0aSMark Lord 
11366abf4678SMark Lord 	mv_set_main_irq_mask(host, 0, coal_enable);
11372b748a0aSMark Lord 	spin_unlock_irqrestore(&host->lock, flags);
11382b748a0aSMark Lord }
11392b748a0aSMark Lord 
114000b81235SMark Lord /**
114100b81235SMark Lord  *      mv_start_edma - Enable eDMA engine
114200b81235SMark Lord  *      @base: port base address
114300b81235SMark Lord  *      @pp: port private data
114400b81235SMark Lord  *
114500b81235SMark Lord  *      Verify the local cache of the eDMA state is accurate with a
114600b81235SMark Lord  *      WARN_ON.
114700b81235SMark Lord  *
114800b81235SMark Lord  *      LOCKING:
114900b81235SMark Lord  *      Inherited from caller.
115000b81235SMark Lord  */
115100b81235SMark Lord static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
115200b81235SMark Lord 			 struct mv_port_priv *pp, u8 protocol)
115300b81235SMark Lord {
115400b81235SMark Lord 	int want_ncq = (protocol == ATA_PROT_NCQ);
115500b81235SMark Lord 
115600b81235SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
115700b81235SMark Lord 		int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
115800b81235SMark Lord 		if (want_ncq != using_ncq)
115900b81235SMark Lord 			mv_stop_edma(ap);
116000b81235SMark Lord 	}
116100b81235SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
116200b81235SMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
116300b81235SMark Lord 
116400b81235SMark Lord 		mv_edma_cfg(ap, want_ncq, 1);
116500b81235SMark Lord 
1166f630d562SMark Lord 		mv_set_edma_ptrs(port_mmio, hpriv, pp);
116700b81235SMark Lord 		mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
1168bdd4dddeSJeff Garzik 
1169cae5a29dSMark Lord 		writelfl(EDMA_EN, port_mmio + EDMA_CMD);
1170c6fd2807SJeff Garzik 		pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
1171c6fd2807SJeff Garzik 	}
1172c6fd2807SJeff Garzik }
1173c6fd2807SJeff Garzik 
11749b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
11759b2c4e0bSMark Lord {
11769b2c4e0bSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
11779b2c4e0bSMark Lord 	const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
11789b2c4e0bSMark Lord 	const int per_loop = 5, timeout = (15 * 1000 / per_loop);
11799b2c4e0bSMark Lord 	int i;
11809b2c4e0bSMark Lord 
11819b2c4e0bSMark Lord 	/*
11829b2c4e0bSMark Lord 	 * Wait for the EDMA engine to finish transactions in progress.
1183c46938ccSMark Lord 	 * No idea what a good "timeout" value might be, but measurements
1184c46938ccSMark Lord 	 * indicate that it often requires hundreds of microseconds
1185c46938ccSMark Lord 	 * with two drives in-use.  So we use the 15msec value above
1186c46938ccSMark Lord 	 * as a rough guess at what even more drives might require.
11879b2c4e0bSMark Lord 	 */
11889b2c4e0bSMark Lord 	for (i = 0; i < timeout; ++i) {
1189cae5a29dSMark Lord 		u32 edma_stat = readl(port_mmio + EDMA_STATUS);
11909b2c4e0bSMark Lord 		if ((edma_stat & empty_idle) == empty_idle)
11919b2c4e0bSMark Lord 			break;
11929b2c4e0bSMark Lord 		udelay(per_loop);
11939b2c4e0bSMark Lord 	}
1194a9a79dfeSJoe Perches 	/* ata_port_info(ap, "%s: %u+ usecs\n", __func__, i); */
11959b2c4e0bSMark Lord }
11969b2c4e0bSMark Lord 
1197c6fd2807SJeff Garzik /**
1198e12bef50SMark Lord  *      mv_stop_edma_engine - Disable eDMA engine
1199b562468cSMark Lord  *      @port_mmio: io base address
1200c6fd2807SJeff Garzik  *
1201c6fd2807SJeff Garzik  *      LOCKING:
1202c6fd2807SJeff Garzik  *      Inherited from caller.
1203c6fd2807SJeff Garzik  */
1204b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
1205c6fd2807SJeff Garzik {
1206b562468cSMark Lord 	int i;
1207c6fd2807SJeff Garzik 
1208b562468cSMark Lord 	/* Disable eDMA.  The disable bit auto clears. */
1209cae5a29dSMark Lord 	writelfl(EDMA_DS, port_mmio + EDMA_CMD);
1210c6fd2807SJeff Garzik 
1211b562468cSMark Lord 	/* Wait for the chip to confirm eDMA is off. */
1212b562468cSMark Lord 	for (i = 10000; i > 0; i--) {
1213cae5a29dSMark Lord 		u32 reg = readl(port_mmio + EDMA_CMD);
12144537deb5SJeff Garzik 		if (!(reg & EDMA_EN))
1215b562468cSMark Lord 			return 0;
1216b562468cSMark Lord 		udelay(10);
1217c6fd2807SJeff Garzik 	}
1218b562468cSMark Lord 	return -EIO;
1219c6fd2807SJeff Garzik }
1220c6fd2807SJeff Garzik 
1221e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
1222c6fd2807SJeff Garzik {
1223c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1224c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
122566e57a2cSMark Lord 	int err = 0;
1226c6fd2807SJeff Garzik 
1227b562468cSMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
1228b562468cSMark Lord 		return 0;
1229c6fd2807SJeff Garzik 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
12309b2c4e0bSMark Lord 	mv_wait_for_edma_empty_idle(ap);
1231b562468cSMark Lord 	if (mv_stop_edma_engine(port_mmio)) {
1232a9a79dfeSJoe Perches 		ata_port_err(ap, "Unable to stop eDMA\n");
123366e57a2cSMark Lord 		err = -EIO;
1234c6fd2807SJeff Garzik 	}
123566e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
123666e57a2cSMark Lord 	return err;
12370ea9e179SJeff Garzik }
12380ea9e179SJeff Garzik 
1239c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1240c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes)
1241c6fd2807SJeff Garzik {
1242c6fd2807SJeff Garzik 	int b, w;
1243c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
1244c6fd2807SJeff Garzik 		DPRINTK("%p: ", start + b);
1245c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
1246c6fd2807SJeff Garzik 			printk("%08x ", readl(start + b));
1247c6fd2807SJeff Garzik 			b += sizeof(u32);
1248c6fd2807SJeff Garzik 		}
1249c6fd2807SJeff Garzik 		printk("\n");
1250c6fd2807SJeff Garzik 	}
1251c6fd2807SJeff Garzik }
1252c6fd2807SJeff Garzik #endif
1253c6fd2807SJeff Garzik 
1254c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
1255c6fd2807SJeff Garzik {
1256c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1257c6fd2807SJeff Garzik 	int b, w;
1258c6fd2807SJeff Garzik 	u32 dw;
1259c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
1260c6fd2807SJeff Garzik 		DPRINTK("%02x: ", b);
1261c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
1262c6fd2807SJeff Garzik 			(void) pci_read_config_dword(pdev, b, &dw);
1263c6fd2807SJeff Garzik 			printk("%08x ", dw);
1264c6fd2807SJeff Garzik 			b += sizeof(u32);
1265c6fd2807SJeff Garzik 		}
1266c6fd2807SJeff Garzik 		printk("\n");
1267c6fd2807SJeff Garzik 	}
1268c6fd2807SJeff Garzik #endif
1269c6fd2807SJeff Garzik }
1270c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port,
1271c6fd2807SJeff Garzik 			     struct pci_dev *pdev)
1272c6fd2807SJeff Garzik {
1273c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1274c6fd2807SJeff Garzik 	void __iomem *hc_base = mv_hc_base(mmio_base,
1275c6fd2807SJeff Garzik 					   port >> MV_PORT_HC_SHIFT);
1276c6fd2807SJeff Garzik 	void __iomem *port_base;
1277c6fd2807SJeff Garzik 	int start_port, num_ports, p, start_hc, num_hcs, hc;
1278c6fd2807SJeff Garzik 
1279c6fd2807SJeff Garzik 	if (0 > port) {
1280c6fd2807SJeff Garzik 		start_hc = start_port = 0;
1281c6fd2807SJeff Garzik 		num_ports = 8;		/* shld be benign for 4 port devs */
1282c6fd2807SJeff Garzik 		num_hcs = 2;
1283c6fd2807SJeff Garzik 	} else {
1284c6fd2807SJeff Garzik 		start_hc = port >> MV_PORT_HC_SHIFT;
1285c6fd2807SJeff Garzik 		start_port = port;
1286c6fd2807SJeff Garzik 		num_ports = num_hcs = 1;
1287c6fd2807SJeff Garzik 	}
1288c6fd2807SJeff Garzik 	DPRINTK("All registers for port(s) %u-%u:\n", start_port,
1289c6fd2807SJeff Garzik 		num_ports > 1 ? num_ports - 1 : start_port);
1290c6fd2807SJeff Garzik 
1291c6fd2807SJeff Garzik 	if (NULL != pdev) {
1292c6fd2807SJeff Garzik 		DPRINTK("PCI config space regs:\n");
1293c6fd2807SJeff Garzik 		mv_dump_pci_cfg(pdev, 0x68);
1294c6fd2807SJeff Garzik 	}
1295c6fd2807SJeff Garzik 	DPRINTK("PCI regs:\n");
1296c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xc00, 0x3c);
1297c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xd00, 0x34);
1298c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xf00, 0x4);
1299c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0x1d00, 0x6c);
1300c6fd2807SJeff Garzik 	for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
1301c6fd2807SJeff Garzik 		hc_base = mv_hc_base(mmio_base, hc);
1302c6fd2807SJeff Garzik 		DPRINTK("HC regs (HC %i):\n", hc);
1303c6fd2807SJeff Garzik 		mv_dump_mem(hc_base, 0x1c);
1304c6fd2807SJeff Garzik 	}
1305c6fd2807SJeff Garzik 	for (p = start_port; p < start_port + num_ports; p++) {
1306c6fd2807SJeff Garzik 		port_base = mv_port_base(mmio_base, p);
1307c6fd2807SJeff Garzik 		DPRINTK("EDMA regs (port %i):\n", p);
1308c6fd2807SJeff Garzik 		mv_dump_mem(port_base, 0x54);
1309c6fd2807SJeff Garzik 		DPRINTK("SATA regs (port %i):\n", p);
1310c6fd2807SJeff Garzik 		mv_dump_mem(port_base+0x300, 0x60);
1311c6fd2807SJeff Garzik 	}
1312c6fd2807SJeff Garzik #endif
1313c6fd2807SJeff Garzik }
1314c6fd2807SJeff Garzik 
1315c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1316c6fd2807SJeff Garzik {
1317c6fd2807SJeff Garzik 	unsigned int ofs;
1318c6fd2807SJeff Garzik 
1319c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1320c6fd2807SJeff Garzik 	case SCR_STATUS:
1321c6fd2807SJeff Garzik 	case SCR_CONTROL:
1322c6fd2807SJeff Garzik 	case SCR_ERROR:
1323cae5a29dSMark Lord 		ofs = SATA_STATUS + (sc_reg_in * sizeof(u32));
1324c6fd2807SJeff Garzik 		break;
1325c6fd2807SJeff Garzik 	case SCR_ACTIVE:
1326cae5a29dSMark Lord 		ofs = SATA_ACTIVE;   /* active is not with the others */
1327c6fd2807SJeff Garzik 		break;
1328c6fd2807SJeff Garzik 	default:
1329c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1330c6fd2807SJeff Garzik 		break;
1331c6fd2807SJeff Garzik 	}
1332c6fd2807SJeff Garzik 	return ofs;
1333c6fd2807SJeff Garzik }
1334c6fd2807SJeff Garzik 
133582ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
1336c6fd2807SJeff Garzik {
1337c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1338c6fd2807SJeff Garzik 
1339da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
134082ef04fbSTejun Heo 		*val = readl(mv_ap_base(link->ap) + ofs);
1341da3dbb17STejun Heo 		return 0;
1342da3dbb17STejun Heo 	} else
1343da3dbb17STejun Heo 		return -EINVAL;
1344c6fd2807SJeff Garzik }
1345c6fd2807SJeff Garzik 
134682ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
1347c6fd2807SJeff Garzik {
1348c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1349c6fd2807SJeff Garzik 
1350da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
135120091773SMark Lord 		void __iomem *addr = mv_ap_base(link->ap) + ofs;
135220091773SMark Lord 		if (sc_reg_in == SCR_CONTROL) {
135320091773SMark Lord 			/*
135420091773SMark Lord 			 * Workaround for 88SX60x1 FEr SATA#26:
135520091773SMark Lord 			 *
135625985edcSLucas De Marchi 			 * COMRESETs have to take care not to accidentally
135720091773SMark Lord 			 * put the drive to sleep when writing SCR_CONTROL.
135820091773SMark Lord 			 * Setting bits 12..15 prevents this problem.
135920091773SMark Lord 			 *
136020091773SMark Lord 			 * So if we see an outbound COMMRESET, set those bits.
136120091773SMark Lord 			 * Ditto for the followup write that clears the reset.
136220091773SMark Lord 			 *
136320091773SMark Lord 			 * The proprietary driver does this for
136420091773SMark Lord 			 * all chip versions, and so do we.
136520091773SMark Lord 			 */
136620091773SMark Lord 			if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1)
136720091773SMark Lord 				val |= 0xf000;
136820091773SMark Lord 		}
136920091773SMark Lord 		writelfl(val, addr);
1370da3dbb17STejun Heo 		return 0;
1371da3dbb17STejun Heo 	} else
1372da3dbb17STejun Heo 		return -EINVAL;
1373c6fd2807SJeff Garzik }
1374c6fd2807SJeff Garzik 
1375f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1376f273827eSMark Lord {
1377f273827eSMark Lord 	/*
1378e49856d8SMark Lord 	 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1379e49856d8SMark Lord 	 *
1380e49856d8SMark Lord 	 * Gen-II does not support NCQ over a port multiplier
1381e49856d8SMark Lord 	 *  (no FIS-based switching).
1382f273827eSMark Lord 	 */
1383e49856d8SMark Lord 	if (adev->flags & ATA_DFLAG_NCQ) {
1384352fab70SMark Lord 		if (sata_pmp_attached(adev->link->ap)) {
1385e49856d8SMark Lord 			adev->flags &= ~ATA_DFLAG_NCQ;
1386a9a79dfeSJoe Perches 			ata_dev_info(adev,
1387352fab70SMark Lord 				"NCQ disabled for command-based switching\n");
1388352fab70SMark Lord 		}
1389f273827eSMark Lord 	}
1390e49856d8SMark Lord }
1391f273827eSMark Lord 
13923e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc)
13933e4a1391SMark Lord {
13943e4a1391SMark Lord 	struct ata_link *link = qc->dev->link;
13953e4a1391SMark Lord 	struct ata_port *ap = link->ap;
13963e4a1391SMark Lord 	struct mv_port_priv *pp = ap->private_data;
13973e4a1391SMark Lord 
13983e4a1391SMark Lord 	/*
139929d187bbSMark Lord 	 * Don't allow new commands if we're in a delayed EH state
140029d187bbSMark Lord 	 * for NCQ and/or FIS-based switching.
140129d187bbSMark Lord 	 */
140229d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
140329d187bbSMark Lord 		return ATA_DEFER_PORT;
1404159a7ff7SGwendal Grignou 
1405159a7ff7SGwendal Grignou 	/* PIO commands need exclusive link: no other commands [DMA or PIO]
1406159a7ff7SGwendal Grignou 	 * can run concurrently.
1407159a7ff7SGwendal Grignou 	 * set excl_link when we want to send a PIO command in DMA mode
1408159a7ff7SGwendal Grignou 	 * or a non-NCQ command in NCQ mode.
1409159a7ff7SGwendal Grignou 	 * When we receive a command from that link, and there are no
1410159a7ff7SGwendal Grignou 	 * outstanding commands, mark a flag to clear excl_link and let
1411159a7ff7SGwendal Grignou 	 * the command go through.
1412159a7ff7SGwendal Grignou 	 */
1413159a7ff7SGwendal Grignou 	if (unlikely(ap->excl_link)) {
1414159a7ff7SGwendal Grignou 		if (link == ap->excl_link) {
1415159a7ff7SGwendal Grignou 			if (ap->nr_active_links)
1416159a7ff7SGwendal Grignou 				return ATA_DEFER_PORT;
1417159a7ff7SGwendal Grignou 			qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
1418159a7ff7SGwendal Grignou 			return 0;
1419159a7ff7SGwendal Grignou 		} else
1420159a7ff7SGwendal Grignou 			return ATA_DEFER_PORT;
1421159a7ff7SGwendal Grignou 	}
1422159a7ff7SGwendal Grignou 
142329d187bbSMark Lord 	/*
14243e4a1391SMark Lord 	 * If the port is completely idle, then allow the new qc.
14253e4a1391SMark Lord 	 */
14263e4a1391SMark Lord 	if (ap->nr_active_links == 0)
14273e4a1391SMark Lord 		return 0;
14283e4a1391SMark Lord 
14293e4a1391SMark Lord 	/*
14304bdee6c5STejun Heo 	 * The port is operating in host queuing mode (EDMA) with NCQ
14314bdee6c5STejun Heo 	 * enabled, allow multiple NCQ commands.  EDMA also allows
14324bdee6c5STejun Heo 	 * queueing multiple DMA commands but libata core currently
14334bdee6c5STejun Heo 	 * doesn't allow it.
14343e4a1391SMark Lord 	 */
14354bdee6c5STejun Heo 	if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
1436159a7ff7SGwendal Grignou 	    (pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
1437159a7ff7SGwendal Grignou 		if (ata_is_ncq(qc->tf.protocol))
14383e4a1391SMark Lord 			return 0;
1439159a7ff7SGwendal Grignou 		else {
1440159a7ff7SGwendal Grignou 			ap->excl_link = link;
1441159a7ff7SGwendal Grignou 			return ATA_DEFER_PORT;
1442159a7ff7SGwendal Grignou 		}
1443159a7ff7SGwendal Grignou 	}
14444bdee6c5STejun Heo 
14453e4a1391SMark Lord 	return ATA_DEFER_PORT;
14463e4a1391SMark Lord }
14473e4a1391SMark Lord 
144808da1759SMark Lord static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
1449e49856d8SMark Lord {
145008da1759SMark Lord 	struct mv_port_priv *pp = ap->private_data;
145108da1759SMark Lord 	void __iomem *port_mmio;
145200f42eabSMark Lord 
145308da1759SMark Lord 	u32 fiscfg,   *old_fiscfg   = &pp->cached.fiscfg;
145408da1759SMark Lord 	u32 ltmode,   *old_ltmode   = &pp->cached.ltmode;
145508da1759SMark Lord 	u32 haltcond, *old_haltcond = &pp->cached.haltcond;
145600f42eabSMark Lord 
145708da1759SMark Lord 	ltmode   = *old_ltmode & ~LTMODE_BIT8;
145808da1759SMark Lord 	haltcond = *old_haltcond | EDMA_ERR_DEV;
145900f42eabSMark Lord 
146000f42eabSMark Lord 	if (want_fbs) {
146108da1759SMark Lord 		fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
146208da1759SMark Lord 		ltmode = *old_ltmode | LTMODE_BIT8;
14634c299ca3SMark Lord 		if (want_ncq)
146408da1759SMark Lord 			haltcond &= ~EDMA_ERR_DEV;
14654c299ca3SMark Lord 		else
146608da1759SMark Lord 			fiscfg |=  FISCFG_WAIT_DEV_ERR;
146708da1759SMark Lord 	} else {
146808da1759SMark Lord 		fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
1469e49856d8SMark Lord 	}
147000f42eabSMark Lord 
147108da1759SMark Lord 	port_mmio = mv_ap_base(ap);
1472cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + FISCFG, old_fiscfg, fiscfg);
1473cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + LTMODE, old_ltmode, ltmode);
1474cae5a29dSMark Lord 	mv_write_cached_reg(port_mmio + EDMA_HALTCOND, old_haltcond, haltcond);
1475e49856d8SMark Lord }
1476c6fd2807SJeff Garzik 
1477dd2890f6SMark Lord static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1478dd2890f6SMark Lord {
1479dd2890f6SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1480dd2890f6SMark Lord 	u32 old, new;
1481dd2890f6SMark Lord 
1482dd2890f6SMark Lord 	/* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1483cae5a29dSMark Lord 	old = readl(hpriv->base + GPIO_PORT_CTL);
1484dd2890f6SMark Lord 	if (want_ncq)
1485dd2890f6SMark Lord 		new = old | (1 << 22);
1486dd2890f6SMark Lord 	else
1487dd2890f6SMark Lord 		new = old & ~(1 << 22);
1488dd2890f6SMark Lord 	if (new != old)
1489cae5a29dSMark Lord 		writel(new, hpriv->base + GPIO_PORT_CTL);
1490dd2890f6SMark Lord }
1491dd2890f6SMark Lord 
1492c01e8a23SMark Lord /**
1493c01e8a23SMark Lord  *	mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
1494c01e8a23SMark Lord  *	@ap: Port being initialized
1495c01e8a23SMark Lord  *
1496c01e8a23SMark Lord  *	There are two DMA modes on these chips:  basic DMA, and EDMA.
1497c01e8a23SMark Lord  *
1498c01e8a23SMark Lord  *	Bit-0 of the "EDMA RESERVED" register enables/disables use
1499c01e8a23SMark Lord  *	of basic DMA on the GEN_IIE versions of the chips.
1500c01e8a23SMark Lord  *
1501c01e8a23SMark Lord  *	This bit survives EDMA resets, and must be set for basic DMA
1502c01e8a23SMark Lord  *	to function, and should be cleared when EDMA is active.
1503c01e8a23SMark Lord  */
1504c01e8a23SMark Lord static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
1505c01e8a23SMark Lord {
1506c01e8a23SMark Lord 	struct mv_port_priv *pp = ap->private_data;
1507c01e8a23SMark Lord 	u32 new, *old = &pp->cached.unknown_rsvd;
1508c01e8a23SMark Lord 
1509c01e8a23SMark Lord 	if (enable_bmdma)
1510c01e8a23SMark Lord 		new = *old | 1;
1511c01e8a23SMark Lord 	else
1512c01e8a23SMark Lord 		new = *old & ~1;
1513cae5a29dSMark Lord 	mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD, old, new);
1514c01e8a23SMark Lord }
1515c01e8a23SMark Lord 
1516000b344fSMark Lord /*
1517000b344fSMark Lord  * SOC chips have an issue whereby the HDD LEDs don't always blink
1518000b344fSMark Lord  * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
1519000b344fSMark Lord  * of the SOC takes care of it, generating a steady blink rate when
1520000b344fSMark Lord  * any drive on the chip is active.
1521000b344fSMark Lord  *
1522000b344fSMark Lord  * Unfortunately, the blink mode is a global hardware setting for the SOC,
1523000b344fSMark Lord  * so we must use it whenever at least one port on the SOC has NCQ enabled.
1524000b344fSMark Lord  *
1525000b344fSMark Lord  * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
1526000b344fSMark Lord  * LED operation works then, and provides better (more accurate) feedback.
1527000b344fSMark Lord  *
1528000b344fSMark Lord  * Note that this code assumes that an SOC never has more than one HC onboard.
1529000b344fSMark Lord  */
1530000b344fSMark Lord static void mv_soc_led_blink_enable(struct ata_port *ap)
1531000b344fSMark Lord {
1532000b344fSMark Lord 	struct ata_host *host = ap->host;
1533000b344fSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1534000b344fSMark Lord 	void __iomem *hc_mmio;
1535000b344fSMark Lord 	u32 led_ctrl;
1536000b344fSMark Lord 
1537000b344fSMark Lord 	if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
1538000b344fSMark Lord 		return;
1539000b344fSMark Lord 	hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN;
1540000b344fSMark Lord 	hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1541cae5a29dSMark Lord 	led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1542cae5a29dSMark Lord 	writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1543000b344fSMark Lord }
1544000b344fSMark Lord 
1545000b344fSMark Lord static void mv_soc_led_blink_disable(struct ata_port *ap)
1546000b344fSMark Lord {
1547000b344fSMark Lord 	struct ata_host *host = ap->host;
1548000b344fSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1549000b344fSMark Lord 	void __iomem *hc_mmio;
1550000b344fSMark Lord 	u32 led_ctrl;
1551000b344fSMark Lord 	unsigned int port;
1552000b344fSMark Lord 
1553000b344fSMark Lord 	if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
1554000b344fSMark Lord 		return;
1555000b344fSMark Lord 
1556000b344fSMark Lord 	/* disable led-blink only if no ports are using NCQ */
1557000b344fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
1558000b344fSMark Lord 		struct ata_port *this_ap = host->ports[port];
1559000b344fSMark Lord 		struct mv_port_priv *pp = this_ap->private_data;
1560000b344fSMark Lord 
1561000b344fSMark Lord 		if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1562000b344fSMark Lord 			return;
1563000b344fSMark Lord 	}
1564000b344fSMark Lord 
1565000b344fSMark Lord 	hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
1566000b344fSMark Lord 	hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1567cae5a29dSMark Lord 	led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1568cae5a29dSMark Lord 	writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1569000b344fSMark Lord }
1570000b344fSMark Lord 
157100b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
1572c6fd2807SJeff Garzik {
1573c6fd2807SJeff Garzik 	u32 cfg;
1574e12bef50SMark Lord 	struct mv_port_priv *pp    = ap->private_data;
1575e12bef50SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1576e12bef50SMark Lord 	void __iomem *port_mmio    = mv_ap_base(ap);
1577c6fd2807SJeff Garzik 
1578c6fd2807SJeff Garzik 	/* set up non-NCQ EDMA configuration */
1579c6fd2807SJeff Garzik 	cfg = EDMA_CFG_Q_DEPTH;		/* always 0x1f for *all* chips */
1580d16ab3f6SMark Lord 	pp->pp_flags &=
1581d16ab3f6SMark Lord 	  ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
1582c6fd2807SJeff Garzik 
1583c6fd2807SJeff Garzik 	if (IS_GEN_I(hpriv))
1584c6fd2807SJeff Garzik 		cfg |= (1 << 8);	/* enab config burst size mask */
1585c6fd2807SJeff Garzik 
1586dd2890f6SMark Lord 	else if (IS_GEN_II(hpriv)) {
1587c6fd2807SJeff Garzik 		cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1588dd2890f6SMark Lord 		mv_60x1_errata_sata25(ap, want_ncq);
1589c6fd2807SJeff Garzik 
1590dd2890f6SMark Lord 	} else if (IS_GEN_IIE(hpriv)) {
159100f42eabSMark Lord 		int want_fbs = sata_pmp_attached(ap);
159200f42eabSMark Lord 		/*
159300f42eabSMark Lord 		 * Possible future enhancement:
159400f42eabSMark Lord 		 *
159500f42eabSMark Lord 		 * The chip can use FBS with non-NCQ, if we allow it,
159600f42eabSMark Lord 		 * But first we need to have the error handling in place
159700f42eabSMark Lord 		 * for this mode (datasheet section 7.3.15.4.2.3).
159800f42eabSMark Lord 		 * So disallow non-NCQ FBS for now.
159900f42eabSMark Lord 		 */
160000f42eabSMark Lord 		want_fbs &= want_ncq;
160100f42eabSMark Lord 
160208da1759SMark Lord 		mv_config_fbs(ap, want_ncq, want_fbs);
160300f42eabSMark Lord 
160400f42eabSMark Lord 		if (want_fbs) {
160500f42eabSMark Lord 			pp->pp_flags |= MV_PP_FLAG_FBS_EN;
160600f42eabSMark Lord 			cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
160700f42eabSMark Lord 		}
160800f42eabSMark Lord 
1609e728eabeSJeff Garzik 		cfg |= (1 << 23);	/* do not mask PM field in rx'd FIS */
161000b81235SMark Lord 		if (want_edma) {
1611e728eabeSJeff Garzik 			cfg |= (1 << 22); /* enab 4-entry host queue cache */
16121f398472SMark Lord 			if (!IS_SOC(hpriv))
1613c6fd2807SJeff Garzik 				cfg |= (1 << 18); /* enab early completion */
161400b81235SMark Lord 		}
1615616d4a98SMark Lord 		if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1616616d4a98SMark Lord 			cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
1617c01e8a23SMark Lord 		mv_bmdma_enable_iie(ap, !want_edma);
1618000b344fSMark Lord 
1619000b344fSMark Lord 		if (IS_SOC(hpriv)) {
1620000b344fSMark Lord 			if (want_ncq)
1621000b344fSMark Lord 				mv_soc_led_blink_enable(ap);
1622000b344fSMark Lord 			else
1623000b344fSMark Lord 				mv_soc_led_blink_disable(ap);
1624000b344fSMark Lord 		}
1625c6fd2807SJeff Garzik 	}
1626c6fd2807SJeff Garzik 
162772109168SMark Lord 	if (want_ncq) {
162872109168SMark Lord 		cfg |= EDMA_CFG_NCQ;
162972109168SMark Lord 		pp->pp_flags |=  MV_PP_FLAG_NCQ_EN;
163000b81235SMark Lord 	}
163172109168SMark Lord 
1632cae5a29dSMark Lord 	writelfl(cfg, port_mmio + EDMA_CFG);
1633c6fd2807SJeff Garzik }
1634c6fd2807SJeff Garzik 
1635da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1636da2fa9baSMark Lord {
1637da2fa9baSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1638da2fa9baSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1639eb73d558SMark Lord 	int tag;
1640da2fa9baSMark Lord 
1641da2fa9baSMark Lord 	if (pp->crqb) {
1642da2fa9baSMark Lord 		dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1643da2fa9baSMark Lord 		pp->crqb = NULL;
1644da2fa9baSMark Lord 	}
1645da2fa9baSMark Lord 	if (pp->crpb) {
1646da2fa9baSMark Lord 		dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1647da2fa9baSMark Lord 		pp->crpb = NULL;
1648da2fa9baSMark Lord 	}
1649eb73d558SMark Lord 	/*
1650eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1651eb73d558SMark Lord 	 * For later hardware, we have one unique sg_tbl per NCQ tag.
1652eb73d558SMark Lord 	 */
1653eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1654eb73d558SMark Lord 		if (pp->sg_tbl[tag]) {
1655eb73d558SMark Lord 			if (tag == 0 || !IS_GEN_I(hpriv))
1656eb73d558SMark Lord 				dma_pool_free(hpriv->sg_tbl_pool,
1657eb73d558SMark Lord 					      pp->sg_tbl[tag],
1658eb73d558SMark Lord 					      pp->sg_tbl_dma[tag]);
1659eb73d558SMark Lord 			pp->sg_tbl[tag] = NULL;
1660eb73d558SMark Lord 		}
1661da2fa9baSMark Lord 	}
1662da2fa9baSMark Lord }
1663da2fa9baSMark Lord 
1664c6fd2807SJeff Garzik /**
1665c6fd2807SJeff Garzik  *      mv_port_start - Port specific init/start routine.
1666c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1667c6fd2807SJeff Garzik  *
1668c6fd2807SJeff Garzik  *      Allocate and point to DMA memory, init port private memory,
1669c6fd2807SJeff Garzik  *      zero indices.
1670c6fd2807SJeff Garzik  *
1671c6fd2807SJeff Garzik  *      LOCKING:
1672c6fd2807SJeff Garzik  *      Inherited from caller.
1673c6fd2807SJeff Garzik  */
1674c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1675c6fd2807SJeff Garzik {
1676cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1677cca3974eSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1678c6fd2807SJeff Garzik 	struct mv_port_priv *pp;
1679933cb8e5SMark Lord 	unsigned long flags;
1680dde20207SJames Bottomley 	int tag;
1681c6fd2807SJeff Garzik 
168224dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1683c6fd2807SJeff Garzik 	if (!pp)
168424dc5f33STejun Heo 		return -ENOMEM;
1685da2fa9baSMark Lord 	ap->private_data = pp;
1686c6fd2807SJeff Garzik 
1687da2fa9baSMark Lord 	pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1688da2fa9baSMark Lord 	if (!pp->crqb)
1689da2fa9baSMark Lord 		return -ENOMEM;
1690da2fa9baSMark Lord 	memset(pp->crqb, 0, MV_CRQB_Q_SZ);
1691c6fd2807SJeff Garzik 
1692da2fa9baSMark Lord 	pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1693da2fa9baSMark Lord 	if (!pp->crpb)
1694da2fa9baSMark Lord 		goto out_port_free_dma_mem;
1695da2fa9baSMark Lord 	memset(pp->crpb, 0, MV_CRPB_Q_SZ);
1696c6fd2807SJeff Garzik 
16973bd0a70eSMark Lord 	/* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
16983bd0a70eSMark Lord 	if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
16993bd0a70eSMark Lord 		ap->flags |= ATA_FLAG_AN;
1700eb73d558SMark Lord 	/*
1701eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1702eb73d558SMark Lord 	 * For later hardware, we need one unique sg_tbl per NCQ tag.
1703eb73d558SMark Lord 	 */
1704eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1705eb73d558SMark Lord 		if (tag == 0 || !IS_GEN_I(hpriv)) {
1706eb73d558SMark Lord 			pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1707eb73d558SMark Lord 					      GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1708eb73d558SMark Lord 			if (!pp->sg_tbl[tag])
1709da2fa9baSMark Lord 				goto out_port_free_dma_mem;
1710eb73d558SMark Lord 		} else {
1711eb73d558SMark Lord 			pp->sg_tbl[tag]     = pp->sg_tbl[0];
1712eb73d558SMark Lord 			pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1713eb73d558SMark Lord 		}
1714eb73d558SMark Lord 	}
1715933cb8e5SMark Lord 
1716933cb8e5SMark Lord 	spin_lock_irqsave(ap->lock, flags);
171708da1759SMark Lord 	mv_save_cached_regs(ap);
171866e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
1719933cb8e5SMark Lord 	spin_unlock_irqrestore(ap->lock, flags);
1720933cb8e5SMark Lord 
1721c6fd2807SJeff Garzik 	return 0;
1722da2fa9baSMark Lord 
1723da2fa9baSMark Lord out_port_free_dma_mem:
1724da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1725da2fa9baSMark Lord 	return -ENOMEM;
1726c6fd2807SJeff Garzik }
1727c6fd2807SJeff Garzik 
1728c6fd2807SJeff Garzik /**
1729c6fd2807SJeff Garzik  *      mv_port_stop - Port specific cleanup/stop routine.
1730c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1731c6fd2807SJeff Garzik  *
1732c6fd2807SJeff Garzik  *      Stop DMA, cleanup port memory.
1733c6fd2807SJeff Garzik  *
1734c6fd2807SJeff Garzik  *      LOCKING:
1735cca3974eSJeff Garzik  *      This routine uses the host lock to protect the DMA stop.
1736c6fd2807SJeff Garzik  */
1737c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1738c6fd2807SJeff Garzik {
1739933cb8e5SMark Lord 	unsigned long flags;
1740933cb8e5SMark Lord 
1741933cb8e5SMark Lord 	spin_lock_irqsave(ap->lock, flags);
1742e12bef50SMark Lord 	mv_stop_edma(ap);
174388e675e1SMark Lord 	mv_enable_port_irqs(ap, 0);
1744933cb8e5SMark Lord 	spin_unlock_irqrestore(ap->lock, flags);
1745da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1746c6fd2807SJeff Garzik }
1747c6fd2807SJeff Garzik 
1748c6fd2807SJeff Garzik /**
1749c6fd2807SJeff Garzik  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1750c6fd2807SJeff Garzik  *      @qc: queued command whose SG list to source from
1751c6fd2807SJeff Garzik  *
1752c6fd2807SJeff Garzik  *      Populate the SG list and mark the last entry.
1753c6fd2807SJeff Garzik  *
1754c6fd2807SJeff Garzik  *      LOCKING:
1755c6fd2807SJeff Garzik  *      Inherited from caller.
1756c6fd2807SJeff Garzik  */
17576c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1758c6fd2807SJeff Garzik {
1759c6fd2807SJeff Garzik 	struct mv_port_priv *pp = qc->ap->private_data;
1760c6fd2807SJeff Garzik 	struct scatterlist *sg;
17613be6cbd7SJeff Garzik 	struct mv_sg *mv_sg, *last_sg = NULL;
1762ff2aeb1eSTejun Heo 	unsigned int si;
1763c6fd2807SJeff Garzik 
1764eb73d558SMark Lord 	mv_sg = pp->sg_tbl[qc->tag];
1765ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1766d88184fbSJeff Garzik 		dma_addr_t addr = sg_dma_address(sg);
1767d88184fbSJeff Garzik 		u32 sg_len = sg_dma_len(sg);
1768c6fd2807SJeff Garzik 
17694007b493SOlof Johansson 		while (sg_len) {
17704007b493SOlof Johansson 			u32 offset = addr & 0xffff;
17714007b493SOlof Johansson 			u32 len = sg_len;
17724007b493SOlof Johansson 
177332cd11a6SMark Lord 			if (offset + len > 0x10000)
17744007b493SOlof Johansson 				len = 0x10000 - offset;
17754007b493SOlof Johansson 
1776d88184fbSJeff Garzik 			mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1777d88184fbSJeff Garzik 			mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
17786c08772eSJeff Garzik 			mv_sg->flags_size = cpu_to_le32(len & 0xffff);
177932cd11a6SMark Lord 			mv_sg->reserved = 0;
1780c6fd2807SJeff Garzik 
17814007b493SOlof Johansson 			sg_len -= len;
17824007b493SOlof Johansson 			addr += len;
17834007b493SOlof Johansson 
17843be6cbd7SJeff Garzik 			last_sg = mv_sg;
1785d88184fbSJeff Garzik 			mv_sg++;
1786c6fd2807SJeff Garzik 		}
17874007b493SOlof Johansson 	}
17883be6cbd7SJeff Garzik 
17893be6cbd7SJeff Garzik 	if (likely(last_sg))
17903be6cbd7SJeff Garzik 		last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
179132cd11a6SMark Lord 	mb(); /* ensure data structure is visible to the chipset */
1792c6fd2807SJeff Garzik }
1793c6fd2807SJeff Garzik 
17945796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1795c6fd2807SJeff Garzik {
1796c6fd2807SJeff Garzik 	u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1797c6fd2807SJeff Garzik 		(last ? CRQB_CMD_LAST : 0);
1798c6fd2807SJeff Garzik 	*cmdw = cpu_to_le16(tmp);
1799c6fd2807SJeff Garzik }
1800c6fd2807SJeff Garzik 
1801c6fd2807SJeff Garzik /**
1802da14265eSMark Lord  *	mv_sff_irq_clear - Clear hardware interrupt after DMA.
1803da14265eSMark Lord  *	@ap: Port associated with this ATA transaction.
1804da14265eSMark Lord  *
1805da14265eSMark Lord  *	We need this only for ATAPI bmdma transactions,
1806da14265eSMark Lord  *	as otherwise we experience spurious interrupts
1807da14265eSMark Lord  *	after libata-sff handles the bmdma interrupts.
1808da14265eSMark Lord  */
1809da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap)
1810da14265eSMark Lord {
1811da14265eSMark Lord 	mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
1812da14265eSMark Lord }
1813da14265eSMark Lord 
1814da14265eSMark Lord /**
1815da14265eSMark Lord  *	mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
1816da14265eSMark Lord  *	@qc: queued command to check for chipset/DMA compatibility.
1817da14265eSMark Lord  *
1818da14265eSMark Lord  *	The bmdma engines cannot handle speculative data sizes
1819da14265eSMark Lord  *	(bytecount under/over flow).  So only allow DMA for
1820da14265eSMark Lord  *	data transfer commands with known data sizes.
1821da14265eSMark Lord  *
1822da14265eSMark Lord  *	LOCKING:
1823da14265eSMark Lord  *	Inherited from caller.
1824da14265eSMark Lord  */
1825da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
1826da14265eSMark Lord {
1827da14265eSMark Lord 	struct scsi_cmnd *scmd = qc->scsicmd;
1828da14265eSMark Lord 
1829da14265eSMark Lord 	if (scmd) {
1830da14265eSMark Lord 		switch (scmd->cmnd[0]) {
1831da14265eSMark Lord 		case READ_6:
1832da14265eSMark Lord 		case READ_10:
1833da14265eSMark Lord 		case READ_12:
1834da14265eSMark Lord 		case WRITE_6:
1835da14265eSMark Lord 		case WRITE_10:
1836da14265eSMark Lord 		case WRITE_12:
1837da14265eSMark Lord 		case GPCMD_READ_CD:
1838da14265eSMark Lord 		case GPCMD_SEND_DVD_STRUCTURE:
1839da14265eSMark Lord 		case GPCMD_SEND_CUE_SHEET:
1840da14265eSMark Lord 			return 0; /* DMA is safe */
1841da14265eSMark Lord 		}
1842da14265eSMark Lord 	}
1843da14265eSMark Lord 	return -EOPNOTSUPP; /* use PIO instead */
1844da14265eSMark Lord }
1845da14265eSMark Lord 
1846da14265eSMark Lord /**
1847da14265eSMark Lord  *	mv_bmdma_setup - Set up BMDMA transaction
1848da14265eSMark Lord  *	@qc: queued command to prepare DMA for.
1849da14265eSMark Lord  *
1850da14265eSMark Lord  *	LOCKING:
1851da14265eSMark Lord  *	Inherited from caller.
1852da14265eSMark Lord  */
1853da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc)
1854da14265eSMark Lord {
1855da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1856da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1857da14265eSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1858da14265eSMark Lord 
1859da14265eSMark Lord 	mv_fill_sg(qc);
1860da14265eSMark Lord 
1861da14265eSMark Lord 	/* clear all DMA cmd bits */
1862cae5a29dSMark Lord 	writel(0, port_mmio + BMDMA_CMD);
1863da14265eSMark Lord 
1864da14265eSMark Lord 	/* load PRD table addr. */
1865da14265eSMark Lord 	writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
1866cae5a29dSMark Lord 		port_mmio + BMDMA_PRD_HIGH);
1867da14265eSMark Lord 	writelfl(pp->sg_tbl_dma[qc->tag],
1868cae5a29dSMark Lord 		port_mmio + BMDMA_PRD_LOW);
1869da14265eSMark Lord 
1870da14265eSMark Lord 	/* issue r/w command */
1871da14265eSMark Lord 	ap->ops->sff_exec_command(ap, &qc->tf);
1872da14265eSMark Lord }
1873da14265eSMark Lord 
1874da14265eSMark Lord /**
1875da14265eSMark Lord  *	mv_bmdma_start - Start a BMDMA transaction
1876da14265eSMark Lord  *	@qc: queued command to start DMA on.
1877da14265eSMark Lord  *
1878da14265eSMark Lord  *	LOCKING:
1879da14265eSMark Lord  *	Inherited from caller.
1880da14265eSMark Lord  */
1881da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc)
1882da14265eSMark Lord {
1883da14265eSMark Lord 	struct ata_port *ap = qc->ap;
1884da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1885da14265eSMark Lord 	unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
1886da14265eSMark Lord 	u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
1887da14265eSMark Lord 
1888da14265eSMark Lord 	/* start host DMA transaction */
1889cae5a29dSMark Lord 	writelfl(cmd, port_mmio + BMDMA_CMD);
1890da14265eSMark Lord }
1891da14265eSMark Lord 
1892da14265eSMark Lord /**
1893da14265eSMark Lord  *	mv_bmdma_stop - Stop BMDMA transfer
1894da14265eSMark Lord  *	@qc: queued command to stop DMA on.
1895da14265eSMark Lord  *
1896da14265eSMark Lord  *	Clears the ATA_DMA_START flag in the bmdma control register
1897da14265eSMark Lord  *
1898da14265eSMark Lord  *	LOCKING:
1899da14265eSMark Lord  *	Inherited from caller.
1900da14265eSMark Lord  */
190144b73380SMark Lord static void mv_bmdma_stop_ap(struct ata_port *ap)
1902da14265eSMark Lord {
1903da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1904da14265eSMark Lord 	u32 cmd;
1905da14265eSMark Lord 
1906da14265eSMark Lord 	/* clear start/stop bit */
1907cae5a29dSMark Lord 	cmd = readl(port_mmio + BMDMA_CMD);
190844b73380SMark Lord 	if (cmd & ATA_DMA_START) {
1909da14265eSMark Lord 		cmd &= ~ATA_DMA_START;
1910cae5a29dSMark Lord 		writelfl(cmd, port_mmio + BMDMA_CMD);
1911da14265eSMark Lord 
1912da14265eSMark Lord 		/* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
1913da14265eSMark Lord 		ata_sff_dma_pause(ap);
1914da14265eSMark Lord 	}
191544b73380SMark Lord }
191644b73380SMark Lord 
191744b73380SMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc)
191844b73380SMark Lord {
191944b73380SMark Lord 	mv_bmdma_stop_ap(qc->ap);
192044b73380SMark Lord }
1921da14265eSMark Lord 
1922da14265eSMark Lord /**
1923da14265eSMark Lord  *	mv_bmdma_status - Read BMDMA status
1924da14265eSMark Lord  *	@ap: port for which to retrieve DMA status.
1925da14265eSMark Lord  *
1926da14265eSMark Lord  *	Read and return equivalent of the sff BMDMA status register.
1927da14265eSMark Lord  *
1928da14265eSMark Lord  *	LOCKING:
1929da14265eSMark Lord  *	Inherited from caller.
1930da14265eSMark Lord  */
1931da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap)
1932da14265eSMark Lord {
1933da14265eSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
1934da14265eSMark Lord 	u32 reg, status;
1935da14265eSMark Lord 
1936da14265eSMark Lord 	/*
1937da14265eSMark Lord 	 * Other bits are valid only if ATA_DMA_ACTIVE==0,
1938da14265eSMark Lord 	 * and the ATA_DMA_INTR bit doesn't exist.
1939da14265eSMark Lord 	 */
1940cae5a29dSMark Lord 	reg = readl(port_mmio + BMDMA_STATUS);
1941da14265eSMark Lord 	if (reg & ATA_DMA_ACTIVE)
1942da14265eSMark Lord 		status = ATA_DMA_ACTIVE;
194344b73380SMark Lord 	else if (reg & ATA_DMA_ERR)
1944da14265eSMark Lord 		status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
194544b73380SMark Lord 	else {
194644b73380SMark Lord 		/*
194744b73380SMark Lord 		 * Just because DMA_ACTIVE is 0 (DMA completed),
194844b73380SMark Lord 		 * this does _not_ mean the device is "done".
194944b73380SMark Lord 		 * So we should not yet be signalling ATA_DMA_INTR
195044b73380SMark Lord 		 * in some cases.  Eg. DSM/TRIM, and perhaps others.
195144b73380SMark Lord 		 */
195244b73380SMark Lord 		mv_bmdma_stop_ap(ap);
195344b73380SMark Lord 		if (ioread8(ap->ioaddr.altstatus_addr) & ATA_BUSY)
195444b73380SMark Lord 			status = 0;
195544b73380SMark Lord 		else
195644b73380SMark Lord 			status = ATA_DMA_INTR;
195744b73380SMark Lord 	}
1958da14265eSMark Lord 	return status;
1959da14265eSMark Lord }
1960da14265eSMark Lord 
1961299b3f8dSMark Lord static void mv_rw_multi_errata_sata24(struct ata_queued_cmd *qc)
1962299b3f8dSMark Lord {
1963299b3f8dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
1964299b3f8dSMark Lord 	/*
1965299b3f8dSMark Lord 	 * Workaround for 88SX60x1 FEr SATA#24.
1966299b3f8dSMark Lord 	 *
1967299b3f8dSMark Lord 	 * Chip may corrupt WRITEs if multi_count >= 4kB.
1968299b3f8dSMark Lord 	 * Note that READs are unaffected.
1969299b3f8dSMark Lord 	 *
1970299b3f8dSMark Lord 	 * It's not clear if this errata really means "4K bytes",
1971299b3f8dSMark Lord 	 * or if it always happens for multi_count > 7
1972299b3f8dSMark Lord 	 * regardless of device sector_size.
1973299b3f8dSMark Lord 	 *
1974299b3f8dSMark Lord 	 * So, for safety, any write with multi_count > 7
1975299b3f8dSMark Lord 	 * gets converted here into a regular PIO write instead:
1976299b3f8dSMark Lord 	 */
1977299b3f8dSMark Lord 	if ((tf->flags & ATA_TFLAG_WRITE) && is_multi_taskfile(tf)) {
1978299b3f8dSMark Lord 		if (qc->dev->multi_count > 7) {
1979299b3f8dSMark Lord 			switch (tf->command) {
1980299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI:
1981299b3f8dSMark Lord 				tf->command = ATA_CMD_PIO_WRITE;
1982299b3f8dSMark Lord 				break;
1983299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI_FUA_EXT:
1984299b3f8dSMark Lord 				tf->flags &= ~ATA_TFLAG_FUA; /* ugh */
1985299b3f8dSMark Lord 				/* fall through */
1986299b3f8dSMark Lord 			case ATA_CMD_WRITE_MULTI_EXT:
1987299b3f8dSMark Lord 				tf->command = ATA_CMD_PIO_WRITE_EXT;
1988299b3f8dSMark Lord 				break;
1989299b3f8dSMark Lord 			}
1990299b3f8dSMark Lord 		}
1991299b3f8dSMark Lord 	}
1992299b3f8dSMark Lord }
1993299b3f8dSMark Lord 
1994da14265eSMark Lord /**
1995c6fd2807SJeff Garzik  *      mv_qc_prep - Host specific command preparation.
1996c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1997c6fd2807SJeff Garzik  *
1998c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1999c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
2000c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
2001c6fd2807SJeff Garzik  *      the SG load routine.
2002c6fd2807SJeff Garzik  *
2003c6fd2807SJeff Garzik  *      LOCKING:
2004c6fd2807SJeff Garzik  *      Inherited from caller.
2005c6fd2807SJeff Garzik  */
2006c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc)
2007c6fd2807SJeff Garzik {
2008c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
2009c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2010c6fd2807SJeff Garzik 	__le16 *cw;
20118d2b450dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
2012c6fd2807SJeff Garzik 	u16 flags = 0;
2013c6fd2807SJeff Garzik 	unsigned in_index;
2014c6fd2807SJeff Garzik 
2015299b3f8dSMark Lord 	switch (tf->protocol) {
2016299b3f8dSMark Lord 	case ATA_PROT_DMA:
201744b73380SMark Lord 		if (tf->command == ATA_CMD_DSM)
201844b73380SMark Lord 			return;
201944b73380SMark Lord 		/* fall-thru */
2020299b3f8dSMark Lord 	case ATA_PROT_NCQ:
2021299b3f8dSMark Lord 		break;	/* continue below */
2022299b3f8dSMark Lord 	case ATA_PROT_PIO:
2023299b3f8dSMark Lord 		mv_rw_multi_errata_sata24(qc);
2024c6fd2807SJeff Garzik 		return;
2025299b3f8dSMark Lord 	default:
2026299b3f8dSMark Lord 		return;
2027299b3f8dSMark Lord 	}
2028c6fd2807SJeff Garzik 
2029c6fd2807SJeff Garzik 	/* Fill in command request block
2030c6fd2807SJeff Garzik 	 */
20318d2b450dSMark Lord 	if (!(tf->flags & ATA_TFLAG_WRITE))
2032c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
2033c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
2034c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
2035e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2036c6fd2807SJeff Garzik 
2037bdd4dddeSJeff Garzik 	/* get current queue index from software */
2038fcfb1f77SMark Lord 	in_index = pp->req_idx;
2039c6fd2807SJeff Garzik 
2040c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr =
2041eb73d558SMark Lord 		cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
2042c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr_hi =
2043eb73d558SMark Lord 		cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
2044c6fd2807SJeff Garzik 	pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
2045c6fd2807SJeff Garzik 
2046c6fd2807SJeff Garzik 	cw = &pp->crqb[in_index].ata_cmd[0];
2047c6fd2807SJeff Garzik 
204825985edcSLucas De Marchi 	/* Sadly, the CRQB cannot accommodate all registers--there are
2049c6fd2807SJeff Garzik 	 * only 11 bytes...so we must pick and choose required
2050c6fd2807SJeff Garzik 	 * registers based on the command.  So, we drop feature and
2051c6fd2807SJeff Garzik 	 * hob_feature for [RW] DMA commands, but they are needed for
2052cd12e1f7SMark Lord 	 * NCQ.  NCQ will drop hob_nsect, which is not needed there
2053cd12e1f7SMark Lord 	 * (nsect is used only for the tag; feat/hob_feat hold true nsect).
2054c6fd2807SJeff Garzik 	 */
2055c6fd2807SJeff Garzik 	switch (tf->command) {
2056c6fd2807SJeff Garzik 	case ATA_CMD_READ:
2057c6fd2807SJeff Garzik 	case ATA_CMD_READ_EXT:
2058c6fd2807SJeff Garzik 	case ATA_CMD_WRITE:
2059c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_EXT:
2060c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_FUA_EXT:
2061c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
2062c6fd2807SJeff Garzik 		break;
2063c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_READ:
2064c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_WRITE:
2065c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
2066c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
2067c6fd2807SJeff Garzik 		break;
2068c6fd2807SJeff Garzik 	default:
2069c6fd2807SJeff Garzik 		/* The only other commands EDMA supports in non-queued and
2070c6fd2807SJeff Garzik 		 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
2071c6fd2807SJeff Garzik 		 * of which are defined/used by Linux.  If we get here, this
2072c6fd2807SJeff Garzik 		 * driver needs work.
2073c6fd2807SJeff Garzik 		 *
2074c6fd2807SJeff Garzik 		 * FIXME: modify libata to give qc_prep a return value and
2075c6fd2807SJeff Garzik 		 * return error here.
2076c6fd2807SJeff Garzik 		 */
2077c6fd2807SJeff Garzik 		BUG_ON(tf->command);
2078c6fd2807SJeff Garzik 		break;
2079c6fd2807SJeff Garzik 	}
2080c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
2081c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
2082c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
2083c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
2084c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
2085c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
2086c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
2087c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
2088c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);	/* last */
2089c6fd2807SJeff Garzik 
2090c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2091c6fd2807SJeff Garzik 		return;
2092c6fd2807SJeff Garzik 	mv_fill_sg(qc);
2093c6fd2807SJeff Garzik }
2094c6fd2807SJeff Garzik 
2095c6fd2807SJeff Garzik /**
2096c6fd2807SJeff Garzik  *      mv_qc_prep_iie - Host specific command preparation.
2097c6fd2807SJeff Garzik  *      @qc: queued command to prepare
2098c6fd2807SJeff Garzik  *
2099c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2100c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
2101c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
2102c6fd2807SJeff Garzik  *      the SG load routine.
2103c6fd2807SJeff Garzik  *
2104c6fd2807SJeff Garzik  *      LOCKING:
2105c6fd2807SJeff Garzik  *      Inherited from caller.
2106c6fd2807SJeff Garzik  */
2107c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
2108c6fd2807SJeff Garzik {
2109c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
2110c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2111c6fd2807SJeff Garzik 	struct mv_crqb_iie *crqb;
21128d2b450dSMark Lord 	struct ata_taskfile *tf = &qc->tf;
2113c6fd2807SJeff Garzik 	unsigned in_index;
2114c6fd2807SJeff Garzik 	u32 flags = 0;
2115c6fd2807SJeff Garzik 
21168d2b450dSMark Lord 	if ((tf->protocol != ATA_PROT_DMA) &&
21178d2b450dSMark Lord 	    (tf->protocol != ATA_PROT_NCQ))
2118c6fd2807SJeff Garzik 		return;
211944b73380SMark Lord 	if (tf->command == ATA_CMD_DSM)
212044b73380SMark Lord 		return;  /* use bmdma for this */
2121c6fd2807SJeff Garzik 
2122e12bef50SMark Lord 	/* Fill in Gen IIE command request block */
21238d2b450dSMark Lord 	if (!(tf->flags & ATA_TFLAG_WRITE))
2124c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
2125c6fd2807SJeff Garzik 
2126c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
2127c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
21288c0aeb4aSMark Lord 	flags |= qc->tag << CRQB_HOSTQ_SHIFT;
2129e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2130c6fd2807SJeff Garzik 
2131bdd4dddeSJeff Garzik 	/* get current queue index from software */
2132fcfb1f77SMark Lord 	in_index = pp->req_idx;
2133c6fd2807SJeff Garzik 
2134c6fd2807SJeff Garzik 	crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
2135eb73d558SMark Lord 	crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
2136eb73d558SMark Lord 	crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
2137c6fd2807SJeff Garzik 	crqb->flags = cpu_to_le32(flags);
2138c6fd2807SJeff Garzik 
2139c6fd2807SJeff Garzik 	crqb->ata_cmd[0] = cpu_to_le32(
2140c6fd2807SJeff Garzik 			(tf->command << 16) |
2141c6fd2807SJeff Garzik 			(tf->feature << 24)
2142c6fd2807SJeff Garzik 		);
2143c6fd2807SJeff Garzik 	crqb->ata_cmd[1] = cpu_to_le32(
2144c6fd2807SJeff Garzik 			(tf->lbal << 0) |
2145c6fd2807SJeff Garzik 			(tf->lbam << 8) |
2146c6fd2807SJeff Garzik 			(tf->lbah << 16) |
2147c6fd2807SJeff Garzik 			(tf->device << 24)
2148c6fd2807SJeff Garzik 		);
2149c6fd2807SJeff Garzik 	crqb->ata_cmd[2] = cpu_to_le32(
2150c6fd2807SJeff Garzik 			(tf->hob_lbal << 0) |
2151c6fd2807SJeff Garzik 			(tf->hob_lbam << 8) |
2152c6fd2807SJeff Garzik 			(tf->hob_lbah << 16) |
2153c6fd2807SJeff Garzik 			(tf->hob_feature << 24)
2154c6fd2807SJeff Garzik 		);
2155c6fd2807SJeff Garzik 	crqb->ata_cmd[3] = cpu_to_le32(
2156c6fd2807SJeff Garzik 			(tf->nsect << 0) |
2157c6fd2807SJeff Garzik 			(tf->hob_nsect << 8)
2158c6fd2807SJeff Garzik 		);
2159c6fd2807SJeff Garzik 
2160c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
2161c6fd2807SJeff Garzik 		return;
2162c6fd2807SJeff Garzik 	mv_fill_sg(qc);
2163c6fd2807SJeff Garzik }
2164c6fd2807SJeff Garzik 
2165c6fd2807SJeff Garzik /**
2166d16ab3f6SMark Lord  *	mv_sff_check_status - fetch device status, if valid
2167d16ab3f6SMark Lord  *	@ap: ATA port to fetch status from
2168d16ab3f6SMark Lord  *
2169d16ab3f6SMark Lord  *	When using command issue via mv_qc_issue_fis(),
2170d16ab3f6SMark Lord  *	the initial ATA_BUSY state does not show up in the
2171d16ab3f6SMark Lord  *	ATA status (shadow) register.  This can confuse libata!
2172d16ab3f6SMark Lord  *
2173d16ab3f6SMark Lord  *	So we have a hook here to fake ATA_BUSY for that situation,
2174d16ab3f6SMark Lord  *	until the first time a BUSY, DRQ, or ERR bit is seen.
2175d16ab3f6SMark Lord  *
2176d16ab3f6SMark Lord  *	The rest of the time, it simply returns the ATA status register.
2177d16ab3f6SMark Lord  */
2178d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap)
2179d16ab3f6SMark Lord {
2180d16ab3f6SMark Lord 	u8 stat = ioread8(ap->ioaddr.status_addr);
2181d16ab3f6SMark Lord 	struct mv_port_priv *pp = ap->private_data;
2182d16ab3f6SMark Lord 
2183d16ab3f6SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
2184d16ab3f6SMark Lord 		if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
2185d16ab3f6SMark Lord 			pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
2186d16ab3f6SMark Lord 		else
2187d16ab3f6SMark Lord 			stat = ATA_BUSY;
2188d16ab3f6SMark Lord 	}
2189d16ab3f6SMark Lord 	return stat;
2190d16ab3f6SMark Lord }
2191d16ab3f6SMark Lord 
2192d16ab3f6SMark Lord /**
219370f8b79cSMark Lord  *	mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
219470f8b79cSMark Lord  *	@fis: fis to be sent
219570f8b79cSMark Lord  *	@nwords: number of 32-bit words in the fis
219670f8b79cSMark Lord  */
219770f8b79cSMark Lord static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
219870f8b79cSMark Lord {
219970f8b79cSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
220070f8b79cSMark Lord 	u32 ifctl, old_ifctl, ifstat;
220170f8b79cSMark Lord 	int i, timeout = 200, final_word = nwords - 1;
220270f8b79cSMark Lord 
220370f8b79cSMark Lord 	/* Initiate FIS transmission mode */
2204cae5a29dSMark Lord 	old_ifctl = readl(port_mmio + SATA_IFCTL);
220570f8b79cSMark Lord 	ifctl = 0x100 | (old_ifctl & 0xf);
2206cae5a29dSMark Lord 	writelfl(ifctl, port_mmio + SATA_IFCTL);
220770f8b79cSMark Lord 
220870f8b79cSMark Lord 	/* Send all words of the FIS except for the final word */
220970f8b79cSMark Lord 	for (i = 0; i < final_word; ++i)
2210cae5a29dSMark Lord 		writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS);
221170f8b79cSMark Lord 
221270f8b79cSMark Lord 	/* Flag end-of-transmission, and then send the final word */
2213cae5a29dSMark Lord 	writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL);
2214cae5a29dSMark Lord 	writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS);
221570f8b79cSMark Lord 
221670f8b79cSMark Lord 	/*
221770f8b79cSMark Lord 	 * Wait for FIS transmission to complete.
221870f8b79cSMark Lord 	 * This typically takes just a single iteration.
221970f8b79cSMark Lord 	 */
222070f8b79cSMark Lord 	do {
2221cae5a29dSMark Lord 		ifstat = readl(port_mmio + SATA_IFSTAT);
222270f8b79cSMark Lord 	} while (!(ifstat & 0x1000) && --timeout);
222370f8b79cSMark Lord 
222470f8b79cSMark Lord 	/* Restore original port configuration */
2225cae5a29dSMark Lord 	writelfl(old_ifctl, port_mmio + SATA_IFCTL);
222670f8b79cSMark Lord 
222770f8b79cSMark Lord 	/* See if it worked */
222870f8b79cSMark Lord 	if ((ifstat & 0x3000) != 0x1000) {
2229a9a79dfeSJoe Perches 		ata_port_warn(ap, "%s transmission error, ifstat=%08x\n",
223070f8b79cSMark Lord 			      __func__, ifstat);
223170f8b79cSMark Lord 		return AC_ERR_OTHER;
223270f8b79cSMark Lord 	}
223370f8b79cSMark Lord 	return 0;
223470f8b79cSMark Lord }
223570f8b79cSMark Lord 
223670f8b79cSMark Lord /**
223770f8b79cSMark Lord  *	mv_qc_issue_fis - Issue a command directly as a FIS
223870f8b79cSMark Lord  *	@qc: queued command to start
223970f8b79cSMark Lord  *
224070f8b79cSMark Lord  *	Note that the ATA shadow registers are not updated
224170f8b79cSMark Lord  *	after command issue, so the device will appear "READY"
224270f8b79cSMark Lord  *	if polled, even while it is BUSY processing the command.
224370f8b79cSMark Lord  *
224470f8b79cSMark Lord  *	So we use a status hook to fake ATA_BUSY until the drive changes state.
224570f8b79cSMark Lord  *
224670f8b79cSMark Lord  *	Note: we don't get updated shadow regs on *completion*
224770f8b79cSMark Lord  *	of non-data commands. So avoid sending them via this function,
224870f8b79cSMark Lord  *	as they will appear to have completed immediately.
224970f8b79cSMark Lord  *
225070f8b79cSMark Lord  *	GEN_IIE has special registers that we could get the result tf from,
225170f8b79cSMark Lord  *	but earlier chipsets do not.  For now, we ignore those registers.
225270f8b79cSMark Lord  */
225370f8b79cSMark Lord static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
225470f8b79cSMark Lord {
225570f8b79cSMark Lord 	struct ata_port *ap = qc->ap;
225670f8b79cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
225770f8b79cSMark Lord 	struct ata_link *link = qc->dev->link;
225870f8b79cSMark Lord 	u32 fis[5];
225970f8b79cSMark Lord 	int err = 0;
226070f8b79cSMark Lord 
226170f8b79cSMark Lord 	ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
22624c4a90fdSThiago Farina 	err = mv_send_fis(ap, fis, ARRAY_SIZE(fis));
226370f8b79cSMark Lord 	if (err)
226470f8b79cSMark Lord 		return err;
226570f8b79cSMark Lord 
226670f8b79cSMark Lord 	switch (qc->tf.protocol) {
226770f8b79cSMark Lord 	case ATAPI_PROT_PIO:
226870f8b79cSMark Lord 		pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
226970f8b79cSMark Lord 		/* fall through */
227070f8b79cSMark Lord 	case ATAPI_PROT_NODATA:
227170f8b79cSMark Lord 		ap->hsm_task_state = HSM_ST_FIRST;
227270f8b79cSMark Lord 		break;
227370f8b79cSMark Lord 	case ATA_PROT_PIO:
227470f8b79cSMark Lord 		pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
227570f8b79cSMark Lord 		if (qc->tf.flags & ATA_TFLAG_WRITE)
227670f8b79cSMark Lord 			ap->hsm_task_state = HSM_ST_FIRST;
227770f8b79cSMark Lord 		else
227870f8b79cSMark Lord 			ap->hsm_task_state = HSM_ST;
227970f8b79cSMark Lord 		break;
228070f8b79cSMark Lord 	default:
228170f8b79cSMark Lord 		ap->hsm_task_state = HSM_ST_LAST;
228270f8b79cSMark Lord 		break;
228370f8b79cSMark Lord 	}
228470f8b79cSMark Lord 
228570f8b79cSMark Lord 	if (qc->tf.flags & ATA_TFLAG_POLLING)
2286ea3c6450SGwendal Grignou 		ata_sff_queue_pio_task(link, 0);
228770f8b79cSMark Lord 	return 0;
228870f8b79cSMark Lord }
228970f8b79cSMark Lord 
229070f8b79cSMark Lord /**
2291c6fd2807SJeff Garzik  *      mv_qc_issue - Initiate a command to the host
2292c6fd2807SJeff Garzik  *      @qc: queued command to start
2293c6fd2807SJeff Garzik  *
2294c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
2295c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it sanity checks our local
2296c6fd2807SJeff Garzik  *      caches of the request producer/consumer indices then enables
2297c6fd2807SJeff Garzik  *      DMA and bumps the request producer index.
2298c6fd2807SJeff Garzik  *
2299c6fd2807SJeff Garzik  *      LOCKING:
2300c6fd2807SJeff Garzik  *      Inherited from caller.
2301c6fd2807SJeff Garzik  */
2302c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
2303c6fd2807SJeff Garzik {
2304f48765ccSMark Lord 	static int limit_warnings = 10;
2305c5d3e45aSJeff Garzik 	struct ata_port *ap = qc->ap;
2306c5d3e45aSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2307c5d3e45aSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2308bdd4dddeSJeff Garzik 	u32 in_index;
230942ed893dSMark Lord 	unsigned int port_irqs;
2310c6fd2807SJeff Garzik 
2311d16ab3f6SMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
2312d16ab3f6SMark Lord 
2313f48765ccSMark Lord 	switch (qc->tf.protocol) {
2314f48765ccSMark Lord 	case ATA_PROT_DMA:
231544b73380SMark Lord 		if (qc->tf.command == ATA_CMD_DSM) {
231644b73380SMark Lord 			if (!ap->ops->bmdma_setup)  /* no bmdma on GEN_I */
231744b73380SMark Lord 				return AC_ERR_OTHER;
231844b73380SMark Lord 			break;  /* use bmdma for this */
231944b73380SMark Lord 		}
232044b73380SMark Lord 		/* fall thru */
2321f48765ccSMark Lord 	case ATA_PROT_NCQ:
2322f48765ccSMark Lord 		mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
2323f48765ccSMark Lord 		pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2324f48765ccSMark Lord 		in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
2325f48765ccSMark Lord 
2326f48765ccSMark Lord 		/* Write the request in pointer to kick the EDMA to life */
2327f48765ccSMark Lord 		writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
2328cae5a29dSMark Lord 					port_mmio + EDMA_REQ_Q_IN_PTR);
2329f48765ccSMark Lord 		return 0;
2330f48765ccSMark Lord 
2331f48765ccSMark Lord 	case ATA_PROT_PIO:
2332c6112bd8SMark Lord 		/*
2333c6112bd8SMark Lord 		 * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
2334c6112bd8SMark Lord 		 *
2335c6112bd8SMark Lord 		 * Someday, we might implement special polling workarounds
2336c6112bd8SMark Lord 		 * for these, but it all seems rather unnecessary since we
2337c6112bd8SMark Lord 		 * normally use only DMA for commands which transfer more
2338c6112bd8SMark Lord 		 * than a single block of data.
2339c6112bd8SMark Lord 		 *
2340c6112bd8SMark Lord 		 * Much of the time, this could just work regardless.
2341c6112bd8SMark Lord 		 * So for now, just log the incident, and allow the attempt.
2342c6112bd8SMark Lord 		 */
2343c7843e8fSMark Lord 		if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
2344c6112bd8SMark Lord 			--limit_warnings;
2345a9a79dfeSJoe Perches 			ata_link_warn(qc->dev->link, DRV_NAME
2346c6112bd8SMark Lord 				      ": attempting PIO w/multiple DRQ: "
2347c6112bd8SMark Lord 				      "this may fail due to h/w errata\n");
2348c6112bd8SMark Lord 		}
2349f48765ccSMark Lord 		/* drop through */
235042ed893dSMark Lord 	case ATA_PROT_NODATA:
2351f48765ccSMark Lord 	case ATAPI_PROT_PIO:
235242ed893dSMark Lord 	case ATAPI_PROT_NODATA:
235342ed893dSMark Lord 		if (ap->flags & ATA_FLAG_PIO_POLLING)
235442ed893dSMark Lord 			qc->tf.flags |= ATA_TFLAG_POLLING;
235542ed893dSMark Lord 		break;
235642ed893dSMark Lord 	}
235742ed893dSMark Lord 
235842ed893dSMark Lord 	if (qc->tf.flags & ATA_TFLAG_POLLING)
235942ed893dSMark Lord 		port_irqs = ERR_IRQ;	/* mask device interrupt when polling */
236042ed893dSMark Lord 	else
236142ed893dSMark Lord 		port_irqs = ERR_IRQ | DONE_IRQ;	/* unmask all interrupts */
236242ed893dSMark Lord 
236317c5aab5SMark Lord 	/*
236417c5aab5SMark Lord 	 * We're about to send a non-EDMA capable command to the
2365c6fd2807SJeff Garzik 	 * port.  Turn off EDMA so there won't be problems accessing
2366c6fd2807SJeff Garzik 	 * shadow block, etc registers.
2367c6fd2807SJeff Garzik 	 */
2368b562468cSMark Lord 	mv_stop_edma(ap);
2369f48765ccSMark Lord 	mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
2370e49856d8SMark Lord 	mv_pmp_select(ap, qc->dev->link->pmp);
237170f8b79cSMark Lord 
237270f8b79cSMark Lord 	if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
237370f8b79cSMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
237470f8b79cSMark Lord 		/*
237570f8b79cSMark Lord 		 * Workaround for 88SX60x1 FEr SATA#25 (part 2).
237670f8b79cSMark Lord 		 *
237770f8b79cSMark Lord 		 * After any NCQ error, the READ_LOG_EXT command
237870f8b79cSMark Lord 		 * from libata-eh *must* use mv_qc_issue_fis().
237970f8b79cSMark Lord 		 * Otherwise it might fail, due to chip errata.
238070f8b79cSMark Lord 		 *
238170f8b79cSMark Lord 		 * Rather than special-case it, we'll just *always*
238270f8b79cSMark Lord 		 * use this method here for READ_LOG_EXT, making for
238370f8b79cSMark Lord 		 * easier testing.
238470f8b79cSMark Lord 		 */
238570f8b79cSMark Lord 		if (IS_GEN_II(hpriv))
238670f8b79cSMark Lord 			return mv_qc_issue_fis(qc);
238770f8b79cSMark Lord 	}
2388360ff783STejun Heo 	return ata_bmdma_qc_issue(qc);
2389c6fd2807SJeff Garzik }
2390c6fd2807SJeff Garzik 
23918f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
23928f767f8aSMark Lord {
23938f767f8aSMark Lord 	struct mv_port_priv *pp = ap->private_data;
23948f767f8aSMark Lord 	struct ata_queued_cmd *qc;
23958f767f8aSMark Lord 
23968f767f8aSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
23978f767f8aSMark Lord 		return NULL;
23988f767f8aSMark Lord 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
23993e4ec344STejun Heo 	if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING))
24008f767f8aSMark Lord 		return qc;
24013e4ec344STejun Heo 	return NULL;
24028f767f8aSMark Lord }
24038f767f8aSMark Lord 
240429d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap)
240529d187bbSMark Lord {
240629d187bbSMark Lord 	unsigned int pmp, pmp_map;
240729d187bbSMark Lord 	struct mv_port_priv *pp = ap->private_data;
240829d187bbSMark Lord 
240929d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
241029d187bbSMark Lord 		/*
241129d187bbSMark Lord 		 * Perform NCQ error analysis on failed PMPs
241229d187bbSMark Lord 		 * before we freeze the port entirely.
241329d187bbSMark Lord 		 *
241429d187bbSMark Lord 		 * The failed PMPs are marked earlier by mv_pmp_eh_prep().
241529d187bbSMark Lord 		 */
241629d187bbSMark Lord 		pmp_map = pp->delayed_eh_pmp_map;
241729d187bbSMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
241829d187bbSMark Lord 		for (pmp = 0; pmp_map != 0; pmp++) {
241929d187bbSMark Lord 			unsigned int this_pmp = (1 << pmp);
242029d187bbSMark Lord 			if (pmp_map & this_pmp) {
242129d187bbSMark Lord 				struct ata_link *link = &ap->pmp_link[pmp];
242229d187bbSMark Lord 				pmp_map &= ~this_pmp;
242329d187bbSMark Lord 				ata_eh_analyze_ncq_error(link);
242429d187bbSMark Lord 			}
242529d187bbSMark Lord 		}
242629d187bbSMark Lord 		ata_port_freeze(ap);
242729d187bbSMark Lord 	}
242829d187bbSMark Lord 	sata_pmp_error_handler(ap);
242929d187bbSMark Lord }
243029d187bbSMark Lord 
24314c299ca3SMark Lord static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
24324c299ca3SMark Lord {
24334c299ca3SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
24344c299ca3SMark Lord 
2435cae5a29dSMark Lord 	return readl(port_mmio + SATA_TESTCTL) >> 16;
24364c299ca3SMark Lord }
24374c299ca3SMark Lord 
24384c299ca3SMark Lord static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
24394c299ca3SMark Lord {
24404c299ca3SMark Lord 	struct ata_eh_info *ehi;
24414c299ca3SMark Lord 	unsigned int pmp;
24424c299ca3SMark Lord 
24434c299ca3SMark Lord 	/*
24444c299ca3SMark Lord 	 * Initialize EH info for PMPs which saw device errors
24454c299ca3SMark Lord 	 */
24464c299ca3SMark Lord 	ehi = &ap->link.eh_info;
24474c299ca3SMark Lord 	for (pmp = 0; pmp_map != 0; pmp++) {
24484c299ca3SMark Lord 		unsigned int this_pmp = (1 << pmp);
24494c299ca3SMark Lord 		if (pmp_map & this_pmp) {
24504c299ca3SMark Lord 			struct ata_link *link = &ap->pmp_link[pmp];
24514c299ca3SMark Lord 
24524c299ca3SMark Lord 			pmp_map &= ~this_pmp;
24534c299ca3SMark Lord 			ehi = &link->eh_info;
24544c299ca3SMark Lord 			ata_ehi_clear_desc(ehi);
24554c299ca3SMark Lord 			ata_ehi_push_desc(ehi, "dev err");
24564c299ca3SMark Lord 			ehi->err_mask |= AC_ERR_DEV;
24574c299ca3SMark Lord 			ehi->action |= ATA_EH_RESET;
24584c299ca3SMark Lord 			ata_link_abort(link);
24594c299ca3SMark Lord 		}
24604c299ca3SMark Lord 	}
24614c299ca3SMark Lord }
24624c299ca3SMark Lord 
246306aaca3fSMark Lord static int mv_req_q_empty(struct ata_port *ap)
246406aaca3fSMark Lord {
246506aaca3fSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
246606aaca3fSMark Lord 	u32 in_ptr, out_ptr;
246706aaca3fSMark Lord 
2468cae5a29dSMark Lord 	in_ptr  = (readl(port_mmio + EDMA_REQ_Q_IN_PTR)
246906aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2470cae5a29dSMark Lord 	out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR)
247106aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
247206aaca3fSMark Lord 	return (in_ptr == out_ptr);	/* 1 == queue_is_empty */
247306aaca3fSMark Lord }
247406aaca3fSMark Lord 
24754c299ca3SMark Lord static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
24764c299ca3SMark Lord {
24774c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
24784c299ca3SMark Lord 	int failed_links;
24794c299ca3SMark Lord 	unsigned int old_map, new_map;
24804c299ca3SMark Lord 
24814c299ca3SMark Lord 	/*
24824c299ca3SMark Lord 	 * Device error during FBS+NCQ operation:
24834c299ca3SMark Lord 	 *
24844c299ca3SMark Lord 	 * Set a port flag to prevent further I/O being enqueued.
24854c299ca3SMark Lord 	 * Leave the EDMA running to drain outstanding commands from this port.
24864c299ca3SMark Lord 	 * Perform the post-mortem/EH only when all responses are complete.
24874c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
24884c299ca3SMark Lord 	 */
24894c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
24904c299ca3SMark Lord 		pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
24914c299ca3SMark Lord 		pp->delayed_eh_pmp_map = 0;
24924c299ca3SMark Lord 	}
24934c299ca3SMark Lord 	old_map = pp->delayed_eh_pmp_map;
24944c299ca3SMark Lord 	new_map = old_map | mv_get_err_pmp_map(ap);
24954c299ca3SMark Lord 
24964c299ca3SMark Lord 	if (old_map != new_map) {
24974c299ca3SMark Lord 		pp->delayed_eh_pmp_map = new_map;
24984c299ca3SMark Lord 		mv_pmp_eh_prep(ap, new_map & ~old_map);
24994c299ca3SMark Lord 	}
2500c46938ccSMark Lord 	failed_links = hweight16(new_map);
25014c299ca3SMark Lord 
2502a9a79dfeSJoe Perches 	ata_port_info(ap,
2503a9a79dfeSJoe Perches 		      "%s: pmp_map=%04x qc_map=%04x failed_links=%d nr_active_links=%d\n",
25044c299ca3SMark Lord 		      __func__, pp->delayed_eh_pmp_map,
25054c299ca3SMark Lord 		      ap->qc_active, failed_links,
25064c299ca3SMark Lord 		      ap->nr_active_links);
25074c299ca3SMark Lord 
250806aaca3fSMark Lord 	if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
25094c299ca3SMark Lord 		mv_process_crpb_entries(ap, pp);
25104c299ca3SMark Lord 		mv_stop_edma(ap);
25114c299ca3SMark Lord 		mv_eh_freeze(ap);
2512a9a79dfeSJoe Perches 		ata_port_info(ap, "%s: done\n", __func__);
25134c299ca3SMark Lord 		return 1;	/* handled */
25144c299ca3SMark Lord 	}
2515a9a79dfeSJoe Perches 	ata_port_info(ap, "%s: waiting\n", __func__);
25164c299ca3SMark Lord 	return 1;	/* handled */
25174c299ca3SMark Lord }
25184c299ca3SMark Lord 
25194c299ca3SMark Lord static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
25204c299ca3SMark Lord {
25214c299ca3SMark Lord 	/*
25224c299ca3SMark Lord 	 * Possible future enhancement:
25234c299ca3SMark Lord 	 *
25244c299ca3SMark Lord 	 * FBS+non-NCQ operation is not yet implemented.
25254c299ca3SMark Lord 	 * See related notes in mv_edma_cfg().
25264c299ca3SMark Lord 	 *
25274c299ca3SMark Lord 	 * Device error during FBS+non-NCQ operation:
25284c299ca3SMark Lord 	 *
25294c299ca3SMark Lord 	 * We need to snapshot the shadow registers for each failed command.
25304c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
25314c299ca3SMark Lord 	 */
25324c299ca3SMark Lord 	return 0;	/* not handled */
25334c299ca3SMark Lord }
25344c299ca3SMark Lord 
25354c299ca3SMark Lord static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
25364c299ca3SMark Lord {
25374c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
25384c299ca3SMark Lord 
25394c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
25404c299ca3SMark Lord 		return 0;	/* EDMA was not active: not handled */
25414c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
25424c299ca3SMark Lord 		return 0;	/* FBS was not active: not handled */
25434c299ca3SMark Lord 
25444c299ca3SMark Lord 	if (!(edma_err_cause & EDMA_ERR_DEV))
25454c299ca3SMark Lord 		return 0;	/* non DEV error: not handled */
25464c299ca3SMark Lord 	edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
25474c299ca3SMark Lord 	if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
25484c299ca3SMark Lord 		return 0;	/* other problems: not handled */
25494c299ca3SMark Lord 
25504c299ca3SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
25514c299ca3SMark Lord 		/*
25524c299ca3SMark Lord 		 * EDMA should NOT have self-disabled for this case.
25534c299ca3SMark Lord 		 * If it did, then something is wrong elsewhere,
25544c299ca3SMark Lord 		 * and we cannot handle it here.
25554c299ca3SMark Lord 		 */
25564c299ca3SMark Lord 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2557a9a79dfeSJoe Perches 			ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
25584c299ca3SMark Lord 				      __func__, edma_err_cause, pp->pp_flags);
25594c299ca3SMark Lord 			return 0; /* not handled */
25604c299ca3SMark Lord 		}
25614c299ca3SMark Lord 		return mv_handle_fbs_ncq_dev_err(ap);
25624c299ca3SMark Lord 	} else {
25634c299ca3SMark Lord 		/*
25644c299ca3SMark Lord 		 * EDMA should have self-disabled for this case.
25654c299ca3SMark Lord 		 * If it did not, then something is wrong elsewhere,
25664c299ca3SMark Lord 		 * and we cannot handle it here.
25674c299ca3SMark Lord 		 */
25684c299ca3SMark Lord 		if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
2569a9a79dfeSJoe Perches 			ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
25704c299ca3SMark Lord 				      __func__, edma_err_cause, pp->pp_flags);
25714c299ca3SMark Lord 			return 0; /* not handled */
25724c299ca3SMark Lord 		}
25734c299ca3SMark Lord 		return mv_handle_fbs_non_ncq_dev_err(ap);
25744c299ca3SMark Lord 	}
25754c299ca3SMark Lord 	return 0;	/* not handled */
25764c299ca3SMark Lord }
25774c299ca3SMark Lord 
2578a9010329SMark Lord static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
25798f767f8aSMark Lord {
25808f767f8aSMark Lord 	struct ata_eh_info *ehi = &ap->link.eh_info;
2581a9010329SMark Lord 	char *when = "idle";
25828f767f8aSMark Lord 
25838f767f8aSMark Lord 	ata_ehi_clear_desc(ehi);
25843e4ec344STejun Heo 	if (edma_was_enabled) {
2585a9010329SMark Lord 		when = "EDMA enabled";
25868f767f8aSMark Lord 	} else {
25878f767f8aSMark Lord 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
25888f767f8aSMark Lord 		if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
2589a9010329SMark Lord 			when = "polling";
25908f767f8aSMark Lord 	}
2591a9010329SMark Lord 	ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
25928f767f8aSMark Lord 	ehi->err_mask |= AC_ERR_OTHER;
25938f767f8aSMark Lord 	ehi->action   |= ATA_EH_RESET;
25948f767f8aSMark Lord 	ata_port_freeze(ap);
25958f767f8aSMark Lord }
25968f767f8aSMark Lord 
2597c6fd2807SJeff Garzik /**
2598c6fd2807SJeff Garzik  *      mv_err_intr - Handle error interrupts on the port
2599c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
2600c6fd2807SJeff Garzik  *
26018d07379dSMark Lord  *      Most cases require a full reset of the chip's state machine,
26028d07379dSMark Lord  *      which also performs a COMRESET.
26038d07379dSMark Lord  *      Also, if the port disabled DMA, update our cached copy to match.
2604c6fd2807SJeff Garzik  *
2605c6fd2807SJeff Garzik  *      LOCKING:
2606c6fd2807SJeff Garzik  *      Inherited from caller.
2607c6fd2807SJeff Garzik  */
260837b9046aSMark Lord static void mv_err_intr(struct ata_port *ap)
2609c6fd2807SJeff Garzik {
2610c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2611bdd4dddeSJeff Garzik 	u32 edma_err_cause, eh_freeze_mask, serr = 0;
2612e4006077SMark Lord 	u32 fis_cause = 0;
2613bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
2614bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2615bdd4dddeSJeff Garzik 	unsigned int action = 0, err_mask = 0;
26169af5c9c9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
261737b9046aSMark Lord 	struct ata_queued_cmd *qc;
261837b9046aSMark Lord 	int abort = 0;
2619c6fd2807SJeff Garzik 
26208d07379dSMark Lord 	/*
262137b9046aSMark Lord 	 * Read and clear the SError and err_cause bits.
2622e4006077SMark Lord 	 * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
2623e4006077SMark Lord 	 * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
2624bdd4dddeSJeff Garzik 	 */
262537b9046aSMark Lord 	sata_scr_read(&ap->link, SCR_ERROR, &serr);
262637b9046aSMark Lord 	sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
262737b9046aSMark Lord 
2628cae5a29dSMark Lord 	edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE);
2629e4006077SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2630cae5a29dSMark Lord 		fis_cause = readl(port_mmio + FIS_IRQ_CAUSE);
2631cae5a29dSMark Lord 		writelfl(~fis_cause, port_mmio + FIS_IRQ_CAUSE);
2632e4006077SMark Lord 	}
2633cae5a29dSMark Lord 	writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE);
2634bdd4dddeSJeff Garzik 
26354c299ca3SMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
26364c299ca3SMark Lord 		/*
26374c299ca3SMark Lord 		 * Device errors during FIS-based switching operation
26384c299ca3SMark Lord 		 * require special handling.
26394c299ca3SMark Lord 		 */
26404c299ca3SMark Lord 		if (mv_handle_dev_err(ap, edma_err_cause))
26414c299ca3SMark Lord 			return;
26424c299ca3SMark Lord 	}
26434c299ca3SMark Lord 
264437b9046aSMark Lord 	qc = mv_get_active_qc(ap);
264537b9046aSMark Lord 	ata_ehi_clear_desc(ehi);
264637b9046aSMark Lord 	ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
264737b9046aSMark Lord 			  edma_err_cause, pp->pp_flags);
2648e4006077SMark Lord 
2649c443c500SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2650e4006077SMark Lord 		ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
2651cae5a29dSMark Lord 		if (fis_cause & FIS_IRQ_CAUSE_AN) {
2652c443c500SMark Lord 			u32 ec = edma_err_cause &
2653c443c500SMark Lord 			       ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
2654c443c500SMark Lord 			sata_async_notification(ap);
2655c443c500SMark Lord 			if (!ec)
2656c443c500SMark Lord 				return; /* Just an AN; no need for the nukes */
2657c443c500SMark Lord 			ata_ehi_push_desc(ehi, "SDB notify");
2658c443c500SMark Lord 		}
2659c443c500SMark Lord 	}
2660bdd4dddeSJeff Garzik 	/*
2661352fab70SMark Lord 	 * All generations share these EDMA error cause bits:
2662bdd4dddeSJeff Garzik 	 */
266337b9046aSMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
2664bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_DEV;
266537b9046aSMark Lord 		action |= ATA_EH_RESET;
266637b9046aSMark Lord 		ata_ehi_push_desc(ehi, "dev error");
266737b9046aSMark Lord 	}
2668bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
26696c1153e0SJeff Garzik 			EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
2670bdd4dddeSJeff Garzik 			EDMA_ERR_INTRL_PAR)) {
2671bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
2672cf480626STejun Heo 		action |= ATA_EH_RESET;
2673b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "parity error");
2674bdd4dddeSJeff Garzik 	}
2675bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
2676bdd4dddeSJeff Garzik 		ata_ehi_hotplugged(ehi);
2677bdd4dddeSJeff Garzik 		ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
2678b64bbc39STejun Heo 			"dev disconnect" : "dev connect");
2679cf480626STejun Heo 		action |= ATA_EH_RESET;
2680bdd4dddeSJeff Garzik 	}
2681bdd4dddeSJeff Garzik 
2682352fab70SMark Lord 	/*
2683352fab70SMark Lord 	 * Gen-I has a different SELF_DIS bit,
2684352fab70SMark Lord 	 * different FREEZE bits, and no SERR bit:
2685352fab70SMark Lord 	 */
2686ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv)) {
2687bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE_5;
2688bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
2689c6fd2807SJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2690b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
2691c6fd2807SJeff Garzik 		}
2692bdd4dddeSJeff Garzik 	} else {
2693bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE;
2694bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2695bdd4dddeSJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2696b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
2697bdd4dddeSJeff Garzik 		}
2698bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SERR) {
26998d07379dSMark Lord 			ata_ehi_push_desc(ehi, "SError=%08x", serr);
27008d07379dSMark Lord 			err_mask |= AC_ERR_ATA_BUS;
2701cf480626STejun Heo 			action |= ATA_EH_RESET;
2702bdd4dddeSJeff Garzik 		}
2703bdd4dddeSJeff Garzik 	}
2704c6fd2807SJeff Garzik 
2705bdd4dddeSJeff Garzik 	if (!err_mask) {
2706bdd4dddeSJeff Garzik 		err_mask = AC_ERR_OTHER;
2707cf480626STejun Heo 		action |= ATA_EH_RESET;
2708bdd4dddeSJeff Garzik 	}
2709bdd4dddeSJeff Garzik 
2710bdd4dddeSJeff Garzik 	ehi->serror |= serr;
2711bdd4dddeSJeff Garzik 	ehi->action |= action;
2712bdd4dddeSJeff Garzik 
2713bdd4dddeSJeff Garzik 	if (qc)
2714bdd4dddeSJeff Garzik 		qc->err_mask |= err_mask;
2715bdd4dddeSJeff Garzik 	else
2716bdd4dddeSJeff Garzik 		ehi->err_mask |= err_mask;
2717bdd4dddeSJeff Garzik 
271837b9046aSMark Lord 	if (err_mask == AC_ERR_DEV) {
271937b9046aSMark Lord 		/*
272037b9046aSMark Lord 		 * Cannot do ata_port_freeze() here,
272137b9046aSMark Lord 		 * because it would kill PIO access,
272237b9046aSMark Lord 		 * which is needed for further diagnosis.
272337b9046aSMark Lord 		 */
272437b9046aSMark Lord 		mv_eh_freeze(ap);
272537b9046aSMark Lord 		abort = 1;
272637b9046aSMark Lord 	} else if (edma_err_cause & eh_freeze_mask) {
272737b9046aSMark Lord 		/*
272837b9046aSMark Lord 		 * Note to self: ata_port_freeze() calls ata_port_abort()
272937b9046aSMark Lord 		 */
2730bdd4dddeSJeff Garzik 		ata_port_freeze(ap);
273137b9046aSMark Lord 	} else {
273237b9046aSMark Lord 		abort = 1;
273337b9046aSMark Lord 	}
273437b9046aSMark Lord 
273537b9046aSMark Lord 	if (abort) {
273637b9046aSMark Lord 		if (qc)
273737b9046aSMark Lord 			ata_link_abort(qc->dev->link);
2738bdd4dddeSJeff Garzik 		else
2739bdd4dddeSJeff Garzik 			ata_port_abort(ap);
2740bdd4dddeSJeff Garzik 	}
274137b9046aSMark Lord }
2742bdd4dddeSJeff Garzik 
27431aadf5c3STejun Heo static bool mv_process_crpb_response(struct ata_port *ap,
2744fcfb1f77SMark Lord 		struct mv_crpb *response, unsigned int tag, int ncq_enabled)
2745fcfb1f77SMark Lord {
2746fcfb1f77SMark Lord 	u8 ata_status;
2747fcfb1f77SMark Lord 	u16 edma_status = le16_to_cpu(response->flags);
2748752e386cSTejun Heo 
2749fcfb1f77SMark Lord 	/*
2750fcfb1f77SMark Lord 	 * edma_status from a response queue entry:
2751cae5a29dSMark Lord 	 *   LSB is from EDMA_ERR_IRQ_CAUSE (non-NCQ only).
2752fcfb1f77SMark Lord 	 *   MSB is saved ATA status from command completion.
2753fcfb1f77SMark Lord 	 */
2754fcfb1f77SMark Lord 	if (!ncq_enabled) {
2755fcfb1f77SMark Lord 		u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
2756fcfb1f77SMark Lord 		if (err_cause) {
2757fcfb1f77SMark Lord 			/*
2758752e386cSTejun Heo 			 * Error will be seen/handled by
2759752e386cSTejun Heo 			 * mv_err_intr().  So do nothing at all here.
2760fcfb1f77SMark Lord 			 */
27611aadf5c3STejun Heo 			return false;
2762fcfb1f77SMark Lord 		}
2763fcfb1f77SMark Lord 	}
2764fcfb1f77SMark Lord 	ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
276537b9046aSMark Lord 	if (!ac_err_mask(ata_status))
27661aadf5c3STejun Heo 		return true;
276737b9046aSMark Lord 	/* else: leave it for mv_err_intr() */
27681aadf5c3STejun Heo 	return false;
2769fcfb1f77SMark Lord }
2770fcfb1f77SMark Lord 
2771fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
2772bdd4dddeSJeff Garzik {
2773bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2774bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2775fcfb1f77SMark Lord 	u32 in_index;
2776bdd4dddeSJeff Garzik 	bool work_done = false;
27771aadf5c3STejun Heo 	u32 done_mask = 0;
2778fcfb1f77SMark Lord 	int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
2779bdd4dddeSJeff Garzik 
2780fcfb1f77SMark Lord 	/* Get the hardware queue position index */
2781cae5a29dSMark Lord 	in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR)
2782bdd4dddeSJeff Garzik 			>> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2783bdd4dddeSJeff Garzik 
2784fcfb1f77SMark Lord 	/* Process new responses from since the last time we looked */
2785fcfb1f77SMark Lord 	while (in_index != pp->resp_idx) {
27866c1153e0SJeff Garzik 		unsigned int tag;
2787fcfb1f77SMark Lord 		struct mv_crpb *response = &pp->crpb[pp->resp_idx];
2788bdd4dddeSJeff Garzik 
2789fcfb1f77SMark Lord 		pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2790bdd4dddeSJeff Garzik 
2791fcfb1f77SMark Lord 		if (IS_GEN_I(hpriv)) {
2792fcfb1f77SMark Lord 			/* 50xx: no NCQ, only one command active at a time */
27939af5c9c9STejun Heo 			tag = ap->link.active_tag;
2794fcfb1f77SMark Lord 		} else {
2795fcfb1f77SMark Lord 			/* Gen II/IIE: get command tag from CRPB entry */
2796fcfb1f77SMark Lord 			tag = le16_to_cpu(response->id) & 0x1f;
2797bdd4dddeSJeff Garzik 		}
27981aadf5c3STejun Heo 		if (mv_process_crpb_response(ap, response, tag, ncq_enabled))
27991aadf5c3STejun Heo 			done_mask |= 1 << tag;
2800bdd4dddeSJeff Garzik 		work_done = true;
2801bdd4dddeSJeff Garzik 	}
2802bdd4dddeSJeff Garzik 
28031aadf5c3STejun Heo 	if (work_done) {
28041aadf5c3STejun Heo 		ata_qc_complete_multiple(ap, ap->qc_active ^ done_mask);
28051aadf5c3STejun Heo 
2806352fab70SMark Lord 		/* Update the software queue position index in hardware */
2807bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
2808fcfb1f77SMark Lord 			 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
2809cae5a29dSMark Lord 			 port_mmio + EDMA_RSP_Q_OUT_PTR);
2810c6fd2807SJeff Garzik 	}
28111aadf5c3STejun Heo }
2812c6fd2807SJeff Garzik 
2813a9010329SMark Lord static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2814a9010329SMark Lord {
2815a9010329SMark Lord 	struct mv_port_priv *pp;
2816a9010329SMark Lord 	int edma_was_enabled;
2817a9010329SMark Lord 
2818a9010329SMark Lord 	/*
2819a9010329SMark Lord 	 * Grab a snapshot of the EDMA_EN flag setting,
2820a9010329SMark Lord 	 * so that we have a consistent view for this port,
2821a9010329SMark Lord 	 * even if something we call of our routines changes it.
2822a9010329SMark Lord 	 */
2823a9010329SMark Lord 	pp = ap->private_data;
2824a9010329SMark Lord 	edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2825a9010329SMark Lord 	/*
2826a9010329SMark Lord 	 * Process completed CRPB response(s) before other events.
2827a9010329SMark Lord 	 */
2828a9010329SMark Lord 	if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2829a9010329SMark Lord 		mv_process_crpb_entries(ap, pp);
28304c299ca3SMark Lord 		if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
28314c299ca3SMark Lord 			mv_handle_fbs_ncq_dev_err(ap);
2832a9010329SMark Lord 	}
2833a9010329SMark Lord 	/*
2834a9010329SMark Lord 	 * Handle chip-reported errors, or continue on to handle PIO.
2835a9010329SMark Lord 	 */
2836a9010329SMark Lord 	if (unlikely(port_cause & ERR_IRQ)) {
2837a9010329SMark Lord 		mv_err_intr(ap);
2838a9010329SMark Lord 	} else if (!edma_was_enabled) {
2839a9010329SMark Lord 		struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2840a9010329SMark Lord 		if (qc)
2841c3b28894STejun Heo 			ata_bmdma_port_intr(ap, qc);
2842a9010329SMark Lord 		else
2843a9010329SMark Lord 			mv_unexpected_intr(ap, edma_was_enabled);
2844a9010329SMark Lord 	}
2845a9010329SMark Lord }
2846a9010329SMark Lord 
2847c6fd2807SJeff Garzik /**
2848c6fd2807SJeff Garzik  *      mv_host_intr - Handle all interrupts on the given host controller
2849cca3974eSJeff Garzik  *      @host: host specific structure
28507368f919SMark Lord  *      @main_irq_cause: Main interrupt cause register for the chip.
2851c6fd2807SJeff Garzik  *
2852c6fd2807SJeff Garzik  *      LOCKING:
2853c6fd2807SJeff Garzik  *      Inherited from caller.
2854c6fd2807SJeff Garzik  */
28557368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
2856c6fd2807SJeff Garzik {
2857f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2858eabd5eb1SMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
2859a3718c1fSMark Lord 	unsigned int handled = 0, port;
2860c6fd2807SJeff Garzik 
28612b748a0aSMark Lord 	/* If asserted, clear the "all ports" IRQ coalescing bit */
28622b748a0aSMark Lord 	if (main_irq_cause & ALL_PORTS_COAL_DONE)
2863cae5a29dSMark Lord 		writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
28642b748a0aSMark Lord 
2865a3718c1fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
2866cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[port];
2867eabd5eb1SMark Lord 		unsigned int p, shift, hardport, port_cause;
2868eabd5eb1SMark Lord 
2869a3718c1fSMark Lord 		MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2870a3718c1fSMark Lord 		/*
2871eabd5eb1SMark Lord 		 * Each hc within the host has its own hc_irq_cause register,
2872eabd5eb1SMark Lord 		 * where the interrupting ports bits get ack'd.
2873a3718c1fSMark Lord 		 */
2874eabd5eb1SMark Lord 		if (hardport == 0) {	/* first port on this hc ? */
2875eabd5eb1SMark Lord 			u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2876eabd5eb1SMark Lord 			u32 port_mask, ack_irqs;
2877eabd5eb1SMark Lord 			/*
2878eabd5eb1SMark Lord 			 * Skip this entire hc if nothing pending for any ports
2879eabd5eb1SMark Lord 			 */
2880eabd5eb1SMark Lord 			if (!hc_cause) {
2881eabd5eb1SMark Lord 				port += MV_PORTS_PER_HC - 1;
2882eabd5eb1SMark Lord 				continue;
2883eabd5eb1SMark Lord 			}
2884eabd5eb1SMark Lord 			/*
2885eabd5eb1SMark Lord 			 * We don't need/want to read the hc_irq_cause register,
2886eabd5eb1SMark Lord 			 * because doing so hurts performance, and
2887eabd5eb1SMark Lord 			 * main_irq_cause already gives us everything we need.
2888eabd5eb1SMark Lord 			 *
2889eabd5eb1SMark Lord 			 * But we do have to *write* to the hc_irq_cause to ack
2890eabd5eb1SMark Lord 			 * the ports that we are handling this time through.
2891eabd5eb1SMark Lord 			 *
2892eabd5eb1SMark Lord 			 * This requires that we create a bitmap for those
2893eabd5eb1SMark Lord 			 * ports which interrupted us, and use that bitmap
2894eabd5eb1SMark Lord 			 * to ack (only) those ports via hc_irq_cause.
2895eabd5eb1SMark Lord 			 */
2896eabd5eb1SMark Lord 			ack_irqs = 0;
28972b748a0aSMark Lord 			if (hc_cause & PORTS_0_3_COAL_DONE)
28982b748a0aSMark Lord 				ack_irqs = HC_COAL_IRQ;
2899eabd5eb1SMark Lord 			for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2900eabd5eb1SMark Lord 				if ((port + p) >= hpriv->n_ports)
2901eabd5eb1SMark Lord 					break;
2902eabd5eb1SMark Lord 				port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2903eabd5eb1SMark Lord 				if (hc_cause & port_mask)
2904eabd5eb1SMark Lord 					ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2905eabd5eb1SMark Lord 			}
2906a3718c1fSMark Lord 			hc_mmio = mv_hc_base_from_port(mmio, port);
2907cae5a29dSMark Lord 			writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE);
2908a3718c1fSMark Lord 			handled = 1;
2909a3718c1fSMark Lord 		}
2910a9010329SMark Lord 		/*
2911a9010329SMark Lord 		 * Handle interrupts signalled for this port:
2912a9010329SMark Lord 		 */
2913eabd5eb1SMark Lord 		port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
2914a9010329SMark Lord 		if (port_cause)
2915a9010329SMark Lord 			mv_port_intr(ap, port_cause);
2916eabd5eb1SMark Lord 	}
2917a3718c1fSMark Lord 	return handled;
2918c6fd2807SJeff Garzik }
2919c6fd2807SJeff Garzik 
2920a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
2921bdd4dddeSJeff Garzik {
292202a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2923bdd4dddeSJeff Garzik 	struct ata_port *ap;
2924bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
2925bdd4dddeSJeff Garzik 	struct ata_eh_info *ehi;
2926bdd4dddeSJeff Garzik 	unsigned int i, err_mask, printed = 0;
2927bdd4dddeSJeff Garzik 	u32 err_cause;
2928bdd4dddeSJeff Garzik 
2929cae5a29dSMark Lord 	err_cause = readl(mmio + hpriv->irq_cause_offset);
2930bdd4dddeSJeff Garzik 
2931a44fec1fSJoe Perches 	dev_err(host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n", err_cause);
2932bdd4dddeSJeff Garzik 
2933bdd4dddeSJeff Garzik 	DPRINTK("All regs @ PCI error\n");
2934bdd4dddeSJeff Garzik 	mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
2935bdd4dddeSJeff Garzik 
2936cae5a29dSMark Lord 	writelfl(0, mmio + hpriv->irq_cause_offset);
2937bdd4dddeSJeff Garzik 
2938bdd4dddeSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
2939bdd4dddeSJeff Garzik 		ap = host->ports[i];
2940936fd732STejun Heo 		if (!ata_link_offline(&ap->link)) {
29419af5c9c9STejun Heo 			ehi = &ap->link.eh_info;
2942bdd4dddeSJeff Garzik 			ata_ehi_clear_desc(ehi);
2943bdd4dddeSJeff Garzik 			if (!printed++)
2944bdd4dddeSJeff Garzik 				ata_ehi_push_desc(ehi,
2945bdd4dddeSJeff Garzik 					"PCI err cause 0x%08x", err_cause);
2946bdd4dddeSJeff Garzik 			err_mask = AC_ERR_HOST_BUS;
2947cf480626STejun Heo 			ehi->action = ATA_EH_RESET;
29489af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
2949bdd4dddeSJeff Garzik 			if (qc)
2950bdd4dddeSJeff Garzik 				qc->err_mask |= err_mask;
2951bdd4dddeSJeff Garzik 			else
2952bdd4dddeSJeff Garzik 				ehi->err_mask |= err_mask;
2953bdd4dddeSJeff Garzik 
2954bdd4dddeSJeff Garzik 			ata_port_freeze(ap);
2955bdd4dddeSJeff Garzik 		}
2956bdd4dddeSJeff Garzik 	}
2957a3718c1fSMark Lord 	return 1;	/* handled */
2958bdd4dddeSJeff Garzik }
2959bdd4dddeSJeff Garzik 
2960c6fd2807SJeff Garzik /**
2961c5d3e45aSJeff Garzik  *      mv_interrupt - Main interrupt event handler
2962c6fd2807SJeff Garzik  *      @irq: unused
2963c6fd2807SJeff Garzik  *      @dev_instance: private data; in this case the host structure
2964c6fd2807SJeff Garzik  *
2965c6fd2807SJeff Garzik  *      Read the read only register to determine if any host
2966c6fd2807SJeff Garzik  *      controllers have pending interrupts.  If so, call lower level
2967c6fd2807SJeff Garzik  *      routine to handle.  Also check for PCI errors which are only
2968c6fd2807SJeff Garzik  *      reported here.
2969c6fd2807SJeff Garzik  *
2970c6fd2807SJeff Garzik  *      LOCKING:
2971cca3974eSJeff Garzik  *      This routine holds the host lock while processing pending
2972c6fd2807SJeff Garzik  *      interrupts.
2973c6fd2807SJeff Garzik  */
29747d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
2975c6fd2807SJeff Garzik {
2976cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
2977f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2978a3718c1fSMark Lord 	unsigned int handled = 0;
29796d3c30efSMark Lord 	int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
298096e2c487SMark Lord 	u32 main_irq_cause, pending_irqs;
2981c6fd2807SJeff Garzik 
2982646a4da5SMark Lord 	spin_lock(&host->lock);
29836d3c30efSMark Lord 
29846d3c30efSMark Lord 	/* for MSI:  block new interrupts while in here */
29856d3c30efSMark Lord 	if (using_msi)
29862b748a0aSMark Lord 		mv_write_main_irq_mask(0, hpriv);
29876d3c30efSMark Lord 
29887368f919SMark Lord 	main_irq_cause = readl(hpriv->main_irq_cause_addr);
298996e2c487SMark Lord 	pending_irqs   = main_irq_cause & hpriv->main_irq_mask;
2990352fab70SMark Lord 	/*
2991352fab70SMark Lord 	 * Deal with cases where we either have nothing pending, or have read
2992352fab70SMark Lord 	 * a bogus register value which can indicate HW removal or PCI fault.
2993c6fd2807SJeff Garzik 	 */
2994a44253d2SMark Lord 	if (pending_irqs && main_irq_cause != 0xffffffffU) {
29951f398472SMark Lord 		if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
2996a3718c1fSMark Lord 			handled = mv_pci_error(host, hpriv->base);
2997a3718c1fSMark Lord 		else
2998a44253d2SMark Lord 			handled = mv_host_intr(host, pending_irqs);
2999bdd4dddeSJeff Garzik 	}
30006d3c30efSMark Lord 
30016d3c30efSMark Lord 	/* for MSI: unmask; interrupt cause bits will retrigger now */
30026d3c30efSMark Lord 	if (using_msi)
30032b748a0aSMark Lord 		mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
30046d3c30efSMark Lord 
30059d51af7bSMark Lord 	spin_unlock(&host->lock);
30069d51af7bSMark Lord 
3007c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
3008c6fd2807SJeff Garzik }
3009c6fd2807SJeff Garzik 
3010c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
3011c6fd2807SJeff Garzik {
3012c6fd2807SJeff Garzik 	unsigned int ofs;
3013c6fd2807SJeff Garzik 
3014c6fd2807SJeff Garzik 	switch (sc_reg_in) {
3015c6fd2807SJeff Garzik 	case SCR_STATUS:
3016c6fd2807SJeff Garzik 	case SCR_ERROR:
3017c6fd2807SJeff Garzik 	case SCR_CONTROL:
3018c6fd2807SJeff Garzik 		ofs = sc_reg_in * sizeof(u32);
3019c6fd2807SJeff Garzik 		break;
3020c6fd2807SJeff Garzik 	default:
3021c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
3022c6fd2807SJeff Garzik 		break;
3023c6fd2807SJeff Garzik 	}
3024c6fd2807SJeff Garzik 	return ofs;
3025c6fd2807SJeff Garzik }
3026c6fd2807SJeff Garzik 
302782ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
3028c6fd2807SJeff Garzik {
302982ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
3030f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
303182ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3032c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
3033c6fd2807SJeff Garzik 
3034da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
3035da3dbb17STejun Heo 		*val = readl(addr + ofs);
3036da3dbb17STejun Heo 		return 0;
3037da3dbb17STejun Heo 	} else
3038da3dbb17STejun Heo 		return -EINVAL;
3039c6fd2807SJeff Garzik }
3040c6fd2807SJeff Garzik 
304182ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
3042c6fd2807SJeff Garzik {
304382ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
3044f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
304582ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3046c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
3047c6fd2807SJeff Garzik 
3048da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
30490d5ff566STejun Heo 		writelfl(val, addr + ofs);
3050da3dbb17STejun Heo 		return 0;
3051da3dbb17STejun Heo 	} else
3052da3dbb17STejun Heo 		return -EINVAL;
3053c6fd2807SJeff Garzik }
3054c6fd2807SJeff Garzik 
30557bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
3056c6fd2807SJeff Garzik {
30577bb3c529SSaeed Bishara 	struct pci_dev *pdev = to_pci_dev(host->dev);
3058c6fd2807SJeff Garzik 	int early_5080;
3059c6fd2807SJeff Garzik 
306044c10138SAuke Kok 	early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
3061c6fd2807SJeff Garzik 
3062c6fd2807SJeff Garzik 	if (!early_5080) {
3063c6fd2807SJeff Garzik 		u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3064c6fd2807SJeff Garzik 		tmp |= (1 << 0);
3065c6fd2807SJeff Garzik 		writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3066c6fd2807SJeff Garzik 	}
3067c6fd2807SJeff Garzik 
30687bb3c529SSaeed Bishara 	mv_reset_pci_bus(host, mmio);
3069c6fd2807SJeff Garzik }
3070c6fd2807SJeff Garzik 
3071c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3072c6fd2807SJeff Garzik {
3073cae5a29dSMark Lord 	writel(0x0fcfffff, mmio + FLASH_CTL);
3074c6fd2807SJeff Garzik }
3075c6fd2807SJeff Garzik 
3076c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
3077c6fd2807SJeff Garzik 			   void __iomem *mmio)
3078c6fd2807SJeff Garzik {
3079c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
3080c6fd2807SJeff Garzik 	u32 tmp;
3081c6fd2807SJeff Garzik 
3082c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
3083c6fd2807SJeff Garzik 
3084c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0x1800;	/* bits 12:11 */
3085c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0xe0;	/* bits 7:5 */
3086c6fd2807SJeff Garzik }
3087c6fd2807SJeff Garzik 
3088c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3089c6fd2807SJeff Garzik {
3090c6fd2807SJeff Garzik 	u32 tmp;
3091c6fd2807SJeff Garzik 
3092cae5a29dSMark Lord 	writel(0, mmio + GPIO_PORT_CTL);
3093c6fd2807SJeff Garzik 
3094c6fd2807SJeff Garzik 	/* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
3095c6fd2807SJeff Garzik 
3096c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3097c6fd2807SJeff Garzik 	tmp |= ~(1 << 0);
3098c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3099c6fd2807SJeff Garzik }
3100c6fd2807SJeff Garzik 
3101c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3102c6fd2807SJeff Garzik 			   unsigned int port)
3103c6fd2807SJeff Garzik {
3104c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, port);
3105c6fd2807SJeff Garzik 	const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
3106c6fd2807SJeff Garzik 	u32 tmp;
3107c6fd2807SJeff Garzik 	int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
3108c6fd2807SJeff Garzik 
3109c6fd2807SJeff Garzik 	if (fix_apm_sq) {
3110cae5a29dSMark Lord 		tmp = readl(phy_mmio + MV5_LTMODE);
3111c6fd2807SJeff Garzik 		tmp |= (1 << 19);
3112cae5a29dSMark Lord 		writel(tmp, phy_mmio + MV5_LTMODE);
3113c6fd2807SJeff Garzik 
3114cae5a29dSMark Lord 		tmp = readl(phy_mmio + MV5_PHY_CTL);
3115c6fd2807SJeff Garzik 		tmp &= ~0x3;
3116c6fd2807SJeff Garzik 		tmp |= 0x1;
3117cae5a29dSMark Lord 		writel(tmp, phy_mmio + MV5_PHY_CTL);
3118c6fd2807SJeff Garzik 	}
3119c6fd2807SJeff Garzik 
3120c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
3121c6fd2807SJeff Garzik 	tmp &= ~mask;
3122c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].pre;
3123c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].amps;
3124c6fd2807SJeff Garzik 	writel(tmp, phy_mmio + MV5_PHY_MODE);
3125c6fd2807SJeff Garzik }
3126c6fd2807SJeff Garzik 
3127c6fd2807SJeff Garzik 
3128c6fd2807SJeff Garzik #undef ZERO
3129c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
3130c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
3131c6fd2807SJeff Garzik 			     unsigned int port)
3132c6fd2807SJeff Garzik {
3133c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
3134c6fd2807SJeff Garzik 
3135e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
3136c6fd2807SJeff Garzik 
3137c6fd2807SJeff Garzik 	ZERO(0x028);	/* command */
3138cae5a29dSMark Lord 	writel(0x11f, port_mmio + EDMA_CFG);
3139c6fd2807SJeff Garzik 	ZERO(0x004);	/* timer */
3140c6fd2807SJeff Garzik 	ZERO(0x008);	/* irq err cause */
3141c6fd2807SJeff Garzik 	ZERO(0x00c);	/* irq err mask */
3142c6fd2807SJeff Garzik 	ZERO(0x010);	/* rq bah */
3143c6fd2807SJeff Garzik 	ZERO(0x014);	/* rq inp */
3144c6fd2807SJeff Garzik 	ZERO(0x018);	/* rq outp */
3145c6fd2807SJeff Garzik 	ZERO(0x01c);	/* respq bah */
3146c6fd2807SJeff Garzik 	ZERO(0x024);	/* respq outp */
3147c6fd2807SJeff Garzik 	ZERO(0x020);	/* respq inp */
3148c6fd2807SJeff Garzik 	ZERO(0x02c);	/* test control */
3149cae5a29dSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
3150c6fd2807SJeff Garzik }
3151c6fd2807SJeff Garzik #undef ZERO
3152c6fd2807SJeff Garzik 
3153c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
3154c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3155c6fd2807SJeff Garzik 			unsigned int hc)
3156c6fd2807SJeff Garzik {
3157c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3158c6fd2807SJeff Garzik 	u32 tmp;
3159c6fd2807SJeff Garzik 
3160c6fd2807SJeff Garzik 	ZERO(0x00c);
3161c6fd2807SJeff Garzik 	ZERO(0x010);
3162c6fd2807SJeff Garzik 	ZERO(0x014);
3163c6fd2807SJeff Garzik 	ZERO(0x018);
3164c6fd2807SJeff Garzik 
3165c6fd2807SJeff Garzik 	tmp = readl(hc_mmio + 0x20);
3166c6fd2807SJeff Garzik 	tmp &= 0x1c1c1c1c;
3167c6fd2807SJeff Garzik 	tmp |= 0x03030303;
3168c6fd2807SJeff Garzik 	writel(tmp, hc_mmio + 0x20);
3169c6fd2807SJeff Garzik }
3170c6fd2807SJeff Garzik #undef ZERO
3171c6fd2807SJeff Garzik 
3172c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3173c6fd2807SJeff Garzik 			unsigned int n_hc)
3174c6fd2807SJeff Garzik {
3175c6fd2807SJeff Garzik 	unsigned int hc, port;
3176c6fd2807SJeff Garzik 
3177c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3178c6fd2807SJeff Garzik 		for (port = 0; port < MV_PORTS_PER_HC; port++)
3179c6fd2807SJeff Garzik 			mv5_reset_hc_port(hpriv, mmio,
3180c6fd2807SJeff Garzik 					  (hc * MV_PORTS_PER_HC) + port);
3181c6fd2807SJeff Garzik 
3182c6fd2807SJeff Garzik 		mv5_reset_one_hc(hpriv, mmio, hc);
3183c6fd2807SJeff Garzik 	}
3184c6fd2807SJeff Garzik 
3185c6fd2807SJeff Garzik 	return 0;
3186c6fd2807SJeff Garzik }
3187c6fd2807SJeff Garzik 
3188c6fd2807SJeff Garzik #undef ZERO
3189c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
31907bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
3191c6fd2807SJeff Garzik {
319202a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3193c6fd2807SJeff Garzik 	u32 tmp;
3194c6fd2807SJeff Garzik 
3195cae5a29dSMark Lord 	tmp = readl(mmio + MV_PCI_MODE);
3196c6fd2807SJeff Garzik 	tmp &= 0xff00ffff;
3197cae5a29dSMark Lord 	writel(tmp, mmio + MV_PCI_MODE);
3198c6fd2807SJeff Garzik 
3199c6fd2807SJeff Garzik 	ZERO(MV_PCI_DISC_TIMER);
3200c6fd2807SJeff Garzik 	ZERO(MV_PCI_MSI_TRIGGER);
3201cae5a29dSMark Lord 	writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
3202c6fd2807SJeff Garzik 	ZERO(MV_PCI_SERR_MASK);
3203cae5a29dSMark Lord 	ZERO(hpriv->irq_cause_offset);
3204cae5a29dSMark Lord 	ZERO(hpriv->irq_mask_offset);
3205c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_LOW_ADDRESS);
3206c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_HIGH_ADDRESS);
3207c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_ATTRIBUTE);
3208c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_COMMAND);
3209c6fd2807SJeff Garzik }
3210c6fd2807SJeff Garzik #undef ZERO
3211c6fd2807SJeff Garzik 
3212c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3213c6fd2807SJeff Garzik {
3214c6fd2807SJeff Garzik 	u32 tmp;
3215c6fd2807SJeff Garzik 
3216c6fd2807SJeff Garzik 	mv5_reset_flash(hpriv, mmio);
3217c6fd2807SJeff Garzik 
3218cae5a29dSMark Lord 	tmp = readl(mmio + GPIO_PORT_CTL);
3219c6fd2807SJeff Garzik 	tmp &= 0x3;
3220c6fd2807SJeff Garzik 	tmp |= (1 << 5) | (1 << 6);
3221cae5a29dSMark Lord 	writel(tmp, mmio + GPIO_PORT_CTL);
3222c6fd2807SJeff Garzik }
3223c6fd2807SJeff Garzik 
3224c6fd2807SJeff Garzik /**
3225c6fd2807SJeff Garzik  *      mv6_reset_hc - Perform the 6xxx global soft reset
3226c6fd2807SJeff Garzik  *      @mmio: base address of the HBA
3227c6fd2807SJeff Garzik  *
3228c6fd2807SJeff Garzik  *      This routine only applies to 6xxx parts.
3229c6fd2807SJeff Garzik  *
3230c6fd2807SJeff Garzik  *      LOCKING:
3231c6fd2807SJeff Garzik  *      Inherited from caller.
3232c6fd2807SJeff Garzik  */
3233c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3234c6fd2807SJeff Garzik 			unsigned int n_hc)
3235c6fd2807SJeff Garzik {
3236cae5a29dSMark Lord 	void __iomem *reg = mmio + PCI_MAIN_CMD_STS;
3237c6fd2807SJeff Garzik 	int i, rc = 0;
3238c6fd2807SJeff Garzik 	u32 t;
3239c6fd2807SJeff Garzik 
3240c6fd2807SJeff Garzik 	/* Following procedure defined in PCI "main command and status
3241c6fd2807SJeff Garzik 	 * register" table.
3242c6fd2807SJeff Garzik 	 */
3243c6fd2807SJeff Garzik 	t = readl(reg);
3244c6fd2807SJeff Garzik 	writel(t | STOP_PCI_MASTER, reg);
3245c6fd2807SJeff Garzik 
3246c6fd2807SJeff Garzik 	for (i = 0; i < 1000; i++) {
3247c6fd2807SJeff Garzik 		udelay(1);
3248c6fd2807SJeff Garzik 		t = readl(reg);
32492dcb407eSJeff Garzik 		if (PCI_MASTER_EMPTY & t)
3250c6fd2807SJeff Garzik 			break;
3251c6fd2807SJeff Garzik 	}
3252c6fd2807SJeff Garzik 	if (!(PCI_MASTER_EMPTY & t)) {
3253c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
3254c6fd2807SJeff Garzik 		rc = 1;
3255c6fd2807SJeff Garzik 		goto done;
3256c6fd2807SJeff Garzik 	}
3257c6fd2807SJeff Garzik 
3258c6fd2807SJeff Garzik 	/* set reset */
3259c6fd2807SJeff Garzik 	i = 5;
3260c6fd2807SJeff Garzik 	do {
3261c6fd2807SJeff Garzik 		writel(t | GLOB_SFT_RST, reg);
3262c6fd2807SJeff Garzik 		t = readl(reg);
3263c6fd2807SJeff Garzik 		udelay(1);
3264c6fd2807SJeff Garzik 	} while (!(GLOB_SFT_RST & t) && (i-- > 0));
3265c6fd2807SJeff Garzik 
3266c6fd2807SJeff Garzik 	if (!(GLOB_SFT_RST & t)) {
3267c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't set global reset\n");
3268c6fd2807SJeff Garzik 		rc = 1;
3269c6fd2807SJeff Garzik 		goto done;
3270c6fd2807SJeff Garzik 	}
3271c6fd2807SJeff Garzik 
3272c6fd2807SJeff Garzik 	/* clear reset and *reenable the PCI master* (not mentioned in spec) */
3273c6fd2807SJeff Garzik 	i = 5;
3274c6fd2807SJeff Garzik 	do {
3275c6fd2807SJeff Garzik 		writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
3276c6fd2807SJeff Garzik 		t = readl(reg);
3277c6fd2807SJeff Garzik 		udelay(1);
3278c6fd2807SJeff Garzik 	} while ((GLOB_SFT_RST & t) && (i-- > 0));
3279c6fd2807SJeff Garzik 
3280c6fd2807SJeff Garzik 	if (GLOB_SFT_RST & t) {
3281c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
3282c6fd2807SJeff Garzik 		rc = 1;
3283c6fd2807SJeff Garzik 	}
3284c6fd2807SJeff Garzik done:
3285c6fd2807SJeff Garzik 	return rc;
3286c6fd2807SJeff Garzik }
3287c6fd2807SJeff Garzik 
3288c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
3289c6fd2807SJeff Garzik 			   void __iomem *mmio)
3290c6fd2807SJeff Garzik {
3291c6fd2807SJeff Garzik 	void __iomem *port_mmio;
3292c6fd2807SJeff Garzik 	u32 tmp;
3293c6fd2807SJeff Garzik 
3294cae5a29dSMark Lord 	tmp = readl(mmio + RESET_CFG);
3295c6fd2807SJeff Garzik 	if ((tmp & (1 << 0)) == 0) {
3296c6fd2807SJeff Garzik 		hpriv->signal[idx].amps = 0x7 << 8;
3297c6fd2807SJeff Garzik 		hpriv->signal[idx].pre = 0x1 << 5;
3298c6fd2807SJeff Garzik 		return;
3299c6fd2807SJeff Garzik 	}
3300c6fd2807SJeff Garzik 
3301c6fd2807SJeff Garzik 	port_mmio = mv_port_base(mmio, idx);
3302c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE2);
3303c6fd2807SJeff Garzik 
3304c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
3305c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
3306c6fd2807SJeff Garzik }
3307c6fd2807SJeff Garzik 
3308c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3309c6fd2807SJeff Garzik {
3310cae5a29dSMark Lord 	writel(0x00000060, mmio + GPIO_PORT_CTL);
3311c6fd2807SJeff Garzik }
3312c6fd2807SJeff Garzik 
3313c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3314c6fd2807SJeff Garzik 			   unsigned int port)
3315c6fd2807SJeff Garzik {
3316c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
3317c6fd2807SJeff Garzik 
3318c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
3319c6fd2807SJeff Garzik 	int fix_phy_mode2 =
3320c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
3321c6fd2807SJeff Garzik 	int fix_phy_mode4 =
3322c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
33238c30a8b9SMark Lord 	u32 m2, m3;
3324c6fd2807SJeff Garzik 
3325c6fd2807SJeff Garzik 	if (fix_phy_mode2) {
3326c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
3327c6fd2807SJeff Garzik 		m2 &= ~(1 << 16);
3328c6fd2807SJeff Garzik 		m2 |= (1 << 31);
3329c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
3330c6fd2807SJeff Garzik 
3331c6fd2807SJeff Garzik 		udelay(200);
3332c6fd2807SJeff Garzik 
3333c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
3334c6fd2807SJeff Garzik 		m2 &= ~((1 << 16) | (1 << 31));
3335c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
3336c6fd2807SJeff Garzik 
3337c6fd2807SJeff Garzik 		udelay(200);
3338c6fd2807SJeff Garzik 	}
3339c6fd2807SJeff Garzik 
33408c30a8b9SMark Lord 	/*
33418c30a8b9SMark Lord 	 * Gen-II/IIe PHY_MODE3 errata RM#2:
33428c30a8b9SMark Lord 	 * Achieves better receiver noise performance than the h/w default:
33438c30a8b9SMark Lord 	 */
33448c30a8b9SMark Lord 	m3 = readl(port_mmio + PHY_MODE3);
33458c30a8b9SMark Lord 	m3 = (m3 & 0x1f) | (0x5555601 << 5);
3346c6fd2807SJeff Garzik 
33470388a8c0SMark Lord 	/* Guideline 88F5182 (GL# SATA-S11) */
33480388a8c0SMark Lord 	if (IS_SOC(hpriv))
33490388a8c0SMark Lord 		m3 &= ~0x1c;
33500388a8c0SMark Lord 
3351c6fd2807SJeff Garzik 	if (fix_phy_mode4) {
3352ba069e37SMark Lord 		u32 m4 = readl(port_mmio + PHY_MODE4);
3353ba069e37SMark Lord 		/*
3354ba069e37SMark Lord 		 * Enforce reserved-bit restrictions on GenIIe devices only.
3355ba069e37SMark Lord 		 * For earlier chipsets, force only the internal config field
3356ba069e37SMark Lord 		 *  (workaround for errata FEr SATA#10 part 1).
3357ba069e37SMark Lord 		 */
33588c30a8b9SMark Lord 		if (IS_GEN_IIE(hpriv))
3359ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
3360ba069e37SMark Lord 		else
3361ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
33628c30a8b9SMark Lord 		writel(m4, port_mmio + PHY_MODE4);
3363c6fd2807SJeff Garzik 	}
3364b406c7a6SMark Lord 	/*
3365b406c7a6SMark Lord 	 * Workaround for 60x1-B2 errata SATA#13:
3366b406c7a6SMark Lord 	 * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
3367b406c7a6SMark Lord 	 * so we must always rewrite PHY_MODE3 after PHY_MODE4.
3368ba68460bSMark Lord 	 * Or ensure we use writelfl() when writing PHY_MODE4.
3369b406c7a6SMark Lord 	 */
3370b406c7a6SMark Lord 	writel(m3, port_mmio + PHY_MODE3);
3371c6fd2807SJeff Garzik 
3372c6fd2807SJeff Garzik 	/* Revert values of pre-emphasis and signal amps to the saved ones */
3373c6fd2807SJeff Garzik 	m2 = readl(port_mmio + PHY_MODE2);
3374c6fd2807SJeff Garzik 
3375c6fd2807SJeff Garzik 	m2 &= ~MV_M2_PREAMP_MASK;
3376c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].amps;
3377c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].pre;
3378c6fd2807SJeff Garzik 	m2 &= ~(1 << 16);
3379c6fd2807SJeff Garzik 
3380c6fd2807SJeff Garzik 	/* according to mvSata 3.6.1, some IIE values are fixed */
3381c6fd2807SJeff Garzik 	if (IS_GEN_IIE(hpriv)) {
3382c6fd2807SJeff Garzik 		m2 &= ~0xC30FF01F;
3383c6fd2807SJeff Garzik 		m2 |= 0x0000900F;
3384c6fd2807SJeff Garzik 	}
3385c6fd2807SJeff Garzik 
3386c6fd2807SJeff Garzik 	writel(m2, port_mmio + PHY_MODE2);
3387c6fd2807SJeff Garzik }
3388c6fd2807SJeff Garzik 
3389f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
3390f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
3391f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
3392f351b2d6SSaeed Bishara 				      void __iomem *mmio)
3393f351b2d6SSaeed Bishara {
3394f351b2d6SSaeed Bishara 	return;
3395f351b2d6SSaeed Bishara }
3396f351b2d6SSaeed Bishara 
3397f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
3398f351b2d6SSaeed Bishara 			   void __iomem *mmio)
3399f351b2d6SSaeed Bishara {
3400f351b2d6SSaeed Bishara 	void __iomem *port_mmio;
3401f351b2d6SSaeed Bishara 	u32 tmp;
3402f351b2d6SSaeed Bishara 
3403f351b2d6SSaeed Bishara 	port_mmio = mv_port_base(mmio, idx);
3404f351b2d6SSaeed Bishara 	tmp = readl(port_mmio + PHY_MODE2);
3405f351b2d6SSaeed Bishara 
3406f351b2d6SSaeed Bishara 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
3407f351b2d6SSaeed Bishara 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
3408f351b2d6SSaeed Bishara }
3409f351b2d6SSaeed Bishara 
3410f351b2d6SSaeed Bishara #undef ZERO
3411f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
3412f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
3413f351b2d6SSaeed Bishara 					void __iomem *mmio, unsigned int port)
3414f351b2d6SSaeed Bishara {
3415f351b2d6SSaeed Bishara 	void __iomem *port_mmio = mv_port_base(mmio, port);
3416f351b2d6SSaeed Bishara 
3417e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
3418f351b2d6SSaeed Bishara 
3419f351b2d6SSaeed Bishara 	ZERO(0x028);		/* command */
3420cae5a29dSMark Lord 	writel(0x101f, port_mmio + EDMA_CFG);
3421f351b2d6SSaeed Bishara 	ZERO(0x004);		/* timer */
3422f351b2d6SSaeed Bishara 	ZERO(0x008);		/* irq err cause */
3423f351b2d6SSaeed Bishara 	ZERO(0x00c);		/* irq err mask */
3424f351b2d6SSaeed Bishara 	ZERO(0x010);		/* rq bah */
3425f351b2d6SSaeed Bishara 	ZERO(0x014);		/* rq inp */
3426f351b2d6SSaeed Bishara 	ZERO(0x018);		/* rq outp */
3427f351b2d6SSaeed Bishara 	ZERO(0x01c);		/* respq bah */
3428f351b2d6SSaeed Bishara 	ZERO(0x024);		/* respq outp */
3429f351b2d6SSaeed Bishara 	ZERO(0x020);		/* respq inp */
3430f351b2d6SSaeed Bishara 	ZERO(0x02c);		/* test control */
3431d7b0c143SSaeed Bishara 	writel(0x800, port_mmio + EDMA_IORDY_TMOUT);
3432f351b2d6SSaeed Bishara }
3433f351b2d6SSaeed Bishara 
3434f351b2d6SSaeed Bishara #undef ZERO
3435f351b2d6SSaeed Bishara 
3436f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
3437f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
3438f351b2d6SSaeed Bishara 				       void __iomem *mmio)
3439f351b2d6SSaeed Bishara {
3440f351b2d6SSaeed Bishara 	void __iomem *hc_mmio = mv_hc_base(mmio, 0);
3441f351b2d6SSaeed Bishara 
3442f351b2d6SSaeed Bishara 	ZERO(0x00c);
3443f351b2d6SSaeed Bishara 	ZERO(0x010);
3444f351b2d6SSaeed Bishara 	ZERO(0x014);
3445f351b2d6SSaeed Bishara 
3446f351b2d6SSaeed Bishara }
3447f351b2d6SSaeed Bishara 
3448f351b2d6SSaeed Bishara #undef ZERO
3449f351b2d6SSaeed Bishara 
3450f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
3451f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc)
3452f351b2d6SSaeed Bishara {
3453f351b2d6SSaeed Bishara 	unsigned int port;
3454f351b2d6SSaeed Bishara 
3455f351b2d6SSaeed Bishara 	for (port = 0; port < hpriv->n_ports; port++)
3456f351b2d6SSaeed Bishara 		mv_soc_reset_hc_port(hpriv, mmio, port);
3457f351b2d6SSaeed Bishara 
3458f351b2d6SSaeed Bishara 	mv_soc_reset_one_hc(hpriv, mmio);
3459f351b2d6SSaeed Bishara 
3460f351b2d6SSaeed Bishara 	return 0;
3461f351b2d6SSaeed Bishara }
3462f351b2d6SSaeed Bishara 
3463f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
3464f351b2d6SSaeed Bishara 				      void __iomem *mmio)
3465f351b2d6SSaeed Bishara {
3466f351b2d6SSaeed Bishara 	return;
3467f351b2d6SSaeed Bishara }
3468f351b2d6SSaeed Bishara 
3469f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
3470f351b2d6SSaeed Bishara {
3471f351b2d6SSaeed Bishara 	return;
3472f351b2d6SSaeed Bishara }
3473f351b2d6SSaeed Bishara 
347429b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
347529b7e43cSMartin Michlmayr 				  void __iomem *mmio, unsigned int port)
347629b7e43cSMartin Michlmayr {
347729b7e43cSMartin Michlmayr 	void __iomem *port_mmio = mv_port_base(mmio, port);
347829b7e43cSMartin Michlmayr 	u32	reg;
347929b7e43cSMartin Michlmayr 
348029b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE3);
348129b7e43cSMartin Michlmayr 	reg &= ~(0x3 << 27);	/* SELMUPF (bits 28:27) to 1 */
348229b7e43cSMartin Michlmayr 	reg |= (0x1 << 27);
348329b7e43cSMartin Michlmayr 	reg &= ~(0x3 << 29);	/* SELMUPI (bits 30:29) to 1 */
348429b7e43cSMartin Michlmayr 	reg |= (0x1 << 29);
348529b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE3);
348629b7e43cSMartin Michlmayr 
348729b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE4);
348829b7e43cSMartin Michlmayr 	reg &= ~0x1;	/* SATU_OD8 (bit 0) to 0, reserved bit 16 must be set */
348929b7e43cSMartin Michlmayr 	reg |= (0x1 << 16);
349029b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE4);
349129b7e43cSMartin Michlmayr 
349229b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE9_GEN2);
349329b7e43cSMartin Michlmayr 	reg &= ~0xf;	/* TXAMP[3:0] (bits 3:0) to 8 */
349429b7e43cSMartin Michlmayr 	reg |= 0x8;
349529b7e43cSMartin Michlmayr 	reg &= ~(0x1 << 14);	/* TXAMP[4] (bit 14) to 0 */
349629b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE9_GEN2);
349729b7e43cSMartin Michlmayr 
349829b7e43cSMartin Michlmayr 	reg = readl(port_mmio + PHY_MODE9_GEN1);
349929b7e43cSMartin Michlmayr 	reg &= ~0xf;	/* TXAMP[3:0] (bits 3:0) to 8 */
350029b7e43cSMartin Michlmayr 	reg |= 0x8;
350129b7e43cSMartin Michlmayr 	reg &= ~(0x1 << 14);	/* TXAMP[4] (bit 14) to 0 */
350229b7e43cSMartin Michlmayr 	writel(reg, port_mmio + PHY_MODE9_GEN1);
350329b7e43cSMartin Michlmayr }
350429b7e43cSMartin Michlmayr 
350529b7e43cSMartin Michlmayr /**
350629b7e43cSMartin Michlmayr  *	soc_is_65 - check if the soc is 65 nano device
350729b7e43cSMartin Michlmayr  *
350829b7e43cSMartin Michlmayr  *	Detect the type of the SoC, this is done by reading the PHYCFG_OFS
350929b7e43cSMartin Michlmayr  *	register, this register should contain non-zero value and it exists only
351029b7e43cSMartin Michlmayr  *	in the 65 nano devices, when reading it from older devices we get 0.
351129b7e43cSMartin Michlmayr  */
351229b7e43cSMartin Michlmayr static bool soc_is_65n(struct mv_host_priv *hpriv)
351329b7e43cSMartin Michlmayr {
351429b7e43cSMartin Michlmayr 	void __iomem *port0_mmio = mv_port_base(hpriv->base, 0);
351529b7e43cSMartin Michlmayr 
351629b7e43cSMartin Michlmayr 	if (readl(port0_mmio + PHYCFG_OFS))
351729b7e43cSMartin Michlmayr 		return true;
351829b7e43cSMartin Michlmayr 	return false;
351929b7e43cSMartin Michlmayr }
352029b7e43cSMartin Michlmayr 
35218e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
3522b67a1064SMark Lord {
3523cae5a29dSMark Lord 	u32 ifcfg = readl(port_mmio + SATA_IFCFG);
3524b67a1064SMark Lord 
35258e7decdbSMark Lord 	ifcfg = (ifcfg & 0xf7f) | 0x9b1000;	/* from chip spec */
3526b67a1064SMark Lord 	if (want_gen2i)
35278e7decdbSMark Lord 		ifcfg |= (1 << 7);		/* enable gen2i speed */
3528cae5a29dSMark Lord 	writelfl(ifcfg, port_mmio + SATA_IFCFG);
3529b67a1064SMark Lord }
3530b67a1064SMark Lord 
3531e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
3532c6fd2807SJeff Garzik 			     unsigned int port_no)
3533c6fd2807SJeff Garzik {
3534c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port_no);
3535c6fd2807SJeff Garzik 
35368e7decdbSMark Lord 	/*
35378e7decdbSMark Lord 	 * The datasheet warns against setting EDMA_RESET when EDMA is active
35388e7decdbSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
35398e7decdbSMark Lord 	 * to disable the EDMA engine before doing the EDMA_RESET operation.
35408e7decdbSMark Lord 	 */
35410d8be5cbSMark Lord 	mv_stop_edma_engine(port_mmio);
3542cae5a29dSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3543c6fd2807SJeff Garzik 
3544b67a1064SMark Lord 	if (!IS_GEN_I(hpriv)) {
35458e7decdbSMark Lord 		/* Enable 3.0gb/s link speed: this survives EDMA_RESET */
35468e7decdbSMark Lord 		mv_setup_ifcfg(port_mmio, 1);
3547c6fd2807SJeff Garzik 	}
3548b67a1064SMark Lord 	/*
35498e7decdbSMark Lord 	 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
3550b67a1064SMark Lord 	 * link, and physical layers.  It resets all SATA interface registers
3551cae5a29dSMark Lord 	 * (except for SATA_IFCFG), and issues a COMRESET to the dev.
3552c6fd2807SJeff Garzik 	 */
3553cae5a29dSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3554b67a1064SMark Lord 	udelay(25);	/* allow reset propagation */
3555cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_CMD);
3556c6fd2807SJeff Garzik 
3557c6fd2807SJeff Garzik 	hpriv->ops->phy_errata(hpriv, mmio, port_no);
3558c6fd2807SJeff Garzik 
3559ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv))
3560c6fd2807SJeff Garzik 		mdelay(1);
3561c6fd2807SJeff Garzik }
3562c6fd2807SJeff Garzik 
3563e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
3564e49856d8SMark Lord {
3565e49856d8SMark Lord 	if (sata_pmp_supported(ap)) {
3566e49856d8SMark Lord 		void __iomem *port_mmio = mv_ap_base(ap);
3567cae5a29dSMark Lord 		u32 reg = readl(port_mmio + SATA_IFCTL);
3568e49856d8SMark Lord 		int old = reg & 0xf;
3569e49856d8SMark Lord 
3570e49856d8SMark Lord 		if (old != pmp) {
3571e49856d8SMark Lord 			reg = (reg & ~0xf) | pmp;
3572cae5a29dSMark Lord 			writelfl(reg, port_mmio + SATA_IFCTL);
3573e49856d8SMark Lord 		}
3574e49856d8SMark Lord 	}
3575e49856d8SMark Lord }
3576e49856d8SMark Lord 
3577e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
3578bdd4dddeSJeff Garzik 				unsigned long deadline)
3579c6fd2807SJeff Garzik {
3580e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
3581e49856d8SMark Lord 	return sata_std_hardreset(link, class, deadline);
3582e49856d8SMark Lord }
3583c6fd2807SJeff Garzik 
3584e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
3585e49856d8SMark Lord 				unsigned long deadline)
3586da3dbb17STejun Heo {
3587e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
3588e49856d8SMark Lord 	return ata_sff_softreset(link, class, deadline);
3589bdd4dddeSJeff Garzik }
3590bdd4dddeSJeff Garzik 
3591cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
3592bdd4dddeSJeff Garzik 			unsigned long deadline)
3593bdd4dddeSJeff Garzik {
3594cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3595bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
3596b562468cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
3597f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
35980d8be5cbSMark Lord 	int rc, attempts = 0, extra = 0;
35990d8be5cbSMark Lord 	u32 sstatus;
36000d8be5cbSMark Lord 	bool online;
3601bdd4dddeSJeff Garzik 
3602e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, ap->port_no);
3603b562468cSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
3604d16ab3f6SMark Lord 	pp->pp_flags &=
3605d16ab3f6SMark Lord 	  ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
3606bdd4dddeSJeff Garzik 
36070d8be5cbSMark Lord 	/* Workaround for errata FEr SATA#10 (part 2) */
36080d8be5cbSMark Lord 	do {
360917c5aab5SMark Lord 		const unsigned long *timing =
361017c5aab5SMark Lord 				sata_ehc_deb_timing(&link->eh_context);
3611bdd4dddeSJeff Garzik 
361217c5aab5SMark Lord 		rc = sata_link_hardreset(link, timing, deadline + extra,
361317c5aab5SMark Lord 					 &online, NULL);
36149dcffd99SMark Lord 		rc = online ? -EAGAIN : rc;
361517c5aab5SMark Lord 		if (rc)
36160d8be5cbSMark Lord 			return rc;
36170d8be5cbSMark Lord 		sata_scr_read(link, SCR_STATUS, &sstatus);
36180d8be5cbSMark Lord 		if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
36190d8be5cbSMark Lord 			/* Force 1.5gb/s link speed and try again */
36208e7decdbSMark Lord 			mv_setup_ifcfg(mv_ap_base(ap), 0);
36210d8be5cbSMark Lord 			if (time_after(jiffies + HZ, deadline))
36220d8be5cbSMark Lord 				extra = HZ; /* only extend it once, max */
3623bdd4dddeSJeff Garzik 		}
36240d8be5cbSMark Lord 	} while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
362508da1759SMark Lord 	mv_save_cached_regs(ap);
362666e57a2cSMark Lord 	mv_edma_cfg(ap, 0, 0);
3627bdd4dddeSJeff Garzik 
362817c5aab5SMark Lord 	return rc;
3629bdd4dddeSJeff Garzik }
3630bdd4dddeSJeff Garzik 
3631bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
3632c6fd2807SJeff Garzik {
36331cfd19aeSMark Lord 	mv_stop_edma(ap);
3634c4de573bSMark Lord 	mv_enable_port_irqs(ap, 0);
3635c6fd2807SJeff Garzik }
3636bdd4dddeSJeff Garzik 
3637bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
3638bdd4dddeSJeff Garzik {
3639f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
3640c4de573bSMark Lord 	unsigned int port = ap->port_no;
3641c4de573bSMark Lord 	unsigned int hardport = mv_hardport_from_port(port);
36421cfd19aeSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
3643bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
3644c4de573bSMark Lord 	u32 hc_irq_cause;
3645bdd4dddeSJeff Garzik 
3646bdd4dddeSJeff Garzik 	/* clear EDMA errors on this port */
3647cae5a29dSMark Lord 	writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3648bdd4dddeSJeff Garzik 
3649bdd4dddeSJeff Garzik 	/* clear pending irq events */
3650cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
3651cae5a29dSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
3652bdd4dddeSJeff Garzik 
365388e675e1SMark Lord 	mv_enable_port_irqs(ap, ERR_IRQ);
3654c6fd2807SJeff Garzik }
3655c6fd2807SJeff Garzik 
3656c6fd2807SJeff Garzik /**
3657c6fd2807SJeff Garzik  *      mv_port_init - Perform some early initialization on a single port.
3658c6fd2807SJeff Garzik  *      @port: libata data structure storing shadow register addresses
3659c6fd2807SJeff Garzik  *      @port_mmio: base address of the port
3660c6fd2807SJeff Garzik  *
3661c6fd2807SJeff Garzik  *      Initialize shadow register mmio addresses, clear outstanding
3662c6fd2807SJeff Garzik  *      interrupts on the port, and unmask interrupts for the future
3663c6fd2807SJeff Garzik  *      start of the port.
3664c6fd2807SJeff Garzik  *
3665c6fd2807SJeff Garzik  *      LOCKING:
3666c6fd2807SJeff Garzik  *      Inherited from caller.
3667c6fd2807SJeff Garzik  */
3668c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
3669c6fd2807SJeff Garzik {
3670cae5a29dSMark Lord 	void __iomem *serr, *shd_base = port_mmio + SHD_BLK;
3671c6fd2807SJeff Garzik 
3672c6fd2807SJeff Garzik 	/* PIO related setup
3673c6fd2807SJeff Garzik 	 */
3674c6fd2807SJeff Garzik 	port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
3675c6fd2807SJeff Garzik 	port->error_addr =
3676c6fd2807SJeff Garzik 		port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
3677c6fd2807SJeff Garzik 	port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
3678c6fd2807SJeff Garzik 	port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
3679c6fd2807SJeff Garzik 	port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
3680c6fd2807SJeff Garzik 	port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
3681c6fd2807SJeff Garzik 	port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
3682c6fd2807SJeff Garzik 	port->status_addr =
3683c6fd2807SJeff Garzik 		port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
3684c6fd2807SJeff Garzik 	/* special case: control/altstatus doesn't have ATA_REG_ address */
3685cae5a29dSMark Lord 	port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST;
3686c6fd2807SJeff Garzik 
3687c6fd2807SJeff Garzik 	/* Clear any currently outstanding port interrupt conditions */
3688cae5a29dSMark Lord 	serr = port_mmio + mv_scr_offset(SCR_ERROR);
3689cae5a29dSMark Lord 	writelfl(readl(serr), serr);
3690cae5a29dSMark Lord 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3691c6fd2807SJeff Garzik 
3692646a4da5SMark Lord 	/* unmask all non-transient EDMA error interrupts */
3693cae5a29dSMark Lord 	writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK);
3694c6fd2807SJeff Garzik 
3695c6fd2807SJeff Garzik 	VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
3696cae5a29dSMark Lord 		readl(port_mmio + EDMA_CFG),
3697cae5a29dSMark Lord 		readl(port_mmio + EDMA_ERR_IRQ_CAUSE),
3698cae5a29dSMark Lord 		readl(port_mmio + EDMA_ERR_IRQ_MASK));
3699c6fd2807SJeff Garzik }
3700c6fd2807SJeff Garzik 
3701616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host)
3702616d4a98SMark Lord {
3703616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3704616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
3705616d4a98SMark Lord 	u32 reg;
3706616d4a98SMark Lord 
37071f398472SMark Lord 	if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
3708616d4a98SMark Lord 		return 0;	/* not PCI-X capable */
3709cae5a29dSMark Lord 	reg = readl(mmio + MV_PCI_MODE);
3710616d4a98SMark Lord 	if ((reg & MV_PCI_MODE_MASK) == 0)
3711616d4a98SMark Lord 		return 0;	/* conventional PCI mode */
3712616d4a98SMark Lord 	return 1;	/* chip is in PCI-X mode */
3713616d4a98SMark Lord }
3714616d4a98SMark Lord 
3715616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host)
3716616d4a98SMark Lord {
3717616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
3718616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
3719616d4a98SMark Lord 	u32 reg;
3720616d4a98SMark Lord 
3721616d4a98SMark Lord 	if (!mv_in_pcix_mode(host)) {
3722cae5a29dSMark Lord 		reg = readl(mmio + MV_PCI_COMMAND);
3723cae5a29dSMark Lord 		if (reg & MV_PCI_COMMAND_MRDTRIG)
3724616d4a98SMark Lord 			return 0; /* not okay */
3725616d4a98SMark Lord 	}
3726616d4a98SMark Lord 	return 1; /* okay */
3727616d4a98SMark Lord }
3728616d4a98SMark Lord 
372965ad7fefSMark Lord static void mv_60x1b2_errata_pci7(struct ata_host *host)
373065ad7fefSMark Lord {
373165ad7fefSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
373265ad7fefSMark Lord 	void __iomem *mmio = hpriv->base;
373365ad7fefSMark Lord 
373465ad7fefSMark Lord 	/* workaround for 60x1-B2 errata PCI#7 */
373565ad7fefSMark Lord 	if (mv_in_pcix_mode(host)) {
3736cae5a29dSMark Lord 		u32 reg = readl(mmio + MV_PCI_COMMAND);
3737cae5a29dSMark Lord 		writelfl(reg & ~MV_PCI_COMMAND_MWRCOM, mmio + MV_PCI_COMMAND);
373865ad7fefSMark Lord 	}
373965ad7fefSMark Lord }
374065ad7fefSMark Lord 
37414447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
3742c6fd2807SJeff Garzik {
37434447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
37444447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3745c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
3746c6fd2807SJeff Garzik 
3747c6fd2807SJeff Garzik 	switch (board_idx) {
3748c6fd2807SJeff Garzik 	case chip_5080:
3749c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
3750ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
3751c6fd2807SJeff Garzik 
375244c10138SAuke Kok 		switch (pdev->revision) {
3753c6fd2807SJeff Garzik 		case 0x1:
3754c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
3755c6fd2807SJeff Garzik 			break;
3756c6fd2807SJeff Garzik 		case 0x3:
3757c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3758c6fd2807SJeff Garzik 			break;
3759c6fd2807SJeff Garzik 		default:
3760a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3761c6fd2807SJeff Garzik 				 "Applying 50XXB2 workarounds to unknown rev\n");
3762c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3763c6fd2807SJeff Garzik 			break;
3764c6fd2807SJeff Garzik 		}
3765c6fd2807SJeff Garzik 		break;
3766c6fd2807SJeff Garzik 
3767c6fd2807SJeff Garzik 	case chip_504x:
3768c6fd2807SJeff Garzik 	case chip_508x:
3769c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
3770ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
3771c6fd2807SJeff Garzik 
377244c10138SAuke Kok 		switch (pdev->revision) {
3773c6fd2807SJeff Garzik 		case 0x0:
3774c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
3775c6fd2807SJeff Garzik 			break;
3776c6fd2807SJeff Garzik 		case 0x3:
3777c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3778c6fd2807SJeff Garzik 			break;
3779c6fd2807SJeff Garzik 		default:
3780a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3781c6fd2807SJeff Garzik 				 "Applying B2 workarounds to unknown rev\n");
3782c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
3783c6fd2807SJeff Garzik 			break;
3784c6fd2807SJeff Garzik 		}
3785c6fd2807SJeff Garzik 		break;
3786c6fd2807SJeff Garzik 
3787c6fd2807SJeff Garzik 	case chip_604x:
3788c6fd2807SJeff Garzik 	case chip_608x:
3789c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
3790ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_II;
3791c6fd2807SJeff Garzik 
379244c10138SAuke Kok 		switch (pdev->revision) {
3793c6fd2807SJeff Garzik 		case 0x7:
379465ad7fefSMark Lord 			mv_60x1b2_errata_pci7(host);
3795c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
3796c6fd2807SJeff Garzik 			break;
3797c6fd2807SJeff Garzik 		case 0x9:
3798c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3799c6fd2807SJeff Garzik 			break;
3800c6fd2807SJeff Garzik 		default:
3801a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3802c6fd2807SJeff Garzik 				 "Applying B2 workarounds to unknown rev\n");
3803c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
3804c6fd2807SJeff Garzik 			break;
3805c6fd2807SJeff Garzik 		}
3806c6fd2807SJeff Garzik 		break;
3807c6fd2807SJeff Garzik 
3808c6fd2807SJeff Garzik 	case chip_7042:
3809616d4a98SMark Lord 		hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
3810306b30f7SMark Lord 		if (pdev->vendor == PCI_VENDOR_ID_TTI &&
3811306b30f7SMark Lord 		    (pdev->device == 0x2300 || pdev->device == 0x2310))
3812306b30f7SMark Lord 		{
38134e520033SMark Lord 			/*
38144e520033SMark Lord 			 * Highpoint RocketRAID PCIe 23xx series cards:
38154e520033SMark Lord 			 *
38164e520033SMark Lord 			 * Unconfigured drives are treated as "Legacy"
38174e520033SMark Lord 			 * by the BIOS, and it overwrites sector 8 with
38184e520033SMark Lord 			 * a "Lgcy" metadata block prior to Linux boot.
38194e520033SMark Lord 			 *
38204e520033SMark Lord 			 * Configured drives (RAID or JBOD) leave sector 8
38214e520033SMark Lord 			 * alone, but instead overwrite a high numbered
38224e520033SMark Lord 			 * sector for the RAID metadata.  This sector can
38234e520033SMark Lord 			 * be determined exactly, by truncating the physical
38244e520033SMark Lord 			 * drive capacity to a nice even GB value.
38254e520033SMark Lord 			 *
38264e520033SMark Lord 			 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
38274e520033SMark Lord 			 *
38284e520033SMark Lord 			 * Warn the user, lest they think we're just buggy.
38294e520033SMark Lord 			 */
38304e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
38314e520033SMark Lord 				" BIOS CORRUPTS DATA on all attached drives,"
38324e520033SMark Lord 				" regardless of if/how they are configured."
38334e520033SMark Lord 				" BEWARE!\n");
38344e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": For data safety, do not"
38354e520033SMark Lord 				" use sectors 8-9 on \"Legacy\" drives,"
38364e520033SMark Lord 				" and avoid the final two gigabytes on"
38374e520033SMark Lord 				" all RocketRAID BIOS initialized drives.\n");
3838306b30f7SMark Lord 		}
38398e7decdbSMark Lord 		/* drop through */
3840c6fd2807SJeff Garzik 	case chip_6042:
3841c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
3842c6fd2807SJeff Garzik 		hp_flags |= MV_HP_GEN_IIE;
3843616d4a98SMark Lord 		if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
3844616d4a98SMark Lord 			hp_flags |= MV_HP_CUT_THROUGH;
3845c6fd2807SJeff Garzik 
384644c10138SAuke Kok 		switch (pdev->revision) {
38475cf73bfbSMark Lord 		case 0x2: /* Rev.B0: the first/only public release */
3848c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3849c6fd2807SJeff Garzik 			break;
3850c6fd2807SJeff Garzik 		default:
3851a44fec1fSJoe Perches 			dev_warn(&pdev->dev,
3852c6fd2807SJeff Garzik 				 "Applying 60X1C0 workarounds to unknown rev\n");
3853c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3854c6fd2807SJeff Garzik 			break;
3855c6fd2807SJeff Garzik 		}
3856c6fd2807SJeff Garzik 		break;
3857f351b2d6SSaeed Bishara 	case chip_soc:
385829b7e43cSMartin Michlmayr 		if (soc_is_65n(hpriv))
385929b7e43cSMartin Michlmayr 			hpriv->ops = &mv_soc_65n_ops;
386029b7e43cSMartin Michlmayr 		else
3861f351b2d6SSaeed Bishara 			hpriv->ops = &mv_soc_ops;
3862eb3a55a9SSaeed Bishara 		hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3863eb3a55a9SSaeed Bishara 			MV_HP_ERRATA_60X1C0;
3864f351b2d6SSaeed Bishara 		break;
3865c6fd2807SJeff Garzik 
3866c6fd2807SJeff Garzik 	default:
3867a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: invalid board index %u\n", board_idx);
3868c6fd2807SJeff Garzik 		return 1;
3869c6fd2807SJeff Garzik 	}
3870c6fd2807SJeff Garzik 
3871c6fd2807SJeff Garzik 	hpriv->hp_flags = hp_flags;
387202a121daSMark Lord 	if (hp_flags & MV_HP_PCIE) {
3873cae5a29dSMark Lord 		hpriv->irq_cause_offset	= PCIE_IRQ_CAUSE;
3874cae5a29dSMark Lord 		hpriv->irq_mask_offset	= PCIE_IRQ_MASK;
387502a121daSMark Lord 		hpriv->unmask_all_irqs	= PCIE_UNMASK_ALL_IRQS;
387602a121daSMark Lord 	} else {
3877cae5a29dSMark Lord 		hpriv->irq_cause_offset	= PCI_IRQ_CAUSE;
3878cae5a29dSMark Lord 		hpriv->irq_mask_offset	= PCI_IRQ_MASK;
387902a121daSMark Lord 		hpriv->unmask_all_irqs	= PCI_UNMASK_ALL_IRQS;
388002a121daSMark Lord 	}
3881c6fd2807SJeff Garzik 
3882c6fd2807SJeff Garzik 	return 0;
3883c6fd2807SJeff Garzik }
3884c6fd2807SJeff Garzik 
3885c6fd2807SJeff Garzik /**
3886c6fd2807SJeff Garzik  *      mv_init_host - Perform some early initialization of the host.
38874447d351STejun Heo  *	@host: ATA host to initialize
3888c6fd2807SJeff Garzik  *
3889c6fd2807SJeff Garzik  *      If possible, do an early global reset of the host.  Then do
3890c6fd2807SJeff Garzik  *      our port init and clear/unmask all/relevant host interrupts.
3891c6fd2807SJeff Garzik  *
3892c6fd2807SJeff Garzik  *      LOCKING:
3893c6fd2807SJeff Garzik  *      Inherited from caller.
3894c6fd2807SJeff Garzik  */
38951bfeff03SSaeed Bishara static int mv_init_host(struct ata_host *host)
3896c6fd2807SJeff Garzik {
3897c6fd2807SJeff Garzik 	int rc = 0, n_hc, port, hc;
38984447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3899f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
3900c6fd2807SJeff Garzik 
39011bfeff03SSaeed Bishara 	rc = mv_chip_id(host, hpriv->board_idx);
3902c6fd2807SJeff Garzik 	if (rc)
3903c6fd2807SJeff Garzik 		goto done;
3904c6fd2807SJeff Garzik 
39051f398472SMark Lord 	if (IS_SOC(hpriv)) {
3906cae5a29dSMark Lord 		hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE;
3907cae5a29dSMark Lord 		hpriv->main_irq_mask_addr  = mmio + SOC_HC_MAIN_IRQ_MASK;
39081f398472SMark Lord 	} else {
3909cae5a29dSMark Lord 		hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE;
3910cae5a29dSMark Lord 		hpriv->main_irq_mask_addr  = mmio + PCI_HC_MAIN_IRQ_MASK;
3911f351b2d6SSaeed Bishara 	}
3912352fab70SMark Lord 
39135d0fb2e7SThomas Reitmayr 	/* initialize shadow irq mask with register's value */
39145d0fb2e7SThomas Reitmayr 	hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
39155d0fb2e7SThomas Reitmayr 
3916352fab70SMark Lord 	/* global interrupt mask: 0 == mask everything */
3917c4de573bSMark Lord 	mv_set_main_irq_mask(host, ~0, 0);
3918f351b2d6SSaeed Bishara 
39194447d351STejun Heo 	n_hc = mv_get_hc_count(host->ports[0]->flags);
3920c6fd2807SJeff Garzik 
39214447d351STejun Heo 	for (port = 0; port < host->n_ports; port++)
392229b7e43cSMartin Michlmayr 		if (hpriv->ops->read_preamp)
3923c6fd2807SJeff Garzik 			hpriv->ops->read_preamp(hpriv, port, mmio);
3924c6fd2807SJeff Garzik 
3925c6fd2807SJeff Garzik 	rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
3926c6fd2807SJeff Garzik 	if (rc)
3927c6fd2807SJeff Garzik 		goto done;
3928c6fd2807SJeff Garzik 
3929c6fd2807SJeff Garzik 	hpriv->ops->reset_flash(hpriv, mmio);
39307bb3c529SSaeed Bishara 	hpriv->ops->reset_bus(host, mmio);
3931c6fd2807SJeff Garzik 	hpriv->ops->enable_leds(hpriv, mmio);
3932c6fd2807SJeff Garzik 
39334447d351STejun Heo 	for (port = 0; port < host->n_ports; port++) {
3934cbcdd875STejun Heo 		struct ata_port *ap = host->ports[port];
3935c6fd2807SJeff Garzik 		void __iomem *port_mmio = mv_port_base(mmio, port);
3936cbcdd875STejun Heo 
3937cbcdd875STejun Heo 		mv_port_init(&ap->ioaddr, port_mmio);
3938c6fd2807SJeff Garzik 	}
3939c6fd2807SJeff Garzik 
3940c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3941c6fd2807SJeff Garzik 		void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3942c6fd2807SJeff Garzik 
3943c6fd2807SJeff Garzik 		VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
3944c6fd2807SJeff Garzik 			"(before clear)=0x%08x\n", hc,
3945cae5a29dSMark Lord 			readl(hc_mmio + HC_CFG),
3946cae5a29dSMark Lord 			readl(hc_mmio + HC_IRQ_CAUSE));
3947c6fd2807SJeff Garzik 
3948c6fd2807SJeff Garzik 		/* Clear any currently outstanding hc interrupt conditions */
3949cae5a29dSMark Lord 		writelfl(0, hc_mmio + HC_IRQ_CAUSE);
3950c6fd2807SJeff Garzik 	}
3951c6fd2807SJeff Garzik 
395244c65d16SMark Lord 	if (!IS_SOC(hpriv)) {
3953c6fd2807SJeff Garzik 		/* Clear any currently outstanding host interrupt conditions */
3954cae5a29dSMark Lord 		writelfl(0, mmio + hpriv->irq_cause_offset);
3955c6fd2807SJeff Garzik 
3956c6fd2807SJeff Garzik 		/* and unmask interrupt generation for host regs */
3957cae5a29dSMark Lord 		writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_offset);
395844c65d16SMark Lord 	}
3959c6fd2807SJeff Garzik 
396051de32d2SMark Lord 	/*
396151de32d2SMark Lord 	 * enable only global host interrupts for now.
396251de32d2SMark Lord 	 * The per-port interrupts get done later as ports are set up.
396351de32d2SMark Lord 	 */
3964c4de573bSMark Lord 	mv_set_main_irq_mask(host, 0, PCI_ERR);
39652b748a0aSMark Lord 	mv_set_irq_coalescing(host, irq_coalescing_io_count,
39662b748a0aSMark Lord 				    irq_coalescing_usecs);
3967c6fd2807SJeff Garzik done:
3968c6fd2807SJeff Garzik 	return rc;
3969c6fd2807SJeff Garzik }
3970c6fd2807SJeff Garzik 
3971fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
3972fbf14e2fSByron Bradley {
3973fbf14e2fSByron Bradley 	hpriv->crqb_pool   = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
3974fbf14e2fSByron Bradley 							     MV_CRQB_Q_SZ, 0);
3975fbf14e2fSByron Bradley 	if (!hpriv->crqb_pool)
3976fbf14e2fSByron Bradley 		return -ENOMEM;
3977fbf14e2fSByron Bradley 
3978fbf14e2fSByron Bradley 	hpriv->crpb_pool   = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
3979fbf14e2fSByron Bradley 							     MV_CRPB_Q_SZ, 0);
3980fbf14e2fSByron Bradley 	if (!hpriv->crpb_pool)
3981fbf14e2fSByron Bradley 		return -ENOMEM;
3982fbf14e2fSByron Bradley 
3983fbf14e2fSByron Bradley 	hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
3984fbf14e2fSByron Bradley 							     MV_SG_TBL_SZ, 0);
3985fbf14e2fSByron Bradley 	if (!hpriv->sg_tbl_pool)
3986fbf14e2fSByron Bradley 		return -ENOMEM;
3987fbf14e2fSByron Bradley 
3988fbf14e2fSByron Bradley 	return 0;
3989fbf14e2fSByron Bradley }
3990fbf14e2fSByron Bradley 
399115a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
399263a9332bSAndrew Lunn 				 const struct mbus_dram_target_info *dram)
399315a32632SLennert Buytenhek {
399415a32632SLennert Buytenhek 	int i;
399515a32632SLennert Buytenhek 
399615a32632SLennert Buytenhek 	for (i = 0; i < 4; i++) {
399715a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_CTRL(i));
399815a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_BASE(i));
399915a32632SLennert Buytenhek 	}
400015a32632SLennert Buytenhek 
400115a32632SLennert Buytenhek 	for (i = 0; i < dram->num_cs; i++) {
400263a9332bSAndrew Lunn 		const struct mbus_dram_window *cs = dram->cs + i;
400315a32632SLennert Buytenhek 
400415a32632SLennert Buytenhek 		writel(((cs->size - 1) & 0xffff0000) |
400515a32632SLennert Buytenhek 			(cs->mbus_attr << 8) |
400615a32632SLennert Buytenhek 			(dram->mbus_dram_target_id << 4) | 1,
400715a32632SLennert Buytenhek 			hpriv->base + WINDOW_CTRL(i));
400815a32632SLennert Buytenhek 		writel(cs->base, hpriv->base + WINDOW_BASE(i));
400915a32632SLennert Buytenhek 	}
401015a32632SLennert Buytenhek }
401115a32632SLennert Buytenhek 
4012f351b2d6SSaeed Bishara /**
4013f351b2d6SSaeed Bishara  *      mv_platform_probe - handle a positive probe of an soc Marvell
4014f351b2d6SSaeed Bishara  *      host
4015f351b2d6SSaeed Bishara  *      @pdev: platform device found
4016f351b2d6SSaeed Bishara  *
4017f351b2d6SSaeed Bishara  *      LOCKING:
4018f351b2d6SSaeed Bishara  *      Inherited from caller.
4019f351b2d6SSaeed Bishara  */
4020f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
4021f351b2d6SSaeed Bishara {
4022f351b2d6SSaeed Bishara 	const struct mv_sata_platform_data *mv_platform_data;
402363a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
4024f351b2d6SSaeed Bishara 	const struct ata_port_info *ppi[] =
4025f351b2d6SSaeed Bishara 	    { &mv_port_info[chip_soc], NULL };
4026f351b2d6SSaeed Bishara 	struct ata_host *host;
4027f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv;
4028f351b2d6SSaeed Bishara 	struct resource *res;
402999b80e97SDan Carpenter 	int n_ports = 0;
403099b80e97SDan Carpenter 	int rc;
4031*eee98990SAndrew Lunn #if defined(CONFIG_HAVE_CLK)
4032*eee98990SAndrew Lunn 	int port;
4033*eee98990SAndrew Lunn #endif
4034f351b2d6SSaeed Bishara 
403506296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
4036f351b2d6SSaeed Bishara 
4037f351b2d6SSaeed Bishara 	/*
4038f351b2d6SSaeed Bishara 	 * Simple resource validation ..
4039f351b2d6SSaeed Bishara 	 */
4040f351b2d6SSaeed Bishara 	if (unlikely(pdev->num_resources != 2)) {
4041f351b2d6SSaeed Bishara 		dev_err(&pdev->dev, "invalid number of resources\n");
4042f351b2d6SSaeed Bishara 		return -EINVAL;
4043f351b2d6SSaeed Bishara 	}
4044f351b2d6SSaeed Bishara 
4045f351b2d6SSaeed Bishara 	/*
4046f351b2d6SSaeed Bishara 	 * Get the register base first
4047f351b2d6SSaeed Bishara 	 */
4048f351b2d6SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
4049f351b2d6SSaeed Bishara 	if (res == NULL)
4050f351b2d6SSaeed Bishara 		return -EINVAL;
4051f351b2d6SSaeed Bishara 
4052f351b2d6SSaeed Bishara 	/* allocate host */
4053f351b2d6SSaeed Bishara 	mv_platform_data = pdev->dev.platform_data;
4054f351b2d6SSaeed Bishara 	n_ports = mv_platform_data->n_ports;
4055f351b2d6SSaeed Bishara 
4056f351b2d6SSaeed Bishara 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
4057f351b2d6SSaeed Bishara 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
4058f351b2d6SSaeed Bishara 
4059f351b2d6SSaeed Bishara 	if (!host || !hpriv)
4060f351b2d6SSaeed Bishara 		return -ENOMEM;
4061*eee98990SAndrew Lunn #if defined(CONFIG_HAVE_CLK)
4062*eee98990SAndrew Lunn 	hpriv->port_clks = devm_kzalloc(&pdev->dev,
4063*eee98990SAndrew Lunn 					sizeof(struct clk *) * n_ports,
4064*eee98990SAndrew Lunn 					GFP_KERNEL);
4065*eee98990SAndrew Lunn 	if (!hpriv->port_clks)
4066*eee98990SAndrew Lunn 		return -ENOMEM;
4067*eee98990SAndrew Lunn #endif
4068f351b2d6SSaeed Bishara 	host->private_data = hpriv;
4069f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
40701bfeff03SSaeed Bishara 	hpriv->board_idx = chip_soc;
4071f351b2d6SSaeed Bishara 
4072f351b2d6SSaeed Bishara 	host->iomap = NULL;
4073f1cb0ea1SSaeed Bishara 	hpriv->base = devm_ioremap(&pdev->dev, res->start,
4074041b5eacSJulia Lawall 				   resource_size(res));
4075cae5a29dSMark Lord 	hpriv->base -= SATAHC0_REG_BASE;
4076f351b2d6SSaeed Bishara 
4077c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4078c77a2f4eSSaeed Bishara 	hpriv->clk = clk_get(&pdev->dev, NULL);
4079c77a2f4eSSaeed Bishara 	if (IS_ERR(hpriv->clk))
4080*eee98990SAndrew Lunn 		dev_notice(&pdev->dev, "cannot get optional clkdev\n");
4081c77a2f4eSSaeed Bishara 	else
4082*eee98990SAndrew Lunn 		clk_prepare_enable(hpriv->clk);
4083*eee98990SAndrew Lunn 
4084*eee98990SAndrew Lunn 	for (port = 0; port < n_ports; port++) {
4085*eee98990SAndrew Lunn 		char port_number[16];
4086*eee98990SAndrew Lunn 		sprintf(port_number, "%d", port);
4087*eee98990SAndrew Lunn 		hpriv->port_clks[port] = clk_get(&pdev->dev, port_number);
4088*eee98990SAndrew Lunn 		if (!IS_ERR(hpriv->port_clks[port]))
4089*eee98990SAndrew Lunn 			clk_prepare_enable(hpriv->port_clks[port]);
4090*eee98990SAndrew Lunn 	}
4091c77a2f4eSSaeed Bishara #endif
4092c77a2f4eSSaeed Bishara 
409315a32632SLennert Buytenhek 	/*
409415a32632SLennert Buytenhek 	 * (Re-)program MBUS remapping windows if we are asked to.
409515a32632SLennert Buytenhek 	 */
409663a9332bSAndrew Lunn 	dram = mv_mbus_dram_info();
409763a9332bSAndrew Lunn 	if (dram)
409863a9332bSAndrew Lunn 		mv_conf_mbus_windows(hpriv, dram);
409915a32632SLennert Buytenhek 
4100fbf14e2fSByron Bradley 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
4101fbf14e2fSByron Bradley 	if (rc)
4102c77a2f4eSSaeed Bishara 		goto err;
4103fbf14e2fSByron Bradley 
4104f351b2d6SSaeed Bishara 	/* initialize adapter */
41051bfeff03SSaeed Bishara 	rc = mv_init_host(host);
4106f351b2d6SSaeed Bishara 	if (rc)
4107c77a2f4eSSaeed Bishara 		goto err;
4108f351b2d6SSaeed Bishara 
4109a44fec1fSJoe Perches 	dev_info(&pdev->dev, "slots %u ports %d\n",
4110a44fec1fSJoe Perches 		 (unsigned)MV_MAX_Q_DEPTH, host->n_ports);
4111f351b2d6SSaeed Bishara 
4112c00a4c9dSSergei Shtylyov 	rc = ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
4113f351b2d6SSaeed Bishara 			       IRQF_SHARED, &mv6_sht);
4114c00a4c9dSSergei Shtylyov 	if (!rc)
4115c00a4c9dSSergei Shtylyov 		return 0;
4116c00a4c9dSSergei Shtylyov 
4117c77a2f4eSSaeed Bishara err:
4118c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4119c77a2f4eSSaeed Bishara 	if (!IS_ERR(hpriv->clk)) {
4120*eee98990SAndrew Lunn 		clk_disable_unprepare(hpriv->clk);
4121c77a2f4eSSaeed Bishara 		clk_put(hpriv->clk);
4122c77a2f4eSSaeed Bishara 	}
4123*eee98990SAndrew Lunn 	for (port = 0; port < n_ports; port++) {
4124*eee98990SAndrew Lunn 		if (!IS_ERR(hpriv->port_clks[port])) {
4125*eee98990SAndrew Lunn 			clk_disable_unprepare(hpriv->port_clks[port]);
4126*eee98990SAndrew Lunn 			clk_put(hpriv->port_clks[port]);
4127*eee98990SAndrew Lunn 		}
4128*eee98990SAndrew Lunn 	}
4129c77a2f4eSSaeed Bishara #endif
4130c77a2f4eSSaeed Bishara 
4131c77a2f4eSSaeed Bishara 	return rc;
4132f351b2d6SSaeed Bishara }
4133f351b2d6SSaeed Bishara 
4134f351b2d6SSaeed Bishara /*
4135f351b2d6SSaeed Bishara  *
4136f351b2d6SSaeed Bishara  *      mv_platform_remove    -       unplug a platform interface
4137f351b2d6SSaeed Bishara  *      @pdev: platform device
4138f351b2d6SSaeed Bishara  *
4139f351b2d6SSaeed Bishara  *      A platform bus SATA device has been unplugged. Perform the needed
4140f351b2d6SSaeed Bishara  *      cleanup. Also called on module unload for any active devices.
4141f351b2d6SSaeed Bishara  */
4142f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev)
4143f351b2d6SSaeed Bishara {
4144d8661921SSergei Shtylyov 	struct ata_host *host = platform_get_drvdata(pdev);
4145c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4146c77a2f4eSSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
4147*eee98990SAndrew Lunn 	int port;
4148c77a2f4eSSaeed Bishara #endif
4149f351b2d6SSaeed Bishara 	ata_host_detach(host);
4150c77a2f4eSSaeed Bishara 
4151c77a2f4eSSaeed Bishara #if defined(CONFIG_HAVE_CLK)
4152c77a2f4eSSaeed Bishara 	if (!IS_ERR(hpriv->clk)) {
4153*eee98990SAndrew Lunn 		clk_disable_unprepare(hpriv->clk);
4154c77a2f4eSSaeed Bishara 		clk_put(hpriv->clk);
4155c77a2f4eSSaeed Bishara 	}
4156*eee98990SAndrew Lunn 	for (port = 0; port < host->n_ports; port++) {
4157*eee98990SAndrew Lunn 		if (!IS_ERR(hpriv->port_clks[port])) {
4158*eee98990SAndrew Lunn 			clk_disable_unprepare(hpriv->port_clks[port]);
4159*eee98990SAndrew Lunn 			clk_put(hpriv->port_clks[port]);
4160*eee98990SAndrew Lunn 		}
4161*eee98990SAndrew Lunn 	}
4162c77a2f4eSSaeed Bishara #endif
4163f351b2d6SSaeed Bishara 	return 0;
4164f351b2d6SSaeed Bishara }
4165f351b2d6SSaeed Bishara 
41666481f2b5SSaeed Bishara #ifdef CONFIG_PM
41676481f2b5SSaeed Bishara static int mv_platform_suspend(struct platform_device *pdev, pm_message_t state)
41686481f2b5SSaeed Bishara {
4169d8661921SSergei Shtylyov 	struct ata_host *host = platform_get_drvdata(pdev);
41706481f2b5SSaeed Bishara 	if (host)
41716481f2b5SSaeed Bishara 		return ata_host_suspend(host, state);
41726481f2b5SSaeed Bishara 	else
41736481f2b5SSaeed Bishara 		return 0;
41746481f2b5SSaeed Bishara }
41756481f2b5SSaeed Bishara 
41766481f2b5SSaeed Bishara static int mv_platform_resume(struct platform_device *pdev)
41776481f2b5SSaeed Bishara {
4178d8661921SSergei Shtylyov 	struct ata_host *host = platform_get_drvdata(pdev);
417963a9332bSAndrew Lunn 	const struct mbus_dram_target_info *dram;
41806481f2b5SSaeed Bishara 	int ret;
41816481f2b5SSaeed Bishara 
41826481f2b5SSaeed Bishara 	if (host) {
41836481f2b5SSaeed Bishara 		struct mv_host_priv *hpriv = host->private_data;
418463a9332bSAndrew Lunn 
41856481f2b5SSaeed Bishara 		/*
41866481f2b5SSaeed Bishara 		 * (Re-)program MBUS remapping windows if we are asked to.
41876481f2b5SSaeed Bishara 		 */
418863a9332bSAndrew Lunn 		dram = mv_mbus_dram_info();
418963a9332bSAndrew Lunn 		if (dram)
419063a9332bSAndrew Lunn 			mv_conf_mbus_windows(hpriv, dram);
41916481f2b5SSaeed Bishara 
41926481f2b5SSaeed Bishara 		/* initialize adapter */
41931bfeff03SSaeed Bishara 		ret = mv_init_host(host);
41946481f2b5SSaeed Bishara 		if (ret) {
41956481f2b5SSaeed Bishara 			printk(KERN_ERR DRV_NAME ": Error during HW init\n");
41966481f2b5SSaeed Bishara 			return ret;
41976481f2b5SSaeed Bishara 		}
41986481f2b5SSaeed Bishara 		ata_host_resume(host);
41996481f2b5SSaeed Bishara 	}
42006481f2b5SSaeed Bishara 
42016481f2b5SSaeed Bishara 	return 0;
42026481f2b5SSaeed Bishara }
42036481f2b5SSaeed Bishara #else
42046481f2b5SSaeed Bishara #define mv_platform_suspend NULL
42056481f2b5SSaeed Bishara #define mv_platform_resume NULL
42066481f2b5SSaeed Bishara #endif
42076481f2b5SSaeed Bishara 
4208f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
4209f351b2d6SSaeed Bishara 	.probe			= mv_platform_probe,
4210f351b2d6SSaeed Bishara 	.remove			= __devexit_p(mv_platform_remove),
42116481f2b5SSaeed Bishara 	.suspend		= mv_platform_suspend,
42126481f2b5SSaeed Bishara 	.resume			= mv_platform_resume,
4213f351b2d6SSaeed Bishara 	.driver			= {
4214f351b2d6SSaeed Bishara 				   .name = DRV_NAME,
4215f351b2d6SSaeed Bishara 				   .owner = THIS_MODULE,
4216f351b2d6SSaeed Bishara 				  },
4217f351b2d6SSaeed Bishara };
4218f351b2d6SSaeed Bishara 
4219f351b2d6SSaeed Bishara 
42207bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4221f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4222f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent);
4223b2dec48cSSaeed Bishara #ifdef CONFIG_PM
4224b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev);
4225b2dec48cSSaeed Bishara #endif
4226f351b2d6SSaeed Bishara 
42277bb3c529SSaeed Bishara 
42287bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
42297bb3c529SSaeed Bishara 	.name			= DRV_NAME,
42307bb3c529SSaeed Bishara 	.id_table		= mv_pci_tbl,
4231f351b2d6SSaeed Bishara 	.probe			= mv_pci_init_one,
42327bb3c529SSaeed Bishara 	.remove			= ata_pci_remove_one,
4233b2dec48cSSaeed Bishara #ifdef CONFIG_PM
4234b2dec48cSSaeed Bishara 	.suspend		= ata_pci_device_suspend,
4235b2dec48cSSaeed Bishara 	.resume			= mv_pci_device_resume,
4236b2dec48cSSaeed Bishara #endif
4237b2dec48cSSaeed Bishara 
42387bb3c529SSaeed Bishara };
42397bb3c529SSaeed Bishara 
42407bb3c529SSaeed Bishara /* move to PCI layer or libata core? */
42417bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev)
42427bb3c529SSaeed Bishara {
42437bb3c529SSaeed Bishara 	int rc;
42447bb3c529SSaeed Bishara 
42456a35528aSYang Hongyang 	if (!pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
42466a35528aSYang Hongyang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
42477bb3c529SSaeed Bishara 		if (rc) {
4248284901a9SYang Hongyang 			rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
42497bb3c529SSaeed Bishara 			if (rc) {
4250a44fec1fSJoe Perches 				dev_err(&pdev->dev,
42517bb3c529SSaeed Bishara 					"64-bit DMA enable failed\n");
42527bb3c529SSaeed Bishara 				return rc;
42537bb3c529SSaeed Bishara 			}
42547bb3c529SSaeed Bishara 		}
42557bb3c529SSaeed Bishara 	} else {
4256284901a9SYang Hongyang 		rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
42577bb3c529SSaeed Bishara 		if (rc) {
4258a44fec1fSJoe Perches 			dev_err(&pdev->dev, "32-bit DMA enable failed\n");
42597bb3c529SSaeed Bishara 			return rc;
42607bb3c529SSaeed Bishara 		}
4261284901a9SYang Hongyang 		rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
42627bb3c529SSaeed Bishara 		if (rc) {
4263a44fec1fSJoe Perches 			dev_err(&pdev->dev,
42647bb3c529SSaeed Bishara 				"32-bit consistent DMA enable failed\n");
42657bb3c529SSaeed Bishara 			return rc;
42667bb3c529SSaeed Bishara 		}
42677bb3c529SSaeed Bishara 	}
42687bb3c529SSaeed Bishara 
42697bb3c529SSaeed Bishara 	return rc;
42707bb3c529SSaeed Bishara }
42717bb3c529SSaeed Bishara 
4272c6fd2807SJeff Garzik /**
4273c6fd2807SJeff Garzik  *      mv_print_info - Dump key info to kernel log for perusal.
42744447d351STejun Heo  *      @host: ATA host to print info about
4275c6fd2807SJeff Garzik  *
4276c6fd2807SJeff Garzik  *      FIXME: complete this.
4277c6fd2807SJeff Garzik  *
4278c6fd2807SJeff Garzik  *      LOCKING:
4279c6fd2807SJeff Garzik  *      Inherited from caller.
4280c6fd2807SJeff Garzik  */
42814447d351STejun Heo static void mv_print_info(struct ata_host *host)
4282c6fd2807SJeff Garzik {
42834447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
42844447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
428544c10138SAuke Kok 	u8 scc;
4286c1e4fe71SJeff Garzik 	const char *scc_s, *gen;
4287c6fd2807SJeff Garzik 
4288c6fd2807SJeff Garzik 	/* Use this to determine the HW stepping of the chip so we know
4289c6fd2807SJeff Garzik 	 * what errata to workaround
4290c6fd2807SJeff Garzik 	 */
4291c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
4292c6fd2807SJeff Garzik 	if (scc == 0)
4293c6fd2807SJeff Garzik 		scc_s = "SCSI";
4294c6fd2807SJeff Garzik 	else if (scc == 0x01)
4295c6fd2807SJeff Garzik 		scc_s = "RAID";
4296c6fd2807SJeff Garzik 	else
4297c1e4fe71SJeff Garzik 		scc_s = "?";
4298c1e4fe71SJeff Garzik 
4299c1e4fe71SJeff Garzik 	if (IS_GEN_I(hpriv))
4300c1e4fe71SJeff Garzik 		gen = "I";
4301c1e4fe71SJeff Garzik 	else if (IS_GEN_II(hpriv))
4302c1e4fe71SJeff Garzik 		gen = "II";
4303c1e4fe71SJeff Garzik 	else if (IS_GEN_IIE(hpriv))
4304c1e4fe71SJeff Garzik 		gen = "IIE";
4305c1e4fe71SJeff Garzik 	else
4306c1e4fe71SJeff Garzik 		gen = "?";
4307c6fd2807SJeff Garzik 
4308a44fec1fSJoe Perches 	dev_info(&pdev->dev, "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
4309c1e4fe71SJeff Garzik 		 gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
4310c6fd2807SJeff Garzik 		 scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
4311c6fd2807SJeff Garzik }
4312c6fd2807SJeff Garzik 
4313c6fd2807SJeff Garzik /**
4314f351b2d6SSaeed Bishara  *      mv_pci_init_one - handle a positive probe of a PCI Marvell host
4315c6fd2807SJeff Garzik  *      @pdev: PCI device found
4316c6fd2807SJeff Garzik  *      @ent: PCI device ID entry for the matched host
4317c6fd2807SJeff Garzik  *
4318c6fd2807SJeff Garzik  *      LOCKING:
4319c6fd2807SJeff Garzik  *      Inherited from caller.
4320c6fd2807SJeff Garzik  */
4321f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4322f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent)
4323c6fd2807SJeff Garzik {
4324c6fd2807SJeff Garzik 	unsigned int board_idx = (unsigned int)ent->driver_data;
43254447d351STejun Heo 	const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
43264447d351STejun Heo 	struct ata_host *host;
43274447d351STejun Heo 	struct mv_host_priv *hpriv;
4328c4bc7d73SSaeed Bishara 	int n_ports, port, rc;
4329c6fd2807SJeff Garzik 
433006296a1eSJoe Perches 	ata_print_version_once(&pdev->dev, DRV_VERSION);
4331c6fd2807SJeff Garzik 
43324447d351STejun Heo 	/* allocate host */
43334447d351STejun Heo 	n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
43344447d351STejun Heo 
43354447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
43364447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
43374447d351STejun Heo 	if (!host || !hpriv)
43384447d351STejun Heo 		return -ENOMEM;
43394447d351STejun Heo 	host->private_data = hpriv;
4340f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
43411bfeff03SSaeed Bishara 	hpriv->board_idx = board_idx;
43424447d351STejun Heo 
43434447d351STejun Heo 	/* acquire resources */
434424dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
434524dc5f33STejun Heo 	if (rc)
4346c6fd2807SJeff Garzik 		return rc;
4347c6fd2807SJeff Garzik 
43480d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
43490d5ff566STejun Heo 	if (rc == -EBUSY)
435024dc5f33STejun Heo 		pcim_pin_device(pdev);
43510d5ff566STejun Heo 	if (rc)
435224dc5f33STejun Heo 		return rc;
43534447d351STejun Heo 	host->iomap = pcim_iomap_table(pdev);
4354f351b2d6SSaeed Bishara 	hpriv->base = host->iomap[MV_PRIMARY_BAR];
4355c6fd2807SJeff Garzik 
4356d88184fbSJeff Garzik 	rc = pci_go_64(pdev);
4357d88184fbSJeff Garzik 	if (rc)
4358d88184fbSJeff Garzik 		return rc;
4359d88184fbSJeff Garzik 
4360da2fa9baSMark Lord 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
4361da2fa9baSMark Lord 	if (rc)
4362da2fa9baSMark Lord 		return rc;
4363da2fa9baSMark Lord 
4364c4bc7d73SSaeed Bishara 	for (port = 0; port < host->n_ports; port++) {
4365c4bc7d73SSaeed Bishara 		struct ata_port *ap = host->ports[port];
4366c4bc7d73SSaeed Bishara 		void __iomem *port_mmio = mv_port_base(hpriv->base, port);
4367c4bc7d73SSaeed Bishara 		unsigned int offset = port_mmio - hpriv->base;
4368c4bc7d73SSaeed Bishara 
4369c4bc7d73SSaeed Bishara 		ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
4370c4bc7d73SSaeed Bishara 		ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
4371c4bc7d73SSaeed Bishara 	}
4372c4bc7d73SSaeed Bishara 
4373c6fd2807SJeff Garzik 	/* initialize adapter */
43741bfeff03SSaeed Bishara 	rc = mv_init_host(host);
437524dc5f33STejun Heo 	if (rc)
437624dc5f33STejun Heo 		return rc;
4377c6fd2807SJeff Garzik 
43786d3c30efSMark Lord 	/* Enable message-switched interrupts, if requested */
43796d3c30efSMark Lord 	if (msi && pci_enable_msi(pdev) == 0)
43806d3c30efSMark Lord 		hpriv->hp_flags |= MV_HP_FLAG_MSI;
4381c6fd2807SJeff Garzik 
4382c6fd2807SJeff Garzik 	mv_dump_pci_cfg(pdev, 0x68);
43834447d351STejun Heo 	mv_print_info(host);
4384c6fd2807SJeff Garzik 
43854447d351STejun Heo 	pci_set_master(pdev);
4386ea8b4db9SJeff Garzik 	pci_try_set_mwi(pdev);
43874447d351STejun Heo 	return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
4388c5d3e45aSJeff Garzik 				 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
4389c6fd2807SJeff Garzik }
4390b2dec48cSSaeed Bishara 
4391b2dec48cSSaeed Bishara #ifdef CONFIG_PM
4392b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev)
4393b2dec48cSSaeed Bishara {
4394d8661921SSergei Shtylyov 	struct ata_host *host = pci_get_drvdata(pdev);
4395b2dec48cSSaeed Bishara 	int rc;
4396b2dec48cSSaeed Bishara 
4397b2dec48cSSaeed Bishara 	rc = ata_pci_device_do_resume(pdev);
4398b2dec48cSSaeed Bishara 	if (rc)
4399b2dec48cSSaeed Bishara 		return rc;
4400b2dec48cSSaeed Bishara 
4401b2dec48cSSaeed Bishara 	/* initialize adapter */
4402b2dec48cSSaeed Bishara 	rc = mv_init_host(host);
4403b2dec48cSSaeed Bishara 	if (rc)
4404b2dec48cSSaeed Bishara 		return rc;
4405b2dec48cSSaeed Bishara 
4406b2dec48cSSaeed Bishara 	ata_host_resume(host);
4407b2dec48cSSaeed Bishara 
4408b2dec48cSSaeed Bishara 	return 0;
4409b2dec48cSSaeed Bishara }
4410b2dec48cSSaeed Bishara #endif
44117bb3c529SSaeed Bishara #endif
4412c6fd2807SJeff Garzik 
4413f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev);
4414f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev);
4415f351b2d6SSaeed Bishara 
4416c6fd2807SJeff Garzik static int __init mv_init(void)
4417c6fd2807SJeff Garzik {
44187bb3c529SSaeed Bishara 	int rc = -ENODEV;
44197bb3c529SSaeed Bishara #ifdef CONFIG_PCI
44207bb3c529SSaeed Bishara 	rc = pci_register_driver(&mv_pci_driver);
4421f351b2d6SSaeed Bishara 	if (rc < 0)
4422f351b2d6SSaeed Bishara 		return rc;
4423f351b2d6SSaeed Bishara #endif
4424f351b2d6SSaeed Bishara 	rc = platform_driver_register(&mv_platform_driver);
4425f351b2d6SSaeed Bishara 
4426f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
4427f351b2d6SSaeed Bishara 	if (rc < 0)
4428f351b2d6SSaeed Bishara 		pci_unregister_driver(&mv_pci_driver);
44297bb3c529SSaeed Bishara #endif
44307bb3c529SSaeed Bishara 	return rc;
4431c6fd2807SJeff Garzik }
4432c6fd2807SJeff Garzik 
4433c6fd2807SJeff Garzik static void __exit mv_exit(void)
4434c6fd2807SJeff Garzik {
44357bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4436c6fd2807SJeff Garzik 	pci_unregister_driver(&mv_pci_driver);
44377bb3c529SSaeed Bishara #endif
4438f351b2d6SSaeed Bishara 	platform_driver_unregister(&mv_platform_driver);
4439c6fd2807SJeff Garzik }
4440c6fd2807SJeff Garzik 
4441c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
4442c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
4443c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
4444c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
4445c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
444617c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
4447c6fd2807SJeff Garzik 
4448c6fd2807SJeff Garzik module_init(mv_init);
4449c6fd2807SJeff Garzik module_exit(mv_exit);
4450