xref: /openbmc/linux/drivers/ata/sata_mv.c (revision 9b2c4e0bae854fb5e88c9cacc0dacf21631c5cb0)
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  * --> Cache frequently-accessed registers in mv_port_priv to reduce overhead.
3985afb934SMark Lord  *
4085afb934SMark Lord  * --> Develop a low-power-consumption strategy, and implement it.
4185afb934SMark Lord  *
4285afb934SMark Lord  * --> [Experiment, low priority] Investigate interrupt coalescing.
4385afb934SMark Lord  *       Quite often, especially with PCI Message Signalled Interrupts (MSI),
4485afb934SMark Lord  *       the overhead reduced by interrupt mitigation is quite often not
4585afb934SMark Lord  *       worth the latency cost.
4685afb934SMark Lord  *
4785afb934SMark Lord  * --> [Experiment, Marvell value added] Is it possible to use target
4885afb934SMark Lord  *       mode to cross-connect two Linux boxes with Marvell cards?  If so,
4985afb934SMark Lord  *       creating LibATA target mode support would be very interesting.
5085afb934SMark Lord  *
5185afb934SMark Lord  *       Target mode, for those without docs, is the ability to directly
5285afb934SMark Lord  *       connect two SATA ports.
534a05e209SJeff Garzik  */
544a05e209SJeff Garzik 
55c6fd2807SJeff Garzik #include <linux/kernel.h>
56c6fd2807SJeff Garzik #include <linux/module.h>
57c6fd2807SJeff Garzik #include <linux/pci.h>
58c6fd2807SJeff Garzik #include <linux/init.h>
59c6fd2807SJeff Garzik #include <linux/blkdev.h>
60c6fd2807SJeff Garzik #include <linux/delay.h>
61c6fd2807SJeff Garzik #include <linux/interrupt.h>
628d8b6004SAndrew Morton #include <linux/dmapool.h>
63c6fd2807SJeff Garzik #include <linux/dma-mapping.h>
64c6fd2807SJeff Garzik #include <linux/device.h>
65f351b2d6SSaeed Bishara #include <linux/platform_device.h>
66f351b2d6SSaeed Bishara #include <linux/ata_platform.h>
6715a32632SLennert Buytenhek #include <linux/mbus.h>
68c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
69c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
706c08772eSJeff Garzik #include <scsi/scsi_device.h>
71c6fd2807SJeff Garzik #include <linux/libata.h>
72c6fd2807SJeff Garzik 
73c6fd2807SJeff Garzik #define DRV_NAME	"sata_mv"
741fd2e1c2SMark Lord #define DRV_VERSION	"1.20"
75c6fd2807SJeff Garzik 
76c6fd2807SJeff Garzik enum {
77c6fd2807SJeff Garzik 	/* BAR's are enumerated in terms of pci_resource_start() terms */
78c6fd2807SJeff Garzik 	MV_PRIMARY_BAR		= 0,	/* offset 0x10: memory space */
79c6fd2807SJeff Garzik 	MV_IO_BAR		= 2,	/* offset 0x18: IO space */
80c6fd2807SJeff Garzik 	MV_MISC_BAR		= 3,	/* offset 0x1c: FLASH, NVRAM, SRAM */
81c6fd2807SJeff Garzik 
82c6fd2807SJeff Garzik 	MV_MAJOR_REG_AREA_SZ	= 0x10000,	/* 64KB */
83c6fd2807SJeff Garzik 	MV_MINOR_REG_AREA_SZ	= 0x2000,	/* 8KB */
84c6fd2807SJeff Garzik 
85c6fd2807SJeff Garzik 	MV_PCI_REG_BASE		= 0,
86c6fd2807SJeff Garzik 	MV_IRQ_COAL_REG_BASE	= 0x18000,	/* 6xxx part only */
87c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE		= (MV_IRQ_COAL_REG_BASE + 0x08),
88c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE_LO		= (MV_IRQ_COAL_REG_BASE + 0x88),
89c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE_HI		= (MV_IRQ_COAL_REG_BASE + 0x8c),
90c6fd2807SJeff Garzik 	MV_IRQ_COAL_THRESHOLD		= (MV_IRQ_COAL_REG_BASE + 0xcc),
91c6fd2807SJeff Garzik 	MV_IRQ_COAL_TIME_THRESHOLD	= (MV_IRQ_COAL_REG_BASE + 0xd0),
92c6fd2807SJeff Garzik 
93c6fd2807SJeff Garzik 	MV_SATAHC0_REG_BASE	= 0x20000,
948e7decdbSMark Lord 	MV_FLASH_CTL_OFS	= 0x1046c,
958e7decdbSMark Lord 	MV_GPIO_PORT_CTL_OFS	= 0x104f0,
968e7decdbSMark Lord 	MV_RESET_CFG_OFS	= 0x180d8,
97c6fd2807SJeff Garzik 
98c6fd2807SJeff Garzik 	MV_PCI_REG_SZ		= MV_MAJOR_REG_AREA_SZ,
99c6fd2807SJeff Garzik 	MV_SATAHC_REG_SZ	= MV_MAJOR_REG_AREA_SZ,
100c6fd2807SJeff Garzik 	MV_SATAHC_ARBTR_REG_SZ	= MV_MINOR_REG_AREA_SZ,		/* arbiter */
101c6fd2807SJeff Garzik 	MV_PORT_REG_SZ		= MV_MINOR_REG_AREA_SZ,
102c6fd2807SJeff Garzik 
103c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH		= 32,
104c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH_MASK	= MV_MAX_Q_DEPTH - 1,
105c6fd2807SJeff Garzik 
106c6fd2807SJeff Garzik 	/* CRQB needs alignment on a 1KB boundary. Size == 1KB
107c6fd2807SJeff Garzik 	 * CRPB needs alignment on a 256B boundary. Size == 256B
108c6fd2807SJeff Garzik 	 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
109c6fd2807SJeff Garzik 	 */
110c6fd2807SJeff Garzik 	MV_CRQB_Q_SZ		= (32 * MV_MAX_Q_DEPTH),
111c6fd2807SJeff Garzik 	MV_CRPB_Q_SZ		= (8 * MV_MAX_Q_DEPTH),
112da2fa9baSMark Lord 	MV_MAX_SG_CT		= 256,
113c6fd2807SJeff Garzik 	MV_SG_TBL_SZ		= (16 * MV_MAX_SG_CT),
114c6fd2807SJeff Garzik 
115352fab70SMark Lord 	/* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
116c6fd2807SJeff Garzik 	MV_PORT_HC_SHIFT	= 2,
117352fab70SMark Lord 	MV_PORTS_PER_HC		= (1 << MV_PORT_HC_SHIFT), /* 4 */
118352fab70SMark Lord 	/* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
119352fab70SMark Lord 	MV_PORT_MASK		= (MV_PORTS_PER_HC - 1),   /* 3 */
120c6fd2807SJeff Garzik 
121c6fd2807SJeff Garzik 	/* Host Flags */
122c6fd2807SJeff Garzik 	MV_FLAG_DUAL_HC		= (1 << 30),  /* two SATA Host Controllers */
123c6fd2807SJeff Garzik 	MV_FLAG_IRQ_COALESCE	= (1 << 29),  /* IRQ coalescing capability */
1247bb3c529SSaeed Bishara 	/* SoC integrated controllers, no PCI interface */
1257bb3c529SSaeed Bishara 	MV_FLAG_SOC		= (1 << 28),
1267bb3c529SSaeed Bishara 
127c5d3e45aSJeff Garzik 	MV_COMMON_FLAGS		= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
128bdd4dddeSJeff Garzik 				  ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
129bdd4dddeSJeff Garzik 				  ATA_FLAG_PIO_POLLING,
130c6fd2807SJeff Garzik 	MV_6XXX_FLAGS		= MV_FLAG_IRQ_COALESCE,
131c6fd2807SJeff Garzik 
132c6fd2807SJeff Garzik 	CRQB_FLAG_READ		= (1 << 0),
133c6fd2807SJeff Garzik 	CRQB_TAG_SHIFT		= 1,
134c5d3e45aSJeff Garzik 	CRQB_IOID_SHIFT		= 6,	/* CRQB Gen-II/IIE IO Id shift */
135e12bef50SMark Lord 	CRQB_PMP_SHIFT		= 12,	/* CRQB Gen-II/IIE PMP shift */
136c5d3e45aSJeff Garzik 	CRQB_HOSTQ_SHIFT	= 17,	/* CRQB Gen-II/IIE HostQueTag shift */
137c6fd2807SJeff Garzik 	CRQB_CMD_ADDR_SHIFT	= 8,
138c6fd2807SJeff Garzik 	CRQB_CMD_CS		= (0x2 << 11),
139c6fd2807SJeff Garzik 	CRQB_CMD_LAST		= (1 << 15),
140c6fd2807SJeff Garzik 
141c6fd2807SJeff Garzik 	CRPB_FLAG_STATUS_SHIFT	= 8,
142c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_6	= 5,	/* CRPB Gen-II IO Id shift */
143c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_7	= 7,	/* CRPB Gen-IIE IO Id shift */
144c6fd2807SJeff Garzik 
145c6fd2807SJeff Garzik 	EPRD_FLAG_END_OF_TBL	= (1 << 31),
146c6fd2807SJeff Garzik 
147c6fd2807SJeff Garzik 	/* PCI interface registers */
148c6fd2807SJeff Garzik 
149c6fd2807SJeff Garzik 	PCI_COMMAND_OFS		= 0xc00,
1508e7decdbSMark Lord 	PCI_COMMAND_MRDTRIG	= (1 << 7),	/* PCI Master Read Trigger */
151c6fd2807SJeff Garzik 
152c6fd2807SJeff Garzik 	PCI_MAIN_CMD_STS_OFS	= 0xd30,
153c6fd2807SJeff Garzik 	STOP_PCI_MASTER		= (1 << 2),
154c6fd2807SJeff Garzik 	PCI_MASTER_EMPTY	= (1 << 3),
155c6fd2807SJeff Garzik 	GLOB_SFT_RST		= (1 << 4),
156c6fd2807SJeff Garzik 
1578e7decdbSMark Lord 	MV_PCI_MODE_OFS		= 0xd00,
1588e7decdbSMark Lord 	MV_PCI_MODE_MASK	= 0x30,
1598e7decdbSMark Lord 
160c6fd2807SJeff Garzik 	MV_PCI_EXP_ROM_BAR_CTL	= 0xd2c,
161c6fd2807SJeff Garzik 	MV_PCI_DISC_TIMER	= 0xd04,
162c6fd2807SJeff Garzik 	MV_PCI_MSI_TRIGGER	= 0xc38,
163c6fd2807SJeff Garzik 	MV_PCI_SERR_MASK	= 0xc28,
1648e7decdbSMark Lord 	MV_PCI_XBAR_TMOUT_OFS	= 0x1d04,
165c6fd2807SJeff Garzik 	MV_PCI_ERR_LOW_ADDRESS	= 0x1d40,
166c6fd2807SJeff Garzik 	MV_PCI_ERR_HIGH_ADDRESS	= 0x1d44,
167c6fd2807SJeff Garzik 	MV_PCI_ERR_ATTRIBUTE	= 0x1d48,
168c6fd2807SJeff Garzik 	MV_PCI_ERR_COMMAND	= 0x1d50,
169c6fd2807SJeff Garzik 
170c6fd2807SJeff Garzik 	PCI_IRQ_CAUSE_OFS	= 0x1d58,
171c6fd2807SJeff Garzik 	PCI_IRQ_MASK_OFS	= 0x1d5c,
172c6fd2807SJeff Garzik 	PCI_UNMASK_ALL_IRQS	= 0x7fffff,	/* bits 22-0 */
173c6fd2807SJeff Garzik 
17402a121daSMark Lord 	PCIE_IRQ_CAUSE_OFS	= 0x1900,
17502a121daSMark Lord 	PCIE_IRQ_MASK_OFS	= 0x1910,
176646a4da5SMark Lord 	PCIE_UNMASK_ALL_IRQS	= 0x40a,	/* assorted bits */
17702a121daSMark Lord 
1787368f919SMark Lord 	/* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
1797368f919SMark Lord 	PCI_HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
1807368f919SMark Lord 	PCI_HC_MAIN_IRQ_MASK_OFS  = 0x1d64,
1817368f919SMark Lord 	SOC_HC_MAIN_IRQ_CAUSE_OFS = 0x20020,
1827368f919SMark Lord 	SOC_HC_MAIN_IRQ_MASK_OFS  = 0x20024,
183352fab70SMark Lord 	ERR_IRQ			= (1 << 0),	/* shift by port # */
184352fab70SMark Lord 	DONE_IRQ		= (1 << 1),	/* shift by port # */
185c6fd2807SJeff Garzik 	HC0_IRQ_PEND		= 0x1ff,	/* bits 0-8 = HC0's ports */
186c6fd2807SJeff Garzik 	HC_SHIFT		= 9,		/* bits 9-17 = HC1's ports */
187c6fd2807SJeff Garzik 	PCI_ERR			= (1 << 18),
188c6fd2807SJeff Garzik 	TRAN_LO_DONE		= (1 << 19),	/* 6xxx: IRQ coalescing */
189c6fd2807SJeff Garzik 	TRAN_HI_DONE		= (1 << 20),	/* 6xxx: IRQ coalescing */
190fb621e2fSJeff Garzik 	PORTS_0_3_COAL_DONE	= (1 << 8),
191fb621e2fSJeff Garzik 	PORTS_4_7_COAL_DONE	= (1 << 17),
192c6fd2807SJeff Garzik 	PORTS_0_7_COAL_DONE	= (1 << 21),	/* 6xxx: IRQ coalescing */
193c6fd2807SJeff Garzik 	GPIO_INT		= (1 << 22),
194c6fd2807SJeff Garzik 	SELF_INT		= (1 << 23),
195c6fd2807SJeff Garzik 	TWSI_INT		= (1 << 24),
196c6fd2807SJeff Garzik 	HC_MAIN_RSVD		= (0x7f << 25),	/* bits 31-25 */
197fb621e2fSJeff Garzik 	HC_MAIN_RSVD_5		= (0x1fff << 19), /* bits 31-19 */
198f351b2d6SSaeed Bishara 	HC_MAIN_RSVD_SOC	= (0x3fffffb << 6),     /* bits 31-9, 7-6 */
199c6fd2807SJeff Garzik 	HC_MAIN_MASKED_IRQS	= (TRAN_LO_DONE | TRAN_HI_DONE |
200f9f7fe01SMark Lord 				   PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
201c6fd2807SJeff Garzik 				   PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
202c6fd2807SJeff Garzik 				   HC_MAIN_RSVD),
203fb621e2fSJeff Garzik 	HC_MAIN_MASKED_IRQS_5	= (PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
204fb621e2fSJeff Garzik 				   HC_MAIN_RSVD_5),
205f351b2d6SSaeed Bishara 	HC_MAIN_MASKED_IRQS_SOC = (PORTS_0_3_COAL_DONE | HC_MAIN_RSVD_SOC),
206c6fd2807SJeff Garzik 
207c6fd2807SJeff Garzik 	/* SATAHC registers */
208c6fd2807SJeff Garzik 	HC_CFG_OFS		= 0,
209c6fd2807SJeff Garzik 
210c6fd2807SJeff Garzik 	HC_IRQ_CAUSE_OFS	= 0x14,
211352fab70SMark Lord 	DMA_IRQ			= (1 << 0),	/* shift by port # */
212352fab70SMark Lord 	HC_COAL_IRQ		= (1 << 4),	/* IRQ coalescing */
213c6fd2807SJeff Garzik 	DEV_IRQ			= (1 << 8),	/* shift by port # */
214c6fd2807SJeff Garzik 
215c6fd2807SJeff Garzik 	/* Shadow block registers */
216c6fd2807SJeff Garzik 	SHD_BLK_OFS		= 0x100,
217c6fd2807SJeff Garzik 	SHD_CTL_AST_OFS		= 0x20,		/* ofs from SHD_BLK_OFS */
218c6fd2807SJeff Garzik 
219c6fd2807SJeff Garzik 	/* SATA registers */
220c6fd2807SJeff Garzik 	SATA_STATUS_OFS		= 0x300,  /* ctrl, err regs follow status */
221c6fd2807SJeff Garzik 	SATA_ACTIVE_OFS		= 0x350,
2220c58912eSMark Lord 	SATA_FIS_IRQ_CAUSE_OFS	= 0x364,
22317c5aab5SMark Lord 
224e12bef50SMark Lord 	LTMODE_OFS		= 0x30c,
22517c5aab5SMark Lord 	LTMODE_BIT8		= (1 << 8),	/* unknown, but necessary */
22617c5aab5SMark Lord 
227c6fd2807SJeff Garzik 	PHY_MODE3		= 0x310,
228c6fd2807SJeff Garzik 	PHY_MODE4		= 0x314,
229c6fd2807SJeff Garzik 	PHY_MODE2		= 0x330,
230e12bef50SMark Lord 	SATA_IFCTL_OFS		= 0x344,
2318e7decdbSMark Lord 	SATA_TESTCTL_OFS	= 0x348,
232e12bef50SMark Lord 	SATA_IFSTAT_OFS		= 0x34c,
233e12bef50SMark Lord 	VENDOR_UNIQUE_FIS_OFS	= 0x35c,
23417c5aab5SMark Lord 
2358e7decdbSMark Lord 	FISCFG_OFS		= 0x360,
2368e7decdbSMark Lord 	FISCFG_WAIT_DEV_ERR	= (1 << 8),	/* wait for host on DevErr */
2378e7decdbSMark Lord 	FISCFG_SINGLE_SYNC	= (1 << 16),	/* SYNC on DMA activation */
23817c5aab5SMark Lord 
239c6fd2807SJeff Garzik 	MV5_PHY_MODE		= 0x74,
2408e7decdbSMark Lord 	MV5_LTMODE_OFS		= 0x30,
2418e7decdbSMark Lord 	MV5_PHY_CTL_OFS		= 0x0C,
2428e7decdbSMark Lord 	SATA_INTERFACE_CFG_OFS	= 0x050,
243c6fd2807SJeff Garzik 
244c6fd2807SJeff Garzik 	MV_M2_PREAMP_MASK	= 0x7e0,
245c6fd2807SJeff Garzik 
246c6fd2807SJeff Garzik 	/* Port registers */
247c6fd2807SJeff Garzik 	EDMA_CFG_OFS		= 0,
2480c58912eSMark Lord 	EDMA_CFG_Q_DEPTH	= 0x1f,		/* max device queue depth */
2490c58912eSMark Lord 	EDMA_CFG_NCQ		= (1 << 5),	/* for R/W FPDMA queued */
250c6fd2807SJeff Garzik 	EDMA_CFG_NCQ_GO_ON_ERR	= (1 << 14),	/* continue on error */
251c6fd2807SJeff Garzik 	EDMA_CFG_RD_BRST_EXT	= (1 << 11),	/* read burst 512B */
252c6fd2807SJeff Garzik 	EDMA_CFG_WR_BUFF_LEN	= (1 << 13),	/* write buffer 512B */
253e12bef50SMark Lord 	EDMA_CFG_EDMA_FBS	= (1 << 16),	/* EDMA FIS-Based Switching */
254e12bef50SMark Lord 	EDMA_CFG_FBS		= (1 << 26),	/* FIS-Based Switching */
255c6fd2807SJeff Garzik 
256c6fd2807SJeff Garzik 	EDMA_ERR_IRQ_CAUSE_OFS	= 0x8,
257c6fd2807SJeff Garzik 	EDMA_ERR_IRQ_MASK_OFS	= 0xc,
2586c1153e0SJeff Garzik 	EDMA_ERR_D_PAR		= (1 << 0),	/* UDMA data parity err */
2596c1153e0SJeff Garzik 	EDMA_ERR_PRD_PAR	= (1 << 1),	/* UDMA PRD parity err */
2606c1153e0SJeff Garzik 	EDMA_ERR_DEV		= (1 << 2),	/* device error */
2616c1153e0SJeff Garzik 	EDMA_ERR_DEV_DCON	= (1 << 3),	/* device disconnect */
2626c1153e0SJeff Garzik 	EDMA_ERR_DEV_CON	= (1 << 4),	/* device connected */
2636c1153e0SJeff Garzik 	EDMA_ERR_SERR		= (1 << 5),	/* SError bits [WBDST] raised */
264c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS	= (1 << 7),	/* Gen II/IIE self-disable */
265c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS_5	= (1 << 8),	/* Gen I self-disable */
2666c1153e0SJeff Garzik 	EDMA_ERR_BIST_ASYNC	= (1 << 8),	/* BIST FIS or Async Notify */
267c5d3e45aSJeff Garzik 	EDMA_ERR_TRANS_IRQ_7	= (1 << 8),	/* Gen IIE transprt layer irq */
2686c1153e0SJeff Garzik 	EDMA_ERR_CRQB_PAR	= (1 << 9),	/* CRQB parity error */
2696c1153e0SJeff Garzik 	EDMA_ERR_CRPB_PAR	= (1 << 10),	/* CRPB parity error */
2706c1153e0SJeff Garzik 	EDMA_ERR_INTRL_PAR	= (1 << 11),	/* internal parity error */
2716c1153e0SJeff Garzik 	EDMA_ERR_IORDY		= (1 << 12),	/* IORdy timeout */
272646a4da5SMark Lord 
2736c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_RX	= (0xf << 13),	/* link ctrl rx error */
274646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_0	= (1 << 13),	/* transient: CRC err */
275646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_1	= (1 << 14),	/* transient: FIFO err */
276646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_2	= (1 << 15),	/* fatal: caught SYNC */
277646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_3	= (1 << 16),	/* transient: FIS rx err */
278646a4da5SMark Lord 
2796c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_RX	= (0xf << 17),	/* link data rx error */
280646a4da5SMark Lord 
2816c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_TX	= (0x1f << 21),	/* link ctrl tx error */
282646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_0	= (1 << 21),	/* transient: CRC err */
283646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_1	= (1 << 22),	/* transient: FIFO err */
284646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_2	= (1 << 23),	/* transient: caught SYNC */
285646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_3	= (1 << 24),	/* transient: caught DMAT */
286646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_4	= (1 << 25),	/* transient: FIS collision */
287646a4da5SMark Lord 
2886c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_TX	= (0x1f << 26),	/* link data tx error */
289646a4da5SMark Lord 
2906c1153e0SJeff Garzik 	EDMA_ERR_TRANS_PROTO	= (1 << 31),	/* transport protocol error */
291c5d3e45aSJeff Garzik 	EDMA_ERR_OVERRUN_5	= (1 << 5),
292c5d3e45aSJeff Garzik 	EDMA_ERR_UNDERRUN_5	= (1 << 6),
293646a4da5SMark Lord 
294646a4da5SMark Lord 	EDMA_ERR_IRQ_TRANSIENT  = EDMA_ERR_LNK_CTRL_RX_0 |
295646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_1 |
296646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_3 |
29785afb934SMark Lord 				  EDMA_ERR_LNK_CTRL_TX,
298646a4da5SMark Lord 
299bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE		= EDMA_ERR_D_PAR |
300bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
301bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
302bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
303bdd4dddeSJeff Garzik 				  EDMA_ERR_SERR |
304bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS |
3056c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
306bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
307bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
308bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY |
309bdd4dddeSJeff Garzik 				  EDMA_ERR_LNK_CTRL_RX_2 |
310c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_RX |
311c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_TX |
312bdd4dddeSJeff Garzik 				  EDMA_ERR_TRANS_PROTO,
313e12bef50SMark Lord 
314bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE_5	= EDMA_ERR_D_PAR |
315bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
316bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
317bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
318bdd4dddeSJeff Garzik 				  EDMA_ERR_OVERRUN_5 |
319bdd4dddeSJeff Garzik 				  EDMA_ERR_UNDERRUN_5 |
320bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS_5 |
3216c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
322bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
323bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
324bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY,
325c6fd2807SJeff Garzik 
326c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_HI_OFS	= 0x10,
327c6fd2807SJeff Garzik 	EDMA_REQ_Q_IN_PTR_OFS	= 0x14,		/* also contains BASE_LO */
328c6fd2807SJeff Garzik 
329c6fd2807SJeff Garzik 	EDMA_REQ_Q_OUT_PTR_OFS	= 0x18,
330c6fd2807SJeff Garzik 	EDMA_REQ_Q_PTR_SHIFT	= 5,
331c6fd2807SJeff Garzik 
332c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_HI_OFS	= 0x1c,
333c6fd2807SJeff Garzik 	EDMA_RSP_Q_IN_PTR_OFS	= 0x20,
334c6fd2807SJeff Garzik 	EDMA_RSP_Q_OUT_PTR_OFS	= 0x24,		/* also contains BASE_LO */
335c6fd2807SJeff Garzik 	EDMA_RSP_Q_PTR_SHIFT	= 3,
336c6fd2807SJeff Garzik 
3370ea9e179SJeff Garzik 	EDMA_CMD_OFS		= 0x28,		/* EDMA command register */
3380ea9e179SJeff Garzik 	EDMA_EN			= (1 << 0),	/* enable EDMA */
3390ea9e179SJeff Garzik 	EDMA_DS			= (1 << 1),	/* disable EDMA; self-negated */
3408e7decdbSMark Lord 	EDMA_RESET		= (1 << 2),	/* reset eng/trans/link/phy */
341c6fd2807SJeff Garzik 
3428e7decdbSMark Lord 	EDMA_STATUS_OFS		= 0x30,		/* EDMA engine status */
3438e7decdbSMark Lord 	EDMA_STATUS_CACHE_EMPTY	= (1 << 6),	/* GenIIe command cache empty */
3448e7decdbSMark Lord 	EDMA_STATUS_IDLE	= (1 << 7),	/* GenIIe EDMA enabled/idle */
3458e7decdbSMark Lord 
3468e7decdbSMark Lord 	EDMA_IORDY_TMOUT_OFS	= 0x34,
3478e7decdbSMark Lord 	EDMA_ARB_CFG_OFS	= 0x38,
3488e7decdbSMark Lord 
3498e7decdbSMark Lord 	EDMA_HALTCOND_OFS	= 0x60,		/* GenIIe halt conditions */
350c6fd2807SJeff Garzik 
351352fab70SMark Lord 	GEN_II_NCQ_MAX_SECTORS	= 256,		/* max sects/io on Gen2 w/NCQ */
352352fab70SMark Lord 
353c6fd2807SJeff Garzik 	/* Host private flags (hp_flags) */
354c6fd2807SJeff Garzik 	MV_HP_FLAG_MSI		= (1 << 0),
355c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB0	= (1 << 1),
356c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB2	= (1 << 2),
357c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1B2	= (1 << 3),
358c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1C0	= (1 << 4),
359c6fd2807SJeff Garzik 	MV_HP_ERRATA_XX42A0	= (1 << 5),
3600ea9e179SJeff Garzik 	MV_HP_GEN_I		= (1 << 6),	/* Generation I: 50xx */
3610ea9e179SJeff Garzik 	MV_HP_GEN_II		= (1 << 7),	/* Generation II: 60xx */
3620ea9e179SJeff Garzik 	MV_HP_GEN_IIE		= (1 << 8),	/* Generation IIE: 6042/7042 */
36302a121daSMark Lord 	MV_HP_PCIE		= (1 << 9),	/* PCIe bus/regs: 7042 */
364616d4a98SMark Lord 	MV_HP_CUT_THROUGH	= (1 << 10),	/* can use EDMA cut-through */
365c6fd2807SJeff Garzik 
366c6fd2807SJeff Garzik 	/* Port private flags (pp_flags) */
3670ea9e179SJeff Garzik 	MV_PP_FLAG_EDMA_EN	= (1 << 0),	/* is EDMA engine enabled? */
36872109168SMark Lord 	MV_PP_FLAG_NCQ_EN	= (1 << 1),	/* is EDMA set up for NCQ? */
369c6fd2807SJeff Garzik };
370c6fd2807SJeff Garzik 
371ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
372ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
373c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
3748e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
3757bb3c529SSaeed Bishara #define HAS_PCI(host) (!((host)->ports[0]->flags & MV_FLAG_SOC))
376c6fd2807SJeff Garzik 
37715a32632SLennert Buytenhek #define WINDOW_CTRL(i)		(0x20030 + ((i) << 4))
37815a32632SLennert Buytenhek #define WINDOW_BASE(i)		(0x20034 + ((i) << 4))
37915a32632SLennert Buytenhek 
380c6fd2807SJeff Garzik enum {
381baf14aa1SJeff Garzik 	/* DMA boundary 0xffff is required by the s/g splitting
382baf14aa1SJeff Garzik 	 * we need on /length/ in mv_fill-sg().
383baf14aa1SJeff Garzik 	 */
384baf14aa1SJeff Garzik 	MV_DMA_BOUNDARY		= 0xffffU,
385c6fd2807SJeff Garzik 
3860ea9e179SJeff Garzik 	/* mask of register bits containing lower 32 bits
3870ea9e179SJeff Garzik 	 * of EDMA request queue DMA address
3880ea9e179SJeff Garzik 	 */
389c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_LO_MASK	= 0xfffffc00U,
390c6fd2807SJeff Garzik 
3910ea9e179SJeff Garzik 	/* ditto, for response queue */
392c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_LO_MASK	= 0xffffff00U,
393c6fd2807SJeff Garzik };
394c6fd2807SJeff Garzik 
395c6fd2807SJeff Garzik enum chip_type {
396c6fd2807SJeff Garzik 	chip_504x,
397c6fd2807SJeff Garzik 	chip_508x,
398c6fd2807SJeff Garzik 	chip_5080,
399c6fd2807SJeff Garzik 	chip_604x,
400c6fd2807SJeff Garzik 	chip_608x,
401c6fd2807SJeff Garzik 	chip_6042,
402c6fd2807SJeff Garzik 	chip_7042,
403f351b2d6SSaeed Bishara 	chip_soc,
404c6fd2807SJeff Garzik };
405c6fd2807SJeff Garzik 
406c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
407c6fd2807SJeff Garzik struct mv_crqb {
408c6fd2807SJeff Garzik 	__le32			sg_addr;
409c6fd2807SJeff Garzik 	__le32			sg_addr_hi;
410c6fd2807SJeff Garzik 	__le16			ctrl_flags;
411c6fd2807SJeff Garzik 	__le16			ata_cmd[11];
412c6fd2807SJeff Garzik };
413c6fd2807SJeff Garzik 
414c6fd2807SJeff Garzik struct mv_crqb_iie {
415c6fd2807SJeff Garzik 	__le32			addr;
416c6fd2807SJeff Garzik 	__le32			addr_hi;
417c6fd2807SJeff Garzik 	__le32			flags;
418c6fd2807SJeff Garzik 	__le32			len;
419c6fd2807SJeff Garzik 	__le32			ata_cmd[4];
420c6fd2807SJeff Garzik };
421c6fd2807SJeff Garzik 
422c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
423c6fd2807SJeff Garzik struct mv_crpb {
424c6fd2807SJeff Garzik 	__le16			id;
425c6fd2807SJeff Garzik 	__le16			flags;
426c6fd2807SJeff Garzik 	__le32			tmstmp;
427c6fd2807SJeff Garzik };
428c6fd2807SJeff Garzik 
429c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
430c6fd2807SJeff Garzik struct mv_sg {
431c6fd2807SJeff Garzik 	__le32			addr;
432c6fd2807SJeff Garzik 	__le32			flags_size;
433c6fd2807SJeff Garzik 	__le32			addr_hi;
434c6fd2807SJeff Garzik 	__le32			reserved;
435c6fd2807SJeff Garzik };
436c6fd2807SJeff Garzik 
437c6fd2807SJeff Garzik struct mv_port_priv {
438c6fd2807SJeff Garzik 	struct mv_crqb		*crqb;
439c6fd2807SJeff Garzik 	dma_addr_t		crqb_dma;
440c6fd2807SJeff Garzik 	struct mv_crpb		*crpb;
441c6fd2807SJeff Garzik 	dma_addr_t		crpb_dma;
442eb73d558SMark Lord 	struct mv_sg		*sg_tbl[MV_MAX_Q_DEPTH];
443eb73d558SMark Lord 	dma_addr_t		sg_tbl_dma[MV_MAX_Q_DEPTH];
444bdd4dddeSJeff Garzik 
445bdd4dddeSJeff Garzik 	unsigned int		req_idx;
446bdd4dddeSJeff Garzik 	unsigned int		resp_idx;
447bdd4dddeSJeff Garzik 
448c6fd2807SJeff Garzik 	u32			pp_flags;
449c6fd2807SJeff Garzik };
450c6fd2807SJeff Garzik 
451c6fd2807SJeff Garzik struct mv_port_signal {
452c6fd2807SJeff Garzik 	u32			amps;
453c6fd2807SJeff Garzik 	u32			pre;
454c6fd2807SJeff Garzik };
455c6fd2807SJeff Garzik 
45602a121daSMark Lord struct mv_host_priv {
45702a121daSMark Lord 	u32			hp_flags;
45802a121daSMark Lord 	struct mv_port_signal	signal[8];
45902a121daSMark Lord 	const struct mv_hw_ops	*ops;
460f351b2d6SSaeed Bishara 	int			n_ports;
461f351b2d6SSaeed Bishara 	void __iomem		*base;
4627368f919SMark Lord 	void __iomem		*main_irq_cause_addr;
4637368f919SMark Lord 	void __iomem		*main_irq_mask_addr;
46402a121daSMark Lord 	u32			irq_cause_ofs;
46502a121daSMark Lord 	u32			irq_mask_ofs;
46602a121daSMark Lord 	u32			unmask_all_irqs;
467da2fa9baSMark Lord 	/*
468da2fa9baSMark Lord 	 * These consistent DMA memory pools give us guaranteed
469da2fa9baSMark Lord 	 * alignment for hardware-accessed data structures,
470da2fa9baSMark Lord 	 * and less memory waste in accomplishing the alignment.
471da2fa9baSMark Lord 	 */
472da2fa9baSMark Lord 	struct dma_pool		*crqb_pool;
473da2fa9baSMark Lord 	struct dma_pool		*crpb_pool;
474da2fa9baSMark Lord 	struct dma_pool		*sg_tbl_pool;
47502a121daSMark Lord };
47602a121daSMark Lord 
477c6fd2807SJeff Garzik struct mv_hw_ops {
478c6fd2807SJeff Garzik 	void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
479c6fd2807SJeff Garzik 			   unsigned int port);
480c6fd2807SJeff Garzik 	void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
481c6fd2807SJeff Garzik 	void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
482c6fd2807SJeff Garzik 			   void __iomem *mmio);
483c6fd2807SJeff Garzik 	int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
484c6fd2807SJeff Garzik 			unsigned int n_hc);
485c6fd2807SJeff Garzik 	void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
4867bb3c529SSaeed Bishara 	void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
487c6fd2807SJeff Garzik };
488c6fd2807SJeff Garzik 
489da3dbb17STejun Heo static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
490da3dbb17STejun Heo static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
491da3dbb17STejun Heo static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
492da3dbb17STejun Heo static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
493c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap);
494c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap);
495c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc);
496c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
497c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
498a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
499a1efdabaSTejun Heo 			unsigned long deadline);
500bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap);
501bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap);
502f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev);
503c6fd2807SJeff Garzik 
504c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
505c6fd2807SJeff Garzik 			   unsigned int port);
506c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
507c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
508c6fd2807SJeff Garzik 			   void __iomem *mmio);
509c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
510c6fd2807SJeff Garzik 			unsigned int n_hc);
511c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
5127bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
513c6fd2807SJeff Garzik 
514c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
515c6fd2807SJeff Garzik 			   unsigned int port);
516c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
517c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
518c6fd2807SJeff Garzik 			   void __iomem *mmio);
519c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
520c6fd2807SJeff Garzik 			unsigned int n_hc);
521c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
522f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
523f351b2d6SSaeed Bishara 				      void __iomem *mmio);
524f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
525f351b2d6SSaeed Bishara 				      void __iomem *mmio);
526f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
527f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc);
528f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
529f351b2d6SSaeed Bishara 				      void __iomem *mmio);
530f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
5317bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
532e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
533c6fd2807SJeff Garzik 			     unsigned int port_no);
534e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap);
535b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio);
536e12bef50SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq);
537c6fd2807SJeff Garzik 
538e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp);
539e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
540e49856d8SMark Lord 				unsigned long deadline);
541e49856d8SMark Lord static int  mv_softreset(struct ata_link *link, unsigned int *class,
542e49856d8SMark Lord 				unsigned long deadline);
543c6fd2807SJeff Garzik 
544eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
545eb73d558SMark Lord  * because we have to allow room for worst case splitting of
546eb73d558SMark Lord  * PRDs for 64K boundaries in mv_fill_sg().
547eb73d558SMark Lord  */
548c5d3e45aSJeff Garzik static struct scsi_host_template mv5_sht = {
54968d1d07bSTejun Heo 	ATA_BASE_SHT(DRV_NAME),
550baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
551c5d3e45aSJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
552c5d3e45aSJeff Garzik };
553c5d3e45aSJeff Garzik 
554c5d3e45aSJeff Garzik static struct scsi_host_template mv6_sht = {
55568d1d07bSTejun Heo 	ATA_NCQ_SHT(DRV_NAME),
556138bfdd0SMark Lord 	.can_queue		= MV_MAX_Q_DEPTH - 1,
557baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
558c6fd2807SJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
559c6fd2807SJeff Garzik };
560c6fd2807SJeff Garzik 
561029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = {
562029cfd6bSTejun Heo 	.inherits		= &ata_sff_port_ops,
563c6fd2807SJeff Garzik 
564c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep,
565c6fd2807SJeff Garzik 	.qc_issue		= mv_qc_issue,
566c6fd2807SJeff Garzik 
567bdd4dddeSJeff Garzik 	.freeze			= mv_eh_freeze,
568bdd4dddeSJeff Garzik 	.thaw			= mv_eh_thaw,
569a1efdabaSTejun Heo 	.hardreset		= mv_hardreset,
570a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler, /* avoid SFF EH */
571029cfd6bSTejun Heo 	.post_internal_cmd	= ATA_OP_NULL,
572bdd4dddeSJeff Garzik 
573c6fd2807SJeff Garzik 	.scr_read		= mv5_scr_read,
574c6fd2807SJeff Garzik 	.scr_write		= mv5_scr_write,
575c6fd2807SJeff Garzik 
576c6fd2807SJeff Garzik 	.port_start		= mv_port_start,
577c6fd2807SJeff Garzik 	.port_stop		= mv_port_stop,
578c6fd2807SJeff Garzik };
579c6fd2807SJeff Garzik 
580029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
581029cfd6bSTejun Heo 	.inherits		= &mv5_ops,
582e49856d8SMark Lord 	.qc_defer		= sata_pmp_qc_defer_cmd_switch,
583f273827eSMark Lord 	.dev_config             = mv6_dev_config,
584c6fd2807SJeff Garzik 	.scr_read		= mv_scr_read,
585c6fd2807SJeff Garzik 	.scr_write		= mv_scr_write,
586c6fd2807SJeff Garzik 
587e49856d8SMark Lord 	.pmp_hardreset		= mv_pmp_hardreset,
588e49856d8SMark Lord 	.pmp_softreset		= mv_softreset,
589e49856d8SMark Lord 	.softreset		= mv_softreset,
590e49856d8SMark Lord 	.error_handler		= sata_pmp_error_handler,
591c6fd2807SJeff Garzik };
592c6fd2807SJeff Garzik 
593029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
594029cfd6bSTejun Heo 	.inherits		= &mv6_ops,
595e49856d8SMark Lord 	.qc_defer		= ata_std_qc_defer, /* FIS-based switching */
596029cfd6bSTejun Heo 	.dev_config		= ATA_OP_NULL,
597c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep_iie,
598c6fd2807SJeff Garzik };
599c6fd2807SJeff Garzik 
600c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
601c6fd2807SJeff Garzik 	{  /* chip_504x */
602cca3974eSJeff Garzik 		.flags		= MV_COMMON_FLAGS,
603c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
604bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
605c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
606c6fd2807SJeff Garzik 	},
607c6fd2807SJeff Garzik 	{  /* chip_508x */
608c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
609c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
610bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
611c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
612c6fd2807SJeff Garzik 	},
613c6fd2807SJeff Garzik 	{  /* chip_5080 */
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_604x */
620138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
621e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
622138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
623c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
624bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
625c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
626c6fd2807SJeff Garzik 	},
627c6fd2807SJeff Garzik 	{  /* chip_608x */
628c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
629e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
630138bfdd0SMark Lord 				  ATA_FLAG_NCQ | MV_FLAG_DUAL_HC,
631c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
632bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
633c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
634c6fd2807SJeff Garzik 	},
635c6fd2807SJeff Garzik 	{  /* chip_6042 */
636138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
637e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
638138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
639c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
640bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
641c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
642c6fd2807SJeff Garzik 	},
643c6fd2807SJeff Garzik 	{  /* chip_7042 */
644138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
645e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
646138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
647c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
648bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
649c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
650c6fd2807SJeff Garzik 	},
651f351b2d6SSaeed Bishara 	{  /* chip_soc */
65202c1f32fSMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
653e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
65402c1f32fSMark Lord 				  ATA_FLAG_NCQ | MV_FLAG_SOC,
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 },
666cfbf723eSAlan Cox 	/* RocketRAID 1740/174x have different identifiers */
667cfbf723eSAlan Cox 	{ PCI_VDEVICE(TTI, 0x1740), chip_508x },
668cfbf723eSAlan Cox 	{ PCI_VDEVICE(TTI, 0x1742), chip_508x },
669c6fd2807SJeff Garzik 
6702d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
6712d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
6722d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
6732d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
6742d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
675c6fd2807SJeff Garzik 
6762d2744fcSJeff Garzik 	{ PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
6772d2744fcSJeff Garzik 
678d9f9c6bcSFlorian Attenberger 	/* Adaptec 1430SA */
679d9f9c6bcSFlorian Attenberger 	{ PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
680d9f9c6bcSFlorian Attenberger 
68102a121daSMark Lord 	/* Marvell 7042 support */
6826a3d586dSMorrison, Tom 	{ PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
6836a3d586dSMorrison, Tom 
68402a121daSMark Lord 	/* Highpoint RocketRAID PCIe series */
68502a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2300), chip_7042 },
68602a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2310), chip_7042 },
68702a121daSMark Lord 
688c6fd2807SJeff Garzik 	{ }			/* terminate list */
689c6fd2807SJeff Garzik };
690c6fd2807SJeff Garzik 
691c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
692c6fd2807SJeff Garzik 	.phy_errata		= mv5_phy_errata,
693c6fd2807SJeff Garzik 	.enable_leds		= mv5_enable_leds,
694c6fd2807SJeff Garzik 	.read_preamp		= mv5_read_preamp,
695c6fd2807SJeff Garzik 	.reset_hc		= mv5_reset_hc,
696c6fd2807SJeff Garzik 	.reset_flash		= mv5_reset_flash,
697c6fd2807SJeff Garzik 	.reset_bus		= mv5_reset_bus,
698c6fd2807SJeff Garzik };
699c6fd2807SJeff Garzik 
700c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
701c6fd2807SJeff Garzik 	.phy_errata		= mv6_phy_errata,
702c6fd2807SJeff Garzik 	.enable_leds		= mv6_enable_leds,
703c6fd2807SJeff Garzik 	.read_preamp		= mv6_read_preamp,
704c6fd2807SJeff Garzik 	.reset_hc		= mv6_reset_hc,
705c6fd2807SJeff Garzik 	.reset_flash		= mv6_reset_flash,
706c6fd2807SJeff Garzik 	.reset_bus		= mv_reset_pci_bus,
707c6fd2807SJeff Garzik };
708c6fd2807SJeff Garzik 
709f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
710f351b2d6SSaeed Bishara 	.phy_errata		= mv6_phy_errata,
711f351b2d6SSaeed Bishara 	.enable_leds		= mv_soc_enable_leds,
712f351b2d6SSaeed Bishara 	.read_preamp		= mv_soc_read_preamp,
713f351b2d6SSaeed Bishara 	.reset_hc		= mv_soc_reset_hc,
714f351b2d6SSaeed Bishara 	.reset_flash		= mv_soc_reset_flash,
715f351b2d6SSaeed Bishara 	.reset_bus		= mv_soc_reset_bus,
716f351b2d6SSaeed Bishara };
717f351b2d6SSaeed Bishara 
718c6fd2807SJeff Garzik /*
719c6fd2807SJeff Garzik  * Functions
720c6fd2807SJeff Garzik  */
721c6fd2807SJeff Garzik 
722c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
723c6fd2807SJeff Garzik {
724c6fd2807SJeff Garzik 	writel(data, addr);
725c6fd2807SJeff Garzik 	(void) readl(addr);	/* flush to avoid PCI posted write */
726c6fd2807SJeff Garzik }
727c6fd2807SJeff Garzik 
728c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
729c6fd2807SJeff Garzik {
730c6fd2807SJeff Garzik 	return port >> MV_PORT_HC_SHIFT;
731c6fd2807SJeff Garzik }
732c6fd2807SJeff Garzik 
733c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
734c6fd2807SJeff Garzik {
735c6fd2807SJeff Garzik 	return port & MV_PORT_MASK;
736c6fd2807SJeff Garzik }
737c6fd2807SJeff Garzik 
7381cfd19aeSMark Lord /*
7391cfd19aeSMark Lord  * Consolidate some rather tricky bit shift calculations.
7401cfd19aeSMark Lord  * This is hot-path stuff, so not a function.
7411cfd19aeSMark Lord  * Simple code, with two return values, so macro rather than inline.
7421cfd19aeSMark Lord  *
7431cfd19aeSMark Lord  * port is the sole input, in range 0..7.
7447368f919SMark Lord  * shift is one output, for use with main_irq_cause / main_irq_mask registers.
7457368f919SMark Lord  * hardport is the other output, in range 0..3.
7461cfd19aeSMark Lord  *
7471cfd19aeSMark Lord  * Note that port and hardport may be the same variable in some cases.
7481cfd19aeSMark Lord  */
7491cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport)	\
7501cfd19aeSMark Lord {								\
7511cfd19aeSMark Lord 	shift    = mv_hc_from_port(port) * HC_SHIFT;		\
7521cfd19aeSMark Lord 	hardport = mv_hardport_from_port(port);			\
7531cfd19aeSMark Lord 	shift   += hardport * 2;				\
7541cfd19aeSMark Lord }
7551cfd19aeSMark Lord 
756352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
757352fab70SMark Lord {
758352fab70SMark Lord 	return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
759352fab70SMark Lord }
760352fab70SMark Lord 
761c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
762c6fd2807SJeff Garzik 						 unsigned int port)
763c6fd2807SJeff Garzik {
764c6fd2807SJeff Garzik 	return mv_hc_base(base, mv_hc_from_port(port));
765c6fd2807SJeff Garzik }
766c6fd2807SJeff Garzik 
767c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
768c6fd2807SJeff Garzik {
769c6fd2807SJeff Garzik 	return  mv_hc_base_from_port(base, port) +
770c6fd2807SJeff Garzik 		MV_SATAHC_ARBTR_REG_SZ +
771c6fd2807SJeff Garzik 		(mv_hardport_from_port(port) * MV_PORT_REG_SZ);
772c6fd2807SJeff Garzik }
773c6fd2807SJeff Garzik 
774e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
775e12bef50SMark Lord {
776e12bef50SMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
777e12bef50SMark Lord 	unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
778e12bef50SMark Lord 
779e12bef50SMark Lord 	return hc_mmio + ofs;
780e12bef50SMark Lord }
781e12bef50SMark Lord 
782f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
783f351b2d6SSaeed Bishara {
784f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
785f351b2d6SSaeed Bishara 	return hpriv->base;
786f351b2d6SSaeed Bishara }
787f351b2d6SSaeed Bishara 
788c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
789c6fd2807SJeff Garzik {
790f351b2d6SSaeed Bishara 	return mv_port_base(mv_host_base(ap->host), ap->port_no);
791c6fd2807SJeff Garzik }
792c6fd2807SJeff Garzik 
793cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
794c6fd2807SJeff Garzik {
795cca3974eSJeff Garzik 	return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
796c6fd2807SJeff Garzik }
797c6fd2807SJeff Garzik 
798c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
799c5d3e45aSJeff Garzik 			     struct mv_host_priv *hpriv,
800c5d3e45aSJeff Garzik 			     struct mv_port_priv *pp)
801c5d3e45aSJeff Garzik {
802bdd4dddeSJeff Garzik 	u32 index;
803bdd4dddeSJeff Garzik 
804c5d3e45aSJeff Garzik 	/*
805c5d3e45aSJeff Garzik 	 * initialize request queue
806c5d3e45aSJeff Garzik 	 */
807fcfb1f77SMark Lord 	pp->req_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
808fcfb1f77SMark Lord 	index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
809bdd4dddeSJeff Garzik 
810c5d3e45aSJeff Garzik 	WARN_ON(pp->crqb_dma & 0x3ff);
811c5d3e45aSJeff Garzik 	writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
812bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
813c5d3e45aSJeff Garzik 		 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
814c5d3e45aSJeff Garzik 
815c5d3e45aSJeff Garzik 	if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
816bdd4dddeSJeff Garzik 		writelfl((pp->crqb_dma & 0xffffffff) | index,
817c5d3e45aSJeff Garzik 			 port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
818c5d3e45aSJeff Garzik 	else
819bdd4dddeSJeff Garzik 		writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
820c5d3e45aSJeff Garzik 
821c5d3e45aSJeff Garzik 	/*
822c5d3e45aSJeff Garzik 	 * initialize response queue
823c5d3e45aSJeff Garzik 	 */
824fcfb1f77SMark Lord 	pp->resp_idx &= MV_MAX_Q_DEPTH_MASK;	/* paranoia */
825fcfb1f77SMark Lord 	index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
826bdd4dddeSJeff Garzik 
827c5d3e45aSJeff Garzik 	WARN_ON(pp->crpb_dma & 0xff);
828c5d3e45aSJeff Garzik 	writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
829c5d3e45aSJeff Garzik 
830c5d3e45aSJeff Garzik 	if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
831bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & 0xffffffff) | index,
832c5d3e45aSJeff Garzik 			 port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
833c5d3e45aSJeff Garzik 	else
834bdd4dddeSJeff Garzik 		writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
835c5d3e45aSJeff Garzik 
836bdd4dddeSJeff Garzik 	writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
837c5d3e45aSJeff Garzik 		 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
838c5d3e45aSJeff Garzik }
839c5d3e45aSJeff Garzik 
840c6fd2807SJeff Garzik /**
841c6fd2807SJeff Garzik  *      mv_start_dma - Enable eDMA engine
842c6fd2807SJeff Garzik  *      @base: port base address
843c6fd2807SJeff Garzik  *      @pp: port private data
844c6fd2807SJeff Garzik  *
845c6fd2807SJeff Garzik  *      Verify the local cache of the eDMA state is accurate with a
846c6fd2807SJeff Garzik  *      WARN_ON.
847c6fd2807SJeff Garzik  *
848c6fd2807SJeff Garzik  *      LOCKING:
849c6fd2807SJeff Garzik  *      Inherited from caller.
850c6fd2807SJeff Garzik  */
8510c58912eSMark Lord static void mv_start_dma(struct ata_port *ap, void __iomem *port_mmio,
85272109168SMark Lord 			 struct mv_port_priv *pp, u8 protocol)
853c6fd2807SJeff Garzik {
85472109168SMark Lord 	int want_ncq = (protocol == ATA_PROT_NCQ);
85572109168SMark Lord 
85672109168SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
85772109168SMark Lord 		int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
85872109168SMark Lord 		if (want_ncq != using_ncq)
859b562468cSMark Lord 			mv_stop_edma(ap);
86072109168SMark Lord 	}
861c5d3e45aSJeff Garzik 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
8620c58912eSMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
863352fab70SMark Lord 		int hardport = mv_hardport_from_port(ap->port_no);
8640c58912eSMark Lord 		void __iomem *hc_mmio = mv_hc_base_from_port(
865352fab70SMark Lord 					mv_host_base(ap->host), hardport);
8660c58912eSMark Lord 		u32 hc_irq_cause, ipending;
8670c58912eSMark Lord 
868bdd4dddeSJeff Garzik 		/* clear EDMA event indicators, if any */
869f630d562SMark Lord 		writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
870bdd4dddeSJeff Garzik 
8710c58912eSMark Lord 		/* clear EDMA interrupt indicator, if any */
8720c58912eSMark Lord 		hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
873352fab70SMark Lord 		ipending = (DEV_IRQ | DMA_IRQ) << hardport;
8740c58912eSMark Lord 		if (hc_irq_cause & ipending) {
8750c58912eSMark Lord 			writelfl(hc_irq_cause & ~ipending,
8760c58912eSMark Lord 				 hc_mmio + HC_IRQ_CAUSE_OFS);
8770c58912eSMark Lord 		}
8780c58912eSMark Lord 
879e12bef50SMark Lord 		mv_edma_cfg(ap, want_ncq);
8800c58912eSMark Lord 
8810c58912eSMark Lord 		/* clear FIS IRQ Cause */
8820c58912eSMark Lord 		writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
8830c58912eSMark Lord 
884f630d562SMark Lord 		mv_set_edma_ptrs(port_mmio, hpriv, pp);
885bdd4dddeSJeff Garzik 
886f630d562SMark Lord 		writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
887c6fd2807SJeff Garzik 		pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
888c6fd2807SJeff Garzik 	}
889c6fd2807SJeff Garzik }
890c6fd2807SJeff Garzik 
891*9b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
892*9b2c4e0bSMark Lord {
893*9b2c4e0bSMark Lord 	void __iomem *port_mmio = mv_ap_base(ap);
894*9b2c4e0bSMark Lord 	const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
895*9b2c4e0bSMark Lord 	const int per_loop = 5, timeout = (15 * 1000 / per_loop);
896*9b2c4e0bSMark Lord 	int i;
897*9b2c4e0bSMark Lord 
898*9b2c4e0bSMark Lord 	/*
899*9b2c4e0bSMark Lord 	 * Wait for the EDMA engine to finish transactions in progress.
900*9b2c4e0bSMark Lord 	 */
901*9b2c4e0bSMark Lord 	for (i = 0; i < timeout; ++i) {
902*9b2c4e0bSMark Lord 		u32 edma_stat = readl(port_mmio + EDMA_STATUS_OFS);
903*9b2c4e0bSMark Lord 		if ((edma_stat & empty_idle) == empty_idle)
904*9b2c4e0bSMark Lord 			break;
905*9b2c4e0bSMark Lord 		udelay(per_loop);
906*9b2c4e0bSMark Lord 	}
907*9b2c4e0bSMark Lord 	/* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
908*9b2c4e0bSMark Lord }
909*9b2c4e0bSMark Lord 
910c6fd2807SJeff Garzik /**
911e12bef50SMark Lord  *      mv_stop_edma_engine - Disable eDMA engine
912b562468cSMark Lord  *      @port_mmio: io base address
913c6fd2807SJeff Garzik  *
914c6fd2807SJeff Garzik  *      LOCKING:
915c6fd2807SJeff Garzik  *      Inherited from caller.
916c6fd2807SJeff Garzik  */
917b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
918c6fd2807SJeff Garzik {
919b562468cSMark Lord 	int i;
920c6fd2807SJeff Garzik 
921b562468cSMark Lord 	/* Disable eDMA.  The disable bit auto clears. */
922c6fd2807SJeff Garzik 	writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
923c6fd2807SJeff Garzik 
924b562468cSMark Lord 	/* Wait for the chip to confirm eDMA is off. */
925b562468cSMark Lord 	for (i = 10000; i > 0; i--) {
926b562468cSMark Lord 		u32 reg = readl(port_mmio + EDMA_CMD_OFS);
9274537deb5SJeff Garzik 		if (!(reg & EDMA_EN))
928b562468cSMark Lord 			return 0;
929b562468cSMark Lord 		udelay(10);
930c6fd2807SJeff Garzik 	}
931b562468cSMark Lord 	return -EIO;
932c6fd2807SJeff Garzik }
933c6fd2807SJeff Garzik 
934e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
935c6fd2807SJeff Garzik {
936c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
937c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
938c6fd2807SJeff Garzik 
939b562468cSMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
940b562468cSMark Lord 		return 0;
941c6fd2807SJeff Garzik 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
942*9b2c4e0bSMark Lord 	mv_wait_for_edma_empty_idle(ap);
943b562468cSMark Lord 	if (mv_stop_edma_engine(port_mmio)) {
944c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
945b562468cSMark Lord 		return -EIO;
946c6fd2807SJeff Garzik 	}
947b562468cSMark Lord 	return 0;
9480ea9e179SJeff Garzik }
9490ea9e179SJeff Garzik 
950c6fd2807SJeff Garzik #ifdef ATA_DEBUG
951c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes)
952c6fd2807SJeff Garzik {
953c6fd2807SJeff Garzik 	int b, w;
954c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
955c6fd2807SJeff Garzik 		DPRINTK("%p: ", start + b);
956c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
957c6fd2807SJeff Garzik 			printk("%08x ", readl(start + b));
958c6fd2807SJeff Garzik 			b += sizeof(u32);
959c6fd2807SJeff Garzik 		}
960c6fd2807SJeff Garzik 		printk("\n");
961c6fd2807SJeff Garzik 	}
962c6fd2807SJeff Garzik }
963c6fd2807SJeff Garzik #endif
964c6fd2807SJeff Garzik 
965c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
966c6fd2807SJeff Garzik {
967c6fd2807SJeff Garzik #ifdef ATA_DEBUG
968c6fd2807SJeff Garzik 	int b, w;
969c6fd2807SJeff Garzik 	u32 dw;
970c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
971c6fd2807SJeff Garzik 		DPRINTK("%02x: ", b);
972c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
973c6fd2807SJeff Garzik 			(void) pci_read_config_dword(pdev, b, &dw);
974c6fd2807SJeff Garzik 			printk("%08x ", dw);
975c6fd2807SJeff Garzik 			b += sizeof(u32);
976c6fd2807SJeff Garzik 		}
977c6fd2807SJeff Garzik 		printk("\n");
978c6fd2807SJeff Garzik 	}
979c6fd2807SJeff Garzik #endif
980c6fd2807SJeff Garzik }
981c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port,
982c6fd2807SJeff Garzik 			     struct pci_dev *pdev)
983c6fd2807SJeff Garzik {
984c6fd2807SJeff Garzik #ifdef ATA_DEBUG
985c6fd2807SJeff Garzik 	void __iomem *hc_base = mv_hc_base(mmio_base,
986c6fd2807SJeff Garzik 					   port >> MV_PORT_HC_SHIFT);
987c6fd2807SJeff Garzik 	void __iomem *port_base;
988c6fd2807SJeff Garzik 	int start_port, num_ports, p, start_hc, num_hcs, hc;
989c6fd2807SJeff Garzik 
990c6fd2807SJeff Garzik 	if (0 > port) {
991c6fd2807SJeff Garzik 		start_hc = start_port = 0;
992c6fd2807SJeff Garzik 		num_ports = 8;		/* shld be benign for 4 port devs */
993c6fd2807SJeff Garzik 		num_hcs = 2;
994c6fd2807SJeff Garzik 	} else {
995c6fd2807SJeff Garzik 		start_hc = port >> MV_PORT_HC_SHIFT;
996c6fd2807SJeff Garzik 		start_port = port;
997c6fd2807SJeff Garzik 		num_ports = num_hcs = 1;
998c6fd2807SJeff Garzik 	}
999c6fd2807SJeff Garzik 	DPRINTK("All registers for port(s) %u-%u:\n", start_port,
1000c6fd2807SJeff Garzik 		num_ports > 1 ? num_ports - 1 : start_port);
1001c6fd2807SJeff Garzik 
1002c6fd2807SJeff Garzik 	if (NULL != pdev) {
1003c6fd2807SJeff Garzik 		DPRINTK("PCI config space regs:\n");
1004c6fd2807SJeff Garzik 		mv_dump_pci_cfg(pdev, 0x68);
1005c6fd2807SJeff Garzik 	}
1006c6fd2807SJeff Garzik 	DPRINTK("PCI regs:\n");
1007c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xc00, 0x3c);
1008c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xd00, 0x34);
1009c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xf00, 0x4);
1010c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0x1d00, 0x6c);
1011c6fd2807SJeff Garzik 	for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
1012c6fd2807SJeff Garzik 		hc_base = mv_hc_base(mmio_base, hc);
1013c6fd2807SJeff Garzik 		DPRINTK("HC regs (HC %i):\n", hc);
1014c6fd2807SJeff Garzik 		mv_dump_mem(hc_base, 0x1c);
1015c6fd2807SJeff Garzik 	}
1016c6fd2807SJeff Garzik 	for (p = start_port; p < start_port + num_ports; p++) {
1017c6fd2807SJeff Garzik 		port_base = mv_port_base(mmio_base, p);
1018c6fd2807SJeff Garzik 		DPRINTK("EDMA regs (port %i):\n", p);
1019c6fd2807SJeff Garzik 		mv_dump_mem(port_base, 0x54);
1020c6fd2807SJeff Garzik 		DPRINTK("SATA regs (port %i):\n", p);
1021c6fd2807SJeff Garzik 		mv_dump_mem(port_base+0x300, 0x60);
1022c6fd2807SJeff Garzik 	}
1023c6fd2807SJeff Garzik #endif
1024c6fd2807SJeff Garzik }
1025c6fd2807SJeff Garzik 
1026c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1027c6fd2807SJeff Garzik {
1028c6fd2807SJeff Garzik 	unsigned int ofs;
1029c6fd2807SJeff Garzik 
1030c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1031c6fd2807SJeff Garzik 	case SCR_STATUS:
1032c6fd2807SJeff Garzik 	case SCR_CONTROL:
1033c6fd2807SJeff Garzik 	case SCR_ERROR:
1034c6fd2807SJeff Garzik 		ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
1035c6fd2807SJeff Garzik 		break;
1036c6fd2807SJeff Garzik 	case SCR_ACTIVE:
1037c6fd2807SJeff Garzik 		ofs = SATA_ACTIVE_OFS;   /* active is not with the others */
1038c6fd2807SJeff Garzik 		break;
1039c6fd2807SJeff Garzik 	default:
1040c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1041c6fd2807SJeff Garzik 		break;
1042c6fd2807SJeff Garzik 	}
1043c6fd2807SJeff Garzik 	return ofs;
1044c6fd2807SJeff Garzik }
1045c6fd2807SJeff Garzik 
1046da3dbb17STejun Heo static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
1047c6fd2807SJeff Garzik {
1048c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1049c6fd2807SJeff Garzik 
1050da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
1051da3dbb17STejun Heo 		*val = readl(mv_ap_base(ap) + ofs);
1052da3dbb17STejun Heo 		return 0;
1053da3dbb17STejun Heo 	} else
1054da3dbb17STejun Heo 		return -EINVAL;
1055c6fd2807SJeff Garzik }
1056c6fd2807SJeff Garzik 
1057da3dbb17STejun Heo static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
1058c6fd2807SJeff Garzik {
1059c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1060c6fd2807SJeff Garzik 
1061da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
1062c6fd2807SJeff Garzik 		writelfl(val, mv_ap_base(ap) + ofs);
1063da3dbb17STejun Heo 		return 0;
1064da3dbb17STejun Heo 	} else
1065da3dbb17STejun Heo 		return -EINVAL;
1066c6fd2807SJeff Garzik }
1067c6fd2807SJeff Garzik 
1068f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1069f273827eSMark Lord {
1070f273827eSMark Lord 	/*
1071e49856d8SMark Lord 	 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1072e49856d8SMark Lord 	 *
1073e49856d8SMark Lord 	 * Gen-II does not support NCQ over a port multiplier
1074e49856d8SMark Lord 	 *  (no FIS-based switching).
1075e49856d8SMark Lord 	 *
1076f273827eSMark Lord 	 * We don't have hob_nsect when doing NCQ commands on Gen-II.
1077f273827eSMark Lord 	 * See mv_qc_prep() for more info.
1078f273827eSMark Lord 	 */
1079e49856d8SMark Lord 	if (adev->flags & ATA_DFLAG_NCQ) {
1080352fab70SMark Lord 		if (sata_pmp_attached(adev->link->ap)) {
1081e49856d8SMark Lord 			adev->flags &= ~ATA_DFLAG_NCQ;
1082352fab70SMark Lord 			ata_dev_printk(adev, KERN_INFO,
1083352fab70SMark Lord 				"NCQ disabled for command-based switching\n");
1084352fab70SMark Lord 		} else if (adev->max_sectors > GEN_II_NCQ_MAX_SECTORS) {
1085352fab70SMark Lord 			adev->max_sectors = GEN_II_NCQ_MAX_SECTORS;
1086352fab70SMark Lord 			ata_dev_printk(adev, KERN_INFO,
1087352fab70SMark Lord 				"max_sectors limited to %u for NCQ\n",
1088352fab70SMark Lord 				adev->max_sectors);
1089352fab70SMark Lord 		}
1090f273827eSMark Lord 	}
1091e49856d8SMark Lord }
1092f273827eSMark Lord 
1093e49856d8SMark Lord static void mv_config_fbs(void __iomem *port_mmio, int enable_fbs)
1094e49856d8SMark Lord {
10958e7decdbSMark Lord 	u32 old_fiscfg, new_fiscfg, old_ltmode, new_ltmode;
1096e49856d8SMark Lord 	/*
1097e49856d8SMark Lord 	 * Various bit settings required for operation
1098e49856d8SMark Lord 	 * in FIS-based switching (fbs) mode on GenIIe:
1099e49856d8SMark Lord 	 */
11008e7decdbSMark Lord 	old_fiscfg = readl(port_mmio + FISCFG_OFS);
1101e49856d8SMark Lord 	old_ltmode = readl(port_mmio + LTMODE_OFS);
1102e49856d8SMark Lord 	if (enable_fbs) {
11038e7decdbSMark Lord 		new_fiscfg = old_fiscfg |  FISCFG_SINGLE_SYNC;
1104e49856d8SMark Lord 		new_ltmode = old_ltmode |  LTMODE_BIT8;
1105e49856d8SMark Lord 	} else { /* disable fbs */
11068e7decdbSMark Lord 		new_fiscfg = old_fiscfg & ~FISCFG_SINGLE_SYNC;
1107e49856d8SMark Lord 		new_ltmode = old_ltmode & ~LTMODE_BIT8;
1108e49856d8SMark Lord 	}
11098e7decdbSMark Lord 	if (new_fiscfg != old_fiscfg)
11108e7decdbSMark Lord 		writelfl(new_fiscfg, port_mmio + FISCFG_OFS);
1111e49856d8SMark Lord 	if (new_ltmode != old_ltmode)
1112e49856d8SMark Lord 		writelfl(new_ltmode, port_mmio + LTMODE_OFS);
1113e49856d8SMark Lord }
1114c6fd2807SJeff Garzik 
1115e12bef50SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq)
1116c6fd2807SJeff Garzik {
1117c6fd2807SJeff Garzik 	u32 cfg;
1118e12bef50SMark Lord 	struct mv_port_priv *pp    = ap->private_data;
1119e12bef50SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1120e12bef50SMark Lord 	void __iomem *port_mmio    = mv_ap_base(ap);
1121c6fd2807SJeff Garzik 
1122c6fd2807SJeff Garzik 	/* set up non-NCQ EDMA configuration */
1123c6fd2807SJeff Garzik 	cfg = EDMA_CFG_Q_DEPTH;		/* always 0x1f for *all* chips */
1124c6fd2807SJeff Garzik 
1125c6fd2807SJeff Garzik 	if (IS_GEN_I(hpriv))
1126c6fd2807SJeff Garzik 		cfg |= (1 << 8);	/* enab config burst size mask */
1127c6fd2807SJeff Garzik 
1128c6fd2807SJeff Garzik 	else if (IS_GEN_II(hpriv))
1129c6fd2807SJeff Garzik 		cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1130c6fd2807SJeff Garzik 
1131c6fd2807SJeff Garzik 	else if (IS_GEN_IIE(hpriv)) {
1132e728eabeSJeff Garzik 		cfg |= (1 << 23);	/* do not mask PM field in rx'd FIS */
1133e728eabeSJeff Garzik 		cfg |= (1 << 22);	/* enab 4-entry host queue cache */
1134616d4a98SMark Lord 		if (HAS_PCI(ap->host))
1135c6fd2807SJeff Garzik 			cfg |= (1 << 18);	/* enab early completion */
1136616d4a98SMark Lord 		if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1137616d4a98SMark Lord 			cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
1138e49856d8SMark Lord 
1139e49856d8SMark Lord 		if (want_ncq && sata_pmp_attached(ap)) {
1140e49856d8SMark Lord 			cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
1141e49856d8SMark Lord 			mv_config_fbs(port_mmio, 1);
1142e49856d8SMark Lord 		} else {
1143e49856d8SMark Lord 			mv_config_fbs(port_mmio, 0);
1144e49856d8SMark Lord 		}
1145c6fd2807SJeff Garzik 	}
1146c6fd2807SJeff Garzik 
114772109168SMark Lord 	if (want_ncq) {
114872109168SMark Lord 		cfg |= EDMA_CFG_NCQ;
114972109168SMark Lord 		pp->pp_flags |=  MV_PP_FLAG_NCQ_EN;
115072109168SMark Lord 	} else
115172109168SMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_NCQ_EN;
115272109168SMark Lord 
1153c6fd2807SJeff Garzik 	writelfl(cfg, port_mmio + EDMA_CFG_OFS);
1154c6fd2807SJeff Garzik }
1155c6fd2807SJeff Garzik 
1156da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1157da2fa9baSMark Lord {
1158da2fa9baSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1159da2fa9baSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1160eb73d558SMark Lord 	int tag;
1161da2fa9baSMark Lord 
1162da2fa9baSMark Lord 	if (pp->crqb) {
1163da2fa9baSMark Lord 		dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1164da2fa9baSMark Lord 		pp->crqb = NULL;
1165da2fa9baSMark Lord 	}
1166da2fa9baSMark Lord 	if (pp->crpb) {
1167da2fa9baSMark Lord 		dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1168da2fa9baSMark Lord 		pp->crpb = NULL;
1169da2fa9baSMark Lord 	}
1170eb73d558SMark Lord 	/*
1171eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1172eb73d558SMark Lord 	 * For later hardware, we have one unique sg_tbl per NCQ tag.
1173eb73d558SMark Lord 	 */
1174eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1175eb73d558SMark Lord 		if (pp->sg_tbl[tag]) {
1176eb73d558SMark Lord 			if (tag == 0 || !IS_GEN_I(hpriv))
1177eb73d558SMark Lord 				dma_pool_free(hpriv->sg_tbl_pool,
1178eb73d558SMark Lord 					      pp->sg_tbl[tag],
1179eb73d558SMark Lord 					      pp->sg_tbl_dma[tag]);
1180eb73d558SMark Lord 			pp->sg_tbl[tag] = NULL;
1181eb73d558SMark Lord 		}
1182da2fa9baSMark Lord 	}
1183da2fa9baSMark Lord }
1184da2fa9baSMark Lord 
1185c6fd2807SJeff Garzik /**
1186c6fd2807SJeff Garzik  *      mv_port_start - Port specific init/start routine.
1187c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1188c6fd2807SJeff Garzik  *
1189c6fd2807SJeff Garzik  *      Allocate and point to DMA memory, init port private memory,
1190c6fd2807SJeff Garzik  *      zero indices.
1191c6fd2807SJeff Garzik  *
1192c6fd2807SJeff Garzik  *      LOCKING:
1193c6fd2807SJeff Garzik  *      Inherited from caller.
1194c6fd2807SJeff Garzik  */
1195c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1196c6fd2807SJeff Garzik {
1197cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1198cca3974eSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1199c6fd2807SJeff Garzik 	struct mv_port_priv *pp;
1200dde20207SJames Bottomley 	int tag;
1201c6fd2807SJeff Garzik 
120224dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1203c6fd2807SJeff Garzik 	if (!pp)
120424dc5f33STejun Heo 		return -ENOMEM;
1205da2fa9baSMark Lord 	ap->private_data = pp;
1206c6fd2807SJeff Garzik 
1207da2fa9baSMark Lord 	pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1208da2fa9baSMark Lord 	if (!pp->crqb)
1209da2fa9baSMark Lord 		return -ENOMEM;
1210da2fa9baSMark Lord 	memset(pp->crqb, 0, MV_CRQB_Q_SZ);
1211c6fd2807SJeff Garzik 
1212da2fa9baSMark Lord 	pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1213da2fa9baSMark Lord 	if (!pp->crpb)
1214da2fa9baSMark Lord 		goto out_port_free_dma_mem;
1215da2fa9baSMark Lord 	memset(pp->crpb, 0, MV_CRPB_Q_SZ);
1216c6fd2807SJeff Garzik 
1217eb73d558SMark Lord 	/*
1218eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1219eb73d558SMark Lord 	 * For later hardware, we need one unique sg_tbl per NCQ tag.
1220eb73d558SMark Lord 	 */
1221eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1222eb73d558SMark Lord 		if (tag == 0 || !IS_GEN_I(hpriv)) {
1223eb73d558SMark Lord 			pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1224eb73d558SMark Lord 					      GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1225eb73d558SMark Lord 			if (!pp->sg_tbl[tag])
1226da2fa9baSMark Lord 				goto out_port_free_dma_mem;
1227eb73d558SMark Lord 		} else {
1228eb73d558SMark Lord 			pp->sg_tbl[tag]     = pp->sg_tbl[0];
1229eb73d558SMark Lord 			pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1230eb73d558SMark Lord 		}
1231eb73d558SMark Lord 	}
1232c6fd2807SJeff Garzik 	return 0;
1233da2fa9baSMark Lord 
1234da2fa9baSMark Lord out_port_free_dma_mem:
1235da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1236da2fa9baSMark Lord 	return -ENOMEM;
1237c6fd2807SJeff Garzik }
1238c6fd2807SJeff Garzik 
1239c6fd2807SJeff Garzik /**
1240c6fd2807SJeff Garzik  *      mv_port_stop - Port specific cleanup/stop routine.
1241c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1242c6fd2807SJeff Garzik  *
1243c6fd2807SJeff Garzik  *      Stop DMA, cleanup port memory.
1244c6fd2807SJeff Garzik  *
1245c6fd2807SJeff Garzik  *      LOCKING:
1246cca3974eSJeff Garzik  *      This routine uses the host lock to protect the DMA stop.
1247c6fd2807SJeff Garzik  */
1248c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1249c6fd2807SJeff Garzik {
1250e12bef50SMark Lord 	mv_stop_edma(ap);
1251da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1252c6fd2807SJeff Garzik }
1253c6fd2807SJeff Garzik 
1254c6fd2807SJeff Garzik /**
1255c6fd2807SJeff Garzik  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1256c6fd2807SJeff Garzik  *      @qc: queued command whose SG list to source from
1257c6fd2807SJeff Garzik  *
1258c6fd2807SJeff Garzik  *      Populate the SG list and mark the last entry.
1259c6fd2807SJeff Garzik  *
1260c6fd2807SJeff Garzik  *      LOCKING:
1261c6fd2807SJeff Garzik  *      Inherited from caller.
1262c6fd2807SJeff Garzik  */
12636c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1264c6fd2807SJeff Garzik {
1265c6fd2807SJeff Garzik 	struct mv_port_priv *pp = qc->ap->private_data;
1266c6fd2807SJeff Garzik 	struct scatterlist *sg;
12673be6cbd7SJeff Garzik 	struct mv_sg *mv_sg, *last_sg = NULL;
1268ff2aeb1eSTejun Heo 	unsigned int si;
1269c6fd2807SJeff Garzik 
1270eb73d558SMark Lord 	mv_sg = pp->sg_tbl[qc->tag];
1271ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1272d88184fbSJeff Garzik 		dma_addr_t addr = sg_dma_address(sg);
1273d88184fbSJeff Garzik 		u32 sg_len = sg_dma_len(sg);
1274c6fd2807SJeff Garzik 
12754007b493SOlof Johansson 		while (sg_len) {
12764007b493SOlof Johansson 			u32 offset = addr & 0xffff;
12774007b493SOlof Johansson 			u32 len = sg_len;
12784007b493SOlof Johansson 
12794007b493SOlof Johansson 			if ((offset + sg_len > 0x10000))
12804007b493SOlof Johansson 				len = 0x10000 - offset;
12814007b493SOlof Johansson 
1282d88184fbSJeff Garzik 			mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1283d88184fbSJeff Garzik 			mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
12846c08772eSJeff Garzik 			mv_sg->flags_size = cpu_to_le32(len & 0xffff);
1285c6fd2807SJeff Garzik 
12864007b493SOlof Johansson 			sg_len -= len;
12874007b493SOlof Johansson 			addr += len;
12884007b493SOlof Johansson 
12893be6cbd7SJeff Garzik 			last_sg = mv_sg;
1290d88184fbSJeff Garzik 			mv_sg++;
1291c6fd2807SJeff Garzik 		}
12924007b493SOlof Johansson 	}
12933be6cbd7SJeff Garzik 
12943be6cbd7SJeff Garzik 	if (likely(last_sg))
12953be6cbd7SJeff Garzik 		last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
1296c6fd2807SJeff Garzik }
1297c6fd2807SJeff Garzik 
12985796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1299c6fd2807SJeff Garzik {
1300c6fd2807SJeff Garzik 	u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1301c6fd2807SJeff Garzik 		(last ? CRQB_CMD_LAST : 0);
1302c6fd2807SJeff Garzik 	*cmdw = cpu_to_le16(tmp);
1303c6fd2807SJeff Garzik }
1304c6fd2807SJeff Garzik 
1305c6fd2807SJeff Garzik /**
1306c6fd2807SJeff Garzik  *      mv_qc_prep - Host specific command preparation.
1307c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1308c6fd2807SJeff Garzik  *
1309c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1310c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1311c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1312c6fd2807SJeff Garzik  *      the SG load routine.
1313c6fd2807SJeff Garzik  *
1314c6fd2807SJeff Garzik  *      LOCKING:
1315c6fd2807SJeff Garzik  *      Inherited from caller.
1316c6fd2807SJeff Garzik  */
1317c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc)
1318c6fd2807SJeff Garzik {
1319c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1320c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1321c6fd2807SJeff Garzik 	__le16 *cw;
1322c6fd2807SJeff Garzik 	struct ata_taskfile *tf;
1323c6fd2807SJeff Garzik 	u16 flags = 0;
1324c6fd2807SJeff Garzik 	unsigned in_index;
1325c6fd2807SJeff Garzik 
1326138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1327138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ))
1328c6fd2807SJeff Garzik 		return;
1329c6fd2807SJeff Garzik 
1330c6fd2807SJeff Garzik 	/* Fill in command request block
1331c6fd2807SJeff Garzik 	 */
1332c6fd2807SJeff Garzik 	if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1333c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
1334c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
1335c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
1336e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
1337c6fd2807SJeff Garzik 
1338bdd4dddeSJeff Garzik 	/* get current queue index from software */
1339fcfb1f77SMark Lord 	in_index = pp->req_idx;
1340c6fd2807SJeff Garzik 
1341c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr =
1342eb73d558SMark Lord 		cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1343c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr_hi =
1344eb73d558SMark Lord 		cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
1345c6fd2807SJeff Garzik 	pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
1346c6fd2807SJeff Garzik 
1347c6fd2807SJeff Garzik 	cw = &pp->crqb[in_index].ata_cmd[0];
1348c6fd2807SJeff Garzik 	tf = &qc->tf;
1349c6fd2807SJeff Garzik 
1350c6fd2807SJeff Garzik 	/* Sadly, the CRQB cannot accomodate all registers--there are
1351c6fd2807SJeff Garzik 	 * only 11 bytes...so we must pick and choose required
1352c6fd2807SJeff Garzik 	 * registers based on the command.  So, we drop feature and
1353c6fd2807SJeff Garzik 	 * hob_feature for [RW] DMA commands, but they are needed for
1354c6fd2807SJeff Garzik 	 * NCQ.  NCQ will drop hob_nsect.
1355c6fd2807SJeff Garzik 	 */
1356c6fd2807SJeff Garzik 	switch (tf->command) {
1357c6fd2807SJeff Garzik 	case ATA_CMD_READ:
1358c6fd2807SJeff Garzik 	case ATA_CMD_READ_EXT:
1359c6fd2807SJeff Garzik 	case ATA_CMD_WRITE:
1360c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_EXT:
1361c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_FUA_EXT:
1362c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1363c6fd2807SJeff Garzik 		break;
1364c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_READ:
1365c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_WRITE:
1366c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
1367c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1368c6fd2807SJeff Garzik 		break;
1369c6fd2807SJeff Garzik 	default:
1370c6fd2807SJeff Garzik 		/* The only other commands EDMA supports in non-queued and
1371c6fd2807SJeff Garzik 		 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1372c6fd2807SJeff Garzik 		 * of which are defined/used by Linux.  If we get here, this
1373c6fd2807SJeff Garzik 		 * driver needs work.
1374c6fd2807SJeff Garzik 		 *
1375c6fd2807SJeff Garzik 		 * FIXME: modify libata to give qc_prep a return value and
1376c6fd2807SJeff Garzik 		 * return error here.
1377c6fd2807SJeff Garzik 		 */
1378c6fd2807SJeff Garzik 		BUG_ON(tf->command);
1379c6fd2807SJeff Garzik 		break;
1380c6fd2807SJeff Garzik 	}
1381c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1382c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1383c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1384c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1385c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1386c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1387c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1388c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1389c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);	/* last */
1390c6fd2807SJeff Garzik 
1391c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1392c6fd2807SJeff Garzik 		return;
1393c6fd2807SJeff Garzik 	mv_fill_sg(qc);
1394c6fd2807SJeff Garzik }
1395c6fd2807SJeff Garzik 
1396c6fd2807SJeff Garzik /**
1397c6fd2807SJeff Garzik  *      mv_qc_prep_iie - Host specific command preparation.
1398c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1399c6fd2807SJeff Garzik  *
1400c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1401c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1402c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1403c6fd2807SJeff Garzik  *      the SG load routine.
1404c6fd2807SJeff Garzik  *
1405c6fd2807SJeff Garzik  *      LOCKING:
1406c6fd2807SJeff Garzik  *      Inherited from caller.
1407c6fd2807SJeff Garzik  */
1408c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
1409c6fd2807SJeff Garzik {
1410c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1411c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1412c6fd2807SJeff Garzik 	struct mv_crqb_iie *crqb;
1413c6fd2807SJeff Garzik 	struct ata_taskfile *tf;
1414c6fd2807SJeff Garzik 	unsigned in_index;
1415c6fd2807SJeff Garzik 	u32 flags = 0;
1416c6fd2807SJeff Garzik 
1417138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1418138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ))
1419c6fd2807SJeff Garzik 		return;
1420c6fd2807SJeff Garzik 
1421e12bef50SMark Lord 	/* Fill in Gen IIE command request block */
1422c6fd2807SJeff Garzik 	if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1423c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
1424c6fd2807SJeff Garzik 
1425c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
1426c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
14278c0aeb4aSMark Lord 	flags |= qc->tag << CRQB_HOSTQ_SHIFT;
1428e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
1429c6fd2807SJeff Garzik 
1430bdd4dddeSJeff Garzik 	/* get current queue index from software */
1431fcfb1f77SMark Lord 	in_index = pp->req_idx;
1432c6fd2807SJeff Garzik 
1433c6fd2807SJeff Garzik 	crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
1434eb73d558SMark Lord 	crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1435eb73d558SMark Lord 	crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
1436c6fd2807SJeff Garzik 	crqb->flags = cpu_to_le32(flags);
1437c6fd2807SJeff Garzik 
1438c6fd2807SJeff Garzik 	tf = &qc->tf;
1439c6fd2807SJeff Garzik 	crqb->ata_cmd[0] = cpu_to_le32(
1440c6fd2807SJeff Garzik 			(tf->command << 16) |
1441c6fd2807SJeff Garzik 			(tf->feature << 24)
1442c6fd2807SJeff Garzik 		);
1443c6fd2807SJeff Garzik 	crqb->ata_cmd[1] = cpu_to_le32(
1444c6fd2807SJeff Garzik 			(tf->lbal << 0) |
1445c6fd2807SJeff Garzik 			(tf->lbam << 8) |
1446c6fd2807SJeff Garzik 			(tf->lbah << 16) |
1447c6fd2807SJeff Garzik 			(tf->device << 24)
1448c6fd2807SJeff Garzik 		);
1449c6fd2807SJeff Garzik 	crqb->ata_cmd[2] = cpu_to_le32(
1450c6fd2807SJeff Garzik 			(tf->hob_lbal << 0) |
1451c6fd2807SJeff Garzik 			(tf->hob_lbam << 8) |
1452c6fd2807SJeff Garzik 			(tf->hob_lbah << 16) |
1453c6fd2807SJeff Garzik 			(tf->hob_feature << 24)
1454c6fd2807SJeff Garzik 		);
1455c6fd2807SJeff Garzik 	crqb->ata_cmd[3] = cpu_to_le32(
1456c6fd2807SJeff Garzik 			(tf->nsect << 0) |
1457c6fd2807SJeff Garzik 			(tf->hob_nsect << 8)
1458c6fd2807SJeff Garzik 		);
1459c6fd2807SJeff Garzik 
1460c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1461c6fd2807SJeff Garzik 		return;
1462c6fd2807SJeff Garzik 	mv_fill_sg(qc);
1463c6fd2807SJeff Garzik }
1464c6fd2807SJeff Garzik 
1465c6fd2807SJeff Garzik /**
1466c6fd2807SJeff Garzik  *      mv_qc_issue - Initiate a command to the host
1467c6fd2807SJeff Garzik  *      @qc: queued command to start
1468c6fd2807SJeff Garzik  *
1469c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1470c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it sanity checks our local
1471c6fd2807SJeff Garzik  *      caches of the request producer/consumer indices then enables
1472c6fd2807SJeff Garzik  *      DMA and bumps the request producer index.
1473c6fd2807SJeff Garzik  *
1474c6fd2807SJeff Garzik  *      LOCKING:
1475c6fd2807SJeff Garzik  *      Inherited from caller.
1476c6fd2807SJeff Garzik  */
1477c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
1478c6fd2807SJeff Garzik {
1479c5d3e45aSJeff Garzik 	struct ata_port *ap = qc->ap;
1480c5d3e45aSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1481c5d3e45aSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1482bdd4dddeSJeff Garzik 	u32 in_index;
1483c6fd2807SJeff Garzik 
1484138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1485138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ)) {
148617c5aab5SMark Lord 		/*
148717c5aab5SMark Lord 		 * We're about to send a non-EDMA capable command to the
1488c6fd2807SJeff Garzik 		 * port.  Turn off EDMA so there won't be problems accessing
1489c6fd2807SJeff Garzik 		 * shadow block, etc registers.
1490c6fd2807SJeff Garzik 		 */
1491b562468cSMark Lord 		mv_stop_edma(ap);
1492e49856d8SMark Lord 		mv_pmp_select(ap, qc->dev->link->pmp);
14939363c382STejun Heo 		return ata_sff_qc_issue(qc);
1494c6fd2807SJeff Garzik 	}
1495c6fd2807SJeff Garzik 
149672109168SMark Lord 	mv_start_dma(ap, port_mmio, pp, qc->tf.protocol);
1497bdd4dddeSJeff Garzik 
1498fcfb1f77SMark Lord 	pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
1499fcfb1f77SMark Lord 	in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
1500c6fd2807SJeff Garzik 
1501c6fd2807SJeff Garzik 	/* and write the request in pointer to kick the EDMA to life */
1502bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
1503bdd4dddeSJeff Garzik 		 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
1504c6fd2807SJeff Garzik 
1505c6fd2807SJeff Garzik 	return 0;
1506c6fd2807SJeff Garzik }
1507c6fd2807SJeff Garzik 
15088f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
15098f767f8aSMark Lord {
15108f767f8aSMark Lord 	struct mv_port_priv *pp = ap->private_data;
15118f767f8aSMark Lord 	struct ata_queued_cmd *qc;
15128f767f8aSMark Lord 
15138f767f8aSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
15148f767f8aSMark Lord 		return NULL;
15158f767f8aSMark Lord 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
15168f767f8aSMark Lord 	if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
15178f767f8aSMark Lord 		qc = NULL;
15188f767f8aSMark Lord 	return qc;
15198f767f8aSMark Lord }
15208f767f8aSMark Lord 
15218f767f8aSMark Lord static void mv_unexpected_intr(struct ata_port *ap)
15228f767f8aSMark Lord {
15238f767f8aSMark Lord 	struct mv_port_priv *pp = ap->private_data;
15248f767f8aSMark Lord 	struct ata_eh_info *ehi = &ap->link.eh_info;
15258f767f8aSMark Lord 	char *when = "";
15268f767f8aSMark Lord 
15278f767f8aSMark Lord 	/*
15288f767f8aSMark Lord 	 * We got a device interrupt from something that
15298f767f8aSMark Lord 	 * was supposed to be using EDMA or polling.
15308f767f8aSMark Lord 	 */
15318f767f8aSMark Lord 	ata_ehi_clear_desc(ehi);
15328f767f8aSMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
15338f767f8aSMark Lord 		when = " while EDMA enabled";
15348f767f8aSMark Lord 	} else {
15358f767f8aSMark Lord 		struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
15368f767f8aSMark Lord 		if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
15378f767f8aSMark Lord 			when = " while polling";
15388f767f8aSMark Lord 	}
15398f767f8aSMark Lord 	ata_ehi_push_desc(ehi, "unexpected device interrupt%s", when);
15408f767f8aSMark Lord 	ehi->err_mask |= AC_ERR_OTHER;
15418f767f8aSMark Lord 	ehi->action   |= ATA_EH_RESET;
15428f767f8aSMark Lord 	ata_port_freeze(ap);
15438f767f8aSMark Lord }
15448f767f8aSMark Lord 
1545c6fd2807SJeff Garzik /**
1546c6fd2807SJeff Garzik  *      mv_err_intr - Handle error interrupts on the port
1547c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
15488d07379dSMark Lord  *      @qc: affected command (non-NCQ), or NULL
1549c6fd2807SJeff Garzik  *
15508d07379dSMark Lord  *      Most cases require a full reset of the chip's state machine,
15518d07379dSMark Lord  *      which also performs a COMRESET.
15528d07379dSMark Lord  *      Also, if the port disabled DMA, update our cached copy to match.
1553c6fd2807SJeff Garzik  *
1554c6fd2807SJeff Garzik  *      LOCKING:
1555c6fd2807SJeff Garzik  *      Inherited from caller.
1556c6fd2807SJeff Garzik  */
1557bdd4dddeSJeff Garzik static void mv_err_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
1558c6fd2807SJeff Garzik {
1559c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1560bdd4dddeSJeff Garzik 	u32 edma_err_cause, eh_freeze_mask, serr = 0;
1561bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1562bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1563bdd4dddeSJeff Garzik 	unsigned int action = 0, err_mask = 0;
15649af5c9c9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
1565c6fd2807SJeff Garzik 
1566bdd4dddeSJeff Garzik 	ata_ehi_clear_desc(ehi);
1567c6fd2807SJeff Garzik 
15688d07379dSMark Lord 	/*
15698d07379dSMark Lord 	 * Read and clear the err_cause bits.  This won't actually
15708d07379dSMark Lord 	 * clear for some errors (eg. SError), but we will be doing
15718d07379dSMark Lord 	 * a hard reset in those cases regardless, which *will* clear it.
1572bdd4dddeSJeff Garzik 	 */
1573bdd4dddeSJeff Garzik 	edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
15748d07379dSMark Lord 	writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1575bdd4dddeSJeff Garzik 
1576352fab70SMark Lord 	ata_ehi_push_desc(ehi, "edma_err_cause=%08x", edma_err_cause);
1577bdd4dddeSJeff Garzik 
1578bdd4dddeSJeff Garzik 	/*
1579352fab70SMark Lord 	 * All generations share these EDMA error cause bits:
1580bdd4dddeSJeff Garzik 	 */
1581bdd4dddeSJeff Garzik 	if (edma_err_cause & EDMA_ERR_DEV)
1582bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_DEV;
1583bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
15846c1153e0SJeff Garzik 			EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
1585bdd4dddeSJeff Garzik 			EDMA_ERR_INTRL_PAR)) {
1586bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1587cf480626STejun Heo 		action |= ATA_EH_RESET;
1588b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "parity error");
1589bdd4dddeSJeff Garzik 	}
1590bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
1591bdd4dddeSJeff Garzik 		ata_ehi_hotplugged(ehi);
1592bdd4dddeSJeff Garzik 		ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
1593b64bbc39STejun Heo 			"dev disconnect" : "dev connect");
1594cf480626STejun Heo 		action |= ATA_EH_RESET;
1595bdd4dddeSJeff Garzik 	}
1596bdd4dddeSJeff Garzik 
1597352fab70SMark Lord 	/*
1598352fab70SMark Lord 	 * Gen-I has a different SELF_DIS bit,
1599352fab70SMark Lord 	 * different FREEZE bits, and no SERR bit:
1600352fab70SMark Lord 	 */
1601ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv)) {
1602bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE_5;
1603bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
1604c6fd2807SJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1605b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
1606c6fd2807SJeff Garzik 		}
1607bdd4dddeSJeff Garzik 	} else {
1608bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE;
1609bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
1610bdd4dddeSJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1611b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
1612bdd4dddeSJeff Garzik 		}
1613bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SERR) {
16148d07379dSMark Lord 			/*
16158d07379dSMark Lord 			 * Ensure that we read our own SCR, not a pmp link SCR:
16168d07379dSMark Lord 			 */
16178d07379dSMark Lord 			ap->ops->scr_read(ap, SCR_ERROR, &serr);
16188d07379dSMark Lord 			/*
16198d07379dSMark Lord 			 * Don't clear SError here; leave it for libata-eh:
16208d07379dSMark Lord 			 */
16218d07379dSMark Lord 			ata_ehi_push_desc(ehi, "SError=%08x", serr);
16228d07379dSMark Lord 			err_mask |= AC_ERR_ATA_BUS;
1623cf480626STejun Heo 			action |= ATA_EH_RESET;
1624bdd4dddeSJeff Garzik 		}
1625bdd4dddeSJeff Garzik 	}
1626c6fd2807SJeff Garzik 
1627bdd4dddeSJeff Garzik 	if (!err_mask) {
1628bdd4dddeSJeff Garzik 		err_mask = AC_ERR_OTHER;
1629cf480626STejun Heo 		action |= ATA_EH_RESET;
1630bdd4dddeSJeff Garzik 	}
1631bdd4dddeSJeff Garzik 
1632bdd4dddeSJeff Garzik 	ehi->serror |= serr;
1633bdd4dddeSJeff Garzik 	ehi->action |= action;
1634bdd4dddeSJeff Garzik 
1635bdd4dddeSJeff Garzik 	if (qc)
1636bdd4dddeSJeff Garzik 		qc->err_mask |= err_mask;
1637bdd4dddeSJeff Garzik 	else
1638bdd4dddeSJeff Garzik 		ehi->err_mask |= err_mask;
1639bdd4dddeSJeff Garzik 
1640bdd4dddeSJeff Garzik 	if (edma_err_cause & eh_freeze_mask)
1641bdd4dddeSJeff Garzik 		ata_port_freeze(ap);
1642bdd4dddeSJeff Garzik 	else
1643bdd4dddeSJeff Garzik 		ata_port_abort(ap);
1644bdd4dddeSJeff Garzik }
1645bdd4dddeSJeff Garzik 
1646fcfb1f77SMark Lord static void mv_process_crpb_response(struct ata_port *ap,
1647fcfb1f77SMark Lord 		struct mv_crpb *response, unsigned int tag, int ncq_enabled)
1648fcfb1f77SMark Lord {
1649fcfb1f77SMark Lord 	struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
1650fcfb1f77SMark Lord 
1651fcfb1f77SMark Lord 	if (qc) {
1652fcfb1f77SMark Lord 		u8 ata_status;
1653fcfb1f77SMark Lord 		u16 edma_status = le16_to_cpu(response->flags);
1654fcfb1f77SMark Lord 		/*
1655fcfb1f77SMark Lord 		 * edma_status from a response queue entry:
1656fcfb1f77SMark Lord 		 *   LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
1657fcfb1f77SMark Lord 		 *   MSB is saved ATA status from command completion.
1658fcfb1f77SMark Lord 		 */
1659fcfb1f77SMark Lord 		if (!ncq_enabled) {
1660fcfb1f77SMark Lord 			u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
1661fcfb1f77SMark Lord 			if (err_cause) {
1662fcfb1f77SMark Lord 				/*
1663fcfb1f77SMark Lord 				 * Error will be seen/handled by mv_err_intr().
1664fcfb1f77SMark Lord 				 * So do nothing at all here.
1665fcfb1f77SMark Lord 				 */
1666fcfb1f77SMark Lord 				return;
1667fcfb1f77SMark Lord 			}
1668fcfb1f77SMark Lord 		}
1669fcfb1f77SMark Lord 		ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
1670fcfb1f77SMark Lord 		qc->err_mask |= ac_err_mask(ata_status);
1671fcfb1f77SMark Lord 		ata_qc_complete(qc);
1672fcfb1f77SMark Lord 	} else {
1673fcfb1f77SMark Lord 		ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
1674fcfb1f77SMark Lord 				__func__, tag);
1675fcfb1f77SMark Lord 	}
1676fcfb1f77SMark Lord }
1677fcfb1f77SMark Lord 
1678fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
1679bdd4dddeSJeff Garzik {
1680bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1681bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1682fcfb1f77SMark Lord 	u32 in_index;
1683bdd4dddeSJeff Garzik 	bool work_done = false;
1684fcfb1f77SMark Lord 	int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
1685bdd4dddeSJeff Garzik 
1686fcfb1f77SMark Lord 	/* Get the hardware queue position index */
1687bdd4dddeSJeff Garzik 	in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
1688bdd4dddeSJeff Garzik 			>> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
1689bdd4dddeSJeff Garzik 
1690fcfb1f77SMark Lord 	/* Process new responses from since the last time we looked */
1691fcfb1f77SMark Lord 	while (in_index != pp->resp_idx) {
16926c1153e0SJeff Garzik 		unsigned int tag;
1693fcfb1f77SMark Lord 		struct mv_crpb *response = &pp->crpb[pp->resp_idx];
1694bdd4dddeSJeff Garzik 
1695fcfb1f77SMark Lord 		pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
1696bdd4dddeSJeff Garzik 
1697fcfb1f77SMark Lord 		if (IS_GEN_I(hpriv)) {
1698fcfb1f77SMark Lord 			/* 50xx: no NCQ, only one command active at a time */
16999af5c9c9STejun Heo 			tag = ap->link.active_tag;
1700fcfb1f77SMark Lord 		} else {
1701fcfb1f77SMark Lord 			/* Gen II/IIE: get command tag from CRPB entry */
1702fcfb1f77SMark Lord 			tag = le16_to_cpu(response->id) & 0x1f;
1703bdd4dddeSJeff Garzik 		}
1704fcfb1f77SMark Lord 		mv_process_crpb_response(ap, response, tag, ncq_enabled);
1705bdd4dddeSJeff Garzik 		work_done = true;
1706bdd4dddeSJeff Garzik 	}
1707bdd4dddeSJeff Garzik 
1708352fab70SMark Lord 	/* Update the software queue position index in hardware */
1709bdd4dddeSJeff Garzik 	if (work_done)
1710bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
1711fcfb1f77SMark Lord 			 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
1712bdd4dddeSJeff Garzik 			 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
1713c6fd2807SJeff Garzik }
1714c6fd2807SJeff Garzik 
1715c6fd2807SJeff Garzik /**
1716c6fd2807SJeff Garzik  *      mv_host_intr - Handle all interrupts on the given host controller
1717cca3974eSJeff Garzik  *      @host: host specific structure
17187368f919SMark Lord  *      @main_irq_cause: Main interrupt cause register for the chip.
1719c6fd2807SJeff Garzik  *
1720c6fd2807SJeff Garzik  *      LOCKING:
1721c6fd2807SJeff Garzik  *      Inherited from caller.
1722c6fd2807SJeff Garzik  */
17237368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
1724c6fd2807SJeff Garzik {
1725f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
1726a3718c1fSMark Lord 	void __iomem *mmio = hpriv->base, *hc_mmio = NULL;
1727a3718c1fSMark Lord 	u32 hc_irq_cause = 0;
1728a3718c1fSMark Lord 	unsigned int handled = 0, port;
1729c6fd2807SJeff Garzik 
1730a3718c1fSMark Lord 	for (port = 0; port < hpriv->n_ports; port++) {
1731cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[port];
17328f71efe2SYinghai Lu 		struct mv_port_priv *pp;
1733a3718c1fSMark Lord 		unsigned int shift, hardport, port_cause;
1734a3718c1fSMark Lord 		/*
1735a3718c1fSMark Lord 		 * When we move to the second hc, flag our cached
1736a3718c1fSMark Lord 		 * copies of hc_mmio (and hc_irq_cause) as invalid again.
1737a3718c1fSMark Lord 		 */
1738a3718c1fSMark Lord 		if (port == MV_PORTS_PER_HC)
1739a3718c1fSMark Lord 			hc_mmio = NULL;
1740a3718c1fSMark Lord 		/*
1741a3718c1fSMark Lord 		 * Do nothing if port is not interrupting or is disabled:
1742a3718c1fSMark Lord 		 */
1743a3718c1fSMark Lord 		MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
17447368f919SMark Lord 		port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
1745a3718c1fSMark Lord 		if (!port_cause || !ap || (ap->flags & ATA_FLAG_DISABLED))
1746c6fd2807SJeff Garzik 			continue;
1747a3718c1fSMark Lord 		/*
1748a3718c1fSMark Lord 		 * Each hc within the host has its own hc_irq_cause register.
1749a3718c1fSMark Lord 		 * We defer reading it until we know we need it, right now:
1750a3718c1fSMark Lord 		 *
1751a3718c1fSMark Lord 		 * FIXME later: we don't really need to read this register
1752a3718c1fSMark Lord 		 * (some logic changes required below if we go that way),
1753a3718c1fSMark Lord 		 * because it doesn't tell us anything new.  But we do need
1754a3718c1fSMark Lord 		 * to write to it, outside the top of this loop,
1755a3718c1fSMark Lord 		 * to reset the interrupt triggers for next time.
1756a3718c1fSMark Lord 		 */
1757a3718c1fSMark Lord 		if (!hc_mmio) {
1758a3718c1fSMark Lord 			hc_mmio = mv_hc_base_from_port(mmio, port);
1759a3718c1fSMark Lord 			hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
1760a3718c1fSMark Lord 			writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
1761a3718c1fSMark Lord 			handled = 1;
1762a3718c1fSMark Lord 		}
17638f767f8aSMark Lord 		/*
17648f767f8aSMark Lord 		 * Process completed CRPB response(s) before other events.
17658f767f8aSMark Lord 		 */
17668f767f8aSMark Lord 		pp = ap->private_data;
17678f767f8aSMark Lord 		if (hc_irq_cause & (DMA_IRQ << hardport)) {
17688f767f8aSMark Lord 			if (pp->pp_flags & MV_PP_FLAG_EDMA_EN)
17698f767f8aSMark Lord 				mv_process_crpb_entries(ap, pp);
17708f767f8aSMark Lord 		}
17718f767f8aSMark Lord 		/*
17728f767f8aSMark Lord 		 * Handle chip-reported errors, or continue on to handle PIO.
17738f767f8aSMark Lord 		 */
1774a3718c1fSMark Lord 		if (unlikely(port_cause & ERR_IRQ)) {
17758f767f8aSMark Lord 			mv_err_intr(ap, mv_get_active_qc(ap));
17768f767f8aSMark Lord 		} else if (hc_irq_cause & (DEV_IRQ << hardport)) {
17778f767f8aSMark Lord 			if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
17788f767f8aSMark Lord 				struct ata_queued_cmd *qc = mv_get_active_qc(ap);
17798f767f8aSMark Lord 				if (qc) {
17808f767f8aSMark Lord 					ata_sff_host_intr(ap, qc);
1781bdd4dddeSJeff Garzik 					continue;
1782c6fd2807SJeff Garzik 				}
17838f767f8aSMark Lord 			}
17848f767f8aSMark Lord 			mv_unexpected_intr(ap);
1785c6fd2807SJeff Garzik 		}
1786c6fd2807SJeff Garzik 	}
1787a3718c1fSMark Lord 	return handled;
1788c6fd2807SJeff Garzik }
1789c6fd2807SJeff Garzik 
1790a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
1791bdd4dddeSJeff Garzik {
179202a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1793bdd4dddeSJeff Garzik 	struct ata_port *ap;
1794bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
1795bdd4dddeSJeff Garzik 	struct ata_eh_info *ehi;
1796bdd4dddeSJeff Garzik 	unsigned int i, err_mask, printed = 0;
1797bdd4dddeSJeff Garzik 	u32 err_cause;
1798bdd4dddeSJeff Garzik 
179902a121daSMark Lord 	err_cause = readl(mmio + hpriv->irq_cause_ofs);
1800bdd4dddeSJeff Garzik 
1801bdd4dddeSJeff Garzik 	dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
1802bdd4dddeSJeff Garzik 		   err_cause);
1803bdd4dddeSJeff Garzik 
1804bdd4dddeSJeff Garzik 	DPRINTK("All regs @ PCI error\n");
1805bdd4dddeSJeff Garzik 	mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
1806bdd4dddeSJeff Garzik 
180702a121daSMark Lord 	writelfl(0, mmio + hpriv->irq_cause_ofs);
1808bdd4dddeSJeff Garzik 
1809bdd4dddeSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
1810bdd4dddeSJeff Garzik 		ap = host->ports[i];
1811936fd732STejun Heo 		if (!ata_link_offline(&ap->link)) {
18129af5c9c9STejun Heo 			ehi = &ap->link.eh_info;
1813bdd4dddeSJeff Garzik 			ata_ehi_clear_desc(ehi);
1814bdd4dddeSJeff Garzik 			if (!printed++)
1815bdd4dddeSJeff Garzik 				ata_ehi_push_desc(ehi,
1816bdd4dddeSJeff Garzik 					"PCI err cause 0x%08x", err_cause);
1817bdd4dddeSJeff Garzik 			err_mask = AC_ERR_HOST_BUS;
1818cf480626STejun Heo 			ehi->action = ATA_EH_RESET;
18199af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
1820bdd4dddeSJeff Garzik 			if (qc)
1821bdd4dddeSJeff Garzik 				qc->err_mask |= err_mask;
1822bdd4dddeSJeff Garzik 			else
1823bdd4dddeSJeff Garzik 				ehi->err_mask |= err_mask;
1824bdd4dddeSJeff Garzik 
1825bdd4dddeSJeff Garzik 			ata_port_freeze(ap);
1826bdd4dddeSJeff Garzik 		}
1827bdd4dddeSJeff Garzik 	}
1828a3718c1fSMark Lord 	return 1;	/* handled */
1829bdd4dddeSJeff Garzik }
1830bdd4dddeSJeff Garzik 
1831c6fd2807SJeff Garzik /**
1832c5d3e45aSJeff Garzik  *      mv_interrupt - Main interrupt event handler
1833c6fd2807SJeff Garzik  *      @irq: unused
1834c6fd2807SJeff Garzik  *      @dev_instance: private data; in this case the host structure
1835c6fd2807SJeff Garzik  *
1836c6fd2807SJeff Garzik  *      Read the read only register to determine if any host
1837c6fd2807SJeff Garzik  *      controllers have pending interrupts.  If so, call lower level
1838c6fd2807SJeff Garzik  *      routine to handle.  Also check for PCI errors which are only
1839c6fd2807SJeff Garzik  *      reported here.
1840c6fd2807SJeff Garzik  *
1841c6fd2807SJeff Garzik  *      LOCKING:
1842cca3974eSJeff Garzik  *      This routine holds the host lock while processing pending
1843c6fd2807SJeff Garzik  *      interrupts.
1844c6fd2807SJeff Garzik  */
18457d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
1846c6fd2807SJeff Garzik {
1847cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
1848f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
1849a3718c1fSMark Lord 	unsigned int handled = 0;
18507368f919SMark Lord 	u32 main_irq_cause, main_irq_mask;
1851c6fd2807SJeff Garzik 
1852646a4da5SMark Lord 	spin_lock(&host->lock);
18537368f919SMark Lord 	main_irq_cause = readl(hpriv->main_irq_cause_addr);
18547368f919SMark Lord 	main_irq_mask  = readl(hpriv->main_irq_mask_addr);
1855352fab70SMark Lord 	/*
1856352fab70SMark Lord 	 * Deal with cases where we either have nothing pending, or have read
1857352fab70SMark Lord 	 * a bogus register value which can indicate HW removal or PCI fault.
1858c6fd2807SJeff Garzik 	 */
18597368f919SMark Lord 	if ((main_irq_cause & main_irq_mask) && (main_irq_cause != 0xffffffffU)) {
18607368f919SMark Lord 		if (unlikely((main_irq_cause & PCI_ERR) && HAS_PCI(host)))
1861a3718c1fSMark Lord 			handled = mv_pci_error(host, hpriv->base);
1862a3718c1fSMark Lord 		else
18637368f919SMark Lord 			handled = mv_host_intr(host, main_irq_cause);
1864bdd4dddeSJeff Garzik 	}
1865cca3974eSJeff Garzik 	spin_unlock(&host->lock);
1866c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
1867c6fd2807SJeff Garzik }
1868c6fd2807SJeff Garzik 
1869c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
1870c6fd2807SJeff Garzik {
1871c6fd2807SJeff Garzik 	unsigned int ofs;
1872c6fd2807SJeff Garzik 
1873c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1874c6fd2807SJeff Garzik 	case SCR_STATUS:
1875c6fd2807SJeff Garzik 	case SCR_ERROR:
1876c6fd2807SJeff Garzik 	case SCR_CONTROL:
1877c6fd2807SJeff Garzik 		ofs = sc_reg_in * sizeof(u32);
1878c6fd2807SJeff Garzik 		break;
1879c6fd2807SJeff Garzik 	default:
1880c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1881c6fd2807SJeff Garzik 		break;
1882c6fd2807SJeff Garzik 	}
1883c6fd2807SJeff Garzik 	return ofs;
1884c6fd2807SJeff Garzik }
1885c6fd2807SJeff Garzik 
1886da3dbb17STejun Heo static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
1887c6fd2807SJeff Garzik {
1888f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
1889f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
18900d5ff566STejun Heo 	void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
1891c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
1892c6fd2807SJeff Garzik 
1893da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
1894da3dbb17STejun Heo 		*val = readl(addr + ofs);
1895da3dbb17STejun Heo 		return 0;
1896da3dbb17STejun Heo 	} else
1897da3dbb17STejun Heo 		return -EINVAL;
1898c6fd2807SJeff Garzik }
1899c6fd2807SJeff Garzik 
1900da3dbb17STejun Heo static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
1901c6fd2807SJeff Garzik {
1902f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
1903f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
19040d5ff566STejun Heo 	void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
1905c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
1906c6fd2807SJeff Garzik 
1907da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
19080d5ff566STejun Heo 		writelfl(val, addr + ofs);
1909da3dbb17STejun Heo 		return 0;
1910da3dbb17STejun Heo 	} else
1911da3dbb17STejun Heo 		return -EINVAL;
1912c6fd2807SJeff Garzik }
1913c6fd2807SJeff Garzik 
19147bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
1915c6fd2807SJeff Garzik {
19167bb3c529SSaeed Bishara 	struct pci_dev *pdev = to_pci_dev(host->dev);
1917c6fd2807SJeff Garzik 	int early_5080;
1918c6fd2807SJeff Garzik 
191944c10138SAuke Kok 	early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
1920c6fd2807SJeff Garzik 
1921c6fd2807SJeff Garzik 	if (!early_5080) {
1922c6fd2807SJeff Garzik 		u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1923c6fd2807SJeff Garzik 		tmp |= (1 << 0);
1924c6fd2807SJeff Garzik 		writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
1925c6fd2807SJeff Garzik 	}
1926c6fd2807SJeff Garzik 
19277bb3c529SSaeed Bishara 	mv_reset_pci_bus(host, mmio);
1928c6fd2807SJeff Garzik }
1929c6fd2807SJeff Garzik 
1930c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
1931c6fd2807SJeff Garzik {
19328e7decdbSMark Lord 	writel(0x0fcfffff, mmio + MV_FLASH_CTL_OFS);
1933c6fd2807SJeff Garzik }
1934c6fd2807SJeff Garzik 
1935c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
1936c6fd2807SJeff Garzik 			   void __iomem *mmio)
1937c6fd2807SJeff Garzik {
1938c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
1939c6fd2807SJeff Garzik 	u32 tmp;
1940c6fd2807SJeff Garzik 
1941c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
1942c6fd2807SJeff Garzik 
1943c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0x1800;	/* bits 12:11 */
1944c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0xe0;	/* bits 7:5 */
1945c6fd2807SJeff Garzik }
1946c6fd2807SJeff Garzik 
1947c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
1948c6fd2807SJeff Garzik {
1949c6fd2807SJeff Garzik 	u32 tmp;
1950c6fd2807SJeff Garzik 
19518e7decdbSMark Lord 	writel(0, mmio + MV_GPIO_PORT_CTL_OFS);
1952c6fd2807SJeff Garzik 
1953c6fd2807SJeff Garzik 	/* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
1954c6fd2807SJeff Garzik 
1955c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1956c6fd2807SJeff Garzik 	tmp |= ~(1 << 0);
1957c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
1958c6fd2807SJeff Garzik }
1959c6fd2807SJeff Garzik 
1960c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
1961c6fd2807SJeff Garzik 			   unsigned int port)
1962c6fd2807SJeff Garzik {
1963c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, port);
1964c6fd2807SJeff Garzik 	const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
1965c6fd2807SJeff Garzik 	u32 tmp;
1966c6fd2807SJeff Garzik 	int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
1967c6fd2807SJeff Garzik 
1968c6fd2807SJeff Garzik 	if (fix_apm_sq) {
19698e7decdbSMark Lord 		tmp = readl(phy_mmio + MV5_LTMODE_OFS);
1970c6fd2807SJeff Garzik 		tmp |= (1 << 19);
19718e7decdbSMark Lord 		writel(tmp, phy_mmio + MV5_LTMODE_OFS);
1972c6fd2807SJeff Garzik 
19738e7decdbSMark Lord 		tmp = readl(phy_mmio + MV5_PHY_CTL_OFS);
1974c6fd2807SJeff Garzik 		tmp &= ~0x3;
1975c6fd2807SJeff Garzik 		tmp |= 0x1;
19768e7decdbSMark Lord 		writel(tmp, phy_mmio + MV5_PHY_CTL_OFS);
1977c6fd2807SJeff Garzik 	}
1978c6fd2807SJeff Garzik 
1979c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
1980c6fd2807SJeff Garzik 	tmp &= ~mask;
1981c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].pre;
1982c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].amps;
1983c6fd2807SJeff Garzik 	writel(tmp, phy_mmio + MV5_PHY_MODE);
1984c6fd2807SJeff Garzik }
1985c6fd2807SJeff Garzik 
1986c6fd2807SJeff Garzik 
1987c6fd2807SJeff Garzik #undef ZERO
1988c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
1989c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
1990c6fd2807SJeff Garzik 			     unsigned int port)
1991c6fd2807SJeff Garzik {
1992c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
1993c6fd2807SJeff Garzik 
1994e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
1995c6fd2807SJeff Garzik 
1996c6fd2807SJeff Garzik 	ZERO(0x028);	/* command */
1997c6fd2807SJeff Garzik 	writel(0x11f, port_mmio + EDMA_CFG_OFS);
1998c6fd2807SJeff Garzik 	ZERO(0x004);	/* timer */
1999c6fd2807SJeff Garzik 	ZERO(0x008);	/* irq err cause */
2000c6fd2807SJeff Garzik 	ZERO(0x00c);	/* irq err mask */
2001c6fd2807SJeff Garzik 	ZERO(0x010);	/* rq bah */
2002c6fd2807SJeff Garzik 	ZERO(0x014);	/* rq inp */
2003c6fd2807SJeff Garzik 	ZERO(0x018);	/* rq outp */
2004c6fd2807SJeff Garzik 	ZERO(0x01c);	/* respq bah */
2005c6fd2807SJeff Garzik 	ZERO(0x024);	/* respq outp */
2006c6fd2807SJeff Garzik 	ZERO(0x020);	/* respq inp */
2007c6fd2807SJeff Garzik 	ZERO(0x02c);	/* test control */
20088e7decdbSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
2009c6fd2807SJeff Garzik }
2010c6fd2807SJeff Garzik #undef ZERO
2011c6fd2807SJeff Garzik 
2012c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
2013c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2014c6fd2807SJeff Garzik 			unsigned int hc)
2015c6fd2807SJeff Garzik {
2016c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2017c6fd2807SJeff Garzik 	u32 tmp;
2018c6fd2807SJeff Garzik 
2019c6fd2807SJeff Garzik 	ZERO(0x00c);
2020c6fd2807SJeff Garzik 	ZERO(0x010);
2021c6fd2807SJeff Garzik 	ZERO(0x014);
2022c6fd2807SJeff Garzik 	ZERO(0x018);
2023c6fd2807SJeff Garzik 
2024c6fd2807SJeff Garzik 	tmp = readl(hc_mmio + 0x20);
2025c6fd2807SJeff Garzik 	tmp &= 0x1c1c1c1c;
2026c6fd2807SJeff Garzik 	tmp |= 0x03030303;
2027c6fd2807SJeff Garzik 	writel(tmp, hc_mmio + 0x20);
2028c6fd2807SJeff Garzik }
2029c6fd2807SJeff Garzik #undef ZERO
2030c6fd2807SJeff Garzik 
2031c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2032c6fd2807SJeff Garzik 			unsigned int n_hc)
2033c6fd2807SJeff Garzik {
2034c6fd2807SJeff Garzik 	unsigned int hc, port;
2035c6fd2807SJeff Garzik 
2036c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
2037c6fd2807SJeff Garzik 		for (port = 0; port < MV_PORTS_PER_HC; port++)
2038c6fd2807SJeff Garzik 			mv5_reset_hc_port(hpriv, mmio,
2039c6fd2807SJeff Garzik 					  (hc * MV_PORTS_PER_HC) + port);
2040c6fd2807SJeff Garzik 
2041c6fd2807SJeff Garzik 		mv5_reset_one_hc(hpriv, mmio, hc);
2042c6fd2807SJeff Garzik 	}
2043c6fd2807SJeff Garzik 
2044c6fd2807SJeff Garzik 	return 0;
2045c6fd2807SJeff Garzik }
2046c6fd2807SJeff Garzik 
2047c6fd2807SJeff Garzik #undef ZERO
2048c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
20497bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
2050c6fd2807SJeff Garzik {
205102a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2052c6fd2807SJeff Garzik 	u32 tmp;
2053c6fd2807SJeff Garzik 
20548e7decdbSMark Lord 	tmp = readl(mmio + MV_PCI_MODE_OFS);
2055c6fd2807SJeff Garzik 	tmp &= 0xff00ffff;
20568e7decdbSMark Lord 	writel(tmp, mmio + MV_PCI_MODE_OFS);
2057c6fd2807SJeff Garzik 
2058c6fd2807SJeff Garzik 	ZERO(MV_PCI_DISC_TIMER);
2059c6fd2807SJeff Garzik 	ZERO(MV_PCI_MSI_TRIGGER);
20608e7decdbSMark Lord 	writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT_OFS);
20617368f919SMark Lord 	ZERO(PCI_HC_MAIN_IRQ_MASK_OFS);
2062c6fd2807SJeff Garzik 	ZERO(MV_PCI_SERR_MASK);
206302a121daSMark Lord 	ZERO(hpriv->irq_cause_ofs);
206402a121daSMark Lord 	ZERO(hpriv->irq_mask_ofs);
2065c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_LOW_ADDRESS);
2066c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_HIGH_ADDRESS);
2067c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_ATTRIBUTE);
2068c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_COMMAND);
2069c6fd2807SJeff Garzik }
2070c6fd2807SJeff Garzik #undef ZERO
2071c6fd2807SJeff Garzik 
2072c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2073c6fd2807SJeff Garzik {
2074c6fd2807SJeff Garzik 	u32 tmp;
2075c6fd2807SJeff Garzik 
2076c6fd2807SJeff Garzik 	mv5_reset_flash(hpriv, mmio);
2077c6fd2807SJeff Garzik 
20788e7decdbSMark Lord 	tmp = readl(mmio + MV_GPIO_PORT_CTL_OFS);
2079c6fd2807SJeff Garzik 	tmp &= 0x3;
2080c6fd2807SJeff Garzik 	tmp |= (1 << 5) | (1 << 6);
20818e7decdbSMark Lord 	writel(tmp, mmio + MV_GPIO_PORT_CTL_OFS);
2082c6fd2807SJeff Garzik }
2083c6fd2807SJeff Garzik 
2084c6fd2807SJeff Garzik /**
2085c6fd2807SJeff Garzik  *      mv6_reset_hc - Perform the 6xxx global soft reset
2086c6fd2807SJeff Garzik  *      @mmio: base address of the HBA
2087c6fd2807SJeff Garzik  *
2088c6fd2807SJeff Garzik  *      This routine only applies to 6xxx parts.
2089c6fd2807SJeff Garzik  *
2090c6fd2807SJeff Garzik  *      LOCKING:
2091c6fd2807SJeff Garzik  *      Inherited from caller.
2092c6fd2807SJeff Garzik  */
2093c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2094c6fd2807SJeff Garzik 			unsigned int n_hc)
2095c6fd2807SJeff Garzik {
2096c6fd2807SJeff Garzik 	void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
2097c6fd2807SJeff Garzik 	int i, rc = 0;
2098c6fd2807SJeff Garzik 	u32 t;
2099c6fd2807SJeff Garzik 
2100c6fd2807SJeff Garzik 	/* Following procedure defined in PCI "main command and status
2101c6fd2807SJeff Garzik 	 * register" table.
2102c6fd2807SJeff Garzik 	 */
2103c6fd2807SJeff Garzik 	t = readl(reg);
2104c6fd2807SJeff Garzik 	writel(t | STOP_PCI_MASTER, reg);
2105c6fd2807SJeff Garzik 
2106c6fd2807SJeff Garzik 	for (i = 0; i < 1000; i++) {
2107c6fd2807SJeff Garzik 		udelay(1);
2108c6fd2807SJeff Garzik 		t = readl(reg);
21092dcb407eSJeff Garzik 		if (PCI_MASTER_EMPTY & t)
2110c6fd2807SJeff Garzik 			break;
2111c6fd2807SJeff Garzik 	}
2112c6fd2807SJeff Garzik 	if (!(PCI_MASTER_EMPTY & t)) {
2113c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
2114c6fd2807SJeff Garzik 		rc = 1;
2115c6fd2807SJeff Garzik 		goto done;
2116c6fd2807SJeff Garzik 	}
2117c6fd2807SJeff Garzik 
2118c6fd2807SJeff Garzik 	/* set reset */
2119c6fd2807SJeff Garzik 	i = 5;
2120c6fd2807SJeff Garzik 	do {
2121c6fd2807SJeff Garzik 		writel(t | GLOB_SFT_RST, reg);
2122c6fd2807SJeff Garzik 		t = readl(reg);
2123c6fd2807SJeff Garzik 		udelay(1);
2124c6fd2807SJeff Garzik 	} while (!(GLOB_SFT_RST & t) && (i-- > 0));
2125c6fd2807SJeff Garzik 
2126c6fd2807SJeff Garzik 	if (!(GLOB_SFT_RST & t)) {
2127c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't set global reset\n");
2128c6fd2807SJeff Garzik 		rc = 1;
2129c6fd2807SJeff Garzik 		goto done;
2130c6fd2807SJeff Garzik 	}
2131c6fd2807SJeff Garzik 
2132c6fd2807SJeff Garzik 	/* clear reset and *reenable the PCI master* (not mentioned in spec) */
2133c6fd2807SJeff Garzik 	i = 5;
2134c6fd2807SJeff Garzik 	do {
2135c6fd2807SJeff Garzik 		writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
2136c6fd2807SJeff Garzik 		t = readl(reg);
2137c6fd2807SJeff Garzik 		udelay(1);
2138c6fd2807SJeff Garzik 	} while ((GLOB_SFT_RST & t) && (i-- > 0));
2139c6fd2807SJeff Garzik 
2140c6fd2807SJeff Garzik 	if (GLOB_SFT_RST & t) {
2141c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
2142c6fd2807SJeff Garzik 		rc = 1;
2143c6fd2807SJeff Garzik 	}
2144c6fd2807SJeff Garzik done:
2145c6fd2807SJeff Garzik 	return rc;
2146c6fd2807SJeff Garzik }
2147c6fd2807SJeff Garzik 
2148c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
2149c6fd2807SJeff Garzik 			   void __iomem *mmio)
2150c6fd2807SJeff Garzik {
2151c6fd2807SJeff Garzik 	void __iomem *port_mmio;
2152c6fd2807SJeff Garzik 	u32 tmp;
2153c6fd2807SJeff Garzik 
21548e7decdbSMark Lord 	tmp = readl(mmio + MV_RESET_CFG_OFS);
2155c6fd2807SJeff Garzik 	if ((tmp & (1 << 0)) == 0) {
2156c6fd2807SJeff Garzik 		hpriv->signal[idx].amps = 0x7 << 8;
2157c6fd2807SJeff Garzik 		hpriv->signal[idx].pre = 0x1 << 5;
2158c6fd2807SJeff Garzik 		return;
2159c6fd2807SJeff Garzik 	}
2160c6fd2807SJeff Garzik 
2161c6fd2807SJeff Garzik 	port_mmio = mv_port_base(mmio, idx);
2162c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE2);
2163c6fd2807SJeff Garzik 
2164c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
2165c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
2166c6fd2807SJeff Garzik }
2167c6fd2807SJeff Garzik 
2168c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
2169c6fd2807SJeff Garzik {
21708e7decdbSMark Lord 	writel(0x00000060, mmio + MV_GPIO_PORT_CTL_OFS);
2171c6fd2807SJeff Garzik }
2172c6fd2807SJeff Garzik 
2173c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2174c6fd2807SJeff Garzik 			   unsigned int port)
2175c6fd2807SJeff Garzik {
2176c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
2177c6fd2807SJeff Garzik 
2178c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
2179c6fd2807SJeff Garzik 	int fix_phy_mode2 =
2180c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
2181c6fd2807SJeff Garzik 	int fix_phy_mode4 =
2182c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
2183c6fd2807SJeff Garzik 	u32 m2, tmp;
2184c6fd2807SJeff Garzik 
2185c6fd2807SJeff Garzik 	if (fix_phy_mode2) {
2186c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
2187c6fd2807SJeff Garzik 		m2 &= ~(1 << 16);
2188c6fd2807SJeff Garzik 		m2 |= (1 << 31);
2189c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
2190c6fd2807SJeff Garzik 
2191c6fd2807SJeff Garzik 		udelay(200);
2192c6fd2807SJeff Garzik 
2193c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
2194c6fd2807SJeff Garzik 		m2 &= ~((1 << 16) | (1 << 31));
2195c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
2196c6fd2807SJeff Garzik 
2197c6fd2807SJeff Garzik 		udelay(200);
2198c6fd2807SJeff Garzik 	}
2199c6fd2807SJeff Garzik 
2200c6fd2807SJeff Garzik 	/* who knows what this magic does */
2201c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE3);
2202c6fd2807SJeff Garzik 	tmp &= ~0x7F800000;
2203c6fd2807SJeff Garzik 	tmp |= 0x2A800000;
2204c6fd2807SJeff Garzik 	writel(tmp, port_mmio + PHY_MODE3);
2205c6fd2807SJeff Garzik 
2206c6fd2807SJeff Garzik 	if (fix_phy_mode4) {
2207c6fd2807SJeff Garzik 		u32 m4;
2208c6fd2807SJeff Garzik 
2209c6fd2807SJeff Garzik 		m4 = readl(port_mmio + PHY_MODE4);
2210c6fd2807SJeff Garzik 
2211c6fd2807SJeff Garzik 		if (hp_flags & MV_HP_ERRATA_60X1B2)
2212e12bef50SMark Lord 			tmp = readl(port_mmio + PHY_MODE3);
2213c6fd2807SJeff Garzik 
2214e12bef50SMark Lord 		/* workaround for errata FEr SATA#10 (part 1) */
2215c6fd2807SJeff Garzik 		m4 = (m4 & ~(1 << 1)) | (1 << 0);
2216c6fd2807SJeff Garzik 
2217c6fd2807SJeff Garzik 		writel(m4, port_mmio + PHY_MODE4);
2218c6fd2807SJeff Garzik 
2219c6fd2807SJeff Garzik 		if (hp_flags & MV_HP_ERRATA_60X1B2)
2220e12bef50SMark Lord 			writel(tmp, port_mmio + PHY_MODE3);
2221c6fd2807SJeff Garzik 	}
2222c6fd2807SJeff Garzik 
2223c6fd2807SJeff Garzik 	/* Revert values of pre-emphasis and signal amps to the saved ones */
2224c6fd2807SJeff Garzik 	m2 = readl(port_mmio + PHY_MODE2);
2225c6fd2807SJeff Garzik 
2226c6fd2807SJeff Garzik 	m2 &= ~MV_M2_PREAMP_MASK;
2227c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].amps;
2228c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].pre;
2229c6fd2807SJeff Garzik 	m2 &= ~(1 << 16);
2230c6fd2807SJeff Garzik 
2231c6fd2807SJeff Garzik 	/* according to mvSata 3.6.1, some IIE values are fixed */
2232c6fd2807SJeff Garzik 	if (IS_GEN_IIE(hpriv)) {
2233c6fd2807SJeff Garzik 		m2 &= ~0xC30FF01F;
2234c6fd2807SJeff Garzik 		m2 |= 0x0000900F;
2235c6fd2807SJeff Garzik 	}
2236c6fd2807SJeff Garzik 
2237c6fd2807SJeff Garzik 	writel(m2, port_mmio + PHY_MODE2);
2238c6fd2807SJeff Garzik }
2239c6fd2807SJeff Garzik 
2240f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
2241f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
2242f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
2243f351b2d6SSaeed Bishara 				      void __iomem *mmio)
2244f351b2d6SSaeed Bishara {
2245f351b2d6SSaeed Bishara 	return;
2246f351b2d6SSaeed Bishara }
2247f351b2d6SSaeed Bishara 
2248f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
2249f351b2d6SSaeed Bishara 			   void __iomem *mmio)
2250f351b2d6SSaeed Bishara {
2251f351b2d6SSaeed Bishara 	void __iomem *port_mmio;
2252f351b2d6SSaeed Bishara 	u32 tmp;
2253f351b2d6SSaeed Bishara 
2254f351b2d6SSaeed Bishara 	port_mmio = mv_port_base(mmio, idx);
2255f351b2d6SSaeed Bishara 	tmp = readl(port_mmio + PHY_MODE2);
2256f351b2d6SSaeed Bishara 
2257f351b2d6SSaeed Bishara 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
2258f351b2d6SSaeed Bishara 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
2259f351b2d6SSaeed Bishara }
2260f351b2d6SSaeed Bishara 
2261f351b2d6SSaeed Bishara #undef ZERO
2262f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
2263f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
2264f351b2d6SSaeed Bishara 					void __iomem *mmio, unsigned int port)
2265f351b2d6SSaeed Bishara {
2266f351b2d6SSaeed Bishara 	void __iomem *port_mmio = mv_port_base(mmio, port);
2267f351b2d6SSaeed Bishara 
2268e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
2269f351b2d6SSaeed Bishara 
2270f351b2d6SSaeed Bishara 	ZERO(0x028);		/* command */
2271f351b2d6SSaeed Bishara 	writel(0x101f, port_mmio + EDMA_CFG_OFS);
2272f351b2d6SSaeed Bishara 	ZERO(0x004);		/* timer */
2273f351b2d6SSaeed Bishara 	ZERO(0x008);		/* irq err cause */
2274f351b2d6SSaeed Bishara 	ZERO(0x00c);		/* irq err mask */
2275f351b2d6SSaeed Bishara 	ZERO(0x010);		/* rq bah */
2276f351b2d6SSaeed Bishara 	ZERO(0x014);		/* rq inp */
2277f351b2d6SSaeed Bishara 	ZERO(0x018);		/* rq outp */
2278f351b2d6SSaeed Bishara 	ZERO(0x01c);		/* respq bah */
2279f351b2d6SSaeed Bishara 	ZERO(0x024);		/* respq outp */
2280f351b2d6SSaeed Bishara 	ZERO(0x020);		/* respq inp */
2281f351b2d6SSaeed Bishara 	ZERO(0x02c);		/* test control */
22828e7decdbSMark Lord 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
2283f351b2d6SSaeed Bishara }
2284f351b2d6SSaeed Bishara 
2285f351b2d6SSaeed Bishara #undef ZERO
2286f351b2d6SSaeed Bishara 
2287f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
2288f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
2289f351b2d6SSaeed Bishara 				       void __iomem *mmio)
2290f351b2d6SSaeed Bishara {
2291f351b2d6SSaeed Bishara 	void __iomem *hc_mmio = mv_hc_base(mmio, 0);
2292f351b2d6SSaeed Bishara 
2293f351b2d6SSaeed Bishara 	ZERO(0x00c);
2294f351b2d6SSaeed Bishara 	ZERO(0x010);
2295f351b2d6SSaeed Bishara 	ZERO(0x014);
2296f351b2d6SSaeed Bishara 
2297f351b2d6SSaeed Bishara }
2298f351b2d6SSaeed Bishara 
2299f351b2d6SSaeed Bishara #undef ZERO
2300f351b2d6SSaeed Bishara 
2301f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
2302f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc)
2303f351b2d6SSaeed Bishara {
2304f351b2d6SSaeed Bishara 	unsigned int port;
2305f351b2d6SSaeed Bishara 
2306f351b2d6SSaeed Bishara 	for (port = 0; port < hpriv->n_ports; port++)
2307f351b2d6SSaeed Bishara 		mv_soc_reset_hc_port(hpriv, mmio, port);
2308f351b2d6SSaeed Bishara 
2309f351b2d6SSaeed Bishara 	mv_soc_reset_one_hc(hpriv, mmio);
2310f351b2d6SSaeed Bishara 
2311f351b2d6SSaeed Bishara 	return 0;
2312f351b2d6SSaeed Bishara }
2313f351b2d6SSaeed Bishara 
2314f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
2315f351b2d6SSaeed Bishara 				      void __iomem *mmio)
2316f351b2d6SSaeed Bishara {
2317f351b2d6SSaeed Bishara 	return;
2318f351b2d6SSaeed Bishara }
2319f351b2d6SSaeed Bishara 
2320f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
2321f351b2d6SSaeed Bishara {
2322f351b2d6SSaeed Bishara 	return;
2323f351b2d6SSaeed Bishara }
2324f351b2d6SSaeed Bishara 
23258e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
2326b67a1064SMark Lord {
23278e7decdbSMark Lord 	u32 ifcfg = readl(port_mmio + SATA_INTERFACE_CFG_OFS);
2328b67a1064SMark Lord 
23298e7decdbSMark Lord 	ifcfg = (ifcfg & 0xf7f) | 0x9b1000;	/* from chip spec */
2330b67a1064SMark Lord 	if (want_gen2i)
23318e7decdbSMark Lord 		ifcfg |= (1 << 7);		/* enable gen2i speed */
23328e7decdbSMark Lord 	writelfl(ifcfg, port_mmio + SATA_INTERFACE_CFG_OFS);
2333b67a1064SMark Lord }
2334b67a1064SMark Lord 
2335e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
2336c6fd2807SJeff Garzik 			     unsigned int port_no)
2337c6fd2807SJeff Garzik {
2338c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port_no);
2339c6fd2807SJeff Garzik 
23408e7decdbSMark Lord 	/*
23418e7decdbSMark Lord 	 * The datasheet warns against setting EDMA_RESET when EDMA is active
23428e7decdbSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
23438e7decdbSMark Lord 	 * to disable the EDMA engine before doing the EDMA_RESET operation.
23448e7decdbSMark Lord 	 */
23450d8be5cbSMark Lord 	mv_stop_edma_engine(port_mmio);
23468e7decdbSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
2347c6fd2807SJeff Garzik 
2348b67a1064SMark Lord 	if (!IS_GEN_I(hpriv)) {
23498e7decdbSMark Lord 		/* Enable 3.0gb/s link speed: this survives EDMA_RESET */
23508e7decdbSMark Lord 		mv_setup_ifcfg(port_mmio, 1);
2351c6fd2807SJeff Garzik 	}
2352b67a1064SMark Lord 	/*
23538e7decdbSMark Lord 	 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
2354b67a1064SMark Lord 	 * link, and physical layers.  It resets all SATA interface registers
2355b67a1064SMark Lord 	 * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
2356c6fd2807SJeff Garzik 	 */
23578e7decdbSMark Lord 	writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
2358b67a1064SMark Lord 	udelay(25);	/* allow reset propagation */
2359c6fd2807SJeff Garzik 	writelfl(0, port_mmio + EDMA_CMD_OFS);
2360c6fd2807SJeff Garzik 
2361c6fd2807SJeff Garzik 	hpriv->ops->phy_errata(hpriv, mmio, port_no);
2362c6fd2807SJeff Garzik 
2363ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv))
2364c6fd2807SJeff Garzik 		mdelay(1);
2365c6fd2807SJeff Garzik }
2366c6fd2807SJeff Garzik 
2367e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
2368e49856d8SMark Lord {
2369e49856d8SMark Lord 	if (sata_pmp_supported(ap)) {
2370e49856d8SMark Lord 		void __iomem *port_mmio = mv_ap_base(ap);
2371e49856d8SMark Lord 		u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
2372e49856d8SMark Lord 		int old = reg & 0xf;
2373e49856d8SMark Lord 
2374e49856d8SMark Lord 		if (old != pmp) {
2375e49856d8SMark Lord 			reg = (reg & ~0xf) | pmp;
2376e49856d8SMark Lord 			writelfl(reg, port_mmio + SATA_IFCTL_OFS);
2377e49856d8SMark Lord 		}
2378e49856d8SMark Lord 	}
2379e49856d8SMark Lord }
2380e49856d8SMark Lord 
2381e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
2382bdd4dddeSJeff Garzik 				unsigned long deadline)
2383c6fd2807SJeff Garzik {
2384e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
2385e49856d8SMark Lord 	return sata_std_hardreset(link, class, deadline);
2386e49856d8SMark Lord }
2387c6fd2807SJeff Garzik 
2388e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
2389e49856d8SMark Lord 				unsigned long deadline)
2390da3dbb17STejun Heo {
2391e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
2392e49856d8SMark Lord 	return ata_sff_softreset(link, class, deadline);
2393bdd4dddeSJeff Garzik }
2394bdd4dddeSJeff Garzik 
2395cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
2396bdd4dddeSJeff Garzik 			unsigned long deadline)
2397bdd4dddeSJeff Garzik {
2398cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
2399bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2400b562468cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
2401f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
24020d8be5cbSMark Lord 	int rc, attempts = 0, extra = 0;
24030d8be5cbSMark Lord 	u32 sstatus;
24040d8be5cbSMark Lord 	bool online;
2405bdd4dddeSJeff Garzik 
2406e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, ap->port_no);
2407b562468cSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2408bdd4dddeSJeff Garzik 
24090d8be5cbSMark Lord 	/* Workaround for errata FEr SATA#10 (part 2) */
24100d8be5cbSMark Lord 	do {
241117c5aab5SMark Lord 		const unsigned long *timing =
241217c5aab5SMark Lord 				sata_ehc_deb_timing(&link->eh_context);
2413bdd4dddeSJeff Garzik 
241417c5aab5SMark Lord 		rc = sata_link_hardreset(link, timing, deadline + extra,
241517c5aab5SMark Lord 					 &online, NULL);
241617c5aab5SMark Lord 		if (rc)
24170d8be5cbSMark Lord 			return rc;
24180d8be5cbSMark Lord 		sata_scr_read(link, SCR_STATUS, &sstatus);
24190d8be5cbSMark Lord 		if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
24200d8be5cbSMark Lord 			/* Force 1.5gb/s link speed and try again */
24218e7decdbSMark Lord 			mv_setup_ifcfg(mv_ap_base(ap), 0);
24220d8be5cbSMark Lord 			if (time_after(jiffies + HZ, deadline))
24230d8be5cbSMark Lord 				extra = HZ; /* only extend it once, max */
2424bdd4dddeSJeff Garzik 		}
24250d8be5cbSMark Lord 	} while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
2426bdd4dddeSJeff Garzik 
242717c5aab5SMark Lord 	return rc;
2428bdd4dddeSJeff Garzik }
2429bdd4dddeSJeff Garzik 
2430bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
2431c6fd2807SJeff Garzik {
2432f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
24331cfd19aeSMark Lord 	unsigned int shift, hardport, port = ap->port_no;
24347368f919SMark Lord 	u32 main_irq_mask;
2435c6fd2807SJeff Garzik 
2436bdd4dddeSJeff Garzik 	/* FIXME: handle coalescing completion events properly */
2437c6fd2807SJeff Garzik 
24381cfd19aeSMark Lord 	mv_stop_edma(ap);
24391cfd19aeSMark Lord 	MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2440c6fd2807SJeff Garzik 
2441bdd4dddeSJeff Garzik 	/* disable assertion of portN err, done events */
24427368f919SMark Lord 	main_irq_mask = readl(hpriv->main_irq_mask_addr);
24437368f919SMark Lord 	main_irq_mask &= ~((DONE_IRQ | ERR_IRQ) << shift);
24447368f919SMark Lord 	writelfl(main_irq_mask, hpriv->main_irq_mask_addr);
2445c6fd2807SJeff Garzik }
2446bdd4dddeSJeff Garzik 
2447bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
2448bdd4dddeSJeff Garzik {
2449f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
24501cfd19aeSMark Lord 	unsigned int shift, hardport, port = ap->port_no;
24511cfd19aeSMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
2452bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
24537368f919SMark Lord 	u32 main_irq_mask, hc_irq_cause;
2454bdd4dddeSJeff Garzik 
2455bdd4dddeSJeff Garzik 	/* FIXME: handle coalescing completion events properly */
2456bdd4dddeSJeff Garzik 
24571cfd19aeSMark Lord 	MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2458bdd4dddeSJeff Garzik 
2459bdd4dddeSJeff Garzik 	/* clear EDMA errors on this port */
2460bdd4dddeSJeff Garzik 	writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2461bdd4dddeSJeff Garzik 
2462bdd4dddeSJeff Garzik 	/* clear pending irq events */
2463bdd4dddeSJeff Garzik 	hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
24641cfd19aeSMark Lord 	hc_irq_cause &= ~((DEV_IRQ | DMA_IRQ) << hardport);
24651cfd19aeSMark Lord 	writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
2466bdd4dddeSJeff Garzik 
2467bdd4dddeSJeff Garzik 	/* enable assertion of portN err, done events */
24687368f919SMark Lord 	main_irq_mask = readl(hpriv->main_irq_mask_addr);
24697368f919SMark Lord 	main_irq_mask |= ((DONE_IRQ | ERR_IRQ) << shift);
24707368f919SMark Lord 	writelfl(main_irq_mask, hpriv->main_irq_mask_addr);
2471c6fd2807SJeff Garzik }
2472c6fd2807SJeff Garzik 
2473c6fd2807SJeff Garzik /**
2474c6fd2807SJeff Garzik  *      mv_port_init - Perform some early initialization on a single port.
2475c6fd2807SJeff Garzik  *      @port: libata data structure storing shadow register addresses
2476c6fd2807SJeff Garzik  *      @port_mmio: base address of the port
2477c6fd2807SJeff Garzik  *
2478c6fd2807SJeff Garzik  *      Initialize shadow register mmio addresses, clear outstanding
2479c6fd2807SJeff Garzik  *      interrupts on the port, and unmask interrupts for the future
2480c6fd2807SJeff Garzik  *      start of the port.
2481c6fd2807SJeff Garzik  *
2482c6fd2807SJeff Garzik  *      LOCKING:
2483c6fd2807SJeff Garzik  *      Inherited from caller.
2484c6fd2807SJeff Garzik  */
2485c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
2486c6fd2807SJeff Garzik {
24870d5ff566STejun Heo 	void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
2488c6fd2807SJeff Garzik 	unsigned serr_ofs;
2489c6fd2807SJeff Garzik 
2490c6fd2807SJeff Garzik 	/* PIO related setup
2491c6fd2807SJeff Garzik 	 */
2492c6fd2807SJeff Garzik 	port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
2493c6fd2807SJeff Garzik 	port->error_addr =
2494c6fd2807SJeff Garzik 		port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
2495c6fd2807SJeff Garzik 	port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
2496c6fd2807SJeff Garzik 	port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
2497c6fd2807SJeff Garzik 	port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
2498c6fd2807SJeff Garzik 	port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
2499c6fd2807SJeff Garzik 	port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
2500c6fd2807SJeff Garzik 	port->status_addr =
2501c6fd2807SJeff Garzik 		port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
2502c6fd2807SJeff Garzik 	/* special case: control/altstatus doesn't have ATA_REG_ address */
2503c6fd2807SJeff Garzik 	port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
2504c6fd2807SJeff Garzik 
2505c6fd2807SJeff Garzik 	/* unused: */
25068d9db2d2SRandy Dunlap 	port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
2507c6fd2807SJeff Garzik 
2508c6fd2807SJeff Garzik 	/* Clear any currently outstanding port interrupt conditions */
2509c6fd2807SJeff Garzik 	serr_ofs = mv_scr_offset(SCR_ERROR);
2510c6fd2807SJeff Garzik 	writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
2511c6fd2807SJeff Garzik 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2512c6fd2807SJeff Garzik 
2513646a4da5SMark Lord 	/* unmask all non-transient EDMA error interrupts */
2514646a4da5SMark Lord 	writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
2515c6fd2807SJeff Garzik 
2516c6fd2807SJeff Garzik 	VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
2517c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_CFG_OFS),
2518c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
2519c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
2520c6fd2807SJeff Garzik }
2521c6fd2807SJeff Garzik 
2522616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host)
2523616d4a98SMark Lord {
2524616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2525616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
2526616d4a98SMark Lord 	u32 reg;
2527616d4a98SMark Lord 
2528616d4a98SMark Lord 	if (!HAS_PCI(host) || !IS_PCIE(hpriv))
2529616d4a98SMark Lord 		return 0;	/* not PCI-X capable */
2530616d4a98SMark Lord 	reg = readl(mmio + MV_PCI_MODE_OFS);
2531616d4a98SMark Lord 	if ((reg & MV_PCI_MODE_MASK) == 0)
2532616d4a98SMark Lord 		return 0;	/* conventional PCI mode */
2533616d4a98SMark Lord 	return 1;	/* chip is in PCI-X mode */
2534616d4a98SMark Lord }
2535616d4a98SMark Lord 
2536616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host)
2537616d4a98SMark Lord {
2538616d4a98SMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2539616d4a98SMark Lord 	void __iomem *mmio = hpriv->base;
2540616d4a98SMark Lord 	u32 reg;
2541616d4a98SMark Lord 
2542616d4a98SMark Lord 	if (!mv_in_pcix_mode(host)) {
2543616d4a98SMark Lord 		reg = readl(mmio + PCI_COMMAND_OFS);
2544616d4a98SMark Lord 		if (reg & PCI_COMMAND_MRDTRIG)
2545616d4a98SMark Lord 			return 0; /* not okay */
2546616d4a98SMark Lord 	}
2547616d4a98SMark Lord 	return 1; /* okay */
2548616d4a98SMark Lord }
2549616d4a98SMark Lord 
25504447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
2551c6fd2807SJeff Garzik {
25524447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
25534447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
2554c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
2555c6fd2807SJeff Garzik 
2556c6fd2807SJeff Garzik 	switch (board_idx) {
2557c6fd2807SJeff Garzik 	case chip_5080:
2558c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
2559ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
2560c6fd2807SJeff Garzik 
256144c10138SAuke Kok 		switch (pdev->revision) {
2562c6fd2807SJeff Garzik 		case 0x1:
2563c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
2564c6fd2807SJeff Garzik 			break;
2565c6fd2807SJeff Garzik 		case 0x3:
2566c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2567c6fd2807SJeff Garzik 			break;
2568c6fd2807SJeff Garzik 		default:
2569c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2570c6fd2807SJeff Garzik 			   "Applying 50XXB2 workarounds to unknown rev\n");
2571c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2572c6fd2807SJeff Garzik 			break;
2573c6fd2807SJeff Garzik 		}
2574c6fd2807SJeff Garzik 		break;
2575c6fd2807SJeff Garzik 
2576c6fd2807SJeff Garzik 	case chip_504x:
2577c6fd2807SJeff Garzik 	case chip_508x:
2578c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
2579ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
2580c6fd2807SJeff Garzik 
258144c10138SAuke Kok 		switch (pdev->revision) {
2582c6fd2807SJeff Garzik 		case 0x0:
2583c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
2584c6fd2807SJeff Garzik 			break;
2585c6fd2807SJeff Garzik 		case 0x3:
2586c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2587c6fd2807SJeff Garzik 			break;
2588c6fd2807SJeff Garzik 		default:
2589c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2590c6fd2807SJeff Garzik 			   "Applying B2 workarounds to unknown rev\n");
2591c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2592c6fd2807SJeff Garzik 			break;
2593c6fd2807SJeff Garzik 		}
2594c6fd2807SJeff Garzik 		break;
2595c6fd2807SJeff Garzik 
2596c6fd2807SJeff Garzik 	case chip_604x:
2597c6fd2807SJeff Garzik 	case chip_608x:
2598c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
2599ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_II;
2600c6fd2807SJeff Garzik 
260144c10138SAuke Kok 		switch (pdev->revision) {
2602c6fd2807SJeff Garzik 		case 0x7:
2603c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
2604c6fd2807SJeff Garzik 			break;
2605c6fd2807SJeff Garzik 		case 0x9:
2606c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2607c6fd2807SJeff Garzik 			break;
2608c6fd2807SJeff Garzik 		default:
2609c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2610c6fd2807SJeff Garzik 				   "Applying B2 workarounds to unknown rev\n");
2611c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
2612c6fd2807SJeff Garzik 			break;
2613c6fd2807SJeff Garzik 		}
2614c6fd2807SJeff Garzik 		break;
2615c6fd2807SJeff Garzik 
2616c6fd2807SJeff Garzik 	case chip_7042:
2617616d4a98SMark Lord 		hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
2618306b30f7SMark Lord 		if (pdev->vendor == PCI_VENDOR_ID_TTI &&
2619306b30f7SMark Lord 		    (pdev->device == 0x2300 || pdev->device == 0x2310))
2620306b30f7SMark Lord 		{
26214e520033SMark Lord 			/*
26224e520033SMark Lord 			 * Highpoint RocketRAID PCIe 23xx series cards:
26234e520033SMark Lord 			 *
26244e520033SMark Lord 			 * Unconfigured drives are treated as "Legacy"
26254e520033SMark Lord 			 * by the BIOS, and it overwrites sector 8 with
26264e520033SMark Lord 			 * a "Lgcy" metadata block prior to Linux boot.
26274e520033SMark Lord 			 *
26284e520033SMark Lord 			 * Configured drives (RAID or JBOD) leave sector 8
26294e520033SMark Lord 			 * alone, but instead overwrite a high numbered
26304e520033SMark Lord 			 * sector for the RAID metadata.  This sector can
26314e520033SMark Lord 			 * be determined exactly, by truncating the physical
26324e520033SMark Lord 			 * drive capacity to a nice even GB value.
26334e520033SMark Lord 			 *
26344e520033SMark Lord 			 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
26354e520033SMark Lord 			 *
26364e520033SMark Lord 			 * Warn the user, lest they think we're just buggy.
26374e520033SMark Lord 			 */
26384e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
26394e520033SMark Lord 				" BIOS CORRUPTS DATA on all attached drives,"
26404e520033SMark Lord 				" regardless of if/how they are configured."
26414e520033SMark Lord 				" BEWARE!\n");
26424e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": For data safety, do not"
26434e520033SMark Lord 				" use sectors 8-9 on \"Legacy\" drives,"
26444e520033SMark Lord 				" and avoid the final two gigabytes on"
26454e520033SMark Lord 				" all RocketRAID BIOS initialized drives.\n");
2646306b30f7SMark Lord 		}
26478e7decdbSMark Lord 		/* drop through */
2648c6fd2807SJeff Garzik 	case chip_6042:
2649c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
2650c6fd2807SJeff Garzik 		hp_flags |= MV_HP_GEN_IIE;
2651616d4a98SMark Lord 		if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
2652616d4a98SMark Lord 			hp_flags |= MV_HP_CUT_THROUGH;
2653c6fd2807SJeff Garzik 
265444c10138SAuke Kok 		switch (pdev->revision) {
2655c6fd2807SJeff Garzik 		case 0x0:
2656c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_XX42A0;
2657c6fd2807SJeff Garzik 			break;
2658c6fd2807SJeff Garzik 		case 0x1:
2659c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2660c6fd2807SJeff Garzik 			break;
2661c6fd2807SJeff Garzik 		default:
2662c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2663c6fd2807SJeff Garzik 			   "Applying 60X1C0 workarounds to unknown rev\n");
2664c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2665c6fd2807SJeff Garzik 			break;
2666c6fd2807SJeff Garzik 		}
2667c6fd2807SJeff Garzik 		break;
2668f351b2d6SSaeed Bishara 	case chip_soc:
2669f351b2d6SSaeed Bishara 		hpriv->ops = &mv_soc_ops;
2670f351b2d6SSaeed Bishara 		hp_flags |= MV_HP_ERRATA_60X1C0;
2671f351b2d6SSaeed Bishara 		break;
2672c6fd2807SJeff Garzik 
2673c6fd2807SJeff Garzik 	default:
2674f351b2d6SSaeed Bishara 		dev_printk(KERN_ERR, host->dev,
26755796d1c4SJeff Garzik 			   "BUG: invalid board index %u\n", board_idx);
2676c6fd2807SJeff Garzik 		return 1;
2677c6fd2807SJeff Garzik 	}
2678c6fd2807SJeff Garzik 
2679c6fd2807SJeff Garzik 	hpriv->hp_flags = hp_flags;
268002a121daSMark Lord 	if (hp_flags & MV_HP_PCIE) {
268102a121daSMark Lord 		hpriv->irq_cause_ofs	= PCIE_IRQ_CAUSE_OFS;
268202a121daSMark Lord 		hpriv->irq_mask_ofs	= PCIE_IRQ_MASK_OFS;
268302a121daSMark Lord 		hpriv->unmask_all_irqs	= PCIE_UNMASK_ALL_IRQS;
268402a121daSMark Lord 	} else {
268502a121daSMark Lord 		hpriv->irq_cause_ofs	= PCI_IRQ_CAUSE_OFS;
268602a121daSMark Lord 		hpriv->irq_mask_ofs	= PCI_IRQ_MASK_OFS;
268702a121daSMark Lord 		hpriv->unmask_all_irqs	= PCI_UNMASK_ALL_IRQS;
268802a121daSMark Lord 	}
2689c6fd2807SJeff Garzik 
2690c6fd2807SJeff Garzik 	return 0;
2691c6fd2807SJeff Garzik }
2692c6fd2807SJeff Garzik 
2693c6fd2807SJeff Garzik /**
2694c6fd2807SJeff Garzik  *      mv_init_host - Perform some early initialization of the host.
26954447d351STejun Heo  *	@host: ATA host to initialize
26964447d351STejun Heo  *      @board_idx: controller index
2697c6fd2807SJeff Garzik  *
2698c6fd2807SJeff Garzik  *      If possible, do an early global reset of the host.  Then do
2699c6fd2807SJeff Garzik  *      our port init and clear/unmask all/relevant host interrupts.
2700c6fd2807SJeff Garzik  *
2701c6fd2807SJeff Garzik  *      LOCKING:
2702c6fd2807SJeff Garzik  *      Inherited from caller.
2703c6fd2807SJeff Garzik  */
27044447d351STejun Heo static int mv_init_host(struct ata_host *host, unsigned int board_idx)
2705c6fd2807SJeff Garzik {
2706c6fd2807SJeff Garzik 	int rc = 0, n_hc, port, hc;
27074447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
2708f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
2709c6fd2807SJeff Garzik 
27104447d351STejun Heo 	rc = mv_chip_id(host, board_idx);
2711c6fd2807SJeff Garzik 	if (rc)
2712c6fd2807SJeff Garzik 		goto done;
2713c6fd2807SJeff Garzik 
2714f351b2d6SSaeed Bishara 	if (HAS_PCI(host)) {
27157368f919SMark Lord 		hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE_OFS;
27167368f919SMark Lord 		hpriv->main_irq_mask_addr  = mmio + PCI_HC_MAIN_IRQ_MASK_OFS;
2717f351b2d6SSaeed Bishara 	} else {
27187368f919SMark Lord 		hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE_OFS;
27197368f919SMark Lord 		hpriv->main_irq_mask_addr  = mmio + SOC_HC_MAIN_IRQ_MASK_OFS;
2720f351b2d6SSaeed Bishara 	}
2721352fab70SMark Lord 
2722352fab70SMark Lord 	/* global interrupt mask: 0 == mask everything */
27237368f919SMark Lord 	writel(0, hpriv->main_irq_mask_addr);
2724f351b2d6SSaeed Bishara 
27254447d351STejun Heo 	n_hc = mv_get_hc_count(host->ports[0]->flags);
2726c6fd2807SJeff Garzik 
27274447d351STejun Heo 	for (port = 0; port < host->n_ports; port++)
2728c6fd2807SJeff Garzik 		hpriv->ops->read_preamp(hpriv, port, mmio);
2729c6fd2807SJeff Garzik 
2730c6fd2807SJeff Garzik 	rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
2731c6fd2807SJeff Garzik 	if (rc)
2732c6fd2807SJeff Garzik 		goto done;
2733c6fd2807SJeff Garzik 
2734c6fd2807SJeff Garzik 	hpriv->ops->reset_flash(hpriv, mmio);
27357bb3c529SSaeed Bishara 	hpriv->ops->reset_bus(host, mmio);
2736c6fd2807SJeff Garzik 	hpriv->ops->enable_leds(hpriv, mmio);
2737c6fd2807SJeff Garzik 
27384447d351STejun Heo 	for (port = 0; port < host->n_ports; port++) {
2739cbcdd875STejun Heo 		struct ata_port *ap = host->ports[port];
2740c6fd2807SJeff Garzik 		void __iomem *port_mmio = mv_port_base(mmio, port);
2741cbcdd875STejun Heo 
2742cbcdd875STejun Heo 		mv_port_init(&ap->ioaddr, port_mmio);
2743cbcdd875STejun Heo 
27447bb3c529SSaeed Bishara #ifdef CONFIG_PCI
2745f351b2d6SSaeed Bishara 		if (HAS_PCI(host)) {
2746f351b2d6SSaeed Bishara 			unsigned int offset = port_mmio - mmio;
2747cbcdd875STejun Heo 			ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
2748cbcdd875STejun Heo 			ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
2749f351b2d6SSaeed Bishara 		}
27507bb3c529SSaeed Bishara #endif
2751c6fd2807SJeff Garzik 	}
2752c6fd2807SJeff Garzik 
2753c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
2754c6fd2807SJeff Garzik 		void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2755c6fd2807SJeff Garzik 
2756c6fd2807SJeff Garzik 		VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
2757c6fd2807SJeff Garzik 			"(before clear)=0x%08x\n", hc,
2758c6fd2807SJeff Garzik 			readl(hc_mmio + HC_CFG_OFS),
2759c6fd2807SJeff Garzik 			readl(hc_mmio + HC_IRQ_CAUSE_OFS));
2760c6fd2807SJeff Garzik 
2761c6fd2807SJeff Garzik 		/* Clear any currently outstanding hc interrupt conditions */
2762c6fd2807SJeff Garzik 		writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
2763c6fd2807SJeff Garzik 	}
2764c6fd2807SJeff Garzik 
2765f351b2d6SSaeed Bishara 	if (HAS_PCI(host)) {
2766c6fd2807SJeff Garzik 		/* Clear any currently outstanding host interrupt conditions */
276702a121daSMark Lord 		writelfl(0, mmio + hpriv->irq_cause_ofs);
2768c6fd2807SJeff Garzik 
2769c6fd2807SJeff Garzik 		/* and unmask interrupt generation for host regs */
277002a121daSMark Lord 		writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
2771ee9ccdf7SJeff Garzik 		if (IS_GEN_I(hpriv))
2772f351b2d6SSaeed Bishara 			writelfl(~HC_MAIN_MASKED_IRQS_5,
27737368f919SMark Lord 				 hpriv->main_irq_mask_addr);
2774fb621e2fSJeff Garzik 		else
2775f351b2d6SSaeed Bishara 			writelfl(~HC_MAIN_MASKED_IRQS,
27767368f919SMark Lord 				 hpriv->main_irq_mask_addr);
2777c6fd2807SJeff Garzik 
2778c6fd2807SJeff Garzik 		VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
2779c6fd2807SJeff Garzik 			"PCI int cause/mask=0x%08x/0x%08x\n",
27807368f919SMark Lord 			readl(hpriv->main_irq_cause_addr),
27817368f919SMark Lord 			readl(hpriv->main_irq_mask_addr),
278202a121daSMark Lord 			readl(mmio + hpriv->irq_cause_ofs),
278302a121daSMark Lord 			readl(mmio + hpriv->irq_mask_ofs));
2784f351b2d6SSaeed Bishara 	} else {
2785f351b2d6SSaeed Bishara 		writelfl(~HC_MAIN_MASKED_IRQS_SOC,
27867368f919SMark Lord 			 hpriv->main_irq_mask_addr);
2787f351b2d6SSaeed Bishara 		VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x\n",
27887368f919SMark Lord 			readl(hpriv->main_irq_cause_addr),
27897368f919SMark Lord 			readl(hpriv->main_irq_mask_addr));
2790f351b2d6SSaeed Bishara 	}
2791c6fd2807SJeff Garzik done:
2792c6fd2807SJeff Garzik 	return rc;
2793c6fd2807SJeff Garzik }
2794c6fd2807SJeff Garzik 
2795fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
2796fbf14e2fSByron Bradley {
2797fbf14e2fSByron Bradley 	hpriv->crqb_pool   = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
2798fbf14e2fSByron Bradley 							     MV_CRQB_Q_SZ, 0);
2799fbf14e2fSByron Bradley 	if (!hpriv->crqb_pool)
2800fbf14e2fSByron Bradley 		return -ENOMEM;
2801fbf14e2fSByron Bradley 
2802fbf14e2fSByron Bradley 	hpriv->crpb_pool   = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
2803fbf14e2fSByron Bradley 							     MV_CRPB_Q_SZ, 0);
2804fbf14e2fSByron Bradley 	if (!hpriv->crpb_pool)
2805fbf14e2fSByron Bradley 		return -ENOMEM;
2806fbf14e2fSByron Bradley 
2807fbf14e2fSByron Bradley 	hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
2808fbf14e2fSByron Bradley 							     MV_SG_TBL_SZ, 0);
2809fbf14e2fSByron Bradley 	if (!hpriv->sg_tbl_pool)
2810fbf14e2fSByron Bradley 		return -ENOMEM;
2811fbf14e2fSByron Bradley 
2812fbf14e2fSByron Bradley 	return 0;
2813fbf14e2fSByron Bradley }
2814fbf14e2fSByron Bradley 
281515a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
281615a32632SLennert Buytenhek 				 struct mbus_dram_target_info *dram)
281715a32632SLennert Buytenhek {
281815a32632SLennert Buytenhek 	int i;
281915a32632SLennert Buytenhek 
282015a32632SLennert Buytenhek 	for (i = 0; i < 4; i++) {
282115a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_CTRL(i));
282215a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_BASE(i));
282315a32632SLennert Buytenhek 	}
282415a32632SLennert Buytenhek 
282515a32632SLennert Buytenhek 	for (i = 0; i < dram->num_cs; i++) {
282615a32632SLennert Buytenhek 		struct mbus_dram_window *cs = dram->cs + i;
282715a32632SLennert Buytenhek 
282815a32632SLennert Buytenhek 		writel(((cs->size - 1) & 0xffff0000) |
282915a32632SLennert Buytenhek 			(cs->mbus_attr << 8) |
283015a32632SLennert Buytenhek 			(dram->mbus_dram_target_id << 4) | 1,
283115a32632SLennert Buytenhek 			hpriv->base + WINDOW_CTRL(i));
283215a32632SLennert Buytenhek 		writel(cs->base, hpriv->base + WINDOW_BASE(i));
283315a32632SLennert Buytenhek 	}
283415a32632SLennert Buytenhek }
283515a32632SLennert Buytenhek 
2836f351b2d6SSaeed Bishara /**
2837f351b2d6SSaeed Bishara  *      mv_platform_probe - handle a positive probe of an soc Marvell
2838f351b2d6SSaeed Bishara  *      host
2839f351b2d6SSaeed Bishara  *      @pdev: platform device found
2840f351b2d6SSaeed Bishara  *
2841f351b2d6SSaeed Bishara  *      LOCKING:
2842f351b2d6SSaeed Bishara  *      Inherited from caller.
2843f351b2d6SSaeed Bishara  */
2844f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
2845f351b2d6SSaeed Bishara {
2846f351b2d6SSaeed Bishara 	static int printed_version;
2847f351b2d6SSaeed Bishara 	const struct mv_sata_platform_data *mv_platform_data;
2848f351b2d6SSaeed Bishara 	const struct ata_port_info *ppi[] =
2849f351b2d6SSaeed Bishara 	    { &mv_port_info[chip_soc], NULL };
2850f351b2d6SSaeed Bishara 	struct ata_host *host;
2851f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv;
2852f351b2d6SSaeed Bishara 	struct resource *res;
2853f351b2d6SSaeed Bishara 	int n_ports, rc;
2854f351b2d6SSaeed Bishara 
2855f351b2d6SSaeed Bishara 	if (!printed_version++)
2856f351b2d6SSaeed Bishara 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
2857f351b2d6SSaeed Bishara 
2858f351b2d6SSaeed Bishara 	/*
2859f351b2d6SSaeed Bishara 	 * Simple resource validation ..
2860f351b2d6SSaeed Bishara 	 */
2861f351b2d6SSaeed Bishara 	if (unlikely(pdev->num_resources != 2)) {
2862f351b2d6SSaeed Bishara 		dev_err(&pdev->dev, "invalid number of resources\n");
2863f351b2d6SSaeed Bishara 		return -EINVAL;
2864f351b2d6SSaeed Bishara 	}
2865f351b2d6SSaeed Bishara 
2866f351b2d6SSaeed Bishara 	/*
2867f351b2d6SSaeed Bishara 	 * Get the register base first
2868f351b2d6SSaeed Bishara 	 */
2869f351b2d6SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2870f351b2d6SSaeed Bishara 	if (res == NULL)
2871f351b2d6SSaeed Bishara 		return -EINVAL;
2872f351b2d6SSaeed Bishara 
2873f351b2d6SSaeed Bishara 	/* allocate host */
2874f351b2d6SSaeed Bishara 	mv_platform_data = pdev->dev.platform_data;
2875f351b2d6SSaeed Bishara 	n_ports = mv_platform_data->n_ports;
2876f351b2d6SSaeed Bishara 
2877f351b2d6SSaeed Bishara 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
2878f351b2d6SSaeed Bishara 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
2879f351b2d6SSaeed Bishara 
2880f351b2d6SSaeed Bishara 	if (!host || !hpriv)
2881f351b2d6SSaeed Bishara 		return -ENOMEM;
2882f351b2d6SSaeed Bishara 	host->private_data = hpriv;
2883f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
2884f351b2d6SSaeed Bishara 
2885f351b2d6SSaeed Bishara 	host->iomap = NULL;
2886f1cb0ea1SSaeed Bishara 	hpriv->base = devm_ioremap(&pdev->dev, res->start,
2887f1cb0ea1SSaeed Bishara 				   res->end - res->start + 1);
2888f351b2d6SSaeed Bishara 	hpriv->base -= MV_SATAHC0_REG_BASE;
2889f351b2d6SSaeed Bishara 
289015a32632SLennert Buytenhek 	/*
289115a32632SLennert Buytenhek 	 * (Re-)program MBUS remapping windows if we are asked to.
289215a32632SLennert Buytenhek 	 */
289315a32632SLennert Buytenhek 	if (mv_platform_data->dram != NULL)
289415a32632SLennert Buytenhek 		mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
289515a32632SLennert Buytenhek 
2896fbf14e2fSByron Bradley 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
2897fbf14e2fSByron Bradley 	if (rc)
2898fbf14e2fSByron Bradley 		return rc;
2899fbf14e2fSByron Bradley 
2900f351b2d6SSaeed Bishara 	/* initialize adapter */
2901f351b2d6SSaeed Bishara 	rc = mv_init_host(host, chip_soc);
2902f351b2d6SSaeed Bishara 	if (rc)
2903f351b2d6SSaeed Bishara 		return rc;
2904f351b2d6SSaeed Bishara 
2905f351b2d6SSaeed Bishara 	dev_printk(KERN_INFO, &pdev->dev,
2906f351b2d6SSaeed Bishara 		   "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
2907f351b2d6SSaeed Bishara 		   host->n_ports);
2908f351b2d6SSaeed Bishara 
2909f351b2d6SSaeed Bishara 	return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
2910f351b2d6SSaeed Bishara 				 IRQF_SHARED, &mv6_sht);
2911f351b2d6SSaeed Bishara }
2912f351b2d6SSaeed Bishara 
2913f351b2d6SSaeed Bishara /*
2914f351b2d6SSaeed Bishara  *
2915f351b2d6SSaeed Bishara  *      mv_platform_remove    -       unplug a platform interface
2916f351b2d6SSaeed Bishara  *      @pdev: platform device
2917f351b2d6SSaeed Bishara  *
2918f351b2d6SSaeed Bishara  *      A platform bus SATA device has been unplugged. Perform the needed
2919f351b2d6SSaeed Bishara  *      cleanup. Also called on module unload for any active devices.
2920f351b2d6SSaeed Bishara  */
2921f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev)
2922f351b2d6SSaeed Bishara {
2923f351b2d6SSaeed Bishara 	struct device *dev = &pdev->dev;
2924f351b2d6SSaeed Bishara 	struct ata_host *host = dev_get_drvdata(dev);
2925f351b2d6SSaeed Bishara 
2926f351b2d6SSaeed Bishara 	ata_host_detach(host);
2927f351b2d6SSaeed Bishara 	return 0;
2928f351b2d6SSaeed Bishara }
2929f351b2d6SSaeed Bishara 
2930f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
2931f351b2d6SSaeed Bishara 	.probe			= mv_platform_probe,
2932f351b2d6SSaeed Bishara 	.remove			= __devexit_p(mv_platform_remove),
2933f351b2d6SSaeed Bishara 	.driver			= {
2934f351b2d6SSaeed Bishara 				   .name = DRV_NAME,
2935f351b2d6SSaeed Bishara 				   .owner = THIS_MODULE,
2936f351b2d6SSaeed Bishara 				  },
2937f351b2d6SSaeed Bishara };
2938f351b2d6SSaeed Bishara 
2939f351b2d6SSaeed Bishara 
29407bb3c529SSaeed Bishara #ifdef CONFIG_PCI
2941f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
2942f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent);
2943f351b2d6SSaeed Bishara 
29447bb3c529SSaeed Bishara 
29457bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
29467bb3c529SSaeed Bishara 	.name			= DRV_NAME,
29477bb3c529SSaeed Bishara 	.id_table		= mv_pci_tbl,
2948f351b2d6SSaeed Bishara 	.probe			= mv_pci_init_one,
29497bb3c529SSaeed Bishara 	.remove			= ata_pci_remove_one,
29507bb3c529SSaeed Bishara };
29517bb3c529SSaeed Bishara 
29527bb3c529SSaeed Bishara /*
29537bb3c529SSaeed Bishara  * module options
29547bb3c529SSaeed Bishara  */
29557bb3c529SSaeed Bishara static int msi;	      /* Use PCI msi; either zero (off, default) or non-zero */
29567bb3c529SSaeed Bishara 
29577bb3c529SSaeed Bishara 
29587bb3c529SSaeed Bishara /* move to PCI layer or libata core? */
29597bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev)
29607bb3c529SSaeed Bishara {
29617bb3c529SSaeed Bishara 	int rc;
29627bb3c529SSaeed Bishara 
29637bb3c529SSaeed Bishara 	if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
29647bb3c529SSaeed Bishara 		rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
29657bb3c529SSaeed Bishara 		if (rc) {
29667bb3c529SSaeed Bishara 			rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
29677bb3c529SSaeed Bishara 			if (rc) {
29687bb3c529SSaeed Bishara 				dev_printk(KERN_ERR, &pdev->dev,
29697bb3c529SSaeed Bishara 					   "64-bit DMA enable failed\n");
29707bb3c529SSaeed Bishara 				return rc;
29717bb3c529SSaeed Bishara 			}
29727bb3c529SSaeed Bishara 		}
29737bb3c529SSaeed Bishara 	} else {
29747bb3c529SSaeed Bishara 		rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
29757bb3c529SSaeed Bishara 		if (rc) {
29767bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
29777bb3c529SSaeed Bishara 				   "32-bit DMA enable failed\n");
29787bb3c529SSaeed Bishara 			return rc;
29797bb3c529SSaeed Bishara 		}
29807bb3c529SSaeed Bishara 		rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
29817bb3c529SSaeed Bishara 		if (rc) {
29827bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
29837bb3c529SSaeed Bishara 				   "32-bit consistent DMA enable failed\n");
29847bb3c529SSaeed Bishara 			return rc;
29857bb3c529SSaeed Bishara 		}
29867bb3c529SSaeed Bishara 	}
29877bb3c529SSaeed Bishara 
29887bb3c529SSaeed Bishara 	return rc;
29897bb3c529SSaeed Bishara }
29907bb3c529SSaeed Bishara 
2991c6fd2807SJeff Garzik /**
2992c6fd2807SJeff Garzik  *      mv_print_info - Dump key info to kernel log for perusal.
29934447d351STejun Heo  *      @host: ATA host to print info about
2994c6fd2807SJeff Garzik  *
2995c6fd2807SJeff Garzik  *      FIXME: complete this.
2996c6fd2807SJeff Garzik  *
2997c6fd2807SJeff Garzik  *      LOCKING:
2998c6fd2807SJeff Garzik  *      Inherited from caller.
2999c6fd2807SJeff Garzik  */
30004447d351STejun Heo static void mv_print_info(struct ata_host *host)
3001c6fd2807SJeff Garzik {
30024447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
30034447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
300444c10138SAuke Kok 	u8 scc;
3005c1e4fe71SJeff Garzik 	const char *scc_s, *gen;
3006c6fd2807SJeff Garzik 
3007c6fd2807SJeff Garzik 	/* Use this to determine the HW stepping of the chip so we know
3008c6fd2807SJeff Garzik 	 * what errata to workaround
3009c6fd2807SJeff Garzik 	 */
3010c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
3011c6fd2807SJeff Garzik 	if (scc == 0)
3012c6fd2807SJeff Garzik 		scc_s = "SCSI";
3013c6fd2807SJeff Garzik 	else if (scc == 0x01)
3014c6fd2807SJeff Garzik 		scc_s = "RAID";
3015c6fd2807SJeff Garzik 	else
3016c1e4fe71SJeff Garzik 		scc_s = "?";
3017c1e4fe71SJeff Garzik 
3018c1e4fe71SJeff Garzik 	if (IS_GEN_I(hpriv))
3019c1e4fe71SJeff Garzik 		gen = "I";
3020c1e4fe71SJeff Garzik 	else if (IS_GEN_II(hpriv))
3021c1e4fe71SJeff Garzik 		gen = "II";
3022c1e4fe71SJeff Garzik 	else if (IS_GEN_IIE(hpriv))
3023c1e4fe71SJeff Garzik 		gen = "IIE";
3024c1e4fe71SJeff Garzik 	else
3025c1e4fe71SJeff Garzik 		gen = "?";
3026c6fd2807SJeff Garzik 
3027c6fd2807SJeff Garzik 	dev_printk(KERN_INFO, &pdev->dev,
3028c1e4fe71SJeff Garzik 	       "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
3029c1e4fe71SJeff Garzik 	       gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
3030c6fd2807SJeff Garzik 	       scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
3031c6fd2807SJeff Garzik }
3032c6fd2807SJeff Garzik 
3033c6fd2807SJeff Garzik /**
3034f351b2d6SSaeed Bishara  *      mv_pci_init_one - handle a positive probe of a PCI Marvell host
3035c6fd2807SJeff Garzik  *      @pdev: PCI device found
3036c6fd2807SJeff Garzik  *      @ent: PCI device ID entry for the matched host
3037c6fd2807SJeff Garzik  *
3038c6fd2807SJeff Garzik  *      LOCKING:
3039c6fd2807SJeff Garzik  *      Inherited from caller.
3040c6fd2807SJeff Garzik  */
3041f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
3042f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent)
3043c6fd2807SJeff Garzik {
30442dcb407eSJeff Garzik 	static int printed_version;
3045c6fd2807SJeff Garzik 	unsigned int board_idx = (unsigned int)ent->driver_data;
30464447d351STejun Heo 	const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
30474447d351STejun Heo 	struct ata_host *host;
30484447d351STejun Heo 	struct mv_host_priv *hpriv;
30494447d351STejun Heo 	int n_ports, rc;
3050c6fd2807SJeff Garzik 
3051c6fd2807SJeff Garzik 	if (!printed_version++)
3052c6fd2807SJeff Garzik 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
3053c6fd2807SJeff Garzik 
30544447d351STejun Heo 	/* allocate host */
30554447d351STejun Heo 	n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
30564447d351STejun Heo 
30574447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
30584447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
30594447d351STejun Heo 	if (!host || !hpriv)
30604447d351STejun Heo 		return -ENOMEM;
30614447d351STejun Heo 	host->private_data = hpriv;
3062f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
30634447d351STejun Heo 
30644447d351STejun Heo 	/* acquire resources */
306524dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
306624dc5f33STejun Heo 	if (rc)
3067c6fd2807SJeff Garzik 		return rc;
3068c6fd2807SJeff Garzik 
30690d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
30700d5ff566STejun Heo 	if (rc == -EBUSY)
307124dc5f33STejun Heo 		pcim_pin_device(pdev);
30720d5ff566STejun Heo 	if (rc)
307324dc5f33STejun Heo 		return rc;
30744447d351STejun Heo 	host->iomap = pcim_iomap_table(pdev);
3075f351b2d6SSaeed Bishara 	hpriv->base = host->iomap[MV_PRIMARY_BAR];
3076c6fd2807SJeff Garzik 
3077d88184fbSJeff Garzik 	rc = pci_go_64(pdev);
3078d88184fbSJeff Garzik 	if (rc)
3079d88184fbSJeff Garzik 		return rc;
3080d88184fbSJeff Garzik 
3081da2fa9baSMark Lord 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
3082da2fa9baSMark Lord 	if (rc)
3083da2fa9baSMark Lord 		return rc;
3084da2fa9baSMark Lord 
3085c6fd2807SJeff Garzik 	/* initialize adapter */
30864447d351STejun Heo 	rc = mv_init_host(host, board_idx);
308724dc5f33STejun Heo 	if (rc)
308824dc5f33STejun Heo 		return rc;
3089c6fd2807SJeff Garzik 
3090c6fd2807SJeff Garzik 	/* Enable interrupts */
30916a59dcf8STejun Heo 	if (msi && pci_enable_msi(pdev))
3092c6fd2807SJeff Garzik 		pci_intx(pdev, 1);
3093c6fd2807SJeff Garzik 
3094c6fd2807SJeff Garzik 	mv_dump_pci_cfg(pdev, 0x68);
30954447d351STejun Heo 	mv_print_info(host);
3096c6fd2807SJeff Garzik 
30974447d351STejun Heo 	pci_set_master(pdev);
3098ea8b4db9SJeff Garzik 	pci_try_set_mwi(pdev);
30994447d351STejun Heo 	return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
3100c5d3e45aSJeff Garzik 				 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
3101c6fd2807SJeff Garzik }
31027bb3c529SSaeed Bishara #endif
3103c6fd2807SJeff Garzik 
3104f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev);
3105f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev);
3106f351b2d6SSaeed Bishara 
3107c6fd2807SJeff Garzik static int __init mv_init(void)
3108c6fd2807SJeff Garzik {
31097bb3c529SSaeed Bishara 	int rc = -ENODEV;
31107bb3c529SSaeed Bishara #ifdef CONFIG_PCI
31117bb3c529SSaeed Bishara 	rc = pci_register_driver(&mv_pci_driver);
3112f351b2d6SSaeed Bishara 	if (rc < 0)
3113f351b2d6SSaeed Bishara 		return rc;
3114f351b2d6SSaeed Bishara #endif
3115f351b2d6SSaeed Bishara 	rc = platform_driver_register(&mv_platform_driver);
3116f351b2d6SSaeed Bishara 
3117f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
3118f351b2d6SSaeed Bishara 	if (rc < 0)
3119f351b2d6SSaeed Bishara 		pci_unregister_driver(&mv_pci_driver);
31207bb3c529SSaeed Bishara #endif
31217bb3c529SSaeed Bishara 	return rc;
3122c6fd2807SJeff Garzik }
3123c6fd2807SJeff Garzik 
3124c6fd2807SJeff Garzik static void __exit mv_exit(void)
3125c6fd2807SJeff Garzik {
31267bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3127c6fd2807SJeff Garzik 	pci_unregister_driver(&mv_pci_driver);
31287bb3c529SSaeed Bishara #endif
3129f351b2d6SSaeed Bishara 	platform_driver_unregister(&mv_platform_driver);
3130c6fd2807SJeff Garzik }
3131c6fd2807SJeff Garzik 
3132c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
3133c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
3134c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
3135c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
3136c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
313717c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
3138c6fd2807SJeff Garzik 
31397bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3140c6fd2807SJeff Garzik module_param(msi, int, 0444);
3141c6fd2807SJeff Garzik MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
31427bb3c529SSaeed Bishara #endif
3143c6fd2807SJeff Garzik 
3144c6fd2807SJeff Garzik module_init(mv_init);
3145c6fd2807SJeff Garzik module_exit(mv_exit);
3146