1873e65bcSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2c6fd2807SJeff Garzik /*
3c6fd2807SJeff Garzik * sata_mv.c - Marvell SATA support
4c6fd2807SJeff Garzik *
540f21b11SMark Lord * Copyright 2008-2009: Marvell Corporation, all rights reserved.
6c6fd2807SJeff Garzik * Copyright 2005: EMC Corporation, all rights reserved.
7c6fd2807SJeff Garzik * Copyright 2005 Red Hat, Inc. All rights reserved.
8c6fd2807SJeff Garzik *
940f21b11SMark Lord * Originally written by Brett Russ.
1040f21b11SMark Lord * Extensive overhaul and enhancement by Mark Lord <mlord@pobox.com>.
1140f21b11SMark Lord *
12c6fd2807SJeff Garzik * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
13c6fd2807SJeff Garzik */
14c6fd2807SJeff Garzik
154a05e209SJeff Garzik /*
1685afb934SMark Lord * sata_mv TODO list:
1785afb934SMark Lord *
1885afb934SMark Lord * --> Develop a low-power-consumption strategy, and implement it.
1985afb934SMark Lord *
202b748a0aSMark Lord * --> Add sysfs attributes for per-chip / per-HC IRQ coalescing thresholds.
2185afb934SMark Lord *
2285afb934SMark Lord * --> [Experiment, Marvell value added] Is it possible to use target
2385afb934SMark Lord * mode to cross-connect two Linux boxes with Marvell cards? If so,
2485afb934SMark Lord * creating LibATA target mode support would be very interesting.
2585afb934SMark Lord *
2685afb934SMark Lord * Target mode, for those without docs, is the ability to directly
2785afb934SMark Lord * connect two SATA ports.
284a05e209SJeff Garzik */
294a05e209SJeff Garzik
3065ad7fefSMark Lord /*
3165ad7fefSMark Lord * 80x1-B2 errata PCI#11:
3265ad7fefSMark Lord *
3365ad7fefSMark Lord * Users of the 6041/6081 Rev.B2 chips (current is C0)
3465ad7fefSMark Lord * should be careful to insert those cards only onto PCI-X bus #0,
3565ad7fefSMark Lord * and only in device slots 0..7, not higher. The chips may not
3665ad7fefSMark Lord * work correctly otherwise (note: this is a pretty rare condition).
3765ad7fefSMark Lord */
3865ad7fefSMark Lord
39c6fd2807SJeff Garzik #include <linux/kernel.h>
40c6fd2807SJeff Garzik #include <linux/module.h>
41c6fd2807SJeff Garzik #include <linux/pci.h>
42c6fd2807SJeff Garzik #include <linux/init.h>
43c6fd2807SJeff Garzik #include <linux/blkdev.h>
44c6fd2807SJeff Garzik #include <linux/delay.h>
45c6fd2807SJeff Garzik #include <linux/interrupt.h>
468d8b6004SAndrew Morton #include <linux/dmapool.h>
47c6fd2807SJeff Garzik #include <linux/dma-mapping.h>
48c6fd2807SJeff Garzik #include <linux/device.h>
49c77a2f4eSSaeed Bishara #include <linux/clk.h>
50b7db4f2eSAndrew Lunn #include <linux/phy/phy.h>
51f351b2d6SSaeed Bishara #include <linux/platform_device.h>
52f351b2d6SSaeed Bishara #include <linux/ata_platform.h>
5315a32632SLennert Buytenhek #include <linux/mbus.h>
54c46938ccSMark Lord #include <linux/bitops.h>
555a0e3ad6STejun Heo #include <linux/gfp.h>
5697b414e1SAndrew Lunn #include <linux/of.h>
5797b414e1SAndrew Lunn #include <linux/of_irq.h>
58c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
59c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
606c08772eSJeff Garzik #include <scsi/scsi_device.h>
61c6fd2807SJeff Garzik #include <linux/libata.h>
62c6fd2807SJeff Garzik
63c6fd2807SJeff Garzik #define DRV_NAME "sata_mv"
64cae5a29dSMark Lord #define DRV_VERSION "1.28"
65c6fd2807SJeff Garzik
6640f21b11SMark Lord /*
6740f21b11SMark Lord * module options
6840f21b11SMark Lord */
6940f21b11SMark Lord
7040f21b11SMark Lord #ifdef CONFIG_PCI
7113b74085SAndrew Lunn static int msi;
7240f21b11SMark Lord module_param(msi, int, S_IRUGO);
7340f21b11SMark Lord MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
7440f21b11SMark Lord #endif
7540f21b11SMark Lord
762b748a0aSMark Lord static int irq_coalescing_io_count;
772b748a0aSMark Lord module_param(irq_coalescing_io_count, int, S_IRUGO);
782b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_io_count,
792b748a0aSMark Lord "IRQ coalescing I/O count threshold (0..255)");
802b748a0aSMark Lord
812b748a0aSMark Lord static int irq_coalescing_usecs;
822b748a0aSMark Lord module_param(irq_coalescing_usecs, int, S_IRUGO);
832b748a0aSMark Lord MODULE_PARM_DESC(irq_coalescing_usecs,
842b748a0aSMark Lord "IRQ coalescing time threshold in usecs");
852b748a0aSMark Lord
86c6fd2807SJeff Garzik enum {
87c6fd2807SJeff Garzik /* BAR's are enumerated in terms of pci_resource_start() terms */
88c6fd2807SJeff Garzik MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
89c6fd2807SJeff Garzik MV_IO_BAR = 2, /* offset 0x18: IO space */
90c6fd2807SJeff Garzik MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
91c6fd2807SJeff Garzik
92c6fd2807SJeff Garzik MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
93c6fd2807SJeff Garzik MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
94c6fd2807SJeff Garzik
952b748a0aSMark Lord /* For use with both IRQ coalescing methods ("all ports" or "per-HC" */
962b748a0aSMark Lord COAL_CLOCKS_PER_USEC = 150, /* for calculating COAL_TIMEs */
972b748a0aSMark Lord MAX_COAL_TIME_THRESHOLD = ((1 << 24) - 1), /* internal clocks count */
982b748a0aSMark Lord MAX_COAL_IO_COUNT = 255, /* completed I/O count */
992b748a0aSMark Lord
100c6fd2807SJeff Garzik MV_PCI_REG_BASE = 0,
101c6fd2807SJeff Garzik
1022b748a0aSMark Lord /*
1032b748a0aSMark Lord * Per-chip ("all ports") interrupt coalescing feature.
1042b748a0aSMark Lord * This is only for GEN_II / GEN_IIE hardware.
1052b748a0aSMark Lord *
1062b748a0aSMark Lord * Coalescing defers the interrupt until either the IO_THRESHOLD
1072b748a0aSMark Lord * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
1082b748a0aSMark Lord */
109cae5a29dSMark Lord COAL_REG_BASE = 0x18000,
110cae5a29dSMark Lord IRQ_COAL_CAUSE = (COAL_REG_BASE + 0x08),
1112b748a0aSMark Lord ALL_PORTS_COAL_IRQ = (1 << 4), /* all ports irq event */
1122b748a0aSMark Lord
113cae5a29dSMark Lord IRQ_COAL_IO_THRESHOLD = (COAL_REG_BASE + 0xcc),
114cae5a29dSMark Lord IRQ_COAL_TIME_THRESHOLD = (COAL_REG_BASE + 0xd0),
1152b748a0aSMark Lord
1162b748a0aSMark Lord /*
1172b748a0aSMark Lord * Registers for the (unused here) transaction coalescing feature:
1182b748a0aSMark Lord */
119cae5a29dSMark Lord TRAN_COAL_CAUSE_LO = (COAL_REG_BASE + 0x88),
120cae5a29dSMark Lord TRAN_COAL_CAUSE_HI = (COAL_REG_BASE + 0x8c),
1212b748a0aSMark Lord
122cae5a29dSMark Lord SATAHC0_REG_BASE = 0x20000,
123cae5a29dSMark Lord FLASH_CTL = 0x1046c,
124cae5a29dSMark Lord GPIO_PORT_CTL = 0x104f0,
125cae5a29dSMark Lord RESET_CFG = 0x180d8,
126c6fd2807SJeff Garzik
127c6fd2807SJeff Garzik MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
128c6fd2807SJeff Garzik MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
129c6fd2807SJeff Garzik MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
130c6fd2807SJeff Garzik MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
131c6fd2807SJeff Garzik
132c6fd2807SJeff Garzik MV_MAX_Q_DEPTH = 32,
133c6fd2807SJeff Garzik MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
134c6fd2807SJeff Garzik
135c6fd2807SJeff Garzik /* CRQB needs alignment on a 1KB boundary. Size == 1KB
136c6fd2807SJeff Garzik * CRPB needs alignment on a 256B boundary. Size == 256B
137c6fd2807SJeff Garzik * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
138c6fd2807SJeff Garzik */
139c6fd2807SJeff Garzik MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
140c6fd2807SJeff Garzik MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
141da2fa9baSMark Lord MV_MAX_SG_CT = 256,
142c6fd2807SJeff Garzik MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
143c6fd2807SJeff Garzik
144352fab70SMark Lord /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
145c6fd2807SJeff Garzik MV_PORT_HC_SHIFT = 2,
146352fab70SMark Lord MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
147352fab70SMark Lord /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
148352fab70SMark Lord MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
149c6fd2807SJeff Garzik
150c6fd2807SJeff Garzik /* Host Flags */
151c6fd2807SJeff Garzik MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
1527bb3c529SSaeed Bishara
1539cbe056fSSergei Shtylyov MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_PIO_POLLING,
154ad3aef51SMark Lord
15591b1a84cSMark Lord MV_GEN_I_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
156c6fd2807SJeff Garzik
15740f21b11SMark Lord MV_GEN_II_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NCQ |
15840f21b11SMark Lord ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
15991b1a84cSMark Lord
16091b1a84cSMark Lord MV_GEN_IIE_FLAGS = MV_GEN_II_FLAGS | ATA_FLAG_AN,
161ad3aef51SMark Lord
162c6fd2807SJeff Garzik CRQB_FLAG_READ = (1 << 0),
163c6fd2807SJeff Garzik CRQB_TAG_SHIFT = 1,
164c5d3e45aSJeff Garzik CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
165e12bef50SMark Lord CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
166c5d3e45aSJeff Garzik CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
167c6fd2807SJeff Garzik CRQB_CMD_ADDR_SHIFT = 8,
168c6fd2807SJeff Garzik CRQB_CMD_CS = (0x2 << 11),
169c6fd2807SJeff Garzik CRQB_CMD_LAST = (1 << 15),
170c6fd2807SJeff Garzik
171c6fd2807SJeff Garzik CRPB_FLAG_STATUS_SHIFT = 8,
172c5d3e45aSJeff Garzik CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
173c5d3e45aSJeff Garzik CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
174c6fd2807SJeff Garzik
175c6fd2807SJeff Garzik EPRD_FLAG_END_OF_TBL = (1 << 31),
176c6fd2807SJeff Garzik
177c6fd2807SJeff Garzik /* PCI interface registers */
178c6fd2807SJeff Garzik
179cae5a29dSMark Lord MV_PCI_COMMAND = 0xc00,
180cae5a29dSMark Lord MV_PCI_COMMAND_MWRCOM = (1 << 4), /* PCI Master Write Combining */
181cae5a29dSMark Lord MV_PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */
182c6fd2807SJeff Garzik
183cae5a29dSMark Lord PCI_MAIN_CMD_STS = 0xd30,
184c6fd2807SJeff Garzik STOP_PCI_MASTER = (1 << 2),
185c6fd2807SJeff Garzik PCI_MASTER_EMPTY = (1 << 3),
186c6fd2807SJeff Garzik GLOB_SFT_RST = (1 << 4),
187c6fd2807SJeff Garzik
188cae5a29dSMark Lord MV_PCI_MODE = 0xd00,
1898e7decdbSMark Lord MV_PCI_MODE_MASK = 0x30,
1908e7decdbSMark Lord
191c6fd2807SJeff Garzik MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
192c6fd2807SJeff Garzik MV_PCI_DISC_TIMER = 0xd04,
193c6fd2807SJeff Garzik MV_PCI_MSI_TRIGGER = 0xc38,
194c6fd2807SJeff Garzik MV_PCI_SERR_MASK = 0xc28,
195cae5a29dSMark Lord MV_PCI_XBAR_TMOUT = 0x1d04,
196c6fd2807SJeff Garzik MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
197c6fd2807SJeff Garzik MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
198c6fd2807SJeff Garzik MV_PCI_ERR_ATTRIBUTE = 0x1d48,
199c6fd2807SJeff Garzik MV_PCI_ERR_COMMAND = 0x1d50,
200c6fd2807SJeff Garzik
201cae5a29dSMark Lord PCI_IRQ_CAUSE = 0x1d58,
202cae5a29dSMark Lord PCI_IRQ_MASK = 0x1d5c,
203c6fd2807SJeff Garzik PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
204c6fd2807SJeff Garzik
205cae5a29dSMark Lord PCIE_IRQ_CAUSE = 0x1900,
206cae5a29dSMark Lord PCIE_IRQ_MASK = 0x1910,
207646a4da5SMark Lord PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
20802a121daSMark Lord
2097368f919SMark Lord /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
210cae5a29dSMark Lord PCI_HC_MAIN_IRQ_CAUSE = 0x1d60,
211cae5a29dSMark Lord PCI_HC_MAIN_IRQ_MASK = 0x1d64,
212cae5a29dSMark Lord SOC_HC_MAIN_IRQ_CAUSE = 0x20020,
213cae5a29dSMark Lord SOC_HC_MAIN_IRQ_MASK = 0x20024,
21440f21b11SMark Lord ERR_IRQ = (1 << 0), /* shift by (2 * port #) */
21540f21b11SMark Lord DONE_IRQ = (1 << 1), /* shift by (2 * port #) */
216c6fd2807SJeff Garzik HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
217c6fd2807SJeff Garzik HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
2182b748a0aSMark Lord DONE_IRQ_0_3 = 0x000000aa, /* DONE_IRQ ports 0,1,2,3 */
2192b748a0aSMark Lord DONE_IRQ_4_7 = (DONE_IRQ_0_3 << HC_SHIFT), /* 4,5,6,7 */
220c6fd2807SJeff Garzik PCI_ERR = (1 << 18),
22140f21b11SMark Lord TRAN_COAL_LO_DONE = (1 << 19), /* transaction coalescing */
22240f21b11SMark Lord TRAN_COAL_HI_DONE = (1 << 20), /* transaction coalescing */
22340f21b11SMark Lord PORTS_0_3_COAL_DONE = (1 << 8), /* HC0 IRQ coalescing */
22440f21b11SMark Lord PORTS_4_7_COAL_DONE = (1 << 17), /* HC1 IRQ coalescing */
22540f21b11SMark Lord ALL_PORTS_COAL_DONE = (1 << 21), /* GEN_II(E) IRQ coalescing */
226c6fd2807SJeff Garzik GPIO_INT = (1 << 22),
227c6fd2807SJeff Garzik SELF_INT = (1 << 23),
228c6fd2807SJeff Garzik TWSI_INT = (1 << 24),
229c6fd2807SJeff Garzik HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
230fb621e2fSJeff Garzik HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
231f351b2d6SSaeed Bishara HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
232c6fd2807SJeff Garzik
233c6fd2807SJeff Garzik /* SATAHC registers */
234cae5a29dSMark Lord HC_CFG = 0x00,
235c6fd2807SJeff Garzik
236cae5a29dSMark Lord HC_IRQ_CAUSE = 0x14,
237352fab70SMark Lord DMA_IRQ = (1 << 0), /* shift by port # */
238352fab70SMark Lord HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
239c6fd2807SJeff Garzik DEV_IRQ = (1 << 8), /* shift by port # */
240c6fd2807SJeff Garzik
2412b748a0aSMark Lord /*
2422b748a0aSMark Lord * Per-HC (Host-Controller) interrupt coalescing feature.
2432b748a0aSMark Lord * This is present on all chip generations.
2442b748a0aSMark Lord *
2452b748a0aSMark Lord * Coalescing defers the interrupt until either the IO_THRESHOLD
2462b748a0aSMark Lord * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
2472b748a0aSMark Lord */
248cae5a29dSMark Lord HC_IRQ_COAL_IO_THRESHOLD = 0x000c,
249cae5a29dSMark Lord HC_IRQ_COAL_TIME_THRESHOLD = 0x0010,
2502b748a0aSMark Lord
251cae5a29dSMark Lord SOC_LED_CTRL = 0x2c,
252000b344fSMark Lord SOC_LED_CTRL_BLINK = (1 << 0), /* Active LED blink */
253000b344fSMark Lord SOC_LED_CTRL_ACT_PRESENCE = (1 << 2), /* Multiplex dev presence */
254000b344fSMark Lord /* with dev activity LED */
255000b344fSMark Lord
256c6fd2807SJeff Garzik /* Shadow block registers */
257cae5a29dSMark Lord SHD_BLK = 0x100,
258cae5a29dSMark Lord SHD_CTL_AST = 0x20, /* ofs from SHD_BLK */
259c6fd2807SJeff Garzik
260c6fd2807SJeff Garzik /* SATA registers */
261cae5a29dSMark Lord SATA_STATUS = 0x300, /* ctrl, err regs follow status */
262cae5a29dSMark Lord SATA_ACTIVE = 0x350,
263cae5a29dSMark Lord FIS_IRQ_CAUSE = 0x364,
264cae5a29dSMark Lord FIS_IRQ_CAUSE_AN = (1 << 9), /* async notification */
26517c5aab5SMark Lord
266cae5a29dSMark Lord LTMODE = 0x30c, /* requires read-after-write */
26717c5aab5SMark Lord LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
26817c5aab5SMark Lord
269cae5a29dSMark Lord PHY_MODE2 = 0x330,
270c6fd2807SJeff Garzik PHY_MODE3 = 0x310,
271cae5a29dSMark Lord
272cae5a29dSMark Lord PHY_MODE4 = 0x314, /* requires read-after-write */
273ba069e37SMark Lord PHY_MODE4_CFG_MASK = 0x00000003, /* phy internal config field */
274ba069e37SMark Lord PHY_MODE4_CFG_VALUE = 0x00000001, /* phy internal config field */
275ba069e37SMark Lord PHY_MODE4_RSVD_ZEROS = 0x5de3fffa, /* Gen2e always write zeros */
276ba069e37SMark Lord PHY_MODE4_RSVD_ONES = 0x00000005, /* Gen2e always write ones */
277ba069e37SMark Lord
278cae5a29dSMark Lord SATA_IFCTL = 0x344,
279cae5a29dSMark Lord SATA_TESTCTL = 0x348,
280cae5a29dSMark Lord SATA_IFSTAT = 0x34c,
281cae5a29dSMark Lord VENDOR_UNIQUE_FIS = 0x35c,
28217c5aab5SMark Lord
283cae5a29dSMark Lord FISCFG = 0x360,
2848e7decdbSMark Lord FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */
2858e7decdbSMark Lord FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
28617c5aab5SMark Lord
28729b7e43cSMartin Michlmayr PHY_MODE9_GEN2 = 0x398,
28829b7e43cSMartin Michlmayr PHY_MODE9_GEN1 = 0x39c,
28929b7e43cSMartin Michlmayr PHYCFG_OFS = 0x3a0, /* only in 65n devices */
29029b7e43cSMartin Michlmayr
291c6fd2807SJeff Garzik MV5_PHY_MODE = 0x74,
292cae5a29dSMark Lord MV5_LTMODE = 0x30,
293cae5a29dSMark Lord MV5_PHY_CTL = 0x0C,
294cae5a29dSMark Lord SATA_IFCFG = 0x050,
2959013d64eSLior Amsalem LP_PHY_CTL = 0x058,
2963661aa99SThomas Petazzoni LP_PHY_CTL_PIN_PU_PLL = (1 << 0),
2973661aa99SThomas Petazzoni LP_PHY_CTL_PIN_PU_RX = (1 << 1),
2983661aa99SThomas Petazzoni LP_PHY_CTL_PIN_PU_TX = (1 << 2),
2993661aa99SThomas Petazzoni LP_PHY_CTL_GEN_TX_3G = (1 << 5),
3003661aa99SThomas Petazzoni LP_PHY_CTL_GEN_RX_3G = (1 << 9),
301c6fd2807SJeff Garzik
302c6fd2807SJeff Garzik MV_M2_PREAMP_MASK = 0x7e0,
303c6fd2807SJeff Garzik
304c6fd2807SJeff Garzik /* Port registers */
305cae5a29dSMark Lord EDMA_CFG = 0,
3060c58912eSMark Lord EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
3070c58912eSMark Lord EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
308c6fd2807SJeff Garzik EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
309c6fd2807SJeff Garzik EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
310c6fd2807SJeff Garzik EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
311e12bef50SMark Lord EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
312e12bef50SMark Lord EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
313c6fd2807SJeff Garzik
314cae5a29dSMark Lord EDMA_ERR_IRQ_CAUSE = 0x8,
315cae5a29dSMark Lord EDMA_ERR_IRQ_MASK = 0xc,
3166c1153e0SJeff Garzik EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
3176c1153e0SJeff Garzik EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
3186c1153e0SJeff Garzik EDMA_ERR_DEV = (1 << 2), /* device error */
3196c1153e0SJeff Garzik EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
3206c1153e0SJeff Garzik EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
3216c1153e0SJeff Garzik EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
322c5d3e45aSJeff Garzik EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
323c5d3e45aSJeff Garzik EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
3246c1153e0SJeff Garzik EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
325c5d3e45aSJeff Garzik EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
3266c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
3276c1153e0SJeff Garzik EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
3286c1153e0SJeff Garzik EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
3296c1153e0SJeff Garzik EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
330646a4da5SMark Lord
3316c1153e0SJeff Garzik EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
332646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
333646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
334646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
335646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
336646a4da5SMark Lord
3376c1153e0SJeff Garzik EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
338646a4da5SMark Lord
3396c1153e0SJeff Garzik EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
340646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
341646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
342646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
343646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
344646a4da5SMark Lord EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
345646a4da5SMark Lord
3466c1153e0SJeff Garzik EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
347646a4da5SMark Lord
3486c1153e0SJeff Garzik EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
349c5d3e45aSJeff Garzik EDMA_ERR_OVERRUN_5 = (1 << 5),
350c5d3e45aSJeff Garzik EDMA_ERR_UNDERRUN_5 = (1 << 6),
351646a4da5SMark Lord
352646a4da5SMark Lord EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
353646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_1 |
354646a4da5SMark Lord EDMA_ERR_LNK_CTRL_RX_3 |
35585afb934SMark Lord EDMA_ERR_LNK_CTRL_TX,
356646a4da5SMark Lord
357bdd4dddeSJeff Garzik EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
358bdd4dddeSJeff Garzik EDMA_ERR_PRD_PAR |
359bdd4dddeSJeff Garzik EDMA_ERR_DEV_DCON |
360bdd4dddeSJeff Garzik EDMA_ERR_DEV_CON |
361bdd4dddeSJeff Garzik EDMA_ERR_SERR |
362bdd4dddeSJeff Garzik EDMA_ERR_SELF_DIS |
3636c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR |
364bdd4dddeSJeff Garzik EDMA_ERR_CRPB_PAR |
365bdd4dddeSJeff Garzik EDMA_ERR_INTRL_PAR |
366bdd4dddeSJeff Garzik EDMA_ERR_IORDY |
367bdd4dddeSJeff Garzik EDMA_ERR_LNK_CTRL_RX_2 |
368c6fd2807SJeff Garzik EDMA_ERR_LNK_DATA_RX |
369c6fd2807SJeff Garzik EDMA_ERR_LNK_DATA_TX |
370bdd4dddeSJeff Garzik EDMA_ERR_TRANS_PROTO,
371e12bef50SMark Lord
372bdd4dddeSJeff Garzik EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
373bdd4dddeSJeff Garzik EDMA_ERR_PRD_PAR |
374bdd4dddeSJeff Garzik EDMA_ERR_DEV_DCON |
375bdd4dddeSJeff Garzik EDMA_ERR_DEV_CON |
376bdd4dddeSJeff Garzik EDMA_ERR_OVERRUN_5 |
377bdd4dddeSJeff Garzik EDMA_ERR_UNDERRUN_5 |
378bdd4dddeSJeff Garzik EDMA_ERR_SELF_DIS_5 |
3796c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR |
380bdd4dddeSJeff Garzik EDMA_ERR_CRPB_PAR |
381bdd4dddeSJeff Garzik EDMA_ERR_INTRL_PAR |
382bdd4dddeSJeff Garzik EDMA_ERR_IORDY,
383c6fd2807SJeff Garzik
384cae5a29dSMark Lord EDMA_REQ_Q_BASE_HI = 0x10,
385cae5a29dSMark Lord EDMA_REQ_Q_IN_PTR = 0x14, /* also contains BASE_LO */
386c6fd2807SJeff Garzik
387cae5a29dSMark Lord EDMA_REQ_Q_OUT_PTR = 0x18,
388c6fd2807SJeff Garzik EDMA_REQ_Q_PTR_SHIFT = 5,
389c6fd2807SJeff Garzik
390cae5a29dSMark Lord EDMA_RSP_Q_BASE_HI = 0x1c,
391cae5a29dSMark Lord EDMA_RSP_Q_IN_PTR = 0x20,
392cae5a29dSMark Lord EDMA_RSP_Q_OUT_PTR = 0x24, /* also contains BASE_LO */
393c6fd2807SJeff Garzik EDMA_RSP_Q_PTR_SHIFT = 3,
394c6fd2807SJeff Garzik
395cae5a29dSMark Lord EDMA_CMD = 0x28, /* EDMA command register */
3960ea9e179SJeff Garzik EDMA_EN = (1 << 0), /* enable EDMA */
3970ea9e179SJeff Garzik EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
3988e7decdbSMark Lord EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */
399c6fd2807SJeff Garzik
400cae5a29dSMark Lord EDMA_STATUS = 0x30, /* EDMA engine status */
4018e7decdbSMark Lord EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */
4028e7decdbSMark Lord EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */
4038e7decdbSMark Lord
404cae5a29dSMark Lord EDMA_IORDY_TMOUT = 0x34,
405cae5a29dSMark Lord EDMA_ARB_CFG = 0x38,
4068e7decdbSMark Lord
407cae5a29dSMark Lord EDMA_HALTCOND = 0x60, /* GenIIe halt conditions */
408cae5a29dSMark Lord EDMA_UNKNOWN_RSVD = 0x6C, /* GenIIe unknown/reserved */
409da14265eSMark Lord
410cae5a29dSMark Lord BMDMA_CMD = 0x224, /* bmdma command register */
411cae5a29dSMark Lord BMDMA_STATUS = 0x228, /* bmdma status register */
412cae5a29dSMark Lord BMDMA_PRD_LOW = 0x22c, /* bmdma PRD addr 31:0 */
413cae5a29dSMark Lord BMDMA_PRD_HIGH = 0x230, /* bmdma PRD addr 63:32 */
414da14265eSMark Lord
415c6fd2807SJeff Garzik /* Host private flags (hp_flags) */
416c6fd2807SJeff Garzik MV_HP_FLAG_MSI = (1 << 0),
417c6fd2807SJeff Garzik MV_HP_ERRATA_50XXB0 = (1 << 1),
418c6fd2807SJeff Garzik MV_HP_ERRATA_50XXB2 = (1 << 2),
419c6fd2807SJeff Garzik MV_HP_ERRATA_60X1B2 = (1 << 3),
420c6fd2807SJeff Garzik MV_HP_ERRATA_60X1C0 = (1 << 4),
4210ea9e179SJeff Garzik MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
4220ea9e179SJeff Garzik MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
4230ea9e179SJeff Garzik MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
42402a121daSMark Lord MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
425616d4a98SMark Lord MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */
4261f398472SMark Lord MV_HP_FLAG_SOC = (1 << 11), /* SystemOnChip, no PCI */
427000b344fSMark Lord MV_HP_QUIRK_LED_BLINK_EN = (1 << 12), /* is led blinking enabled? */
4289013d64eSLior Amsalem MV_HP_FIX_LP_PHY_CTL = (1 << 13), /* fix speed in LP_PHY_CTL ? */
429c6fd2807SJeff Garzik
430c6fd2807SJeff Garzik /* Port private flags (pp_flags) */
4310ea9e179SJeff Garzik MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
43272109168SMark Lord MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
43300f42eabSMark Lord MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */
43429d187bbSMark Lord MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */
435d16ab3f6SMark Lord MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4), /* ignore initial ATA_DRDY */
436c6fd2807SJeff Garzik };
437c6fd2807SJeff Garzik
438ee9ccdf7SJeff Garzik #define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
439ee9ccdf7SJeff Garzik #define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
440c6fd2807SJeff Garzik #define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
4418e7decdbSMark Lord #define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
4421f398472SMark Lord #define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
443c6fd2807SJeff Garzik
44415a32632SLennert Buytenhek #define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
44515a32632SLennert Buytenhek #define WINDOW_BASE(i) (0x20034 + ((i) << 4))
44615a32632SLennert Buytenhek
447c6fd2807SJeff Garzik enum {
448baf14aa1SJeff Garzik /* DMA boundary 0xffff is required by the s/g splitting
449baf14aa1SJeff Garzik * we need on /length/ in mv_fill-sg().
450baf14aa1SJeff Garzik */
451baf14aa1SJeff Garzik MV_DMA_BOUNDARY = 0xffffU,
452c6fd2807SJeff Garzik
4530ea9e179SJeff Garzik /* mask of register bits containing lower 32 bits
4540ea9e179SJeff Garzik * of EDMA request queue DMA address
4550ea9e179SJeff Garzik */
456c6fd2807SJeff Garzik EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
457c6fd2807SJeff Garzik
4580ea9e179SJeff Garzik /* ditto, for response queue */
459c6fd2807SJeff Garzik EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
460c6fd2807SJeff Garzik };
461c6fd2807SJeff Garzik
462c6fd2807SJeff Garzik enum chip_type {
463c6fd2807SJeff Garzik chip_504x,
464c6fd2807SJeff Garzik chip_508x,
465c6fd2807SJeff Garzik chip_5080,
466c6fd2807SJeff Garzik chip_604x,
467c6fd2807SJeff Garzik chip_608x,
468c6fd2807SJeff Garzik chip_6042,
469c6fd2807SJeff Garzik chip_7042,
470f351b2d6SSaeed Bishara chip_soc,
471c6fd2807SJeff Garzik };
472c6fd2807SJeff Garzik
473c6fd2807SJeff Garzik /* Command ReQuest Block: 32B */
474c6fd2807SJeff Garzik struct mv_crqb {
475c6fd2807SJeff Garzik __le32 sg_addr;
476c6fd2807SJeff Garzik __le32 sg_addr_hi;
477c6fd2807SJeff Garzik __le16 ctrl_flags;
478c6fd2807SJeff Garzik __le16 ata_cmd[11];
479c6fd2807SJeff Garzik };
480c6fd2807SJeff Garzik
481c6fd2807SJeff Garzik struct mv_crqb_iie {
482c6fd2807SJeff Garzik __le32 addr;
483c6fd2807SJeff Garzik __le32 addr_hi;
484c6fd2807SJeff Garzik __le32 flags;
485c6fd2807SJeff Garzik __le32 len;
486c6fd2807SJeff Garzik __le32 ata_cmd[4];
487c6fd2807SJeff Garzik };
488c6fd2807SJeff Garzik
489c6fd2807SJeff Garzik /* Command ResPonse Block: 8B */
490c6fd2807SJeff Garzik struct mv_crpb {
491c6fd2807SJeff Garzik __le16 id;
492c6fd2807SJeff Garzik __le16 flags;
493c6fd2807SJeff Garzik __le32 tmstmp;
494c6fd2807SJeff Garzik };
495c6fd2807SJeff Garzik
496c6fd2807SJeff Garzik /* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
497c6fd2807SJeff Garzik struct mv_sg {
498c6fd2807SJeff Garzik __le32 addr;
499c6fd2807SJeff Garzik __le32 flags_size;
500c6fd2807SJeff Garzik __le32 addr_hi;
501c6fd2807SJeff Garzik __le32 reserved;
502c6fd2807SJeff Garzik };
503c6fd2807SJeff Garzik
50408da1759SMark Lord /*
50508da1759SMark Lord * We keep a local cache of a few frequently accessed port
50608da1759SMark Lord * registers here, to avoid having to read them (very slow)
50708da1759SMark Lord * when switching between EDMA and non-EDMA modes.
50808da1759SMark Lord */
50908da1759SMark Lord struct mv_cached_regs {
51008da1759SMark Lord u32 fiscfg;
51108da1759SMark Lord u32 ltmode;
51208da1759SMark Lord u32 haltcond;
513c01e8a23SMark Lord u32 unknown_rsvd;
51408da1759SMark Lord };
51508da1759SMark Lord
516c6fd2807SJeff Garzik struct mv_port_priv {
517c6fd2807SJeff Garzik struct mv_crqb *crqb;
518c6fd2807SJeff Garzik dma_addr_t crqb_dma;
519c6fd2807SJeff Garzik struct mv_crpb *crpb;
520c6fd2807SJeff Garzik dma_addr_t crpb_dma;
521eb73d558SMark Lord struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
522eb73d558SMark Lord dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
523bdd4dddeSJeff Garzik
524bdd4dddeSJeff Garzik unsigned int req_idx;
525bdd4dddeSJeff Garzik unsigned int resp_idx;
526bdd4dddeSJeff Garzik
527c6fd2807SJeff Garzik u32 pp_flags;
52808da1759SMark Lord struct mv_cached_regs cached;
52929d187bbSMark Lord unsigned int delayed_eh_pmp_map;
530c6fd2807SJeff Garzik };
531c6fd2807SJeff Garzik
532c6fd2807SJeff Garzik struct mv_port_signal {
533c6fd2807SJeff Garzik u32 amps;
534c6fd2807SJeff Garzik u32 pre;
535c6fd2807SJeff Garzik };
536c6fd2807SJeff Garzik
53702a121daSMark Lord struct mv_host_priv {
53802a121daSMark Lord u32 hp_flags;
5391bfeff03SSaeed Bishara unsigned int board_idx;
54096e2c487SMark Lord u32 main_irq_mask;
54102a121daSMark Lord struct mv_port_signal signal[8];
54202a121daSMark Lord const struct mv_hw_ops *ops;
543f351b2d6SSaeed Bishara int n_ports;
544f351b2d6SSaeed Bishara void __iomem *base;
5457368f919SMark Lord void __iomem *main_irq_cause_addr;
5467368f919SMark Lord void __iomem *main_irq_mask_addr;
547cae5a29dSMark Lord u32 irq_cause_offset;
548cae5a29dSMark Lord u32 irq_mask_offset;
54902a121daSMark Lord u32 unmask_all_irqs;
550c77a2f4eSSaeed Bishara
551e0067f0bSEzequiel Garcia /*
552e0067f0bSEzequiel Garcia * Needed on some devices that require their clocks to be enabled.
553e0067f0bSEzequiel Garcia * These are optional: if the platform device does not have any
554e0067f0bSEzequiel Garcia * clocks, they won't be used. Also, if the underlying hardware
555e0067f0bSEzequiel Garcia * does not support the common clock framework (CONFIG_HAVE_CLK=n),
556e0067f0bSEzequiel Garcia * all the clock operations become no-ops (see clk.h).
557e0067f0bSEzequiel Garcia */
558c77a2f4eSSaeed Bishara struct clk *clk;
559eee98990SAndrew Lunn struct clk **port_clks;
560da2fa9baSMark Lord /*
561b7db4f2eSAndrew Lunn * Some devices have a SATA PHY which can be enabled/disabled
562b7db4f2eSAndrew Lunn * in order to save power. These are optional: if the platform
563b7db4f2eSAndrew Lunn * devices does not have any phy, they won't be used.
564b7db4f2eSAndrew Lunn */
565b7db4f2eSAndrew Lunn struct phy **port_phys;
566b7db4f2eSAndrew Lunn /*
567da2fa9baSMark Lord * These consistent DMA memory pools give us guaranteed
568da2fa9baSMark Lord * alignment for hardware-accessed data structures,
569da2fa9baSMark Lord * and less memory waste in accomplishing the alignment.
570da2fa9baSMark Lord */
571da2fa9baSMark Lord struct dma_pool *crqb_pool;
572da2fa9baSMark Lord struct dma_pool *crpb_pool;
573da2fa9baSMark Lord struct dma_pool *sg_tbl_pool;
57402a121daSMark Lord };
57502a121daSMark Lord
576c6fd2807SJeff Garzik struct mv_hw_ops {
577c6fd2807SJeff Garzik void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
578c6fd2807SJeff Garzik unsigned int port);
579c6fd2807SJeff Garzik void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
580c6fd2807SJeff Garzik void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
581c6fd2807SJeff Garzik void __iomem *mmio);
582f76ba003SHannes Reinecke int (*reset_hc)(struct ata_host *host, void __iomem *mmio,
583c6fd2807SJeff Garzik unsigned int n_hc);
584c6fd2807SJeff Garzik void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
5857bb3c529SSaeed Bishara void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
586c6fd2807SJeff Garzik };
587c6fd2807SJeff Garzik
58882ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
58982ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
59082ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
59182ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
592c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap);
593c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap);
5943e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc);
59595364f36SJiri Slaby static enum ata_completion_errors mv_qc_prep(struct ata_queued_cmd *qc);
59695364f36SJiri Slaby static enum ata_completion_errors mv_qc_prep_iie(struct ata_queued_cmd *qc);
597c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
598a1efdabaSTejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
599a1efdabaSTejun Heo unsigned long deadline);
600bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap);
601bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap);
602f273827eSMark Lord static void mv6_dev_config(struct ata_device *dev);
603c6fd2807SJeff Garzik
604c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
605c6fd2807SJeff Garzik unsigned int port);
606c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
607c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
608c6fd2807SJeff Garzik void __iomem *mmio);
609f76ba003SHannes Reinecke static int mv5_reset_hc(struct ata_host *host, void __iomem *mmio,
610c6fd2807SJeff Garzik unsigned int n_hc);
611c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
6127bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
613c6fd2807SJeff Garzik
614c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
615c6fd2807SJeff Garzik unsigned int port);
616c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
617c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
618c6fd2807SJeff Garzik void __iomem *mmio);
619f76ba003SHannes Reinecke static int mv6_reset_hc(struct ata_host *host, void __iomem *mmio,
620c6fd2807SJeff Garzik unsigned int n_hc);
621c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
622f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
623f351b2d6SSaeed Bishara void __iomem *mmio);
624f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
625f351b2d6SSaeed Bishara void __iomem *mmio);
626f76ba003SHannes Reinecke static int mv_soc_reset_hc(struct ata_host *host,
627f351b2d6SSaeed Bishara void __iomem *mmio, unsigned int n_hc);
628f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
629f351b2d6SSaeed Bishara void __iomem *mmio);
630f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
63129b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
63229b7e43cSMartin Michlmayr void __iomem *mmio, unsigned int port);
6337bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
634e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
635c6fd2807SJeff Garzik unsigned int port_no);
636e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap);
637b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio);
63800b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
639c6fd2807SJeff Garzik
640e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp);
641e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
642e49856d8SMark Lord unsigned long deadline);
643e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
644e49856d8SMark Lord unsigned long deadline);
64529d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap);
6464c299ca3SMark Lord static void mv_process_crpb_entries(struct ata_port *ap,
6474c299ca3SMark Lord struct mv_port_priv *pp);
648c6fd2807SJeff Garzik
649da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap);
650da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
651da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc);
652da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc);
653da14265eSMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc);
654da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap);
655d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap);
656da14265eSMark Lord
657eb73d558SMark Lord /* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
658eb73d558SMark Lord * because we have to allow room for worst case splitting of
659eb73d558SMark Lord * PRDs for 64K boundaries in mv_fill_sg().
660eb73d558SMark Lord */
66113b74085SAndrew Lunn #ifdef CONFIG_PCI
66225df73d9SBart Van Assche static const struct scsi_host_template mv5_sht = {
66368d1d07bSTejun Heo ATA_BASE_SHT(DRV_NAME),
664baf14aa1SJeff Garzik .sg_tablesize = MV_MAX_SG_CT / 2,
665c5d3e45aSJeff Garzik .dma_boundary = MV_DMA_BOUNDARY,
666c5d3e45aSJeff Garzik };
66713b74085SAndrew Lunn #endif
66825df73d9SBart Van Assche static const struct scsi_host_template mv6_sht = {
669e75f41a9SLee Jones __ATA_BASE_SHT(DRV_NAME),
670138bfdd0SMark Lord .can_queue = MV_MAX_Q_DEPTH - 1,
671baf14aa1SJeff Garzik .sg_tablesize = MV_MAX_SG_CT / 2,
672c6fd2807SJeff Garzik .dma_boundary = MV_DMA_BOUNDARY,
673c3f69c7fSBart Van Assche .sdev_groups = ata_ncq_sdev_groups,
674e75f41a9SLee Jones .change_queue_depth = ata_scsi_change_queue_depth,
675e75f41a9SLee Jones .tag_alloc_policy = BLK_TAG_ALLOC_RR,
676e75f41a9SLee Jones .slave_configure = ata_scsi_slave_config
677c6fd2807SJeff Garzik };
678c6fd2807SJeff Garzik
679029cfd6bSTejun Heo static struct ata_port_operations mv5_ops = {
680029cfd6bSTejun Heo .inherits = &ata_sff_port_ops,
681c6fd2807SJeff Garzik
682c96f1732SAlan Cox .lost_interrupt = ATA_OP_NULL,
683c96f1732SAlan Cox
6843e4a1391SMark Lord .qc_defer = mv_qc_defer,
685c6fd2807SJeff Garzik .qc_prep = mv_qc_prep,
686c6fd2807SJeff Garzik .qc_issue = mv_qc_issue,
687c6fd2807SJeff Garzik
688bdd4dddeSJeff Garzik .freeze = mv_eh_freeze,
689bdd4dddeSJeff Garzik .thaw = mv_eh_thaw,
690a1efdabaSTejun Heo .hardreset = mv_hardreset,
691bdd4dddeSJeff Garzik
692c6fd2807SJeff Garzik .scr_read = mv5_scr_read,
693c6fd2807SJeff Garzik .scr_write = mv5_scr_write,
694c6fd2807SJeff Garzik
695c6fd2807SJeff Garzik .port_start = mv_port_start,
696c6fd2807SJeff Garzik .port_stop = mv_port_stop,
697c6fd2807SJeff Garzik };
698c6fd2807SJeff Garzik
699029cfd6bSTejun Heo static struct ata_port_operations mv6_ops = {
7008930ff25STejun Heo .inherits = &ata_bmdma_port_ops,
701c6fd2807SJeff Garzik
7028930ff25STejun Heo .lost_interrupt = ATA_OP_NULL,
7038930ff25STejun Heo
7048930ff25STejun Heo .qc_defer = mv_qc_defer,
7058930ff25STejun Heo .qc_prep = mv_qc_prep,
7068930ff25STejun Heo .qc_issue = mv_qc_issue,
7078930ff25STejun Heo
7088930ff25STejun Heo .dev_config = mv6_dev_config,
7098930ff25STejun Heo
7108930ff25STejun Heo .freeze = mv_eh_freeze,
7118930ff25STejun Heo .thaw = mv_eh_thaw,
7128930ff25STejun Heo .hardreset = mv_hardreset,
7138930ff25STejun Heo .softreset = mv_softreset,
714e49856d8SMark Lord .pmp_hardreset = mv_pmp_hardreset,
715e49856d8SMark Lord .pmp_softreset = mv_softreset,
71629d187bbSMark Lord .error_handler = mv_pmp_error_handler,
717da14265eSMark Lord
7188930ff25STejun Heo .scr_read = mv_scr_read,
7198930ff25STejun Heo .scr_write = mv_scr_write,
7208930ff25STejun Heo
721d16ab3f6SMark Lord .sff_check_status = mv_sff_check_status,
722da14265eSMark Lord .sff_irq_clear = mv_sff_irq_clear,
723da14265eSMark Lord .check_atapi_dma = mv_check_atapi_dma,
724da14265eSMark Lord .bmdma_setup = mv_bmdma_setup,
725da14265eSMark Lord .bmdma_start = mv_bmdma_start,
726da14265eSMark Lord .bmdma_stop = mv_bmdma_stop,
727da14265eSMark Lord .bmdma_status = mv_bmdma_status,
7288930ff25STejun Heo
7298930ff25STejun Heo .port_start = mv_port_start,
7308930ff25STejun Heo .port_stop = mv_port_stop,
731c6fd2807SJeff Garzik };
732c6fd2807SJeff Garzik
733029cfd6bSTejun Heo static struct ata_port_operations mv_iie_ops = {
734029cfd6bSTejun Heo .inherits = &mv6_ops,
735029cfd6bSTejun Heo .dev_config = ATA_OP_NULL,
736c6fd2807SJeff Garzik .qc_prep = mv_qc_prep_iie,
737c6fd2807SJeff Garzik };
738c6fd2807SJeff Garzik
739c6fd2807SJeff Garzik static const struct ata_port_info mv_port_info[] = {
740c6fd2807SJeff Garzik { /* chip_504x */
74191b1a84cSMark Lord .flags = MV_GEN_I_FLAGS,
742c361acbcSMark Lord .pio_mask = ATA_PIO4,
743bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
744c6fd2807SJeff Garzik .port_ops = &mv5_ops,
745c6fd2807SJeff Garzik },
746c6fd2807SJeff Garzik { /* chip_508x */
74791b1a84cSMark Lord .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
748c361acbcSMark Lord .pio_mask = ATA_PIO4,
749bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
750c6fd2807SJeff Garzik .port_ops = &mv5_ops,
751c6fd2807SJeff Garzik },
752c6fd2807SJeff Garzik { /* chip_5080 */
75391b1a84cSMark Lord .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
754c361acbcSMark Lord .pio_mask = ATA_PIO4,
755bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
756c6fd2807SJeff Garzik .port_ops = &mv5_ops,
757c6fd2807SJeff Garzik },
758c6fd2807SJeff Garzik { /* chip_604x */
75991b1a84cSMark Lord .flags = MV_GEN_II_FLAGS,
760c361acbcSMark Lord .pio_mask = ATA_PIO4,
761bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
762c6fd2807SJeff Garzik .port_ops = &mv6_ops,
763c6fd2807SJeff Garzik },
764c6fd2807SJeff Garzik { /* chip_608x */
76591b1a84cSMark Lord .flags = MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
766c361acbcSMark Lord .pio_mask = ATA_PIO4,
767bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
768c6fd2807SJeff Garzik .port_ops = &mv6_ops,
769c6fd2807SJeff Garzik },
770c6fd2807SJeff Garzik { /* chip_6042 */
77191b1a84cSMark Lord .flags = MV_GEN_IIE_FLAGS,
772c361acbcSMark Lord .pio_mask = ATA_PIO4,
773bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
774c6fd2807SJeff Garzik .port_ops = &mv_iie_ops,
775c6fd2807SJeff Garzik },
776c6fd2807SJeff Garzik { /* chip_7042 */
77791b1a84cSMark Lord .flags = MV_GEN_IIE_FLAGS,
778c361acbcSMark Lord .pio_mask = ATA_PIO4,
779bf6263a8SJeff Garzik .udma_mask = ATA_UDMA6,
780c6fd2807SJeff Garzik .port_ops = &mv_iie_ops,
781c6fd2807SJeff Garzik },
782f351b2d6SSaeed Bishara { /* chip_soc */
78391b1a84cSMark Lord .flags = MV_GEN_IIE_FLAGS,
784c361acbcSMark Lord .pio_mask = ATA_PIO4,
785f351b2d6SSaeed Bishara .udma_mask = ATA_UDMA6,
786f351b2d6SSaeed Bishara .port_ops = &mv_iie_ops,
787f351b2d6SSaeed Bishara },
788c6fd2807SJeff Garzik };
789c6fd2807SJeff Garzik
790c6fd2807SJeff Garzik static const struct mv_hw_ops mv5xxx_ops = {
791c6fd2807SJeff Garzik .phy_errata = mv5_phy_errata,
792c6fd2807SJeff Garzik .enable_leds = mv5_enable_leds,
793c6fd2807SJeff Garzik .read_preamp = mv5_read_preamp,
794c6fd2807SJeff Garzik .reset_hc = mv5_reset_hc,
795c6fd2807SJeff Garzik .reset_flash = mv5_reset_flash,
796c6fd2807SJeff Garzik .reset_bus = mv5_reset_bus,
797c6fd2807SJeff Garzik };
798c6fd2807SJeff Garzik
799c6fd2807SJeff Garzik static const struct mv_hw_ops mv6xxx_ops = {
800c6fd2807SJeff Garzik .phy_errata = mv6_phy_errata,
801c6fd2807SJeff Garzik .enable_leds = mv6_enable_leds,
802c6fd2807SJeff Garzik .read_preamp = mv6_read_preamp,
803c6fd2807SJeff Garzik .reset_hc = mv6_reset_hc,
804c6fd2807SJeff Garzik .reset_flash = mv6_reset_flash,
805c6fd2807SJeff Garzik .reset_bus = mv_reset_pci_bus,
806c6fd2807SJeff Garzik };
807c6fd2807SJeff Garzik
808f351b2d6SSaeed Bishara static const struct mv_hw_ops mv_soc_ops = {
809f351b2d6SSaeed Bishara .phy_errata = mv6_phy_errata,
810f351b2d6SSaeed Bishara .enable_leds = mv_soc_enable_leds,
811f351b2d6SSaeed Bishara .read_preamp = mv_soc_read_preamp,
812f351b2d6SSaeed Bishara .reset_hc = mv_soc_reset_hc,
813f351b2d6SSaeed Bishara .reset_flash = mv_soc_reset_flash,
814f351b2d6SSaeed Bishara .reset_bus = mv_soc_reset_bus,
815f351b2d6SSaeed Bishara };
816f351b2d6SSaeed Bishara
81729b7e43cSMartin Michlmayr static const struct mv_hw_ops mv_soc_65n_ops = {
81829b7e43cSMartin Michlmayr .phy_errata = mv_soc_65n_phy_errata,
81929b7e43cSMartin Michlmayr .enable_leds = mv_soc_enable_leds,
82029b7e43cSMartin Michlmayr .reset_hc = mv_soc_reset_hc,
82129b7e43cSMartin Michlmayr .reset_flash = mv_soc_reset_flash,
82229b7e43cSMartin Michlmayr .reset_bus = mv_soc_reset_bus,
82329b7e43cSMartin Michlmayr };
82429b7e43cSMartin Michlmayr
825c6fd2807SJeff Garzik /*
826c6fd2807SJeff Garzik * Functions
827c6fd2807SJeff Garzik */
828c6fd2807SJeff Garzik
writelfl(unsigned long data,void __iomem * addr)829c6fd2807SJeff Garzik static inline void writelfl(unsigned long data, void __iomem *addr)
830c6fd2807SJeff Garzik {
831c6fd2807SJeff Garzik writel(data, addr);
832c6fd2807SJeff Garzik (void) readl(addr); /* flush to avoid PCI posted write */
833c6fd2807SJeff Garzik }
834c6fd2807SJeff Garzik
mv_hc_from_port(unsigned int port)835c6fd2807SJeff Garzik static inline unsigned int mv_hc_from_port(unsigned int port)
836c6fd2807SJeff Garzik {
837c6fd2807SJeff Garzik return port >> MV_PORT_HC_SHIFT;
838c6fd2807SJeff Garzik }
839c6fd2807SJeff Garzik
mv_hardport_from_port(unsigned int port)840c6fd2807SJeff Garzik static inline unsigned int mv_hardport_from_port(unsigned int port)
841c6fd2807SJeff Garzik {
842c6fd2807SJeff Garzik return port & MV_PORT_MASK;
843c6fd2807SJeff Garzik }
844c6fd2807SJeff Garzik
8451cfd19aeSMark Lord /*
8461cfd19aeSMark Lord * Consolidate some rather tricky bit shift calculations.
8471cfd19aeSMark Lord * This is hot-path stuff, so not a function.
8481cfd19aeSMark Lord * Simple code, with two return values, so macro rather than inline.
8491cfd19aeSMark Lord *
8501cfd19aeSMark Lord * port is the sole input, in range 0..7.
8517368f919SMark Lord * shift is one output, for use with main_irq_cause / main_irq_mask registers.
8527368f919SMark Lord * hardport is the other output, in range 0..3.
8531cfd19aeSMark Lord *
8541cfd19aeSMark Lord * Note that port and hardport may be the same variable in some cases.
8551cfd19aeSMark Lord */
8561cfd19aeSMark Lord #define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
8571cfd19aeSMark Lord { \
8581cfd19aeSMark Lord shift = mv_hc_from_port(port) * HC_SHIFT; \
8591cfd19aeSMark Lord hardport = mv_hardport_from_port(port); \
8601cfd19aeSMark Lord shift += hardport * 2; \
8611cfd19aeSMark Lord }
8621cfd19aeSMark Lord
mv_hc_base(void __iomem * base,unsigned int hc)863352fab70SMark Lord static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
864352fab70SMark Lord {
865cae5a29dSMark Lord return (base + SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
866352fab70SMark Lord }
867352fab70SMark Lord
mv_hc_base_from_port(void __iomem * base,unsigned int port)868c6fd2807SJeff Garzik static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
869c6fd2807SJeff Garzik unsigned int port)
870c6fd2807SJeff Garzik {
871c6fd2807SJeff Garzik return mv_hc_base(base, mv_hc_from_port(port));
872c6fd2807SJeff Garzik }
873c6fd2807SJeff Garzik
mv_port_base(void __iomem * base,unsigned int port)874c6fd2807SJeff Garzik static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
875c6fd2807SJeff Garzik {
876c6fd2807SJeff Garzik return mv_hc_base_from_port(base, port) +
877c6fd2807SJeff Garzik MV_SATAHC_ARBTR_REG_SZ +
878c6fd2807SJeff Garzik (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
879c6fd2807SJeff Garzik }
880c6fd2807SJeff Garzik
mv5_phy_base(void __iomem * mmio,unsigned int port)881e12bef50SMark Lord static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
882e12bef50SMark Lord {
883e12bef50SMark Lord void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
884e12bef50SMark Lord unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
885e12bef50SMark Lord
886e12bef50SMark Lord return hc_mmio + ofs;
887e12bef50SMark Lord }
888e12bef50SMark Lord
mv_host_base(struct ata_host * host)889f351b2d6SSaeed Bishara static inline void __iomem *mv_host_base(struct ata_host *host)
890f351b2d6SSaeed Bishara {
891f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = host->private_data;
892f351b2d6SSaeed Bishara return hpriv->base;
893f351b2d6SSaeed Bishara }
894f351b2d6SSaeed Bishara
mv_ap_base(struct ata_port * ap)895c6fd2807SJeff Garzik static inline void __iomem *mv_ap_base(struct ata_port *ap)
896c6fd2807SJeff Garzik {
897f351b2d6SSaeed Bishara return mv_port_base(mv_host_base(ap->host), ap->port_no);
898c6fd2807SJeff Garzik }
899c6fd2807SJeff Garzik
mv_get_hc_count(unsigned long port_flags)900cca3974eSJeff Garzik static inline int mv_get_hc_count(unsigned long port_flags)
901c6fd2807SJeff Garzik {
902cca3974eSJeff Garzik return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
903c6fd2807SJeff Garzik }
904c6fd2807SJeff Garzik
90508da1759SMark Lord /**
90608da1759SMark Lord * mv_save_cached_regs - (re-)initialize cached port registers
90708da1759SMark Lord * @ap: the port whose registers we are caching
90808da1759SMark Lord *
90908da1759SMark Lord * Initialize the local cache of port registers,
91008da1759SMark Lord * so that reading them over and over again can
91108da1759SMark Lord * be avoided on the hotter paths of this driver.
91208da1759SMark Lord * This saves a few microseconds each time we switch
91308da1759SMark Lord * to/from EDMA mode to perform (eg.) a drive cache flush.
91408da1759SMark Lord */
mv_save_cached_regs(struct ata_port * ap)91508da1759SMark Lord static void mv_save_cached_regs(struct ata_port *ap)
91608da1759SMark Lord {
91708da1759SMark Lord void __iomem *port_mmio = mv_ap_base(ap);
91808da1759SMark Lord struct mv_port_priv *pp = ap->private_data;
91908da1759SMark Lord
920cae5a29dSMark Lord pp->cached.fiscfg = readl(port_mmio + FISCFG);
921cae5a29dSMark Lord pp->cached.ltmode = readl(port_mmio + LTMODE);
922cae5a29dSMark Lord pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND);
923cae5a29dSMark Lord pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD);
92408da1759SMark Lord }
92508da1759SMark Lord
92608da1759SMark Lord /**
92708da1759SMark Lord * mv_write_cached_reg - write to a cached port register
92808da1759SMark Lord * @addr: hardware address of the register
92908da1759SMark Lord * @old: pointer to cached value of the register
93008da1759SMark Lord * @new: new value for the register
93108da1759SMark Lord *
93208da1759SMark Lord * Write a new value to a cached register,
93308da1759SMark Lord * but only if the value is different from before.
93408da1759SMark Lord */
mv_write_cached_reg(void __iomem * addr,u32 * old,u32 new)93508da1759SMark Lord static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
93608da1759SMark Lord {
93708da1759SMark Lord if (new != *old) {
93812f3b6d7SMark Lord unsigned long laddr;
93908da1759SMark Lord *old = new;
94012f3b6d7SMark Lord /*
94112f3b6d7SMark Lord * Workaround for 88SX60x1-B2 FEr SATA#13:
94212f3b6d7SMark Lord * Read-after-write is needed to prevent generating 64-bit
94312f3b6d7SMark Lord * write cycles on the PCI bus for SATA interface registers
94412f3b6d7SMark Lord * at offsets ending in 0x4 or 0xc.
94512f3b6d7SMark Lord *
94612f3b6d7SMark Lord * Looks like a lot of fuss, but it avoids an unnecessary
94712f3b6d7SMark Lord * +1 usec read-after-write delay for unaffected registers.
94812f3b6d7SMark Lord */
94976bf3441SBen Dooks laddr = (unsigned long)addr & 0xffff;
95012f3b6d7SMark Lord if (laddr >= 0x300 && laddr <= 0x33c) {
95112f3b6d7SMark Lord laddr &= 0x000f;
95212f3b6d7SMark Lord if (laddr == 0x4 || laddr == 0xc) {
95312f3b6d7SMark Lord writelfl(new, addr); /* read after write */
95412f3b6d7SMark Lord return;
95512f3b6d7SMark Lord }
95612f3b6d7SMark Lord }
95712f3b6d7SMark Lord writel(new, addr); /* unaffected by the errata */
95808da1759SMark Lord }
95908da1759SMark Lord }
96008da1759SMark Lord
mv_set_edma_ptrs(void __iomem * port_mmio,struct mv_host_priv * hpriv,struct mv_port_priv * pp)961c5d3e45aSJeff Garzik static void mv_set_edma_ptrs(void __iomem *port_mmio,
962c5d3e45aSJeff Garzik struct mv_host_priv *hpriv,
963c5d3e45aSJeff Garzik struct mv_port_priv *pp)
964c5d3e45aSJeff Garzik {
965bdd4dddeSJeff Garzik u32 index;
966bdd4dddeSJeff Garzik
967c5d3e45aSJeff Garzik /*
968c5d3e45aSJeff Garzik * initialize request queue
969c5d3e45aSJeff Garzik */
970fcfb1f77SMark Lord pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
971fcfb1f77SMark Lord index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
972bdd4dddeSJeff Garzik
973c5d3e45aSJeff Garzik WARN_ON(pp->crqb_dma & 0x3ff);
974cae5a29dSMark Lord writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI);
975bdd4dddeSJeff Garzik writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
976cae5a29dSMark Lord port_mmio + EDMA_REQ_Q_IN_PTR);
977cae5a29dSMark Lord writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR);
978c5d3e45aSJeff Garzik
979c5d3e45aSJeff Garzik /*
980c5d3e45aSJeff Garzik * initialize response queue
981c5d3e45aSJeff Garzik */
982fcfb1f77SMark Lord pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
983fcfb1f77SMark Lord index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
984bdd4dddeSJeff Garzik
985c5d3e45aSJeff Garzik WARN_ON(pp->crpb_dma & 0xff);
986cae5a29dSMark Lord writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI);
987cae5a29dSMark Lord writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR);
988bdd4dddeSJeff Garzik writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
989cae5a29dSMark Lord port_mmio + EDMA_RSP_Q_OUT_PTR);
990c5d3e45aSJeff Garzik }
991c5d3e45aSJeff Garzik
mv_write_main_irq_mask(u32 mask,struct mv_host_priv * hpriv)9922b748a0aSMark Lord static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
9932b748a0aSMark Lord {
9942b748a0aSMark Lord /*
9952b748a0aSMark Lord * When writing to the main_irq_mask in hardware,
9962b748a0aSMark Lord * we must ensure exclusivity between the interrupt coalescing bits
9972b748a0aSMark Lord * and the corresponding individual port DONE_IRQ bits.
9982b748a0aSMark Lord *
9992b748a0aSMark Lord * Note that this register is really an "IRQ enable" register,
10002b748a0aSMark Lord * not an "IRQ mask" register as Marvell's naming might suggest.
10012b748a0aSMark Lord */
10022b748a0aSMark Lord if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
10032b748a0aSMark Lord mask &= ~DONE_IRQ_0_3;
10042b748a0aSMark Lord if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
10052b748a0aSMark Lord mask &= ~DONE_IRQ_4_7;
10062b748a0aSMark Lord writelfl(mask, hpriv->main_irq_mask_addr);
10072b748a0aSMark Lord }
10082b748a0aSMark Lord
mv_set_main_irq_mask(struct ata_host * host,u32 disable_bits,u32 enable_bits)1009c4de573bSMark Lord static void mv_set_main_irq_mask(struct ata_host *host,
1010c4de573bSMark Lord u32 disable_bits, u32 enable_bits)
1011c4de573bSMark Lord {
1012c4de573bSMark Lord struct mv_host_priv *hpriv = host->private_data;
1013c4de573bSMark Lord u32 old_mask, new_mask;
1014c4de573bSMark Lord
101596e2c487SMark Lord old_mask = hpriv->main_irq_mask;
1016c4de573bSMark Lord new_mask = (old_mask & ~disable_bits) | enable_bits;
101796e2c487SMark Lord if (new_mask != old_mask) {
101896e2c487SMark Lord hpriv->main_irq_mask = new_mask;
10192b748a0aSMark Lord mv_write_main_irq_mask(new_mask, hpriv);
1020c4de573bSMark Lord }
102196e2c487SMark Lord }
1022c4de573bSMark Lord
mv_enable_port_irqs(struct ata_port * ap,unsigned int port_bits)1023c4de573bSMark Lord static void mv_enable_port_irqs(struct ata_port *ap,
1024c4de573bSMark Lord unsigned int port_bits)
1025c4de573bSMark Lord {
1026c4de573bSMark Lord unsigned int shift, hardport, port = ap->port_no;
1027c4de573bSMark Lord u32 disable_bits, enable_bits;
1028c4de573bSMark Lord
1029c4de573bSMark Lord MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
1030c4de573bSMark Lord
1031c4de573bSMark Lord disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
1032c4de573bSMark Lord enable_bits = port_bits << shift;
1033c4de573bSMark Lord mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
1034c4de573bSMark Lord }
1035c4de573bSMark Lord
mv_clear_and_enable_port_irqs(struct ata_port * ap,void __iomem * port_mmio,unsigned int port_irqs)103600b81235SMark Lord static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
103700b81235SMark Lord void __iomem *port_mmio,
103800b81235SMark Lord unsigned int port_irqs)
1039c6fd2807SJeff Garzik {
10400c58912eSMark Lord struct mv_host_priv *hpriv = ap->host->private_data;
1041352fab70SMark Lord int hardport = mv_hardport_from_port(ap->port_no);
10420c58912eSMark Lord void __iomem *hc_mmio = mv_hc_base_from_port(
1043b0bccb18SMark Lord mv_host_base(ap->host), ap->port_no);
1044cae6edc3SMark Lord u32 hc_irq_cause;
10450c58912eSMark Lord
1046bdd4dddeSJeff Garzik /* clear EDMA event indicators, if any */
1047cae5a29dSMark Lord writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
1048bdd4dddeSJeff Garzik
1049cae6edc3SMark Lord /* clear pending irq events */
1050cae6edc3SMark Lord hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1051cae5a29dSMark Lord writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
10520c58912eSMark Lord
10530c58912eSMark Lord /* clear FIS IRQ Cause */
1054e4006077SMark Lord if (IS_GEN_IIE(hpriv))
1055cae5a29dSMark Lord writelfl(0, port_mmio + FIS_IRQ_CAUSE);
10560c58912eSMark Lord
105700b81235SMark Lord mv_enable_port_irqs(ap, port_irqs);
105800b81235SMark Lord }
105900b81235SMark Lord
mv_set_irq_coalescing(struct ata_host * host,unsigned int count,unsigned int usecs)10602b748a0aSMark Lord static void mv_set_irq_coalescing(struct ata_host *host,
10612b748a0aSMark Lord unsigned int count, unsigned int usecs)
10622b748a0aSMark Lord {
10632b748a0aSMark Lord struct mv_host_priv *hpriv = host->private_data;
10642b748a0aSMark Lord void __iomem *mmio = hpriv->base, *hc_mmio;
10652b748a0aSMark Lord u32 coal_enable = 0;
10662b748a0aSMark Lord unsigned long flags;
10676abf4678SMark Lord unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
10682b748a0aSMark Lord const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
10692b748a0aSMark Lord ALL_PORTS_COAL_DONE;
10702b748a0aSMark Lord
10712b748a0aSMark Lord /* Disable IRQ coalescing if either threshold is zero */
10722b748a0aSMark Lord if (!usecs || !count) {
10732b748a0aSMark Lord clks = count = 0;
10742b748a0aSMark Lord } else {
10752b748a0aSMark Lord /* Respect maximum limits of the hardware */
10762b748a0aSMark Lord clks = usecs * COAL_CLOCKS_PER_USEC;
10772b748a0aSMark Lord if (clks > MAX_COAL_TIME_THRESHOLD)
10782b748a0aSMark Lord clks = MAX_COAL_TIME_THRESHOLD;
10792b748a0aSMark Lord if (count > MAX_COAL_IO_COUNT)
10802b748a0aSMark Lord count = MAX_COAL_IO_COUNT;
10812b748a0aSMark Lord }
10822b748a0aSMark Lord
10832b748a0aSMark Lord spin_lock_irqsave(&host->lock, flags);
10846abf4678SMark Lord mv_set_main_irq_mask(host, coal_disable, 0);
10852b748a0aSMark Lord
10866abf4678SMark Lord if (is_dual_hc && !IS_GEN_I(hpriv)) {
10872b748a0aSMark Lord /*
10886abf4678SMark Lord * GEN_II/GEN_IIE with dual host controllers:
10896abf4678SMark Lord * one set of global thresholds for the entire chip.
10902b748a0aSMark Lord */
1091cae5a29dSMark Lord writel(clks, mmio + IRQ_COAL_TIME_THRESHOLD);
1092cae5a29dSMark Lord writel(count, mmio + IRQ_COAL_IO_THRESHOLD);
10932b748a0aSMark Lord /* clear leftover coal IRQ bit */
1094cae5a29dSMark Lord writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
10956abf4678SMark Lord if (count)
10962b748a0aSMark Lord coal_enable = ALL_PORTS_COAL_DONE;
10976abf4678SMark Lord clks = count = 0; /* force clearing of regular regs below */
10982b748a0aSMark Lord }
10996abf4678SMark Lord
11002b748a0aSMark Lord /*
11012b748a0aSMark Lord * All chips: independent thresholds for each HC on the chip.
11022b748a0aSMark Lord */
11032b748a0aSMark Lord hc_mmio = mv_hc_base_from_port(mmio, 0);
1104cae5a29dSMark Lord writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1105cae5a29dSMark Lord writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1106cae5a29dSMark Lord writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11076abf4678SMark Lord if (count)
11082b748a0aSMark Lord coal_enable |= PORTS_0_3_COAL_DONE;
11096abf4678SMark Lord if (is_dual_hc) {
11102b748a0aSMark Lord hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
1111cae5a29dSMark Lord writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD);
1112cae5a29dSMark Lord writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD);
1113cae5a29dSMark Lord writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE);
11146abf4678SMark Lord if (count)
11152b748a0aSMark Lord coal_enable |= PORTS_4_7_COAL_DONE;
11162b748a0aSMark Lord }
11172b748a0aSMark Lord
11186abf4678SMark Lord mv_set_main_irq_mask(host, 0, coal_enable);
11192b748a0aSMark Lord spin_unlock_irqrestore(&host->lock, flags);
11202b748a0aSMark Lord }
11212b748a0aSMark Lord
1122f3a23c2cSLee Jones /*
112300b81235SMark Lord * mv_start_edma - Enable eDMA engine
112400b81235SMark Lord * @pp: port private data
112500b81235SMark Lord *
112600b81235SMark Lord * Verify the local cache of the eDMA state is accurate with a
112700b81235SMark Lord * WARN_ON.
112800b81235SMark Lord *
112900b81235SMark Lord * LOCKING:
113000b81235SMark Lord * Inherited from caller.
113100b81235SMark Lord */
mv_start_edma(struct ata_port * ap,void __iomem * port_mmio,struct mv_port_priv * pp,u8 protocol)113200b81235SMark Lord static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
113300b81235SMark Lord struct mv_port_priv *pp, u8 protocol)
113400b81235SMark Lord {
113500b81235SMark Lord int want_ncq = (protocol == ATA_PROT_NCQ);
113600b81235SMark Lord
113700b81235SMark Lord if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
113800b81235SMark Lord int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
113900b81235SMark Lord if (want_ncq != using_ncq)
114000b81235SMark Lord mv_stop_edma(ap);
114100b81235SMark Lord }
114200b81235SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
114300b81235SMark Lord struct mv_host_priv *hpriv = ap->host->private_data;
114400b81235SMark Lord
114500b81235SMark Lord mv_edma_cfg(ap, want_ncq, 1);
114600b81235SMark Lord
1147f630d562SMark Lord mv_set_edma_ptrs(port_mmio, hpriv, pp);
114800b81235SMark Lord mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
1149bdd4dddeSJeff Garzik
1150cae5a29dSMark Lord writelfl(EDMA_EN, port_mmio + EDMA_CMD);
1151c6fd2807SJeff Garzik pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
1152c6fd2807SJeff Garzik }
1153c6fd2807SJeff Garzik }
1154c6fd2807SJeff Garzik
mv_wait_for_edma_empty_idle(struct ata_port * ap)11559b2c4e0bSMark Lord static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
11569b2c4e0bSMark Lord {
11579b2c4e0bSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
11589b2c4e0bSMark Lord const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
11599b2c4e0bSMark Lord const int per_loop = 5, timeout = (15 * 1000 / per_loop);
11609b2c4e0bSMark Lord int i;
11619b2c4e0bSMark Lord
11629b2c4e0bSMark Lord /*
11639b2c4e0bSMark Lord * Wait for the EDMA engine to finish transactions in progress.
1164c46938ccSMark Lord * No idea what a good "timeout" value might be, but measurements
1165c46938ccSMark Lord * indicate that it often requires hundreds of microseconds
1166c46938ccSMark Lord * with two drives in-use. So we use the 15msec value above
1167c46938ccSMark Lord * as a rough guess at what even more drives might require.
11689b2c4e0bSMark Lord */
11699b2c4e0bSMark Lord for (i = 0; i < timeout; ++i) {
1170cae5a29dSMark Lord u32 edma_stat = readl(port_mmio + EDMA_STATUS);
11719b2c4e0bSMark Lord if ((edma_stat & empty_idle) == empty_idle)
11729b2c4e0bSMark Lord break;
11739b2c4e0bSMark Lord udelay(per_loop);
11749b2c4e0bSMark Lord }
1175a9a79dfeSJoe Perches /* ata_port_info(ap, "%s: %u+ usecs\n", __func__, i); */
11769b2c4e0bSMark Lord }
11779b2c4e0bSMark Lord
1178c6fd2807SJeff Garzik /**
1179e12bef50SMark Lord * mv_stop_edma_engine - Disable eDMA engine
1180b562468cSMark Lord * @port_mmio: io base address
1181c6fd2807SJeff Garzik *
1182c6fd2807SJeff Garzik * LOCKING:
1183c6fd2807SJeff Garzik * Inherited from caller.
1184c6fd2807SJeff Garzik */
mv_stop_edma_engine(void __iomem * port_mmio)1185b562468cSMark Lord static int mv_stop_edma_engine(void __iomem *port_mmio)
1186c6fd2807SJeff Garzik {
1187b562468cSMark Lord int i;
1188c6fd2807SJeff Garzik
1189b562468cSMark Lord /* Disable eDMA. The disable bit auto clears. */
1190cae5a29dSMark Lord writelfl(EDMA_DS, port_mmio + EDMA_CMD);
1191c6fd2807SJeff Garzik
1192b562468cSMark Lord /* Wait for the chip to confirm eDMA is off. */
1193b562468cSMark Lord for (i = 10000; i > 0; i--) {
1194cae5a29dSMark Lord u32 reg = readl(port_mmio + EDMA_CMD);
11954537deb5SJeff Garzik if (!(reg & EDMA_EN))
1196b562468cSMark Lord return 0;
1197b562468cSMark Lord udelay(10);
1198c6fd2807SJeff Garzik }
1199b562468cSMark Lord return -EIO;
1200c6fd2807SJeff Garzik }
1201c6fd2807SJeff Garzik
mv_stop_edma(struct ata_port * ap)1202e12bef50SMark Lord static int mv_stop_edma(struct ata_port *ap)
1203c6fd2807SJeff Garzik {
1204c6fd2807SJeff Garzik void __iomem *port_mmio = mv_ap_base(ap);
1205c6fd2807SJeff Garzik struct mv_port_priv *pp = ap->private_data;
120666e57a2cSMark Lord int err = 0;
1207c6fd2807SJeff Garzik
1208b562468cSMark Lord if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
1209b562468cSMark Lord return 0;
1210c6fd2807SJeff Garzik pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
12119b2c4e0bSMark Lord mv_wait_for_edma_empty_idle(ap);
1212b562468cSMark Lord if (mv_stop_edma_engine(port_mmio)) {
1213a9a79dfeSJoe Perches ata_port_err(ap, "Unable to stop eDMA\n");
121466e57a2cSMark Lord err = -EIO;
1215c6fd2807SJeff Garzik }
121666e57a2cSMark Lord mv_edma_cfg(ap, 0, 0);
121766e57a2cSMark Lord return err;
12180ea9e179SJeff Garzik }
12190ea9e179SJeff Garzik
mv_dump_mem(struct device * dev,void __iomem * start,unsigned bytes)1220a2715a42SHannes Reinecke static void mv_dump_mem(struct device *dev, void __iomem *start, unsigned bytes)
1221c6fd2807SJeff Garzik {
1222a2715a42SHannes Reinecke int b, w, o;
1223a2715a42SHannes Reinecke unsigned char linebuf[38];
1224a2715a42SHannes Reinecke
1225c6fd2807SJeff Garzik for (b = 0; b < bytes; ) {
1226a2715a42SHannes Reinecke for (w = 0, o = 0; b < bytes && w < 4; w++) {
1227e97eb65dSChristophe JAILLET o += scnprintf(linebuf + o, sizeof(linebuf) - o,
1228a2715a42SHannes Reinecke "%08x ", readl(start + b));
1229c6fd2807SJeff Garzik b += sizeof(u32);
1230c6fd2807SJeff Garzik }
1231a2715a42SHannes Reinecke dev_dbg(dev, "%s: %p: %s\n",
1232a2715a42SHannes Reinecke __func__, start + b, linebuf);
1233c6fd2807SJeff Garzik }
1234c6fd2807SJeff Garzik }
1235a2715a42SHannes Reinecke
mv_dump_pci_cfg(struct pci_dev * pdev,unsigned bytes)1236c6fd2807SJeff Garzik static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
1237c6fd2807SJeff Garzik {
1238a2715a42SHannes Reinecke int b, w, o;
1239a2715a42SHannes Reinecke u32 dw = 0;
1240a2715a42SHannes Reinecke unsigned char linebuf[38];
1241a2715a42SHannes Reinecke
1242c6fd2807SJeff Garzik for (b = 0; b < bytes; ) {
1243a2715a42SHannes Reinecke for (w = 0, o = 0; b < bytes && w < 4; w++) {
1244c6fd2807SJeff Garzik (void) pci_read_config_dword(pdev, b, &dw);
1245a2715a42SHannes Reinecke o += snprintf(linebuf + o, sizeof(linebuf) - o,
1246a2715a42SHannes Reinecke "%08x ", dw);
1247c6fd2807SJeff Garzik b += sizeof(u32);
1248c6fd2807SJeff Garzik }
1249a2715a42SHannes Reinecke dev_dbg(&pdev->dev, "%s: %02x: %s\n",
1250a2715a42SHannes Reinecke __func__, b, linebuf);
1251c6fd2807SJeff Garzik }
1252c6fd2807SJeff Garzik }
1253a2715a42SHannes Reinecke
mv_dump_all_regs(void __iomem * mmio_base,struct pci_dev * pdev)125437fcfadeSHannes Reinecke static void mv_dump_all_regs(void __iomem *mmio_base,
1255c6fd2807SJeff Garzik struct pci_dev *pdev)
1256c6fd2807SJeff Garzik {
125737fcfadeSHannes Reinecke void __iomem *hc_base;
1258c6fd2807SJeff Garzik void __iomem *port_base;
1259c6fd2807SJeff Garzik int start_port, num_ports, p, start_hc, num_hcs, hc;
1260c6fd2807SJeff Garzik
1261c6fd2807SJeff Garzik start_hc = start_port = 0;
126237fcfadeSHannes Reinecke num_ports = 8; /* should be benign for 4 port devs */
1263c6fd2807SJeff Garzik num_hcs = 2;
1264a2715a42SHannes Reinecke dev_dbg(&pdev->dev,
1265a2715a42SHannes Reinecke "%s: All registers for port(s) %u-%u:\n", __func__,
1266a2715a42SHannes Reinecke start_port, num_ports > 1 ? num_ports - 1 : start_port);
1267c6fd2807SJeff Garzik
1268a2715a42SHannes Reinecke dev_dbg(&pdev->dev, "%s: PCI config space regs:\n", __func__);
1269c6fd2807SJeff Garzik mv_dump_pci_cfg(pdev, 0x68);
1270a2715a42SHannes Reinecke
1271a2715a42SHannes Reinecke dev_dbg(&pdev->dev, "%s: PCI regs:\n", __func__);
1272a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, mmio_base+0xc00, 0x3c);
1273a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, mmio_base+0xd00, 0x34);
1274a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, mmio_base+0xf00, 0x4);
1275a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, mmio_base+0x1d00, 0x6c);
1276c6fd2807SJeff Garzik for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
1277c6fd2807SJeff Garzik hc_base = mv_hc_base(mmio_base, hc);
1278a2715a42SHannes Reinecke dev_dbg(&pdev->dev, "%s: HC regs (HC %i):\n", __func__, hc);
1279a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, hc_base, 0x1c);
1280c6fd2807SJeff Garzik }
1281c6fd2807SJeff Garzik for (p = start_port; p < start_port + num_ports; p++) {
1282c6fd2807SJeff Garzik port_base = mv_port_base(mmio_base, p);
1283a2715a42SHannes Reinecke dev_dbg(&pdev->dev, "%s: EDMA regs (port %i):\n", __func__, p);
1284a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, port_base, 0x54);
1285a2715a42SHannes Reinecke dev_dbg(&pdev->dev, "%s: SATA regs (port %i):\n", __func__, p);
1286a2715a42SHannes Reinecke mv_dump_mem(&pdev->dev, port_base+0x300, 0x60);
1287c6fd2807SJeff Garzik }
1288c6fd2807SJeff Garzik }
1289c6fd2807SJeff Garzik
mv_scr_offset(unsigned int sc_reg_in)1290c6fd2807SJeff Garzik static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1291c6fd2807SJeff Garzik {
1292c6fd2807SJeff Garzik unsigned int ofs;
1293c6fd2807SJeff Garzik
1294c6fd2807SJeff Garzik switch (sc_reg_in) {
1295c6fd2807SJeff Garzik case SCR_STATUS:
1296c6fd2807SJeff Garzik case SCR_CONTROL:
1297c6fd2807SJeff Garzik case SCR_ERROR:
1298cae5a29dSMark Lord ofs = SATA_STATUS + (sc_reg_in * sizeof(u32));
1299c6fd2807SJeff Garzik break;
1300c6fd2807SJeff Garzik case SCR_ACTIVE:
1301cae5a29dSMark Lord ofs = SATA_ACTIVE; /* active is not with the others */
1302c6fd2807SJeff Garzik break;
1303c6fd2807SJeff Garzik default:
1304c6fd2807SJeff Garzik ofs = 0xffffffffU;
1305c6fd2807SJeff Garzik break;
1306c6fd2807SJeff Garzik }
1307c6fd2807SJeff Garzik return ofs;
1308c6fd2807SJeff Garzik }
1309c6fd2807SJeff Garzik
mv_scr_read(struct ata_link * link,unsigned int sc_reg_in,u32 * val)131082ef04fbSTejun Heo static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
1311c6fd2807SJeff Garzik {
1312c6fd2807SJeff Garzik unsigned int ofs = mv_scr_offset(sc_reg_in);
1313c6fd2807SJeff Garzik
1314da3dbb17STejun Heo if (ofs != 0xffffffffU) {
131582ef04fbSTejun Heo *val = readl(mv_ap_base(link->ap) + ofs);
1316da3dbb17STejun Heo return 0;
1317da3dbb17STejun Heo } else
1318da3dbb17STejun Heo return -EINVAL;
1319c6fd2807SJeff Garzik }
1320c6fd2807SJeff Garzik
mv_scr_write(struct ata_link * link,unsigned int sc_reg_in,u32 val)132182ef04fbSTejun Heo static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
1322c6fd2807SJeff Garzik {
1323c6fd2807SJeff Garzik unsigned int ofs = mv_scr_offset(sc_reg_in);
1324c6fd2807SJeff Garzik
1325da3dbb17STejun Heo if (ofs != 0xffffffffU) {
132620091773SMark Lord void __iomem *addr = mv_ap_base(link->ap) + ofs;
13279013d64eSLior Amsalem struct mv_host_priv *hpriv = link->ap->host->private_data;
132820091773SMark Lord if (sc_reg_in == SCR_CONTROL) {
132920091773SMark Lord /*
133020091773SMark Lord * Workaround for 88SX60x1 FEr SATA#26:
133120091773SMark Lord *
133225985edcSLucas De Marchi * COMRESETs have to take care not to accidentally
133320091773SMark Lord * put the drive to sleep when writing SCR_CONTROL.
133420091773SMark Lord * Setting bits 12..15 prevents this problem.
133520091773SMark Lord *
133620091773SMark Lord * So if we see an outbound COMMRESET, set those bits.
133720091773SMark Lord * Ditto for the followup write that clears the reset.
133820091773SMark Lord *
133920091773SMark Lord * The proprietary driver does this for
134020091773SMark Lord * all chip versions, and so do we.
134120091773SMark Lord */
134220091773SMark Lord if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1)
134320091773SMark Lord val |= 0xf000;
13449013d64eSLior Amsalem
13459013d64eSLior Amsalem if (hpriv->hp_flags & MV_HP_FIX_LP_PHY_CTL) {
13469013d64eSLior Amsalem void __iomem *lp_phy_addr =
13479013d64eSLior Amsalem mv_ap_base(link->ap) + LP_PHY_CTL;
13489013d64eSLior Amsalem /*
13499013d64eSLior Amsalem * Set PHY speed according to SControl speed.
13509013d64eSLior Amsalem */
13513661aa99SThomas Petazzoni u32 lp_phy_val =
13523661aa99SThomas Petazzoni LP_PHY_CTL_PIN_PU_PLL |
13533661aa99SThomas Petazzoni LP_PHY_CTL_PIN_PU_RX |
13543661aa99SThomas Petazzoni LP_PHY_CTL_PIN_PU_TX;
13553661aa99SThomas Petazzoni
13563661aa99SThomas Petazzoni if ((val & 0xf0) != 0x10)
13573661aa99SThomas Petazzoni lp_phy_val |=
13583661aa99SThomas Petazzoni LP_PHY_CTL_GEN_TX_3G |
13593661aa99SThomas Petazzoni LP_PHY_CTL_GEN_RX_3G;
13603661aa99SThomas Petazzoni
13613661aa99SThomas Petazzoni writelfl(lp_phy_val, lp_phy_addr);
13629013d64eSLior Amsalem }
136320091773SMark Lord }
136420091773SMark Lord writelfl(val, addr);
1365da3dbb17STejun Heo return 0;
1366da3dbb17STejun Heo } else
1367da3dbb17STejun Heo return -EINVAL;
1368c6fd2807SJeff Garzik }
1369c6fd2807SJeff Garzik
mv6_dev_config(struct ata_device * adev)1370f273827eSMark Lord static void mv6_dev_config(struct ata_device *adev)
1371f273827eSMark Lord {
1372f273827eSMark Lord /*
1373e49856d8SMark Lord * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1374e49856d8SMark Lord *
1375e49856d8SMark Lord * Gen-II does not support NCQ over a port multiplier
1376e49856d8SMark Lord * (no FIS-based switching).
1377f273827eSMark Lord */
1378e49856d8SMark Lord if (adev->flags & ATA_DFLAG_NCQ) {
1379352fab70SMark Lord if (sata_pmp_attached(adev->link->ap)) {
1380e49856d8SMark Lord adev->flags &= ~ATA_DFLAG_NCQ;
1381a9a79dfeSJoe Perches ata_dev_info(adev,
1382352fab70SMark Lord "NCQ disabled for command-based switching\n");
1383352fab70SMark Lord }
1384f273827eSMark Lord }
1385e49856d8SMark Lord }
1386f273827eSMark Lord
mv_qc_defer(struct ata_queued_cmd * qc)13873e4a1391SMark Lord static int mv_qc_defer(struct ata_queued_cmd *qc)
13883e4a1391SMark Lord {
13893e4a1391SMark Lord struct ata_link *link = qc->dev->link;
13903e4a1391SMark Lord struct ata_port *ap = link->ap;
13913e4a1391SMark Lord struct mv_port_priv *pp = ap->private_data;
13923e4a1391SMark Lord
13933e4a1391SMark Lord /*
139429d187bbSMark Lord * Don't allow new commands if we're in a delayed EH state
139529d187bbSMark Lord * for NCQ and/or FIS-based switching.
139629d187bbSMark Lord */
139729d187bbSMark Lord if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
139829d187bbSMark Lord return ATA_DEFER_PORT;
1399159a7ff7SGwendal Grignou
1400159a7ff7SGwendal Grignou /* PIO commands need exclusive link: no other commands [DMA or PIO]
1401159a7ff7SGwendal Grignou * can run concurrently.
1402159a7ff7SGwendal Grignou * set excl_link when we want to send a PIO command in DMA mode
1403159a7ff7SGwendal Grignou * or a non-NCQ command in NCQ mode.
1404159a7ff7SGwendal Grignou * When we receive a command from that link, and there are no
1405159a7ff7SGwendal Grignou * outstanding commands, mark a flag to clear excl_link and let
1406159a7ff7SGwendal Grignou * the command go through.
1407159a7ff7SGwendal Grignou */
1408159a7ff7SGwendal Grignou if (unlikely(ap->excl_link)) {
1409159a7ff7SGwendal Grignou if (link == ap->excl_link) {
1410159a7ff7SGwendal Grignou if (ap->nr_active_links)
1411159a7ff7SGwendal Grignou return ATA_DEFER_PORT;
1412159a7ff7SGwendal Grignou qc->flags |= ATA_QCFLAG_CLEAR_EXCL;
1413159a7ff7SGwendal Grignou return 0;
1414159a7ff7SGwendal Grignou } else
1415159a7ff7SGwendal Grignou return ATA_DEFER_PORT;
1416159a7ff7SGwendal Grignou }
1417159a7ff7SGwendal Grignou
141829d187bbSMark Lord /*
14193e4a1391SMark Lord * If the port is completely idle, then allow the new qc.
14203e4a1391SMark Lord */
14213e4a1391SMark Lord if (ap->nr_active_links == 0)
14223e4a1391SMark Lord return 0;
14233e4a1391SMark Lord
14243e4a1391SMark Lord /*
14254bdee6c5STejun Heo * The port is operating in host queuing mode (EDMA) with NCQ
14264bdee6c5STejun Heo * enabled, allow multiple NCQ commands. EDMA also allows
14274bdee6c5STejun Heo * queueing multiple DMA commands but libata core currently
14284bdee6c5STejun Heo * doesn't allow it.
14293e4a1391SMark Lord */
14304bdee6c5STejun Heo if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
1431159a7ff7SGwendal Grignou (pp->pp_flags & MV_PP_FLAG_NCQ_EN)) {
1432159a7ff7SGwendal Grignou if (ata_is_ncq(qc->tf.protocol))
14333e4a1391SMark Lord return 0;
1434159a7ff7SGwendal Grignou else {
1435159a7ff7SGwendal Grignou ap->excl_link = link;
1436159a7ff7SGwendal Grignou return ATA_DEFER_PORT;
1437159a7ff7SGwendal Grignou }
1438159a7ff7SGwendal Grignou }
14394bdee6c5STejun Heo
14403e4a1391SMark Lord return ATA_DEFER_PORT;
14413e4a1391SMark Lord }
14423e4a1391SMark Lord
mv_config_fbs(struct ata_port * ap,int want_ncq,int want_fbs)144308da1759SMark Lord static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
1444e49856d8SMark Lord {
144508da1759SMark Lord struct mv_port_priv *pp = ap->private_data;
144608da1759SMark Lord void __iomem *port_mmio;
144700f42eabSMark Lord
144808da1759SMark Lord u32 fiscfg, *old_fiscfg = &pp->cached.fiscfg;
144908da1759SMark Lord u32 ltmode, *old_ltmode = &pp->cached.ltmode;
145008da1759SMark Lord u32 haltcond, *old_haltcond = &pp->cached.haltcond;
145100f42eabSMark Lord
145208da1759SMark Lord ltmode = *old_ltmode & ~LTMODE_BIT8;
145308da1759SMark Lord haltcond = *old_haltcond | EDMA_ERR_DEV;
145400f42eabSMark Lord
145500f42eabSMark Lord if (want_fbs) {
145608da1759SMark Lord fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
145708da1759SMark Lord ltmode = *old_ltmode | LTMODE_BIT8;
14584c299ca3SMark Lord if (want_ncq)
145908da1759SMark Lord haltcond &= ~EDMA_ERR_DEV;
14604c299ca3SMark Lord else
146108da1759SMark Lord fiscfg |= FISCFG_WAIT_DEV_ERR;
146208da1759SMark Lord } else {
146308da1759SMark Lord fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
1464e49856d8SMark Lord }
146500f42eabSMark Lord
146608da1759SMark Lord port_mmio = mv_ap_base(ap);
1467cae5a29dSMark Lord mv_write_cached_reg(port_mmio + FISCFG, old_fiscfg, fiscfg);
1468cae5a29dSMark Lord mv_write_cached_reg(port_mmio + LTMODE, old_ltmode, ltmode);
1469cae5a29dSMark Lord mv_write_cached_reg(port_mmio + EDMA_HALTCOND, old_haltcond, haltcond);
1470e49856d8SMark Lord }
1471c6fd2807SJeff Garzik
mv_60x1_errata_sata25(struct ata_port * ap,int want_ncq)1472dd2890f6SMark Lord static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1473dd2890f6SMark Lord {
1474dd2890f6SMark Lord struct mv_host_priv *hpriv = ap->host->private_data;
1475dd2890f6SMark Lord u32 old, new;
1476dd2890f6SMark Lord
1477dd2890f6SMark Lord /* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1478cae5a29dSMark Lord old = readl(hpriv->base + GPIO_PORT_CTL);
1479dd2890f6SMark Lord if (want_ncq)
1480dd2890f6SMark Lord new = old | (1 << 22);
1481dd2890f6SMark Lord else
1482dd2890f6SMark Lord new = old & ~(1 << 22);
1483dd2890f6SMark Lord if (new != old)
1484cae5a29dSMark Lord writel(new, hpriv->base + GPIO_PORT_CTL);
1485dd2890f6SMark Lord }
1486dd2890f6SMark Lord
1487f3a23c2cSLee Jones /*
1488c01e8a23SMark Lord * mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
1489c01e8a23SMark Lord * @ap: Port being initialized
1490c01e8a23SMark Lord *
1491c01e8a23SMark Lord * There are two DMA modes on these chips: basic DMA, and EDMA.
1492c01e8a23SMark Lord *
1493c01e8a23SMark Lord * Bit-0 of the "EDMA RESERVED" register enables/disables use
1494c01e8a23SMark Lord * of basic DMA on the GEN_IIE versions of the chips.
1495c01e8a23SMark Lord *
1496c01e8a23SMark Lord * This bit survives EDMA resets, and must be set for basic DMA
1497c01e8a23SMark Lord * to function, and should be cleared when EDMA is active.
1498c01e8a23SMark Lord */
mv_bmdma_enable_iie(struct ata_port * ap,int enable_bmdma)1499c01e8a23SMark Lord static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
1500c01e8a23SMark Lord {
1501c01e8a23SMark Lord struct mv_port_priv *pp = ap->private_data;
1502c01e8a23SMark Lord u32 new, *old = &pp->cached.unknown_rsvd;
1503c01e8a23SMark Lord
1504c01e8a23SMark Lord if (enable_bmdma)
1505c01e8a23SMark Lord new = *old | 1;
1506c01e8a23SMark Lord else
1507c01e8a23SMark Lord new = *old & ~1;
1508cae5a29dSMark Lord mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD, old, new);
1509c01e8a23SMark Lord }
1510c01e8a23SMark Lord
1511000b344fSMark Lord /*
1512000b344fSMark Lord * SOC chips have an issue whereby the HDD LEDs don't always blink
1513000b344fSMark Lord * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
1514000b344fSMark Lord * of the SOC takes care of it, generating a steady blink rate when
1515000b344fSMark Lord * any drive on the chip is active.
1516000b344fSMark Lord *
1517000b344fSMark Lord * Unfortunately, the blink mode is a global hardware setting for the SOC,
1518000b344fSMark Lord * so we must use it whenever at least one port on the SOC has NCQ enabled.
1519000b344fSMark Lord *
1520000b344fSMark Lord * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
1521000b344fSMark Lord * LED operation works then, and provides better (more accurate) feedback.
1522000b344fSMark Lord *
1523000b344fSMark Lord * Note that this code assumes that an SOC never has more than one HC onboard.
1524000b344fSMark Lord */
mv_soc_led_blink_enable(struct ata_port * ap)1525000b344fSMark Lord static void mv_soc_led_blink_enable(struct ata_port *ap)
1526000b344fSMark Lord {
1527000b344fSMark Lord struct ata_host *host = ap->host;
1528000b344fSMark Lord struct mv_host_priv *hpriv = host->private_data;
1529000b344fSMark Lord void __iomem *hc_mmio;
1530000b344fSMark Lord u32 led_ctrl;
1531000b344fSMark Lord
1532000b344fSMark Lord if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
1533000b344fSMark Lord return;
1534000b344fSMark Lord hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN;
1535000b344fSMark Lord hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1536cae5a29dSMark Lord led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1537cae5a29dSMark Lord writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1538000b344fSMark Lord }
1539000b344fSMark Lord
mv_soc_led_blink_disable(struct ata_port * ap)1540000b344fSMark Lord static void mv_soc_led_blink_disable(struct ata_port *ap)
1541000b344fSMark Lord {
1542000b344fSMark Lord struct ata_host *host = ap->host;
1543000b344fSMark Lord struct mv_host_priv *hpriv = host->private_data;
1544000b344fSMark Lord void __iomem *hc_mmio;
1545000b344fSMark Lord u32 led_ctrl;
1546000b344fSMark Lord unsigned int port;
1547000b344fSMark Lord
1548000b344fSMark Lord if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
1549000b344fSMark Lord return;
1550000b344fSMark Lord
1551000b344fSMark Lord /* disable led-blink only if no ports are using NCQ */
1552000b344fSMark Lord for (port = 0; port < hpriv->n_ports; port++) {
1553000b344fSMark Lord struct ata_port *this_ap = host->ports[port];
1554000b344fSMark Lord struct mv_port_priv *pp = this_ap->private_data;
1555000b344fSMark Lord
1556000b344fSMark Lord if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1557000b344fSMark Lord return;
1558000b344fSMark Lord }
1559000b344fSMark Lord
1560000b344fSMark Lord hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
1561000b344fSMark Lord hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1562cae5a29dSMark Lord led_ctrl = readl(hc_mmio + SOC_LED_CTRL);
1563cae5a29dSMark Lord writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL);
1564000b344fSMark Lord }
1565000b344fSMark Lord
mv_edma_cfg(struct ata_port * ap,int want_ncq,int want_edma)156600b81235SMark Lord static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
1567c6fd2807SJeff Garzik {
1568c6fd2807SJeff Garzik u32 cfg;
1569e12bef50SMark Lord struct mv_port_priv *pp = ap->private_data;
1570e12bef50SMark Lord struct mv_host_priv *hpriv = ap->host->private_data;
1571e12bef50SMark Lord void __iomem *port_mmio = mv_ap_base(ap);
1572c6fd2807SJeff Garzik
1573c6fd2807SJeff Garzik /* set up non-NCQ EDMA configuration */
1574c6fd2807SJeff Garzik cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
1575d16ab3f6SMark Lord pp->pp_flags &=
1576d16ab3f6SMark Lord ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
1577c6fd2807SJeff Garzik
1578c6fd2807SJeff Garzik if (IS_GEN_I(hpriv))
1579c6fd2807SJeff Garzik cfg |= (1 << 8); /* enab config burst size mask */
1580c6fd2807SJeff Garzik
1581dd2890f6SMark Lord else if (IS_GEN_II(hpriv)) {
1582c6fd2807SJeff Garzik cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1583dd2890f6SMark Lord mv_60x1_errata_sata25(ap, want_ncq);
1584c6fd2807SJeff Garzik
1585dd2890f6SMark Lord } else if (IS_GEN_IIE(hpriv)) {
158600f42eabSMark Lord int want_fbs = sata_pmp_attached(ap);
158700f42eabSMark Lord /*
158800f42eabSMark Lord * Possible future enhancement:
158900f42eabSMark Lord *
159000f42eabSMark Lord * The chip can use FBS with non-NCQ, if we allow it,
159100f42eabSMark Lord * But first we need to have the error handling in place
159200f42eabSMark Lord * for this mode (datasheet section 7.3.15.4.2.3).
159300f42eabSMark Lord * So disallow non-NCQ FBS for now.
159400f42eabSMark Lord */
159500f42eabSMark Lord want_fbs &= want_ncq;
159600f42eabSMark Lord
159708da1759SMark Lord mv_config_fbs(ap, want_ncq, want_fbs);
159800f42eabSMark Lord
159900f42eabSMark Lord if (want_fbs) {
160000f42eabSMark Lord pp->pp_flags |= MV_PP_FLAG_FBS_EN;
160100f42eabSMark Lord cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
160200f42eabSMark Lord }
160300f42eabSMark Lord
1604e728eabeSJeff Garzik cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
160500b81235SMark Lord if (want_edma) {
1606e728eabeSJeff Garzik cfg |= (1 << 22); /* enab 4-entry host queue cache */
16071f398472SMark Lord if (!IS_SOC(hpriv))
1608c6fd2807SJeff Garzik cfg |= (1 << 18); /* enab early completion */
160900b81235SMark Lord }
1610616d4a98SMark Lord if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1611616d4a98SMark Lord cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
1612c01e8a23SMark Lord mv_bmdma_enable_iie(ap, !want_edma);
1613000b344fSMark Lord
1614000b344fSMark Lord if (IS_SOC(hpriv)) {
1615000b344fSMark Lord if (want_ncq)
1616000b344fSMark Lord mv_soc_led_blink_enable(ap);
1617000b344fSMark Lord else
1618000b344fSMark Lord mv_soc_led_blink_disable(ap);
1619000b344fSMark Lord }
1620c6fd2807SJeff Garzik }
1621c6fd2807SJeff Garzik
162272109168SMark Lord if (want_ncq) {
162372109168SMark Lord cfg |= EDMA_CFG_NCQ;
162472109168SMark Lord pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
162500b81235SMark Lord }
162672109168SMark Lord
1627cae5a29dSMark Lord writelfl(cfg, port_mmio + EDMA_CFG);
1628c6fd2807SJeff Garzik }
1629c6fd2807SJeff Garzik
mv_port_free_dma_mem(struct ata_port * ap)1630da2fa9baSMark Lord static void mv_port_free_dma_mem(struct ata_port *ap)
1631da2fa9baSMark Lord {
1632da2fa9baSMark Lord struct mv_host_priv *hpriv = ap->host->private_data;
1633da2fa9baSMark Lord struct mv_port_priv *pp = ap->private_data;
1634eb73d558SMark Lord int tag;
1635da2fa9baSMark Lord
1636da2fa9baSMark Lord if (pp->crqb) {
1637da2fa9baSMark Lord dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1638da2fa9baSMark Lord pp->crqb = NULL;
1639da2fa9baSMark Lord }
1640da2fa9baSMark Lord if (pp->crpb) {
1641da2fa9baSMark Lord dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1642da2fa9baSMark Lord pp->crpb = NULL;
1643da2fa9baSMark Lord }
1644eb73d558SMark Lord /*
1645eb73d558SMark Lord * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1646eb73d558SMark Lord * For later hardware, we have one unique sg_tbl per NCQ tag.
1647eb73d558SMark Lord */
1648eb73d558SMark Lord for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1649eb73d558SMark Lord if (pp->sg_tbl[tag]) {
1650eb73d558SMark Lord if (tag == 0 || !IS_GEN_I(hpriv))
1651eb73d558SMark Lord dma_pool_free(hpriv->sg_tbl_pool,
1652eb73d558SMark Lord pp->sg_tbl[tag],
1653eb73d558SMark Lord pp->sg_tbl_dma[tag]);
1654eb73d558SMark Lord pp->sg_tbl[tag] = NULL;
1655eb73d558SMark Lord }
1656da2fa9baSMark Lord }
1657da2fa9baSMark Lord }
1658da2fa9baSMark Lord
1659c6fd2807SJeff Garzik /**
1660c6fd2807SJeff Garzik * mv_port_start - Port specific init/start routine.
1661c6fd2807SJeff Garzik * @ap: ATA channel to manipulate
1662c6fd2807SJeff Garzik *
1663c6fd2807SJeff Garzik * Allocate and point to DMA memory, init port private memory,
1664c6fd2807SJeff Garzik * zero indices.
1665c6fd2807SJeff Garzik *
1666c6fd2807SJeff Garzik * LOCKING:
1667c6fd2807SJeff Garzik * Inherited from caller.
1668c6fd2807SJeff Garzik */
mv_port_start(struct ata_port * ap)1669c6fd2807SJeff Garzik static int mv_port_start(struct ata_port *ap)
1670c6fd2807SJeff Garzik {
1671cca3974eSJeff Garzik struct device *dev = ap->host->dev;
1672cca3974eSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data;
1673c6fd2807SJeff Garzik struct mv_port_priv *pp;
1674933cb8e5SMark Lord unsigned long flags;
1675dde20207SJames Bottomley int tag;
1676c6fd2807SJeff Garzik
167724dc5f33STejun Heo pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
1678c6fd2807SJeff Garzik if (!pp)
167924dc5f33STejun Heo return -ENOMEM;
1680da2fa9baSMark Lord ap->private_data = pp;
1681c6fd2807SJeff Garzik
16826ec76070SHarman Kalra pp->crqb = dma_pool_zalloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1683da2fa9baSMark Lord if (!pp->crqb)
1684da2fa9baSMark Lord return -ENOMEM;
1685c6fd2807SJeff Garzik
16866ec76070SHarman Kalra pp->crpb = dma_pool_zalloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1687da2fa9baSMark Lord if (!pp->crpb)
1688da2fa9baSMark Lord goto out_port_free_dma_mem;
1689c6fd2807SJeff Garzik
16903bd0a70eSMark Lord /* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
16913bd0a70eSMark Lord if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
16923bd0a70eSMark Lord ap->flags |= ATA_FLAG_AN;
1693eb73d558SMark Lord /*
1694eb73d558SMark Lord * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1695eb73d558SMark Lord * For later hardware, we need one unique sg_tbl per NCQ tag.
1696eb73d558SMark Lord */
1697eb73d558SMark Lord for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1698eb73d558SMark Lord if (tag == 0 || !IS_GEN_I(hpriv)) {
1699eb73d558SMark Lord pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1700eb73d558SMark Lord GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1701eb73d558SMark Lord if (!pp->sg_tbl[tag])
1702da2fa9baSMark Lord goto out_port_free_dma_mem;
1703eb73d558SMark Lord } else {
1704eb73d558SMark Lord pp->sg_tbl[tag] = pp->sg_tbl[0];
1705eb73d558SMark Lord pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1706eb73d558SMark Lord }
1707eb73d558SMark Lord }
1708933cb8e5SMark Lord
1709933cb8e5SMark Lord spin_lock_irqsave(ap->lock, flags);
171008da1759SMark Lord mv_save_cached_regs(ap);
171166e57a2cSMark Lord mv_edma_cfg(ap, 0, 0);
1712933cb8e5SMark Lord spin_unlock_irqrestore(ap->lock, flags);
1713933cb8e5SMark Lord
1714c6fd2807SJeff Garzik return 0;
1715da2fa9baSMark Lord
1716da2fa9baSMark Lord out_port_free_dma_mem:
1717da2fa9baSMark Lord mv_port_free_dma_mem(ap);
1718da2fa9baSMark Lord return -ENOMEM;
1719c6fd2807SJeff Garzik }
1720c6fd2807SJeff Garzik
1721c6fd2807SJeff Garzik /**
1722c6fd2807SJeff Garzik * mv_port_stop - Port specific cleanup/stop routine.
1723c6fd2807SJeff Garzik * @ap: ATA channel to manipulate
1724c6fd2807SJeff Garzik *
1725c6fd2807SJeff Garzik * Stop DMA, cleanup port memory.
1726c6fd2807SJeff Garzik *
1727c6fd2807SJeff Garzik * LOCKING:
1728cca3974eSJeff Garzik * This routine uses the host lock to protect the DMA stop.
1729c6fd2807SJeff Garzik */
mv_port_stop(struct ata_port * ap)1730c6fd2807SJeff Garzik static void mv_port_stop(struct ata_port *ap)
1731c6fd2807SJeff Garzik {
1732933cb8e5SMark Lord unsigned long flags;
1733933cb8e5SMark Lord
1734933cb8e5SMark Lord spin_lock_irqsave(ap->lock, flags);
1735e12bef50SMark Lord mv_stop_edma(ap);
173688e675e1SMark Lord mv_enable_port_irqs(ap, 0);
1737933cb8e5SMark Lord spin_unlock_irqrestore(ap->lock, flags);
1738da2fa9baSMark Lord mv_port_free_dma_mem(ap);
1739c6fd2807SJeff Garzik }
1740c6fd2807SJeff Garzik
1741c6fd2807SJeff Garzik /**
1742c6fd2807SJeff Garzik * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1743c6fd2807SJeff Garzik * @qc: queued command whose SG list to source from
1744c6fd2807SJeff Garzik *
1745c6fd2807SJeff Garzik * Populate the SG list and mark the last entry.
1746c6fd2807SJeff Garzik *
1747c6fd2807SJeff Garzik * LOCKING:
1748c6fd2807SJeff Garzik * Inherited from caller.
1749c6fd2807SJeff Garzik */
mv_fill_sg(struct ata_queued_cmd * qc)17506c08772eSJeff Garzik static void mv_fill_sg(struct ata_queued_cmd *qc)
1751c6fd2807SJeff Garzik {
1752c6fd2807SJeff Garzik struct mv_port_priv *pp = qc->ap->private_data;
1753c6fd2807SJeff Garzik struct scatterlist *sg;
17543be6cbd7SJeff Garzik struct mv_sg *mv_sg, *last_sg = NULL;
1755ff2aeb1eSTejun Heo unsigned int si;
1756c6fd2807SJeff Garzik
17574e5b6260SJens Axboe mv_sg = pp->sg_tbl[qc->hw_tag];
1758ff2aeb1eSTejun Heo for_each_sg(qc->sg, sg, qc->n_elem, si) {
1759d88184fbSJeff Garzik dma_addr_t addr = sg_dma_address(sg);
1760d88184fbSJeff Garzik u32 sg_len = sg_dma_len(sg);
1761c6fd2807SJeff Garzik
17624007b493SOlof Johansson while (sg_len) {
17634007b493SOlof Johansson u32 offset = addr & 0xffff;
17644007b493SOlof Johansson u32 len = sg_len;
17654007b493SOlof Johansson
176632cd11a6SMark Lord if (offset + len > 0x10000)
17674007b493SOlof Johansson len = 0x10000 - offset;
17684007b493SOlof Johansson
1769d88184fbSJeff Garzik mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1770d88184fbSJeff Garzik mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
17716c08772eSJeff Garzik mv_sg->flags_size = cpu_to_le32(len & 0xffff);
177232cd11a6SMark Lord mv_sg->reserved = 0;
1773c6fd2807SJeff Garzik
17744007b493SOlof Johansson sg_len -= len;
17754007b493SOlof Johansson addr += len;
17764007b493SOlof Johansson
17773be6cbd7SJeff Garzik last_sg = mv_sg;
1778d88184fbSJeff Garzik mv_sg++;
1779c6fd2807SJeff Garzik }
17804007b493SOlof Johansson }
17813be6cbd7SJeff Garzik
17823be6cbd7SJeff Garzik if (likely(last_sg))
17833be6cbd7SJeff Garzik last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
178432cd11a6SMark Lord mb(); /* ensure data structure is visible to the chipset */
1785c6fd2807SJeff Garzik }
1786c6fd2807SJeff Garzik
mv_crqb_pack_cmd(__le16 * cmdw,u8 data,u8 addr,unsigned last)17875796d1c4SJeff Garzik static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
1788c6fd2807SJeff Garzik {
1789c6fd2807SJeff Garzik u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
1790c6fd2807SJeff Garzik (last ? CRQB_CMD_LAST : 0);
1791c6fd2807SJeff Garzik *cmdw = cpu_to_le16(tmp);
1792c6fd2807SJeff Garzik }
1793c6fd2807SJeff Garzik
1794c6fd2807SJeff Garzik /**
1795da14265eSMark Lord * mv_sff_irq_clear - Clear hardware interrupt after DMA.
1796da14265eSMark Lord * @ap: Port associated with this ATA transaction.
1797da14265eSMark Lord *
1798da14265eSMark Lord * We need this only for ATAPI bmdma transactions,
1799da14265eSMark Lord * as otherwise we experience spurious interrupts
1800da14265eSMark Lord * after libata-sff handles the bmdma interrupts.
1801da14265eSMark Lord */
mv_sff_irq_clear(struct ata_port * ap)1802da14265eSMark Lord static void mv_sff_irq_clear(struct ata_port *ap)
1803da14265eSMark Lord {
1804da14265eSMark Lord mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
1805da14265eSMark Lord }
1806da14265eSMark Lord
1807da14265eSMark Lord /**
1808da14265eSMark Lord * mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
1809da14265eSMark Lord * @qc: queued command to check for chipset/DMA compatibility.
1810da14265eSMark Lord *
1811da14265eSMark Lord * The bmdma engines cannot handle speculative data sizes
1812da14265eSMark Lord * (bytecount under/over flow). So only allow DMA for
1813da14265eSMark Lord * data transfer commands with known data sizes.
1814da14265eSMark Lord *
1815da14265eSMark Lord * LOCKING:
1816da14265eSMark Lord * Inherited from caller.
1817da14265eSMark Lord */
mv_check_atapi_dma(struct ata_queued_cmd * qc)1818da14265eSMark Lord static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
1819da14265eSMark Lord {
1820da14265eSMark Lord struct scsi_cmnd *scmd = qc->scsicmd;
1821da14265eSMark Lord
1822da14265eSMark Lord if (scmd) {
1823da14265eSMark Lord switch (scmd->cmnd[0]) {
1824da14265eSMark Lord case READ_6:
1825da14265eSMark Lord case READ_10:
1826da14265eSMark Lord case READ_12:
1827da14265eSMark Lord case WRITE_6:
1828da14265eSMark Lord case WRITE_10:
1829da14265eSMark Lord case WRITE_12:
1830da14265eSMark Lord case GPCMD_READ_CD:
1831da14265eSMark Lord case GPCMD_SEND_DVD_STRUCTURE:
1832da14265eSMark Lord case GPCMD_SEND_CUE_SHEET:
1833da14265eSMark Lord return 0; /* DMA is safe */
1834da14265eSMark Lord }
1835da14265eSMark Lord }
1836da14265eSMark Lord return -EOPNOTSUPP; /* use PIO instead */
1837da14265eSMark Lord }
1838da14265eSMark Lord
1839da14265eSMark Lord /**
1840da14265eSMark Lord * mv_bmdma_setup - Set up BMDMA transaction
1841da14265eSMark Lord * @qc: queued command to prepare DMA for.
1842da14265eSMark Lord *
1843da14265eSMark Lord * LOCKING:
1844da14265eSMark Lord * Inherited from caller.
1845da14265eSMark Lord */
mv_bmdma_setup(struct ata_queued_cmd * qc)1846da14265eSMark Lord static void mv_bmdma_setup(struct ata_queued_cmd *qc)
1847da14265eSMark Lord {
1848da14265eSMark Lord struct ata_port *ap = qc->ap;
1849da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
1850da14265eSMark Lord struct mv_port_priv *pp = ap->private_data;
1851da14265eSMark Lord
1852da14265eSMark Lord mv_fill_sg(qc);
1853da14265eSMark Lord
1854da14265eSMark Lord /* clear all DMA cmd bits */
1855cae5a29dSMark Lord writel(0, port_mmio + BMDMA_CMD);
1856da14265eSMark Lord
1857da14265eSMark Lord /* load PRD table addr. */
18584e5b6260SJens Axboe writel((pp->sg_tbl_dma[qc->hw_tag] >> 16) >> 16,
1859cae5a29dSMark Lord port_mmio + BMDMA_PRD_HIGH);
18604e5b6260SJens Axboe writelfl(pp->sg_tbl_dma[qc->hw_tag],
1861cae5a29dSMark Lord port_mmio + BMDMA_PRD_LOW);
1862da14265eSMark Lord
1863da14265eSMark Lord /* issue r/w command */
1864da14265eSMark Lord ap->ops->sff_exec_command(ap, &qc->tf);
1865da14265eSMark Lord }
1866da14265eSMark Lord
1867da14265eSMark Lord /**
1868da14265eSMark Lord * mv_bmdma_start - Start a BMDMA transaction
1869da14265eSMark Lord * @qc: queued command to start DMA on.
1870da14265eSMark Lord *
1871da14265eSMark Lord * LOCKING:
1872da14265eSMark Lord * Inherited from caller.
1873da14265eSMark Lord */
mv_bmdma_start(struct ata_queued_cmd * qc)1874da14265eSMark Lord static void mv_bmdma_start(struct ata_queued_cmd *qc)
1875da14265eSMark Lord {
1876da14265eSMark Lord struct ata_port *ap = qc->ap;
1877da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
1878da14265eSMark Lord unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
1879da14265eSMark Lord u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
1880da14265eSMark Lord
1881da14265eSMark Lord /* start host DMA transaction */
1882cae5a29dSMark Lord writelfl(cmd, port_mmio + BMDMA_CMD);
1883da14265eSMark Lord }
1884da14265eSMark Lord
1885da14265eSMark Lord /**
1886c172b359SLee Jones * mv_bmdma_stop_ap - Stop BMDMA transfer
1887f3a23c2cSLee Jones * @ap: port to stop
1888da14265eSMark Lord *
1889da14265eSMark Lord * Clears the ATA_DMA_START flag in the bmdma control register
1890da14265eSMark Lord *
1891da14265eSMark Lord * LOCKING:
1892da14265eSMark Lord * Inherited from caller.
1893da14265eSMark Lord */
mv_bmdma_stop_ap(struct ata_port * ap)189444b73380SMark Lord static void mv_bmdma_stop_ap(struct ata_port *ap)
1895da14265eSMark Lord {
1896da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
1897da14265eSMark Lord u32 cmd;
1898da14265eSMark Lord
1899da14265eSMark Lord /* clear start/stop bit */
1900cae5a29dSMark Lord cmd = readl(port_mmio + BMDMA_CMD);
190144b73380SMark Lord if (cmd & ATA_DMA_START) {
1902da14265eSMark Lord cmd &= ~ATA_DMA_START;
1903cae5a29dSMark Lord writelfl(cmd, port_mmio + BMDMA_CMD);
1904da14265eSMark Lord
1905da14265eSMark Lord /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
1906da14265eSMark Lord ata_sff_dma_pause(ap);
1907da14265eSMark Lord }
190844b73380SMark Lord }
190944b73380SMark Lord
mv_bmdma_stop(struct ata_queued_cmd * qc)191044b73380SMark Lord static void mv_bmdma_stop(struct ata_queued_cmd *qc)
191144b73380SMark Lord {
191244b73380SMark Lord mv_bmdma_stop_ap(qc->ap);
191344b73380SMark Lord }
1914da14265eSMark Lord
1915da14265eSMark Lord /**
1916da14265eSMark Lord * mv_bmdma_status - Read BMDMA status
1917da14265eSMark Lord * @ap: port for which to retrieve DMA status.
1918da14265eSMark Lord *
1919da14265eSMark Lord * Read and return equivalent of the sff BMDMA status register.
1920da14265eSMark Lord *
1921da14265eSMark Lord * LOCKING:
1922da14265eSMark Lord * Inherited from caller.
1923da14265eSMark Lord */
mv_bmdma_status(struct ata_port * ap)1924da14265eSMark Lord static u8 mv_bmdma_status(struct ata_port *ap)
1925da14265eSMark Lord {
1926da14265eSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
1927da14265eSMark Lord u32 reg, status;
1928da14265eSMark Lord
1929da14265eSMark Lord /*
1930da14265eSMark Lord * Other bits are valid only if ATA_DMA_ACTIVE==0,
1931da14265eSMark Lord * and the ATA_DMA_INTR bit doesn't exist.
1932da14265eSMark Lord */
1933cae5a29dSMark Lord reg = readl(port_mmio + BMDMA_STATUS);
1934da14265eSMark Lord if (reg & ATA_DMA_ACTIVE)
1935da14265eSMark Lord status = ATA_DMA_ACTIVE;
193644b73380SMark Lord else if (reg & ATA_DMA_ERR)
1937da14265eSMark Lord status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
193844b73380SMark Lord else {
193944b73380SMark Lord /*
194044b73380SMark Lord * Just because DMA_ACTIVE is 0 (DMA completed),
194144b73380SMark Lord * this does _not_ mean the device is "done".
194244b73380SMark Lord * So we should not yet be signalling ATA_DMA_INTR
194344b73380SMark Lord * in some cases. Eg. DSM/TRIM, and perhaps others.
194444b73380SMark Lord */
194544b73380SMark Lord mv_bmdma_stop_ap(ap);
194644b73380SMark Lord if (ioread8(ap->ioaddr.altstatus_addr) & ATA_BUSY)
194744b73380SMark Lord status = 0;
194844b73380SMark Lord else
194944b73380SMark Lord status = ATA_DMA_INTR;
195044b73380SMark Lord }
1951da14265eSMark Lord return status;
1952da14265eSMark Lord }
1953da14265eSMark Lord
mv_rw_multi_errata_sata24(struct ata_queued_cmd * qc)1954299b3f8dSMark Lord static void mv_rw_multi_errata_sata24(struct ata_queued_cmd *qc)
1955299b3f8dSMark Lord {
1956299b3f8dSMark Lord struct ata_taskfile *tf = &qc->tf;
1957299b3f8dSMark Lord /*
1958299b3f8dSMark Lord * Workaround for 88SX60x1 FEr SATA#24.
1959299b3f8dSMark Lord *
1960299b3f8dSMark Lord * Chip may corrupt WRITEs if multi_count >= 4kB.
1961299b3f8dSMark Lord * Note that READs are unaffected.
1962299b3f8dSMark Lord *
1963299b3f8dSMark Lord * It's not clear if this errata really means "4K bytes",
1964299b3f8dSMark Lord * or if it always happens for multi_count > 7
1965299b3f8dSMark Lord * regardless of device sector_size.
1966299b3f8dSMark Lord *
1967299b3f8dSMark Lord * So, for safety, any write with multi_count > 7
1968299b3f8dSMark Lord * gets converted here into a regular PIO write instead:
1969299b3f8dSMark Lord */
1970299b3f8dSMark Lord if ((tf->flags & ATA_TFLAG_WRITE) && is_multi_taskfile(tf)) {
1971299b3f8dSMark Lord if (qc->dev->multi_count > 7) {
1972299b3f8dSMark Lord switch (tf->command) {
1973299b3f8dSMark Lord case ATA_CMD_WRITE_MULTI:
1974299b3f8dSMark Lord tf->command = ATA_CMD_PIO_WRITE;
1975299b3f8dSMark Lord break;
1976299b3f8dSMark Lord case ATA_CMD_WRITE_MULTI_FUA_EXT:
1977299b3f8dSMark Lord tf->flags &= ~ATA_TFLAG_FUA; /* ugh */
1978df561f66SGustavo A. R. Silva fallthrough;
1979299b3f8dSMark Lord case ATA_CMD_WRITE_MULTI_EXT:
1980299b3f8dSMark Lord tf->command = ATA_CMD_PIO_WRITE_EXT;
1981299b3f8dSMark Lord break;
1982299b3f8dSMark Lord }
1983299b3f8dSMark Lord }
1984299b3f8dSMark Lord }
1985299b3f8dSMark Lord }
1986299b3f8dSMark Lord
1987da14265eSMark Lord /**
1988c6fd2807SJeff Garzik * mv_qc_prep - Host specific command preparation.
1989c6fd2807SJeff Garzik * @qc: queued command to prepare
1990c6fd2807SJeff Garzik *
1991c6fd2807SJeff Garzik * This routine simply redirects to the general purpose routine
1992c6fd2807SJeff Garzik * if command is not DMA. Else, it handles prep of the CRQB
1993c6fd2807SJeff Garzik * (command request block), does some sanity checking, and calls
1994c6fd2807SJeff Garzik * the SG load routine.
1995c6fd2807SJeff Garzik *
1996c6fd2807SJeff Garzik * LOCKING:
1997c6fd2807SJeff Garzik * Inherited from caller.
1998c6fd2807SJeff Garzik */
mv_qc_prep(struct ata_queued_cmd * qc)199995364f36SJiri Slaby static enum ata_completion_errors mv_qc_prep(struct ata_queued_cmd *qc)
2000c6fd2807SJeff Garzik {
2001c6fd2807SJeff Garzik struct ata_port *ap = qc->ap;
2002c6fd2807SJeff Garzik struct mv_port_priv *pp = ap->private_data;
2003c6fd2807SJeff Garzik __le16 *cw;
20048d2b450dSMark Lord struct ata_taskfile *tf = &qc->tf;
2005c6fd2807SJeff Garzik u16 flags = 0;
2006c6fd2807SJeff Garzik unsigned in_index;
2007c6fd2807SJeff Garzik
2008299b3f8dSMark Lord switch (tf->protocol) {
2009299b3f8dSMark Lord case ATA_PROT_DMA:
201044b73380SMark Lord if (tf->command == ATA_CMD_DSM)
201195364f36SJiri Slaby return AC_ERR_OK;
2012df561f66SGustavo A. R. Silva fallthrough;
2013299b3f8dSMark Lord case ATA_PROT_NCQ:
2014299b3f8dSMark Lord break; /* continue below */
2015299b3f8dSMark Lord case ATA_PROT_PIO:
2016299b3f8dSMark Lord mv_rw_multi_errata_sata24(qc);
201795364f36SJiri Slaby return AC_ERR_OK;
2018299b3f8dSMark Lord default:
201995364f36SJiri Slaby return AC_ERR_OK;
2020299b3f8dSMark Lord }
2021c6fd2807SJeff Garzik
2022c6fd2807SJeff Garzik /* Fill in command request block
2023c6fd2807SJeff Garzik */
20248d2b450dSMark Lord if (!(tf->flags & ATA_TFLAG_WRITE))
2025c6fd2807SJeff Garzik flags |= CRQB_FLAG_READ;
20264e5b6260SJens Axboe WARN_ON(MV_MAX_Q_DEPTH <= qc->hw_tag);
20274e5b6260SJens Axboe flags |= qc->hw_tag << CRQB_TAG_SHIFT;
2028e49856d8SMark Lord flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2029c6fd2807SJeff Garzik
2030bdd4dddeSJeff Garzik /* get current queue index from software */
2031fcfb1f77SMark Lord in_index = pp->req_idx;
2032c6fd2807SJeff Garzik
2033c6fd2807SJeff Garzik pp->crqb[in_index].sg_addr =
20344e5b6260SJens Axboe cpu_to_le32(pp->sg_tbl_dma[qc->hw_tag] & 0xffffffff);
2035c6fd2807SJeff Garzik pp->crqb[in_index].sg_addr_hi =
20364e5b6260SJens Axboe cpu_to_le32((pp->sg_tbl_dma[qc->hw_tag] >> 16) >> 16);
2037c6fd2807SJeff Garzik pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
2038c6fd2807SJeff Garzik
2039c6fd2807SJeff Garzik cw = &pp->crqb[in_index].ata_cmd[0];
2040c6fd2807SJeff Garzik
204125985edcSLucas De Marchi /* Sadly, the CRQB cannot accommodate all registers--there are
2042c6fd2807SJeff Garzik * only 11 bytes...so we must pick and choose required
2043c6fd2807SJeff Garzik * registers based on the command. So, we drop feature and
2044c6fd2807SJeff Garzik * hob_feature for [RW] DMA commands, but they are needed for
2045cd12e1f7SMark Lord * NCQ. NCQ will drop hob_nsect, which is not needed there
2046cd12e1f7SMark Lord * (nsect is used only for the tag; feat/hob_feat hold true nsect).
2047c6fd2807SJeff Garzik */
2048c6fd2807SJeff Garzik switch (tf->command) {
2049c6fd2807SJeff Garzik case ATA_CMD_READ:
2050c6fd2807SJeff Garzik case ATA_CMD_READ_EXT:
2051c6fd2807SJeff Garzik case ATA_CMD_WRITE:
2052c6fd2807SJeff Garzik case ATA_CMD_WRITE_EXT:
2053c6fd2807SJeff Garzik case ATA_CMD_WRITE_FUA_EXT:
2054c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
2055c6fd2807SJeff Garzik break;
2056c6fd2807SJeff Garzik case ATA_CMD_FPDMA_READ:
2057c6fd2807SJeff Garzik case ATA_CMD_FPDMA_WRITE:
2058c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
2059c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
2060c6fd2807SJeff Garzik break;
2061c6fd2807SJeff Garzik default:
2062c6fd2807SJeff Garzik /* The only other commands EDMA supports in non-queued and
2063c6fd2807SJeff Garzik * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
2064c6fd2807SJeff Garzik * of which are defined/used by Linux. If we get here, this
2065c6fd2807SJeff Garzik * driver needs work.
2066c6fd2807SJeff Garzik */
2067e9f691d8SJiri Slaby ata_port_err(ap, "%s: unsupported command: %.2x\n", __func__,
2068e9f691d8SJiri Slaby tf->command);
2069e9f691d8SJiri Slaby return AC_ERR_INVALID;
2070c6fd2807SJeff Garzik }
2071c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
2072c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
2073c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
2074c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
2075c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
2076c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
2077c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
2078c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
2079c6fd2807SJeff Garzik mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
2080c6fd2807SJeff Garzik
2081c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_DMAMAP))
208295364f36SJiri Slaby return AC_ERR_OK;
2083c6fd2807SJeff Garzik mv_fill_sg(qc);
208495364f36SJiri Slaby
208595364f36SJiri Slaby return AC_ERR_OK;
2086c6fd2807SJeff Garzik }
2087c6fd2807SJeff Garzik
2088c6fd2807SJeff Garzik /**
2089c6fd2807SJeff Garzik * mv_qc_prep_iie - Host specific command preparation.
2090c6fd2807SJeff Garzik * @qc: queued command to prepare
2091c6fd2807SJeff Garzik *
2092c6fd2807SJeff Garzik * This routine simply redirects to the general purpose routine
2093c6fd2807SJeff Garzik * if command is not DMA. Else, it handles prep of the CRQB
2094c6fd2807SJeff Garzik * (command request block), does some sanity checking, and calls
2095c6fd2807SJeff Garzik * the SG load routine.
2096c6fd2807SJeff Garzik *
2097c6fd2807SJeff Garzik * LOCKING:
2098c6fd2807SJeff Garzik * Inherited from caller.
2099c6fd2807SJeff Garzik */
mv_qc_prep_iie(struct ata_queued_cmd * qc)210095364f36SJiri Slaby static enum ata_completion_errors mv_qc_prep_iie(struct ata_queued_cmd *qc)
2101c6fd2807SJeff Garzik {
2102c6fd2807SJeff Garzik struct ata_port *ap = qc->ap;
2103c6fd2807SJeff Garzik struct mv_port_priv *pp = ap->private_data;
2104c6fd2807SJeff Garzik struct mv_crqb_iie *crqb;
21058d2b450dSMark Lord struct ata_taskfile *tf = &qc->tf;
2106c6fd2807SJeff Garzik unsigned in_index;
2107c6fd2807SJeff Garzik u32 flags = 0;
2108c6fd2807SJeff Garzik
21098d2b450dSMark Lord if ((tf->protocol != ATA_PROT_DMA) &&
21108d2b450dSMark Lord (tf->protocol != ATA_PROT_NCQ))
211195364f36SJiri Slaby return AC_ERR_OK;
211244b73380SMark Lord if (tf->command == ATA_CMD_DSM)
211395364f36SJiri Slaby return AC_ERR_OK; /* use bmdma for this */
2114c6fd2807SJeff Garzik
2115e12bef50SMark Lord /* Fill in Gen IIE command request block */
21168d2b450dSMark Lord if (!(tf->flags & ATA_TFLAG_WRITE))
2117c6fd2807SJeff Garzik flags |= CRQB_FLAG_READ;
2118c6fd2807SJeff Garzik
21194e5b6260SJens Axboe WARN_ON(MV_MAX_Q_DEPTH <= qc->hw_tag);
21204e5b6260SJens Axboe flags |= qc->hw_tag << CRQB_TAG_SHIFT;
21214e5b6260SJens Axboe flags |= qc->hw_tag << CRQB_HOSTQ_SHIFT;
2122e49856d8SMark Lord flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
2123c6fd2807SJeff Garzik
2124bdd4dddeSJeff Garzik /* get current queue index from software */
2125fcfb1f77SMark Lord in_index = pp->req_idx;
2126c6fd2807SJeff Garzik
2127c6fd2807SJeff Garzik crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
21284e5b6260SJens Axboe crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->hw_tag] & 0xffffffff);
21294e5b6260SJens Axboe crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->hw_tag] >> 16) >> 16);
2130c6fd2807SJeff Garzik crqb->flags = cpu_to_le32(flags);
2131c6fd2807SJeff Garzik
2132c6fd2807SJeff Garzik crqb->ata_cmd[0] = cpu_to_le32(
2133c6fd2807SJeff Garzik (tf->command << 16) |
2134c6fd2807SJeff Garzik (tf->feature << 24)
2135c6fd2807SJeff Garzik );
2136c6fd2807SJeff Garzik crqb->ata_cmd[1] = cpu_to_le32(
2137c6fd2807SJeff Garzik (tf->lbal << 0) |
2138c6fd2807SJeff Garzik (tf->lbam << 8) |
2139c6fd2807SJeff Garzik (tf->lbah << 16) |
2140c6fd2807SJeff Garzik (tf->device << 24)
2141c6fd2807SJeff Garzik );
2142c6fd2807SJeff Garzik crqb->ata_cmd[2] = cpu_to_le32(
2143c6fd2807SJeff Garzik (tf->hob_lbal << 0) |
2144c6fd2807SJeff Garzik (tf->hob_lbam << 8) |
2145c6fd2807SJeff Garzik (tf->hob_lbah << 16) |
2146c6fd2807SJeff Garzik (tf->hob_feature << 24)
2147c6fd2807SJeff Garzik );
2148c6fd2807SJeff Garzik crqb->ata_cmd[3] = cpu_to_le32(
2149c6fd2807SJeff Garzik (tf->nsect << 0) |
2150c6fd2807SJeff Garzik (tf->hob_nsect << 8)
2151c6fd2807SJeff Garzik );
2152c6fd2807SJeff Garzik
2153c6fd2807SJeff Garzik if (!(qc->flags & ATA_QCFLAG_DMAMAP))
215495364f36SJiri Slaby return AC_ERR_OK;
2155c6fd2807SJeff Garzik mv_fill_sg(qc);
215695364f36SJiri Slaby
215795364f36SJiri Slaby return AC_ERR_OK;
2158c6fd2807SJeff Garzik }
2159c6fd2807SJeff Garzik
2160c6fd2807SJeff Garzik /**
2161d16ab3f6SMark Lord * mv_sff_check_status - fetch device status, if valid
2162d16ab3f6SMark Lord * @ap: ATA port to fetch status from
2163d16ab3f6SMark Lord *
2164d16ab3f6SMark Lord * When using command issue via mv_qc_issue_fis(),
2165d16ab3f6SMark Lord * the initial ATA_BUSY state does not show up in the
2166d16ab3f6SMark Lord * ATA status (shadow) register. This can confuse libata!
2167d16ab3f6SMark Lord *
2168d16ab3f6SMark Lord * So we have a hook here to fake ATA_BUSY for that situation,
2169d16ab3f6SMark Lord * until the first time a BUSY, DRQ, or ERR bit is seen.
2170d16ab3f6SMark Lord *
2171d16ab3f6SMark Lord * The rest of the time, it simply returns the ATA status register.
2172d16ab3f6SMark Lord */
mv_sff_check_status(struct ata_port * ap)2173d16ab3f6SMark Lord static u8 mv_sff_check_status(struct ata_port *ap)
2174d16ab3f6SMark Lord {
2175d16ab3f6SMark Lord u8 stat = ioread8(ap->ioaddr.status_addr);
2176d16ab3f6SMark Lord struct mv_port_priv *pp = ap->private_data;
2177d16ab3f6SMark Lord
2178d16ab3f6SMark Lord if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
2179d16ab3f6SMark Lord if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
2180d16ab3f6SMark Lord pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
2181d16ab3f6SMark Lord else
2182d16ab3f6SMark Lord stat = ATA_BUSY;
2183d16ab3f6SMark Lord }
2184d16ab3f6SMark Lord return stat;
2185d16ab3f6SMark Lord }
2186d16ab3f6SMark Lord
2187d16ab3f6SMark Lord /**
218870f8b79cSMark Lord * mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
2189f3a23c2cSLee Jones * @ap: ATA port to send a FIS
219070f8b79cSMark Lord * @fis: fis to be sent
219170f8b79cSMark Lord * @nwords: number of 32-bit words in the fis
219270f8b79cSMark Lord */
mv_send_fis(struct ata_port * ap,u32 * fis,int nwords)219370f8b79cSMark Lord static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
219470f8b79cSMark Lord {
219570f8b79cSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
219670f8b79cSMark Lord u32 ifctl, old_ifctl, ifstat;
219770f8b79cSMark Lord int i, timeout = 200, final_word = nwords - 1;
219870f8b79cSMark Lord
219970f8b79cSMark Lord /* Initiate FIS transmission mode */
2200cae5a29dSMark Lord old_ifctl = readl(port_mmio + SATA_IFCTL);
220170f8b79cSMark Lord ifctl = 0x100 | (old_ifctl & 0xf);
2202cae5a29dSMark Lord writelfl(ifctl, port_mmio + SATA_IFCTL);
220370f8b79cSMark Lord
220470f8b79cSMark Lord /* Send all words of the FIS except for the final word */
220570f8b79cSMark Lord for (i = 0; i < final_word; ++i)
2206cae5a29dSMark Lord writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS);
220770f8b79cSMark Lord
220870f8b79cSMark Lord /* Flag end-of-transmission, and then send the final word */
2209cae5a29dSMark Lord writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL);
2210cae5a29dSMark Lord writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS);
221170f8b79cSMark Lord
221270f8b79cSMark Lord /*
221370f8b79cSMark Lord * Wait for FIS transmission to complete.
221470f8b79cSMark Lord * This typically takes just a single iteration.
221570f8b79cSMark Lord */
221670f8b79cSMark Lord do {
2217cae5a29dSMark Lord ifstat = readl(port_mmio + SATA_IFSTAT);
221870f8b79cSMark Lord } while (!(ifstat & 0x1000) && --timeout);
221970f8b79cSMark Lord
222070f8b79cSMark Lord /* Restore original port configuration */
2221cae5a29dSMark Lord writelfl(old_ifctl, port_mmio + SATA_IFCTL);
222270f8b79cSMark Lord
222370f8b79cSMark Lord /* See if it worked */
222470f8b79cSMark Lord if ((ifstat & 0x3000) != 0x1000) {
2225a9a79dfeSJoe Perches ata_port_warn(ap, "%s transmission error, ifstat=%08x\n",
222670f8b79cSMark Lord __func__, ifstat);
222770f8b79cSMark Lord return AC_ERR_OTHER;
222870f8b79cSMark Lord }
222970f8b79cSMark Lord return 0;
223070f8b79cSMark Lord }
223170f8b79cSMark Lord
223270f8b79cSMark Lord /**
223370f8b79cSMark Lord * mv_qc_issue_fis - Issue a command directly as a FIS
223470f8b79cSMark Lord * @qc: queued command to start
223570f8b79cSMark Lord *
223670f8b79cSMark Lord * Note that the ATA shadow registers are not updated
223770f8b79cSMark Lord * after command issue, so the device will appear "READY"
223870f8b79cSMark Lord * if polled, even while it is BUSY processing the command.
223970f8b79cSMark Lord *
224070f8b79cSMark Lord * So we use a status hook to fake ATA_BUSY until the drive changes state.
224170f8b79cSMark Lord *
224270f8b79cSMark Lord * Note: we don't get updated shadow regs on *completion*
224370f8b79cSMark Lord * of non-data commands. So avoid sending them via this function,
224470f8b79cSMark Lord * as they will appear to have completed immediately.
224570f8b79cSMark Lord *
224670f8b79cSMark Lord * GEN_IIE has special registers that we could get the result tf from,
224770f8b79cSMark Lord * but earlier chipsets do not. For now, we ignore those registers.
224870f8b79cSMark Lord */
mv_qc_issue_fis(struct ata_queued_cmd * qc)224970f8b79cSMark Lord static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
225070f8b79cSMark Lord {
225170f8b79cSMark Lord struct ata_port *ap = qc->ap;
225270f8b79cSMark Lord struct mv_port_priv *pp = ap->private_data;
225370f8b79cSMark Lord struct ata_link *link = qc->dev->link;
225470f8b79cSMark Lord u32 fis[5];
225570f8b79cSMark Lord int err = 0;
225670f8b79cSMark Lord
225770f8b79cSMark Lord ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
22584c4a90fdSThiago Farina err = mv_send_fis(ap, fis, ARRAY_SIZE(fis));
225970f8b79cSMark Lord if (err)
226070f8b79cSMark Lord return err;
226170f8b79cSMark Lord
226270f8b79cSMark Lord switch (qc->tf.protocol) {
226370f8b79cSMark Lord case ATAPI_PROT_PIO:
226470f8b79cSMark Lord pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
2265df561f66SGustavo A. R. Silva fallthrough;
226670f8b79cSMark Lord case ATAPI_PROT_NODATA:
226770f8b79cSMark Lord ap->hsm_task_state = HSM_ST_FIRST;
226870f8b79cSMark Lord break;
226970f8b79cSMark Lord case ATA_PROT_PIO:
227070f8b79cSMark Lord pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
227170f8b79cSMark Lord if (qc->tf.flags & ATA_TFLAG_WRITE)
227270f8b79cSMark Lord ap->hsm_task_state = HSM_ST_FIRST;
227370f8b79cSMark Lord else
227470f8b79cSMark Lord ap->hsm_task_state = HSM_ST;
227570f8b79cSMark Lord break;
227670f8b79cSMark Lord default:
227770f8b79cSMark Lord ap->hsm_task_state = HSM_ST_LAST;
227870f8b79cSMark Lord break;
227970f8b79cSMark Lord }
228070f8b79cSMark Lord
228170f8b79cSMark Lord if (qc->tf.flags & ATA_TFLAG_POLLING)
2282ea3c6450SGwendal Grignou ata_sff_queue_pio_task(link, 0);
228370f8b79cSMark Lord return 0;
228470f8b79cSMark Lord }
228570f8b79cSMark Lord
228670f8b79cSMark Lord /**
2287c6fd2807SJeff Garzik * mv_qc_issue - Initiate a command to the host
2288c6fd2807SJeff Garzik * @qc: queued command to start
2289c6fd2807SJeff Garzik *
2290c6fd2807SJeff Garzik * This routine simply redirects to the general purpose routine
2291c6fd2807SJeff Garzik * if command is not DMA. Else, it sanity checks our local
2292c6fd2807SJeff Garzik * caches of the request producer/consumer indices then enables
2293c6fd2807SJeff Garzik * DMA and bumps the request producer index.
2294c6fd2807SJeff Garzik *
2295c6fd2807SJeff Garzik * LOCKING:
2296c6fd2807SJeff Garzik * Inherited from caller.
2297c6fd2807SJeff Garzik */
mv_qc_issue(struct ata_queued_cmd * qc)2298c6fd2807SJeff Garzik static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
2299c6fd2807SJeff Garzik {
2300f48765ccSMark Lord static int limit_warnings = 10;
2301c5d3e45aSJeff Garzik struct ata_port *ap = qc->ap;
2302c5d3e45aSJeff Garzik void __iomem *port_mmio = mv_ap_base(ap);
2303c5d3e45aSJeff Garzik struct mv_port_priv *pp = ap->private_data;
2304bdd4dddeSJeff Garzik u32 in_index;
230542ed893dSMark Lord unsigned int port_irqs;
2306c6fd2807SJeff Garzik
2307d16ab3f6SMark Lord pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
2308d16ab3f6SMark Lord
2309f48765ccSMark Lord switch (qc->tf.protocol) {
2310f48765ccSMark Lord case ATA_PROT_DMA:
231144b73380SMark Lord if (qc->tf.command == ATA_CMD_DSM) {
231244b73380SMark Lord if (!ap->ops->bmdma_setup) /* no bmdma on GEN_I */
231344b73380SMark Lord return AC_ERR_OTHER;
231444b73380SMark Lord break; /* use bmdma for this */
231544b73380SMark Lord }
2316df561f66SGustavo A. R. Silva fallthrough;
2317f48765ccSMark Lord case ATA_PROT_NCQ:
2318f48765ccSMark Lord mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
2319f48765ccSMark Lord pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2320f48765ccSMark Lord in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
2321f48765ccSMark Lord
2322f48765ccSMark Lord /* Write the request in pointer to kick the EDMA to life */
2323f48765ccSMark Lord writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
2324cae5a29dSMark Lord port_mmio + EDMA_REQ_Q_IN_PTR);
2325f48765ccSMark Lord return 0;
2326f48765ccSMark Lord
2327f48765ccSMark Lord case ATA_PROT_PIO:
2328c6112bd8SMark Lord /*
2329c6112bd8SMark Lord * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
2330c6112bd8SMark Lord *
2331c6112bd8SMark Lord * Someday, we might implement special polling workarounds
2332c6112bd8SMark Lord * for these, but it all seems rather unnecessary since we
2333c6112bd8SMark Lord * normally use only DMA for commands which transfer more
2334c6112bd8SMark Lord * than a single block of data.
2335c6112bd8SMark Lord *
2336c6112bd8SMark Lord * Much of the time, this could just work regardless.
2337c6112bd8SMark Lord * So for now, just log the incident, and allow the attempt.
2338c6112bd8SMark Lord */
2339c7843e8fSMark Lord if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
2340c6112bd8SMark Lord --limit_warnings;
2341a9a79dfeSJoe Perches ata_link_warn(qc->dev->link, DRV_NAME
2342c6112bd8SMark Lord ": attempting PIO w/multiple DRQ: "
2343c6112bd8SMark Lord "this may fail due to h/w errata\n");
2344c6112bd8SMark Lord }
2345df561f66SGustavo A. R. Silva fallthrough;
234642ed893dSMark Lord case ATA_PROT_NODATA:
2347f48765ccSMark Lord case ATAPI_PROT_PIO:
234842ed893dSMark Lord case ATAPI_PROT_NODATA:
234942ed893dSMark Lord if (ap->flags & ATA_FLAG_PIO_POLLING)
235042ed893dSMark Lord qc->tf.flags |= ATA_TFLAG_POLLING;
235142ed893dSMark Lord break;
235242ed893dSMark Lord }
235342ed893dSMark Lord
235442ed893dSMark Lord if (qc->tf.flags & ATA_TFLAG_POLLING)
235542ed893dSMark Lord port_irqs = ERR_IRQ; /* mask device interrupt when polling */
235642ed893dSMark Lord else
235742ed893dSMark Lord port_irqs = ERR_IRQ | DONE_IRQ; /* unmask all interrupts */
235842ed893dSMark Lord
235917c5aab5SMark Lord /*
236017c5aab5SMark Lord * We're about to send a non-EDMA capable command to the
2361c6fd2807SJeff Garzik * port. Turn off EDMA so there won't be problems accessing
2362c6fd2807SJeff Garzik * shadow block, etc registers.
2363c6fd2807SJeff Garzik */
2364b562468cSMark Lord mv_stop_edma(ap);
2365f48765ccSMark Lord mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
2366e49856d8SMark Lord mv_pmp_select(ap, qc->dev->link->pmp);
236770f8b79cSMark Lord
236870f8b79cSMark Lord if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
236970f8b79cSMark Lord struct mv_host_priv *hpriv = ap->host->private_data;
237070f8b79cSMark Lord /*
237170f8b79cSMark Lord * Workaround for 88SX60x1 FEr SATA#25 (part 2).
237270f8b79cSMark Lord *
237370f8b79cSMark Lord * After any NCQ error, the READ_LOG_EXT command
237470f8b79cSMark Lord * from libata-eh *must* use mv_qc_issue_fis().
237570f8b79cSMark Lord * Otherwise it might fail, due to chip errata.
237670f8b79cSMark Lord *
237770f8b79cSMark Lord * Rather than special-case it, we'll just *always*
237870f8b79cSMark Lord * use this method here for READ_LOG_EXT, making for
237970f8b79cSMark Lord * easier testing.
238070f8b79cSMark Lord */
238170f8b79cSMark Lord if (IS_GEN_II(hpriv))
238270f8b79cSMark Lord return mv_qc_issue_fis(qc);
238370f8b79cSMark Lord }
2384360ff783STejun Heo return ata_bmdma_qc_issue(qc);
2385c6fd2807SJeff Garzik }
2386c6fd2807SJeff Garzik
mv_get_active_qc(struct ata_port * ap)23878f767f8aSMark Lord static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
23888f767f8aSMark Lord {
23898f767f8aSMark Lord struct mv_port_priv *pp = ap->private_data;
23908f767f8aSMark Lord struct ata_queued_cmd *qc;
23918f767f8aSMark Lord
23928f767f8aSMark Lord if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
23938f767f8aSMark Lord return NULL;
23948f767f8aSMark Lord qc = ata_qc_from_tag(ap, ap->link.active_tag);
23953e4ec344STejun Heo if (qc && !(qc->tf.flags & ATA_TFLAG_POLLING))
23968f767f8aSMark Lord return qc;
23973e4ec344STejun Heo return NULL;
23988f767f8aSMark Lord }
23998f767f8aSMark Lord
mv_pmp_error_handler(struct ata_port * ap)240029d187bbSMark Lord static void mv_pmp_error_handler(struct ata_port *ap)
240129d187bbSMark Lord {
240229d187bbSMark Lord unsigned int pmp, pmp_map;
240329d187bbSMark Lord struct mv_port_priv *pp = ap->private_data;
240429d187bbSMark Lord
240529d187bbSMark Lord if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
240629d187bbSMark Lord /*
240729d187bbSMark Lord * Perform NCQ error analysis on failed PMPs
240829d187bbSMark Lord * before we freeze the port entirely.
240929d187bbSMark Lord *
241029d187bbSMark Lord * The failed PMPs are marked earlier by mv_pmp_eh_prep().
241129d187bbSMark Lord */
241229d187bbSMark Lord pmp_map = pp->delayed_eh_pmp_map;
241329d187bbSMark Lord pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
241429d187bbSMark Lord for (pmp = 0; pmp_map != 0; pmp++) {
241529d187bbSMark Lord unsigned int this_pmp = (1 << pmp);
241629d187bbSMark Lord if (pmp_map & this_pmp) {
241729d187bbSMark Lord struct ata_link *link = &ap->pmp_link[pmp];
241829d187bbSMark Lord pmp_map &= ~this_pmp;
241929d187bbSMark Lord ata_eh_analyze_ncq_error(link);
242029d187bbSMark Lord }
242129d187bbSMark Lord }
242229d187bbSMark Lord ata_port_freeze(ap);
242329d187bbSMark Lord }
242429d187bbSMark Lord sata_pmp_error_handler(ap);
242529d187bbSMark Lord }
242629d187bbSMark Lord
mv_get_err_pmp_map(struct ata_port * ap)24274c299ca3SMark Lord static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
24284c299ca3SMark Lord {
24294c299ca3SMark Lord void __iomem *port_mmio = mv_ap_base(ap);
24304c299ca3SMark Lord
2431cae5a29dSMark Lord return readl(port_mmio + SATA_TESTCTL) >> 16;
24324c299ca3SMark Lord }
24334c299ca3SMark Lord
mv_pmp_eh_prep(struct ata_port * ap,unsigned int pmp_map)24344c299ca3SMark Lord static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
24354c299ca3SMark Lord {
24364c299ca3SMark Lord unsigned int pmp;
24374c299ca3SMark Lord
24384c299ca3SMark Lord /*
24394c299ca3SMark Lord * Initialize EH info for PMPs which saw device errors
24404c299ca3SMark Lord */
24414c299ca3SMark Lord for (pmp = 0; pmp_map != 0; pmp++) {
24424c299ca3SMark Lord unsigned int this_pmp = (1 << pmp);
24434c299ca3SMark Lord if (pmp_map & this_pmp) {
24444c299ca3SMark Lord struct ata_link *link = &ap->pmp_link[pmp];
244514d7045cSColin Ian King struct ata_eh_info *ehi = &link->eh_info;
24464c299ca3SMark Lord
24474c299ca3SMark Lord pmp_map &= ~this_pmp;
24484c299ca3SMark Lord ata_ehi_clear_desc(ehi);
24494c299ca3SMark Lord ata_ehi_push_desc(ehi, "dev err");
24504c299ca3SMark Lord ehi->err_mask |= AC_ERR_DEV;
24514c299ca3SMark Lord ehi->action |= ATA_EH_RESET;
24524c299ca3SMark Lord ata_link_abort(link);
24534c299ca3SMark Lord }
24544c299ca3SMark Lord }
24554c299ca3SMark Lord }
24564c299ca3SMark Lord
mv_req_q_empty(struct ata_port * ap)245706aaca3fSMark Lord static int mv_req_q_empty(struct ata_port *ap)
245806aaca3fSMark Lord {
245906aaca3fSMark Lord void __iomem *port_mmio = mv_ap_base(ap);
246006aaca3fSMark Lord u32 in_ptr, out_ptr;
246106aaca3fSMark Lord
2462cae5a29dSMark Lord in_ptr = (readl(port_mmio + EDMA_REQ_Q_IN_PTR)
246306aaca3fSMark Lord >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2464cae5a29dSMark Lord out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR)
246506aaca3fSMark Lord >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
246606aaca3fSMark Lord return (in_ptr == out_ptr); /* 1 == queue_is_empty */
246706aaca3fSMark Lord }
246806aaca3fSMark Lord
mv_handle_fbs_ncq_dev_err(struct ata_port * ap)24694c299ca3SMark Lord static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
24704c299ca3SMark Lord {
24714c299ca3SMark Lord struct mv_port_priv *pp = ap->private_data;
24724c299ca3SMark Lord int failed_links;
24734c299ca3SMark Lord unsigned int old_map, new_map;
24744c299ca3SMark Lord
24754c299ca3SMark Lord /*
24764c299ca3SMark Lord * Device error during FBS+NCQ operation:
24774c299ca3SMark Lord *
24784c299ca3SMark Lord * Set a port flag to prevent further I/O being enqueued.
24794c299ca3SMark Lord * Leave the EDMA running to drain outstanding commands from this port.
24804c299ca3SMark Lord * Perform the post-mortem/EH only when all responses are complete.
24814c299ca3SMark Lord * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
24824c299ca3SMark Lord */
24834c299ca3SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
24844c299ca3SMark Lord pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
24854c299ca3SMark Lord pp->delayed_eh_pmp_map = 0;
24864c299ca3SMark Lord }
24874c299ca3SMark Lord old_map = pp->delayed_eh_pmp_map;
24884c299ca3SMark Lord new_map = old_map | mv_get_err_pmp_map(ap);
24894c299ca3SMark Lord
24904c299ca3SMark Lord if (old_map != new_map) {
24914c299ca3SMark Lord pp->delayed_eh_pmp_map = new_map;
24924c299ca3SMark Lord mv_pmp_eh_prep(ap, new_map & ~old_map);
24934c299ca3SMark Lord }
2494c46938ccSMark Lord failed_links = hweight16(new_map);
24954c299ca3SMark Lord
2496a9a79dfeSJoe Perches ata_port_info(ap,
2497e3ed8939SJens Axboe "%s: pmp_map=%04x qc_map=%04llx failed_links=%d nr_active_links=%d\n",
24984c299ca3SMark Lord __func__, pp->delayed_eh_pmp_map,
24994c299ca3SMark Lord ap->qc_active, failed_links,
25004c299ca3SMark Lord ap->nr_active_links);
25014c299ca3SMark Lord
250206aaca3fSMark Lord if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
25034c299ca3SMark Lord mv_process_crpb_entries(ap, pp);
25044c299ca3SMark Lord mv_stop_edma(ap);
25054c299ca3SMark Lord mv_eh_freeze(ap);
2506a9a79dfeSJoe Perches ata_port_info(ap, "%s: done\n", __func__);
25074c299ca3SMark Lord return 1; /* handled */
25084c299ca3SMark Lord }
2509a9a79dfeSJoe Perches ata_port_info(ap, "%s: waiting\n", __func__);
25104c299ca3SMark Lord return 1; /* handled */
25114c299ca3SMark Lord }
25124c299ca3SMark Lord
mv_handle_fbs_non_ncq_dev_err(struct ata_port * ap)25134c299ca3SMark Lord static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
25144c299ca3SMark Lord {
25154c299ca3SMark Lord /*
25164c299ca3SMark Lord * Possible future enhancement:
25174c299ca3SMark Lord *
25184c299ca3SMark Lord * FBS+non-NCQ operation is not yet implemented.
25194c299ca3SMark Lord * See related notes in mv_edma_cfg().
25204c299ca3SMark Lord *
25214c299ca3SMark Lord * Device error during FBS+non-NCQ operation:
25224c299ca3SMark Lord *
25234c299ca3SMark Lord * We need to snapshot the shadow registers for each failed command.
25244c299ca3SMark Lord * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
25254c299ca3SMark Lord */
25264c299ca3SMark Lord return 0; /* not handled */
25274c299ca3SMark Lord }
25284c299ca3SMark Lord
mv_handle_dev_err(struct ata_port * ap,u32 edma_err_cause)25294c299ca3SMark Lord static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
25304c299ca3SMark Lord {
25314c299ca3SMark Lord struct mv_port_priv *pp = ap->private_data;
25324c299ca3SMark Lord
25334c299ca3SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
25344c299ca3SMark Lord return 0; /* EDMA was not active: not handled */
25354c299ca3SMark Lord if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
25364c299ca3SMark Lord return 0; /* FBS was not active: not handled */
25374c299ca3SMark Lord
25384c299ca3SMark Lord if (!(edma_err_cause & EDMA_ERR_DEV))
25394c299ca3SMark Lord return 0; /* non DEV error: not handled */
25404c299ca3SMark Lord edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
25414c299ca3SMark Lord if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
25424c299ca3SMark Lord return 0; /* other problems: not handled */
25434c299ca3SMark Lord
25444c299ca3SMark Lord if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
25454c299ca3SMark Lord /*
25464c299ca3SMark Lord * EDMA should NOT have self-disabled for this case.
25474c299ca3SMark Lord * If it did, then something is wrong elsewhere,
25484c299ca3SMark Lord * and we cannot handle it here.
25494c299ca3SMark Lord */
25504c299ca3SMark Lord if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2551a9a79dfeSJoe Perches ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
25524c299ca3SMark Lord __func__, edma_err_cause, pp->pp_flags);
25534c299ca3SMark Lord return 0; /* not handled */
25544c299ca3SMark Lord }
25554c299ca3SMark Lord return mv_handle_fbs_ncq_dev_err(ap);
25564c299ca3SMark Lord } else {
25574c299ca3SMark Lord /*
25584c299ca3SMark Lord * EDMA should have self-disabled for this case.
25594c299ca3SMark Lord * If it did not, then something is wrong elsewhere,
25604c299ca3SMark Lord * and we cannot handle it here.
25614c299ca3SMark Lord */
25624c299ca3SMark Lord if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
2563a9a79dfeSJoe Perches ata_port_warn(ap, "%s: err_cause=0x%x pp_flags=0x%x\n",
25644c299ca3SMark Lord __func__, edma_err_cause, pp->pp_flags);
25654c299ca3SMark Lord return 0; /* not handled */
25664c299ca3SMark Lord }
25674c299ca3SMark Lord return mv_handle_fbs_non_ncq_dev_err(ap);
25684c299ca3SMark Lord }
25694c299ca3SMark Lord return 0; /* not handled */
25704c299ca3SMark Lord }
25714c299ca3SMark Lord
mv_unexpected_intr(struct ata_port * ap,int edma_was_enabled)2572a9010329SMark Lord static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
25738f767f8aSMark Lord {
25748f767f8aSMark Lord struct ata_eh_info *ehi = &ap->link.eh_info;
2575a9010329SMark Lord char *when = "idle";
25768f767f8aSMark Lord
25778f767f8aSMark Lord ata_ehi_clear_desc(ehi);
25783e4ec344STejun Heo if (edma_was_enabled) {
2579a9010329SMark Lord when = "EDMA enabled";
25808f767f8aSMark Lord } else {
25818f767f8aSMark Lord struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
25828f767f8aSMark Lord if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
2583a9010329SMark Lord when = "polling";
25848f767f8aSMark Lord }
2585a9010329SMark Lord ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
25868f767f8aSMark Lord ehi->err_mask |= AC_ERR_OTHER;
25878f767f8aSMark Lord ehi->action |= ATA_EH_RESET;
25888f767f8aSMark Lord ata_port_freeze(ap);
25898f767f8aSMark Lord }
25908f767f8aSMark Lord
2591c6fd2807SJeff Garzik /**
2592c6fd2807SJeff Garzik * mv_err_intr - Handle error interrupts on the port
2593c6fd2807SJeff Garzik * @ap: ATA channel to manipulate
2594c6fd2807SJeff Garzik *
25958d07379dSMark Lord * Most cases require a full reset of the chip's state machine,
25968d07379dSMark Lord * which also performs a COMRESET.
25978d07379dSMark Lord * Also, if the port disabled DMA, update our cached copy to match.
2598c6fd2807SJeff Garzik *
2599c6fd2807SJeff Garzik * LOCKING:
2600c6fd2807SJeff Garzik * Inherited from caller.
2601c6fd2807SJeff Garzik */
mv_err_intr(struct ata_port * ap)260237b9046aSMark Lord static void mv_err_intr(struct ata_port *ap)
2603c6fd2807SJeff Garzik {
2604c6fd2807SJeff Garzik void __iomem *port_mmio = mv_ap_base(ap);
2605bdd4dddeSJeff Garzik u32 edma_err_cause, eh_freeze_mask, serr = 0;
2606e4006077SMark Lord u32 fis_cause = 0;
2607bdd4dddeSJeff Garzik struct mv_port_priv *pp = ap->private_data;
2608bdd4dddeSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data;
2609bdd4dddeSJeff Garzik unsigned int action = 0, err_mask = 0;
26109af5c9c9STejun Heo struct ata_eh_info *ehi = &ap->link.eh_info;
261137b9046aSMark Lord struct ata_queued_cmd *qc;
261237b9046aSMark Lord int abort = 0;
2613c6fd2807SJeff Garzik
26148d07379dSMark Lord /*
261537b9046aSMark Lord * Read and clear the SError and err_cause bits.
2616e4006077SMark Lord * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
2617e4006077SMark Lord * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
2618bdd4dddeSJeff Garzik */
261937b9046aSMark Lord sata_scr_read(&ap->link, SCR_ERROR, &serr);
262037b9046aSMark Lord sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
262137b9046aSMark Lord
2622cae5a29dSMark Lord edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE);
2623e4006077SMark Lord if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2624cae5a29dSMark Lord fis_cause = readl(port_mmio + FIS_IRQ_CAUSE);
2625cae5a29dSMark Lord writelfl(~fis_cause, port_mmio + FIS_IRQ_CAUSE);
2626e4006077SMark Lord }
2627cae5a29dSMark Lord writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE);
2628bdd4dddeSJeff Garzik
26294c299ca3SMark Lord if (edma_err_cause & EDMA_ERR_DEV) {
26304c299ca3SMark Lord /*
26314c299ca3SMark Lord * Device errors during FIS-based switching operation
26324c299ca3SMark Lord * require special handling.
26334c299ca3SMark Lord */
26344c299ca3SMark Lord if (mv_handle_dev_err(ap, edma_err_cause))
26354c299ca3SMark Lord return;
26364c299ca3SMark Lord }
26374c299ca3SMark Lord
263837b9046aSMark Lord qc = mv_get_active_qc(ap);
263937b9046aSMark Lord ata_ehi_clear_desc(ehi);
264037b9046aSMark Lord ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
264137b9046aSMark Lord edma_err_cause, pp->pp_flags);
2642e4006077SMark Lord
2643c443c500SMark Lord if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2644e4006077SMark Lord ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
2645cae5a29dSMark Lord if (fis_cause & FIS_IRQ_CAUSE_AN) {
2646c443c500SMark Lord u32 ec = edma_err_cause &
2647c443c500SMark Lord ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
2648c443c500SMark Lord sata_async_notification(ap);
2649c443c500SMark Lord if (!ec)
2650c443c500SMark Lord return; /* Just an AN; no need for the nukes */
2651c443c500SMark Lord ata_ehi_push_desc(ehi, "SDB notify");
2652c443c500SMark Lord }
2653c443c500SMark Lord }
2654bdd4dddeSJeff Garzik /*
2655352fab70SMark Lord * All generations share these EDMA error cause bits:
2656bdd4dddeSJeff Garzik */
265737b9046aSMark Lord if (edma_err_cause & EDMA_ERR_DEV) {
2658bdd4dddeSJeff Garzik err_mask |= AC_ERR_DEV;
265937b9046aSMark Lord action |= ATA_EH_RESET;
266037b9046aSMark Lord ata_ehi_push_desc(ehi, "dev error");
266137b9046aSMark Lord }
2662bdd4dddeSJeff Garzik if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
26636c1153e0SJeff Garzik EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
2664bdd4dddeSJeff Garzik EDMA_ERR_INTRL_PAR)) {
2665bdd4dddeSJeff Garzik err_mask |= AC_ERR_ATA_BUS;
2666cf480626STejun Heo action |= ATA_EH_RESET;
2667b64bbc39STejun Heo ata_ehi_push_desc(ehi, "parity error");
2668bdd4dddeSJeff Garzik }
2669bdd4dddeSJeff Garzik if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
2670bdd4dddeSJeff Garzik ata_ehi_hotplugged(ehi);
2671bdd4dddeSJeff Garzik ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
2672b64bbc39STejun Heo "dev disconnect" : "dev connect");
2673cf480626STejun Heo action |= ATA_EH_RESET;
2674bdd4dddeSJeff Garzik }
2675bdd4dddeSJeff Garzik
2676352fab70SMark Lord /*
2677352fab70SMark Lord * Gen-I has a different SELF_DIS bit,
2678352fab70SMark Lord * different FREEZE bits, and no SERR bit:
2679352fab70SMark Lord */
2680ee9ccdf7SJeff Garzik if (IS_GEN_I(hpriv)) {
2681bdd4dddeSJeff Garzik eh_freeze_mask = EDMA_EH_FREEZE_5;
2682bdd4dddeSJeff Garzik if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
2683c6fd2807SJeff Garzik pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2684b64bbc39STejun Heo ata_ehi_push_desc(ehi, "EDMA self-disable");
2685c6fd2807SJeff Garzik }
2686bdd4dddeSJeff Garzik } else {
2687bdd4dddeSJeff Garzik eh_freeze_mask = EDMA_EH_FREEZE;
2688bdd4dddeSJeff Garzik if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2689bdd4dddeSJeff Garzik pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
2690b64bbc39STejun Heo ata_ehi_push_desc(ehi, "EDMA self-disable");
2691bdd4dddeSJeff Garzik }
2692bdd4dddeSJeff Garzik if (edma_err_cause & EDMA_ERR_SERR) {
26938d07379dSMark Lord ata_ehi_push_desc(ehi, "SError=%08x", serr);
26948d07379dSMark Lord err_mask |= AC_ERR_ATA_BUS;
2695cf480626STejun Heo action |= ATA_EH_RESET;
2696bdd4dddeSJeff Garzik }
2697bdd4dddeSJeff Garzik }
2698c6fd2807SJeff Garzik
2699bdd4dddeSJeff Garzik if (!err_mask) {
2700bdd4dddeSJeff Garzik err_mask = AC_ERR_OTHER;
2701cf480626STejun Heo action |= ATA_EH_RESET;
2702bdd4dddeSJeff Garzik }
2703bdd4dddeSJeff Garzik
2704bdd4dddeSJeff Garzik ehi->serror |= serr;
2705bdd4dddeSJeff Garzik ehi->action |= action;
2706bdd4dddeSJeff Garzik
2707bdd4dddeSJeff Garzik if (qc)
2708bdd4dddeSJeff Garzik qc->err_mask |= err_mask;
2709bdd4dddeSJeff Garzik else
2710bdd4dddeSJeff Garzik ehi->err_mask |= err_mask;
2711bdd4dddeSJeff Garzik
271237b9046aSMark Lord if (err_mask == AC_ERR_DEV) {
271337b9046aSMark Lord /*
271437b9046aSMark Lord * Cannot do ata_port_freeze() here,
271537b9046aSMark Lord * because it would kill PIO access,
271637b9046aSMark Lord * which is needed for further diagnosis.
271737b9046aSMark Lord */
271837b9046aSMark Lord mv_eh_freeze(ap);
271937b9046aSMark Lord abort = 1;
272037b9046aSMark Lord } else if (edma_err_cause & eh_freeze_mask) {
272137b9046aSMark Lord /*
272237b9046aSMark Lord * Note to self: ata_port_freeze() calls ata_port_abort()
272337b9046aSMark Lord */
2724bdd4dddeSJeff Garzik ata_port_freeze(ap);
272537b9046aSMark Lord } else {
272637b9046aSMark Lord abort = 1;
272737b9046aSMark Lord }
272837b9046aSMark Lord
272937b9046aSMark Lord if (abort) {
273037b9046aSMark Lord if (qc)
273137b9046aSMark Lord ata_link_abort(qc->dev->link);
2732bdd4dddeSJeff Garzik else
2733bdd4dddeSJeff Garzik ata_port_abort(ap);
2734bdd4dddeSJeff Garzik }
273537b9046aSMark Lord }
2736bdd4dddeSJeff Garzik
mv_process_crpb_response(struct ata_port * ap,struct mv_crpb * response,unsigned int tag,int ncq_enabled)27371aadf5c3STejun Heo static bool mv_process_crpb_response(struct ata_port *ap,
2738fcfb1f77SMark Lord struct mv_crpb *response, unsigned int tag, int ncq_enabled)
2739fcfb1f77SMark Lord {
2740fcfb1f77SMark Lord u8 ata_status;
2741fcfb1f77SMark Lord u16 edma_status = le16_to_cpu(response->flags);
2742752e386cSTejun Heo
2743fcfb1f77SMark Lord /*
2744fcfb1f77SMark Lord * edma_status from a response queue entry:
2745cae5a29dSMark Lord * LSB is from EDMA_ERR_IRQ_CAUSE (non-NCQ only).
2746fcfb1f77SMark Lord * MSB is saved ATA status from command completion.
2747fcfb1f77SMark Lord */
2748fcfb1f77SMark Lord if (!ncq_enabled) {
2749fcfb1f77SMark Lord u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
2750fcfb1f77SMark Lord if (err_cause) {
2751fcfb1f77SMark Lord /*
2752752e386cSTejun Heo * Error will be seen/handled by
2753752e386cSTejun Heo * mv_err_intr(). So do nothing at all here.
2754fcfb1f77SMark Lord */
27551aadf5c3STejun Heo return false;
2756fcfb1f77SMark Lord }
2757fcfb1f77SMark Lord }
2758fcfb1f77SMark Lord ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
275937b9046aSMark Lord if (!ac_err_mask(ata_status))
27601aadf5c3STejun Heo return true;
276137b9046aSMark Lord /* else: leave it for mv_err_intr() */
27621aadf5c3STejun Heo return false;
2763fcfb1f77SMark Lord }
2764fcfb1f77SMark Lord
mv_process_crpb_entries(struct ata_port * ap,struct mv_port_priv * pp)2765fcfb1f77SMark Lord static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
2766bdd4dddeSJeff Garzik {
2767bdd4dddeSJeff Garzik void __iomem *port_mmio = mv_ap_base(ap);
2768bdd4dddeSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data;
2769fcfb1f77SMark Lord u32 in_index;
2770bdd4dddeSJeff Garzik bool work_done = false;
27711aadf5c3STejun Heo u32 done_mask = 0;
2772fcfb1f77SMark Lord int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
2773bdd4dddeSJeff Garzik
2774fcfb1f77SMark Lord /* Get the hardware queue position index */
2775cae5a29dSMark Lord in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR)
2776bdd4dddeSJeff Garzik >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2777bdd4dddeSJeff Garzik
2778fcfb1f77SMark Lord /* Process new responses from since the last time we looked */
2779fcfb1f77SMark Lord while (in_index != pp->resp_idx) {
27806c1153e0SJeff Garzik unsigned int tag;
2781fcfb1f77SMark Lord struct mv_crpb *response = &pp->crpb[pp->resp_idx];
2782bdd4dddeSJeff Garzik
2783fcfb1f77SMark Lord pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2784bdd4dddeSJeff Garzik
2785fcfb1f77SMark Lord if (IS_GEN_I(hpriv)) {
2786fcfb1f77SMark Lord /* 50xx: no NCQ, only one command active at a time */
27879af5c9c9STejun Heo tag = ap->link.active_tag;
2788fcfb1f77SMark Lord } else {
2789fcfb1f77SMark Lord /* Gen II/IIE: get command tag from CRPB entry */
2790fcfb1f77SMark Lord tag = le16_to_cpu(response->id) & 0x1f;
2791bdd4dddeSJeff Garzik }
27921aadf5c3STejun Heo if (mv_process_crpb_response(ap, response, tag, ncq_enabled))
27931aadf5c3STejun Heo done_mask |= 1 << tag;
2794bdd4dddeSJeff Garzik work_done = true;
2795bdd4dddeSJeff Garzik }
2796bdd4dddeSJeff Garzik
27971aadf5c3STejun Heo if (work_done) {
27988385d756SSascha Hauer ata_qc_complete_multiple(ap, ata_qc_get_active(ap) ^ done_mask);
27991aadf5c3STejun Heo
2800352fab70SMark Lord /* Update the software queue position index in hardware */
2801bdd4dddeSJeff Garzik writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
2802fcfb1f77SMark Lord (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
2803cae5a29dSMark Lord port_mmio + EDMA_RSP_Q_OUT_PTR);
2804c6fd2807SJeff Garzik }
28051aadf5c3STejun Heo }
2806c6fd2807SJeff Garzik
mv_port_intr(struct ata_port * ap,u32 port_cause)2807a9010329SMark Lord static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2808a9010329SMark Lord {
2809a9010329SMark Lord struct mv_port_priv *pp;
2810a9010329SMark Lord int edma_was_enabled;
2811a9010329SMark Lord
2812a9010329SMark Lord /*
2813a9010329SMark Lord * Grab a snapshot of the EDMA_EN flag setting,
2814a9010329SMark Lord * so that we have a consistent view for this port,
2815a9010329SMark Lord * even if something we call of our routines changes it.
2816a9010329SMark Lord */
2817a9010329SMark Lord pp = ap->private_data;
2818a9010329SMark Lord edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2819a9010329SMark Lord /*
2820a9010329SMark Lord * Process completed CRPB response(s) before other events.
2821a9010329SMark Lord */
2822a9010329SMark Lord if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2823a9010329SMark Lord mv_process_crpb_entries(ap, pp);
28244c299ca3SMark Lord if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
28254c299ca3SMark Lord mv_handle_fbs_ncq_dev_err(ap);
2826a9010329SMark Lord }
2827a9010329SMark Lord /*
2828a9010329SMark Lord * Handle chip-reported errors, or continue on to handle PIO.
2829a9010329SMark Lord */
2830a9010329SMark Lord if (unlikely(port_cause & ERR_IRQ)) {
2831a9010329SMark Lord mv_err_intr(ap);
2832a9010329SMark Lord } else if (!edma_was_enabled) {
2833a9010329SMark Lord struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2834a9010329SMark Lord if (qc)
2835c3b28894STejun Heo ata_bmdma_port_intr(ap, qc);
2836a9010329SMark Lord else
2837a9010329SMark Lord mv_unexpected_intr(ap, edma_was_enabled);
2838a9010329SMark Lord }
2839a9010329SMark Lord }
2840a9010329SMark Lord
2841c6fd2807SJeff Garzik /**
2842c6fd2807SJeff Garzik * mv_host_intr - Handle all interrupts on the given host controller
2843cca3974eSJeff Garzik * @host: host specific structure
28447368f919SMark Lord * @main_irq_cause: Main interrupt cause register for the chip.
2845c6fd2807SJeff Garzik *
2846c6fd2807SJeff Garzik * LOCKING:
2847c6fd2807SJeff Garzik * Inherited from caller.
2848c6fd2807SJeff Garzik */
mv_host_intr(struct ata_host * host,u32 main_irq_cause)28497368f919SMark Lord static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
2850c6fd2807SJeff Garzik {
2851f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = host->private_data;
2852eabd5eb1SMark Lord void __iomem *mmio = hpriv->base, *hc_mmio;
2853a3718c1fSMark Lord unsigned int handled = 0, port;
2854c6fd2807SJeff Garzik
28552b748a0aSMark Lord /* If asserted, clear the "all ports" IRQ coalescing bit */
28562b748a0aSMark Lord if (main_irq_cause & ALL_PORTS_COAL_DONE)
2857cae5a29dSMark Lord writel(~ALL_PORTS_COAL_IRQ, mmio + IRQ_COAL_CAUSE);
28582b748a0aSMark Lord
2859a3718c1fSMark Lord for (port = 0; port < hpriv->n_ports; port++) {
2860cca3974eSJeff Garzik struct ata_port *ap = host->ports[port];
2861eabd5eb1SMark Lord unsigned int p, shift, hardport, port_cause;
2862eabd5eb1SMark Lord
2863a3718c1fSMark Lord MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
2864a3718c1fSMark Lord /*
2865eabd5eb1SMark Lord * Each hc within the host has its own hc_irq_cause register,
2866eabd5eb1SMark Lord * where the interrupting ports bits get ack'd.
2867a3718c1fSMark Lord */
2868eabd5eb1SMark Lord if (hardport == 0) { /* first port on this hc ? */
2869eabd5eb1SMark Lord u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2870eabd5eb1SMark Lord u32 port_mask, ack_irqs;
2871eabd5eb1SMark Lord /*
2872eabd5eb1SMark Lord * Skip this entire hc if nothing pending for any ports
2873eabd5eb1SMark Lord */
2874eabd5eb1SMark Lord if (!hc_cause) {
2875eabd5eb1SMark Lord port += MV_PORTS_PER_HC - 1;
2876eabd5eb1SMark Lord continue;
2877eabd5eb1SMark Lord }
2878eabd5eb1SMark Lord /*
2879eabd5eb1SMark Lord * We don't need/want to read the hc_irq_cause register,
2880eabd5eb1SMark Lord * because doing so hurts performance, and
2881eabd5eb1SMark Lord * main_irq_cause already gives us everything we need.
2882eabd5eb1SMark Lord *
2883eabd5eb1SMark Lord * But we do have to *write* to the hc_irq_cause to ack
2884eabd5eb1SMark Lord * the ports that we are handling this time through.
2885eabd5eb1SMark Lord *
2886eabd5eb1SMark Lord * This requires that we create a bitmap for those
2887eabd5eb1SMark Lord * ports which interrupted us, and use that bitmap
2888eabd5eb1SMark Lord * to ack (only) those ports via hc_irq_cause.
2889eabd5eb1SMark Lord */
2890eabd5eb1SMark Lord ack_irqs = 0;
28912b748a0aSMark Lord if (hc_cause & PORTS_0_3_COAL_DONE)
28922b748a0aSMark Lord ack_irqs = HC_COAL_IRQ;
2893eabd5eb1SMark Lord for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2894eabd5eb1SMark Lord if ((port + p) >= hpriv->n_ports)
2895eabd5eb1SMark Lord break;
2896eabd5eb1SMark Lord port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2897eabd5eb1SMark Lord if (hc_cause & port_mask)
2898eabd5eb1SMark Lord ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2899eabd5eb1SMark Lord }
2900a3718c1fSMark Lord hc_mmio = mv_hc_base_from_port(mmio, port);
2901cae5a29dSMark Lord writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE);
2902a3718c1fSMark Lord handled = 1;
2903a3718c1fSMark Lord }
2904a9010329SMark Lord /*
2905a9010329SMark Lord * Handle interrupts signalled for this port:
2906a9010329SMark Lord */
2907eabd5eb1SMark Lord port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
2908a9010329SMark Lord if (port_cause)
2909a9010329SMark Lord mv_port_intr(ap, port_cause);
2910eabd5eb1SMark Lord }
2911a3718c1fSMark Lord return handled;
2912c6fd2807SJeff Garzik }
2913c6fd2807SJeff Garzik
mv_pci_error(struct ata_host * host,void __iomem * mmio)2914a3718c1fSMark Lord static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
2915bdd4dddeSJeff Garzik {
291602a121daSMark Lord struct mv_host_priv *hpriv = host->private_data;
2917bdd4dddeSJeff Garzik struct ata_port *ap;
2918bdd4dddeSJeff Garzik struct ata_queued_cmd *qc;
2919bdd4dddeSJeff Garzik struct ata_eh_info *ehi;
2920bdd4dddeSJeff Garzik unsigned int i, err_mask, printed = 0;
2921bdd4dddeSJeff Garzik u32 err_cause;
2922bdd4dddeSJeff Garzik
2923cae5a29dSMark Lord err_cause = readl(mmio + hpriv->irq_cause_offset);
2924bdd4dddeSJeff Garzik
2925a44fec1fSJoe Perches dev_err(host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n", err_cause);
2926bdd4dddeSJeff Garzik
2927a2715a42SHannes Reinecke dev_dbg(host->dev, "%s: All regs @ PCI error\n", __func__);
292837fcfadeSHannes Reinecke mv_dump_all_regs(mmio, to_pci_dev(host->dev));
2929bdd4dddeSJeff Garzik
2930cae5a29dSMark Lord writelfl(0, mmio + hpriv->irq_cause_offset);
2931bdd4dddeSJeff Garzik
2932bdd4dddeSJeff Garzik for (i = 0; i < host->n_ports; i++) {
2933bdd4dddeSJeff Garzik ap = host->ports[i];
2934936fd732STejun Heo if (!ata_link_offline(&ap->link)) {
29359af5c9c9STejun Heo ehi = &ap->link.eh_info;
2936bdd4dddeSJeff Garzik ata_ehi_clear_desc(ehi);
2937bdd4dddeSJeff Garzik if (!printed++)
2938bdd4dddeSJeff Garzik ata_ehi_push_desc(ehi,
2939bdd4dddeSJeff Garzik "PCI err cause 0x%08x", err_cause);
2940bdd4dddeSJeff Garzik err_mask = AC_ERR_HOST_BUS;
2941cf480626STejun Heo ehi->action = ATA_EH_RESET;
29429af5c9c9STejun Heo qc = ata_qc_from_tag(ap, ap->link.active_tag);
2943bdd4dddeSJeff Garzik if (qc)
2944bdd4dddeSJeff Garzik qc->err_mask |= err_mask;
2945bdd4dddeSJeff Garzik else
2946bdd4dddeSJeff Garzik ehi->err_mask |= err_mask;
2947bdd4dddeSJeff Garzik
2948bdd4dddeSJeff Garzik ata_port_freeze(ap);
2949bdd4dddeSJeff Garzik }
2950bdd4dddeSJeff Garzik }
2951a3718c1fSMark Lord return 1; /* handled */
2952bdd4dddeSJeff Garzik }
2953bdd4dddeSJeff Garzik
2954c6fd2807SJeff Garzik /**
2955c5d3e45aSJeff Garzik * mv_interrupt - Main interrupt event handler
2956c6fd2807SJeff Garzik * @irq: unused
2957c6fd2807SJeff Garzik * @dev_instance: private data; in this case the host structure
2958c6fd2807SJeff Garzik *
2959c6fd2807SJeff Garzik * Read the read only register to determine if any host
2960c6fd2807SJeff Garzik * controllers have pending interrupts. If so, call lower level
2961c6fd2807SJeff Garzik * routine to handle. Also check for PCI errors which are only
2962c6fd2807SJeff Garzik * reported here.
2963c6fd2807SJeff Garzik *
2964c6fd2807SJeff Garzik * LOCKING:
2965cca3974eSJeff Garzik * This routine holds the host lock while processing pending
2966c6fd2807SJeff Garzik * interrupts.
2967c6fd2807SJeff Garzik */
mv_interrupt(int irq,void * dev_instance)29687d12e780SDavid Howells static irqreturn_t mv_interrupt(int irq, void *dev_instance)
2969c6fd2807SJeff Garzik {
2970cca3974eSJeff Garzik struct ata_host *host = dev_instance;
2971f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = host->private_data;
2972a3718c1fSMark Lord unsigned int handled = 0;
29736d3c30efSMark Lord int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
297496e2c487SMark Lord u32 main_irq_cause, pending_irqs;
2975c6fd2807SJeff Garzik
2976646a4da5SMark Lord spin_lock(&host->lock);
29776d3c30efSMark Lord
29786d3c30efSMark Lord /* for MSI: block new interrupts while in here */
29796d3c30efSMark Lord if (using_msi)
29802b748a0aSMark Lord mv_write_main_irq_mask(0, hpriv);
29816d3c30efSMark Lord
29827368f919SMark Lord main_irq_cause = readl(hpriv->main_irq_cause_addr);
298396e2c487SMark Lord pending_irqs = main_irq_cause & hpriv->main_irq_mask;
2984352fab70SMark Lord /*
2985352fab70SMark Lord * Deal with cases where we either have nothing pending, or have read
2986352fab70SMark Lord * a bogus register value which can indicate HW removal or PCI fault.
2987c6fd2807SJeff Garzik */
2988a44253d2SMark Lord if (pending_irqs && main_irq_cause != 0xffffffffU) {
29891f398472SMark Lord if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
2990a3718c1fSMark Lord handled = mv_pci_error(host, hpriv->base);
2991a3718c1fSMark Lord else
2992a44253d2SMark Lord handled = mv_host_intr(host, pending_irqs);
2993bdd4dddeSJeff Garzik }
29946d3c30efSMark Lord
29956d3c30efSMark Lord /* for MSI: unmask; interrupt cause bits will retrigger now */
29966d3c30efSMark Lord if (using_msi)
29972b748a0aSMark Lord mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
29986d3c30efSMark Lord
29999d51af7bSMark Lord spin_unlock(&host->lock);
30009d51af7bSMark Lord
3001c6fd2807SJeff Garzik return IRQ_RETVAL(handled);
3002c6fd2807SJeff Garzik }
3003c6fd2807SJeff Garzik
mv5_scr_offset(unsigned int sc_reg_in)3004c6fd2807SJeff Garzik static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
3005c6fd2807SJeff Garzik {
3006c6fd2807SJeff Garzik unsigned int ofs;
3007c6fd2807SJeff Garzik
3008c6fd2807SJeff Garzik switch (sc_reg_in) {
3009c6fd2807SJeff Garzik case SCR_STATUS:
3010c6fd2807SJeff Garzik case SCR_ERROR:
3011c6fd2807SJeff Garzik case SCR_CONTROL:
3012c6fd2807SJeff Garzik ofs = sc_reg_in * sizeof(u32);
3013c6fd2807SJeff Garzik break;
3014c6fd2807SJeff Garzik default:
3015c6fd2807SJeff Garzik ofs = 0xffffffffU;
3016c6fd2807SJeff Garzik break;
3017c6fd2807SJeff Garzik }
3018c6fd2807SJeff Garzik return ofs;
3019c6fd2807SJeff Garzik }
3020c6fd2807SJeff Garzik
mv5_scr_read(struct ata_link * link,unsigned int sc_reg_in,u32 * val)302182ef04fbSTejun Heo static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
3022c6fd2807SJeff Garzik {
302382ef04fbSTejun Heo struct mv_host_priv *hpriv = link->ap->host->private_data;
3024f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base;
302582ef04fbSTejun Heo void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3026c6fd2807SJeff Garzik unsigned int ofs = mv5_scr_offset(sc_reg_in);
3027c6fd2807SJeff Garzik
3028da3dbb17STejun Heo if (ofs != 0xffffffffU) {
3029da3dbb17STejun Heo *val = readl(addr + ofs);
3030da3dbb17STejun Heo return 0;
3031da3dbb17STejun Heo } else
3032da3dbb17STejun Heo return -EINVAL;
3033c6fd2807SJeff Garzik }
3034c6fd2807SJeff Garzik
mv5_scr_write(struct ata_link * link,unsigned int sc_reg_in,u32 val)303582ef04fbSTejun Heo static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
3036c6fd2807SJeff Garzik {
303782ef04fbSTejun Heo struct mv_host_priv *hpriv = link->ap->host->private_data;
3038f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base;
303982ef04fbSTejun Heo void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
3040c6fd2807SJeff Garzik unsigned int ofs = mv5_scr_offset(sc_reg_in);
3041c6fd2807SJeff Garzik
3042da3dbb17STejun Heo if (ofs != 0xffffffffU) {
30430d5ff566STejun Heo writelfl(val, addr + ofs);
3044da3dbb17STejun Heo return 0;
3045da3dbb17STejun Heo } else
3046da3dbb17STejun Heo return -EINVAL;
3047c6fd2807SJeff Garzik }
3048c6fd2807SJeff Garzik
mv5_reset_bus(struct ata_host * host,void __iomem * mmio)30497bb3c529SSaeed Bishara static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
3050c6fd2807SJeff Garzik {
30517bb3c529SSaeed Bishara struct pci_dev *pdev = to_pci_dev(host->dev);
3052c6fd2807SJeff Garzik int early_5080;
3053c6fd2807SJeff Garzik
305444c10138SAuke Kok early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
3055c6fd2807SJeff Garzik
3056c6fd2807SJeff Garzik if (!early_5080) {
3057c6fd2807SJeff Garzik u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3058c6fd2807SJeff Garzik tmp |= (1 << 0);
3059c6fd2807SJeff Garzik writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3060c6fd2807SJeff Garzik }
3061c6fd2807SJeff Garzik
30627bb3c529SSaeed Bishara mv_reset_pci_bus(host, mmio);
3063c6fd2807SJeff Garzik }
3064c6fd2807SJeff Garzik
mv5_reset_flash(struct mv_host_priv * hpriv,void __iomem * mmio)3065c6fd2807SJeff Garzik static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3066c6fd2807SJeff Garzik {
3067cae5a29dSMark Lord writel(0x0fcfffff, mmio + FLASH_CTL);
3068c6fd2807SJeff Garzik }
3069c6fd2807SJeff Garzik
mv5_read_preamp(struct mv_host_priv * hpriv,int idx,void __iomem * mmio)3070c6fd2807SJeff Garzik static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
3071c6fd2807SJeff Garzik void __iomem *mmio)
3072c6fd2807SJeff Garzik {
3073c6fd2807SJeff Garzik void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
3074c6fd2807SJeff Garzik u32 tmp;
3075c6fd2807SJeff Garzik
3076c6fd2807SJeff Garzik tmp = readl(phy_mmio + MV5_PHY_MODE);
3077c6fd2807SJeff Garzik
3078c6fd2807SJeff Garzik hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
3079c6fd2807SJeff Garzik hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
3080c6fd2807SJeff Garzik }
3081c6fd2807SJeff Garzik
mv5_enable_leds(struct mv_host_priv * hpriv,void __iomem * mmio)3082c6fd2807SJeff Garzik static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3083c6fd2807SJeff Garzik {
3084c6fd2807SJeff Garzik u32 tmp;
3085c6fd2807SJeff Garzik
3086cae5a29dSMark Lord writel(0, mmio + GPIO_PORT_CTL);
3087c6fd2807SJeff Garzik
3088c6fd2807SJeff Garzik /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
3089c6fd2807SJeff Garzik
3090c6fd2807SJeff Garzik tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
3091c6fd2807SJeff Garzik tmp |= ~(1 << 0);
3092c6fd2807SJeff Garzik writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
3093c6fd2807SJeff Garzik }
3094c6fd2807SJeff Garzik
mv5_phy_errata(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int port)3095c6fd2807SJeff Garzik static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3096c6fd2807SJeff Garzik unsigned int port)
3097c6fd2807SJeff Garzik {
3098c6fd2807SJeff Garzik void __iomem *phy_mmio = mv5_phy_base(mmio, port);
3099c6fd2807SJeff Garzik const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
3100c6fd2807SJeff Garzik u32 tmp;
3101c6fd2807SJeff Garzik int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
3102c6fd2807SJeff Garzik
3103c6fd2807SJeff Garzik if (fix_apm_sq) {
3104cae5a29dSMark Lord tmp = readl(phy_mmio + MV5_LTMODE);
3105c6fd2807SJeff Garzik tmp |= (1 << 19);
3106cae5a29dSMark Lord writel(tmp, phy_mmio + MV5_LTMODE);
3107c6fd2807SJeff Garzik
3108cae5a29dSMark Lord tmp = readl(phy_mmio + MV5_PHY_CTL);
3109c6fd2807SJeff Garzik tmp &= ~0x3;
3110c6fd2807SJeff Garzik tmp |= 0x1;
3111cae5a29dSMark Lord writel(tmp, phy_mmio + MV5_PHY_CTL);
3112c6fd2807SJeff Garzik }
3113c6fd2807SJeff Garzik
3114c6fd2807SJeff Garzik tmp = readl(phy_mmio + MV5_PHY_MODE);
3115c6fd2807SJeff Garzik tmp &= ~mask;
3116c6fd2807SJeff Garzik tmp |= hpriv->signal[port].pre;
3117c6fd2807SJeff Garzik tmp |= hpriv->signal[port].amps;
3118c6fd2807SJeff Garzik writel(tmp, phy_mmio + MV5_PHY_MODE);
3119c6fd2807SJeff Garzik }
3120c6fd2807SJeff Garzik
3121c6fd2807SJeff Garzik
3122c6fd2807SJeff Garzik #undef ZERO
3123c6fd2807SJeff Garzik #define ZERO(reg) writel(0, port_mmio + (reg))
mv5_reset_hc_port(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int port)3124c6fd2807SJeff Garzik static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
3125c6fd2807SJeff Garzik unsigned int port)
3126c6fd2807SJeff Garzik {
3127c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port);
3128c6fd2807SJeff Garzik
3129e12bef50SMark Lord mv_reset_channel(hpriv, mmio, port);
3130c6fd2807SJeff Garzik
3131c6fd2807SJeff Garzik ZERO(0x028); /* command */
3132cae5a29dSMark Lord writel(0x11f, port_mmio + EDMA_CFG);
3133c6fd2807SJeff Garzik ZERO(0x004); /* timer */
3134c6fd2807SJeff Garzik ZERO(0x008); /* irq err cause */
3135c6fd2807SJeff Garzik ZERO(0x00c); /* irq err mask */
3136c6fd2807SJeff Garzik ZERO(0x010); /* rq bah */
3137c6fd2807SJeff Garzik ZERO(0x014); /* rq inp */
3138c6fd2807SJeff Garzik ZERO(0x018); /* rq outp */
3139c6fd2807SJeff Garzik ZERO(0x01c); /* respq bah */
3140c6fd2807SJeff Garzik ZERO(0x024); /* respq outp */
3141c6fd2807SJeff Garzik ZERO(0x020); /* respq inp */
3142c6fd2807SJeff Garzik ZERO(0x02c); /* test control */
3143cae5a29dSMark Lord writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
3144c6fd2807SJeff Garzik }
3145c6fd2807SJeff Garzik #undef ZERO
3146c6fd2807SJeff Garzik
3147c6fd2807SJeff Garzik #define ZERO(reg) writel(0, hc_mmio + (reg))
mv5_reset_one_hc(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int hc)3148c6fd2807SJeff Garzik static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3149c6fd2807SJeff Garzik unsigned int hc)
3150c6fd2807SJeff Garzik {
3151c6fd2807SJeff Garzik void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3152c6fd2807SJeff Garzik u32 tmp;
3153c6fd2807SJeff Garzik
3154c6fd2807SJeff Garzik ZERO(0x00c);
3155c6fd2807SJeff Garzik ZERO(0x010);
3156c6fd2807SJeff Garzik ZERO(0x014);
3157c6fd2807SJeff Garzik ZERO(0x018);
3158c6fd2807SJeff Garzik
3159c6fd2807SJeff Garzik tmp = readl(hc_mmio + 0x20);
3160c6fd2807SJeff Garzik tmp &= 0x1c1c1c1c;
3161c6fd2807SJeff Garzik tmp |= 0x03030303;
3162c6fd2807SJeff Garzik writel(tmp, hc_mmio + 0x20);
3163c6fd2807SJeff Garzik }
3164c6fd2807SJeff Garzik #undef ZERO
3165c6fd2807SJeff Garzik
mv5_reset_hc(struct ata_host * host,void __iomem * mmio,unsigned int n_hc)3166f76ba003SHannes Reinecke static int mv5_reset_hc(struct ata_host *host, void __iomem *mmio,
3167c6fd2807SJeff Garzik unsigned int n_hc)
3168c6fd2807SJeff Garzik {
3169f76ba003SHannes Reinecke struct mv_host_priv *hpriv = host->private_data;
3170c6fd2807SJeff Garzik unsigned int hc, port;
3171c6fd2807SJeff Garzik
3172c6fd2807SJeff Garzik for (hc = 0; hc < n_hc; hc++) {
3173c6fd2807SJeff Garzik for (port = 0; port < MV_PORTS_PER_HC; port++)
3174c6fd2807SJeff Garzik mv5_reset_hc_port(hpriv, mmio,
3175c6fd2807SJeff Garzik (hc * MV_PORTS_PER_HC) + port);
3176c6fd2807SJeff Garzik
3177c6fd2807SJeff Garzik mv5_reset_one_hc(hpriv, mmio, hc);
3178c6fd2807SJeff Garzik }
3179c6fd2807SJeff Garzik
3180c6fd2807SJeff Garzik return 0;
3181c6fd2807SJeff Garzik }
3182c6fd2807SJeff Garzik
3183c6fd2807SJeff Garzik #undef ZERO
3184c6fd2807SJeff Garzik #define ZERO(reg) writel(0, mmio + (reg))
mv_reset_pci_bus(struct ata_host * host,void __iomem * mmio)31857bb3c529SSaeed Bishara static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
3186c6fd2807SJeff Garzik {
318702a121daSMark Lord struct mv_host_priv *hpriv = host->private_data;
3188c6fd2807SJeff Garzik u32 tmp;
3189c6fd2807SJeff Garzik
3190cae5a29dSMark Lord tmp = readl(mmio + MV_PCI_MODE);
3191c6fd2807SJeff Garzik tmp &= 0xff00ffff;
3192cae5a29dSMark Lord writel(tmp, mmio + MV_PCI_MODE);
3193c6fd2807SJeff Garzik
3194c6fd2807SJeff Garzik ZERO(MV_PCI_DISC_TIMER);
3195c6fd2807SJeff Garzik ZERO(MV_PCI_MSI_TRIGGER);
3196cae5a29dSMark Lord writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
3197c6fd2807SJeff Garzik ZERO(MV_PCI_SERR_MASK);
3198cae5a29dSMark Lord ZERO(hpriv->irq_cause_offset);
3199cae5a29dSMark Lord ZERO(hpriv->irq_mask_offset);
3200c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_LOW_ADDRESS);
3201c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_HIGH_ADDRESS);
3202c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_ATTRIBUTE);
3203c6fd2807SJeff Garzik ZERO(MV_PCI_ERR_COMMAND);
3204c6fd2807SJeff Garzik }
3205c6fd2807SJeff Garzik #undef ZERO
3206c6fd2807SJeff Garzik
mv6_reset_flash(struct mv_host_priv * hpriv,void __iomem * mmio)3207c6fd2807SJeff Garzik static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3208c6fd2807SJeff Garzik {
3209c6fd2807SJeff Garzik u32 tmp;
3210c6fd2807SJeff Garzik
3211c6fd2807SJeff Garzik mv5_reset_flash(hpriv, mmio);
3212c6fd2807SJeff Garzik
3213cae5a29dSMark Lord tmp = readl(mmio + GPIO_PORT_CTL);
3214c6fd2807SJeff Garzik tmp &= 0x3;
3215c6fd2807SJeff Garzik tmp |= (1 << 5) | (1 << 6);
3216cae5a29dSMark Lord writel(tmp, mmio + GPIO_PORT_CTL);
3217c6fd2807SJeff Garzik }
3218c6fd2807SJeff Garzik
3219f3a23c2cSLee Jones /*
3220c6fd2807SJeff Garzik * mv6_reset_hc - Perform the 6xxx global soft reset
3221c6fd2807SJeff Garzik * @mmio: base address of the HBA
3222c6fd2807SJeff Garzik *
3223c6fd2807SJeff Garzik * This routine only applies to 6xxx parts.
3224c6fd2807SJeff Garzik *
3225c6fd2807SJeff Garzik * LOCKING:
3226c6fd2807SJeff Garzik * Inherited from caller.
3227c6fd2807SJeff Garzik */
mv6_reset_hc(struct ata_host * host,void __iomem * mmio,unsigned int n_hc)3228f76ba003SHannes Reinecke static int mv6_reset_hc(struct ata_host *host, void __iomem *mmio,
3229c6fd2807SJeff Garzik unsigned int n_hc)
3230c6fd2807SJeff Garzik {
3231cae5a29dSMark Lord void __iomem *reg = mmio + PCI_MAIN_CMD_STS;
3232c6fd2807SJeff Garzik int i, rc = 0;
3233c6fd2807SJeff Garzik u32 t;
3234c6fd2807SJeff Garzik
3235c6fd2807SJeff Garzik /* Following procedure defined in PCI "main command and status
3236c6fd2807SJeff Garzik * register" table.
3237c6fd2807SJeff Garzik */
3238c6fd2807SJeff Garzik t = readl(reg);
3239c6fd2807SJeff Garzik writel(t | STOP_PCI_MASTER, reg);
3240c6fd2807SJeff Garzik
3241c6fd2807SJeff Garzik for (i = 0; i < 1000; i++) {
3242c6fd2807SJeff Garzik udelay(1);
3243c6fd2807SJeff Garzik t = readl(reg);
32442dcb407eSJeff Garzik if (PCI_MASTER_EMPTY & t)
3245c6fd2807SJeff Garzik break;
3246c6fd2807SJeff Garzik }
3247c6fd2807SJeff Garzik if (!(PCI_MASTER_EMPTY & t)) {
3248f76ba003SHannes Reinecke dev_err(host->dev, "PCI master won't flush\n");
3249c6fd2807SJeff Garzik rc = 1;
3250c6fd2807SJeff Garzik goto done;
3251c6fd2807SJeff Garzik }
3252c6fd2807SJeff Garzik
3253c6fd2807SJeff Garzik /* set reset */
3254c6fd2807SJeff Garzik i = 5;
3255c6fd2807SJeff Garzik do {
3256c6fd2807SJeff Garzik writel(t | GLOB_SFT_RST, reg);
3257c6fd2807SJeff Garzik t = readl(reg);
3258c6fd2807SJeff Garzik udelay(1);
3259c6fd2807SJeff Garzik } while (!(GLOB_SFT_RST & t) && (i-- > 0));
3260c6fd2807SJeff Garzik
3261c6fd2807SJeff Garzik if (!(GLOB_SFT_RST & t)) {
3262f76ba003SHannes Reinecke dev_err(host->dev, "can't set global reset\n");
3263c6fd2807SJeff Garzik rc = 1;
3264c6fd2807SJeff Garzik goto done;
3265c6fd2807SJeff Garzik }
3266c6fd2807SJeff Garzik
3267c6fd2807SJeff Garzik /* clear reset and *reenable the PCI master* (not mentioned in spec) */
3268c6fd2807SJeff Garzik i = 5;
3269c6fd2807SJeff Garzik do {
3270c6fd2807SJeff Garzik writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
3271c6fd2807SJeff Garzik t = readl(reg);
3272c6fd2807SJeff Garzik udelay(1);
3273c6fd2807SJeff Garzik } while ((GLOB_SFT_RST & t) && (i-- > 0));
3274c6fd2807SJeff Garzik
3275c6fd2807SJeff Garzik if (GLOB_SFT_RST & t) {
3276f76ba003SHannes Reinecke dev_err(host->dev, "can't clear global reset\n");
3277c6fd2807SJeff Garzik rc = 1;
3278c6fd2807SJeff Garzik }
3279c6fd2807SJeff Garzik done:
3280c6fd2807SJeff Garzik return rc;
3281c6fd2807SJeff Garzik }
3282c6fd2807SJeff Garzik
mv6_read_preamp(struct mv_host_priv * hpriv,int idx,void __iomem * mmio)3283c6fd2807SJeff Garzik static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
3284c6fd2807SJeff Garzik void __iomem *mmio)
3285c6fd2807SJeff Garzik {
3286c6fd2807SJeff Garzik void __iomem *port_mmio;
3287c6fd2807SJeff Garzik u32 tmp;
3288c6fd2807SJeff Garzik
3289cae5a29dSMark Lord tmp = readl(mmio + RESET_CFG);
3290c6fd2807SJeff Garzik if ((tmp & (1 << 0)) == 0) {
3291c6fd2807SJeff Garzik hpriv->signal[idx].amps = 0x7 << 8;
3292c6fd2807SJeff Garzik hpriv->signal[idx].pre = 0x1 << 5;
3293c6fd2807SJeff Garzik return;
3294c6fd2807SJeff Garzik }
3295c6fd2807SJeff Garzik
3296c6fd2807SJeff Garzik port_mmio = mv_port_base(mmio, idx);
3297c6fd2807SJeff Garzik tmp = readl(port_mmio + PHY_MODE2);
3298c6fd2807SJeff Garzik
3299c6fd2807SJeff Garzik hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
3300c6fd2807SJeff Garzik hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
3301c6fd2807SJeff Garzik }
3302c6fd2807SJeff Garzik
mv6_enable_leds(struct mv_host_priv * hpriv,void __iomem * mmio)3303c6fd2807SJeff Garzik static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
3304c6fd2807SJeff Garzik {
3305cae5a29dSMark Lord writel(0x00000060, mmio + GPIO_PORT_CTL);
3306c6fd2807SJeff Garzik }
3307c6fd2807SJeff Garzik
mv6_phy_errata(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int port)3308c6fd2807SJeff Garzik static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
3309c6fd2807SJeff Garzik unsigned int port)
3310c6fd2807SJeff Garzik {
3311c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port);
3312c6fd2807SJeff Garzik
3313c6fd2807SJeff Garzik u32 hp_flags = hpriv->hp_flags;
3314c6fd2807SJeff Garzik int fix_phy_mode2 =
3315c6fd2807SJeff Garzik hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
3316c6fd2807SJeff Garzik int fix_phy_mode4 =
3317c6fd2807SJeff Garzik hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
33188c30a8b9SMark Lord u32 m2, m3;
3319c6fd2807SJeff Garzik
3320c6fd2807SJeff Garzik if (fix_phy_mode2) {
3321c6fd2807SJeff Garzik m2 = readl(port_mmio + PHY_MODE2);
3322c6fd2807SJeff Garzik m2 &= ~(1 << 16);
3323c6fd2807SJeff Garzik m2 |= (1 << 31);
3324c6fd2807SJeff Garzik writel(m2, port_mmio + PHY_MODE2);
3325c6fd2807SJeff Garzik
3326c6fd2807SJeff Garzik udelay(200);
3327c6fd2807SJeff Garzik
3328c6fd2807SJeff Garzik m2 = readl(port_mmio + PHY_MODE2);
3329c6fd2807SJeff Garzik m2 &= ~((1 << 16) | (1 << 31));
3330c6fd2807SJeff Garzik writel(m2, port_mmio + PHY_MODE2);
3331c6fd2807SJeff Garzik
3332c6fd2807SJeff Garzik udelay(200);
3333c6fd2807SJeff Garzik }
3334c6fd2807SJeff Garzik
33358c30a8b9SMark Lord /*
33368c30a8b9SMark Lord * Gen-II/IIe PHY_MODE3 errata RM#2:
33378c30a8b9SMark Lord * Achieves better receiver noise performance than the h/w default:
33388c30a8b9SMark Lord */
33398c30a8b9SMark Lord m3 = readl(port_mmio + PHY_MODE3);
33408c30a8b9SMark Lord m3 = (m3 & 0x1f) | (0x5555601 << 5);
3341c6fd2807SJeff Garzik
33420388a8c0SMark Lord /* Guideline 88F5182 (GL# SATA-S11) */
33430388a8c0SMark Lord if (IS_SOC(hpriv))
33440388a8c0SMark Lord m3 &= ~0x1c;
33450388a8c0SMark Lord
3346c6fd2807SJeff Garzik if (fix_phy_mode4) {
3347ba069e37SMark Lord u32 m4 = readl(port_mmio + PHY_MODE4);
3348ba069e37SMark Lord /*
3349ba069e37SMark Lord * Enforce reserved-bit restrictions on GenIIe devices only.
3350ba069e37SMark Lord * For earlier chipsets, force only the internal config field
3351ba069e37SMark Lord * (workaround for errata FEr SATA#10 part 1).
3352ba069e37SMark Lord */
33538c30a8b9SMark Lord if (IS_GEN_IIE(hpriv))
3354ba069e37SMark Lord m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
3355ba069e37SMark Lord else
3356ba069e37SMark Lord m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
33578c30a8b9SMark Lord writel(m4, port_mmio + PHY_MODE4);
3358c6fd2807SJeff Garzik }
3359b406c7a6SMark Lord /*
3360b406c7a6SMark Lord * Workaround for 60x1-B2 errata SATA#13:
3361b406c7a6SMark Lord * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
3362b406c7a6SMark Lord * so we must always rewrite PHY_MODE3 after PHY_MODE4.
3363ba68460bSMark Lord * Or ensure we use writelfl() when writing PHY_MODE4.
3364b406c7a6SMark Lord */
3365b406c7a6SMark Lord writel(m3, port_mmio + PHY_MODE3);
3366c6fd2807SJeff Garzik
3367c6fd2807SJeff Garzik /* Revert values of pre-emphasis and signal amps to the saved ones */
3368c6fd2807SJeff Garzik m2 = readl(port_mmio + PHY_MODE2);
3369c6fd2807SJeff Garzik
3370c6fd2807SJeff Garzik m2 &= ~MV_M2_PREAMP_MASK;
3371c6fd2807SJeff Garzik m2 |= hpriv->signal[port].amps;
3372c6fd2807SJeff Garzik m2 |= hpriv->signal[port].pre;
3373c6fd2807SJeff Garzik m2 &= ~(1 << 16);
3374c6fd2807SJeff Garzik
3375c6fd2807SJeff Garzik /* according to mvSata 3.6.1, some IIE values are fixed */
3376c6fd2807SJeff Garzik if (IS_GEN_IIE(hpriv)) {
3377c6fd2807SJeff Garzik m2 &= ~0xC30FF01F;
3378c6fd2807SJeff Garzik m2 |= 0x0000900F;
3379c6fd2807SJeff Garzik }
3380c6fd2807SJeff Garzik
3381c6fd2807SJeff Garzik writel(m2, port_mmio + PHY_MODE2);
3382c6fd2807SJeff Garzik }
3383c6fd2807SJeff Garzik
3384f351b2d6SSaeed Bishara /* TODO: use the generic LED interface to configure the SATA Presence */
3385f351b2d6SSaeed Bishara /* & Acitivy LEDs on the board */
mv_soc_enable_leds(struct mv_host_priv * hpriv,void __iomem * mmio)3386f351b2d6SSaeed Bishara static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
3387f351b2d6SSaeed Bishara void __iomem *mmio)
3388f351b2d6SSaeed Bishara {
3389f351b2d6SSaeed Bishara return;
3390f351b2d6SSaeed Bishara }
3391f351b2d6SSaeed Bishara
mv_soc_read_preamp(struct mv_host_priv * hpriv,int idx,void __iomem * mmio)3392f351b2d6SSaeed Bishara static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
3393f351b2d6SSaeed Bishara void __iomem *mmio)
3394f351b2d6SSaeed Bishara {
3395f351b2d6SSaeed Bishara void __iomem *port_mmio;
3396f351b2d6SSaeed Bishara u32 tmp;
3397f351b2d6SSaeed Bishara
3398f351b2d6SSaeed Bishara port_mmio = mv_port_base(mmio, idx);
3399f351b2d6SSaeed Bishara tmp = readl(port_mmio + PHY_MODE2);
3400f351b2d6SSaeed Bishara
3401f351b2d6SSaeed Bishara hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
3402f351b2d6SSaeed Bishara hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
3403f351b2d6SSaeed Bishara }
3404f351b2d6SSaeed Bishara
3405f351b2d6SSaeed Bishara #undef ZERO
3406f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, port_mmio + (reg))
mv_soc_reset_hc_port(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int port)3407f351b2d6SSaeed Bishara static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
3408f351b2d6SSaeed Bishara void __iomem *mmio, unsigned int port)
3409f351b2d6SSaeed Bishara {
3410f351b2d6SSaeed Bishara void __iomem *port_mmio = mv_port_base(mmio, port);
3411f351b2d6SSaeed Bishara
3412e12bef50SMark Lord mv_reset_channel(hpriv, mmio, port);
3413f351b2d6SSaeed Bishara
3414f351b2d6SSaeed Bishara ZERO(0x028); /* command */
3415cae5a29dSMark Lord writel(0x101f, port_mmio + EDMA_CFG);
3416f351b2d6SSaeed Bishara ZERO(0x004); /* timer */
3417f351b2d6SSaeed Bishara ZERO(0x008); /* irq err cause */
3418f351b2d6SSaeed Bishara ZERO(0x00c); /* irq err mask */
3419f351b2d6SSaeed Bishara ZERO(0x010); /* rq bah */
3420f351b2d6SSaeed Bishara ZERO(0x014); /* rq inp */
3421f351b2d6SSaeed Bishara ZERO(0x018); /* rq outp */
3422f351b2d6SSaeed Bishara ZERO(0x01c); /* respq bah */
3423f351b2d6SSaeed Bishara ZERO(0x024); /* respq outp */
3424f351b2d6SSaeed Bishara ZERO(0x020); /* respq inp */
3425f351b2d6SSaeed Bishara ZERO(0x02c); /* test control */
3426d7b0c143SSaeed Bishara writel(0x800, port_mmio + EDMA_IORDY_TMOUT);
3427f351b2d6SSaeed Bishara }
3428f351b2d6SSaeed Bishara
3429f351b2d6SSaeed Bishara #undef ZERO
3430f351b2d6SSaeed Bishara
3431f351b2d6SSaeed Bishara #define ZERO(reg) writel(0, hc_mmio + (reg))
mv_soc_reset_one_hc(struct mv_host_priv * hpriv,void __iomem * mmio)3432f351b2d6SSaeed Bishara static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
3433f351b2d6SSaeed Bishara void __iomem *mmio)
3434f351b2d6SSaeed Bishara {
3435f351b2d6SSaeed Bishara void __iomem *hc_mmio = mv_hc_base(mmio, 0);
3436f351b2d6SSaeed Bishara
3437f351b2d6SSaeed Bishara ZERO(0x00c);
3438f351b2d6SSaeed Bishara ZERO(0x010);
3439f351b2d6SSaeed Bishara ZERO(0x014);
3440f351b2d6SSaeed Bishara
3441f351b2d6SSaeed Bishara }
3442f351b2d6SSaeed Bishara
3443f351b2d6SSaeed Bishara #undef ZERO
3444f351b2d6SSaeed Bishara
mv_soc_reset_hc(struct ata_host * host,void __iomem * mmio,unsigned int n_hc)3445f76ba003SHannes Reinecke static int mv_soc_reset_hc(struct ata_host *host,
3446f351b2d6SSaeed Bishara void __iomem *mmio, unsigned int n_hc)
3447f351b2d6SSaeed Bishara {
3448f76ba003SHannes Reinecke struct mv_host_priv *hpriv = host->private_data;
3449f351b2d6SSaeed Bishara unsigned int port;
3450f351b2d6SSaeed Bishara
3451f351b2d6SSaeed Bishara for (port = 0; port < hpriv->n_ports; port++)
3452f351b2d6SSaeed Bishara mv_soc_reset_hc_port(hpriv, mmio, port);
3453f351b2d6SSaeed Bishara
3454f351b2d6SSaeed Bishara mv_soc_reset_one_hc(hpriv, mmio);
3455f351b2d6SSaeed Bishara
3456f351b2d6SSaeed Bishara return 0;
3457f351b2d6SSaeed Bishara }
3458f351b2d6SSaeed Bishara
mv_soc_reset_flash(struct mv_host_priv * hpriv,void __iomem * mmio)3459f351b2d6SSaeed Bishara static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
3460f351b2d6SSaeed Bishara void __iomem *mmio)
3461f351b2d6SSaeed Bishara {
3462f351b2d6SSaeed Bishara return;
3463f351b2d6SSaeed Bishara }
3464f351b2d6SSaeed Bishara
mv_soc_reset_bus(struct ata_host * host,void __iomem * mmio)3465f351b2d6SSaeed Bishara static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
3466f351b2d6SSaeed Bishara {
3467f351b2d6SSaeed Bishara return;
3468f351b2d6SSaeed Bishara }
3469f351b2d6SSaeed Bishara
mv_soc_65n_phy_errata(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int port)347029b7e43cSMartin Michlmayr static void mv_soc_65n_phy_errata(struct mv_host_priv *hpriv,
347129b7e43cSMartin Michlmayr void __iomem *mmio, unsigned int port)
347229b7e43cSMartin Michlmayr {
347329b7e43cSMartin Michlmayr void __iomem *port_mmio = mv_port_base(mmio, port);
347429b7e43cSMartin Michlmayr u32 reg;
347529b7e43cSMartin Michlmayr
347629b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE3);
347729b7e43cSMartin Michlmayr reg &= ~(0x3 << 27); /* SELMUPF (bits 28:27) to 1 */
347829b7e43cSMartin Michlmayr reg |= (0x1 << 27);
347929b7e43cSMartin Michlmayr reg &= ~(0x3 << 29); /* SELMUPI (bits 30:29) to 1 */
348029b7e43cSMartin Michlmayr reg |= (0x1 << 29);
348129b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE3);
348229b7e43cSMartin Michlmayr
348329b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE4);
348429b7e43cSMartin Michlmayr reg &= ~0x1; /* SATU_OD8 (bit 0) to 0, reserved bit 16 must be set */
348529b7e43cSMartin Michlmayr reg |= (0x1 << 16);
348629b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE4);
348729b7e43cSMartin Michlmayr
348829b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE9_GEN2);
348929b7e43cSMartin Michlmayr reg &= ~0xf; /* TXAMP[3:0] (bits 3:0) to 8 */
349029b7e43cSMartin Michlmayr reg |= 0x8;
349129b7e43cSMartin Michlmayr reg &= ~(0x1 << 14); /* TXAMP[4] (bit 14) to 0 */
349229b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE9_GEN2);
349329b7e43cSMartin Michlmayr
349429b7e43cSMartin Michlmayr reg = readl(port_mmio + PHY_MODE9_GEN1);
349529b7e43cSMartin Michlmayr reg &= ~0xf; /* TXAMP[3:0] (bits 3:0) to 8 */
349629b7e43cSMartin Michlmayr reg |= 0x8;
349729b7e43cSMartin Michlmayr reg &= ~(0x1 << 14); /* TXAMP[4] (bit 14) to 0 */
349829b7e43cSMartin Michlmayr writel(reg, port_mmio + PHY_MODE9_GEN1);
349929b7e43cSMartin Michlmayr }
350029b7e43cSMartin Michlmayr
3501f3a23c2cSLee Jones /*
350229b7e43cSMartin Michlmayr * soc_is_65 - check if the soc is 65 nano device
350329b7e43cSMartin Michlmayr *
350429b7e43cSMartin Michlmayr * Detect the type of the SoC, this is done by reading the PHYCFG_OFS
350529b7e43cSMartin Michlmayr * register, this register should contain non-zero value and it exists only
350629b7e43cSMartin Michlmayr * in the 65 nano devices, when reading it from older devices we get 0.
350729b7e43cSMartin Michlmayr */
soc_is_65n(struct mv_host_priv * hpriv)350829b7e43cSMartin Michlmayr static bool soc_is_65n(struct mv_host_priv *hpriv)
350929b7e43cSMartin Michlmayr {
351029b7e43cSMartin Michlmayr void __iomem *port0_mmio = mv_port_base(hpriv->base, 0);
351129b7e43cSMartin Michlmayr
351229b7e43cSMartin Michlmayr if (readl(port0_mmio + PHYCFG_OFS))
351329b7e43cSMartin Michlmayr return true;
351429b7e43cSMartin Michlmayr return false;
351529b7e43cSMartin Michlmayr }
351629b7e43cSMartin Michlmayr
mv_setup_ifcfg(void __iomem * port_mmio,int want_gen2i)35178e7decdbSMark Lord static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
3518b67a1064SMark Lord {
3519cae5a29dSMark Lord u32 ifcfg = readl(port_mmio + SATA_IFCFG);
3520b67a1064SMark Lord
35218e7decdbSMark Lord ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */
3522b67a1064SMark Lord if (want_gen2i)
35238e7decdbSMark Lord ifcfg |= (1 << 7); /* enable gen2i speed */
3524cae5a29dSMark Lord writelfl(ifcfg, port_mmio + SATA_IFCFG);
3525b67a1064SMark Lord }
3526b67a1064SMark Lord
mv_reset_channel(struct mv_host_priv * hpriv,void __iomem * mmio,unsigned int port_no)3527e12bef50SMark Lord static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
3528c6fd2807SJeff Garzik unsigned int port_no)
3529c6fd2807SJeff Garzik {
3530c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port_no);
3531c6fd2807SJeff Garzik
35328e7decdbSMark Lord /*
35338e7decdbSMark Lord * The datasheet warns against setting EDMA_RESET when EDMA is active
35348e7decdbSMark Lord * (but doesn't say what the problem might be). So we first try
35358e7decdbSMark Lord * to disable the EDMA engine before doing the EDMA_RESET operation.
35368e7decdbSMark Lord */
35370d8be5cbSMark Lord mv_stop_edma_engine(port_mmio);
3538cae5a29dSMark Lord writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3539c6fd2807SJeff Garzik
3540b67a1064SMark Lord if (!IS_GEN_I(hpriv)) {
35418e7decdbSMark Lord /* Enable 3.0gb/s link speed: this survives EDMA_RESET */
35428e7decdbSMark Lord mv_setup_ifcfg(port_mmio, 1);
3543c6fd2807SJeff Garzik }
3544b67a1064SMark Lord /*
35458e7decdbSMark Lord * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
3546b67a1064SMark Lord * link, and physical layers. It resets all SATA interface registers
3547cae5a29dSMark Lord * (except for SATA_IFCFG), and issues a COMRESET to the dev.
3548c6fd2807SJeff Garzik */
3549cae5a29dSMark Lord writelfl(EDMA_RESET, port_mmio + EDMA_CMD);
3550b67a1064SMark Lord udelay(25); /* allow reset propagation */
3551cae5a29dSMark Lord writelfl(0, port_mmio + EDMA_CMD);
3552c6fd2807SJeff Garzik
3553c6fd2807SJeff Garzik hpriv->ops->phy_errata(hpriv, mmio, port_no);
3554c6fd2807SJeff Garzik
3555ee9ccdf7SJeff Garzik if (IS_GEN_I(hpriv))
3556e72685dbSJia-Ju Bai usleep_range(500, 1000);
3557c6fd2807SJeff Garzik }
3558c6fd2807SJeff Garzik
mv_pmp_select(struct ata_port * ap,int pmp)3559e49856d8SMark Lord static void mv_pmp_select(struct ata_port *ap, int pmp)
3560e49856d8SMark Lord {
3561e49856d8SMark Lord if (sata_pmp_supported(ap)) {
3562e49856d8SMark Lord void __iomem *port_mmio = mv_ap_base(ap);
3563cae5a29dSMark Lord u32 reg = readl(port_mmio + SATA_IFCTL);
3564e49856d8SMark Lord int old = reg & 0xf;
3565e49856d8SMark Lord
3566e49856d8SMark Lord if (old != pmp) {
3567e49856d8SMark Lord reg = (reg & ~0xf) | pmp;
3568cae5a29dSMark Lord writelfl(reg, port_mmio + SATA_IFCTL);
3569e49856d8SMark Lord }
3570e49856d8SMark Lord }
3571e49856d8SMark Lord }
3572e49856d8SMark Lord
mv_pmp_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)3573e49856d8SMark Lord static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
3574bdd4dddeSJeff Garzik unsigned long deadline)
3575c6fd2807SJeff Garzik {
3576e49856d8SMark Lord mv_pmp_select(link->ap, sata_srst_pmp(link));
3577e49856d8SMark Lord return sata_std_hardreset(link, class, deadline);
3578e49856d8SMark Lord }
3579c6fd2807SJeff Garzik
mv_softreset(struct ata_link * link,unsigned int * class,unsigned long deadline)3580e49856d8SMark Lord static int mv_softreset(struct ata_link *link, unsigned int *class,
3581e49856d8SMark Lord unsigned long deadline)
3582da3dbb17STejun Heo {
3583e49856d8SMark Lord mv_pmp_select(link->ap, sata_srst_pmp(link));
3584e49856d8SMark Lord return ata_sff_softreset(link, class, deadline);
3585bdd4dddeSJeff Garzik }
3586bdd4dddeSJeff Garzik
mv_hardreset(struct ata_link * link,unsigned int * class,unsigned long deadline)3587cc0680a5STejun Heo static int mv_hardreset(struct ata_link *link, unsigned int *class,
3588bdd4dddeSJeff Garzik unsigned long deadline)
3589bdd4dddeSJeff Garzik {
3590cc0680a5STejun Heo struct ata_port *ap = link->ap;
3591bdd4dddeSJeff Garzik struct mv_host_priv *hpriv = ap->host->private_data;
3592b562468cSMark Lord struct mv_port_priv *pp = ap->private_data;
3593f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base;
35940d8be5cbSMark Lord int rc, attempts = 0, extra = 0;
35950d8be5cbSMark Lord u32 sstatus;
35960d8be5cbSMark Lord bool online;
3597bdd4dddeSJeff Garzik
3598e12bef50SMark Lord mv_reset_channel(hpriv, mmio, ap->port_no);
3599b562468cSMark Lord pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
3600d16ab3f6SMark Lord pp->pp_flags &=
3601d16ab3f6SMark Lord ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
3602bdd4dddeSJeff Garzik
36030d8be5cbSMark Lord /* Workaround for errata FEr SATA#10 (part 2) */
36040d8be5cbSMark Lord do {
3605d14d41ccSSergey Shtylyov const unsigned int *timing =
360617c5aab5SMark Lord sata_ehc_deb_timing(&link->eh_context);
3607bdd4dddeSJeff Garzik
360817c5aab5SMark Lord rc = sata_link_hardreset(link, timing, deadline + extra,
360917c5aab5SMark Lord &online, NULL);
36109dcffd99SMark Lord rc = online ? -EAGAIN : rc;
361117c5aab5SMark Lord if (rc)
36120d8be5cbSMark Lord return rc;
36130d8be5cbSMark Lord sata_scr_read(link, SCR_STATUS, &sstatus);
36140d8be5cbSMark Lord if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
36150d8be5cbSMark Lord /* Force 1.5gb/s link speed and try again */
36168e7decdbSMark Lord mv_setup_ifcfg(mv_ap_base(ap), 0);
36170d8be5cbSMark Lord if (time_after(jiffies + HZ, deadline))
36180d8be5cbSMark Lord extra = HZ; /* only extend it once, max */
3619bdd4dddeSJeff Garzik }
36200d8be5cbSMark Lord } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
362108da1759SMark Lord mv_save_cached_regs(ap);
362266e57a2cSMark Lord mv_edma_cfg(ap, 0, 0);
3623bdd4dddeSJeff Garzik
362417c5aab5SMark Lord return rc;
3625bdd4dddeSJeff Garzik }
3626bdd4dddeSJeff Garzik
mv_eh_freeze(struct ata_port * ap)3627bdd4dddeSJeff Garzik static void mv_eh_freeze(struct ata_port *ap)
3628c6fd2807SJeff Garzik {
36291cfd19aeSMark Lord mv_stop_edma(ap);
3630c4de573bSMark Lord mv_enable_port_irqs(ap, 0);
3631c6fd2807SJeff Garzik }
3632bdd4dddeSJeff Garzik
mv_eh_thaw(struct ata_port * ap)3633bdd4dddeSJeff Garzik static void mv_eh_thaw(struct ata_port *ap)
3634bdd4dddeSJeff Garzik {
3635f351b2d6SSaeed Bishara struct mv_host_priv *hpriv = ap->host->private_data;
3636c4de573bSMark Lord unsigned int port = ap->port_no;
3637c4de573bSMark Lord unsigned int hardport = mv_hardport_from_port(port);
36381cfd19aeSMark Lord void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
3639bdd4dddeSJeff Garzik void __iomem *port_mmio = mv_ap_base(ap);
3640c4de573bSMark Lord u32 hc_irq_cause;
3641bdd4dddeSJeff Garzik
3642bdd4dddeSJeff Garzik /* clear EDMA errors on this port */
3643cae5a29dSMark Lord writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3644bdd4dddeSJeff Garzik
3645bdd4dddeSJeff Garzik /* clear pending irq events */
3646cae6edc3SMark Lord hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
3647cae5a29dSMark Lord writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE);
3648bdd4dddeSJeff Garzik
364988e675e1SMark Lord mv_enable_port_irqs(ap, ERR_IRQ);
3650c6fd2807SJeff Garzik }
3651c6fd2807SJeff Garzik
3652c6fd2807SJeff Garzik /**
3653c6fd2807SJeff Garzik * mv_port_init - Perform some early initialization on a single port.
3654c6fd2807SJeff Garzik * @port: libata data structure storing shadow register addresses
3655c6fd2807SJeff Garzik * @port_mmio: base address of the port
3656c6fd2807SJeff Garzik *
3657c6fd2807SJeff Garzik * Initialize shadow register mmio addresses, clear outstanding
3658c6fd2807SJeff Garzik * interrupts on the port, and unmask interrupts for the future
3659c6fd2807SJeff Garzik * start of the port.
3660c6fd2807SJeff Garzik *
3661c6fd2807SJeff Garzik * LOCKING:
3662c6fd2807SJeff Garzik * Inherited from caller.
3663c6fd2807SJeff Garzik */
mv_port_init(struct ata_ioports * port,void __iomem * port_mmio)3664c6fd2807SJeff Garzik static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
3665c6fd2807SJeff Garzik {
3666cae5a29dSMark Lord void __iomem *serr, *shd_base = port_mmio + SHD_BLK;
3667c6fd2807SJeff Garzik
3668c6fd2807SJeff Garzik /* PIO related setup
3669c6fd2807SJeff Garzik */
3670c6fd2807SJeff Garzik port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
3671c6fd2807SJeff Garzik port->error_addr =
3672c6fd2807SJeff Garzik port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
3673c6fd2807SJeff Garzik port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
3674c6fd2807SJeff Garzik port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
3675c6fd2807SJeff Garzik port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
3676c6fd2807SJeff Garzik port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
3677c6fd2807SJeff Garzik port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
3678c6fd2807SJeff Garzik port->status_addr =
3679c6fd2807SJeff Garzik port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
3680c6fd2807SJeff Garzik /* special case: control/altstatus doesn't have ATA_REG_ address */
3681cae5a29dSMark Lord port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST;
3682c6fd2807SJeff Garzik
3683c6fd2807SJeff Garzik /* Clear any currently outstanding port interrupt conditions */
3684cae5a29dSMark Lord serr = port_mmio + mv_scr_offset(SCR_ERROR);
3685cae5a29dSMark Lord writelfl(readl(serr), serr);
3686cae5a29dSMark Lord writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE);
3687c6fd2807SJeff Garzik
3688646a4da5SMark Lord /* unmask all non-transient EDMA error interrupts */
3689cae5a29dSMark Lord writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK);
3690c6fd2807SJeff Garzik }
3691c6fd2807SJeff Garzik
mv_in_pcix_mode(struct ata_host * host)3692616d4a98SMark Lord static unsigned int mv_in_pcix_mode(struct ata_host *host)
3693616d4a98SMark Lord {
3694616d4a98SMark Lord struct mv_host_priv *hpriv = host->private_data;
3695616d4a98SMark Lord void __iomem *mmio = hpriv->base;
3696616d4a98SMark Lord u32 reg;
3697616d4a98SMark Lord
36981f398472SMark Lord if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
3699616d4a98SMark Lord return 0; /* not PCI-X capable */
3700cae5a29dSMark Lord reg = readl(mmio + MV_PCI_MODE);
3701616d4a98SMark Lord if ((reg & MV_PCI_MODE_MASK) == 0)
3702616d4a98SMark Lord return 0; /* conventional PCI mode */
3703616d4a98SMark Lord return 1; /* chip is in PCI-X mode */
3704616d4a98SMark Lord }
3705616d4a98SMark Lord
mv_pci_cut_through_okay(struct ata_host * host)3706616d4a98SMark Lord static int mv_pci_cut_through_okay(struct ata_host *host)
3707616d4a98SMark Lord {
3708616d4a98SMark Lord struct mv_host_priv *hpriv = host->private_data;
3709616d4a98SMark Lord void __iomem *mmio = hpriv->base;
3710616d4a98SMark Lord u32 reg;
3711616d4a98SMark Lord
3712616d4a98SMark Lord if (!mv_in_pcix_mode(host)) {
3713cae5a29dSMark Lord reg = readl(mmio + MV_PCI_COMMAND);
3714cae5a29dSMark Lord if (reg & MV_PCI_COMMAND_MRDTRIG)
3715616d4a98SMark Lord return 0; /* not okay */
3716616d4a98SMark Lord }
3717616d4a98SMark Lord return 1; /* okay */
3718616d4a98SMark Lord }
3719616d4a98SMark Lord
mv_60x1b2_errata_pci7(struct ata_host * host)372065ad7fefSMark Lord static void mv_60x1b2_errata_pci7(struct ata_host *host)
372165ad7fefSMark Lord {
372265ad7fefSMark Lord struct mv_host_priv *hpriv = host->private_data;
372365ad7fefSMark Lord void __iomem *mmio = hpriv->base;
372465ad7fefSMark Lord
372565ad7fefSMark Lord /* workaround for 60x1-B2 errata PCI#7 */
372665ad7fefSMark Lord if (mv_in_pcix_mode(host)) {
3727cae5a29dSMark Lord u32 reg = readl(mmio + MV_PCI_COMMAND);
3728cae5a29dSMark Lord writelfl(reg & ~MV_PCI_COMMAND_MWRCOM, mmio + MV_PCI_COMMAND);
372965ad7fefSMark Lord }
373065ad7fefSMark Lord }
373165ad7fefSMark Lord
mv_chip_id(struct ata_host * host,unsigned int board_idx)37324447d351STejun Heo static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
3733c6fd2807SJeff Garzik {
37344447d351STejun Heo struct pci_dev *pdev = to_pci_dev(host->dev);
37354447d351STejun Heo struct mv_host_priv *hpriv = host->private_data;
3736c6fd2807SJeff Garzik u32 hp_flags = hpriv->hp_flags;
3737c6fd2807SJeff Garzik
3738c6fd2807SJeff Garzik switch (board_idx) {
3739c6fd2807SJeff Garzik case chip_5080:
3740c6fd2807SJeff Garzik hpriv->ops = &mv5xxx_ops;
3741ee9ccdf7SJeff Garzik hp_flags |= MV_HP_GEN_I;
3742c6fd2807SJeff Garzik
374344c10138SAuke Kok switch (pdev->revision) {
3744c6fd2807SJeff Garzik case 0x1:
3745c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB0;
3746c6fd2807SJeff Garzik break;
3747c6fd2807SJeff Garzik case 0x3:
3748c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2;
3749c6fd2807SJeff Garzik break;
3750c6fd2807SJeff Garzik default:
3751a44fec1fSJoe Perches dev_warn(&pdev->dev,
3752c6fd2807SJeff Garzik "Applying 50XXB2 workarounds to unknown rev\n");
3753c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2;
3754c6fd2807SJeff Garzik break;
3755c6fd2807SJeff Garzik }
3756c6fd2807SJeff Garzik break;
3757c6fd2807SJeff Garzik
3758c6fd2807SJeff Garzik case chip_504x:
3759c6fd2807SJeff Garzik case chip_508x:
3760c6fd2807SJeff Garzik hpriv->ops = &mv5xxx_ops;
3761ee9ccdf7SJeff Garzik hp_flags |= MV_HP_GEN_I;
3762c6fd2807SJeff Garzik
376344c10138SAuke Kok switch (pdev->revision) {
3764c6fd2807SJeff Garzik case 0x0:
3765c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB0;
3766c6fd2807SJeff Garzik break;
3767c6fd2807SJeff Garzik case 0x3:
3768c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2;
3769c6fd2807SJeff Garzik break;
3770c6fd2807SJeff Garzik default:
3771a44fec1fSJoe Perches dev_warn(&pdev->dev,
3772c6fd2807SJeff Garzik "Applying B2 workarounds to unknown rev\n");
3773c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_50XXB2;
3774c6fd2807SJeff Garzik break;
3775c6fd2807SJeff Garzik }
3776c6fd2807SJeff Garzik break;
3777c6fd2807SJeff Garzik
3778c6fd2807SJeff Garzik case chip_604x:
3779c6fd2807SJeff Garzik case chip_608x:
3780c6fd2807SJeff Garzik hpriv->ops = &mv6xxx_ops;
3781ee9ccdf7SJeff Garzik hp_flags |= MV_HP_GEN_II;
3782c6fd2807SJeff Garzik
378344c10138SAuke Kok switch (pdev->revision) {
3784c6fd2807SJeff Garzik case 0x7:
378565ad7fefSMark Lord mv_60x1b2_errata_pci7(host);
3786c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1B2;
3787c6fd2807SJeff Garzik break;
3788c6fd2807SJeff Garzik case 0x9:
3789c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1C0;
3790c6fd2807SJeff Garzik break;
3791c6fd2807SJeff Garzik default:
3792a44fec1fSJoe Perches dev_warn(&pdev->dev,
3793c6fd2807SJeff Garzik "Applying B2 workarounds to unknown rev\n");
3794c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1B2;
3795c6fd2807SJeff Garzik break;
3796c6fd2807SJeff Garzik }
3797c6fd2807SJeff Garzik break;
3798c6fd2807SJeff Garzik
3799c6fd2807SJeff Garzik case chip_7042:
3800616d4a98SMark Lord hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
3801306b30f7SMark Lord if (pdev->vendor == PCI_VENDOR_ID_TTI &&
3802306b30f7SMark Lord (pdev->device == 0x2300 || pdev->device == 0x2310))
3803306b30f7SMark Lord {
38044e520033SMark Lord /*
38054e520033SMark Lord * Highpoint RocketRAID PCIe 23xx series cards:
38064e520033SMark Lord *
38074e520033SMark Lord * Unconfigured drives are treated as "Legacy"
38084e520033SMark Lord * by the BIOS, and it overwrites sector 8 with
38094e520033SMark Lord * a "Lgcy" metadata block prior to Linux boot.
38104e520033SMark Lord *
38114e520033SMark Lord * Configured drives (RAID or JBOD) leave sector 8
38124e520033SMark Lord * alone, but instead overwrite a high numbered
38134e520033SMark Lord * sector for the RAID metadata. This sector can
38144e520033SMark Lord * be determined exactly, by truncating the physical
38154e520033SMark Lord * drive capacity to a nice even GB value.
38164e520033SMark Lord *
38174e520033SMark Lord * RAID metadata is at: (dev->n_sectors & ~0xfffff)
38184e520033SMark Lord *
38194e520033SMark Lord * Warn the user, lest they think we're just buggy.
38204e520033SMark Lord */
3821f76ba003SHannes Reinecke dev_warn(&pdev->dev, "Highpoint RocketRAID"
38224e520033SMark Lord " BIOS CORRUPTS DATA on all attached drives,"
38234e520033SMark Lord " regardless of if/how they are configured."
38244e520033SMark Lord " BEWARE!\n");
3825f76ba003SHannes Reinecke dev_warn(&pdev->dev, "For data safety, do not"
38264e520033SMark Lord " use sectors 8-9 on \"Legacy\" drives,"
38274e520033SMark Lord " and avoid the final two gigabytes on"
38284e520033SMark Lord " all RocketRAID BIOS initialized drives.\n");
3829306b30f7SMark Lord }
3830df561f66SGustavo A. R. Silva fallthrough;
3831c6fd2807SJeff Garzik case chip_6042:
3832c6fd2807SJeff Garzik hpriv->ops = &mv6xxx_ops;
3833c6fd2807SJeff Garzik hp_flags |= MV_HP_GEN_IIE;
3834616d4a98SMark Lord if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
3835616d4a98SMark Lord hp_flags |= MV_HP_CUT_THROUGH;
3836c6fd2807SJeff Garzik
383744c10138SAuke Kok switch (pdev->revision) {
38385cf73bfbSMark Lord case 0x2: /* Rev.B0: the first/only public release */
3839c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1C0;
3840c6fd2807SJeff Garzik break;
3841c6fd2807SJeff Garzik default:
3842a44fec1fSJoe Perches dev_warn(&pdev->dev,
3843c6fd2807SJeff Garzik "Applying 60X1C0 workarounds to unknown rev\n");
3844c6fd2807SJeff Garzik hp_flags |= MV_HP_ERRATA_60X1C0;
3845c6fd2807SJeff Garzik break;
3846c6fd2807SJeff Garzik }
3847c6fd2807SJeff Garzik break;
3848f351b2d6SSaeed Bishara case chip_soc:
384929b7e43cSMartin Michlmayr if (soc_is_65n(hpriv))
385029b7e43cSMartin Michlmayr hpriv->ops = &mv_soc_65n_ops;
385129b7e43cSMartin Michlmayr else
3852f351b2d6SSaeed Bishara hpriv->ops = &mv_soc_ops;
3853eb3a55a9SSaeed Bishara hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3854eb3a55a9SSaeed Bishara MV_HP_ERRATA_60X1C0;
3855f351b2d6SSaeed Bishara break;
3856c6fd2807SJeff Garzik
3857c6fd2807SJeff Garzik default:
3858a0023bb9SZheyu Ma dev_alert(host->dev, "BUG: invalid board index %u\n", board_idx);
3859a0023bb9SZheyu Ma return -EINVAL;
3860c6fd2807SJeff Garzik }
3861c6fd2807SJeff Garzik
3862c6fd2807SJeff Garzik hpriv->hp_flags = hp_flags;
386302a121daSMark Lord if (hp_flags & MV_HP_PCIE) {
3864cae5a29dSMark Lord hpriv->irq_cause_offset = PCIE_IRQ_CAUSE;
3865cae5a29dSMark Lord hpriv->irq_mask_offset = PCIE_IRQ_MASK;
386602a121daSMark Lord hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
386702a121daSMark Lord } else {
3868cae5a29dSMark Lord hpriv->irq_cause_offset = PCI_IRQ_CAUSE;
3869cae5a29dSMark Lord hpriv->irq_mask_offset = PCI_IRQ_MASK;
387002a121daSMark Lord hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
387102a121daSMark Lord }
3872c6fd2807SJeff Garzik
3873c6fd2807SJeff Garzik return 0;
3874c6fd2807SJeff Garzik }
3875c6fd2807SJeff Garzik
3876c6fd2807SJeff Garzik /**
3877c6fd2807SJeff Garzik * mv_init_host - Perform some early initialization of the host.
38784447d351STejun Heo * @host: ATA host to initialize
3879c6fd2807SJeff Garzik *
3880c6fd2807SJeff Garzik * If possible, do an early global reset of the host. Then do
3881c6fd2807SJeff Garzik * our port init and clear/unmask all/relevant host interrupts.
3882c6fd2807SJeff Garzik *
3883c6fd2807SJeff Garzik * LOCKING:
3884c6fd2807SJeff Garzik * Inherited from caller.
3885c6fd2807SJeff Garzik */
mv_init_host(struct ata_host * host)38861bfeff03SSaeed Bishara static int mv_init_host(struct ata_host *host)
3887c6fd2807SJeff Garzik {
3888c6fd2807SJeff Garzik int rc = 0, n_hc, port, hc;
38894447d351STejun Heo struct mv_host_priv *hpriv = host->private_data;
3890f351b2d6SSaeed Bishara void __iomem *mmio = hpriv->base;
3891c6fd2807SJeff Garzik
38921bfeff03SSaeed Bishara rc = mv_chip_id(host, hpriv->board_idx);
3893c6fd2807SJeff Garzik if (rc)
3894c6fd2807SJeff Garzik goto done;
3895c6fd2807SJeff Garzik
38961f398472SMark Lord if (IS_SOC(hpriv)) {
3897cae5a29dSMark Lord hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE;
3898cae5a29dSMark Lord hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK;
38991f398472SMark Lord } else {
3900cae5a29dSMark Lord hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE;
3901cae5a29dSMark Lord hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK;
3902f351b2d6SSaeed Bishara }
3903352fab70SMark Lord
39045d0fb2e7SThomas Reitmayr /* initialize shadow irq mask with register's value */
39055d0fb2e7SThomas Reitmayr hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
39065d0fb2e7SThomas Reitmayr
3907352fab70SMark Lord /* global interrupt mask: 0 == mask everything */
3908c4de573bSMark Lord mv_set_main_irq_mask(host, ~0, 0);
3909f351b2d6SSaeed Bishara
39104447d351STejun Heo n_hc = mv_get_hc_count(host->ports[0]->flags);
3911c6fd2807SJeff Garzik
39124447d351STejun Heo for (port = 0; port < host->n_ports; port++)
391329b7e43cSMartin Michlmayr if (hpriv->ops->read_preamp)
3914c6fd2807SJeff Garzik hpriv->ops->read_preamp(hpriv, port, mmio);
3915c6fd2807SJeff Garzik
3916f76ba003SHannes Reinecke rc = hpriv->ops->reset_hc(host, mmio, n_hc);
3917c6fd2807SJeff Garzik if (rc)
3918c6fd2807SJeff Garzik goto done;
3919c6fd2807SJeff Garzik
3920c6fd2807SJeff Garzik hpriv->ops->reset_flash(hpriv, mmio);
39217bb3c529SSaeed Bishara hpriv->ops->reset_bus(host, mmio);
3922c6fd2807SJeff Garzik hpriv->ops->enable_leds(hpriv, mmio);
3923c6fd2807SJeff Garzik
39244447d351STejun Heo for (port = 0; port < host->n_ports; port++) {
3925cbcdd875STejun Heo struct ata_port *ap = host->ports[port];
3926c6fd2807SJeff Garzik void __iomem *port_mmio = mv_port_base(mmio, port);
3927cbcdd875STejun Heo
3928cbcdd875STejun Heo mv_port_init(&ap->ioaddr, port_mmio);
3929c6fd2807SJeff Garzik }
3930c6fd2807SJeff Garzik
3931c6fd2807SJeff Garzik for (hc = 0; hc < n_hc; hc++) {
3932c6fd2807SJeff Garzik void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3933c6fd2807SJeff Garzik
393423b87b9fSHannes Reinecke dev_dbg(host->dev, "HC%i: HC config=0x%08x HC IRQ cause "
3935c6fd2807SJeff Garzik "(before clear)=0x%08x\n", hc,
3936cae5a29dSMark Lord readl(hc_mmio + HC_CFG),
3937cae5a29dSMark Lord readl(hc_mmio + HC_IRQ_CAUSE));
3938c6fd2807SJeff Garzik
3939c6fd2807SJeff Garzik /* Clear any currently outstanding hc interrupt conditions */
3940cae5a29dSMark Lord writelfl(0, hc_mmio + HC_IRQ_CAUSE);
3941c6fd2807SJeff Garzik }
3942c6fd2807SJeff Garzik
394344c65d16SMark Lord if (!IS_SOC(hpriv)) {
3944c6fd2807SJeff Garzik /* Clear any currently outstanding host interrupt conditions */
3945cae5a29dSMark Lord writelfl(0, mmio + hpriv->irq_cause_offset);
3946c6fd2807SJeff Garzik
3947c6fd2807SJeff Garzik /* and unmask interrupt generation for host regs */
3948cae5a29dSMark Lord writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_offset);
394944c65d16SMark Lord }
3950c6fd2807SJeff Garzik
395151de32d2SMark Lord /*
395251de32d2SMark Lord * enable only global host interrupts for now.
395351de32d2SMark Lord * The per-port interrupts get done later as ports are set up.
395451de32d2SMark Lord */
3955c4de573bSMark Lord mv_set_main_irq_mask(host, 0, PCI_ERR);
39562b748a0aSMark Lord mv_set_irq_coalescing(host, irq_coalescing_io_count,
39572b748a0aSMark Lord irq_coalescing_usecs);
3958c6fd2807SJeff Garzik done:
3959c6fd2807SJeff Garzik return rc;
3960c6fd2807SJeff Garzik }
3961c6fd2807SJeff Garzik
mv_create_dma_pools(struct mv_host_priv * hpriv,struct device * dev)3962fbf14e2fSByron Bradley static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
3963fbf14e2fSByron Bradley {
3964fbf14e2fSByron Bradley hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
3965fbf14e2fSByron Bradley MV_CRQB_Q_SZ, 0);
3966fbf14e2fSByron Bradley if (!hpriv->crqb_pool)
3967fbf14e2fSByron Bradley return -ENOMEM;
3968fbf14e2fSByron Bradley
3969fbf14e2fSByron Bradley hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
3970fbf14e2fSByron Bradley MV_CRPB_Q_SZ, 0);
3971fbf14e2fSByron Bradley if (!hpriv->crpb_pool)
3972fbf14e2fSByron Bradley return -ENOMEM;
3973fbf14e2fSByron Bradley
3974fbf14e2fSByron Bradley hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
3975fbf14e2fSByron Bradley MV_SG_TBL_SZ, 0);
3976fbf14e2fSByron Bradley if (!hpriv->sg_tbl_pool)
3977fbf14e2fSByron Bradley return -ENOMEM;
3978fbf14e2fSByron Bradley
3979fbf14e2fSByron Bradley return 0;
3980fbf14e2fSByron Bradley }
3981fbf14e2fSByron Bradley
mv_conf_mbus_windows(struct mv_host_priv * hpriv,const struct mbus_dram_target_info * dram)398215a32632SLennert Buytenhek static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
398363a9332bSAndrew Lunn const struct mbus_dram_target_info *dram)
398415a32632SLennert Buytenhek {
398515a32632SLennert Buytenhek int i;
398615a32632SLennert Buytenhek
398715a32632SLennert Buytenhek for (i = 0; i < 4; i++) {
398815a32632SLennert Buytenhek writel(0, hpriv->base + WINDOW_CTRL(i));
398915a32632SLennert Buytenhek writel(0, hpriv->base + WINDOW_BASE(i));
399015a32632SLennert Buytenhek }
399115a32632SLennert Buytenhek
399215a32632SLennert Buytenhek for (i = 0; i < dram->num_cs; i++) {
399363a9332bSAndrew Lunn const struct mbus_dram_window *cs = dram->cs + i;
399415a32632SLennert Buytenhek
399515a32632SLennert Buytenhek writel(((cs->size - 1) & 0xffff0000) |
399615a32632SLennert Buytenhek (cs->mbus_attr << 8) |
399715a32632SLennert Buytenhek (dram->mbus_dram_target_id << 4) | 1,
399815a32632SLennert Buytenhek hpriv->base + WINDOW_CTRL(i));
399915a32632SLennert Buytenhek writel(cs->base, hpriv->base + WINDOW_BASE(i));
400015a32632SLennert Buytenhek }
400115a32632SLennert Buytenhek }
400215a32632SLennert Buytenhek
4003f351b2d6SSaeed Bishara /**
4004f351b2d6SSaeed Bishara * mv_platform_probe - handle a positive probe of an soc Marvell
4005f351b2d6SSaeed Bishara * host
4006f351b2d6SSaeed Bishara * @pdev: platform device found
4007f351b2d6SSaeed Bishara *
4008f351b2d6SSaeed Bishara * LOCKING:
4009f351b2d6SSaeed Bishara * Inherited from caller.
4010f351b2d6SSaeed Bishara */
mv_platform_probe(struct platform_device * pdev)4011f351b2d6SSaeed Bishara static int mv_platform_probe(struct platform_device *pdev)
4012f351b2d6SSaeed Bishara {
4013f351b2d6SSaeed Bishara const struct mv_sata_platform_data *mv_platform_data;
401463a9332bSAndrew Lunn const struct mbus_dram_target_info *dram;
4015f351b2d6SSaeed Bishara const struct ata_port_info *ppi[] =
4016f351b2d6SSaeed Bishara { &mv_port_info[chip_soc], NULL };
4017f351b2d6SSaeed Bishara struct ata_host *host;
4018f351b2d6SSaeed Bishara struct mv_host_priv *hpriv;
4019f351b2d6SSaeed Bishara struct resource *res;
402097b414e1SAndrew Lunn int n_ports = 0, irq = 0;
402199b80e97SDan Carpenter int rc;
4022eee98990SAndrew Lunn int port;
4023f351b2d6SSaeed Bishara
402406296a1eSJoe Perches ata_print_version_once(&pdev->dev, DRV_VERSION);
4025f351b2d6SSaeed Bishara
4026f351b2d6SSaeed Bishara /*
4027f351b2d6SSaeed Bishara * Simple resource validation ..
4028f351b2d6SSaeed Bishara */
4029b3b2bec9SAndrew Lunn if (unlikely(pdev->num_resources != 1)) {
4030f351b2d6SSaeed Bishara dev_err(&pdev->dev, "invalid number of resources\n");
4031f351b2d6SSaeed Bishara return -EINVAL;
4032f351b2d6SSaeed Bishara }
4033f351b2d6SSaeed Bishara
4034f351b2d6SSaeed Bishara /*
4035f351b2d6SSaeed Bishara * Get the register base first
4036f351b2d6SSaeed Bishara */
4037f351b2d6SSaeed Bishara res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
40383e4240daSAndrew Lunn if (res == NULL)
40393e4240daSAndrew Lunn return -EINVAL;
4040f351b2d6SSaeed Bishara
4041f351b2d6SSaeed Bishara /* allocate host */
404297b414e1SAndrew Lunn if (pdev->dev.of_node) {
40435c3ef397SUwe Kleine-König rc = of_property_read_u32(pdev->dev.of_node, "nr-ports",
40445c3ef397SUwe Kleine-König &n_ports);
40455c3ef397SUwe Kleine-König if (rc) {
40465c3ef397SUwe Kleine-König dev_err(&pdev->dev,
40475c3ef397SUwe Kleine-König "error parsing nr-ports property: %d\n", rc);
40485c3ef397SUwe Kleine-König return rc;
40495c3ef397SUwe Kleine-König }
40505c3ef397SUwe Kleine-König
40515c3ef397SUwe Kleine-König if (n_ports <= 0) {
40525c3ef397SUwe Kleine-König dev_err(&pdev->dev, "nr-ports must be positive: %d\n",
40535c3ef397SUwe Kleine-König n_ports);
40545c3ef397SUwe Kleine-König return -EINVAL;
40555c3ef397SUwe Kleine-König }
40565c3ef397SUwe Kleine-König
405797b414e1SAndrew Lunn irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
405897b414e1SAndrew Lunn } else {
405961b8c345SJingoo Han mv_platform_data = dev_get_platdata(&pdev->dev);
4060f351b2d6SSaeed Bishara n_ports = mv_platform_data->n_ports;
406197b414e1SAndrew Lunn irq = platform_get_irq(pdev, 0);
406297b414e1SAndrew Lunn }
4063e6471a65SSergey Shtylyov if (irq < 0)
4064e6471a65SSergey Shtylyov return irq;
4065e6471a65SSergey Shtylyov if (!irq)
4066e6471a65SSergey Shtylyov return -EINVAL;
4067f351b2d6SSaeed Bishara
4068f351b2d6SSaeed Bishara host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
4069f351b2d6SSaeed Bishara hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
4070f351b2d6SSaeed Bishara
4071f351b2d6SSaeed Bishara if (!host || !hpriv)
4072f351b2d6SSaeed Bishara return -ENOMEM;
4073a86854d0SKees Cook hpriv->port_clks = devm_kcalloc(&pdev->dev,
4074a86854d0SKees Cook n_ports, sizeof(struct clk *),
4075eee98990SAndrew Lunn GFP_KERNEL);
4076eee98990SAndrew Lunn if (!hpriv->port_clks)
4077eee98990SAndrew Lunn return -ENOMEM;
4078a86854d0SKees Cook hpriv->port_phys = devm_kcalloc(&pdev->dev,
4079a86854d0SKees Cook n_ports, sizeof(struct phy *),
4080b7db4f2eSAndrew Lunn GFP_KERNEL);
4081b7db4f2eSAndrew Lunn if (!hpriv->port_phys)
4082b7db4f2eSAndrew Lunn return -ENOMEM;
4083f351b2d6SSaeed Bishara host->private_data = hpriv;
40841bfeff03SSaeed Bishara hpriv->board_idx = chip_soc;
4085f351b2d6SSaeed Bishara
4086f351b2d6SSaeed Bishara host->iomap = NULL;
40873e4240daSAndrew Lunn hpriv->base = devm_ioremap(&pdev->dev, res->start,
40883e4240daSAndrew Lunn resource_size(res));
40893e4240daSAndrew Lunn if (!hpriv->base)
40903e4240daSAndrew Lunn return -ENOMEM;
40913e4240daSAndrew Lunn
40923e4240daSAndrew Lunn hpriv->base -= SATAHC0_REG_BASE;
4093f351b2d6SSaeed Bishara
4094c77a2f4eSSaeed Bishara hpriv->clk = clk_get(&pdev->dev, NULL);
4095c77a2f4eSSaeed Bishara if (IS_ERR(hpriv->clk))
4096eee98990SAndrew Lunn dev_notice(&pdev->dev, "cannot get optional clkdev\n");
4097c77a2f4eSSaeed Bishara else
4098eee98990SAndrew Lunn clk_prepare_enable(hpriv->clk);
4099eee98990SAndrew Lunn
4100eee98990SAndrew Lunn for (port = 0; port < n_ports; port++) {
4101eee98990SAndrew Lunn char port_number[16];
4102eee98990SAndrew Lunn sprintf(port_number, "%d", port);
4103eee98990SAndrew Lunn hpriv->port_clks[port] = clk_get(&pdev->dev, port_number);
4104eee98990SAndrew Lunn if (!IS_ERR(hpriv->port_clks[port]))
4105eee98990SAndrew Lunn clk_prepare_enable(hpriv->port_clks[port]);
4106b7db4f2eSAndrew Lunn
4107b7db4f2eSAndrew Lunn sprintf(port_number, "port%d", port);
410890aa2997SAndrew Lunn hpriv->port_phys[port] = devm_phy_optional_get(&pdev->dev,
410990aa2997SAndrew Lunn port_number);
4110b7db4f2eSAndrew Lunn if (IS_ERR(hpriv->port_phys[port])) {
4111b7db4f2eSAndrew Lunn rc = PTR_ERR(hpriv->port_phys[port]);
4112b7db4f2eSAndrew Lunn hpriv->port_phys[port] = NULL;
411390aa2997SAndrew Lunn if (rc != -EPROBE_DEFER)
411454dfffdeSLinus Torvalds dev_warn(&pdev->dev, "error getting phy %d", rc);
41158ad116e6SEzequiel Garcia
41168ad116e6SEzequiel Garcia /* Cleanup only the initialized ports */
41178ad116e6SEzequiel Garcia hpriv->n_ports = port;
4118b7db4f2eSAndrew Lunn goto err;
4119b7db4f2eSAndrew Lunn } else
4120b7db4f2eSAndrew Lunn phy_power_on(hpriv->port_phys[port]);
4121eee98990SAndrew Lunn }
4122c77a2f4eSSaeed Bishara
41238ad116e6SEzequiel Garcia /* All the ports have been initialized */
41248ad116e6SEzequiel Garcia hpriv->n_ports = n_ports;
41258ad116e6SEzequiel Garcia
412615a32632SLennert Buytenhek /*
412715a32632SLennert Buytenhek * (Re-)program MBUS remapping windows if we are asked to.
412815a32632SLennert Buytenhek */
412963a9332bSAndrew Lunn dram = mv_mbus_dram_info();
413063a9332bSAndrew Lunn if (dram)
413163a9332bSAndrew Lunn mv_conf_mbus_windows(hpriv, dram);
413215a32632SLennert Buytenhek
4133fbf14e2fSByron Bradley rc = mv_create_dma_pools(hpriv, &pdev->dev);
4134fbf14e2fSByron Bradley if (rc)
4135c77a2f4eSSaeed Bishara goto err;
4136fbf14e2fSByron Bradley
41379013d64eSLior Amsalem /*
41389013d64eSLior Amsalem * To allow disk hotplug on Armada 370/XP SoCs, the PHY speed must be
41399013d64eSLior Amsalem * updated in the LP_PHY_CTL register.
41409013d64eSLior Amsalem */
41419013d64eSLior Amsalem if (pdev->dev.of_node &&
41429013d64eSLior Amsalem of_device_is_compatible(pdev->dev.of_node,
41439013d64eSLior Amsalem "marvell,armada-370-sata"))
41449013d64eSLior Amsalem hpriv->hp_flags |= MV_HP_FIX_LP_PHY_CTL;
41459013d64eSLior Amsalem
4146f351b2d6SSaeed Bishara /* initialize adapter */
41471bfeff03SSaeed Bishara rc = mv_init_host(host);
4148f351b2d6SSaeed Bishara if (rc)
4149c77a2f4eSSaeed Bishara goto err;
4150f351b2d6SSaeed Bishara
4151a44fec1fSJoe Perches dev_info(&pdev->dev, "slots %u ports %d\n",
4152a44fec1fSJoe Perches (unsigned)MV_MAX_Q_DEPTH, host->n_ports);
4153f351b2d6SSaeed Bishara
415497b414e1SAndrew Lunn rc = ata_host_activate(host, irq, mv_interrupt, IRQF_SHARED, &mv6_sht);
4155c00a4c9dSSergei Shtylyov if (!rc)
4156c00a4c9dSSergei Shtylyov return 0;
4157c00a4c9dSSergei Shtylyov
4158c77a2f4eSSaeed Bishara err:
4159c77a2f4eSSaeed Bishara if (!IS_ERR(hpriv->clk)) {
4160eee98990SAndrew Lunn clk_disable_unprepare(hpriv->clk);
4161c77a2f4eSSaeed Bishara clk_put(hpriv->clk);
4162c77a2f4eSSaeed Bishara }
41638ad116e6SEzequiel Garcia for (port = 0; port < hpriv->n_ports; port++) {
4164eee98990SAndrew Lunn if (!IS_ERR(hpriv->port_clks[port])) {
4165eee98990SAndrew Lunn clk_disable_unprepare(hpriv->port_clks[port]);
4166eee98990SAndrew Lunn clk_put(hpriv->port_clks[port]);
4167eee98990SAndrew Lunn }
4168b7db4f2eSAndrew Lunn phy_power_off(hpriv->port_phys[port]);
4169eee98990SAndrew Lunn }
4170c77a2f4eSSaeed Bishara
4171c77a2f4eSSaeed Bishara return rc;
4172f351b2d6SSaeed Bishara }
4173f351b2d6SSaeed Bishara
4174f351b2d6SSaeed Bishara /*
4175f351b2d6SSaeed Bishara *
4176f351b2d6SSaeed Bishara * mv_platform_remove - unplug a platform interface
4177f351b2d6SSaeed Bishara * @pdev: platform device
4178f351b2d6SSaeed Bishara *
4179f351b2d6SSaeed Bishara * A platform bus SATA device has been unplugged. Perform the needed
4180f351b2d6SSaeed Bishara * cleanup. Also called on module unload for any active devices.
4181f351b2d6SSaeed Bishara */
mv_platform_remove(struct platform_device * pdev)41823596b025SUwe Kleine-König static void mv_platform_remove(struct platform_device *pdev)
4183f351b2d6SSaeed Bishara {
4184d8661921SSergei Shtylyov struct ata_host *host = platform_get_drvdata(pdev);
4185c77a2f4eSSaeed Bishara struct mv_host_priv *hpriv = host->private_data;
4186eee98990SAndrew Lunn int port;
4187f351b2d6SSaeed Bishara ata_host_detach(host);
4188c77a2f4eSSaeed Bishara
4189c77a2f4eSSaeed Bishara if (!IS_ERR(hpriv->clk)) {
4190eee98990SAndrew Lunn clk_disable_unprepare(hpriv->clk);
4191c77a2f4eSSaeed Bishara clk_put(hpriv->clk);
4192c77a2f4eSSaeed Bishara }
4193eee98990SAndrew Lunn for (port = 0; port < host->n_ports; port++) {
4194eee98990SAndrew Lunn if (!IS_ERR(hpriv->port_clks[port])) {
4195eee98990SAndrew Lunn clk_disable_unprepare(hpriv->port_clks[port]);
4196eee98990SAndrew Lunn clk_put(hpriv->port_clks[port]);
4197eee98990SAndrew Lunn }
4198b7db4f2eSAndrew Lunn phy_power_off(hpriv->port_phys[port]);
4199eee98990SAndrew Lunn }
4200f351b2d6SSaeed Bishara }
4201f351b2d6SSaeed Bishara
420258eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
mv_platform_suspend(struct platform_device * pdev,pm_message_t state)42036481f2b5SSaeed Bishara static int mv_platform_suspend(struct platform_device *pdev, pm_message_t state)
42046481f2b5SSaeed Bishara {
4205d8661921SSergei Shtylyov struct ata_host *host = platform_get_drvdata(pdev);
4206ec87cf37SSergey Shtylyov
42076481f2b5SSaeed Bishara if (host)
4208ec87cf37SSergey Shtylyov ata_host_suspend(host, state);
42096481f2b5SSaeed Bishara return 0;
42106481f2b5SSaeed Bishara }
42116481f2b5SSaeed Bishara
mv_platform_resume(struct platform_device * pdev)42126481f2b5SSaeed Bishara static int mv_platform_resume(struct platform_device *pdev)
42136481f2b5SSaeed Bishara {
4214d8661921SSergei Shtylyov struct ata_host *host = platform_get_drvdata(pdev);
421563a9332bSAndrew Lunn const struct mbus_dram_target_info *dram;
42166481f2b5SSaeed Bishara int ret;
42176481f2b5SSaeed Bishara
42186481f2b5SSaeed Bishara if (host) {
42196481f2b5SSaeed Bishara struct mv_host_priv *hpriv = host->private_data;
422063a9332bSAndrew Lunn
42216481f2b5SSaeed Bishara /*
42226481f2b5SSaeed Bishara * (Re-)program MBUS remapping windows if we are asked to.
42236481f2b5SSaeed Bishara */
422463a9332bSAndrew Lunn dram = mv_mbus_dram_info();
422563a9332bSAndrew Lunn if (dram)
422663a9332bSAndrew Lunn mv_conf_mbus_windows(hpriv, dram);
42276481f2b5SSaeed Bishara
42286481f2b5SSaeed Bishara /* initialize adapter */
42291bfeff03SSaeed Bishara ret = mv_init_host(host);
42306481f2b5SSaeed Bishara if (ret) {
4231f76ba003SHannes Reinecke dev_err(&pdev->dev, "Error during HW init\n");
42326481f2b5SSaeed Bishara return ret;
42336481f2b5SSaeed Bishara }
42346481f2b5SSaeed Bishara ata_host_resume(host);
42356481f2b5SSaeed Bishara }
42366481f2b5SSaeed Bishara
42376481f2b5SSaeed Bishara return 0;
42386481f2b5SSaeed Bishara }
42396481f2b5SSaeed Bishara #else
42406481f2b5SSaeed Bishara #define mv_platform_suspend NULL
42416481f2b5SSaeed Bishara #define mv_platform_resume NULL
42426481f2b5SSaeed Bishara #endif
42436481f2b5SSaeed Bishara
424497b414e1SAndrew Lunn #ifdef CONFIG_OF
4245e3779f6aSBhumika Goyal static const struct of_device_id mv_sata_dt_ids[] = {
4246b1f5c73bSSimon Guinot { .compatible = "marvell,armada-370-sata", },
424797b414e1SAndrew Lunn { .compatible = "marvell,orion-sata", },
42485e776d7bSGeert Uytterhoeven { /* sentinel */ }
424997b414e1SAndrew Lunn };
425097b414e1SAndrew Lunn MODULE_DEVICE_TABLE(of, mv_sata_dt_ids);
425197b414e1SAndrew Lunn #endif
425297b414e1SAndrew Lunn
4253f351b2d6SSaeed Bishara static struct platform_driver mv_platform_driver = {
4254f351b2d6SSaeed Bishara .probe = mv_platform_probe,
42553596b025SUwe Kleine-König .remove_new = mv_platform_remove,
42566481f2b5SSaeed Bishara .suspend = mv_platform_suspend,
42576481f2b5SSaeed Bishara .resume = mv_platform_resume,
4258f351b2d6SSaeed Bishara .driver = {
4259f351b2d6SSaeed Bishara .name = DRV_NAME,
426097b414e1SAndrew Lunn .of_match_table = of_match_ptr(mv_sata_dt_ids),
4261f351b2d6SSaeed Bishara },
4262f351b2d6SSaeed Bishara };
4263f351b2d6SSaeed Bishara
4264f351b2d6SSaeed Bishara
42657bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4266f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4267f351b2d6SSaeed Bishara const struct pci_device_id *ent);
426858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
4269b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev);
4270b2dec48cSSaeed Bishara #endif
4271f351b2d6SSaeed Bishara
4272*cbd080c3SArnd Bergmann static const struct pci_device_id mv_pci_tbl[] = {
4273*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
4274*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
4275*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
4276*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
4277*cbd080c3SArnd Bergmann /* RocketRAID 1720/174x have different identifiers */
4278*cbd080c3SArnd Bergmann { PCI_VDEVICE(TTI, 0x1720), chip_6042 },
4279*cbd080c3SArnd Bergmann { PCI_VDEVICE(TTI, 0x1740), chip_6042 },
4280*cbd080c3SArnd Bergmann { PCI_VDEVICE(TTI, 0x1742), chip_6042 },
4281*cbd080c3SArnd Bergmann
4282*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
4283*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
4284*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
4285*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
4286*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
4287*cbd080c3SArnd Bergmann
4288*cbd080c3SArnd Bergmann { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
4289*cbd080c3SArnd Bergmann
4290*cbd080c3SArnd Bergmann /* Adaptec 1430SA */
4291*cbd080c3SArnd Bergmann { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
4292*cbd080c3SArnd Bergmann
4293*cbd080c3SArnd Bergmann /* Marvell 7042 support */
4294*cbd080c3SArnd Bergmann { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
4295*cbd080c3SArnd Bergmann
4296*cbd080c3SArnd Bergmann /* Highpoint RocketRAID PCIe series */
4297*cbd080c3SArnd Bergmann { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
4298*cbd080c3SArnd Bergmann { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
4299*cbd080c3SArnd Bergmann
4300*cbd080c3SArnd Bergmann { } /* terminate list */
4301*cbd080c3SArnd Bergmann };
43027bb3c529SSaeed Bishara
43037bb3c529SSaeed Bishara static struct pci_driver mv_pci_driver = {
43047bb3c529SSaeed Bishara .name = DRV_NAME,
43057bb3c529SSaeed Bishara .id_table = mv_pci_tbl,
4306f351b2d6SSaeed Bishara .probe = mv_pci_init_one,
43077bb3c529SSaeed Bishara .remove = ata_pci_remove_one,
430858eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
4309b2dec48cSSaeed Bishara .suspend = ata_pci_device_suspend,
4310b2dec48cSSaeed Bishara .resume = mv_pci_device_resume,
4311b2dec48cSSaeed Bishara #endif
4312b2dec48cSSaeed Bishara
43137bb3c529SSaeed Bishara };
4314*cbd080c3SArnd Bergmann MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
43157bb3c529SSaeed Bishara
4316c6fd2807SJeff Garzik /**
4317c6fd2807SJeff Garzik * mv_print_info - Dump key info to kernel log for perusal.
43184447d351STejun Heo * @host: ATA host to print info about
4319c6fd2807SJeff Garzik *
4320c6fd2807SJeff Garzik * FIXME: complete this.
4321c6fd2807SJeff Garzik *
4322c6fd2807SJeff Garzik * LOCKING:
4323c6fd2807SJeff Garzik * Inherited from caller.
4324c6fd2807SJeff Garzik */
mv_print_info(struct ata_host * host)43254447d351STejun Heo static void mv_print_info(struct ata_host *host)
4326c6fd2807SJeff Garzik {
43274447d351STejun Heo struct pci_dev *pdev = to_pci_dev(host->dev);
43284447d351STejun Heo struct mv_host_priv *hpriv = host->private_data;
432944c10138SAuke Kok u8 scc;
4330c1e4fe71SJeff Garzik const char *scc_s, *gen;
4331c6fd2807SJeff Garzik
4332c6fd2807SJeff Garzik /* Use this to determine the HW stepping of the chip so we know
4333c6fd2807SJeff Garzik * what errata to workaround
4334c6fd2807SJeff Garzik */
4335c6fd2807SJeff Garzik pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
4336c6fd2807SJeff Garzik if (scc == 0)
4337c6fd2807SJeff Garzik scc_s = "SCSI";
4338c6fd2807SJeff Garzik else if (scc == 0x01)
4339c6fd2807SJeff Garzik scc_s = "RAID";
4340c6fd2807SJeff Garzik else
4341c1e4fe71SJeff Garzik scc_s = "?";
4342c1e4fe71SJeff Garzik
4343c1e4fe71SJeff Garzik if (IS_GEN_I(hpriv))
4344c1e4fe71SJeff Garzik gen = "I";
4345c1e4fe71SJeff Garzik else if (IS_GEN_II(hpriv))
4346c1e4fe71SJeff Garzik gen = "II";
4347c1e4fe71SJeff Garzik else if (IS_GEN_IIE(hpriv))
4348c1e4fe71SJeff Garzik gen = "IIE";
4349c1e4fe71SJeff Garzik else
4350c1e4fe71SJeff Garzik gen = "?";
4351c6fd2807SJeff Garzik
4352a44fec1fSJoe Perches dev_info(&pdev->dev, "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
4353c1e4fe71SJeff Garzik gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
4354c6fd2807SJeff Garzik scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
4355c6fd2807SJeff Garzik }
4356c6fd2807SJeff Garzik
4357c6fd2807SJeff Garzik /**
4358f351b2d6SSaeed Bishara * mv_pci_init_one - handle a positive probe of a PCI Marvell host
4359c6fd2807SJeff Garzik * @pdev: PCI device found
4360c6fd2807SJeff Garzik * @ent: PCI device ID entry for the matched host
4361c6fd2807SJeff Garzik *
4362c6fd2807SJeff Garzik * LOCKING:
4363c6fd2807SJeff Garzik * Inherited from caller.
4364c6fd2807SJeff Garzik */
mv_pci_init_one(struct pci_dev * pdev,const struct pci_device_id * ent)4365f351b2d6SSaeed Bishara static int mv_pci_init_one(struct pci_dev *pdev,
4366f351b2d6SSaeed Bishara const struct pci_device_id *ent)
4367c6fd2807SJeff Garzik {
4368c6fd2807SJeff Garzik unsigned int board_idx = (unsigned int)ent->driver_data;
43694447d351STejun Heo const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
43704447d351STejun Heo struct ata_host *host;
43714447d351STejun Heo struct mv_host_priv *hpriv;
4372c4bc7d73SSaeed Bishara int n_ports, port, rc;
4373c6fd2807SJeff Garzik
437406296a1eSJoe Perches ata_print_version_once(&pdev->dev, DRV_VERSION);
4375c6fd2807SJeff Garzik
43764447d351STejun Heo /* allocate host */
43774447d351STejun Heo n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
43784447d351STejun Heo
43794447d351STejun Heo host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
43804447d351STejun Heo hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
43814447d351STejun Heo if (!host || !hpriv)
43824447d351STejun Heo return -ENOMEM;
43834447d351STejun Heo host->private_data = hpriv;
4384f351b2d6SSaeed Bishara hpriv->n_ports = n_ports;
43851bfeff03SSaeed Bishara hpriv->board_idx = board_idx;
43864447d351STejun Heo
43874447d351STejun Heo /* acquire resources */
438824dc5f33STejun Heo rc = pcim_enable_device(pdev);
438924dc5f33STejun Heo if (rc)
4390c6fd2807SJeff Garzik return rc;
4391c6fd2807SJeff Garzik
43920d5ff566STejun Heo rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
43930d5ff566STejun Heo if (rc == -EBUSY)
439424dc5f33STejun Heo pcim_pin_device(pdev);
43950d5ff566STejun Heo if (rc)
439624dc5f33STejun Heo return rc;
43974447d351STejun Heo host->iomap = pcim_iomap_table(pdev);
4398f351b2d6SSaeed Bishara hpriv->base = host->iomap[MV_PRIMARY_BAR];
4399c6fd2807SJeff Garzik
4400496d4575SChristoph Hellwig rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
4401496d4575SChristoph Hellwig if (rc) {
4402496d4575SChristoph Hellwig dev_err(&pdev->dev, "DMA enable failed\n");
4403d88184fbSJeff Garzik return rc;
4404496d4575SChristoph Hellwig }
4405d88184fbSJeff Garzik
4406da2fa9baSMark Lord rc = mv_create_dma_pools(hpriv, &pdev->dev);
4407da2fa9baSMark Lord if (rc)
4408da2fa9baSMark Lord return rc;
4409da2fa9baSMark Lord
4410c4bc7d73SSaeed Bishara for (port = 0; port < host->n_ports; port++) {
4411c4bc7d73SSaeed Bishara struct ata_port *ap = host->ports[port];
4412c4bc7d73SSaeed Bishara void __iomem *port_mmio = mv_port_base(hpriv->base, port);
4413c4bc7d73SSaeed Bishara unsigned int offset = port_mmio - hpriv->base;
4414c4bc7d73SSaeed Bishara
4415c4bc7d73SSaeed Bishara ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
4416c4bc7d73SSaeed Bishara ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
4417c4bc7d73SSaeed Bishara }
4418c4bc7d73SSaeed Bishara
4419c6fd2807SJeff Garzik /* initialize adapter */
44201bfeff03SSaeed Bishara rc = mv_init_host(host);
442124dc5f33STejun Heo if (rc)
442224dc5f33STejun Heo return rc;
4423c6fd2807SJeff Garzik
44246d3c30efSMark Lord /* Enable message-switched interrupts, if requested */
44256d3c30efSMark Lord if (msi && pci_enable_msi(pdev) == 0)
44266d3c30efSMark Lord hpriv->hp_flags |= MV_HP_FLAG_MSI;
4427c6fd2807SJeff Garzik
4428c6fd2807SJeff Garzik mv_dump_pci_cfg(pdev, 0x68);
44294447d351STejun Heo mv_print_info(host);
4430c6fd2807SJeff Garzik
44314447d351STejun Heo pci_set_master(pdev);
4432ea8b4db9SJeff Garzik pci_try_set_mwi(pdev);
44334447d351STejun Heo return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
4434c5d3e45aSJeff Garzik IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
4435c6fd2807SJeff Garzik }
4436b2dec48cSSaeed Bishara
443758eb8cd5SBartlomiej Zolnierkiewicz #ifdef CONFIG_PM_SLEEP
mv_pci_device_resume(struct pci_dev * pdev)4438b2dec48cSSaeed Bishara static int mv_pci_device_resume(struct pci_dev *pdev)
4439b2dec48cSSaeed Bishara {
4440d8661921SSergei Shtylyov struct ata_host *host = pci_get_drvdata(pdev);
4441b2dec48cSSaeed Bishara int rc;
4442b2dec48cSSaeed Bishara
4443b2dec48cSSaeed Bishara rc = ata_pci_device_do_resume(pdev);
4444b2dec48cSSaeed Bishara if (rc)
4445b2dec48cSSaeed Bishara return rc;
4446b2dec48cSSaeed Bishara
4447b2dec48cSSaeed Bishara /* initialize adapter */
4448b2dec48cSSaeed Bishara rc = mv_init_host(host);
4449b2dec48cSSaeed Bishara if (rc)
4450b2dec48cSSaeed Bishara return rc;
4451b2dec48cSSaeed Bishara
4452b2dec48cSSaeed Bishara ata_host_resume(host);
4453b2dec48cSSaeed Bishara
4454b2dec48cSSaeed Bishara return 0;
4455b2dec48cSSaeed Bishara }
4456b2dec48cSSaeed Bishara #endif
44577bb3c529SSaeed Bishara #endif
4458c6fd2807SJeff Garzik
mv_init(void)4459c6fd2807SJeff Garzik static int __init mv_init(void)
4460c6fd2807SJeff Garzik {
44617bb3c529SSaeed Bishara int rc = -ENODEV;
44627bb3c529SSaeed Bishara #ifdef CONFIG_PCI
44637bb3c529SSaeed Bishara rc = pci_register_driver(&mv_pci_driver);
4464f351b2d6SSaeed Bishara if (rc < 0)
4465f351b2d6SSaeed Bishara return rc;
4466f351b2d6SSaeed Bishara #endif
4467f351b2d6SSaeed Bishara rc = platform_driver_register(&mv_platform_driver);
4468f351b2d6SSaeed Bishara
4469f351b2d6SSaeed Bishara #ifdef CONFIG_PCI
4470f351b2d6SSaeed Bishara if (rc < 0)
4471f351b2d6SSaeed Bishara pci_unregister_driver(&mv_pci_driver);
44727bb3c529SSaeed Bishara #endif
44737bb3c529SSaeed Bishara return rc;
4474c6fd2807SJeff Garzik }
4475c6fd2807SJeff Garzik
mv_exit(void)4476c6fd2807SJeff Garzik static void __exit mv_exit(void)
4477c6fd2807SJeff Garzik {
44787bb3c529SSaeed Bishara #ifdef CONFIG_PCI
4479c6fd2807SJeff Garzik pci_unregister_driver(&mv_pci_driver);
44807bb3c529SSaeed Bishara #endif
4481f351b2d6SSaeed Bishara platform_driver_unregister(&mv_platform_driver);
4482c6fd2807SJeff Garzik }
4483c6fd2807SJeff Garzik
4484c6fd2807SJeff Garzik MODULE_AUTHOR("Brett Russ");
4485c6fd2807SJeff Garzik MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
448688af4bbdSUwe Kleine-König MODULE_LICENSE("GPL v2");
4487c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
448817c5aab5SMark Lord MODULE_ALIAS("platform:" DRV_NAME);
4489c6fd2807SJeff Garzik
4490c6fd2807SJeff Garzik module_init(mv_init);
4491c6fd2807SJeff Garzik module_exit(mv_exit);
4492