xref: /openbmc/linux/drivers/ata/sata_mv.c (revision 352fab701ca4753dd005b67ce5e512be944eb591)
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 /*
264a05e209SJeff Garzik   sata_mv TODO list:
274a05e209SJeff Garzik 
284a05e209SJeff Garzik   1) Needs a full errata audit for all chipsets.  I implemented most
294a05e209SJeff Garzik   of the errata workarounds found in the Marvell vendor driver, but
304a05e209SJeff Garzik   I distinctly remember a couple workarounds (one related to PCI-X)
314a05e209SJeff Garzik   are still needed.
324a05e209SJeff Garzik 
331fd2e1c2SMark Lord   2) Improve/fix IRQ and error handling sequences.
341fd2e1c2SMark Lord 
351fd2e1c2SMark Lord   3) ATAPI support (Marvell claims the 60xx/70xx chips can do it).
361fd2e1c2SMark Lord 
371fd2e1c2SMark Lord   4) Think about TCQ support here, and for libata in general
381fd2e1c2SMark Lord   with controllers that suppport it via host-queuing hardware
391fd2e1c2SMark Lord   (a software-only implementation could be a nightmare).
404a05e209SJeff Garzik 
414a05e209SJeff Garzik   5) Investigate problems with PCI Message Signalled Interrupts (MSI).
424a05e209SJeff Garzik 
43e49856d8SMark Lord   6) Cache frequently-accessed registers in mv_port_priv to reduce overhead.
444a05e209SJeff Garzik 
4540f0bc2dSMark Lord   7) Fix/reenable hot plug/unplug (should happen as a side-effect of (2) above).
464a05e209SJeff Garzik 
474a05e209SJeff Garzik   8) Develop a low-power-consumption strategy, and implement it.
484a05e209SJeff Garzik 
494a05e209SJeff Garzik   9) [Experiment, low priority] See if ATAPI can be supported using
504a05e209SJeff Garzik   "unknown FIS" or "vendor-specific FIS" support, or something creative
514a05e209SJeff Garzik   like that.
524a05e209SJeff Garzik 
534a05e209SJeff Garzik   10) [Experiment, low priority] Investigate interrupt coalescing.
544a05e209SJeff Garzik   Quite often, especially with PCI Message Signalled Interrupts (MSI),
554a05e209SJeff Garzik   the overhead reduced by interrupt mitigation is quite often not
564a05e209SJeff Garzik   worth the latency cost.
574a05e209SJeff Garzik 
584a05e209SJeff Garzik   11) [Experiment, Marvell value added] Is it possible to use target
594a05e209SJeff Garzik   mode to cross-connect two Linux boxes with Marvell cards?  If so,
604a05e209SJeff Garzik   creating LibATA target mode support would be very interesting.
614a05e209SJeff Garzik 
624a05e209SJeff Garzik   Target mode, for those without docs, is the ability to directly
634a05e209SJeff Garzik   connect two SATA controllers.
644a05e209SJeff Garzik 
654a05e209SJeff Garzik */
664a05e209SJeff Garzik 
67c6fd2807SJeff Garzik #include <linux/kernel.h>
68c6fd2807SJeff Garzik #include <linux/module.h>
69c6fd2807SJeff Garzik #include <linux/pci.h>
70c6fd2807SJeff Garzik #include <linux/init.h>
71c6fd2807SJeff Garzik #include <linux/blkdev.h>
72c6fd2807SJeff Garzik #include <linux/delay.h>
73c6fd2807SJeff Garzik #include <linux/interrupt.h>
748d8b6004SAndrew Morton #include <linux/dmapool.h>
75c6fd2807SJeff Garzik #include <linux/dma-mapping.h>
76c6fd2807SJeff Garzik #include <linux/device.h>
77f351b2d6SSaeed Bishara #include <linux/platform_device.h>
78f351b2d6SSaeed Bishara #include <linux/ata_platform.h>
7915a32632SLennert Buytenhek #include <linux/mbus.h>
80c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
81c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
826c08772eSJeff Garzik #include <scsi/scsi_device.h>
83c6fd2807SJeff Garzik #include <linux/libata.h>
84c6fd2807SJeff Garzik 
85c6fd2807SJeff Garzik #define DRV_NAME	"sata_mv"
861fd2e1c2SMark Lord #define DRV_VERSION	"1.20"
87c6fd2807SJeff Garzik 
88c6fd2807SJeff Garzik enum {
89c6fd2807SJeff Garzik 	/* BAR's are enumerated in terms of pci_resource_start() terms */
90c6fd2807SJeff Garzik 	MV_PRIMARY_BAR		= 0,	/* offset 0x10: memory space */
91c6fd2807SJeff Garzik 	MV_IO_BAR		= 2,	/* offset 0x18: IO space */
92c6fd2807SJeff Garzik 	MV_MISC_BAR		= 3,	/* offset 0x1c: FLASH, NVRAM, SRAM */
93c6fd2807SJeff Garzik 
94c6fd2807SJeff Garzik 	MV_MAJOR_REG_AREA_SZ	= 0x10000,	/* 64KB */
95c6fd2807SJeff Garzik 	MV_MINOR_REG_AREA_SZ	= 0x2000,	/* 8KB */
96c6fd2807SJeff Garzik 
97c6fd2807SJeff Garzik 	MV_PCI_REG_BASE		= 0,
98c6fd2807SJeff Garzik 	MV_IRQ_COAL_REG_BASE	= 0x18000,	/* 6xxx part only */
99c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE		= (MV_IRQ_COAL_REG_BASE + 0x08),
100c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE_LO		= (MV_IRQ_COAL_REG_BASE + 0x88),
101c6fd2807SJeff Garzik 	MV_IRQ_COAL_CAUSE_HI		= (MV_IRQ_COAL_REG_BASE + 0x8c),
102c6fd2807SJeff Garzik 	MV_IRQ_COAL_THRESHOLD		= (MV_IRQ_COAL_REG_BASE + 0xcc),
103c6fd2807SJeff Garzik 	MV_IRQ_COAL_TIME_THRESHOLD	= (MV_IRQ_COAL_REG_BASE + 0xd0),
104c6fd2807SJeff Garzik 
105c6fd2807SJeff Garzik 	MV_SATAHC0_REG_BASE	= 0x20000,
106c6fd2807SJeff Garzik 	MV_FLASH_CTL		= 0x1046c,
107c6fd2807SJeff Garzik 	MV_GPIO_PORT_CTL	= 0x104f0,
108c6fd2807SJeff Garzik 	MV_RESET_CFG		= 0x180d8,
109c6fd2807SJeff Garzik 
110c6fd2807SJeff Garzik 	MV_PCI_REG_SZ		= MV_MAJOR_REG_AREA_SZ,
111c6fd2807SJeff Garzik 	MV_SATAHC_REG_SZ	= MV_MAJOR_REG_AREA_SZ,
112c6fd2807SJeff Garzik 	MV_SATAHC_ARBTR_REG_SZ	= MV_MINOR_REG_AREA_SZ,		/* arbiter */
113c6fd2807SJeff Garzik 	MV_PORT_REG_SZ		= MV_MINOR_REG_AREA_SZ,
114c6fd2807SJeff Garzik 
115c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH		= 32,
116c6fd2807SJeff Garzik 	MV_MAX_Q_DEPTH_MASK	= MV_MAX_Q_DEPTH - 1,
117c6fd2807SJeff Garzik 
118c6fd2807SJeff Garzik 	/* CRQB needs alignment on a 1KB boundary. Size == 1KB
119c6fd2807SJeff Garzik 	 * CRPB needs alignment on a 256B boundary. Size == 256B
120c6fd2807SJeff Garzik 	 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
121c6fd2807SJeff Garzik 	 */
122c6fd2807SJeff Garzik 	MV_CRQB_Q_SZ		= (32 * MV_MAX_Q_DEPTH),
123c6fd2807SJeff Garzik 	MV_CRPB_Q_SZ		= (8 * MV_MAX_Q_DEPTH),
124da2fa9baSMark Lord 	MV_MAX_SG_CT		= 256,
125c6fd2807SJeff Garzik 	MV_SG_TBL_SZ		= (16 * MV_MAX_SG_CT),
126c6fd2807SJeff Garzik 
127*352fab70SMark Lord 	/* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
128c6fd2807SJeff Garzik 	MV_PORT_HC_SHIFT	= 2,
129*352fab70SMark Lord 	MV_PORTS_PER_HC		= (1 << MV_PORT_HC_SHIFT), /* 4 */
130*352fab70SMark Lord 	/* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
131*352fab70SMark Lord 	MV_PORT_MASK		= (MV_PORTS_PER_HC - 1),   /* 3 */
132c6fd2807SJeff Garzik 
133c6fd2807SJeff Garzik 	/* Host Flags */
134c6fd2807SJeff Garzik 	MV_FLAG_DUAL_HC		= (1 << 30),  /* two SATA Host Controllers */
135c6fd2807SJeff Garzik 	MV_FLAG_IRQ_COALESCE	= (1 << 29),  /* IRQ coalescing capability */
1367bb3c529SSaeed Bishara 	/* SoC integrated controllers, no PCI interface */
1377bb3c529SSaeed Bishara 	MV_FLAG_SOC		= (1 << 28),
1387bb3c529SSaeed Bishara 
139c5d3e45aSJeff Garzik 	MV_COMMON_FLAGS		= ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
140bdd4dddeSJeff Garzik 				  ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
141bdd4dddeSJeff Garzik 				  ATA_FLAG_PIO_POLLING,
142c6fd2807SJeff Garzik 	MV_6XXX_FLAGS		= MV_FLAG_IRQ_COALESCE,
143c6fd2807SJeff Garzik 
144c6fd2807SJeff Garzik 	CRQB_FLAG_READ		= (1 << 0),
145c6fd2807SJeff Garzik 	CRQB_TAG_SHIFT		= 1,
146c5d3e45aSJeff Garzik 	CRQB_IOID_SHIFT		= 6,	/* CRQB Gen-II/IIE IO Id shift */
147e12bef50SMark Lord 	CRQB_PMP_SHIFT		= 12,	/* CRQB Gen-II/IIE PMP shift */
148c5d3e45aSJeff Garzik 	CRQB_HOSTQ_SHIFT	= 17,	/* CRQB Gen-II/IIE HostQueTag shift */
149c6fd2807SJeff Garzik 	CRQB_CMD_ADDR_SHIFT	= 8,
150c6fd2807SJeff Garzik 	CRQB_CMD_CS		= (0x2 << 11),
151c6fd2807SJeff Garzik 	CRQB_CMD_LAST		= (1 << 15),
152c6fd2807SJeff Garzik 
153c6fd2807SJeff Garzik 	CRPB_FLAG_STATUS_SHIFT	= 8,
154c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_6	= 5,	/* CRPB Gen-II IO Id shift */
155c5d3e45aSJeff Garzik 	CRPB_IOID_SHIFT_7	= 7,	/* CRPB Gen-IIE IO Id shift */
156c6fd2807SJeff Garzik 
157c6fd2807SJeff Garzik 	EPRD_FLAG_END_OF_TBL	= (1 << 31),
158c6fd2807SJeff Garzik 
159c6fd2807SJeff Garzik 	/* PCI interface registers */
160c6fd2807SJeff Garzik 
161c6fd2807SJeff Garzik 	PCI_COMMAND_OFS		= 0xc00,
162c6fd2807SJeff Garzik 
163c6fd2807SJeff Garzik 	PCI_MAIN_CMD_STS_OFS	= 0xd30,
164c6fd2807SJeff Garzik 	STOP_PCI_MASTER		= (1 << 2),
165c6fd2807SJeff Garzik 	PCI_MASTER_EMPTY	= (1 << 3),
166c6fd2807SJeff Garzik 	GLOB_SFT_RST		= (1 << 4),
167c6fd2807SJeff Garzik 
168c6fd2807SJeff Garzik 	MV_PCI_MODE		= 0xd00,
169c6fd2807SJeff Garzik 	MV_PCI_EXP_ROM_BAR_CTL	= 0xd2c,
170c6fd2807SJeff Garzik 	MV_PCI_DISC_TIMER	= 0xd04,
171c6fd2807SJeff Garzik 	MV_PCI_MSI_TRIGGER	= 0xc38,
172c6fd2807SJeff Garzik 	MV_PCI_SERR_MASK	= 0xc28,
173c6fd2807SJeff Garzik 	MV_PCI_XBAR_TMOUT	= 0x1d04,
174c6fd2807SJeff Garzik 	MV_PCI_ERR_LOW_ADDRESS	= 0x1d40,
175c6fd2807SJeff Garzik 	MV_PCI_ERR_HIGH_ADDRESS	= 0x1d44,
176c6fd2807SJeff Garzik 	MV_PCI_ERR_ATTRIBUTE	= 0x1d48,
177c6fd2807SJeff Garzik 	MV_PCI_ERR_COMMAND	= 0x1d50,
178c6fd2807SJeff Garzik 
179c6fd2807SJeff Garzik 	PCI_IRQ_CAUSE_OFS	= 0x1d58,
180c6fd2807SJeff Garzik 	PCI_IRQ_MASK_OFS	= 0x1d5c,
181c6fd2807SJeff Garzik 	PCI_UNMASK_ALL_IRQS	= 0x7fffff,	/* bits 22-0 */
182c6fd2807SJeff Garzik 
18302a121daSMark Lord 	PCIE_IRQ_CAUSE_OFS	= 0x1900,
18402a121daSMark Lord 	PCIE_IRQ_MASK_OFS	= 0x1910,
185646a4da5SMark Lord 	PCIE_UNMASK_ALL_IRQS	= 0x40a,	/* assorted bits */
18602a121daSMark Lord 
187c6fd2807SJeff Garzik 	HC_MAIN_IRQ_CAUSE_OFS	= 0x1d60,
188c6fd2807SJeff Garzik 	HC_MAIN_IRQ_MASK_OFS	= 0x1d64,
189f351b2d6SSaeed Bishara 	HC_SOC_MAIN_IRQ_CAUSE_OFS = 0x20020,
190f351b2d6SSaeed Bishara 	HC_SOC_MAIN_IRQ_MASK_OFS = 0x20024,
191*352fab70SMark Lord 	ERR_IRQ			= (1 << 0),	/* shift by port # */
192*352fab70SMark Lord 	DONE_IRQ		= (1 << 1),	/* shift by port # */
193c6fd2807SJeff Garzik 	HC0_IRQ_PEND		= 0x1ff,	/* bits 0-8 = HC0's ports */
194c6fd2807SJeff Garzik 	HC_SHIFT		= 9,		/* bits 9-17 = HC1's ports */
195c6fd2807SJeff Garzik 	PCI_ERR			= (1 << 18),
196c6fd2807SJeff Garzik 	TRAN_LO_DONE		= (1 << 19),	/* 6xxx: IRQ coalescing */
197c6fd2807SJeff Garzik 	TRAN_HI_DONE		= (1 << 20),	/* 6xxx: IRQ coalescing */
198fb621e2fSJeff Garzik 	PORTS_0_3_COAL_DONE	= (1 << 8),
199fb621e2fSJeff Garzik 	PORTS_4_7_COAL_DONE	= (1 << 17),
200c6fd2807SJeff Garzik 	PORTS_0_7_COAL_DONE	= (1 << 21),	/* 6xxx: IRQ coalescing */
201c6fd2807SJeff Garzik 	GPIO_INT		= (1 << 22),
202c6fd2807SJeff Garzik 	SELF_INT		= (1 << 23),
203c6fd2807SJeff Garzik 	TWSI_INT		= (1 << 24),
204c6fd2807SJeff Garzik 	HC_MAIN_RSVD		= (0x7f << 25),	/* bits 31-25 */
205fb621e2fSJeff Garzik 	HC_MAIN_RSVD_5		= (0x1fff << 19), /* bits 31-19 */
206f351b2d6SSaeed Bishara 	HC_MAIN_RSVD_SOC	= (0x3fffffb << 6),     /* bits 31-9, 7-6 */
207c6fd2807SJeff Garzik 	HC_MAIN_MASKED_IRQS	= (TRAN_LO_DONE | TRAN_HI_DONE |
208c6fd2807SJeff Garzik 				   PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
209c6fd2807SJeff Garzik 				   HC_MAIN_RSVD),
210fb621e2fSJeff Garzik 	HC_MAIN_MASKED_IRQS_5	= (PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
211fb621e2fSJeff Garzik 				   HC_MAIN_RSVD_5),
212f351b2d6SSaeed Bishara 	HC_MAIN_MASKED_IRQS_SOC = (PORTS_0_3_COAL_DONE | HC_MAIN_RSVD_SOC),
213c6fd2807SJeff Garzik 
214c6fd2807SJeff Garzik 	/* SATAHC registers */
215c6fd2807SJeff Garzik 	HC_CFG_OFS		= 0,
216c6fd2807SJeff Garzik 
217c6fd2807SJeff Garzik 	HC_IRQ_CAUSE_OFS	= 0x14,
218*352fab70SMark Lord 	DMA_IRQ			= (1 << 0),	/* shift by port # */
219*352fab70SMark Lord 	HC_COAL_IRQ		= (1 << 4),	/* IRQ coalescing */
220c6fd2807SJeff Garzik 	DEV_IRQ			= (1 << 8),	/* shift by port # */
221c6fd2807SJeff Garzik 
222c6fd2807SJeff Garzik 	/* Shadow block registers */
223c6fd2807SJeff Garzik 	SHD_BLK_OFS		= 0x100,
224c6fd2807SJeff Garzik 	SHD_CTL_AST_OFS		= 0x20,		/* ofs from SHD_BLK_OFS */
225c6fd2807SJeff Garzik 
226c6fd2807SJeff Garzik 	/* SATA registers */
227c6fd2807SJeff Garzik 	SATA_STATUS_OFS		= 0x300,  /* ctrl, err regs follow status */
228c6fd2807SJeff Garzik 	SATA_ACTIVE_OFS		= 0x350,
2290c58912eSMark Lord 	SATA_FIS_IRQ_CAUSE_OFS	= 0x364,
23017c5aab5SMark Lord 
231e12bef50SMark Lord 	LTMODE_OFS		= 0x30c,
23217c5aab5SMark Lord 	LTMODE_BIT8		= (1 << 8),	/* unknown, but necessary */
23317c5aab5SMark Lord 
234c6fd2807SJeff Garzik 	PHY_MODE3		= 0x310,
235c6fd2807SJeff Garzik 	PHY_MODE4		= 0x314,
236c6fd2807SJeff Garzik 	PHY_MODE2		= 0x330,
237e12bef50SMark Lord 	SATA_IFCTL_OFS		= 0x344,
238e12bef50SMark Lord 	SATA_IFSTAT_OFS		= 0x34c,
239e12bef50SMark Lord 	VENDOR_UNIQUE_FIS_OFS	= 0x35c,
24017c5aab5SMark Lord 
241e12bef50SMark Lord 	FIS_CFG_OFS		= 0x360,
24217c5aab5SMark Lord 	FIS_CFG_SINGLE_SYNC	= (1 << 16),	/* SYNC on DMA activation */
24317c5aab5SMark Lord 
244c6fd2807SJeff Garzik 	MV5_PHY_MODE		= 0x74,
245c6fd2807SJeff Garzik 	MV5_LT_MODE		= 0x30,
246c6fd2807SJeff Garzik 	MV5_PHY_CTL		= 0x0C,
247e12bef50SMark Lord 	SATA_INTERFACE_CFG	= 0x050,
248c6fd2807SJeff Garzik 
249c6fd2807SJeff Garzik 	MV_M2_PREAMP_MASK	= 0x7e0,
250c6fd2807SJeff Garzik 
251c6fd2807SJeff Garzik 	/* Port registers */
252c6fd2807SJeff Garzik 	EDMA_CFG_OFS		= 0,
2530c58912eSMark Lord 	EDMA_CFG_Q_DEPTH	= 0x1f,		/* max device queue depth */
2540c58912eSMark Lord 	EDMA_CFG_NCQ		= (1 << 5),	/* for R/W FPDMA queued */
255c6fd2807SJeff Garzik 	EDMA_CFG_NCQ_GO_ON_ERR	= (1 << 14),	/* continue on error */
256c6fd2807SJeff Garzik 	EDMA_CFG_RD_BRST_EXT	= (1 << 11),	/* read burst 512B */
257c6fd2807SJeff Garzik 	EDMA_CFG_WR_BUFF_LEN	= (1 << 13),	/* write buffer 512B */
258e12bef50SMark Lord 	EDMA_CFG_EDMA_FBS	= (1 << 16),	/* EDMA FIS-Based Switching */
259e12bef50SMark Lord 	EDMA_CFG_FBS		= (1 << 26),	/* FIS-Based Switching */
260c6fd2807SJeff Garzik 
261c6fd2807SJeff Garzik 	EDMA_ERR_IRQ_CAUSE_OFS	= 0x8,
262c6fd2807SJeff Garzik 	EDMA_ERR_IRQ_MASK_OFS	= 0xc,
2636c1153e0SJeff Garzik 	EDMA_ERR_D_PAR		= (1 << 0),	/* UDMA data parity err */
2646c1153e0SJeff Garzik 	EDMA_ERR_PRD_PAR	= (1 << 1),	/* UDMA PRD parity err */
2656c1153e0SJeff Garzik 	EDMA_ERR_DEV		= (1 << 2),	/* device error */
2666c1153e0SJeff Garzik 	EDMA_ERR_DEV_DCON	= (1 << 3),	/* device disconnect */
2676c1153e0SJeff Garzik 	EDMA_ERR_DEV_CON	= (1 << 4),	/* device connected */
2686c1153e0SJeff Garzik 	EDMA_ERR_SERR		= (1 << 5),	/* SError bits [WBDST] raised */
269c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS	= (1 << 7),	/* Gen II/IIE self-disable */
270c5d3e45aSJeff Garzik 	EDMA_ERR_SELF_DIS_5	= (1 << 8),	/* Gen I self-disable */
2716c1153e0SJeff Garzik 	EDMA_ERR_BIST_ASYNC	= (1 << 8),	/* BIST FIS or Async Notify */
272c5d3e45aSJeff Garzik 	EDMA_ERR_TRANS_IRQ_7	= (1 << 8),	/* Gen IIE transprt layer irq */
2736c1153e0SJeff Garzik 	EDMA_ERR_CRQB_PAR	= (1 << 9),	/* CRQB parity error */
2746c1153e0SJeff Garzik 	EDMA_ERR_CRPB_PAR	= (1 << 10),	/* CRPB parity error */
2756c1153e0SJeff Garzik 	EDMA_ERR_INTRL_PAR	= (1 << 11),	/* internal parity error */
2766c1153e0SJeff Garzik 	EDMA_ERR_IORDY		= (1 << 12),	/* IORdy timeout */
277646a4da5SMark Lord 
2786c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_RX	= (0xf << 13),	/* link ctrl rx error */
279646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_0	= (1 << 13),	/* transient: CRC err */
280646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_1	= (1 << 14),	/* transient: FIFO err */
281646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_2	= (1 << 15),	/* fatal: caught SYNC */
282646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_RX_3	= (1 << 16),	/* transient: FIS rx err */
283646a4da5SMark Lord 
2846c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_RX	= (0xf << 17),	/* link data rx error */
285646a4da5SMark Lord 
2866c1153e0SJeff Garzik 	EDMA_ERR_LNK_CTRL_TX	= (0x1f << 21),	/* link ctrl tx error */
287646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_0	= (1 << 21),	/* transient: CRC err */
288646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_1	= (1 << 22),	/* transient: FIFO err */
289646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_2	= (1 << 23),	/* transient: caught SYNC */
290646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_3	= (1 << 24),	/* transient: caught DMAT */
291646a4da5SMark Lord 	EDMA_ERR_LNK_CTRL_TX_4	= (1 << 25),	/* transient: FIS collision */
292646a4da5SMark Lord 
2936c1153e0SJeff Garzik 	EDMA_ERR_LNK_DATA_TX	= (0x1f << 26),	/* link data tx error */
294646a4da5SMark Lord 
2956c1153e0SJeff Garzik 	EDMA_ERR_TRANS_PROTO	= (1 << 31),	/* transport protocol error */
296c5d3e45aSJeff Garzik 	EDMA_ERR_OVERRUN_5	= (1 << 5),
297c5d3e45aSJeff Garzik 	EDMA_ERR_UNDERRUN_5	= (1 << 6),
298646a4da5SMark Lord 
299646a4da5SMark Lord 	EDMA_ERR_IRQ_TRANSIENT  = EDMA_ERR_LNK_CTRL_RX_0 |
300646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_1 |
301646a4da5SMark Lord 				  EDMA_ERR_LNK_CTRL_RX_3 |
30240f0bc2dSMark Lord 				  EDMA_ERR_LNK_CTRL_TX |
30340f0bc2dSMark Lord 				 /* temporary, until we fix hotplug: */
30440f0bc2dSMark Lord 				 (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON),
305646a4da5SMark Lord 
306bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE		= EDMA_ERR_D_PAR |
307bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
308bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
309bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
310bdd4dddeSJeff Garzik 				  EDMA_ERR_SERR |
311bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS |
3126c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
313bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
314bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
315bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY |
316bdd4dddeSJeff Garzik 				  EDMA_ERR_LNK_CTRL_RX_2 |
317c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_RX |
318c6fd2807SJeff Garzik 				  EDMA_ERR_LNK_DATA_TX |
319bdd4dddeSJeff Garzik 				  EDMA_ERR_TRANS_PROTO,
320e12bef50SMark Lord 
321bdd4dddeSJeff Garzik 	EDMA_EH_FREEZE_5	= EDMA_ERR_D_PAR |
322bdd4dddeSJeff Garzik 				  EDMA_ERR_PRD_PAR |
323bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_DCON |
324bdd4dddeSJeff Garzik 				  EDMA_ERR_DEV_CON |
325bdd4dddeSJeff Garzik 				  EDMA_ERR_OVERRUN_5 |
326bdd4dddeSJeff Garzik 				  EDMA_ERR_UNDERRUN_5 |
327bdd4dddeSJeff Garzik 				  EDMA_ERR_SELF_DIS_5 |
3286c1153e0SJeff Garzik 				  EDMA_ERR_CRQB_PAR |
329bdd4dddeSJeff Garzik 				  EDMA_ERR_CRPB_PAR |
330bdd4dddeSJeff Garzik 				  EDMA_ERR_INTRL_PAR |
331bdd4dddeSJeff Garzik 				  EDMA_ERR_IORDY,
332c6fd2807SJeff Garzik 
333c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_HI_OFS	= 0x10,
334c6fd2807SJeff Garzik 	EDMA_REQ_Q_IN_PTR_OFS	= 0x14,		/* also contains BASE_LO */
335c6fd2807SJeff Garzik 
336c6fd2807SJeff Garzik 	EDMA_REQ_Q_OUT_PTR_OFS	= 0x18,
337c6fd2807SJeff Garzik 	EDMA_REQ_Q_PTR_SHIFT	= 5,
338c6fd2807SJeff Garzik 
339c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_HI_OFS	= 0x1c,
340c6fd2807SJeff Garzik 	EDMA_RSP_Q_IN_PTR_OFS	= 0x20,
341c6fd2807SJeff Garzik 	EDMA_RSP_Q_OUT_PTR_OFS	= 0x24,		/* also contains BASE_LO */
342c6fd2807SJeff Garzik 	EDMA_RSP_Q_PTR_SHIFT	= 3,
343c6fd2807SJeff Garzik 
3440ea9e179SJeff Garzik 	EDMA_CMD_OFS		= 0x28,		/* EDMA command register */
3450ea9e179SJeff Garzik 	EDMA_EN			= (1 << 0),	/* enable EDMA */
3460ea9e179SJeff Garzik 	EDMA_DS			= (1 << 1),	/* disable EDMA; self-negated */
3470ea9e179SJeff Garzik 	ATA_RST			= (1 << 2),	/* reset trans/link/phy */
348c6fd2807SJeff Garzik 
349c6fd2807SJeff Garzik 	EDMA_IORDY_TMOUT	= 0x34,
350c6fd2807SJeff Garzik 	EDMA_ARB_CFG		= 0x38,
351c6fd2807SJeff Garzik 
352*352fab70SMark Lord 	GEN_II_NCQ_MAX_SECTORS	= 256,		/* max sects/io on Gen2 w/NCQ */
353*352fab70SMark Lord 
354c6fd2807SJeff Garzik 	/* Host private flags (hp_flags) */
355c6fd2807SJeff Garzik 	MV_HP_FLAG_MSI		= (1 << 0),
356c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB0	= (1 << 1),
357c6fd2807SJeff Garzik 	MV_HP_ERRATA_50XXB2	= (1 << 2),
358c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1B2	= (1 << 3),
359c6fd2807SJeff Garzik 	MV_HP_ERRATA_60X1C0	= (1 << 4),
360c6fd2807SJeff Garzik 	MV_HP_ERRATA_XX42A0	= (1 << 5),
3610ea9e179SJeff Garzik 	MV_HP_GEN_I		= (1 << 6),	/* Generation I: 50xx */
3620ea9e179SJeff Garzik 	MV_HP_GEN_II		= (1 << 7),	/* Generation II: 60xx */
3630ea9e179SJeff Garzik 	MV_HP_GEN_IIE		= (1 << 8),	/* Generation IIE: 6042/7042 */
36402a121daSMark Lord 	MV_HP_PCIE		= (1 << 9),	/* PCIe bus/regs: 7042 */
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)
3747bb3c529SSaeed Bishara #define HAS_PCI(host) (!((host)->ports[0]->flags & MV_FLAG_SOC))
375c6fd2807SJeff Garzik 
37615a32632SLennert Buytenhek #define WINDOW_CTRL(i)		(0x20030 + ((i) << 4))
37715a32632SLennert Buytenhek #define WINDOW_BASE(i)		(0x20034 + ((i) << 4))
37815a32632SLennert Buytenhek 
379c6fd2807SJeff Garzik enum {
380baf14aa1SJeff Garzik 	/* DMA boundary 0xffff is required by the s/g splitting
381baf14aa1SJeff Garzik 	 * we need on /length/ in mv_fill-sg().
382baf14aa1SJeff Garzik 	 */
383baf14aa1SJeff Garzik 	MV_DMA_BOUNDARY		= 0xffffU,
384c6fd2807SJeff Garzik 
3850ea9e179SJeff Garzik 	/* mask of register bits containing lower 32 bits
3860ea9e179SJeff Garzik 	 * of EDMA request queue DMA address
3870ea9e179SJeff Garzik 	 */
388c6fd2807SJeff Garzik 	EDMA_REQ_Q_BASE_LO_MASK	= 0xfffffc00U,
389c6fd2807SJeff Garzik 
3900ea9e179SJeff Garzik 	/* ditto, for response queue */
391c6fd2807SJeff Garzik 	EDMA_RSP_Q_BASE_LO_MASK	= 0xffffff00U,
392c6fd2807SJeff Garzik };
393c6fd2807SJeff Garzik 
394c6fd2807SJeff Garzik enum chip_type {
395c6fd2807SJeff Garzik 	chip_504x,
396c6fd2807SJeff Garzik 	chip_508x,
397c6fd2807SJeff Garzik 	chip_5080,
398c6fd2807SJeff Garzik 	chip_604x,
399c6fd2807SJeff Garzik 	chip_608x,
400c6fd2807SJeff Garzik 	chip_6042,
401c6fd2807SJeff Garzik 	chip_7042,
402f351b2d6SSaeed Bishara 	chip_soc,
403c6fd2807SJeff Garzik };
404c6fd2807SJeff Garzik 
405c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
406c6fd2807SJeff Garzik struct mv_crqb {
407c6fd2807SJeff Garzik 	__le32			sg_addr;
408c6fd2807SJeff Garzik 	__le32			sg_addr_hi;
409c6fd2807SJeff Garzik 	__le16			ctrl_flags;
410c6fd2807SJeff Garzik 	__le16			ata_cmd[11];
411c6fd2807SJeff Garzik };
412c6fd2807SJeff Garzik 
413c6fd2807SJeff Garzik struct mv_crqb_iie {
414c6fd2807SJeff Garzik 	__le32			addr;
415c6fd2807SJeff Garzik 	__le32			addr_hi;
416c6fd2807SJeff Garzik 	__le32			flags;
417c6fd2807SJeff Garzik 	__le32			len;
418c6fd2807SJeff Garzik 	__le32			ata_cmd[4];
419c6fd2807SJeff Garzik };
420c6fd2807SJeff Garzik 
421c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
422c6fd2807SJeff Garzik struct mv_crpb {
423c6fd2807SJeff Garzik 	__le16			id;
424c6fd2807SJeff Garzik 	__le16			flags;
425c6fd2807SJeff Garzik 	__le32			tmstmp;
426c6fd2807SJeff Garzik };
427c6fd2807SJeff Garzik 
428c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
429c6fd2807SJeff Garzik struct mv_sg {
430c6fd2807SJeff Garzik 	__le32			addr;
431c6fd2807SJeff Garzik 	__le32			flags_size;
432c6fd2807SJeff Garzik 	__le32			addr_hi;
433c6fd2807SJeff Garzik 	__le32			reserved;
434c6fd2807SJeff Garzik };
435c6fd2807SJeff Garzik 
436c6fd2807SJeff Garzik struct mv_port_priv {
437c6fd2807SJeff Garzik 	struct mv_crqb		*crqb;
438c6fd2807SJeff Garzik 	dma_addr_t		crqb_dma;
439c6fd2807SJeff Garzik 	struct mv_crpb		*crpb;
440c6fd2807SJeff Garzik 	dma_addr_t		crpb_dma;
441eb73d558SMark Lord 	struct mv_sg		*sg_tbl[MV_MAX_Q_DEPTH];
442eb73d558SMark Lord 	dma_addr_t		sg_tbl_dma[MV_MAX_Q_DEPTH];
443bdd4dddeSJeff Garzik 
444bdd4dddeSJeff Garzik 	unsigned int		req_idx;
445bdd4dddeSJeff Garzik 	unsigned int		resp_idx;
446bdd4dddeSJeff Garzik 
447c6fd2807SJeff Garzik 	u32			pp_flags;
448c6fd2807SJeff Garzik };
449c6fd2807SJeff Garzik 
450c6fd2807SJeff Garzik struct mv_port_signal {
451c6fd2807SJeff Garzik 	u32			amps;
452c6fd2807SJeff Garzik 	u32			pre;
453c6fd2807SJeff Garzik };
454c6fd2807SJeff Garzik 
45502a121daSMark Lord struct mv_host_priv {
45602a121daSMark Lord 	u32			hp_flags;
45702a121daSMark Lord 	struct mv_port_signal	signal[8];
45802a121daSMark Lord 	const struct mv_hw_ops	*ops;
459f351b2d6SSaeed Bishara 	int			n_ports;
460f351b2d6SSaeed Bishara 	void __iomem		*base;
461f351b2d6SSaeed Bishara 	void __iomem		*main_cause_reg_addr;
462f351b2d6SSaeed Bishara 	void __iomem		*main_mask_reg_addr;
46302a121daSMark Lord 	u32			irq_cause_ofs;
46402a121daSMark Lord 	u32			irq_mask_ofs;
46502a121daSMark Lord 	u32			unmask_all_irqs;
466da2fa9baSMark Lord 	/*
467da2fa9baSMark Lord 	 * These consistent DMA memory pools give us guaranteed
468da2fa9baSMark Lord 	 * alignment for hardware-accessed data structures,
469da2fa9baSMark Lord 	 * and less memory waste in accomplishing the alignment.
470da2fa9baSMark Lord 	 */
471da2fa9baSMark Lord 	struct dma_pool		*crqb_pool;
472da2fa9baSMark Lord 	struct dma_pool		*crpb_pool;
473da2fa9baSMark Lord 	struct dma_pool		*sg_tbl_pool;
47402a121daSMark Lord };
47502a121daSMark Lord 
476c6fd2807SJeff Garzik struct mv_hw_ops {
477c6fd2807SJeff Garzik 	void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
478c6fd2807SJeff Garzik 			   unsigned int port);
479c6fd2807SJeff Garzik 	void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
480c6fd2807SJeff Garzik 	void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
481c6fd2807SJeff Garzik 			   void __iomem *mmio);
482c6fd2807SJeff Garzik 	int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
483c6fd2807SJeff Garzik 			unsigned int n_hc);
484c6fd2807SJeff Garzik 	void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
4857bb3c529SSaeed Bishara 	void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
486c6fd2807SJeff Garzik };
487c6fd2807SJeff Garzik 
488da3dbb17STejun Heo static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
489da3dbb17STejun Heo static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
490da3dbb17STejun Heo static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
491da3dbb17STejun Heo static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
492c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap);
493c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap);
494c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc);
495c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
496c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
497a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
498a1efdabaSTejun Heo 			unsigned long deadline);
499bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap);
500bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap);
501f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev);
502c6fd2807SJeff Garzik 
503c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
504c6fd2807SJeff Garzik 			   unsigned int port);
505c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
506c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
507c6fd2807SJeff Garzik 			   void __iomem *mmio);
508c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
509c6fd2807SJeff Garzik 			unsigned int n_hc);
510c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
5117bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
512c6fd2807SJeff Garzik 
513c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
514c6fd2807SJeff Garzik 			   unsigned int port);
515c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
516c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
517c6fd2807SJeff Garzik 			   void __iomem *mmio);
518c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
519c6fd2807SJeff Garzik 			unsigned int n_hc);
520c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
521f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
522f351b2d6SSaeed Bishara 				      void __iomem *mmio);
523f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
524f351b2d6SSaeed Bishara 				      void __iomem *mmio);
525f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
526f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc);
527f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
528f351b2d6SSaeed Bishara 				      void __iomem *mmio);
529f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
5307bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
531e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
532c6fd2807SJeff Garzik 			     unsigned int port_no);
533e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap);
534b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio);
535e12bef50SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq);
536c6fd2807SJeff Garzik 
537e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp);
538e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
539e49856d8SMark Lord 				unsigned long deadline);
540e49856d8SMark Lord static int  mv_softreset(struct ata_link *link, unsigned int *class,
541e49856d8SMark Lord 				unsigned long deadline);
542c6fd2807SJeff Garzik 
543eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
544eb73d558SMark Lord  * because we have to allow room for worst case splitting of
545eb73d558SMark Lord  * PRDs for 64K boundaries in mv_fill_sg().
546eb73d558SMark Lord  */
547c5d3e45aSJeff Garzik static struct scsi_host_template mv5_sht = {
54868d1d07bSTejun Heo 	ATA_BASE_SHT(DRV_NAME),
549baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
550c5d3e45aSJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
551c5d3e45aSJeff Garzik };
552c5d3e45aSJeff Garzik 
553c5d3e45aSJeff Garzik static struct scsi_host_template mv6_sht = {
55468d1d07bSTejun Heo 	ATA_NCQ_SHT(DRV_NAME),
555138bfdd0SMark Lord 	.can_queue		= MV_MAX_Q_DEPTH - 1,
556baf14aa1SJeff Garzik 	.sg_tablesize		= MV_MAX_SG_CT / 2,
557c6fd2807SJeff Garzik 	.dma_boundary		= MV_DMA_BOUNDARY,
558c6fd2807SJeff Garzik };
559c6fd2807SJeff Garzik 
560029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = {
561029cfd6bSTejun Heo 	.inherits		= &ata_sff_port_ops,
562c6fd2807SJeff Garzik 
563c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep,
564c6fd2807SJeff Garzik 	.qc_issue		= mv_qc_issue,
565c6fd2807SJeff Garzik 
566bdd4dddeSJeff Garzik 	.freeze			= mv_eh_freeze,
567bdd4dddeSJeff Garzik 	.thaw			= mv_eh_thaw,
568a1efdabaSTejun Heo 	.hardreset		= mv_hardreset,
569a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler, /* avoid SFF EH */
570029cfd6bSTejun Heo 	.post_internal_cmd	= ATA_OP_NULL,
571bdd4dddeSJeff Garzik 
572c6fd2807SJeff Garzik 	.scr_read		= mv5_scr_read,
573c6fd2807SJeff Garzik 	.scr_write		= mv5_scr_write,
574c6fd2807SJeff Garzik 
575c6fd2807SJeff Garzik 	.port_start		= mv_port_start,
576c6fd2807SJeff Garzik 	.port_stop		= mv_port_stop,
577c6fd2807SJeff Garzik };
578c6fd2807SJeff Garzik 
579029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
580029cfd6bSTejun Heo 	.inherits		= &mv5_ops,
581e49856d8SMark Lord 	.qc_defer		= sata_pmp_qc_defer_cmd_switch,
582f273827eSMark Lord 	.dev_config             = mv6_dev_config,
583c6fd2807SJeff Garzik 	.scr_read		= mv_scr_read,
584c6fd2807SJeff Garzik 	.scr_write		= mv_scr_write,
585c6fd2807SJeff Garzik 
586e49856d8SMark Lord 	.pmp_hardreset		= mv_pmp_hardreset,
587e49856d8SMark Lord 	.pmp_softreset		= mv_softreset,
588e49856d8SMark Lord 	.softreset		= mv_softreset,
589e49856d8SMark Lord 	.error_handler		= sata_pmp_error_handler,
590c6fd2807SJeff Garzik };
591c6fd2807SJeff Garzik 
592029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
593029cfd6bSTejun Heo 	.inherits		= &mv6_ops,
594e49856d8SMark Lord 	.qc_defer		= ata_std_qc_defer, /* FIS-based switching */
595029cfd6bSTejun Heo 	.dev_config		= ATA_OP_NULL,
596c6fd2807SJeff Garzik 	.qc_prep		= mv_qc_prep_iie,
597c6fd2807SJeff Garzik };
598c6fd2807SJeff Garzik 
599c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
600c6fd2807SJeff Garzik 	{  /* chip_504x */
601cca3974eSJeff Garzik 		.flags		= MV_COMMON_FLAGS,
602c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
603bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
604c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
605c6fd2807SJeff Garzik 	},
606c6fd2807SJeff Garzik 	{  /* chip_508x */
607c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
608c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
609bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
610c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
611c6fd2807SJeff Garzik 	},
612c6fd2807SJeff Garzik 	{  /* chip_5080 */
613c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
614c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
615bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
616c6fd2807SJeff Garzik 		.port_ops	= &mv5_ops,
617c6fd2807SJeff Garzik 	},
618c6fd2807SJeff Garzik 	{  /* chip_604x */
619138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
620e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
621138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
622c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
623bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
624c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
625c6fd2807SJeff Garzik 	},
626c6fd2807SJeff Garzik 	{  /* chip_608x */
627c5d3e45aSJeff Garzik 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
628e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
629138bfdd0SMark Lord 				  ATA_FLAG_NCQ | MV_FLAG_DUAL_HC,
630c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
631bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
632c6fd2807SJeff Garzik 		.port_ops	= &mv6_ops,
633c6fd2807SJeff Garzik 	},
634c6fd2807SJeff Garzik 	{  /* chip_6042 */
635138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
636e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
637138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
638c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
639bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
640c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
641c6fd2807SJeff Garzik 	},
642c6fd2807SJeff Garzik 	{  /* chip_7042 */
643138bfdd0SMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
644e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
645138bfdd0SMark Lord 				  ATA_FLAG_NCQ,
646c6fd2807SJeff Garzik 		.pio_mask	= 0x1f,	/* pio0-4 */
647bf6263a8SJeff Garzik 		.udma_mask	= ATA_UDMA6,
648c6fd2807SJeff Garzik 		.port_ops	= &mv_iie_ops,
649c6fd2807SJeff Garzik 	},
650f351b2d6SSaeed Bishara 	{  /* chip_soc */
65102c1f32fSMark Lord 		.flags		= MV_COMMON_FLAGS | MV_6XXX_FLAGS |
652e49856d8SMark Lord 				  ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
65302c1f32fSMark Lord 				  ATA_FLAG_NCQ | MV_FLAG_SOC,
654f351b2d6SSaeed Bishara 		.pio_mask	= 0x1f,	/* pio0-4 */
655f351b2d6SSaeed Bishara 		.udma_mask	= ATA_UDMA6,
656f351b2d6SSaeed Bishara 		.port_ops	= &mv_iie_ops,
657f351b2d6SSaeed Bishara 	},
658c6fd2807SJeff Garzik };
659c6fd2807SJeff Garzik 
660c6fd2807SJeff Garzik static const struct pci_device_id mv_pci_tbl[] = {
6612d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
6622d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
6632d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
6642d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
665cfbf723eSAlan Cox 	/* RocketRAID 1740/174x have different identifiers */
666cfbf723eSAlan Cox 	{ PCI_VDEVICE(TTI, 0x1740), chip_508x },
667cfbf723eSAlan Cox 	{ PCI_VDEVICE(TTI, 0x1742), chip_508x },
668c6fd2807SJeff Garzik 
6692d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
6702d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
6712d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
6722d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
6732d2744fcSJeff Garzik 	{ PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
674c6fd2807SJeff Garzik 
6752d2744fcSJeff Garzik 	{ PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
6762d2744fcSJeff Garzik 
677d9f9c6bcSFlorian Attenberger 	/* Adaptec 1430SA */
678d9f9c6bcSFlorian Attenberger 	{ PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
679d9f9c6bcSFlorian Attenberger 
68002a121daSMark Lord 	/* Marvell 7042 support */
6816a3d586dSMorrison, Tom 	{ PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
6826a3d586dSMorrison, Tom 
68302a121daSMark Lord 	/* Highpoint RocketRAID PCIe series */
68402a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2300), chip_7042 },
68502a121daSMark Lord 	{ PCI_VDEVICE(TTI, 0x2310), chip_7042 },
68602a121daSMark Lord 
687c6fd2807SJeff Garzik 	{ }			/* terminate list */
688c6fd2807SJeff Garzik };
689c6fd2807SJeff Garzik 
690c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
691c6fd2807SJeff Garzik 	.phy_errata		= mv5_phy_errata,
692c6fd2807SJeff Garzik 	.enable_leds		= mv5_enable_leds,
693c6fd2807SJeff Garzik 	.read_preamp		= mv5_read_preamp,
694c6fd2807SJeff Garzik 	.reset_hc		= mv5_reset_hc,
695c6fd2807SJeff Garzik 	.reset_flash		= mv5_reset_flash,
696c6fd2807SJeff Garzik 	.reset_bus		= mv5_reset_bus,
697c6fd2807SJeff Garzik };
698c6fd2807SJeff Garzik 
699c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
700c6fd2807SJeff Garzik 	.phy_errata		= mv6_phy_errata,
701c6fd2807SJeff Garzik 	.enable_leds		= mv6_enable_leds,
702c6fd2807SJeff Garzik 	.read_preamp		= mv6_read_preamp,
703c6fd2807SJeff Garzik 	.reset_hc		= mv6_reset_hc,
704c6fd2807SJeff Garzik 	.reset_flash		= mv6_reset_flash,
705c6fd2807SJeff Garzik 	.reset_bus		= mv_reset_pci_bus,
706c6fd2807SJeff Garzik };
707c6fd2807SJeff Garzik 
708f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
709f351b2d6SSaeed Bishara 	.phy_errata		= mv6_phy_errata,
710f351b2d6SSaeed Bishara 	.enable_leds		= mv_soc_enable_leds,
711f351b2d6SSaeed Bishara 	.read_preamp		= mv_soc_read_preamp,
712f351b2d6SSaeed Bishara 	.reset_hc		= mv_soc_reset_hc,
713f351b2d6SSaeed Bishara 	.reset_flash		= mv_soc_reset_flash,
714f351b2d6SSaeed Bishara 	.reset_bus		= mv_soc_reset_bus,
715f351b2d6SSaeed Bishara };
716f351b2d6SSaeed Bishara 
717c6fd2807SJeff Garzik /*
718c6fd2807SJeff Garzik  * Functions
719c6fd2807SJeff Garzik  */
720c6fd2807SJeff Garzik 
721c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
722c6fd2807SJeff Garzik {
723c6fd2807SJeff Garzik 	writel(data, addr);
724c6fd2807SJeff Garzik 	(void) readl(addr);	/* flush to avoid PCI posted write */
725c6fd2807SJeff Garzik }
726c6fd2807SJeff Garzik 
727c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
728c6fd2807SJeff Garzik {
729c6fd2807SJeff Garzik 	return port >> MV_PORT_HC_SHIFT;
730c6fd2807SJeff Garzik }
731c6fd2807SJeff Garzik 
732c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
733c6fd2807SJeff Garzik {
734c6fd2807SJeff Garzik 	return port & MV_PORT_MASK;
735c6fd2807SJeff Garzik }
736c6fd2807SJeff Garzik 
737*352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
738*352fab70SMark Lord {
739*352fab70SMark Lord 	return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
740*352fab70SMark Lord }
741*352fab70SMark Lord 
742c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
743c6fd2807SJeff Garzik 						 unsigned int port)
744c6fd2807SJeff Garzik {
745c6fd2807SJeff Garzik 	return mv_hc_base(base, mv_hc_from_port(port));
746c6fd2807SJeff Garzik }
747c6fd2807SJeff Garzik 
748c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
749c6fd2807SJeff Garzik {
750c6fd2807SJeff Garzik 	return  mv_hc_base_from_port(base, port) +
751c6fd2807SJeff Garzik 		MV_SATAHC_ARBTR_REG_SZ +
752c6fd2807SJeff Garzik 		(mv_hardport_from_port(port) * MV_PORT_REG_SZ);
753c6fd2807SJeff Garzik }
754c6fd2807SJeff Garzik 
755e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
756e12bef50SMark Lord {
757e12bef50SMark Lord 	void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
758e12bef50SMark Lord 	unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
759e12bef50SMark Lord 
760e12bef50SMark Lord 	return hc_mmio + ofs;
761e12bef50SMark Lord }
762e12bef50SMark Lord 
763f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
764f351b2d6SSaeed Bishara {
765f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
766f351b2d6SSaeed Bishara 	return hpriv->base;
767f351b2d6SSaeed Bishara }
768f351b2d6SSaeed Bishara 
769c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
770c6fd2807SJeff Garzik {
771f351b2d6SSaeed Bishara 	return mv_port_base(mv_host_base(ap->host), ap->port_no);
772c6fd2807SJeff Garzik }
773c6fd2807SJeff Garzik 
774cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
775c6fd2807SJeff Garzik {
776cca3974eSJeff Garzik 	return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
777c6fd2807SJeff Garzik }
778c6fd2807SJeff Garzik 
779c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
780c5d3e45aSJeff Garzik 			     struct mv_host_priv *hpriv,
781c5d3e45aSJeff Garzik 			     struct mv_port_priv *pp)
782c5d3e45aSJeff Garzik {
783bdd4dddeSJeff Garzik 	u32 index;
784bdd4dddeSJeff Garzik 
785c5d3e45aSJeff Garzik 	/*
786c5d3e45aSJeff Garzik 	 * initialize request queue
787c5d3e45aSJeff Garzik 	 */
788bdd4dddeSJeff Garzik 	index = (pp->req_idx & MV_MAX_Q_DEPTH_MASK) << EDMA_REQ_Q_PTR_SHIFT;
789bdd4dddeSJeff Garzik 
790c5d3e45aSJeff Garzik 	WARN_ON(pp->crqb_dma & 0x3ff);
791c5d3e45aSJeff Garzik 	writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
792bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
793c5d3e45aSJeff Garzik 		 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
794c5d3e45aSJeff Garzik 
795c5d3e45aSJeff Garzik 	if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
796bdd4dddeSJeff Garzik 		writelfl((pp->crqb_dma & 0xffffffff) | index,
797c5d3e45aSJeff Garzik 			 port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
798c5d3e45aSJeff Garzik 	else
799bdd4dddeSJeff Garzik 		writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
800c5d3e45aSJeff Garzik 
801c5d3e45aSJeff Garzik 	/*
802c5d3e45aSJeff Garzik 	 * initialize response queue
803c5d3e45aSJeff Garzik 	 */
804bdd4dddeSJeff Garzik 	index = (pp->resp_idx & MV_MAX_Q_DEPTH_MASK) << EDMA_RSP_Q_PTR_SHIFT;
805bdd4dddeSJeff Garzik 
806c5d3e45aSJeff Garzik 	WARN_ON(pp->crpb_dma & 0xff);
807c5d3e45aSJeff Garzik 	writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
808c5d3e45aSJeff Garzik 
809c5d3e45aSJeff Garzik 	if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
810bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & 0xffffffff) | index,
811c5d3e45aSJeff Garzik 			 port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
812c5d3e45aSJeff Garzik 	else
813bdd4dddeSJeff Garzik 		writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
814c5d3e45aSJeff Garzik 
815bdd4dddeSJeff Garzik 	writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
816c5d3e45aSJeff Garzik 		 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
817c5d3e45aSJeff Garzik }
818c5d3e45aSJeff Garzik 
819c6fd2807SJeff Garzik /**
820c6fd2807SJeff Garzik  *      mv_start_dma - Enable eDMA engine
821c6fd2807SJeff Garzik  *      @base: port base address
822c6fd2807SJeff Garzik  *      @pp: port private data
823c6fd2807SJeff Garzik  *
824c6fd2807SJeff Garzik  *      Verify the local cache of the eDMA state is accurate with a
825c6fd2807SJeff Garzik  *      WARN_ON.
826c6fd2807SJeff Garzik  *
827c6fd2807SJeff Garzik  *      LOCKING:
828c6fd2807SJeff Garzik  *      Inherited from caller.
829c6fd2807SJeff Garzik  */
8300c58912eSMark Lord static void mv_start_dma(struct ata_port *ap, void __iomem *port_mmio,
83172109168SMark Lord 			 struct mv_port_priv *pp, u8 protocol)
832c6fd2807SJeff Garzik {
83372109168SMark Lord 	int want_ncq = (protocol == ATA_PROT_NCQ);
83472109168SMark Lord 
83572109168SMark Lord 	if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
83672109168SMark Lord 		int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
83772109168SMark Lord 		if (want_ncq != using_ncq)
838b562468cSMark Lord 			mv_stop_edma(ap);
83972109168SMark Lord 	}
840c5d3e45aSJeff Garzik 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
8410c58912eSMark Lord 		struct mv_host_priv *hpriv = ap->host->private_data;
842*352fab70SMark Lord 		int hardport = mv_hardport_from_port(ap->port_no);
8430c58912eSMark Lord 		void __iomem *hc_mmio = mv_hc_base_from_port(
844*352fab70SMark Lord 					mv_host_base(ap->host), hardport);
8450c58912eSMark Lord 		u32 hc_irq_cause, ipending;
8460c58912eSMark Lord 
847bdd4dddeSJeff Garzik 		/* clear EDMA event indicators, if any */
848f630d562SMark Lord 		writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
849bdd4dddeSJeff Garzik 
8500c58912eSMark Lord 		/* clear EDMA interrupt indicator, if any */
8510c58912eSMark Lord 		hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
852*352fab70SMark Lord 		ipending = (DEV_IRQ | DMA_IRQ) << hardport;
8530c58912eSMark Lord 		if (hc_irq_cause & ipending) {
8540c58912eSMark Lord 			writelfl(hc_irq_cause & ~ipending,
8550c58912eSMark Lord 				 hc_mmio + HC_IRQ_CAUSE_OFS);
8560c58912eSMark Lord 		}
8570c58912eSMark Lord 
858e12bef50SMark Lord 		mv_edma_cfg(ap, want_ncq);
8590c58912eSMark Lord 
8600c58912eSMark Lord 		/* clear FIS IRQ Cause */
8610c58912eSMark Lord 		writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
8620c58912eSMark Lord 
863f630d562SMark Lord 		mv_set_edma_ptrs(port_mmio, hpriv, pp);
864bdd4dddeSJeff Garzik 
865f630d562SMark Lord 		writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
866c6fd2807SJeff Garzik 		pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
867c6fd2807SJeff Garzik 	}
868c6fd2807SJeff Garzik }
869c6fd2807SJeff Garzik 
870c6fd2807SJeff Garzik /**
871e12bef50SMark Lord  *      mv_stop_edma_engine - Disable eDMA engine
872b562468cSMark Lord  *      @port_mmio: io base address
873c6fd2807SJeff Garzik  *
874c6fd2807SJeff Garzik  *      LOCKING:
875c6fd2807SJeff Garzik  *      Inherited from caller.
876c6fd2807SJeff Garzik  */
877b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
878c6fd2807SJeff Garzik {
879b562468cSMark Lord 	int i;
880c6fd2807SJeff Garzik 
881b562468cSMark Lord 	/* Disable eDMA.  The disable bit auto clears. */
882c6fd2807SJeff Garzik 	writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
883c6fd2807SJeff Garzik 
884b562468cSMark Lord 	/* Wait for the chip to confirm eDMA is off. */
885b562468cSMark Lord 	for (i = 10000; i > 0; i--) {
886b562468cSMark Lord 		u32 reg = readl(port_mmio + EDMA_CMD_OFS);
8874537deb5SJeff Garzik 		if (!(reg & EDMA_EN))
888b562468cSMark Lord 			return 0;
889b562468cSMark Lord 		udelay(10);
890c6fd2807SJeff Garzik 	}
891b562468cSMark Lord 	return -EIO;
892c6fd2807SJeff Garzik }
893c6fd2807SJeff Garzik 
894e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
895c6fd2807SJeff Garzik {
896c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
897c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
898c6fd2807SJeff Garzik 
899b562468cSMark Lord 	if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
900b562468cSMark Lord 		return 0;
901c6fd2807SJeff Garzik 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
902b562468cSMark Lord 	if (mv_stop_edma_engine(port_mmio)) {
903c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
904b562468cSMark Lord 		return -EIO;
905c6fd2807SJeff Garzik 	}
906b562468cSMark Lord 	return 0;
9070ea9e179SJeff Garzik }
9080ea9e179SJeff Garzik 
909c6fd2807SJeff Garzik #ifdef ATA_DEBUG
910c6fd2807SJeff Garzik static void mv_dump_mem(void __iomem *start, unsigned bytes)
911c6fd2807SJeff Garzik {
912c6fd2807SJeff Garzik 	int b, w;
913c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
914c6fd2807SJeff Garzik 		DPRINTK("%p: ", start + b);
915c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
916c6fd2807SJeff Garzik 			printk("%08x ", readl(start + b));
917c6fd2807SJeff Garzik 			b += sizeof(u32);
918c6fd2807SJeff Garzik 		}
919c6fd2807SJeff Garzik 		printk("\n");
920c6fd2807SJeff Garzik 	}
921c6fd2807SJeff Garzik }
922c6fd2807SJeff Garzik #endif
923c6fd2807SJeff Garzik 
924c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
925c6fd2807SJeff Garzik {
926c6fd2807SJeff Garzik #ifdef ATA_DEBUG
927c6fd2807SJeff Garzik 	int b, w;
928c6fd2807SJeff Garzik 	u32 dw;
929c6fd2807SJeff Garzik 	for (b = 0; b < bytes; ) {
930c6fd2807SJeff Garzik 		DPRINTK("%02x: ", b);
931c6fd2807SJeff Garzik 		for (w = 0; b < bytes && w < 4; w++) {
932c6fd2807SJeff Garzik 			(void) pci_read_config_dword(pdev, b, &dw);
933c6fd2807SJeff Garzik 			printk("%08x ", dw);
934c6fd2807SJeff Garzik 			b += sizeof(u32);
935c6fd2807SJeff Garzik 		}
936c6fd2807SJeff Garzik 		printk("\n");
937c6fd2807SJeff Garzik 	}
938c6fd2807SJeff Garzik #endif
939c6fd2807SJeff Garzik }
940c6fd2807SJeff Garzik static void mv_dump_all_regs(void __iomem *mmio_base, int port,
941c6fd2807SJeff Garzik 			     struct pci_dev *pdev)
942c6fd2807SJeff Garzik {
943c6fd2807SJeff Garzik #ifdef ATA_DEBUG
944c6fd2807SJeff Garzik 	void __iomem *hc_base = mv_hc_base(mmio_base,
945c6fd2807SJeff Garzik 					   port >> MV_PORT_HC_SHIFT);
946c6fd2807SJeff Garzik 	void __iomem *port_base;
947c6fd2807SJeff Garzik 	int start_port, num_ports, p, start_hc, num_hcs, hc;
948c6fd2807SJeff Garzik 
949c6fd2807SJeff Garzik 	if (0 > port) {
950c6fd2807SJeff Garzik 		start_hc = start_port = 0;
951c6fd2807SJeff Garzik 		num_ports = 8;		/* shld be benign for 4 port devs */
952c6fd2807SJeff Garzik 		num_hcs = 2;
953c6fd2807SJeff Garzik 	} else {
954c6fd2807SJeff Garzik 		start_hc = port >> MV_PORT_HC_SHIFT;
955c6fd2807SJeff Garzik 		start_port = port;
956c6fd2807SJeff Garzik 		num_ports = num_hcs = 1;
957c6fd2807SJeff Garzik 	}
958c6fd2807SJeff Garzik 	DPRINTK("All registers for port(s) %u-%u:\n", start_port,
959c6fd2807SJeff Garzik 		num_ports > 1 ? num_ports - 1 : start_port);
960c6fd2807SJeff Garzik 
961c6fd2807SJeff Garzik 	if (NULL != pdev) {
962c6fd2807SJeff Garzik 		DPRINTK("PCI config space regs:\n");
963c6fd2807SJeff Garzik 		mv_dump_pci_cfg(pdev, 0x68);
964c6fd2807SJeff Garzik 	}
965c6fd2807SJeff Garzik 	DPRINTK("PCI regs:\n");
966c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xc00, 0x3c);
967c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xd00, 0x34);
968c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0xf00, 0x4);
969c6fd2807SJeff Garzik 	mv_dump_mem(mmio_base+0x1d00, 0x6c);
970c6fd2807SJeff Garzik 	for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
971c6fd2807SJeff Garzik 		hc_base = mv_hc_base(mmio_base, hc);
972c6fd2807SJeff Garzik 		DPRINTK("HC regs (HC %i):\n", hc);
973c6fd2807SJeff Garzik 		mv_dump_mem(hc_base, 0x1c);
974c6fd2807SJeff Garzik 	}
975c6fd2807SJeff Garzik 	for (p = start_port; p < start_port + num_ports; p++) {
976c6fd2807SJeff Garzik 		port_base = mv_port_base(mmio_base, p);
977c6fd2807SJeff Garzik 		DPRINTK("EDMA regs (port %i):\n", p);
978c6fd2807SJeff Garzik 		mv_dump_mem(port_base, 0x54);
979c6fd2807SJeff Garzik 		DPRINTK("SATA regs (port %i):\n", p);
980c6fd2807SJeff Garzik 		mv_dump_mem(port_base+0x300, 0x60);
981c6fd2807SJeff Garzik 	}
982c6fd2807SJeff Garzik #endif
983c6fd2807SJeff Garzik }
984c6fd2807SJeff Garzik 
985c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
986c6fd2807SJeff Garzik {
987c6fd2807SJeff Garzik 	unsigned int ofs;
988c6fd2807SJeff Garzik 
989c6fd2807SJeff Garzik 	switch (sc_reg_in) {
990c6fd2807SJeff Garzik 	case SCR_STATUS:
991c6fd2807SJeff Garzik 	case SCR_CONTROL:
992c6fd2807SJeff Garzik 	case SCR_ERROR:
993c6fd2807SJeff Garzik 		ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
994c6fd2807SJeff Garzik 		break;
995c6fd2807SJeff Garzik 	case SCR_ACTIVE:
996c6fd2807SJeff Garzik 		ofs = SATA_ACTIVE_OFS;   /* active is not with the others */
997c6fd2807SJeff Garzik 		break;
998c6fd2807SJeff Garzik 	default:
999c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1000c6fd2807SJeff Garzik 		break;
1001c6fd2807SJeff Garzik 	}
1002c6fd2807SJeff Garzik 	return ofs;
1003c6fd2807SJeff Garzik }
1004c6fd2807SJeff Garzik 
1005da3dbb17STejun Heo static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
1006c6fd2807SJeff Garzik {
1007c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1008c6fd2807SJeff Garzik 
1009da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
1010da3dbb17STejun Heo 		*val = readl(mv_ap_base(ap) + ofs);
1011da3dbb17STejun Heo 		return 0;
1012da3dbb17STejun Heo 	} else
1013da3dbb17STejun Heo 		return -EINVAL;
1014c6fd2807SJeff Garzik }
1015c6fd2807SJeff Garzik 
1016da3dbb17STejun Heo static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
1017c6fd2807SJeff Garzik {
1018c6fd2807SJeff Garzik 	unsigned int ofs = mv_scr_offset(sc_reg_in);
1019c6fd2807SJeff Garzik 
1020da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
1021c6fd2807SJeff Garzik 		writelfl(val, mv_ap_base(ap) + ofs);
1022da3dbb17STejun Heo 		return 0;
1023da3dbb17STejun Heo 	} else
1024da3dbb17STejun Heo 		return -EINVAL;
1025c6fd2807SJeff Garzik }
1026c6fd2807SJeff Garzik 
1027f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1028f273827eSMark Lord {
1029f273827eSMark Lord 	/*
1030e49856d8SMark Lord 	 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1031e49856d8SMark Lord 	 *
1032e49856d8SMark Lord 	 * Gen-II does not support NCQ over a port multiplier
1033e49856d8SMark Lord 	 *  (no FIS-based switching).
1034e49856d8SMark Lord 	 *
1035f273827eSMark Lord 	 * We don't have hob_nsect when doing NCQ commands on Gen-II.
1036f273827eSMark Lord 	 * See mv_qc_prep() for more info.
1037f273827eSMark Lord 	 */
1038e49856d8SMark Lord 	if (adev->flags & ATA_DFLAG_NCQ) {
1039*352fab70SMark Lord 		if (sata_pmp_attached(adev->link->ap)) {
1040e49856d8SMark Lord 			adev->flags &= ~ATA_DFLAG_NCQ;
1041*352fab70SMark Lord 			ata_dev_printk(adev, KERN_INFO,
1042*352fab70SMark Lord 				"NCQ disabled for command-based switching\n");
1043*352fab70SMark Lord 		} else if (adev->max_sectors > GEN_II_NCQ_MAX_SECTORS) {
1044*352fab70SMark Lord 			adev->max_sectors = GEN_II_NCQ_MAX_SECTORS;
1045*352fab70SMark Lord 			ata_dev_printk(adev, KERN_INFO,
1046*352fab70SMark Lord 				"max_sectors limited to %u for NCQ\n",
1047*352fab70SMark Lord 				adev->max_sectors);
1048*352fab70SMark Lord 		}
1049f273827eSMark Lord 	}
1050e49856d8SMark Lord }
1051f273827eSMark Lord 
1052e49856d8SMark Lord static void mv_config_fbs(void __iomem *port_mmio, int enable_fbs)
1053e49856d8SMark Lord {
1054e49856d8SMark Lord 	u32 old_fcfg, new_fcfg, old_ltmode, new_ltmode;
1055e49856d8SMark Lord 	/*
1056e49856d8SMark Lord 	 * Various bit settings required for operation
1057e49856d8SMark Lord 	 * in FIS-based switching (fbs) mode on GenIIe:
1058e49856d8SMark Lord 	 */
1059e49856d8SMark Lord 	old_fcfg   = readl(port_mmio + FIS_CFG_OFS);
1060e49856d8SMark Lord 	old_ltmode = readl(port_mmio + LTMODE_OFS);
1061e49856d8SMark Lord 	if (enable_fbs) {
1062e49856d8SMark Lord 		new_fcfg   = old_fcfg   |  FIS_CFG_SINGLE_SYNC;
1063e49856d8SMark Lord 		new_ltmode = old_ltmode |  LTMODE_BIT8;
1064e49856d8SMark Lord 	} else { /* disable fbs */
1065e49856d8SMark Lord 		new_fcfg   = old_fcfg   & ~FIS_CFG_SINGLE_SYNC;
1066e49856d8SMark Lord 		new_ltmode = old_ltmode & ~LTMODE_BIT8;
1067e49856d8SMark Lord 	}
1068e49856d8SMark Lord 	if (new_fcfg != old_fcfg)
1069e49856d8SMark Lord 		writelfl(new_fcfg, port_mmio + FIS_CFG_OFS);
1070e49856d8SMark Lord 	if (new_ltmode != old_ltmode)
1071e49856d8SMark Lord 		writelfl(new_ltmode, port_mmio + LTMODE_OFS);
1072e49856d8SMark Lord }
1073c6fd2807SJeff Garzik 
1074e12bef50SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq)
1075c6fd2807SJeff Garzik {
1076c6fd2807SJeff Garzik 	u32 cfg;
1077e12bef50SMark Lord 	struct mv_port_priv *pp    = ap->private_data;
1078e12bef50SMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1079e12bef50SMark Lord 	void __iomem *port_mmio    = mv_ap_base(ap);
1080c6fd2807SJeff Garzik 
1081c6fd2807SJeff Garzik 	/* set up non-NCQ EDMA configuration */
1082c6fd2807SJeff Garzik 	cfg = EDMA_CFG_Q_DEPTH;		/* always 0x1f for *all* chips */
1083c6fd2807SJeff Garzik 
1084c6fd2807SJeff Garzik 	if (IS_GEN_I(hpriv))
1085c6fd2807SJeff Garzik 		cfg |= (1 << 8);	/* enab config burst size mask */
1086c6fd2807SJeff Garzik 
1087c6fd2807SJeff Garzik 	else if (IS_GEN_II(hpriv))
1088c6fd2807SJeff Garzik 		cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1089c6fd2807SJeff Garzik 
1090c6fd2807SJeff Garzik 	else if (IS_GEN_IIE(hpriv)) {
1091e728eabeSJeff Garzik 		cfg |= (1 << 23);	/* do not mask PM field in rx'd FIS */
1092e728eabeSJeff Garzik 		cfg |= (1 << 22);	/* enab 4-entry host queue cache */
1093c6fd2807SJeff Garzik 		cfg |= (1 << 18);	/* enab early completion */
1094e728eabeSJeff Garzik 		cfg |= (1 << 17);	/* enab cut-through (dis stor&forwrd) */
1095e49856d8SMark Lord 
1096e49856d8SMark Lord 		if (want_ncq && sata_pmp_attached(ap)) {
1097e49856d8SMark Lord 			cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
1098e49856d8SMark Lord 			mv_config_fbs(port_mmio, 1);
1099e49856d8SMark Lord 		} else {
1100e49856d8SMark Lord 			mv_config_fbs(port_mmio, 0);
1101e49856d8SMark Lord 		}
1102c6fd2807SJeff Garzik 	}
1103c6fd2807SJeff Garzik 
110472109168SMark Lord 	if (want_ncq) {
110572109168SMark Lord 		cfg |= EDMA_CFG_NCQ;
110672109168SMark Lord 		pp->pp_flags |=  MV_PP_FLAG_NCQ_EN;
110772109168SMark Lord 	} else
110872109168SMark Lord 		pp->pp_flags &= ~MV_PP_FLAG_NCQ_EN;
110972109168SMark Lord 
1110c6fd2807SJeff Garzik 	writelfl(cfg, port_mmio + EDMA_CFG_OFS);
1111c6fd2807SJeff Garzik }
1112c6fd2807SJeff Garzik 
1113da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1114da2fa9baSMark Lord {
1115da2fa9baSMark Lord 	struct mv_host_priv *hpriv = ap->host->private_data;
1116da2fa9baSMark Lord 	struct mv_port_priv *pp = ap->private_data;
1117eb73d558SMark Lord 	int tag;
1118da2fa9baSMark Lord 
1119da2fa9baSMark Lord 	if (pp->crqb) {
1120da2fa9baSMark Lord 		dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1121da2fa9baSMark Lord 		pp->crqb = NULL;
1122da2fa9baSMark Lord 	}
1123da2fa9baSMark Lord 	if (pp->crpb) {
1124da2fa9baSMark Lord 		dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1125da2fa9baSMark Lord 		pp->crpb = NULL;
1126da2fa9baSMark Lord 	}
1127eb73d558SMark Lord 	/*
1128eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1129eb73d558SMark Lord 	 * For later hardware, we have one unique sg_tbl per NCQ tag.
1130eb73d558SMark Lord 	 */
1131eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1132eb73d558SMark Lord 		if (pp->sg_tbl[tag]) {
1133eb73d558SMark Lord 			if (tag == 0 || !IS_GEN_I(hpriv))
1134eb73d558SMark Lord 				dma_pool_free(hpriv->sg_tbl_pool,
1135eb73d558SMark Lord 					      pp->sg_tbl[tag],
1136eb73d558SMark Lord 					      pp->sg_tbl_dma[tag]);
1137eb73d558SMark Lord 			pp->sg_tbl[tag] = NULL;
1138eb73d558SMark Lord 		}
1139da2fa9baSMark Lord 	}
1140da2fa9baSMark Lord }
1141da2fa9baSMark Lord 
1142c6fd2807SJeff Garzik /**
1143c6fd2807SJeff Garzik  *      mv_port_start - Port specific init/start routine.
1144c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1145c6fd2807SJeff Garzik  *
1146c6fd2807SJeff Garzik  *      Allocate and point to DMA memory, init port private memory,
1147c6fd2807SJeff Garzik  *      zero indices.
1148c6fd2807SJeff Garzik  *
1149c6fd2807SJeff Garzik  *      LOCKING:
1150c6fd2807SJeff Garzik  *      Inherited from caller.
1151c6fd2807SJeff Garzik  */
1152c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1153c6fd2807SJeff Garzik {
1154cca3974eSJeff Garzik 	struct device *dev = ap->host->dev;
1155cca3974eSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1156c6fd2807SJeff Garzik 	struct mv_port_priv *pp;
1157dde20207SJames Bottomley 	int tag;
1158c6fd2807SJeff Garzik 
115924dc5f33STejun Heo 	pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1160c6fd2807SJeff Garzik 	if (!pp)
116124dc5f33STejun Heo 		return -ENOMEM;
1162da2fa9baSMark Lord 	ap->private_data = pp;
1163c6fd2807SJeff Garzik 
1164da2fa9baSMark Lord 	pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1165da2fa9baSMark Lord 	if (!pp->crqb)
1166da2fa9baSMark Lord 		return -ENOMEM;
1167da2fa9baSMark Lord 	memset(pp->crqb, 0, MV_CRQB_Q_SZ);
1168c6fd2807SJeff Garzik 
1169da2fa9baSMark Lord 	pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1170da2fa9baSMark Lord 	if (!pp->crpb)
1171da2fa9baSMark Lord 		goto out_port_free_dma_mem;
1172da2fa9baSMark Lord 	memset(pp->crpb, 0, MV_CRPB_Q_SZ);
1173c6fd2807SJeff Garzik 
1174eb73d558SMark Lord 	/*
1175eb73d558SMark Lord 	 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1176eb73d558SMark Lord 	 * For later hardware, we need one unique sg_tbl per NCQ tag.
1177eb73d558SMark Lord 	 */
1178eb73d558SMark Lord 	for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1179eb73d558SMark Lord 		if (tag == 0 || !IS_GEN_I(hpriv)) {
1180eb73d558SMark Lord 			pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1181eb73d558SMark Lord 					      GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1182eb73d558SMark Lord 			if (!pp->sg_tbl[tag])
1183da2fa9baSMark Lord 				goto out_port_free_dma_mem;
1184eb73d558SMark Lord 		} else {
1185eb73d558SMark Lord 			pp->sg_tbl[tag]     = pp->sg_tbl[0];
1186eb73d558SMark Lord 			pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1187eb73d558SMark Lord 		}
1188eb73d558SMark Lord 	}
1189c6fd2807SJeff Garzik 	return 0;
1190da2fa9baSMark Lord 
1191da2fa9baSMark Lord out_port_free_dma_mem:
1192da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1193da2fa9baSMark Lord 	return -ENOMEM;
1194c6fd2807SJeff Garzik }
1195c6fd2807SJeff Garzik 
1196c6fd2807SJeff Garzik /**
1197c6fd2807SJeff Garzik  *      mv_port_stop - Port specific cleanup/stop routine.
1198c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1199c6fd2807SJeff Garzik  *
1200c6fd2807SJeff Garzik  *      Stop DMA, cleanup port memory.
1201c6fd2807SJeff Garzik  *
1202c6fd2807SJeff Garzik  *      LOCKING:
1203cca3974eSJeff Garzik  *      This routine uses the host lock to protect the DMA stop.
1204c6fd2807SJeff Garzik  */
1205c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1206c6fd2807SJeff Garzik {
1207e12bef50SMark Lord 	mv_stop_edma(ap);
1208da2fa9baSMark Lord 	mv_port_free_dma_mem(ap);
1209c6fd2807SJeff Garzik }
1210c6fd2807SJeff Garzik 
1211c6fd2807SJeff Garzik /**
1212c6fd2807SJeff Garzik  *      mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1213c6fd2807SJeff Garzik  *      @qc: queued command whose SG list to source from
1214c6fd2807SJeff Garzik  *
1215c6fd2807SJeff Garzik  *      Populate the SG list and mark the last entry.
1216c6fd2807SJeff Garzik  *
1217c6fd2807SJeff Garzik  *      LOCKING:
1218c6fd2807SJeff Garzik  *      Inherited from caller.
1219c6fd2807SJeff Garzik  */
12206c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1221c6fd2807SJeff Garzik {
1222c6fd2807SJeff Garzik 	struct mv_port_priv *pp = qc->ap->private_data;
1223c6fd2807SJeff Garzik 	struct scatterlist *sg;
12243be6cbd7SJeff Garzik 	struct mv_sg *mv_sg, *last_sg = NULL;
1225ff2aeb1eSTejun Heo 	unsigned int si;
1226c6fd2807SJeff Garzik 
1227eb73d558SMark Lord 	mv_sg = pp->sg_tbl[qc->tag];
1228ff2aeb1eSTejun Heo 	for_each_sg(qc->sg, sg, qc->n_elem, si) {
1229d88184fbSJeff Garzik 		dma_addr_t addr = sg_dma_address(sg);
1230d88184fbSJeff Garzik 		u32 sg_len = sg_dma_len(sg);
1231c6fd2807SJeff Garzik 
12324007b493SOlof Johansson 		while (sg_len) {
12334007b493SOlof Johansson 			u32 offset = addr & 0xffff;
12344007b493SOlof Johansson 			u32 len = sg_len;
12354007b493SOlof Johansson 
12364007b493SOlof Johansson 			if ((offset + sg_len > 0x10000))
12374007b493SOlof Johansson 				len = 0x10000 - offset;
12384007b493SOlof Johansson 
1239d88184fbSJeff Garzik 			mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1240d88184fbSJeff Garzik 			mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
12416c08772eSJeff Garzik 			mv_sg->flags_size = cpu_to_le32(len & 0xffff);
1242c6fd2807SJeff Garzik 
12434007b493SOlof Johansson 			sg_len -= len;
12444007b493SOlof Johansson 			addr += len;
12454007b493SOlof Johansson 
12463be6cbd7SJeff Garzik 			last_sg = mv_sg;
1247d88184fbSJeff Garzik 			mv_sg++;
1248c6fd2807SJeff Garzik 		}
12494007b493SOlof Johansson 	}
12503be6cbd7SJeff Garzik 
12513be6cbd7SJeff Garzik 	if (likely(last_sg))
12523be6cbd7SJeff Garzik 		last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
1253c6fd2807SJeff Garzik }
1254c6fd2807SJeff Garzik 
12555796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1256c6fd2807SJeff Garzik {
1257c6fd2807SJeff Garzik 	u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1258c6fd2807SJeff Garzik 		(last ? CRQB_CMD_LAST : 0);
1259c6fd2807SJeff Garzik 	*cmdw = cpu_to_le16(tmp);
1260c6fd2807SJeff Garzik }
1261c6fd2807SJeff Garzik 
1262c6fd2807SJeff Garzik /**
1263c6fd2807SJeff Garzik  *      mv_qc_prep - Host specific command preparation.
1264c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1265c6fd2807SJeff Garzik  *
1266c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1267c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1268c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1269c6fd2807SJeff Garzik  *      the SG load routine.
1270c6fd2807SJeff Garzik  *
1271c6fd2807SJeff Garzik  *      LOCKING:
1272c6fd2807SJeff Garzik  *      Inherited from caller.
1273c6fd2807SJeff Garzik  */
1274c6fd2807SJeff Garzik static void mv_qc_prep(struct ata_queued_cmd *qc)
1275c6fd2807SJeff Garzik {
1276c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1277c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1278c6fd2807SJeff Garzik 	__le16 *cw;
1279c6fd2807SJeff Garzik 	struct ata_taskfile *tf;
1280c6fd2807SJeff Garzik 	u16 flags = 0;
1281c6fd2807SJeff Garzik 	unsigned in_index;
1282c6fd2807SJeff Garzik 
1283138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1284138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ))
1285c6fd2807SJeff Garzik 		return;
1286c6fd2807SJeff Garzik 
1287c6fd2807SJeff Garzik 	/* Fill in command request block
1288c6fd2807SJeff Garzik 	 */
1289c6fd2807SJeff Garzik 	if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1290c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
1291c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
1292c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
1293e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
1294c6fd2807SJeff Garzik 
1295bdd4dddeSJeff Garzik 	/* get current queue index from software */
1296bdd4dddeSJeff Garzik 	in_index = pp->req_idx & MV_MAX_Q_DEPTH_MASK;
1297c6fd2807SJeff Garzik 
1298c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr =
1299eb73d558SMark Lord 		cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1300c6fd2807SJeff Garzik 	pp->crqb[in_index].sg_addr_hi =
1301eb73d558SMark Lord 		cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
1302c6fd2807SJeff Garzik 	pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
1303c6fd2807SJeff Garzik 
1304c6fd2807SJeff Garzik 	cw = &pp->crqb[in_index].ata_cmd[0];
1305c6fd2807SJeff Garzik 	tf = &qc->tf;
1306c6fd2807SJeff Garzik 
1307c6fd2807SJeff Garzik 	/* Sadly, the CRQB cannot accomodate all registers--there are
1308c6fd2807SJeff Garzik 	 * only 11 bytes...so we must pick and choose required
1309c6fd2807SJeff Garzik 	 * registers based on the command.  So, we drop feature and
1310c6fd2807SJeff Garzik 	 * hob_feature for [RW] DMA commands, but they are needed for
1311c6fd2807SJeff Garzik 	 * NCQ.  NCQ will drop hob_nsect.
1312c6fd2807SJeff Garzik 	 */
1313c6fd2807SJeff Garzik 	switch (tf->command) {
1314c6fd2807SJeff Garzik 	case ATA_CMD_READ:
1315c6fd2807SJeff Garzik 	case ATA_CMD_READ_EXT:
1316c6fd2807SJeff Garzik 	case ATA_CMD_WRITE:
1317c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_EXT:
1318c6fd2807SJeff Garzik 	case ATA_CMD_WRITE_FUA_EXT:
1319c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1320c6fd2807SJeff Garzik 		break;
1321c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_READ:
1322c6fd2807SJeff Garzik 	case ATA_CMD_FPDMA_WRITE:
1323c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
1324c6fd2807SJeff Garzik 		mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1325c6fd2807SJeff Garzik 		break;
1326c6fd2807SJeff Garzik 	default:
1327c6fd2807SJeff Garzik 		/* The only other commands EDMA supports in non-queued and
1328c6fd2807SJeff Garzik 		 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1329c6fd2807SJeff Garzik 		 * of which are defined/used by Linux.  If we get here, this
1330c6fd2807SJeff Garzik 		 * driver needs work.
1331c6fd2807SJeff Garzik 		 *
1332c6fd2807SJeff Garzik 		 * FIXME: modify libata to give qc_prep a return value and
1333c6fd2807SJeff Garzik 		 * return error here.
1334c6fd2807SJeff Garzik 		 */
1335c6fd2807SJeff Garzik 		BUG_ON(tf->command);
1336c6fd2807SJeff Garzik 		break;
1337c6fd2807SJeff Garzik 	}
1338c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1339c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1340c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1341c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1342c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1343c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1344c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1345c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1346c6fd2807SJeff Garzik 	mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1);	/* last */
1347c6fd2807SJeff Garzik 
1348c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1349c6fd2807SJeff Garzik 		return;
1350c6fd2807SJeff Garzik 	mv_fill_sg(qc);
1351c6fd2807SJeff Garzik }
1352c6fd2807SJeff Garzik 
1353c6fd2807SJeff Garzik /**
1354c6fd2807SJeff Garzik  *      mv_qc_prep_iie - Host specific command preparation.
1355c6fd2807SJeff Garzik  *      @qc: queued command to prepare
1356c6fd2807SJeff Garzik  *
1357c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1358c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it handles prep of the CRQB
1359c6fd2807SJeff Garzik  *      (command request block), does some sanity checking, and calls
1360c6fd2807SJeff Garzik  *      the SG load routine.
1361c6fd2807SJeff Garzik  *
1362c6fd2807SJeff Garzik  *      LOCKING:
1363c6fd2807SJeff Garzik  *      Inherited from caller.
1364c6fd2807SJeff Garzik  */
1365c6fd2807SJeff Garzik static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
1366c6fd2807SJeff Garzik {
1367c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
1368c6fd2807SJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1369c6fd2807SJeff Garzik 	struct mv_crqb_iie *crqb;
1370c6fd2807SJeff Garzik 	struct ata_taskfile *tf;
1371c6fd2807SJeff Garzik 	unsigned in_index;
1372c6fd2807SJeff Garzik 	u32 flags = 0;
1373c6fd2807SJeff Garzik 
1374138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1375138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ))
1376c6fd2807SJeff Garzik 		return;
1377c6fd2807SJeff Garzik 
1378e12bef50SMark Lord 	/* Fill in Gen IIE command request block */
1379c6fd2807SJeff Garzik 	if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1380c6fd2807SJeff Garzik 		flags |= CRQB_FLAG_READ;
1381c6fd2807SJeff Garzik 
1382c6fd2807SJeff Garzik 	WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
1383c6fd2807SJeff Garzik 	flags |= qc->tag << CRQB_TAG_SHIFT;
13848c0aeb4aSMark Lord 	flags |= qc->tag << CRQB_HOSTQ_SHIFT;
1385e49856d8SMark Lord 	flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
1386c6fd2807SJeff Garzik 
1387bdd4dddeSJeff Garzik 	/* get current queue index from software */
1388bdd4dddeSJeff Garzik 	in_index = pp->req_idx & MV_MAX_Q_DEPTH_MASK;
1389c6fd2807SJeff Garzik 
1390c6fd2807SJeff Garzik 	crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
1391eb73d558SMark Lord 	crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1392eb73d558SMark Lord 	crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
1393c6fd2807SJeff Garzik 	crqb->flags = cpu_to_le32(flags);
1394c6fd2807SJeff Garzik 
1395c6fd2807SJeff Garzik 	tf = &qc->tf;
1396c6fd2807SJeff Garzik 	crqb->ata_cmd[0] = cpu_to_le32(
1397c6fd2807SJeff Garzik 			(tf->command << 16) |
1398c6fd2807SJeff Garzik 			(tf->feature << 24)
1399c6fd2807SJeff Garzik 		);
1400c6fd2807SJeff Garzik 	crqb->ata_cmd[1] = cpu_to_le32(
1401c6fd2807SJeff Garzik 			(tf->lbal << 0) |
1402c6fd2807SJeff Garzik 			(tf->lbam << 8) |
1403c6fd2807SJeff Garzik 			(tf->lbah << 16) |
1404c6fd2807SJeff Garzik 			(tf->device << 24)
1405c6fd2807SJeff Garzik 		);
1406c6fd2807SJeff Garzik 	crqb->ata_cmd[2] = cpu_to_le32(
1407c6fd2807SJeff Garzik 			(tf->hob_lbal << 0) |
1408c6fd2807SJeff Garzik 			(tf->hob_lbam << 8) |
1409c6fd2807SJeff Garzik 			(tf->hob_lbah << 16) |
1410c6fd2807SJeff Garzik 			(tf->hob_feature << 24)
1411c6fd2807SJeff Garzik 		);
1412c6fd2807SJeff Garzik 	crqb->ata_cmd[3] = cpu_to_le32(
1413c6fd2807SJeff Garzik 			(tf->nsect << 0) |
1414c6fd2807SJeff Garzik 			(tf->hob_nsect << 8)
1415c6fd2807SJeff Garzik 		);
1416c6fd2807SJeff Garzik 
1417c6fd2807SJeff Garzik 	if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1418c6fd2807SJeff Garzik 		return;
1419c6fd2807SJeff Garzik 	mv_fill_sg(qc);
1420c6fd2807SJeff Garzik }
1421c6fd2807SJeff Garzik 
1422c6fd2807SJeff Garzik /**
1423c6fd2807SJeff Garzik  *      mv_qc_issue - Initiate a command to the host
1424c6fd2807SJeff Garzik  *      @qc: queued command to start
1425c6fd2807SJeff Garzik  *
1426c6fd2807SJeff Garzik  *      This routine simply redirects to the general purpose routine
1427c6fd2807SJeff Garzik  *      if command is not DMA.  Else, it sanity checks our local
1428c6fd2807SJeff Garzik  *      caches of the request producer/consumer indices then enables
1429c6fd2807SJeff Garzik  *      DMA and bumps the request producer index.
1430c6fd2807SJeff Garzik  *
1431c6fd2807SJeff Garzik  *      LOCKING:
1432c6fd2807SJeff Garzik  *      Inherited from caller.
1433c6fd2807SJeff Garzik  */
1434c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
1435c6fd2807SJeff Garzik {
1436c5d3e45aSJeff Garzik 	struct ata_port *ap = qc->ap;
1437c5d3e45aSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1438c5d3e45aSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1439bdd4dddeSJeff Garzik 	u32 in_index;
1440c6fd2807SJeff Garzik 
1441138bfdd0SMark Lord 	if ((qc->tf.protocol != ATA_PROT_DMA) &&
1442138bfdd0SMark Lord 	    (qc->tf.protocol != ATA_PROT_NCQ)) {
144317c5aab5SMark Lord 		/*
144417c5aab5SMark Lord 		 * We're about to send a non-EDMA capable command to the
1445c6fd2807SJeff Garzik 		 * port.  Turn off EDMA so there won't be problems accessing
1446c6fd2807SJeff Garzik 		 * shadow block, etc registers.
1447c6fd2807SJeff Garzik 		 */
1448b562468cSMark Lord 		mv_stop_edma(ap);
1449e49856d8SMark Lord 		mv_pmp_select(ap, qc->dev->link->pmp);
14509363c382STejun Heo 		return ata_sff_qc_issue(qc);
1451c6fd2807SJeff Garzik 	}
1452c6fd2807SJeff Garzik 
145372109168SMark Lord 	mv_start_dma(ap, port_mmio, pp, qc->tf.protocol);
1454bdd4dddeSJeff Garzik 
1455bdd4dddeSJeff Garzik 	pp->req_idx++;
1456c6fd2807SJeff Garzik 
1457bdd4dddeSJeff Garzik 	in_index = (pp->req_idx & MV_MAX_Q_DEPTH_MASK) << EDMA_REQ_Q_PTR_SHIFT;
1458c6fd2807SJeff Garzik 
1459c6fd2807SJeff Garzik 	/* and write the request in pointer to kick the EDMA to life */
1460bdd4dddeSJeff Garzik 	writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
1461bdd4dddeSJeff Garzik 		 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
1462c6fd2807SJeff Garzik 
1463c6fd2807SJeff Garzik 	return 0;
1464c6fd2807SJeff Garzik }
1465c6fd2807SJeff Garzik 
1466c6fd2807SJeff Garzik /**
1467c6fd2807SJeff Garzik  *      mv_err_intr - Handle error interrupts on the port
1468c6fd2807SJeff Garzik  *      @ap: ATA channel to manipulate
1469c6fd2807SJeff Garzik  *      @reset_allowed: bool: 0 == don't trigger from reset here
1470c6fd2807SJeff Garzik  *
1471c6fd2807SJeff Garzik  *      In most cases, just clear the interrupt and move on.  However,
1472e12bef50SMark Lord  *      some cases require an eDMA reset, which also performs a COMRESET.
1473e12bef50SMark Lord  *      The SERR case requires a clear of pending errors in the SATA
1474e12bef50SMark Lord  *      SERROR register.  Finally, if the port disabled DMA,
1475e12bef50SMark Lord  *      update our cached copy to match.
1476c6fd2807SJeff Garzik  *
1477c6fd2807SJeff Garzik  *      LOCKING:
1478c6fd2807SJeff Garzik  *      Inherited from caller.
1479c6fd2807SJeff Garzik  */
1480bdd4dddeSJeff Garzik static void mv_err_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
1481c6fd2807SJeff Garzik {
1482c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1483bdd4dddeSJeff Garzik 	u32 edma_err_cause, eh_freeze_mask, serr = 0;
1484bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1485bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1486bdd4dddeSJeff Garzik 	unsigned int edma_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
1487bdd4dddeSJeff Garzik 	unsigned int action = 0, err_mask = 0;
14889af5c9c9STejun Heo 	struct ata_eh_info *ehi = &ap->link.eh_info;
1489c6fd2807SJeff Garzik 
1490bdd4dddeSJeff Garzik 	ata_ehi_clear_desc(ehi);
1491c6fd2807SJeff Garzik 
1492bdd4dddeSJeff Garzik 	if (!edma_enabled) {
1493bdd4dddeSJeff Garzik 		/* just a guess: do we need to do this? should we
1494bdd4dddeSJeff Garzik 		 * expand this, and do it in all cases?
1495bdd4dddeSJeff Garzik 		 */
1496936fd732STejun Heo 		sata_scr_read(&ap->link, SCR_ERROR, &serr);
1497936fd732STejun Heo 		sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
1498c6fd2807SJeff Garzik 	}
1499bdd4dddeSJeff Garzik 
1500bdd4dddeSJeff Garzik 	edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1501bdd4dddeSJeff Garzik 
1502*352fab70SMark Lord 	ata_ehi_push_desc(ehi, "edma_err_cause=%08x", edma_err_cause);
1503bdd4dddeSJeff Garzik 
1504bdd4dddeSJeff Garzik 	/*
1505*352fab70SMark Lord 	 * All generations share these EDMA error cause bits:
1506bdd4dddeSJeff Garzik 	 */
1507bdd4dddeSJeff Garzik 	if (edma_err_cause & EDMA_ERR_DEV)
1508bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_DEV;
1509bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
15106c1153e0SJeff Garzik 			EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
1511bdd4dddeSJeff Garzik 			EDMA_ERR_INTRL_PAR)) {
1512bdd4dddeSJeff Garzik 		err_mask |= AC_ERR_ATA_BUS;
1513cf480626STejun Heo 		action |= ATA_EH_RESET;
1514b64bbc39STejun Heo 		ata_ehi_push_desc(ehi, "parity error");
1515bdd4dddeSJeff Garzik 	}
1516bdd4dddeSJeff Garzik 	if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
1517bdd4dddeSJeff Garzik 		ata_ehi_hotplugged(ehi);
1518bdd4dddeSJeff Garzik 		ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
1519b64bbc39STejun Heo 			"dev disconnect" : "dev connect");
1520cf480626STejun Heo 		action |= ATA_EH_RESET;
1521bdd4dddeSJeff Garzik 	}
1522bdd4dddeSJeff Garzik 
1523*352fab70SMark Lord 	/*
1524*352fab70SMark Lord 	 * Gen-I has a different SELF_DIS bit,
1525*352fab70SMark Lord 	 * different FREEZE bits, and no SERR bit:
1526*352fab70SMark Lord 	 */
1527ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv)) {
1528bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE_5;
1529bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
1530c6fd2807SJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1531b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
1532c6fd2807SJeff Garzik 		}
1533bdd4dddeSJeff Garzik 	} else {
1534bdd4dddeSJeff Garzik 		eh_freeze_mask = EDMA_EH_FREEZE;
1535bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SELF_DIS) {
1536bdd4dddeSJeff Garzik 			pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1537b64bbc39STejun Heo 			ata_ehi_push_desc(ehi, "EDMA self-disable");
1538bdd4dddeSJeff Garzik 		}
1539bdd4dddeSJeff Garzik 		if (edma_err_cause & EDMA_ERR_SERR) {
1540936fd732STejun Heo 			sata_scr_read(&ap->link, SCR_ERROR, &serr);
1541936fd732STejun Heo 			sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
1542bdd4dddeSJeff Garzik 			err_mask = AC_ERR_ATA_BUS;
1543cf480626STejun Heo 			action |= ATA_EH_RESET;
1544bdd4dddeSJeff Garzik 		}
1545bdd4dddeSJeff Garzik 	}
1546c6fd2807SJeff Garzik 
1547c6fd2807SJeff Garzik 	/* Clear EDMA now that SERR cleanup done */
15483606a380SMark Lord 	writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1549c6fd2807SJeff Garzik 
1550bdd4dddeSJeff Garzik 	if (!err_mask) {
1551bdd4dddeSJeff Garzik 		err_mask = AC_ERR_OTHER;
1552cf480626STejun Heo 		action |= ATA_EH_RESET;
1553bdd4dddeSJeff Garzik 	}
1554bdd4dddeSJeff Garzik 
1555bdd4dddeSJeff Garzik 	ehi->serror |= serr;
1556bdd4dddeSJeff Garzik 	ehi->action |= action;
1557bdd4dddeSJeff Garzik 
1558bdd4dddeSJeff Garzik 	if (qc)
1559bdd4dddeSJeff Garzik 		qc->err_mask |= err_mask;
1560bdd4dddeSJeff Garzik 	else
1561bdd4dddeSJeff Garzik 		ehi->err_mask |= err_mask;
1562bdd4dddeSJeff Garzik 
1563bdd4dddeSJeff Garzik 	if (edma_err_cause & eh_freeze_mask)
1564bdd4dddeSJeff Garzik 		ata_port_freeze(ap);
1565bdd4dddeSJeff Garzik 	else
1566bdd4dddeSJeff Garzik 		ata_port_abort(ap);
1567bdd4dddeSJeff Garzik }
1568bdd4dddeSJeff Garzik 
1569bdd4dddeSJeff Garzik static void mv_intr_pio(struct ata_port *ap)
1570bdd4dddeSJeff Garzik {
1571bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
1572bdd4dddeSJeff Garzik 	u8 ata_status;
1573bdd4dddeSJeff Garzik 
1574bdd4dddeSJeff Garzik 	/* ignore spurious intr if drive still BUSY */
1575bdd4dddeSJeff Garzik 	ata_status = readb(ap->ioaddr.status_addr);
1576bdd4dddeSJeff Garzik 	if (unlikely(ata_status & ATA_BUSY))
1577bdd4dddeSJeff Garzik 		return;
1578bdd4dddeSJeff Garzik 
1579bdd4dddeSJeff Garzik 	/* get active ATA command */
15809af5c9c9STejun Heo 	qc = ata_qc_from_tag(ap, ap->link.active_tag);
1581bdd4dddeSJeff Garzik 	if (unlikely(!qc))			/* no active tag */
1582bdd4dddeSJeff Garzik 		return;
1583bdd4dddeSJeff Garzik 	if (qc->tf.flags & ATA_TFLAG_POLLING)	/* polling; we don't own qc */
1584bdd4dddeSJeff Garzik 		return;
1585bdd4dddeSJeff Garzik 
1586bdd4dddeSJeff Garzik 	/* and finally, complete the ATA command */
1587bdd4dddeSJeff Garzik 	qc->err_mask |= ac_err_mask(ata_status);
1588bdd4dddeSJeff Garzik 	ata_qc_complete(qc);
1589bdd4dddeSJeff Garzik }
1590bdd4dddeSJeff Garzik 
1591bdd4dddeSJeff Garzik static void mv_intr_edma(struct ata_port *ap)
1592bdd4dddeSJeff Garzik {
1593bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
1594bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
1595bdd4dddeSJeff Garzik 	struct mv_port_priv *pp = ap->private_data;
1596bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
1597bdd4dddeSJeff Garzik 	u32 out_index, in_index;
1598bdd4dddeSJeff Garzik 	bool work_done = false;
1599bdd4dddeSJeff Garzik 
1600bdd4dddeSJeff Garzik 	/* get h/w response queue pointer */
1601bdd4dddeSJeff Garzik 	in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
1602bdd4dddeSJeff Garzik 			>> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
1603bdd4dddeSJeff Garzik 
1604bdd4dddeSJeff Garzik 	while (1) {
1605bdd4dddeSJeff Garzik 		u16 status;
16066c1153e0SJeff Garzik 		unsigned int tag;
1607bdd4dddeSJeff Garzik 
1608bdd4dddeSJeff Garzik 		/* get s/w response queue last-read pointer, and compare */
1609bdd4dddeSJeff Garzik 		out_index = pp->resp_idx & MV_MAX_Q_DEPTH_MASK;
1610bdd4dddeSJeff Garzik 		if (in_index == out_index)
1611bdd4dddeSJeff Garzik 			break;
1612bdd4dddeSJeff Garzik 
1613bdd4dddeSJeff Garzik 		/* 50xx: get active ATA command */
1614bdd4dddeSJeff Garzik 		if (IS_GEN_I(hpriv))
16159af5c9c9STejun Heo 			tag = ap->link.active_tag;
1616bdd4dddeSJeff Garzik 
16176c1153e0SJeff Garzik 		/* Gen II/IIE: get active ATA command via tag, to enable
16186c1153e0SJeff Garzik 		 * support for queueing.  this works transparently for
16196c1153e0SJeff Garzik 		 * queued and non-queued modes.
1620bdd4dddeSJeff Garzik 		 */
16218c0aeb4aSMark Lord 		else
16228c0aeb4aSMark Lord 			tag = le16_to_cpu(pp->crpb[out_index].id) & 0x1f;
1623bdd4dddeSJeff Garzik 
1624bdd4dddeSJeff Garzik 		qc = ata_qc_from_tag(ap, tag);
1625bdd4dddeSJeff Garzik 
1626cb924419SMark Lord 		/* For non-NCQ mode, the lower 8 bits of status
1627cb924419SMark Lord 		 * are from EDMA_ERR_IRQ_CAUSE_OFS,
1628cb924419SMark Lord 		 * which should be zero if all went well.
1629bdd4dddeSJeff Garzik 		 */
1630bdd4dddeSJeff Garzik 		status = le16_to_cpu(pp->crpb[out_index].flags);
1631cb924419SMark Lord 		if ((status & 0xff) && !(pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
1632bdd4dddeSJeff Garzik 			mv_err_intr(ap, qc);
1633bdd4dddeSJeff Garzik 			return;
1634bdd4dddeSJeff Garzik 		}
1635bdd4dddeSJeff Garzik 
1636bdd4dddeSJeff Garzik 		/* and finally, complete the ATA command */
1637bdd4dddeSJeff Garzik 		if (qc) {
1638bdd4dddeSJeff Garzik 			qc->err_mask |=
1639bdd4dddeSJeff Garzik 				ac_err_mask(status >> CRPB_FLAG_STATUS_SHIFT);
1640bdd4dddeSJeff Garzik 			ata_qc_complete(qc);
1641bdd4dddeSJeff Garzik 		}
1642bdd4dddeSJeff Garzik 
1643bdd4dddeSJeff Garzik 		/* advance software response queue pointer, to
1644bdd4dddeSJeff Garzik 		 * indicate (after the loop completes) to hardware
1645bdd4dddeSJeff Garzik 		 * that we have consumed a response queue entry.
1646bdd4dddeSJeff Garzik 		 */
1647bdd4dddeSJeff Garzik 		work_done = true;
1648bdd4dddeSJeff Garzik 		pp->resp_idx++;
1649bdd4dddeSJeff Garzik 	}
1650bdd4dddeSJeff Garzik 
1651*352fab70SMark Lord 	/* Update the software queue position index in hardware */
1652bdd4dddeSJeff Garzik 	if (work_done)
1653bdd4dddeSJeff Garzik 		writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
1654bdd4dddeSJeff Garzik 			 (out_index << EDMA_RSP_Q_PTR_SHIFT),
1655bdd4dddeSJeff Garzik 			 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
1656c6fd2807SJeff Garzik }
1657c6fd2807SJeff Garzik 
1658c6fd2807SJeff Garzik /**
1659c6fd2807SJeff Garzik  *      mv_host_intr - Handle all interrupts on the given host controller
1660cca3974eSJeff Garzik  *      @host: host specific structure
1661c6fd2807SJeff Garzik  *      @relevant: port error bits relevant to this host controller
1662c6fd2807SJeff Garzik  *      @hc: which host controller we're to look at
1663c6fd2807SJeff Garzik  *
1664c6fd2807SJeff Garzik  *      Read then write clear the HC interrupt status then walk each
1665c6fd2807SJeff Garzik  *      port connected to the HC and see if it needs servicing.  Port
1666c6fd2807SJeff Garzik  *      success ints are reported in the HC interrupt status reg, the
1667c6fd2807SJeff Garzik  *      port error ints are reported in the higher level main
1668c6fd2807SJeff Garzik  *      interrupt status register and thus are passed in via the
1669c6fd2807SJeff Garzik  *      'relevant' argument.
1670c6fd2807SJeff Garzik  *
1671c6fd2807SJeff Garzik  *      LOCKING:
1672c6fd2807SJeff Garzik  *      Inherited from caller.
1673c6fd2807SJeff Garzik  */
1674cca3974eSJeff Garzik static void mv_host_intr(struct ata_host *host, u32 relevant, unsigned int hc)
1675c6fd2807SJeff Garzik {
1676f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
1677f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
1678c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
1679c6fd2807SJeff Garzik 	u32 hc_irq_cause;
1680f351b2d6SSaeed Bishara 	int port, port0, last_port;
1681c6fd2807SJeff Garzik 
168235177265SJeff Garzik 	if (hc == 0)
1683c6fd2807SJeff Garzik 		port0 = 0;
168435177265SJeff Garzik 	else
1685c6fd2807SJeff Garzik 		port0 = MV_PORTS_PER_HC;
1686c6fd2807SJeff Garzik 
1687f351b2d6SSaeed Bishara 	if (HAS_PCI(host))
1688f351b2d6SSaeed Bishara 		last_port = port0 + MV_PORTS_PER_HC;
1689f351b2d6SSaeed Bishara 	else
1690f351b2d6SSaeed Bishara 		last_port = port0 + hpriv->n_ports;
1691c6fd2807SJeff Garzik 	/* we'll need the HC success int register in most cases */
1692c6fd2807SJeff Garzik 	hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
1693bdd4dddeSJeff Garzik 	if (!hc_irq_cause)
1694bdd4dddeSJeff Garzik 		return;
1695bdd4dddeSJeff Garzik 
1696c6fd2807SJeff Garzik 	writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
1697c6fd2807SJeff Garzik 
1698c6fd2807SJeff Garzik 	VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
1699c6fd2807SJeff Garzik 		hc, relevant, hc_irq_cause);
1700c6fd2807SJeff Garzik 
17018f71efe2SYinghai Lu 	for (port = port0; port < last_port; port++) {
1702cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[port];
17038f71efe2SYinghai Lu 		struct mv_port_priv *pp;
1704*352fab70SMark Lord 		int have_err_bits, hardport, shift;
1705c6fd2807SJeff Garzik 
1706bdd4dddeSJeff Garzik 		if ((!ap) || (ap->flags & ATA_FLAG_DISABLED))
1707c6fd2807SJeff Garzik 			continue;
1708c6fd2807SJeff Garzik 
17098f71efe2SYinghai Lu 		pp = ap->private_data;
17108f71efe2SYinghai Lu 
1711c6fd2807SJeff Garzik 		shift = port << 1;		/* (port * 2) */
1712e12bef50SMark Lord 		if (port >= MV_PORTS_PER_HC)
1713c6fd2807SJeff Garzik 			shift++;	/* skip bit 8 in the HC Main IRQ reg */
1714e12bef50SMark Lord 
1715*352fab70SMark Lord 		have_err_bits = ((ERR_IRQ << shift) & relevant);
1716bdd4dddeSJeff Garzik 
1717bdd4dddeSJeff Garzik 		if (unlikely(have_err_bits)) {
1718bdd4dddeSJeff Garzik 			struct ata_queued_cmd *qc;
1719bdd4dddeSJeff Garzik 
17209af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
1721bdd4dddeSJeff Garzik 			if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
1722bdd4dddeSJeff Garzik 				continue;
1723bdd4dddeSJeff Garzik 
1724bdd4dddeSJeff Garzik 			mv_err_intr(ap, qc);
1725bdd4dddeSJeff Garzik 			continue;
1726c6fd2807SJeff Garzik 		}
1727c6fd2807SJeff Garzik 
1728*352fab70SMark Lord 		hardport = mv_hardport_from_port(port); /* range 0..3 */
1729bdd4dddeSJeff Garzik 
1730bdd4dddeSJeff Garzik 		if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
1731*352fab70SMark Lord 			if ((DMA_IRQ << hardport) & hc_irq_cause)
1732bdd4dddeSJeff Garzik 				mv_intr_edma(ap);
1733bdd4dddeSJeff Garzik 		} else {
1734*352fab70SMark Lord 			if ((DEV_IRQ << hardport) & hc_irq_cause)
1735bdd4dddeSJeff Garzik 				mv_intr_pio(ap);
1736c6fd2807SJeff Garzik 		}
1737c6fd2807SJeff Garzik 	}
1738c6fd2807SJeff Garzik 	VPRINTK("EXIT\n");
1739c6fd2807SJeff Garzik }
1740c6fd2807SJeff Garzik 
1741bdd4dddeSJeff Garzik static void mv_pci_error(struct ata_host *host, void __iomem *mmio)
1742bdd4dddeSJeff Garzik {
174302a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
1744bdd4dddeSJeff Garzik 	struct ata_port *ap;
1745bdd4dddeSJeff Garzik 	struct ata_queued_cmd *qc;
1746bdd4dddeSJeff Garzik 	struct ata_eh_info *ehi;
1747bdd4dddeSJeff Garzik 	unsigned int i, err_mask, printed = 0;
1748bdd4dddeSJeff Garzik 	u32 err_cause;
1749bdd4dddeSJeff Garzik 
175002a121daSMark Lord 	err_cause = readl(mmio + hpriv->irq_cause_ofs);
1751bdd4dddeSJeff Garzik 
1752bdd4dddeSJeff Garzik 	dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
1753bdd4dddeSJeff Garzik 		   err_cause);
1754bdd4dddeSJeff Garzik 
1755bdd4dddeSJeff Garzik 	DPRINTK("All regs @ PCI error\n");
1756bdd4dddeSJeff Garzik 	mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
1757bdd4dddeSJeff Garzik 
175802a121daSMark Lord 	writelfl(0, mmio + hpriv->irq_cause_ofs);
1759bdd4dddeSJeff Garzik 
1760bdd4dddeSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
1761bdd4dddeSJeff Garzik 		ap = host->ports[i];
1762936fd732STejun Heo 		if (!ata_link_offline(&ap->link)) {
17639af5c9c9STejun Heo 			ehi = &ap->link.eh_info;
1764bdd4dddeSJeff Garzik 			ata_ehi_clear_desc(ehi);
1765bdd4dddeSJeff Garzik 			if (!printed++)
1766bdd4dddeSJeff Garzik 				ata_ehi_push_desc(ehi,
1767bdd4dddeSJeff Garzik 					"PCI err cause 0x%08x", err_cause);
1768bdd4dddeSJeff Garzik 			err_mask = AC_ERR_HOST_BUS;
1769cf480626STejun Heo 			ehi->action = ATA_EH_RESET;
17709af5c9c9STejun Heo 			qc = ata_qc_from_tag(ap, ap->link.active_tag);
1771bdd4dddeSJeff Garzik 			if (qc)
1772bdd4dddeSJeff Garzik 				qc->err_mask |= err_mask;
1773bdd4dddeSJeff Garzik 			else
1774bdd4dddeSJeff Garzik 				ehi->err_mask |= err_mask;
1775bdd4dddeSJeff Garzik 
1776bdd4dddeSJeff Garzik 			ata_port_freeze(ap);
1777bdd4dddeSJeff Garzik 		}
1778bdd4dddeSJeff Garzik 	}
1779bdd4dddeSJeff Garzik }
1780bdd4dddeSJeff Garzik 
1781c6fd2807SJeff Garzik /**
1782c5d3e45aSJeff Garzik  *      mv_interrupt - Main interrupt event handler
1783c6fd2807SJeff Garzik  *      @irq: unused
1784c6fd2807SJeff Garzik  *      @dev_instance: private data; in this case the host structure
1785c6fd2807SJeff Garzik  *
1786c6fd2807SJeff Garzik  *      Read the read only register to determine if any host
1787c6fd2807SJeff Garzik  *      controllers have pending interrupts.  If so, call lower level
1788c6fd2807SJeff Garzik  *      routine to handle.  Also check for PCI errors which are only
1789c6fd2807SJeff Garzik  *      reported here.
1790c6fd2807SJeff Garzik  *
1791c6fd2807SJeff Garzik  *      LOCKING:
1792cca3974eSJeff Garzik  *      This routine holds the host lock while processing pending
1793c6fd2807SJeff Garzik  *      interrupts.
1794c6fd2807SJeff Garzik  */
17957d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
1796c6fd2807SJeff Garzik {
1797cca3974eSJeff Garzik 	struct ata_host *host = dev_instance;
1798f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = host->private_data;
1799c6fd2807SJeff Garzik 	unsigned int hc, handled = 0, n_hcs;
1800f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
1801*352fab70SMark Lord 	u32 main_cause, main_mask;
1802c6fd2807SJeff Garzik 
1803646a4da5SMark Lord 	spin_lock(&host->lock);
1804*352fab70SMark Lord 	main_cause = readl(hpriv->main_cause_reg_addr);
1805*352fab70SMark Lord 	main_mask  = readl(hpriv->main_mask_reg_addr);
1806*352fab70SMark Lord 	/*
1807*352fab70SMark Lord 	 * Deal with cases where we either have nothing pending, or have read
1808*352fab70SMark Lord 	 * a bogus register value which can indicate HW removal or PCI fault.
1809c6fd2807SJeff Garzik 	 */
1810*352fab70SMark Lord 	if (!(main_cause & main_mask) || (main_cause == 0xffffffffU))
1811646a4da5SMark Lord 		goto out_unlock;
1812c6fd2807SJeff Garzik 
1813cca3974eSJeff Garzik 	n_hcs = mv_get_hc_count(host->ports[0]->flags);
1814c6fd2807SJeff Garzik 
1815*352fab70SMark Lord 	if (unlikely((main_cause & PCI_ERR) && HAS_PCI(host))) {
1816bdd4dddeSJeff Garzik 		mv_pci_error(host, mmio);
1817bdd4dddeSJeff Garzik 		handled = 1;
1818bdd4dddeSJeff Garzik 		goto out_unlock;	/* skip all other HC irq handling */
1819bdd4dddeSJeff Garzik 	}
1820bdd4dddeSJeff Garzik 
1821c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hcs; hc++) {
1822*352fab70SMark Lord 		u32 relevant = main_cause & (HC0_IRQ_PEND << (hc * HC_SHIFT));
1823c6fd2807SJeff Garzik 		if (relevant) {
1824cca3974eSJeff Garzik 			mv_host_intr(host, relevant, hc);
1825bdd4dddeSJeff Garzik 			handled = 1;
1826c6fd2807SJeff Garzik 		}
1827c6fd2807SJeff Garzik 	}
1828c6fd2807SJeff Garzik 
1829bdd4dddeSJeff Garzik out_unlock:
1830cca3974eSJeff Garzik 	spin_unlock(&host->lock);
1831c6fd2807SJeff Garzik 	return IRQ_RETVAL(handled);
1832c6fd2807SJeff Garzik }
1833c6fd2807SJeff Garzik 
1834c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
1835c6fd2807SJeff Garzik {
1836c6fd2807SJeff Garzik 	unsigned int ofs;
1837c6fd2807SJeff Garzik 
1838c6fd2807SJeff Garzik 	switch (sc_reg_in) {
1839c6fd2807SJeff Garzik 	case SCR_STATUS:
1840c6fd2807SJeff Garzik 	case SCR_ERROR:
1841c6fd2807SJeff Garzik 	case SCR_CONTROL:
1842c6fd2807SJeff Garzik 		ofs = sc_reg_in * sizeof(u32);
1843c6fd2807SJeff Garzik 		break;
1844c6fd2807SJeff Garzik 	default:
1845c6fd2807SJeff Garzik 		ofs = 0xffffffffU;
1846c6fd2807SJeff Garzik 		break;
1847c6fd2807SJeff Garzik 	}
1848c6fd2807SJeff Garzik 	return ofs;
1849c6fd2807SJeff Garzik }
1850c6fd2807SJeff Garzik 
1851da3dbb17STejun Heo static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
1852c6fd2807SJeff Garzik {
1853f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
1854f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
18550d5ff566STejun Heo 	void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
1856c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
1857c6fd2807SJeff Garzik 
1858da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
1859da3dbb17STejun Heo 		*val = readl(addr + ofs);
1860da3dbb17STejun Heo 		return 0;
1861da3dbb17STejun Heo 	} else
1862da3dbb17STejun Heo 		return -EINVAL;
1863c6fd2807SJeff Garzik }
1864c6fd2807SJeff Garzik 
1865da3dbb17STejun Heo static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
1866c6fd2807SJeff Garzik {
1867f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
1868f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
18690d5ff566STejun Heo 	void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
1870c6fd2807SJeff Garzik 	unsigned int ofs = mv5_scr_offset(sc_reg_in);
1871c6fd2807SJeff Garzik 
1872da3dbb17STejun Heo 	if (ofs != 0xffffffffU) {
18730d5ff566STejun Heo 		writelfl(val, addr + ofs);
1874da3dbb17STejun Heo 		return 0;
1875da3dbb17STejun Heo 	} else
1876da3dbb17STejun Heo 		return -EINVAL;
1877c6fd2807SJeff Garzik }
1878c6fd2807SJeff Garzik 
18797bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
1880c6fd2807SJeff Garzik {
18817bb3c529SSaeed Bishara 	struct pci_dev *pdev = to_pci_dev(host->dev);
1882c6fd2807SJeff Garzik 	int early_5080;
1883c6fd2807SJeff Garzik 
188444c10138SAuke Kok 	early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
1885c6fd2807SJeff Garzik 
1886c6fd2807SJeff Garzik 	if (!early_5080) {
1887c6fd2807SJeff Garzik 		u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1888c6fd2807SJeff Garzik 		tmp |= (1 << 0);
1889c6fd2807SJeff Garzik 		writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
1890c6fd2807SJeff Garzik 	}
1891c6fd2807SJeff Garzik 
18927bb3c529SSaeed Bishara 	mv_reset_pci_bus(host, mmio);
1893c6fd2807SJeff Garzik }
1894c6fd2807SJeff Garzik 
1895c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
1896c6fd2807SJeff Garzik {
1897c6fd2807SJeff Garzik 	writel(0x0fcfffff, mmio + MV_FLASH_CTL);
1898c6fd2807SJeff Garzik }
1899c6fd2807SJeff Garzik 
1900c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
1901c6fd2807SJeff Garzik 			   void __iomem *mmio)
1902c6fd2807SJeff Garzik {
1903c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
1904c6fd2807SJeff Garzik 	u32 tmp;
1905c6fd2807SJeff Garzik 
1906c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
1907c6fd2807SJeff Garzik 
1908c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0x1800;	/* bits 12:11 */
1909c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0xe0;	/* bits 7:5 */
1910c6fd2807SJeff Garzik }
1911c6fd2807SJeff Garzik 
1912c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
1913c6fd2807SJeff Garzik {
1914c6fd2807SJeff Garzik 	u32 tmp;
1915c6fd2807SJeff Garzik 
1916c6fd2807SJeff Garzik 	writel(0, mmio + MV_GPIO_PORT_CTL);
1917c6fd2807SJeff Garzik 
1918c6fd2807SJeff Garzik 	/* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
1919c6fd2807SJeff Garzik 
1920c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1921c6fd2807SJeff Garzik 	tmp |= ~(1 << 0);
1922c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
1923c6fd2807SJeff Garzik }
1924c6fd2807SJeff Garzik 
1925c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
1926c6fd2807SJeff Garzik 			   unsigned int port)
1927c6fd2807SJeff Garzik {
1928c6fd2807SJeff Garzik 	void __iomem *phy_mmio = mv5_phy_base(mmio, port);
1929c6fd2807SJeff Garzik 	const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
1930c6fd2807SJeff Garzik 	u32 tmp;
1931c6fd2807SJeff Garzik 	int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
1932c6fd2807SJeff Garzik 
1933c6fd2807SJeff Garzik 	if (fix_apm_sq) {
1934c6fd2807SJeff Garzik 		tmp = readl(phy_mmio + MV5_LT_MODE);
1935c6fd2807SJeff Garzik 		tmp |= (1 << 19);
1936c6fd2807SJeff Garzik 		writel(tmp, phy_mmio + MV5_LT_MODE);
1937c6fd2807SJeff Garzik 
1938c6fd2807SJeff Garzik 		tmp = readl(phy_mmio + MV5_PHY_CTL);
1939c6fd2807SJeff Garzik 		tmp &= ~0x3;
1940c6fd2807SJeff Garzik 		tmp |= 0x1;
1941c6fd2807SJeff Garzik 		writel(tmp, phy_mmio + MV5_PHY_CTL);
1942c6fd2807SJeff Garzik 	}
1943c6fd2807SJeff Garzik 
1944c6fd2807SJeff Garzik 	tmp = readl(phy_mmio + MV5_PHY_MODE);
1945c6fd2807SJeff Garzik 	tmp &= ~mask;
1946c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].pre;
1947c6fd2807SJeff Garzik 	tmp |= hpriv->signal[port].amps;
1948c6fd2807SJeff Garzik 	writel(tmp, phy_mmio + MV5_PHY_MODE);
1949c6fd2807SJeff Garzik }
1950c6fd2807SJeff Garzik 
1951c6fd2807SJeff Garzik 
1952c6fd2807SJeff Garzik #undef ZERO
1953c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
1954c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
1955c6fd2807SJeff Garzik 			     unsigned int port)
1956c6fd2807SJeff Garzik {
1957c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
1958c6fd2807SJeff Garzik 
1959b562468cSMark Lord 	/*
1960b562468cSMark Lord 	 * The datasheet warns against setting ATA_RST when EDMA is active
1961b562468cSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
1962b562468cSMark Lord 	 * to disable the EDMA engine before doing the ATA_RST operation.
1963b562468cSMark Lord 	 */
1964e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
1965c6fd2807SJeff Garzik 
1966c6fd2807SJeff Garzik 	ZERO(0x028);	/* command */
1967c6fd2807SJeff Garzik 	writel(0x11f, port_mmio + EDMA_CFG_OFS);
1968c6fd2807SJeff Garzik 	ZERO(0x004);	/* timer */
1969c6fd2807SJeff Garzik 	ZERO(0x008);	/* irq err cause */
1970c6fd2807SJeff Garzik 	ZERO(0x00c);	/* irq err mask */
1971c6fd2807SJeff Garzik 	ZERO(0x010);	/* rq bah */
1972c6fd2807SJeff Garzik 	ZERO(0x014);	/* rq inp */
1973c6fd2807SJeff Garzik 	ZERO(0x018);	/* rq outp */
1974c6fd2807SJeff Garzik 	ZERO(0x01c);	/* respq bah */
1975c6fd2807SJeff Garzik 	ZERO(0x024);	/* respq outp */
1976c6fd2807SJeff Garzik 	ZERO(0x020);	/* respq inp */
1977c6fd2807SJeff Garzik 	ZERO(0x02c);	/* test control */
1978c6fd2807SJeff Garzik 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
1979c6fd2807SJeff Garzik }
1980c6fd2807SJeff Garzik #undef ZERO
1981c6fd2807SJeff Garzik 
1982c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
1983c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
1984c6fd2807SJeff Garzik 			unsigned int hc)
1985c6fd2807SJeff Garzik {
1986c6fd2807SJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
1987c6fd2807SJeff Garzik 	u32 tmp;
1988c6fd2807SJeff Garzik 
1989c6fd2807SJeff Garzik 	ZERO(0x00c);
1990c6fd2807SJeff Garzik 	ZERO(0x010);
1991c6fd2807SJeff Garzik 	ZERO(0x014);
1992c6fd2807SJeff Garzik 	ZERO(0x018);
1993c6fd2807SJeff Garzik 
1994c6fd2807SJeff Garzik 	tmp = readl(hc_mmio + 0x20);
1995c6fd2807SJeff Garzik 	tmp &= 0x1c1c1c1c;
1996c6fd2807SJeff Garzik 	tmp |= 0x03030303;
1997c6fd2807SJeff Garzik 	writel(tmp, hc_mmio + 0x20);
1998c6fd2807SJeff Garzik }
1999c6fd2807SJeff Garzik #undef ZERO
2000c6fd2807SJeff Garzik 
2001c6fd2807SJeff Garzik static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2002c6fd2807SJeff Garzik 			unsigned int n_hc)
2003c6fd2807SJeff Garzik {
2004c6fd2807SJeff Garzik 	unsigned int hc, port;
2005c6fd2807SJeff Garzik 
2006c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
2007c6fd2807SJeff Garzik 		for (port = 0; port < MV_PORTS_PER_HC; port++)
2008c6fd2807SJeff Garzik 			mv5_reset_hc_port(hpriv, mmio,
2009c6fd2807SJeff Garzik 					  (hc * MV_PORTS_PER_HC) + port);
2010c6fd2807SJeff Garzik 
2011c6fd2807SJeff Garzik 		mv5_reset_one_hc(hpriv, mmio, hc);
2012c6fd2807SJeff Garzik 	}
2013c6fd2807SJeff Garzik 
2014c6fd2807SJeff Garzik 	return 0;
2015c6fd2807SJeff Garzik }
2016c6fd2807SJeff Garzik 
2017c6fd2807SJeff Garzik #undef ZERO
2018c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
20197bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
2020c6fd2807SJeff Garzik {
202102a121daSMark Lord 	struct mv_host_priv *hpriv = host->private_data;
2022c6fd2807SJeff Garzik 	u32 tmp;
2023c6fd2807SJeff Garzik 
2024c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_PCI_MODE);
2025c6fd2807SJeff Garzik 	tmp &= 0xff00ffff;
2026c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_PCI_MODE);
2027c6fd2807SJeff Garzik 
2028c6fd2807SJeff Garzik 	ZERO(MV_PCI_DISC_TIMER);
2029c6fd2807SJeff Garzik 	ZERO(MV_PCI_MSI_TRIGGER);
2030c6fd2807SJeff Garzik 	writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
2031c6fd2807SJeff Garzik 	ZERO(HC_MAIN_IRQ_MASK_OFS);
2032c6fd2807SJeff Garzik 	ZERO(MV_PCI_SERR_MASK);
203302a121daSMark Lord 	ZERO(hpriv->irq_cause_ofs);
203402a121daSMark Lord 	ZERO(hpriv->irq_mask_ofs);
2035c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_LOW_ADDRESS);
2036c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_HIGH_ADDRESS);
2037c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_ATTRIBUTE);
2038c6fd2807SJeff Garzik 	ZERO(MV_PCI_ERR_COMMAND);
2039c6fd2807SJeff Garzik }
2040c6fd2807SJeff Garzik #undef ZERO
2041c6fd2807SJeff Garzik 
2042c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2043c6fd2807SJeff Garzik {
2044c6fd2807SJeff Garzik 	u32 tmp;
2045c6fd2807SJeff Garzik 
2046c6fd2807SJeff Garzik 	mv5_reset_flash(hpriv, mmio);
2047c6fd2807SJeff Garzik 
2048c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_GPIO_PORT_CTL);
2049c6fd2807SJeff Garzik 	tmp &= 0x3;
2050c6fd2807SJeff Garzik 	tmp |= (1 << 5) | (1 << 6);
2051c6fd2807SJeff Garzik 	writel(tmp, mmio + MV_GPIO_PORT_CTL);
2052c6fd2807SJeff Garzik }
2053c6fd2807SJeff Garzik 
2054c6fd2807SJeff Garzik /**
2055c6fd2807SJeff Garzik  *      mv6_reset_hc - Perform the 6xxx global soft reset
2056c6fd2807SJeff Garzik  *      @mmio: base address of the HBA
2057c6fd2807SJeff Garzik  *
2058c6fd2807SJeff Garzik  *      This routine only applies to 6xxx parts.
2059c6fd2807SJeff Garzik  *
2060c6fd2807SJeff Garzik  *      LOCKING:
2061c6fd2807SJeff Garzik  *      Inherited from caller.
2062c6fd2807SJeff Garzik  */
2063c6fd2807SJeff Garzik static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2064c6fd2807SJeff Garzik 			unsigned int n_hc)
2065c6fd2807SJeff Garzik {
2066c6fd2807SJeff Garzik 	void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
2067c6fd2807SJeff Garzik 	int i, rc = 0;
2068c6fd2807SJeff Garzik 	u32 t;
2069c6fd2807SJeff Garzik 
2070c6fd2807SJeff Garzik 	/* Following procedure defined in PCI "main command and status
2071c6fd2807SJeff Garzik 	 * register" table.
2072c6fd2807SJeff Garzik 	 */
2073c6fd2807SJeff Garzik 	t = readl(reg);
2074c6fd2807SJeff Garzik 	writel(t | STOP_PCI_MASTER, reg);
2075c6fd2807SJeff Garzik 
2076c6fd2807SJeff Garzik 	for (i = 0; i < 1000; i++) {
2077c6fd2807SJeff Garzik 		udelay(1);
2078c6fd2807SJeff Garzik 		t = readl(reg);
20792dcb407eSJeff Garzik 		if (PCI_MASTER_EMPTY & t)
2080c6fd2807SJeff Garzik 			break;
2081c6fd2807SJeff Garzik 	}
2082c6fd2807SJeff Garzik 	if (!(PCI_MASTER_EMPTY & t)) {
2083c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
2084c6fd2807SJeff Garzik 		rc = 1;
2085c6fd2807SJeff Garzik 		goto done;
2086c6fd2807SJeff Garzik 	}
2087c6fd2807SJeff Garzik 
2088c6fd2807SJeff Garzik 	/* set reset */
2089c6fd2807SJeff Garzik 	i = 5;
2090c6fd2807SJeff Garzik 	do {
2091c6fd2807SJeff Garzik 		writel(t | GLOB_SFT_RST, reg);
2092c6fd2807SJeff Garzik 		t = readl(reg);
2093c6fd2807SJeff Garzik 		udelay(1);
2094c6fd2807SJeff Garzik 	} while (!(GLOB_SFT_RST & t) && (i-- > 0));
2095c6fd2807SJeff Garzik 
2096c6fd2807SJeff Garzik 	if (!(GLOB_SFT_RST & t)) {
2097c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't set global reset\n");
2098c6fd2807SJeff Garzik 		rc = 1;
2099c6fd2807SJeff Garzik 		goto done;
2100c6fd2807SJeff Garzik 	}
2101c6fd2807SJeff Garzik 
2102c6fd2807SJeff Garzik 	/* clear reset and *reenable the PCI master* (not mentioned in spec) */
2103c6fd2807SJeff Garzik 	i = 5;
2104c6fd2807SJeff Garzik 	do {
2105c6fd2807SJeff Garzik 		writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
2106c6fd2807SJeff Garzik 		t = readl(reg);
2107c6fd2807SJeff Garzik 		udelay(1);
2108c6fd2807SJeff Garzik 	} while ((GLOB_SFT_RST & t) && (i-- > 0));
2109c6fd2807SJeff Garzik 
2110c6fd2807SJeff Garzik 	if (GLOB_SFT_RST & t) {
2111c6fd2807SJeff Garzik 		printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
2112c6fd2807SJeff Garzik 		rc = 1;
2113c6fd2807SJeff Garzik 	}
2114094e50b2SMark Lord 	/*
2115094e50b2SMark Lord 	 * Temporary: wait 3 seconds before port-probing can happen,
2116094e50b2SMark Lord 	 * so that we don't miss finding sleepy SilXXXX port-multipliers.
2117094e50b2SMark Lord 	 * This can go away once hotplug is fully/correctly implemented.
2118094e50b2SMark Lord 	 */
2119094e50b2SMark Lord 	if (rc == 0)
2120094e50b2SMark Lord 		msleep(3000);
2121c6fd2807SJeff Garzik done:
2122c6fd2807SJeff Garzik 	return rc;
2123c6fd2807SJeff Garzik }
2124c6fd2807SJeff Garzik 
2125c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
2126c6fd2807SJeff Garzik 			   void __iomem *mmio)
2127c6fd2807SJeff Garzik {
2128c6fd2807SJeff Garzik 	void __iomem *port_mmio;
2129c6fd2807SJeff Garzik 	u32 tmp;
2130c6fd2807SJeff Garzik 
2131c6fd2807SJeff Garzik 	tmp = readl(mmio + MV_RESET_CFG);
2132c6fd2807SJeff Garzik 	if ((tmp & (1 << 0)) == 0) {
2133c6fd2807SJeff Garzik 		hpriv->signal[idx].amps = 0x7 << 8;
2134c6fd2807SJeff Garzik 		hpriv->signal[idx].pre = 0x1 << 5;
2135c6fd2807SJeff Garzik 		return;
2136c6fd2807SJeff Garzik 	}
2137c6fd2807SJeff Garzik 
2138c6fd2807SJeff Garzik 	port_mmio = mv_port_base(mmio, idx);
2139c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE2);
2140c6fd2807SJeff Garzik 
2141c6fd2807SJeff Garzik 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
2142c6fd2807SJeff Garzik 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
2143c6fd2807SJeff Garzik }
2144c6fd2807SJeff Garzik 
2145c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
2146c6fd2807SJeff Garzik {
2147c6fd2807SJeff Garzik 	writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
2148c6fd2807SJeff Garzik }
2149c6fd2807SJeff Garzik 
2150c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2151c6fd2807SJeff Garzik 			   unsigned int port)
2152c6fd2807SJeff Garzik {
2153c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port);
2154c6fd2807SJeff Garzik 
2155c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
2156c6fd2807SJeff Garzik 	int fix_phy_mode2 =
2157c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
2158c6fd2807SJeff Garzik 	int fix_phy_mode4 =
2159c6fd2807SJeff Garzik 		hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
2160c6fd2807SJeff Garzik 	u32 m2, tmp;
2161c6fd2807SJeff Garzik 
2162c6fd2807SJeff Garzik 	if (fix_phy_mode2) {
2163c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
2164c6fd2807SJeff Garzik 		m2 &= ~(1 << 16);
2165c6fd2807SJeff Garzik 		m2 |= (1 << 31);
2166c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
2167c6fd2807SJeff Garzik 
2168c6fd2807SJeff Garzik 		udelay(200);
2169c6fd2807SJeff Garzik 
2170c6fd2807SJeff Garzik 		m2 = readl(port_mmio + PHY_MODE2);
2171c6fd2807SJeff Garzik 		m2 &= ~((1 << 16) | (1 << 31));
2172c6fd2807SJeff Garzik 		writel(m2, port_mmio + PHY_MODE2);
2173c6fd2807SJeff Garzik 
2174c6fd2807SJeff Garzik 		udelay(200);
2175c6fd2807SJeff Garzik 	}
2176c6fd2807SJeff Garzik 
2177c6fd2807SJeff Garzik 	/* who knows what this magic does */
2178c6fd2807SJeff Garzik 	tmp = readl(port_mmio + PHY_MODE3);
2179c6fd2807SJeff Garzik 	tmp &= ~0x7F800000;
2180c6fd2807SJeff Garzik 	tmp |= 0x2A800000;
2181c6fd2807SJeff Garzik 	writel(tmp, port_mmio + PHY_MODE3);
2182c6fd2807SJeff Garzik 
2183c6fd2807SJeff Garzik 	if (fix_phy_mode4) {
2184c6fd2807SJeff Garzik 		u32 m4;
2185c6fd2807SJeff Garzik 
2186c6fd2807SJeff Garzik 		m4 = readl(port_mmio + PHY_MODE4);
2187c6fd2807SJeff Garzik 
2188c6fd2807SJeff Garzik 		if (hp_flags & MV_HP_ERRATA_60X1B2)
2189e12bef50SMark Lord 			tmp = readl(port_mmio + PHY_MODE3);
2190c6fd2807SJeff Garzik 
2191e12bef50SMark Lord 		/* workaround for errata FEr SATA#10 (part 1) */
2192c6fd2807SJeff Garzik 		m4 = (m4 & ~(1 << 1)) | (1 << 0);
2193c6fd2807SJeff Garzik 
2194c6fd2807SJeff Garzik 		writel(m4, port_mmio + PHY_MODE4);
2195c6fd2807SJeff Garzik 
2196c6fd2807SJeff Garzik 		if (hp_flags & MV_HP_ERRATA_60X1B2)
2197e12bef50SMark Lord 			writel(tmp, port_mmio + PHY_MODE3);
2198c6fd2807SJeff Garzik 	}
2199c6fd2807SJeff Garzik 
2200c6fd2807SJeff Garzik 	/* Revert values of pre-emphasis and signal amps to the saved ones */
2201c6fd2807SJeff Garzik 	m2 = readl(port_mmio + PHY_MODE2);
2202c6fd2807SJeff Garzik 
2203c6fd2807SJeff Garzik 	m2 &= ~MV_M2_PREAMP_MASK;
2204c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].amps;
2205c6fd2807SJeff Garzik 	m2 |= hpriv->signal[port].pre;
2206c6fd2807SJeff Garzik 	m2 &= ~(1 << 16);
2207c6fd2807SJeff Garzik 
2208c6fd2807SJeff Garzik 	/* according to mvSata 3.6.1, some IIE values are fixed */
2209c6fd2807SJeff Garzik 	if (IS_GEN_IIE(hpriv)) {
2210c6fd2807SJeff Garzik 		m2 &= ~0xC30FF01F;
2211c6fd2807SJeff Garzik 		m2 |= 0x0000900F;
2212c6fd2807SJeff Garzik 	}
2213c6fd2807SJeff Garzik 
2214c6fd2807SJeff Garzik 	writel(m2, port_mmio + PHY_MODE2);
2215c6fd2807SJeff Garzik }
2216c6fd2807SJeff Garzik 
2217f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
2218f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
2219f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
2220f351b2d6SSaeed Bishara 				      void __iomem *mmio)
2221f351b2d6SSaeed Bishara {
2222f351b2d6SSaeed Bishara 	return;
2223f351b2d6SSaeed Bishara }
2224f351b2d6SSaeed Bishara 
2225f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
2226f351b2d6SSaeed Bishara 			   void __iomem *mmio)
2227f351b2d6SSaeed Bishara {
2228f351b2d6SSaeed Bishara 	void __iomem *port_mmio;
2229f351b2d6SSaeed Bishara 	u32 tmp;
2230f351b2d6SSaeed Bishara 
2231f351b2d6SSaeed Bishara 	port_mmio = mv_port_base(mmio, idx);
2232f351b2d6SSaeed Bishara 	tmp = readl(port_mmio + PHY_MODE2);
2233f351b2d6SSaeed Bishara 
2234f351b2d6SSaeed Bishara 	hpriv->signal[idx].amps = tmp & 0x700;	/* bits 10:8 */
2235f351b2d6SSaeed Bishara 	hpriv->signal[idx].pre = tmp & 0xe0;	/* bits 7:5 */
2236f351b2d6SSaeed Bishara }
2237f351b2d6SSaeed Bishara 
2238f351b2d6SSaeed Bishara #undef ZERO
2239f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
2240f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
2241f351b2d6SSaeed Bishara 					void __iomem *mmio, unsigned int port)
2242f351b2d6SSaeed Bishara {
2243f351b2d6SSaeed Bishara 	void __iomem *port_mmio = mv_port_base(mmio, port);
2244f351b2d6SSaeed Bishara 
2245b562468cSMark Lord 	/*
2246b562468cSMark Lord 	 * The datasheet warns against setting ATA_RST when EDMA is active
2247b562468cSMark Lord 	 * (but doesn't say what the problem might be).  So we first try
2248b562468cSMark Lord 	 * to disable the EDMA engine before doing the ATA_RST operation.
2249b562468cSMark Lord 	 */
2250e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, port);
2251f351b2d6SSaeed Bishara 
2252f351b2d6SSaeed Bishara 	ZERO(0x028);		/* command */
2253f351b2d6SSaeed Bishara 	writel(0x101f, port_mmio + EDMA_CFG_OFS);
2254f351b2d6SSaeed Bishara 	ZERO(0x004);		/* timer */
2255f351b2d6SSaeed Bishara 	ZERO(0x008);		/* irq err cause */
2256f351b2d6SSaeed Bishara 	ZERO(0x00c);		/* irq err mask */
2257f351b2d6SSaeed Bishara 	ZERO(0x010);		/* rq bah */
2258f351b2d6SSaeed Bishara 	ZERO(0x014);		/* rq inp */
2259f351b2d6SSaeed Bishara 	ZERO(0x018);		/* rq outp */
2260f351b2d6SSaeed Bishara 	ZERO(0x01c);		/* respq bah */
2261f351b2d6SSaeed Bishara 	ZERO(0x024);		/* respq outp */
2262f351b2d6SSaeed Bishara 	ZERO(0x020);		/* respq inp */
2263f351b2d6SSaeed Bishara 	ZERO(0x02c);		/* test control */
2264f351b2d6SSaeed Bishara 	writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
2265f351b2d6SSaeed Bishara }
2266f351b2d6SSaeed Bishara 
2267f351b2d6SSaeed Bishara #undef ZERO
2268f351b2d6SSaeed Bishara 
2269f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
2270f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
2271f351b2d6SSaeed Bishara 				       void __iomem *mmio)
2272f351b2d6SSaeed Bishara {
2273f351b2d6SSaeed Bishara 	void __iomem *hc_mmio = mv_hc_base(mmio, 0);
2274f351b2d6SSaeed Bishara 
2275f351b2d6SSaeed Bishara 	ZERO(0x00c);
2276f351b2d6SSaeed Bishara 	ZERO(0x010);
2277f351b2d6SSaeed Bishara 	ZERO(0x014);
2278f351b2d6SSaeed Bishara 
2279f351b2d6SSaeed Bishara }
2280f351b2d6SSaeed Bishara 
2281f351b2d6SSaeed Bishara #undef ZERO
2282f351b2d6SSaeed Bishara 
2283f351b2d6SSaeed Bishara static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
2284f351b2d6SSaeed Bishara 				  void __iomem *mmio, unsigned int n_hc)
2285f351b2d6SSaeed Bishara {
2286f351b2d6SSaeed Bishara 	unsigned int port;
2287f351b2d6SSaeed Bishara 
2288f351b2d6SSaeed Bishara 	for (port = 0; port < hpriv->n_ports; port++)
2289f351b2d6SSaeed Bishara 		mv_soc_reset_hc_port(hpriv, mmio, port);
2290f351b2d6SSaeed Bishara 
2291f351b2d6SSaeed Bishara 	mv_soc_reset_one_hc(hpriv, mmio);
2292f351b2d6SSaeed Bishara 
2293f351b2d6SSaeed Bishara 	return 0;
2294f351b2d6SSaeed Bishara }
2295f351b2d6SSaeed Bishara 
2296f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
2297f351b2d6SSaeed Bishara 				      void __iomem *mmio)
2298f351b2d6SSaeed Bishara {
2299f351b2d6SSaeed Bishara 	return;
2300f351b2d6SSaeed Bishara }
2301f351b2d6SSaeed Bishara 
2302f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
2303f351b2d6SSaeed Bishara {
2304f351b2d6SSaeed Bishara 	return;
2305f351b2d6SSaeed Bishara }
2306f351b2d6SSaeed Bishara 
2307b67a1064SMark Lord static void mv_setup_ifctl(void __iomem *port_mmio, int want_gen2i)
2308b67a1064SMark Lord {
2309b67a1064SMark Lord 	u32 ifctl = readl(port_mmio + SATA_INTERFACE_CFG);
2310b67a1064SMark Lord 
2311b67a1064SMark Lord 	ifctl = (ifctl & 0xf7f) | 0x9b1000;	/* from chip spec */
2312b67a1064SMark Lord 	if (want_gen2i)
2313b67a1064SMark Lord 		ifctl |= (1 << 7);		/* enable gen2i speed */
2314b67a1064SMark Lord 	writelfl(ifctl, port_mmio + SATA_INTERFACE_CFG);
2315b67a1064SMark Lord }
2316b67a1064SMark Lord 
2317b562468cSMark Lord /*
2318b562468cSMark Lord  * Caller must ensure that EDMA is not active,
2319b562468cSMark Lord  * by first doing mv_stop_edma() where needed.
2320b562468cSMark Lord  */
2321e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
2322c6fd2807SJeff Garzik 			     unsigned int port_no)
2323c6fd2807SJeff Garzik {
2324c6fd2807SJeff Garzik 	void __iomem *port_mmio = mv_port_base(mmio, port_no);
2325c6fd2807SJeff Garzik 
23260d8be5cbSMark Lord 	mv_stop_edma_engine(port_mmio);
2327c6fd2807SJeff Garzik 	writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
2328c6fd2807SJeff Garzik 
2329b67a1064SMark Lord 	if (!IS_GEN_I(hpriv)) {
2330b67a1064SMark Lord 		/* Enable 3.0gb/s link speed */
2331b67a1064SMark Lord 		mv_setup_ifctl(port_mmio, 1);
2332c6fd2807SJeff Garzik 	}
2333b67a1064SMark Lord 	/*
2334b67a1064SMark Lord 	 * Strobing ATA_RST here causes a hard reset of the SATA transport,
2335b67a1064SMark Lord 	 * link, and physical layers.  It resets all SATA interface registers
2336b67a1064SMark Lord 	 * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
2337c6fd2807SJeff Garzik 	 */
2338b67a1064SMark Lord 	writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
2339b67a1064SMark Lord 	udelay(25);	/* allow reset propagation */
2340c6fd2807SJeff Garzik 	writelfl(0, port_mmio + EDMA_CMD_OFS);
2341c6fd2807SJeff Garzik 
2342c6fd2807SJeff Garzik 	hpriv->ops->phy_errata(hpriv, mmio, port_no);
2343c6fd2807SJeff Garzik 
2344ee9ccdf7SJeff Garzik 	if (IS_GEN_I(hpriv))
2345c6fd2807SJeff Garzik 		mdelay(1);
2346c6fd2807SJeff Garzik }
2347c6fd2807SJeff Garzik 
2348e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
2349e49856d8SMark Lord {
2350e49856d8SMark Lord 	if (sata_pmp_supported(ap)) {
2351e49856d8SMark Lord 		void __iomem *port_mmio = mv_ap_base(ap);
2352e49856d8SMark Lord 		u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
2353e49856d8SMark Lord 		int old = reg & 0xf;
2354e49856d8SMark Lord 
2355e49856d8SMark Lord 		if (old != pmp) {
2356e49856d8SMark Lord 			reg = (reg & ~0xf) | pmp;
2357e49856d8SMark Lord 			writelfl(reg, port_mmio + SATA_IFCTL_OFS);
2358e49856d8SMark Lord 		}
2359e49856d8SMark Lord 	}
2360e49856d8SMark Lord }
2361e49856d8SMark Lord 
2362e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
2363bdd4dddeSJeff Garzik 				unsigned long deadline)
2364c6fd2807SJeff Garzik {
2365e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
2366e49856d8SMark Lord 	return sata_std_hardreset(link, class, deadline);
2367e49856d8SMark Lord }
2368c6fd2807SJeff Garzik 
2369e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
2370e49856d8SMark Lord 				unsigned long deadline)
2371da3dbb17STejun Heo {
2372e49856d8SMark Lord 	mv_pmp_select(link->ap, sata_srst_pmp(link));
2373e49856d8SMark Lord 	return ata_sff_softreset(link, class, deadline);
2374bdd4dddeSJeff Garzik }
2375bdd4dddeSJeff Garzik 
2376cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
2377bdd4dddeSJeff Garzik 			unsigned long deadline)
2378bdd4dddeSJeff Garzik {
2379cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
2380bdd4dddeSJeff Garzik 	struct mv_host_priv *hpriv = ap->host->private_data;
2381b562468cSMark Lord 	struct mv_port_priv *pp = ap->private_data;
2382f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
23830d8be5cbSMark Lord 	int rc, attempts = 0, extra = 0;
23840d8be5cbSMark Lord 	u32 sstatus;
23850d8be5cbSMark Lord 	bool online;
2386bdd4dddeSJeff Garzik 
2387e12bef50SMark Lord 	mv_reset_channel(hpriv, mmio, ap->port_no);
2388b562468cSMark Lord 	pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2389bdd4dddeSJeff Garzik 
23900d8be5cbSMark Lord 	/* Workaround for errata FEr SATA#10 (part 2) */
23910d8be5cbSMark Lord 	do {
239217c5aab5SMark Lord 		const unsigned long *timing =
239317c5aab5SMark Lord 				sata_ehc_deb_timing(&link->eh_context);
2394bdd4dddeSJeff Garzik 
239517c5aab5SMark Lord 		rc = sata_link_hardreset(link, timing, deadline + extra,
239617c5aab5SMark Lord 					 &online, NULL);
239717c5aab5SMark Lord 		if (rc)
23980d8be5cbSMark Lord 			return rc;
23990d8be5cbSMark Lord 		sata_scr_read(link, SCR_STATUS, &sstatus);
24000d8be5cbSMark Lord 		if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
24010d8be5cbSMark Lord 			/* Force 1.5gb/s link speed and try again */
24020d8be5cbSMark Lord 			mv_setup_ifctl(mv_ap_base(ap), 0);
24030d8be5cbSMark Lord 			if (time_after(jiffies + HZ, deadline))
24040d8be5cbSMark Lord 				extra = HZ; /* only extend it once, max */
2405bdd4dddeSJeff Garzik 		}
24060d8be5cbSMark Lord 	} while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
2407bdd4dddeSJeff Garzik 
240817c5aab5SMark Lord 	return rc;
2409bdd4dddeSJeff Garzik }
2410bdd4dddeSJeff Garzik 
2411bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
2412c6fd2807SJeff Garzik {
2413f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
2414bdd4dddeSJeff Garzik 	unsigned int hc = (ap->port_no > 3) ? 1 : 0;
2415bdd4dddeSJeff Garzik 	unsigned int shift;
2416*352fab70SMark Lord 	u32 main_mask;
2417c6fd2807SJeff Garzik 
2418bdd4dddeSJeff Garzik 	/* FIXME: handle coalescing completion events properly */
2419c6fd2807SJeff Garzik 
2420bdd4dddeSJeff Garzik 	shift = ap->port_no * 2;
2421bdd4dddeSJeff Garzik 	if (hc > 0)
2422bdd4dddeSJeff Garzik 		shift++;
2423c6fd2807SJeff Garzik 
2424bdd4dddeSJeff Garzik 	/* disable assertion of portN err, done events */
2425*352fab70SMark Lord 	main_mask = readl(hpriv->main_mask_reg_addr);
2426*352fab70SMark Lord 	main_mask &= ~((DONE_IRQ | ERR_IRQ) << shift);
2427*352fab70SMark Lord 	writelfl(main_mask, hpriv->main_mask_reg_addr);
2428c6fd2807SJeff Garzik }
2429bdd4dddeSJeff Garzik 
2430bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
2431bdd4dddeSJeff Garzik {
2432f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv = ap->host->private_data;
2433f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
2434bdd4dddeSJeff Garzik 	unsigned int hc = (ap->port_no > 3) ? 1 : 0;
2435bdd4dddeSJeff Garzik 	void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2436bdd4dddeSJeff Garzik 	void __iomem *port_mmio = mv_ap_base(ap);
2437bdd4dddeSJeff Garzik 	unsigned int shift, hc_port_no = ap->port_no;
2438*352fab70SMark Lord 	u32 main_mask, hc_irq_cause;
2439bdd4dddeSJeff Garzik 
2440bdd4dddeSJeff Garzik 	/* FIXME: handle coalescing completion events properly */
2441bdd4dddeSJeff Garzik 
2442bdd4dddeSJeff Garzik 	shift = ap->port_no * 2;
2443bdd4dddeSJeff Garzik 	if (hc > 0) {
2444bdd4dddeSJeff Garzik 		shift++;
2445bdd4dddeSJeff Garzik 		hc_port_no -= 4;
2446bdd4dddeSJeff Garzik 	}
2447bdd4dddeSJeff Garzik 
2448bdd4dddeSJeff Garzik 	/* clear EDMA errors on this port */
2449bdd4dddeSJeff Garzik 	writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2450bdd4dddeSJeff Garzik 
2451bdd4dddeSJeff Garzik 	/* clear pending irq events */
2452bdd4dddeSJeff Garzik 	hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
2453*352fab70SMark Lord 	hc_irq_cause &= ~((DEV_IRQ | DMA_IRQ) << hc_port_no);
2454bdd4dddeSJeff Garzik 	writel(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
2455bdd4dddeSJeff Garzik 
2456bdd4dddeSJeff Garzik 	/* enable assertion of portN err, done events */
2457*352fab70SMark Lord 	main_mask = readl(hpriv->main_mask_reg_addr);
2458*352fab70SMark Lord 	main_mask |= ((DONE_IRQ | ERR_IRQ) << shift);
2459*352fab70SMark Lord 	writelfl(main_mask, hpriv->main_mask_reg_addr);
2460c6fd2807SJeff Garzik }
2461c6fd2807SJeff Garzik 
2462c6fd2807SJeff Garzik /**
2463c6fd2807SJeff Garzik  *      mv_port_init - Perform some early initialization on a single port.
2464c6fd2807SJeff Garzik  *      @port: libata data structure storing shadow register addresses
2465c6fd2807SJeff Garzik  *      @port_mmio: base address of the port
2466c6fd2807SJeff Garzik  *
2467c6fd2807SJeff Garzik  *      Initialize shadow register mmio addresses, clear outstanding
2468c6fd2807SJeff Garzik  *      interrupts on the port, and unmask interrupts for the future
2469c6fd2807SJeff Garzik  *      start of the port.
2470c6fd2807SJeff Garzik  *
2471c6fd2807SJeff Garzik  *      LOCKING:
2472c6fd2807SJeff Garzik  *      Inherited from caller.
2473c6fd2807SJeff Garzik  */
2474c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port,  void __iomem *port_mmio)
2475c6fd2807SJeff Garzik {
24760d5ff566STejun Heo 	void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
2477c6fd2807SJeff Garzik 	unsigned serr_ofs;
2478c6fd2807SJeff Garzik 
2479c6fd2807SJeff Garzik 	/* PIO related setup
2480c6fd2807SJeff Garzik 	 */
2481c6fd2807SJeff Garzik 	port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
2482c6fd2807SJeff Garzik 	port->error_addr =
2483c6fd2807SJeff Garzik 		port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
2484c6fd2807SJeff Garzik 	port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
2485c6fd2807SJeff Garzik 	port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
2486c6fd2807SJeff Garzik 	port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
2487c6fd2807SJeff Garzik 	port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
2488c6fd2807SJeff Garzik 	port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
2489c6fd2807SJeff Garzik 	port->status_addr =
2490c6fd2807SJeff Garzik 		port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
2491c6fd2807SJeff Garzik 	/* special case: control/altstatus doesn't have ATA_REG_ address */
2492c6fd2807SJeff Garzik 	port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
2493c6fd2807SJeff Garzik 
2494c6fd2807SJeff Garzik 	/* unused: */
24958d9db2d2SRandy Dunlap 	port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
2496c6fd2807SJeff Garzik 
2497c6fd2807SJeff Garzik 	/* Clear any currently outstanding port interrupt conditions */
2498c6fd2807SJeff Garzik 	serr_ofs = mv_scr_offset(SCR_ERROR);
2499c6fd2807SJeff Garzik 	writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
2500c6fd2807SJeff Garzik 	writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2501c6fd2807SJeff Garzik 
2502646a4da5SMark Lord 	/* unmask all non-transient EDMA error interrupts */
2503646a4da5SMark Lord 	writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
2504c6fd2807SJeff Garzik 
2505c6fd2807SJeff Garzik 	VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
2506c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_CFG_OFS),
2507c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
2508c6fd2807SJeff Garzik 		readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
2509c6fd2807SJeff Garzik }
2510c6fd2807SJeff Garzik 
25114447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
2512c6fd2807SJeff Garzik {
25134447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
25144447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
2515c6fd2807SJeff Garzik 	u32 hp_flags = hpriv->hp_flags;
2516c6fd2807SJeff Garzik 
2517c6fd2807SJeff Garzik 	switch (board_idx) {
2518c6fd2807SJeff Garzik 	case chip_5080:
2519c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
2520ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
2521c6fd2807SJeff Garzik 
252244c10138SAuke Kok 		switch (pdev->revision) {
2523c6fd2807SJeff Garzik 		case 0x1:
2524c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
2525c6fd2807SJeff Garzik 			break;
2526c6fd2807SJeff Garzik 		case 0x3:
2527c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2528c6fd2807SJeff Garzik 			break;
2529c6fd2807SJeff Garzik 		default:
2530c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2531c6fd2807SJeff Garzik 			   "Applying 50XXB2 workarounds to unknown rev\n");
2532c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2533c6fd2807SJeff Garzik 			break;
2534c6fd2807SJeff Garzik 		}
2535c6fd2807SJeff Garzik 		break;
2536c6fd2807SJeff Garzik 
2537c6fd2807SJeff Garzik 	case chip_504x:
2538c6fd2807SJeff Garzik 	case chip_508x:
2539c6fd2807SJeff Garzik 		hpriv->ops = &mv5xxx_ops;
2540ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_I;
2541c6fd2807SJeff Garzik 
254244c10138SAuke Kok 		switch (pdev->revision) {
2543c6fd2807SJeff Garzik 		case 0x0:
2544c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB0;
2545c6fd2807SJeff Garzik 			break;
2546c6fd2807SJeff Garzik 		case 0x3:
2547c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2548c6fd2807SJeff Garzik 			break;
2549c6fd2807SJeff Garzik 		default:
2550c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2551c6fd2807SJeff Garzik 			   "Applying B2 workarounds to unknown rev\n");
2552c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_50XXB2;
2553c6fd2807SJeff Garzik 			break;
2554c6fd2807SJeff Garzik 		}
2555c6fd2807SJeff Garzik 		break;
2556c6fd2807SJeff Garzik 
2557c6fd2807SJeff Garzik 	case chip_604x:
2558c6fd2807SJeff Garzik 	case chip_608x:
2559c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
2560ee9ccdf7SJeff Garzik 		hp_flags |= MV_HP_GEN_II;
2561c6fd2807SJeff Garzik 
256244c10138SAuke Kok 		switch (pdev->revision) {
2563c6fd2807SJeff Garzik 		case 0x7:
2564c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
2565c6fd2807SJeff Garzik 			break;
2566c6fd2807SJeff Garzik 		case 0x9:
2567c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2568c6fd2807SJeff Garzik 			break;
2569c6fd2807SJeff Garzik 		default:
2570c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2571c6fd2807SJeff Garzik 				   "Applying B2 workarounds to unknown rev\n");
2572c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1B2;
2573c6fd2807SJeff Garzik 			break;
2574c6fd2807SJeff Garzik 		}
2575c6fd2807SJeff Garzik 		break;
2576c6fd2807SJeff Garzik 
2577c6fd2807SJeff Garzik 	case chip_7042:
257802a121daSMark Lord 		hp_flags |= MV_HP_PCIE;
2579306b30f7SMark Lord 		if (pdev->vendor == PCI_VENDOR_ID_TTI &&
2580306b30f7SMark Lord 		    (pdev->device == 0x2300 || pdev->device == 0x2310))
2581306b30f7SMark Lord 		{
25824e520033SMark Lord 			/*
25834e520033SMark Lord 			 * Highpoint RocketRAID PCIe 23xx series cards:
25844e520033SMark Lord 			 *
25854e520033SMark Lord 			 * Unconfigured drives are treated as "Legacy"
25864e520033SMark Lord 			 * by the BIOS, and it overwrites sector 8 with
25874e520033SMark Lord 			 * a "Lgcy" metadata block prior to Linux boot.
25884e520033SMark Lord 			 *
25894e520033SMark Lord 			 * Configured drives (RAID or JBOD) leave sector 8
25904e520033SMark Lord 			 * alone, but instead overwrite a high numbered
25914e520033SMark Lord 			 * sector for the RAID metadata.  This sector can
25924e520033SMark Lord 			 * be determined exactly, by truncating the physical
25934e520033SMark Lord 			 * drive capacity to a nice even GB value.
25944e520033SMark Lord 			 *
25954e520033SMark Lord 			 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
25964e520033SMark Lord 			 *
25974e520033SMark Lord 			 * Warn the user, lest they think we're just buggy.
25984e520033SMark Lord 			 */
25994e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
26004e520033SMark Lord 				" BIOS CORRUPTS DATA on all attached drives,"
26014e520033SMark Lord 				" regardless of if/how they are configured."
26024e520033SMark Lord 				" BEWARE!\n");
26034e520033SMark Lord 			printk(KERN_WARNING DRV_NAME ": For data safety, do not"
26044e520033SMark Lord 				" use sectors 8-9 on \"Legacy\" drives,"
26054e520033SMark Lord 				" and avoid the final two gigabytes on"
26064e520033SMark Lord 				" all RocketRAID BIOS initialized drives.\n");
2607306b30f7SMark Lord 		}
2608c6fd2807SJeff Garzik 	case chip_6042:
2609c6fd2807SJeff Garzik 		hpriv->ops = &mv6xxx_ops;
2610c6fd2807SJeff Garzik 		hp_flags |= MV_HP_GEN_IIE;
2611c6fd2807SJeff Garzik 
261244c10138SAuke Kok 		switch (pdev->revision) {
2613c6fd2807SJeff Garzik 		case 0x0:
2614c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_XX42A0;
2615c6fd2807SJeff Garzik 			break;
2616c6fd2807SJeff Garzik 		case 0x1:
2617c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2618c6fd2807SJeff Garzik 			break;
2619c6fd2807SJeff Garzik 		default:
2620c6fd2807SJeff Garzik 			dev_printk(KERN_WARNING, &pdev->dev,
2621c6fd2807SJeff Garzik 			   "Applying 60X1C0 workarounds to unknown rev\n");
2622c6fd2807SJeff Garzik 			hp_flags |= MV_HP_ERRATA_60X1C0;
2623c6fd2807SJeff Garzik 			break;
2624c6fd2807SJeff Garzik 		}
2625c6fd2807SJeff Garzik 		break;
2626f351b2d6SSaeed Bishara 	case chip_soc:
2627f351b2d6SSaeed Bishara 		hpriv->ops = &mv_soc_ops;
2628f351b2d6SSaeed Bishara 		hp_flags |= MV_HP_ERRATA_60X1C0;
2629f351b2d6SSaeed Bishara 		break;
2630c6fd2807SJeff Garzik 
2631c6fd2807SJeff Garzik 	default:
2632f351b2d6SSaeed Bishara 		dev_printk(KERN_ERR, host->dev,
26335796d1c4SJeff Garzik 			   "BUG: invalid board index %u\n", board_idx);
2634c6fd2807SJeff Garzik 		return 1;
2635c6fd2807SJeff Garzik 	}
2636c6fd2807SJeff Garzik 
2637c6fd2807SJeff Garzik 	hpriv->hp_flags = hp_flags;
263802a121daSMark Lord 	if (hp_flags & MV_HP_PCIE) {
263902a121daSMark Lord 		hpriv->irq_cause_ofs	= PCIE_IRQ_CAUSE_OFS;
264002a121daSMark Lord 		hpriv->irq_mask_ofs	= PCIE_IRQ_MASK_OFS;
264102a121daSMark Lord 		hpriv->unmask_all_irqs	= PCIE_UNMASK_ALL_IRQS;
264202a121daSMark Lord 	} else {
264302a121daSMark Lord 		hpriv->irq_cause_ofs	= PCI_IRQ_CAUSE_OFS;
264402a121daSMark Lord 		hpriv->irq_mask_ofs	= PCI_IRQ_MASK_OFS;
264502a121daSMark Lord 		hpriv->unmask_all_irqs	= PCI_UNMASK_ALL_IRQS;
264602a121daSMark Lord 	}
2647c6fd2807SJeff Garzik 
2648c6fd2807SJeff Garzik 	return 0;
2649c6fd2807SJeff Garzik }
2650c6fd2807SJeff Garzik 
2651c6fd2807SJeff Garzik /**
2652c6fd2807SJeff Garzik  *      mv_init_host - Perform some early initialization of the host.
26534447d351STejun Heo  *	@host: ATA host to initialize
26544447d351STejun Heo  *      @board_idx: controller index
2655c6fd2807SJeff Garzik  *
2656c6fd2807SJeff Garzik  *      If possible, do an early global reset of the host.  Then do
2657c6fd2807SJeff Garzik  *      our port init and clear/unmask all/relevant host interrupts.
2658c6fd2807SJeff Garzik  *
2659c6fd2807SJeff Garzik  *      LOCKING:
2660c6fd2807SJeff Garzik  *      Inherited from caller.
2661c6fd2807SJeff Garzik  */
26624447d351STejun Heo static int mv_init_host(struct ata_host *host, unsigned int board_idx)
2663c6fd2807SJeff Garzik {
2664c6fd2807SJeff Garzik 	int rc = 0, n_hc, port, hc;
26654447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
2666f351b2d6SSaeed Bishara 	void __iomem *mmio = hpriv->base;
2667c6fd2807SJeff Garzik 
26684447d351STejun Heo 	rc = mv_chip_id(host, board_idx);
2669c6fd2807SJeff Garzik 	if (rc)
2670c6fd2807SJeff Garzik 		goto done;
2671c6fd2807SJeff Garzik 
2672f351b2d6SSaeed Bishara 	if (HAS_PCI(host)) {
2673*352fab70SMark Lord 		hpriv->main_cause_reg_addr = mmio + HC_MAIN_IRQ_CAUSE_OFS;
2674*352fab70SMark Lord 		hpriv->main_mask_reg_addr  = mmio + HC_MAIN_IRQ_MASK_OFS;
2675f351b2d6SSaeed Bishara 	} else {
2676*352fab70SMark Lord 		hpriv->main_cause_reg_addr = mmio + HC_SOC_MAIN_IRQ_CAUSE_OFS;
2677*352fab70SMark Lord 		hpriv->main_mask_reg_addr  = mmio + HC_SOC_MAIN_IRQ_MASK_OFS;
2678f351b2d6SSaeed Bishara 	}
2679*352fab70SMark Lord 
2680*352fab70SMark Lord 	/* global interrupt mask: 0 == mask everything */
2681f351b2d6SSaeed Bishara 	writel(0, hpriv->main_mask_reg_addr);
2682f351b2d6SSaeed Bishara 
26834447d351STejun Heo 	n_hc = mv_get_hc_count(host->ports[0]->flags);
2684c6fd2807SJeff Garzik 
26854447d351STejun Heo 	for (port = 0; port < host->n_ports; port++)
2686c6fd2807SJeff Garzik 		hpriv->ops->read_preamp(hpriv, port, mmio);
2687c6fd2807SJeff Garzik 
2688c6fd2807SJeff Garzik 	rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
2689c6fd2807SJeff Garzik 	if (rc)
2690c6fd2807SJeff Garzik 		goto done;
2691c6fd2807SJeff Garzik 
2692c6fd2807SJeff Garzik 	hpriv->ops->reset_flash(hpriv, mmio);
26937bb3c529SSaeed Bishara 	hpriv->ops->reset_bus(host, mmio);
2694c6fd2807SJeff Garzik 	hpriv->ops->enable_leds(hpriv, mmio);
2695c6fd2807SJeff Garzik 
26964447d351STejun Heo 	for (port = 0; port < host->n_ports; port++) {
2697cbcdd875STejun Heo 		struct ata_port *ap = host->ports[port];
2698c6fd2807SJeff Garzik 		void __iomem *port_mmio = mv_port_base(mmio, port);
2699cbcdd875STejun Heo 
2700cbcdd875STejun Heo 		mv_port_init(&ap->ioaddr, port_mmio);
2701cbcdd875STejun Heo 
27027bb3c529SSaeed Bishara #ifdef CONFIG_PCI
2703f351b2d6SSaeed Bishara 		if (HAS_PCI(host)) {
2704f351b2d6SSaeed Bishara 			unsigned int offset = port_mmio - mmio;
2705cbcdd875STejun Heo 			ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
2706cbcdd875STejun Heo 			ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
2707f351b2d6SSaeed Bishara 		}
27087bb3c529SSaeed Bishara #endif
2709c6fd2807SJeff Garzik 	}
2710c6fd2807SJeff Garzik 
2711c6fd2807SJeff Garzik 	for (hc = 0; hc < n_hc; hc++) {
2712c6fd2807SJeff Garzik 		void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2713c6fd2807SJeff Garzik 
2714c6fd2807SJeff Garzik 		VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
2715c6fd2807SJeff Garzik 			"(before clear)=0x%08x\n", hc,
2716c6fd2807SJeff Garzik 			readl(hc_mmio + HC_CFG_OFS),
2717c6fd2807SJeff Garzik 			readl(hc_mmio + HC_IRQ_CAUSE_OFS));
2718c6fd2807SJeff Garzik 
2719c6fd2807SJeff Garzik 		/* Clear any currently outstanding hc interrupt conditions */
2720c6fd2807SJeff Garzik 		writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
2721c6fd2807SJeff Garzik 	}
2722c6fd2807SJeff Garzik 
2723f351b2d6SSaeed Bishara 	if (HAS_PCI(host)) {
2724c6fd2807SJeff Garzik 		/* Clear any currently outstanding host interrupt conditions */
272502a121daSMark Lord 		writelfl(0, mmio + hpriv->irq_cause_ofs);
2726c6fd2807SJeff Garzik 
2727c6fd2807SJeff Garzik 		/* and unmask interrupt generation for host regs */
272802a121daSMark Lord 		writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
2729ee9ccdf7SJeff Garzik 		if (IS_GEN_I(hpriv))
2730f351b2d6SSaeed Bishara 			writelfl(~HC_MAIN_MASKED_IRQS_5,
2731f351b2d6SSaeed Bishara 				 hpriv->main_mask_reg_addr);
2732fb621e2fSJeff Garzik 		else
2733f351b2d6SSaeed Bishara 			writelfl(~HC_MAIN_MASKED_IRQS,
2734f351b2d6SSaeed Bishara 				 hpriv->main_mask_reg_addr);
2735c6fd2807SJeff Garzik 
2736c6fd2807SJeff Garzik 		VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
2737c6fd2807SJeff Garzik 			"PCI int cause/mask=0x%08x/0x%08x\n",
2738f351b2d6SSaeed Bishara 			readl(hpriv->main_cause_reg_addr),
2739f351b2d6SSaeed Bishara 			readl(hpriv->main_mask_reg_addr),
274002a121daSMark Lord 			readl(mmio + hpriv->irq_cause_ofs),
274102a121daSMark Lord 			readl(mmio + hpriv->irq_mask_ofs));
2742f351b2d6SSaeed Bishara 	} else {
2743f351b2d6SSaeed Bishara 		writelfl(~HC_MAIN_MASKED_IRQS_SOC,
2744f351b2d6SSaeed Bishara 			 hpriv->main_mask_reg_addr);
2745f351b2d6SSaeed Bishara 		VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x\n",
2746f351b2d6SSaeed Bishara 			readl(hpriv->main_cause_reg_addr),
2747f351b2d6SSaeed Bishara 			readl(hpriv->main_mask_reg_addr));
2748f351b2d6SSaeed Bishara 	}
2749c6fd2807SJeff Garzik done:
2750c6fd2807SJeff Garzik 	return rc;
2751c6fd2807SJeff Garzik }
2752c6fd2807SJeff Garzik 
2753fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
2754fbf14e2fSByron Bradley {
2755fbf14e2fSByron Bradley 	hpriv->crqb_pool   = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
2756fbf14e2fSByron Bradley 							     MV_CRQB_Q_SZ, 0);
2757fbf14e2fSByron Bradley 	if (!hpriv->crqb_pool)
2758fbf14e2fSByron Bradley 		return -ENOMEM;
2759fbf14e2fSByron Bradley 
2760fbf14e2fSByron Bradley 	hpriv->crpb_pool   = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
2761fbf14e2fSByron Bradley 							     MV_CRPB_Q_SZ, 0);
2762fbf14e2fSByron Bradley 	if (!hpriv->crpb_pool)
2763fbf14e2fSByron Bradley 		return -ENOMEM;
2764fbf14e2fSByron Bradley 
2765fbf14e2fSByron Bradley 	hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
2766fbf14e2fSByron Bradley 							     MV_SG_TBL_SZ, 0);
2767fbf14e2fSByron Bradley 	if (!hpriv->sg_tbl_pool)
2768fbf14e2fSByron Bradley 		return -ENOMEM;
2769fbf14e2fSByron Bradley 
2770fbf14e2fSByron Bradley 	return 0;
2771fbf14e2fSByron Bradley }
2772fbf14e2fSByron Bradley 
277315a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
277415a32632SLennert Buytenhek 				 struct mbus_dram_target_info *dram)
277515a32632SLennert Buytenhek {
277615a32632SLennert Buytenhek 	int i;
277715a32632SLennert Buytenhek 
277815a32632SLennert Buytenhek 	for (i = 0; i < 4; i++) {
277915a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_CTRL(i));
278015a32632SLennert Buytenhek 		writel(0, hpriv->base + WINDOW_BASE(i));
278115a32632SLennert Buytenhek 	}
278215a32632SLennert Buytenhek 
278315a32632SLennert Buytenhek 	for (i = 0; i < dram->num_cs; i++) {
278415a32632SLennert Buytenhek 		struct mbus_dram_window *cs = dram->cs + i;
278515a32632SLennert Buytenhek 
278615a32632SLennert Buytenhek 		writel(((cs->size - 1) & 0xffff0000) |
278715a32632SLennert Buytenhek 			(cs->mbus_attr << 8) |
278815a32632SLennert Buytenhek 			(dram->mbus_dram_target_id << 4) | 1,
278915a32632SLennert Buytenhek 			hpriv->base + WINDOW_CTRL(i));
279015a32632SLennert Buytenhek 		writel(cs->base, hpriv->base + WINDOW_BASE(i));
279115a32632SLennert Buytenhek 	}
279215a32632SLennert Buytenhek }
279315a32632SLennert Buytenhek 
2794f351b2d6SSaeed Bishara /**
2795f351b2d6SSaeed Bishara  *      mv_platform_probe - handle a positive probe of an soc Marvell
2796f351b2d6SSaeed Bishara  *      host
2797f351b2d6SSaeed Bishara  *      @pdev: platform device found
2798f351b2d6SSaeed Bishara  *
2799f351b2d6SSaeed Bishara  *      LOCKING:
2800f351b2d6SSaeed Bishara  *      Inherited from caller.
2801f351b2d6SSaeed Bishara  */
2802f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
2803f351b2d6SSaeed Bishara {
2804f351b2d6SSaeed Bishara 	static int printed_version;
2805f351b2d6SSaeed Bishara 	const struct mv_sata_platform_data *mv_platform_data;
2806f351b2d6SSaeed Bishara 	const struct ata_port_info *ppi[] =
2807f351b2d6SSaeed Bishara 	    { &mv_port_info[chip_soc], NULL };
2808f351b2d6SSaeed Bishara 	struct ata_host *host;
2809f351b2d6SSaeed Bishara 	struct mv_host_priv *hpriv;
2810f351b2d6SSaeed Bishara 	struct resource *res;
2811f351b2d6SSaeed Bishara 	int n_ports, rc;
2812f351b2d6SSaeed Bishara 
2813f351b2d6SSaeed Bishara 	if (!printed_version++)
2814f351b2d6SSaeed Bishara 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
2815f351b2d6SSaeed Bishara 
2816f351b2d6SSaeed Bishara 	/*
2817f351b2d6SSaeed Bishara 	 * Simple resource validation ..
2818f351b2d6SSaeed Bishara 	 */
2819f351b2d6SSaeed Bishara 	if (unlikely(pdev->num_resources != 2)) {
2820f351b2d6SSaeed Bishara 		dev_err(&pdev->dev, "invalid number of resources\n");
2821f351b2d6SSaeed Bishara 		return -EINVAL;
2822f351b2d6SSaeed Bishara 	}
2823f351b2d6SSaeed Bishara 
2824f351b2d6SSaeed Bishara 	/*
2825f351b2d6SSaeed Bishara 	 * Get the register base first
2826f351b2d6SSaeed Bishara 	 */
2827f351b2d6SSaeed Bishara 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2828f351b2d6SSaeed Bishara 	if (res == NULL)
2829f351b2d6SSaeed Bishara 		return -EINVAL;
2830f351b2d6SSaeed Bishara 
2831f351b2d6SSaeed Bishara 	/* allocate host */
2832f351b2d6SSaeed Bishara 	mv_platform_data = pdev->dev.platform_data;
2833f351b2d6SSaeed Bishara 	n_ports = mv_platform_data->n_ports;
2834f351b2d6SSaeed Bishara 
2835f351b2d6SSaeed Bishara 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
2836f351b2d6SSaeed Bishara 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
2837f351b2d6SSaeed Bishara 
2838f351b2d6SSaeed Bishara 	if (!host || !hpriv)
2839f351b2d6SSaeed Bishara 		return -ENOMEM;
2840f351b2d6SSaeed Bishara 	host->private_data = hpriv;
2841f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
2842f351b2d6SSaeed Bishara 
2843f351b2d6SSaeed Bishara 	host->iomap = NULL;
2844f1cb0ea1SSaeed Bishara 	hpriv->base = devm_ioremap(&pdev->dev, res->start,
2845f1cb0ea1SSaeed Bishara 				   res->end - res->start + 1);
2846f351b2d6SSaeed Bishara 	hpriv->base -= MV_SATAHC0_REG_BASE;
2847f351b2d6SSaeed Bishara 
284815a32632SLennert Buytenhek 	/*
284915a32632SLennert Buytenhek 	 * (Re-)program MBUS remapping windows if we are asked to.
285015a32632SLennert Buytenhek 	 */
285115a32632SLennert Buytenhek 	if (mv_platform_data->dram != NULL)
285215a32632SLennert Buytenhek 		mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
285315a32632SLennert Buytenhek 
2854fbf14e2fSByron Bradley 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
2855fbf14e2fSByron Bradley 	if (rc)
2856fbf14e2fSByron Bradley 		return rc;
2857fbf14e2fSByron Bradley 
2858f351b2d6SSaeed Bishara 	/* initialize adapter */
2859f351b2d6SSaeed Bishara 	rc = mv_init_host(host, chip_soc);
2860f351b2d6SSaeed Bishara 	if (rc)
2861f351b2d6SSaeed Bishara 		return rc;
2862f351b2d6SSaeed Bishara 
2863f351b2d6SSaeed Bishara 	dev_printk(KERN_INFO, &pdev->dev,
2864f351b2d6SSaeed Bishara 		   "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
2865f351b2d6SSaeed Bishara 		   host->n_ports);
2866f351b2d6SSaeed Bishara 
2867f351b2d6SSaeed Bishara 	return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
2868f351b2d6SSaeed Bishara 				 IRQF_SHARED, &mv6_sht);
2869f351b2d6SSaeed Bishara }
2870f351b2d6SSaeed Bishara 
2871f351b2d6SSaeed Bishara /*
2872f351b2d6SSaeed Bishara  *
2873f351b2d6SSaeed Bishara  *      mv_platform_remove    -       unplug a platform interface
2874f351b2d6SSaeed Bishara  *      @pdev: platform device
2875f351b2d6SSaeed Bishara  *
2876f351b2d6SSaeed Bishara  *      A platform bus SATA device has been unplugged. Perform the needed
2877f351b2d6SSaeed Bishara  *      cleanup. Also called on module unload for any active devices.
2878f351b2d6SSaeed Bishara  */
2879f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev)
2880f351b2d6SSaeed Bishara {
2881f351b2d6SSaeed Bishara 	struct device *dev = &pdev->dev;
2882f351b2d6SSaeed Bishara 	struct ata_host *host = dev_get_drvdata(dev);
2883f351b2d6SSaeed Bishara 
2884f351b2d6SSaeed Bishara 	ata_host_detach(host);
2885f351b2d6SSaeed Bishara 	return 0;
2886f351b2d6SSaeed Bishara }
2887f351b2d6SSaeed Bishara 
2888f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
2889f351b2d6SSaeed Bishara 	.probe			= mv_platform_probe,
2890f351b2d6SSaeed Bishara 	.remove			= __devexit_p(mv_platform_remove),
2891f351b2d6SSaeed Bishara 	.driver			= {
2892f351b2d6SSaeed Bishara 				   .name = DRV_NAME,
2893f351b2d6SSaeed Bishara 				   .owner = THIS_MODULE,
2894f351b2d6SSaeed Bishara 				  },
2895f351b2d6SSaeed Bishara };
2896f351b2d6SSaeed Bishara 
2897f351b2d6SSaeed Bishara 
28987bb3c529SSaeed Bishara #ifdef CONFIG_PCI
2899f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
2900f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent);
2901f351b2d6SSaeed Bishara 
29027bb3c529SSaeed Bishara 
29037bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
29047bb3c529SSaeed Bishara 	.name			= DRV_NAME,
29057bb3c529SSaeed Bishara 	.id_table		= mv_pci_tbl,
2906f351b2d6SSaeed Bishara 	.probe			= mv_pci_init_one,
29077bb3c529SSaeed Bishara 	.remove			= ata_pci_remove_one,
29087bb3c529SSaeed Bishara };
29097bb3c529SSaeed Bishara 
29107bb3c529SSaeed Bishara /*
29117bb3c529SSaeed Bishara  * module options
29127bb3c529SSaeed Bishara  */
29137bb3c529SSaeed Bishara static int msi;	      /* Use PCI msi; either zero (off, default) or non-zero */
29147bb3c529SSaeed Bishara 
29157bb3c529SSaeed Bishara 
29167bb3c529SSaeed Bishara /* move to PCI layer or libata core? */
29177bb3c529SSaeed Bishara static int pci_go_64(struct pci_dev *pdev)
29187bb3c529SSaeed Bishara {
29197bb3c529SSaeed Bishara 	int rc;
29207bb3c529SSaeed Bishara 
29217bb3c529SSaeed Bishara 	if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
29227bb3c529SSaeed Bishara 		rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
29237bb3c529SSaeed Bishara 		if (rc) {
29247bb3c529SSaeed Bishara 			rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
29257bb3c529SSaeed Bishara 			if (rc) {
29267bb3c529SSaeed Bishara 				dev_printk(KERN_ERR, &pdev->dev,
29277bb3c529SSaeed Bishara 					   "64-bit DMA enable failed\n");
29287bb3c529SSaeed Bishara 				return rc;
29297bb3c529SSaeed Bishara 			}
29307bb3c529SSaeed Bishara 		}
29317bb3c529SSaeed Bishara 	} else {
29327bb3c529SSaeed Bishara 		rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
29337bb3c529SSaeed Bishara 		if (rc) {
29347bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
29357bb3c529SSaeed Bishara 				   "32-bit DMA enable failed\n");
29367bb3c529SSaeed Bishara 			return rc;
29377bb3c529SSaeed Bishara 		}
29387bb3c529SSaeed Bishara 		rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
29397bb3c529SSaeed Bishara 		if (rc) {
29407bb3c529SSaeed Bishara 			dev_printk(KERN_ERR, &pdev->dev,
29417bb3c529SSaeed Bishara 				   "32-bit consistent DMA enable failed\n");
29427bb3c529SSaeed Bishara 			return rc;
29437bb3c529SSaeed Bishara 		}
29447bb3c529SSaeed Bishara 	}
29457bb3c529SSaeed Bishara 
29467bb3c529SSaeed Bishara 	return rc;
29477bb3c529SSaeed Bishara }
29487bb3c529SSaeed Bishara 
2949c6fd2807SJeff Garzik /**
2950c6fd2807SJeff Garzik  *      mv_print_info - Dump key info to kernel log for perusal.
29514447d351STejun Heo  *      @host: ATA host to print info about
2952c6fd2807SJeff Garzik  *
2953c6fd2807SJeff Garzik  *      FIXME: complete this.
2954c6fd2807SJeff Garzik  *
2955c6fd2807SJeff Garzik  *      LOCKING:
2956c6fd2807SJeff Garzik  *      Inherited from caller.
2957c6fd2807SJeff Garzik  */
29584447d351STejun Heo static void mv_print_info(struct ata_host *host)
2959c6fd2807SJeff Garzik {
29604447d351STejun Heo 	struct pci_dev *pdev = to_pci_dev(host->dev);
29614447d351STejun Heo 	struct mv_host_priv *hpriv = host->private_data;
296244c10138SAuke Kok 	u8 scc;
2963c1e4fe71SJeff Garzik 	const char *scc_s, *gen;
2964c6fd2807SJeff Garzik 
2965c6fd2807SJeff Garzik 	/* Use this to determine the HW stepping of the chip so we know
2966c6fd2807SJeff Garzik 	 * what errata to workaround
2967c6fd2807SJeff Garzik 	 */
2968c6fd2807SJeff Garzik 	pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
2969c6fd2807SJeff Garzik 	if (scc == 0)
2970c6fd2807SJeff Garzik 		scc_s = "SCSI";
2971c6fd2807SJeff Garzik 	else if (scc == 0x01)
2972c6fd2807SJeff Garzik 		scc_s = "RAID";
2973c6fd2807SJeff Garzik 	else
2974c1e4fe71SJeff Garzik 		scc_s = "?";
2975c1e4fe71SJeff Garzik 
2976c1e4fe71SJeff Garzik 	if (IS_GEN_I(hpriv))
2977c1e4fe71SJeff Garzik 		gen = "I";
2978c1e4fe71SJeff Garzik 	else if (IS_GEN_II(hpriv))
2979c1e4fe71SJeff Garzik 		gen = "II";
2980c1e4fe71SJeff Garzik 	else if (IS_GEN_IIE(hpriv))
2981c1e4fe71SJeff Garzik 		gen = "IIE";
2982c1e4fe71SJeff Garzik 	else
2983c1e4fe71SJeff Garzik 		gen = "?";
2984c6fd2807SJeff Garzik 
2985c6fd2807SJeff Garzik 	dev_printk(KERN_INFO, &pdev->dev,
2986c1e4fe71SJeff Garzik 	       "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
2987c1e4fe71SJeff Garzik 	       gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
2988c6fd2807SJeff Garzik 	       scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
2989c6fd2807SJeff Garzik }
2990c6fd2807SJeff Garzik 
2991c6fd2807SJeff Garzik /**
2992f351b2d6SSaeed Bishara  *      mv_pci_init_one - handle a positive probe of a PCI Marvell host
2993c6fd2807SJeff Garzik  *      @pdev: PCI device found
2994c6fd2807SJeff Garzik  *      @ent: PCI device ID entry for the matched host
2995c6fd2807SJeff Garzik  *
2996c6fd2807SJeff Garzik  *      LOCKING:
2997c6fd2807SJeff Garzik  *      Inherited from caller.
2998c6fd2807SJeff Garzik  */
2999f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
3000f351b2d6SSaeed Bishara 			   const struct pci_device_id *ent)
3001c6fd2807SJeff Garzik {
30022dcb407eSJeff Garzik 	static int printed_version;
3003c6fd2807SJeff Garzik 	unsigned int board_idx = (unsigned int)ent->driver_data;
30044447d351STejun Heo 	const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
30054447d351STejun Heo 	struct ata_host *host;
30064447d351STejun Heo 	struct mv_host_priv *hpriv;
30074447d351STejun Heo 	int n_ports, rc;
3008c6fd2807SJeff Garzik 
3009c6fd2807SJeff Garzik 	if (!printed_version++)
3010c6fd2807SJeff Garzik 		dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
3011c6fd2807SJeff Garzik 
30124447d351STejun Heo 	/* allocate host */
30134447d351STejun Heo 	n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
30144447d351STejun Heo 
30154447d351STejun Heo 	host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
30164447d351STejun Heo 	hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
30174447d351STejun Heo 	if (!host || !hpriv)
30184447d351STejun Heo 		return -ENOMEM;
30194447d351STejun Heo 	host->private_data = hpriv;
3020f351b2d6SSaeed Bishara 	hpriv->n_ports = n_ports;
30214447d351STejun Heo 
30224447d351STejun Heo 	/* acquire resources */
302324dc5f33STejun Heo 	rc = pcim_enable_device(pdev);
302424dc5f33STejun Heo 	if (rc)
3025c6fd2807SJeff Garzik 		return rc;
3026c6fd2807SJeff Garzik 
30270d5ff566STejun Heo 	rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
30280d5ff566STejun Heo 	if (rc == -EBUSY)
302924dc5f33STejun Heo 		pcim_pin_device(pdev);
30300d5ff566STejun Heo 	if (rc)
303124dc5f33STejun Heo 		return rc;
30324447d351STejun Heo 	host->iomap = pcim_iomap_table(pdev);
3033f351b2d6SSaeed Bishara 	hpriv->base = host->iomap[MV_PRIMARY_BAR];
3034c6fd2807SJeff Garzik 
3035d88184fbSJeff Garzik 	rc = pci_go_64(pdev);
3036d88184fbSJeff Garzik 	if (rc)
3037d88184fbSJeff Garzik 		return rc;
3038d88184fbSJeff Garzik 
3039da2fa9baSMark Lord 	rc = mv_create_dma_pools(hpriv, &pdev->dev);
3040da2fa9baSMark Lord 	if (rc)
3041da2fa9baSMark Lord 		return rc;
3042da2fa9baSMark Lord 
3043c6fd2807SJeff Garzik 	/* initialize adapter */
30444447d351STejun Heo 	rc = mv_init_host(host, board_idx);
304524dc5f33STejun Heo 	if (rc)
304624dc5f33STejun Heo 		return rc;
3047c6fd2807SJeff Garzik 
3048c6fd2807SJeff Garzik 	/* Enable interrupts */
30496a59dcf8STejun Heo 	if (msi && pci_enable_msi(pdev))
3050c6fd2807SJeff Garzik 		pci_intx(pdev, 1);
3051c6fd2807SJeff Garzik 
3052c6fd2807SJeff Garzik 	mv_dump_pci_cfg(pdev, 0x68);
30534447d351STejun Heo 	mv_print_info(host);
3054c6fd2807SJeff Garzik 
30554447d351STejun Heo 	pci_set_master(pdev);
3056ea8b4db9SJeff Garzik 	pci_try_set_mwi(pdev);
30574447d351STejun Heo 	return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
3058c5d3e45aSJeff Garzik 				 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
3059c6fd2807SJeff Garzik }
30607bb3c529SSaeed Bishara #endif
3061c6fd2807SJeff Garzik 
3062f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev);
3063f351b2d6SSaeed Bishara static int __devexit mv_platform_remove(struct platform_device *pdev);
3064f351b2d6SSaeed Bishara 
3065c6fd2807SJeff Garzik static int __init mv_init(void)
3066c6fd2807SJeff Garzik {
30677bb3c529SSaeed Bishara 	int rc = -ENODEV;
30687bb3c529SSaeed Bishara #ifdef CONFIG_PCI
30697bb3c529SSaeed Bishara 	rc = pci_register_driver(&mv_pci_driver);
3070f351b2d6SSaeed Bishara 	if (rc < 0)
3071f351b2d6SSaeed Bishara 		return rc;
3072f351b2d6SSaeed Bishara #endif
3073f351b2d6SSaeed Bishara 	rc = platform_driver_register(&mv_platform_driver);
3074f351b2d6SSaeed Bishara 
3075f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
3076f351b2d6SSaeed Bishara 	if (rc < 0)
3077f351b2d6SSaeed Bishara 		pci_unregister_driver(&mv_pci_driver);
30787bb3c529SSaeed Bishara #endif
30797bb3c529SSaeed Bishara 	return rc;
3080c6fd2807SJeff Garzik }
3081c6fd2807SJeff Garzik 
3082c6fd2807SJeff Garzik static void __exit mv_exit(void)
3083c6fd2807SJeff Garzik {
30847bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3085c6fd2807SJeff Garzik 	pci_unregister_driver(&mv_pci_driver);
30867bb3c529SSaeed Bishara #endif
3087f351b2d6SSaeed Bishara 	platform_driver_unregister(&mv_platform_driver);
3088c6fd2807SJeff Garzik }
3089c6fd2807SJeff Garzik 
3090c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
3091c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
3092c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
3093c6fd2807SJeff Garzik MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
3094c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
309517c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
3096c6fd2807SJeff Garzik 
30977bb3c529SSaeed Bishara #ifdef CONFIG_PCI
3098c6fd2807SJeff Garzik module_param(msi, int, 0444);
3099c6fd2807SJeff Garzik MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
31007bb3c529SSaeed Bishara #endif
3101c6fd2807SJeff Garzik 
3102c6fd2807SJeff Garzik module_init(mv_init);
3103c6fd2807SJeff Garzik module_exit(mv_exit);
3104