xref: /openbmc/linux/drivers/ata/sata_mv.c (revision 5d0fb2e730e2085021cf5c8b6d14983e92aea75b)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  * sata_mv.c - Marvell SATA support
3c6fd2807SJeff Garzik  *
4e12bef50SMark Lord  * Copyright 2008: 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  *
8c6fd2807SJeff Garzik  * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
9c6fd2807SJeff Garzik  *
10c6fd2807SJeff Garzik  * This program is free software; you can redistribute it and/or modify
11c6fd2807SJeff Garzik  * it under the terms of the GNU General Public License as published by
12c6fd2807SJeff Garzik  * the Free Software Foundation; version 2 of the License.
13c6fd2807SJeff Garzik  *
14c6fd2807SJeff Garzik  * This program is distributed in the hope that it will be useful,
15c6fd2807SJeff Garzik  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16c6fd2807SJeff Garzik  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17c6fd2807SJeff Garzik  * GNU General Public License for more details.
18c6fd2807SJeff Garzik  *
19c6fd2807SJeff Garzik  * You should have received a copy of the GNU General Public License
20c6fd2807SJeff Garzik  * along with this program; if not, write to the Free Software
21c6fd2807SJeff Garzik  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22c6fd2807SJeff Garzik  *
23c6fd2807SJeff Garzik  */
24c6fd2807SJeff Garzik 
254a05e209SJeff Garzik /*
2685afb934SMark Lord  * sata_mv TODO list:
2785afb934SMark Lord  *
2885afb934SMark Lord  * --> Errata workaround for NCQ device errors.
2985afb934SMark Lord  *
3085afb934SMark Lord  * --> More errata workarounds for PCI-X.
3185afb934SMark Lord  *
3285afb934SMark Lord  * --> Complete a full errata audit for all chipsets to identify others.
3385afb934SMark Lord  *
3485afb934SMark Lord  * --> ATAPI support (Marvell claims the 60xx/70xx chips can do it).
3585afb934SMark Lord  *
3685afb934SMark Lord  * --> Investigate problems with PCI Message Signalled Interrupts (MSI).
3785afb934SMark Lord  *
3885afb934SMark Lord  * --> Develop a low-power-consumption strategy, and implement it.
3985afb934SMark Lord  *
4085afb934SMark Lord  * --> [Experiment, low priority] Investigate interrupt coalescing.
4185afb934SMark Lord  *       Quite often, especially with PCI Message Signalled Interrupts (MSI),
4285afb934SMark Lord  *       the overhead reduced by interrupt mitigation is quite often not
4385afb934SMark Lord  *       worth the latency cost.
4485afb934SMark Lord  *
4585afb934SMark Lord  * --> [Experiment, Marvell value added] Is it possible to use target
4685afb934SMark Lord  *       mode to cross-connect two Linux boxes with Marvell cards?  If so,
4785afb934SMark Lord  *       creating LibATA target mode support would be very interesting.
4885afb934SMark Lord  *
4985afb934SMark Lord  *       Target mode, for those without docs, is the ability to directly
5085afb934SMark Lord  *       connect two SATA ports.
514a05e209SJeff Garzik  */
524a05e209SJeff Garzik 
53c6fd2807SJeff Garzik #include <linux/kernel.h>
54c6fd2807SJeff Garzik #include <linux/module.h>
55c6fd2807SJeff Garzik #include <linux/pci.h>
56c6fd2807SJeff Garzik #include <linux/init.h>
57c6fd2807SJeff Garzik #include <linux/blkdev.h>
58c6fd2807SJeff Garzik #include <linux/delay.h>
59c6fd2807SJeff Garzik #include <linux/interrupt.h>
608d8b6004SAndrew Morton #include <linux/dmapool.h>
61c6fd2807SJeff Garzik #include <linux/dma-mapping.h>
62c6fd2807SJeff Garzik #include <linux/device.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>
67c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
68c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
696c08772eSJeff Garzik #include <scsi/scsi_device.h>
70c6fd2807SJeff Garzik #include <linux/libata.h>
71c6fd2807SJeff Garzik 
72c6fd2807SJeff Garzik #define DRV_NAME	"sata_mv"
730388a8c0SMark Lord #define DRV_VERSION	"1.24"
74c6fd2807SJeff Garzik 
75c6fd2807SJeff Garzik enum {
76c6fd2807SJeff Garzik 	/* BAR's are enumerated in terms of pci_resource_start() terms */
77c6fd2807SJeff Garzik 	MV_PRIMARY_BAR		= 0,	/* offset 0x10: memory space */
78c6fd2807SJeff Garzik 	MV_IO_BAR		= 2,	/* offset 0x18: IO space */
79c6fd2807SJeff Garzik 	MV_MISC_BAR		= 3,	/* offset 0x1c: FLASH, NVRAM, SRAM */
80c6fd2807SJeff Garzik 
81c6fd2807SJeff Garzik 	MV_MAJOR_REG_AREA_SZ	= 0x10000,	/* 64KB */
82c6fd2807SJeff Garzik 	MV_MINOR_REG_AREA_SZ	= 0x2000,	/* 8KB */
83c6fd2807SJeff Garzik 
84c6fd2807SJeff Garzik 	MV_PCI_REG_BASE		= 0,
85c6fd2807SJeff Garzik 	MV_IRQ_COAL_REG_BASE	= 0x18000,	/* 6xxx part only */
86c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE		= (MV_IRQ_COAL_REG_BASE + 0x08),
87c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE_LO		= (MV_IRQ_COAL_REG_BASE + 0x88),
88c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE_HI		= (MV_IRQ_COAL_REG_BASE + 0x8c),
89c6fd2807SJeff Garzik 	MV_IRQ_COAL_THRESHOLD		= (MV_IRQ_COAL_REG_BASE + 0xcc),
90c6fd2807SJeff Garzik 	MV_IRQ_COAL_TIME_THRESHOLD	= (MV_IRQ_COAL_REG_BASE + 0xd0),
91c6fd2807SJeff Garzik 
92c6fd2807SJeff Garzik 	MV_SATAHC0_REG_BASE	= 0x20000,
938e7decdbSMark Lord 	MV_FLASH_CTL_OFS	= 0x1046c,
948e7decdbSMark Lord 	MV_GPIO_PORT_CTL_OFS	= 0x104f0,
958e7decdbSMark Lord 	MV_RESET_CFG_OFS	= 0x180d8,
96c6fd2807SJeff Garzik 
97c6fd2807SJeff Garzik 	MV_PCI_REG_SZ		= MV_MAJOR_REG_AREA_SZ,
98c6fd2807SJeff Garzik 	MV_SATAHC_REG_SZ	= MV_MAJOR_REG_AREA_SZ,
99c6fd2807SJeff Garzik 	MV_SATAHC_ARBTR_REG_SZ	= MV_MINOR_REG_AREA_SZ,		/* arbiter */
100c6fd2807SJeff Garzik 	MV_PORT_REG_SZ		= MV_MINOR_REG_AREA_SZ,
101c6fd2807SJeff Garzik 
102c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH		= 32,
103c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH_MASK	= MV_MAX_Q_DEPTH - 1,
104c6fd2807SJeff Garzik 
105c6fd2807SJeff Garzik 	/* CRQB needs alignment on a 1KB boundary. Size == 1KB
106c6fd2807SJeff Garzik 	 * CRPB needs alignment on a 256B boundary. Size == 256B
107c6fd2807SJeff Garzik 	 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
108c6fd2807SJeff Garzik 	 */
109c6fd2807SJeff Garzik 	MV_CRQB_Q_SZ		= (32 * MV_MAX_Q_DEPTH),
110c6fd2807SJeff Garzik 	MV_CRPB_Q_SZ		= (8 * MV_MAX_Q_DEPTH),
111da2fa9baSMark Lord 	MV_MAX_SG_CT		= 256,
112c6fd2807SJeff Garzik 	MV_SG_TBL_SZ		= (16 * MV_MAX_SG_CT),
113c6fd2807SJeff Garzik 
114352fab70SMark Lord 	/* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
115c6fd2807SJeff Garzik 	MV_PORT_HC_SHIFT	= 2,
116352fab70SMark Lord 	MV_PORTS_PER_HC		= (1 << MV_PORT_HC_SHIFT), /* 4 */
117352fab70SMark Lord 	/* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
118352fab70SMark Lord 	MV_PORT_MASK		= (MV_PORTS_PER_HC - 1),   /* 3 */
119c6fd2807SJeff Garzik 
120c6fd2807SJeff Garzik 	/* Host Flags */
121c6fd2807SJeff Garzik 	MV_FLAG_DUAL_HC		= (1 << 30),  /* two SATA Host Controllers */
122c6fd2807SJeff Garzik 	MV_FLAG_IRQ_COALESCE	= (1 << 29),  /* IRQ coalescing capability */
1237bb3c529SSaeed Bishara 
124c5d3e45aSJeff Garzik 	MV_COMMON_FLAGS		= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
125bdd4dddeSJeff Garzik 				  ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
126bdd4dddeSJeff Garzik 				  ATA_FLAG_PIO_POLLING,
127ad3aef51SMark Lord 
128c6fd2807SJeff Garzik 	MV_6XXX_FLAGS		= MV_FLAG_IRQ_COALESCE,
129c6fd2807SJeff Garzik 
130ad3aef51SMark Lord 	MV_GENIIE_FLAGS		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
131ad3aef51SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
132c443c500SMark Lord 				  ATA_FLAG_NCQ | ATA_FLAG_AN,
133ad3aef51SMark Lord 
134c6fd2807SJeff Garzik 	CRQB_FLAG_READ		= (1 << 0),
135c6fd2807SJeff Garzik 	CRQB_TAG_SHIFT		= 1,
136c5d3e45aSJeff Garzik 	CRQB_IOID_SHIFT		= 6,	/* CRQB Gen-II/IIE IO Id shift */
137e12bef50SMark Lord 	CRQB_PMP_SHIFT		= 12,	/* CRQB Gen-II/IIE PMP shift */
138c5d3e45aSJeff Garzik 	CRQB_HOSTQ_SHIFT	= 17,	/* CRQB Gen-II/IIE HostQueTag shift */
139c6fd2807SJeff Garzik 	CRQB_CMD_ADDR_SHIFT	= 8,
140c6fd2807SJeff Garzik 	CRQB_CMD_CS		= (0x2 << 11),
141c6fd2807SJeff Garzik 	CRQB_CMD_LAST		= (1 << 15),
142c6fd2807SJeff Garzik 
143c6fd2807SJeff Garzik 	CRPB_FLAG_STATUS_SHIFT	= 8,
144c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_6	= 5,	/* CRPB Gen-II IO Id shift */
145c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_7	= 7,	/* CRPB Gen-IIE IO Id shift */
146c6fd2807SJeff Garzik 
147c6fd2807SJeff Garzik 	EPRD_FLAG_END_OF_TBL	= (1 << 31),
148c6fd2807SJeff Garzik 
149c6fd2807SJeff Garzik 	/* PCI interface registers */
150c6fd2807SJeff Garzik 
151c6fd2807SJeff Garzik 	PCI_COMMAND_OFS		= 0xc00,
1528e7decdbSMark Lord 	PCI_COMMAND_MRDTRIG	= (1 << 7),	/* PCI Master Read Trigger */
153c6fd2807SJeff Garzik 
154c6fd2807SJeff Garzik 	PCI_MAIN_CMD_STS_OFS	= 0xd30,
155c6fd2807SJeff Garzik 	STOP_PCI_MASTER		= (1 << 2),
156c6fd2807SJeff Garzik 	PCI_MASTER_EMPTY	= (1 << 3),
157c6fd2807SJeff Garzik 	GLOB_SFT_RST		= (1 << 4),
158c6fd2807SJeff Garzik 
1598e7decdbSMark Lord 	MV_PCI_MODE_OFS		= 0xd00,
1608e7decdbSMark Lord 	MV_PCI_MODE_MASK	= 0x30,
1618e7decdbSMark Lord 
162c6fd2807SJeff Garzik 	MV_PCI_EXP_ROM_BAR_CTL	= 0xd2c,
163c6fd2807SJeff Garzik 	MV_PCI_DISC_TIMER	= 0xd04,
164c6fd2807SJeff Garzik 	MV_PCI_MSI_TRIGGER	= 0xc38,
165c6fd2807SJeff Garzik 	MV_PCI_SERR_MASK	= 0xc28,
1668e7decdbSMark Lord 	MV_PCI_XBAR_TMOUT_OFS	= 0x1d04,
167c6fd2807SJeff Garzik 	MV_PCI_ERR_LOW_ADDRESS	= 0x1d40,
168c6fd2807SJeff Garzik 	MV_PCI_ERR_HIGH_ADDRESS	= 0x1d44,
169c6fd2807SJeff Garzik 	MV_PCI_ERR_ATTRIBUTE	= 0x1d48,
170c6fd2807SJeff Garzik 	MV_PCI_ERR_COMMAND	= 0x1d50,
171c6fd2807SJeff Garzik 
172c6fd2807SJeff Garzik 	PCI_IRQ_CAUSE_OFS	= 0x1d58,
173c6fd2807SJeff Garzik 	PCI_IRQ_MASK_OFS	= 0x1d5c,
174c6fd2807SJeff Garzik 	PCI_UNMASK_ALL_IRQS	= 0x7fffff,	/* bits 22-0 */
175c6fd2807SJeff Garzik 
17602a121daSMark Lord 	PCIE_IRQ_CAUSE_OFS	= 0x1900,
17702a121daSMark Lord 	PCIE_IRQ_MASK_OFS	= 0x1910,
178646a4da5SMark Lord 	PCIE_UNMASK_ALL_IRQS	= 0x40a,	/* assorted bits */
17902a121daSMark Lord 
1807368f919SMark Lord 	/* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
1817368f919SMark Lord 	PCI_HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
1827368f919SMark Lord 	PCI_HC_MAIN_IRQ_MASK_OFS  = 0x1d64,
1837368f919SMark Lord 	SOC_HC_MAIN_IRQ_CAUSE_OFS = 0x20020,
1847368f919SMark Lord 	SOC_HC_MAIN_IRQ_MASK_OFS  = 0x20024,
185352fab70SMark Lord 	ERR_IRQ			= (1 << 0),	/* shift by port # */
186352fab70SMark Lord 	DONE_IRQ		= (1 << 1),	/* shift by port # */
187c6fd2807SJeff Garzik 	HC0_IRQ_PEND		= 0x1ff,	/* bits 0-8 = HC0's ports */
188c6fd2807SJeff Garzik 	HC_SHIFT		= 9,		/* bits 9-17 = HC1's ports */
189c6fd2807SJeff Garzik 	PCI_ERR			= (1 << 18),
190c6fd2807SJeff Garzik 	TRAN_LO_DONE		= (1 << 19),	/* 6xxx: IRQ coalescing */
191c6fd2807SJeff Garzik 	TRAN_HI_DONE		= (1 << 20),	/* 6xxx: IRQ coalescing */
192fb621e2fSJeff Garzik 	PORTS_0_3_COAL_DONE	= (1 << 8),
193fb621e2fSJeff Garzik 	PORTS_4_7_COAL_DONE	= (1 << 17),
194c6fd2807SJeff Garzik 	PORTS_0_7_COAL_DONE	= (1 << 21),	/* 6xxx: IRQ coalescing */
195c6fd2807SJeff Garzik 	GPIO_INT		= (1 << 22),
196c6fd2807SJeff Garzik 	SELF_INT		= (1 << 23),
197c6fd2807SJeff Garzik 	TWSI_INT		= (1 << 24),
198c6fd2807SJeff Garzik 	HC_MAIN_RSVD		= (0x7f << 25),	/* bits 31-25 */
199fb621e2fSJeff Garzik 	HC_MAIN_RSVD_5		= (0x1fff << 19), /* bits 31-19 */
200f351b2d6SSaeed Bishara 	HC_MAIN_RSVD_SOC	= (0x3fffffb << 6),     /* bits 31-9, 7-6 */
201c6fd2807SJeff Garzik 
202c6fd2807SJeff Garzik 	/* SATAHC registers */
203c6fd2807SJeff Garzik 	HC_CFG_OFS		= 0,
204c6fd2807SJeff Garzik 
205c6fd2807SJeff Garzik 	HC_IRQ_CAUSE_OFS	= 0x14,
206352fab70SMark Lord 	DMA_IRQ			= (1 << 0),	/* shift by port # */
207352fab70SMark Lord 	HC_COAL_IRQ		= (1 << 4),	/* IRQ coalescing */
208c6fd2807SJeff Garzik 	DEV_IRQ			= (1 << 8),	/* shift by port # */
209c6fd2807SJeff Garzik 
210c6fd2807SJeff Garzik 	/* Shadow block registers */
211c6fd2807SJeff Garzik 	SHD_BLK_OFS		= 0x100,
212c6fd2807SJeff Garzik 	SHD_CTL_AST_OFS		= 0x20,		/* ofs from SHD_BLK_OFS */
213c6fd2807SJeff Garzik 
214c6fd2807SJeff Garzik 	/* SATA registers */
215c6fd2807SJeff Garzik 	SATA_STATUS_OFS		= 0x300,  /* ctrl, err regs follow status */
216c6fd2807SJeff Garzik 	SATA_ACTIVE_OFS		= 0x350,
2170c58912eSMark Lord 	SATA_FIS_IRQ_CAUSE_OFS	= 0x364,
218c443c500SMark Lord 	SATA_FIS_IRQ_AN		= (1 << 9),	/* async notification */
21917c5aab5SMark Lord 
220e12bef50SMark Lord 	LTMODE_OFS		= 0x30c,
22117c5aab5SMark Lord 	LTMODE_BIT8		= (1 << 8),	/* unknown, but necessary */
22217c5aab5SMark Lord 
223c6fd2807SJeff Garzik 	PHY_MODE3		= 0x310,
224c6fd2807SJeff Garzik 	PHY_MODE4		= 0x314,
225ba069e37SMark Lord 	PHY_MODE4_CFG_MASK	= 0x00000003,	/* phy internal config field */
226ba069e37SMark Lord 	PHY_MODE4_CFG_VALUE	= 0x00000001,	/* phy internal config field */
227ba069e37SMark Lord 	PHY_MODE4_RSVD_ZEROS	= 0x5de3fffa,	/* Gen2e always write zeros */
228ba069e37SMark Lord 	PHY_MODE4_RSVD_ONES	= 0x00000005,	/* Gen2e always write ones */
229ba069e37SMark Lord 
230c6fd2807SJeff Garzik 	PHY_MODE2		= 0x330,
231e12bef50SMark Lord 	SATA_IFCTL_OFS		= 0x344,
2328e7decdbSMark Lord 	SATA_TESTCTL_OFS	= 0x348,
233e12bef50SMark Lord 	SATA_IFSTAT_OFS		= 0x34c,
234e12bef50SMark Lord 	VENDOR_UNIQUE_FIS_OFS	= 0x35c,
23517c5aab5SMark Lord 
2368e7decdbSMark Lord 	FISCFG_OFS		= 0x360,
2378e7decdbSMark Lord 	FISCFG_WAIT_DEV_ERR	= (1 << 8),	/* wait for host on DevErr */
2388e7decdbSMark Lord 	FISCFG_SINGLE_SYNC	= (1 << 16),	/* SYNC on DMA activation */
23917c5aab5SMark Lord 
240c6fd2807SJeff Garzik 	MV5_PHY_MODE		= 0x74,
2418e7decdbSMark Lord 	MV5_LTMODE_OFS		= 0x30,
2428e7decdbSMark Lord 	MV5_PHY_CTL_OFS		= 0x0C,
2438e7decdbSMark Lord 	SATA_INTERFACE_CFG_OFS	= 0x050,
244c6fd2807SJeff Garzik 
245c6fd2807SJeff Garzik 	MV_M2_PREAMP_MASK	= 0x7e0,
246c6fd2807SJeff Garzik 
247c6fd2807SJeff Garzik 	/* Port registers */
248c6fd2807SJeff Garzik 	EDMA_CFG_OFS		= 0,
2490c58912eSMark Lord 	EDMA_CFG_Q_DEPTH	= 0x1f,		/* max device queue depth */
2500c58912eSMark Lord 	EDMA_CFG_NCQ		= (1 << 5),	/* for R/W FPDMA queued */
251c6fd2807SJeff Garzik 	EDMA_CFG_NCQ_GO_ON_ERR	= (1 << 14),	/* continue on error */
252c6fd2807SJeff Garzik 	EDMA_CFG_RD_BRST_EXT	= (1 << 11),	/* read burst 512B */
253c6fd2807SJeff Garzik 	EDMA_CFG_WR_BUFF_LEN	= (1 << 13),	/* write buffer 512B */
254e12bef50SMark Lord 	EDMA_CFG_EDMA_FBS	= (1 << 16),	/* EDMA FIS-Based Switching */
255e12bef50SMark Lord 	EDMA_CFG_FBS		= (1 << 26),	/* FIS-Based Switching */
256c6fd2807SJeff Garzik 
257c6fd2807SJeff Garzik 	EDMA_ERR_IRQ_CAUSE_OFS	= 0x8,
258c6fd2807SJeff Garzik 	EDMA_ERR_IRQ_MASK_OFS	= 0xc,
2596c1153e0SJeff Garzik 	EDMA_ERR_D_PAR		= (1 << 0),	/* UDMA data parity err */
2606c1153e0SJeff Garzik 	EDMA_ERR_PRD_PAR	= (1 << 1),	/* UDMA PRD parity err */
2616c1153e0SJeff Garzik 	EDMA_ERR_DEV		= (1 << 2),	/* device error */
2626c1153e0SJeff Garzik 	EDMA_ERR_DEV_DCON	= (1 << 3),	/* device disconnect */
2636c1153e0SJeff Garzik 	EDMA_ERR_DEV_CON	= (1 << 4),	/* device connected */
2646c1153e0SJeff Garzik 	EDMA_ERR_SERR		= (1 << 5),	/* SError bits [WBDST] raised */
265c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS	= (1 << 7),	/* Gen II/IIE self-disable */
266c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS_5	= (1 << 8),	/* Gen I self-disable */
2676c1153e0SJeff Garzik 	EDMA_ERR_BIST_ASYNC	= (1 << 8),	/* BIST FIS or Async Notify */
268c5d3e45aSJeff Garzik 	EDMA_ERR_TRANS_IRQ_7	= (1 << 8),	/* Gen IIE transprt layer irq */
2696c1153e0SJeff Garzik 	EDMA_ERR_CRQB_PAR	= (1 << 9),	/* CRQB parity error */
2706c1153e0SJeff Garzik 	EDMA_ERR_CRPB_PAR	= (1 << 10),	/* CRPB parity error */
2716c1153e0SJeff Garzik 	EDMA_ERR_INTRL_PAR	= (1 << 11),	/* internal parity error */
2726c1153e0SJeff Garzik 	EDMA_ERR_IORDY		= (1 << 12),	/* IORdy timeout */
273646a4da5SMark Lord 
2746c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_RX	= (0xf << 13),	/* link ctrl rx error */
275646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_0	= (1 << 13),	/* transient: CRC err */
276646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_1	= (1 << 14),	/* transient: FIFO err */
277646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_2	= (1 << 15),	/* fatal: caught SYNC */
278646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_3	= (1 << 16),	/* transient: FIS rx err */
279646a4da5SMark Lord 
2806c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_RX	= (0xf << 17),	/* link data rx error */
281646a4da5SMark Lord 
2826c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_TX	= (0x1f << 21),	/* link ctrl tx error */
283646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_0	= (1 << 21),	/* transient: CRC err */
284646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_1	= (1 << 22),	/* transient: FIFO err */
285646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_2	= (1 << 23),	/* transient: caught SYNC */
286646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_3	= (1 << 24),	/* transient: caught DMAT */
287646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_4	= (1 << 25),	/* transient: FIS collision */
288646a4da5SMark Lord 
2896c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_TX	= (0x1f << 26),	/* link data tx error */
290646a4da5SMark Lord 
2916c1153e0SJeff Garzik 	EDMA_ERR_TRANS_PROTO	= (1 << 31),	/* transport protocol error */
292c5d3e45aSJeff Garzik 	EDMA_ERR_OVERRUN_5	= (1 << 5),
293c5d3e45aSJeff Garzik 	EDMA_ERR_UNDERRUN_5	= (1 << 6),
294646a4da5SMark Lord 
295646a4da5SMark Lord 	EDMA_ERR_IRQ_TRANSIENT  = EDMA_ERR_LNK_CTRL_RX_0 |
296646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_1 |
297646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_3 |
29885afb934SMark Lord 				  EDMA_ERR_LNK_CTRL_TX,
299646a4da5SMark Lord 
300bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE		= EDMA_ERR_D_PAR |
301bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
302bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
303bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
304bdd4dddeSJeff Garzik 				  EDMA_ERR_SERR |
305bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS |
3066c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
307bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
308bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
309bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY |
310bdd4dddeSJeff Garzik 				  EDMA_ERR_LNK_CTRL_RX_2 |
311c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_RX |
312c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_TX |
313bdd4dddeSJeff Garzik 				  EDMA_ERR_TRANS_PROTO,
314e12bef50SMark Lord 
315bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE_5	= EDMA_ERR_D_PAR |
316bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
317bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
318bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
319bdd4dddeSJeff Garzik 				  EDMA_ERR_OVERRUN_5 |
320bdd4dddeSJeff Garzik 				  EDMA_ERR_UNDERRUN_5 |
321bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS_5 |
3226c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
323bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
324bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
325bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY,
326c6fd2807SJeff Garzik 
327c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_HI_OFS	= 0x10,
328c6fd2807SJeff Garzik 	EDMA_REQ_Q_IN_PTR_OFS	= 0x14,		/* also contains BASE_LO */
329c6fd2807SJeff Garzik 
330c6fd2807SJeff Garzik 	EDMA_REQ_Q_OUT_PTR_OFS	= 0x18,
331c6fd2807SJeff Garzik 	EDMA_REQ_Q_PTR_SHIFT	= 5,
332c6fd2807SJeff Garzik 
333c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_HI_OFS	= 0x1c,
334c6fd2807SJeff Garzik 	EDMA_RSP_Q_IN_PTR_OFS	= 0x20,
335c6fd2807SJeff Garzik 	EDMA_RSP_Q_OUT_PTR_OFS	= 0x24,		/* also contains BASE_LO */
336c6fd2807SJeff Garzik 	EDMA_RSP_Q_PTR_SHIFT	= 3,
337c6fd2807SJeff Garzik 
3380ea9e179SJeff Garzik 	EDMA_CMD_OFS		= 0x28,		/* EDMA command register */
3390ea9e179SJeff Garzik 	EDMA_EN			= (1 << 0),	/* enable EDMA */
3400ea9e179SJeff Garzik 	EDMA_DS			= (1 << 1),	/* disable EDMA; self-negated */
3418e7decdbSMark Lord 	EDMA_RESET		= (1 << 2),	/* reset eng/trans/link/phy */
342c6fd2807SJeff Garzik 
3438e7decdbSMark Lord 	EDMA_STATUS_OFS		= 0x30,		/* EDMA engine status */
3448e7decdbSMark Lord 	EDMA_STATUS_CACHE_EMPTY	= (1 << 6),	/* GenIIe command cache empty */
3458e7decdbSMark Lord 	EDMA_STATUS_IDLE	= (1 << 7),	/* GenIIe EDMA enabled/idle */
3468e7decdbSMark Lord 
3478e7decdbSMark Lord 	EDMA_IORDY_TMOUT_OFS	= 0x34,
3488e7decdbSMark Lord 	EDMA_ARB_CFG_OFS	= 0x38,
3498e7decdbSMark Lord 
3508e7decdbSMark Lord 	EDMA_HALTCOND_OFS	= 0x60,		/* GenIIe halt conditions */
351c6fd2807SJeff Garzik 
352c6fd2807SJeff Garzik 	/* Host private flags (hp_flags) */
353c6fd2807SJeff Garzik 	MV_HP_FLAG_MSI		= (1 << 0),
354c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB0	= (1 << 1),
355c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB2	= (1 << 2),
356c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1B2	= (1 << 3),
357c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1C0	= (1 << 4),
3580ea9e179SJeff Garzik 	MV_HP_GEN_I		= (1 << 6),	/* Generation I: 50xx */
3590ea9e179SJeff Garzik 	MV_HP_GEN_II		= (1 << 7),	/* Generation II: 60xx */
3600ea9e179SJeff Garzik 	MV_HP_GEN_IIE		= (1 << 8),	/* Generation IIE: 6042/7042 */
36102a121daSMark Lord 	MV_HP_PCIE		= (1 << 9),	/* PCIe bus/regs: 7042 */
362616d4a98SMark Lord 	MV_HP_CUT_THROUGH	= (1 << 10),	/* can use EDMA cut-through */
3631f398472SMark Lord 	MV_HP_FLAG_SOC		= (1 << 11),	/* SystemOnChip, no PCI */
364c6fd2807SJeff Garzik 
365c6fd2807SJeff Garzik 	/* Port private flags (pp_flags) */
3660ea9e179SJeff Garzik 	MV_PP_FLAG_EDMA_EN	= (1 << 0),	/* is EDMA engine enabled? */
36772109168SMark Lord 	MV_PP_FLAG_NCQ_EN	= (1 << 1),	/* is EDMA set up for NCQ? */
36800f42eabSMark Lord 	MV_PP_FLAG_FBS_EN	= (1 << 2),	/* is EDMA set up for FBS? */
36929d187bbSMark Lord 	MV_PP_FLAG_DELAYED_EH	= (1 << 3),	/* delayed dev err handling */
370c6fd2807SJeff Garzik };
371c6fd2807SJeff Garzik 
372ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
373ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
374c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
3758e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
3761f398472SMark Lord #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
377c6fd2807SJeff Garzik 
37815a32632SLennert Buytenhek #define WINDOW_CTRL(i)		(0x20030 + ((i) << 4))
37915a32632SLennert Buytenhek #define WINDOW_BASE(i)		(0x20034 + ((i) << 4))
38015a32632SLennert Buytenhek 
381c6fd2807SJeff Garzik enum {
382baf14aa1SJeff Garzik 	/* DMA boundary 0xffff is required by the s/g splitting
383baf14aa1SJeff Garzik 	 * we need on /length/ in mv_fill-sg().
384baf14aa1SJeff Garzik 	 */
385baf14aa1SJeff Garzik 	MV_DMA_BOUNDARY		= 0xffffU,
386c6fd2807SJeff Garzik 
3870ea9e179SJeff Garzik 	/* mask of register bits containing lower 32 bits
3880ea9e179SJeff Garzik 	 * of EDMA request queue DMA address
3890ea9e179SJeff Garzik 	 */
390c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_LO_MASK	= 0xfffffc00U,
391c6fd2807SJeff Garzik 
3920ea9e179SJeff Garzik 	/* ditto, for response queue */
393c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_LO_MASK	= 0xffffff00U,
394c6fd2807SJeff Garzik };
395c6fd2807SJeff Garzik 
396c6fd2807SJeff Garzik enum chip_type {
397c6fd2807SJeff Garzik 	chip_504x,
398c6fd2807SJeff Garzik 	chip_508x,
399c6fd2807SJeff Garzik 	chip_5080,
400c6fd2807SJeff Garzik 	chip_604x,
401c6fd2807SJeff Garzik 	chip_608x,
402c6fd2807SJeff Garzik 	chip_6042,
403c6fd2807SJeff Garzik 	chip_7042,
404f351b2d6SSaeed Bishara 	chip_soc,
405c6fd2807SJeff Garzik };
406c6fd2807SJeff Garzik 
407c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
408c6fd2807SJeff Garzik struct mv_crqb {
409c6fd2807SJeff Garzik 	__le32			sg_addr;
410c6fd2807SJeff Garzik 	__le32			sg_addr_hi;
411c6fd2807SJeff Garzik 	__le16			ctrl_flags;
412c6fd2807SJeff Garzik 	__le16			ata_cmd[11];
413c6fd2807SJeff Garzik };
414c6fd2807SJeff Garzik 
415c6fd2807SJeff Garzik struct mv_crqb_iie {
416c6fd2807SJeff Garzik 	__le32			addr;
417c6fd2807SJeff Garzik 	__le32			addr_hi;
418c6fd2807SJeff Garzik 	__le32			flags;
419c6fd2807SJeff Garzik 	__le32			len;
420c6fd2807SJeff Garzik 	__le32			ata_cmd[4];
421c6fd2807SJeff Garzik };
422c6fd2807SJeff Garzik 
423c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
424c6fd2807SJeff Garzik struct mv_crpb {
425c6fd2807SJeff Garzik 	__le16			id;
426c6fd2807SJeff Garzik 	__le16			flags;
427c6fd2807SJeff Garzik 	__le32			tmstmp;
428c6fd2807SJeff Garzik };
429c6fd2807SJeff Garzik 
430c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
431c6fd2807SJeff Garzik struct mv_sg {
432c6fd2807SJeff Garzik 	__le32			addr;
433c6fd2807SJeff Garzik 	__le32			flags_size;
434c6fd2807SJeff Garzik 	__le32			addr_hi;
435c6fd2807SJeff Garzik 	__le32			reserved;
436c6fd2807SJeff Garzik };
437c6fd2807SJeff Garzik 
438c6fd2807SJeff Garzik struct mv_port_priv {
439c6fd2807SJeff Garzik 	struct mv_crqb		*crqb;
440c6fd2807SJeff Garzik 	dma_addr_t		crqb_dma;
441c6fd2807SJeff Garzik 	struct mv_crpb		*crpb;
442c6fd2807SJeff Garzik 	dma_addr_t		crpb_dma;
443eb73d558SMark Lord 	struct mv_sg		*sg_tbl[MV_MAX_Q_DEPTH];
444eb73d558SMark Lord 	dma_addr_t		sg_tbl_dma[MV_MAX_Q_DEPTH];
445bdd4dddeSJeff Garzik 
446bdd4dddeSJeff Garzik 	unsigned int		req_idx;
447bdd4dddeSJeff Garzik 	unsigned int		resp_idx;
448bdd4dddeSJeff Garzik 
449c6fd2807SJeff Garzik 	u32			pp_flags;
45029d187bbSMark Lord 	unsigned int		delayed_eh_pmp_map;
451c6fd2807SJeff Garzik };
452c6fd2807SJeff Garzik 
453c6fd2807SJeff Garzik struct mv_port_signal {
454c6fd2807SJeff Garzik 	u32			amps;
455c6fd2807SJeff Garzik 	u32			pre;
456c6fd2807SJeff Garzik };
457c6fd2807SJeff Garzik 
45802a121daSMark Lord struct mv_host_priv {
45902a121daSMark Lord 	u32			hp_flags;
46096e2c487SMark Lord 	u32			main_irq_mask;
46102a121daSMark Lord 	struct mv_port_signal	signal[8];
46202a121daSMark Lord 	const struct mv_hw_ops	*ops;
463f351b2d6SSaeed Bishara 	int			n_ports;
464f351b2d6SSaeed Bishara 	void __iomem		*base;
4657368f919SMark Lord 	void __iomem		*main_irq_cause_addr;
4667368f919SMark Lord 	void __iomem		*main_irq_mask_addr;
46702a121daSMark Lord 	u32			irq_cause_ofs;
46802a121daSMark Lord 	u32			irq_mask_ofs;
46902a121daSMark Lord 	u32			unmask_all_irqs;
470da2fa9baSMark Lord 	/*
471da2fa9baSMark Lord 	 * These consistent DMA memory pools give us guaranteed
472da2fa9baSMark Lord 	 * alignment for hardware-accessed data structures,
473da2fa9baSMark Lord 	 * and less memory waste in accomplishing the alignment.
474da2fa9baSMark Lord 	 */
475da2fa9baSMark Lord 	struct dma_pool		*crqb_pool;
476da2fa9baSMark Lord 	struct dma_pool		*crpb_pool;
477da2fa9baSMark Lord 	struct dma_pool		*sg_tbl_pool;
47802a121daSMark Lord };
47902a121daSMark Lord 
480c6fd2807SJeff Garzik struct mv_hw_ops {
481c6fd2807SJeff Garzik 	void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
482c6fd2807SJeff Garzik 			   unsigned int port);
483c6fd2807SJeff Garzik 	void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
484c6fd2807SJeff Garzik 	void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
485c6fd2807SJeff Garzik 			   void __iomem *mmio);
486c6fd2807SJeff Garzik 	int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
487c6fd2807SJeff Garzik 			unsigned int n_hc);
488c6fd2807SJeff Garzik 	void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
4897bb3c529SSaeed Bishara 	void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
490c6fd2807SJeff Garzik };
491c6fd2807SJeff Garzik 
49282ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
49382ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
49482ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
49582ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
496c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap);
497c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap);
4983e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc);
499c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc);
500c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
501c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
502a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
503a1efdabaSTejun Heo 			unsigned long deadline);
504bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap);
505bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap);
506f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev);
507c6fd2807SJeff Garzik 
508c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
509c6fd2807SJeff Garzik 			   unsigned int port);
510c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
511c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
512c6fd2807SJeff Garzik 			   void __iomem *mmio);
513c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
514c6fd2807SJeff Garzik 			unsigned int n_hc);
515c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
5167bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
517c6fd2807SJeff Garzik 
518c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
519c6fd2807SJeff Garzik 			   unsigned int port);
520c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
521c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
522c6fd2807SJeff Garzik 			   void __iomem *mmio);
523c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
524c6fd2807SJeff Garzik 			unsigned int n_hc);
525c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
526f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
527f351b2d6SSaeed Bishara 				      void __iomem *mmio);
528f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
529f351b2d6SSaeed Bishara 				      void __iomem *mmio);
530f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
531f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc);
532f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
533f351b2d6SSaeed Bishara 				      void __iomem *mmio);
534f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
5357bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
536e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
537c6fd2807SJeff Garzik 			     unsigned int port_no);
538e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap);
539b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio);
540e12bef50SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq);
541c6fd2807SJeff Garzik 
542e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp);
543e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
544e49856d8SMark Lord 				unsigned long deadline);
545e49856d8SMark Lord static int  mv_softreset(struct ata_link *link, unsigned int *class,
546e49856d8SMark Lord 				unsigned long deadline);
54729d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap);
5484c299ca3SMark Lord static void mv_process_crpb_entries(struct ata_port *ap,
5494c299ca3SMark Lord 					struct mv_port_priv *pp);
550c6fd2807SJeff Garzik 
551eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
552eb73d558SMark Lord  * because we have to allow room for worst case splitting of
553eb73d558SMark Lord  * PRDs for 64K boundaries in mv_fill_sg().
554eb73d558SMark Lord  */
555c5d3e45aSJeff Garzik static struct scsi_host_template mv5_sht = {
55668d1d07bSTejun Heo 	ATA_BASE_SHT(DRV_NAME),
557baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
558c5d3e45aSJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
559c5d3e45aSJeff Garzik };
560c5d3e45aSJeff Garzik 
561c5d3e45aSJeff Garzik static struct scsi_host_template mv6_sht = {
56268d1d07bSTejun Heo 	ATA_NCQ_SHT(DRV_NAME),
563138bfdd0SMark Lord 	.can_queue		= MV_MAX_Q_DEPTH - 1,
564baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
565c6fd2807SJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
566c6fd2807SJeff Garzik };
567c6fd2807SJeff Garzik 
568029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = {
569029cfd6bSTejun Heo 	.inherits		= &ata_sff_port_ops,
570c6fd2807SJeff Garzik 
5713e4a1391SMark Lord 	.qc_defer		= mv_qc_defer,
572c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep,
573c6fd2807SJeff Garzik 	.qc_issue		= mv_qc_issue,
574c6fd2807SJeff Garzik 
575bdd4dddeSJeff Garzik 	.freeze			= mv_eh_freeze,
576bdd4dddeSJeff Garzik 	.thaw			= mv_eh_thaw,
577a1efdabaSTejun Heo 	.hardreset		= mv_hardreset,
578a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler, /* avoid SFF EH */
579029cfd6bSTejun Heo 	.post_internal_cmd	= ATA_OP_NULL,
580bdd4dddeSJeff Garzik 
581c6fd2807SJeff Garzik 	.scr_read		= mv5_scr_read,
582c6fd2807SJeff Garzik 	.scr_write		= mv5_scr_write,
583c6fd2807SJeff Garzik 
584c6fd2807SJeff Garzik 	.port_start		= mv_port_start,
585c6fd2807SJeff Garzik 	.port_stop		= mv_port_stop,
586c6fd2807SJeff Garzik };
587c6fd2807SJeff Garzik 
588029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
589029cfd6bSTejun Heo 	.inherits		= &mv5_ops,
590f273827eSMark Lord 	.dev_config             = mv6_dev_config,
591c6fd2807SJeff Garzik 	.scr_read		= mv_scr_read,
592c6fd2807SJeff Garzik 	.scr_write		= mv_scr_write,
593c6fd2807SJeff Garzik 
594e49856d8SMark Lord 	.pmp_hardreset		= mv_pmp_hardreset,
595e49856d8SMark Lord 	.pmp_softreset		= mv_softreset,
596e49856d8SMark Lord 	.softreset		= mv_softreset,
59729d187bbSMark Lord 	.error_handler		= mv_pmp_error_handler,
598c6fd2807SJeff Garzik };
599c6fd2807SJeff Garzik 
600029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
601029cfd6bSTejun Heo 	.inherits		= &mv6_ops,
602029cfd6bSTejun Heo 	.dev_config		= ATA_OP_NULL,
603c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep_iie,
604c6fd2807SJeff Garzik };
605c6fd2807SJeff Garzik 
606c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
607c6fd2807SJeff Garzik 	{  /* chip_504x */
608cca3974eSJeff Garzik 		.flags		= MV_COMMON_FLAGS,
609c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
610bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
611c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
612c6fd2807SJeff Garzik 	},
613c6fd2807SJeff Garzik 	{  /* chip_508x */
614c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
615c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
616bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
617c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
618c6fd2807SJeff Garzik 	},
619c6fd2807SJeff Garzik 	{  /* chip_5080 */
620c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
621c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
622bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
623c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
624c6fd2807SJeff Garzik 	},
625c6fd2807SJeff Garzik 	{  /* chip_604x */
626138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
627e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
628138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
629c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
630bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
631c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
632c6fd2807SJeff Garzik 	},
633c6fd2807SJeff Garzik 	{  /* chip_608x */
634c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
635e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
636138bfdd0SMark Lord 				  ATA_FLAG_NCQ | MV_FLAG_DUAL_HC,
637c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
638bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
639c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
640c6fd2807SJeff Garzik 	},
641c6fd2807SJeff Garzik 	{  /* chip_6042 */
642ad3aef51SMark Lord 		.flags		= MV_GENIIE_FLAGS,
643c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
644bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
645c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
646c6fd2807SJeff Garzik 	},
647c6fd2807SJeff Garzik 	{  /* chip_7042 */
648ad3aef51SMark Lord 		.flags		= MV_GENIIE_FLAGS,
649c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
650bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
651c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
652c6fd2807SJeff Garzik 	},
653f351b2d6SSaeed Bishara 	{  /* chip_soc */
6541f398472SMark Lord 		.flags		= MV_GENIIE_FLAGS,
655f351b2d6SSaeed Bishara 		.pio_mask	= 0x1f,	/* pio0-4 */
656f351b2d6SSaeed Bishara 		.udma_mask	= ATA_UDMA6,
657f351b2d6SSaeed Bishara 		.port_ops	= &mv_iie_ops,
658f351b2d6SSaeed Bishara 	},
659c6fd2807SJeff Garzik };
660c6fd2807SJeff Garzik 
661c6fd2807SJeff Garzik static const struct pci_device_id mv_pci_tbl[] = {
6622d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
6632d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
6642d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
6652d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
66646c5784cSMark Lord 	/* RocketRAID 1720/174x have different identifiers */
66746c5784cSMark Lord 	{ PCI_VDEVICE(TTI, 0x1720), chip_6042 },
668cfbf723eSAlan Cox 	{ PCI_VDEVICE(TTI, 0x1740), chip_508x },
669cfbf723eSAlan Cox 	{ PCI_VDEVICE(TTI, 0x1742), chip_508x },
670c6fd2807SJeff Garzik 
6712d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
6722d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
6732d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
6742d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
6752d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
676c6fd2807SJeff Garzik 
6772d2744fcSJeff Garzik 	{ PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
6782d2744fcSJeff Garzik 
679d9f9c6bcSFlorian Attenberger 	/* Adaptec 1430SA */
680d9f9c6bcSFlorian Attenberger 	{ PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
681d9f9c6bcSFlorian Attenberger 
68202a121daSMark Lord 	/* Marvell 7042 support */
6836a3d586dSMorrison, Tom 	{ PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
6846a3d586dSMorrison, Tom 
68502a121daSMark Lord 	/* Highpoint RocketRAID PCIe series */
68602a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2300), chip_7042 },
68702a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2310), chip_7042 },
68802a121daSMark Lord 
689c6fd2807SJeff Garzik 	{ }			/* terminate list */
690c6fd2807SJeff Garzik };
691c6fd2807SJeff Garzik 
692c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
693c6fd2807SJeff Garzik 	.phy_errata		= mv5_phy_errata,
694c6fd2807SJeff Garzik 	.enable_leds		= mv5_enable_leds,
695c6fd2807SJeff Garzik 	.read_preamp		= mv5_read_preamp,
696c6fd2807SJeff Garzik 	.reset_hc		= mv5_reset_hc,
697c6fd2807SJeff Garzik 	.reset_flash		= mv5_reset_flash,
698c6fd2807SJeff Garzik 	.reset_bus		= mv5_reset_bus,
699c6fd2807SJeff Garzik };
700c6fd2807SJeff Garzik 
701c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
702c6fd2807SJeff Garzik 	.phy_errata		= mv6_phy_errata,
703c6fd2807SJeff Garzik 	.enable_leds		= mv6_enable_leds,
704c6fd2807SJeff Garzik 	.read_preamp		= mv6_read_preamp,
705c6fd2807SJeff Garzik 	.reset_hc		= mv6_reset_hc,
706c6fd2807SJeff Garzik 	.reset_flash		= mv6_reset_flash,
707c6fd2807SJeff Garzik 	.reset_bus		= mv_reset_pci_bus,
708c6fd2807SJeff Garzik };
709c6fd2807SJeff Garzik 
710f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
711f351b2d6SSaeed Bishara 	.phy_errata		= mv6_phy_errata,
712f351b2d6SSaeed Bishara 	.enable_leds		= mv_soc_enable_leds,
713f351b2d6SSaeed Bishara 	.read_preamp		= mv_soc_read_preamp,
714f351b2d6SSaeed Bishara 	.reset_hc		= mv_soc_reset_hc,
715f351b2d6SSaeed Bishara 	.reset_flash		= mv_soc_reset_flash,
716f351b2d6SSaeed Bishara 	.reset_bus		= mv_soc_reset_bus,
717f351b2d6SSaeed Bishara };
718f351b2d6SSaeed Bishara 
719c6fd2807SJeff Garzik /*
720c6fd2807SJeff Garzik  * Functions
721c6fd2807SJeff Garzik  */
722c6fd2807SJeff Garzik 
723c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
724c6fd2807SJeff Garzik {
725c6fd2807SJeff Garzik 	writel(data, addr);
726c6fd2807SJeff Garzik 	(void) readl(addr);	/* flush to avoid PCI posted write */
727c6fd2807SJeff Garzik }
728c6fd2807SJeff Garzik 
729c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
730c6fd2807SJeff Garzik {
731c6fd2807SJeff Garzik 	return port >> MV_PORT_HC_SHIFT;
732c6fd2807SJeff Garzik }
733c6fd2807SJeff Garzik 
734c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
735c6fd2807SJeff Garzik {
736c6fd2807SJeff Garzik 	return port & MV_PORT_MASK;
737c6fd2807SJeff Garzik }
738c6fd2807SJeff Garzik 
7391cfd19aeSMark Lord /*
7401cfd19aeSMark Lord  * Consolidate some rather tricky bit shift calculations.
7411cfd19aeSMark Lord  * This is hot-path stuff, so not a function.
7421cfd19aeSMark Lord  * Simple code, with two return values, so macro rather than inline.
7431cfd19aeSMark Lord  *
7441cfd19aeSMark Lord  * port is the sole input, in range 0..7.
7457368f919SMark Lord  * shift is one output, for use with main_irq_cause / main_irq_mask registers.
7467368f919SMark Lord  * hardport is the other output, in range 0..3.
7471cfd19aeSMark Lord  *
7481cfd19aeSMark Lord  * Note that port and hardport may be the same variable in some cases.
7491cfd19aeSMark Lord  */
7501cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport)	\
7511cfd19aeSMark Lord {								\
7521cfd19aeSMark Lord 	shift    = mv_hc_from_port(port) * HC_SHIFT;		\
7531cfd19aeSMark Lord 	hardport = mv_hardport_from_port(port);			\
7541cfd19aeSMark Lord 	shift   += hardport * 2;				\
7551cfd19aeSMark Lord }
7561cfd19aeSMark Lord 
757352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
758352fab70SMark Lord {
759352fab70SMark Lord 	return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
760352fab70SMark Lord }
761352fab70SMark Lord 
762c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
763c6fd2807SJeff Garzik 						 unsigned int port)
764c6fd2807SJeff Garzik {
765c6fd2807SJeff Garzik 	return mv_hc_base(base, mv_hc_from_port(port));
766c6fd2807SJeff Garzik }
767c6fd2807SJeff Garzik 
768c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
769c6fd2807SJeff Garzik {
770c6fd2807SJeff Garzik 	return  mv_hc_base_from_port(base, port) +
771c6fd2807SJeff Garzik 		MV_SATAHC_ARBTR_REG_SZ +
772c6fd2807SJeff Garzik 		(mv_hardport_from_port(port) * MV_PORT_REG_SZ);
773c6fd2807SJeff Garzik }
774c6fd2807SJeff Garzik 
775e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
776e12bef50SMark Lord {
777e12bef50SMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
778e12bef50SMark Lord 	unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
779e12bef50SMark Lord 
780e12bef50SMark Lord 	return hc_mmio + ofs;
781e12bef50SMark Lord }
782e12bef50SMark Lord 
783f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
784f351b2d6SSaeed Bishara {
785f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
786f351b2d6SSaeed Bishara 	return hpriv->base;
787f351b2d6SSaeed Bishara }
788f351b2d6SSaeed Bishara 
789c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
790c6fd2807SJeff Garzik {
791f351b2d6SSaeed Bishara 	return mv_port_base(mv_host_base(ap->host), ap->port_no);
792c6fd2807SJeff Garzik }
793c6fd2807SJeff Garzik 
794cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
795c6fd2807SJeff Garzik {
796cca3974eSJeff Garzik 	return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
797c6fd2807SJeff Garzik }
798c6fd2807SJeff Garzik 
799c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
800c5d3e45aSJeff Garzik 			     struct mv_host_priv *hpriv,
801c5d3e45aSJeff Garzik 			     struct mv_port_priv *pp)
802c5d3e45aSJeff Garzik {
803bdd4dddeSJeff Garzik 	u32 index;
804bdd4dddeSJeff Garzik 
805c5d3e45aSJeff Garzik 	/*
806c5d3e45aSJeff Garzik 	 * initialize request queue
807c5d3e45aSJeff Garzik 	 */
808fcfb1f77SMark Lord 	pp->req_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
809fcfb1f77SMark Lord 	index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
810bdd4dddeSJeff Garzik 
811c5d3e45aSJeff Garzik 	WARN_ON(pp->crqb_dma & 0x3ff);
812c5d3e45aSJeff Garzik 	writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
813bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
814c5d3e45aSJeff Garzik 		 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
815bdd4dddeSJeff Garzik 	writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
816c5d3e45aSJeff Garzik 
817c5d3e45aSJeff Garzik 	/*
818c5d3e45aSJeff Garzik 	 * initialize response queue
819c5d3e45aSJeff Garzik 	 */
820fcfb1f77SMark Lord 	pp->resp_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
821fcfb1f77SMark Lord 	index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
822bdd4dddeSJeff Garzik 
823c5d3e45aSJeff Garzik 	WARN_ON(pp->crpb_dma & 0xff);
824c5d3e45aSJeff Garzik 	writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
825bdd4dddeSJeff Garzik 	writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
826bdd4dddeSJeff Garzik 	writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
827c5d3e45aSJeff Garzik 		 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
828c5d3e45aSJeff Garzik }
829c5d3e45aSJeff Garzik 
830c4de573bSMark Lord static void mv_set_main_irq_mask(struct ata_host *host,
831c4de573bSMark Lord 				 u32 disable_bits, u32 enable_bits)
832c4de573bSMark Lord {
833c4de573bSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
834c4de573bSMark Lord 	u32 old_mask, new_mask;
835c4de573bSMark Lord 
83696e2c487SMark Lord 	old_mask = hpriv->main_irq_mask;
837c4de573bSMark Lord 	new_mask = (old_mask & ~disable_bits) | enable_bits;
83896e2c487SMark Lord 	if (new_mask != old_mask) {
83996e2c487SMark Lord 		hpriv->main_irq_mask = new_mask;
840c4de573bSMark Lord 		writelfl(new_mask, hpriv->main_irq_mask_addr);
841c4de573bSMark Lord 	}
84296e2c487SMark Lord }
843c4de573bSMark Lord 
844c4de573bSMark Lord static void mv_enable_port_irqs(struct ata_port *ap,
845c4de573bSMark Lord 				     unsigned int port_bits)
846c4de573bSMark Lord {
847c4de573bSMark Lord 	unsigned int shift, hardport, port = ap->port_no;
848c4de573bSMark Lord 	u32 disable_bits, enable_bits;
849c4de573bSMark Lord 
850c4de573bSMark Lord 	MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
851c4de573bSMark Lord 
852c4de573bSMark Lord 	disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
853c4de573bSMark Lord 	enable_bits  = port_bits << shift;
854c4de573bSMark Lord 	mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
855c4de573bSMark Lord }
856c4de573bSMark Lord 
857c6fd2807SJeff Garzik /**
858c6fd2807SJeff Garzik  *      mv_start_dma - Enable eDMA engine
859c6fd2807SJeff Garzik  *      @base: port base address
860c6fd2807SJeff Garzik  *      @pp: port private data
861c6fd2807SJeff Garzik  *
862c6fd2807SJeff Garzik  *      Verify the local cache of the eDMA state is accurate with a
863c6fd2807SJeff Garzik  *      WARN_ON.
864c6fd2807SJeff Garzik  *
865c6fd2807SJeff Garzik  *      LOCKING:
866c6fd2807SJeff Garzik  *      Inherited from caller.
867c6fd2807SJeff Garzik  */
8680c58912eSMark Lord static void mv_start_dma(struct ata_port *ap, void __iomem *port_mmio,
86972109168SMark Lord 			 struct mv_port_priv *pp, u8 protocol)
870c6fd2807SJeff Garzik {
87172109168SMark Lord 	int want_ncq = (protocol == ATA_PROT_NCQ);
87272109168SMark Lord 
87372109168SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
87472109168SMark Lord 		int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
87572109168SMark Lord 		if (want_ncq != using_ncq)
876b562468cSMark Lord 			mv_stop_edma(ap);
87772109168SMark Lord 	}
878c5d3e45aSJeff Garzik 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
8790c58912eSMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
880352fab70SMark Lord 		int hardport = mv_hardport_from_port(ap->port_no);
8810c58912eSMark Lord 		void __iomem *hc_mmio = mv_hc_base_from_port(
882b0bccb18SMark Lord 					mv_host_base(ap->host), ap->port_no);
883cae6edc3SMark Lord 		u32 hc_irq_cause;
8840c58912eSMark Lord 
885bdd4dddeSJeff Garzik 		/* clear EDMA event indicators, if any */
886f630d562SMark Lord 		writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
887bdd4dddeSJeff Garzik 
888cae6edc3SMark Lord 		/* clear pending irq events */
889cae6edc3SMark Lord 		hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
890cae6edc3SMark Lord 		writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
8910c58912eSMark Lord 
892e12bef50SMark Lord 		mv_edma_cfg(ap, want_ncq);
8930c58912eSMark Lord 
8940c58912eSMark Lord 		/* clear FIS IRQ Cause */
895e4006077SMark Lord 		if (IS_GEN_IIE(hpriv))
8960c58912eSMark Lord 			writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
8970c58912eSMark Lord 
898f630d562SMark Lord 		mv_set_edma_ptrs(port_mmio, hpriv, pp);
89988e675e1SMark Lord 		mv_enable_port_irqs(ap, DONE_IRQ|ERR_IRQ);
900bdd4dddeSJeff Garzik 
901f630d562SMark Lord 		writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
902c6fd2807SJeff Garzik 		pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
903c6fd2807SJeff Garzik 	}
904c6fd2807SJeff Garzik }
905c6fd2807SJeff Garzik 
9069b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
9079b2c4e0bSMark Lord {
9089b2c4e0bSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
9099b2c4e0bSMark Lord 	const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
9109b2c4e0bSMark Lord 	const int per_loop = 5, timeout = (15 * 1000 / per_loop);
9119b2c4e0bSMark Lord 	int i;
9129b2c4e0bSMark Lord 
9139b2c4e0bSMark Lord 	/*
9149b2c4e0bSMark Lord 	 * Wait for the EDMA engine to finish transactions in progress.
915c46938ccSMark Lord 	 * No idea what a good "timeout" value might be, but measurements
916c46938ccSMark Lord 	 * indicate that it often requires hundreds of microseconds
917c46938ccSMark Lord 	 * with two drives in-use.  So we use the 15msec value above
918c46938ccSMark Lord 	 * as a rough guess at what even more drives might require.
9199b2c4e0bSMark Lord 	 */
9209b2c4e0bSMark Lord 	for (i = 0; i < timeout; ++i) {
9219b2c4e0bSMark Lord 		u32 edma_stat = readl(port_mmio + EDMA_STATUS_OFS);
9229b2c4e0bSMark Lord 		if ((edma_stat & empty_idle) == empty_idle)
9239b2c4e0bSMark Lord 			break;
9249b2c4e0bSMark Lord 		udelay(per_loop);
9259b2c4e0bSMark Lord 	}
9269b2c4e0bSMark Lord 	/* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
9279b2c4e0bSMark Lord }
9289b2c4e0bSMark Lord 
929c6fd2807SJeff Garzik /**
930e12bef50SMark Lord  *      mv_stop_edma_engine - Disable eDMA engine
931b562468cSMark Lord  *      @port_mmio: io base address
932c6fd2807SJeff Garzik  *
933c6fd2807SJeff Garzik  *      LOCKING:
934c6fd2807SJeff Garzik  *      Inherited from caller.
935c6fd2807SJeff Garzik  */
936b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
937c6fd2807SJeff Garzik {
938b562468cSMark Lord 	int i;
939c6fd2807SJeff Garzik 
940b562468cSMark Lord 	/* Disable eDMA.  The disable bit auto clears. */
941c6fd2807SJeff Garzik 	writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
942c6fd2807SJeff Garzik 
943b562468cSMark Lord 	/* Wait for the chip to confirm eDMA is off. */
944b562468cSMark Lord 	for (i = 10000; i > 0; i--) {
945b562468cSMark Lord 		u32 reg = readl(port_mmio + EDMA_CMD_OFS);
9464537deb5SJeff Garzik 		if (!(reg & EDMA_EN))
947b562468cSMark Lord 			return 0;
948b562468cSMark Lord 		udelay(10);
949c6fd2807SJeff Garzik 	}
950b562468cSMark Lord 	return -EIO;
951c6fd2807SJeff Garzik }
952c6fd2807SJeff Garzik 
953e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
954c6fd2807SJeff Garzik {
955c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
956c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
957c6fd2807SJeff Garzik 
958b562468cSMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
959b562468cSMark Lord 		return 0;
960c6fd2807SJeff Garzik 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
9619b2c4e0bSMark Lord 	mv_wait_for_edma_empty_idle(ap);
962b562468cSMark Lord 	if (mv_stop_edma_engine(port_mmio)) {
963c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
964b562468cSMark Lord 		return -EIO;
965c6fd2807SJeff Garzik 	}
966b562468cSMark Lord 	return 0;
9670ea9e179SJeff Garzik }
9680ea9e179SJeff Garzik 
969c6fd2807SJeff Garzik #ifdef ATA_DEBUG
970c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes)
971c6fd2807SJeff Garzik {
972c6fd2807SJeff Garzik 	int b, w;
973c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
974c6fd2807SJeff Garzik 		DPRINTK("%p: ", start + b);
975c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
976c6fd2807SJeff Garzik 			printk("%08x ", readl(start + b));
977c6fd2807SJeff Garzik 			b += sizeof(u32);
978c6fd2807SJeff Garzik 		}
979c6fd2807SJeff Garzik 		printk("\n");
980c6fd2807SJeff Garzik 	}
981c6fd2807SJeff Garzik }
982c6fd2807SJeff Garzik #endif
983c6fd2807SJeff Garzik 
984c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
985c6fd2807SJeff Garzik {
986c6fd2807SJeff Garzik #ifdef ATA_DEBUG
987c6fd2807SJeff Garzik 	int b, w;
988c6fd2807SJeff Garzik 	u32 dw;
989c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
990c6fd2807SJeff Garzik 		DPRINTK("%02x: ", b);
991c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
992c6fd2807SJeff Garzik 			(void) pci_read_config_dword(pdev, b, &dw);
993c6fd2807SJeff Garzik 			printk("%08x ", dw);
994c6fd2807SJeff Garzik 			b += sizeof(u32);
995c6fd2807SJeff Garzik 		}
996c6fd2807SJeff Garzik 		printk("\n");
997c6fd2807SJeff Garzik 	}
998c6fd2807SJeff Garzik #endif
999c6fd2807SJeff Garzik }
1000c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port,
1001c6fd2807SJeff Garzik 			     struct pci_dev *pdev)
1002c6fd2807SJeff Garzik {
1003c6fd2807SJeff Garzik #ifdef ATA_DEBUG
1004c6fd2807SJeff Garzik 	void __iomem *hc_base = mv_hc_base(mmio_base,
1005c6fd2807SJeff Garzik 					   port >> MV_PORT_HC_SHIFT);
1006c6fd2807SJeff Garzik 	void __iomem *port_base;
1007c6fd2807SJeff Garzik 	int start_port, num_ports, p, start_hc, num_hcs, hc;
1008c6fd2807SJeff Garzik 
1009c6fd2807SJeff Garzik 	if (0 > port) {
1010c6fd2807SJeff Garzik 		start_hc = start_port = 0;
1011c6fd2807SJeff Garzik 		num_ports = 8;		/* shld be benign for 4 port devs */
1012c6fd2807SJeff Garzik 		num_hcs = 2;
1013c6fd2807SJeff Garzik 	} else {
1014c6fd2807SJeff Garzik 		start_hc = port >> MV_PORT_HC_SHIFT;
1015c6fd2807SJeff Garzik 		start_port = port;
1016c6fd2807SJeff Garzik 		num_ports = num_hcs = 1;
1017c6fd2807SJeff Garzik 	}
1018c6fd2807SJeff Garzik 	DPRINTK("All registers for port(s) %u-%u:\n", start_port,
1019c6fd2807SJeff Garzik 		num_ports > 1 ? num_ports - 1 : start_port);
1020c6fd2807SJeff Garzik 
1021c6fd2807SJeff Garzik 	if (NULL != pdev) {
1022c6fd2807SJeff Garzik 		DPRINTK("PCI config space regs:\n");
1023c6fd2807SJeff Garzik 		mv_dump_pci_cfg(pdev, 0x68);
1024c6fd2807SJeff Garzik 	}
1025c6fd2807SJeff Garzik 	DPRINTK("PCI regs:\n");
1026c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xc00, 0x3c);
1027c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xd00, 0x34);
1028c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xf00, 0x4);
1029c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0x1d00, 0x6c);
1030c6fd2807SJeff Garzik 	for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
1031c6fd2807SJeff Garzik 		hc_base = mv_hc_base(mmio_base, hc);
1032c6fd2807SJeff Garzik 		DPRINTK("HC regs (HC %i):\n", hc);
1033c6fd2807SJeff Garzik 		mv_dump_mem(hc_base, 0x1c);
1034c6fd2807SJeff Garzik 	}
1035c6fd2807SJeff Garzik 	for (p = start_port; p < start_port + num_ports; p++) {
1036c6fd2807SJeff Garzik 		port_base = mv_port_base(mmio_base, p);
1037c6fd2807SJeff Garzik 		DPRINTK("EDMA regs (port %i):\n", p);
1038c6fd2807SJeff Garzik 		mv_dump_mem(port_base, 0x54);
1039c6fd2807SJeff Garzik 		DPRINTK("SATA regs (port %i):\n", p);
1040c6fd2807SJeff Garzik 		mv_dump_mem(port_base+0x300, 0x60);
1041c6fd2807SJeff Garzik 	}
1042c6fd2807SJeff Garzik #endif
1043c6fd2807SJeff Garzik }
1044c6fd2807SJeff Garzik 
1045c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1046c6fd2807SJeff Garzik {
1047c6fd2807SJeff Garzik 	unsigned int ofs;
1048c6fd2807SJeff Garzik 
1049c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1050c6fd2807SJeff Garzik 	case SCR_STATUS:
1051c6fd2807SJeff Garzik 	case SCR_CONTROL:
1052c6fd2807SJeff Garzik 	case SCR_ERROR:
1053c6fd2807SJeff Garzik 		ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
1054c6fd2807SJeff Garzik 		break;
1055c6fd2807SJeff Garzik 	case SCR_ACTIVE:
1056c6fd2807SJeff Garzik 		ofs = SATA_ACTIVE_OFS;   /* active is not with the others */
1057c6fd2807SJeff Garzik 		break;
1058c6fd2807SJeff Garzik 	default:
1059c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1060c6fd2807SJeff Garzik 		break;
1061c6fd2807SJeff Garzik 	}
1062c6fd2807SJeff Garzik 	return ofs;
1063c6fd2807SJeff Garzik }
1064c6fd2807SJeff Garzik 
106582ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
1066c6fd2807SJeff Garzik {
1067c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1068c6fd2807SJeff Garzik 
1069da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
107082ef04fbSTejun Heo 		*val = readl(mv_ap_base(link->ap) + ofs);
1071da3dbb17STejun Heo 		return 0;
1072da3dbb17STejun Heo 	} else
1073da3dbb17STejun Heo 		return -EINVAL;
1074c6fd2807SJeff Garzik }
1075c6fd2807SJeff Garzik 
107682ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
1077c6fd2807SJeff Garzik {
1078c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1079c6fd2807SJeff Garzik 
1080da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
108182ef04fbSTejun Heo 		writelfl(val, mv_ap_base(link->ap) + ofs);
1082da3dbb17STejun Heo 		return 0;
1083da3dbb17STejun Heo 	} else
1084da3dbb17STejun Heo 		return -EINVAL;
1085c6fd2807SJeff Garzik }
1086c6fd2807SJeff Garzik 
1087f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1088f273827eSMark Lord {
1089f273827eSMark Lord 	/*
1090e49856d8SMark Lord 	 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1091e49856d8SMark Lord 	 *
1092e49856d8SMark Lord 	 * Gen-II does not support NCQ over a port multiplier
1093e49856d8SMark Lord 	 *  (no FIS-based switching).
1094f273827eSMark Lord 	 */
1095e49856d8SMark Lord 	if (adev->flags & ATA_DFLAG_NCQ) {
1096352fab70SMark Lord 		if (sata_pmp_attached(adev->link->ap)) {
1097e49856d8SMark Lord 			adev->flags &= ~ATA_DFLAG_NCQ;
1098352fab70SMark Lord 			ata_dev_printk(adev, KERN_INFO,
1099352fab70SMark Lord 				"NCQ disabled for command-based switching\n");
1100352fab70SMark Lord 		}
1101f273827eSMark Lord 	}
1102e49856d8SMark Lord }
1103f273827eSMark Lord 
11043e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc)
11053e4a1391SMark Lord {
11063e4a1391SMark Lord 	struct ata_link *link = qc->dev->link;
11073e4a1391SMark Lord 	struct ata_port *ap = link->ap;
11083e4a1391SMark Lord 	struct mv_port_priv *pp = ap->private_data;
11093e4a1391SMark Lord 
11103e4a1391SMark Lord 	/*
111129d187bbSMark Lord 	 * Don't allow new commands if we're in a delayed EH state
111229d187bbSMark Lord 	 * for NCQ and/or FIS-based switching.
111329d187bbSMark Lord 	 */
111429d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
111529d187bbSMark Lord 		return ATA_DEFER_PORT;
111629d187bbSMark Lord 	/*
11173e4a1391SMark Lord 	 * If the port is completely idle, then allow the new qc.
11183e4a1391SMark Lord 	 */
11193e4a1391SMark Lord 	if (ap->nr_active_links == 0)
11203e4a1391SMark Lord 		return 0;
11213e4a1391SMark Lord 
11223e4a1391SMark Lord 	/*
11234bdee6c5STejun Heo 	 * The port is operating in host queuing mode (EDMA) with NCQ
11244bdee6c5STejun Heo 	 * enabled, allow multiple NCQ commands.  EDMA also allows
11254bdee6c5STejun Heo 	 * queueing multiple DMA commands but libata core currently
11264bdee6c5STejun Heo 	 * doesn't allow it.
11273e4a1391SMark Lord 	 */
11284bdee6c5STejun Heo 	if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
11294bdee6c5STejun Heo 	    (pp->pp_flags & MV_PP_FLAG_NCQ_EN) && ata_is_ncq(qc->tf.protocol))
11303e4a1391SMark Lord 		return 0;
11314bdee6c5STejun Heo 
11323e4a1391SMark Lord 	return ATA_DEFER_PORT;
11333e4a1391SMark Lord }
11343e4a1391SMark Lord 
113500f42eabSMark Lord static void mv_config_fbs(void __iomem *port_mmio, int want_ncq, int want_fbs)
1136e49856d8SMark Lord {
113700f42eabSMark Lord 	u32 new_fiscfg, old_fiscfg;
113800f42eabSMark Lord 	u32 new_ltmode, old_ltmode;
113900f42eabSMark Lord 	u32 new_haltcond, old_haltcond;
114000f42eabSMark Lord 
11418e7decdbSMark Lord 	old_fiscfg   = readl(port_mmio + FISCFG_OFS);
1142e49856d8SMark Lord 	old_ltmode   = readl(port_mmio + LTMODE_OFS);
114300f42eabSMark Lord 	old_haltcond = readl(port_mmio + EDMA_HALTCOND_OFS);
114400f42eabSMark Lord 
114500f42eabSMark Lord 	new_fiscfg   = old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
114600f42eabSMark Lord 	new_ltmode   = old_ltmode & ~LTMODE_BIT8;
114700f42eabSMark Lord 	new_haltcond = old_haltcond | EDMA_ERR_DEV;
114800f42eabSMark Lord 
114900f42eabSMark Lord 	if (want_fbs) {
11508e7decdbSMark Lord 		new_fiscfg = old_fiscfg | FISCFG_SINGLE_SYNC;
1151e49856d8SMark Lord 		new_ltmode = old_ltmode | LTMODE_BIT8;
11524c299ca3SMark Lord 		if (want_ncq)
11534c299ca3SMark Lord 			new_haltcond &= ~EDMA_ERR_DEV;
11544c299ca3SMark Lord 		else
11554c299ca3SMark Lord 			new_fiscfg |=  FISCFG_WAIT_DEV_ERR;
1156e49856d8SMark Lord 	}
115700f42eabSMark Lord 
11588e7decdbSMark Lord 	if (new_fiscfg != old_fiscfg)
11598e7decdbSMark Lord 		writelfl(new_fiscfg, port_mmio + FISCFG_OFS);
1160e49856d8SMark Lord 	if (new_ltmode != old_ltmode)
1161e49856d8SMark Lord 		writelfl(new_ltmode, port_mmio + LTMODE_OFS);
116200f42eabSMark Lord 	if (new_haltcond != old_haltcond)
116300f42eabSMark Lord 		writelfl(new_haltcond, port_mmio + EDMA_HALTCOND_OFS);
1164e49856d8SMark Lord }
1165c6fd2807SJeff Garzik 
1166dd2890f6SMark Lord static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1167dd2890f6SMark Lord {
1168dd2890f6SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1169dd2890f6SMark Lord 	u32 old, new;
1170dd2890f6SMark Lord 
1171dd2890f6SMark Lord 	/* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1172dd2890f6SMark Lord 	old = readl(hpriv->base + MV_GPIO_PORT_CTL_OFS);
1173dd2890f6SMark Lord 	if (want_ncq)
1174dd2890f6SMark Lord 		new = old | (1 << 22);
1175dd2890f6SMark Lord 	else
1176dd2890f6SMark Lord 		new = old & ~(1 << 22);
1177dd2890f6SMark Lord 	if (new != old)
1178dd2890f6SMark Lord 		writel(new, hpriv->base + MV_GPIO_PORT_CTL_OFS);
1179dd2890f6SMark Lord }
1180dd2890f6SMark Lord 
1181e12bef50SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq)
1182c6fd2807SJeff Garzik {
1183c6fd2807SJeff Garzik 	u32 cfg;
1184e12bef50SMark Lord 	struct mv_port_priv *pp    = ap->private_data;
1185e12bef50SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1186e12bef50SMark Lord 	void __iomem *port_mmio    = mv_ap_base(ap);
1187c6fd2807SJeff Garzik 
1188c6fd2807SJeff Garzik 	/* set up non-NCQ EDMA configuration */
1189c6fd2807SJeff Garzik 	cfg = EDMA_CFG_Q_DEPTH;		/* always 0x1f for *all* chips */
119000f42eabSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_FBS_EN;
1191c6fd2807SJeff Garzik 
1192c6fd2807SJeff Garzik 	if (IS_GEN_I(hpriv))
1193c6fd2807SJeff Garzik 		cfg |= (1 << 8);	/* enab config burst size mask */
1194c6fd2807SJeff Garzik 
1195dd2890f6SMark Lord 	else if (IS_GEN_II(hpriv)) {
1196c6fd2807SJeff Garzik 		cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1197dd2890f6SMark Lord 		mv_60x1_errata_sata25(ap, want_ncq);
1198c6fd2807SJeff Garzik 
1199dd2890f6SMark Lord 	} else if (IS_GEN_IIE(hpriv)) {
120000f42eabSMark Lord 		int want_fbs = sata_pmp_attached(ap);
120100f42eabSMark Lord 		/*
120200f42eabSMark Lord 		 * Possible future enhancement:
120300f42eabSMark Lord 		 *
120400f42eabSMark Lord 		 * The chip can use FBS with non-NCQ, if we allow it,
120500f42eabSMark Lord 		 * But first we need to have the error handling in place
120600f42eabSMark Lord 		 * for this mode (datasheet section 7.3.15.4.2.3).
120700f42eabSMark Lord 		 * So disallow non-NCQ FBS for now.
120800f42eabSMark Lord 		 */
120900f42eabSMark Lord 		want_fbs &= want_ncq;
121000f42eabSMark Lord 
121100f42eabSMark Lord 		mv_config_fbs(port_mmio, want_ncq, want_fbs);
121200f42eabSMark Lord 
121300f42eabSMark Lord 		if (want_fbs) {
121400f42eabSMark Lord 			pp->pp_flags |= MV_PP_FLAG_FBS_EN;
121500f42eabSMark Lord 			cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
121600f42eabSMark Lord 		}
121700f42eabSMark Lord 
1218e728eabeSJeff Garzik 		cfg |= (1 << 23);	/* do not mask PM field in rx'd FIS */
1219e728eabeSJeff Garzik 		cfg |= (1 << 22);	/* enab 4-entry host queue cache */
12201f398472SMark Lord 		if (!IS_SOC(hpriv))
1221c6fd2807SJeff Garzik 			cfg |= (1 << 18);	/* enab early completion */
1222616d4a98SMark Lord 		if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1223616d4a98SMark Lord 			cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
1224c6fd2807SJeff Garzik 	}
1225c6fd2807SJeff Garzik 
122672109168SMark Lord 	if (want_ncq) {
122772109168SMark Lord 		cfg |= EDMA_CFG_NCQ;
122872109168SMark Lord 		pp->pp_flags |=  MV_PP_FLAG_NCQ_EN;
122972109168SMark Lord 	} else
123072109168SMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_NCQ_EN;
123172109168SMark Lord 
1232c6fd2807SJeff Garzik 	writelfl(cfg, port_mmio + EDMA_CFG_OFS);
1233c6fd2807SJeff Garzik }
1234c6fd2807SJeff Garzik 
1235da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1236da2fa9baSMark Lord {
1237da2fa9baSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1238da2fa9baSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1239eb73d558SMark Lord 	int tag;
1240da2fa9baSMark Lord 
1241da2fa9baSMark Lord 	if (pp->crqb) {
1242da2fa9baSMark Lord 		dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1243da2fa9baSMark Lord 		pp->crqb = NULL;
1244da2fa9baSMark Lord 	}
1245da2fa9baSMark Lord 	if (pp->crpb) {
1246da2fa9baSMark Lord 		dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1247da2fa9baSMark Lord 		pp->crpb = NULL;
1248da2fa9baSMark Lord 	}
1249eb73d558SMark Lord 	/*
1250eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1251eb73d558SMark Lord 	 * For later hardware, we have one unique sg_tbl per NCQ tag.
1252eb73d558SMark Lord 	 */
1253eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1254eb73d558SMark Lord 		if (pp->sg_tbl[tag]) {
1255eb73d558SMark Lord 			if (tag == 0 || !IS_GEN_I(hpriv))
1256eb73d558SMark Lord 				dma_pool_free(hpriv->sg_tbl_pool,
1257eb73d558SMark Lord 					      pp->sg_tbl[tag],
1258eb73d558SMark Lord 					      pp->sg_tbl_dma[tag]);
1259eb73d558SMark Lord 			pp->sg_tbl[tag] = NULL;
1260eb73d558SMark Lord 		}
1261da2fa9baSMark Lord 	}
1262da2fa9baSMark Lord }
1263da2fa9baSMark Lord 
1264c6fd2807SJeff Garzik /**
1265c6fd2807SJeff Garzik  *      mv_port_start - Port specific init/start routine.
1266c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1267c6fd2807SJeff Garzik  *
1268c6fd2807SJeff Garzik  *      Allocate and point to DMA memory, init port private memory,
1269c6fd2807SJeff Garzik  *      zero indices.
1270c6fd2807SJeff Garzik  *
1271c6fd2807SJeff Garzik  *      LOCKING:
1272c6fd2807SJeff Garzik  *      Inherited from caller.
1273c6fd2807SJeff Garzik  */
1274c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1275c6fd2807SJeff Garzik {
1276cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1277cca3974eSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1278c6fd2807SJeff Garzik 	struct mv_port_priv *pp;
1279dde20207SJames Bottomley 	int tag;
1280c6fd2807SJeff Garzik 
128124dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1282c6fd2807SJeff Garzik 	if (!pp)
128324dc5f33STejun Heo 		return -ENOMEM;
1284da2fa9baSMark Lord 	ap->private_data = pp;
1285c6fd2807SJeff Garzik 
1286da2fa9baSMark Lord 	pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1287da2fa9baSMark Lord 	if (!pp->crqb)
1288da2fa9baSMark Lord 		return -ENOMEM;
1289da2fa9baSMark Lord 	memset(pp->crqb, 0, MV_CRQB_Q_SZ);
1290c6fd2807SJeff Garzik 
1291da2fa9baSMark Lord 	pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1292da2fa9baSMark Lord 	if (!pp->crpb)
1293da2fa9baSMark Lord 		goto out_port_free_dma_mem;
1294da2fa9baSMark Lord 	memset(pp->crpb, 0, MV_CRPB_Q_SZ);
1295c6fd2807SJeff Garzik 
12963bd0a70eSMark Lord 	/* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
12973bd0a70eSMark Lord 	if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
12983bd0a70eSMark Lord 		ap->flags |= ATA_FLAG_AN;
1299eb73d558SMark Lord 	/*
1300eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1301eb73d558SMark Lord 	 * For later hardware, we need one unique sg_tbl per NCQ tag.
1302eb73d558SMark Lord 	 */
1303eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1304eb73d558SMark Lord 		if (tag == 0 || !IS_GEN_I(hpriv)) {
1305eb73d558SMark Lord 			pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1306eb73d558SMark Lord 					      GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1307eb73d558SMark Lord 			if (!pp->sg_tbl[tag])
1308da2fa9baSMark Lord 				goto out_port_free_dma_mem;
1309eb73d558SMark Lord 		} else {
1310eb73d558SMark Lord 			pp->sg_tbl[tag]     = pp->sg_tbl[0];
1311eb73d558SMark Lord 			pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1312eb73d558SMark Lord 		}
1313eb73d558SMark Lord 	}
1314c6fd2807SJeff Garzik 	return 0;
1315da2fa9baSMark Lord 
1316da2fa9baSMark Lord out_port_free_dma_mem:
1317da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1318da2fa9baSMark Lord 	return -ENOMEM;
1319c6fd2807SJeff Garzik }
1320c6fd2807SJeff Garzik 
1321c6fd2807SJeff Garzik /**
1322c6fd2807SJeff Garzik  *      mv_port_stop - Port specific cleanup/stop routine.
1323c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1324c6fd2807SJeff Garzik  *
1325c6fd2807SJeff Garzik  *      Stop DMA, cleanup port memory.
1326c6fd2807SJeff Garzik  *
1327c6fd2807SJeff Garzik  *      LOCKING:
1328cca3974eSJeff Garzik  *      This routine uses the host lock to protect the DMA stop.
1329c6fd2807SJeff Garzik  */
1330c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1331c6fd2807SJeff Garzik {
1332e12bef50SMark Lord 	mv_stop_edma(ap);
133388e675e1SMark Lord 	mv_enable_port_irqs(ap, 0);
1334da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1335c6fd2807SJeff Garzik }
1336c6fd2807SJeff Garzik 
1337c6fd2807SJeff Garzik /**
1338c6fd2807SJeff Garzik  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1339c6fd2807SJeff Garzik  *      @qc: queued command whose SG list to source from
1340c6fd2807SJeff Garzik  *
1341c6fd2807SJeff Garzik  *      Populate the SG list and mark the last entry.
1342c6fd2807SJeff Garzik  *
1343c6fd2807SJeff Garzik  *      LOCKING:
1344c6fd2807SJeff Garzik  *      Inherited from caller.
1345c6fd2807SJeff Garzik  */
13466c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1347c6fd2807SJeff Garzik {
1348c6fd2807SJeff Garzik 	struct mv_port_priv *pp = qc->ap->private_data;
1349c6fd2807SJeff Garzik 	struct scatterlist *sg;
13503be6cbd7SJeff Garzik 	struct mv_sg *mv_sg, *last_sg = NULL;
1351ff2aeb1eSTejun Heo 	unsigned int si;
1352c6fd2807SJeff Garzik 
1353eb73d558SMark Lord 	mv_sg = pp->sg_tbl[qc->tag];
1354ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1355d88184fbSJeff Garzik 		dma_addr_t addr = sg_dma_address(sg);
1356d88184fbSJeff Garzik 		u32 sg_len = sg_dma_len(sg);
1357c6fd2807SJeff Garzik 
13584007b493SOlof Johansson 		while (sg_len) {
13594007b493SOlof Johansson 			u32 offset = addr & 0xffff;
13604007b493SOlof Johansson 			u32 len = sg_len;
13614007b493SOlof Johansson 
13624007b493SOlof Johansson 			if ((offset + sg_len > 0x10000))
13634007b493SOlof Johansson 				len = 0x10000 - offset;
13644007b493SOlof Johansson 
1365d88184fbSJeff Garzik 			mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1366d88184fbSJeff Garzik 			mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
13676c08772eSJeff Garzik 			mv_sg->flags_size = cpu_to_le32(len & 0xffff);
1368c6fd2807SJeff Garzik 
13694007b493SOlof Johansson 			sg_len -= len;
13704007b493SOlof Johansson 			addr += len;
13714007b493SOlof Johansson 
13723be6cbd7SJeff Garzik 			last_sg = mv_sg;
1373d88184fbSJeff Garzik 			mv_sg++;
1374c6fd2807SJeff Garzik 		}
13754007b493SOlof Johansson 	}
13763be6cbd7SJeff Garzik 
13773be6cbd7SJeff Garzik 	if (likely(last_sg))
13783be6cbd7SJeff Garzik 		last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
1379c6fd2807SJeff Garzik }
1380c6fd2807SJeff Garzik 
13815796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1382c6fd2807SJeff Garzik {
1383c6fd2807SJeff Garzik 	u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1384c6fd2807SJeff Garzik 		(last ? CRQB_CMD_LAST : 0);
1385c6fd2807SJeff Garzik 	*cmdw = cpu_to_le16(tmp);
1386c6fd2807SJeff Garzik }
1387c6fd2807SJeff Garzik 
1388c6fd2807SJeff Garzik /**
1389c6fd2807SJeff Garzik  *      mv_qc_prep - Host specific command preparation.
1390c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1391c6fd2807SJeff Garzik  *
1392c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1393c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1394c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1395c6fd2807SJeff Garzik  *      the SG load routine.
1396c6fd2807SJeff Garzik  *
1397c6fd2807SJeff Garzik  *      LOCKING:
1398c6fd2807SJeff Garzik  *      Inherited from caller.
1399c6fd2807SJeff Garzik  */
1400c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc)
1401c6fd2807SJeff Garzik {
1402c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1403c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1404c6fd2807SJeff Garzik 	__le16 *cw;
1405c6fd2807SJeff Garzik 	struct ata_taskfile *tf;
1406c6fd2807SJeff Garzik 	u16 flags = 0;
1407c6fd2807SJeff Garzik 	unsigned in_index;
1408c6fd2807SJeff Garzik 
1409138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1410138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ))
1411c6fd2807SJeff Garzik 		return;
1412c6fd2807SJeff Garzik 
1413c6fd2807SJeff Garzik 	/* Fill in command request block
1414c6fd2807SJeff Garzik 	 */
1415c6fd2807SJeff Garzik 	if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1416c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
1417c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
1418c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
1419e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
1420c6fd2807SJeff Garzik 
1421bdd4dddeSJeff Garzik 	/* get current queue index from software */
1422fcfb1f77SMark Lord 	in_index = pp->req_idx;
1423c6fd2807SJeff Garzik 
1424c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr =
1425eb73d558SMark Lord 		cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1426c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr_hi =
1427eb73d558SMark Lord 		cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
1428c6fd2807SJeff Garzik 	pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
1429c6fd2807SJeff Garzik 
1430c6fd2807SJeff Garzik 	cw = &pp->crqb[in_index].ata_cmd[0];
1431c6fd2807SJeff Garzik 	tf = &qc->tf;
1432c6fd2807SJeff Garzik 
1433c6fd2807SJeff Garzik 	/* Sadly, the CRQB cannot accomodate all registers--there are
1434c6fd2807SJeff Garzik 	 * only 11 bytes...so we must pick and choose required
1435c6fd2807SJeff Garzik 	 * registers based on the command.  So, we drop feature and
1436c6fd2807SJeff Garzik 	 * hob_feature for [RW] DMA commands, but they are needed for
1437cd12e1f7SMark Lord 	 * NCQ.  NCQ will drop hob_nsect, which is not needed there
1438cd12e1f7SMark Lord 	 * (nsect is used only for the tag; feat/hob_feat hold true nsect).
1439c6fd2807SJeff Garzik 	 */
1440c6fd2807SJeff Garzik 	switch (tf->command) {
1441c6fd2807SJeff Garzik 	case ATA_CMD_READ:
1442c6fd2807SJeff Garzik 	case ATA_CMD_READ_EXT:
1443c6fd2807SJeff Garzik 	case ATA_CMD_WRITE:
1444c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_EXT:
1445c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_FUA_EXT:
1446c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1447c6fd2807SJeff Garzik 		break;
1448c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_READ:
1449c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_WRITE:
1450c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
1451c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1452c6fd2807SJeff Garzik 		break;
1453c6fd2807SJeff Garzik 	default:
1454c6fd2807SJeff Garzik 		/* The only other commands EDMA supports in non-queued and
1455c6fd2807SJeff Garzik 		 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1456c6fd2807SJeff Garzik 		 * of which are defined/used by Linux.  If we get here, this
1457c6fd2807SJeff Garzik 		 * driver needs work.
1458c6fd2807SJeff Garzik 		 *
1459c6fd2807SJeff Garzik 		 * FIXME: modify libata to give qc_prep a return value and
1460c6fd2807SJeff Garzik 		 * return error here.
1461c6fd2807SJeff Garzik 		 */
1462c6fd2807SJeff Garzik 		BUG_ON(tf->command);
1463c6fd2807SJeff Garzik 		break;
1464c6fd2807SJeff Garzik 	}
1465c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1466c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1467c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1468c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1469c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1470c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1471c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1472c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1473c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);	/* last */
1474c6fd2807SJeff Garzik 
1475c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1476c6fd2807SJeff Garzik 		return;
1477c6fd2807SJeff Garzik 	mv_fill_sg(qc);
1478c6fd2807SJeff Garzik }
1479c6fd2807SJeff Garzik 
1480c6fd2807SJeff Garzik /**
1481c6fd2807SJeff Garzik  *      mv_qc_prep_iie - Host specific command preparation.
1482c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1483c6fd2807SJeff Garzik  *
1484c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1485c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1486c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1487c6fd2807SJeff Garzik  *      the SG load routine.
1488c6fd2807SJeff Garzik  *
1489c6fd2807SJeff Garzik  *      LOCKING:
1490c6fd2807SJeff Garzik  *      Inherited from caller.
1491c6fd2807SJeff Garzik  */
1492c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
1493c6fd2807SJeff Garzik {
1494c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1495c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1496c6fd2807SJeff Garzik 	struct mv_crqb_iie *crqb;
1497c6fd2807SJeff Garzik 	struct ata_taskfile *tf;
1498c6fd2807SJeff Garzik 	unsigned in_index;
1499c6fd2807SJeff Garzik 	u32 flags = 0;
1500c6fd2807SJeff Garzik 
1501138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1502138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ))
1503c6fd2807SJeff Garzik 		return;
1504c6fd2807SJeff Garzik 
1505e12bef50SMark Lord 	/* Fill in Gen IIE command request block */
1506c6fd2807SJeff Garzik 	if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1507c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
1508c6fd2807SJeff Garzik 
1509c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
1510c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
15118c0aeb4aSMark Lord 	flags |= qc->tag << CRQB_HOSTQ_SHIFT;
1512e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
1513c6fd2807SJeff Garzik 
1514bdd4dddeSJeff Garzik 	/* get current queue index from software */
1515fcfb1f77SMark Lord 	in_index = pp->req_idx;
1516c6fd2807SJeff Garzik 
1517c6fd2807SJeff Garzik 	crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
1518eb73d558SMark Lord 	crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1519eb73d558SMark Lord 	crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
1520c6fd2807SJeff Garzik 	crqb->flags = cpu_to_le32(flags);
1521c6fd2807SJeff Garzik 
1522c6fd2807SJeff Garzik 	tf = &qc->tf;
1523c6fd2807SJeff Garzik 	crqb->ata_cmd[0] = cpu_to_le32(
1524c6fd2807SJeff Garzik 			(tf->command << 16) |
1525c6fd2807SJeff Garzik 			(tf->feature << 24)
1526c6fd2807SJeff Garzik 		);
1527c6fd2807SJeff Garzik 	crqb->ata_cmd[1] = cpu_to_le32(
1528c6fd2807SJeff Garzik 			(tf->lbal << 0) |
1529c6fd2807SJeff Garzik 			(tf->lbam << 8) |
1530c6fd2807SJeff Garzik 			(tf->lbah << 16) |
1531c6fd2807SJeff Garzik 			(tf->device << 24)
1532c6fd2807SJeff Garzik 		);
1533c6fd2807SJeff Garzik 	crqb->ata_cmd[2] = cpu_to_le32(
1534c6fd2807SJeff Garzik 			(tf->hob_lbal << 0) |
1535c6fd2807SJeff Garzik 			(tf->hob_lbam << 8) |
1536c6fd2807SJeff Garzik 			(tf->hob_lbah << 16) |
1537c6fd2807SJeff Garzik 			(tf->hob_feature << 24)
1538c6fd2807SJeff Garzik 		);
1539c6fd2807SJeff Garzik 	crqb->ata_cmd[3] = cpu_to_le32(
1540c6fd2807SJeff Garzik 			(tf->nsect << 0) |
1541c6fd2807SJeff Garzik 			(tf->hob_nsect << 8)
1542c6fd2807SJeff Garzik 		);
1543c6fd2807SJeff Garzik 
1544c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1545c6fd2807SJeff Garzik 		return;
1546c6fd2807SJeff Garzik 	mv_fill_sg(qc);
1547c6fd2807SJeff Garzik }
1548c6fd2807SJeff Garzik 
1549c6fd2807SJeff Garzik /**
1550c6fd2807SJeff Garzik  *      mv_qc_issue - Initiate a command to the host
1551c6fd2807SJeff Garzik  *      @qc: queued command to start
1552c6fd2807SJeff Garzik  *
1553c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1554c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it sanity checks our local
1555c6fd2807SJeff Garzik  *      caches of the request producer/consumer indices then enables
1556c6fd2807SJeff Garzik  *      DMA and bumps the request producer index.
1557c6fd2807SJeff Garzik  *
1558c6fd2807SJeff Garzik  *      LOCKING:
1559c6fd2807SJeff Garzik  *      Inherited from caller.
1560c6fd2807SJeff Garzik  */
1561c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
1562c6fd2807SJeff Garzik {
1563c5d3e45aSJeff Garzik 	struct ata_port *ap = qc->ap;
1564c5d3e45aSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1565c5d3e45aSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1566bdd4dddeSJeff Garzik 	u32 in_index;
1567c6fd2807SJeff Garzik 
1568138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1569138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ)) {
1570c6112bd8SMark Lord 		static int limit_warnings = 10;
1571c6112bd8SMark Lord 		/*
1572c6112bd8SMark Lord 		 * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
1573c6112bd8SMark Lord 		 *
1574c6112bd8SMark Lord 		 * Someday, we might implement special polling workarounds
1575c6112bd8SMark Lord 		 * for these, but it all seems rather unnecessary since we
1576c6112bd8SMark Lord 		 * normally use only DMA for commands which transfer more
1577c6112bd8SMark Lord 		 * than a single block of data.
1578c6112bd8SMark Lord 		 *
1579c6112bd8SMark Lord 		 * Much of the time, this could just work regardless.
1580c6112bd8SMark Lord 		 * So for now, just log the incident, and allow the attempt.
1581c6112bd8SMark Lord 		 */
1582c7843e8fSMark Lord 		if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
1583c6112bd8SMark Lord 			--limit_warnings;
1584c6112bd8SMark Lord 			ata_link_printk(qc->dev->link, KERN_WARNING, DRV_NAME
1585c6112bd8SMark Lord 					": attempting PIO w/multiple DRQ: "
1586c6112bd8SMark Lord 					"this may fail due to h/w errata\n");
1587c6112bd8SMark Lord 		}
158817c5aab5SMark Lord 		/*
158917c5aab5SMark Lord 		 * We're about to send a non-EDMA capable command to the
1590c6fd2807SJeff Garzik 		 * port.  Turn off EDMA so there won't be problems accessing
1591c6fd2807SJeff Garzik 		 * shadow block, etc registers.
1592c6fd2807SJeff Garzik 		 */
1593b562468cSMark Lord 		mv_stop_edma(ap);
159488e675e1SMark Lord 		mv_enable_port_irqs(ap, ERR_IRQ);
1595e49856d8SMark Lord 		mv_pmp_select(ap, qc->dev->link->pmp);
15969363c382STejun Heo 		return ata_sff_qc_issue(qc);
1597c6fd2807SJeff Garzik 	}
1598c6fd2807SJeff Garzik 
159972109168SMark Lord 	mv_start_dma(ap, port_mmio, pp, qc->tf.protocol);
1600bdd4dddeSJeff Garzik 
1601fcfb1f77SMark Lord 	pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
1602fcfb1f77SMark Lord 	in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
1603c6fd2807SJeff Garzik 
1604c6fd2807SJeff Garzik 	/* and write the request in pointer to kick the EDMA to life */
1605bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
1606bdd4dddeSJeff Garzik 		 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
1607c6fd2807SJeff Garzik 
1608c6fd2807SJeff Garzik 	return 0;
1609c6fd2807SJeff Garzik }
1610c6fd2807SJeff Garzik 
16118f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
16128f767f8aSMark Lord {
16138f767f8aSMark Lord 	struct mv_port_priv *pp = ap->private_data;
16148f767f8aSMark Lord 	struct ata_queued_cmd *qc;
16158f767f8aSMark Lord 
16168f767f8aSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
16178f767f8aSMark Lord 		return NULL;
16188f767f8aSMark Lord 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
16198f767f8aSMark Lord 	if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
16208f767f8aSMark Lord 		qc = NULL;
16218f767f8aSMark Lord 	return qc;
16228f767f8aSMark Lord }
16238f767f8aSMark Lord 
162429d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap)
162529d187bbSMark Lord {
162629d187bbSMark Lord 	unsigned int pmp, pmp_map;
162729d187bbSMark Lord 	struct mv_port_priv *pp = ap->private_data;
162829d187bbSMark Lord 
162929d187bbSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
163029d187bbSMark Lord 		/*
163129d187bbSMark Lord 		 * Perform NCQ error analysis on failed PMPs
163229d187bbSMark Lord 		 * before we freeze the port entirely.
163329d187bbSMark Lord 		 *
163429d187bbSMark Lord 		 * The failed PMPs are marked earlier by mv_pmp_eh_prep().
163529d187bbSMark Lord 		 */
163629d187bbSMark Lord 		pmp_map = pp->delayed_eh_pmp_map;
163729d187bbSMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
163829d187bbSMark Lord 		for (pmp = 0; pmp_map != 0; pmp++) {
163929d187bbSMark Lord 			unsigned int this_pmp = (1 << pmp);
164029d187bbSMark Lord 			if (pmp_map & this_pmp) {
164129d187bbSMark Lord 				struct ata_link *link = &ap->pmp_link[pmp];
164229d187bbSMark Lord 				pmp_map &= ~this_pmp;
164329d187bbSMark Lord 				ata_eh_analyze_ncq_error(link);
164429d187bbSMark Lord 			}
164529d187bbSMark Lord 		}
164629d187bbSMark Lord 		ata_port_freeze(ap);
164729d187bbSMark Lord 	}
164829d187bbSMark Lord 	sata_pmp_error_handler(ap);
164929d187bbSMark Lord }
165029d187bbSMark Lord 
16514c299ca3SMark Lord static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
16524c299ca3SMark Lord {
16534c299ca3SMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
16544c299ca3SMark Lord 
16554c299ca3SMark Lord 	return readl(port_mmio + SATA_TESTCTL_OFS) >> 16;
16564c299ca3SMark Lord }
16574c299ca3SMark Lord 
16584c299ca3SMark Lord static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
16594c299ca3SMark Lord {
16604c299ca3SMark Lord 	struct ata_eh_info *ehi;
16614c299ca3SMark Lord 	unsigned int pmp;
16624c299ca3SMark Lord 
16634c299ca3SMark Lord 	/*
16644c299ca3SMark Lord 	 * Initialize EH info for PMPs which saw device errors
16654c299ca3SMark Lord 	 */
16664c299ca3SMark Lord 	ehi = &ap->link.eh_info;
16674c299ca3SMark Lord 	for (pmp = 0; pmp_map != 0; pmp++) {
16684c299ca3SMark Lord 		unsigned int this_pmp = (1 << pmp);
16694c299ca3SMark Lord 		if (pmp_map & this_pmp) {
16704c299ca3SMark Lord 			struct ata_link *link = &ap->pmp_link[pmp];
16714c299ca3SMark Lord 
16724c299ca3SMark Lord 			pmp_map &= ~this_pmp;
16734c299ca3SMark Lord 			ehi = &link->eh_info;
16744c299ca3SMark Lord 			ata_ehi_clear_desc(ehi);
16754c299ca3SMark Lord 			ata_ehi_push_desc(ehi, "dev err");
16764c299ca3SMark Lord 			ehi->err_mask |= AC_ERR_DEV;
16774c299ca3SMark Lord 			ehi->action |= ATA_EH_RESET;
16784c299ca3SMark Lord 			ata_link_abort(link);
16794c299ca3SMark Lord 		}
16804c299ca3SMark Lord 	}
16814c299ca3SMark Lord }
16824c299ca3SMark Lord 
168306aaca3fSMark Lord static int mv_req_q_empty(struct ata_port *ap)
168406aaca3fSMark Lord {
168506aaca3fSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
168606aaca3fSMark Lord 	u32 in_ptr, out_ptr;
168706aaca3fSMark Lord 
168806aaca3fSMark Lord 	in_ptr  = (readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS)
168906aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
169006aaca3fSMark Lord 	out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
169106aaca3fSMark Lord 			>> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
169206aaca3fSMark Lord 	return (in_ptr == out_ptr);	/* 1 == queue_is_empty */
169306aaca3fSMark Lord }
169406aaca3fSMark Lord 
16954c299ca3SMark Lord static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
16964c299ca3SMark Lord {
16974c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
16984c299ca3SMark Lord 	int failed_links;
16994c299ca3SMark Lord 	unsigned int old_map, new_map;
17004c299ca3SMark Lord 
17014c299ca3SMark Lord 	/*
17024c299ca3SMark Lord 	 * Device error during FBS+NCQ operation:
17034c299ca3SMark Lord 	 *
17044c299ca3SMark Lord 	 * Set a port flag to prevent further I/O being enqueued.
17054c299ca3SMark Lord 	 * Leave the EDMA running to drain outstanding commands from this port.
17064c299ca3SMark Lord 	 * Perform the post-mortem/EH only when all responses are complete.
17074c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
17084c299ca3SMark Lord 	 */
17094c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
17104c299ca3SMark Lord 		pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
17114c299ca3SMark Lord 		pp->delayed_eh_pmp_map = 0;
17124c299ca3SMark Lord 	}
17134c299ca3SMark Lord 	old_map = pp->delayed_eh_pmp_map;
17144c299ca3SMark Lord 	new_map = old_map | mv_get_err_pmp_map(ap);
17154c299ca3SMark Lord 
17164c299ca3SMark Lord 	if (old_map != new_map) {
17174c299ca3SMark Lord 		pp->delayed_eh_pmp_map = new_map;
17184c299ca3SMark Lord 		mv_pmp_eh_prep(ap, new_map & ~old_map);
17194c299ca3SMark Lord 	}
1720c46938ccSMark Lord 	failed_links = hweight16(new_map);
17214c299ca3SMark Lord 
17224c299ca3SMark Lord 	ata_port_printk(ap, KERN_INFO, "%s: pmp_map=%04x qc_map=%04x "
17234c299ca3SMark Lord 			"failed_links=%d nr_active_links=%d\n",
17244c299ca3SMark Lord 			__func__, pp->delayed_eh_pmp_map,
17254c299ca3SMark Lord 			ap->qc_active, failed_links,
17264c299ca3SMark Lord 			ap->nr_active_links);
17274c299ca3SMark Lord 
172806aaca3fSMark Lord 	if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
17294c299ca3SMark Lord 		mv_process_crpb_entries(ap, pp);
17304c299ca3SMark Lord 		mv_stop_edma(ap);
17314c299ca3SMark Lord 		mv_eh_freeze(ap);
17324c299ca3SMark Lord 		ata_port_printk(ap, KERN_INFO, "%s: done\n", __func__);
17334c299ca3SMark Lord 		return 1;	/* handled */
17344c299ca3SMark Lord 	}
17354c299ca3SMark Lord 	ata_port_printk(ap, KERN_INFO, "%s: waiting\n", __func__);
17364c299ca3SMark Lord 	return 1;	/* handled */
17374c299ca3SMark Lord }
17384c299ca3SMark Lord 
17394c299ca3SMark Lord static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
17404c299ca3SMark Lord {
17414c299ca3SMark Lord 	/*
17424c299ca3SMark Lord 	 * Possible future enhancement:
17434c299ca3SMark Lord 	 *
17444c299ca3SMark Lord 	 * FBS+non-NCQ operation is not yet implemented.
17454c299ca3SMark Lord 	 * See related notes in mv_edma_cfg().
17464c299ca3SMark Lord 	 *
17474c299ca3SMark Lord 	 * Device error during FBS+non-NCQ operation:
17484c299ca3SMark Lord 	 *
17494c299ca3SMark Lord 	 * We need to snapshot the shadow registers for each failed command.
17504c299ca3SMark Lord 	 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
17514c299ca3SMark Lord 	 */
17524c299ca3SMark Lord 	return 0;	/* not handled */
17534c299ca3SMark Lord }
17544c299ca3SMark Lord 
17554c299ca3SMark Lord static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
17564c299ca3SMark Lord {
17574c299ca3SMark Lord 	struct mv_port_priv *pp = ap->private_data;
17584c299ca3SMark Lord 
17594c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
17604c299ca3SMark Lord 		return 0;	/* EDMA was not active: not handled */
17614c299ca3SMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
17624c299ca3SMark Lord 		return 0;	/* FBS was not active: not handled */
17634c299ca3SMark Lord 
17644c299ca3SMark Lord 	if (!(edma_err_cause & EDMA_ERR_DEV))
17654c299ca3SMark Lord 		return 0;	/* non DEV error: not handled */
17664c299ca3SMark Lord 	edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
17674c299ca3SMark Lord 	if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
17684c299ca3SMark Lord 		return 0;	/* other problems: not handled */
17694c299ca3SMark Lord 
17704c299ca3SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
17714c299ca3SMark Lord 		/*
17724c299ca3SMark Lord 		 * EDMA should NOT have self-disabled for this case.
17734c299ca3SMark Lord 		 * If it did, then something is wrong elsewhere,
17744c299ca3SMark Lord 		 * and we cannot handle it here.
17754c299ca3SMark Lord 		 */
17764c299ca3SMark Lord 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
17774c299ca3SMark Lord 			ata_port_printk(ap, KERN_WARNING,
17784c299ca3SMark Lord 				"%s: err_cause=0x%x pp_flags=0x%x\n",
17794c299ca3SMark Lord 				__func__, edma_err_cause, pp->pp_flags);
17804c299ca3SMark Lord 			return 0; /* not handled */
17814c299ca3SMark Lord 		}
17824c299ca3SMark Lord 		return mv_handle_fbs_ncq_dev_err(ap);
17834c299ca3SMark Lord 	} else {
17844c299ca3SMark Lord 		/*
17854c299ca3SMark Lord 		 * EDMA should have self-disabled for this case.
17864c299ca3SMark Lord 		 * If it did not, then something is wrong elsewhere,
17874c299ca3SMark Lord 		 * and we cannot handle it here.
17884c299ca3SMark Lord 		 */
17894c299ca3SMark Lord 		if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
17904c299ca3SMark Lord 			ata_port_printk(ap, KERN_WARNING,
17914c299ca3SMark Lord 				"%s: err_cause=0x%x pp_flags=0x%x\n",
17924c299ca3SMark Lord 				__func__, edma_err_cause, pp->pp_flags);
17934c299ca3SMark Lord 			return 0; /* not handled */
17944c299ca3SMark Lord 		}
17954c299ca3SMark Lord 		return mv_handle_fbs_non_ncq_dev_err(ap);
17964c299ca3SMark Lord 	}
17974c299ca3SMark Lord 	return 0;	/* not handled */
17984c299ca3SMark Lord }
17994c299ca3SMark Lord 
1800a9010329SMark Lord static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
18018f767f8aSMark Lord {
18028f767f8aSMark Lord 	struct ata_eh_info *ehi = &ap->link.eh_info;
1803a9010329SMark Lord 	char *when = "idle";
18048f767f8aSMark Lord 
18058f767f8aSMark Lord 	ata_ehi_clear_desc(ehi);
1806a9010329SMark Lord 	if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
1807a9010329SMark Lord 		when = "disabled";
1808a9010329SMark Lord 	} else if (edma_was_enabled) {
1809a9010329SMark Lord 		when = "EDMA enabled";
18108f767f8aSMark Lord 	} else {
18118f767f8aSMark Lord 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
18128f767f8aSMark Lord 		if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
1813a9010329SMark Lord 			when = "polling";
18148f767f8aSMark Lord 	}
1815a9010329SMark Lord 	ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
18168f767f8aSMark Lord 	ehi->err_mask |= AC_ERR_OTHER;
18178f767f8aSMark Lord 	ehi->action   |= ATA_EH_RESET;
18188f767f8aSMark Lord 	ata_port_freeze(ap);
18198f767f8aSMark Lord }
18208f767f8aSMark Lord 
1821c6fd2807SJeff Garzik /**
1822c6fd2807SJeff Garzik  *      mv_err_intr - Handle error interrupts on the port
1823c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1824c6fd2807SJeff Garzik  *
18258d07379dSMark Lord  *      Most cases require a full reset of the chip's state machine,
18268d07379dSMark Lord  *      which also performs a COMRESET.
18278d07379dSMark Lord  *      Also, if the port disabled DMA, update our cached copy to match.
1828c6fd2807SJeff Garzik  *
1829c6fd2807SJeff Garzik  *      LOCKING:
1830c6fd2807SJeff Garzik  *      Inherited from caller.
1831c6fd2807SJeff Garzik  */
183237b9046aSMark Lord static void mv_err_intr(struct ata_port *ap)
1833c6fd2807SJeff Garzik {
1834c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1835bdd4dddeSJeff Garzik 	u32 edma_err_cause, eh_freeze_mask, serr = 0;
1836e4006077SMark Lord 	u32 fis_cause = 0;
1837bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1838bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1839bdd4dddeSJeff Garzik 	unsigned int action = 0, err_mask = 0;
18409af5c9c9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
184137b9046aSMark Lord 	struct ata_queued_cmd *qc;
184237b9046aSMark Lord 	int abort = 0;
1843c6fd2807SJeff Garzik 
18448d07379dSMark Lord 	/*
184537b9046aSMark Lord 	 * Read and clear the SError and err_cause bits.
1846e4006077SMark Lord 	 * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
1847e4006077SMark Lord 	 * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
1848bdd4dddeSJeff Garzik 	 */
184937b9046aSMark Lord 	sata_scr_read(&ap->link, SCR_ERROR, &serr);
185037b9046aSMark Lord 	sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
185137b9046aSMark Lord 
1852bdd4dddeSJeff Garzik 	edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1853e4006077SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
1854e4006077SMark Lord 		fis_cause = readl(port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
1855e4006077SMark Lord 		writelfl(~fis_cause, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
1856e4006077SMark Lord 	}
18578d07379dSMark Lord 	writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1858bdd4dddeSJeff Garzik 
18594c299ca3SMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
18604c299ca3SMark Lord 		/*
18614c299ca3SMark Lord 		 * Device errors during FIS-based switching operation
18624c299ca3SMark Lord 		 * require special handling.
18634c299ca3SMark Lord 		 */
18644c299ca3SMark Lord 		if (mv_handle_dev_err(ap, edma_err_cause))
18654c299ca3SMark Lord 			return;
18664c299ca3SMark Lord 	}
18674c299ca3SMark Lord 
186837b9046aSMark Lord 	qc = mv_get_active_qc(ap);
186937b9046aSMark Lord 	ata_ehi_clear_desc(ehi);
187037b9046aSMark Lord 	ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
187137b9046aSMark Lord 			  edma_err_cause, pp->pp_flags);
1872e4006077SMark Lord 
1873c443c500SMark Lord 	if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
1874e4006077SMark Lord 		ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
1875c443c500SMark Lord 		if (fis_cause & SATA_FIS_IRQ_AN) {
1876c443c500SMark Lord 			u32 ec = edma_err_cause &
1877c443c500SMark Lord 			       ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
1878c443c500SMark Lord 			sata_async_notification(ap);
1879c443c500SMark Lord 			if (!ec)
1880c443c500SMark Lord 				return; /* Just an AN; no need for the nukes */
1881c443c500SMark Lord 			ata_ehi_push_desc(ehi, "SDB notify");
1882c443c500SMark Lord 		}
1883c443c500SMark Lord 	}
1884bdd4dddeSJeff Garzik 	/*
1885352fab70SMark Lord 	 * All generations share these EDMA error cause bits:
1886bdd4dddeSJeff Garzik 	 */
188737b9046aSMark Lord 	if (edma_err_cause & EDMA_ERR_DEV) {
1888bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_DEV;
188937b9046aSMark Lord 		action |= ATA_EH_RESET;
189037b9046aSMark Lord 		ata_ehi_push_desc(ehi, "dev error");
189137b9046aSMark Lord 	}
1892bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
18936c1153e0SJeff Garzik 			EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
1894bdd4dddeSJeff Garzik 			EDMA_ERR_INTRL_PAR)) {
1895bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1896cf480626STejun Heo 		action |= ATA_EH_RESET;
1897b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "parity error");
1898bdd4dddeSJeff Garzik 	}
1899bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
1900bdd4dddeSJeff Garzik 		ata_ehi_hotplugged(ehi);
1901bdd4dddeSJeff Garzik 		ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
1902b64bbc39STejun Heo 			"dev disconnect" : "dev connect");
1903cf480626STejun Heo 		action |= ATA_EH_RESET;
1904bdd4dddeSJeff Garzik 	}
1905bdd4dddeSJeff Garzik 
1906352fab70SMark Lord 	/*
1907352fab70SMark Lord 	 * Gen-I has a different SELF_DIS bit,
1908352fab70SMark Lord 	 * different FREEZE bits, and no SERR bit:
1909352fab70SMark Lord 	 */
1910ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv)) {
1911bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE_5;
1912bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
1913c6fd2807SJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1914b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
1915c6fd2807SJeff Garzik 		}
1916bdd4dddeSJeff Garzik 	} else {
1917bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE;
1918bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
1919bdd4dddeSJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1920b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
1921bdd4dddeSJeff Garzik 		}
1922bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SERR) {
19238d07379dSMark Lord 			ata_ehi_push_desc(ehi, "SError=%08x", serr);
19248d07379dSMark Lord 			err_mask |= AC_ERR_ATA_BUS;
1925cf480626STejun Heo 			action |= ATA_EH_RESET;
1926bdd4dddeSJeff Garzik 		}
1927bdd4dddeSJeff Garzik 	}
1928c6fd2807SJeff Garzik 
1929bdd4dddeSJeff Garzik 	if (!err_mask) {
1930bdd4dddeSJeff Garzik 		err_mask = AC_ERR_OTHER;
1931cf480626STejun Heo 		action |= ATA_EH_RESET;
1932bdd4dddeSJeff Garzik 	}
1933bdd4dddeSJeff Garzik 
1934bdd4dddeSJeff Garzik 	ehi->serror |= serr;
1935bdd4dddeSJeff Garzik 	ehi->action |= action;
1936bdd4dddeSJeff Garzik 
1937bdd4dddeSJeff Garzik 	if (qc)
1938bdd4dddeSJeff Garzik 		qc->err_mask |= err_mask;
1939bdd4dddeSJeff Garzik 	else
1940bdd4dddeSJeff Garzik 		ehi->err_mask |= err_mask;
1941bdd4dddeSJeff Garzik 
194237b9046aSMark Lord 	if (err_mask == AC_ERR_DEV) {
194337b9046aSMark Lord 		/*
194437b9046aSMark Lord 		 * Cannot do ata_port_freeze() here,
194537b9046aSMark Lord 		 * because it would kill PIO access,
194637b9046aSMark Lord 		 * which is needed for further diagnosis.
194737b9046aSMark Lord 		 */
194837b9046aSMark Lord 		mv_eh_freeze(ap);
194937b9046aSMark Lord 		abort = 1;
195037b9046aSMark Lord 	} else if (edma_err_cause & eh_freeze_mask) {
195137b9046aSMark Lord 		/*
195237b9046aSMark Lord 		 * Note to self: ata_port_freeze() calls ata_port_abort()
195337b9046aSMark Lord 		 */
1954bdd4dddeSJeff Garzik 		ata_port_freeze(ap);
195537b9046aSMark Lord 	} else {
195637b9046aSMark Lord 		abort = 1;
195737b9046aSMark Lord 	}
195837b9046aSMark Lord 
195937b9046aSMark Lord 	if (abort) {
196037b9046aSMark Lord 		if (qc)
196137b9046aSMark Lord 			ata_link_abort(qc->dev->link);
1962bdd4dddeSJeff Garzik 		else
1963bdd4dddeSJeff Garzik 			ata_port_abort(ap);
1964bdd4dddeSJeff Garzik 	}
196537b9046aSMark Lord }
1966bdd4dddeSJeff Garzik 
1967fcfb1f77SMark Lord static void mv_process_crpb_response(struct ata_port *ap,
1968fcfb1f77SMark Lord 		struct mv_crpb *response, unsigned int tag, int ncq_enabled)
1969fcfb1f77SMark Lord {
1970fcfb1f77SMark Lord 	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
1971fcfb1f77SMark Lord 
1972fcfb1f77SMark Lord 	if (qc) {
1973fcfb1f77SMark Lord 		u8 ata_status;
1974fcfb1f77SMark Lord 		u16 edma_status = le16_to_cpu(response->flags);
1975fcfb1f77SMark Lord 		/*
1976fcfb1f77SMark Lord 		 * edma_status from a response queue entry:
1977fcfb1f77SMark Lord 		 *   LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
1978fcfb1f77SMark Lord 		 *   MSB is saved ATA status from command completion.
1979fcfb1f77SMark Lord 		 */
1980fcfb1f77SMark Lord 		if (!ncq_enabled) {
1981fcfb1f77SMark Lord 			u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
1982fcfb1f77SMark Lord 			if (err_cause) {
1983fcfb1f77SMark Lord 				/*
1984fcfb1f77SMark Lord 				 * Error will be seen/handled by mv_err_intr().
1985fcfb1f77SMark Lord 				 * So do nothing at all here.
1986fcfb1f77SMark Lord 				 */
1987fcfb1f77SMark Lord 				return;
1988fcfb1f77SMark Lord 			}
1989fcfb1f77SMark Lord 		}
1990fcfb1f77SMark Lord 		ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
199137b9046aSMark Lord 		if (!ac_err_mask(ata_status))
1992fcfb1f77SMark Lord 			ata_qc_complete(qc);
199337b9046aSMark Lord 		/* else: leave it for mv_err_intr() */
1994fcfb1f77SMark Lord 	} else {
1995fcfb1f77SMark Lord 		ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
1996fcfb1f77SMark Lord 				__func__, tag);
1997fcfb1f77SMark Lord 	}
1998fcfb1f77SMark Lord }
1999fcfb1f77SMark Lord 
2000fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
2001bdd4dddeSJeff Garzik {
2002bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2003bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2004fcfb1f77SMark Lord 	u32 in_index;
2005bdd4dddeSJeff Garzik 	bool work_done = false;
2006fcfb1f77SMark Lord 	int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
2007bdd4dddeSJeff Garzik 
2008fcfb1f77SMark Lord 	/* Get the hardware queue position index */
2009bdd4dddeSJeff Garzik 	in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
2010bdd4dddeSJeff Garzik 			>> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2011bdd4dddeSJeff Garzik 
2012fcfb1f77SMark Lord 	/* Process new responses from since the last time we looked */
2013fcfb1f77SMark Lord 	while (in_index != pp->resp_idx) {
20146c1153e0SJeff Garzik 		unsigned int tag;
2015fcfb1f77SMark Lord 		struct mv_crpb *response = &pp->crpb[pp->resp_idx];
2016bdd4dddeSJeff Garzik 
2017fcfb1f77SMark Lord 		pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2018bdd4dddeSJeff Garzik 
2019fcfb1f77SMark Lord 		if (IS_GEN_I(hpriv)) {
2020fcfb1f77SMark Lord 			/* 50xx: no NCQ, only one command active at a time */
20219af5c9c9STejun Heo 			tag = ap->link.active_tag;
2022fcfb1f77SMark Lord 		} else {
2023fcfb1f77SMark Lord 			/* Gen II/IIE: get command tag from CRPB entry */
2024fcfb1f77SMark Lord 			tag = le16_to_cpu(response->id) & 0x1f;
2025bdd4dddeSJeff Garzik 		}
2026fcfb1f77SMark Lord 		mv_process_crpb_response(ap, response, tag, ncq_enabled);
2027bdd4dddeSJeff Garzik 		work_done = true;
2028bdd4dddeSJeff Garzik 	}
2029bdd4dddeSJeff Garzik 
2030352fab70SMark Lord 	/* Update the software queue position index in hardware */
2031bdd4dddeSJeff Garzik 	if (work_done)
2032bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
2033fcfb1f77SMark Lord 			 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
2034bdd4dddeSJeff Garzik 			 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
2035c6fd2807SJeff Garzik }
2036c6fd2807SJeff Garzik 
2037a9010329SMark Lord static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2038a9010329SMark Lord {
2039a9010329SMark Lord 	struct mv_port_priv *pp;
2040a9010329SMark Lord 	int edma_was_enabled;
2041a9010329SMark Lord 
2042a9010329SMark Lord 	if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
2043a9010329SMark Lord 		mv_unexpected_intr(ap, 0);
2044a9010329SMark Lord 		return;
2045a9010329SMark Lord 	}
2046a9010329SMark Lord 	/*
2047a9010329SMark Lord 	 * Grab a snapshot of the EDMA_EN flag setting,
2048a9010329SMark Lord 	 * so that we have a consistent view for this port,
2049a9010329SMark Lord 	 * even if something we call of our routines changes it.
2050a9010329SMark Lord 	 */
2051a9010329SMark Lord 	pp = ap->private_data;
2052a9010329SMark Lord 	edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2053a9010329SMark Lord 	/*
2054a9010329SMark Lord 	 * Process completed CRPB response(s) before other events.
2055a9010329SMark Lord 	 */
2056a9010329SMark Lord 	if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2057a9010329SMark Lord 		mv_process_crpb_entries(ap, pp);
20584c299ca3SMark Lord 		if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
20594c299ca3SMark Lord 			mv_handle_fbs_ncq_dev_err(ap);
2060a9010329SMark Lord 	}
2061a9010329SMark Lord 	/*
2062a9010329SMark Lord 	 * Handle chip-reported errors, or continue on to handle PIO.
2063a9010329SMark Lord 	 */
2064a9010329SMark Lord 	if (unlikely(port_cause & ERR_IRQ)) {
2065a9010329SMark Lord 		mv_err_intr(ap);
2066a9010329SMark Lord 	} else if (!edma_was_enabled) {
2067a9010329SMark Lord 		struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2068a9010329SMark Lord 		if (qc)
2069a9010329SMark Lord 			ata_sff_host_intr(ap, qc);
2070a9010329SMark Lord 		else
2071a9010329SMark Lord 			mv_unexpected_intr(ap, edma_was_enabled);
2072a9010329SMark Lord 	}
2073a9010329SMark Lord }
2074a9010329SMark Lord 
2075c6fd2807SJeff Garzik /**
2076c6fd2807SJeff Garzik  *      mv_host_intr - Handle all interrupts on the given host controller
2077cca3974eSJeff Garzik  *      @host: host specific structure
20787368f919SMark Lord  *      @main_irq_cause: Main interrupt cause register for the chip.
2079c6fd2807SJeff Garzik  *
2080c6fd2807SJeff Garzik  *      LOCKING:
2081c6fd2807SJeff Garzik  *      Inherited from caller.
2082c6fd2807SJeff Garzik  */
20837368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
2084c6fd2807SJeff Garzik {
2085f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2086eabd5eb1SMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio;
2087a3718c1fSMark Lord 	unsigned int handled = 0, port;
2088c6fd2807SJeff Garzik 
2089a3718c1fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
2090cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[port];
2091eabd5eb1SMark Lord 		unsigned int p, shift, hardport, port_cause;
2092eabd5eb1SMark Lord 
2093a3718c1fSMark Lord 		MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2094a3718c1fSMark Lord 		/*
2095eabd5eb1SMark Lord 		 * Each hc within the host has its own hc_irq_cause register,
2096eabd5eb1SMark Lord 		 * where the interrupting ports bits get ack'd.
2097a3718c1fSMark Lord 		 */
2098eabd5eb1SMark Lord 		if (hardport == 0) {	/* first port on this hc ? */
2099eabd5eb1SMark Lord 			u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2100eabd5eb1SMark Lord 			u32 port_mask, ack_irqs;
2101eabd5eb1SMark Lord 			/*
2102eabd5eb1SMark Lord 			 * Skip this entire hc if nothing pending for any ports
2103eabd5eb1SMark Lord 			 */
2104eabd5eb1SMark Lord 			if (!hc_cause) {
2105eabd5eb1SMark Lord 				port += MV_PORTS_PER_HC - 1;
2106eabd5eb1SMark Lord 				continue;
2107eabd5eb1SMark Lord 			}
2108eabd5eb1SMark Lord 			/*
2109eabd5eb1SMark Lord 			 * We don't need/want to read the hc_irq_cause register,
2110eabd5eb1SMark Lord 			 * because doing so hurts performance, and
2111eabd5eb1SMark Lord 			 * main_irq_cause already gives us everything we need.
2112eabd5eb1SMark Lord 			 *
2113eabd5eb1SMark Lord 			 * But we do have to *write* to the hc_irq_cause to ack
2114eabd5eb1SMark Lord 			 * the ports that we are handling this time through.
2115eabd5eb1SMark Lord 			 *
2116eabd5eb1SMark Lord 			 * This requires that we create a bitmap for those
2117eabd5eb1SMark Lord 			 * ports which interrupted us, and use that bitmap
2118eabd5eb1SMark Lord 			 * to ack (only) those ports via hc_irq_cause.
2119eabd5eb1SMark Lord 			 */
2120eabd5eb1SMark Lord 			ack_irqs = 0;
2121eabd5eb1SMark Lord 			for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2122eabd5eb1SMark Lord 				if ((port + p) >= hpriv->n_ports)
2123eabd5eb1SMark Lord 					break;
2124eabd5eb1SMark Lord 				port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2125eabd5eb1SMark Lord 				if (hc_cause & port_mask)
2126eabd5eb1SMark Lord 					ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2127eabd5eb1SMark Lord 			}
2128a3718c1fSMark Lord 			hc_mmio = mv_hc_base_from_port(mmio, port);
2129eabd5eb1SMark Lord 			writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE_OFS);
2130a3718c1fSMark Lord 			handled = 1;
2131a3718c1fSMark Lord 		}
2132a9010329SMark Lord 		/*
2133a9010329SMark Lord 		 * Handle interrupts signalled for this port:
2134a9010329SMark Lord 		 */
2135eabd5eb1SMark Lord 		port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
2136a9010329SMark Lord 		if (port_cause)
2137a9010329SMark Lord 			mv_port_intr(ap, port_cause);
2138eabd5eb1SMark Lord 	}
2139a3718c1fSMark Lord 	return handled;
2140c6fd2807SJeff Garzik }
2141c6fd2807SJeff Garzik 
2142a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
2143bdd4dddeSJeff Garzik {
214402a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2145bdd4dddeSJeff Garzik 	struct ata_port *ap;
2146bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
2147bdd4dddeSJeff Garzik 	struct ata_eh_info *ehi;
2148bdd4dddeSJeff Garzik 	unsigned int i, err_mask, printed = 0;
2149bdd4dddeSJeff Garzik 	u32 err_cause;
2150bdd4dddeSJeff Garzik 
215102a121daSMark Lord 	err_cause = readl(mmio + hpriv->irq_cause_ofs);
2152bdd4dddeSJeff Garzik 
2153bdd4dddeSJeff Garzik 	dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
2154bdd4dddeSJeff Garzik 		   err_cause);
2155bdd4dddeSJeff Garzik 
2156bdd4dddeSJeff Garzik 	DPRINTK("All regs @ PCI error\n");
2157bdd4dddeSJeff Garzik 	mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
2158bdd4dddeSJeff Garzik 
215902a121daSMark Lord 	writelfl(0, mmio + hpriv->irq_cause_ofs);
2160bdd4dddeSJeff Garzik 
2161bdd4dddeSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
2162bdd4dddeSJeff Garzik 		ap = host->ports[i];
2163936fd732STejun Heo 		if (!ata_link_offline(&ap->link)) {
21649af5c9c9STejun Heo 			ehi = &ap->link.eh_info;
2165bdd4dddeSJeff Garzik 			ata_ehi_clear_desc(ehi);
2166bdd4dddeSJeff Garzik 			if (!printed++)
2167bdd4dddeSJeff Garzik 				ata_ehi_push_desc(ehi,
2168bdd4dddeSJeff Garzik 					"PCI err cause 0x%08x", err_cause);
2169bdd4dddeSJeff Garzik 			err_mask = AC_ERR_HOST_BUS;
2170cf480626STejun Heo 			ehi->action = ATA_EH_RESET;
21719af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
2172bdd4dddeSJeff Garzik 			if (qc)
2173bdd4dddeSJeff Garzik 				qc->err_mask |= err_mask;
2174bdd4dddeSJeff Garzik 			else
2175bdd4dddeSJeff Garzik 				ehi->err_mask |= err_mask;
2176bdd4dddeSJeff Garzik 
2177bdd4dddeSJeff Garzik 			ata_port_freeze(ap);
2178bdd4dddeSJeff Garzik 		}
2179bdd4dddeSJeff Garzik 	}
2180a3718c1fSMark Lord 	return 1;	/* handled */
2181bdd4dddeSJeff Garzik }
2182bdd4dddeSJeff Garzik 
2183c6fd2807SJeff Garzik /**
2184c5d3e45aSJeff Garzik  *      mv_interrupt - Main interrupt event handler
2185c6fd2807SJeff Garzik  *      @irq: unused
2186c6fd2807SJeff Garzik  *      @dev_instance: private data; in this case the host structure
2187c6fd2807SJeff Garzik  *
2188c6fd2807SJeff Garzik  *      Read the read only register to determine if any host
2189c6fd2807SJeff Garzik  *      controllers have pending interrupts.  If so, call lower level
2190c6fd2807SJeff Garzik  *      routine to handle.  Also check for PCI errors which are only
2191c6fd2807SJeff Garzik  *      reported here.
2192c6fd2807SJeff Garzik  *
2193c6fd2807SJeff Garzik  *      LOCKING:
2194cca3974eSJeff Garzik  *      This routine holds the host lock while processing pending
2195c6fd2807SJeff Garzik  *      interrupts.
2196c6fd2807SJeff Garzik  */
21977d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
2198c6fd2807SJeff Garzik {
2199cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
2200f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
2201a3718c1fSMark Lord 	unsigned int handled = 0;
220296e2c487SMark Lord 	u32 main_irq_cause, pending_irqs;
2203c6fd2807SJeff Garzik 
2204646a4da5SMark Lord 	spin_lock(&host->lock);
22057368f919SMark Lord 	main_irq_cause = readl(hpriv->main_irq_cause_addr);
220696e2c487SMark Lord 	pending_irqs   = main_irq_cause & hpriv->main_irq_mask;
2207352fab70SMark Lord 	/*
2208352fab70SMark Lord 	 * Deal with cases where we either have nothing pending, or have read
2209352fab70SMark Lord 	 * a bogus register value which can indicate HW removal or PCI fault.
2210c6fd2807SJeff Garzik 	 */
2211a44253d2SMark Lord 	if (pending_irqs && main_irq_cause != 0xffffffffU) {
22121f398472SMark Lord 		if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
2213a3718c1fSMark Lord 			handled = mv_pci_error(host, hpriv->base);
2214a3718c1fSMark Lord 		else
2215a44253d2SMark Lord 			handled = mv_host_intr(host, pending_irqs);
2216bdd4dddeSJeff Garzik 	}
2217cca3974eSJeff Garzik 	spin_unlock(&host->lock);
2218c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
2219c6fd2807SJeff Garzik }
2220c6fd2807SJeff Garzik 
2221c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
2222c6fd2807SJeff Garzik {
2223c6fd2807SJeff Garzik 	unsigned int ofs;
2224c6fd2807SJeff Garzik 
2225c6fd2807SJeff Garzik 	switch (sc_reg_in) {
2226c6fd2807SJeff Garzik 	case SCR_STATUS:
2227c6fd2807SJeff Garzik 	case SCR_ERROR:
2228c6fd2807SJeff Garzik 	case SCR_CONTROL:
2229c6fd2807SJeff Garzik 		ofs = sc_reg_in * sizeof(u32);
2230c6fd2807SJeff Garzik 		break;
2231c6fd2807SJeff Garzik 	default:
2232c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
2233c6fd2807SJeff Garzik 		break;
2234c6fd2807SJeff Garzik 	}
2235c6fd2807SJeff Garzik 	return ofs;
2236c6fd2807SJeff Garzik }
2237c6fd2807SJeff Garzik 
223882ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
2239c6fd2807SJeff Garzik {
224082ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
2241f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
224282ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
2243c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
2244c6fd2807SJeff Garzik 
2245da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
2246da3dbb17STejun Heo 		*val = readl(addr + ofs);
2247da3dbb17STejun Heo 		return 0;
2248da3dbb17STejun Heo 	} else
2249da3dbb17STejun Heo 		return -EINVAL;
2250c6fd2807SJeff Garzik }
2251c6fd2807SJeff Garzik 
225282ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
2253c6fd2807SJeff Garzik {
225482ef04fbSTejun Heo 	struct mv_host_priv *hpriv = link->ap->host->private_data;
2255f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
225682ef04fbSTejun Heo 	void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
2257c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
2258c6fd2807SJeff Garzik 
2259da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
22600d5ff566STejun Heo 		writelfl(val, addr + ofs);
2261da3dbb17STejun Heo 		return 0;
2262da3dbb17STejun Heo 	} else
2263da3dbb17STejun Heo 		return -EINVAL;
2264c6fd2807SJeff Garzik }
2265c6fd2807SJeff Garzik 
22667bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
2267c6fd2807SJeff Garzik {
22687bb3c529SSaeed Bishara 	struct pci_dev *pdev = to_pci_dev(host->dev);
2269c6fd2807SJeff Garzik 	int early_5080;
2270c6fd2807SJeff Garzik 
227144c10138SAuke Kok 	early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
2272c6fd2807SJeff Garzik 
2273c6fd2807SJeff Garzik 	if (!early_5080) {
2274c6fd2807SJeff Garzik 		u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
2275c6fd2807SJeff Garzik 		tmp |= (1 << 0);
2276c6fd2807SJeff Garzik 		writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
2277c6fd2807SJeff Garzik 	}
2278c6fd2807SJeff Garzik 
22797bb3c529SSaeed Bishara 	mv_reset_pci_bus(host, mmio);
2280c6fd2807SJeff Garzik }
2281c6fd2807SJeff Garzik 
2282c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2283c6fd2807SJeff Garzik {
22848e7decdbSMark Lord 	writel(0x0fcfffff, mmio + MV_FLASH_CTL_OFS);
2285c6fd2807SJeff Garzik }
2286c6fd2807SJeff Garzik 
2287c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
2288c6fd2807SJeff Garzik 			   void __iomem *mmio)
2289c6fd2807SJeff Garzik {
2290c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
2291c6fd2807SJeff Garzik 	u32 tmp;
2292c6fd2807SJeff Garzik 
2293c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
2294c6fd2807SJeff Garzik 
2295c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0x1800;	/* bits 12:11 */
2296c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0xe0;	/* bits 7:5 */
2297c6fd2807SJeff Garzik }
2298c6fd2807SJeff Garzik 
2299c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
2300c6fd2807SJeff Garzik {
2301c6fd2807SJeff Garzik 	u32 tmp;
2302c6fd2807SJeff Garzik 
23038e7decdbSMark Lord 	writel(0, mmio + MV_GPIO_PORT_CTL_OFS);
2304c6fd2807SJeff Garzik 
2305c6fd2807SJeff Garzik 	/* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
2306c6fd2807SJeff Garzik 
2307c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
2308c6fd2807SJeff Garzik 	tmp |= ~(1 << 0);
2309c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
2310c6fd2807SJeff Garzik }
2311c6fd2807SJeff Garzik 
2312c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2313c6fd2807SJeff Garzik 			   unsigned int port)
2314c6fd2807SJeff Garzik {
2315c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, port);
2316c6fd2807SJeff Garzik 	const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
2317c6fd2807SJeff Garzik 	u32 tmp;
2318c6fd2807SJeff Garzik 	int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
2319c6fd2807SJeff Garzik 
2320c6fd2807SJeff Garzik 	if (fix_apm_sq) {
23218e7decdbSMark Lord 		tmp = readl(phy_mmio + MV5_LTMODE_OFS);
2322c6fd2807SJeff Garzik 		tmp |= (1 << 19);
23238e7decdbSMark Lord 		writel(tmp, phy_mmio + MV5_LTMODE_OFS);
2324c6fd2807SJeff Garzik 
23258e7decdbSMark Lord 		tmp = readl(phy_mmio + MV5_PHY_CTL_OFS);
2326c6fd2807SJeff Garzik 		tmp &= ~0x3;
2327c6fd2807SJeff Garzik 		tmp |= 0x1;
23288e7decdbSMark Lord 		writel(tmp, phy_mmio + MV5_PHY_CTL_OFS);
2329c6fd2807SJeff Garzik 	}
2330c6fd2807SJeff Garzik 
2331c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
2332c6fd2807SJeff Garzik 	tmp &= ~mask;
2333c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].pre;
2334c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].amps;
2335c6fd2807SJeff Garzik 	writel(tmp, phy_mmio + MV5_PHY_MODE);
2336c6fd2807SJeff Garzik }
2337c6fd2807SJeff Garzik 
2338c6fd2807SJeff Garzik 
2339c6fd2807SJeff Garzik #undef ZERO
2340c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
2341c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
2342c6fd2807SJeff Garzik 			     unsigned int port)
2343c6fd2807SJeff Garzik {
2344c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
2345c6fd2807SJeff Garzik 
2346e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
2347c6fd2807SJeff Garzik 
2348c6fd2807SJeff Garzik 	ZERO(0x028);	/* command */
2349c6fd2807SJeff Garzik 	writel(0x11f, port_mmio + EDMA_CFG_OFS);
2350c6fd2807SJeff Garzik 	ZERO(0x004);	/* timer */
2351c6fd2807SJeff Garzik 	ZERO(0x008);	/* irq err cause */
2352c6fd2807SJeff Garzik 	ZERO(0x00c);	/* irq err mask */
2353c6fd2807SJeff Garzik 	ZERO(0x010);	/* rq bah */
2354c6fd2807SJeff Garzik 	ZERO(0x014);	/* rq inp */
2355c6fd2807SJeff Garzik 	ZERO(0x018);	/* rq outp */
2356c6fd2807SJeff Garzik 	ZERO(0x01c);	/* respq bah */
2357c6fd2807SJeff Garzik 	ZERO(0x024);	/* respq outp */
2358c6fd2807SJeff Garzik 	ZERO(0x020);	/* respq inp */
2359c6fd2807SJeff Garzik 	ZERO(0x02c);	/* test control */
23608e7decdbSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
2361c6fd2807SJeff Garzik }
2362c6fd2807SJeff Garzik #undef ZERO
2363c6fd2807SJeff Garzik 
2364c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
2365c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2366c6fd2807SJeff Garzik 			unsigned int hc)
2367c6fd2807SJeff Garzik {
2368c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2369c6fd2807SJeff Garzik 	u32 tmp;
2370c6fd2807SJeff Garzik 
2371c6fd2807SJeff Garzik 	ZERO(0x00c);
2372c6fd2807SJeff Garzik 	ZERO(0x010);
2373c6fd2807SJeff Garzik 	ZERO(0x014);
2374c6fd2807SJeff Garzik 	ZERO(0x018);
2375c6fd2807SJeff Garzik 
2376c6fd2807SJeff Garzik 	tmp = readl(hc_mmio + 0x20);
2377c6fd2807SJeff Garzik 	tmp &= 0x1c1c1c1c;
2378c6fd2807SJeff Garzik 	tmp |= 0x03030303;
2379c6fd2807SJeff Garzik 	writel(tmp, hc_mmio + 0x20);
2380c6fd2807SJeff Garzik }
2381c6fd2807SJeff Garzik #undef ZERO
2382c6fd2807SJeff Garzik 
2383c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2384c6fd2807SJeff Garzik 			unsigned int n_hc)
2385c6fd2807SJeff Garzik {
2386c6fd2807SJeff Garzik 	unsigned int hc, port;
2387c6fd2807SJeff Garzik 
2388c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
2389c6fd2807SJeff Garzik 		for (port = 0; port < MV_PORTS_PER_HC; port++)
2390c6fd2807SJeff Garzik 			mv5_reset_hc_port(hpriv, mmio,
2391c6fd2807SJeff Garzik 					  (hc * MV_PORTS_PER_HC) + port);
2392c6fd2807SJeff Garzik 
2393c6fd2807SJeff Garzik 		mv5_reset_one_hc(hpriv, mmio, hc);
2394c6fd2807SJeff Garzik 	}
2395c6fd2807SJeff Garzik 
2396c6fd2807SJeff Garzik 	return 0;
2397c6fd2807SJeff Garzik }
2398c6fd2807SJeff Garzik 
2399c6fd2807SJeff Garzik #undef ZERO
2400c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
24017bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
2402c6fd2807SJeff Garzik {
240302a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2404c6fd2807SJeff Garzik 	u32 tmp;
2405c6fd2807SJeff Garzik 
24068e7decdbSMark Lord 	tmp = readl(mmio + MV_PCI_MODE_OFS);
2407c6fd2807SJeff Garzik 	tmp &= 0xff00ffff;
24088e7decdbSMark Lord 	writel(tmp, mmio + MV_PCI_MODE_OFS);
2409c6fd2807SJeff Garzik 
2410c6fd2807SJeff Garzik 	ZERO(MV_PCI_DISC_TIMER);
2411c6fd2807SJeff Garzik 	ZERO(MV_PCI_MSI_TRIGGER);
24128e7decdbSMark Lord 	writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT_OFS);
2413c6fd2807SJeff Garzik 	ZERO(MV_PCI_SERR_MASK);
241402a121daSMark Lord 	ZERO(hpriv->irq_cause_ofs);
241502a121daSMark Lord 	ZERO(hpriv->irq_mask_ofs);
2416c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_LOW_ADDRESS);
2417c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_HIGH_ADDRESS);
2418c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_ATTRIBUTE);
2419c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_COMMAND);
2420c6fd2807SJeff Garzik }
2421c6fd2807SJeff Garzik #undef ZERO
2422c6fd2807SJeff Garzik 
2423c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2424c6fd2807SJeff Garzik {
2425c6fd2807SJeff Garzik 	u32 tmp;
2426c6fd2807SJeff Garzik 
2427c6fd2807SJeff Garzik 	mv5_reset_flash(hpriv, mmio);
2428c6fd2807SJeff Garzik 
24298e7decdbSMark Lord 	tmp = readl(mmio + MV_GPIO_PORT_CTL_OFS);
2430c6fd2807SJeff Garzik 	tmp &= 0x3;
2431c6fd2807SJeff Garzik 	tmp |= (1 << 5) | (1 << 6);
24328e7decdbSMark Lord 	writel(tmp, mmio + MV_GPIO_PORT_CTL_OFS);
2433c6fd2807SJeff Garzik }
2434c6fd2807SJeff Garzik 
2435c6fd2807SJeff Garzik /**
2436c6fd2807SJeff Garzik  *      mv6_reset_hc - Perform the 6xxx global soft reset
2437c6fd2807SJeff Garzik  *      @mmio: base address of the HBA
2438c6fd2807SJeff Garzik  *
2439c6fd2807SJeff Garzik  *      This routine only applies to 6xxx parts.
2440c6fd2807SJeff Garzik  *
2441c6fd2807SJeff Garzik  *      LOCKING:
2442c6fd2807SJeff Garzik  *      Inherited from caller.
2443c6fd2807SJeff Garzik  */
2444c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2445c6fd2807SJeff Garzik 			unsigned int n_hc)
2446c6fd2807SJeff Garzik {
2447c6fd2807SJeff Garzik 	void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
2448c6fd2807SJeff Garzik 	int i, rc = 0;
2449c6fd2807SJeff Garzik 	u32 t;
2450c6fd2807SJeff Garzik 
2451c6fd2807SJeff Garzik 	/* Following procedure defined in PCI "main command and status
2452c6fd2807SJeff Garzik 	 * register" table.
2453c6fd2807SJeff Garzik 	 */
2454c6fd2807SJeff Garzik 	t = readl(reg);
2455c6fd2807SJeff Garzik 	writel(t | STOP_PCI_MASTER, reg);
2456c6fd2807SJeff Garzik 
2457c6fd2807SJeff Garzik 	for (i = 0; i < 1000; i++) {
2458c6fd2807SJeff Garzik 		udelay(1);
2459c6fd2807SJeff Garzik 		t = readl(reg);
24602dcb407eSJeff Garzik 		if (PCI_MASTER_EMPTY & t)
2461c6fd2807SJeff Garzik 			break;
2462c6fd2807SJeff Garzik 	}
2463c6fd2807SJeff Garzik 	if (!(PCI_MASTER_EMPTY & t)) {
2464c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
2465c6fd2807SJeff Garzik 		rc = 1;
2466c6fd2807SJeff Garzik 		goto done;
2467c6fd2807SJeff Garzik 	}
2468c6fd2807SJeff Garzik 
2469c6fd2807SJeff Garzik 	/* set reset */
2470c6fd2807SJeff Garzik 	i = 5;
2471c6fd2807SJeff Garzik 	do {
2472c6fd2807SJeff Garzik 		writel(t | GLOB_SFT_RST, reg);
2473c6fd2807SJeff Garzik 		t = readl(reg);
2474c6fd2807SJeff Garzik 		udelay(1);
2475c6fd2807SJeff Garzik 	} while (!(GLOB_SFT_RST & t) && (i-- > 0));
2476c6fd2807SJeff Garzik 
2477c6fd2807SJeff Garzik 	if (!(GLOB_SFT_RST & t)) {
2478c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't set global reset\n");
2479c6fd2807SJeff Garzik 		rc = 1;
2480c6fd2807SJeff Garzik 		goto done;
2481c6fd2807SJeff Garzik 	}
2482c6fd2807SJeff Garzik 
2483c6fd2807SJeff Garzik 	/* clear reset and *reenable the PCI master* (not mentioned in spec) */
2484c6fd2807SJeff Garzik 	i = 5;
2485c6fd2807SJeff Garzik 	do {
2486c6fd2807SJeff Garzik 		writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
2487c6fd2807SJeff Garzik 		t = readl(reg);
2488c6fd2807SJeff Garzik 		udelay(1);
2489c6fd2807SJeff Garzik 	} while ((GLOB_SFT_RST & t) && (i-- > 0));
2490c6fd2807SJeff Garzik 
2491c6fd2807SJeff Garzik 	if (GLOB_SFT_RST & t) {
2492c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
2493c6fd2807SJeff Garzik 		rc = 1;
2494c6fd2807SJeff Garzik 	}
2495c6fd2807SJeff Garzik done:
2496c6fd2807SJeff Garzik 	return rc;
2497c6fd2807SJeff Garzik }
2498c6fd2807SJeff Garzik 
2499c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
2500c6fd2807SJeff Garzik 			   void __iomem *mmio)
2501c6fd2807SJeff Garzik {
2502c6fd2807SJeff Garzik 	void __iomem *port_mmio;
2503c6fd2807SJeff Garzik 	u32 tmp;
2504c6fd2807SJeff Garzik 
25058e7decdbSMark Lord 	tmp = readl(mmio + MV_RESET_CFG_OFS);
2506c6fd2807SJeff Garzik 	if ((tmp & (1 << 0)) == 0) {
2507c6fd2807SJeff Garzik 		hpriv->signal[idx].amps = 0x7 << 8;
2508c6fd2807SJeff Garzik 		hpriv->signal[idx].pre = 0x1 << 5;
2509c6fd2807SJeff Garzik 		return;
2510c6fd2807SJeff Garzik 	}
2511c6fd2807SJeff Garzik 
2512c6fd2807SJeff Garzik 	port_mmio = mv_port_base(mmio, idx);
2513c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE2);
2514c6fd2807SJeff Garzik 
2515c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
2516c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
2517c6fd2807SJeff Garzik }
2518c6fd2807SJeff Garzik 
2519c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
2520c6fd2807SJeff Garzik {
25218e7decdbSMark Lord 	writel(0x00000060, mmio + MV_GPIO_PORT_CTL_OFS);
2522c6fd2807SJeff Garzik }
2523c6fd2807SJeff Garzik 
2524c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2525c6fd2807SJeff Garzik 			   unsigned int port)
2526c6fd2807SJeff Garzik {
2527c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
2528c6fd2807SJeff Garzik 
2529c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
2530c6fd2807SJeff Garzik 	int fix_phy_mode2 =
2531c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
2532c6fd2807SJeff Garzik 	int fix_phy_mode4 =
2533c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
25348c30a8b9SMark Lord 	u32 m2, m3;
2535c6fd2807SJeff Garzik 
2536c6fd2807SJeff Garzik 	if (fix_phy_mode2) {
2537c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
2538c6fd2807SJeff Garzik 		m2 &= ~(1 << 16);
2539c6fd2807SJeff Garzik 		m2 |= (1 << 31);
2540c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
2541c6fd2807SJeff Garzik 
2542c6fd2807SJeff Garzik 		udelay(200);
2543c6fd2807SJeff Garzik 
2544c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
2545c6fd2807SJeff Garzik 		m2 &= ~((1 << 16) | (1 << 31));
2546c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
2547c6fd2807SJeff Garzik 
2548c6fd2807SJeff Garzik 		udelay(200);
2549c6fd2807SJeff Garzik 	}
2550c6fd2807SJeff Garzik 
25518c30a8b9SMark Lord 	/*
25528c30a8b9SMark Lord 	 * Gen-II/IIe PHY_MODE3 errata RM#2:
25538c30a8b9SMark Lord 	 * Achieves better receiver noise performance than the h/w default:
25548c30a8b9SMark Lord 	 */
25558c30a8b9SMark Lord 	m3 = readl(port_mmio + PHY_MODE3);
25568c30a8b9SMark Lord 	m3 = (m3 & 0x1f) | (0x5555601 << 5);
2557c6fd2807SJeff Garzik 
25580388a8c0SMark Lord 	/* Guideline 88F5182 (GL# SATA-S11) */
25590388a8c0SMark Lord 	if (IS_SOC(hpriv))
25600388a8c0SMark Lord 		m3 &= ~0x1c;
25610388a8c0SMark Lord 
2562c6fd2807SJeff Garzik 	if (fix_phy_mode4) {
2563ba069e37SMark Lord 		u32 m4 = readl(port_mmio + PHY_MODE4);
2564ba069e37SMark Lord 		/*
2565ba069e37SMark Lord 		 * Enforce reserved-bit restrictions on GenIIe devices only.
2566ba069e37SMark Lord 		 * For earlier chipsets, force only the internal config field
2567ba069e37SMark Lord 		 *  (workaround for errata FEr SATA#10 part 1).
2568ba069e37SMark Lord 		 */
25698c30a8b9SMark Lord 		if (IS_GEN_IIE(hpriv))
2570ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
2571ba069e37SMark Lord 		else
2572ba069e37SMark Lord 			m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
25738c30a8b9SMark Lord 		writel(m4, port_mmio + PHY_MODE4);
2574c6fd2807SJeff Garzik 	}
2575b406c7a6SMark Lord 	/*
2576b406c7a6SMark Lord 	 * Workaround for 60x1-B2 errata SATA#13:
2577b406c7a6SMark Lord 	 * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
2578b406c7a6SMark Lord 	 * so we must always rewrite PHY_MODE3 after PHY_MODE4.
2579b406c7a6SMark Lord 	 */
2580b406c7a6SMark Lord 	writel(m3, port_mmio + PHY_MODE3);
2581c6fd2807SJeff Garzik 
2582c6fd2807SJeff Garzik 	/* Revert values of pre-emphasis and signal amps to the saved ones */
2583c6fd2807SJeff Garzik 	m2 = readl(port_mmio + PHY_MODE2);
2584c6fd2807SJeff Garzik 
2585c6fd2807SJeff Garzik 	m2 &= ~MV_M2_PREAMP_MASK;
2586c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].amps;
2587c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].pre;
2588c6fd2807SJeff Garzik 	m2 &= ~(1 << 16);
2589c6fd2807SJeff Garzik 
2590c6fd2807SJeff Garzik 	/* according to mvSata 3.6.1, some IIE values are fixed */
2591c6fd2807SJeff Garzik 	if (IS_GEN_IIE(hpriv)) {
2592c6fd2807SJeff Garzik 		m2 &= ~0xC30FF01F;
2593c6fd2807SJeff Garzik 		m2 |= 0x0000900F;
2594c6fd2807SJeff Garzik 	}
2595c6fd2807SJeff Garzik 
2596c6fd2807SJeff Garzik 	writel(m2, port_mmio + PHY_MODE2);
2597c6fd2807SJeff Garzik }
2598c6fd2807SJeff Garzik 
2599f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
2600f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
2601f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
2602f351b2d6SSaeed Bishara 				      void __iomem *mmio)
2603f351b2d6SSaeed Bishara {
2604f351b2d6SSaeed Bishara 	return;
2605f351b2d6SSaeed Bishara }
2606f351b2d6SSaeed Bishara 
2607f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
2608f351b2d6SSaeed Bishara 			   void __iomem *mmio)
2609f351b2d6SSaeed Bishara {
2610f351b2d6SSaeed Bishara 	void __iomem *port_mmio;
2611f351b2d6SSaeed Bishara 	u32 tmp;
2612f351b2d6SSaeed Bishara 
2613f351b2d6SSaeed Bishara 	port_mmio = mv_port_base(mmio, idx);
2614f351b2d6SSaeed Bishara 	tmp = readl(port_mmio + PHY_MODE2);
2615f351b2d6SSaeed Bishara 
2616f351b2d6SSaeed Bishara 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
2617f351b2d6SSaeed Bishara 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
2618f351b2d6SSaeed Bishara }
2619f351b2d6SSaeed Bishara 
2620f351b2d6SSaeed Bishara #undef ZERO
2621f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
2622f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
2623f351b2d6SSaeed Bishara 					void __iomem *mmio, unsigned int port)
2624f351b2d6SSaeed Bishara {
2625f351b2d6SSaeed Bishara 	void __iomem *port_mmio = mv_port_base(mmio, port);
2626f351b2d6SSaeed Bishara 
2627e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
2628f351b2d6SSaeed Bishara 
2629f351b2d6SSaeed Bishara 	ZERO(0x028);		/* command */
2630f351b2d6SSaeed Bishara 	writel(0x101f, port_mmio + EDMA_CFG_OFS);
2631f351b2d6SSaeed Bishara 	ZERO(0x004);		/* timer */
2632f351b2d6SSaeed Bishara 	ZERO(0x008);		/* irq err cause */
2633f351b2d6SSaeed Bishara 	ZERO(0x00c);		/* irq err mask */
2634f351b2d6SSaeed Bishara 	ZERO(0x010);		/* rq bah */
2635f351b2d6SSaeed Bishara 	ZERO(0x014);		/* rq inp */
2636f351b2d6SSaeed Bishara 	ZERO(0x018);		/* rq outp */
2637f351b2d6SSaeed Bishara 	ZERO(0x01c);		/* respq bah */
2638f351b2d6SSaeed Bishara 	ZERO(0x024);		/* respq outp */
2639f351b2d6SSaeed Bishara 	ZERO(0x020);		/* respq inp */
2640f351b2d6SSaeed Bishara 	ZERO(0x02c);		/* test control */
26418e7decdbSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
2642f351b2d6SSaeed Bishara }
2643f351b2d6SSaeed Bishara 
2644f351b2d6SSaeed Bishara #undef ZERO
2645f351b2d6SSaeed Bishara 
2646f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
2647f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
2648f351b2d6SSaeed Bishara 				       void __iomem *mmio)
2649f351b2d6SSaeed Bishara {
2650f351b2d6SSaeed Bishara 	void __iomem *hc_mmio = mv_hc_base(mmio, 0);
2651f351b2d6SSaeed Bishara 
2652f351b2d6SSaeed Bishara 	ZERO(0x00c);
2653f351b2d6SSaeed Bishara 	ZERO(0x010);
2654f351b2d6SSaeed Bishara 	ZERO(0x014);
2655f351b2d6SSaeed Bishara 
2656f351b2d6SSaeed Bishara }
2657f351b2d6SSaeed Bishara 
2658f351b2d6SSaeed Bishara #undef ZERO
2659f351b2d6SSaeed Bishara 
2660f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
2661f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc)
2662f351b2d6SSaeed Bishara {
2663f351b2d6SSaeed Bishara 	unsigned int port;
2664f351b2d6SSaeed Bishara 
2665f351b2d6SSaeed Bishara 	for (port = 0; port < hpriv->n_ports; port++)
2666f351b2d6SSaeed Bishara 		mv_soc_reset_hc_port(hpriv, mmio, port);
2667f351b2d6SSaeed Bishara 
2668f351b2d6SSaeed Bishara 	mv_soc_reset_one_hc(hpriv, mmio);
2669f351b2d6SSaeed Bishara 
2670f351b2d6SSaeed Bishara 	return 0;
2671f351b2d6SSaeed Bishara }
2672f351b2d6SSaeed Bishara 
2673f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
2674f351b2d6SSaeed Bishara 				      void __iomem *mmio)
2675f351b2d6SSaeed Bishara {
2676f351b2d6SSaeed Bishara 	return;
2677f351b2d6SSaeed Bishara }
2678f351b2d6SSaeed Bishara 
2679f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
2680f351b2d6SSaeed Bishara {
2681f351b2d6SSaeed Bishara 	return;
2682f351b2d6SSaeed Bishara }
2683f351b2d6SSaeed Bishara 
26848e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
2685b67a1064SMark Lord {
26868e7decdbSMark Lord 	u32 ifcfg = readl(port_mmio + SATA_INTERFACE_CFG_OFS);
2687b67a1064SMark Lord 
26888e7decdbSMark Lord 	ifcfg = (ifcfg & 0xf7f) | 0x9b1000;	/* from chip spec */
2689b67a1064SMark Lord 	if (want_gen2i)
26908e7decdbSMark Lord 		ifcfg |= (1 << 7);		/* enable gen2i speed */
26918e7decdbSMark Lord 	writelfl(ifcfg, port_mmio + SATA_INTERFACE_CFG_OFS);
2692b67a1064SMark Lord }
2693b67a1064SMark Lord 
2694e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
2695c6fd2807SJeff Garzik 			     unsigned int port_no)
2696c6fd2807SJeff Garzik {
2697c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port_no);
2698c6fd2807SJeff Garzik 
26998e7decdbSMark Lord 	/*
27008e7decdbSMark Lord 	 * The datasheet warns against setting EDMA_RESET when EDMA is active
27018e7decdbSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
27028e7decdbSMark Lord 	 * to disable the EDMA engine before doing the EDMA_RESET operation.
27038e7decdbSMark Lord 	 */
27040d8be5cbSMark Lord 	mv_stop_edma_engine(port_mmio);
27058e7decdbSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
2706c6fd2807SJeff Garzik 
2707b67a1064SMark Lord 	if (!IS_GEN_I(hpriv)) {
27088e7decdbSMark Lord 		/* Enable 3.0gb/s link speed: this survives EDMA_RESET */
27098e7decdbSMark Lord 		mv_setup_ifcfg(port_mmio, 1);
2710c6fd2807SJeff Garzik 	}
2711b67a1064SMark Lord 	/*
27128e7decdbSMark Lord 	 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
2713b67a1064SMark Lord 	 * link, and physical layers.  It resets all SATA interface registers
2714b67a1064SMark Lord 	 * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
2715c6fd2807SJeff Garzik 	 */
27168e7decdbSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
2717b67a1064SMark Lord 	udelay(25);	/* allow reset propagation */
2718c6fd2807SJeff Garzik 	writelfl(0, port_mmio + EDMA_CMD_OFS);
2719c6fd2807SJeff Garzik 
2720c6fd2807SJeff Garzik 	hpriv->ops->phy_errata(hpriv, mmio, port_no);
2721c6fd2807SJeff Garzik 
2722ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv))
2723c6fd2807SJeff Garzik 		mdelay(1);
2724c6fd2807SJeff Garzik }
2725c6fd2807SJeff Garzik 
2726e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
2727e49856d8SMark Lord {
2728e49856d8SMark Lord 	if (sata_pmp_supported(ap)) {
2729e49856d8SMark Lord 		void __iomem *port_mmio = mv_ap_base(ap);
2730e49856d8SMark Lord 		u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
2731e49856d8SMark Lord 		int old = reg & 0xf;
2732e49856d8SMark Lord 
2733e49856d8SMark Lord 		if (old != pmp) {
2734e49856d8SMark Lord 			reg = (reg & ~0xf) | pmp;
2735e49856d8SMark Lord 			writelfl(reg, port_mmio + SATA_IFCTL_OFS);
2736e49856d8SMark Lord 		}
2737e49856d8SMark Lord 	}
2738e49856d8SMark Lord }
2739e49856d8SMark Lord 
2740e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
2741bdd4dddeSJeff Garzik 				unsigned long deadline)
2742c6fd2807SJeff Garzik {
2743e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
2744e49856d8SMark Lord 	return sata_std_hardreset(link, class, deadline);
2745e49856d8SMark Lord }
2746c6fd2807SJeff Garzik 
2747e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
2748e49856d8SMark Lord 				unsigned long deadline)
2749da3dbb17STejun Heo {
2750e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
2751e49856d8SMark Lord 	return ata_sff_softreset(link, class, deadline);
2752bdd4dddeSJeff Garzik }
2753bdd4dddeSJeff Garzik 
2754cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
2755bdd4dddeSJeff Garzik 			unsigned long deadline)
2756bdd4dddeSJeff Garzik {
2757cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
2758bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2759b562468cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
2760f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
27610d8be5cbSMark Lord 	int rc, attempts = 0, extra = 0;
27620d8be5cbSMark Lord 	u32 sstatus;
27630d8be5cbSMark Lord 	bool online;
2764bdd4dddeSJeff Garzik 
2765e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, ap->port_no);
2766b562468cSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2767bdd4dddeSJeff Garzik 
27680d8be5cbSMark Lord 	/* Workaround for errata FEr SATA#10 (part 2) */
27690d8be5cbSMark Lord 	do {
277017c5aab5SMark Lord 		const unsigned long *timing =
277117c5aab5SMark Lord 				sata_ehc_deb_timing(&link->eh_context);
2772bdd4dddeSJeff Garzik 
277317c5aab5SMark Lord 		rc = sata_link_hardreset(link, timing, deadline + extra,
277417c5aab5SMark Lord 					 &online, NULL);
27759dcffd99SMark Lord 		rc = online ? -EAGAIN : rc;
277617c5aab5SMark Lord 		if (rc)
27770d8be5cbSMark Lord 			return rc;
27780d8be5cbSMark Lord 		sata_scr_read(link, SCR_STATUS, &sstatus);
27790d8be5cbSMark Lord 		if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
27800d8be5cbSMark Lord 			/* Force 1.5gb/s link speed and try again */
27818e7decdbSMark Lord 			mv_setup_ifcfg(mv_ap_base(ap), 0);
27820d8be5cbSMark Lord 			if (time_after(jiffies + HZ, deadline))
27830d8be5cbSMark Lord 				extra = HZ; /* only extend it once, max */
2784bdd4dddeSJeff Garzik 		}
27850d8be5cbSMark Lord 	} while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
2786bdd4dddeSJeff Garzik 
278717c5aab5SMark Lord 	return rc;
2788bdd4dddeSJeff Garzik }
2789bdd4dddeSJeff Garzik 
2790bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
2791c6fd2807SJeff Garzik {
27921cfd19aeSMark Lord 	mv_stop_edma(ap);
2793c4de573bSMark Lord 	mv_enable_port_irqs(ap, 0);
2794c6fd2807SJeff Garzik }
2795bdd4dddeSJeff Garzik 
2796bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
2797bdd4dddeSJeff Garzik {
2798f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
2799c4de573bSMark Lord 	unsigned int port = ap->port_no;
2800c4de573bSMark Lord 	unsigned int hardport = mv_hardport_from_port(port);
28011cfd19aeSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
2802bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2803c4de573bSMark Lord 	u32 hc_irq_cause;
2804bdd4dddeSJeff Garzik 
2805bdd4dddeSJeff Garzik 	/* clear EDMA errors on this port */
2806bdd4dddeSJeff Garzik 	writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2807bdd4dddeSJeff Garzik 
2808bdd4dddeSJeff Garzik 	/* clear pending irq events */
2809cae6edc3SMark Lord 	hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
28101cfd19aeSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
2811bdd4dddeSJeff Garzik 
281288e675e1SMark Lord 	mv_enable_port_irqs(ap, ERR_IRQ);
2813c6fd2807SJeff Garzik }
2814c6fd2807SJeff Garzik 
2815c6fd2807SJeff Garzik /**
2816c6fd2807SJeff Garzik  *      mv_port_init - Perform some early initialization on a single port.
2817c6fd2807SJeff Garzik  *      @port: libata data structure storing shadow register addresses
2818c6fd2807SJeff Garzik  *      @port_mmio: base address of the port
2819c6fd2807SJeff Garzik  *
2820c6fd2807SJeff Garzik  *      Initialize shadow register mmio addresses, clear outstanding
2821c6fd2807SJeff Garzik  *      interrupts on the port, and unmask interrupts for the future
2822c6fd2807SJeff Garzik  *      start of the port.
2823c6fd2807SJeff Garzik  *
2824c6fd2807SJeff Garzik  *      LOCKING:
2825c6fd2807SJeff Garzik  *      Inherited from caller.
2826c6fd2807SJeff Garzik  */
2827c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
2828c6fd2807SJeff Garzik {
28290d5ff566STejun Heo 	void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
2830c6fd2807SJeff Garzik 	unsigned serr_ofs;
2831c6fd2807SJeff Garzik 
2832c6fd2807SJeff Garzik 	/* PIO related setup
2833c6fd2807SJeff Garzik 	 */
2834c6fd2807SJeff Garzik 	port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
2835c6fd2807SJeff Garzik 	port->error_addr =
2836c6fd2807SJeff Garzik 		port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
2837c6fd2807SJeff Garzik 	port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
2838c6fd2807SJeff Garzik 	port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
2839c6fd2807SJeff Garzik 	port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
2840c6fd2807SJeff Garzik 	port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
2841c6fd2807SJeff Garzik 	port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
2842c6fd2807SJeff Garzik 	port->status_addr =
2843c6fd2807SJeff Garzik 		port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
2844c6fd2807SJeff Garzik 	/* special case: control/altstatus doesn't have ATA_REG_ address */
2845c6fd2807SJeff Garzik 	port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
2846c6fd2807SJeff Garzik 
2847c6fd2807SJeff Garzik 	/* unused: */
28488d9db2d2SRandy Dunlap 	port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
2849c6fd2807SJeff Garzik 
2850c6fd2807SJeff Garzik 	/* Clear any currently outstanding port interrupt conditions */
2851c6fd2807SJeff Garzik 	serr_ofs = mv_scr_offset(SCR_ERROR);
2852c6fd2807SJeff Garzik 	writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
2853c6fd2807SJeff Garzik 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2854c6fd2807SJeff Garzik 
2855646a4da5SMark Lord 	/* unmask all non-transient EDMA error interrupts */
2856646a4da5SMark Lord 	writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
2857c6fd2807SJeff Garzik 
2858c6fd2807SJeff Garzik 	VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
2859c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_CFG_OFS),
2860c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
2861c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
2862c6fd2807SJeff Garzik }
2863c6fd2807SJeff Garzik 
2864616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host)
2865616d4a98SMark Lord {
2866616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2867616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
2868616d4a98SMark Lord 	u32 reg;
2869616d4a98SMark Lord 
28701f398472SMark Lord 	if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
2871616d4a98SMark Lord 		return 0;	/* not PCI-X capable */
2872616d4a98SMark Lord 	reg = readl(mmio + MV_PCI_MODE_OFS);
2873616d4a98SMark Lord 	if ((reg & MV_PCI_MODE_MASK) == 0)
2874616d4a98SMark Lord 		return 0;	/* conventional PCI mode */
2875616d4a98SMark Lord 	return 1;	/* chip is in PCI-X mode */
2876616d4a98SMark Lord }
2877616d4a98SMark Lord 
2878616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host)
2879616d4a98SMark Lord {
2880616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2881616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
2882616d4a98SMark Lord 	u32 reg;
2883616d4a98SMark Lord 
2884616d4a98SMark Lord 	if (!mv_in_pcix_mode(host)) {
2885616d4a98SMark Lord 		reg = readl(mmio + PCI_COMMAND_OFS);
2886616d4a98SMark Lord 		if (reg & PCI_COMMAND_MRDTRIG)
2887616d4a98SMark Lord 			return 0; /* not okay */
2888616d4a98SMark Lord 	}
2889616d4a98SMark Lord 	return 1; /* okay */
2890616d4a98SMark Lord }
2891616d4a98SMark Lord 
28924447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
2893c6fd2807SJeff Garzik {
28944447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
28954447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
2896c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
2897c6fd2807SJeff Garzik 
2898c6fd2807SJeff Garzik 	switch (board_idx) {
2899c6fd2807SJeff Garzik 	case chip_5080:
2900c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
2901ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
2902c6fd2807SJeff Garzik 
290344c10138SAuke Kok 		switch (pdev->revision) {
2904c6fd2807SJeff Garzik 		case 0x1:
2905c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
2906c6fd2807SJeff Garzik 			break;
2907c6fd2807SJeff Garzik 		case 0x3:
2908c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2909c6fd2807SJeff Garzik 			break;
2910c6fd2807SJeff Garzik 		default:
2911c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2912c6fd2807SJeff Garzik 			   "Applying 50XXB2 workarounds to unknown rev\n");
2913c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2914c6fd2807SJeff Garzik 			break;
2915c6fd2807SJeff Garzik 		}
2916c6fd2807SJeff Garzik 		break;
2917c6fd2807SJeff Garzik 
2918c6fd2807SJeff Garzik 	case chip_504x:
2919c6fd2807SJeff Garzik 	case chip_508x:
2920c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
2921ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
2922c6fd2807SJeff Garzik 
292344c10138SAuke Kok 		switch (pdev->revision) {
2924c6fd2807SJeff Garzik 		case 0x0:
2925c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
2926c6fd2807SJeff Garzik 			break;
2927c6fd2807SJeff Garzik 		case 0x3:
2928c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2929c6fd2807SJeff Garzik 			break;
2930c6fd2807SJeff Garzik 		default:
2931c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2932c6fd2807SJeff Garzik 			   "Applying B2 workarounds to unknown rev\n");
2933c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2934c6fd2807SJeff Garzik 			break;
2935c6fd2807SJeff Garzik 		}
2936c6fd2807SJeff Garzik 		break;
2937c6fd2807SJeff Garzik 
2938c6fd2807SJeff Garzik 	case chip_604x:
2939c6fd2807SJeff Garzik 	case chip_608x:
2940c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
2941ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_II;
2942c6fd2807SJeff Garzik 
294344c10138SAuke Kok 		switch (pdev->revision) {
2944c6fd2807SJeff Garzik 		case 0x7:
2945c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
2946c6fd2807SJeff Garzik 			break;
2947c6fd2807SJeff Garzik 		case 0x9:
2948c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2949c6fd2807SJeff Garzik 			break;
2950c6fd2807SJeff Garzik 		default:
2951c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2952c6fd2807SJeff Garzik 				   "Applying B2 workarounds to unknown rev\n");
2953c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
2954c6fd2807SJeff Garzik 			break;
2955c6fd2807SJeff Garzik 		}
2956c6fd2807SJeff Garzik 		break;
2957c6fd2807SJeff Garzik 
2958c6fd2807SJeff Garzik 	case chip_7042:
2959616d4a98SMark Lord 		hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
2960306b30f7SMark Lord 		if (pdev->vendor == PCI_VENDOR_ID_TTI &&
2961306b30f7SMark Lord 		    (pdev->device == 0x2300 || pdev->device == 0x2310))
2962306b30f7SMark Lord 		{
29634e520033SMark Lord 			/*
29644e520033SMark Lord 			 * Highpoint RocketRAID PCIe 23xx series cards:
29654e520033SMark Lord 			 *
29664e520033SMark Lord 			 * Unconfigured drives are treated as "Legacy"
29674e520033SMark Lord 			 * by the BIOS, and it overwrites sector 8 with
29684e520033SMark Lord 			 * a "Lgcy" metadata block prior to Linux boot.
29694e520033SMark Lord 			 *
29704e520033SMark Lord 			 * Configured drives (RAID or JBOD) leave sector 8
29714e520033SMark Lord 			 * alone, but instead overwrite a high numbered
29724e520033SMark Lord 			 * sector for the RAID metadata.  This sector can
29734e520033SMark Lord 			 * be determined exactly, by truncating the physical
29744e520033SMark Lord 			 * drive capacity to a nice even GB value.
29754e520033SMark Lord 			 *
29764e520033SMark Lord 			 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
29774e520033SMark Lord 			 *
29784e520033SMark Lord 			 * Warn the user, lest they think we're just buggy.
29794e520033SMark Lord 			 */
29804e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
29814e520033SMark Lord 				" BIOS CORRUPTS DATA on all attached drives,"
29824e520033SMark Lord 				" regardless of if/how they are configured."
29834e520033SMark Lord 				" BEWARE!\n");
29844e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": For data safety, do not"
29854e520033SMark Lord 				" use sectors 8-9 on \"Legacy\" drives,"
29864e520033SMark Lord 				" and avoid the final two gigabytes on"
29874e520033SMark Lord 				" all RocketRAID BIOS initialized drives.\n");
2988306b30f7SMark Lord 		}
29898e7decdbSMark Lord 		/* drop through */
2990c6fd2807SJeff Garzik 	case chip_6042:
2991c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
2992c6fd2807SJeff Garzik 		hp_flags |= MV_HP_GEN_IIE;
2993616d4a98SMark Lord 		if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
2994616d4a98SMark Lord 			hp_flags |= MV_HP_CUT_THROUGH;
2995c6fd2807SJeff Garzik 
299644c10138SAuke Kok 		switch (pdev->revision) {
29975cf73bfbSMark Lord 		case 0x2: /* Rev.B0: the first/only public release */
2998c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2999c6fd2807SJeff Garzik 			break;
3000c6fd2807SJeff Garzik 		default:
3001c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
3002c6fd2807SJeff Garzik 			   "Applying 60X1C0 workarounds to unknown rev\n");
3003c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
3004c6fd2807SJeff Garzik 			break;
3005c6fd2807SJeff Garzik 		}
3006c6fd2807SJeff Garzik 		break;
3007f351b2d6SSaeed Bishara 	case chip_soc:
3008f351b2d6SSaeed Bishara 		hpriv->ops = &mv_soc_ops;
3009eb3a55a9SSaeed Bishara 		hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3010eb3a55a9SSaeed Bishara 			MV_HP_ERRATA_60X1C0;
3011f351b2d6SSaeed Bishara 		break;
3012c6fd2807SJeff Garzik 
3013c6fd2807SJeff Garzik 	default:
3014f351b2d6SSaeed Bishara 		dev_printk(KERN_ERR, host->dev,
30155796d1c4SJeff Garzik 			   "BUG: invalid board index %u\n", board_idx);
3016c6fd2807SJeff Garzik 		return 1;
3017c6fd2807SJeff Garzik 	}
3018c6fd2807SJeff Garzik 
3019c6fd2807SJeff Garzik 	hpriv->hp_flags = hp_flags;
302002a121daSMark Lord 	if (hp_flags & MV_HP_PCIE) {
302102a121daSMark Lord 		hpriv->irq_cause_ofs	= PCIE_IRQ_CAUSE_OFS;
302202a121daSMark Lord 		hpriv->irq_mask_ofs	= PCIE_IRQ_MASK_OFS;
302302a121daSMark Lord 		hpriv->unmask_all_irqs	= PCIE_UNMASK_ALL_IRQS;
302402a121daSMark Lord 	} else {
302502a121daSMark Lord 		hpriv->irq_cause_ofs	= PCI_IRQ_CAUSE_OFS;
302602a121daSMark Lord 		hpriv->irq_mask_ofs	= PCI_IRQ_MASK_OFS;
302702a121daSMark Lord 		hpriv->unmask_all_irqs	= PCI_UNMASK_ALL_IRQS;
302802a121daSMark Lord 	}
3029c6fd2807SJeff Garzik 
3030c6fd2807SJeff Garzik 	return 0;
3031c6fd2807SJeff Garzik }
3032c6fd2807SJeff Garzik 
3033c6fd2807SJeff Garzik /**
3034c6fd2807SJeff Garzik  *      mv_init_host - Perform some early initialization of the host.
30354447d351STejun Heo  *	@host: ATA host to initialize
30364447d351STejun Heo  *      @board_idx: controller index
3037c6fd2807SJeff Garzik  *
3038c6fd2807SJeff Garzik  *      If possible, do an early global reset of the host.  Then do
3039c6fd2807SJeff Garzik  *      our port init and clear/unmask all/relevant host interrupts.
3040c6fd2807SJeff Garzik  *
3041c6fd2807SJeff Garzik  *      LOCKING:
3042c6fd2807SJeff Garzik  *      Inherited from caller.
3043c6fd2807SJeff Garzik  */
30444447d351STejun Heo static int mv_init_host(struct ata_host *host, unsigned int board_idx)
3045c6fd2807SJeff Garzik {
3046c6fd2807SJeff Garzik 	int rc = 0, n_hc, port, hc;
30474447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
3048f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
3049c6fd2807SJeff Garzik 
30504447d351STejun Heo 	rc = mv_chip_id(host, board_idx);
3051c6fd2807SJeff Garzik 	if (rc)
3052c6fd2807SJeff Garzik 		goto done;
3053c6fd2807SJeff Garzik 
30541f398472SMark Lord 	if (IS_SOC(hpriv)) {
30557368f919SMark Lord 		hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE_OFS;
30567368f919SMark Lord 		hpriv->main_irq_mask_addr  = mmio + SOC_HC_MAIN_IRQ_MASK_OFS;
30571f398472SMark Lord 	} else {
30581f398472SMark Lord 		hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE_OFS;
30591f398472SMark Lord 		hpriv->main_irq_mask_addr  = mmio + PCI_HC_MAIN_IRQ_MASK_OFS;
3060f351b2d6SSaeed Bishara 	}
3061352fab70SMark Lord 
3062*5d0fb2e7SThomas Reitmayr 	/* initialize shadow irq mask with register's value */
3063*5d0fb2e7SThomas Reitmayr 	hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
3064*5d0fb2e7SThomas Reitmayr 
3065352fab70SMark Lord 	/* global interrupt mask: 0 == mask everything */
3066c4de573bSMark Lord 	mv_set_main_irq_mask(host, ~0, 0);
3067f351b2d6SSaeed Bishara 
30684447d351STejun Heo 	n_hc = mv_get_hc_count(host->ports[0]->flags);
3069c6fd2807SJeff Garzik 
30704447d351STejun Heo 	for (port = 0; port < host->n_ports; port++)
3071c6fd2807SJeff Garzik 		hpriv->ops->read_preamp(hpriv, port, mmio);
3072c6fd2807SJeff Garzik 
3073c6fd2807SJeff Garzik 	rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
3074c6fd2807SJeff Garzik 	if (rc)
3075c6fd2807SJeff Garzik 		goto done;
3076c6fd2807SJeff Garzik 
3077c6fd2807SJeff Garzik 	hpriv->ops->reset_flash(hpriv, mmio);
30787bb3c529SSaeed Bishara 	hpriv->ops->reset_bus(host, mmio);
3079c6fd2807SJeff Garzik 	hpriv->ops->enable_leds(hpriv, mmio);
3080c6fd2807SJeff Garzik 
30814447d351STejun Heo 	for (port = 0; port < host->n_ports; port++) {
3082cbcdd875STejun Heo 		struct ata_port *ap = host->ports[port];
3083c6fd2807SJeff Garzik 		void __iomem *port_mmio = mv_port_base(mmio, port);
3084cbcdd875STejun Heo 
3085cbcdd875STejun Heo 		mv_port_init(&ap->ioaddr, port_mmio);
3086cbcdd875STejun Heo 
30877bb3c529SSaeed Bishara #ifdef CONFIG_PCI
30881f398472SMark Lord 		if (!IS_SOC(hpriv)) {
3089f351b2d6SSaeed Bishara 			unsigned int offset = port_mmio - mmio;
3090cbcdd875STejun Heo 			ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
3091cbcdd875STejun Heo 			ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
3092f351b2d6SSaeed Bishara 		}
30937bb3c529SSaeed Bishara #endif
3094c6fd2807SJeff Garzik 	}
3095c6fd2807SJeff Garzik 
3096c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
3097c6fd2807SJeff Garzik 		void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3098c6fd2807SJeff Garzik 
3099c6fd2807SJeff Garzik 		VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
3100c6fd2807SJeff Garzik 			"(before clear)=0x%08x\n", hc,
3101c6fd2807SJeff Garzik 			readl(hc_mmio + HC_CFG_OFS),
3102c6fd2807SJeff Garzik 			readl(hc_mmio + HC_IRQ_CAUSE_OFS));
3103c6fd2807SJeff Garzik 
3104c6fd2807SJeff Garzik 		/* Clear any currently outstanding hc interrupt conditions */
3105c6fd2807SJeff Garzik 		writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
3106c6fd2807SJeff Garzik 	}
3107c6fd2807SJeff Garzik 
31081f398472SMark Lord 	if (!IS_SOC(hpriv)) {
3109c6fd2807SJeff Garzik 		/* Clear any currently outstanding host interrupt conditions */
311002a121daSMark Lord 		writelfl(0, mmio + hpriv->irq_cause_ofs);
3111c6fd2807SJeff Garzik 
3112c6fd2807SJeff Garzik 		/* and unmask interrupt generation for host regs */
311302a121daSMark Lord 		writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
3114c6fd2807SJeff Garzik 
311551de32d2SMark Lord 		/*
311651de32d2SMark Lord 		 * enable only global host interrupts for now.
311751de32d2SMark Lord 		 * The per-port interrupts get done later as ports are set up.
311851de32d2SMark Lord 		 */
3119c4de573bSMark Lord 		mv_set_main_irq_mask(host, 0, PCI_ERR);
3120f351b2d6SSaeed Bishara 	}
3121c6fd2807SJeff Garzik done:
3122c6fd2807SJeff Garzik 	return rc;
3123c6fd2807SJeff Garzik }
3124c6fd2807SJeff Garzik 
3125fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
3126fbf14e2fSByron Bradley {
3127fbf14e2fSByron Bradley 	hpriv->crqb_pool   = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
3128fbf14e2fSByron Bradley 							     MV_CRQB_Q_SZ, 0);
3129fbf14e2fSByron Bradley 	if (!hpriv->crqb_pool)
3130fbf14e2fSByron Bradley 		return -ENOMEM;
3131fbf14e2fSByron Bradley 
3132fbf14e2fSByron Bradley 	hpriv->crpb_pool   = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
3133fbf14e2fSByron Bradley 							     MV_CRPB_Q_SZ, 0);
3134fbf14e2fSByron Bradley 	if (!hpriv->crpb_pool)
3135fbf14e2fSByron Bradley 		return -ENOMEM;
3136fbf14e2fSByron Bradley 
3137fbf14e2fSByron Bradley 	hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
3138fbf14e2fSByron Bradley 							     MV_SG_TBL_SZ, 0);
3139fbf14e2fSByron Bradley 	if (!hpriv->sg_tbl_pool)
3140fbf14e2fSByron Bradley 		return -ENOMEM;
3141fbf14e2fSByron Bradley 
3142fbf14e2fSByron Bradley 	return 0;
3143fbf14e2fSByron Bradley }
3144fbf14e2fSByron Bradley 
314515a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
314615a32632SLennert Buytenhek 				 struct mbus_dram_target_info *dram)
314715a32632SLennert Buytenhek {
314815a32632SLennert Buytenhek 	int i;
314915a32632SLennert Buytenhek 
315015a32632SLennert Buytenhek 	for (i = 0; i < 4; i++) {
315115a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_CTRL(i));
315215a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_BASE(i));
315315a32632SLennert Buytenhek 	}
315415a32632SLennert Buytenhek 
315515a32632SLennert Buytenhek 	for (i = 0; i < dram->num_cs; i++) {
315615a32632SLennert Buytenhek 		struct mbus_dram_window *cs = dram->cs + i;
315715a32632SLennert Buytenhek 
315815a32632SLennert Buytenhek 		writel(((cs->size - 1) & 0xffff0000) |
315915a32632SLennert Buytenhek 			(cs->mbus_attr << 8) |
316015a32632SLennert Buytenhek 			(dram->mbus_dram_target_id << 4) | 1,
316115a32632SLennert Buytenhek 			hpriv->base + WINDOW_CTRL(i));
316215a32632SLennert Buytenhek 		writel(cs->base, hpriv->base + WINDOW_BASE(i));
316315a32632SLennert Buytenhek 	}
316415a32632SLennert Buytenhek }
316515a32632SLennert Buytenhek 
3166f351b2d6SSaeed Bishara /**
3167f351b2d6SSaeed Bishara  *      mv_platform_probe - handle a positive probe of an soc Marvell
3168f351b2d6SSaeed Bishara  *      host
3169f351b2d6SSaeed Bishara  *      @pdev: platform device found
3170f351b2d6SSaeed Bishara  *
3171f351b2d6SSaeed Bishara  *      LOCKING:
3172f351b2d6SSaeed Bishara  *      Inherited from caller.
3173f351b2d6SSaeed Bishara  */
3174f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
3175f351b2d6SSaeed Bishara {
3176f351b2d6SSaeed Bishara 	static int printed_version;
3177f351b2d6SSaeed Bishara 	const struct mv_sata_platform_data *mv_platform_data;
3178f351b2d6SSaeed Bishara 	const struct ata_port_info *ppi[] =
3179f351b2d6SSaeed Bishara 	    { &mv_port_info[chip_soc], NULL };
3180f351b2d6SSaeed Bishara 	struct ata_host *host;
3181f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv;
3182f351b2d6SSaeed Bishara 	struct resource *res;
3183f351b2d6SSaeed Bishara 	int n_ports, rc;
3184f351b2d6SSaeed Bishara 
3185f351b2d6SSaeed Bishara 	if (!printed_version++)
3186f351b2d6SSaeed Bishara 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
3187f351b2d6SSaeed Bishara 
3188f351b2d6SSaeed Bishara 	/*
3189f351b2d6SSaeed Bishara 	 * Simple resource validation ..
3190f351b2d6SSaeed Bishara 	 */
3191f351b2d6SSaeed Bishara 	if (unlikely(pdev->num_resources != 2)) {
3192f351b2d6SSaeed Bishara 		dev_err(&pdev->dev, "invalid number of resources\n");
3193f351b2d6SSaeed Bishara 		return -EINVAL;
3194f351b2d6SSaeed Bishara 	}
3195f351b2d6SSaeed Bishara 
3196f351b2d6SSaeed Bishara 	/*
3197f351b2d6SSaeed Bishara 	 * Get the register base first
3198f351b2d6SSaeed Bishara 	 */
3199f351b2d6SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3200f351b2d6SSaeed Bishara 	if (res == NULL)
3201f351b2d6SSaeed Bishara 		return -EINVAL;
3202f351b2d6SSaeed Bishara 
3203f351b2d6SSaeed Bishara 	/* allocate host */
3204f351b2d6SSaeed Bishara 	mv_platform_data = pdev->dev.platform_data;
3205f351b2d6SSaeed Bishara 	n_ports = mv_platform_data->n_ports;
3206f351b2d6SSaeed Bishara 
3207f351b2d6SSaeed Bishara 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
3208f351b2d6SSaeed Bishara 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
3209f351b2d6SSaeed Bishara 
3210f351b2d6SSaeed Bishara 	if (!host || !hpriv)
3211f351b2d6SSaeed Bishara 		return -ENOMEM;
3212f351b2d6SSaeed Bishara 	host->private_data = hpriv;
3213f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
3214f351b2d6SSaeed Bishara 
3215f351b2d6SSaeed Bishara 	host->iomap = NULL;
3216f1cb0ea1SSaeed Bishara 	hpriv->base = devm_ioremap(&pdev->dev, res->start,
3217f1cb0ea1SSaeed Bishara 				   res->end - res->start + 1);
3218f351b2d6SSaeed Bishara 	hpriv->base -= MV_SATAHC0_REG_BASE;
3219f351b2d6SSaeed Bishara 
322015a32632SLennert Buytenhek 	/*
322115a32632SLennert Buytenhek 	 * (Re-)program MBUS remapping windows if we are asked to.
322215a32632SLennert Buytenhek 	 */
322315a32632SLennert Buytenhek 	if (mv_platform_data->dram != NULL)
322415a32632SLennert Buytenhek 		mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
322515a32632SLennert Buytenhek 
3226fbf14e2fSByron Bradley 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
3227fbf14e2fSByron Bradley 	if (rc)
3228fbf14e2fSByron Bradley 		return rc;
3229fbf14e2fSByron Bradley 
3230f351b2d6SSaeed Bishara 	/* initialize adapter */
3231f351b2d6SSaeed Bishara 	rc = mv_init_host(host, chip_soc);
3232f351b2d6SSaeed Bishara 	if (rc)
3233f351b2d6SSaeed Bishara 		return rc;
3234f351b2d6SSaeed Bishara 
3235f351b2d6SSaeed Bishara 	dev_printk(KERN_INFO, &pdev->dev,
3236f351b2d6SSaeed Bishara 		   "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
3237f351b2d6SSaeed Bishara 		   host->n_ports);
3238f351b2d6SSaeed Bishara 
3239f351b2d6SSaeed Bishara 	return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
3240f351b2d6SSaeed Bishara 				 IRQF_SHARED, &mv6_sht);
3241f351b2d6SSaeed Bishara }
3242f351b2d6SSaeed Bishara 
3243f351b2d6SSaeed Bishara /*
3244f351b2d6SSaeed Bishara  *
3245f351b2d6SSaeed Bishara  *      mv_platform_remove    -       unplug a platform interface
3246f351b2d6SSaeed Bishara  *      @pdev: platform device
3247f351b2d6SSaeed Bishara  *
3248f351b2d6SSaeed Bishara  *      A platform bus SATA device has been unplugged. Perform the needed
3249f351b2d6SSaeed Bishara  *      cleanup. Also called on module unload for any active devices.
3250f351b2d6SSaeed Bishara  */
3251f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev)
3252f351b2d6SSaeed Bishara {
3253f351b2d6SSaeed Bishara 	struct device *dev = &pdev->dev;
3254f351b2d6SSaeed Bishara 	struct ata_host *host = dev_get_drvdata(dev);
3255f351b2d6SSaeed Bishara 
3256f351b2d6SSaeed Bishara 	ata_host_detach(host);
3257f351b2d6SSaeed Bishara 	return 0;
3258f351b2d6SSaeed Bishara }
3259f351b2d6SSaeed Bishara 
3260f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
3261f351b2d6SSaeed Bishara 	.probe			= mv_platform_probe,
3262f351b2d6SSaeed Bishara 	.remove			= __devexit_p(mv_platform_remove),
3263f351b2d6SSaeed Bishara 	.driver			= {
3264f351b2d6SSaeed Bishara 				   .name = DRV_NAME,
3265f351b2d6SSaeed Bishara 				   .owner = THIS_MODULE,
3266f351b2d6SSaeed Bishara 				  },
3267f351b2d6SSaeed Bishara };
3268f351b2d6SSaeed Bishara 
3269f351b2d6SSaeed Bishara 
32707bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3271f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
3272f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent);
3273f351b2d6SSaeed Bishara 
32747bb3c529SSaeed Bishara 
32757bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
32767bb3c529SSaeed Bishara 	.name			= DRV_NAME,
32777bb3c529SSaeed Bishara 	.id_table		= mv_pci_tbl,
3278f351b2d6SSaeed Bishara 	.probe			= mv_pci_init_one,
32797bb3c529SSaeed Bishara 	.remove			= ata_pci_remove_one,
32807bb3c529SSaeed Bishara };
32817bb3c529SSaeed Bishara 
32827bb3c529SSaeed Bishara /*
32837bb3c529SSaeed Bishara  * module options
32847bb3c529SSaeed Bishara  */
32857bb3c529SSaeed Bishara static int msi;	      /* Use PCI msi; either zero (off, default) or non-zero */
32867bb3c529SSaeed Bishara 
32877bb3c529SSaeed Bishara 
32887bb3c529SSaeed Bishara /* move to PCI layer or libata core? */
32897bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev)
32907bb3c529SSaeed Bishara {
32917bb3c529SSaeed Bishara 	int rc;
32927bb3c529SSaeed Bishara 
32937bb3c529SSaeed Bishara 	if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
32947bb3c529SSaeed Bishara 		rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
32957bb3c529SSaeed Bishara 		if (rc) {
32967bb3c529SSaeed Bishara 			rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
32977bb3c529SSaeed Bishara 			if (rc) {
32987bb3c529SSaeed Bishara 				dev_printk(KERN_ERR, &pdev->dev,
32997bb3c529SSaeed Bishara 					   "64-bit DMA enable failed\n");
33007bb3c529SSaeed Bishara 				return rc;
33017bb3c529SSaeed Bishara 			}
33027bb3c529SSaeed Bishara 		}
33037bb3c529SSaeed Bishara 	} else {
33047bb3c529SSaeed Bishara 		rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
33057bb3c529SSaeed Bishara 		if (rc) {
33067bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
33077bb3c529SSaeed Bishara 				   "32-bit DMA enable failed\n");
33087bb3c529SSaeed Bishara 			return rc;
33097bb3c529SSaeed Bishara 		}
33107bb3c529SSaeed Bishara 		rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
33117bb3c529SSaeed Bishara 		if (rc) {
33127bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
33137bb3c529SSaeed Bishara 				   "32-bit consistent DMA enable failed\n");
33147bb3c529SSaeed Bishara 			return rc;
33157bb3c529SSaeed Bishara 		}
33167bb3c529SSaeed Bishara 	}
33177bb3c529SSaeed Bishara 
33187bb3c529SSaeed Bishara 	return rc;
33197bb3c529SSaeed Bishara }
33207bb3c529SSaeed Bishara 
3321c6fd2807SJeff Garzik /**
3322c6fd2807SJeff Garzik  *      mv_print_info - Dump key info to kernel log for perusal.
33234447d351STejun Heo  *      @host: ATA host to print info about
3324c6fd2807SJeff Garzik  *
3325c6fd2807SJeff Garzik  *      FIXME: complete this.
3326c6fd2807SJeff Garzik  *
3327c6fd2807SJeff Garzik  *      LOCKING:
3328c6fd2807SJeff Garzik  *      Inherited from caller.
3329c6fd2807SJeff Garzik  */
33304447d351STejun Heo static void mv_print_info(struct ata_host *host)
3331c6fd2807SJeff Garzik {
33324447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
33334447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
333444c10138SAuke Kok 	u8 scc;
3335c1e4fe71SJeff Garzik 	const char *scc_s, *gen;
3336c6fd2807SJeff Garzik 
3337c6fd2807SJeff Garzik 	/* Use this to determine the HW stepping of the chip so we know
3338c6fd2807SJeff Garzik 	 * what errata to workaround
3339c6fd2807SJeff Garzik 	 */
3340c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
3341c6fd2807SJeff Garzik 	if (scc == 0)
3342c6fd2807SJeff Garzik 		scc_s = "SCSI";
3343c6fd2807SJeff Garzik 	else if (scc == 0x01)
3344c6fd2807SJeff Garzik 		scc_s = "RAID";
3345c6fd2807SJeff Garzik 	else
3346c1e4fe71SJeff Garzik 		scc_s = "?";
3347c1e4fe71SJeff Garzik 
3348c1e4fe71SJeff Garzik 	if (IS_GEN_I(hpriv))
3349c1e4fe71SJeff Garzik 		gen = "I";
3350c1e4fe71SJeff Garzik 	else if (IS_GEN_II(hpriv))
3351c1e4fe71SJeff Garzik 		gen = "II";
3352c1e4fe71SJeff Garzik 	else if (IS_GEN_IIE(hpriv))
3353c1e4fe71SJeff Garzik 		gen = "IIE";
3354c1e4fe71SJeff Garzik 	else
3355c1e4fe71SJeff Garzik 		gen = "?";
3356c6fd2807SJeff Garzik 
3357c6fd2807SJeff Garzik 	dev_printk(KERN_INFO, &pdev->dev,
3358c1e4fe71SJeff Garzik 	       "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
3359c1e4fe71SJeff Garzik 	       gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
3360c6fd2807SJeff Garzik 	       scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
3361c6fd2807SJeff Garzik }
3362c6fd2807SJeff Garzik 
3363c6fd2807SJeff Garzik /**
3364f351b2d6SSaeed Bishara  *      mv_pci_init_one - handle a positive probe of a PCI Marvell host
3365c6fd2807SJeff Garzik  *      @pdev: PCI device found
3366c6fd2807SJeff Garzik  *      @ent: PCI device ID entry for the matched host
3367c6fd2807SJeff Garzik  *
3368c6fd2807SJeff Garzik  *      LOCKING:
3369c6fd2807SJeff Garzik  *      Inherited from caller.
3370c6fd2807SJeff Garzik  */
3371f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
3372f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent)
3373c6fd2807SJeff Garzik {
33742dcb407eSJeff Garzik 	static int printed_version;
3375c6fd2807SJeff Garzik 	unsigned int board_idx = (unsigned int)ent->driver_data;
33764447d351STejun Heo 	const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
33774447d351STejun Heo 	struct ata_host *host;
33784447d351STejun Heo 	struct mv_host_priv *hpriv;
33794447d351STejun Heo 	int n_ports, rc;
3380c6fd2807SJeff Garzik 
3381c6fd2807SJeff Garzik 	if (!printed_version++)
3382c6fd2807SJeff Garzik 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
3383c6fd2807SJeff Garzik 
33844447d351STejun Heo 	/* allocate host */
33854447d351STejun Heo 	n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
33864447d351STejun Heo 
33874447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
33884447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
33894447d351STejun Heo 	if (!host || !hpriv)
33904447d351STejun Heo 		return -ENOMEM;
33914447d351STejun Heo 	host->private_data = hpriv;
3392f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
33934447d351STejun Heo 
33944447d351STejun Heo 	/* acquire resources */
339524dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
339624dc5f33STejun Heo 	if (rc)
3397c6fd2807SJeff Garzik 		return rc;
3398c6fd2807SJeff Garzik 
33990d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
34000d5ff566STejun Heo 	if (rc == -EBUSY)
340124dc5f33STejun Heo 		pcim_pin_device(pdev);
34020d5ff566STejun Heo 	if (rc)
340324dc5f33STejun Heo 		return rc;
34044447d351STejun Heo 	host->iomap = pcim_iomap_table(pdev);
3405f351b2d6SSaeed Bishara 	hpriv->base = host->iomap[MV_PRIMARY_BAR];
3406c6fd2807SJeff Garzik 
3407d88184fbSJeff Garzik 	rc = pci_go_64(pdev);
3408d88184fbSJeff Garzik 	if (rc)
3409d88184fbSJeff Garzik 		return rc;
3410d88184fbSJeff Garzik 
3411da2fa9baSMark Lord 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
3412da2fa9baSMark Lord 	if (rc)
3413da2fa9baSMark Lord 		return rc;
3414da2fa9baSMark Lord 
3415c6fd2807SJeff Garzik 	/* initialize adapter */
34164447d351STejun Heo 	rc = mv_init_host(host, board_idx);
341724dc5f33STejun Heo 	if (rc)
341824dc5f33STejun Heo 		return rc;
3419c6fd2807SJeff Garzik 
3420c6fd2807SJeff Garzik 	/* Enable interrupts */
34216a59dcf8STejun Heo 	if (msi && pci_enable_msi(pdev))
3422c6fd2807SJeff Garzik 		pci_intx(pdev, 1);
3423c6fd2807SJeff Garzik 
3424c6fd2807SJeff Garzik 	mv_dump_pci_cfg(pdev, 0x68);
34254447d351STejun Heo 	mv_print_info(host);
3426c6fd2807SJeff Garzik 
34274447d351STejun Heo 	pci_set_master(pdev);
3428ea8b4db9SJeff Garzik 	pci_try_set_mwi(pdev);
34294447d351STejun Heo 	return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
3430c5d3e45aSJeff Garzik 				 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
3431c6fd2807SJeff Garzik }
34327bb3c529SSaeed Bishara #endif
3433c6fd2807SJeff Garzik 
3434f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev);
3435f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev);
3436f351b2d6SSaeed Bishara 
3437c6fd2807SJeff Garzik static int __init mv_init(void)
3438c6fd2807SJeff Garzik {
34397bb3c529SSaeed Bishara 	int rc = -ENODEV;
34407bb3c529SSaeed Bishara #ifdef CONFIG_PCI
34417bb3c529SSaeed Bishara 	rc = pci_register_driver(&mv_pci_driver);
3442f351b2d6SSaeed Bishara 	if (rc < 0)
3443f351b2d6SSaeed Bishara 		return rc;
3444f351b2d6SSaeed Bishara #endif
3445f351b2d6SSaeed Bishara 	rc = platform_driver_register(&mv_platform_driver);
3446f351b2d6SSaeed Bishara 
3447f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
3448f351b2d6SSaeed Bishara 	if (rc < 0)
3449f351b2d6SSaeed Bishara 		pci_unregister_driver(&mv_pci_driver);
34507bb3c529SSaeed Bishara #endif
34517bb3c529SSaeed Bishara 	return rc;
3452c6fd2807SJeff Garzik }
3453c6fd2807SJeff Garzik 
3454c6fd2807SJeff Garzik static void __exit mv_exit(void)
3455c6fd2807SJeff Garzik {
34567bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3457c6fd2807SJeff Garzik 	pci_unregister_driver(&mv_pci_driver);
34587bb3c529SSaeed Bishara #endif
3459f351b2d6SSaeed Bishara 	platform_driver_unregister(&mv_platform_driver);
3460c6fd2807SJeff Garzik }
3461c6fd2807SJeff Garzik 
3462c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
3463c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
3464c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
3465c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
3466c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
346717c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
3468c6fd2807SJeff Garzik 
34697bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3470c6fd2807SJeff Garzik module_param(msi, int, 0444);
3471c6fd2807SJeff Garzik MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
34727bb3c529SSaeed Bishara #endif
3473c6fd2807SJeff Garzik 
3474c6fd2807SJeff Garzik module_init(mv_init);
3475c6fd2807SJeff Garzik module_exit(mv_exit);
3476