blob: 8a751054c8a1a19a72ec1b2f702e43cfd2203a61 [file] [log] [blame]
Brett Russ20f733e2005-09-01 18:26:17 -04001/*
2 * sata_mv.c - Marvell SATA support
3 *
Mark Lord40f21b12009-03-10 18:51:04 -04004 * Copyright 2008-2009: Marvell Corporation, all rights reserved.
Jeff Garzik8b260242005-11-12 12:32:50 -05005 * Copyright 2005: EMC Corporation, all rights reserved.
Jeff Garzike2b1be52005-11-18 14:04:23 -05006 * Copyright 2005 Red Hat, Inc. All rights reserved.
Brett Russ20f733e2005-09-01 18:26:17 -04007 *
Mark Lord40f21b12009-03-10 18:51:04 -04008 * Originally written by Brett Russ.
9 * Extensive overhaul and enhancement by Mark Lord <mlord@pobox.com>.
10 *
Brett Russ20f733e2005-09-01 18:26:17 -040011 * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; version 2 of the License.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *
26 */
27
Jeff Garzik4a05e202007-05-24 23:40:15 -040028/*
Mark Lord85afb932008-04-19 14:54:41 -040029 * sata_mv TODO list:
30 *
Mark Lord85afb932008-04-19 14:54:41 -040031 * --> More errata workarounds for PCI-X.
32 *
33 * --> Complete a full errata audit for all chipsets to identify others.
34 *
Mark Lord85afb932008-04-19 14:54:41 -040035 * --> Develop a low-power-consumption strategy, and implement it.
36 *
Mark Lord2b748a02009-03-10 22:01:17 -040037 * --> Add sysfs attributes for per-chip / per-HC IRQ coalescing thresholds.
Mark Lord85afb932008-04-19 14:54:41 -040038 *
39 * --> [Experiment, Marvell value added] Is it possible to use target
40 * mode to cross-connect two Linux boxes with Marvell cards? If so,
41 * creating LibATA target mode support would be very interesting.
42 *
43 * Target mode, for those without docs, is the ability to directly
44 * connect two SATA ports.
45 */
Jeff Garzik4a05e202007-05-24 23:40:15 -040046
Brett Russ20f733e2005-09-01 18:26:17 -040047#include <linux/kernel.h>
48#include <linux/module.h>
49#include <linux/pci.h>
50#include <linux/init.h>
51#include <linux/blkdev.h>
52#include <linux/delay.h>
53#include <linux/interrupt.h>
Andrew Morton8d8b6002008-02-04 23:43:44 -080054#include <linux/dmapool.h>
Brett Russ20f733e2005-09-01 18:26:17 -040055#include <linux/dma-mapping.h>
Jeff Garzika9524a72005-10-30 14:39:11 -050056#include <linux/device.h>
Saeed Bisharaf351b2d2008-02-01 18:08:03 -050057#include <linux/platform_device.h>
58#include <linux/ata_platform.h>
Lennert Buytenhek15a32632008-03-27 14:51:39 -040059#include <linux/mbus.h>
Mark Lordc46938c2008-05-02 14:02:28 -040060#include <linux/bitops.h>
Brett Russ20f733e2005-09-01 18:26:17 -040061#include <scsi/scsi_host.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050062#include <scsi/scsi_cmnd.h>
Jeff Garzik6c087722007-10-12 00:16:23 -040063#include <scsi/scsi_device.h>
Brett Russ20f733e2005-09-01 18:26:17 -040064#include <linux/libata.h>
Brett Russ20f733e2005-09-01 18:26:17 -040065
66#define DRV_NAME "sata_mv"
Mark Lord2b748a02009-03-10 22:01:17 -040067#define DRV_VERSION "1.27"
Brett Russ20f733e2005-09-01 18:26:17 -040068
Mark Lord40f21b12009-03-10 18:51:04 -040069/*
70 * module options
71 */
72
73static int msi;
74#ifdef CONFIG_PCI
75module_param(msi, int, S_IRUGO);
76MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
77#endif
78
Mark Lord2b748a02009-03-10 22:01:17 -040079static int irq_coalescing_io_count;
80module_param(irq_coalescing_io_count, int, S_IRUGO);
81MODULE_PARM_DESC(irq_coalescing_io_count,
82 "IRQ coalescing I/O count threshold (0..255)");
83
84static int irq_coalescing_usecs;
85module_param(irq_coalescing_usecs, int, S_IRUGO);
86MODULE_PARM_DESC(irq_coalescing_usecs,
87 "IRQ coalescing time threshold in usecs");
88
Brett Russ20f733e2005-09-01 18:26:17 -040089enum {
90 /* BAR's are enumerated in terms of pci_resource_start() terms */
91 MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
92 MV_IO_BAR = 2, /* offset 0x18: IO space */
93 MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
94
95 MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
96 MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
97
Mark Lord2b748a02009-03-10 22:01:17 -040098 /* For use with both IRQ coalescing methods ("all ports" or "per-HC" */
99 COAL_CLOCKS_PER_USEC = 150, /* for calculating COAL_TIMEs */
100 MAX_COAL_TIME_THRESHOLD = ((1 << 24) - 1), /* internal clocks count */
101 MAX_COAL_IO_COUNT = 255, /* completed I/O count */
102
Brett Russ20f733e2005-09-01 18:26:17 -0400103 MV_PCI_REG_BASE = 0,
Mark Lord615ab952006-05-19 16:24:56 -0400104
Mark Lord2b748a02009-03-10 22:01:17 -0400105 /*
106 * Per-chip ("all ports") interrupt coalescing feature.
107 * This is only for GEN_II / GEN_IIE hardware.
108 *
109 * Coalescing defers the interrupt until either the IO_THRESHOLD
110 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
111 */
112 MV_COAL_REG_BASE = 0x18000,
113 MV_IRQ_COAL_CAUSE = (MV_COAL_REG_BASE + 0x08),
114 ALL_PORTS_COAL_IRQ = (1 << 4), /* all ports irq event */
115
116 MV_IRQ_COAL_IO_THRESHOLD = (MV_COAL_REG_BASE + 0xcc),
117 MV_IRQ_COAL_TIME_THRESHOLD = (MV_COAL_REG_BASE + 0xd0),
118
119 /*
120 * Registers for the (unused here) transaction coalescing feature:
121 */
122 MV_TRAN_COAL_CAUSE_LO = (MV_COAL_REG_BASE + 0x88),
123 MV_TRAN_COAL_CAUSE_HI = (MV_COAL_REG_BASE + 0x8c),
124
Brett Russ20f733e2005-09-01 18:26:17 -0400125 MV_SATAHC0_REG_BASE = 0x20000,
Mark Lord8e7decd2008-05-02 02:07:51 -0400126 MV_FLASH_CTL_OFS = 0x1046c,
127 MV_GPIO_PORT_CTL_OFS = 0x104f0,
128 MV_RESET_CFG_OFS = 0x180d8,
Brett Russ20f733e2005-09-01 18:26:17 -0400129
130 MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
131 MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
132 MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
133 MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
134
Brett Russ31961942005-09-30 01:36:00 -0400135 MV_MAX_Q_DEPTH = 32,
136 MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
137
138 /* CRQB needs alignment on a 1KB boundary. Size == 1KB
139 * CRPB needs alignment on a 256B boundary. Size == 256B
Brett Russ31961942005-09-30 01:36:00 -0400140 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
141 */
142 MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
143 MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
Mark Lordda2fa9b2008-01-26 18:32:45 -0500144 MV_MAX_SG_CT = 256,
Brett Russ31961942005-09-30 01:36:00 -0400145 MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
Brett Russ31961942005-09-30 01:36:00 -0400146
Mark Lord352fab72008-04-19 14:43:42 -0400147 /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
Brett Russ20f733e2005-09-01 18:26:17 -0400148 MV_PORT_HC_SHIFT = 2,
Mark Lord352fab72008-04-19 14:43:42 -0400149 MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
150 /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
151 MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
Brett Russ20f733e2005-09-01 18:26:17 -0400152
153 /* Host Flags */
154 MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100155
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400156 MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
Mark Lord91b1a842009-01-30 18:46:39 -0500157 ATA_FLAG_MMIO | ATA_FLAG_PIO_POLLING,
Mark Lordad3aef52008-05-14 09:21:43 -0400158
Mark Lord91b1a842009-01-30 18:46:39 -0500159 MV_GEN_I_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
Brett Russ20f733e2005-09-01 18:26:17 -0400160
Mark Lord40f21b12009-03-10 18:51:04 -0400161 MV_GEN_II_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NCQ |
162 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
Mark Lord91b1a842009-01-30 18:46:39 -0500163
164 MV_GEN_IIE_FLAGS = MV_GEN_II_FLAGS | ATA_FLAG_AN,
Mark Lordad3aef52008-05-14 09:21:43 -0400165
Brett Russ31961942005-09-30 01:36:00 -0400166 CRQB_FLAG_READ = (1 << 0),
167 CRQB_TAG_SHIFT = 1,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400168 CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
Mark Lorde12bef52008-03-31 19:33:56 -0400169 CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400170 CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
Brett Russ31961942005-09-30 01:36:00 -0400171 CRQB_CMD_ADDR_SHIFT = 8,
172 CRQB_CMD_CS = (0x2 << 11),
173 CRQB_CMD_LAST = (1 << 15),
174
175 CRPB_FLAG_STATUS_SHIFT = 8,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400176 CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
177 CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
Brett Russ31961942005-09-30 01:36:00 -0400178
179 EPRD_FLAG_END_OF_TBL = (1 << 31),
180
Brett Russ20f733e2005-09-01 18:26:17 -0400181 /* PCI interface registers */
182
Brett Russ31961942005-09-30 01:36:00 -0400183 PCI_COMMAND_OFS = 0xc00,
Mark Lord8e7decd2008-05-02 02:07:51 -0400184 PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */
Brett Russ31961942005-09-30 01:36:00 -0400185
Brett Russ20f733e2005-09-01 18:26:17 -0400186 PCI_MAIN_CMD_STS_OFS = 0xd30,
187 STOP_PCI_MASTER = (1 << 2),
188 PCI_MASTER_EMPTY = (1 << 3),
189 GLOB_SFT_RST = (1 << 4),
190
Mark Lord8e7decd2008-05-02 02:07:51 -0400191 MV_PCI_MODE_OFS = 0xd00,
192 MV_PCI_MODE_MASK = 0x30,
193
Jeff Garzik522479f2005-11-12 22:14:02 -0500194 MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
195 MV_PCI_DISC_TIMER = 0xd04,
196 MV_PCI_MSI_TRIGGER = 0xc38,
197 MV_PCI_SERR_MASK = 0xc28,
Mark Lord8e7decd2008-05-02 02:07:51 -0400198 MV_PCI_XBAR_TMOUT_OFS = 0x1d04,
Jeff Garzik522479f2005-11-12 22:14:02 -0500199 MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
200 MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
201 MV_PCI_ERR_ATTRIBUTE = 0x1d48,
202 MV_PCI_ERR_COMMAND = 0x1d50,
203
Mark Lord02a121d2007-12-01 13:07:22 -0500204 PCI_IRQ_CAUSE_OFS = 0x1d58,
205 PCI_IRQ_MASK_OFS = 0x1d5c,
Brett Russ20f733e2005-09-01 18:26:17 -0400206 PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
207
Mark Lord02a121d2007-12-01 13:07:22 -0500208 PCIE_IRQ_CAUSE_OFS = 0x1900,
209 PCIE_IRQ_MASK_OFS = 0x1910,
Mark Lord646a4da2008-01-26 18:30:37 -0500210 PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
Mark Lord02a121d2007-12-01 13:07:22 -0500211
Mark Lord7368f912008-04-25 11:24:24 -0400212 /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
213 PCI_HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
214 PCI_HC_MAIN_IRQ_MASK_OFS = 0x1d64,
215 SOC_HC_MAIN_IRQ_CAUSE_OFS = 0x20020,
216 SOC_HC_MAIN_IRQ_MASK_OFS = 0x20024,
Mark Lord40f21b12009-03-10 18:51:04 -0400217 ERR_IRQ = (1 << 0), /* shift by (2 * port #) */
218 DONE_IRQ = (1 << 1), /* shift by (2 * port #) */
Brett Russ20f733e2005-09-01 18:26:17 -0400219 HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
220 HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
Mark Lord2b748a02009-03-10 22:01:17 -0400221 DONE_IRQ_0_3 = 0x000000aa, /* DONE_IRQ ports 0,1,2,3 */
222 DONE_IRQ_4_7 = (DONE_IRQ_0_3 << HC_SHIFT), /* 4,5,6,7 */
Brett Russ20f733e2005-09-01 18:26:17 -0400223 PCI_ERR = (1 << 18),
Mark Lord40f21b12009-03-10 18:51:04 -0400224 TRAN_COAL_LO_DONE = (1 << 19), /* transaction coalescing */
225 TRAN_COAL_HI_DONE = (1 << 20), /* transaction coalescing */
226 PORTS_0_3_COAL_DONE = (1 << 8), /* HC0 IRQ coalescing */
227 PORTS_4_7_COAL_DONE = (1 << 17), /* HC1 IRQ coalescing */
228 ALL_PORTS_COAL_DONE = (1 << 21), /* GEN_II(E) IRQ coalescing */
Brett Russ20f733e2005-09-01 18:26:17 -0400229 GPIO_INT = (1 << 22),
230 SELF_INT = (1 << 23),
231 TWSI_INT = (1 << 24),
232 HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
Jeff Garzikfb621e22007-02-25 04:19:45 -0500233 HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
Mark Lorde12bef52008-03-31 19:33:56 -0400234 HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
Brett Russ20f733e2005-09-01 18:26:17 -0400235
236 /* SATAHC registers */
237 HC_CFG_OFS = 0,
238
239 HC_IRQ_CAUSE_OFS = 0x14,
Mark Lord352fab72008-04-19 14:43:42 -0400240 DMA_IRQ = (1 << 0), /* shift by port # */
241 HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
Brett Russ20f733e2005-09-01 18:26:17 -0400242 DEV_IRQ = (1 << 8), /* shift by port # */
243
Mark Lord2b748a02009-03-10 22:01:17 -0400244 /*
245 * Per-HC (Host-Controller) interrupt coalescing feature.
246 * This is present on all chip generations.
247 *
248 * Coalescing defers the interrupt until either the IO_THRESHOLD
249 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
250 */
251 HC_IRQ_COAL_IO_THRESHOLD_OFS = 0x000c,
252 HC_IRQ_COAL_TIME_THRESHOLD_OFS = 0x0010,
253
Mark Lord000b3442009-03-15 11:33:19 -0400254 SOC_LED_CTRL_OFS = 0x2c,
255 SOC_LED_CTRL_BLINK = (1 << 0), /* Active LED blink */
256 SOC_LED_CTRL_ACT_PRESENCE = (1 << 2), /* Multiplex dev presence */
257 /* with dev activity LED */
258
Brett Russ20f733e2005-09-01 18:26:17 -0400259 /* Shadow block registers */
Brett Russ31961942005-09-30 01:36:00 -0400260 SHD_BLK_OFS = 0x100,
261 SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
Brett Russ20f733e2005-09-01 18:26:17 -0400262
263 /* SATA registers */
264 SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
265 SATA_ACTIVE_OFS = 0x350,
Mark Lord0c589122008-01-26 18:31:16 -0500266 SATA_FIS_IRQ_CAUSE_OFS = 0x364,
Mark Lordc443c502008-05-14 09:24:39 -0400267 SATA_FIS_IRQ_AN = (1 << 9), /* async notification */
Mark Lord17c5aab2008-04-16 14:56:51 -0400268
Mark Lorde12bef52008-03-31 19:33:56 -0400269 LTMODE_OFS = 0x30c,
Mark Lord17c5aab2008-04-16 14:56:51 -0400270 LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
271
Jeff Garzik47c2b672005-11-12 21:13:17 -0500272 PHY_MODE3 = 0x310,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500273 PHY_MODE4 = 0x314,
Mark Lordba069e32008-05-31 16:46:34 -0400274 PHY_MODE4_CFG_MASK = 0x00000003, /* phy internal config field */
275 PHY_MODE4_CFG_VALUE = 0x00000001, /* phy internal config field */
276 PHY_MODE4_RSVD_ZEROS = 0x5de3fffa, /* Gen2e always write zeros */
277 PHY_MODE4_RSVD_ONES = 0x00000005, /* Gen2e always write ones */
278
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500279 PHY_MODE2 = 0x330,
Mark Lorde12bef52008-03-31 19:33:56 -0400280 SATA_IFCTL_OFS = 0x344,
Mark Lord8e7decd2008-05-02 02:07:51 -0400281 SATA_TESTCTL_OFS = 0x348,
Mark Lorde12bef52008-03-31 19:33:56 -0400282 SATA_IFSTAT_OFS = 0x34c,
283 VENDOR_UNIQUE_FIS_OFS = 0x35c,
Mark Lord17c5aab2008-04-16 14:56:51 -0400284
Mark Lord8e7decd2008-05-02 02:07:51 -0400285 FISCFG_OFS = 0x360,
286 FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */
287 FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
Mark Lord17c5aab2008-04-16 14:56:51 -0400288
Jeff Garzikc9d39132005-11-13 17:47:51 -0500289 MV5_PHY_MODE = 0x74,
Mark Lord8e7decd2008-05-02 02:07:51 -0400290 MV5_LTMODE_OFS = 0x30,
291 MV5_PHY_CTL_OFS = 0x0C,
292 SATA_INTERFACE_CFG_OFS = 0x050,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500293
294 MV_M2_PREAMP_MASK = 0x7e0,
Brett Russ20f733e2005-09-01 18:26:17 -0400295
296 /* Port registers */
297 EDMA_CFG_OFS = 0,
Mark Lord0c589122008-01-26 18:31:16 -0500298 EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
299 EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
300 EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
301 EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
302 EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
Mark Lorde12bef52008-03-31 19:33:56 -0400303 EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
304 EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
Brett Russ20f733e2005-09-01 18:26:17 -0400305
306 EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
307 EDMA_ERR_IRQ_MASK_OFS = 0xc,
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400308 EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
309 EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
310 EDMA_ERR_DEV = (1 << 2), /* device error */
311 EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
312 EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
313 EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400314 EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
315 EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400316 EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400317 EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400318 EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
319 EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
320 EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
321 EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
Mark Lord646a4da2008-01-26 18:30:37 -0500322
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400323 EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500324 EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
325 EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
326 EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
327 EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
328
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400329 EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500330
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400331 EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500332 EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
333 EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
334 EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
335 EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
336 EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
337
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400338 EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500339
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400340 EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400341 EDMA_ERR_OVERRUN_5 = (1 << 5),
342 EDMA_ERR_UNDERRUN_5 = (1 << 6),
Mark Lord646a4da2008-01-26 18:30:37 -0500343
344 EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
345 EDMA_ERR_LNK_CTRL_RX_1 |
346 EDMA_ERR_LNK_CTRL_RX_3 |
Mark Lord85afb932008-04-19 14:54:41 -0400347 EDMA_ERR_LNK_CTRL_TX,
Mark Lord646a4da2008-01-26 18:30:37 -0500348
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400349 EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
350 EDMA_ERR_PRD_PAR |
351 EDMA_ERR_DEV_DCON |
352 EDMA_ERR_DEV_CON |
353 EDMA_ERR_SERR |
354 EDMA_ERR_SELF_DIS |
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400355 EDMA_ERR_CRQB_PAR |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400356 EDMA_ERR_CRPB_PAR |
357 EDMA_ERR_INTRL_PAR |
358 EDMA_ERR_IORDY |
359 EDMA_ERR_LNK_CTRL_RX_2 |
360 EDMA_ERR_LNK_DATA_RX |
361 EDMA_ERR_LNK_DATA_TX |
362 EDMA_ERR_TRANS_PROTO,
Mark Lorde12bef52008-03-31 19:33:56 -0400363
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400364 EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
365 EDMA_ERR_PRD_PAR |
366 EDMA_ERR_DEV_DCON |
367 EDMA_ERR_DEV_CON |
368 EDMA_ERR_OVERRUN_5 |
369 EDMA_ERR_UNDERRUN_5 |
370 EDMA_ERR_SELF_DIS_5 |
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400371 EDMA_ERR_CRQB_PAR |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400372 EDMA_ERR_CRPB_PAR |
373 EDMA_ERR_INTRL_PAR |
374 EDMA_ERR_IORDY,
Brett Russ20f733e2005-09-01 18:26:17 -0400375
Brett Russ31961942005-09-30 01:36:00 -0400376 EDMA_REQ_Q_BASE_HI_OFS = 0x10,
377 EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
Brett Russ31961942005-09-30 01:36:00 -0400378
379 EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
380 EDMA_REQ_Q_PTR_SHIFT = 5,
381
382 EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
383 EDMA_RSP_Q_IN_PTR_OFS = 0x20,
384 EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
Brett Russ31961942005-09-30 01:36:00 -0400385 EDMA_RSP_Q_PTR_SHIFT = 3,
386
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400387 EDMA_CMD_OFS = 0x28, /* EDMA command register */
388 EDMA_EN = (1 << 0), /* enable EDMA */
389 EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
Mark Lord8e7decd2008-05-02 02:07:51 -0400390 EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */
Brett Russ20f733e2005-09-01 18:26:17 -0400391
Mark Lord8e7decd2008-05-02 02:07:51 -0400392 EDMA_STATUS_OFS = 0x30, /* EDMA engine status */
393 EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */
394 EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */
395
396 EDMA_IORDY_TMOUT_OFS = 0x34,
397 EDMA_ARB_CFG_OFS = 0x38,
398
399 EDMA_HALTCOND_OFS = 0x60, /* GenIIe halt conditions */
Mark Lordc01e8a22009-02-25 15:14:48 -0500400 EDMA_UNKNOWN_RSVD_OFS = 0x6C, /* GenIIe unknown/reserved */
Mark Lordda142652009-01-30 18:51:54 -0500401
402 BMDMA_CMD_OFS = 0x224, /* bmdma command register */
403 BMDMA_STATUS_OFS = 0x228, /* bmdma status register */
404 BMDMA_PRD_LOW_OFS = 0x22c, /* bmdma PRD addr 31:0 */
405 BMDMA_PRD_HIGH_OFS = 0x230, /* bmdma PRD addr 63:32 */
406
Brett Russ31961942005-09-30 01:36:00 -0400407 /* Host private flags (hp_flags) */
408 MV_HP_FLAG_MSI = (1 << 0),
Jeff Garzik47c2b672005-11-12 21:13:17 -0500409 MV_HP_ERRATA_50XXB0 = (1 << 1),
410 MV_HP_ERRATA_50XXB2 = (1 << 2),
411 MV_HP_ERRATA_60X1B2 = (1 << 3),
412 MV_HP_ERRATA_60X1C0 = (1 << 4),
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400413 MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
414 MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
415 MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
Mark Lord02a121d2007-12-01 13:07:22 -0500416 MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
Mark Lord616d4a92008-05-02 02:08:32 -0400417 MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */
Mark Lord1f398472008-05-27 17:54:48 -0400418 MV_HP_FLAG_SOC = (1 << 11), /* SystemOnChip, no PCI */
Mark Lord000b3442009-03-15 11:33:19 -0400419 MV_HP_QUIRK_LED_BLINK_EN = (1 << 12), /* is led blinking enabled? */
Brett Russ20f733e2005-09-01 18:26:17 -0400420
Brett Russ31961942005-09-30 01:36:00 -0400421 /* Port private flags (pp_flags) */
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400422 MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
Mark Lord72109162008-01-26 18:31:33 -0500423 MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
Mark Lord00f42ea2008-05-02 02:11:45 -0400424 MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */
Mark Lord29d187b2008-05-02 02:15:37 -0400425 MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */
Mark Lordd16ab3f2009-02-25 15:17:43 -0500426 MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4), /* ignore initial ATA_DRDY */
Brett Russ31961942005-09-30 01:36:00 -0400427};
428
Jeff Garzikee9ccdf2007-07-12 15:51:22 -0400429#define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
430#define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
Jeff Garzike4e7b892006-01-31 12:18:41 -0500431#define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
Mark Lord8e7decd2008-05-02 02:07:51 -0400432#define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
Mark Lord1f398472008-05-27 17:54:48 -0400433#define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500434
Lennert Buytenhek15a32632008-03-27 14:51:39 -0400435#define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
436#define WINDOW_BASE(i) (0x20034 + ((i) << 4))
437
Jeff Garzik095fec82005-11-12 09:50:49 -0500438enum {
Jeff Garzikbaf14aa2007-10-09 13:51:57 -0400439 /* DMA boundary 0xffff is required by the s/g splitting
440 * we need on /length/ in mv_fill-sg().
441 */
442 MV_DMA_BOUNDARY = 0xffffU,
Jeff Garzik095fec82005-11-12 09:50:49 -0500443
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400444 /* mask of register bits containing lower 32 bits
445 * of EDMA request queue DMA address
446 */
Jeff Garzik095fec82005-11-12 09:50:49 -0500447 EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
448
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400449 /* ditto, for response queue */
Jeff Garzik095fec82005-11-12 09:50:49 -0500450 EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
451};
452
Jeff Garzik522479f2005-11-12 22:14:02 -0500453enum chip_type {
454 chip_504x,
455 chip_508x,
456 chip_5080,
457 chip_604x,
458 chip_608x,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500459 chip_6042,
460 chip_7042,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500461 chip_soc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500462};
463
Brett Russ31961942005-09-30 01:36:00 -0400464/* Command ReQuest Block: 32B */
465struct mv_crqb {
Mark Lorde1469872006-05-22 19:02:03 -0400466 __le32 sg_addr;
467 __le32 sg_addr_hi;
468 __le16 ctrl_flags;
469 __le16 ata_cmd[11];
Brett Russ31961942005-09-30 01:36:00 -0400470};
471
Jeff Garzike4e7b892006-01-31 12:18:41 -0500472struct mv_crqb_iie {
Mark Lorde1469872006-05-22 19:02:03 -0400473 __le32 addr;
474 __le32 addr_hi;
475 __le32 flags;
476 __le32 len;
477 __le32 ata_cmd[4];
Jeff Garzike4e7b892006-01-31 12:18:41 -0500478};
479
Brett Russ31961942005-09-30 01:36:00 -0400480/* Command ResPonse Block: 8B */
481struct mv_crpb {
Mark Lorde1469872006-05-22 19:02:03 -0400482 __le16 id;
483 __le16 flags;
484 __le32 tmstmp;
Brett Russ31961942005-09-30 01:36:00 -0400485};
486
487/* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
488struct mv_sg {
Mark Lorde1469872006-05-22 19:02:03 -0400489 __le32 addr;
490 __le32 flags_size;
491 __le32 addr_hi;
492 __le32 reserved;
Brett Russ20f733e2005-09-01 18:26:17 -0400493};
494
Mark Lord08da1752009-02-25 15:13:03 -0500495/*
496 * We keep a local cache of a few frequently accessed port
497 * registers here, to avoid having to read them (very slow)
498 * when switching between EDMA and non-EDMA modes.
499 */
500struct mv_cached_regs {
501 u32 fiscfg;
502 u32 ltmode;
503 u32 haltcond;
Mark Lordc01e8a22009-02-25 15:14:48 -0500504 u32 unknown_rsvd;
Mark Lord08da1752009-02-25 15:13:03 -0500505};
506
Brett Russ20f733e2005-09-01 18:26:17 -0400507struct mv_port_priv {
Brett Russ31961942005-09-30 01:36:00 -0400508 struct mv_crqb *crqb;
509 dma_addr_t crqb_dma;
510 struct mv_crpb *crpb;
511 dma_addr_t crpb_dma;
Mark Lordeb73d552008-01-29 13:24:00 -0500512 struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
513 dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400514
515 unsigned int req_idx;
516 unsigned int resp_idx;
517
Brett Russ31961942005-09-30 01:36:00 -0400518 u32 pp_flags;
Mark Lord08da1752009-02-25 15:13:03 -0500519 struct mv_cached_regs cached;
Mark Lord29d187b2008-05-02 02:15:37 -0400520 unsigned int delayed_eh_pmp_map;
Brett Russ20f733e2005-09-01 18:26:17 -0400521};
522
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500523struct mv_port_signal {
524 u32 amps;
525 u32 pre;
526};
527
Mark Lord02a121d2007-12-01 13:07:22 -0500528struct mv_host_priv {
529 u32 hp_flags;
Mark Lord96e2c482008-05-17 13:38:00 -0400530 u32 main_irq_mask;
Mark Lord02a121d2007-12-01 13:07:22 -0500531 struct mv_port_signal signal[8];
532 const struct mv_hw_ops *ops;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500533 int n_ports;
534 void __iomem *base;
Mark Lord7368f912008-04-25 11:24:24 -0400535 void __iomem *main_irq_cause_addr;
536 void __iomem *main_irq_mask_addr;
Mark Lord02a121d2007-12-01 13:07:22 -0500537 u32 irq_cause_ofs;
538 u32 irq_mask_ofs;
539 u32 unmask_all_irqs;
Mark Lordda2fa9b2008-01-26 18:32:45 -0500540 /*
541 * These consistent DMA memory pools give us guaranteed
542 * alignment for hardware-accessed data structures,
543 * and less memory waste in accomplishing the alignment.
544 */
545 struct dma_pool *crqb_pool;
546 struct dma_pool *crpb_pool;
547 struct dma_pool *sg_tbl_pool;
Mark Lord02a121d2007-12-01 13:07:22 -0500548};
549
Jeff Garzik47c2b672005-11-12 21:13:17 -0500550struct mv_hw_ops {
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500551 void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
552 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500553 void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
554 void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
555 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500556 int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
557 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500558 void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100559 void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500560};
561
Tejun Heo82ef04f2008-07-31 17:02:40 +0900562static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
563static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
564static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
565static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
Brett Russ31961942005-09-30 01:36:00 -0400566static int mv_port_start(struct ata_port *ap);
567static void mv_port_stop(struct ata_port *ap);
Mark Lord3e4a1392008-05-02 02:10:02 -0400568static int mv_qc_defer(struct ata_queued_cmd *qc);
Brett Russ31961942005-09-30 01:36:00 -0400569static void mv_qc_prep(struct ata_queued_cmd *qc);
Jeff Garzike4e7b892006-01-31 12:18:41 -0500570static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900571static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
Tejun Heoa1efdab2008-03-25 12:22:50 +0900572static int mv_hardreset(struct ata_link *link, unsigned int *class,
573 unsigned long deadline);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400574static void mv_eh_freeze(struct ata_port *ap);
575static void mv_eh_thaw(struct ata_port *ap);
Mark Lordf2738272008-01-26 18:32:29 -0500576static void mv6_dev_config(struct ata_device *dev);
Brett Russ20f733e2005-09-01 18:26:17 -0400577
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500578static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
579 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500580static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
581static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
582 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500583static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
584 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500585static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100586static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500587
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500588static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
589 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500590static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
591static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
592 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500593static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
594 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500595static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500596static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
597 void __iomem *mmio);
598static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
599 void __iomem *mmio);
600static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
601 void __iomem *mmio, unsigned int n_hc);
602static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
603 void __iomem *mmio);
604static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100605static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
Mark Lorde12bef52008-03-31 19:33:56 -0400606static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500607 unsigned int port_no);
Mark Lorde12bef52008-03-31 19:33:56 -0400608static int mv_stop_edma(struct ata_port *ap);
Mark Lordb5624682008-03-31 19:34:40 -0400609static int mv_stop_edma_engine(void __iomem *port_mmio);
Mark Lord00b81232009-01-30 18:47:51 -0500610static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500611
Mark Lorde49856d2008-04-16 14:59:07 -0400612static void mv_pmp_select(struct ata_port *ap, int pmp);
613static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
614 unsigned long deadline);
615static int mv_softreset(struct ata_link *link, unsigned int *class,
616 unsigned long deadline);
Mark Lord29d187b2008-05-02 02:15:37 -0400617static void mv_pmp_error_handler(struct ata_port *ap);
Mark Lord4c299ca2008-05-02 02:16:20 -0400618static void mv_process_crpb_entries(struct ata_port *ap,
619 struct mv_port_priv *pp);
Brett Russ20f733e2005-09-01 18:26:17 -0400620
Mark Lordda142652009-01-30 18:51:54 -0500621static void mv_sff_irq_clear(struct ata_port *ap);
622static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
623static void mv_bmdma_setup(struct ata_queued_cmd *qc);
624static void mv_bmdma_start(struct ata_queued_cmd *qc);
625static void mv_bmdma_stop(struct ata_queued_cmd *qc);
626static u8 mv_bmdma_status(struct ata_port *ap);
Mark Lordd16ab3f2009-02-25 15:17:43 -0500627static u8 mv_sff_check_status(struct ata_port *ap);
Mark Lordda142652009-01-30 18:51:54 -0500628
Mark Lordeb73d552008-01-29 13:24:00 -0500629/* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
630 * because we have to allow room for worst case splitting of
631 * PRDs for 64K boundaries in mv_fill_sg().
632 */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400633static struct scsi_host_template mv5_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900634 ATA_BASE_SHT(DRV_NAME),
Jeff Garzikbaf14aa2007-10-09 13:51:57 -0400635 .sg_tablesize = MV_MAX_SG_CT / 2,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400636 .dma_boundary = MV_DMA_BOUNDARY,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400637};
638
639static struct scsi_host_template mv6_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900640 ATA_NCQ_SHT(DRV_NAME),
Mark Lord138bfdd2008-01-26 18:33:18 -0500641 .can_queue = MV_MAX_Q_DEPTH - 1,
Jeff Garzikbaf14aa2007-10-09 13:51:57 -0400642 .sg_tablesize = MV_MAX_SG_CT / 2,
Brett Russ20f733e2005-09-01 18:26:17 -0400643 .dma_boundary = MV_DMA_BOUNDARY,
Brett Russ20f733e2005-09-01 18:26:17 -0400644};
645
Tejun Heo029cfd62008-03-25 12:22:49 +0900646static struct ata_port_operations mv5_ops = {
647 .inherits = &ata_sff_port_ops,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500648
Mark Lord3e4a1392008-05-02 02:10:02 -0400649 .qc_defer = mv_qc_defer,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500650 .qc_prep = mv_qc_prep,
651 .qc_issue = mv_qc_issue,
652
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400653 .freeze = mv_eh_freeze,
654 .thaw = mv_eh_thaw,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900655 .hardreset = mv_hardreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900656 .error_handler = ata_std_error_handler, /* avoid SFF EH */
Tejun Heo029cfd62008-03-25 12:22:49 +0900657 .post_internal_cmd = ATA_OP_NULL,
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400658
Jeff Garzikc9d39132005-11-13 17:47:51 -0500659 .scr_read = mv5_scr_read,
660 .scr_write = mv5_scr_write,
661
662 .port_start = mv_port_start,
663 .port_stop = mv_port_stop,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500664};
665
Tejun Heo029cfd62008-03-25 12:22:49 +0900666static struct ata_port_operations mv6_ops = {
667 .inherits = &mv5_ops,
Mark Lordf2738272008-01-26 18:32:29 -0500668 .dev_config = mv6_dev_config,
Brett Russ20f733e2005-09-01 18:26:17 -0400669 .scr_read = mv_scr_read,
670 .scr_write = mv_scr_write,
671
Mark Lorde49856d2008-04-16 14:59:07 -0400672 .pmp_hardreset = mv_pmp_hardreset,
673 .pmp_softreset = mv_softreset,
674 .softreset = mv_softreset,
Mark Lord29d187b2008-05-02 02:15:37 -0400675 .error_handler = mv_pmp_error_handler,
Mark Lordda142652009-01-30 18:51:54 -0500676
Mark Lord40f21b12009-03-10 18:51:04 -0400677 .sff_check_status = mv_sff_check_status,
Mark Lordda142652009-01-30 18:51:54 -0500678 .sff_irq_clear = mv_sff_irq_clear,
679 .check_atapi_dma = mv_check_atapi_dma,
680 .bmdma_setup = mv_bmdma_setup,
681 .bmdma_start = mv_bmdma_start,
682 .bmdma_stop = mv_bmdma_stop,
683 .bmdma_status = mv_bmdma_status,
Brett Russ20f733e2005-09-01 18:26:17 -0400684};
685
Tejun Heo029cfd62008-03-25 12:22:49 +0900686static struct ata_port_operations mv_iie_ops = {
687 .inherits = &mv6_ops,
688 .dev_config = ATA_OP_NULL,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500689 .qc_prep = mv_qc_prep_iie,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500690};
691
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100692static const struct ata_port_info mv_port_info[] = {
Brett Russ20f733e2005-09-01 18:26:17 -0400693 { /* chip_504x */
Mark Lord91b1a842009-01-30 18:46:39 -0500694 .flags = MV_GEN_I_FLAGS,
Brett Russ31961942005-09-30 01:36:00 -0400695 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400696 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500697 .port_ops = &mv5_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400698 },
699 { /* chip_508x */
Mark Lord91b1a842009-01-30 18:46:39 -0500700 .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
Brett Russ31961942005-09-30 01:36:00 -0400701 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400702 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500703 .port_ops = &mv5_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400704 },
Jeff Garzik47c2b672005-11-12 21:13:17 -0500705 { /* chip_5080 */
Mark Lord91b1a842009-01-30 18:46:39 -0500706 .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500707 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400708 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500709 .port_ops = &mv5_ops,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500710 },
Brett Russ20f733e2005-09-01 18:26:17 -0400711 { /* chip_604x */
Mark Lord91b1a842009-01-30 18:46:39 -0500712 .flags = MV_GEN_II_FLAGS,
Brett Russ31961942005-09-30 01:36:00 -0400713 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400714 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500715 .port_ops = &mv6_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400716 },
717 { /* chip_608x */
Mark Lord91b1a842009-01-30 18:46:39 -0500718 .flags = MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
Brett Russ31961942005-09-30 01:36:00 -0400719 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400720 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500721 .port_ops = &mv6_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400722 },
Jeff Garzike4e7b892006-01-31 12:18:41 -0500723 { /* chip_6042 */
Mark Lord91b1a842009-01-30 18:46:39 -0500724 .flags = MV_GEN_IIE_FLAGS,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500725 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400726 .udma_mask = ATA_UDMA6,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500727 .port_ops = &mv_iie_ops,
728 },
729 { /* chip_7042 */
Mark Lord91b1a842009-01-30 18:46:39 -0500730 .flags = MV_GEN_IIE_FLAGS,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500731 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400732 .udma_mask = ATA_UDMA6,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500733 .port_ops = &mv_iie_ops,
734 },
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500735 { /* chip_soc */
Mark Lord91b1a842009-01-30 18:46:39 -0500736 .flags = MV_GEN_IIE_FLAGS,
Mark Lord17c5aab2008-04-16 14:56:51 -0400737 .pio_mask = 0x1f, /* pio0-4 */
738 .udma_mask = ATA_UDMA6,
739 .port_ops = &mv_iie_ops,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500740 },
Brett Russ20f733e2005-09-01 18:26:17 -0400741};
742
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500743static const struct pci_device_id mv_pci_tbl[] = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400744 { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
745 { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
746 { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
747 { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
Mark Lord46c57842008-09-04 18:21:07 -0400748 /* RocketRAID 1720/174x have different identifiers */
749 { PCI_VDEVICE(TTI, 0x1720), chip_6042 },
Mark Lord44622542009-01-27 16:33:13 -0500750 { PCI_VDEVICE(TTI, 0x1740), chip_6042 },
751 { PCI_VDEVICE(TTI, 0x1742), chip_6042 },
Brett Russ20f733e2005-09-01 18:26:17 -0400752
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400753 { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
754 { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
755 { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
756 { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
757 { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
Jeff Garzik29179532005-11-11 08:08:03 -0500758
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400759 { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
760
Florian Attenbergerd9f9c6b2007-07-02 17:09:29 +0200761 /* Adaptec 1430SA */
762 { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
763
Mark Lord02a121d2007-12-01 13:07:22 -0500764 /* Marvell 7042 support */
Morrison, Tom6a3d5862007-03-06 02:38:10 -0800765 { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
766
Mark Lord02a121d2007-12-01 13:07:22 -0500767 /* Highpoint RocketRAID PCIe series */
768 { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
769 { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
770
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400771 { } /* terminate list */
Brett Russ20f733e2005-09-01 18:26:17 -0400772};
773
Jeff Garzik47c2b672005-11-12 21:13:17 -0500774static const struct mv_hw_ops mv5xxx_ops = {
775 .phy_errata = mv5_phy_errata,
776 .enable_leds = mv5_enable_leds,
777 .read_preamp = mv5_read_preamp,
778 .reset_hc = mv5_reset_hc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500779 .reset_flash = mv5_reset_flash,
780 .reset_bus = mv5_reset_bus,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500781};
782
783static const struct mv_hw_ops mv6xxx_ops = {
784 .phy_errata = mv6_phy_errata,
785 .enable_leds = mv6_enable_leds,
786 .read_preamp = mv6_read_preamp,
787 .reset_hc = mv6_reset_hc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500788 .reset_flash = mv6_reset_flash,
789 .reset_bus = mv_reset_pci_bus,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500790};
791
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500792static const struct mv_hw_ops mv_soc_ops = {
793 .phy_errata = mv6_phy_errata,
794 .enable_leds = mv_soc_enable_leds,
795 .read_preamp = mv_soc_read_preamp,
796 .reset_hc = mv_soc_reset_hc,
797 .reset_flash = mv_soc_reset_flash,
798 .reset_bus = mv_soc_reset_bus,
799};
800
Brett Russ20f733e2005-09-01 18:26:17 -0400801/*
802 * Functions
803 */
804
805static inline void writelfl(unsigned long data, void __iomem *addr)
806{
807 writel(data, addr);
808 (void) readl(addr); /* flush to avoid PCI posted write */
809}
810
Jeff Garzikc9d39132005-11-13 17:47:51 -0500811static inline unsigned int mv_hc_from_port(unsigned int port)
812{
813 return port >> MV_PORT_HC_SHIFT;
814}
815
816static inline unsigned int mv_hardport_from_port(unsigned int port)
817{
818 return port & MV_PORT_MASK;
819}
820
Mark Lord1cfd19a2008-04-19 15:05:50 -0400821/*
822 * Consolidate some rather tricky bit shift calculations.
823 * This is hot-path stuff, so not a function.
824 * Simple code, with two return values, so macro rather than inline.
825 *
826 * port is the sole input, in range 0..7.
Mark Lord7368f912008-04-25 11:24:24 -0400827 * shift is one output, for use with main_irq_cause / main_irq_mask registers.
828 * hardport is the other output, in range 0..3.
Mark Lord1cfd19a2008-04-19 15:05:50 -0400829 *
830 * Note that port and hardport may be the same variable in some cases.
831 */
832#define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
833{ \
834 shift = mv_hc_from_port(port) * HC_SHIFT; \
835 hardport = mv_hardport_from_port(port); \
836 shift += hardport * 2; \
837}
838
Mark Lord352fab72008-04-19 14:43:42 -0400839static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
840{
841 return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
842}
843
Jeff Garzikc9d39132005-11-13 17:47:51 -0500844static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
845 unsigned int port)
846{
847 return mv_hc_base(base, mv_hc_from_port(port));
848}
849
Brett Russ20f733e2005-09-01 18:26:17 -0400850static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
851{
Jeff Garzikc9d39132005-11-13 17:47:51 -0500852 return mv_hc_base_from_port(base, port) +
Jeff Garzik8b260242005-11-12 12:32:50 -0500853 MV_SATAHC_ARBTR_REG_SZ +
Jeff Garzikc9d39132005-11-13 17:47:51 -0500854 (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
Brett Russ20f733e2005-09-01 18:26:17 -0400855}
856
Mark Lorde12bef52008-03-31 19:33:56 -0400857static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
858{
859 void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
860 unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
861
862 return hc_mmio + ofs;
863}
864
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500865static inline void __iomem *mv_host_base(struct ata_host *host)
866{
867 struct mv_host_priv *hpriv = host->private_data;
868 return hpriv->base;
869}
870
Brett Russ20f733e2005-09-01 18:26:17 -0400871static inline void __iomem *mv_ap_base(struct ata_port *ap)
872{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500873 return mv_port_base(mv_host_base(ap->host), ap->port_no);
Brett Russ20f733e2005-09-01 18:26:17 -0400874}
875
Jeff Garzikcca39742006-08-24 03:19:22 -0400876static inline int mv_get_hc_count(unsigned long port_flags)
Brett Russ20f733e2005-09-01 18:26:17 -0400877{
Jeff Garzikcca39742006-08-24 03:19:22 -0400878 return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
Brett Russ20f733e2005-09-01 18:26:17 -0400879}
880
Mark Lord08da1752009-02-25 15:13:03 -0500881/**
882 * mv_save_cached_regs - (re-)initialize cached port registers
883 * @ap: the port whose registers we are caching
884 *
885 * Initialize the local cache of port registers,
886 * so that reading them over and over again can
887 * be avoided on the hotter paths of this driver.
888 * This saves a few microseconds each time we switch
889 * to/from EDMA mode to perform (eg.) a drive cache flush.
890 */
891static void mv_save_cached_regs(struct ata_port *ap)
892{
893 void __iomem *port_mmio = mv_ap_base(ap);
894 struct mv_port_priv *pp = ap->private_data;
895
896 pp->cached.fiscfg = readl(port_mmio + FISCFG_OFS);
897 pp->cached.ltmode = readl(port_mmio + LTMODE_OFS);
898 pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND_OFS);
Mark Lordc01e8a22009-02-25 15:14:48 -0500899 pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD_OFS);
Mark Lord08da1752009-02-25 15:13:03 -0500900}
901
902/**
903 * mv_write_cached_reg - write to a cached port register
904 * @addr: hardware address of the register
905 * @old: pointer to cached value of the register
906 * @new: new value for the register
907 *
908 * Write a new value to a cached register,
909 * but only if the value is different from before.
910 */
911static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
912{
913 if (new != *old) {
914 *old = new;
915 writel(new, addr);
916 }
917}
918
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400919static void mv_set_edma_ptrs(void __iomem *port_mmio,
920 struct mv_host_priv *hpriv,
921 struct mv_port_priv *pp)
922{
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400923 u32 index;
924
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400925 /*
926 * initialize request queue
927 */
Mark Lordfcfb1f72008-04-19 15:06:40 -0400928 pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
929 index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400930
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400931 WARN_ON(pp->crqb_dma & 0x3ff);
932 writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400933 writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400934 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
Mark Lord5cf73bf2008-05-27 17:58:56 -0400935 writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400936
937 /*
938 * initialize response queue
939 */
Mark Lordfcfb1f72008-04-19 15:06:40 -0400940 pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
941 index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400942
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400943 WARN_ON(pp->crpb_dma & 0xff);
944 writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
Mark Lord5cf73bf2008-05-27 17:58:56 -0400945 writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400946 writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400947 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400948}
949
Mark Lord2b748a02009-03-10 22:01:17 -0400950static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
951{
952 /*
953 * When writing to the main_irq_mask in hardware,
954 * we must ensure exclusivity between the interrupt coalescing bits
955 * and the corresponding individual port DONE_IRQ bits.
956 *
957 * Note that this register is really an "IRQ enable" register,
958 * not an "IRQ mask" register as Marvell's naming might suggest.
959 */
960 if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
961 mask &= ~DONE_IRQ_0_3;
962 if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
963 mask &= ~DONE_IRQ_4_7;
964 writelfl(mask, hpriv->main_irq_mask_addr);
965}
966
Mark Lordc4de5732008-05-17 13:35:21 -0400967static void mv_set_main_irq_mask(struct ata_host *host,
968 u32 disable_bits, u32 enable_bits)
969{
970 struct mv_host_priv *hpriv = host->private_data;
971 u32 old_mask, new_mask;
972
Mark Lord96e2c482008-05-17 13:38:00 -0400973 old_mask = hpriv->main_irq_mask;
Mark Lordc4de5732008-05-17 13:35:21 -0400974 new_mask = (old_mask & ~disable_bits) | enable_bits;
Mark Lord96e2c482008-05-17 13:38:00 -0400975 if (new_mask != old_mask) {
976 hpriv->main_irq_mask = new_mask;
Mark Lord2b748a02009-03-10 22:01:17 -0400977 mv_write_main_irq_mask(new_mask, hpriv);
Mark Lord96e2c482008-05-17 13:38:00 -0400978 }
Mark Lordc4de5732008-05-17 13:35:21 -0400979}
980
981static void mv_enable_port_irqs(struct ata_port *ap,
982 unsigned int port_bits)
983{
984 unsigned int shift, hardport, port = ap->port_no;
985 u32 disable_bits, enable_bits;
986
987 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
988
989 disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
990 enable_bits = port_bits << shift;
991 mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
992}
993
Mark Lord00b81232009-01-30 18:47:51 -0500994static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
995 void __iomem *port_mmio,
996 unsigned int port_irqs)
997{
998 struct mv_host_priv *hpriv = ap->host->private_data;
999 int hardport = mv_hardport_from_port(ap->port_no);
1000 void __iomem *hc_mmio = mv_hc_base_from_port(
1001 mv_host_base(ap->host), ap->port_no);
1002 u32 hc_irq_cause;
1003
1004 /* clear EDMA event indicators, if any */
1005 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1006
1007 /* clear pending irq events */
1008 hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1009 writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
1010
1011 /* clear FIS IRQ Cause */
1012 if (IS_GEN_IIE(hpriv))
1013 writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
1014
1015 mv_enable_port_irqs(ap, port_irqs);
1016}
1017
Mark Lord2b748a02009-03-10 22:01:17 -04001018static void mv_set_irq_coalescing(struct ata_host *host,
1019 unsigned int count, unsigned int usecs)
1020{
1021 struct mv_host_priv *hpriv = host->private_data;
1022 void __iomem *mmio = hpriv->base, *hc_mmio;
1023 u32 coal_enable = 0;
1024 unsigned long flags;
Mark Lord6abf4672009-03-11 00:56:00 -04001025 unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
Mark Lord2b748a02009-03-10 22:01:17 -04001026 const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
1027 ALL_PORTS_COAL_DONE;
1028
1029 /* Disable IRQ coalescing if either threshold is zero */
1030 if (!usecs || !count) {
1031 clks = count = 0;
1032 } else {
1033 /* Respect maximum limits of the hardware */
1034 clks = usecs * COAL_CLOCKS_PER_USEC;
1035 if (clks > MAX_COAL_TIME_THRESHOLD)
1036 clks = MAX_COAL_TIME_THRESHOLD;
1037 if (count > MAX_COAL_IO_COUNT)
1038 count = MAX_COAL_IO_COUNT;
1039 }
1040
1041 spin_lock_irqsave(&host->lock, flags);
Mark Lord6abf4672009-03-11 00:56:00 -04001042 mv_set_main_irq_mask(host, coal_disable, 0);
Mark Lord2b748a02009-03-10 22:01:17 -04001043
Mark Lord6abf4672009-03-11 00:56:00 -04001044 if (is_dual_hc && !IS_GEN_I(hpriv)) {
Mark Lord2b748a02009-03-10 22:01:17 -04001045 /*
Mark Lord6abf4672009-03-11 00:56:00 -04001046 * GEN_II/GEN_IIE with dual host controllers:
1047 * one set of global thresholds for the entire chip.
Mark Lord2b748a02009-03-10 22:01:17 -04001048 */
1049 writel(clks, mmio + MV_IRQ_COAL_TIME_THRESHOLD);
1050 writel(count, mmio + MV_IRQ_COAL_IO_THRESHOLD);
1051 /* clear leftover coal IRQ bit */
Mark Lord6abf4672009-03-11 00:56:00 -04001052 writel(~ALL_PORTS_COAL_IRQ, mmio + MV_IRQ_COAL_CAUSE);
1053 if (count)
1054 coal_enable = ALL_PORTS_COAL_DONE;
1055 clks = count = 0; /* force clearing of regular regs below */
Mark Lord2b748a02009-03-10 22:01:17 -04001056 }
Mark Lord6abf4672009-03-11 00:56:00 -04001057
Mark Lord2b748a02009-03-10 22:01:17 -04001058 /*
1059 * All chips: independent thresholds for each HC on the chip.
1060 */
1061 hc_mmio = mv_hc_base_from_port(mmio, 0);
1062 writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD_OFS);
1063 writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD_OFS);
Mark Lord6abf4672009-03-11 00:56:00 -04001064 writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE_OFS);
1065 if (count)
1066 coal_enable |= PORTS_0_3_COAL_DONE;
1067 if (is_dual_hc) {
Mark Lord2b748a02009-03-10 22:01:17 -04001068 hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
1069 writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD_OFS);
1070 writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD_OFS);
Mark Lord6abf4672009-03-11 00:56:00 -04001071 writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE_OFS);
1072 if (count)
1073 coal_enable |= PORTS_4_7_COAL_DONE;
Mark Lord2b748a02009-03-10 22:01:17 -04001074 }
Mark Lord2b748a02009-03-10 22:01:17 -04001075
Mark Lord6abf4672009-03-11 00:56:00 -04001076 mv_set_main_irq_mask(host, 0, coal_enable);
Mark Lord2b748a02009-03-10 22:01:17 -04001077 spin_unlock_irqrestore(&host->lock, flags);
1078}
1079
Brett Russ05b308e2005-10-05 17:08:53 -04001080/**
Mark Lord00b81232009-01-30 18:47:51 -05001081 * mv_start_edma - Enable eDMA engine
Brett Russ05b308e2005-10-05 17:08:53 -04001082 * @base: port base address
1083 * @pp: port private data
1084 *
Tejun Heobeec7db2006-02-11 19:11:13 +09001085 * Verify the local cache of the eDMA state is accurate with a
1086 * WARN_ON.
Brett Russ05b308e2005-10-05 17:08:53 -04001087 *
1088 * LOCKING:
1089 * Inherited from caller.
1090 */
Mark Lord00b81232009-01-30 18:47:51 -05001091static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
Mark Lord72109162008-01-26 18:31:33 -05001092 struct mv_port_priv *pp, u8 protocol)
Brett Russ31961942005-09-30 01:36:00 -04001093{
Mark Lord72109162008-01-26 18:31:33 -05001094 int want_ncq = (protocol == ATA_PROT_NCQ);
1095
1096 if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
1097 int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
1098 if (want_ncq != using_ncq)
Mark Lordb5624682008-03-31 19:34:40 -04001099 mv_stop_edma(ap);
Mark Lord72109162008-01-26 18:31:33 -05001100 }
Jeff Garzikc5d3e452007-07-11 18:30:50 -04001101 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
Mark Lord0c589122008-01-26 18:31:16 -05001102 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lord0c589122008-01-26 18:31:16 -05001103
Mark Lord00b81232009-01-30 18:47:51 -05001104 mv_edma_cfg(ap, want_ncq, 1);
Mark Lord0c589122008-01-26 18:31:16 -05001105
Mark Lordf630d562008-01-26 18:31:00 -05001106 mv_set_edma_ptrs(port_mmio, hpriv, pp);
Mark Lord00b81232009-01-30 18:47:51 -05001107 mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001108
Mark Lordf630d562008-01-26 18:31:00 -05001109 writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
Brett Russafb0edd2005-10-05 17:08:42 -04001110 pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
1111 }
Brett Russ31961942005-09-30 01:36:00 -04001112}
1113
Mark Lord9b2c4e02008-05-02 02:09:14 -04001114static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
1115{
1116 void __iomem *port_mmio = mv_ap_base(ap);
1117 const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
1118 const int per_loop = 5, timeout = (15 * 1000 / per_loop);
1119 int i;
1120
1121 /*
1122 * Wait for the EDMA engine to finish transactions in progress.
Mark Lordc46938c2008-05-02 14:02:28 -04001123 * No idea what a good "timeout" value might be, but measurements
1124 * indicate that it often requires hundreds of microseconds
1125 * with two drives in-use. So we use the 15msec value above
1126 * as a rough guess at what even more drives might require.
Mark Lord9b2c4e02008-05-02 02:09:14 -04001127 */
1128 for (i = 0; i < timeout; ++i) {
1129 u32 edma_stat = readl(port_mmio + EDMA_STATUS_OFS);
1130 if ((edma_stat & empty_idle) == empty_idle)
1131 break;
1132 udelay(per_loop);
1133 }
1134 /* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
1135}
1136
Brett Russ05b308e2005-10-05 17:08:53 -04001137/**
Mark Lorde12bef52008-03-31 19:33:56 -04001138 * mv_stop_edma_engine - Disable eDMA engine
Mark Lordb5624682008-03-31 19:34:40 -04001139 * @port_mmio: io base address
Brett Russ05b308e2005-10-05 17:08:53 -04001140 *
1141 * LOCKING:
1142 * Inherited from caller.
1143 */
Mark Lordb5624682008-03-31 19:34:40 -04001144static int mv_stop_edma_engine(void __iomem *port_mmio)
Brett Russ31961942005-09-30 01:36:00 -04001145{
Mark Lordb5624682008-03-31 19:34:40 -04001146 int i;
Brett Russ31961942005-09-30 01:36:00 -04001147
Mark Lordb5624682008-03-31 19:34:40 -04001148 /* Disable eDMA. The disable bit auto clears. */
1149 writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
Jeff Garzik8b260242005-11-12 12:32:50 -05001150
Mark Lordb5624682008-03-31 19:34:40 -04001151 /* Wait for the chip to confirm eDMA is off. */
1152 for (i = 10000; i > 0; i--) {
1153 u32 reg = readl(port_mmio + EDMA_CMD_OFS);
Jeff Garzik4537deb2007-07-12 14:30:19 -04001154 if (!(reg & EDMA_EN))
Mark Lordb5624682008-03-31 19:34:40 -04001155 return 0;
1156 udelay(10);
Brett Russ31961942005-09-30 01:36:00 -04001157 }
Mark Lordb5624682008-03-31 19:34:40 -04001158 return -EIO;
Brett Russ31961942005-09-30 01:36:00 -04001159}
1160
Mark Lorde12bef52008-03-31 19:33:56 -04001161static int mv_stop_edma(struct ata_port *ap)
Jeff Garzik0ea9e172007-07-13 17:06:45 -04001162{
Mark Lordb5624682008-03-31 19:34:40 -04001163 void __iomem *port_mmio = mv_ap_base(ap);
1164 struct mv_port_priv *pp = ap->private_data;
Mark Lord66e57a22009-01-30 18:52:58 -05001165 int err = 0;
Jeff Garzik0ea9e172007-07-13 17:06:45 -04001166
Mark Lordb5624682008-03-31 19:34:40 -04001167 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
1168 return 0;
1169 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Mark Lord9b2c4e02008-05-02 02:09:14 -04001170 mv_wait_for_edma_empty_idle(ap);
Mark Lordb5624682008-03-31 19:34:40 -04001171 if (mv_stop_edma_engine(port_mmio)) {
1172 ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
Mark Lord66e57a22009-01-30 18:52:58 -05001173 err = -EIO;
Mark Lordb5624682008-03-31 19:34:40 -04001174 }
Mark Lord66e57a22009-01-30 18:52:58 -05001175 mv_edma_cfg(ap, 0, 0);
1176 return err;
Jeff Garzik0ea9e172007-07-13 17:06:45 -04001177}
1178
Jeff Garzik8a70f8d2005-10-05 17:19:47 -04001179#ifdef ATA_DEBUG
Brett Russ31961942005-09-30 01:36:00 -04001180static void mv_dump_mem(void __iomem *start, unsigned bytes)
1181{
Brett Russ31961942005-09-30 01:36:00 -04001182 int b, w;
1183 for (b = 0; b < bytes; ) {
1184 DPRINTK("%p: ", start + b);
1185 for (w = 0; b < bytes && w < 4; w++) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001186 printk("%08x ", readl(start + b));
Brett Russ31961942005-09-30 01:36:00 -04001187 b += sizeof(u32);
1188 }
1189 printk("\n");
1190 }
Brett Russ31961942005-09-30 01:36:00 -04001191}
Jeff Garzik8a70f8d2005-10-05 17:19:47 -04001192#endif
1193
Brett Russ31961942005-09-30 01:36:00 -04001194static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
1195{
1196#ifdef ATA_DEBUG
1197 int b, w;
1198 u32 dw;
1199 for (b = 0; b < bytes; ) {
1200 DPRINTK("%02x: ", b);
1201 for (w = 0; b < bytes && w < 4; w++) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001202 (void) pci_read_config_dword(pdev, b, &dw);
1203 printk("%08x ", dw);
Brett Russ31961942005-09-30 01:36:00 -04001204 b += sizeof(u32);
1205 }
1206 printk("\n");
1207 }
1208#endif
1209}
1210static void mv_dump_all_regs(void __iomem *mmio_base, int port,
1211 struct pci_dev *pdev)
1212{
1213#ifdef ATA_DEBUG
Jeff Garzik8b260242005-11-12 12:32:50 -05001214 void __iomem *hc_base = mv_hc_base(mmio_base,
Brett Russ31961942005-09-30 01:36:00 -04001215 port >> MV_PORT_HC_SHIFT);
1216 void __iomem *port_base;
1217 int start_port, num_ports, p, start_hc, num_hcs, hc;
1218
1219 if (0 > port) {
1220 start_hc = start_port = 0;
1221 num_ports = 8; /* shld be benign for 4 port devs */
1222 num_hcs = 2;
1223 } else {
1224 start_hc = port >> MV_PORT_HC_SHIFT;
1225 start_port = port;
1226 num_ports = num_hcs = 1;
1227 }
Jeff Garzik8b260242005-11-12 12:32:50 -05001228 DPRINTK("All registers for port(s) %u-%u:\n", start_port,
Brett Russ31961942005-09-30 01:36:00 -04001229 num_ports > 1 ? num_ports - 1 : start_port);
1230
1231 if (NULL != pdev) {
1232 DPRINTK("PCI config space regs:\n");
1233 mv_dump_pci_cfg(pdev, 0x68);
1234 }
1235 DPRINTK("PCI regs:\n");
1236 mv_dump_mem(mmio_base+0xc00, 0x3c);
1237 mv_dump_mem(mmio_base+0xd00, 0x34);
1238 mv_dump_mem(mmio_base+0xf00, 0x4);
1239 mv_dump_mem(mmio_base+0x1d00, 0x6c);
1240 for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
Dan Alonid220c372006-04-10 23:20:22 -07001241 hc_base = mv_hc_base(mmio_base, hc);
Brett Russ31961942005-09-30 01:36:00 -04001242 DPRINTK("HC regs (HC %i):\n", hc);
1243 mv_dump_mem(hc_base, 0x1c);
1244 }
1245 for (p = start_port; p < start_port + num_ports; p++) {
1246 port_base = mv_port_base(mmio_base, p);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001247 DPRINTK("EDMA regs (port %i):\n", p);
Brett Russ31961942005-09-30 01:36:00 -04001248 mv_dump_mem(port_base, 0x54);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001249 DPRINTK("SATA regs (port %i):\n", p);
Brett Russ31961942005-09-30 01:36:00 -04001250 mv_dump_mem(port_base+0x300, 0x60);
1251 }
1252#endif
1253}
1254
Brett Russ20f733e2005-09-01 18:26:17 -04001255static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1256{
1257 unsigned int ofs;
1258
1259 switch (sc_reg_in) {
1260 case SCR_STATUS:
1261 case SCR_CONTROL:
1262 case SCR_ERROR:
1263 ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
1264 break;
1265 case SCR_ACTIVE:
1266 ofs = SATA_ACTIVE_OFS; /* active is not with the others */
1267 break;
1268 default:
1269 ofs = 0xffffffffU;
1270 break;
1271 }
1272 return ofs;
1273}
1274
Tejun Heo82ef04f2008-07-31 17:02:40 +09001275static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
Brett Russ20f733e2005-09-01 18:26:17 -04001276{
1277 unsigned int ofs = mv_scr_offset(sc_reg_in);
1278
Tejun Heoda3dbb12007-07-16 14:29:40 +09001279 if (ofs != 0xffffffffU) {
Tejun Heo82ef04f2008-07-31 17:02:40 +09001280 *val = readl(mv_ap_base(link->ap) + ofs);
Tejun Heoda3dbb12007-07-16 14:29:40 +09001281 return 0;
1282 } else
1283 return -EINVAL;
Brett Russ20f733e2005-09-01 18:26:17 -04001284}
1285
Tejun Heo82ef04f2008-07-31 17:02:40 +09001286static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
Brett Russ20f733e2005-09-01 18:26:17 -04001287{
1288 unsigned int ofs = mv_scr_offset(sc_reg_in);
1289
Tejun Heoda3dbb12007-07-16 14:29:40 +09001290 if (ofs != 0xffffffffU) {
Tejun Heo82ef04f2008-07-31 17:02:40 +09001291 writelfl(val, mv_ap_base(link->ap) + ofs);
Tejun Heoda3dbb12007-07-16 14:29:40 +09001292 return 0;
1293 } else
1294 return -EINVAL;
Brett Russ20f733e2005-09-01 18:26:17 -04001295}
1296
Mark Lordf2738272008-01-26 18:32:29 -05001297static void mv6_dev_config(struct ata_device *adev)
1298{
1299 /*
Mark Lorde49856d2008-04-16 14:59:07 -04001300 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1301 *
1302 * Gen-II does not support NCQ over a port multiplier
1303 * (no FIS-based switching).
Mark Lordf2738272008-01-26 18:32:29 -05001304 */
Mark Lorde49856d2008-04-16 14:59:07 -04001305 if (adev->flags & ATA_DFLAG_NCQ) {
Mark Lord352fab72008-04-19 14:43:42 -04001306 if (sata_pmp_attached(adev->link->ap)) {
Mark Lorde49856d2008-04-16 14:59:07 -04001307 adev->flags &= ~ATA_DFLAG_NCQ;
Mark Lord352fab72008-04-19 14:43:42 -04001308 ata_dev_printk(adev, KERN_INFO,
1309 "NCQ disabled for command-based switching\n");
Mark Lord352fab72008-04-19 14:43:42 -04001310 }
Mark Lorde49856d2008-04-16 14:59:07 -04001311 }
Mark Lordf2738272008-01-26 18:32:29 -05001312}
1313
Mark Lord3e4a1392008-05-02 02:10:02 -04001314static int mv_qc_defer(struct ata_queued_cmd *qc)
1315{
1316 struct ata_link *link = qc->dev->link;
1317 struct ata_port *ap = link->ap;
1318 struct mv_port_priv *pp = ap->private_data;
1319
1320 /*
Mark Lord29d187b2008-05-02 02:15:37 -04001321 * Don't allow new commands if we're in a delayed EH state
1322 * for NCQ and/or FIS-based switching.
1323 */
1324 if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
1325 return ATA_DEFER_PORT;
1326 /*
Mark Lord3e4a1392008-05-02 02:10:02 -04001327 * If the port is completely idle, then allow the new qc.
1328 */
1329 if (ap->nr_active_links == 0)
1330 return 0;
1331
Tejun Heo4bdee6c2008-08-13 20:24:16 +09001332 /*
1333 * The port is operating in host queuing mode (EDMA) with NCQ
1334 * enabled, allow multiple NCQ commands. EDMA also allows
1335 * queueing multiple DMA commands but libata core currently
1336 * doesn't allow it.
1337 */
1338 if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
1339 (pp->pp_flags & MV_PP_FLAG_NCQ_EN) && ata_is_ncq(qc->tf.protocol))
1340 return 0;
1341
Mark Lord3e4a1392008-05-02 02:10:02 -04001342 return ATA_DEFER_PORT;
1343}
1344
Mark Lord08da1752009-02-25 15:13:03 -05001345static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
Mark Lorde49856d2008-04-16 14:59:07 -04001346{
Mark Lord08da1752009-02-25 15:13:03 -05001347 struct mv_port_priv *pp = ap->private_data;
1348 void __iomem *port_mmio;
Mark Lord00f42ea2008-05-02 02:11:45 -04001349
Mark Lord08da1752009-02-25 15:13:03 -05001350 u32 fiscfg, *old_fiscfg = &pp->cached.fiscfg;
1351 u32 ltmode, *old_ltmode = &pp->cached.ltmode;
1352 u32 haltcond, *old_haltcond = &pp->cached.haltcond;
Mark Lord00f42ea2008-05-02 02:11:45 -04001353
Mark Lord08da1752009-02-25 15:13:03 -05001354 ltmode = *old_ltmode & ~LTMODE_BIT8;
1355 haltcond = *old_haltcond | EDMA_ERR_DEV;
Mark Lord00f42ea2008-05-02 02:11:45 -04001356
1357 if (want_fbs) {
Mark Lord08da1752009-02-25 15:13:03 -05001358 fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
1359 ltmode = *old_ltmode | LTMODE_BIT8;
Mark Lord4c299ca2008-05-02 02:16:20 -04001360 if (want_ncq)
Mark Lord08da1752009-02-25 15:13:03 -05001361 haltcond &= ~EDMA_ERR_DEV;
Mark Lord4c299ca2008-05-02 02:16:20 -04001362 else
Mark Lord08da1752009-02-25 15:13:03 -05001363 fiscfg |= FISCFG_WAIT_DEV_ERR;
1364 } else {
1365 fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
Mark Lorde49856d2008-04-16 14:59:07 -04001366 }
Mark Lord00f42ea2008-05-02 02:11:45 -04001367
Mark Lord08da1752009-02-25 15:13:03 -05001368 port_mmio = mv_ap_base(ap);
1369 mv_write_cached_reg(port_mmio + FISCFG_OFS, old_fiscfg, fiscfg);
1370 mv_write_cached_reg(port_mmio + LTMODE_OFS, old_ltmode, ltmode);
1371 mv_write_cached_reg(port_mmio + EDMA_HALTCOND_OFS, old_haltcond, haltcond);
Mark Lord0c589122008-01-26 18:31:16 -05001372}
Jeff Garzike4e7b892006-01-31 12:18:41 -05001373
Mark Lorddd2890f2008-05-02 02:10:56 -04001374static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1375{
1376 struct mv_host_priv *hpriv = ap->host->private_data;
1377 u32 old, new;
1378
1379 /* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1380 old = readl(hpriv->base + MV_GPIO_PORT_CTL_OFS);
1381 if (want_ncq)
1382 new = old | (1 << 22);
1383 else
1384 new = old & ~(1 << 22);
1385 if (new != old)
1386 writel(new, hpriv->base + MV_GPIO_PORT_CTL_OFS);
1387}
1388
Mark Lordc01e8a22009-02-25 15:14:48 -05001389/**
Mark Lord40f21b12009-03-10 18:51:04 -04001390 * mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
1391 * @ap: Port being initialized
Mark Lordc01e8a22009-02-25 15:14:48 -05001392 *
1393 * There are two DMA modes on these chips: basic DMA, and EDMA.
1394 *
1395 * Bit-0 of the "EDMA RESERVED" register enables/disables use
1396 * of basic DMA on the GEN_IIE versions of the chips.
1397 *
1398 * This bit survives EDMA resets, and must be set for basic DMA
1399 * to function, and should be cleared when EDMA is active.
1400 */
1401static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
1402{
1403 struct mv_port_priv *pp = ap->private_data;
1404 u32 new, *old = &pp->cached.unknown_rsvd;
1405
1406 if (enable_bmdma)
1407 new = *old | 1;
1408 else
1409 new = *old & ~1;
1410 mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD_OFS, old, new);
1411}
1412
Mark Lord000b3442009-03-15 11:33:19 -04001413/*
1414 * SOC chips have an issue whereby the HDD LEDs don't always blink
1415 * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
1416 * of the SOC takes care of it, generating a steady blink rate when
1417 * any drive on the chip is active.
1418 *
1419 * Unfortunately, the blink mode is a global hardware setting for the SOC,
1420 * so we must use it whenever at least one port on the SOC has NCQ enabled.
1421 *
1422 * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
1423 * LED operation works then, and provides better (more accurate) feedback.
1424 *
1425 * Note that this code assumes that an SOC never has more than one HC onboard.
1426 */
1427static void mv_soc_led_blink_enable(struct ata_port *ap)
1428{
1429 struct ata_host *host = ap->host;
1430 struct mv_host_priv *hpriv = host->private_data;
1431 void __iomem *hc_mmio;
1432 u32 led_ctrl;
1433
1434 if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
1435 return;
1436 hpriv->hp_flags |= MV_HP_QUIRK_LED_BLINK_EN;
1437 hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1438 led_ctrl = readl(hc_mmio + SOC_LED_CTRL_OFS);
1439 writel(led_ctrl | SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL_OFS);
1440}
1441
1442static void mv_soc_led_blink_disable(struct ata_port *ap)
1443{
1444 struct ata_host *host = ap->host;
1445 struct mv_host_priv *hpriv = host->private_data;
1446 void __iomem *hc_mmio;
1447 u32 led_ctrl;
1448 unsigned int port;
1449
1450 if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
1451 return;
1452
1453 /* disable led-blink only if no ports are using NCQ */
1454 for (port = 0; port < hpriv->n_ports; port++) {
1455 struct ata_port *this_ap = host->ports[port];
1456 struct mv_port_priv *pp = this_ap->private_data;
1457
1458 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1459 return;
1460 }
1461
1462 hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
1463 hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1464 led_ctrl = readl(hc_mmio + SOC_LED_CTRL_OFS);
1465 writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL_OFS);
1466}
1467
Mark Lord00b81232009-01-30 18:47:51 -05001468static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
Jeff Garzike4e7b892006-01-31 12:18:41 -05001469{
1470 u32 cfg;
Mark Lorde12bef52008-03-31 19:33:56 -04001471 struct mv_port_priv *pp = ap->private_data;
1472 struct mv_host_priv *hpriv = ap->host->private_data;
1473 void __iomem *port_mmio = mv_ap_base(ap);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001474
1475 /* set up non-NCQ EDMA configuration */
1476 cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
Mark Lordd16ab3f2009-02-25 15:17:43 -05001477 pp->pp_flags &=
1478 ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001479
1480 if (IS_GEN_I(hpriv))
1481 cfg |= (1 << 8); /* enab config burst size mask */
1482
Mark Lorddd2890f2008-05-02 02:10:56 -04001483 else if (IS_GEN_II(hpriv)) {
Jeff Garzike4e7b892006-01-31 12:18:41 -05001484 cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
Mark Lorddd2890f2008-05-02 02:10:56 -04001485 mv_60x1_errata_sata25(ap, want_ncq);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001486
Mark Lorddd2890f2008-05-02 02:10:56 -04001487 } else if (IS_GEN_IIE(hpriv)) {
Mark Lord00f42ea2008-05-02 02:11:45 -04001488 int want_fbs = sata_pmp_attached(ap);
1489 /*
1490 * Possible future enhancement:
1491 *
1492 * The chip can use FBS with non-NCQ, if we allow it,
1493 * But first we need to have the error handling in place
1494 * for this mode (datasheet section 7.3.15.4.2.3).
1495 * So disallow non-NCQ FBS for now.
1496 */
1497 want_fbs &= want_ncq;
1498
Mark Lord08da1752009-02-25 15:13:03 -05001499 mv_config_fbs(ap, want_ncq, want_fbs);
Mark Lord00f42ea2008-05-02 02:11:45 -04001500
1501 if (want_fbs) {
1502 pp->pp_flags |= MV_PP_FLAG_FBS_EN;
1503 cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
1504 }
1505
Jeff Garzike728eab2007-02-25 02:53:41 -05001506 cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
Mark Lord00b81232009-01-30 18:47:51 -05001507 if (want_edma) {
1508 cfg |= (1 << 22); /* enab 4-entry host queue cache */
1509 if (!IS_SOC(hpriv))
1510 cfg |= (1 << 18); /* enab early completion */
1511 }
Mark Lord616d4a92008-05-02 02:08:32 -04001512 if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1513 cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
Mark Lordc01e8a22009-02-25 15:14:48 -05001514 mv_bmdma_enable_iie(ap, !want_edma);
Mark Lord000b3442009-03-15 11:33:19 -04001515
1516 if (IS_SOC(hpriv)) {
1517 if (want_ncq)
1518 mv_soc_led_blink_enable(ap);
1519 else
1520 mv_soc_led_blink_disable(ap);
1521 }
Jeff Garzike4e7b892006-01-31 12:18:41 -05001522 }
1523
Mark Lord72109162008-01-26 18:31:33 -05001524 if (want_ncq) {
1525 cfg |= EDMA_CFG_NCQ;
1526 pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
Mark Lord00b81232009-01-30 18:47:51 -05001527 }
Mark Lord72109162008-01-26 18:31:33 -05001528
Jeff Garzike4e7b892006-01-31 12:18:41 -05001529 writelfl(cfg, port_mmio + EDMA_CFG_OFS);
1530}
1531
Mark Lordda2fa9b2008-01-26 18:32:45 -05001532static void mv_port_free_dma_mem(struct ata_port *ap)
1533{
1534 struct mv_host_priv *hpriv = ap->host->private_data;
1535 struct mv_port_priv *pp = ap->private_data;
Mark Lordeb73d552008-01-29 13:24:00 -05001536 int tag;
Mark Lordda2fa9b2008-01-26 18:32:45 -05001537
1538 if (pp->crqb) {
1539 dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1540 pp->crqb = NULL;
1541 }
1542 if (pp->crpb) {
1543 dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1544 pp->crpb = NULL;
1545 }
Mark Lordeb73d552008-01-29 13:24:00 -05001546 /*
1547 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1548 * For later hardware, we have one unique sg_tbl per NCQ tag.
1549 */
1550 for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1551 if (pp->sg_tbl[tag]) {
1552 if (tag == 0 || !IS_GEN_I(hpriv))
1553 dma_pool_free(hpriv->sg_tbl_pool,
1554 pp->sg_tbl[tag],
1555 pp->sg_tbl_dma[tag]);
1556 pp->sg_tbl[tag] = NULL;
1557 }
Mark Lordda2fa9b2008-01-26 18:32:45 -05001558 }
1559}
1560
Brett Russ05b308e2005-10-05 17:08:53 -04001561/**
1562 * mv_port_start - Port specific init/start routine.
1563 * @ap: ATA channel to manipulate
1564 *
1565 * Allocate and point to DMA memory, init port private memory,
1566 * zero indices.
1567 *
1568 * LOCKING:
1569 * Inherited from caller.
1570 */
Brett Russ31961942005-09-30 01:36:00 -04001571static int mv_port_start(struct ata_port *ap)
1572{
Jeff Garzikcca39742006-08-24 03:19:22 -04001573 struct device *dev = ap->host->dev;
1574 struct mv_host_priv *hpriv = ap->host->private_data;
Brett Russ31961942005-09-30 01:36:00 -04001575 struct mv_port_priv *pp;
James Bottomleydde20202008-02-19 11:36:56 +01001576 int tag;
Brett Russ31961942005-09-30 01:36:00 -04001577
Tejun Heo24dc5f32007-01-20 16:00:28 +09001578 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05001579 if (!pp)
Tejun Heo24dc5f32007-01-20 16:00:28 +09001580 return -ENOMEM;
Mark Lordda2fa9b2008-01-26 18:32:45 -05001581 ap->private_data = pp;
Brett Russ31961942005-09-30 01:36:00 -04001582
Mark Lordda2fa9b2008-01-26 18:32:45 -05001583 pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1584 if (!pp->crqb)
1585 return -ENOMEM;
1586 memset(pp->crqb, 0, MV_CRQB_Q_SZ);
Brett Russ31961942005-09-30 01:36:00 -04001587
Mark Lordda2fa9b2008-01-26 18:32:45 -05001588 pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1589 if (!pp->crpb)
1590 goto out_port_free_dma_mem;
1591 memset(pp->crpb, 0, MV_CRPB_Q_SZ);
Brett Russ31961942005-09-30 01:36:00 -04001592
Mark Lord3bd0a702008-06-18 12:11:16 -04001593 /* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
1594 if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
1595 ap->flags |= ATA_FLAG_AN;
Mark Lordeb73d552008-01-29 13:24:00 -05001596 /*
1597 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1598 * For later hardware, we need one unique sg_tbl per NCQ tag.
1599 */
1600 for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1601 if (tag == 0 || !IS_GEN_I(hpriv)) {
1602 pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1603 GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1604 if (!pp->sg_tbl[tag])
1605 goto out_port_free_dma_mem;
1606 } else {
1607 pp->sg_tbl[tag] = pp->sg_tbl[0];
1608 pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1609 }
1610 }
Mark Lord08da1752009-02-25 15:13:03 -05001611 mv_save_cached_regs(ap);
Mark Lord66e57a22009-01-30 18:52:58 -05001612 mv_edma_cfg(ap, 0, 0);
Brett Russ31961942005-09-30 01:36:00 -04001613 return 0;
Mark Lordda2fa9b2008-01-26 18:32:45 -05001614
1615out_port_free_dma_mem:
1616 mv_port_free_dma_mem(ap);
1617 return -ENOMEM;
Brett Russ31961942005-09-30 01:36:00 -04001618}
1619
Brett Russ05b308e2005-10-05 17:08:53 -04001620/**
1621 * mv_port_stop - Port specific cleanup/stop routine.
1622 * @ap: ATA channel to manipulate
1623 *
1624 * Stop DMA, cleanup port memory.
1625 *
1626 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001627 * This routine uses the host lock to protect the DMA stop.
Brett Russ05b308e2005-10-05 17:08:53 -04001628 */
Brett Russ31961942005-09-30 01:36:00 -04001629static void mv_port_stop(struct ata_port *ap)
1630{
Mark Lorde12bef52008-03-31 19:33:56 -04001631 mv_stop_edma(ap);
Mark Lord88e675e2008-05-17 13:36:30 -04001632 mv_enable_port_irqs(ap, 0);
Mark Lordda2fa9b2008-01-26 18:32:45 -05001633 mv_port_free_dma_mem(ap);
Brett Russ31961942005-09-30 01:36:00 -04001634}
1635
Brett Russ05b308e2005-10-05 17:08:53 -04001636/**
1637 * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1638 * @qc: queued command whose SG list to source from
1639 *
1640 * Populate the SG list and mark the last entry.
1641 *
1642 * LOCKING:
1643 * Inherited from caller.
1644 */
Jeff Garzik6c087722007-10-12 00:16:23 -04001645static void mv_fill_sg(struct ata_queued_cmd *qc)
Brett Russ31961942005-09-30 01:36:00 -04001646{
1647 struct mv_port_priv *pp = qc->ap->private_data;
Jeff Garzik972c26b2005-10-18 22:14:54 -04001648 struct scatterlist *sg;
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04001649 struct mv_sg *mv_sg, *last_sg = NULL;
Tejun Heoff2aeb12007-12-05 16:43:11 +09001650 unsigned int si;
Brett Russ31961942005-09-30 01:36:00 -04001651
Mark Lordeb73d552008-01-29 13:24:00 -05001652 mv_sg = pp->sg_tbl[qc->tag];
Tejun Heoff2aeb12007-12-05 16:43:11 +09001653 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Jeff Garzikd88184f2007-02-26 01:26:06 -05001654 dma_addr_t addr = sg_dma_address(sg);
1655 u32 sg_len = sg_dma_len(sg);
Brett Russ31961942005-09-30 01:36:00 -04001656
Olof Johansson4007b492007-10-02 20:45:27 -05001657 while (sg_len) {
1658 u32 offset = addr & 0xffff;
1659 u32 len = sg_len;
Brett Russ31961942005-09-30 01:36:00 -04001660
Mark Lord32cd11a2009-02-01 16:50:32 -05001661 if (offset + len > 0x10000)
Olof Johansson4007b492007-10-02 20:45:27 -05001662 len = 0x10000 - offset;
Jeff Garzik972c26b2005-10-18 22:14:54 -04001663
Olof Johansson4007b492007-10-02 20:45:27 -05001664 mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1665 mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
Jeff Garzik6c087722007-10-12 00:16:23 -04001666 mv_sg->flags_size = cpu_to_le32(len & 0xffff);
Mark Lord32cd11a2009-02-01 16:50:32 -05001667 mv_sg->reserved = 0;
Olof Johansson4007b492007-10-02 20:45:27 -05001668
1669 sg_len -= len;
1670 addr += len;
1671
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04001672 last_sg = mv_sg;
Olof Johansson4007b492007-10-02 20:45:27 -05001673 mv_sg++;
Olof Johansson4007b492007-10-02 20:45:27 -05001674 }
Brett Russ31961942005-09-30 01:36:00 -04001675 }
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04001676
1677 if (likely(last_sg))
1678 last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
Mark Lord32cd11a2009-02-01 16:50:32 -05001679 mb(); /* ensure data structure is visible to the chipset */
Brett Russ31961942005-09-30 01:36:00 -04001680}
1681
Jeff Garzik5796d1c2007-10-26 00:03:37 -04001682static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
Brett Russ31961942005-09-30 01:36:00 -04001683{
Mark Lord559eeda2006-05-19 16:40:15 -04001684 u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
Brett Russ31961942005-09-30 01:36:00 -04001685 (last ? CRQB_CMD_LAST : 0);
Mark Lord559eeda2006-05-19 16:40:15 -04001686 *cmdw = cpu_to_le16(tmp);
Brett Russ31961942005-09-30 01:36:00 -04001687}
1688
Brett Russ05b308e2005-10-05 17:08:53 -04001689/**
Mark Lordda142652009-01-30 18:51:54 -05001690 * mv_sff_irq_clear - Clear hardware interrupt after DMA.
1691 * @ap: Port associated with this ATA transaction.
1692 *
1693 * We need this only for ATAPI bmdma transactions,
1694 * as otherwise we experience spurious interrupts
1695 * after libata-sff handles the bmdma interrupts.
1696 */
1697static void mv_sff_irq_clear(struct ata_port *ap)
1698{
1699 mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
1700}
1701
1702/**
1703 * mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
1704 * @qc: queued command to check for chipset/DMA compatibility.
1705 *
1706 * The bmdma engines cannot handle speculative data sizes
1707 * (bytecount under/over flow). So only allow DMA for
1708 * data transfer commands with known data sizes.
1709 *
1710 * LOCKING:
1711 * Inherited from caller.
1712 */
1713static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
1714{
1715 struct scsi_cmnd *scmd = qc->scsicmd;
1716
1717 if (scmd) {
1718 switch (scmd->cmnd[0]) {
1719 case READ_6:
1720 case READ_10:
1721 case READ_12:
1722 case WRITE_6:
1723 case WRITE_10:
1724 case WRITE_12:
1725 case GPCMD_READ_CD:
1726 case GPCMD_SEND_DVD_STRUCTURE:
1727 case GPCMD_SEND_CUE_SHEET:
1728 return 0; /* DMA is safe */
1729 }
1730 }
1731 return -EOPNOTSUPP; /* use PIO instead */
1732}
1733
1734/**
1735 * mv_bmdma_setup - Set up BMDMA transaction
1736 * @qc: queued command to prepare DMA for.
1737 *
1738 * LOCKING:
1739 * Inherited from caller.
1740 */
1741static void mv_bmdma_setup(struct ata_queued_cmd *qc)
1742{
1743 struct ata_port *ap = qc->ap;
1744 void __iomem *port_mmio = mv_ap_base(ap);
1745 struct mv_port_priv *pp = ap->private_data;
1746
1747 mv_fill_sg(qc);
1748
1749 /* clear all DMA cmd bits */
1750 writel(0, port_mmio + BMDMA_CMD_OFS);
1751
1752 /* load PRD table addr. */
1753 writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
1754 port_mmio + BMDMA_PRD_HIGH_OFS);
1755 writelfl(pp->sg_tbl_dma[qc->tag],
1756 port_mmio + BMDMA_PRD_LOW_OFS);
1757
1758 /* issue r/w command */
1759 ap->ops->sff_exec_command(ap, &qc->tf);
1760}
1761
1762/**
1763 * mv_bmdma_start - Start a BMDMA transaction
1764 * @qc: queued command to start DMA on.
1765 *
1766 * LOCKING:
1767 * Inherited from caller.
1768 */
1769static void mv_bmdma_start(struct ata_queued_cmd *qc)
1770{
1771 struct ata_port *ap = qc->ap;
1772 void __iomem *port_mmio = mv_ap_base(ap);
1773 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
1774 u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
1775
1776 /* start host DMA transaction */
1777 writelfl(cmd, port_mmio + BMDMA_CMD_OFS);
1778}
1779
1780/**
1781 * mv_bmdma_stop - Stop BMDMA transfer
1782 * @qc: queued command to stop DMA on.
1783 *
1784 * Clears the ATA_DMA_START flag in the bmdma control register
1785 *
1786 * LOCKING:
1787 * Inherited from caller.
1788 */
1789static void mv_bmdma_stop(struct ata_queued_cmd *qc)
1790{
1791 struct ata_port *ap = qc->ap;
1792 void __iomem *port_mmio = mv_ap_base(ap);
1793 u32 cmd;
1794
1795 /* clear start/stop bit */
1796 cmd = readl(port_mmio + BMDMA_CMD_OFS);
1797 cmd &= ~ATA_DMA_START;
1798 writelfl(cmd, port_mmio + BMDMA_CMD_OFS);
1799
1800 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
1801 ata_sff_dma_pause(ap);
1802}
1803
1804/**
1805 * mv_bmdma_status - Read BMDMA status
1806 * @ap: port for which to retrieve DMA status.
1807 *
1808 * Read and return equivalent of the sff BMDMA status register.
1809 *
1810 * LOCKING:
1811 * Inherited from caller.
1812 */
1813static u8 mv_bmdma_status(struct ata_port *ap)
1814{
1815 void __iomem *port_mmio = mv_ap_base(ap);
1816 u32 reg, status;
1817
1818 /*
1819 * Other bits are valid only if ATA_DMA_ACTIVE==0,
1820 * and the ATA_DMA_INTR bit doesn't exist.
1821 */
1822 reg = readl(port_mmio + BMDMA_STATUS_OFS);
1823 if (reg & ATA_DMA_ACTIVE)
1824 status = ATA_DMA_ACTIVE;
1825 else
1826 status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
1827 return status;
1828}
1829
1830/**
Brett Russ05b308e2005-10-05 17:08:53 -04001831 * mv_qc_prep - Host specific command preparation.
1832 * @qc: queued command to prepare
1833 *
1834 * This routine simply redirects to the general purpose routine
1835 * if command is not DMA. Else, it handles prep of the CRQB
1836 * (command request block), does some sanity checking, and calls
1837 * the SG load routine.
1838 *
1839 * LOCKING:
1840 * Inherited from caller.
1841 */
Brett Russ31961942005-09-30 01:36:00 -04001842static void mv_qc_prep(struct ata_queued_cmd *qc)
1843{
1844 struct ata_port *ap = qc->ap;
1845 struct mv_port_priv *pp = ap->private_data;
Mark Lorde1469872006-05-22 19:02:03 -04001846 __le16 *cw;
Brett Russ31961942005-09-30 01:36:00 -04001847 struct ata_taskfile *tf;
1848 u16 flags = 0;
Mark Lorda6432432006-05-19 16:36:36 -04001849 unsigned in_index;
Brett Russ31961942005-09-30 01:36:00 -04001850
Mark Lord138bfdd2008-01-26 18:33:18 -05001851 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1852 (qc->tf.protocol != ATA_PROT_NCQ))
Brett Russ31961942005-09-30 01:36:00 -04001853 return;
Brett Russ20f733e2005-09-01 18:26:17 -04001854
Brett Russ31961942005-09-30 01:36:00 -04001855 /* Fill in command request block
1856 */
Jeff Garzike4e7b892006-01-31 12:18:41 -05001857 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
Brett Russ31961942005-09-30 01:36:00 -04001858 flags |= CRQB_FLAG_READ;
Tejun Heobeec7db2006-02-11 19:11:13 +09001859 WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
Brett Russ31961942005-09-30 01:36:00 -04001860 flags |= qc->tag << CRQB_TAG_SHIFT;
Mark Lorde49856d2008-04-16 14:59:07 -04001861 flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
Brett Russ31961942005-09-30 01:36:00 -04001862
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001863 /* get current queue index from software */
Mark Lordfcfb1f72008-04-19 15:06:40 -04001864 in_index = pp->req_idx;
Brett Russ31961942005-09-30 01:36:00 -04001865
Mark Lorda6432432006-05-19 16:36:36 -04001866 pp->crqb[in_index].sg_addr =
Mark Lordeb73d552008-01-29 13:24:00 -05001867 cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
Mark Lorda6432432006-05-19 16:36:36 -04001868 pp->crqb[in_index].sg_addr_hi =
Mark Lordeb73d552008-01-29 13:24:00 -05001869 cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
Mark Lorda6432432006-05-19 16:36:36 -04001870 pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
1871
1872 cw = &pp->crqb[in_index].ata_cmd[0];
Brett Russ31961942005-09-30 01:36:00 -04001873 tf = &qc->tf;
1874
1875 /* Sadly, the CRQB cannot accomodate all registers--there are
1876 * only 11 bytes...so we must pick and choose required
1877 * registers based on the command. So, we drop feature and
1878 * hob_feature for [RW] DMA commands, but they are needed for
Mark Lordcd12e1f2009-01-19 18:06:28 -05001879 * NCQ. NCQ will drop hob_nsect, which is not needed there
1880 * (nsect is used only for the tag; feat/hob_feat hold true nsect).
Brett Russ31961942005-09-30 01:36:00 -04001881 */
1882 switch (tf->command) {
1883 case ATA_CMD_READ:
1884 case ATA_CMD_READ_EXT:
1885 case ATA_CMD_WRITE:
1886 case ATA_CMD_WRITE_EXT:
Jens Axboec15d85c2006-02-15 15:59:25 +01001887 case ATA_CMD_WRITE_FUA_EXT:
Brett Russ31961942005-09-30 01:36:00 -04001888 mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1889 break;
Brett Russ31961942005-09-30 01:36:00 -04001890 case ATA_CMD_FPDMA_READ:
1891 case ATA_CMD_FPDMA_WRITE:
Jeff Garzik8b260242005-11-12 12:32:50 -05001892 mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
Brett Russ31961942005-09-30 01:36:00 -04001893 mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1894 break;
Brett Russ31961942005-09-30 01:36:00 -04001895 default:
1896 /* The only other commands EDMA supports in non-queued and
1897 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1898 * of which are defined/used by Linux. If we get here, this
1899 * driver needs work.
1900 *
1901 * FIXME: modify libata to give qc_prep a return value and
1902 * return error here.
1903 */
1904 BUG_ON(tf->command);
1905 break;
1906 }
1907 mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1908 mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1909 mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1910 mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1911 mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1912 mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1913 mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1914 mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1915 mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
1916
Jeff Garzike4e7b892006-01-31 12:18:41 -05001917 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
Brett Russ31961942005-09-30 01:36:00 -04001918 return;
Jeff Garzike4e7b892006-01-31 12:18:41 -05001919 mv_fill_sg(qc);
1920}
1921
1922/**
1923 * mv_qc_prep_iie - Host specific command preparation.
1924 * @qc: queued command to prepare
1925 *
1926 * This routine simply redirects to the general purpose routine
1927 * if command is not DMA. Else, it handles prep of the CRQB
1928 * (command request block), does some sanity checking, and calls
1929 * the SG load routine.
1930 *
1931 * LOCKING:
1932 * Inherited from caller.
1933 */
1934static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
1935{
1936 struct ata_port *ap = qc->ap;
1937 struct mv_port_priv *pp = ap->private_data;
1938 struct mv_crqb_iie *crqb;
1939 struct ata_taskfile *tf;
Mark Lorda6432432006-05-19 16:36:36 -04001940 unsigned in_index;
Jeff Garzike4e7b892006-01-31 12:18:41 -05001941 u32 flags = 0;
1942
Mark Lord138bfdd2008-01-26 18:33:18 -05001943 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1944 (qc->tf.protocol != ATA_PROT_NCQ))
Jeff Garzike4e7b892006-01-31 12:18:41 -05001945 return;
1946
Mark Lorde12bef52008-03-31 19:33:56 -04001947 /* Fill in Gen IIE command request block */
Jeff Garzike4e7b892006-01-31 12:18:41 -05001948 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1949 flags |= CRQB_FLAG_READ;
1950
Tejun Heobeec7db2006-02-11 19:11:13 +09001951 WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001952 flags |= qc->tag << CRQB_TAG_SHIFT;
Mark Lord8c0aeb42008-01-26 18:31:48 -05001953 flags |= qc->tag << CRQB_HOSTQ_SHIFT;
Mark Lorde49856d2008-04-16 14:59:07 -04001954 flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
Jeff Garzike4e7b892006-01-31 12:18:41 -05001955
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001956 /* get current queue index from software */
Mark Lordfcfb1f72008-04-19 15:06:40 -04001957 in_index = pp->req_idx;
Mark Lorda6432432006-05-19 16:36:36 -04001958
1959 crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
Mark Lordeb73d552008-01-29 13:24:00 -05001960 crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1961 crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001962 crqb->flags = cpu_to_le32(flags);
1963
1964 tf = &qc->tf;
1965 crqb->ata_cmd[0] = cpu_to_le32(
1966 (tf->command << 16) |
1967 (tf->feature << 24)
1968 );
1969 crqb->ata_cmd[1] = cpu_to_le32(
1970 (tf->lbal << 0) |
1971 (tf->lbam << 8) |
1972 (tf->lbah << 16) |
1973 (tf->device << 24)
1974 );
1975 crqb->ata_cmd[2] = cpu_to_le32(
1976 (tf->hob_lbal << 0) |
1977 (tf->hob_lbam << 8) |
1978 (tf->hob_lbah << 16) |
1979 (tf->hob_feature << 24)
1980 );
1981 crqb->ata_cmd[3] = cpu_to_le32(
1982 (tf->nsect << 0) |
1983 (tf->hob_nsect << 8)
1984 );
1985
1986 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1987 return;
Brett Russ31961942005-09-30 01:36:00 -04001988 mv_fill_sg(qc);
1989}
1990
Brett Russ05b308e2005-10-05 17:08:53 -04001991/**
Mark Lordd16ab3f2009-02-25 15:17:43 -05001992 * mv_sff_check_status - fetch device status, if valid
1993 * @ap: ATA port to fetch status from
1994 *
1995 * When using command issue via mv_qc_issue_fis(),
1996 * the initial ATA_BUSY state does not show up in the
1997 * ATA status (shadow) register. This can confuse libata!
1998 *
1999 * So we have a hook here to fake ATA_BUSY for that situation,
2000 * until the first time a BUSY, DRQ, or ERR bit is seen.
2001 *
2002 * The rest of the time, it simply returns the ATA status register.
2003 */
2004static u8 mv_sff_check_status(struct ata_port *ap)
2005{
2006 u8 stat = ioread8(ap->ioaddr.status_addr);
2007 struct mv_port_priv *pp = ap->private_data;
2008
2009 if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
2010 if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
2011 pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
2012 else
2013 stat = ATA_BUSY;
2014 }
2015 return stat;
2016}
2017
2018/**
Mark Lord70f8b792009-02-25 15:19:20 -05002019 * mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
2020 * @fis: fis to be sent
2021 * @nwords: number of 32-bit words in the fis
2022 */
2023static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
2024{
2025 void __iomem *port_mmio = mv_ap_base(ap);
2026 u32 ifctl, old_ifctl, ifstat;
2027 int i, timeout = 200, final_word = nwords - 1;
2028
2029 /* Initiate FIS transmission mode */
2030 old_ifctl = readl(port_mmio + SATA_IFCTL_OFS);
2031 ifctl = 0x100 | (old_ifctl & 0xf);
2032 writelfl(ifctl, port_mmio + SATA_IFCTL_OFS);
2033
2034 /* Send all words of the FIS except for the final word */
2035 for (i = 0; i < final_word; ++i)
2036 writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS_OFS);
2037
2038 /* Flag end-of-transmission, and then send the final word */
2039 writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL_OFS);
2040 writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS_OFS);
2041
2042 /*
2043 * Wait for FIS transmission to complete.
2044 * This typically takes just a single iteration.
2045 */
2046 do {
2047 ifstat = readl(port_mmio + SATA_IFSTAT_OFS);
2048 } while (!(ifstat & 0x1000) && --timeout);
2049
2050 /* Restore original port configuration */
2051 writelfl(old_ifctl, port_mmio + SATA_IFCTL_OFS);
2052
2053 /* See if it worked */
2054 if ((ifstat & 0x3000) != 0x1000) {
2055 ata_port_printk(ap, KERN_WARNING,
2056 "%s transmission error, ifstat=%08x\n",
2057 __func__, ifstat);
2058 return AC_ERR_OTHER;
2059 }
2060 return 0;
2061}
2062
2063/**
2064 * mv_qc_issue_fis - Issue a command directly as a FIS
2065 * @qc: queued command to start
2066 *
2067 * Note that the ATA shadow registers are not updated
2068 * after command issue, so the device will appear "READY"
2069 * if polled, even while it is BUSY processing the command.
2070 *
2071 * So we use a status hook to fake ATA_BUSY until the drive changes state.
2072 *
2073 * Note: we don't get updated shadow regs on *completion*
2074 * of non-data commands. So avoid sending them via this function,
2075 * as they will appear to have completed immediately.
2076 *
2077 * GEN_IIE has special registers that we could get the result tf from,
2078 * but earlier chipsets do not. For now, we ignore those registers.
2079 */
2080static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
2081{
2082 struct ata_port *ap = qc->ap;
2083 struct mv_port_priv *pp = ap->private_data;
2084 struct ata_link *link = qc->dev->link;
2085 u32 fis[5];
2086 int err = 0;
2087
2088 ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
2089 err = mv_send_fis(ap, fis, sizeof(fis) / sizeof(fis[0]));
2090 if (err)
2091 return err;
2092
2093 switch (qc->tf.protocol) {
2094 case ATAPI_PROT_PIO:
2095 pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
2096 /* fall through */
2097 case ATAPI_PROT_NODATA:
2098 ap->hsm_task_state = HSM_ST_FIRST;
2099 break;
2100 case ATA_PROT_PIO:
2101 pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
2102 if (qc->tf.flags & ATA_TFLAG_WRITE)
2103 ap->hsm_task_state = HSM_ST_FIRST;
2104 else
2105 ap->hsm_task_state = HSM_ST;
2106 break;
2107 default:
2108 ap->hsm_task_state = HSM_ST_LAST;
2109 break;
2110 }
2111
2112 if (qc->tf.flags & ATA_TFLAG_POLLING)
2113 ata_pio_queue_task(ap, qc, 0);
2114 return 0;
2115}
2116
2117/**
Brett Russ05b308e2005-10-05 17:08:53 -04002118 * mv_qc_issue - Initiate a command to the host
2119 * @qc: queued command to start
2120 *
2121 * This routine simply redirects to the general purpose routine
2122 * if command is not DMA. Else, it sanity checks our local
2123 * caches of the request producer/consumer indices then enables
2124 * DMA and bumps the request producer index.
2125 *
2126 * LOCKING:
2127 * Inherited from caller.
2128 */
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09002129static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
Brett Russ31961942005-09-30 01:36:00 -04002130{
Mark Lordf48765c2009-01-30 18:48:41 -05002131 static int limit_warnings = 10;
Jeff Garzikc5d3e452007-07-11 18:30:50 -04002132 struct ata_port *ap = qc->ap;
2133 void __iomem *port_mmio = mv_ap_base(ap);
2134 struct mv_port_priv *pp = ap->private_data;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002135 u32 in_index;
Mark Lord42ed8932009-02-25 15:15:39 -05002136 unsigned int port_irqs;
Brett Russ31961942005-09-30 01:36:00 -04002137
Mark Lordd16ab3f2009-02-25 15:17:43 -05002138 pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
2139
Mark Lordf48765c2009-01-30 18:48:41 -05002140 switch (qc->tf.protocol) {
2141 case ATA_PROT_DMA:
2142 case ATA_PROT_NCQ:
2143 mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
2144 pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2145 in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
2146
2147 /* Write the request in pointer to kick the EDMA to life */
2148 writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
2149 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
2150 return 0;
2151
2152 case ATA_PROT_PIO:
Mark Lordc6112bd2008-06-18 12:13:02 -04002153 /*
2154 * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
2155 *
2156 * Someday, we might implement special polling workarounds
2157 * for these, but it all seems rather unnecessary since we
2158 * normally use only DMA for commands which transfer more
2159 * than a single block of data.
2160 *
2161 * Much of the time, this could just work regardless.
2162 * So for now, just log the incident, and allow the attempt.
2163 */
Mark Lordc7843e82008-06-18 21:57:42 -04002164 if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
Mark Lordc6112bd2008-06-18 12:13:02 -04002165 --limit_warnings;
2166 ata_link_printk(qc->dev->link, KERN_WARNING, DRV_NAME
2167 ": attempting PIO w/multiple DRQ: "
2168 "this may fail due to h/w errata\n");
2169 }
Mark Lordf48765c2009-01-30 18:48:41 -05002170 /* drop through */
Mark Lord42ed8932009-02-25 15:15:39 -05002171 case ATA_PROT_NODATA:
Mark Lordf48765c2009-01-30 18:48:41 -05002172 case ATAPI_PROT_PIO:
Mark Lord42ed8932009-02-25 15:15:39 -05002173 case ATAPI_PROT_NODATA:
2174 if (ap->flags & ATA_FLAG_PIO_POLLING)
2175 qc->tf.flags |= ATA_TFLAG_POLLING;
2176 break;
Brett Russ31961942005-09-30 01:36:00 -04002177 }
Mark Lord42ed8932009-02-25 15:15:39 -05002178
2179 if (qc->tf.flags & ATA_TFLAG_POLLING)
2180 port_irqs = ERR_IRQ; /* mask device interrupt when polling */
2181 else
2182 port_irqs = ERR_IRQ | DONE_IRQ; /* unmask all interrupts */
2183
2184 /*
2185 * We're about to send a non-EDMA capable command to the
2186 * port. Turn off EDMA so there won't be problems accessing
2187 * shadow block, etc registers.
2188 */
2189 mv_stop_edma(ap);
2190 mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
2191 mv_pmp_select(ap, qc->dev->link->pmp);
Mark Lord70f8b792009-02-25 15:19:20 -05002192
2193 if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
2194 struct mv_host_priv *hpriv = ap->host->private_data;
2195 /*
2196 * Workaround for 88SX60x1 FEr SATA#25 (part 2).
Mark Lord40f21b12009-03-10 18:51:04 -04002197 *
Mark Lord70f8b792009-02-25 15:19:20 -05002198 * After any NCQ error, the READ_LOG_EXT command
2199 * from libata-eh *must* use mv_qc_issue_fis().
2200 * Otherwise it might fail, due to chip errata.
2201 *
2202 * Rather than special-case it, we'll just *always*
2203 * use this method here for READ_LOG_EXT, making for
2204 * easier testing.
2205 */
2206 if (IS_GEN_II(hpriv))
2207 return mv_qc_issue_fis(qc);
2208 }
Mark Lord42ed8932009-02-25 15:15:39 -05002209 return ata_sff_qc_issue(qc);
Brett Russ31961942005-09-30 01:36:00 -04002210}
2211
Mark Lord8f767f82008-04-19 14:53:07 -04002212static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
2213{
2214 struct mv_port_priv *pp = ap->private_data;
2215 struct ata_queued_cmd *qc;
2216
2217 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
2218 return NULL;
2219 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Mark Lord95db5052009-01-30 18:49:29 -05002220 if (qc) {
2221 if (qc->tf.flags & ATA_TFLAG_POLLING)
2222 qc = NULL;
2223 else if (!(qc->flags & ATA_QCFLAG_ACTIVE))
2224 qc = NULL;
2225 }
Mark Lord8f767f82008-04-19 14:53:07 -04002226 return qc;
2227}
2228
Mark Lord29d187b2008-05-02 02:15:37 -04002229static void mv_pmp_error_handler(struct ata_port *ap)
2230{
2231 unsigned int pmp, pmp_map;
2232 struct mv_port_priv *pp = ap->private_data;
2233
2234 if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
2235 /*
2236 * Perform NCQ error analysis on failed PMPs
2237 * before we freeze the port entirely.
2238 *
2239 * The failed PMPs are marked earlier by mv_pmp_eh_prep().
2240 */
2241 pmp_map = pp->delayed_eh_pmp_map;
2242 pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
2243 for (pmp = 0; pmp_map != 0; pmp++) {
2244 unsigned int this_pmp = (1 << pmp);
2245 if (pmp_map & this_pmp) {
2246 struct ata_link *link = &ap->pmp_link[pmp];
2247 pmp_map &= ~this_pmp;
2248 ata_eh_analyze_ncq_error(link);
2249 }
2250 }
2251 ata_port_freeze(ap);
2252 }
2253 sata_pmp_error_handler(ap);
2254}
2255
Mark Lord4c299ca2008-05-02 02:16:20 -04002256static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
2257{
2258 void __iomem *port_mmio = mv_ap_base(ap);
2259
2260 return readl(port_mmio + SATA_TESTCTL_OFS) >> 16;
2261}
2262
Mark Lord4c299ca2008-05-02 02:16:20 -04002263static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
2264{
2265 struct ata_eh_info *ehi;
2266 unsigned int pmp;
2267
2268 /*
2269 * Initialize EH info for PMPs which saw device errors
2270 */
2271 ehi = &ap->link.eh_info;
2272 for (pmp = 0; pmp_map != 0; pmp++) {
2273 unsigned int this_pmp = (1 << pmp);
2274 if (pmp_map & this_pmp) {
2275 struct ata_link *link = &ap->pmp_link[pmp];
2276
2277 pmp_map &= ~this_pmp;
2278 ehi = &link->eh_info;
2279 ata_ehi_clear_desc(ehi);
2280 ata_ehi_push_desc(ehi, "dev err");
2281 ehi->err_mask |= AC_ERR_DEV;
2282 ehi->action |= ATA_EH_RESET;
2283 ata_link_abort(link);
2284 }
2285 }
2286}
2287
Mark Lord06aaca32008-05-19 09:01:24 -04002288static int mv_req_q_empty(struct ata_port *ap)
2289{
2290 void __iomem *port_mmio = mv_ap_base(ap);
2291 u32 in_ptr, out_ptr;
2292
2293 in_ptr = (readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS)
2294 >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2295 out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
2296 >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2297 return (in_ptr == out_ptr); /* 1 == queue_is_empty */
2298}
2299
Mark Lord4c299ca2008-05-02 02:16:20 -04002300static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
2301{
2302 struct mv_port_priv *pp = ap->private_data;
2303 int failed_links;
2304 unsigned int old_map, new_map;
2305
2306 /*
2307 * Device error during FBS+NCQ operation:
2308 *
2309 * Set a port flag to prevent further I/O being enqueued.
2310 * Leave the EDMA running to drain outstanding commands from this port.
2311 * Perform the post-mortem/EH only when all responses are complete.
2312 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
2313 */
2314 if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
2315 pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
2316 pp->delayed_eh_pmp_map = 0;
2317 }
2318 old_map = pp->delayed_eh_pmp_map;
2319 new_map = old_map | mv_get_err_pmp_map(ap);
2320
2321 if (old_map != new_map) {
2322 pp->delayed_eh_pmp_map = new_map;
2323 mv_pmp_eh_prep(ap, new_map & ~old_map);
2324 }
Mark Lordc46938c2008-05-02 14:02:28 -04002325 failed_links = hweight16(new_map);
Mark Lord4c299ca2008-05-02 02:16:20 -04002326
2327 ata_port_printk(ap, KERN_INFO, "%s: pmp_map=%04x qc_map=%04x "
2328 "failed_links=%d nr_active_links=%d\n",
2329 __func__, pp->delayed_eh_pmp_map,
2330 ap->qc_active, failed_links,
2331 ap->nr_active_links);
2332
Mark Lord06aaca32008-05-19 09:01:24 -04002333 if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
Mark Lord4c299ca2008-05-02 02:16:20 -04002334 mv_process_crpb_entries(ap, pp);
2335 mv_stop_edma(ap);
2336 mv_eh_freeze(ap);
2337 ata_port_printk(ap, KERN_INFO, "%s: done\n", __func__);
2338 return 1; /* handled */
2339 }
2340 ata_port_printk(ap, KERN_INFO, "%s: waiting\n", __func__);
2341 return 1; /* handled */
2342}
2343
2344static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
2345{
2346 /*
2347 * Possible future enhancement:
2348 *
2349 * FBS+non-NCQ operation is not yet implemented.
2350 * See related notes in mv_edma_cfg().
2351 *
2352 * Device error during FBS+non-NCQ operation:
2353 *
2354 * We need to snapshot the shadow registers for each failed command.
2355 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
2356 */
2357 return 0; /* not handled */
2358}
2359
2360static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
2361{
2362 struct mv_port_priv *pp = ap->private_data;
2363
2364 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
2365 return 0; /* EDMA was not active: not handled */
2366 if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
2367 return 0; /* FBS was not active: not handled */
2368
2369 if (!(edma_err_cause & EDMA_ERR_DEV))
2370 return 0; /* non DEV error: not handled */
2371 edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
2372 if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
2373 return 0; /* other problems: not handled */
2374
2375 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
2376 /*
2377 * EDMA should NOT have self-disabled for this case.
2378 * If it did, then something is wrong elsewhere,
2379 * and we cannot handle it here.
2380 */
2381 if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2382 ata_port_printk(ap, KERN_WARNING,
2383 "%s: err_cause=0x%x pp_flags=0x%x\n",
2384 __func__, edma_err_cause, pp->pp_flags);
2385 return 0; /* not handled */
2386 }
2387 return mv_handle_fbs_ncq_dev_err(ap);
2388 } else {
2389 /*
2390 * EDMA should have self-disabled for this case.
2391 * If it did not, then something is wrong elsewhere,
2392 * and we cannot handle it here.
2393 */
2394 if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
2395 ata_port_printk(ap, KERN_WARNING,
2396 "%s: err_cause=0x%x pp_flags=0x%x\n",
2397 __func__, edma_err_cause, pp->pp_flags);
2398 return 0; /* not handled */
2399 }
2400 return mv_handle_fbs_non_ncq_dev_err(ap);
2401 }
2402 return 0; /* not handled */
2403}
2404
Mark Lorda9010322008-05-02 02:14:02 -04002405static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
Mark Lord8f767f82008-04-19 14:53:07 -04002406{
Mark Lord8f767f82008-04-19 14:53:07 -04002407 struct ata_eh_info *ehi = &ap->link.eh_info;
Mark Lorda9010322008-05-02 02:14:02 -04002408 char *when = "idle";
Mark Lord8f767f82008-04-19 14:53:07 -04002409
Mark Lord8f767f82008-04-19 14:53:07 -04002410 ata_ehi_clear_desc(ehi);
Mark Lorda9010322008-05-02 02:14:02 -04002411 if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
2412 when = "disabled";
2413 } else if (edma_was_enabled) {
2414 when = "EDMA enabled";
Mark Lord8f767f82008-04-19 14:53:07 -04002415 } else {
2416 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
2417 if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
Mark Lorda9010322008-05-02 02:14:02 -04002418 when = "polling";
Mark Lord8f767f82008-04-19 14:53:07 -04002419 }
Mark Lorda9010322008-05-02 02:14:02 -04002420 ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
Mark Lord8f767f82008-04-19 14:53:07 -04002421 ehi->err_mask |= AC_ERR_OTHER;
2422 ehi->action |= ATA_EH_RESET;
2423 ata_port_freeze(ap);
2424}
2425
Brett Russ05b308e2005-10-05 17:08:53 -04002426/**
Brett Russ05b308e2005-10-05 17:08:53 -04002427 * mv_err_intr - Handle error interrupts on the port
2428 * @ap: ATA channel to manipulate
2429 *
Mark Lord8d073792008-04-19 15:07:49 -04002430 * Most cases require a full reset of the chip's state machine,
2431 * which also performs a COMRESET.
2432 * Also, if the port disabled DMA, update our cached copy to match.
Brett Russ05b308e2005-10-05 17:08:53 -04002433 *
2434 * LOCKING:
2435 * Inherited from caller.
2436 */
Mark Lord37b90462008-05-02 02:12:34 -04002437static void mv_err_intr(struct ata_port *ap)
Brett Russ20f733e2005-09-01 18:26:17 -04002438{
Brett Russ31961942005-09-30 01:36:00 -04002439 void __iomem *port_mmio = mv_ap_base(ap);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002440 u32 edma_err_cause, eh_freeze_mask, serr = 0;
Mark Lorde4006072008-05-14 09:19:30 -04002441 u32 fis_cause = 0;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002442 struct mv_port_priv *pp = ap->private_data;
2443 struct mv_host_priv *hpriv = ap->host->private_data;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002444 unsigned int action = 0, err_mask = 0;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002445 struct ata_eh_info *ehi = &ap->link.eh_info;
Mark Lord37b90462008-05-02 02:12:34 -04002446 struct ata_queued_cmd *qc;
2447 int abort = 0;
Brett Russ20f733e2005-09-01 18:26:17 -04002448
Mark Lord8d073792008-04-19 15:07:49 -04002449 /*
Mark Lord37b90462008-05-02 02:12:34 -04002450 * Read and clear the SError and err_cause bits.
Mark Lorde4006072008-05-14 09:19:30 -04002451 * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
2452 * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
Mark Lord8d073792008-04-19 15:07:49 -04002453 */
Mark Lord37b90462008-05-02 02:12:34 -04002454 sata_scr_read(&ap->link, SCR_ERROR, &serr);
2455 sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
2456
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002457 edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
Mark Lorde4006072008-05-14 09:19:30 -04002458 if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2459 fis_cause = readl(port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
2460 writelfl(~fis_cause, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
2461 }
Mark Lord8d073792008-04-19 15:07:49 -04002462 writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002463
Mark Lord4c299ca2008-05-02 02:16:20 -04002464 if (edma_err_cause & EDMA_ERR_DEV) {
2465 /*
2466 * Device errors during FIS-based switching operation
2467 * require special handling.
2468 */
2469 if (mv_handle_dev_err(ap, edma_err_cause))
2470 return;
2471 }
2472
Mark Lord37b90462008-05-02 02:12:34 -04002473 qc = mv_get_active_qc(ap);
2474 ata_ehi_clear_desc(ehi);
2475 ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
2476 edma_err_cause, pp->pp_flags);
Mark Lorde4006072008-05-14 09:19:30 -04002477
Mark Lordc443c502008-05-14 09:24:39 -04002478 if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
Mark Lorde4006072008-05-14 09:19:30 -04002479 ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
Mark Lordc443c502008-05-14 09:24:39 -04002480 if (fis_cause & SATA_FIS_IRQ_AN) {
2481 u32 ec = edma_err_cause &
2482 ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
2483 sata_async_notification(ap);
2484 if (!ec)
2485 return; /* Just an AN; no need for the nukes */
2486 ata_ehi_push_desc(ehi, "SDB notify");
2487 }
2488 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002489 /*
Mark Lord352fab72008-04-19 14:43:42 -04002490 * All generations share these EDMA error cause bits:
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002491 */
Mark Lord37b90462008-05-02 02:12:34 -04002492 if (edma_err_cause & EDMA_ERR_DEV) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002493 err_mask |= AC_ERR_DEV;
Mark Lord37b90462008-05-02 02:12:34 -04002494 action |= ATA_EH_RESET;
2495 ata_ehi_push_desc(ehi, "dev error");
2496 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002497 if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
Jeff Garzik6c1153e2007-07-13 15:20:15 -04002498 EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002499 EDMA_ERR_INTRL_PAR)) {
2500 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09002501 action |= ATA_EH_RESET;
Tejun Heob64bbc32007-07-16 14:29:39 +09002502 ata_ehi_push_desc(ehi, "parity error");
Brett Russafb0edd2005-10-05 17:08:42 -04002503 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002504 if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
2505 ata_ehi_hotplugged(ehi);
2506 ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
Tejun Heob64bbc32007-07-16 14:29:39 +09002507 "dev disconnect" : "dev connect");
Tejun Heocf480622008-01-24 00:05:14 +09002508 action |= ATA_EH_RESET;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002509 }
2510
Mark Lord352fab72008-04-19 14:43:42 -04002511 /*
2512 * Gen-I has a different SELF_DIS bit,
2513 * different FREEZE bits, and no SERR bit:
2514 */
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04002515 if (IS_GEN_I(hpriv)) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002516 eh_freeze_mask = EDMA_EH_FREEZE_5;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002517 if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002518 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Tejun Heob64bbc32007-07-16 14:29:39 +09002519 ata_ehi_push_desc(ehi, "EDMA self-disable");
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002520 }
2521 } else {
2522 eh_freeze_mask = EDMA_EH_FREEZE;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002523 if (edma_err_cause & EDMA_ERR_SELF_DIS) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002524 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Tejun Heob64bbc32007-07-16 14:29:39 +09002525 ata_ehi_push_desc(ehi, "EDMA self-disable");
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002526 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002527 if (edma_err_cause & EDMA_ERR_SERR) {
Mark Lord8d073792008-04-19 15:07:49 -04002528 ata_ehi_push_desc(ehi, "SError=%08x", serr);
2529 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09002530 action |= ATA_EH_RESET;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002531 }
2532 }
Brett Russ20f733e2005-09-01 18:26:17 -04002533
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002534 if (!err_mask) {
2535 err_mask = AC_ERR_OTHER;
Tejun Heocf480622008-01-24 00:05:14 +09002536 action |= ATA_EH_RESET;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002537 }
2538
2539 ehi->serror |= serr;
2540 ehi->action |= action;
2541
2542 if (qc)
2543 qc->err_mask |= err_mask;
2544 else
2545 ehi->err_mask |= err_mask;
2546
Mark Lord37b90462008-05-02 02:12:34 -04002547 if (err_mask == AC_ERR_DEV) {
2548 /*
2549 * Cannot do ata_port_freeze() here,
2550 * because it would kill PIO access,
2551 * which is needed for further diagnosis.
2552 */
2553 mv_eh_freeze(ap);
2554 abort = 1;
2555 } else if (edma_err_cause & eh_freeze_mask) {
2556 /*
2557 * Note to self: ata_port_freeze() calls ata_port_abort()
2558 */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002559 ata_port_freeze(ap);
Mark Lord37b90462008-05-02 02:12:34 -04002560 } else {
2561 abort = 1;
2562 }
2563
2564 if (abort) {
2565 if (qc)
2566 ata_link_abort(qc->dev->link);
2567 else
2568 ata_port_abort(ap);
2569 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002570}
2571
Mark Lordfcfb1f72008-04-19 15:06:40 -04002572static void mv_process_crpb_response(struct ata_port *ap,
2573 struct mv_crpb *response, unsigned int tag, int ncq_enabled)
2574{
2575 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
2576
2577 if (qc) {
2578 u8 ata_status;
2579 u16 edma_status = le16_to_cpu(response->flags);
2580 /*
2581 * edma_status from a response queue entry:
2582 * LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
2583 * MSB is saved ATA status from command completion.
2584 */
2585 if (!ncq_enabled) {
2586 u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
2587 if (err_cause) {
2588 /*
2589 * Error will be seen/handled by mv_err_intr().
2590 * So do nothing at all here.
2591 */
2592 return;
2593 }
2594 }
2595 ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
Mark Lord37b90462008-05-02 02:12:34 -04002596 if (!ac_err_mask(ata_status))
2597 ata_qc_complete(qc);
2598 /* else: leave it for mv_err_intr() */
Mark Lordfcfb1f72008-04-19 15:06:40 -04002599 } else {
2600 ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
2601 __func__, tag);
2602 }
2603}
2604
2605static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002606{
2607 void __iomem *port_mmio = mv_ap_base(ap);
2608 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lordfcfb1f72008-04-19 15:06:40 -04002609 u32 in_index;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002610 bool work_done = false;
Mark Lordfcfb1f72008-04-19 15:06:40 -04002611 int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002612
Mark Lordfcfb1f72008-04-19 15:06:40 -04002613 /* Get the hardware queue position index */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002614 in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
2615 >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2616
Mark Lordfcfb1f72008-04-19 15:06:40 -04002617 /* Process new responses from since the last time we looked */
2618 while (in_index != pp->resp_idx) {
Jeff Garzik6c1153e2007-07-13 15:20:15 -04002619 unsigned int tag;
Mark Lordfcfb1f72008-04-19 15:06:40 -04002620 struct mv_crpb *response = &pp->crpb[pp->resp_idx];
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002621
Mark Lordfcfb1f72008-04-19 15:06:40 -04002622 pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002623
Mark Lordfcfb1f72008-04-19 15:06:40 -04002624 if (IS_GEN_I(hpriv)) {
2625 /* 50xx: no NCQ, only one command active at a time */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002626 tag = ap->link.active_tag;
Mark Lordfcfb1f72008-04-19 15:06:40 -04002627 } else {
2628 /* Gen II/IIE: get command tag from CRPB entry */
2629 tag = le16_to_cpu(response->id) & 0x1f;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002630 }
Mark Lordfcfb1f72008-04-19 15:06:40 -04002631 mv_process_crpb_response(ap, response, tag, ncq_enabled);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002632 work_done = true;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002633 }
2634
Mark Lord352fab72008-04-19 14:43:42 -04002635 /* Update the software queue position index in hardware */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002636 if (work_done)
2637 writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
Mark Lordfcfb1f72008-04-19 15:06:40 -04002638 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002639 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04002640}
2641
Mark Lorda9010322008-05-02 02:14:02 -04002642static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2643{
2644 struct mv_port_priv *pp;
2645 int edma_was_enabled;
2646
2647 if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
2648 mv_unexpected_intr(ap, 0);
2649 return;
2650 }
2651 /*
2652 * Grab a snapshot of the EDMA_EN flag setting,
2653 * so that we have a consistent view for this port,
2654 * even if something we call of our routines changes it.
2655 */
2656 pp = ap->private_data;
2657 edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2658 /*
2659 * Process completed CRPB response(s) before other events.
2660 */
2661 if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2662 mv_process_crpb_entries(ap, pp);
Mark Lord4c299ca2008-05-02 02:16:20 -04002663 if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
2664 mv_handle_fbs_ncq_dev_err(ap);
Mark Lorda9010322008-05-02 02:14:02 -04002665 }
2666 /*
2667 * Handle chip-reported errors, or continue on to handle PIO.
2668 */
2669 if (unlikely(port_cause & ERR_IRQ)) {
2670 mv_err_intr(ap);
2671 } else if (!edma_was_enabled) {
2672 struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2673 if (qc)
2674 ata_sff_host_intr(ap, qc);
2675 else
2676 mv_unexpected_intr(ap, edma_was_enabled);
2677 }
2678}
2679
Brett Russ05b308e2005-10-05 17:08:53 -04002680/**
2681 * mv_host_intr - Handle all interrupts on the given host controller
Jeff Garzikcca39742006-08-24 03:19:22 -04002682 * @host: host specific structure
Mark Lord7368f912008-04-25 11:24:24 -04002683 * @main_irq_cause: Main interrupt cause register for the chip.
Brett Russ05b308e2005-10-05 17:08:53 -04002684 *
2685 * LOCKING:
2686 * Inherited from caller.
2687 */
Mark Lord7368f912008-04-25 11:24:24 -04002688static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
Brett Russ20f733e2005-09-01 18:26:17 -04002689{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002690 struct mv_host_priv *hpriv = host->private_data;
Mark Lordeabd5eb2008-05-02 02:13:27 -04002691 void __iomem *mmio = hpriv->base, *hc_mmio;
Mark Lorda3718c12008-04-19 15:07:18 -04002692 unsigned int handled = 0, port;
Brett Russ20f733e2005-09-01 18:26:17 -04002693
Mark Lord2b748a02009-03-10 22:01:17 -04002694 /* If asserted, clear the "all ports" IRQ coalescing bit */
2695 if (main_irq_cause & ALL_PORTS_COAL_DONE)
2696 writel(~ALL_PORTS_COAL_IRQ, mmio + MV_IRQ_COAL_CAUSE);
2697
Mark Lorda3718c12008-04-19 15:07:18 -04002698 for (port = 0; port < hpriv->n_ports; port++) {
Jeff Garzikcca39742006-08-24 03:19:22 -04002699 struct ata_port *ap = host->ports[port];
Mark Lordeabd5eb2008-05-02 02:13:27 -04002700 unsigned int p, shift, hardport, port_cause;
2701
Mark Lorda3718c12008-04-19 15:07:18 -04002702 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
Mark Lorda3718c12008-04-19 15:07:18 -04002703 /*
Mark Lordeabd5eb2008-05-02 02:13:27 -04002704 * Each hc within the host has its own hc_irq_cause register,
2705 * where the interrupting ports bits get ack'd.
Mark Lorda3718c12008-04-19 15:07:18 -04002706 */
Mark Lordeabd5eb2008-05-02 02:13:27 -04002707 if (hardport == 0) { /* first port on this hc ? */
2708 u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2709 u32 port_mask, ack_irqs;
2710 /*
2711 * Skip this entire hc if nothing pending for any ports
2712 */
2713 if (!hc_cause) {
2714 port += MV_PORTS_PER_HC - 1;
2715 continue;
2716 }
2717 /*
2718 * We don't need/want to read the hc_irq_cause register,
2719 * because doing so hurts performance, and
2720 * main_irq_cause already gives us everything we need.
2721 *
2722 * But we do have to *write* to the hc_irq_cause to ack
2723 * the ports that we are handling this time through.
2724 *
2725 * This requires that we create a bitmap for those
2726 * ports which interrupted us, and use that bitmap
2727 * to ack (only) those ports via hc_irq_cause.
2728 */
2729 ack_irqs = 0;
Mark Lord2b748a02009-03-10 22:01:17 -04002730 if (hc_cause & PORTS_0_3_COAL_DONE)
2731 ack_irqs = HC_COAL_IRQ;
Mark Lordeabd5eb2008-05-02 02:13:27 -04002732 for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2733 if ((port + p) >= hpriv->n_ports)
2734 break;
2735 port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2736 if (hc_cause & port_mask)
2737 ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2738 }
Mark Lorda3718c12008-04-19 15:07:18 -04002739 hc_mmio = mv_hc_base_from_port(mmio, port);
Mark Lordeabd5eb2008-05-02 02:13:27 -04002740 writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE_OFS);
Mark Lorda3718c12008-04-19 15:07:18 -04002741 handled = 1;
2742 }
Mark Lorda9010322008-05-02 02:14:02 -04002743 /*
2744 * Handle interrupts signalled for this port:
2745 */
Mark Lordeabd5eb2008-05-02 02:13:27 -04002746 port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
Mark Lorda9010322008-05-02 02:14:02 -04002747 if (port_cause)
2748 mv_port_intr(ap, port_cause);
Brett Russ20f733e2005-09-01 18:26:17 -04002749 }
Mark Lorda3718c12008-04-19 15:07:18 -04002750 return handled;
Brett Russ20f733e2005-09-01 18:26:17 -04002751}
2752
Mark Lorda3718c12008-04-19 15:07:18 -04002753static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002754{
Mark Lord02a121d2007-12-01 13:07:22 -05002755 struct mv_host_priv *hpriv = host->private_data;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002756 struct ata_port *ap;
2757 struct ata_queued_cmd *qc;
2758 struct ata_eh_info *ehi;
2759 unsigned int i, err_mask, printed = 0;
2760 u32 err_cause;
2761
Mark Lord02a121d2007-12-01 13:07:22 -05002762 err_cause = readl(mmio + hpriv->irq_cause_ofs);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002763
2764 dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
2765 err_cause);
2766
2767 DPRINTK("All regs @ PCI error\n");
2768 mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
2769
Mark Lord02a121d2007-12-01 13:07:22 -05002770 writelfl(0, mmio + hpriv->irq_cause_ofs);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002771
2772 for (i = 0; i < host->n_ports; i++) {
2773 ap = host->ports[i];
Tejun Heo936fd732007-08-06 18:36:23 +09002774 if (!ata_link_offline(&ap->link)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002775 ehi = &ap->link.eh_info;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002776 ata_ehi_clear_desc(ehi);
2777 if (!printed++)
2778 ata_ehi_push_desc(ehi,
2779 "PCI err cause 0x%08x", err_cause);
2780 err_mask = AC_ERR_HOST_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09002781 ehi->action = ATA_EH_RESET;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002782 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002783 if (qc)
2784 qc->err_mask |= err_mask;
2785 else
2786 ehi->err_mask |= err_mask;
2787
2788 ata_port_freeze(ap);
2789 }
2790 }
Mark Lorda3718c12008-04-19 15:07:18 -04002791 return 1; /* handled */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002792}
2793
Brett Russ05b308e2005-10-05 17:08:53 -04002794/**
Jeff Garzikc5d3e452007-07-11 18:30:50 -04002795 * mv_interrupt - Main interrupt event handler
Brett Russ05b308e2005-10-05 17:08:53 -04002796 * @irq: unused
2797 * @dev_instance: private data; in this case the host structure
Brett Russ05b308e2005-10-05 17:08:53 -04002798 *
2799 * Read the read only register to determine if any host
2800 * controllers have pending interrupts. If so, call lower level
2801 * routine to handle. Also check for PCI errors which are only
2802 * reported here.
2803 *
Jeff Garzik8b260242005-11-12 12:32:50 -05002804 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04002805 * This routine holds the host lock while processing pending
Brett Russ05b308e2005-10-05 17:08:53 -04002806 * interrupts.
2807 */
David Howells7d12e782006-10-05 14:55:46 +01002808static irqreturn_t mv_interrupt(int irq, void *dev_instance)
Brett Russ20f733e2005-09-01 18:26:17 -04002809{
Jeff Garzikcca39742006-08-24 03:19:22 -04002810 struct ata_host *host = dev_instance;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002811 struct mv_host_priv *hpriv = host->private_data;
Mark Lorda3718c12008-04-19 15:07:18 -04002812 unsigned int handled = 0;
Mark Lord6d3c30e2009-01-21 10:31:29 -05002813 int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
Mark Lord96e2c482008-05-17 13:38:00 -04002814 u32 main_irq_cause, pending_irqs;
Brett Russ20f733e2005-09-01 18:26:17 -04002815
Mark Lord646a4da2008-01-26 18:30:37 -05002816 spin_lock(&host->lock);
Mark Lord6d3c30e2009-01-21 10:31:29 -05002817
2818 /* for MSI: block new interrupts while in here */
2819 if (using_msi)
Mark Lord2b748a02009-03-10 22:01:17 -04002820 mv_write_main_irq_mask(0, hpriv);
Mark Lord6d3c30e2009-01-21 10:31:29 -05002821
Mark Lord7368f912008-04-25 11:24:24 -04002822 main_irq_cause = readl(hpriv->main_irq_cause_addr);
Mark Lord96e2c482008-05-17 13:38:00 -04002823 pending_irqs = main_irq_cause & hpriv->main_irq_mask;
Mark Lord352fab72008-04-19 14:43:42 -04002824 /*
2825 * Deal with cases where we either have nothing pending, or have read
2826 * a bogus register value which can indicate HW removal or PCI fault.
Brett Russ20f733e2005-09-01 18:26:17 -04002827 */
Mark Lorda44253d2008-05-17 13:37:07 -04002828 if (pending_irqs && main_irq_cause != 0xffffffffU) {
Mark Lord1f398472008-05-27 17:54:48 -04002829 if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
Mark Lorda3718c12008-04-19 15:07:18 -04002830 handled = mv_pci_error(host, hpriv->base);
2831 else
Mark Lorda44253d2008-05-17 13:37:07 -04002832 handled = mv_host_intr(host, pending_irqs);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002833 }
Mark Lord6d3c30e2009-01-21 10:31:29 -05002834
2835 /* for MSI: unmask; interrupt cause bits will retrigger now */
2836 if (using_msi)
Mark Lord2b748a02009-03-10 22:01:17 -04002837 mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
Mark Lord6d3c30e2009-01-21 10:31:29 -05002838
Mark Lord9d51af72009-03-10 16:28:51 -04002839 spin_unlock(&host->lock);
2840
Brett Russ20f733e2005-09-01 18:26:17 -04002841 return IRQ_RETVAL(handled);
2842}
2843
Jeff Garzikc9d39132005-11-13 17:47:51 -05002844static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
2845{
2846 unsigned int ofs;
2847
2848 switch (sc_reg_in) {
2849 case SCR_STATUS:
2850 case SCR_ERROR:
2851 case SCR_CONTROL:
2852 ofs = sc_reg_in * sizeof(u32);
2853 break;
2854 default:
2855 ofs = 0xffffffffU;
2856 break;
2857 }
2858 return ofs;
2859}
2860
Tejun Heo82ef04f2008-07-31 17:02:40 +09002861static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
Jeff Garzikc9d39132005-11-13 17:47:51 -05002862{
Tejun Heo82ef04f2008-07-31 17:02:40 +09002863 struct mv_host_priv *hpriv = link->ap->host->private_data;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002864 void __iomem *mmio = hpriv->base;
Tejun Heo82ef04f2008-07-31 17:02:40 +09002865 void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002866 unsigned int ofs = mv5_scr_offset(sc_reg_in);
2867
Tejun Heoda3dbb12007-07-16 14:29:40 +09002868 if (ofs != 0xffffffffU) {
2869 *val = readl(addr + ofs);
2870 return 0;
2871 } else
2872 return -EINVAL;
Jeff Garzikc9d39132005-11-13 17:47:51 -05002873}
2874
Tejun Heo82ef04f2008-07-31 17:02:40 +09002875static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
Jeff Garzikc9d39132005-11-13 17:47:51 -05002876{
Tejun Heo82ef04f2008-07-31 17:02:40 +09002877 struct mv_host_priv *hpriv = link->ap->host->private_data;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002878 void __iomem *mmio = hpriv->base;
Tejun Heo82ef04f2008-07-31 17:02:40 +09002879 void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002880 unsigned int ofs = mv5_scr_offset(sc_reg_in);
2881
Tejun Heoda3dbb12007-07-16 14:29:40 +09002882 if (ofs != 0xffffffffU) {
Tejun Heo0d5ff562007-02-01 15:06:36 +09002883 writelfl(val, addr + ofs);
Tejun Heoda3dbb12007-07-16 14:29:40 +09002884 return 0;
2885 } else
2886 return -EINVAL;
Jeff Garzikc9d39132005-11-13 17:47:51 -05002887}
2888
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002889static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
Jeff Garzik522479f2005-11-12 22:14:02 -05002890{
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002891 struct pci_dev *pdev = to_pci_dev(host->dev);
Jeff Garzik522479f2005-11-12 22:14:02 -05002892 int early_5080;
2893
Auke Kok44c10132007-06-08 15:46:36 -07002894 early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
Jeff Garzik522479f2005-11-12 22:14:02 -05002895
2896 if (!early_5080) {
2897 u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
2898 tmp |= (1 << 0);
2899 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
2900 }
2901
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002902 mv_reset_pci_bus(host, mmio);
Jeff Garzik522479f2005-11-12 22:14:02 -05002903}
2904
2905static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2906{
Mark Lord8e7decd2008-05-02 02:07:51 -04002907 writel(0x0fcfffff, mmio + MV_FLASH_CTL_OFS);
Jeff Garzik522479f2005-11-12 22:14:02 -05002908}
2909
Jeff Garzik47c2b672005-11-12 21:13:17 -05002910static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002911 void __iomem *mmio)
2912{
Jeff Garzikc9d39132005-11-13 17:47:51 -05002913 void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
2914 u32 tmp;
2915
2916 tmp = readl(phy_mmio + MV5_PHY_MODE);
2917
2918 hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
2919 hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002920}
2921
Jeff Garzik47c2b672005-11-12 21:13:17 -05002922static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002923{
Jeff Garzik522479f2005-11-12 22:14:02 -05002924 u32 tmp;
2925
Mark Lord8e7decd2008-05-02 02:07:51 -04002926 writel(0, mmio + MV_GPIO_PORT_CTL_OFS);
Jeff Garzik522479f2005-11-12 22:14:02 -05002927
2928 /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
2929
2930 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
2931 tmp |= ~(1 << 0);
2932 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002933}
2934
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002935static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2936 unsigned int port)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002937{
Jeff Garzikc9d39132005-11-13 17:47:51 -05002938 void __iomem *phy_mmio = mv5_phy_base(mmio, port);
2939 const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
2940 u32 tmp;
2941 int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
2942
2943 if (fix_apm_sq) {
Mark Lord8e7decd2008-05-02 02:07:51 -04002944 tmp = readl(phy_mmio + MV5_LTMODE_OFS);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002945 tmp |= (1 << 19);
Mark Lord8e7decd2008-05-02 02:07:51 -04002946 writel(tmp, phy_mmio + MV5_LTMODE_OFS);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002947
Mark Lord8e7decd2008-05-02 02:07:51 -04002948 tmp = readl(phy_mmio + MV5_PHY_CTL_OFS);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002949 tmp &= ~0x3;
2950 tmp |= 0x1;
Mark Lord8e7decd2008-05-02 02:07:51 -04002951 writel(tmp, phy_mmio + MV5_PHY_CTL_OFS);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002952 }
2953
2954 tmp = readl(phy_mmio + MV5_PHY_MODE);
2955 tmp &= ~mask;
2956 tmp |= hpriv->signal[port].pre;
2957 tmp |= hpriv->signal[port].amps;
2958 writel(tmp, phy_mmio + MV5_PHY_MODE);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002959}
2960
Jeff Garzikc9d39132005-11-13 17:47:51 -05002961
2962#undef ZERO
2963#define ZERO(reg) writel(0, port_mmio + (reg))
2964static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
2965 unsigned int port)
Jeff Garzik47c2b672005-11-12 21:13:17 -05002966{
Jeff Garzikc9d39132005-11-13 17:47:51 -05002967 void __iomem *port_mmio = mv_port_base(mmio, port);
2968
Mark Lorde12bef52008-03-31 19:33:56 -04002969 mv_reset_channel(hpriv, mmio, port);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002970
2971 ZERO(0x028); /* command */
2972 writel(0x11f, port_mmio + EDMA_CFG_OFS);
2973 ZERO(0x004); /* timer */
2974 ZERO(0x008); /* irq err cause */
2975 ZERO(0x00c); /* irq err mask */
2976 ZERO(0x010); /* rq bah */
2977 ZERO(0x014); /* rq inp */
2978 ZERO(0x018); /* rq outp */
2979 ZERO(0x01c); /* respq bah */
2980 ZERO(0x024); /* respq outp */
2981 ZERO(0x020); /* respq inp */
2982 ZERO(0x02c); /* test control */
Mark Lord8e7decd2008-05-02 02:07:51 -04002983 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
Jeff Garzikc9d39132005-11-13 17:47:51 -05002984}
2985#undef ZERO
2986
2987#define ZERO(reg) writel(0, hc_mmio + (reg))
2988static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2989 unsigned int hc)
2990{
2991 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2992 u32 tmp;
2993
2994 ZERO(0x00c);
2995 ZERO(0x010);
2996 ZERO(0x014);
2997 ZERO(0x018);
2998
2999 tmp = readl(hc_mmio + 0x20);
3000 tmp &= 0x1c1c1c1c;
3001 tmp |= 0x03030303;
3002 writel(tmp, hc_mmio + 0x20);
3003}
3004#undef ZERO
3005
3006static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3007 unsigned int n_hc)
3008{
3009 unsigned int hc, port;
3010
3011 for (hc = 0; hc < n_hc; hc++) {
3012 for (port = 0; port < MV_PORTS_PER_HC; port++)
3013 mv5_reset_hc_port(hpriv, mmio,
3014 (hc * MV_PORTS_PER_HC) + port);
3015
3016 mv5_reset_one_hc(hpriv, mmio, hc);
3017 }
3018
3019 return 0;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003020}
3021
Jeff Garzik101ffae2005-11-12 22:17:49 -05003022#undef ZERO
3023#define ZERO(reg) writel(0, mmio + (reg))
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003024static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
Jeff Garzik101ffae2005-11-12 22:17:49 -05003025{
Mark Lord02a121d2007-12-01 13:07:22 -05003026 struct mv_host_priv *hpriv = host->private_data;
Jeff Garzik101ffae2005-11-12 22:17:49 -05003027 u32 tmp;
3028
Mark Lord8e7decd2008-05-02 02:07:51 -04003029 tmp = readl(mmio + MV_PCI_MODE_OFS);
Jeff Garzik101ffae2005-11-12 22:17:49 -05003030 tmp &= 0xff00ffff;
Mark Lord8e7decd2008-05-02 02:07:51 -04003031 writel(tmp, mmio + MV_PCI_MODE_OFS);
Jeff Garzik101ffae2005-11-12 22:17:49 -05003032
3033 ZERO(MV_PCI_DISC_TIMER);
3034 ZERO(MV_PCI_MSI_TRIGGER);
Mark Lord8e7decd2008-05-02 02:07:51 -04003035 writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT_OFS);
Jeff Garzik101ffae2005-11-12 22:17:49 -05003036 ZERO(MV_PCI_SERR_MASK);
Mark Lord02a121d2007-12-01 13:07:22 -05003037 ZERO(hpriv->irq_cause_ofs);
3038 ZERO(hpriv->irq_mask_ofs);
Jeff Garzik101ffae2005-11-12 22:17:49 -05003039 ZERO(MV_PCI_ERR_LOW_ADDRESS);
3040 ZERO(MV_PCI_ERR_HIGH_ADDRESS);
3041 ZERO(MV_PCI_ERR_ATTRIBUTE);
3042 ZERO(MV_PCI_ERR_COMMAND);
3043}
3044#undef ZERO
3045
3046static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3047{
3048 u32 tmp;
3049
3050 mv5_reset_flash(hpriv, mmio);
3051
Mark Lord8e7decd2008-05-02 02:07:51 -04003052 tmp = readl(mmio + MV_GPIO_PORT_CTL_OFS);
Jeff Garzik101ffae2005-11-12 22:17:49 -05003053 tmp &= 0x3;
3054 tmp |= (1 << 5) | (1 << 6);
Mark Lord8e7decd2008-05-02 02:07:51 -04003055 writel(tmp, mmio + MV_GPIO_PORT_CTL_OFS);
Jeff Garzik101ffae2005-11-12 22:17:49 -05003056}
3057
3058/**
3059 * mv6_reset_hc - Perform the 6xxx global soft reset
3060 * @mmio: base address of the HBA
3061 *
3062 * This routine only applies to 6xxx parts.
3063 *
3064 * LOCKING:
3065 * Inherited from caller.
3066 */
Jeff Garzikc9d39132005-11-13 17:47:51 -05003067static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3068 unsigned int n_hc)
Jeff Garzik101ffae2005-11-12 22:17:49 -05003069{
3070 void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
3071 int i, rc = 0;
3072 u32 t;
3073
3074 /* Following procedure defined in PCI "main command and status
3075 * register" table.
3076 */
3077 t = readl(reg);
3078 writel(t | STOP_PCI_MASTER, reg);
3079
3080 for (i = 0; i < 1000; i++) {
3081 udelay(1);
3082 t = readl(reg);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003083 if (PCI_MASTER_EMPTY & t)
Jeff Garzik101ffae2005-11-12 22:17:49 -05003084 break;
Jeff Garzik101ffae2005-11-12 22:17:49 -05003085 }
3086 if (!(PCI_MASTER_EMPTY & t)) {
3087 printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
3088 rc = 1;
3089 goto done;
3090 }
3091
3092 /* set reset */
3093 i = 5;
3094 do {
3095 writel(t | GLOB_SFT_RST, reg);
3096 t = readl(reg);
3097 udelay(1);
3098 } while (!(GLOB_SFT_RST & t) && (i-- > 0));
3099
3100 if (!(GLOB_SFT_RST & t)) {
3101 printk(KERN_ERR DRV_NAME ": can't set global reset\n");
3102 rc = 1;
3103 goto done;
3104 }
3105
3106 /* clear reset and *reenable the PCI master* (not mentioned in spec) */
3107 i = 5;
3108 do {
3109 writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
3110 t = readl(reg);
3111 udelay(1);
3112 } while ((GLOB_SFT_RST & t) && (i-- > 0));
3113
3114 if (GLOB_SFT_RST & t) {
3115 printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
3116 rc = 1;
3117 }
3118done:
3119 return rc;
3120}
3121
Jeff Garzik47c2b672005-11-12 21:13:17 -05003122static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05003123 void __iomem *mmio)
3124{
3125 void __iomem *port_mmio;
3126 u32 tmp;
3127
Mark Lord8e7decd2008-05-02 02:07:51 -04003128 tmp = readl(mmio + MV_RESET_CFG_OFS);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05003129 if ((tmp & (1 << 0)) == 0) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05003130 hpriv->signal[idx].amps = 0x7 << 8;
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05003131 hpriv->signal[idx].pre = 0x1 << 5;
3132 return;
3133 }
3134
3135 port_mmio = mv_port_base(mmio, idx);
3136 tmp = readl(port_mmio + PHY_MODE2);
3137
3138 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
3139 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
3140}
3141
Jeff Garzik47c2b672005-11-12 21:13:17 -05003142static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05003143{
Mark Lord8e7decd2008-05-02 02:07:51 -04003144 writel(0x00000060, mmio + MV_GPIO_PORT_CTL_OFS);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05003145}
3146
Jeff Garzikc9d39132005-11-13 17:47:51 -05003147static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzik2a47ce02005-11-12 23:05:14 -05003148 unsigned int port)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003149{
Jeff Garzikc9d39132005-11-13 17:47:51 -05003150 void __iomem *port_mmio = mv_port_base(mmio, port);
3151
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003152 u32 hp_flags = hpriv->hp_flags;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003153 int fix_phy_mode2 =
3154 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003155 int fix_phy_mode4 =
Jeff Garzik47c2b672005-11-12 21:13:17 -05003156 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
Mark Lord8c30a8b2008-05-27 17:56:31 -04003157 u32 m2, m3;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003158
3159 if (fix_phy_mode2) {
3160 m2 = readl(port_mmio + PHY_MODE2);
3161 m2 &= ~(1 << 16);
3162 m2 |= (1 << 31);
3163 writel(m2, port_mmio + PHY_MODE2);
3164
3165 udelay(200);
3166
3167 m2 = readl(port_mmio + PHY_MODE2);
3168 m2 &= ~((1 << 16) | (1 << 31));
3169 writel(m2, port_mmio + PHY_MODE2);
3170
3171 udelay(200);
3172 }
3173
Mark Lord8c30a8b2008-05-27 17:56:31 -04003174 /*
3175 * Gen-II/IIe PHY_MODE3 errata RM#2:
3176 * Achieves better receiver noise performance than the h/w default:
3177 */
3178 m3 = readl(port_mmio + PHY_MODE3);
3179 m3 = (m3 & 0x1f) | (0x5555601 << 5);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003180
Mark Lord0388a8c2008-05-28 13:41:52 -04003181 /* Guideline 88F5182 (GL# SATA-S11) */
3182 if (IS_SOC(hpriv))
3183 m3 &= ~0x1c;
3184
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003185 if (fix_phy_mode4) {
Mark Lordba069e32008-05-31 16:46:34 -04003186 u32 m4 = readl(port_mmio + PHY_MODE4);
3187 /*
3188 * Enforce reserved-bit restrictions on GenIIe devices only.
3189 * For earlier chipsets, force only the internal config field
3190 * (workaround for errata FEr SATA#10 part 1).
3191 */
Mark Lord8c30a8b2008-05-27 17:56:31 -04003192 if (IS_GEN_IIE(hpriv))
Mark Lordba069e32008-05-31 16:46:34 -04003193 m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
3194 else
3195 m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
Mark Lord8c30a8b2008-05-27 17:56:31 -04003196 writel(m4, port_mmio + PHY_MODE4);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003197 }
Mark Lordb406c7a2008-05-28 12:01:12 -04003198 /*
3199 * Workaround for 60x1-B2 errata SATA#13:
3200 * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
3201 * so we must always rewrite PHY_MODE3 after PHY_MODE4.
3202 */
3203 writel(m3, port_mmio + PHY_MODE3);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003204
3205 /* Revert values of pre-emphasis and signal amps to the saved ones */
3206 m2 = readl(port_mmio + PHY_MODE2);
3207
3208 m2 &= ~MV_M2_PREAMP_MASK;
Jeff Garzik2a47ce02005-11-12 23:05:14 -05003209 m2 |= hpriv->signal[port].amps;
3210 m2 |= hpriv->signal[port].pre;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003211 m2 &= ~(1 << 16);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003212
Jeff Garzike4e7b892006-01-31 12:18:41 -05003213 /* according to mvSata 3.6.1, some IIE values are fixed */
3214 if (IS_GEN_IIE(hpriv)) {
3215 m2 &= ~0xC30FF01F;
3216 m2 |= 0x0000900F;
3217 }
3218
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003219 writel(m2, port_mmio + PHY_MODE2);
3220}
3221
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003222/* TODO: use the generic LED interface to configure the SATA Presence */
3223/* & Acitivy LEDs on the board */
3224static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
3225 void __iomem *mmio)
3226{
3227 return;
3228}
3229
3230static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
3231 void __iomem *mmio)
3232{
3233 void __iomem *port_mmio;
3234 u32 tmp;
3235
3236 port_mmio = mv_port_base(mmio, idx);
3237 tmp = readl(port_mmio + PHY_MODE2);
3238
3239 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
3240 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
3241}
3242
3243#undef ZERO
3244#define ZERO(reg) writel(0, port_mmio + (reg))
3245static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
3246 void __iomem *mmio, unsigned int port)
3247{
3248 void __iomem *port_mmio = mv_port_base(mmio, port);
3249
Mark Lorde12bef52008-03-31 19:33:56 -04003250 mv_reset_channel(hpriv, mmio, port);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003251
3252 ZERO(0x028); /* command */
3253 writel(0x101f, port_mmio + EDMA_CFG_OFS);
3254 ZERO(0x004); /* timer */
3255 ZERO(0x008); /* irq err cause */
3256 ZERO(0x00c); /* irq err mask */
3257 ZERO(0x010); /* rq bah */
3258 ZERO(0x014); /* rq inp */
3259 ZERO(0x018); /* rq outp */
3260 ZERO(0x01c); /* respq bah */
3261 ZERO(0x024); /* respq outp */
3262 ZERO(0x020); /* respq inp */
3263 ZERO(0x02c); /* test control */
Mark Lord8e7decd2008-05-02 02:07:51 -04003264 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003265}
3266
3267#undef ZERO
3268
3269#define ZERO(reg) writel(0, hc_mmio + (reg))
3270static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
3271 void __iomem *mmio)
3272{
3273 void __iomem *hc_mmio = mv_hc_base(mmio, 0);
3274
3275 ZERO(0x00c);
3276 ZERO(0x010);
3277 ZERO(0x014);
3278
3279}
3280
3281#undef ZERO
3282
3283static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
3284 void __iomem *mmio, unsigned int n_hc)
3285{
3286 unsigned int port;
3287
3288 for (port = 0; port < hpriv->n_ports; port++)
3289 mv_soc_reset_hc_port(hpriv, mmio, port);
3290
3291 mv_soc_reset_one_hc(hpriv, mmio);
3292
3293 return 0;
3294}
3295
3296static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
3297 void __iomem *mmio)
3298{
3299 return;
3300}
3301
3302static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
3303{
3304 return;
3305}
3306
Mark Lord8e7decd2008-05-02 02:07:51 -04003307static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
Mark Lordb67a1062008-03-31 19:35:13 -04003308{
Mark Lord8e7decd2008-05-02 02:07:51 -04003309 u32 ifcfg = readl(port_mmio + SATA_INTERFACE_CFG_OFS);
Mark Lordb67a1062008-03-31 19:35:13 -04003310
Mark Lord8e7decd2008-05-02 02:07:51 -04003311 ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */
Mark Lordb67a1062008-03-31 19:35:13 -04003312 if (want_gen2i)
Mark Lord8e7decd2008-05-02 02:07:51 -04003313 ifcfg |= (1 << 7); /* enable gen2i speed */
3314 writelfl(ifcfg, port_mmio + SATA_INTERFACE_CFG_OFS);
Mark Lordb67a1062008-03-31 19:35:13 -04003315}
3316
Mark Lorde12bef52008-03-31 19:33:56 -04003317static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzikc9d39132005-11-13 17:47:51 -05003318 unsigned int port_no)
Brett Russ20f733e2005-09-01 18:26:17 -04003319{
Jeff Garzikc9d39132005-11-13 17:47:51 -05003320 void __iomem *port_mmio = mv_port_base(mmio, port_no);
Brett Russ20f733e2005-09-01 18:26:17 -04003321
Mark Lord8e7decd2008-05-02 02:07:51 -04003322 /*
3323 * The datasheet warns against setting EDMA_RESET when EDMA is active
3324 * (but doesn't say what the problem might be). So we first try
3325 * to disable the EDMA engine before doing the EDMA_RESET operation.
3326 */
Mark Lord0d8be5c2008-04-16 14:56:12 -04003327 mv_stop_edma_engine(port_mmio);
Mark Lord8e7decd2008-05-02 02:07:51 -04003328 writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003329
Mark Lordb67a1062008-03-31 19:35:13 -04003330 if (!IS_GEN_I(hpriv)) {
Mark Lord8e7decd2008-05-02 02:07:51 -04003331 /* Enable 3.0gb/s link speed: this survives EDMA_RESET */
3332 mv_setup_ifcfg(port_mmio, 1);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003333 }
Mark Lordb67a1062008-03-31 19:35:13 -04003334 /*
Mark Lord8e7decd2008-05-02 02:07:51 -04003335 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
Mark Lordb67a1062008-03-31 19:35:13 -04003336 * link, and physical layers. It resets all SATA interface registers
3337 * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
Brett Russ20f733e2005-09-01 18:26:17 -04003338 */
Mark Lord8e7decd2008-05-02 02:07:51 -04003339 writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
Mark Lordb67a1062008-03-31 19:35:13 -04003340 udelay(25); /* allow reset propagation */
Brett Russ31961942005-09-30 01:36:00 -04003341 writelfl(0, port_mmio + EDMA_CMD_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04003342
Jeff Garzikc9d39132005-11-13 17:47:51 -05003343 hpriv->ops->phy_errata(hpriv, mmio, port_no);
3344
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04003345 if (IS_GEN_I(hpriv))
Jeff Garzikc9d39132005-11-13 17:47:51 -05003346 mdelay(1);
3347}
3348
Mark Lorde49856d2008-04-16 14:59:07 -04003349static void mv_pmp_select(struct ata_port *ap, int pmp)
Jeff Garzikc9d39132005-11-13 17:47:51 -05003350{
Mark Lorde49856d2008-04-16 14:59:07 -04003351 if (sata_pmp_supported(ap)) {
3352 void __iomem *port_mmio = mv_ap_base(ap);
3353 u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
3354 int old = reg & 0xf;
Jeff Garzikc9d39132005-11-13 17:47:51 -05003355
Mark Lorde49856d2008-04-16 14:59:07 -04003356 if (old != pmp) {
3357 reg = (reg & ~0xf) | pmp;
3358 writelfl(reg, port_mmio + SATA_IFCTL_OFS);
3359 }
Tejun Heoda3dbb12007-07-16 14:29:40 +09003360 }
Brett Russ20f733e2005-09-01 18:26:17 -04003361}
3362
Mark Lorde49856d2008-04-16 14:59:07 -04003363static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
3364 unsigned long deadline)
Jeff Garzik22374672005-11-17 10:59:48 -05003365{
Mark Lorde49856d2008-04-16 14:59:07 -04003366 mv_pmp_select(link->ap, sata_srst_pmp(link));
3367 return sata_std_hardreset(link, class, deadline);
3368}
Jeff Garzik0ea9e172007-07-13 17:06:45 -04003369
Mark Lorde49856d2008-04-16 14:59:07 -04003370static int mv_softreset(struct ata_link *link, unsigned int *class,
3371 unsigned long deadline)
3372{
3373 mv_pmp_select(link->ap, sata_srst_pmp(link));
3374 return ata_sff_softreset(link, class, deadline);
Jeff Garzik22374672005-11-17 10:59:48 -05003375}
3376
Tejun Heocc0680a2007-08-06 18:36:23 +09003377static int mv_hardreset(struct ata_link *link, unsigned int *class,
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003378 unsigned long deadline)
3379{
Tejun Heocc0680a2007-08-06 18:36:23 +09003380 struct ata_port *ap = link->ap;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003381 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lordb5624682008-03-31 19:34:40 -04003382 struct mv_port_priv *pp = ap->private_data;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003383 void __iomem *mmio = hpriv->base;
Mark Lord0d8be5c2008-04-16 14:56:12 -04003384 int rc, attempts = 0, extra = 0;
3385 u32 sstatus;
3386 bool online;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003387
Mark Lorde12bef52008-03-31 19:33:56 -04003388 mv_reset_channel(hpriv, mmio, ap->port_no);
Mark Lordb5624682008-03-31 19:34:40 -04003389 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Mark Lordd16ab3f2009-02-25 15:17:43 -05003390 pp->pp_flags &=
3391 ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003392
Mark Lord0d8be5c2008-04-16 14:56:12 -04003393 /* Workaround for errata FEr SATA#10 (part 2) */
3394 do {
Mark Lord17c5aab2008-04-16 14:56:51 -04003395 const unsigned long *timing =
3396 sata_ehc_deb_timing(&link->eh_context);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003397
Mark Lord17c5aab2008-04-16 14:56:51 -04003398 rc = sata_link_hardreset(link, timing, deadline + extra,
3399 &online, NULL);
Mark Lord9dcffd92008-05-14 09:18:12 -04003400 rc = online ? -EAGAIN : rc;
Mark Lord17c5aab2008-04-16 14:56:51 -04003401 if (rc)
Mark Lord0d8be5c2008-04-16 14:56:12 -04003402 return rc;
Mark Lord0d8be5c2008-04-16 14:56:12 -04003403 sata_scr_read(link, SCR_STATUS, &sstatus);
3404 if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
3405 /* Force 1.5gb/s link speed and try again */
Mark Lord8e7decd2008-05-02 02:07:51 -04003406 mv_setup_ifcfg(mv_ap_base(ap), 0);
Mark Lord0d8be5c2008-04-16 14:56:12 -04003407 if (time_after(jiffies + HZ, deadline))
3408 extra = HZ; /* only extend it once, max */
3409 }
3410 } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
Mark Lord08da1752009-02-25 15:13:03 -05003411 mv_save_cached_regs(ap);
Mark Lord66e57a22009-01-30 18:52:58 -05003412 mv_edma_cfg(ap, 0, 0);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003413
Mark Lord17c5aab2008-04-16 14:56:51 -04003414 return rc;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003415}
3416
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003417static void mv_eh_freeze(struct ata_port *ap)
Brett Russ20f733e2005-09-01 18:26:17 -04003418{
Mark Lord1cfd19a2008-04-19 15:05:50 -04003419 mv_stop_edma(ap);
Mark Lordc4de5732008-05-17 13:35:21 -04003420 mv_enable_port_irqs(ap, 0);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003421}
3422
3423static void mv_eh_thaw(struct ata_port *ap)
3424{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003425 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lordc4de5732008-05-17 13:35:21 -04003426 unsigned int port = ap->port_no;
3427 unsigned int hardport = mv_hardport_from_port(port);
Mark Lord1cfd19a2008-04-19 15:05:50 -04003428 void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003429 void __iomem *port_mmio = mv_ap_base(ap);
Mark Lordc4de5732008-05-17 13:35:21 -04003430 u32 hc_irq_cause;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003431
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003432 /* clear EDMA errors on this port */
3433 writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
3434
3435 /* clear pending irq events */
Mark Lordcae6edc2009-01-19 18:05:42 -05003436 hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
Mark Lord1cfd19a2008-04-19 15:05:50 -04003437 writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04003438
Mark Lord88e675e2008-05-17 13:36:30 -04003439 mv_enable_port_irqs(ap, ERR_IRQ);
Brett Russ31961942005-09-30 01:36:00 -04003440}
3441
Brett Russ05b308e2005-10-05 17:08:53 -04003442/**
3443 * mv_port_init - Perform some early initialization on a single port.
3444 * @port: libata data structure storing shadow register addresses
3445 * @port_mmio: base address of the port
3446 *
3447 * Initialize shadow register mmio addresses, clear outstanding
3448 * interrupts on the port, and unmask interrupts for the future
3449 * start of the port.
3450 *
3451 * LOCKING:
3452 * Inherited from caller.
3453 */
Brett Russ31961942005-09-30 01:36:00 -04003454static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
3455{
Tejun Heo0d5ff562007-02-01 15:06:36 +09003456 void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
Brett Russ31961942005-09-30 01:36:00 -04003457 unsigned serr_ofs;
3458
Jeff Garzik8b260242005-11-12 12:32:50 -05003459 /* PIO related setup
Brett Russ31961942005-09-30 01:36:00 -04003460 */
3461 port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
Jeff Garzik8b260242005-11-12 12:32:50 -05003462 port->error_addr =
Brett Russ31961942005-09-30 01:36:00 -04003463 port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
3464 port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
3465 port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
3466 port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
3467 port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
3468 port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
Jeff Garzik8b260242005-11-12 12:32:50 -05003469 port->status_addr =
Brett Russ31961942005-09-30 01:36:00 -04003470 port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
3471 /* special case: control/altstatus doesn't have ATA_REG_ address */
3472 port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
3473
3474 /* unused: */
Randy Dunlap8d9db2d2007-02-16 01:40:06 -08003475 port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
Brett Russ20f733e2005-09-01 18:26:17 -04003476
Brett Russ31961942005-09-30 01:36:00 -04003477 /* Clear any currently outstanding port interrupt conditions */
3478 serr_ofs = mv_scr_offset(SCR_ERROR);
3479 writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
3480 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
3481
Mark Lord646a4da2008-01-26 18:30:37 -05003482 /* unmask all non-transient EDMA error interrupts */
3483 writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04003484
Jeff Garzik8b260242005-11-12 12:32:50 -05003485 VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
Brett Russ31961942005-09-30 01:36:00 -04003486 readl(port_mmio + EDMA_CFG_OFS),
3487 readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
3488 readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
Brett Russ20f733e2005-09-01 18:26:17 -04003489}
3490
Mark Lord616d4a92008-05-02 02:08:32 -04003491static unsigned int mv_in_pcix_mode(struct ata_host *host)
3492{
3493 struct mv_host_priv *hpriv = host->private_data;
3494 void __iomem *mmio = hpriv->base;
3495 u32 reg;
3496
Mark Lord1f398472008-05-27 17:54:48 -04003497 if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
Mark Lord616d4a92008-05-02 02:08:32 -04003498 return 0; /* not PCI-X capable */
3499 reg = readl(mmio + MV_PCI_MODE_OFS);
3500 if ((reg & MV_PCI_MODE_MASK) == 0)
3501 return 0; /* conventional PCI mode */
3502 return 1; /* chip is in PCI-X mode */
3503}
3504
3505static int mv_pci_cut_through_okay(struct ata_host *host)
3506{
3507 struct mv_host_priv *hpriv = host->private_data;
3508 void __iomem *mmio = hpriv->base;
3509 u32 reg;
3510
3511 if (!mv_in_pcix_mode(host)) {
3512 reg = readl(mmio + PCI_COMMAND_OFS);
3513 if (reg & PCI_COMMAND_MRDTRIG)
3514 return 0; /* not okay */
3515 }
3516 return 1; /* okay */
3517}
3518
Tejun Heo4447d352007-04-17 23:44:08 +09003519static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003520{
Tejun Heo4447d352007-04-17 23:44:08 +09003521 struct pci_dev *pdev = to_pci_dev(host->dev);
3522 struct mv_host_priv *hpriv = host->private_data;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003523 u32 hp_flags = hpriv->hp_flags;
3524
Jeff Garzik5796d1c2007-10-26 00:03:37 -04003525 switch (board_idx) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05003526 case chip_5080:
3527 hpriv->ops = &mv5xxx_ops;
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04003528 hp_flags |= MV_HP_GEN_I;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003529
Auke Kok44c10132007-06-08 15:46:36 -07003530 switch (pdev->revision) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05003531 case 0x1:
3532 hp_flags |= MV_HP_ERRATA_50XXB0;
3533 break;
3534 case 0x3:
3535 hp_flags |= MV_HP_ERRATA_50XXB2;
3536 break;
3537 default:
3538 dev_printk(KERN_WARNING, &pdev->dev,
3539 "Applying 50XXB2 workarounds to unknown rev\n");
3540 hp_flags |= MV_HP_ERRATA_50XXB2;
3541 break;
3542 }
3543 break;
3544
3545 case chip_504x:
3546 case chip_508x:
3547 hpriv->ops = &mv5xxx_ops;
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04003548 hp_flags |= MV_HP_GEN_I;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003549
Auke Kok44c10132007-06-08 15:46:36 -07003550 switch (pdev->revision) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05003551 case 0x0:
3552 hp_flags |= MV_HP_ERRATA_50XXB0;
3553 break;
3554 case 0x3:
3555 hp_flags |= MV_HP_ERRATA_50XXB2;
3556 break;
3557 default:
3558 dev_printk(KERN_WARNING, &pdev->dev,
3559 "Applying B2 workarounds to unknown rev\n");
3560 hp_flags |= MV_HP_ERRATA_50XXB2;
3561 break;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003562 }
3563 break;
3564
3565 case chip_604x:
3566 case chip_608x:
Jeff Garzik47c2b672005-11-12 21:13:17 -05003567 hpriv->ops = &mv6xxx_ops;
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04003568 hp_flags |= MV_HP_GEN_II;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003569
Auke Kok44c10132007-06-08 15:46:36 -07003570 switch (pdev->revision) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05003571 case 0x7:
3572 hp_flags |= MV_HP_ERRATA_60X1B2;
3573 break;
3574 case 0x9:
3575 hp_flags |= MV_HP_ERRATA_60X1C0;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003576 break;
3577 default:
3578 dev_printk(KERN_WARNING, &pdev->dev,
Jeff Garzik47c2b672005-11-12 21:13:17 -05003579 "Applying B2 workarounds to unknown rev\n");
3580 hp_flags |= MV_HP_ERRATA_60X1B2;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003581 break;
3582 }
3583 break;
3584
Jeff Garzike4e7b892006-01-31 12:18:41 -05003585 case chip_7042:
Mark Lord616d4a92008-05-02 02:08:32 -04003586 hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
Mark Lord306b30f2007-12-04 14:07:52 -05003587 if (pdev->vendor == PCI_VENDOR_ID_TTI &&
3588 (pdev->device == 0x2300 || pdev->device == 0x2310))
3589 {
Mark Lord4e520032007-12-11 12:58:05 -05003590 /*
3591 * Highpoint RocketRAID PCIe 23xx series cards:
3592 *
3593 * Unconfigured drives are treated as "Legacy"
3594 * by the BIOS, and it overwrites sector 8 with
3595 * a "Lgcy" metadata block prior to Linux boot.
3596 *
3597 * Configured drives (RAID or JBOD) leave sector 8
3598 * alone, but instead overwrite a high numbered
3599 * sector for the RAID metadata. This sector can
3600 * be determined exactly, by truncating the physical
3601 * drive capacity to a nice even GB value.
3602 *
3603 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
3604 *
3605 * Warn the user, lest they think we're just buggy.
3606 */
3607 printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
3608 " BIOS CORRUPTS DATA on all attached drives,"
3609 " regardless of if/how they are configured."
3610 " BEWARE!\n");
3611 printk(KERN_WARNING DRV_NAME ": For data safety, do not"
3612 " use sectors 8-9 on \"Legacy\" drives,"
3613 " and avoid the final two gigabytes on"
3614 " all RocketRAID BIOS initialized drives.\n");
Mark Lord306b30f2007-12-04 14:07:52 -05003615 }
Mark Lord8e7decd2008-05-02 02:07:51 -04003616 /* drop through */
Jeff Garzike4e7b892006-01-31 12:18:41 -05003617 case chip_6042:
3618 hpriv->ops = &mv6xxx_ops;
Jeff Garzike4e7b892006-01-31 12:18:41 -05003619 hp_flags |= MV_HP_GEN_IIE;
Mark Lord616d4a92008-05-02 02:08:32 -04003620 if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
3621 hp_flags |= MV_HP_CUT_THROUGH;
Jeff Garzike4e7b892006-01-31 12:18:41 -05003622
Auke Kok44c10132007-06-08 15:46:36 -07003623 switch (pdev->revision) {
Mark Lord5cf73bf2008-05-27 17:58:56 -04003624 case 0x2: /* Rev.B0: the first/only public release */
Jeff Garzike4e7b892006-01-31 12:18:41 -05003625 hp_flags |= MV_HP_ERRATA_60X1C0;
3626 break;
3627 default:
3628 dev_printk(KERN_WARNING, &pdev->dev,
3629 "Applying 60X1C0 workarounds to unknown rev\n");
3630 hp_flags |= MV_HP_ERRATA_60X1C0;
3631 break;
3632 }
3633 break;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003634 case chip_soc:
3635 hpriv->ops = &mv_soc_ops;
Saeed Bisharaeb3a55a2008-08-04 00:52:55 -11003636 hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3637 MV_HP_ERRATA_60X1C0;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003638 break;
Jeff Garzike4e7b892006-01-31 12:18:41 -05003639
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003640 default:
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003641 dev_printk(KERN_ERR, host->dev,
Jeff Garzik5796d1c2007-10-26 00:03:37 -04003642 "BUG: invalid board index %u\n", board_idx);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003643 return 1;
3644 }
3645
3646 hpriv->hp_flags = hp_flags;
Mark Lord02a121d2007-12-01 13:07:22 -05003647 if (hp_flags & MV_HP_PCIE) {
3648 hpriv->irq_cause_ofs = PCIE_IRQ_CAUSE_OFS;
3649 hpriv->irq_mask_ofs = PCIE_IRQ_MASK_OFS;
3650 hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
3651 } else {
3652 hpriv->irq_cause_ofs = PCI_IRQ_CAUSE_OFS;
3653 hpriv->irq_mask_ofs = PCI_IRQ_MASK_OFS;
3654 hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
3655 }
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003656
3657 return 0;
3658}
3659
Brett Russ05b308e2005-10-05 17:08:53 -04003660/**
Jeff Garzik47c2b672005-11-12 21:13:17 -05003661 * mv_init_host - Perform some early initialization of the host.
Tejun Heo4447d352007-04-17 23:44:08 +09003662 * @host: ATA host to initialize
3663 * @board_idx: controller index
Brett Russ05b308e2005-10-05 17:08:53 -04003664 *
3665 * If possible, do an early global reset of the host. Then do
3666 * our port init and clear/unmask all/relevant host interrupts.
3667 *
3668 * LOCKING:
3669 * Inherited from caller.
3670 */
Tejun Heo4447d352007-04-17 23:44:08 +09003671static int mv_init_host(struct ata_host *host, unsigned int board_idx)
Brett Russ20f733e2005-09-01 18:26:17 -04003672{
3673 int rc = 0, n_hc, port, hc;
Tejun Heo4447d352007-04-17 23:44:08 +09003674 struct mv_host_priv *hpriv = host->private_data;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003675 void __iomem *mmio = hpriv->base;
Jeff Garzik47c2b672005-11-12 21:13:17 -05003676
Tejun Heo4447d352007-04-17 23:44:08 +09003677 rc = mv_chip_id(host, board_idx);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003678 if (rc)
Mark Lord352fab72008-04-19 14:43:42 -04003679 goto done;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003680
Mark Lord1f398472008-05-27 17:54:48 -04003681 if (IS_SOC(hpriv)) {
Mark Lord7368f912008-04-25 11:24:24 -04003682 hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE_OFS;
3683 hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK_OFS;
Mark Lord1f398472008-05-27 17:54:48 -04003684 } else {
3685 hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE_OFS;
3686 hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK_OFS;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003687 }
Mark Lord352fab72008-04-19 14:43:42 -04003688
Thomas Reitmayr5d0fb2e2009-01-24 20:24:58 +01003689 /* initialize shadow irq mask with register's value */
3690 hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
3691
Mark Lord352fab72008-04-19 14:43:42 -04003692 /* global interrupt mask: 0 == mask everything */
Mark Lordc4de5732008-05-17 13:35:21 -04003693 mv_set_main_irq_mask(host, ~0, 0);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003694
Tejun Heo4447d352007-04-17 23:44:08 +09003695 n_hc = mv_get_hc_count(host->ports[0]->flags);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05003696
Tejun Heo4447d352007-04-17 23:44:08 +09003697 for (port = 0; port < host->n_ports; port++)
Jeff Garzik47c2b672005-11-12 21:13:17 -05003698 hpriv->ops->read_preamp(hpriv, port, mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04003699
Jeff Garzikc9d39132005-11-13 17:47:51 -05003700 rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
Jeff Garzik47c2b672005-11-12 21:13:17 -05003701 if (rc)
Brett Russ20f733e2005-09-01 18:26:17 -04003702 goto done;
Brett Russ20f733e2005-09-01 18:26:17 -04003703
Jeff Garzik522479f2005-11-12 22:14:02 -05003704 hpriv->ops->reset_flash(hpriv, mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003705 hpriv->ops->reset_bus(host, mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -05003706 hpriv->ops->enable_leds(hpriv, mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04003707
Tejun Heo4447d352007-04-17 23:44:08 +09003708 for (port = 0; port < host->n_ports; port++) {
Tejun Heocbcdd872007-08-18 13:14:55 +09003709 struct ata_port *ap = host->ports[port];
Jeff Garzik2a47ce02005-11-12 23:05:14 -05003710 void __iomem *port_mmio = mv_port_base(mmio, port);
Tejun Heocbcdd872007-08-18 13:14:55 +09003711
3712 mv_port_init(&ap->ioaddr, port_mmio);
3713
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003714#ifdef CONFIG_PCI
Mark Lord1f398472008-05-27 17:54:48 -04003715 if (!IS_SOC(hpriv)) {
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003716 unsigned int offset = port_mmio - mmio;
3717 ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
3718 ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
3719 }
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003720#endif
Brett Russ20f733e2005-09-01 18:26:17 -04003721 }
3722
3723 for (hc = 0; hc < n_hc; hc++) {
Brett Russ31961942005-09-30 01:36:00 -04003724 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3725
3726 VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
3727 "(before clear)=0x%08x\n", hc,
3728 readl(hc_mmio + HC_CFG_OFS),
3729 readl(hc_mmio + HC_IRQ_CAUSE_OFS));
3730
3731 /* Clear any currently outstanding hc interrupt conditions */
3732 writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04003733 }
3734
Mark Lord6be96ac2009-02-19 10:38:04 -05003735 /* Clear any currently outstanding host interrupt conditions */
3736 writelfl(0, mmio + hpriv->irq_cause_ofs);
Brett Russ31961942005-09-30 01:36:00 -04003737
Mark Lord6be96ac2009-02-19 10:38:04 -05003738 /* and unmask interrupt generation for host regs */
3739 writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
Jeff Garzikfb621e22007-02-25 04:19:45 -05003740
Mark Lord6be96ac2009-02-19 10:38:04 -05003741 /*
3742 * enable only global host interrupts for now.
3743 * The per-port interrupts get done later as ports are set up.
3744 */
3745 mv_set_main_irq_mask(host, 0, PCI_ERR);
Mark Lord2b748a02009-03-10 22:01:17 -04003746 mv_set_irq_coalescing(host, irq_coalescing_io_count,
3747 irq_coalescing_usecs);
Brett Russ31961942005-09-30 01:36:00 -04003748done:
Brett Russ20f733e2005-09-01 18:26:17 -04003749 return rc;
3750}
3751
Byron Bradleyfbf14e22008-02-10 21:17:30 +00003752static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
3753{
3754 hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
3755 MV_CRQB_Q_SZ, 0);
3756 if (!hpriv->crqb_pool)
3757 return -ENOMEM;
3758
3759 hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
3760 MV_CRPB_Q_SZ, 0);
3761 if (!hpriv->crpb_pool)
3762 return -ENOMEM;
3763
3764 hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
3765 MV_SG_TBL_SZ, 0);
3766 if (!hpriv->sg_tbl_pool)
3767 return -ENOMEM;
3768
3769 return 0;
3770}
3771
Lennert Buytenhek15a32632008-03-27 14:51:39 -04003772static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
3773 struct mbus_dram_target_info *dram)
3774{
3775 int i;
3776
3777 for (i = 0; i < 4; i++) {
3778 writel(0, hpriv->base + WINDOW_CTRL(i));
3779 writel(0, hpriv->base + WINDOW_BASE(i));
3780 }
3781
3782 for (i = 0; i < dram->num_cs; i++) {
3783 struct mbus_dram_window *cs = dram->cs + i;
3784
3785 writel(((cs->size - 1) & 0xffff0000) |
3786 (cs->mbus_attr << 8) |
3787 (dram->mbus_dram_target_id << 4) | 1,
3788 hpriv->base + WINDOW_CTRL(i));
3789 writel(cs->base, hpriv->base + WINDOW_BASE(i));
3790 }
3791}
3792
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003793/**
3794 * mv_platform_probe - handle a positive probe of an soc Marvell
3795 * host
3796 * @pdev: platform device found
3797 *
3798 * LOCKING:
3799 * Inherited from caller.
3800 */
3801static int mv_platform_probe(struct platform_device *pdev)
3802{
3803 static int printed_version;
3804 const struct mv_sata_platform_data *mv_platform_data;
3805 const struct ata_port_info *ppi[] =
3806 { &mv_port_info[chip_soc], NULL };
3807 struct ata_host *host;
3808 struct mv_host_priv *hpriv;
3809 struct resource *res;
3810 int n_ports, rc;
3811
3812 if (!printed_version++)
3813 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
3814
3815 /*
3816 * Simple resource validation ..
3817 */
3818 if (unlikely(pdev->num_resources != 2)) {
3819 dev_err(&pdev->dev, "invalid number of resources\n");
3820 return -EINVAL;
3821 }
3822
3823 /*
3824 * Get the register base first
3825 */
3826 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3827 if (res == NULL)
3828 return -EINVAL;
3829
3830 /* allocate host */
3831 mv_platform_data = pdev->dev.platform_data;
3832 n_ports = mv_platform_data->n_ports;
3833
3834 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
3835 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
3836
3837 if (!host || !hpriv)
3838 return -ENOMEM;
3839 host->private_data = hpriv;
3840 hpriv->n_ports = n_ports;
3841
3842 host->iomap = NULL;
Saeed Bisharaf1cb0ea2008-02-18 07:42:28 -11003843 hpriv->base = devm_ioremap(&pdev->dev, res->start,
3844 res->end - res->start + 1);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003845 hpriv->base -= MV_SATAHC0_REG_BASE;
3846
Lennert Buytenhek15a32632008-03-27 14:51:39 -04003847 /*
3848 * (Re-)program MBUS remapping windows if we are asked to.
3849 */
3850 if (mv_platform_data->dram != NULL)
3851 mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
3852
Byron Bradleyfbf14e22008-02-10 21:17:30 +00003853 rc = mv_create_dma_pools(hpriv, &pdev->dev);
3854 if (rc)
3855 return rc;
3856
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003857 /* initialize adapter */
3858 rc = mv_init_host(host, chip_soc);
3859 if (rc)
3860 return rc;
3861
3862 dev_printk(KERN_INFO, &pdev->dev,
3863 "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
3864 host->n_ports);
3865
3866 return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
3867 IRQF_SHARED, &mv6_sht);
3868}
3869
3870/*
3871 *
3872 * mv_platform_remove - unplug a platform interface
3873 * @pdev: platform device
3874 *
3875 * A platform bus SATA device has been unplugged. Perform the needed
3876 * cleanup. Also called on module unload for any active devices.
3877 */
3878static int __devexit mv_platform_remove(struct platform_device *pdev)
3879{
3880 struct device *dev = &pdev->dev;
3881 struct ata_host *host = dev_get_drvdata(dev);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003882
3883 ata_host_detach(host);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003884 return 0;
3885}
3886
3887static struct platform_driver mv_platform_driver = {
3888 .probe = mv_platform_probe,
3889 .remove = __devexit_p(mv_platform_remove),
3890 .driver = {
3891 .name = DRV_NAME,
3892 .owner = THIS_MODULE,
3893 },
3894};
3895
3896
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003897#ifdef CONFIG_PCI
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003898static int mv_pci_init_one(struct pci_dev *pdev,
3899 const struct pci_device_id *ent);
3900
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003901
3902static struct pci_driver mv_pci_driver = {
3903 .name = DRV_NAME,
3904 .id_table = mv_pci_tbl,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003905 .probe = mv_pci_init_one,
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003906 .remove = ata_pci_remove_one,
3907};
3908
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003909/* move to PCI layer or libata core? */
3910static int pci_go_64(struct pci_dev *pdev)
3911{
3912 int rc;
3913
3914 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
3915 rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3916 if (rc) {
3917 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3918 if (rc) {
3919 dev_printk(KERN_ERR, &pdev->dev,
3920 "64-bit DMA enable failed\n");
3921 return rc;
3922 }
3923 }
3924 } else {
3925 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3926 if (rc) {
3927 dev_printk(KERN_ERR, &pdev->dev,
3928 "32-bit DMA enable failed\n");
3929 return rc;
3930 }
3931 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3932 if (rc) {
3933 dev_printk(KERN_ERR, &pdev->dev,
3934 "32-bit consistent DMA enable failed\n");
3935 return rc;
3936 }
3937 }
3938
3939 return rc;
3940}
3941
Brett Russ05b308e2005-10-05 17:08:53 -04003942/**
3943 * mv_print_info - Dump key info to kernel log for perusal.
Tejun Heo4447d352007-04-17 23:44:08 +09003944 * @host: ATA host to print info about
Brett Russ05b308e2005-10-05 17:08:53 -04003945 *
3946 * FIXME: complete this.
3947 *
3948 * LOCKING:
3949 * Inherited from caller.
3950 */
Tejun Heo4447d352007-04-17 23:44:08 +09003951static void mv_print_info(struct ata_host *host)
Brett Russ31961942005-09-30 01:36:00 -04003952{
Tejun Heo4447d352007-04-17 23:44:08 +09003953 struct pci_dev *pdev = to_pci_dev(host->dev);
3954 struct mv_host_priv *hpriv = host->private_data;
Auke Kok44c10132007-06-08 15:46:36 -07003955 u8 scc;
Jeff Garzikc1e4fe72007-07-09 12:29:31 -04003956 const char *scc_s, *gen;
Brett Russ31961942005-09-30 01:36:00 -04003957
3958 /* Use this to determine the HW stepping of the chip so we know
3959 * what errata to workaround
3960 */
Brett Russ31961942005-09-30 01:36:00 -04003961 pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
3962 if (scc == 0)
3963 scc_s = "SCSI";
3964 else if (scc == 0x01)
3965 scc_s = "RAID";
3966 else
Jeff Garzikc1e4fe72007-07-09 12:29:31 -04003967 scc_s = "?";
3968
3969 if (IS_GEN_I(hpriv))
3970 gen = "I";
3971 else if (IS_GEN_II(hpriv))
3972 gen = "II";
3973 else if (IS_GEN_IIE(hpriv))
3974 gen = "IIE";
3975 else
3976 gen = "?";
Brett Russ31961942005-09-30 01:36:00 -04003977
Jeff Garzika9524a72005-10-30 14:39:11 -05003978 dev_printk(KERN_INFO, &pdev->dev,
Jeff Garzikc1e4fe72007-07-09 12:29:31 -04003979 "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
3980 gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
Brett Russ31961942005-09-30 01:36:00 -04003981 scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
3982}
3983
Brett Russ05b308e2005-10-05 17:08:53 -04003984/**
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003985 * mv_pci_init_one - handle a positive probe of a PCI Marvell host
Brett Russ05b308e2005-10-05 17:08:53 -04003986 * @pdev: PCI device found
3987 * @ent: PCI device ID entry for the matched host
3988 *
3989 * LOCKING:
3990 * Inherited from caller.
3991 */
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003992static int mv_pci_init_one(struct pci_dev *pdev,
3993 const struct pci_device_id *ent)
Brett Russ20f733e2005-09-01 18:26:17 -04003994{
Jeff Garzik2dcb4072007-10-19 06:42:56 -04003995 static int printed_version;
Brett Russ20f733e2005-09-01 18:26:17 -04003996 unsigned int board_idx = (unsigned int)ent->driver_data;
Tejun Heo4447d352007-04-17 23:44:08 +09003997 const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
3998 struct ata_host *host;
3999 struct mv_host_priv *hpriv;
4000 int n_ports, rc;
Brett Russ20f733e2005-09-01 18:26:17 -04004001
Jeff Garzika9524a72005-10-30 14:39:11 -05004002 if (!printed_version++)
4003 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
Brett Russ20f733e2005-09-01 18:26:17 -04004004
Tejun Heo4447d352007-04-17 23:44:08 +09004005 /* allocate host */
4006 n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
4007
4008 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
4009 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
4010 if (!host || !hpriv)
4011 return -ENOMEM;
4012 host->private_data = hpriv;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05004013 hpriv->n_ports = n_ports;
Tejun Heo4447d352007-04-17 23:44:08 +09004014
4015 /* acquire resources */
Tejun Heo24dc5f32007-01-20 16:00:28 +09004016 rc = pcim_enable_device(pdev);
4017 if (rc)
Brett Russ20f733e2005-09-01 18:26:17 -04004018 return rc;
Brett Russ20f733e2005-09-01 18:26:17 -04004019
Tejun Heo0d5ff562007-02-01 15:06:36 +09004020 rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
4021 if (rc == -EBUSY)
Tejun Heo24dc5f32007-01-20 16:00:28 +09004022 pcim_pin_device(pdev);
Tejun Heo0d5ff562007-02-01 15:06:36 +09004023 if (rc)
Tejun Heo24dc5f32007-01-20 16:00:28 +09004024 return rc;
Tejun Heo4447d352007-04-17 23:44:08 +09004025 host->iomap = pcim_iomap_table(pdev);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05004026 hpriv->base = host->iomap[MV_PRIMARY_BAR];
Brett Russ20f733e2005-09-01 18:26:17 -04004027
Jeff Garzikd88184f2007-02-26 01:26:06 -05004028 rc = pci_go_64(pdev);
4029 if (rc)
4030 return rc;
4031
Mark Lordda2fa9b2008-01-26 18:32:45 -05004032 rc = mv_create_dma_pools(hpriv, &pdev->dev);
4033 if (rc)
4034 return rc;
4035
Brett Russ20f733e2005-09-01 18:26:17 -04004036 /* initialize adapter */
Tejun Heo4447d352007-04-17 23:44:08 +09004037 rc = mv_init_host(host, board_idx);
Tejun Heo24dc5f32007-01-20 16:00:28 +09004038 if (rc)
4039 return rc;
Brett Russ20f733e2005-09-01 18:26:17 -04004040
Mark Lord6d3c30e2009-01-21 10:31:29 -05004041 /* Enable message-switched interrupts, if requested */
4042 if (msi && pci_enable_msi(pdev) == 0)
4043 hpriv->hp_flags |= MV_HP_FLAG_MSI;
Brett Russ20f733e2005-09-01 18:26:17 -04004044
Brett Russ31961942005-09-30 01:36:00 -04004045 mv_dump_pci_cfg(pdev, 0x68);
Tejun Heo4447d352007-04-17 23:44:08 +09004046 mv_print_info(host);
Brett Russ20f733e2005-09-01 18:26:17 -04004047
Tejun Heo4447d352007-04-17 23:44:08 +09004048 pci_set_master(pdev);
Jeff Garzikea8b4db2007-07-17 02:21:50 -04004049 pci_try_set_mwi(pdev);
Tejun Heo4447d352007-04-17 23:44:08 +09004050 return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
Jeff Garzikc5d3e452007-07-11 18:30:50 -04004051 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
Brett Russ20f733e2005-09-01 18:26:17 -04004052}
Saeed Bishara7bb3c522008-01-30 11:50:45 -11004053#endif
Brett Russ20f733e2005-09-01 18:26:17 -04004054
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05004055static int mv_platform_probe(struct platform_device *pdev);
4056static int __devexit mv_platform_remove(struct platform_device *pdev);
4057
Brett Russ20f733e2005-09-01 18:26:17 -04004058static int __init mv_init(void)
4059{
Saeed Bishara7bb3c522008-01-30 11:50:45 -11004060 int rc = -ENODEV;
4061#ifdef CONFIG_PCI
4062 rc = pci_register_driver(&mv_pci_driver);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05004063 if (rc < 0)
4064 return rc;
4065#endif
4066 rc = platform_driver_register(&mv_platform_driver);
4067
4068#ifdef CONFIG_PCI
4069 if (rc < 0)
4070 pci_unregister_driver(&mv_pci_driver);
Saeed Bishara7bb3c522008-01-30 11:50:45 -11004071#endif
4072 return rc;
Brett Russ20f733e2005-09-01 18:26:17 -04004073}
4074
4075static void __exit mv_exit(void)
4076{
Saeed Bishara7bb3c522008-01-30 11:50:45 -11004077#ifdef CONFIG_PCI
Brett Russ20f733e2005-09-01 18:26:17 -04004078 pci_unregister_driver(&mv_pci_driver);
Saeed Bishara7bb3c522008-01-30 11:50:45 -11004079#endif
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05004080 platform_driver_unregister(&mv_platform_driver);
Brett Russ20f733e2005-09-01 18:26:17 -04004081}
4082
4083MODULE_AUTHOR("Brett Russ");
4084MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
4085MODULE_LICENSE("GPL");
4086MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
4087MODULE_VERSION(DRV_VERSION);
Mark Lord17c5aab2008-04-16 14:56:51 -04004088MODULE_ALIAS("platform:" DRV_NAME);
Brett Russ20f733e2005-09-01 18:26:17 -04004089
Brett Russ20f733e2005-09-01 18:26:17 -04004090module_init(mv_init);
4091module_exit(mv_exit);