sata_mv: cosmetic renames
[deliverable/linux.git] / drivers / ata / sata_mv.c
CommitLineData
20f733e7
BR
1/*
2 * sata_mv.c - Marvell SATA support
3 *
40f21b11 4 * Copyright 2008-2009: Marvell Corporation, all rights reserved.
8b260248 5 * Copyright 2005: EMC Corporation, all rights reserved.
e2b1be56 6 * Copyright 2005 Red Hat, Inc. All rights reserved.
20f733e7 7 *
40f21b11
ML
8 * Originally written by Brett Russ.
9 * Extensive overhaul and enhancement by Mark Lord <mlord@pobox.com>.
10 *
20f733e7
BR
11 * 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
4a05e209 28/*
85afb934
ML
29 * sata_mv TODO list:
30 *
85afb934
ML
31 * --> Develop a low-power-consumption strategy, and implement it.
32 *
2b748a0a 33 * --> Add sysfs attributes for per-chip / per-HC IRQ coalescing thresholds.
85afb934
ML
34 *
35 * --> [Experiment, Marvell value added] Is it possible to use target
36 * mode to cross-connect two Linux boxes with Marvell cards? If so,
37 * creating LibATA target mode support would be very interesting.
38 *
39 * Target mode, for those without docs, is the ability to directly
40 * connect two SATA ports.
41 */
4a05e209 42
65ad7fef
ML
43/*
44 * 80x1-B2 errata PCI#11:
45 *
46 * Users of the 6041/6081 Rev.B2 chips (current is C0)
47 * should be careful to insert those cards only onto PCI-X bus #0,
48 * and only in device slots 0..7, not higher. The chips may not
49 * work correctly otherwise (note: this is a pretty rare condition).
50 */
51
20f733e7
BR
52#include <linux/kernel.h>
53#include <linux/module.h>
54#include <linux/pci.h>
55#include <linux/init.h>
56#include <linux/blkdev.h>
57#include <linux/delay.h>
58#include <linux/interrupt.h>
8d8b6004 59#include <linux/dmapool.h>
20f733e7 60#include <linux/dma-mapping.h>
a9524a76 61#include <linux/device.h>
f351b2d6
SB
62#include <linux/platform_device.h>
63#include <linux/ata_platform.h>
15a32632 64#include <linux/mbus.h>
c46938cc 65#include <linux/bitops.h>
20f733e7 66#include <scsi/scsi_host.h>
193515d5 67#include <scsi/scsi_cmnd.h>
6c08772e 68#include <scsi/scsi_device.h>
20f733e7 69#include <linux/libata.h>
20f733e7
BR
70
71#define DRV_NAME "sata_mv"
2b748a0a 72#define DRV_VERSION "1.27"
20f733e7 73
40f21b11
ML
74/*
75 * module options
76 */
77
78static int msi;
79#ifdef CONFIG_PCI
80module_param(msi, int, S_IRUGO);
81MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
82#endif
83
2b748a0a
ML
84static int irq_coalescing_io_count;
85module_param(irq_coalescing_io_count, int, S_IRUGO);
86MODULE_PARM_DESC(irq_coalescing_io_count,
87 "IRQ coalescing I/O count threshold (0..255)");
88
89static int irq_coalescing_usecs;
90module_param(irq_coalescing_usecs, int, S_IRUGO);
91MODULE_PARM_DESC(irq_coalescing_usecs,
92 "IRQ coalescing time threshold in usecs");
93
20f733e7
BR
94enum {
95 /* BAR's are enumerated in terms of pci_resource_start() terms */
96 MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
97 MV_IO_BAR = 2, /* offset 0x18: IO space */
98 MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
99
100 MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
101 MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
102
2b748a0a
ML
103 /* For use with both IRQ coalescing methods ("all ports" or "per-HC" */
104 COAL_CLOCKS_PER_USEC = 150, /* for calculating COAL_TIMEs */
105 MAX_COAL_TIME_THRESHOLD = ((1 << 24) - 1), /* internal clocks count */
106 MAX_COAL_IO_COUNT = 255, /* completed I/O count */
107
20f733e7 108 MV_PCI_REG_BASE = 0,
615ab953 109
2b748a0a
ML
110 /*
111 * Per-chip ("all ports") interrupt coalescing feature.
112 * This is only for GEN_II / GEN_IIE hardware.
113 *
114 * Coalescing defers the interrupt until either the IO_THRESHOLD
115 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
116 */
117 MV_COAL_REG_BASE = 0x18000,
118 MV_IRQ_COAL_CAUSE = (MV_COAL_REG_BASE + 0x08),
119 ALL_PORTS_COAL_IRQ = (1 << 4), /* all ports irq event */
120
121 MV_IRQ_COAL_IO_THRESHOLD = (MV_COAL_REG_BASE + 0xcc),
122 MV_IRQ_COAL_TIME_THRESHOLD = (MV_COAL_REG_BASE + 0xd0),
123
124 /*
125 * Registers for the (unused here) transaction coalescing feature:
126 */
127 MV_TRAN_COAL_CAUSE_LO = (MV_COAL_REG_BASE + 0x88),
128 MV_TRAN_COAL_CAUSE_HI = (MV_COAL_REG_BASE + 0x8c),
129
20f733e7 130 MV_SATAHC0_REG_BASE = 0x20000,
8e7decdb
ML
131 MV_FLASH_CTL_OFS = 0x1046c,
132 MV_GPIO_PORT_CTL_OFS = 0x104f0,
133 MV_RESET_CFG_OFS = 0x180d8,
20f733e7
BR
134
135 MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
136 MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
137 MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
138 MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
139
31961943
BR
140 MV_MAX_Q_DEPTH = 32,
141 MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
142
143 /* CRQB needs alignment on a 1KB boundary. Size == 1KB
144 * CRPB needs alignment on a 256B boundary. Size == 256B
31961943
BR
145 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
146 */
147 MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
148 MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
da2fa9ba 149 MV_MAX_SG_CT = 256,
31961943 150 MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
31961943 151
352fab70 152 /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
20f733e7 153 MV_PORT_HC_SHIFT = 2,
352fab70
ML
154 MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
155 /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
156 MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
20f733e7
BR
157
158 /* Host Flags */
159 MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
7bb3c529 160
c5d3e45a 161 MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
91b1a84c 162 ATA_FLAG_MMIO | ATA_FLAG_PIO_POLLING,
ad3aef51 163
91b1a84c 164 MV_GEN_I_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NO_ATAPI,
20f733e7 165
40f21b11
ML
166 MV_GEN_II_FLAGS = MV_COMMON_FLAGS | ATA_FLAG_NCQ |
167 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA,
91b1a84c
ML
168
169 MV_GEN_IIE_FLAGS = MV_GEN_II_FLAGS | ATA_FLAG_AN,
ad3aef51 170
31961943
BR
171 CRQB_FLAG_READ = (1 << 0),
172 CRQB_TAG_SHIFT = 1,
c5d3e45a 173 CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
e12bef50 174 CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
c5d3e45a 175 CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
31961943
BR
176 CRQB_CMD_ADDR_SHIFT = 8,
177 CRQB_CMD_CS = (0x2 << 11),
178 CRQB_CMD_LAST = (1 << 15),
179
180 CRPB_FLAG_STATUS_SHIFT = 8,
c5d3e45a
JG
181 CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
182 CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
31961943
BR
183
184 EPRD_FLAG_END_OF_TBL = (1 << 31),
185
20f733e7
BR
186 /* PCI interface registers */
187
31961943 188 PCI_COMMAND_OFS = 0xc00,
65ad7fef 189 PCI_COMMAND_MWRCOM = (1 << 4), /* PCI Master Write Combining */
8e7decdb 190 PCI_COMMAND_MRDTRIG = (1 << 7), /* PCI Master Read Trigger */
31961943 191
20f733e7
BR
192 PCI_MAIN_CMD_STS_OFS = 0xd30,
193 STOP_PCI_MASTER = (1 << 2),
194 PCI_MASTER_EMPTY = (1 << 3),
195 GLOB_SFT_RST = (1 << 4),
196
8e7decdb
ML
197 MV_PCI_MODE_OFS = 0xd00,
198 MV_PCI_MODE_MASK = 0x30,
199
522479fb
JG
200 MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
201 MV_PCI_DISC_TIMER = 0xd04,
202 MV_PCI_MSI_TRIGGER = 0xc38,
203 MV_PCI_SERR_MASK = 0xc28,
8e7decdb 204 MV_PCI_XBAR_TMOUT_OFS = 0x1d04,
522479fb
JG
205 MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
206 MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
207 MV_PCI_ERR_ATTRIBUTE = 0x1d48,
208 MV_PCI_ERR_COMMAND = 0x1d50,
209
02a121da
ML
210 PCI_IRQ_CAUSE_OFS = 0x1d58,
211 PCI_IRQ_MASK_OFS = 0x1d5c,
20f733e7
BR
212 PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
213
02a121da
ML
214 PCIE_IRQ_CAUSE_OFS = 0x1900,
215 PCIE_IRQ_MASK_OFS = 0x1910,
646a4da5 216 PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
02a121da 217
7368f919
ML
218 /* Host Controller Main Interrupt Cause/Mask registers (1 per-chip) */
219 PCI_HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
220 PCI_HC_MAIN_IRQ_MASK_OFS = 0x1d64,
221 SOC_HC_MAIN_IRQ_CAUSE_OFS = 0x20020,
222 SOC_HC_MAIN_IRQ_MASK_OFS = 0x20024,
40f21b11
ML
223 ERR_IRQ = (1 << 0), /* shift by (2 * port #) */
224 DONE_IRQ = (1 << 1), /* shift by (2 * port #) */
20f733e7
BR
225 HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
226 HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
2b748a0a
ML
227 DONE_IRQ_0_3 = 0x000000aa, /* DONE_IRQ ports 0,1,2,3 */
228 DONE_IRQ_4_7 = (DONE_IRQ_0_3 << HC_SHIFT), /* 4,5,6,7 */
20f733e7 229 PCI_ERR = (1 << 18),
40f21b11
ML
230 TRAN_COAL_LO_DONE = (1 << 19), /* transaction coalescing */
231 TRAN_COAL_HI_DONE = (1 << 20), /* transaction coalescing */
232 PORTS_0_3_COAL_DONE = (1 << 8), /* HC0 IRQ coalescing */
233 PORTS_4_7_COAL_DONE = (1 << 17), /* HC1 IRQ coalescing */
234 ALL_PORTS_COAL_DONE = (1 << 21), /* GEN_II(E) IRQ coalescing */
20f733e7
BR
235 GPIO_INT = (1 << 22),
236 SELF_INT = (1 << 23),
237 TWSI_INT = (1 << 24),
238 HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
fb621e2f 239 HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
e12bef50 240 HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
20f733e7
BR
241
242 /* SATAHC registers */
243 HC_CFG_OFS = 0,
244
245 HC_IRQ_CAUSE_OFS = 0x14,
352fab70
ML
246 DMA_IRQ = (1 << 0), /* shift by port # */
247 HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
20f733e7
BR
248 DEV_IRQ = (1 << 8), /* shift by port # */
249
2b748a0a
ML
250 /*
251 * Per-HC (Host-Controller) interrupt coalescing feature.
252 * This is present on all chip generations.
253 *
254 * Coalescing defers the interrupt until either the IO_THRESHOLD
255 * (count of completed I/Os) is met, or the TIME_THRESHOLD is met.
256 */
257 HC_IRQ_COAL_IO_THRESHOLD_OFS = 0x000c,
258 HC_IRQ_COAL_TIME_THRESHOLD_OFS = 0x0010,
259
000b344f
ML
260 SOC_LED_CTRL_OFS = 0x2c,
261 SOC_LED_CTRL_BLINK = (1 << 0), /* Active LED blink */
262 SOC_LED_CTRL_ACT_PRESENCE = (1 << 2), /* Multiplex dev presence */
263 /* with dev activity LED */
264
20f733e7 265 /* Shadow block registers */
31961943
BR
266 SHD_BLK_OFS = 0x100,
267 SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
20f733e7
BR
268
269 /* SATA registers */
270 SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
271 SATA_ACTIVE_OFS = 0x350,
0c58912e 272 SATA_FIS_IRQ_CAUSE_OFS = 0x364,
c443c500 273 SATA_FIS_IRQ_AN = (1 << 9), /* async notification */
17c5aab5 274
ba68460b 275 LTMODE_OFS = 0x30c, /* requires read-after-write */
17c5aab5
ML
276 LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
277
ba68460b
ML
278 PHY_MODE2_OFS = 0x330,
279 PHY_MODE3_OFS = 0x310,
280 PHY_MODE4_OFS = 0x314, /* requires read-after-write */
ba069e37
ML
281 PHY_MODE4_CFG_MASK = 0x00000003, /* phy internal config field */
282 PHY_MODE4_CFG_VALUE = 0x00000001, /* phy internal config field */
283 PHY_MODE4_RSVD_ZEROS = 0x5de3fffa, /* Gen2e always write zeros */
284 PHY_MODE4_RSVD_ONES = 0x00000005, /* Gen2e always write ones */
285
e12bef50 286 SATA_IFCTL_OFS = 0x344,
8e7decdb 287 SATA_TESTCTL_OFS = 0x348,
e12bef50
ML
288 SATA_IFSTAT_OFS = 0x34c,
289 VENDOR_UNIQUE_FIS_OFS = 0x35c,
17c5aab5 290
8e7decdb
ML
291 FISCFG_OFS = 0x360,
292 FISCFG_WAIT_DEV_ERR = (1 << 8), /* wait for host on DevErr */
293 FISCFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
17c5aab5 294
c9d39130 295 MV5_PHY_MODE = 0x74,
8e7decdb
ML
296 MV5_LTMODE_OFS = 0x30,
297 MV5_PHY_CTL_OFS = 0x0C,
298 SATA_INTERFACE_CFG_OFS = 0x050,
bca1c4eb
JG
299
300 MV_M2_PREAMP_MASK = 0x7e0,
20f733e7
BR
301
302 /* Port registers */
303 EDMA_CFG_OFS = 0,
0c58912e
ML
304 EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
305 EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
306 EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
307 EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
308 EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
e12bef50
ML
309 EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
310 EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
20f733e7
BR
311
312 EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
313 EDMA_ERR_IRQ_MASK_OFS = 0xc,
6c1153e0
JG
314 EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
315 EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
316 EDMA_ERR_DEV = (1 << 2), /* device error */
317 EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
318 EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
319 EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
c5d3e45a
JG
320 EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
321 EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
6c1153e0 322 EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
c5d3e45a 323 EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
6c1153e0
JG
324 EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
325 EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
326 EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
327 EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
646a4da5 328
6c1153e0 329 EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
646a4da5
ML
330 EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
331 EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
332 EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
333 EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
334
6c1153e0 335 EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
646a4da5 336
6c1153e0 337 EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
646a4da5
ML
338 EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
339 EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
340 EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
341 EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
342 EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
343
6c1153e0 344 EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
646a4da5 345
6c1153e0 346 EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
c5d3e45a
JG
347 EDMA_ERR_OVERRUN_5 = (1 << 5),
348 EDMA_ERR_UNDERRUN_5 = (1 << 6),
646a4da5
ML
349
350 EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
351 EDMA_ERR_LNK_CTRL_RX_1 |
352 EDMA_ERR_LNK_CTRL_RX_3 |
85afb934 353 EDMA_ERR_LNK_CTRL_TX,
646a4da5 354
bdd4ddde
JG
355 EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
356 EDMA_ERR_PRD_PAR |
357 EDMA_ERR_DEV_DCON |
358 EDMA_ERR_DEV_CON |
359 EDMA_ERR_SERR |
360 EDMA_ERR_SELF_DIS |
6c1153e0 361 EDMA_ERR_CRQB_PAR |
bdd4ddde
JG
362 EDMA_ERR_CRPB_PAR |
363 EDMA_ERR_INTRL_PAR |
364 EDMA_ERR_IORDY |
365 EDMA_ERR_LNK_CTRL_RX_2 |
366 EDMA_ERR_LNK_DATA_RX |
367 EDMA_ERR_LNK_DATA_TX |
368 EDMA_ERR_TRANS_PROTO,
e12bef50 369
bdd4ddde
JG
370 EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
371 EDMA_ERR_PRD_PAR |
372 EDMA_ERR_DEV_DCON |
373 EDMA_ERR_DEV_CON |
374 EDMA_ERR_OVERRUN_5 |
375 EDMA_ERR_UNDERRUN_5 |
376 EDMA_ERR_SELF_DIS_5 |
6c1153e0 377 EDMA_ERR_CRQB_PAR |
bdd4ddde
JG
378 EDMA_ERR_CRPB_PAR |
379 EDMA_ERR_INTRL_PAR |
380 EDMA_ERR_IORDY,
20f733e7 381
31961943
BR
382 EDMA_REQ_Q_BASE_HI_OFS = 0x10,
383 EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
31961943
BR
384
385 EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
386 EDMA_REQ_Q_PTR_SHIFT = 5,
387
388 EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
389 EDMA_RSP_Q_IN_PTR_OFS = 0x20,
390 EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
31961943
BR
391 EDMA_RSP_Q_PTR_SHIFT = 3,
392
0ea9e179
JG
393 EDMA_CMD_OFS = 0x28, /* EDMA command register */
394 EDMA_EN = (1 << 0), /* enable EDMA */
395 EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
8e7decdb
ML
396 EDMA_RESET = (1 << 2), /* reset eng/trans/link/phy */
397
398 EDMA_STATUS_OFS = 0x30, /* EDMA engine status */
399 EDMA_STATUS_CACHE_EMPTY = (1 << 6), /* GenIIe command cache empty */
400 EDMA_STATUS_IDLE = (1 << 7), /* GenIIe EDMA enabled/idle */
20f733e7 401
8e7decdb
ML
402 EDMA_IORDY_TMOUT_OFS = 0x34,
403 EDMA_ARB_CFG_OFS = 0x38,
404
405 EDMA_HALTCOND_OFS = 0x60, /* GenIIe halt conditions */
c01e8a23 406 EDMA_UNKNOWN_RSVD_OFS = 0x6C, /* GenIIe unknown/reserved */
da14265e
ML
407
408 BMDMA_CMD_OFS = 0x224, /* bmdma command register */
409 BMDMA_STATUS_OFS = 0x228, /* bmdma status register */
410 BMDMA_PRD_LOW_OFS = 0x22c, /* bmdma PRD addr 31:0 */
411 BMDMA_PRD_HIGH_OFS = 0x230, /* bmdma PRD addr 63:32 */
412
31961943
BR
413 /* Host private flags (hp_flags) */
414 MV_HP_FLAG_MSI = (1 << 0),
47c2b677
JG
415 MV_HP_ERRATA_50XXB0 = (1 << 1),
416 MV_HP_ERRATA_50XXB2 = (1 << 2),
417 MV_HP_ERRATA_60X1B2 = (1 << 3),
418 MV_HP_ERRATA_60X1C0 = (1 << 4),
0ea9e179
JG
419 MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
420 MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
421 MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
02a121da 422 MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
616d4a98 423 MV_HP_CUT_THROUGH = (1 << 10), /* can use EDMA cut-through */
1f398472 424 MV_HP_FLAG_SOC = (1 << 11), /* SystemOnChip, no PCI */
000b344f 425 MV_HP_QUIRK_LED_BLINK_EN = (1 << 12), /* is led blinking enabled? */
20f733e7 426
31961943 427 /* Port private flags (pp_flags) */
0ea9e179 428 MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
72109168 429 MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
00f42eab 430 MV_PP_FLAG_FBS_EN = (1 << 2), /* is EDMA set up for FBS? */
29d187bb 431 MV_PP_FLAG_DELAYED_EH = (1 << 3), /* delayed dev err handling */
d16ab3f6 432 MV_PP_FLAG_FAKE_ATA_BUSY = (1 << 4), /* ignore initial ATA_DRDY */
20f733e7
BR
433};
434
ee9ccdf7
JG
435#define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
436#define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
e4e7b892 437#define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
8e7decdb 438#define IS_PCIE(hpriv) ((hpriv)->hp_flags & MV_HP_PCIE)
1f398472 439#define IS_SOC(hpriv) ((hpriv)->hp_flags & MV_HP_FLAG_SOC)
bca1c4eb 440
15a32632
LB
441#define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
442#define WINDOW_BASE(i) (0x20034 + ((i) << 4))
443
095fec88 444enum {
baf14aa1
JG
445 /* DMA boundary 0xffff is required by the s/g splitting
446 * we need on /length/ in mv_fill-sg().
447 */
448 MV_DMA_BOUNDARY = 0xffffU,
095fec88 449
0ea9e179
JG
450 /* mask of register bits containing lower 32 bits
451 * of EDMA request queue DMA address
452 */
095fec88
JG
453 EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
454
0ea9e179 455 /* ditto, for response queue */
095fec88
JG
456 EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
457};
458
522479fb
JG
459enum chip_type {
460 chip_504x,
461 chip_508x,
462 chip_5080,
463 chip_604x,
464 chip_608x,
e4e7b892
JG
465 chip_6042,
466 chip_7042,
f351b2d6 467 chip_soc,
522479fb
JG
468};
469
31961943
BR
470/* Command ReQuest Block: 32B */
471struct mv_crqb {
e1469874
ML
472 __le32 sg_addr;
473 __le32 sg_addr_hi;
474 __le16 ctrl_flags;
475 __le16 ata_cmd[11];
31961943 476};
20f733e7 477
e4e7b892 478struct mv_crqb_iie {
e1469874
ML
479 __le32 addr;
480 __le32 addr_hi;
481 __le32 flags;
482 __le32 len;
483 __le32 ata_cmd[4];
e4e7b892
JG
484};
485
31961943
BR
486/* Command ResPonse Block: 8B */
487struct mv_crpb {
e1469874
ML
488 __le16 id;
489 __le16 flags;
490 __le32 tmstmp;
20f733e7
BR
491};
492
31961943
BR
493/* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
494struct mv_sg {
e1469874
ML
495 __le32 addr;
496 __le32 flags_size;
497 __le32 addr_hi;
498 __le32 reserved;
31961943 499};
20f733e7 500
08da1759
ML
501/*
502 * We keep a local cache of a few frequently accessed port
503 * registers here, to avoid having to read them (very slow)
504 * when switching between EDMA and non-EDMA modes.
505 */
506struct mv_cached_regs {
507 u32 fiscfg;
508 u32 ltmode;
509 u32 haltcond;
c01e8a23 510 u32 unknown_rsvd;
08da1759
ML
511};
512
31961943
BR
513struct mv_port_priv {
514 struct mv_crqb *crqb;
515 dma_addr_t crqb_dma;
516 struct mv_crpb *crpb;
517 dma_addr_t crpb_dma;
eb73d558
ML
518 struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
519 dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
bdd4ddde
JG
520
521 unsigned int req_idx;
522 unsigned int resp_idx;
523
31961943 524 u32 pp_flags;
08da1759 525 struct mv_cached_regs cached;
29d187bb 526 unsigned int delayed_eh_pmp_map;
31961943
BR
527};
528
bca1c4eb
JG
529struct mv_port_signal {
530 u32 amps;
531 u32 pre;
532};
533
02a121da
ML
534struct mv_host_priv {
535 u32 hp_flags;
96e2c487 536 u32 main_irq_mask;
02a121da
ML
537 struct mv_port_signal signal[8];
538 const struct mv_hw_ops *ops;
f351b2d6
SB
539 int n_ports;
540 void __iomem *base;
7368f919
ML
541 void __iomem *main_irq_cause_addr;
542 void __iomem *main_irq_mask_addr;
02a121da
ML
543 u32 irq_cause_ofs;
544 u32 irq_mask_ofs;
545 u32 unmask_all_irqs;
da2fa9ba
ML
546 /*
547 * These consistent DMA memory pools give us guaranteed
548 * alignment for hardware-accessed data structures,
549 * and less memory waste in accomplishing the alignment.
550 */
551 struct dma_pool *crqb_pool;
552 struct dma_pool *crpb_pool;
553 struct dma_pool *sg_tbl_pool;
02a121da
ML
554};
555
47c2b677 556struct mv_hw_ops {
2a47ce06
JG
557 void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
558 unsigned int port);
47c2b677
JG
559 void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
560 void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
561 void __iomem *mmio);
c9d39130
JG
562 int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
563 unsigned int n_hc);
522479fb 564 void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
7bb3c529 565 void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
47c2b677
JG
566};
567
82ef04fb
TH
568static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
569static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
570static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val);
571static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val);
31961943
BR
572static int mv_port_start(struct ata_port *ap);
573static void mv_port_stop(struct ata_port *ap);
3e4a1391 574static int mv_qc_defer(struct ata_queued_cmd *qc);
31961943 575static void mv_qc_prep(struct ata_queued_cmd *qc);
e4e7b892 576static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
9a3d9eb0 577static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
a1efdaba
TH
578static int mv_hardreset(struct ata_link *link, unsigned int *class,
579 unsigned long deadline);
bdd4ddde
JG
580static void mv_eh_freeze(struct ata_port *ap);
581static void mv_eh_thaw(struct ata_port *ap);
f273827e 582static void mv6_dev_config(struct ata_device *dev);
20f733e7 583
2a47ce06
JG
584static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
585 unsigned int port);
47c2b677
JG
586static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
587static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
588 void __iomem *mmio);
c9d39130
JG
589static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
590 unsigned int n_hc);
522479fb 591static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
7bb3c529 592static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
47c2b677 593
2a47ce06
JG
594static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
595 unsigned int port);
47c2b677
JG
596static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
597static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
598 void __iomem *mmio);
c9d39130
JG
599static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
600 unsigned int n_hc);
522479fb 601static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
f351b2d6
SB
602static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
603 void __iomem *mmio);
604static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
605 void __iomem *mmio);
606static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
607 void __iomem *mmio, unsigned int n_hc);
608static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
609 void __iomem *mmio);
610static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
7bb3c529 611static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
e12bef50 612static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
c9d39130 613 unsigned int port_no);
e12bef50 614static int mv_stop_edma(struct ata_port *ap);
b562468c 615static int mv_stop_edma_engine(void __iomem *port_mmio);
00b81235 616static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma);
47c2b677 617
e49856d8
ML
618static void mv_pmp_select(struct ata_port *ap, int pmp);
619static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
620 unsigned long deadline);
621static int mv_softreset(struct ata_link *link, unsigned int *class,
622 unsigned long deadline);
29d187bb 623static void mv_pmp_error_handler(struct ata_port *ap);
4c299ca3
ML
624static void mv_process_crpb_entries(struct ata_port *ap,
625 struct mv_port_priv *pp);
47c2b677 626
da14265e
ML
627static void mv_sff_irq_clear(struct ata_port *ap);
628static int mv_check_atapi_dma(struct ata_queued_cmd *qc);
629static void mv_bmdma_setup(struct ata_queued_cmd *qc);
630static void mv_bmdma_start(struct ata_queued_cmd *qc);
631static void mv_bmdma_stop(struct ata_queued_cmd *qc);
632static u8 mv_bmdma_status(struct ata_port *ap);
d16ab3f6 633static u8 mv_sff_check_status(struct ata_port *ap);
da14265e 634
eb73d558
ML
635/* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
636 * because we have to allow room for worst case splitting of
637 * PRDs for 64K boundaries in mv_fill_sg().
638 */
c5d3e45a 639static struct scsi_host_template mv5_sht = {
68d1d07b 640 ATA_BASE_SHT(DRV_NAME),
baf14aa1 641 .sg_tablesize = MV_MAX_SG_CT / 2,
c5d3e45a 642 .dma_boundary = MV_DMA_BOUNDARY,
c5d3e45a
JG
643};
644
645static struct scsi_host_template mv6_sht = {
68d1d07b 646 ATA_NCQ_SHT(DRV_NAME),
138bfdd0 647 .can_queue = MV_MAX_Q_DEPTH - 1,
baf14aa1 648 .sg_tablesize = MV_MAX_SG_CT / 2,
20f733e7 649 .dma_boundary = MV_DMA_BOUNDARY,
20f733e7
BR
650};
651
029cfd6b
TH
652static struct ata_port_operations mv5_ops = {
653 .inherits = &ata_sff_port_ops,
c9d39130 654
c96f1732
AC
655 .lost_interrupt = ATA_OP_NULL,
656
3e4a1391 657 .qc_defer = mv_qc_defer,
c9d39130
JG
658 .qc_prep = mv_qc_prep,
659 .qc_issue = mv_qc_issue,
c9d39130 660
bdd4ddde
JG
661 .freeze = mv_eh_freeze,
662 .thaw = mv_eh_thaw,
a1efdaba 663 .hardreset = mv_hardreset,
a1efdaba 664 .error_handler = ata_std_error_handler, /* avoid SFF EH */
029cfd6b 665 .post_internal_cmd = ATA_OP_NULL,
bdd4ddde 666
c9d39130
JG
667 .scr_read = mv5_scr_read,
668 .scr_write = mv5_scr_write,
669
670 .port_start = mv_port_start,
671 .port_stop = mv_port_stop,
c9d39130
JG
672};
673
029cfd6b
TH
674static struct ata_port_operations mv6_ops = {
675 .inherits = &mv5_ops,
f273827e 676 .dev_config = mv6_dev_config,
20f733e7
BR
677 .scr_read = mv_scr_read,
678 .scr_write = mv_scr_write,
679
e49856d8
ML
680 .pmp_hardreset = mv_pmp_hardreset,
681 .pmp_softreset = mv_softreset,
682 .softreset = mv_softreset,
29d187bb 683 .error_handler = mv_pmp_error_handler,
da14265e 684
40f21b11 685 .sff_check_status = mv_sff_check_status,
da14265e
ML
686 .sff_irq_clear = mv_sff_irq_clear,
687 .check_atapi_dma = mv_check_atapi_dma,
688 .bmdma_setup = mv_bmdma_setup,
689 .bmdma_start = mv_bmdma_start,
690 .bmdma_stop = mv_bmdma_stop,
691 .bmdma_status = mv_bmdma_status,
20f733e7
BR
692};
693
029cfd6b
TH
694static struct ata_port_operations mv_iie_ops = {
695 .inherits = &mv6_ops,
696 .dev_config = ATA_OP_NULL,
e4e7b892 697 .qc_prep = mv_qc_prep_iie,
e4e7b892
JG
698};
699
98ac62de 700static const struct ata_port_info mv_port_info[] = {
20f733e7 701 { /* chip_504x */
91b1a84c 702 .flags = MV_GEN_I_FLAGS,
c361acbc 703 .pio_mask = ATA_PIO4,
bf6263a8 704 .udma_mask = ATA_UDMA6,
c9d39130 705 .port_ops = &mv5_ops,
20f733e7
BR
706 },
707 { /* chip_508x */
91b1a84c 708 .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
c361acbc 709 .pio_mask = ATA_PIO4,
bf6263a8 710 .udma_mask = ATA_UDMA6,
c9d39130 711 .port_ops = &mv5_ops,
20f733e7 712 },
47c2b677 713 { /* chip_5080 */
91b1a84c 714 .flags = MV_GEN_I_FLAGS | MV_FLAG_DUAL_HC,
c361acbc 715 .pio_mask = ATA_PIO4,
bf6263a8 716 .udma_mask = ATA_UDMA6,
c9d39130 717 .port_ops = &mv5_ops,
47c2b677 718 },
20f733e7 719 { /* chip_604x */
91b1a84c 720 .flags = MV_GEN_II_FLAGS,
c361acbc 721 .pio_mask = ATA_PIO4,
bf6263a8 722 .udma_mask = ATA_UDMA6,
c9d39130 723 .port_ops = &mv6_ops,
20f733e7
BR
724 },
725 { /* chip_608x */
91b1a84c 726 .flags = MV_GEN_II_FLAGS | MV_FLAG_DUAL_HC,
c361acbc 727 .pio_mask = ATA_PIO4,
bf6263a8 728 .udma_mask = ATA_UDMA6,
c9d39130 729 .port_ops = &mv6_ops,
20f733e7 730 },
e4e7b892 731 { /* chip_6042 */
91b1a84c 732 .flags = MV_GEN_IIE_FLAGS,
c361acbc 733 .pio_mask = ATA_PIO4,
bf6263a8 734 .udma_mask = ATA_UDMA6,
e4e7b892
JG
735 .port_ops = &mv_iie_ops,
736 },
737 { /* chip_7042 */
91b1a84c 738 .flags = MV_GEN_IIE_FLAGS,
c361acbc 739 .pio_mask = ATA_PIO4,
bf6263a8 740 .udma_mask = ATA_UDMA6,
e4e7b892
JG
741 .port_ops = &mv_iie_ops,
742 },
f351b2d6 743 { /* chip_soc */
91b1a84c 744 .flags = MV_GEN_IIE_FLAGS,
c361acbc 745 .pio_mask = ATA_PIO4,
17c5aab5
ML
746 .udma_mask = ATA_UDMA6,
747 .port_ops = &mv_iie_ops,
f351b2d6 748 },
20f733e7
BR
749};
750
3b7d697d 751static const struct pci_device_id mv_pci_tbl[] = {
2d2744fc
JG
752 { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
753 { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
754 { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
755 { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
46c5784c
ML
756 /* RocketRAID 1720/174x have different identifiers */
757 { PCI_VDEVICE(TTI, 0x1720), chip_6042 },
4462254a
ML
758 { PCI_VDEVICE(TTI, 0x1740), chip_6042 },
759 { PCI_VDEVICE(TTI, 0x1742), chip_6042 },
2d2744fc
JG
760
761 { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
762 { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
763 { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
764 { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
765 { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
766
767 { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
768
d9f9c6bc
FA
769 /* Adaptec 1430SA */
770 { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
771
02a121da 772 /* Marvell 7042 support */
6a3d586d
MT
773 { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
774
02a121da
ML
775 /* Highpoint RocketRAID PCIe series */
776 { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
777 { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
778
2d2744fc 779 { } /* terminate list */
20f733e7
BR
780};
781
47c2b677
JG
782static const struct mv_hw_ops mv5xxx_ops = {
783 .phy_errata = mv5_phy_errata,
784 .enable_leds = mv5_enable_leds,
785 .read_preamp = mv5_read_preamp,
786 .reset_hc = mv5_reset_hc,
522479fb
JG
787 .reset_flash = mv5_reset_flash,
788 .reset_bus = mv5_reset_bus,
47c2b677
JG
789};
790
791static const struct mv_hw_ops mv6xxx_ops = {
792 .phy_errata = mv6_phy_errata,
793 .enable_leds = mv6_enable_leds,
794 .read_preamp = mv6_read_preamp,
795 .reset_hc = mv6_reset_hc,
522479fb
JG
796 .reset_flash = mv6_reset_flash,
797 .reset_bus = mv_reset_pci_bus,
47c2b677
JG
798};
799
f351b2d6
SB
800static const struct mv_hw_ops mv_soc_ops = {
801 .phy_errata = mv6_phy_errata,
802 .enable_leds = mv_soc_enable_leds,
803 .read_preamp = mv_soc_read_preamp,
804 .reset_hc = mv_soc_reset_hc,
805 .reset_flash = mv_soc_reset_flash,
806 .reset_bus = mv_soc_reset_bus,
807};
808
20f733e7
BR
809/*
810 * Functions
811 */
812
813static inline void writelfl(unsigned long data, void __iomem *addr)
814{
815 writel(data, addr);
816 (void) readl(addr); /* flush to avoid PCI posted write */
817}
818
c9d39130
JG
819static inline unsigned int mv_hc_from_port(unsigned int port)
820{
821 return port >> MV_PORT_HC_SHIFT;
822}
823
824static inline unsigned int mv_hardport_from_port(unsigned int port)
825{
826 return port & MV_PORT_MASK;
827}
828
1cfd19ae
ML
829/*
830 * Consolidate some rather tricky bit shift calculations.
831 * This is hot-path stuff, so not a function.
832 * Simple code, with two return values, so macro rather than inline.
833 *
834 * port is the sole input, in range 0..7.
7368f919
ML
835 * shift is one output, for use with main_irq_cause / main_irq_mask registers.
836 * hardport is the other output, in range 0..3.
1cfd19ae
ML
837 *
838 * Note that port and hardport may be the same variable in some cases.
839 */
840#define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
841{ \
842 shift = mv_hc_from_port(port) * HC_SHIFT; \
843 hardport = mv_hardport_from_port(port); \
844 shift += hardport * 2; \
845}
846
352fab70
ML
847static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
848{
849 return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
850}
851
c9d39130
JG
852static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
853 unsigned int port)
854{
855 return mv_hc_base(base, mv_hc_from_port(port));
856}
857
20f733e7
BR
858static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
859{
c9d39130 860 return mv_hc_base_from_port(base, port) +
8b260248 861 MV_SATAHC_ARBTR_REG_SZ +
c9d39130 862 (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
20f733e7
BR
863}
864
e12bef50
ML
865static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
866{
867 void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
868 unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
869
870 return hc_mmio + ofs;
871}
872
f351b2d6
SB
873static inline void __iomem *mv_host_base(struct ata_host *host)
874{
875 struct mv_host_priv *hpriv = host->private_data;
876 return hpriv->base;
877}
878
20f733e7
BR
879static inline void __iomem *mv_ap_base(struct ata_port *ap)
880{
f351b2d6 881 return mv_port_base(mv_host_base(ap->host), ap->port_no);
20f733e7
BR
882}
883
cca3974e 884static inline int mv_get_hc_count(unsigned long port_flags)
31961943 885{
cca3974e 886 return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
31961943
BR
887}
888
08da1759
ML
889/**
890 * mv_save_cached_regs - (re-)initialize cached port registers
891 * @ap: the port whose registers we are caching
892 *
893 * Initialize the local cache of port registers,
894 * so that reading them over and over again can
895 * be avoided on the hotter paths of this driver.
896 * This saves a few microseconds each time we switch
897 * to/from EDMA mode to perform (eg.) a drive cache flush.
898 */
899static void mv_save_cached_regs(struct ata_port *ap)
900{
901 void __iomem *port_mmio = mv_ap_base(ap);
902 struct mv_port_priv *pp = ap->private_data;
903
904 pp->cached.fiscfg = readl(port_mmio + FISCFG_OFS);
905 pp->cached.ltmode = readl(port_mmio + LTMODE_OFS);
906 pp->cached.haltcond = readl(port_mmio + EDMA_HALTCOND_OFS);
c01e8a23 907 pp->cached.unknown_rsvd = readl(port_mmio + EDMA_UNKNOWN_RSVD_OFS);
08da1759
ML
908}
909
910/**
911 * mv_write_cached_reg - write to a cached port register
912 * @addr: hardware address of the register
913 * @old: pointer to cached value of the register
914 * @new: new value for the register
915 *
916 * Write a new value to a cached register,
917 * but only if the value is different from before.
918 */
919static inline void mv_write_cached_reg(void __iomem *addr, u32 *old, u32 new)
920{
921 if (new != *old) {
922 *old = new;
923 writel(new, addr);
924 }
925}
926
c5d3e45a
JG
927static void mv_set_edma_ptrs(void __iomem *port_mmio,
928 struct mv_host_priv *hpriv,
929 struct mv_port_priv *pp)
930{
bdd4ddde
JG
931 u32 index;
932
c5d3e45a
JG
933 /*
934 * initialize request queue
935 */
fcfb1f77
ML
936 pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
937 index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
bdd4ddde 938
c5d3e45a
JG
939 WARN_ON(pp->crqb_dma & 0x3ff);
940 writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
bdd4ddde 941 writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
c5d3e45a 942 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
5cf73bfb 943 writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
c5d3e45a
JG
944
945 /*
946 * initialize response queue
947 */
fcfb1f77
ML
948 pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
949 index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
bdd4ddde 950
c5d3e45a
JG
951 WARN_ON(pp->crpb_dma & 0xff);
952 writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
5cf73bfb 953 writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
bdd4ddde 954 writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
c5d3e45a 955 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
c5d3e45a
JG
956}
957
2b748a0a
ML
958static void mv_write_main_irq_mask(u32 mask, struct mv_host_priv *hpriv)
959{
960 /*
961 * When writing to the main_irq_mask in hardware,
962 * we must ensure exclusivity between the interrupt coalescing bits
963 * and the corresponding individual port DONE_IRQ bits.
964 *
965 * Note that this register is really an "IRQ enable" register,
966 * not an "IRQ mask" register as Marvell's naming might suggest.
967 */
968 if (mask & (ALL_PORTS_COAL_DONE | PORTS_0_3_COAL_DONE))
969 mask &= ~DONE_IRQ_0_3;
970 if (mask & (ALL_PORTS_COAL_DONE | PORTS_4_7_COAL_DONE))
971 mask &= ~DONE_IRQ_4_7;
972 writelfl(mask, hpriv->main_irq_mask_addr);
973}
974
c4de573b
ML
975static void mv_set_main_irq_mask(struct ata_host *host,
976 u32 disable_bits, u32 enable_bits)
977{
978 struct mv_host_priv *hpriv = host->private_data;
979 u32 old_mask, new_mask;
980
96e2c487 981 old_mask = hpriv->main_irq_mask;
c4de573b 982 new_mask = (old_mask & ~disable_bits) | enable_bits;
96e2c487
ML
983 if (new_mask != old_mask) {
984 hpriv->main_irq_mask = new_mask;
2b748a0a 985 mv_write_main_irq_mask(new_mask, hpriv);
96e2c487 986 }
c4de573b
ML
987}
988
989static void mv_enable_port_irqs(struct ata_port *ap,
990 unsigned int port_bits)
991{
992 unsigned int shift, hardport, port = ap->port_no;
993 u32 disable_bits, enable_bits;
994
995 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
996
997 disable_bits = (DONE_IRQ | ERR_IRQ) << shift;
998 enable_bits = port_bits << shift;
999 mv_set_main_irq_mask(ap->host, disable_bits, enable_bits);
1000}
1001
00b81235
ML
1002static void mv_clear_and_enable_port_irqs(struct ata_port *ap,
1003 void __iomem *port_mmio,
1004 unsigned int port_irqs)
1005{
1006 struct mv_host_priv *hpriv = ap->host->private_data;
1007 int hardport = mv_hardport_from_port(ap->port_no);
1008 void __iomem *hc_mmio = mv_hc_base_from_port(
1009 mv_host_base(ap->host), ap->port_no);
1010 u32 hc_irq_cause;
1011
1012 /* clear EDMA event indicators, if any */
1013 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1014
1015 /* clear pending irq events */
1016 hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1017 writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
1018
1019 /* clear FIS IRQ Cause */
1020 if (IS_GEN_IIE(hpriv))
1021 writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
1022
1023 mv_enable_port_irqs(ap, port_irqs);
1024}
1025
2b748a0a
ML
1026static void mv_set_irq_coalescing(struct ata_host *host,
1027 unsigned int count, unsigned int usecs)
1028{
1029 struct mv_host_priv *hpriv = host->private_data;
1030 void __iomem *mmio = hpriv->base, *hc_mmio;
1031 u32 coal_enable = 0;
1032 unsigned long flags;
6abf4678 1033 unsigned int clks, is_dual_hc = hpriv->n_ports > MV_PORTS_PER_HC;
2b748a0a
ML
1034 const u32 coal_disable = PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
1035 ALL_PORTS_COAL_DONE;
1036
1037 /* Disable IRQ coalescing if either threshold is zero */
1038 if (!usecs || !count) {
1039 clks = count = 0;
1040 } else {
1041 /* Respect maximum limits of the hardware */
1042 clks = usecs * COAL_CLOCKS_PER_USEC;
1043 if (clks > MAX_COAL_TIME_THRESHOLD)
1044 clks = MAX_COAL_TIME_THRESHOLD;
1045 if (count > MAX_COAL_IO_COUNT)
1046 count = MAX_COAL_IO_COUNT;
1047 }
1048
1049 spin_lock_irqsave(&host->lock, flags);
6abf4678 1050 mv_set_main_irq_mask(host, coal_disable, 0);
2b748a0a 1051
6abf4678 1052 if (is_dual_hc && !IS_GEN_I(hpriv)) {
2b748a0a 1053 /*
6abf4678
ML
1054 * GEN_II/GEN_IIE with dual host controllers:
1055 * one set of global thresholds for the entire chip.
2b748a0a
ML
1056 */
1057 writel(clks, mmio + MV_IRQ_COAL_TIME_THRESHOLD);
1058 writel(count, mmio + MV_IRQ_COAL_IO_THRESHOLD);
1059 /* clear leftover coal IRQ bit */
6abf4678
ML
1060 writel(~ALL_PORTS_COAL_IRQ, mmio + MV_IRQ_COAL_CAUSE);
1061 if (count)
1062 coal_enable = ALL_PORTS_COAL_DONE;
1063 clks = count = 0; /* force clearing of regular regs below */
2b748a0a 1064 }
6abf4678 1065
2b748a0a
ML
1066 /*
1067 * All chips: independent thresholds for each HC on the chip.
1068 */
1069 hc_mmio = mv_hc_base_from_port(mmio, 0);
1070 writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD_OFS);
1071 writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD_OFS);
6abf4678
ML
1072 writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE_OFS);
1073 if (count)
1074 coal_enable |= PORTS_0_3_COAL_DONE;
1075 if (is_dual_hc) {
2b748a0a
ML
1076 hc_mmio = mv_hc_base_from_port(mmio, MV_PORTS_PER_HC);
1077 writel(clks, hc_mmio + HC_IRQ_COAL_TIME_THRESHOLD_OFS);
1078 writel(count, hc_mmio + HC_IRQ_COAL_IO_THRESHOLD_OFS);
6abf4678
ML
1079 writel(~HC_COAL_IRQ, hc_mmio + HC_IRQ_CAUSE_OFS);
1080 if (count)
1081 coal_enable |= PORTS_4_7_COAL_DONE;
2b748a0a 1082 }
2b748a0a 1083
6abf4678 1084 mv_set_main_irq_mask(host, 0, coal_enable);
2b748a0a
ML
1085 spin_unlock_irqrestore(&host->lock, flags);
1086}
1087
05b308e1 1088/**
00b81235 1089 * mv_start_edma - Enable eDMA engine
05b308e1
BR
1090 * @base: port base address
1091 * @pp: port private data
1092 *
beec7dbc
TH
1093 * Verify the local cache of the eDMA state is accurate with a
1094 * WARN_ON.
05b308e1
BR
1095 *
1096 * LOCKING:
1097 * Inherited from caller.
1098 */
00b81235 1099static void mv_start_edma(struct ata_port *ap, void __iomem *port_mmio,
72109168 1100 struct mv_port_priv *pp, u8 protocol)
20f733e7 1101{
72109168
ML
1102 int want_ncq = (protocol == ATA_PROT_NCQ);
1103
1104 if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
1105 int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
1106 if (want_ncq != using_ncq)
b562468c 1107 mv_stop_edma(ap);
72109168 1108 }
c5d3e45a 1109 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
0c58912e 1110 struct mv_host_priv *hpriv = ap->host->private_data;
0c58912e 1111
00b81235 1112 mv_edma_cfg(ap, want_ncq, 1);
0c58912e 1113
f630d562 1114 mv_set_edma_ptrs(port_mmio, hpriv, pp);
00b81235 1115 mv_clear_and_enable_port_irqs(ap, port_mmio, DONE_IRQ|ERR_IRQ);
bdd4ddde 1116
f630d562 1117 writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
afb0edd9
BR
1118 pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
1119 }
20f733e7
BR
1120}
1121
9b2c4e0b
ML
1122static void mv_wait_for_edma_empty_idle(struct ata_port *ap)
1123{
1124 void __iomem *port_mmio = mv_ap_base(ap);
1125 const u32 empty_idle = (EDMA_STATUS_CACHE_EMPTY | EDMA_STATUS_IDLE);
1126 const int per_loop = 5, timeout = (15 * 1000 / per_loop);
1127 int i;
1128
1129 /*
1130 * Wait for the EDMA engine to finish transactions in progress.
c46938cc
ML
1131 * No idea what a good "timeout" value might be, but measurements
1132 * indicate that it often requires hundreds of microseconds
1133 * with two drives in-use. So we use the 15msec value above
1134 * as a rough guess at what even more drives might require.
9b2c4e0b
ML
1135 */
1136 for (i = 0; i < timeout; ++i) {
1137 u32 edma_stat = readl(port_mmio + EDMA_STATUS_OFS);
1138 if ((edma_stat & empty_idle) == empty_idle)
1139 break;
1140 udelay(per_loop);
1141 }
1142 /* ata_port_printk(ap, KERN_INFO, "%s: %u+ usecs\n", __func__, i); */
1143}
1144
05b308e1 1145/**
e12bef50 1146 * mv_stop_edma_engine - Disable eDMA engine
b562468c 1147 * @port_mmio: io base address
05b308e1
BR
1148 *
1149 * LOCKING:
1150 * Inherited from caller.
1151 */
b562468c 1152static int mv_stop_edma_engine(void __iomem *port_mmio)
20f733e7 1153{
b562468c 1154 int i;
31961943 1155
b562468c
ML
1156 /* Disable eDMA. The disable bit auto clears. */
1157 writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
8b260248 1158
b562468c
ML
1159 /* Wait for the chip to confirm eDMA is off. */
1160 for (i = 10000; i > 0; i--) {
1161 u32 reg = readl(port_mmio + EDMA_CMD_OFS);
4537deb5 1162 if (!(reg & EDMA_EN))
b562468c
ML
1163 return 0;
1164 udelay(10);
31961943 1165 }
b562468c 1166 return -EIO;
20f733e7
BR
1167}
1168
e12bef50 1169static int mv_stop_edma(struct ata_port *ap)
0ea9e179 1170{
b562468c
ML
1171 void __iomem *port_mmio = mv_ap_base(ap);
1172 struct mv_port_priv *pp = ap->private_data;
66e57a2c 1173 int err = 0;
0ea9e179 1174
b562468c
ML
1175 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
1176 return 0;
1177 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
9b2c4e0b 1178 mv_wait_for_edma_empty_idle(ap);
b562468c
ML
1179 if (mv_stop_edma_engine(port_mmio)) {
1180 ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
66e57a2c 1181 err = -EIO;
b562468c 1182 }
66e57a2c
ML
1183 mv_edma_cfg(ap, 0, 0);
1184 return err;
0ea9e179
JG
1185}
1186
8a70f8dc 1187#ifdef ATA_DEBUG
31961943 1188static void mv_dump_mem(void __iomem *start, unsigned bytes)
20f733e7 1189{
31961943
BR
1190 int b, w;
1191 for (b = 0; b < bytes; ) {
1192 DPRINTK("%p: ", start + b);
1193 for (w = 0; b < bytes && w < 4; w++) {
2dcb407e 1194 printk("%08x ", readl(start + b));
31961943
BR
1195 b += sizeof(u32);
1196 }
1197 printk("\n");
1198 }
31961943 1199}
8a70f8dc
JG
1200#endif
1201
31961943
BR
1202static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
1203{
1204#ifdef ATA_DEBUG
1205 int b, w;
1206 u32 dw;
1207 for (b = 0; b < bytes; ) {
1208 DPRINTK("%02x: ", b);
1209 for (w = 0; b < bytes && w < 4; w++) {
2dcb407e
JG
1210 (void) pci_read_config_dword(pdev, b, &dw);
1211 printk("%08x ", dw);
31961943
BR
1212 b += sizeof(u32);
1213 }
1214 printk("\n");
1215 }
1216#endif
1217}
1218static void mv_dump_all_regs(void __iomem *mmio_base, int port,
1219 struct pci_dev *pdev)
1220{
1221#ifdef ATA_DEBUG
8b260248 1222 void __iomem *hc_base = mv_hc_base(mmio_base,
31961943
BR
1223 port >> MV_PORT_HC_SHIFT);
1224 void __iomem *port_base;
1225 int start_port, num_ports, p, start_hc, num_hcs, hc;
1226
1227 if (0 > port) {
1228 start_hc = start_port = 0;
1229 num_ports = 8; /* shld be benign for 4 port devs */
1230 num_hcs = 2;
1231 } else {
1232 start_hc = port >> MV_PORT_HC_SHIFT;
1233 start_port = port;
1234 num_ports = num_hcs = 1;
1235 }
8b260248 1236 DPRINTK("All registers for port(s) %u-%u:\n", start_port,
31961943
BR
1237 num_ports > 1 ? num_ports - 1 : start_port);
1238
1239 if (NULL != pdev) {
1240 DPRINTK("PCI config space regs:\n");
1241 mv_dump_pci_cfg(pdev, 0x68);
1242 }
1243 DPRINTK("PCI regs:\n");
1244 mv_dump_mem(mmio_base+0xc00, 0x3c);
1245 mv_dump_mem(mmio_base+0xd00, 0x34);
1246 mv_dump_mem(mmio_base+0xf00, 0x4);
1247 mv_dump_mem(mmio_base+0x1d00, 0x6c);
1248 for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
d220c37e 1249 hc_base = mv_hc_base(mmio_base, hc);
31961943
BR
1250 DPRINTK("HC regs (HC %i):\n", hc);
1251 mv_dump_mem(hc_base, 0x1c);
1252 }
1253 for (p = start_port; p < start_port + num_ports; p++) {
1254 port_base = mv_port_base(mmio_base, p);
2dcb407e 1255 DPRINTK("EDMA regs (port %i):\n", p);
31961943 1256 mv_dump_mem(port_base, 0x54);
2dcb407e 1257 DPRINTK("SATA regs (port %i):\n", p);
31961943
BR
1258 mv_dump_mem(port_base+0x300, 0x60);
1259 }
1260#endif
20f733e7
BR
1261}
1262
1263static unsigned int mv_scr_offset(unsigned int sc_reg_in)
1264{
1265 unsigned int ofs;
1266
1267 switch (sc_reg_in) {
1268 case SCR_STATUS:
1269 case SCR_CONTROL:
1270 case SCR_ERROR:
1271 ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
1272 break;
1273 case SCR_ACTIVE:
1274 ofs = SATA_ACTIVE_OFS; /* active is not with the others */
1275 break;
1276 default:
1277 ofs = 0xffffffffU;
1278 break;
1279 }
1280 return ofs;
1281}
1282
82ef04fb 1283static int mv_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
20f733e7
BR
1284{
1285 unsigned int ofs = mv_scr_offset(sc_reg_in);
1286
da3dbb17 1287 if (ofs != 0xffffffffU) {
82ef04fb 1288 *val = readl(mv_ap_base(link->ap) + ofs);
da3dbb17
TH
1289 return 0;
1290 } else
1291 return -EINVAL;
20f733e7
BR
1292}
1293
82ef04fb 1294static int mv_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
20f733e7
BR
1295{
1296 unsigned int ofs = mv_scr_offset(sc_reg_in);
1297
da3dbb17 1298 if (ofs != 0xffffffffU) {
20091773
ML
1299 void __iomem *addr = mv_ap_base(link->ap) + ofs;
1300 if (sc_reg_in == SCR_CONTROL) {
1301 /*
1302 * Workaround for 88SX60x1 FEr SATA#26:
1303 *
1304 * COMRESETs have to take care not to accidently
1305 * put the drive to sleep when writing SCR_CONTROL.
1306 * Setting bits 12..15 prevents this problem.
1307 *
1308 * So if we see an outbound COMMRESET, set those bits.
1309 * Ditto for the followup write that clears the reset.
1310 *
1311 * The proprietary driver does this for
1312 * all chip versions, and so do we.
1313 */
1314 if ((val & 0xf) == 1 || (readl(addr) & 0xf) == 1)
1315 val |= 0xf000;
1316 }
1317 writelfl(val, addr);
da3dbb17
TH
1318 return 0;
1319 } else
1320 return -EINVAL;
20f733e7
BR
1321}
1322
f273827e
ML
1323static void mv6_dev_config(struct ata_device *adev)
1324{
1325 /*
e49856d8
ML
1326 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1327 *
1328 * Gen-II does not support NCQ over a port multiplier
1329 * (no FIS-based switching).
f273827e 1330 */
e49856d8 1331 if (adev->flags & ATA_DFLAG_NCQ) {
352fab70 1332 if (sata_pmp_attached(adev->link->ap)) {
e49856d8 1333 adev->flags &= ~ATA_DFLAG_NCQ;
352fab70
ML
1334 ata_dev_printk(adev, KERN_INFO,
1335 "NCQ disabled for command-based switching\n");
352fab70 1336 }
e49856d8 1337 }
f273827e
ML
1338}
1339
3e4a1391
ML
1340static int mv_qc_defer(struct ata_queued_cmd *qc)
1341{
1342 struct ata_link *link = qc->dev->link;
1343 struct ata_port *ap = link->ap;
1344 struct mv_port_priv *pp = ap->private_data;
1345
29d187bb
ML
1346 /*
1347 * Don't allow new commands if we're in a delayed EH state
1348 * for NCQ and/or FIS-based switching.
1349 */
1350 if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
1351 return ATA_DEFER_PORT;
3e4a1391
ML
1352 /*
1353 * If the port is completely idle, then allow the new qc.
1354 */
1355 if (ap->nr_active_links == 0)
1356 return 0;
1357
4bdee6c5
TH
1358 /*
1359 * The port is operating in host queuing mode (EDMA) with NCQ
1360 * enabled, allow multiple NCQ commands. EDMA also allows
1361 * queueing multiple DMA commands but libata core currently
1362 * doesn't allow it.
1363 */
1364 if ((pp->pp_flags & MV_PP_FLAG_EDMA_EN) &&
1365 (pp->pp_flags & MV_PP_FLAG_NCQ_EN) && ata_is_ncq(qc->tf.protocol))
1366 return 0;
1367
3e4a1391
ML
1368 return ATA_DEFER_PORT;
1369}
1370
08da1759 1371static void mv_config_fbs(struct ata_port *ap, int want_ncq, int want_fbs)
e49856d8 1372{
08da1759
ML
1373 struct mv_port_priv *pp = ap->private_data;
1374 void __iomem *port_mmio;
00f42eab 1375
08da1759
ML
1376 u32 fiscfg, *old_fiscfg = &pp->cached.fiscfg;
1377 u32 ltmode, *old_ltmode = &pp->cached.ltmode;
1378 u32 haltcond, *old_haltcond = &pp->cached.haltcond;
00f42eab 1379
08da1759
ML
1380 ltmode = *old_ltmode & ~LTMODE_BIT8;
1381 haltcond = *old_haltcond | EDMA_ERR_DEV;
00f42eab
ML
1382
1383 if (want_fbs) {
08da1759
ML
1384 fiscfg = *old_fiscfg | FISCFG_SINGLE_SYNC;
1385 ltmode = *old_ltmode | LTMODE_BIT8;
4c299ca3 1386 if (want_ncq)
08da1759 1387 haltcond &= ~EDMA_ERR_DEV;
4c299ca3 1388 else
08da1759
ML
1389 fiscfg |= FISCFG_WAIT_DEV_ERR;
1390 } else {
1391 fiscfg = *old_fiscfg & ~(FISCFG_SINGLE_SYNC | FISCFG_WAIT_DEV_ERR);
e49856d8 1392 }
00f42eab 1393
08da1759
ML
1394 port_mmio = mv_ap_base(ap);
1395 mv_write_cached_reg(port_mmio + FISCFG_OFS, old_fiscfg, fiscfg);
1396 mv_write_cached_reg(port_mmio + LTMODE_OFS, old_ltmode, ltmode);
1397 mv_write_cached_reg(port_mmio + EDMA_HALTCOND_OFS, old_haltcond, haltcond);
f273827e
ML
1398}
1399
dd2890f6
ML
1400static void mv_60x1_errata_sata25(struct ata_port *ap, int want_ncq)
1401{
1402 struct mv_host_priv *hpriv = ap->host->private_data;
1403 u32 old, new;
1404
1405 /* workaround for 88SX60x1 FEr SATA#25 (part 1) */
1406 old = readl(hpriv->base + MV_GPIO_PORT_CTL_OFS);
1407 if (want_ncq)
1408 new = old | (1 << 22);
1409 else
1410 new = old & ~(1 << 22);
1411 if (new != old)
1412 writel(new, hpriv->base + MV_GPIO_PORT_CTL_OFS);
1413}
1414
c01e8a23 1415/**
40f21b11
ML
1416 * mv_bmdma_enable - set a magic bit on GEN_IIE to allow bmdma
1417 * @ap: Port being initialized
c01e8a23
ML
1418 *
1419 * There are two DMA modes on these chips: basic DMA, and EDMA.
1420 *
1421 * Bit-0 of the "EDMA RESERVED" register enables/disables use
1422 * of basic DMA on the GEN_IIE versions of the chips.
1423 *
1424 * This bit survives EDMA resets, and must be set for basic DMA
1425 * to function, and should be cleared when EDMA is active.
1426 */
1427static void mv_bmdma_enable_iie(struct ata_port *ap, int enable_bmdma)
1428{
1429 struct mv_port_priv *pp = ap->private_data;
1430 u32 new, *old = &pp->cached.unknown_rsvd;
1431
1432 if (enable_bmdma)
1433 new = *old | 1;
1434 else
1435 new = *old & ~1;
1436 mv_write_cached_reg(mv_ap_base(ap) + EDMA_UNKNOWN_RSVD_OFS, old, new);
1437}
1438
000b344f
ML
1439/*
1440 * SOC chips have an issue whereby the HDD LEDs don't always blink
1441 * during I/O when NCQ is enabled. Enabling a special "LED blink" mode
1442 * of the SOC takes care of it, generating a steady blink rate when
1443 * any drive on the chip is active.
1444 *
1445 * Unfortunately, the blink mode is a global hardware setting for the SOC,
1446 * so we must use it whenever at least one port on the SOC has NCQ enabled.
1447 *
1448 * We turn "LED blink" off when NCQ is not in use anywhere, because the normal
1449 * LED operation works then, and provides better (more accurate) feedback.
1450 *
1451 * Note that this code assumes that an SOC never has more than one HC onboard.
1452 */
1453static void mv_soc_led_blink_enable(struct ata_port *ap)
1454{
1455 struct ata_host *host = ap->host;
1456 struct mv_host_priv *hpriv = host->private_data;
1457 void __iomem *hc_mmio;
1458 u32 led_ctrl;
1459
1460 if (hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN)
1461 return;
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
1468static void mv_soc_led_blink_disable(struct ata_port *ap)
1469{
1470 struct ata_host *host = ap->host;
1471 struct mv_host_priv *hpriv = host->private_data;
1472 void __iomem *hc_mmio;
1473 u32 led_ctrl;
1474 unsigned int port;
1475
1476 if (!(hpriv->hp_flags & MV_HP_QUIRK_LED_BLINK_EN))
1477 return;
1478
1479 /* disable led-blink only if no ports are using NCQ */
1480 for (port = 0; port < hpriv->n_ports; port++) {
1481 struct ata_port *this_ap = host->ports[port];
1482 struct mv_port_priv *pp = this_ap->private_data;
1483
1484 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1485 return;
1486 }
1487
1488 hpriv->hp_flags &= ~MV_HP_QUIRK_LED_BLINK_EN;
1489 hc_mmio = mv_hc_base_from_port(mv_host_base(host), ap->port_no);
1490 led_ctrl = readl(hc_mmio + SOC_LED_CTRL_OFS);
1491 writel(led_ctrl & ~SOC_LED_CTRL_BLINK, hc_mmio + SOC_LED_CTRL_OFS);
1492}
1493
00b81235 1494static void mv_edma_cfg(struct ata_port *ap, int want_ncq, int want_edma)
e4e7b892 1495{
0c58912e 1496 u32 cfg;
e12bef50
ML
1497 struct mv_port_priv *pp = ap->private_data;
1498 struct mv_host_priv *hpriv = ap->host->private_data;
1499 void __iomem *port_mmio = mv_ap_base(ap);
e4e7b892
JG
1500
1501 /* set up non-NCQ EDMA configuration */
0c58912e 1502 cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
d16ab3f6
ML
1503 pp->pp_flags &=
1504 ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
e4e7b892 1505
0c58912e 1506 if (IS_GEN_I(hpriv))
e4e7b892
JG
1507 cfg |= (1 << 8); /* enab config burst size mask */
1508
dd2890f6 1509 else if (IS_GEN_II(hpriv)) {
e4e7b892 1510 cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
dd2890f6 1511 mv_60x1_errata_sata25(ap, want_ncq);
e4e7b892 1512
dd2890f6 1513 } else if (IS_GEN_IIE(hpriv)) {
00f42eab
ML
1514 int want_fbs = sata_pmp_attached(ap);
1515 /*
1516 * Possible future enhancement:
1517 *
1518 * The chip can use FBS with non-NCQ, if we allow it,
1519 * But first we need to have the error handling in place
1520 * for this mode (datasheet section 7.3.15.4.2.3).
1521 * So disallow non-NCQ FBS for now.
1522 */
1523 want_fbs &= want_ncq;
1524
08da1759 1525 mv_config_fbs(ap, want_ncq, want_fbs);
00f42eab
ML
1526
1527 if (want_fbs) {
1528 pp->pp_flags |= MV_PP_FLAG_FBS_EN;
1529 cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
1530 }
1531
e728eabe 1532 cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
00b81235
ML
1533 if (want_edma) {
1534 cfg |= (1 << 22); /* enab 4-entry host queue cache */
1535 if (!IS_SOC(hpriv))
1536 cfg |= (1 << 18); /* enab early completion */
1537 }
616d4a98
ML
1538 if (hpriv->hp_flags & MV_HP_CUT_THROUGH)
1539 cfg |= (1 << 17); /* enab cut-thru (dis stor&forwrd) */
c01e8a23 1540 mv_bmdma_enable_iie(ap, !want_edma);
000b344f
ML
1541
1542 if (IS_SOC(hpriv)) {
1543 if (want_ncq)
1544 mv_soc_led_blink_enable(ap);
1545 else
1546 mv_soc_led_blink_disable(ap);
1547 }
e4e7b892
JG
1548 }
1549
72109168
ML
1550 if (want_ncq) {
1551 cfg |= EDMA_CFG_NCQ;
1552 pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
00b81235 1553 }
72109168 1554
e4e7b892
JG
1555 writelfl(cfg, port_mmio + EDMA_CFG_OFS);
1556}
1557
da2fa9ba
ML
1558static void mv_port_free_dma_mem(struct ata_port *ap)
1559{
1560 struct mv_host_priv *hpriv = ap->host->private_data;
1561 struct mv_port_priv *pp = ap->private_data;
eb73d558 1562 int tag;
da2fa9ba
ML
1563
1564 if (pp->crqb) {
1565 dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1566 pp->crqb = NULL;
1567 }
1568 if (pp->crpb) {
1569 dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1570 pp->crpb = NULL;
1571 }
eb73d558
ML
1572 /*
1573 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1574 * For later hardware, we have one unique sg_tbl per NCQ tag.
1575 */
1576 for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1577 if (pp->sg_tbl[tag]) {
1578 if (tag == 0 || !IS_GEN_I(hpriv))
1579 dma_pool_free(hpriv->sg_tbl_pool,
1580 pp->sg_tbl[tag],
1581 pp->sg_tbl_dma[tag]);
1582 pp->sg_tbl[tag] = NULL;
1583 }
da2fa9ba
ML
1584 }
1585}
1586
05b308e1
BR
1587/**
1588 * mv_port_start - Port specific init/start routine.
1589 * @ap: ATA channel to manipulate
1590 *
1591 * Allocate and point to DMA memory, init port private memory,
1592 * zero indices.
1593 *
1594 * LOCKING:
1595 * Inherited from caller.
1596 */
31961943
BR
1597static int mv_port_start(struct ata_port *ap)
1598{
cca3974e
JG
1599 struct device *dev = ap->host->dev;
1600 struct mv_host_priv *hpriv = ap->host->private_data;
31961943 1601 struct mv_port_priv *pp;
933cb8e5 1602 unsigned long flags;
dde20207 1603 int tag;
31961943 1604
24dc5f33 1605 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
6037d6bb 1606 if (!pp)
24dc5f33 1607 return -ENOMEM;
da2fa9ba 1608 ap->private_data = pp;
31961943 1609
da2fa9ba
ML
1610 pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1611 if (!pp->crqb)
1612 return -ENOMEM;
1613 memset(pp->crqb, 0, MV_CRQB_Q_SZ);
31961943 1614
da2fa9ba
ML
1615 pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1616 if (!pp->crpb)
1617 goto out_port_free_dma_mem;
1618 memset(pp->crpb, 0, MV_CRPB_Q_SZ);
31961943 1619
3bd0a70e
ML
1620 /* 6041/6081 Rev. "C0" (and newer) are okay with async notify */
1621 if (hpriv->hp_flags & MV_HP_ERRATA_60X1C0)
1622 ap->flags |= ATA_FLAG_AN;
eb73d558
ML
1623 /*
1624 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1625 * For later hardware, we need one unique sg_tbl per NCQ tag.
1626 */
1627 for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1628 if (tag == 0 || !IS_GEN_I(hpriv)) {
1629 pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1630 GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1631 if (!pp->sg_tbl[tag])
1632 goto out_port_free_dma_mem;
1633 } else {
1634 pp->sg_tbl[tag] = pp->sg_tbl[0];
1635 pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1636 }
1637 }
933cb8e5
ML
1638
1639 spin_lock_irqsave(ap->lock, flags);
08da1759 1640 mv_save_cached_regs(ap);
66e57a2c 1641 mv_edma_cfg(ap, 0, 0);
933cb8e5
ML
1642 spin_unlock_irqrestore(ap->lock, flags);
1643
31961943 1644 return 0;
da2fa9ba
ML
1645
1646out_port_free_dma_mem:
1647 mv_port_free_dma_mem(ap);
1648 return -ENOMEM;
31961943
BR
1649}
1650
05b308e1
BR
1651/**
1652 * mv_port_stop - Port specific cleanup/stop routine.
1653 * @ap: ATA channel to manipulate
1654 *
1655 * Stop DMA, cleanup port memory.
1656 *
1657 * LOCKING:
cca3974e 1658 * This routine uses the host lock to protect the DMA stop.
05b308e1 1659 */
31961943
BR
1660static void mv_port_stop(struct ata_port *ap)
1661{
933cb8e5
ML
1662 unsigned long flags;
1663
1664 spin_lock_irqsave(ap->lock, flags);
e12bef50 1665 mv_stop_edma(ap);
88e675e1 1666 mv_enable_port_irqs(ap, 0);
933cb8e5 1667 spin_unlock_irqrestore(ap->lock, flags);
da2fa9ba 1668 mv_port_free_dma_mem(ap);
31961943
BR
1669}
1670
05b308e1
BR
1671/**
1672 * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1673 * @qc: queued command whose SG list to source from
1674 *
1675 * Populate the SG list and mark the last entry.
1676 *
1677 * LOCKING:
1678 * Inherited from caller.
1679 */
6c08772e 1680static void mv_fill_sg(struct ata_queued_cmd *qc)
31961943
BR
1681{
1682 struct mv_port_priv *pp = qc->ap->private_data;
972c26bd 1683 struct scatterlist *sg;
3be6cbd7 1684 struct mv_sg *mv_sg, *last_sg = NULL;
ff2aeb1e 1685 unsigned int si;
31961943 1686
eb73d558 1687 mv_sg = pp->sg_tbl[qc->tag];
ff2aeb1e 1688 for_each_sg(qc->sg, sg, qc->n_elem, si) {
d88184fb
JG
1689 dma_addr_t addr = sg_dma_address(sg);
1690 u32 sg_len = sg_dma_len(sg);
22374677 1691
4007b493
OJ
1692 while (sg_len) {
1693 u32 offset = addr & 0xffff;
1694 u32 len = sg_len;
22374677 1695
32cd11a6 1696 if (offset + len > 0x10000)
4007b493
OJ
1697 len = 0x10000 - offset;
1698
1699 mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1700 mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
6c08772e 1701 mv_sg->flags_size = cpu_to_le32(len & 0xffff);
32cd11a6 1702 mv_sg->reserved = 0;
4007b493
OJ
1703
1704 sg_len -= len;
1705 addr += len;
1706
3be6cbd7 1707 last_sg = mv_sg;
4007b493 1708 mv_sg++;
4007b493 1709 }
31961943 1710 }
3be6cbd7
JG
1711
1712 if (likely(last_sg))
1713 last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
32cd11a6 1714 mb(); /* ensure data structure is visible to the chipset */
31961943
BR
1715}
1716
5796d1c4 1717static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
31961943 1718{
559eedad 1719 u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
31961943 1720 (last ? CRQB_CMD_LAST : 0);
559eedad 1721 *cmdw = cpu_to_le16(tmp);
31961943
BR
1722}
1723
da14265e
ML
1724/**
1725 * mv_sff_irq_clear - Clear hardware interrupt after DMA.
1726 * @ap: Port associated with this ATA transaction.
1727 *
1728 * We need this only for ATAPI bmdma transactions,
1729 * as otherwise we experience spurious interrupts
1730 * after libata-sff handles the bmdma interrupts.
1731 */
1732static void mv_sff_irq_clear(struct ata_port *ap)
1733{
1734 mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), ERR_IRQ);
1735}
1736
1737/**
1738 * mv_check_atapi_dma - Filter ATAPI cmds which are unsuitable for DMA.
1739 * @qc: queued command to check for chipset/DMA compatibility.
1740 *
1741 * The bmdma engines cannot handle speculative data sizes
1742 * (bytecount under/over flow). So only allow DMA for
1743 * data transfer commands with known data sizes.
1744 *
1745 * LOCKING:
1746 * Inherited from caller.
1747 */
1748static int mv_check_atapi_dma(struct ata_queued_cmd *qc)
1749{
1750 struct scsi_cmnd *scmd = qc->scsicmd;
1751
1752 if (scmd) {
1753 switch (scmd->cmnd[0]) {
1754 case READ_6:
1755 case READ_10:
1756 case READ_12:
1757 case WRITE_6:
1758 case WRITE_10:
1759 case WRITE_12:
1760 case GPCMD_READ_CD:
1761 case GPCMD_SEND_DVD_STRUCTURE:
1762 case GPCMD_SEND_CUE_SHEET:
1763 return 0; /* DMA is safe */
1764 }
1765 }
1766 return -EOPNOTSUPP; /* use PIO instead */
1767}
1768
1769/**
1770 * mv_bmdma_setup - Set up BMDMA transaction
1771 * @qc: queued command to prepare DMA for.
1772 *
1773 * LOCKING:
1774 * Inherited from caller.
1775 */
1776static void mv_bmdma_setup(struct ata_queued_cmd *qc)
1777{
1778 struct ata_port *ap = qc->ap;
1779 void __iomem *port_mmio = mv_ap_base(ap);
1780 struct mv_port_priv *pp = ap->private_data;
1781
1782 mv_fill_sg(qc);
1783
1784 /* clear all DMA cmd bits */
1785 writel(0, port_mmio + BMDMA_CMD_OFS);
1786
1787 /* load PRD table addr. */
1788 writel((pp->sg_tbl_dma[qc->tag] >> 16) >> 16,
1789 port_mmio + BMDMA_PRD_HIGH_OFS);
1790 writelfl(pp->sg_tbl_dma[qc->tag],
1791 port_mmio + BMDMA_PRD_LOW_OFS);
1792
1793 /* issue r/w command */
1794 ap->ops->sff_exec_command(ap, &qc->tf);
1795}
1796
1797/**
1798 * mv_bmdma_start - Start a BMDMA transaction
1799 * @qc: queued command to start DMA on.
1800 *
1801 * LOCKING:
1802 * Inherited from caller.
1803 */
1804static void mv_bmdma_start(struct ata_queued_cmd *qc)
1805{
1806 struct ata_port *ap = qc->ap;
1807 void __iomem *port_mmio = mv_ap_base(ap);
1808 unsigned int rw = (qc->tf.flags & ATA_TFLAG_WRITE);
1809 u32 cmd = (rw ? 0 : ATA_DMA_WR) | ATA_DMA_START;
1810
1811 /* start host DMA transaction */
1812 writelfl(cmd, port_mmio + BMDMA_CMD_OFS);
1813}
1814
1815/**
1816 * mv_bmdma_stop - Stop BMDMA transfer
1817 * @qc: queued command to stop DMA on.
1818 *
1819 * Clears the ATA_DMA_START flag in the bmdma control register
1820 *
1821 * LOCKING:
1822 * Inherited from caller.
1823 */
1824static void mv_bmdma_stop(struct ata_queued_cmd *qc)
1825{
1826 struct ata_port *ap = qc->ap;
1827 void __iomem *port_mmio = mv_ap_base(ap);
1828 u32 cmd;
1829
1830 /* clear start/stop bit */
1831 cmd = readl(port_mmio + BMDMA_CMD_OFS);
1832 cmd &= ~ATA_DMA_START;
1833 writelfl(cmd, port_mmio + BMDMA_CMD_OFS);
1834
1835 /* one-PIO-cycle guaranteed wait, per spec, for HDMA1:0 transition */
1836 ata_sff_dma_pause(ap);
1837}
1838
1839/**
1840 * mv_bmdma_status - Read BMDMA status
1841 * @ap: port for which to retrieve DMA status.
1842 *
1843 * Read and return equivalent of the sff BMDMA status register.
1844 *
1845 * LOCKING:
1846 * Inherited from caller.
1847 */
1848static u8 mv_bmdma_status(struct ata_port *ap)
1849{
1850 void __iomem *port_mmio = mv_ap_base(ap);
1851 u32 reg, status;
1852
1853 /*
1854 * Other bits are valid only if ATA_DMA_ACTIVE==0,
1855 * and the ATA_DMA_INTR bit doesn't exist.
1856 */
1857 reg = readl(port_mmio + BMDMA_STATUS_OFS);
1858 if (reg & ATA_DMA_ACTIVE)
1859 status = ATA_DMA_ACTIVE;
1860 else
1861 status = (reg & ATA_DMA_ERR) | ATA_DMA_INTR;
1862 return status;
1863}
1864
05b308e1
BR
1865/**
1866 * mv_qc_prep - Host specific command preparation.
1867 * @qc: queued command to prepare
1868 *
1869 * This routine simply redirects to the general purpose routine
1870 * if command is not DMA. Else, it handles prep of the CRQB
1871 * (command request block), does some sanity checking, and calls
1872 * the SG load routine.
1873 *
1874 * LOCKING:
1875 * Inherited from caller.
1876 */
31961943
BR
1877static void mv_qc_prep(struct ata_queued_cmd *qc)
1878{
1879 struct ata_port *ap = qc->ap;
1880 struct mv_port_priv *pp = ap->private_data;
e1469874 1881 __le16 *cw;
31961943
BR
1882 struct ata_taskfile *tf;
1883 u16 flags = 0;
a6432436 1884 unsigned in_index;
31961943 1885
138bfdd0
ML
1886 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1887 (qc->tf.protocol != ATA_PROT_NCQ))
31961943 1888 return;
20f733e7 1889
31961943
BR
1890 /* Fill in command request block
1891 */
e4e7b892 1892 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
31961943 1893 flags |= CRQB_FLAG_READ;
beec7dbc 1894 WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
31961943 1895 flags |= qc->tag << CRQB_TAG_SHIFT;
e49856d8 1896 flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
31961943 1897
bdd4ddde 1898 /* get current queue index from software */
fcfb1f77 1899 in_index = pp->req_idx;
a6432436
ML
1900
1901 pp->crqb[in_index].sg_addr =
eb73d558 1902 cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
a6432436 1903 pp->crqb[in_index].sg_addr_hi =
eb73d558 1904 cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
a6432436 1905 pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
31961943 1906
a6432436 1907 cw = &pp->crqb[in_index].ata_cmd[0];
31961943
BR
1908 tf = &qc->tf;
1909
1910 /* Sadly, the CRQB cannot accomodate all registers--there are
1911 * only 11 bytes...so we must pick and choose required
1912 * registers based on the command. So, we drop feature and
1913 * hob_feature for [RW] DMA commands, but they are needed for
cd12e1f7
ML
1914 * NCQ. NCQ will drop hob_nsect, which is not needed there
1915 * (nsect is used only for the tag; feat/hob_feat hold true nsect).
20f733e7 1916 */
31961943
BR
1917 switch (tf->command) {
1918 case ATA_CMD_READ:
1919 case ATA_CMD_READ_EXT:
1920 case ATA_CMD_WRITE:
1921 case ATA_CMD_WRITE_EXT:
c15d85c8 1922 case ATA_CMD_WRITE_FUA_EXT:
31961943
BR
1923 mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1924 break;
31961943
BR
1925 case ATA_CMD_FPDMA_READ:
1926 case ATA_CMD_FPDMA_WRITE:
8b260248 1927 mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
31961943
BR
1928 mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1929 break;
31961943
BR
1930 default:
1931 /* The only other commands EDMA supports in non-queued and
1932 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1933 * of which are defined/used by Linux. If we get here, this
1934 * driver needs work.
1935 *
1936 * FIXME: modify libata to give qc_prep a return value and
1937 * return error here.
1938 */
1939 BUG_ON(tf->command);
1940 break;
1941 }
1942 mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1943 mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1944 mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1945 mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1946 mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1947 mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1948 mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1949 mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1950 mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
1951
e4e7b892
JG
1952 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1953 return;
1954 mv_fill_sg(qc);
1955}
1956
1957/**
1958 * mv_qc_prep_iie - Host specific command preparation.
1959 * @qc: queued command to prepare
1960 *
1961 * This routine simply redirects to the general purpose routine
1962 * if command is not DMA. Else, it handles prep of the CRQB
1963 * (command request block), does some sanity checking, and calls
1964 * the SG load routine.
1965 *
1966 * LOCKING:
1967 * Inherited from caller.
1968 */
1969static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
1970{
1971 struct ata_port *ap = qc->ap;
1972 struct mv_port_priv *pp = ap->private_data;
1973 struct mv_crqb_iie *crqb;
1974 struct ata_taskfile *tf;
a6432436 1975 unsigned in_index;
e4e7b892
JG
1976 u32 flags = 0;
1977
138bfdd0
ML
1978 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1979 (qc->tf.protocol != ATA_PROT_NCQ))
e4e7b892
JG
1980 return;
1981
e12bef50 1982 /* Fill in Gen IIE command request block */
e4e7b892
JG
1983 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1984 flags |= CRQB_FLAG_READ;
1985
beec7dbc 1986 WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
e4e7b892 1987 flags |= qc->tag << CRQB_TAG_SHIFT;
8c0aeb4a 1988 flags |= qc->tag << CRQB_HOSTQ_SHIFT;
e49856d8 1989 flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
e4e7b892 1990
bdd4ddde 1991 /* get current queue index from software */
fcfb1f77 1992 in_index = pp->req_idx;
a6432436
ML
1993
1994 crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
eb73d558
ML
1995 crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1996 crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
e4e7b892
JG
1997 crqb->flags = cpu_to_le32(flags);
1998
1999 tf = &qc->tf;
2000 crqb->ata_cmd[0] = cpu_to_le32(
2001 (tf->command << 16) |
2002 (tf->feature << 24)
2003 );
2004 crqb->ata_cmd[1] = cpu_to_le32(
2005 (tf->lbal << 0) |
2006 (tf->lbam << 8) |
2007 (tf->lbah << 16) |
2008 (tf->device << 24)
2009 );
2010 crqb->ata_cmd[2] = cpu_to_le32(
2011 (tf->hob_lbal << 0) |
2012 (tf->hob_lbam << 8) |
2013 (tf->hob_lbah << 16) |
2014 (tf->hob_feature << 24)
2015 );
2016 crqb->ata_cmd[3] = cpu_to_le32(
2017 (tf->nsect << 0) |
2018 (tf->hob_nsect << 8)
2019 );
2020
2021 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
31961943 2022 return;
31961943
BR
2023 mv_fill_sg(qc);
2024}
2025
d16ab3f6
ML
2026/**
2027 * mv_sff_check_status - fetch device status, if valid
2028 * @ap: ATA port to fetch status from
2029 *
2030 * When using command issue via mv_qc_issue_fis(),
2031 * the initial ATA_BUSY state does not show up in the
2032 * ATA status (shadow) register. This can confuse libata!
2033 *
2034 * So we have a hook here to fake ATA_BUSY for that situation,
2035 * until the first time a BUSY, DRQ, or ERR bit is seen.
2036 *
2037 * The rest of the time, it simply returns the ATA status register.
2038 */
2039static u8 mv_sff_check_status(struct ata_port *ap)
2040{
2041 u8 stat = ioread8(ap->ioaddr.status_addr);
2042 struct mv_port_priv *pp = ap->private_data;
2043
2044 if (pp->pp_flags & MV_PP_FLAG_FAKE_ATA_BUSY) {
2045 if (stat & (ATA_BUSY | ATA_DRQ | ATA_ERR))
2046 pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY;
2047 else
2048 stat = ATA_BUSY;
2049 }
2050 return stat;
2051}
2052
70f8b79c
ML
2053/**
2054 * mv_send_fis - Send a FIS, using the "Vendor-Unique FIS" register
2055 * @fis: fis to be sent
2056 * @nwords: number of 32-bit words in the fis
2057 */
2058static unsigned int mv_send_fis(struct ata_port *ap, u32 *fis, int nwords)
2059{
2060 void __iomem *port_mmio = mv_ap_base(ap);
2061 u32 ifctl, old_ifctl, ifstat;
2062 int i, timeout = 200, final_word = nwords - 1;
2063
2064 /* Initiate FIS transmission mode */
2065 old_ifctl = readl(port_mmio + SATA_IFCTL_OFS);
2066 ifctl = 0x100 | (old_ifctl & 0xf);
2067 writelfl(ifctl, port_mmio + SATA_IFCTL_OFS);
2068
2069 /* Send all words of the FIS except for the final word */
2070 for (i = 0; i < final_word; ++i)
2071 writel(fis[i], port_mmio + VENDOR_UNIQUE_FIS_OFS);
2072
2073 /* Flag end-of-transmission, and then send the final word */
2074 writelfl(ifctl | 0x200, port_mmio + SATA_IFCTL_OFS);
2075 writelfl(fis[final_word], port_mmio + VENDOR_UNIQUE_FIS_OFS);
2076
2077 /*
2078 * Wait for FIS transmission to complete.
2079 * This typically takes just a single iteration.
2080 */
2081 do {
2082 ifstat = readl(port_mmio + SATA_IFSTAT_OFS);
2083 } while (!(ifstat & 0x1000) && --timeout);
2084
2085 /* Restore original port configuration */
2086 writelfl(old_ifctl, port_mmio + SATA_IFCTL_OFS);
2087
2088 /* See if it worked */
2089 if ((ifstat & 0x3000) != 0x1000) {
2090 ata_port_printk(ap, KERN_WARNING,
2091 "%s transmission error, ifstat=%08x\n",
2092 __func__, ifstat);
2093 return AC_ERR_OTHER;
2094 }
2095 return 0;
2096}
2097
2098/**
2099 * mv_qc_issue_fis - Issue a command directly as a FIS
2100 * @qc: queued command to start
2101 *
2102 * Note that the ATA shadow registers are not updated
2103 * after command issue, so the device will appear "READY"
2104 * if polled, even while it is BUSY processing the command.
2105 *
2106 * So we use a status hook to fake ATA_BUSY until the drive changes state.
2107 *
2108 * Note: we don't get updated shadow regs on *completion*
2109 * of non-data commands. So avoid sending them via this function,
2110 * as they will appear to have completed immediately.
2111 *
2112 * GEN_IIE has special registers that we could get the result tf from,
2113 * but earlier chipsets do not. For now, we ignore those registers.
2114 */
2115static unsigned int mv_qc_issue_fis(struct ata_queued_cmd *qc)
2116{
2117 struct ata_port *ap = qc->ap;
2118 struct mv_port_priv *pp = ap->private_data;
2119 struct ata_link *link = qc->dev->link;
2120 u32 fis[5];
2121 int err = 0;
2122
2123 ata_tf_to_fis(&qc->tf, link->pmp, 1, (void *)fis);
2124 err = mv_send_fis(ap, fis, sizeof(fis) / sizeof(fis[0]));
2125 if (err)
2126 return err;
2127
2128 switch (qc->tf.protocol) {
2129 case ATAPI_PROT_PIO:
2130 pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
2131 /* fall through */
2132 case ATAPI_PROT_NODATA:
2133 ap->hsm_task_state = HSM_ST_FIRST;
2134 break;
2135 case ATA_PROT_PIO:
2136 pp->pp_flags |= MV_PP_FLAG_FAKE_ATA_BUSY;
2137 if (qc->tf.flags & ATA_TFLAG_WRITE)
2138 ap->hsm_task_state = HSM_ST_FIRST;
2139 else
2140 ap->hsm_task_state = HSM_ST;
2141 break;
2142 default:
2143 ap->hsm_task_state = HSM_ST_LAST;
2144 break;
2145 }
2146
2147 if (qc->tf.flags & ATA_TFLAG_POLLING)
2148 ata_pio_queue_task(ap, qc, 0);
2149 return 0;
2150}
2151
05b308e1
BR
2152/**
2153 * mv_qc_issue - Initiate a command to the host
2154 * @qc: queued command to start
2155 *
2156 * This routine simply redirects to the general purpose routine
2157 * if command is not DMA. Else, it sanity checks our local
2158 * caches of the request producer/consumer indices then enables
2159 * DMA and bumps the request producer index.
2160 *
2161 * LOCKING:
2162 * Inherited from caller.
2163 */
9a3d9eb0 2164static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
31961943 2165{
f48765cc 2166 static int limit_warnings = 10;
c5d3e45a
JG
2167 struct ata_port *ap = qc->ap;
2168 void __iomem *port_mmio = mv_ap_base(ap);
2169 struct mv_port_priv *pp = ap->private_data;
bdd4ddde 2170 u32 in_index;
42ed893d 2171 unsigned int port_irqs;
f48765cc 2172
d16ab3f6
ML
2173 pp->pp_flags &= ~MV_PP_FLAG_FAKE_ATA_BUSY; /* paranoia */
2174
f48765cc
ML
2175 switch (qc->tf.protocol) {
2176 case ATA_PROT_DMA:
2177 case ATA_PROT_NCQ:
2178 mv_start_edma(ap, port_mmio, pp, qc->tf.protocol);
2179 pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
2180 in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
2181
2182 /* Write the request in pointer to kick the EDMA to life */
2183 writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
2184 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
2185 return 0;
31961943 2186
f48765cc 2187 case ATA_PROT_PIO:
c6112bd8
ML
2188 /*
2189 * Errata SATA#16, SATA#24: warn if multiple DRQs expected.
2190 *
2191 * Someday, we might implement special polling workarounds
2192 * for these, but it all seems rather unnecessary since we
2193 * normally use only DMA for commands which transfer more
2194 * than a single block of data.
2195 *
2196 * Much of the time, this could just work regardless.
2197 * So for now, just log the incident, and allow the attempt.
2198 */
c7843e8f 2199 if (limit_warnings > 0 && (qc->nbytes / qc->sect_size) > 1) {
c6112bd8
ML
2200 --limit_warnings;
2201 ata_link_printk(qc->dev->link, KERN_WARNING, DRV_NAME
2202 ": attempting PIO w/multiple DRQ: "
2203 "this may fail due to h/w errata\n");
2204 }
f48765cc 2205 /* drop through */
42ed893d 2206 case ATA_PROT_NODATA:
f48765cc 2207 case ATAPI_PROT_PIO:
42ed893d
ML
2208 case ATAPI_PROT_NODATA:
2209 if (ap->flags & ATA_FLAG_PIO_POLLING)
2210 qc->tf.flags |= ATA_TFLAG_POLLING;
2211 break;
31961943 2212 }
42ed893d
ML
2213
2214 if (qc->tf.flags & ATA_TFLAG_POLLING)
2215 port_irqs = ERR_IRQ; /* mask device interrupt when polling */
2216 else
2217 port_irqs = ERR_IRQ | DONE_IRQ; /* unmask all interrupts */
2218
2219 /*
2220 * We're about to send a non-EDMA capable command to the
2221 * port. Turn off EDMA so there won't be problems accessing
2222 * shadow block, etc registers.
2223 */
2224 mv_stop_edma(ap);
2225 mv_clear_and_enable_port_irqs(ap, mv_ap_base(ap), port_irqs);
2226 mv_pmp_select(ap, qc->dev->link->pmp);
70f8b79c
ML
2227
2228 if (qc->tf.command == ATA_CMD_READ_LOG_EXT) {
2229 struct mv_host_priv *hpriv = ap->host->private_data;
2230 /*
2231 * Workaround for 88SX60x1 FEr SATA#25 (part 2).
40f21b11 2232 *
70f8b79c
ML
2233 * After any NCQ error, the READ_LOG_EXT command
2234 * from libata-eh *must* use mv_qc_issue_fis().
2235 * Otherwise it might fail, due to chip errata.
2236 *
2237 * Rather than special-case it, we'll just *always*
2238 * use this method here for READ_LOG_EXT, making for
2239 * easier testing.
2240 */
2241 if (IS_GEN_II(hpriv))
2242 return mv_qc_issue_fis(qc);
2243 }
42ed893d 2244 return ata_sff_qc_issue(qc);
31961943
BR
2245}
2246
8f767f8a
ML
2247static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
2248{
2249 struct mv_port_priv *pp = ap->private_data;
2250 struct ata_queued_cmd *qc;
2251
2252 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
2253 return NULL;
2254 qc = ata_qc_from_tag(ap, ap->link.active_tag);
95db5051
ML
2255 if (qc) {
2256 if (qc->tf.flags & ATA_TFLAG_POLLING)
2257 qc = NULL;
2258 else if (!(qc->flags & ATA_QCFLAG_ACTIVE))
2259 qc = NULL;
2260 }
8f767f8a
ML
2261 return qc;
2262}
2263
29d187bb
ML
2264static void mv_pmp_error_handler(struct ata_port *ap)
2265{
2266 unsigned int pmp, pmp_map;
2267 struct mv_port_priv *pp = ap->private_data;
2268
2269 if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH) {
2270 /*
2271 * Perform NCQ error analysis on failed PMPs
2272 * before we freeze the port entirely.
2273 *
2274 * The failed PMPs are marked earlier by mv_pmp_eh_prep().
2275 */
2276 pmp_map = pp->delayed_eh_pmp_map;
2277 pp->pp_flags &= ~MV_PP_FLAG_DELAYED_EH;
2278 for (pmp = 0; pmp_map != 0; pmp++) {
2279 unsigned int this_pmp = (1 << pmp);
2280 if (pmp_map & this_pmp) {
2281 struct ata_link *link = &ap->pmp_link[pmp];
2282 pmp_map &= ~this_pmp;
2283 ata_eh_analyze_ncq_error(link);
2284 }
2285 }
2286 ata_port_freeze(ap);
2287 }
2288 sata_pmp_error_handler(ap);
2289}
2290
4c299ca3
ML
2291static unsigned int mv_get_err_pmp_map(struct ata_port *ap)
2292{
2293 void __iomem *port_mmio = mv_ap_base(ap);
2294
2295 return readl(port_mmio + SATA_TESTCTL_OFS) >> 16;
2296}
2297
4c299ca3
ML
2298static void mv_pmp_eh_prep(struct ata_port *ap, unsigned int pmp_map)
2299{
2300 struct ata_eh_info *ehi;
2301 unsigned int pmp;
2302
2303 /*
2304 * Initialize EH info for PMPs which saw device errors
2305 */
2306 ehi = &ap->link.eh_info;
2307 for (pmp = 0; pmp_map != 0; pmp++) {
2308 unsigned int this_pmp = (1 << pmp);
2309 if (pmp_map & this_pmp) {
2310 struct ata_link *link = &ap->pmp_link[pmp];
2311
2312 pmp_map &= ~this_pmp;
2313 ehi = &link->eh_info;
2314 ata_ehi_clear_desc(ehi);
2315 ata_ehi_push_desc(ehi, "dev err");
2316 ehi->err_mask |= AC_ERR_DEV;
2317 ehi->action |= ATA_EH_RESET;
2318 ata_link_abort(link);
2319 }
2320 }
2321}
2322
06aaca3f
ML
2323static int mv_req_q_empty(struct ata_port *ap)
2324{
2325 void __iomem *port_mmio = mv_ap_base(ap);
2326 u32 in_ptr, out_ptr;
2327
2328 in_ptr = (readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS)
2329 >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2330 out_ptr = (readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS)
2331 >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2332 return (in_ptr == out_ptr); /* 1 == queue_is_empty */
2333}
2334
4c299ca3
ML
2335static int mv_handle_fbs_ncq_dev_err(struct ata_port *ap)
2336{
2337 struct mv_port_priv *pp = ap->private_data;
2338 int failed_links;
2339 unsigned int old_map, new_map;
2340
2341 /*
2342 * Device error during FBS+NCQ operation:
2343 *
2344 * Set a port flag to prevent further I/O being enqueued.
2345 * Leave the EDMA running to drain outstanding commands from this port.
2346 * Perform the post-mortem/EH only when all responses are complete.
2347 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.2).
2348 */
2349 if (!(pp->pp_flags & MV_PP_FLAG_DELAYED_EH)) {
2350 pp->pp_flags |= MV_PP_FLAG_DELAYED_EH;
2351 pp->delayed_eh_pmp_map = 0;
2352 }
2353 old_map = pp->delayed_eh_pmp_map;
2354 new_map = old_map | mv_get_err_pmp_map(ap);
2355
2356 if (old_map != new_map) {
2357 pp->delayed_eh_pmp_map = new_map;
2358 mv_pmp_eh_prep(ap, new_map & ~old_map);
2359 }
c46938cc 2360 failed_links = hweight16(new_map);
4c299ca3
ML
2361
2362 ata_port_printk(ap, KERN_INFO, "%s: pmp_map=%04x qc_map=%04x "
2363 "failed_links=%d nr_active_links=%d\n",
2364 __func__, pp->delayed_eh_pmp_map,
2365 ap->qc_active, failed_links,
2366 ap->nr_active_links);
2367
06aaca3f 2368 if (ap->nr_active_links <= failed_links && mv_req_q_empty(ap)) {
4c299ca3
ML
2369 mv_process_crpb_entries(ap, pp);
2370 mv_stop_edma(ap);
2371 mv_eh_freeze(ap);
2372 ata_port_printk(ap, KERN_INFO, "%s: done\n", __func__);
2373 return 1; /* handled */
2374 }
2375 ata_port_printk(ap, KERN_INFO, "%s: waiting\n", __func__);
2376 return 1; /* handled */
2377}
2378
2379static int mv_handle_fbs_non_ncq_dev_err(struct ata_port *ap)
2380{
2381 /*
2382 * Possible future enhancement:
2383 *
2384 * FBS+non-NCQ operation is not yet implemented.
2385 * See related notes in mv_edma_cfg().
2386 *
2387 * Device error during FBS+non-NCQ operation:
2388 *
2389 * We need to snapshot the shadow registers for each failed command.
2390 * Follow recovery sequence from 6042/7042 datasheet (7.3.15.4.2.3).
2391 */
2392 return 0; /* not handled */
2393}
2394
2395static int mv_handle_dev_err(struct ata_port *ap, u32 edma_err_cause)
2396{
2397 struct mv_port_priv *pp = ap->private_data;
2398
2399 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
2400 return 0; /* EDMA was not active: not handled */
2401 if (!(pp->pp_flags & MV_PP_FLAG_FBS_EN))
2402 return 0; /* FBS was not active: not handled */
2403
2404 if (!(edma_err_cause & EDMA_ERR_DEV))
2405 return 0; /* non DEV error: not handled */
2406 edma_err_cause &= ~EDMA_ERR_IRQ_TRANSIENT;
2407 if (edma_err_cause & ~(EDMA_ERR_DEV | EDMA_ERR_SELF_DIS))
2408 return 0; /* other problems: not handled */
2409
2410 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN) {
2411 /*
2412 * EDMA should NOT have self-disabled for this case.
2413 * If it did, then something is wrong elsewhere,
2414 * and we cannot handle it here.
2415 */
2416 if (edma_err_cause & EDMA_ERR_SELF_DIS) {
2417 ata_port_printk(ap, KERN_WARNING,
2418 "%s: err_cause=0x%x pp_flags=0x%x\n",
2419 __func__, edma_err_cause, pp->pp_flags);
2420 return 0; /* not handled */
2421 }
2422 return mv_handle_fbs_ncq_dev_err(ap);
2423 } else {
2424 /*
2425 * EDMA should have self-disabled for this case.
2426 * If it did not, then something is wrong elsewhere,
2427 * and we cannot handle it here.
2428 */
2429 if (!(edma_err_cause & EDMA_ERR_SELF_DIS)) {
2430 ata_port_printk(ap, KERN_WARNING,
2431 "%s: err_cause=0x%x pp_flags=0x%x\n",
2432 __func__, edma_err_cause, pp->pp_flags);
2433 return 0; /* not handled */
2434 }
2435 return mv_handle_fbs_non_ncq_dev_err(ap);
2436 }
2437 return 0; /* not handled */
2438}
2439
a9010329 2440static void mv_unexpected_intr(struct ata_port *ap, int edma_was_enabled)
8f767f8a 2441{
8f767f8a 2442 struct ata_eh_info *ehi = &ap->link.eh_info;
a9010329 2443 char *when = "idle";
8f767f8a 2444
8f767f8a 2445 ata_ehi_clear_desc(ehi);
a9010329
ML
2446 if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
2447 when = "disabled";
2448 } else if (edma_was_enabled) {
2449 when = "EDMA enabled";
8f767f8a
ML
2450 } else {
2451 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
2452 if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
a9010329 2453 when = "polling";
8f767f8a 2454 }
a9010329 2455 ata_ehi_push_desc(ehi, "unexpected device interrupt while %s", when);
8f767f8a
ML
2456 ehi->err_mask |= AC_ERR_OTHER;
2457 ehi->action |= ATA_EH_RESET;
2458 ata_port_freeze(ap);
2459}
2460
05b308e1
BR
2461/**
2462 * mv_err_intr - Handle error interrupts on the port
2463 * @ap: ATA channel to manipulate
2464 *
8d07379d
ML
2465 * Most cases require a full reset of the chip's state machine,
2466 * which also performs a COMRESET.
2467 * Also, if the port disabled DMA, update our cached copy to match.
05b308e1
BR
2468 *
2469 * LOCKING:
2470 * Inherited from caller.
2471 */
37b9046a 2472static void mv_err_intr(struct ata_port *ap)
31961943
BR
2473{
2474 void __iomem *port_mmio = mv_ap_base(ap);
bdd4ddde 2475 u32 edma_err_cause, eh_freeze_mask, serr = 0;
e4006077 2476 u32 fis_cause = 0;
bdd4ddde
JG
2477 struct mv_port_priv *pp = ap->private_data;
2478 struct mv_host_priv *hpriv = ap->host->private_data;
bdd4ddde 2479 unsigned int action = 0, err_mask = 0;
9af5c9c9 2480 struct ata_eh_info *ehi = &ap->link.eh_info;
37b9046a
ML
2481 struct ata_queued_cmd *qc;
2482 int abort = 0;
20f733e7 2483
8d07379d 2484 /*
37b9046a 2485 * Read and clear the SError and err_cause bits.
e4006077
ML
2486 * For GenIIe, if EDMA_ERR_TRANS_IRQ_7 is set, we also must read/clear
2487 * the FIS_IRQ_CAUSE register before clearing edma_err_cause.
8d07379d 2488 */
37b9046a
ML
2489 sata_scr_read(&ap->link, SCR_ERROR, &serr);
2490 sata_scr_write_flush(&ap->link, SCR_ERROR, serr);
2491
bdd4ddde 2492 edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
e4006077
ML
2493 if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
2494 fis_cause = readl(port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
2495 writelfl(~fis_cause, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
2496 }
8d07379d 2497 writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
bdd4ddde 2498
4c299ca3
ML
2499 if (edma_err_cause & EDMA_ERR_DEV) {
2500 /*
2501 * Device errors during FIS-based switching operation
2502 * require special handling.
2503 */
2504 if (mv_handle_dev_err(ap, edma_err_cause))
2505 return;
2506 }
2507
37b9046a
ML
2508 qc = mv_get_active_qc(ap);
2509 ata_ehi_clear_desc(ehi);
2510 ata_ehi_push_desc(ehi, "edma_err_cause=%08x pp_flags=%08x",
2511 edma_err_cause, pp->pp_flags);
e4006077 2512
c443c500 2513 if (IS_GEN_IIE(hpriv) && (edma_err_cause & EDMA_ERR_TRANS_IRQ_7)) {
e4006077 2514 ata_ehi_push_desc(ehi, "fis_cause=%08x", fis_cause);
c443c500
ML
2515 if (fis_cause & SATA_FIS_IRQ_AN) {
2516 u32 ec = edma_err_cause &
2517 ~(EDMA_ERR_TRANS_IRQ_7 | EDMA_ERR_IRQ_TRANSIENT);
2518 sata_async_notification(ap);
2519 if (!ec)
2520 return; /* Just an AN; no need for the nukes */
2521 ata_ehi_push_desc(ehi, "SDB notify");
2522 }
2523 }
bdd4ddde 2524 /*
352fab70 2525 * All generations share these EDMA error cause bits:
bdd4ddde 2526 */
37b9046a 2527 if (edma_err_cause & EDMA_ERR_DEV) {
bdd4ddde 2528 err_mask |= AC_ERR_DEV;
37b9046a
ML
2529 action |= ATA_EH_RESET;
2530 ata_ehi_push_desc(ehi, "dev error");
2531 }
bdd4ddde 2532 if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
6c1153e0 2533 EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
bdd4ddde
JG
2534 EDMA_ERR_INTRL_PAR)) {
2535 err_mask |= AC_ERR_ATA_BUS;
cf480626 2536 action |= ATA_EH_RESET;
b64bbc39 2537 ata_ehi_push_desc(ehi, "parity error");
bdd4ddde
JG
2538 }
2539 if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
2540 ata_ehi_hotplugged(ehi);
2541 ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
b64bbc39 2542 "dev disconnect" : "dev connect");
cf480626 2543 action |= ATA_EH_RESET;
bdd4ddde
JG
2544 }
2545
352fab70
ML
2546 /*
2547 * Gen-I has a different SELF_DIS bit,
2548 * different FREEZE bits, and no SERR bit:
2549 */
ee9ccdf7 2550 if (IS_GEN_I(hpriv)) {
bdd4ddde 2551 eh_freeze_mask = EDMA_EH_FREEZE_5;
bdd4ddde 2552 if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
bdd4ddde 2553 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
b64bbc39 2554 ata_ehi_push_desc(ehi, "EDMA self-disable");
bdd4ddde
JG
2555 }
2556 } else {
2557 eh_freeze_mask = EDMA_EH_FREEZE;
bdd4ddde 2558 if (edma_err_cause & EDMA_ERR_SELF_DIS) {
bdd4ddde 2559 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
b64bbc39 2560 ata_ehi_push_desc(ehi, "EDMA self-disable");
bdd4ddde 2561 }
bdd4ddde 2562 if (edma_err_cause & EDMA_ERR_SERR) {
8d07379d
ML
2563 ata_ehi_push_desc(ehi, "SError=%08x", serr);
2564 err_mask |= AC_ERR_ATA_BUS;
cf480626 2565 action |= ATA_EH_RESET;
bdd4ddde 2566 }
afb0edd9 2567 }
20f733e7 2568
bdd4ddde
JG
2569 if (!err_mask) {
2570 err_mask = AC_ERR_OTHER;
cf480626 2571 action |= ATA_EH_RESET;
bdd4ddde
JG
2572 }
2573
2574 ehi->serror |= serr;
2575 ehi->action |= action;
2576
2577 if (qc)
2578 qc->err_mask |= err_mask;
2579 else
2580 ehi->err_mask |= err_mask;
2581
37b9046a
ML
2582 if (err_mask == AC_ERR_DEV) {
2583 /*
2584 * Cannot do ata_port_freeze() here,
2585 * because it would kill PIO access,
2586 * which is needed for further diagnosis.
2587 */
2588 mv_eh_freeze(ap);
2589 abort = 1;
2590 } else if (edma_err_cause & eh_freeze_mask) {
2591 /*
2592 * Note to self: ata_port_freeze() calls ata_port_abort()
2593 */
bdd4ddde 2594 ata_port_freeze(ap);
37b9046a
ML
2595 } else {
2596 abort = 1;
2597 }
2598
2599 if (abort) {
2600 if (qc)
2601 ata_link_abort(qc->dev->link);
2602 else
2603 ata_port_abort(ap);
2604 }
bdd4ddde
JG
2605}
2606
fcfb1f77
ML
2607static void mv_process_crpb_response(struct ata_port *ap,
2608 struct mv_crpb *response, unsigned int tag, int ncq_enabled)
2609{
2610 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
2611
2612 if (qc) {
2613 u8 ata_status;
2614 u16 edma_status = le16_to_cpu(response->flags);
2615 /*
2616 * edma_status from a response queue entry:
2617 * LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
2618 * MSB is saved ATA status from command completion.
2619 */
2620 if (!ncq_enabled) {
2621 u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
2622 if (err_cause) {
2623 /*
2624 * Error will be seen/handled by mv_err_intr().
2625 * So do nothing at all here.
2626 */
2627 return;
2628 }
2629 }
2630 ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
37b9046a
ML
2631 if (!ac_err_mask(ata_status))
2632 ata_qc_complete(qc);
2633 /* else: leave it for mv_err_intr() */
fcfb1f77
ML
2634 } else {
2635 ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
2636 __func__, tag);
2637 }
2638}
2639
2640static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
bdd4ddde
JG
2641{
2642 void __iomem *port_mmio = mv_ap_base(ap);
2643 struct mv_host_priv *hpriv = ap->host->private_data;
fcfb1f77 2644 u32 in_index;
bdd4ddde 2645 bool work_done = false;
fcfb1f77 2646 int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
bdd4ddde 2647
fcfb1f77 2648 /* Get the hardware queue position index */
bdd4ddde
JG
2649 in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
2650 >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
2651
fcfb1f77
ML
2652 /* Process new responses from since the last time we looked */
2653 while (in_index != pp->resp_idx) {
6c1153e0 2654 unsigned int tag;
fcfb1f77 2655 struct mv_crpb *response = &pp->crpb[pp->resp_idx];
bdd4ddde 2656
fcfb1f77 2657 pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
bdd4ddde 2658
fcfb1f77
ML
2659 if (IS_GEN_I(hpriv)) {
2660 /* 50xx: no NCQ, only one command active at a time */
9af5c9c9 2661 tag = ap->link.active_tag;
fcfb1f77
ML
2662 } else {
2663 /* Gen II/IIE: get command tag from CRPB entry */
2664 tag = le16_to_cpu(response->id) & 0x1f;
bdd4ddde 2665 }
fcfb1f77 2666 mv_process_crpb_response(ap, response, tag, ncq_enabled);
bdd4ddde 2667 work_done = true;
bdd4ddde
JG
2668 }
2669
352fab70 2670 /* Update the software queue position index in hardware */
bdd4ddde
JG
2671 if (work_done)
2672 writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
fcfb1f77 2673 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
bdd4ddde 2674 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
20f733e7
BR
2675}
2676
a9010329
ML
2677static void mv_port_intr(struct ata_port *ap, u32 port_cause)
2678{
2679 struct mv_port_priv *pp;
2680 int edma_was_enabled;
2681
2682 if (!ap || (ap->flags & ATA_FLAG_DISABLED)) {
2683 mv_unexpected_intr(ap, 0);
2684 return;
2685 }
2686 /*
2687 * Grab a snapshot of the EDMA_EN flag setting,
2688 * so that we have a consistent view for this port,
2689 * even if something we call of our routines changes it.
2690 */
2691 pp = ap->private_data;
2692 edma_was_enabled = (pp->pp_flags & MV_PP_FLAG_EDMA_EN);
2693 /*
2694 * Process completed CRPB response(s) before other events.
2695 */
2696 if (edma_was_enabled && (port_cause & DONE_IRQ)) {
2697 mv_process_crpb_entries(ap, pp);
4c299ca3
ML
2698 if (pp->pp_flags & MV_PP_FLAG_DELAYED_EH)
2699 mv_handle_fbs_ncq_dev_err(ap);
a9010329
ML
2700 }
2701 /*
2702 * Handle chip-reported errors, or continue on to handle PIO.
2703 */
2704 if (unlikely(port_cause & ERR_IRQ)) {
2705 mv_err_intr(ap);
2706 } else if (!edma_was_enabled) {
2707 struct ata_queued_cmd *qc = mv_get_active_qc(ap);
2708 if (qc)
2709 ata_sff_host_intr(ap, qc);
2710 else
2711 mv_unexpected_intr(ap, edma_was_enabled);
2712 }
2713}
2714
05b308e1
BR
2715/**
2716 * mv_host_intr - Handle all interrupts on the given host controller
cca3974e 2717 * @host: host specific structure
7368f919 2718 * @main_irq_cause: Main interrupt cause register for the chip.
05b308e1
BR
2719 *
2720 * LOCKING:
2721 * Inherited from caller.
2722 */
7368f919 2723static int mv_host_intr(struct ata_host *host, u32 main_irq_cause)
20f733e7 2724{
f351b2d6 2725 struct mv_host_priv *hpriv = host->private_data;
eabd5eb1 2726 void __iomem *mmio = hpriv->base, *hc_mmio;
a3718c1f 2727 unsigned int handled = 0, port;
20f733e7 2728
2b748a0a
ML
2729 /* If asserted, clear the "all ports" IRQ coalescing bit */
2730 if (main_irq_cause & ALL_PORTS_COAL_DONE)
2731 writel(~ALL_PORTS_COAL_IRQ, mmio + MV_IRQ_COAL_CAUSE);
2732
a3718c1f 2733 for (port = 0; port < hpriv->n_ports; port++) {
cca3974e 2734 struct ata_port *ap = host->ports[port];
eabd5eb1
ML
2735 unsigned int p, shift, hardport, port_cause;
2736
a3718c1f 2737 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
a3718c1f 2738 /*
eabd5eb1
ML
2739 * Each hc within the host has its own hc_irq_cause register,
2740 * where the interrupting ports bits get ack'd.
a3718c1f 2741 */
eabd5eb1
ML
2742 if (hardport == 0) { /* first port on this hc ? */
2743 u32 hc_cause = (main_irq_cause >> shift) & HC0_IRQ_PEND;
2744 u32 port_mask, ack_irqs;
2745 /*
2746 * Skip this entire hc if nothing pending for any ports
2747 */
2748 if (!hc_cause) {
2749 port += MV_PORTS_PER_HC - 1;
2750 continue;
2751 }
2752 /*
2753 * We don't need/want to read the hc_irq_cause register,
2754 * because doing so hurts performance, and
2755 * main_irq_cause already gives us everything we need.
2756 *
2757 * But we do have to *write* to the hc_irq_cause to ack
2758 * the ports that we are handling this time through.
2759 *
2760 * This requires that we create a bitmap for those
2761 * ports which interrupted us, and use that bitmap
2762 * to ack (only) those ports via hc_irq_cause.
2763 */
2764 ack_irqs = 0;
2b748a0a
ML
2765 if (hc_cause & PORTS_0_3_COAL_DONE)
2766 ack_irqs = HC_COAL_IRQ;
eabd5eb1
ML
2767 for (p = 0; p < MV_PORTS_PER_HC; ++p) {
2768 if ((port + p) >= hpriv->n_ports)
2769 break;
2770 port_mask = (DONE_IRQ | ERR_IRQ) << (p * 2);
2771 if (hc_cause & port_mask)
2772 ack_irqs |= (DMA_IRQ | DEV_IRQ) << p;
2773 }
a3718c1f 2774 hc_mmio = mv_hc_base_from_port(mmio, port);
eabd5eb1 2775 writelfl(~ack_irqs, hc_mmio + HC_IRQ_CAUSE_OFS);
a3718c1f
ML
2776 handled = 1;
2777 }
8f767f8a 2778 /*
a9010329 2779 * Handle interrupts signalled for this port:
8f767f8a 2780 */
a9010329
ML
2781 port_cause = (main_irq_cause >> shift) & (DONE_IRQ | ERR_IRQ);
2782 if (port_cause)
2783 mv_port_intr(ap, port_cause);
20f733e7 2784 }
a3718c1f 2785 return handled;
20f733e7
BR
2786}
2787
a3718c1f 2788static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
bdd4ddde 2789{
02a121da 2790 struct mv_host_priv *hpriv = host->private_data;
bdd4ddde
JG
2791 struct ata_port *ap;
2792 struct ata_queued_cmd *qc;
2793 struct ata_eh_info *ehi;
2794 unsigned int i, err_mask, printed = 0;
2795 u32 err_cause;
2796
02a121da 2797 err_cause = readl(mmio + hpriv->irq_cause_ofs);
bdd4ddde
JG
2798
2799 dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
2800 err_cause);
2801
2802 DPRINTK("All regs @ PCI error\n");
2803 mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
2804
02a121da 2805 writelfl(0, mmio + hpriv->irq_cause_ofs);
bdd4ddde
JG
2806
2807 for (i = 0; i < host->n_ports; i++) {
2808 ap = host->ports[i];
936fd732 2809 if (!ata_link_offline(&ap->link)) {
9af5c9c9 2810 ehi = &ap->link.eh_info;
bdd4ddde
JG
2811 ata_ehi_clear_desc(ehi);
2812 if (!printed++)
2813 ata_ehi_push_desc(ehi,
2814 "PCI err cause 0x%08x", err_cause);
2815 err_mask = AC_ERR_HOST_BUS;
cf480626 2816 ehi->action = ATA_EH_RESET;
9af5c9c9 2817 qc = ata_qc_from_tag(ap, ap->link.active_tag);
bdd4ddde
JG
2818 if (qc)
2819 qc->err_mask |= err_mask;
2820 else
2821 ehi->err_mask |= err_mask;
2822
2823 ata_port_freeze(ap);
2824 }
2825 }
a3718c1f 2826 return 1; /* handled */
bdd4ddde
JG
2827}
2828
05b308e1 2829/**
c5d3e45a 2830 * mv_interrupt - Main interrupt event handler
05b308e1
BR
2831 * @irq: unused
2832 * @dev_instance: private data; in this case the host structure
05b308e1
BR
2833 *
2834 * Read the read only register to determine if any host
2835 * controllers have pending interrupts. If so, call lower level
2836 * routine to handle. Also check for PCI errors which are only
2837 * reported here.
2838 *
8b260248 2839 * LOCKING:
cca3974e 2840 * This routine holds the host lock while processing pending
05b308e1
BR
2841 * interrupts.
2842 */
7d12e780 2843static irqreturn_t mv_interrupt(int irq, void *dev_instance)
20f733e7 2844{
cca3974e 2845 struct ata_host *host = dev_instance;
f351b2d6 2846 struct mv_host_priv *hpriv = host->private_data;
a3718c1f 2847 unsigned int handled = 0;
6d3c30ef 2848 int using_msi = hpriv->hp_flags & MV_HP_FLAG_MSI;
96e2c487 2849 u32 main_irq_cause, pending_irqs;
20f733e7 2850
646a4da5 2851 spin_lock(&host->lock);
6d3c30ef
ML
2852
2853 /* for MSI: block new interrupts while in here */
2854 if (using_msi)
2b748a0a 2855 mv_write_main_irq_mask(0, hpriv);
6d3c30ef 2856
7368f919 2857 main_irq_cause = readl(hpriv->main_irq_cause_addr);
96e2c487 2858 pending_irqs = main_irq_cause & hpriv->main_irq_mask;
352fab70
ML
2859 /*
2860 * Deal with cases where we either have nothing pending, or have read
2861 * a bogus register value which can indicate HW removal or PCI fault.
20f733e7 2862 */
a44253d2 2863 if (pending_irqs && main_irq_cause != 0xffffffffU) {
1f398472 2864 if (unlikely((pending_irqs & PCI_ERR) && !IS_SOC(hpriv)))
a3718c1f
ML
2865 handled = mv_pci_error(host, hpriv->base);
2866 else
a44253d2 2867 handled = mv_host_intr(host, pending_irqs);
bdd4ddde 2868 }
6d3c30ef
ML
2869
2870 /* for MSI: unmask; interrupt cause bits will retrigger now */
2871 if (using_msi)
2b748a0a 2872 mv_write_main_irq_mask(hpriv->main_irq_mask, hpriv);
6d3c30ef 2873
9d51af7b
ML
2874 spin_unlock(&host->lock);
2875
20f733e7
BR
2876 return IRQ_RETVAL(handled);
2877}
2878
c9d39130
JG
2879static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
2880{
2881 unsigned int ofs;
2882
2883 switch (sc_reg_in) {
2884 case SCR_STATUS:
2885 case SCR_ERROR:
2886 case SCR_CONTROL:
2887 ofs = sc_reg_in * sizeof(u32);
2888 break;
2889 default:
2890 ofs = 0xffffffffU;
2891 break;
2892 }
2893 return ofs;
2894}
2895
82ef04fb 2896static int mv5_scr_read(struct ata_link *link, unsigned int sc_reg_in, u32 *val)
c9d39130 2897{
82ef04fb 2898 struct mv_host_priv *hpriv = link->ap->host->private_data;
f351b2d6 2899 void __iomem *mmio = hpriv->base;
82ef04fb 2900 void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
c9d39130
JG
2901 unsigned int ofs = mv5_scr_offset(sc_reg_in);
2902
da3dbb17
TH
2903 if (ofs != 0xffffffffU) {
2904 *val = readl(addr + ofs);
2905 return 0;
2906 } else
2907 return -EINVAL;
c9d39130
JG
2908}
2909
82ef04fb 2910static int mv5_scr_write(struct ata_link *link, unsigned int sc_reg_in, u32 val)
c9d39130 2911{
82ef04fb 2912 struct mv_host_priv *hpriv = link->ap->host->private_data;
f351b2d6 2913 void __iomem *mmio = hpriv->base;
82ef04fb 2914 void __iomem *addr = mv5_phy_base(mmio, link->ap->port_no);
c9d39130
JG
2915 unsigned int ofs = mv5_scr_offset(sc_reg_in);
2916
da3dbb17 2917 if (ofs != 0xffffffffU) {
0d5ff566 2918 writelfl(val, addr + ofs);
da3dbb17
TH
2919 return 0;
2920 } else
2921 return -EINVAL;
c9d39130
JG
2922}
2923
7bb3c529 2924static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
522479fb 2925{
7bb3c529 2926 struct pci_dev *pdev = to_pci_dev(host->dev);
522479fb
JG
2927 int early_5080;
2928
44c10138 2929 early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
522479fb
JG
2930
2931 if (!early_5080) {
2932 u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
2933 tmp |= (1 << 0);
2934 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
2935 }
2936
7bb3c529 2937 mv_reset_pci_bus(host, mmio);
522479fb
JG
2938}
2939
2940static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2941{
8e7decdb 2942 writel(0x0fcfffff, mmio + MV_FLASH_CTL_OFS);
522479fb
JG
2943}
2944
47c2b677 2945static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
ba3fe8fb
JG
2946 void __iomem *mmio)
2947{
c9d39130
JG
2948 void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
2949 u32 tmp;
2950
2951 tmp = readl(phy_mmio + MV5_PHY_MODE);
2952
2953 hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
2954 hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
ba3fe8fb
JG
2955}
2956
47c2b677 2957static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
ba3fe8fb 2958{
522479fb
JG
2959 u32 tmp;
2960
8e7decdb 2961 writel(0, mmio + MV_GPIO_PORT_CTL_OFS);
522479fb
JG
2962
2963 /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
2964
2965 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
2966 tmp |= ~(1 << 0);
2967 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
ba3fe8fb
JG
2968}
2969
2a47ce06
JG
2970static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2971 unsigned int port)
bca1c4eb 2972{
c9d39130
JG
2973 void __iomem *phy_mmio = mv5_phy_base(mmio, port);
2974 const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
2975 u32 tmp;
2976 int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
2977
2978 if (fix_apm_sq) {
8e7decdb 2979 tmp = readl(phy_mmio + MV5_LTMODE_OFS);
c9d39130 2980 tmp |= (1 << 19);
8e7decdb 2981 writel(tmp, phy_mmio + MV5_LTMODE_OFS);
c9d39130 2982
8e7decdb 2983 tmp = readl(phy_mmio + MV5_PHY_CTL_OFS);
c9d39130
JG
2984 tmp &= ~0x3;
2985 tmp |= 0x1;
8e7decdb 2986 writel(tmp, phy_mmio + MV5_PHY_CTL_OFS);
c9d39130
JG
2987 }
2988
2989 tmp = readl(phy_mmio + MV5_PHY_MODE);
2990 tmp &= ~mask;
2991 tmp |= hpriv->signal[port].pre;
2992 tmp |= hpriv->signal[port].amps;
2993 writel(tmp, phy_mmio + MV5_PHY_MODE);
bca1c4eb
JG
2994}
2995
c9d39130
JG
2996
2997#undef ZERO
2998#define ZERO(reg) writel(0, port_mmio + (reg))
2999static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
3000 unsigned int port)
3001{
3002 void __iomem *port_mmio = mv_port_base(mmio, port);
3003
e12bef50 3004 mv_reset_channel(hpriv, mmio, port);
c9d39130
JG
3005
3006 ZERO(0x028); /* command */
3007 writel(0x11f, port_mmio + EDMA_CFG_OFS);
3008 ZERO(0x004); /* timer */
3009 ZERO(0x008); /* irq err cause */
3010 ZERO(0x00c); /* irq err mask */
3011 ZERO(0x010); /* rq bah */
3012 ZERO(0x014); /* rq inp */
3013 ZERO(0x018); /* rq outp */
3014 ZERO(0x01c); /* respq bah */
3015 ZERO(0x024); /* respq outp */
3016 ZERO(0x020); /* respq inp */
3017 ZERO(0x02c); /* test control */
8e7decdb 3018 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
c9d39130
JG
3019}
3020#undef ZERO
3021
3022#define ZERO(reg) writel(0, hc_mmio + (reg))
3023static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3024 unsigned int hc)
47c2b677 3025{
c9d39130
JG
3026 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3027 u32 tmp;
3028
3029 ZERO(0x00c);
3030 ZERO(0x010);
3031 ZERO(0x014);
3032 ZERO(0x018);
3033
3034 tmp = readl(hc_mmio + 0x20);
3035 tmp &= 0x1c1c1c1c;
3036 tmp |= 0x03030303;
3037 writel(tmp, hc_mmio + 0x20);
3038}
3039#undef ZERO
3040
3041static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3042 unsigned int n_hc)
3043{
3044 unsigned int hc, port;
3045
3046 for (hc = 0; hc < n_hc; hc++) {
3047 for (port = 0; port < MV_PORTS_PER_HC; port++)
3048 mv5_reset_hc_port(hpriv, mmio,
3049 (hc * MV_PORTS_PER_HC) + port);
3050
3051 mv5_reset_one_hc(hpriv, mmio, hc);
3052 }
3053
3054 return 0;
47c2b677
JG
3055}
3056
101ffae2
JG
3057#undef ZERO
3058#define ZERO(reg) writel(0, mmio + (reg))
7bb3c529 3059static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
101ffae2 3060{
02a121da 3061 struct mv_host_priv *hpriv = host->private_data;
101ffae2
JG
3062 u32 tmp;
3063
8e7decdb 3064 tmp = readl(mmio + MV_PCI_MODE_OFS);
101ffae2 3065 tmp &= 0xff00ffff;
8e7decdb 3066 writel(tmp, mmio + MV_PCI_MODE_OFS);
101ffae2
JG
3067
3068 ZERO(MV_PCI_DISC_TIMER);
3069 ZERO(MV_PCI_MSI_TRIGGER);
8e7decdb 3070 writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT_OFS);
101ffae2 3071 ZERO(MV_PCI_SERR_MASK);
02a121da
ML
3072 ZERO(hpriv->irq_cause_ofs);
3073 ZERO(hpriv->irq_mask_ofs);
101ffae2
JG
3074 ZERO(MV_PCI_ERR_LOW_ADDRESS);
3075 ZERO(MV_PCI_ERR_HIGH_ADDRESS);
3076 ZERO(MV_PCI_ERR_ATTRIBUTE);
3077 ZERO(MV_PCI_ERR_COMMAND);
3078}
3079#undef ZERO
3080
3081static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
3082{
3083 u32 tmp;
3084
3085 mv5_reset_flash(hpriv, mmio);
3086
8e7decdb 3087 tmp = readl(mmio + MV_GPIO_PORT_CTL_OFS);
101ffae2
JG
3088 tmp &= 0x3;
3089 tmp |= (1 << 5) | (1 << 6);
8e7decdb 3090 writel(tmp, mmio + MV_GPIO_PORT_CTL_OFS);
101ffae2
JG
3091}
3092
3093/**
3094 * mv6_reset_hc - Perform the 6xxx global soft reset
3095 * @mmio: base address of the HBA
3096 *
3097 * This routine only applies to 6xxx parts.
3098 *
3099 * LOCKING:
3100 * Inherited from caller.
3101 */
c9d39130
JG
3102static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
3103 unsigned int n_hc)
101ffae2
JG
3104{
3105 void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
3106 int i, rc = 0;
3107 u32 t;
3108
3109 /* Following procedure defined in PCI "main command and status
3110 * register" table.
3111 */
3112 t = readl(reg);
3113 writel(t | STOP_PCI_MASTER, reg);
3114
3115 for (i = 0; i < 1000; i++) {
3116 udelay(1);
3117 t = readl(reg);
2dcb407e 3118 if (PCI_MASTER_EMPTY & t)
101ffae2 3119 break;
101ffae2
JG
3120 }
3121 if (!(PCI_MASTER_EMPTY & t)) {
3122 printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
3123 rc = 1;
3124 goto done;
3125 }
3126
3127 /* set reset */
3128 i = 5;
3129 do {
3130 writel(t | GLOB_SFT_RST, reg);
3131 t = readl(reg);
3132 udelay(1);
3133 } while (!(GLOB_SFT_RST & t) && (i-- > 0));
3134
3135 if (!(GLOB_SFT_RST & t)) {
3136 printk(KERN_ERR DRV_NAME ": can't set global reset\n");
3137 rc = 1;
3138 goto done;
3139 }
3140
3141 /* clear reset and *reenable the PCI master* (not mentioned in spec) */
3142 i = 5;
3143 do {
3144 writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
3145 t = readl(reg);
3146 udelay(1);
3147 } while ((GLOB_SFT_RST & t) && (i-- > 0));
3148
3149 if (GLOB_SFT_RST & t) {
3150 printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
3151 rc = 1;
3152 }
3153done:
3154 return rc;
3155}
3156
47c2b677 3157static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
ba3fe8fb
JG
3158 void __iomem *mmio)
3159{
3160 void __iomem *port_mmio;
3161 u32 tmp;
3162
8e7decdb 3163 tmp = readl(mmio + MV_RESET_CFG_OFS);
ba3fe8fb 3164 if ((tmp & (1 << 0)) == 0) {
47c2b677 3165 hpriv->signal[idx].amps = 0x7 << 8;
ba3fe8fb
JG
3166 hpriv->signal[idx].pre = 0x1 << 5;
3167 return;
3168 }
3169
3170 port_mmio = mv_port_base(mmio, idx);
ba68460b 3171 tmp = readl(port_mmio + PHY_MODE2_OFS);
ba3fe8fb
JG
3172
3173 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
3174 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
3175}
3176
47c2b677 3177static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
ba3fe8fb 3178{
8e7decdb 3179 writel(0x00000060, mmio + MV_GPIO_PORT_CTL_OFS);
ba3fe8fb
JG
3180}
3181
c9d39130 3182static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
2a47ce06 3183 unsigned int port)
bca1c4eb 3184{
c9d39130
JG
3185 void __iomem *port_mmio = mv_port_base(mmio, port);
3186
bca1c4eb 3187 u32 hp_flags = hpriv->hp_flags;
47c2b677
JG
3188 int fix_phy_mode2 =
3189 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
bca1c4eb 3190 int fix_phy_mode4 =
47c2b677 3191 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
8c30a8b9 3192 u32 m2, m3;
47c2b677
JG
3193
3194 if (fix_phy_mode2) {
ba68460b 3195 m2 = readl(port_mmio + PHY_MODE2_OFS);
47c2b677
JG
3196 m2 &= ~(1 << 16);
3197 m2 |= (1 << 31);
ba68460b 3198 writel(m2, port_mmio + PHY_MODE2_OFS);
47c2b677
JG
3199
3200 udelay(200);
3201
ba68460b 3202 m2 = readl(port_mmio + PHY_MODE2_OFS);
47c2b677 3203 m2 &= ~((1 << 16) | (1 << 31));
ba68460b 3204 writel(m2, port_mmio + PHY_MODE2_OFS);
47c2b677
JG
3205
3206 udelay(200);
3207 }
3208
8c30a8b9 3209 /*
ba68460b 3210 * Gen-II/IIe PHY_MODE3_OFS errata RM#2:
8c30a8b9
ML
3211 * Achieves better receiver noise performance than the h/w default:
3212 */
ba68460b 3213 m3 = readl(port_mmio + PHY_MODE3_OFS);
8c30a8b9 3214 m3 = (m3 & 0x1f) | (0x5555601 << 5);
bca1c4eb 3215
0388a8c0
ML
3216 /* Guideline 88F5182 (GL# SATA-S11) */
3217 if (IS_SOC(hpriv))
3218 m3 &= ~0x1c;
3219
bca1c4eb 3220 if (fix_phy_mode4) {
ba68460b 3221 u32 m4 = readl(port_mmio + PHY_MODE4_OFS);
ba069e37
ML
3222 /*
3223 * Enforce reserved-bit restrictions on GenIIe devices only.
3224 * For earlier chipsets, force only the internal config field
3225 * (workaround for errata FEr SATA#10 part 1).
3226 */
8c30a8b9 3227 if (IS_GEN_IIE(hpriv))
ba069e37
ML
3228 m4 = (m4 & ~PHY_MODE4_RSVD_ZEROS) | PHY_MODE4_RSVD_ONES;
3229 else
3230 m4 = (m4 & ~PHY_MODE4_CFG_MASK) | PHY_MODE4_CFG_VALUE;
ba68460b 3231 writel(m4, port_mmio + PHY_MODE4_OFS);
bca1c4eb 3232 }
b406c7a6
ML
3233 /*
3234 * Workaround for 60x1-B2 errata SATA#13:
3235 * Any write to PHY_MODE4 (above) may corrupt PHY_MODE3,
3236 * so we must always rewrite PHY_MODE3 after PHY_MODE4.
ba68460b 3237 * Or ensure we use writelfl() when writing PHY_MODE4.
b406c7a6 3238 */
ba68460b 3239 writel(m3, port_mmio + PHY_MODE3_OFS);
bca1c4eb
JG
3240
3241 /* Revert values of pre-emphasis and signal amps to the saved ones */
ba68460b 3242 m2 = readl(port_mmio + PHY_MODE2_OFS);
bca1c4eb
JG
3243
3244 m2 &= ~MV_M2_PREAMP_MASK;
2a47ce06
JG
3245 m2 |= hpriv->signal[port].amps;
3246 m2 |= hpriv->signal[port].pre;
47c2b677 3247 m2 &= ~(1 << 16);
bca1c4eb 3248
e4e7b892
JG
3249 /* according to mvSata 3.6.1, some IIE values are fixed */
3250 if (IS_GEN_IIE(hpriv)) {
3251 m2 &= ~0xC30FF01F;
3252 m2 |= 0x0000900F;
3253 }
3254
ba68460b 3255 writel(m2, port_mmio + PHY_MODE2_OFS);
bca1c4eb
JG
3256}
3257
f351b2d6
SB
3258/* TODO: use the generic LED interface to configure the SATA Presence */
3259/* & Acitivy LEDs on the board */
3260static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
3261 void __iomem *mmio)
3262{
3263 return;
3264}
3265
3266static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
3267 void __iomem *mmio)
3268{
3269 void __iomem *port_mmio;
3270 u32 tmp;
3271
3272 port_mmio = mv_port_base(mmio, idx);
ba68460b 3273 tmp = readl(port_mmio + PHY_MODE2_OFS);
f351b2d6
SB
3274
3275 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
3276 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
3277}
3278
3279#undef ZERO
3280#define ZERO(reg) writel(0, port_mmio + (reg))
3281static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
3282 void __iomem *mmio, unsigned int port)
3283{
3284 void __iomem *port_mmio = mv_port_base(mmio, port);
3285
e12bef50 3286 mv_reset_channel(hpriv, mmio, port);
f351b2d6
SB
3287
3288 ZERO(0x028); /* command */
3289 writel(0x101f, port_mmio + EDMA_CFG_OFS);
3290 ZERO(0x004); /* timer */
3291 ZERO(0x008); /* irq err cause */
3292 ZERO(0x00c); /* irq err mask */
3293 ZERO(0x010); /* rq bah */
3294 ZERO(0x014); /* rq inp */
3295 ZERO(0x018); /* rq outp */
3296 ZERO(0x01c); /* respq bah */
3297 ZERO(0x024); /* respq outp */
3298 ZERO(0x020); /* respq inp */
3299 ZERO(0x02c); /* test control */
8e7decdb 3300 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT_OFS);
f351b2d6
SB
3301}
3302
3303#undef ZERO
3304
3305#define ZERO(reg) writel(0, hc_mmio + (reg))
3306static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
3307 void __iomem *mmio)
3308{
3309 void __iomem *hc_mmio = mv_hc_base(mmio, 0);
3310
3311 ZERO(0x00c);
3312 ZERO(0x010);
3313 ZERO(0x014);
3314
3315}
3316
3317#undef ZERO
3318
3319static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
3320 void __iomem *mmio, unsigned int n_hc)
3321{
3322 unsigned int port;
3323
3324 for (port = 0; port < hpriv->n_ports; port++)
3325 mv_soc_reset_hc_port(hpriv, mmio, port);
3326
3327 mv_soc_reset_one_hc(hpriv, mmio);
3328
3329 return 0;
3330}
3331
3332static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
3333 void __iomem *mmio)
3334{
3335 return;
3336}
3337
3338static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
3339{
3340 return;
3341}
3342
8e7decdb 3343static void mv_setup_ifcfg(void __iomem *port_mmio, int want_gen2i)
b67a1064 3344{
8e7decdb 3345 u32 ifcfg = readl(port_mmio + SATA_INTERFACE_CFG_OFS);
b67a1064 3346
8e7decdb 3347 ifcfg = (ifcfg & 0xf7f) | 0x9b1000; /* from chip spec */
b67a1064 3348 if (want_gen2i)
8e7decdb
ML
3349 ifcfg |= (1 << 7); /* enable gen2i speed */
3350 writelfl(ifcfg, port_mmio + SATA_INTERFACE_CFG_OFS);
b67a1064
ML
3351}
3352
e12bef50 3353static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
c9d39130
JG
3354 unsigned int port_no)
3355{
3356 void __iomem *port_mmio = mv_port_base(mmio, port_no);
3357
8e7decdb
ML
3358 /*
3359 * The datasheet warns against setting EDMA_RESET when EDMA is active
3360 * (but doesn't say what the problem might be). So we first try
3361 * to disable the EDMA engine before doing the EDMA_RESET operation.
3362 */
0d8be5cb 3363 mv_stop_edma_engine(port_mmio);
8e7decdb 3364 writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
c9d39130 3365
b67a1064 3366 if (!IS_GEN_I(hpriv)) {
8e7decdb
ML
3367 /* Enable 3.0gb/s link speed: this survives EDMA_RESET */
3368 mv_setup_ifcfg(port_mmio, 1);
c9d39130 3369 }
b67a1064 3370 /*
8e7decdb 3371 * Strobing EDMA_RESET here causes a hard reset of the SATA transport,
b67a1064
ML
3372 * link, and physical layers. It resets all SATA interface registers
3373 * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
c9d39130 3374 */
8e7decdb 3375 writelfl(EDMA_RESET, port_mmio + EDMA_CMD_OFS);
b67a1064 3376 udelay(25); /* allow reset propagation */
c9d39130
JG
3377 writelfl(0, port_mmio + EDMA_CMD_OFS);
3378
3379 hpriv->ops->phy_errata(hpriv, mmio, port_no);
3380
ee9ccdf7 3381 if (IS_GEN_I(hpriv))
c9d39130
JG
3382 mdelay(1);
3383}
3384
e49856d8 3385static void mv_pmp_select(struct ata_port *ap, int pmp)
20f733e7 3386{
e49856d8
ML
3387 if (sata_pmp_supported(ap)) {
3388 void __iomem *port_mmio = mv_ap_base(ap);
3389 u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
3390 int old = reg & 0xf;
22374677 3391
e49856d8
ML
3392 if (old != pmp) {
3393 reg = (reg & ~0xf) | pmp;
3394 writelfl(reg, port_mmio + SATA_IFCTL_OFS);
3395 }
22374677 3396 }
20f733e7
BR
3397}
3398
e49856d8
ML
3399static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
3400 unsigned long deadline)
22374677 3401{
e49856d8
ML
3402 mv_pmp_select(link->ap, sata_srst_pmp(link));
3403 return sata_std_hardreset(link, class, deadline);
3404}
bdd4ddde 3405
e49856d8
ML
3406static int mv_softreset(struct ata_link *link, unsigned int *class,
3407 unsigned long deadline)
3408{
3409 mv_pmp_select(link->ap, sata_srst_pmp(link));
3410 return ata_sff_softreset(link, class, deadline);
22374677
JG
3411}
3412
cc0680a5 3413static int mv_hardreset(struct ata_link *link, unsigned int *class,
bdd4ddde 3414 unsigned long deadline)
31961943 3415{
cc0680a5 3416 struct ata_port *ap = link->ap;
bdd4ddde 3417 struct mv_host_priv *hpriv = ap->host->private_data;
b562468c 3418 struct mv_port_priv *pp = ap->private_data;
f351b2d6 3419 void __iomem *mmio = hpriv->base;
0d8be5cb
ML
3420 int rc, attempts = 0, extra = 0;
3421 u32 sstatus;
3422 bool online;
31961943 3423
e12bef50 3424 mv_reset_channel(hpriv, mmio, ap->port_no);
b562468c 3425 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
d16ab3f6
ML
3426 pp->pp_flags &=
3427 ~(MV_PP_FLAG_FBS_EN | MV_PP_FLAG_NCQ_EN | MV_PP_FLAG_FAKE_ATA_BUSY);
bdd4ddde 3428
0d8be5cb
ML
3429 /* Workaround for errata FEr SATA#10 (part 2) */
3430 do {
17c5aab5
ML
3431 const unsigned long *timing =
3432 sata_ehc_deb_timing(&link->eh_context);
bdd4ddde 3433
17c5aab5
ML
3434 rc = sata_link_hardreset(link, timing, deadline + extra,
3435 &online, NULL);
9dcffd99 3436 rc = online ? -EAGAIN : rc;
17c5aab5 3437 if (rc)
0d8be5cb 3438 return rc;
0d8be5cb
ML
3439 sata_scr_read(link, SCR_STATUS, &sstatus);
3440 if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
3441 /* Force 1.5gb/s link speed and try again */
8e7decdb 3442 mv_setup_ifcfg(mv_ap_base(ap), 0);
0d8be5cb
ML
3443 if (time_after(jiffies + HZ, deadline))
3444 extra = HZ; /* only extend it once, max */
3445 }
3446 } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
08da1759 3447 mv_save_cached_regs(ap);
66e57a2c 3448 mv_edma_cfg(ap, 0, 0);
bdd4ddde 3449
17c5aab5 3450 return rc;
bdd4ddde
JG
3451}
3452
bdd4ddde
JG
3453static void mv_eh_freeze(struct ata_port *ap)
3454{
1cfd19ae 3455 mv_stop_edma(ap);
c4de573b 3456 mv_enable_port_irqs(ap, 0);
bdd4ddde
JG
3457}
3458
3459static void mv_eh_thaw(struct ata_port *ap)
3460{
f351b2d6 3461 struct mv_host_priv *hpriv = ap->host->private_data;
c4de573b
ML
3462 unsigned int port = ap->port_no;
3463 unsigned int hardport = mv_hardport_from_port(port);
1cfd19ae 3464 void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
bdd4ddde 3465 void __iomem *port_mmio = mv_ap_base(ap);
c4de573b 3466 u32 hc_irq_cause;
bdd4ddde 3467
bdd4ddde
JG
3468 /* clear EDMA errors on this port */
3469 writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
3470
3471 /* clear pending irq events */
cae6edc3 3472 hc_irq_cause = ~((DEV_IRQ | DMA_IRQ) << hardport);
1cfd19ae 3473 writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
bdd4ddde 3474
88e675e1 3475 mv_enable_port_irqs(ap, ERR_IRQ);
31961943
BR
3476}
3477
05b308e1
BR
3478/**
3479 * mv_port_init - Perform some early initialization on a single port.
3480 * @port: libata data structure storing shadow register addresses
3481 * @port_mmio: base address of the port
3482 *
3483 * Initialize shadow register mmio addresses, clear outstanding
3484 * interrupts on the port, and unmask interrupts for the future
3485 * start of the port.
3486 *
3487 * LOCKING:
3488 * Inherited from caller.
3489 */
31961943 3490static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
20f733e7 3491{
0d5ff566 3492 void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
31961943
BR
3493 unsigned serr_ofs;
3494
8b260248 3495 /* PIO related setup
31961943
BR
3496 */
3497 port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
8b260248 3498 port->error_addr =
31961943
BR
3499 port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
3500 port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
3501 port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
3502 port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
3503 port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
3504 port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
8b260248 3505 port->status_addr =
31961943
BR
3506 port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
3507 /* special case: control/altstatus doesn't have ATA_REG_ address */
3508 port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
3509
3510 /* unused: */
8d9db2d2 3511 port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
20f733e7 3512
31961943
BR
3513 /* Clear any currently outstanding port interrupt conditions */
3514 serr_ofs = mv_scr_offset(SCR_ERROR);
3515 writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
3516 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
3517
646a4da5
ML
3518 /* unmask all non-transient EDMA error interrupts */
3519 writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
20f733e7 3520
8b260248 3521 VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
31961943
BR
3522 readl(port_mmio + EDMA_CFG_OFS),
3523 readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
3524 readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
20f733e7
BR
3525}
3526
616d4a98
ML
3527static unsigned int mv_in_pcix_mode(struct ata_host *host)
3528{
3529 struct mv_host_priv *hpriv = host->private_data;
3530 void __iomem *mmio = hpriv->base;
3531 u32 reg;
3532
1f398472 3533 if (IS_SOC(hpriv) || !IS_PCIE(hpriv))
616d4a98
ML
3534 return 0; /* not PCI-X capable */
3535 reg = readl(mmio + MV_PCI_MODE_OFS);
3536 if ((reg & MV_PCI_MODE_MASK) == 0)
3537 return 0; /* conventional PCI mode */
3538 return 1; /* chip is in PCI-X mode */
3539}
3540
3541static int mv_pci_cut_through_okay(struct ata_host *host)
3542{
3543 struct mv_host_priv *hpriv = host->private_data;
3544 void __iomem *mmio = hpriv->base;
3545 u32 reg;
3546
3547 if (!mv_in_pcix_mode(host)) {
3548 reg = readl(mmio + PCI_COMMAND_OFS);
3549 if (reg & PCI_COMMAND_MRDTRIG)
3550 return 0; /* not okay */
3551 }
3552 return 1; /* okay */
3553}
3554
65ad7fef
ML
3555static void mv_60x1b2_errata_pci7(struct ata_host *host)
3556{
3557 struct mv_host_priv *hpriv = host->private_data;
3558 void __iomem *mmio = hpriv->base;
3559
3560 /* workaround for 60x1-B2 errata PCI#7 */
3561 if (mv_in_pcix_mode(host)) {
3562 u32 reg = readl(mmio + PCI_COMMAND_OFS);
3563 writelfl(reg & ~PCI_COMMAND_MWRCOM, mmio + PCI_COMMAND_OFS);
3564 }
3565}
3566
4447d351 3567static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
bca1c4eb 3568{
4447d351
TH
3569 struct pci_dev *pdev = to_pci_dev(host->dev);
3570 struct mv_host_priv *hpriv = host->private_data;
bca1c4eb
JG
3571 u32 hp_flags = hpriv->hp_flags;
3572
5796d1c4 3573 switch (board_idx) {
47c2b677
JG
3574 case chip_5080:
3575 hpriv->ops = &mv5xxx_ops;
ee9ccdf7 3576 hp_flags |= MV_HP_GEN_I;
47c2b677 3577
44c10138 3578 switch (pdev->revision) {
47c2b677
JG
3579 case 0x1:
3580 hp_flags |= MV_HP_ERRATA_50XXB0;
3581 break;
3582 case 0x3:
3583 hp_flags |= MV_HP_ERRATA_50XXB2;
3584 break;
3585 default:
3586 dev_printk(KERN_WARNING, &pdev->dev,
3587 "Applying 50XXB2 workarounds to unknown rev\n");
3588 hp_flags |= MV_HP_ERRATA_50XXB2;
3589 break;
3590 }
3591 break;
3592
bca1c4eb
JG
3593 case chip_504x:
3594 case chip_508x:
47c2b677 3595 hpriv->ops = &mv5xxx_ops;
ee9ccdf7 3596 hp_flags |= MV_HP_GEN_I;
bca1c4eb 3597
44c10138 3598 switch (pdev->revision) {
47c2b677
JG
3599 case 0x0:
3600 hp_flags |= MV_HP_ERRATA_50XXB0;
3601 break;
3602 case 0x3:
3603 hp_flags |= MV_HP_ERRATA_50XXB2;
3604 break;
3605 default:
3606 dev_printk(KERN_WARNING, &pdev->dev,
3607 "Applying B2 workarounds to unknown rev\n");
3608 hp_flags |= MV_HP_ERRATA_50XXB2;
3609 break;
bca1c4eb
JG
3610 }
3611 break;
3612
3613 case chip_604x:
3614 case chip_608x:
47c2b677 3615 hpriv->ops = &mv6xxx_ops;
ee9ccdf7 3616 hp_flags |= MV_HP_GEN_II;
47c2b677 3617
44c10138 3618 switch (pdev->revision) {
47c2b677 3619 case 0x7:
65ad7fef 3620 mv_60x1b2_errata_pci7(host);
47c2b677
JG
3621 hp_flags |= MV_HP_ERRATA_60X1B2;
3622 break;
3623 case 0x9:
3624 hp_flags |= MV_HP_ERRATA_60X1C0;
bca1c4eb
JG
3625 break;
3626 default:
3627 dev_printk(KERN_WARNING, &pdev->dev,
47c2b677
JG
3628 "Applying B2 workarounds to unknown rev\n");
3629 hp_flags |= MV_HP_ERRATA_60X1B2;
bca1c4eb
JG
3630 break;
3631 }
3632 break;
3633
e4e7b892 3634 case chip_7042:
616d4a98 3635 hp_flags |= MV_HP_PCIE | MV_HP_CUT_THROUGH;
306b30f7
ML
3636 if (pdev->vendor == PCI_VENDOR_ID_TTI &&
3637 (pdev->device == 0x2300 || pdev->device == 0x2310))
3638 {
4e520033
ML
3639 /*
3640 * Highpoint RocketRAID PCIe 23xx series cards:
3641 *
3642 * Unconfigured drives are treated as "Legacy"
3643 * by the BIOS, and it overwrites sector 8 with
3644 * a "Lgcy" metadata block prior to Linux boot.
3645 *
3646 * Configured drives (RAID or JBOD) leave sector 8
3647 * alone, but instead overwrite a high numbered
3648 * sector for the RAID metadata. This sector can
3649 * be determined exactly, by truncating the physical
3650 * drive capacity to a nice even GB value.
3651 *
3652 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
3653 *
3654 * Warn the user, lest they think we're just buggy.
3655 */
3656 printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
3657 " BIOS CORRUPTS DATA on all attached drives,"
3658 " regardless of if/how they are configured."
3659 " BEWARE!\n");
3660 printk(KERN_WARNING DRV_NAME ": For data safety, do not"
3661 " use sectors 8-9 on \"Legacy\" drives,"
3662 " and avoid the final two gigabytes on"
3663 " all RocketRAID BIOS initialized drives.\n");
306b30f7 3664 }
8e7decdb 3665 /* drop through */
e4e7b892
JG
3666 case chip_6042:
3667 hpriv->ops = &mv6xxx_ops;
e4e7b892 3668 hp_flags |= MV_HP_GEN_IIE;
616d4a98
ML
3669 if (board_idx == chip_6042 && mv_pci_cut_through_okay(host))
3670 hp_flags |= MV_HP_CUT_THROUGH;
e4e7b892 3671
44c10138 3672 switch (pdev->revision) {
5cf73bfb 3673 case 0x2: /* Rev.B0: the first/only public release */
e4e7b892
JG
3674 hp_flags |= MV_HP_ERRATA_60X1C0;
3675 break;
3676 default:
3677 dev_printk(KERN_WARNING, &pdev->dev,
3678 "Applying 60X1C0 workarounds to unknown rev\n");
3679 hp_flags |= MV_HP_ERRATA_60X1C0;
3680 break;
3681 }
3682 break;
f351b2d6
SB
3683 case chip_soc:
3684 hpriv->ops = &mv_soc_ops;
eb3a55a9
SB
3685 hp_flags |= MV_HP_FLAG_SOC | MV_HP_GEN_IIE |
3686 MV_HP_ERRATA_60X1C0;
f351b2d6 3687 break;
e4e7b892 3688
bca1c4eb 3689 default:
f351b2d6 3690 dev_printk(KERN_ERR, host->dev,
5796d1c4 3691 "BUG: invalid board index %u\n", board_idx);
bca1c4eb
JG
3692 return 1;
3693 }
3694
3695 hpriv->hp_flags = hp_flags;
02a121da
ML
3696 if (hp_flags & MV_HP_PCIE) {
3697 hpriv->irq_cause_ofs = PCIE_IRQ_CAUSE_OFS;
3698 hpriv->irq_mask_ofs = PCIE_IRQ_MASK_OFS;
3699 hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
3700 } else {
3701 hpriv->irq_cause_ofs = PCI_IRQ_CAUSE_OFS;
3702 hpriv->irq_mask_ofs = PCI_IRQ_MASK_OFS;
3703 hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
3704 }
bca1c4eb
JG
3705
3706 return 0;
3707}
3708
05b308e1 3709/**
47c2b677 3710 * mv_init_host - Perform some early initialization of the host.
4447d351
TH
3711 * @host: ATA host to initialize
3712 * @board_idx: controller index
05b308e1
BR
3713 *
3714 * If possible, do an early global reset of the host. Then do
3715 * our port init and clear/unmask all/relevant host interrupts.
3716 *
3717 * LOCKING:
3718 * Inherited from caller.
3719 */
4447d351 3720static int mv_init_host(struct ata_host *host, unsigned int board_idx)
20f733e7
BR
3721{
3722 int rc = 0, n_hc, port, hc;
4447d351 3723 struct mv_host_priv *hpriv = host->private_data;
f351b2d6 3724 void __iomem *mmio = hpriv->base;
47c2b677 3725
4447d351 3726 rc = mv_chip_id(host, board_idx);
bca1c4eb 3727 if (rc)
352fab70 3728 goto done;
f351b2d6 3729
1f398472 3730 if (IS_SOC(hpriv)) {
7368f919
ML
3731 hpriv->main_irq_cause_addr = mmio + SOC_HC_MAIN_IRQ_CAUSE_OFS;
3732 hpriv->main_irq_mask_addr = mmio + SOC_HC_MAIN_IRQ_MASK_OFS;
1f398472
ML
3733 } else {
3734 hpriv->main_irq_cause_addr = mmio + PCI_HC_MAIN_IRQ_CAUSE_OFS;
3735 hpriv->main_irq_mask_addr = mmio + PCI_HC_MAIN_IRQ_MASK_OFS;
f351b2d6 3736 }
352fab70 3737
5d0fb2e7
TR
3738 /* initialize shadow irq mask with register's value */
3739 hpriv->main_irq_mask = readl(hpriv->main_irq_mask_addr);
3740
352fab70 3741 /* global interrupt mask: 0 == mask everything */
c4de573b 3742 mv_set_main_irq_mask(host, ~0, 0);
bca1c4eb 3743
4447d351 3744 n_hc = mv_get_hc_count(host->ports[0]->flags);
bca1c4eb 3745
4447d351 3746 for (port = 0; port < host->n_ports; port++)
47c2b677 3747 hpriv->ops->read_preamp(hpriv, port, mmio);
20f733e7 3748
c9d39130 3749 rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
47c2b677 3750 if (rc)
20f733e7 3751 goto done;
20f733e7 3752
522479fb 3753 hpriv->ops->reset_flash(hpriv, mmio);
7bb3c529 3754 hpriv->ops->reset_bus(host, mmio);
47c2b677 3755 hpriv->ops->enable_leds(hpriv, mmio);
20f733e7 3756
4447d351 3757 for (port = 0; port < host->n_ports; port++) {
cbcdd875 3758 struct ata_port *ap = host->ports[port];
2a47ce06 3759 void __iomem *port_mmio = mv_port_base(mmio, port);
cbcdd875
TH
3760
3761 mv_port_init(&ap->ioaddr, port_mmio);
3762
7bb3c529 3763#ifdef CONFIG_PCI
1f398472 3764 if (!IS_SOC(hpriv)) {
f351b2d6
SB
3765 unsigned int offset = port_mmio - mmio;
3766 ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
3767 ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
3768 }
7bb3c529 3769#endif
20f733e7
BR
3770 }
3771
3772 for (hc = 0; hc < n_hc; hc++) {
31961943
BR
3773 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
3774
3775 VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
3776 "(before clear)=0x%08x\n", hc,
3777 readl(hc_mmio + HC_CFG_OFS),
3778 readl(hc_mmio + HC_IRQ_CAUSE_OFS));
3779
3780 /* Clear any currently outstanding hc interrupt conditions */
3781 writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
20f733e7
BR
3782 }
3783
44c65d16
ML
3784 if (!IS_SOC(hpriv)) {
3785 /* Clear any currently outstanding host interrupt conditions */
3786 writelfl(0, mmio + hpriv->irq_cause_ofs);
31961943 3787
44c65d16
ML
3788 /* and unmask interrupt generation for host regs */
3789 writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
3790 }
51de32d2 3791
6be96ac1
ML
3792 /*
3793 * enable only global host interrupts for now.
3794 * The per-port interrupts get done later as ports are set up.
3795 */
3796 mv_set_main_irq_mask(host, 0, PCI_ERR);
2b748a0a
ML
3797 mv_set_irq_coalescing(host, irq_coalescing_io_count,
3798 irq_coalescing_usecs);
f351b2d6
SB
3799done:
3800 return rc;
3801}
fb621e2f 3802
fbf14e2f
BB
3803static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
3804{
3805 hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
3806 MV_CRQB_Q_SZ, 0);
3807 if (!hpriv->crqb_pool)
3808 return -ENOMEM;
3809
3810 hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
3811 MV_CRPB_Q_SZ, 0);
3812 if (!hpriv->crpb_pool)
3813 return -ENOMEM;
3814
3815 hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
3816 MV_SG_TBL_SZ, 0);
3817 if (!hpriv->sg_tbl_pool)
3818 return -ENOMEM;
3819
3820 return 0;
3821}
3822
15a32632
LB
3823static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
3824 struct mbus_dram_target_info *dram)
3825{
3826 int i;
3827
3828 for (i = 0; i < 4; i++) {
3829 writel(0, hpriv->base + WINDOW_CTRL(i));
3830 writel(0, hpriv->base + WINDOW_BASE(i));
3831 }
3832
3833 for (i = 0; i < dram->num_cs; i++) {
3834 struct mbus_dram_window *cs = dram->cs + i;
3835
3836 writel(((cs->size - 1) & 0xffff0000) |
3837 (cs->mbus_attr << 8) |
3838 (dram->mbus_dram_target_id << 4) | 1,
3839 hpriv->base + WINDOW_CTRL(i));
3840 writel(cs->base, hpriv->base + WINDOW_BASE(i));
3841 }
3842}
3843
f351b2d6
SB
3844/**
3845 * mv_platform_probe - handle a positive probe of an soc Marvell
3846 * host
3847 * @pdev: platform device found
3848 *
3849 * LOCKING:
3850 * Inherited from caller.
3851 */
3852static int mv_platform_probe(struct platform_device *pdev)
3853{
3854 static int printed_version;
3855 const struct mv_sata_platform_data *mv_platform_data;
3856 const struct ata_port_info *ppi[] =
3857 { &mv_port_info[chip_soc], NULL };
3858 struct ata_host *host;
3859 struct mv_host_priv *hpriv;
3860 struct resource *res;
3861 int n_ports, rc;
20f733e7 3862
f351b2d6
SB
3863 if (!printed_version++)
3864 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
bca1c4eb 3865
f351b2d6
SB
3866 /*
3867 * Simple resource validation ..
3868 */
3869 if (unlikely(pdev->num_resources != 2)) {
3870 dev_err(&pdev->dev, "invalid number of resources\n");
3871 return -EINVAL;
3872 }
3873
3874 /*
3875 * Get the register base first
3876 */
3877 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3878 if (res == NULL)
3879 return -EINVAL;
3880
3881 /* allocate host */
3882 mv_platform_data = pdev->dev.platform_data;
3883 n_ports = mv_platform_data->n_ports;
3884
3885 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
3886 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
3887
3888 if (!host || !hpriv)
3889 return -ENOMEM;
3890 host->private_data = hpriv;
3891 hpriv->n_ports = n_ports;
3892
3893 host->iomap = NULL;
f1cb0ea1
SB
3894 hpriv->base = devm_ioremap(&pdev->dev, res->start,
3895 res->end - res->start + 1);
f351b2d6
SB
3896 hpriv->base -= MV_SATAHC0_REG_BASE;
3897
15a32632
LB
3898 /*
3899 * (Re-)program MBUS remapping windows if we are asked to.
3900 */
3901 if (mv_platform_data->dram != NULL)
3902 mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
3903
fbf14e2f
BB
3904 rc = mv_create_dma_pools(hpriv, &pdev->dev);
3905 if (rc)
3906 return rc;
3907
f351b2d6
SB
3908 /* initialize adapter */
3909 rc = mv_init_host(host, chip_soc);
3910 if (rc)
3911 return rc;
3912
3913 dev_printk(KERN_INFO, &pdev->dev,
3914 "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
3915 host->n_ports);
3916
3917 return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
3918 IRQF_SHARED, &mv6_sht);
3919}
3920
3921/*
3922 *
3923 * mv_platform_remove - unplug a platform interface
3924 * @pdev: platform device
3925 *
3926 * A platform bus SATA device has been unplugged. Perform the needed
3927 * cleanup. Also called on module unload for any active devices.
3928 */
3929static int __devexit mv_platform_remove(struct platform_device *pdev)
3930{
3931 struct device *dev = &pdev->dev;
3932 struct ata_host *host = dev_get_drvdata(dev);
f351b2d6
SB
3933
3934 ata_host_detach(host);
f351b2d6 3935 return 0;
20f733e7
BR
3936}
3937
f351b2d6
SB
3938static struct platform_driver mv_platform_driver = {
3939 .probe = mv_platform_probe,
3940 .remove = __devexit_p(mv_platform_remove),
3941 .driver = {
3942 .name = DRV_NAME,
3943 .owner = THIS_MODULE,
3944 },
3945};
3946
3947
7bb3c529 3948#ifdef CONFIG_PCI
f351b2d6
SB
3949static int mv_pci_init_one(struct pci_dev *pdev,
3950 const struct pci_device_id *ent);
3951
7bb3c529
SB
3952
3953static struct pci_driver mv_pci_driver = {
3954 .name = DRV_NAME,
3955 .id_table = mv_pci_tbl,
f351b2d6 3956 .probe = mv_pci_init_one,
7bb3c529
SB
3957 .remove = ata_pci_remove_one,
3958};
3959
7bb3c529
SB
3960/* move to PCI layer or libata core? */
3961static int pci_go_64(struct pci_dev *pdev)
3962{
3963 int rc;
3964
3965 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
3966 rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
3967 if (rc) {
3968 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3969 if (rc) {
3970 dev_printk(KERN_ERR, &pdev->dev,
3971 "64-bit DMA enable failed\n");
3972 return rc;
3973 }
3974 }
3975 } else {
3976 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
3977 if (rc) {
3978 dev_printk(KERN_ERR, &pdev->dev,
3979 "32-bit DMA enable failed\n");
3980 return rc;
3981 }
3982 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
3983 if (rc) {
3984 dev_printk(KERN_ERR, &pdev->dev,
3985 "32-bit consistent DMA enable failed\n");
3986 return rc;
3987 }
3988 }
3989
3990 return rc;
3991}
3992
05b308e1
BR
3993/**
3994 * mv_print_info - Dump key info to kernel log for perusal.
4447d351 3995 * @host: ATA host to print info about
05b308e1
BR
3996 *
3997 * FIXME: complete this.
3998 *
3999 * LOCKING:
4000 * Inherited from caller.
4001 */
4447d351 4002static void mv_print_info(struct ata_host *host)
31961943 4003{
4447d351
TH
4004 struct pci_dev *pdev = to_pci_dev(host->dev);
4005 struct mv_host_priv *hpriv = host->private_data;
44c10138 4006 u8 scc;
c1e4fe71 4007 const char *scc_s, *gen;
31961943
BR
4008
4009 /* Use this to determine the HW stepping of the chip so we know
4010 * what errata to workaround
4011 */
31961943
BR
4012 pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
4013 if (scc == 0)
4014 scc_s = "SCSI";
4015 else if (scc == 0x01)
4016 scc_s = "RAID";
4017 else
c1e4fe71
JG
4018 scc_s = "?";
4019
4020 if (IS_GEN_I(hpriv))
4021 gen = "I";
4022 else if (IS_GEN_II(hpriv))
4023 gen = "II";
4024 else if (IS_GEN_IIE(hpriv))
4025 gen = "IIE";
4026 else
4027 gen = "?";
31961943 4028
a9524a76 4029 dev_printk(KERN_INFO, &pdev->dev,
c1e4fe71
JG
4030 "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
4031 gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
31961943
BR
4032 scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
4033}
4034
05b308e1 4035/**
f351b2d6 4036 * mv_pci_init_one - handle a positive probe of a PCI Marvell host
05b308e1
BR
4037 * @pdev: PCI device found
4038 * @ent: PCI device ID entry for the matched host
4039 *
4040 * LOCKING:
4041 * Inherited from caller.
4042 */
f351b2d6
SB
4043static int mv_pci_init_one(struct pci_dev *pdev,
4044 const struct pci_device_id *ent)
20f733e7 4045{
2dcb407e 4046 static int printed_version;
20f733e7 4047 unsigned int board_idx = (unsigned int)ent->driver_data;
4447d351
TH
4048 const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
4049 struct ata_host *host;
4050 struct mv_host_priv *hpriv;
4051 int n_ports, rc;
20f733e7 4052
a9524a76
JG
4053 if (!printed_version++)
4054 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
20f733e7 4055
4447d351
TH
4056 /* allocate host */
4057 n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
4058
4059 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
4060 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
4061 if (!host || !hpriv)
4062 return -ENOMEM;
4063 host->private_data = hpriv;
f351b2d6 4064 hpriv->n_ports = n_ports;
4447d351
TH
4065
4066 /* acquire resources */
24dc5f33
TH
4067 rc = pcim_enable_device(pdev);
4068 if (rc)
20f733e7 4069 return rc;
20f733e7 4070
0d5ff566
TH
4071 rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
4072 if (rc == -EBUSY)
24dc5f33 4073 pcim_pin_device(pdev);
0d5ff566 4074 if (rc)
24dc5f33 4075 return rc;
4447d351 4076 host->iomap = pcim_iomap_table(pdev);
f351b2d6 4077 hpriv->base = host->iomap[MV_PRIMARY_BAR];
20f733e7 4078
d88184fb
JG
4079 rc = pci_go_64(pdev);
4080 if (rc)
4081 return rc;
4082
da2fa9ba
ML
4083 rc = mv_create_dma_pools(hpriv, &pdev->dev);
4084 if (rc)
4085 return rc;
4086
20f733e7 4087 /* initialize adapter */
4447d351 4088 rc = mv_init_host(host, board_idx);
24dc5f33
TH
4089 if (rc)
4090 return rc;
20f733e7 4091
6d3c30ef
ML
4092 /* Enable message-switched interrupts, if requested */
4093 if (msi && pci_enable_msi(pdev) == 0)
4094 hpriv->hp_flags |= MV_HP_FLAG_MSI;
20f733e7 4095
31961943 4096 mv_dump_pci_cfg(pdev, 0x68);
4447d351 4097 mv_print_info(host);
20f733e7 4098
4447d351 4099 pci_set_master(pdev);
ea8b4db9 4100 pci_try_set_mwi(pdev);
4447d351 4101 return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
c5d3e45a 4102 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
20f733e7 4103}
7bb3c529 4104#endif
20f733e7 4105
f351b2d6
SB
4106static int mv_platform_probe(struct platform_device *pdev);
4107static int __devexit mv_platform_remove(struct platform_device *pdev);
4108
20f733e7
BR
4109static int __init mv_init(void)
4110{
7bb3c529
SB
4111 int rc = -ENODEV;
4112#ifdef CONFIG_PCI
4113 rc = pci_register_driver(&mv_pci_driver);
f351b2d6
SB
4114 if (rc < 0)
4115 return rc;
4116#endif
4117 rc = platform_driver_register(&mv_platform_driver);
4118
4119#ifdef CONFIG_PCI
4120 if (rc < 0)
4121 pci_unregister_driver(&mv_pci_driver);
7bb3c529
SB
4122#endif
4123 return rc;
20f733e7
BR
4124}
4125
4126static void __exit mv_exit(void)
4127{
7bb3c529 4128#ifdef CONFIG_PCI
20f733e7 4129 pci_unregister_driver(&mv_pci_driver);
7bb3c529 4130#endif
f351b2d6 4131 platform_driver_unregister(&mv_platform_driver);
20f733e7
BR
4132}
4133
4134MODULE_AUTHOR("Brett Russ");
4135MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
4136MODULE_LICENSE("GPL");
4137MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
4138MODULE_VERSION(DRV_VERSION);
17c5aab5 4139MODULE_ALIAS("platform:" DRV_NAME);
20f733e7
BR
4140
4141module_init(mv_init);
4142module_exit(mv_exit);
This page took 0.688396 seconds and 5 git commands to generate.