blob: 26a6337195b3681587d2025a9a97519f01bf8ca1 [file] [log] [blame]
Brett Russ20f733e2005-09-01 18:26:17 -04001/*
2 * sata_mv.c - Marvell SATA support
3 *
Mark Lorde12bef52008-03-31 19:33:56 -04004 * Copyright 2008: Marvell Corporation, all rights reserved.
Jeff Garzik8b260242005-11-12 12:32:50 -05005 * Copyright 2005: EMC Corporation, all rights reserved.
Jeff Garzike2b1be52005-11-18 14:04:23 -05006 * Copyright 2005 Red Hat, Inc. All rights reserved.
Brett Russ20f733e2005-09-01 18:26:17 -04007 *
8 * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2 of the License.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 *
23 */
24
Jeff Garzik4a05e202007-05-24 23:40:15 -040025/*
Mark Lord85afb932008-04-19 14:54:41 -040026 * sata_mv TODO list:
27 *
28 * --> Errata workaround for NCQ device errors.
29 *
30 * --> More errata workarounds for PCI-X.
31 *
32 * --> Complete a full errata audit for all chipsets to identify others.
33 *
34 * --> ATAPI support (Marvell claims the 60xx/70xx chips can do it).
35 *
36 * --> Investigate problems with PCI Message Signalled Interrupts (MSI).
37 *
38 * --> Cache frequently-accessed registers in mv_port_priv to reduce overhead.
39 *
40 * --> Develop a low-power-consumption strategy, and implement it.
41 *
42 * --> [Experiment, low priority] Investigate interrupt coalescing.
43 * Quite often, especially with PCI Message Signalled Interrupts (MSI),
44 * the overhead reduced by interrupt mitigation is quite often not
45 * worth the latency cost.
46 *
47 * --> [Experiment, Marvell value added] Is it possible to use target
48 * mode to cross-connect two Linux boxes with Marvell cards? If so,
49 * creating LibATA target mode support would be very interesting.
50 *
51 * Target mode, for those without docs, is the ability to directly
52 * connect two SATA ports.
53 */
Jeff Garzik4a05e202007-05-24 23:40:15 -040054
Brett Russ20f733e2005-09-01 18:26:17 -040055#include <linux/kernel.h>
56#include <linux/module.h>
57#include <linux/pci.h>
58#include <linux/init.h>
59#include <linux/blkdev.h>
60#include <linux/delay.h>
61#include <linux/interrupt.h>
Andrew Morton8d8b6002008-02-04 23:43:44 -080062#include <linux/dmapool.h>
Brett Russ20f733e2005-09-01 18:26:17 -040063#include <linux/dma-mapping.h>
Jeff Garzika9524a72005-10-30 14:39:11 -050064#include <linux/device.h>
Saeed Bisharaf351b2d2008-02-01 18:08:03 -050065#include <linux/platform_device.h>
66#include <linux/ata_platform.h>
Lennert Buytenhek15a32632008-03-27 14:51:39 -040067#include <linux/mbus.h>
Brett Russ20f733e2005-09-01 18:26:17 -040068#include <scsi/scsi_host.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050069#include <scsi/scsi_cmnd.h>
Jeff Garzik6c087722007-10-12 00:16:23 -040070#include <scsi/scsi_device.h>
Brett Russ20f733e2005-09-01 18:26:17 -040071#include <linux/libata.h>
Brett Russ20f733e2005-09-01 18:26:17 -040072
73#define DRV_NAME "sata_mv"
Mark Lord1fd2e1c2008-01-26 18:33:59 -050074#define DRV_VERSION "1.20"
Brett Russ20f733e2005-09-01 18:26:17 -040075
76enum {
77 /* BAR's are enumerated in terms of pci_resource_start() terms */
78 MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
79 MV_IO_BAR = 2, /* offset 0x18: IO space */
80 MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
81
82 MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
83 MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
84
85 MV_PCI_REG_BASE = 0,
86 MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
Mark Lord615ab952006-05-19 16:24:56 -040087 MV_IRQ_COAL_CAUSE = (MV_IRQ_COAL_REG_BASE + 0x08),
88 MV_IRQ_COAL_CAUSE_LO = (MV_IRQ_COAL_REG_BASE + 0x88),
89 MV_IRQ_COAL_CAUSE_HI = (MV_IRQ_COAL_REG_BASE + 0x8c),
90 MV_IRQ_COAL_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xcc),
91 MV_IRQ_COAL_TIME_THRESHOLD = (MV_IRQ_COAL_REG_BASE + 0xd0),
92
Brett Russ20f733e2005-09-01 18:26:17 -040093 MV_SATAHC0_REG_BASE = 0x20000,
Jeff Garzik522479f2005-11-12 22:14:02 -050094 MV_FLASH_CTL = 0x1046c,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -050095 MV_GPIO_PORT_CTL = 0x104f0,
96 MV_RESET_CFG = 0x180d8,
Brett Russ20f733e2005-09-01 18:26:17 -040097
98 MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
99 MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
100 MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
101 MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
102
Brett Russ31961942005-09-30 01:36:00 -0400103 MV_MAX_Q_DEPTH = 32,
104 MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
105
106 /* CRQB needs alignment on a 1KB boundary. Size == 1KB
107 * CRPB needs alignment on a 256B boundary. Size == 256B
Brett Russ31961942005-09-30 01:36:00 -0400108 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
109 */
110 MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
111 MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
Mark Lordda2fa9b2008-01-26 18:32:45 -0500112 MV_MAX_SG_CT = 256,
Brett Russ31961942005-09-30 01:36:00 -0400113 MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
Brett Russ31961942005-09-30 01:36:00 -0400114
Mark Lord352fab72008-04-19 14:43:42 -0400115 /* Determine hc from 0-7 port: hc = port >> MV_PORT_HC_SHIFT */
Brett Russ20f733e2005-09-01 18:26:17 -0400116 MV_PORT_HC_SHIFT = 2,
Mark Lord352fab72008-04-19 14:43:42 -0400117 MV_PORTS_PER_HC = (1 << MV_PORT_HC_SHIFT), /* 4 */
118 /* Determine hc port from 0-7 port: hardport = port & MV_PORT_MASK */
119 MV_PORT_MASK = (MV_PORTS_PER_HC - 1), /* 3 */
Brett Russ20f733e2005-09-01 18:26:17 -0400120
121 /* Host Flags */
122 MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
123 MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100124 /* SoC integrated controllers, no PCI interface */
Mark Lorde12bef52008-03-31 19:33:56 -0400125 MV_FLAG_SOC = (1 << 28),
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100126
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400127 MV_COMMON_FLAGS = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400128 ATA_FLAG_MMIO | ATA_FLAG_NO_ATAPI |
129 ATA_FLAG_PIO_POLLING,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500130 MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
Brett Russ20f733e2005-09-01 18:26:17 -0400131
Brett Russ31961942005-09-30 01:36:00 -0400132 CRQB_FLAG_READ = (1 << 0),
133 CRQB_TAG_SHIFT = 1,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400134 CRQB_IOID_SHIFT = 6, /* CRQB Gen-II/IIE IO Id shift */
Mark Lorde12bef52008-03-31 19:33:56 -0400135 CRQB_PMP_SHIFT = 12, /* CRQB Gen-II/IIE PMP shift */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400136 CRQB_HOSTQ_SHIFT = 17, /* CRQB Gen-II/IIE HostQueTag shift */
Brett Russ31961942005-09-30 01:36:00 -0400137 CRQB_CMD_ADDR_SHIFT = 8,
138 CRQB_CMD_CS = (0x2 << 11),
139 CRQB_CMD_LAST = (1 << 15),
140
141 CRPB_FLAG_STATUS_SHIFT = 8,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400142 CRPB_IOID_SHIFT_6 = 5, /* CRPB Gen-II IO Id shift */
143 CRPB_IOID_SHIFT_7 = 7, /* CRPB Gen-IIE IO Id shift */
Brett Russ31961942005-09-30 01:36:00 -0400144
145 EPRD_FLAG_END_OF_TBL = (1 << 31),
146
Brett Russ20f733e2005-09-01 18:26:17 -0400147 /* PCI interface registers */
148
Brett Russ31961942005-09-30 01:36:00 -0400149 PCI_COMMAND_OFS = 0xc00,
150
Brett Russ20f733e2005-09-01 18:26:17 -0400151 PCI_MAIN_CMD_STS_OFS = 0xd30,
152 STOP_PCI_MASTER = (1 << 2),
153 PCI_MASTER_EMPTY = (1 << 3),
154 GLOB_SFT_RST = (1 << 4),
155
Jeff Garzik522479f2005-11-12 22:14:02 -0500156 MV_PCI_MODE = 0xd00,
157 MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
158 MV_PCI_DISC_TIMER = 0xd04,
159 MV_PCI_MSI_TRIGGER = 0xc38,
160 MV_PCI_SERR_MASK = 0xc28,
161 MV_PCI_XBAR_TMOUT = 0x1d04,
162 MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
163 MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
164 MV_PCI_ERR_ATTRIBUTE = 0x1d48,
165 MV_PCI_ERR_COMMAND = 0x1d50,
166
Mark Lord02a121d2007-12-01 13:07:22 -0500167 PCI_IRQ_CAUSE_OFS = 0x1d58,
168 PCI_IRQ_MASK_OFS = 0x1d5c,
Brett Russ20f733e2005-09-01 18:26:17 -0400169 PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
170
Mark Lord02a121d2007-12-01 13:07:22 -0500171 PCIE_IRQ_CAUSE_OFS = 0x1900,
172 PCIE_IRQ_MASK_OFS = 0x1910,
Mark Lord646a4da2008-01-26 18:30:37 -0500173 PCIE_UNMASK_ALL_IRQS = 0x40a, /* assorted bits */
Mark Lord02a121d2007-12-01 13:07:22 -0500174
Brett Russ20f733e2005-09-01 18:26:17 -0400175 HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
176 HC_MAIN_IRQ_MASK_OFS = 0x1d64,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500177 HC_SOC_MAIN_IRQ_CAUSE_OFS = 0x20020,
178 HC_SOC_MAIN_IRQ_MASK_OFS = 0x20024,
Mark Lord352fab72008-04-19 14:43:42 -0400179 ERR_IRQ = (1 << 0), /* shift by port # */
180 DONE_IRQ = (1 << 1), /* shift by port # */
Brett Russ20f733e2005-09-01 18:26:17 -0400181 HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
182 HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
183 PCI_ERR = (1 << 18),
184 TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
185 TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
Jeff Garzikfb621e22007-02-25 04:19:45 -0500186 PORTS_0_3_COAL_DONE = (1 << 8),
187 PORTS_4_7_COAL_DONE = (1 << 17),
Brett Russ20f733e2005-09-01 18:26:17 -0400188 PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
189 GPIO_INT = (1 << 22),
190 SELF_INT = (1 << 23),
191 TWSI_INT = (1 << 24),
192 HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
Jeff Garzikfb621e22007-02-25 04:19:45 -0500193 HC_MAIN_RSVD_5 = (0x1fff << 19), /* bits 31-19 */
Mark Lorde12bef52008-03-31 19:33:56 -0400194 HC_MAIN_RSVD_SOC = (0x3fffffb << 6), /* bits 31-9, 7-6 */
Jeff Garzik8b260242005-11-12 12:32:50 -0500195 HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
Mark Lordf9f7fe02008-04-19 14:44:42 -0400196 PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
Brett Russ20f733e2005-09-01 18:26:17 -0400197 PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
198 HC_MAIN_RSVD),
Jeff Garzikfb621e22007-02-25 04:19:45 -0500199 HC_MAIN_MASKED_IRQS_5 = (PORTS_0_3_COAL_DONE | PORTS_4_7_COAL_DONE |
200 HC_MAIN_RSVD_5),
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500201 HC_MAIN_MASKED_IRQS_SOC = (PORTS_0_3_COAL_DONE | HC_MAIN_RSVD_SOC),
Brett Russ20f733e2005-09-01 18:26:17 -0400202
203 /* SATAHC registers */
204 HC_CFG_OFS = 0,
205
206 HC_IRQ_CAUSE_OFS = 0x14,
Mark Lord352fab72008-04-19 14:43:42 -0400207 DMA_IRQ = (1 << 0), /* shift by port # */
208 HC_COAL_IRQ = (1 << 4), /* IRQ coalescing */
Brett Russ20f733e2005-09-01 18:26:17 -0400209 DEV_IRQ = (1 << 8), /* shift by port # */
210
211 /* Shadow block registers */
Brett Russ31961942005-09-30 01:36:00 -0400212 SHD_BLK_OFS = 0x100,
213 SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
Brett Russ20f733e2005-09-01 18:26:17 -0400214
215 /* SATA registers */
216 SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
217 SATA_ACTIVE_OFS = 0x350,
Mark Lord0c589122008-01-26 18:31:16 -0500218 SATA_FIS_IRQ_CAUSE_OFS = 0x364,
Mark Lord17c5aab2008-04-16 14:56:51 -0400219
Mark Lorde12bef52008-03-31 19:33:56 -0400220 LTMODE_OFS = 0x30c,
Mark Lord17c5aab2008-04-16 14:56:51 -0400221 LTMODE_BIT8 = (1 << 8), /* unknown, but necessary */
222
Jeff Garzik47c2b672005-11-12 21:13:17 -0500223 PHY_MODE3 = 0x310,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500224 PHY_MODE4 = 0x314,
225 PHY_MODE2 = 0x330,
Mark Lorde12bef52008-03-31 19:33:56 -0400226 SATA_IFCTL_OFS = 0x344,
227 SATA_IFSTAT_OFS = 0x34c,
228 VENDOR_UNIQUE_FIS_OFS = 0x35c,
Mark Lord17c5aab2008-04-16 14:56:51 -0400229
Mark Lorde12bef52008-03-31 19:33:56 -0400230 FIS_CFG_OFS = 0x360,
Mark Lord17c5aab2008-04-16 14:56:51 -0400231 FIS_CFG_SINGLE_SYNC = (1 << 16), /* SYNC on DMA activation */
232
Jeff Garzikc9d39132005-11-13 17:47:51 -0500233 MV5_PHY_MODE = 0x74,
234 MV5_LT_MODE = 0x30,
235 MV5_PHY_CTL = 0x0C,
Mark Lorde12bef52008-03-31 19:33:56 -0400236 SATA_INTERFACE_CFG = 0x050,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500237
238 MV_M2_PREAMP_MASK = 0x7e0,
Brett Russ20f733e2005-09-01 18:26:17 -0400239
240 /* Port registers */
241 EDMA_CFG_OFS = 0,
Mark Lord0c589122008-01-26 18:31:16 -0500242 EDMA_CFG_Q_DEPTH = 0x1f, /* max device queue depth */
243 EDMA_CFG_NCQ = (1 << 5), /* for R/W FPDMA queued */
244 EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
245 EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
246 EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
Mark Lorde12bef52008-03-31 19:33:56 -0400247 EDMA_CFG_EDMA_FBS = (1 << 16), /* EDMA FIS-Based Switching */
248 EDMA_CFG_FBS = (1 << 26), /* FIS-Based Switching */
Brett Russ20f733e2005-09-01 18:26:17 -0400249
250 EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
251 EDMA_ERR_IRQ_MASK_OFS = 0xc,
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400252 EDMA_ERR_D_PAR = (1 << 0), /* UDMA data parity err */
253 EDMA_ERR_PRD_PAR = (1 << 1), /* UDMA PRD parity err */
254 EDMA_ERR_DEV = (1 << 2), /* device error */
255 EDMA_ERR_DEV_DCON = (1 << 3), /* device disconnect */
256 EDMA_ERR_DEV_CON = (1 << 4), /* device connected */
257 EDMA_ERR_SERR = (1 << 5), /* SError bits [WBDST] raised */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400258 EDMA_ERR_SELF_DIS = (1 << 7), /* Gen II/IIE self-disable */
259 EDMA_ERR_SELF_DIS_5 = (1 << 8), /* Gen I self-disable */
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400260 EDMA_ERR_BIST_ASYNC = (1 << 8), /* BIST FIS or Async Notify */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400261 EDMA_ERR_TRANS_IRQ_7 = (1 << 8), /* Gen IIE transprt layer irq */
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400262 EDMA_ERR_CRQB_PAR = (1 << 9), /* CRQB parity error */
263 EDMA_ERR_CRPB_PAR = (1 << 10), /* CRPB parity error */
264 EDMA_ERR_INTRL_PAR = (1 << 11), /* internal parity error */
265 EDMA_ERR_IORDY = (1 << 12), /* IORdy timeout */
Mark Lord646a4da2008-01-26 18:30:37 -0500266
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400267 EDMA_ERR_LNK_CTRL_RX = (0xf << 13), /* link ctrl rx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500268 EDMA_ERR_LNK_CTRL_RX_0 = (1 << 13), /* transient: CRC err */
269 EDMA_ERR_LNK_CTRL_RX_1 = (1 << 14), /* transient: FIFO err */
270 EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15), /* fatal: caught SYNC */
271 EDMA_ERR_LNK_CTRL_RX_3 = (1 << 16), /* transient: FIS rx err */
272
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400273 EDMA_ERR_LNK_DATA_RX = (0xf << 17), /* link data rx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500274
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400275 EDMA_ERR_LNK_CTRL_TX = (0x1f << 21), /* link ctrl tx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500276 EDMA_ERR_LNK_CTRL_TX_0 = (1 << 21), /* transient: CRC err */
277 EDMA_ERR_LNK_CTRL_TX_1 = (1 << 22), /* transient: FIFO err */
278 EDMA_ERR_LNK_CTRL_TX_2 = (1 << 23), /* transient: caught SYNC */
279 EDMA_ERR_LNK_CTRL_TX_3 = (1 << 24), /* transient: caught DMAT */
280 EDMA_ERR_LNK_CTRL_TX_4 = (1 << 25), /* transient: FIS collision */
281
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400282 EDMA_ERR_LNK_DATA_TX = (0x1f << 26), /* link data tx error */
Mark Lord646a4da2008-01-26 18:30:37 -0500283
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400284 EDMA_ERR_TRANS_PROTO = (1 << 31), /* transport protocol error */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400285 EDMA_ERR_OVERRUN_5 = (1 << 5),
286 EDMA_ERR_UNDERRUN_5 = (1 << 6),
Mark Lord646a4da2008-01-26 18:30:37 -0500287
288 EDMA_ERR_IRQ_TRANSIENT = EDMA_ERR_LNK_CTRL_RX_0 |
289 EDMA_ERR_LNK_CTRL_RX_1 |
290 EDMA_ERR_LNK_CTRL_RX_3 |
Mark Lord85afb932008-04-19 14:54:41 -0400291 EDMA_ERR_LNK_CTRL_TX,
Mark Lord646a4da2008-01-26 18:30:37 -0500292
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400293 EDMA_EH_FREEZE = EDMA_ERR_D_PAR |
294 EDMA_ERR_PRD_PAR |
295 EDMA_ERR_DEV_DCON |
296 EDMA_ERR_DEV_CON |
297 EDMA_ERR_SERR |
298 EDMA_ERR_SELF_DIS |
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400299 EDMA_ERR_CRQB_PAR |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400300 EDMA_ERR_CRPB_PAR |
301 EDMA_ERR_INTRL_PAR |
302 EDMA_ERR_IORDY |
303 EDMA_ERR_LNK_CTRL_RX_2 |
304 EDMA_ERR_LNK_DATA_RX |
305 EDMA_ERR_LNK_DATA_TX |
306 EDMA_ERR_TRANS_PROTO,
Mark Lorde12bef52008-03-31 19:33:56 -0400307
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400308 EDMA_EH_FREEZE_5 = EDMA_ERR_D_PAR |
309 EDMA_ERR_PRD_PAR |
310 EDMA_ERR_DEV_DCON |
311 EDMA_ERR_DEV_CON |
312 EDMA_ERR_OVERRUN_5 |
313 EDMA_ERR_UNDERRUN_5 |
314 EDMA_ERR_SELF_DIS_5 |
Jeff Garzik6c1153e2007-07-13 15:20:15 -0400315 EDMA_ERR_CRQB_PAR |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400316 EDMA_ERR_CRPB_PAR |
317 EDMA_ERR_INTRL_PAR |
318 EDMA_ERR_IORDY,
Brett Russ20f733e2005-09-01 18:26:17 -0400319
Brett Russ31961942005-09-30 01:36:00 -0400320 EDMA_REQ_Q_BASE_HI_OFS = 0x10,
321 EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
Brett Russ31961942005-09-30 01:36:00 -0400322
323 EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
324 EDMA_REQ_Q_PTR_SHIFT = 5,
325
326 EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
327 EDMA_RSP_Q_IN_PTR_OFS = 0x20,
328 EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
Brett Russ31961942005-09-30 01:36:00 -0400329 EDMA_RSP_Q_PTR_SHIFT = 3,
330
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400331 EDMA_CMD_OFS = 0x28, /* EDMA command register */
332 EDMA_EN = (1 << 0), /* enable EDMA */
333 EDMA_DS = (1 << 1), /* disable EDMA; self-negated */
334 ATA_RST = (1 << 2), /* reset trans/link/phy */
Brett Russ20f733e2005-09-01 18:26:17 -0400335
Jeff Garzikc9d39132005-11-13 17:47:51 -0500336 EDMA_IORDY_TMOUT = 0x34,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500337 EDMA_ARB_CFG = 0x38,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500338
Mark Lord352fab72008-04-19 14:43:42 -0400339 GEN_II_NCQ_MAX_SECTORS = 256, /* max sects/io on Gen2 w/NCQ */
340
Brett Russ31961942005-09-30 01:36:00 -0400341 /* Host private flags (hp_flags) */
342 MV_HP_FLAG_MSI = (1 << 0),
Jeff Garzik47c2b672005-11-12 21:13:17 -0500343 MV_HP_ERRATA_50XXB0 = (1 << 1),
344 MV_HP_ERRATA_50XXB2 = (1 << 2),
345 MV_HP_ERRATA_60X1B2 = (1 << 3),
346 MV_HP_ERRATA_60X1C0 = (1 << 4),
Jeff Garzike4e7b892006-01-31 12:18:41 -0500347 MV_HP_ERRATA_XX42A0 = (1 << 5),
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400348 MV_HP_GEN_I = (1 << 6), /* Generation I: 50xx */
349 MV_HP_GEN_II = (1 << 7), /* Generation II: 60xx */
350 MV_HP_GEN_IIE = (1 << 8), /* Generation IIE: 6042/7042 */
Mark Lord02a121d2007-12-01 13:07:22 -0500351 MV_HP_PCIE = (1 << 9), /* PCIe bus/regs: 7042 */
Brett Russ20f733e2005-09-01 18:26:17 -0400352
Brett Russ31961942005-09-30 01:36:00 -0400353 /* Port private flags (pp_flags) */
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400354 MV_PP_FLAG_EDMA_EN = (1 << 0), /* is EDMA engine enabled? */
Mark Lord72109162008-01-26 18:31:33 -0500355 MV_PP_FLAG_NCQ_EN = (1 << 1), /* is EDMA set up for NCQ? */
Brett Russ31961942005-09-30 01:36:00 -0400356};
357
Jeff Garzikee9ccdf2007-07-12 15:51:22 -0400358#define IS_GEN_I(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_I)
359#define IS_GEN_II(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_II)
Jeff Garzike4e7b892006-01-31 12:18:41 -0500360#define IS_GEN_IIE(hpriv) ((hpriv)->hp_flags & MV_HP_GEN_IIE)
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100361#define HAS_PCI(host) (!((host)->ports[0]->flags & MV_FLAG_SOC))
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500362
Lennert Buytenhek15a32632008-03-27 14:51:39 -0400363#define WINDOW_CTRL(i) (0x20030 + ((i) << 4))
364#define WINDOW_BASE(i) (0x20034 + ((i) << 4))
365
Jeff Garzik095fec82005-11-12 09:50:49 -0500366enum {
Jeff Garzikbaf14aa2007-10-09 13:51:57 -0400367 /* DMA boundary 0xffff is required by the s/g splitting
368 * we need on /length/ in mv_fill-sg().
369 */
370 MV_DMA_BOUNDARY = 0xffffU,
Jeff Garzik095fec82005-11-12 09:50:49 -0500371
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400372 /* mask of register bits containing lower 32 bits
373 * of EDMA request queue DMA address
374 */
Jeff Garzik095fec82005-11-12 09:50:49 -0500375 EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
376
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400377 /* ditto, for response queue */
Jeff Garzik095fec82005-11-12 09:50:49 -0500378 EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
379};
380
Jeff Garzik522479f2005-11-12 22:14:02 -0500381enum chip_type {
382 chip_504x,
383 chip_508x,
384 chip_5080,
385 chip_604x,
386 chip_608x,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500387 chip_6042,
388 chip_7042,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500389 chip_soc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500390};
391
Brett Russ31961942005-09-30 01:36:00 -0400392/* Command ReQuest Block: 32B */
393struct mv_crqb {
Mark Lorde1469872006-05-22 19:02:03 -0400394 __le32 sg_addr;
395 __le32 sg_addr_hi;
396 __le16 ctrl_flags;
397 __le16 ata_cmd[11];
Brett Russ31961942005-09-30 01:36:00 -0400398};
399
Jeff Garzike4e7b892006-01-31 12:18:41 -0500400struct mv_crqb_iie {
Mark Lorde1469872006-05-22 19:02:03 -0400401 __le32 addr;
402 __le32 addr_hi;
403 __le32 flags;
404 __le32 len;
405 __le32 ata_cmd[4];
Jeff Garzike4e7b892006-01-31 12:18:41 -0500406};
407
Brett Russ31961942005-09-30 01:36:00 -0400408/* Command ResPonse Block: 8B */
409struct mv_crpb {
Mark Lorde1469872006-05-22 19:02:03 -0400410 __le16 id;
411 __le16 flags;
412 __le32 tmstmp;
Brett Russ31961942005-09-30 01:36:00 -0400413};
414
415/* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
416struct mv_sg {
Mark Lorde1469872006-05-22 19:02:03 -0400417 __le32 addr;
418 __le32 flags_size;
419 __le32 addr_hi;
420 __le32 reserved;
Brett Russ20f733e2005-09-01 18:26:17 -0400421};
422
423struct mv_port_priv {
Brett Russ31961942005-09-30 01:36:00 -0400424 struct mv_crqb *crqb;
425 dma_addr_t crqb_dma;
426 struct mv_crpb *crpb;
427 dma_addr_t crpb_dma;
Mark Lordeb73d552008-01-29 13:24:00 -0500428 struct mv_sg *sg_tbl[MV_MAX_Q_DEPTH];
429 dma_addr_t sg_tbl_dma[MV_MAX_Q_DEPTH];
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400430
431 unsigned int req_idx;
432 unsigned int resp_idx;
433
Brett Russ31961942005-09-30 01:36:00 -0400434 u32 pp_flags;
Brett Russ20f733e2005-09-01 18:26:17 -0400435};
436
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500437struct mv_port_signal {
438 u32 amps;
439 u32 pre;
440};
441
Mark Lord02a121d2007-12-01 13:07:22 -0500442struct mv_host_priv {
443 u32 hp_flags;
444 struct mv_port_signal signal[8];
445 const struct mv_hw_ops *ops;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500446 int n_ports;
447 void __iomem *base;
448 void __iomem *main_cause_reg_addr;
449 void __iomem *main_mask_reg_addr;
Mark Lord02a121d2007-12-01 13:07:22 -0500450 u32 irq_cause_ofs;
451 u32 irq_mask_ofs;
452 u32 unmask_all_irqs;
Mark Lordda2fa9b2008-01-26 18:32:45 -0500453 /*
454 * These consistent DMA memory pools give us guaranteed
455 * alignment for hardware-accessed data structures,
456 * and less memory waste in accomplishing the alignment.
457 */
458 struct dma_pool *crqb_pool;
459 struct dma_pool *crpb_pool;
460 struct dma_pool *sg_tbl_pool;
Mark Lord02a121d2007-12-01 13:07:22 -0500461};
462
Jeff Garzik47c2b672005-11-12 21:13:17 -0500463struct mv_hw_ops {
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500464 void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
465 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500466 void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
467 void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
468 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500469 int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
470 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500471 void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100472 void (*reset_bus)(struct ata_host *host, void __iomem *mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500473};
474
Tejun Heoda3dbb12007-07-16 14:29:40 +0900475static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
476static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
477static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val);
478static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
Brett Russ31961942005-09-30 01:36:00 -0400479static int mv_port_start(struct ata_port *ap);
480static void mv_port_stop(struct ata_port *ap);
481static void mv_qc_prep(struct ata_queued_cmd *qc);
Jeff Garzike4e7b892006-01-31 12:18:41 -0500482static void mv_qc_prep_iie(struct ata_queued_cmd *qc);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900483static unsigned int mv_qc_issue(struct ata_queued_cmd *qc);
Tejun Heoa1efdab2008-03-25 12:22:50 +0900484static int mv_hardreset(struct ata_link *link, unsigned int *class,
485 unsigned long deadline);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400486static void mv_eh_freeze(struct ata_port *ap);
487static void mv_eh_thaw(struct ata_port *ap);
Mark Lordf2738272008-01-26 18:32:29 -0500488static void mv6_dev_config(struct ata_device *dev);
Brett Russ20f733e2005-09-01 18:26:17 -0400489
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500490static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
491 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500492static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
493static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
494 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500495static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
496 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500497static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100498static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500499
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500500static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
501 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500502static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
503static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
504 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500505static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
506 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500507static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500508static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
509 void __iomem *mmio);
510static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
511 void __iomem *mmio);
512static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
513 void __iomem *mmio, unsigned int n_hc);
514static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
515 void __iomem *mmio);
516static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -1100517static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio);
Mark Lorde12bef52008-03-31 19:33:56 -0400518static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500519 unsigned int port_no);
Mark Lorde12bef52008-03-31 19:33:56 -0400520static int mv_stop_edma(struct ata_port *ap);
Mark Lordb5624682008-03-31 19:34:40 -0400521static int mv_stop_edma_engine(void __iomem *port_mmio);
Mark Lorde12bef52008-03-31 19:33:56 -0400522static void mv_edma_cfg(struct ata_port *ap, int want_ncq);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500523
Mark Lorde49856d2008-04-16 14:59:07 -0400524static void mv_pmp_select(struct ata_port *ap, int pmp);
525static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
526 unsigned long deadline);
527static int mv_softreset(struct ata_link *link, unsigned int *class,
528 unsigned long deadline);
Brett Russ20f733e2005-09-01 18:26:17 -0400529
Mark Lordeb73d552008-01-29 13:24:00 -0500530/* .sg_tablesize is (MV_MAX_SG_CT / 2) in the structures below
531 * because we have to allow room for worst case splitting of
532 * PRDs for 64K boundaries in mv_fill_sg().
533 */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400534static struct scsi_host_template mv5_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900535 ATA_BASE_SHT(DRV_NAME),
Jeff Garzikbaf14aa2007-10-09 13:51:57 -0400536 .sg_tablesize = MV_MAX_SG_CT / 2,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400537 .dma_boundary = MV_DMA_BOUNDARY,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400538};
539
540static struct scsi_host_template mv6_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900541 ATA_NCQ_SHT(DRV_NAME),
Mark Lord138bfdd2008-01-26 18:33:18 -0500542 .can_queue = MV_MAX_Q_DEPTH - 1,
Jeff Garzikbaf14aa2007-10-09 13:51:57 -0400543 .sg_tablesize = MV_MAX_SG_CT / 2,
Brett Russ20f733e2005-09-01 18:26:17 -0400544 .dma_boundary = MV_DMA_BOUNDARY,
Brett Russ20f733e2005-09-01 18:26:17 -0400545};
546
Tejun Heo029cfd62008-03-25 12:22:49 +0900547static struct ata_port_operations mv5_ops = {
548 .inherits = &ata_sff_port_ops,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500549
550 .qc_prep = mv_qc_prep,
551 .qc_issue = mv_qc_issue,
552
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400553 .freeze = mv_eh_freeze,
554 .thaw = mv_eh_thaw,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900555 .hardreset = mv_hardreset,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900556 .error_handler = ata_std_error_handler, /* avoid SFF EH */
Tejun Heo029cfd62008-03-25 12:22:49 +0900557 .post_internal_cmd = ATA_OP_NULL,
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400558
Jeff Garzikc9d39132005-11-13 17:47:51 -0500559 .scr_read = mv5_scr_read,
560 .scr_write = mv5_scr_write,
561
562 .port_start = mv_port_start,
563 .port_stop = mv_port_stop,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500564};
565
Tejun Heo029cfd62008-03-25 12:22:49 +0900566static struct ata_port_operations mv6_ops = {
567 .inherits = &mv5_ops,
Mark Lorde49856d2008-04-16 14:59:07 -0400568 .qc_defer = sata_pmp_qc_defer_cmd_switch,
Mark Lordf2738272008-01-26 18:32:29 -0500569 .dev_config = mv6_dev_config,
Brett Russ20f733e2005-09-01 18:26:17 -0400570 .scr_read = mv_scr_read,
571 .scr_write = mv_scr_write,
572
Mark Lorde49856d2008-04-16 14:59:07 -0400573 .pmp_hardreset = mv_pmp_hardreset,
574 .pmp_softreset = mv_softreset,
575 .softreset = mv_softreset,
576 .error_handler = sata_pmp_error_handler,
Brett Russ20f733e2005-09-01 18:26:17 -0400577};
578
Tejun Heo029cfd62008-03-25 12:22:49 +0900579static struct ata_port_operations mv_iie_ops = {
580 .inherits = &mv6_ops,
Mark Lorde49856d2008-04-16 14:59:07 -0400581 .qc_defer = ata_std_qc_defer, /* FIS-based switching */
Tejun Heo029cfd62008-03-25 12:22:49 +0900582 .dev_config = ATA_OP_NULL,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500583 .qc_prep = mv_qc_prep_iie,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500584};
585
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100586static const struct ata_port_info mv_port_info[] = {
Brett Russ20f733e2005-09-01 18:26:17 -0400587 { /* chip_504x */
Jeff Garzikcca39742006-08-24 03:19:22 -0400588 .flags = MV_COMMON_FLAGS,
Brett Russ31961942005-09-30 01:36:00 -0400589 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400590 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500591 .port_ops = &mv5_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400592 },
593 { /* chip_508x */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400594 .flags = MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
Brett Russ31961942005-09-30 01:36:00 -0400595 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400596 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500597 .port_ops = &mv5_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400598 },
Jeff Garzik47c2b672005-11-12 21:13:17 -0500599 { /* chip_5080 */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400600 .flags = MV_COMMON_FLAGS | MV_FLAG_DUAL_HC,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500601 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400602 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500603 .port_ops = &mv5_ops,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500604 },
Brett Russ20f733e2005-09-01 18:26:17 -0400605 { /* chip_604x */
Mark Lord138bfdd2008-01-26 18:33:18 -0500606 .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
Mark Lorde49856d2008-04-16 14:59:07 -0400607 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
Mark Lord138bfdd2008-01-26 18:33:18 -0500608 ATA_FLAG_NCQ,
Brett Russ31961942005-09-30 01:36:00 -0400609 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400610 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500611 .port_ops = &mv6_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400612 },
613 { /* chip_608x */
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400614 .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
Mark Lorde49856d2008-04-16 14:59:07 -0400615 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
Mark Lord138bfdd2008-01-26 18:33:18 -0500616 ATA_FLAG_NCQ | MV_FLAG_DUAL_HC,
Brett Russ31961942005-09-30 01:36:00 -0400617 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400618 .udma_mask = ATA_UDMA6,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500619 .port_ops = &mv6_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400620 },
Jeff Garzike4e7b892006-01-31 12:18:41 -0500621 { /* chip_6042 */
Mark Lord138bfdd2008-01-26 18:33:18 -0500622 .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
Mark Lorde49856d2008-04-16 14:59:07 -0400623 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
Mark Lord138bfdd2008-01-26 18:33:18 -0500624 ATA_FLAG_NCQ,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500625 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400626 .udma_mask = ATA_UDMA6,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500627 .port_ops = &mv_iie_ops,
628 },
629 { /* chip_7042 */
Mark Lord138bfdd2008-01-26 18:33:18 -0500630 .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
Mark Lorde49856d2008-04-16 14:59:07 -0400631 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
Mark Lord138bfdd2008-01-26 18:33:18 -0500632 ATA_FLAG_NCQ,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500633 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikbf6263a2007-07-09 12:16:50 -0400634 .udma_mask = ATA_UDMA6,
Jeff Garzike4e7b892006-01-31 12:18:41 -0500635 .port_ops = &mv_iie_ops,
636 },
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500637 { /* chip_soc */
Mark Lord02c1f322008-04-16 14:58:13 -0400638 .flags = MV_COMMON_FLAGS | MV_6XXX_FLAGS |
Mark Lorde49856d2008-04-16 14:59:07 -0400639 ATA_FLAG_PMP | ATA_FLAG_ACPI_SATA |
Mark Lord02c1f322008-04-16 14:58:13 -0400640 ATA_FLAG_NCQ | MV_FLAG_SOC,
Mark Lord17c5aab2008-04-16 14:56:51 -0400641 .pio_mask = 0x1f, /* pio0-4 */
642 .udma_mask = ATA_UDMA6,
643 .port_ops = &mv_iie_ops,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500644 },
Brett Russ20f733e2005-09-01 18:26:17 -0400645};
646
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500647static const struct pci_device_id mv_pci_tbl[] = {
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400648 { PCI_VDEVICE(MARVELL, 0x5040), chip_504x },
649 { PCI_VDEVICE(MARVELL, 0x5041), chip_504x },
650 { PCI_VDEVICE(MARVELL, 0x5080), chip_5080 },
651 { PCI_VDEVICE(MARVELL, 0x5081), chip_508x },
Alan Coxcfbf7232007-07-09 14:38:41 +0100652 /* RocketRAID 1740/174x have different identifiers */
653 { PCI_VDEVICE(TTI, 0x1740), chip_508x },
654 { PCI_VDEVICE(TTI, 0x1742), chip_508x },
Brett Russ20f733e2005-09-01 18:26:17 -0400655
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400656 { PCI_VDEVICE(MARVELL, 0x6040), chip_604x },
657 { PCI_VDEVICE(MARVELL, 0x6041), chip_604x },
658 { PCI_VDEVICE(MARVELL, 0x6042), chip_6042 },
659 { PCI_VDEVICE(MARVELL, 0x6080), chip_608x },
660 { PCI_VDEVICE(MARVELL, 0x6081), chip_608x },
Jeff Garzik29179532005-11-11 08:08:03 -0500661
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400662 { PCI_VDEVICE(ADAPTEC2, 0x0241), chip_604x },
663
Florian Attenbergerd9f9c6b2007-07-02 17:09:29 +0200664 /* Adaptec 1430SA */
665 { PCI_VDEVICE(ADAPTEC2, 0x0243), chip_7042 },
666
Mark Lord02a121d2007-12-01 13:07:22 -0500667 /* Marvell 7042 support */
Morrison, Tom6a3d5862007-03-06 02:38:10 -0800668 { PCI_VDEVICE(MARVELL, 0x7042), chip_7042 },
669
Mark Lord02a121d2007-12-01 13:07:22 -0500670 /* Highpoint RocketRAID PCIe series */
671 { PCI_VDEVICE(TTI, 0x2300), chip_7042 },
672 { PCI_VDEVICE(TTI, 0x2310), chip_7042 },
673
Jeff Garzik2d2744f2006-09-28 20:21:59 -0400674 { } /* terminate list */
Brett Russ20f733e2005-09-01 18:26:17 -0400675};
676
Jeff Garzik47c2b672005-11-12 21:13:17 -0500677static const struct mv_hw_ops mv5xxx_ops = {
678 .phy_errata = mv5_phy_errata,
679 .enable_leds = mv5_enable_leds,
680 .read_preamp = mv5_read_preamp,
681 .reset_hc = mv5_reset_hc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500682 .reset_flash = mv5_reset_flash,
683 .reset_bus = mv5_reset_bus,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500684};
685
686static const struct mv_hw_ops mv6xxx_ops = {
687 .phy_errata = mv6_phy_errata,
688 .enable_leds = mv6_enable_leds,
689 .read_preamp = mv6_read_preamp,
690 .reset_hc = mv6_reset_hc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500691 .reset_flash = mv6_reset_flash,
692 .reset_bus = mv_reset_pci_bus,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500693};
694
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500695static const struct mv_hw_ops mv_soc_ops = {
696 .phy_errata = mv6_phy_errata,
697 .enable_leds = mv_soc_enable_leds,
698 .read_preamp = mv_soc_read_preamp,
699 .reset_hc = mv_soc_reset_hc,
700 .reset_flash = mv_soc_reset_flash,
701 .reset_bus = mv_soc_reset_bus,
702};
703
Brett Russ20f733e2005-09-01 18:26:17 -0400704/*
705 * Functions
706 */
707
708static inline void writelfl(unsigned long data, void __iomem *addr)
709{
710 writel(data, addr);
711 (void) readl(addr); /* flush to avoid PCI posted write */
712}
713
Jeff Garzikc9d39132005-11-13 17:47:51 -0500714static inline unsigned int mv_hc_from_port(unsigned int port)
715{
716 return port >> MV_PORT_HC_SHIFT;
717}
718
719static inline unsigned int mv_hardport_from_port(unsigned int port)
720{
721 return port & MV_PORT_MASK;
722}
723
Mark Lord1cfd19a2008-04-19 15:05:50 -0400724/*
725 * Consolidate some rather tricky bit shift calculations.
726 * This is hot-path stuff, so not a function.
727 * Simple code, with two return values, so macro rather than inline.
728 *
729 * port is the sole input, in range 0..7.
730 * shift is one output, for use with the main_cause and main_mask registers.
731 * hardport is the other output, in range 0..3
732 *
733 * Note that port and hardport may be the same variable in some cases.
734 */
735#define MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport) \
736{ \
737 shift = mv_hc_from_port(port) * HC_SHIFT; \
738 hardport = mv_hardport_from_port(port); \
739 shift += hardport * 2; \
740}
741
Mark Lord352fab72008-04-19 14:43:42 -0400742static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
743{
744 return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
745}
746
Jeff Garzikc9d39132005-11-13 17:47:51 -0500747static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
748 unsigned int port)
749{
750 return mv_hc_base(base, mv_hc_from_port(port));
751}
752
Brett Russ20f733e2005-09-01 18:26:17 -0400753static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
754{
Jeff Garzikc9d39132005-11-13 17:47:51 -0500755 return mv_hc_base_from_port(base, port) +
Jeff Garzik8b260242005-11-12 12:32:50 -0500756 MV_SATAHC_ARBTR_REG_SZ +
Jeff Garzikc9d39132005-11-13 17:47:51 -0500757 (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
Brett Russ20f733e2005-09-01 18:26:17 -0400758}
759
Mark Lorde12bef52008-03-31 19:33:56 -0400760static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
761{
762 void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
763 unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
764
765 return hc_mmio + ofs;
766}
767
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500768static inline void __iomem *mv_host_base(struct ata_host *host)
769{
770 struct mv_host_priv *hpriv = host->private_data;
771 return hpriv->base;
772}
773
Brett Russ20f733e2005-09-01 18:26:17 -0400774static inline void __iomem *mv_ap_base(struct ata_port *ap)
775{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -0500776 return mv_port_base(mv_host_base(ap->host), ap->port_no);
Brett Russ20f733e2005-09-01 18:26:17 -0400777}
778
Jeff Garzikcca39742006-08-24 03:19:22 -0400779static inline int mv_get_hc_count(unsigned long port_flags)
Brett Russ20f733e2005-09-01 18:26:17 -0400780{
Jeff Garzikcca39742006-08-24 03:19:22 -0400781 return ((port_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
Brett Russ20f733e2005-09-01 18:26:17 -0400782}
783
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400784static void mv_set_edma_ptrs(void __iomem *port_mmio,
785 struct mv_host_priv *hpriv,
786 struct mv_port_priv *pp)
787{
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400788 u32 index;
789
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400790 /*
791 * initialize request queue
792 */
Mark Lordfcfb1f72008-04-19 15:06:40 -0400793 pp->req_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
794 index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400795
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400796 WARN_ON(pp->crqb_dma & 0x3ff);
797 writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400798 writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | index,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400799 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
800
801 if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400802 writelfl((pp->crqb_dma & 0xffffffff) | index,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400803 port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
804 else
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400805 writelfl(index, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400806
807 /*
808 * initialize response queue
809 */
Mark Lordfcfb1f72008-04-19 15:06:40 -0400810 pp->resp_idx &= MV_MAX_Q_DEPTH_MASK; /* paranoia */
811 index = pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400812
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400813 WARN_ON(pp->crpb_dma & 0xff);
814 writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
815
816 if (hpriv->hp_flags & MV_HP_ERRATA_XX42A0)
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400817 writelfl((pp->crpb_dma & 0xffffffff) | index,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400818 port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
819 else
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400820 writelfl(index, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400821
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400822 writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) | index,
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400823 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400824}
825
Brett Russ05b308e2005-10-05 17:08:53 -0400826/**
827 * mv_start_dma - Enable eDMA engine
828 * @base: port base address
829 * @pp: port private data
830 *
Tejun Heobeec7db2006-02-11 19:11:13 +0900831 * Verify the local cache of the eDMA state is accurate with a
832 * WARN_ON.
Brett Russ05b308e2005-10-05 17:08:53 -0400833 *
834 * LOCKING:
835 * Inherited from caller.
836 */
Mark Lord0c589122008-01-26 18:31:16 -0500837static void mv_start_dma(struct ata_port *ap, void __iomem *port_mmio,
Mark Lord72109162008-01-26 18:31:33 -0500838 struct mv_port_priv *pp, u8 protocol)
Brett Russ31961942005-09-30 01:36:00 -0400839{
Mark Lord72109162008-01-26 18:31:33 -0500840 int want_ncq = (protocol == ATA_PROT_NCQ);
841
842 if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
843 int using_ncq = ((pp->pp_flags & MV_PP_FLAG_NCQ_EN) != 0);
844 if (want_ncq != using_ncq)
Mark Lordb5624682008-03-31 19:34:40 -0400845 mv_stop_edma(ap);
Mark Lord72109162008-01-26 18:31:33 -0500846 }
Jeff Garzikc5d3e452007-07-11 18:30:50 -0400847 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
Mark Lord0c589122008-01-26 18:31:16 -0500848 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lord352fab72008-04-19 14:43:42 -0400849 int hardport = mv_hardport_from_port(ap->port_no);
Mark Lord0c589122008-01-26 18:31:16 -0500850 void __iomem *hc_mmio = mv_hc_base_from_port(
Mark Lord352fab72008-04-19 14:43:42 -0400851 mv_host_base(ap->host), hardport);
Mark Lord0c589122008-01-26 18:31:16 -0500852 u32 hc_irq_cause, ipending;
853
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400854 /* clear EDMA event indicators, if any */
Mark Lordf630d562008-01-26 18:31:00 -0500855 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400856
Mark Lord0c589122008-01-26 18:31:16 -0500857 /* clear EDMA interrupt indicator, if any */
858 hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
Mark Lord352fab72008-04-19 14:43:42 -0400859 ipending = (DEV_IRQ | DMA_IRQ) << hardport;
Mark Lord0c589122008-01-26 18:31:16 -0500860 if (hc_irq_cause & ipending) {
861 writelfl(hc_irq_cause & ~ipending,
862 hc_mmio + HC_IRQ_CAUSE_OFS);
863 }
864
Mark Lorde12bef52008-03-31 19:33:56 -0400865 mv_edma_cfg(ap, want_ncq);
Mark Lord0c589122008-01-26 18:31:16 -0500866
867 /* clear FIS IRQ Cause */
868 writelfl(0, port_mmio + SATA_FIS_IRQ_CAUSE_OFS);
869
Mark Lordf630d562008-01-26 18:31:00 -0500870 mv_set_edma_ptrs(port_mmio, hpriv, pp);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -0400871
Mark Lordf630d562008-01-26 18:31:00 -0500872 writelfl(EDMA_EN, port_mmio + EDMA_CMD_OFS);
Brett Russafb0edd2005-10-05 17:08:42 -0400873 pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
874 }
Brett Russ31961942005-09-30 01:36:00 -0400875}
876
Brett Russ05b308e2005-10-05 17:08:53 -0400877/**
Mark Lorde12bef52008-03-31 19:33:56 -0400878 * mv_stop_edma_engine - Disable eDMA engine
Mark Lordb5624682008-03-31 19:34:40 -0400879 * @port_mmio: io base address
Brett Russ05b308e2005-10-05 17:08:53 -0400880 *
881 * LOCKING:
882 * Inherited from caller.
883 */
Mark Lordb5624682008-03-31 19:34:40 -0400884static int mv_stop_edma_engine(void __iomem *port_mmio)
Brett Russ31961942005-09-30 01:36:00 -0400885{
Mark Lordb5624682008-03-31 19:34:40 -0400886 int i;
Brett Russ31961942005-09-30 01:36:00 -0400887
Mark Lordb5624682008-03-31 19:34:40 -0400888 /* Disable eDMA. The disable bit auto clears. */
889 writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
Jeff Garzik8b260242005-11-12 12:32:50 -0500890
Mark Lordb5624682008-03-31 19:34:40 -0400891 /* Wait for the chip to confirm eDMA is off. */
892 for (i = 10000; i > 0; i--) {
893 u32 reg = readl(port_mmio + EDMA_CMD_OFS);
Jeff Garzik4537deb2007-07-12 14:30:19 -0400894 if (!(reg & EDMA_EN))
Mark Lordb5624682008-03-31 19:34:40 -0400895 return 0;
896 udelay(10);
Brett Russ31961942005-09-30 01:36:00 -0400897 }
Mark Lordb5624682008-03-31 19:34:40 -0400898 return -EIO;
Brett Russ31961942005-09-30 01:36:00 -0400899}
900
Mark Lorde12bef52008-03-31 19:33:56 -0400901static int mv_stop_edma(struct ata_port *ap)
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400902{
Mark Lordb5624682008-03-31 19:34:40 -0400903 void __iomem *port_mmio = mv_ap_base(ap);
904 struct mv_port_priv *pp = ap->private_data;
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400905
Mark Lordb5624682008-03-31 19:34:40 -0400906 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN))
907 return 0;
908 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
909 if (mv_stop_edma_engine(port_mmio)) {
910 ata_port_printk(ap, KERN_ERR, "Unable to stop eDMA\n");
911 return -EIO;
912 }
913 return 0;
Jeff Garzik0ea9e172007-07-13 17:06:45 -0400914}
915
Jeff Garzik8a70f8d2005-10-05 17:19:47 -0400916#ifdef ATA_DEBUG
Brett Russ31961942005-09-30 01:36:00 -0400917static void mv_dump_mem(void __iomem *start, unsigned bytes)
918{
Brett Russ31961942005-09-30 01:36:00 -0400919 int b, w;
920 for (b = 0; b < bytes; ) {
921 DPRINTK("%p: ", start + b);
922 for (w = 0; b < bytes && w < 4; w++) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400923 printk("%08x ", readl(start + b));
Brett Russ31961942005-09-30 01:36:00 -0400924 b += sizeof(u32);
925 }
926 printk("\n");
927 }
Brett Russ31961942005-09-30 01:36:00 -0400928}
Jeff Garzik8a70f8d2005-10-05 17:19:47 -0400929#endif
930
Brett Russ31961942005-09-30 01:36:00 -0400931static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
932{
933#ifdef ATA_DEBUG
934 int b, w;
935 u32 dw;
936 for (b = 0; b < bytes; ) {
937 DPRINTK("%02x: ", b);
938 for (w = 0; b < bytes && w < 4; w++) {
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400939 (void) pci_read_config_dword(pdev, b, &dw);
940 printk("%08x ", dw);
Brett Russ31961942005-09-30 01:36:00 -0400941 b += sizeof(u32);
942 }
943 printk("\n");
944 }
945#endif
946}
947static void mv_dump_all_regs(void __iomem *mmio_base, int port,
948 struct pci_dev *pdev)
949{
950#ifdef ATA_DEBUG
Jeff Garzik8b260242005-11-12 12:32:50 -0500951 void __iomem *hc_base = mv_hc_base(mmio_base,
Brett Russ31961942005-09-30 01:36:00 -0400952 port >> MV_PORT_HC_SHIFT);
953 void __iomem *port_base;
954 int start_port, num_ports, p, start_hc, num_hcs, hc;
955
956 if (0 > port) {
957 start_hc = start_port = 0;
958 num_ports = 8; /* shld be benign for 4 port devs */
959 num_hcs = 2;
960 } else {
961 start_hc = port >> MV_PORT_HC_SHIFT;
962 start_port = port;
963 num_ports = num_hcs = 1;
964 }
Jeff Garzik8b260242005-11-12 12:32:50 -0500965 DPRINTK("All registers for port(s) %u-%u:\n", start_port,
Brett Russ31961942005-09-30 01:36:00 -0400966 num_ports > 1 ? num_ports - 1 : start_port);
967
968 if (NULL != pdev) {
969 DPRINTK("PCI config space regs:\n");
970 mv_dump_pci_cfg(pdev, 0x68);
971 }
972 DPRINTK("PCI regs:\n");
973 mv_dump_mem(mmio_base+0xc00, 0x3c);
974 mv_dump_mem(mmio_base+0xd00, 0x34);
975 mv_dump_mem(mmio_base+0xf00, 0x4);
976 mv_dump_mem(mmio_base+0x1d00, 0x6c);
977 for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
Dan Alonid220c372006-04-10 23:20:22 -0700978 hc_base = mv_hc_base(mmio_base, hc);
Brett Russ31961942005-09-30 01:36:00 -0400979 DPRINTK("HC regs (HC %i):\n", hc);
980 mv_dump_mem(hc_base, 0x1c);
981 }
982 for (p = start_port; p < start_port + num_ports; p++) {
983 port_base = mv_port_base(mmio_base, p);
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400984 DPRINTK("EDMA regs (port %i):\n", p);
Brett Russ31961942005-09-30 01:36:00 -0400985 mv_dump_mem(port_base, 0x54);
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400986 DPRINTK("SATA regs (port %i):\n", p);
Brett Russ31961942005-09-30 01:36:00 -0400987 mv_dump_mem(port_base+0x300, 0x60);
988 }
989#endif
990}
991
Brett Russ20f733e2005-09-01 18:26:17 -0400992static unsigned int mv_scr_offset(unsigned int sc_reg_in)
993{
994 unsigned int ofs;
995
996 switch (sc_reg_in) {
997 case SCR_STATUS:
998 case SCR_CONTROL:
999 case SCR_ERROR:
1000 ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
1001 break;
1002 case SCR_ACTIVE:
1003 ofs = SATA_ACTIVE_OFS; /* active is not with the others */
1004 break;
1005 default:
1006 ofs = 0xffffffffU;
1007 break;
1008 }
1009 return ofs;
1010}
1011
Tejun Heoda3dbb12007-07-16 14:29:40 +09001012static int mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
Brett Russ20f733e2005-09-01 18:26:17 -04001013{
1014 unsigned int ofs = mv_scr_offset(sc_reg_in);
1015
Tejun Heoda3dbb12007-07-16 14:29:40 +09001016 if (ofs != 0xffffffffU) {
1017 *val = readl(mv_ap_base(ap) + ofs);
1018 return 0;
1019 } else
1020 return -EINVAL;
Brett Russ20f733e2005-09-01 18:26:17 -04001021}
1022
Tejun Heoda3dbb12007-07-16 14:29:40 +09001023static int mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
Brett Russ20f733e2005-09-01 18:26:17 -04001024{
1025 unsigned int ofs = mv_scr_offset(sc_reg_in);
1026
Tejun Heoda3dbb12007-07-16 14:29:40 +09001027 if (ofs != 0xffffffffU) {
Brett Russ20f733e2005-09-01 18:26:17 -04001028 writelfl(val, mv_ap_base(ap) + ofs);
Tejun Heoda3dbb12007-07-16 14:29:40 +09001029 return 0;
1030 } else
1031 return -EINVAL;
Brett Russ20f733e2005-09-01 18:26:17 -04001032}
1033
Mark Lordf2738272008-01-26 18:32:29 -05001034static void mv6_dev_config(struct ata_device *adev)
1035{
1036 /*
Mark Lorde49856d2008-04-16 14:59:07 -04001037 * Deal with Gen-II ("mv6") hardware quirks/restrictions:
1038 *
1039 * Gen-II does not support NCQ over a port multiplier
1040 * (no FIS-based switching).
1041 *
Mark Lordf2738272008-01-26 18:32:29 -05001042 * We don't have hob_nsect when doing NCQ commands on Gen-II.
1043 * See mv_qc_prep() for more info.
1044 */
Mark Lorde49856d2008-04-16 14:59:07 -04001045 if (adev->flags & ATA_DFLAG_NCQ) {
Mark Lord352fab72008-04-19 14:43:42 -04001046 if (sata_pmp_attached(adev->link->ap)) {
Mark Lorde49856d2008-04-16 14:59:07 -04001047 adev->flags &= ~ATA_DFLAG_NCQ;
Mark Lord352fab72008-04-19 14:43:42 -04001048 ata_dev_printk(adev, KERN_INFO,
1049 "NCQ disabled for command-based switching\n");
1050 } else if (adev->max_sectors > GEN_II_NCQ_MAX_SECTORS) {
1051 adev->max_sectors = GEN_II_NCQ_MAX_SECTORS;
1052 ata_dev_printk(adev, KERN_INFO,
1053 "max_sectors limited to %u for NCQ\n",
1054 adev->max_sectors);
1055 }
Mark Lorde49856d2008-04-16 14:59:07 -04001056 }
Mark Lordf2738272008-01-26 18:32:29 -05001057}
1058
Mark Lorde49856d2008-04-16 14:59:07 -04001059static void mv_config_fbs(void __iomem *port_mmio, int enable_fbs)
1060{
1061 u32 old_fcfg, new_fcfg, old_ltmode, new_ltmode;
1062 /*
1063 * Various bit settings required for operation
1064 * in FIS-based switching (fbs) mode on GenIIe:
1065 */
1066 old_fcfg = readl(port_mmio + FIS_CFG_OFS);
1067 old_ltmode = readl(port_mmio + LTMODE_OFS);
1068 if (enable_fbs) {
1069 new_fcfg = old_fcfg | FIS_CFG_SINGLE_SYNC;
1070 new_ltmode = old_ltmode | LTMODE_BIT8;
1071 } else { /* disable fbs */
1072 new_fcfg = old_fcfg & ~FIS_CFG_SINGLE_SYNC;
1073 new_ltmode = old_ltmode & ~LTMODE_BIT8;
1074 }
1075 if (new_fcfg != old_fcfg)
1076 writelfl(new_fcfg, port_mmio + FIS_CFG_OFS);
1077 if (new_ltmode != old_ltmode)
1078 writelfl(new_ltmode, port_mmio + LTMODE_OFS);
Mark Lord0c589122008-01-26 18:31:16 -05001079}
Jeff Garzike4e7b892006-01-31 12:18:41 -05001080
Mark Lorde12bef52008-03-31 19:33:56 -04001081static void mv_edma_cfg(struct ata_port *ap, int want_ncq)
Jeff Garzike4e7b892006-01-31 12:18:41 -05001082{
1083 u32 cfg;
Mark Lorde12bef52008-03-31 19:33:56 -04001084 struct mv_port_priv *pp = ap->private_data;
1085 struct mv_host_priv *hpriv = ap->host->private_data;
1086 void __iomem *port_mmio = mv_ap_base(ap);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001087
1088 /* set up non-NCQ EDMA configuration */
1089 cfg = EDMA_CFG_Q_DEPTH; /* always 0x1f for *all* chips */
1090
1091 if (IS_GEN_I(hpriv))
1092 cfg |= (1 << 8); /* enab config burst size mask */
1093
1094 else if (IS_GEN_II(hpriv))
1095 cfg |= EDMA_CFG_RD_BRST_EXT | EDMA_CFG_WR_BUFF_LEN;
1096
1097 else if (IS_GEN_IIE(hpriv)) {
Jeff Garzike728eab2007-02-25 02:53:41 -05001098 cfg |= (1 << 23); /* do not mask PM field in rx'd FIS */
1099 cfg |= (1 << 22); /* enab 4-entry host queue cache */
Jeff Garzike4e7b892006-01-31 12:18:41 -05001100 cfg |= (1 << 18); /* enab early completion */
Jeff Garzike728eab2007-02-25 02:53:41 -05001101 cfg |= (1 << 17); /* enab cut-through (dis stor&forwrd) */
Mark Lorde49856d2008-04-16 14:59:07 -04001102
1103 if (want_ncq && sata_pmp_attached(ap)) {
1104 cfg |= EDMA_CFG_EDMA_FBS; /* FIS-based switching */
1105 mv_config_fbs(port_mmio, 1);
1106 } else {
1107 mv_config_fbs(port_mmio, 0);
1108 }
Jeff Garzike4e7b892006-01-31 12:18:41 -05001109 }
1110
Mark Lord72109162008-01-26 18:31:33 -05001111 if (want_ncq) {
1112 cfg |= EDMA_CFG_NCQ;
1113 pp->pp_flags |= MV_PP_FLAG_NCQ_EN;
1114 } else
1115 pp->pp_flags &= ~MV_PP_FLAG_NCQ_EN;
1116
Jeff Garzike4e7b892006-01-31 12:18:41 -05001117 writelfl(cfg, port_mmio + EDMA_CFG_OFS);
1118}
1119
Mark Lordda2fa9b2008-01-26 18:32:45 -05001120static void mv_port_free_dma_mem(struct ata_port *ap)
1121{
1122 struct mv_host_priv *hpriv = ap->host->private_data;
1123 struct mv_port_priv *pp = ap->private_data;
Mark Lordeb73d552008-01-29 13:24:00 -05001124 int tag;
Mark Lordda2fa9b2008-01-26 18:32:45 -05001125
1126 if (pp->crqb) {
1127 dma_pool_free(hpriv->crqb_pool, pp->crqb, pp->crqb_dma);
1128 pp->crqb = NULL;
1129 }
1130 if (pp->crpb) {
1131 dma_pool_free(hpriv->crpb_pool, pp->crpb, pp->crpb_dma);
1132 pp->crpb = NULL;
1133 }
Mark Lordeb73d552008-01-29 13:24:00 -05001134 /*
1135 * For GEN_I, there's no NCQ, so we have only a single sg_tbl.
1136 * For later hardware, we have one unique sg_tbl per NCQ tag.
1137 */
1138 for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1139 if (pp->sg_tbl[tag]) {
1140 if (tag == 0 || !IS_GEN_I(hpriv))
1141 dma_pool_free(hpriv->sg_tbl_pool,
1142 pp->sg_tbl[tag],
1143 pp->sg_tbl_dma[tag]);
1144 pp->sg_tbl[tag] = NULL;
1145 }
Mark Lordda2fa9b2008-01-26 18:32:45 -05001146 }
1147}
1148
Brett Russ05b308e2005-10-05 17:08:53 -04001149/**
1150 * mv_port_start - Port specific init/start routine.
1151 * @ap: ATA channel to manipulate
1152 *
1153 * Allocate and point to DMA memory, init port private memory,
1154 * zero indices.
1155 *
1156 * LOCKING:
1157 * Inherited from caller.
1158 */
Brett Russ31961942005-09-30 01:36:00 -04001159static int mv_port_start(struct ata_port *ap)
1160{
Jeff Garzikcca39742006-08-24 03:19:22 -04001161 struct device *dev = ap->host->dev;
1162 struct mv_host_priv *hpriv = ap->host->private_data;
Brett Russ31961942005-09-30 01:36:00 -04001163 struct mv_port_priv *pp;
James Bottomleydde20202008-02-19 11:36:56 +01001164 int tag;
Brett Russ31961942005-09-30 01:36:00 -04001165
Tejun Heo24dc5f32007-01-20 16:00:28 +09001166 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
Jeff Garzik6037d6b2005-11-04 22:08:00 -05001167 if (!pp)
Tejun Heo24dc5f32007-01-20 16:00:28 +09001168 return -ENOMEM;
Mark Lordda2fa9b2008-01-26 18:32:45 -05001169 ap->private_data = pp;
Brett Russ31961942005-09-30 01:36:00 -04001170
Mark Lordda2fa9b2008-01-26 18:32:45 -05001171 pp->crqb = dma_pool_alloc(hpriv->crqb_pool, GFP_KERNEL, &pp->crqb_dma);
1172 if (!pp->crqb)
1173 return -ENOMEM;
1174 memset(pp->crqb, 0, MV_CRQB_Q_SZ);
Brett Russ31961942005-09-30 01:36:00 -04001175
Mark Lordda2fa9b2008-01-26 18:32:45 -05001176 pp->crpb = dma_pool_alloc(hpriv->crpb_pool, GFP_KERNEL, &pp->crpb_dma);
1177 if (!pp->crpb)
1178 goto out_port_free_dma_mem;
1179 memset(pp->crpb, 0, MV_CRPB_Q_SZ);
Brett Russ31961942005-09-30 01:36:00 -04001180
Mark Lordeb73d552008-01-29 13:24:00 -05001181 /*
1182 * For GEN_I, there's no NCQ, so we only allocate a single sg_tbl.
1183 * For later hardware, we need one unique sg_tbl per NCQ tag.
1184 */
1185 for (tag = 0; tag < MV_MAX_Q_DEPTH; ++tag) {
1186 if (tag == 0 || !IS_GEN_I(hpriv)) {
1187 pp->sg_tbl[tag] = dma_pool_alloc(hpriv->sg_tbl_pool,
1188 GFP_KERNEL, &pp->sg_tbl_dma[tag]);
1189 if (!pp->sg_tbl[tag])
1190 goto out_port_free_dma_mem;
1191 } else {
1192 pp->sg_tbl[tag] = pp->sg_tbl[0];
1193 pp->sg_tbl_dma[tag] = pp->sg_tbl_dma[0];
1194 }
1195 }
Brett Russ31961942005-09-30 01:36:00 -04001196 return 0;
Mark Lordda2fa9b2008-01-26 18:32:45 -05001197
1198out_port_free_dma_mem:
1199 mv_port_free_dma_mem(ap);
1200 return -ENOMEM;
Brett Russ31961942005-09-30 01:36:00 -04001201}
1202
Brett Russ05b308e2005-10-05 17:08:53 -04001203/**
1204 * mv_port_stop - Port specific cleanup/stop routine.
1205 * @ap: ATA channel to manipulate
1206 *
1207 * Stop DMA, cleanup port memory.
1208 *
1209 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001210 * This routine uses the host lock to protect the DMA stop.
Brett Russ05b308e2005-10-05 17:08:53 -04001211 */
Brett Russ31961942005-09-30 01:36:00 -04001212static void mv_port_stop(struct ata_port *ap)
1213{
Mark Lorde12bef52008-03-31 19:33:56 -04001214 mv_stop_edma(ap);
Mark Lordda2fa9b2008-01-26 18:32:45 -05001215 mv_port_free_dma_mem(ap);
Brett Russ31961942005-09-30 01:36:00 -04001216}
1217
Brett Russ05b308e2005-10-05 17:08:53 -04001218/**
1219 * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
1220 * @qc: queued command whose SG list to source from
1221 *
1222 * Populate the SG list and mark the last entry.
1223 *
1224 * LOCKING:
1225 * Inherited from caller.
1226 */
Jeff Garzik6c087722007-10-12 00:16:23 -04001227static void mv_fill_sg(struct ata_queued_cmd *qc)
Brett Russ31961942005-09-30 01:36:00 -04001228{
1229 struct mv_port_priv *pp = qc->ap->private_data;
Jeff Garzik972c26b2005-10-18 22:14:54 -04001230 struct scatterlist *sg;
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04001231 struct mv_sg *mv_sg, *last_sg = NULL;
Tejun Heoff2aeb12007-12-05 16:43:11 +09001232 unsigned int si;
Brett Russ31961942005-09-30 01:36:00 -04001233
Mark Lordeb73d552008-01-29 13:24:00 -05001234 mv_sg = pp->sg_tbl[qc->tag];
Tejun Heoff2aeb12007-12-05 16:43:11 +09001235 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Jeff Garzikd88184f2007-02-26 01:26:06 -05001236 dma_addr_t addr = sg_dma_address(sg);
1237 u32 sg_len = sg_dma_len(sg);
Brett Russ31961942005-09-30 01:36:00 -04001238
Olof Johansson4007b492007-10-02 20:45:27 -05001239 while (sg_len) {
1240 u32 offset = addr & 0xffff;
1241 u32 len = sg_len;
Brett Russ31961942005-09-30 01:36:00 -04001242
Olof Johansson4007b492007-10-02 20:45:27 -05001243 if ((offset + sg_len > 0x10000))
1244 len = 0x10000 - offset;
Jeff Garzik972c26b2005-10-18 22:14:54 -04001245
Olof Johansson4007b492007-10-02 20:45:27 -05001246 mv_sg->addr = cpu_to_le32(addr & 0xffffffff);
1247 mv_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
Jeff Garzik6c087722007-10-12 00:16:23 -04001248 mv_sg->flags_size = cpu_to_le32(len & 0xffff);
Olof Johansson4007b492007-10-02 20:45:27 -05001249
1250 sg_len -= len;
1251 addr += len;
1252
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04001253 last_sg = mv_sg;
Olof Johansson4007b492007-10-02 20:45:27 -05001254 mv_sg++;
Olof Johansson4007b492007-10-02 20:45:27 -05001255 }
Brett Russ31961942005-09-30 01:36:00 -04001256 }
Jeff Garzik3be6cbd2007-10-18 16:21:18 -04001257
1258 if (likely(last_sg))
1259 last_sg->flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
Brett Russ31961942005-09-30 01:36:00 -04001260}
1261
Jeff Garzik5796d1c2007-10-26 00:03:37 -04001262static void mv_crqb_pack_cmd(__le16 *cmdw, u8 data, u8 addr, unsigned last)
Brett Russ31961942005-09-30 01:36:00 -04001263{
Mark Lord559eeda2006-05-19 16:40:15 -04001264 u16 tmp = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
Brett Russ31961942005-09-30 01:36:00 -04001265 (last ? CRQB_CMD_LAST : 0);
Mark Lord559eeda2006-05-19 16:40:15 -04001266 *cmdw = cpu_to_le16(tmp);
Brett Russ31961942005-09-30 01:36:00 -04001267}
1268
Brett Russ05b308e2005-10-05 17:08:53 -04001269/**
1270 * mv_qc_prep - Host specific command preparation.
1271 * @qc: queued command to prepare
1272 *
1273 * This routine simply redirects to the general purpose routine
1274 * if command is not DMA. Else, it handles prep of the CRQB
1275 * (command request block), does some sanity checking, and calls
1276 * the SG load routine.
1277 *
1278 * LOCKING:
1279 * Inherited from caller.
1280 */
Brett Russ31961942005-09-30 01:36:00 -04001281static void mv_qc_prep(struct ata_queued_cmd *qc)
1282{
1283 struct ata_port *ap = qc->ap;
1284 struct mv_port_priv *pp = ap->private_data;
Mark Lorde1469872006-05-22 19:02:03 -04001285 __le16 *cw;
Brett Russ31961942005-09-30 01:36:00 -04001286 struct ata_taskfile *tf;
1287 u16 flags = 0;
Mark Lorda6432432006-05-19 16:36:36 -04001288 unsigned in_index;
Brett Russ31961942005-09-30 01:36:00 -04001289
Mark Lord138bfdd2008-01-26 18:33:18 -05001290 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1291 (qc->tf.protocol != ATA_PROT_NCQ))
Brett Russ31961942005-09-30 01:36:00 -04001292 return;
Brett Russ20f733e2005-09-01 18:26:17 -04001293
Brett Russ31961942005-09-30 01:36:00 -04001294 /* Fill in command request block
1295 */
Jeff Garzike4e7b892006-01-31 12:18:41 -05001296 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
Brett Russ31961942005-09-30 01:36:00 -04001297 flags |= CRQB_FLAG_READ;
Tejun Heobeec7db2006-02-11 19:11:13 +09001298 WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
Brett Russ31961942005-09-30 01:36:00 -04001299 flags |= qc->tag << CRQB_TAG_SHIFT;
Mark Lorde49856d2008-04-16 14:59:07 -04001300 flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
Brett Russ31961942005-09-30 01:36:00 -04001301
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001302 /* get current queue index from software */
Mark Lordfcfb1f72008-04-19 15:06:40 -04001303 in_index = pp->req_idx;
Brett Russ31961942005-09-30 01:36:00 -04001304
Mark Lorda6432432006-05-19 16:36:36 -04001305 pp->crqb[in_index].sg_addr =
Mark Lordeb73d552008-01-29 13:24:00 -05001306 cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
Mark Lorda6432432006-05-19 16:36:36 -04001307 pp->crqb[in_index].sg_addr_hi =
Mark Lordeb73d552008-01-29 13:24:00 -05001308 cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
Mark Lorda6432432006-05-19 16:36:36 -04001309 pp->crqb[in_index].ctrl_flags = cpu_to_le16(flags);
1310
1311 cw = &pp->crqb[in_index].ata_cmd[0];
Brett Russ31961942005-09-30 01:36:00 -04001312 tf = &qc->tf;
1313
1314 /* Sadly, the CRQB cannot accomodate all registers--there are
1315 * only 11 bytes...so we must pick and choose required
1316 * registers based on the command. So, we drop feature and
1317 * hob_feature for [RW] DMA commands, but they are needed for
1318 * NCQ. NCQ will drop hob_nsect.
1319 */
1320 switch (tf->command) {
1321 case ATA_CMD_READ:
1322 case ATA_CMD_READ_EXT:
1323 case ATA_CMD_WRITE:
1324 case ATA_CMD_WRITE_EXT:
Jens Axboec15d85c2006-02-15 15:59:25 +01001325 case ATA_CMD_WRITE_FUA_EXT:
Brett Russ31961942005-09-30 01:36:00 -04001326 mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1327 break;
Brett Russ31961942005-09-30 01:36:00 -04001328 case ATA_CMD_FPDMA_READ:
1329 case ATA_CMD_FPDMA_WRITE:
Jeff Garzik8b260242005-11-12 12:32:50 -05001330 mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
Brett Russ31961942005-09-30 01:36:00 -04001331 mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1332 break;
Brett Russ31961942005-09-30 01:36:00 -04001333 default:
1334 /* The only other commands EDMA supports in non-queued and
1335 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1336 * of which are defined/used by Linux. If we get here, this
1337 * driver needs work.
1338 *
1339 * FIXME: modify libata to give qc_prep a return value and
1340 * return error here.
1341 */
1342 BUG_ON(tf->command);
1343 break;
1344 }
1345 mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1346 mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1347 mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1348 mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1349 mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1350 mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1351 mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1352 mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1353 mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
1354
Jeff Garzike4e7b892006-01-31 12:18:41 -05001355 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
Brett Russ31961942005-09-30 01:36:00 -04001356 return;
Jeff Garzike4e7b892006-01-31 12:18:41 -05001357 mv_fill_sg(qc);
1358}
1359
1360/**
1361 * mv_qc_prep_iie - Host specific command preparation.
1362 * @qc: queued command to prepare
1363 *
1364 * This routine simply redirects to the general purpose routine
1365 * if command is not DMA. Else, it handles prep of the CRQB
1366 * (command request block), does some sanity checking, and calls
1367 * the SG load routine.
1368 *
1369 * LOCKING:
1370 * Inherited from caller.
1371 */
1372static void mv_qc_prep_iie(struct ata_queued_cmd *qc)
1373{
1374 struct ata_port *ap = qc->ap;
1375 struct mv_port_priv *pp = ap->private_data;
1376 struct mv_crqb_iie *crqb;
1377 struct ata_taskfile *tf;
Mark Lorda6432432006-05-19 16:36:36 -04001378 unsigned in_index;
Jeff Garzike4e7b892006-01-31 12:18:41 -05001379 u32 flags = 0;
1380
Mark Lord138bfdd2008-01-26 18:33:18 -05001381 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1382 (qc->tf.protocol != ATA_PROT_NCQ))
Jeff Garzike4e7b892006-01-31 12:18:41 -05001383 return;
1384
Mark Lorde12bef52008-03-31 19:33:56 -04001385 /* Fill in Gen IIE command request block */
Jeff Garzike4e7b892006-01-31 12:18:41 -05001386 if (!(qc->tf.flags & ATA_TFLAG_WRITE))
1387 flags |= CRQB_FLAG_READ;
1388
Tejun Heobeec7db2006-02-11 19:11:13 +09001389 WARN_ON(MV_MAX_Q_DEPTH <= qc->tag);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001390 flags |= qc->tag << CRQB_TAG_SHIFT;
Mark Lord8c0aeb42008-01-26 18:31:48 -05001391 flags |= qc->tag << CRQB_HOSTQ_SHIFT;
Mark Lorde49856d2008-04-16 14:59:07 -04001392 flags |= (qc->dev->link->pmp & 0xf) << CRQB_PMP_SHIFT;
Jeff Garzike4e7b892006-01-31 12:18:41 -05001393
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001394 /* get current queue index from software */
Mark Lordfcfb1f72008-04-19 15:06:40 -04001395 in_index = pp->req_idx;
Mark Lorda6432432006-05-19 16:36:36 -04001396
1397 crqb = (struct mv_crqb_iie *) &pp->crqb[in_index];
Mark Lordeb73d552008-01-29 13:24:00 -05001398 crqb->addr = cpu_to_le32(pp->sg_tbl_dma[qc->tag] & 0xffffffff);
1399 crqb->addr_hi = cpu_to_le32((pp->sg_tbl_dma[qc->tag] >> 16) >> 16);
Jeff Garzike4e7b892006-01-31 12:18:41 -05001400 crqb->flags = cpu_to_le32(flags);
1401
1402 tf = &qc->tf;
1403 crqb->ata_cmd[0] = cpu_to_le32(
1404 (tf->command << 16) |
1405 (tf->feature << 24)
1406 );
1407 crqb->ata_cmd[1] = cpu_to_le32(
1408 (tf->lbal << 0) |
1409 (tf->lbam << 8) |
1410 (tf->lbah << 16) |
1411 (tf->device << 24)
1412 );
1413 crqb->ata_cmd[2] = cpu_to_le32(
1414 (tf->hob_lbal << 0) |
1415 (tf->hob_lbam << 8) |
1416 (tf->hob_lbah << 16) |
1417 (tf->hob_feature << 24)
1418 );
1419 crqb->ata_cmd[3] = cpu_to_le32(
1420 (tf->nsect << 0) |
1421 (tf->hob_nsect << 8)
1422 );
1423
1424 if (!(qc->flags & ATA_QCFLAG_DMAMAP))
1425 return;
Brett Russ31961942005-09-30 01:36:00 -04001426 mv_fill_sg(qc);
1427}
1428
Brett Russ05b308e2005-10-05 17:08:53 -04001429/**
1430 * mv_qc_issue - Initiate a command to the host
1431 * @qc: queued command to start
1432 *
1433 * This routine simply redirects to the general purpose routine
1434 * if command is not DMA. Else, it sanity checks our local
1435 * caches of the request producer/consumer indices then enables
1436 * DMA and bumps the request producer index.
1437 *
1438 * LOCKING:
1439 * Inherited from caller.
1440 */
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001441static unsigned int mv_qc_issue(struct ata_queued_cmd *qc)
Brett Russ31961942005-09-30 01:36:00 -04001442{
Jeff Garzikc5d3e452007-07-11 18:30:50 -04001443 struct ata_port *ap = qc->ap;
1444 void __iomem *port_mmio = mv_ap_base(ap);
1445 struct mv_port_priv *pp = ap->private_data;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001446 u32 in_index;
Brett Russ31961942005-09-30 01:36:00 -04001447
Mark Lord138bfdd2008-01-26 18:33:18 -05001448 if ((qc->tf.protocol != ATA_PROT_DMA) &&
1449 (qc->tf.protocol != ATA_PROT_NCQ)) {
Mark Lord17c5aab2008-04-16 14:56:51 -04001450 /*
1451 * We're about to send a non-EDMA capable command to the
Brett Russ31961942005-09-30 01:36:00 -04001452 * port. Turn off EDMA so there won't be problems accessing
1453 * shadow block, etc registers.
1454 */
Mark Lordb5624682008-03-31 19:34:40 -04001455 mv_stop_edma(ap);
Mark Lorde49856d2008-04-16 14:59:07 -04001456 mv_pmp_select(ap, qc->dev->link->pmp);
Tejun Heo9363c382008-04-07 22:47:16 +09001457 return ata_sff_qc_issue(qc);
Brett Russ31961942005-09-30 01:36:00 -04001458 }
1459
Mark Lord72109162008-01-26 18:31:33 -05001460 mv_start_dma(ap, port_mmio, pp, qc->tf.protocol);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001461
Mark Lordfcfb1f72008-04-19 15:06:40 -04001462 pp->req_idx = (pp->req_idx + 1) & MV_MAX_Q_DEPTH_MASK;
1463 in_index = pp->req_idx << EDMA_REQ_Q_PTR_SHIFT;
Brett Russ31961942005-09-30 01:36:00 -04001464
1465 /* and write the request in pointer to kick the EDMA to life */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001466 writelfl((pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK) | in_index,
1467 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
Brett Russ31961942005-09-30 01:36:00 -04001468
1469 return 0;
1470}
1471
Mark Lord8f767f82008-04-19 14:53:07 -04001472static struct ata_queued_cmd *mv_get_active_qc(struct ata_port *ap)
1473{
1474 struct mv_port_priv *pp = ap->private_data;
1475 struct ata_queued_cmd *qc;
1476
1477 if (pp->pp_flags & MV_PP_FLAG_NCQ_EN)
1478 return NULL;
1479 qc = ata_qc_from_tag(ap, ap->link.active_tag);
1480 if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
1481 qc = NULL;
1482 return qc;
1483}
1484
1485static void mv_unexpected_intr(struct ata_port *ap)
1486{
1487 struct mv_port_priv *pp = ap->private_data;
1488 struct ata_eh_info *ehi = &ap->link.eh_info;
1489 char *when = "";
1490
1491 /*
1492 * We got a device interrupt from something that
1493 * was supposed to be using EDMA or polling.
1494 */
1495 ata_ehi_clear_desc(ehi);
1496 if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
1497 when = " while EDMA enabled";
1498 } else {
1499 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, ap->link.active_tag);
1500 if (qc && (qc->tf.flags & ATA_TFLAG_POLLING))
1501 when = " while polling";
1502 }
1503 ata_ehi_push_desc(ehi, "unexpected device interrupt%s", when);
1504 ehi->err_mask |= AC_ERR_OTHER;
1505 ehi->action |= ATA_EH_RESET;
1506 ata_port_freeze(ap);
1507}
1508
Brett Russ05b308e2005-10-05 17:08:53 -04001509/**
Brett Russ05b308e2005-10-05 17:08:53 -04001510 * mv_err_intr - Handle error interrupts on the port
1511 * @ap: ATA channel to manipulate
Mark Lord8d073792008-04-19 15:07:49 -04001512 * @qc: affected command (non-NCQ), or NULL
Brett Russ05b308e2005-10-05 17:08:53 -04001513 *
Mark Lord8d073792008-04-19 15:07:49 -04001514 * Most cases require a full reset of the chip's state machine,
1515 * which also performs a COMRESET.
1516 * Also, if the port disabled DMA, update our cached copy to match.
Brett Russ05b308e2005-10-05 17:08:53 -04001517 *
1518 * LOCKING:
1519 * Inherited from caller.
1520 */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001521static void mv_err_intr(struct ata_port *ap, struct ata_queued_cmd *qc)
Brett Russ20f733e2005-09-01 18:26:17 -04001522{
Brett Russ31961942005-09-30 01:36:00 -04001523 void __iomem *port_mmio = mv_ap_base(ap);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001524 u32 edma_err_cause, eh_freeze_mask, serr = 0;
1525 struct mv_port_priv *pp = ap->private_data;
1526 struct mv_host_priv *hpriv = ap->host->private_data;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001527 unsigned int action = 0, err_mask = 0;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001528 struct ata_eh_info *ehi = &ap->link.eh_info;
Brett Russ20f733e2005-09-01 18:26:17 -04001529
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001530 ata_ehi_clear_desc(ehi);
Brett Russ20f733e2005-09-01 18:26:17 -04001531
Mark Lord8d073792008-04-19 15:07:49 -04001532 /*
1533 * Read and clear the err_cause bits. This won't actually
1534 * clear for some errors (eg. SError), but we will be doing
1535 * a hard reset in those cases regardless, which *will* clear it.
1536 */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001537 edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
Mark Lord8d073792008-04-19 15:07:49 -04001538 writelfl(~edma_err_cause, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001539
Mark Lord352fab72008-04-19 14:43:42 -04001540 ata_ehi_push_desc(ehi, "edma_err_cause=%08x", edma_err_cause);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001541
1542 /*
Mark Lord352fab72008-04-19 14:43:42 -04001543 * All generations share these EDMA error cause bits:
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001544 */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001545 if (edma_err_cause & EDMA_ERR_DEV)
1546 err_mask |= AC_ERR_DEV;
1547 if (edma_err_cause & (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
Jeff Garzik6c1153e2007-07-13 15:20:15 -04001548 EDMA_ERR_CRQB_PAR | EDMA_ERR_CRPB_PAR |
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001549 EDMA_ERR_INTRL_PAR)) {
1550 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001551 action |= ATA_EH_RESET;
Tejun Heob64bbc32007-07-16 14:29:39 +09001552 ata_ehi_push_desc(ehi, "parity error");
Brett Russafb0edd2005-10-05 17:08:42 -04001553 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001554 if (edma_err_cause & (EDMA_ERR_DEV_DCON | EDMA_ERR_DEV_CON)) {
1555 ata_ehi_hotplugged(ehi);
1556 ata_ehi_push_desc(ehi, edma_err_cause & EDMA_ERR_DEV_DCON ?
Tejun Heob64bbc32007-07-16 14:29:39 +09001557 "dev disconnect" : "dev connect");
Tejun Heocf480622008-01-24 00:05:14 +09001558 action |= ATA_EH_RESET;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001559 }
1560
Mark Lord352fab72008-04-19 14:43:42 -04001561 /*
1562 * Gen-I has a different SELF_DIS bit,
1563 * different FREEZE bits, and no SERR bit:
1564 */
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04001565 if (IS_GEN_I(hpriv)) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001566 eh_freeze_mask = EDMA_EH_FREEZE_5;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001567 if (edma_err_cause & EDMA_ERR_SELF_DIS_5) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001568 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Tejun Heob64bbc32007-07-16 14:29:39 +09001569 ata_ehi_push_desc(ehi, "EDMA self-disable");
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001570 }
1571 } else {
1572 eh_freeze_mask = EDMA_EH_FREEZE;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001573 if (edma_err_cause & EDMA_ERR_SELF_DIS) {
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001574 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Tejun Heob64bbc32007-07-16 14:29:39 +09001575 ata_ehi_push_desc(ehi, "EDMA self-disable");
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001576 }
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001577 if (edma_err_cause & EDMA_ERR_SERR) {
Mark Lord8d073792008-04-19 15:07:49 -04001578 /*
1579 * Ensure that we read our own SCR, not a pmp link SCR:
1580 */
1581 ap->ops->scr_read(ap, SCR_ERROR, &serr);
1582 /*
1583 * Don't clear SError here; leave it for libata-eh:
1584 */
1585 ata_ehi_push_desc(ehi, "SError=%08x", serr);
1586 err_mask |= AC_ERR_ATA_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001587 action |= ATA_EH_RESET;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001588 }
1589 }
Brett Russ20f733e2005-09-01 18:26:17 -04001590
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001591 if (!err_mask) {
1592 err_mask = AC_ERR_OTHER;
Tejun Heocf480622008-01-24 00:05:14 +09001593 action |= ATA_EH_RESET;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001594 }
1595
1596 ehi->serror |= serr;
1597 ehi->action |= action;
1598
1599 if (qc)
1600 qc->err_mask |= err_mask;
1601 else
1602 ehi->err_mask |= err_mask;
1603
1604 if (edma_err_cause & eh_freeze_mask)
1605 ata_port_freeze(ap);
1606 else
1607 ata_port_abort(ap);
1608}
1609
Mark Lordfcfb1f72008-04-19 15:06:40 -04001610static void mv_process_crpb_response(struct ata_port *ap,
1611 struct mv_crpb *response, unsigned int tag, int ncq_enabled)
1612{
1613 struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
1614
1615 if (qc) {
1616 u8 ata_status;
1617 u16 edma_status = le16_to_cpu(response->flags);
1618 /*
1619 * edma_status from a response queue entry:
1620 * LSB is from EDMA_ERR_IRQ_CAUSE_OFS (non-NCQ only).
1621 * MSB is saved ATA status from command completion.
1622 */
1623 if (!ncq_enabled) {
1624 u8 err_cause = edma_status & 0xff & ~EDMA_ERR_DEV;
1625 if (err_cause) {
1626 /*
1627 * Error will be seen/handled by mv_err_intr().
1628 * So do nothing at all here.
1629 */
1630 return;
1631 }
1632 }
1633 ata_status = edma_status >> CRPB_FLAG_STATUS_SHIFT;
1634 qc->err_mask |= ac_err_mask(ata_status);
1635 ata_qc_complete(qc);
1636 } else {
1637 ata_port_printk(ap, KERN_ERR, "%s: no qc for tag=%d\n",
1638 __func__, tag);
1639 }
1640}
1641
1642static void mv_process_crpb_entries(struct ata_port *ap, struct mv_port_priv *pp)
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001643{
1644 void __iomem *port_mmio = mv_ap_base(ap);
1645 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lordfcfb1f72008-04-19 15:06:40 -04001646 u32 in_index;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001647 bool work_done = false;
Mark Lordfcfb1f72008-04-19 15:06:40 -04001648 int ncq_enabled = (pp->pp_flags & MV_PP_FLAG_NCQ_EN);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001649
Mark Lordfcfb1f72008-04-19 15:06:40 -04001650 /* Get the hardware queue position index */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001651 in_index = (readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS)
1652 >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK;
1653
Mark Lordfcfb1f72008-04-19 15:06:40 -04001654 /* Process new responses from since the last time we looked */
1655 while (in_index != pp->resp_idx) {
Jeff Garzik6c1153e2007-07-13 15:20:15 -04001656 unsigned int tag;
Mark Lordfcfb1f72008-04-19 15:06:40 -04001657 struct mv_crpb *response = &pp->crpb[pp->resp_idx];
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001658
Mark Lordfcfb1f72008-04-19 15:06:40 -04001659 pp->resp_idx = (pp->resp_idx + 1) & MV_MAX_Q_DEPTH_MASK;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001660
Mark Lordfcfb1f72008-04-19 15:06:40 -04001661 if (IS_GEN_I(hpriv)) {
1662 /* 50xx: no NCQ, only one command active at a time */
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001663 tag = ap->link.active_tag;
Mark Lordfcfb1f72008-04-19 15:06:40 -04001664 } else {
1665 /* Gen II/IIE: get command tag from CRPB entry */
1666 tag = le16_to_cpu(response->id) & 0x1f;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001667 }
Mark Lordfcfb1f72008-04-19 15:06:40 -04001668 mv_process_crpb_response(ap, response, tag, ncq_enabled);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001669 work_done = true;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001670 }
1671
Mark Lord352fab72008-04-19 14:43:42 -04001672 /* Update the software queue position index in hardware */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001673 if (work_done)
1674 writelfl((pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK) |
Mark Lordfcfb1f72008-04-19 15:06:40 -04001675 (pp->resp_idx << EDMA_RSP_Q_PTR_SHIFT),
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001676 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04001677}
1678
Brett Russ05b308e2005-10-05 17:08:53 -04001679/**
1680 * mv_host_intr - Handle all interrupts on the given host controller
Jeff Garzikcca39742006-08-24 03:19:22 -04001681 * @host: host specific structure
Mark Lord8f767f82008-04-19 14:53:07 -04001682 * @main_cause: Main interrupt cause register for the chip.
Brett Russ05b308e2005-10-05 17:08:53 -04001683 *
1684 * LOCKING:
1685 * Inherited from caller.
1686 */
Mark Lorda3718c12008-04-19 15:07:18 -04001687static int mv_host_intr(struct ata_host *host, u32 main_cause)
Brett Russ20f733e2005-09-01 18:26:17 -04001688{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05001689 struct mv_host_priv *hpriv = host->private_data;
Mark Lorda3718c12008-04-19 15:07:18 -04001690 void __iomem *mmio = hpriv->base, *hc_mmio = NULL;
1691 u32 hc_irq_cause = 0;
1692 unsigned int handled = 0, port;
Brett Russ20f733e2005-09-01 18:26:17 -04001693
Mark Lorda3718c12008-04-19 15:07:18 -04001694 for (port = 0; port < hpriv->n_ports; port++) {
Jeff Garzikcca39742006-08-24 03:19:22 -04001695 struct ata_port *ap = host->ports[port];
Yinghai Lu8f71efe2008-02-07 15:06:17 -08001696 struct mv_port_priv *pp;
Mark Lorda3718c12008-04-19 15:07:18 -04001697 unsigned int shift, hardport, port_cause;
1698 /*
1699 * When we move to the second hc, flag our cached
1700 * copies of hc_mmio (and hc_irq_cause) as invalid again.
1701 */
1702 if (port == MV_PORTS_PER_HC)
1703 hc_mmio = NULL;
1704 /*
1705 * Do nothing if port is not interrupting or is disabled:
1706 */
1707 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
1708 port_cause = (main_cause >> shift) & (DONE_IRQ | ERR_IRQ);
1709 if (!port_cause || !ap || (ap->flags & ATA_FLAG_DISABLED))
Jeff Garzika2c91a82005-11-17 05:44:44 -05001710 continue;
Mark Lorda3718c12008-04-19 15:07:18 -04001711 /*
1712 * Each hc within the host has its own hc_irq_cause register.
1713 * We defer reading it until we know we need it, right now:
1714 *
1715 * FIXME later: we don't really need to read this register
1716 * (some logic changes required below if we go that way),
1717 * because it doesn't tell us anything new. But we do need
1718 * to write to it, outside the top of this loop,
1719 * to reset the interrupt triggers for next time.
1720 */
1721 if (!hc_mmio) {
1722 hc_mmio = mv_hc_base_from_port(mmio, port);
1723 hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
1724 writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
1725 handled = 1;
1726 }
Mark Lord8f767f82008-04-19 14:53:07 -04001727 /*
1728 * Process completed CRPB response(s) before other events.
1729 */
Mark Lorda3718c12008-04-19 15:07:18 -04001730 pp = ap->private_data;
Mark Lord8f767f82008-04-19 14:53:07 -04001731 if (hc_irq_cause & (DMA_IRQ << hardport)) {
1732 if (pp->pp_flags & MV_PP_FLAG_EDMA_EN)
Mark Lordfcfb1f72008-04-19 15:06:40 -04001733 mv_process_crpb_entries(ap, pp);
Mark Lord8f767f82008-04-19 14:53:07 -04001734 }
1735 /*
1736 * Handle chip-reported errors, or continue on to handle PIO.
1737 */
1738 if (unlikely(port_cause & ERR_IRQ)) {
1739 mv_err_intr(ap, mv_get_active_qc(ap));
1740 } else if (hc_irq_cause & (DEV_IRQ << hardport)) {
1741 if (!(pp->pp_flags & MV_PP_FLAG_EDMA_EN)) {
1742 struct ata_queued_cmd *qc = mv_get_active_qc(ap);
1743 if (qc) {
1744 ata_sff_host_intr(ap, qc);
1745 continue;
1746 }
1747 }
1748 mv_unexpected_intr(ap);
Brett Russ20f733e2005-09-01 18:26:17 -04001749 }
1750 }
Mark Lorda3718c12008-04-19 15:07:18 -04001751 return handled;
Brett Russ20f733e2005-09-01 18:26:17 -04001752}
1753
Mark Lorda3718c12008-04-19 15:07:18 -04001754static int mv_pci_error(struct ata_host *host, void __iomem *mmio)
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001755{
Mark Lord02a121d2007-12-01 13:07:22 -05001756 struct mv_host_priv *hpriv = host->private_data;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001757 struct ata_port *ap;
1758 struct ata_queued_cmd *qc;
1759 struct ata_eh_info *ehi;
1760 unsigned int i, err_mask, printed = 0;
1761 u32 err_cause;
1762
Mark Lord02a121d2007-12-01 13:07:22 -05001763 err_cause = readl(mmio + hpriv->irq_cause_ofs);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001764
1765 dev_printk(KERN_ERR, host->dev, "PCI ERROR; PCI IRQ cause=0x%08x\n",
1766 err_cause);
1767
1768 DPRINTK("All regs @ PCI error\n");
1769 mv_dump_all_regs(mmio, -1, to_pci_dev(host->dev));
1770
Mark Lord02a121d2007-12-01 13:07:22 -05001771 writelfl(0, mmio + hpriv->irq_cause_ofs);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001772
1773 for (i = 0; i < host->n_ports; i++) {
1774 ap = host->ports[i];
Tejun Heo936fd732007-08-06 18:36:23 +09001775 if (!ata_link_offline(&ap->link)) {
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001776 ehi = &ap->link.eh_info;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001777 ata_ehi_clear_desc(ehi);
1778 if (!printed++)
1779 ata_ehi_push_desc(ehi,
1780 "PCI err cause 0x%08x", err_cause);
1781 err_mask = AC_ERR_HOST_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09001782 ehi->action = ATA_EH_RESET;
Tejun Heo9af5c9c2007-08-06 18:36:22 +09001783 qc = ata_qc_from_tag(ap, ap->link.active_tag);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001784 if (qc)
1785 qc->err_mask |= err_mask;
1786 else
1787 ehi->err_mask |= err_mask;
1788
1789 ata_port_freeze(ap);
1790 }
1791 }
Mark Lorda3718c12008-04-19 15:07:18 -04001792 return 1; /* handled */
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001793}
1794
Brett Russ05b308e2005-10-05 17:08:53 -04001795/**
Jeff Garzikc5d3e452007-07-11 18:30:50 -04001796 * mv_interrupt - Main interrupt event handler
Brett Russ05b308e2005-10-05 17:08:53 -04001797 * @irq: unused
1798 * @dev_instance: private data; in this case the host structure
Brett Russ05b308e2005-10-05 17:08:53 -04001799 *
1800 * Read the read only register to determine if any host
1801 * controllers have pending interrupts. If so, call lower level
1802 * routine to handle. Also check for PCI errors which are only
1803 * reported here.
1804 *
Jeff Garzik8b260242005-11-12 12:32:50 -05001805 * LOCKING:
Jeff Garzikcca39742006-08-24 03:19:22 -04001806 * This routine holds the host lock while processing pending
Brett Russ05b308e2005-10-05 17:08:53 -04001807 * interrupts.
1808 */
David Howells7d12e782006-10-05 14:55:46 +01001809static irqreturn_t mv_interrupt(int irq, void *dev_instance)
Brett Russ20f733e2005-09-01 18:26:17 -04001810{
Jeff Garzikcca39742006-08-24 03:19:22 -04001811 struct ata_host *host = dev_instance;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05001812 struct mv_host_priv *hpriv = host->private_data;
Mark Lorda3718c12008-04-19 15:07:18 -04001813 unsigned int handled = 0;
Mark Lord352fab72008-04-19 14:43:42 -04001814 u32 main_cause, main_mask;
Brett Russ20f733e2005-09-01 18:26:17 -04001815
Mark Lord646a4da2008-01-26 18:30:37 -05001816 spin_lock(&host->lock);
Mark Lord352fab72008-04-19 14:43:42 -04001817 main_cause = readl(hpriv->main_cause_reg_addr);
1818 main_mask = readl(hpriv->main_mask_reg_addr);
1819 /*
1820 * Deal with cases where we either have nothing pending, or have read
1821 * a bogus register value which can indicate HW removal or PCI fault.
Brett Russ20f733e2005-09-01 18:26:17 -04001822 */
Mark Lorda3718c12008-04-19 15:07:18 -04001823 if ((main_cause & main_mask) && (main_cause != 0xffffffffU)) {
1824 if (unlikely((main_cause & PCI_ERR) && HAS_PCI(host)))
1825 handled = mv_pci_error(host, hpriv->base);
1826 else
1827 handled = mv_host_intr(host, main_cause);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04001828 }
Jeff Garzikcca39742006-08-24 03:19:22 -04001829 spin_unlock(&host->lock);
Brett Russ20f733e2005-09-01 18:26:17 -04001830 return IRQ_RETVAL(handled);
1831}
1832
Jeff Garzikc9d39132005-11-13 17:47:51 -05001833static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
1834{
1835 unsigned int ofs;
1836
1837 switch (sc_reg_in) {
1838 case SCR_STATUS:
1839 case SCR_ERROR:
1840 case SCR_CONTROL:
1841 ofs = sc_reg_in * sizeof(u32);
1842 break;
1843 default:
1844 ofs = 0xffffffffU;
1845 break;
1846 }
1847 return ofs;
1848}
1849
Tejun Heoda3dbb12007-07-16 14:29:40 +09001850static int mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in, u32 *val)
Jeff Garzikc9d39132005-11-13 17:47:51 -05001851{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05001852 struct mv_host_priv *hpriv = ap->host->private_data;
1853 void __iomem *mmio = hpriv->base;
Tejun Heo0d5ff562007-02-01 15:06:36 +09001854 void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
Jeff Garzikc9d39132005-11-13 17:47:51 -05001855 unsigned int ofs = mv5_scr_offset(sc_reg_in);
1856
Tejun Heoda3dbb12007-07-16 14:29:40 +09001857 if (ofs != 0xffffffffU) {
1858 *val = readl(addr + ofs);
1859 return 0;
1860 } else
1861 return -EINVAL;
Jeff Garzikc9d39132005-11-13 17:47:51 -05001862}
1863
Tejun Heoda3dbb12007-07-16 14:29:40 +09001864static int mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
Jeff Garzikc9d39132005-11-13 17:47:51 -05001865{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05001866 struct mv_host_priv *hpriv = ap->host->private_data;
1867 void __iomem *mmio = hpriv->base;
Tejun Heo0d5ff562007-02-01 15:06:36 +09001868 void __iomem *addr = mv5_phy_base(mmio, ap->port_no);
Jeff Garzikc9d39132005-11-13 17:47:51 -05001869 unsigned int ofs = mv5_scr_offset(sc_reg_in);
1870
Tejun Heoda3dbb12007-07-16 14:29:40 +09001871 if (ofs != 0xffffffffU) {
Tejun Heo0d5ff562007-02-01 15:06:36 +09001872 writelfl(val, addr + ofs);
Tejun Heoda3dbb12007-07-16 14:29:40 +09001873 return 0;
1874 } else
1875 return -EINVAL;
Jeff Garzikc9d39132005-11-13 17:47:51 -05001876}
1877
Saeed Bishara7bb3c522008-01-30 11:50:45 -11001878static void mv5_reset_bus(struct ata_host *host, void __iomem *mmio)
Jeff Garzik522479f2005-11-12 22:14:02 -05001879{
Saeed Bishara7bb3c522008-01-30 11:50:45 -11001880 struct pci_dev *pdev = to_pci_dev(host->dev);
Jeff Garzik522479f2005-11-12 22:14:02 -05001881 int early_5080;
1882
Auke Kok44c10132007-06-08 15:46:36 -07001883 early_5080 = (pdev->device == 0x5080) && (pdev->revision == 0);
Jeff Garzik522479f2005-11-12 22:14:02 -05001884
1885 if (!early_5080) {
1886 u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1887 tmp |= (1 << 0);
1888 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
1889 }
1890
Saeed Bishara7bb3c522008-01-30 11:50:45 -11001891 mv_reset_pci_bus(host, mmio);
Jeff Garzik522479f2005-11-12 22:14:02 -05001892}
1893
1894static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
1895{
1896 writel(0x0fcfffff, mmio + MV_FLASH_CTL);
1897}
1898
Jeff Garzik47c2b672005-11-12 21:13:17 -05001899static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001900 void __iomem *mmio)
1901{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001902 void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
1903 u32 tmp;
1904
1905 tmp = readl(phy_mmio + MV5_PHY_MODE);
1906
1907 hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
1908 hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001909}
1910
Jeff Garzik47c2b672005-11-12 21:13:17 -05001911static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001912{
Jeff Garzik522479f2005-11-12 22:14:02 -05001913 u32 tmp;
1914
1915 writel(0, mmio + MV_GPIO_PORT_CTL);
1916
1917 /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
1918
1919 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1920 tmp |= ~(1 << 0);
1921 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001922}
1923
Jeff Garzik2a47ce02005-11-12 23:05:14 -05001924static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
1925 unsigned int port)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001926{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001927 void __iomem *phy_mmio = mv5_phy_base(mmio, port);
1928 const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
1929 u32 tmp;
1930 int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
1931
1932 if (fix_apm_sq) {
1933 tmp = readl(phy_mmio + MV5_LT_MODE);
1934 tmp |= (1 << 19);
1935 writel(tmp, phy_mmio + MV5_LT_MODE);
1936
1937 tmp = readl(phy_mmio + MV5_PHY_CTL);
1938 tmp &= ~0x3;
1939 tmp |= 0x1;
1940 writel(tmp, phy_mmio + MV5_PHY_CTL);
1941 }
1942
1943 tmp = readl(phy_mmio + MV5_PHY_MODE);
1944 tmp &= ~mask;
1945 tmp |= hpriv->signal[port].pre;
1946 tmp |= hpriv->signal[port].amps;
1947 writel(tmp, phy_mmio + MV5_PHY_MODE);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001948}
1949
Jeff Garzikc9d39132005-11-13 17:47:51 -05001950
1951#undef ZERO
1952#define ZERO(reg) writel(0, port_mmio + (reg))
1953static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
1954 unsigned int port)
Jeff Garzik47c2b672005-11-12 21:13:17 -05001955{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001956 void __iomem *port_mmio = mv_port_base(mmio, port);
1957
Mark Lordb5624682008-03-31 19:34:40 -04001958 /*
1959 * The datasheet warns against setting ATA_RST when EDMA is active
1960 * (but doesn't say what the problem might be). So we first try
1961 * to disable the EDMA engine before doing the ATA_RST operation.
1962 */
Mark Lorde12bef52008-03-31 19:33:56 -04001963 mv_reset_channel(hpriv, mmio, port);
Jeff Garzikc9d39132005-11-13 17:47:51 -05001964
1965 ZERO(0x028); /* command */
1966 writel(0x11f, port_mmio + EDMA_CFG_OFS);
1967 ZERO(0x004); /* timer */
1968 ZERO(0x008); /* irq err cause */
1969 ZERO(0x00c); /* irq err mask */
1970 ZERO(0x010); /* rq bah */
1971 ZERO(0x014); /* rq inp */
1972 ZERO(0x018); /* rq outp */
1973 ZERO(0x01c); /* respq bah */
1974 ZERO(0x024); /* respq outp */
1975 ZERO(0x020); /* respq inp */
1976 ZERO(0x02c); /* test control */
1977 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
1978}
1979#undef ZERO
1980
1981#define ZERO(reg) writel(0, hc_mmio + (reg))
1982static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
1983 unsigned int hc)
1984{
1985 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
1986 u32 tmp;
1987
1988 ZERO(0x00c);
1989 ZERO(0x010);
1990 ZERO(0x014);
1991 ZERO(0x018);
1992
1993 tmp = readl(hc_mmio + 0x20);
1994 tmp &= 0x1c1c1c1c;
1995 tmp |= 0x03030303;
1996 writel(tmp, hc_mmio + 0x20);
1997}
1998#undef ZERO
1999
2000static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2001 unsigned int n_hc)
2002{
2003 unsigned int hc, port;
2004
2005 for (hc = 0; hc < n_hc; hc++) {
2006 for (port = 0; port < MV_PORTS_PER_HC; port++)
2007 mv5_reset_hc_port(hpriv, mmio,
2008 (hc * MV_PORTS_PER_HC) + port);
2009
2010 mv5_reset_one_hc(hpriv, mmio, hc);
2011 }
2012
2013 return 0;
Jeff Garzik47c2b672005-11-12 21:13:17 -05002014}
2015
Jeff Garzik101ffae2005-11-12 22:17:49 -05002016#undef ZERO
2017#define ZERO(reg) writel(0, mmio + (reg))
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002018static void mv_reset_pci_bus(struct ata_host *host, void __iomem *mmio)
Jeff Garzik101ffae2005-11-12 22:17:49 -05002019{
Mark Lord02a121d2007-12-01 13:07:22 -05002020 struct mv_host_priv *hpriv = host->private_data;
Jeff Garzik101ffae2005-11-12 22:17:49 -05002021 u32 tmp;
2022
2023 tmp = readl(mmio + MV_PCI_MODE);
2024 tmp &= 0xff00ffff;
2025 writel(tmp, mmio + MV_PCI_MODE);
2026
2027 ZERO(MV_PCI_DISC_TIMER);
2028 ZERO(MV_PCI_MSI_TRIGGER);
2029 writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
2030 ZERO(HC_MAIN_IRQ_MASK_OFS);
2031 ZERO(MV_PCI_SERR_MASK);
Mark Lord02a121d2007-12-01 13:07:22 -05002032 ZERO(hpriv->irq_cause_ofs);
2033 ZERO(hpriv->irq_mask_ofs);
Jeff Garzik101ffae2005-11-12 22:17:49 -05002034 ZERO(MV_PCI_ERR_LOW_ADDRESS);
2035 ZERO(MV_PCI_ERR_HIGH_ADDRESS);
2036 ZERO(MV_PCI_ERR_ATTRIBUTE);
2037 ZERO(MV_PCI_ERR_COMMAND);
2038}
2039#undef ZERO
2040
2041static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
2042{
2043 u32 tmp;
2044
2045 mv5_reset_flash(hpriv, mmio);
2046
2047 tmp = readl(mmio + MV_GPIO_PORT_CTL);
2048 tmp &= 0x3;
2049 tmp |= (1 << 5) | (1 << 6);
2050 writel(tmp, mmio + MV_GPIO_PORT_CTL);
2051}
2052
2053/**
2054 * mv6_reset_hc - Perform the 6xxx global soft reset
2055 * @mmio: base address of the HBA
2056 *
2057 * This routine only applies to 6xxx parts.
2058 *
2059 * LOCKING:
2060 * Inherited from caller.
2061 */
Jeff Garzikc9d39132005-11-13 17:47:51 -05002062static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
2063 unsigned int n_hc)
Jeff Garzik101ffae2005-11-12 22:17:49 -05002064{
2065 void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
2066 int i, rc = 0;
2067 u32 t;
2068
2069 /* Following procedure defined in PCI "main command and status
2070 * register" table.
2071 */
2072 t = readl(reg);
2073 writel(t | STOP_PCI_MASTER, reg);
2074
2075 for (i = 0; i < 1000; i++) {
2076 udelay(1);
2077 t = readl(reg);
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002078 if (PCI_MASTER_EMPTY & t)
Jeff Garzik101ffae2005-11-12 22:17:49 -05002079 break;
Jeff Garzik101ffae2005-11-12 22:17:49 -05002080 }
2081 if (!(PCI_MASTER_EMPTY & t)) {
2082 printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
2083 rc = 1;
2084 goto done;
2085 }
2086
2087 /* set reset */
2088 i = 5;
2089 do {
2090 writel(t | GLOB_SFT_RST, reg);
2091 t = readl(reg);
2092 udelay(1);
2093 } while (!(GLOB_SFT_RST & t) && (i-- > 0));
2094
2095 if (!(GLOB_SFT_RST & t)) {
2096 printk(KERN_ERR DRV_NAME ": can't set global reset\n");
2097 rc = 1;
2098 goto done;
2099 }
2100
2101 /* clear reset and *reenable the PCI master* (not mentioned in spec) */
2102 i = 5;
2103 do {
2104 writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
2105 t = readl(reg);
2106 udelay(1);
2107 } while ((GLOB_SFT_RST & t) && (i-- > 0));
2108
2109 if (GLOB_SFT_RST & t) {
2110 printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
2111 rc = 1;
2112 }
2113done:
2114 return rc;
2115}
2116
Jeff Garzik47c2b672005-11-12 21:13:17 -05002117static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002118 void __iomem *mmio)
2119{
2120 void __iomem *port_mmio;
2121 u32 tmp;
2122
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002123 tmp = readl(mmio + MV_RESET_CFG);
2124 if ((tmp & (1 << 0)) == 0) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05002125 hpriv->signal[idx].amps = 0x7 << 8;
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002126 hpriv->signal[idx].pre = 0x1 << 5;
2127 return;
2128 }
2129
2130 port_mmio = mv_port_base(mmio, idx);
2131 tmp = readl(port_mmio + PHY_MODE2);
2132
2133 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
2134 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
2135}
2136
Jeff Garzik47c2b672005-11-12 21:13:17 -05002137static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002138{
Jeff Garzik47c2b672005-11-12 21:13:17 -05002139 writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05002140}
2141
Jeff Garzikc9d39132005-11-13 17:47:51 -05002142static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002143 unsigned int port)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002144{
Jeff Garzikc9d39132005-11-13 17:47:51 -05002145 void __iomem *port_mmio = mv_port_base(mmio, port);
2146
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002147 u32 hp_flags = hpriv->hp_flags;
Jeff Garzik47c2b672005-11-12 21:13:17 -05002148 int fix_phy_mode2 =
2149 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002150 int fix_phy_mode4 =
Jeff Garzik47c2b672005-11-12 21:13:17 -05002151 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
2152 u32 m2, tmp;
2153
2154 if (fix_phy_mode2) {
2155 m2 = readl(port_mmio + PHY_MODE2);
2156 m2 &= ~(1 << 16);
2157 m2 |= (1 << 31);
2158 writel(m2, port_mmio + PHY_MODE2);
2159
2160 udelay(200);
2161
2162 m2 = readl(port_mmio + PHY_MODE2);
2163 m2 &= ~((1 << 16) | (1 << 31));
2164 writel(m2, port_mmio + PHY_MODE2);
2165
2166 udelay(200);
2167 }
2168
2169 /* who knows what this magic does */
2170 tmp = readl(port_mmio + PHY_MODE3);
2171 tmp &= ~0x7F800000;
2172 tmp |= 0x2A800000;
2173 writel(tmp, port_mmio + PHY_MODE3);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002174
2175 if (fix_phy_mode4) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05002176 u32 m4;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002177
2178 m4 = readl(port_mmio + PHY_MODE4);
Jeff Garzik47c2b672005-11-12 21:13:17 -05002179
2180 if (hp_flags & MV_HP_ERRATA_60X1B2)
Mark Lorde12bef52008-03-31 19:33:56 -04002181 tmp = readl(port_mmio + PHY_MODE3);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002182
Mark Lorde12bef52008-03-31 19:33:56 -04002183 /* workaround for errata FEr SATA#10 (part 1) */
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002184 m4 = (m4 & ~(1 << 1)) | (1 << 0);
2185
2186 writel(m4, port_mmio + PHY_MODE4);
Jeff Garzik47c2b672005-11-12 21:13:17 -05002187
2188 if (hp_flags & MV_HP_ERRATA_60X1B2)
Mark Lorde12bef52008-03-31 19:33:56 -04002189 writel(tmp, port_mmio + PHY_MODE3);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002190 }
2191
2192 /* Revert values of pre-emphasis and signal amps to the saved ones */
2193 m2 = readl(port_mmio + PHY_MODE2);
2194
2195 m2 &= ~MV_M2_PREAMP_MASK;
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002196 m2 |= hpriv->signal[port].amps;
2197 m2 |= hpriv->signal[port].pre;
Jeff Garzik47c2b672005-11-12 21:13:17 -05002198 m2 &= ~(1 << 16);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002199
Jeff Garzike4e7b892006-01-31 12:18:41 -05002200 /* according to mvSata 3.6.1, some IIE values are fixed */
2201 if (IS_GEN_IIE(hpriv)) {
2202 m2 &= ~0xC30FF01F;
2203 m2 |= 0x0000900F;
2204 }
2205
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002206 writel(m2, port_mmio + PHY_MODE2);
2207}
2208
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002209/* TODO: use the generic LED interface to configure the SATA Presence */
2210/* & Acitivy LEDs on the board */
2211static void mv_soc_enable_leds(struct mv_host_priv *hpriv,
2212 void __iomem *mmio)
2213{
2214 return;
2215}
2216
2217static void mv_soc_read_preamp(struct mv_host_priv *hpriv, int idx,
2218 void __iomem *mmio)
2219{
2220 void __iomem *port_mmio;
2221 u32 tmp;
2222
2223 port_mmio = mv_port_base(mmio, idx);
2224 tmp = readl(port_mmio + PHY_MODE2);
2225
2226 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
2227 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
2228}
2229
2230#undef ZERO
2231#define ZERO(reg) writel(0, port_mmio + (reg))
2232static void mv_soc_reset_hc_port(struct mv_host_priv *hpriv,
2233 void __iomem *mmio, unsigned int port)
2234{
2235 void __iomem *port_mmio = mv_port_base(mmio, port);
2236
Mark Lordb5624682008-03-31 19:34:40 -04002237 /*
2238 * The datasheet warns against setting ATA_RST when EDMA is active
2239 * (but doesn't say what the problem might be). So we first try
2240 * to disable the EDMA engine before doing the ATA_RST operation.
2241 */
Mark Lorde12bef52008-03-31 19:33:56 -04002242 mv_reset_channel(hpriv, mmio, port);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002243
2244 ZERO(0x028); /* command */
2245 writel(0x101f, port_mmio + EDMA_CFG_OFS);
2246 ZERO(0x004); /* timer */
2247 ZERO(0x008); /* irq err cause */
2248 ZERO(0x00c); /* irq err mask */
2249 ZERO(0x010); /* rq bah */
2250 ZERO(0x014); /* rq inp */
2251 ZERO(0x018); /* rq outp */
2252 ZERO(0x01c); /* respq bah */
2253 ZERO(0x024); /* respq outp */
2254 ZERO(0x020); /* respq inp */
2255 ZERO(0x02c); /* test control */
2256 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
2257}
2258
2259#undef ZERO
2260
2261#define ZERO(reg) writel(0, hc_mmio + (reg))
2262static void mv_soc_reset_one_hc(struct mv_host_priv *hpriv,
2263 void __iomem *mmio)
2264{
2265 void __iomem *hc_mmio = mv_hc_base(mmio, 0);
2266
2267 ZERO(0x00c);
2268 ZERO(0x010);
2269 ZERO(0x014);
2270
2271}
2272
2273#undef ZERO
2274
2275static int mv_soc_reset_hc(struct mv_host_priv *hpriv,
2276 void __iomem *mmio, unsigned int n_hc)
2277{
2278 unsigned int port;
2279
2280 for (port = 0; port < hpriv->n_ports; port++)
2281 mv_soc_reset_hc_port(hpriv, mmio, port);
2282
2283 mv_soc_reset_one_hc(hpriv, mmio);
2284
2285 return 0;
2286}
2287
2288static void mv_soc_reset_flash(struct mv_host_priv *hpriv,
2289 void __iomem *mmio)
2290{
2291 return;
2292}
2293
2294static void mv_soc_reset_bus(struct ata_host *host, void __iomem *mmio)
2295{
2296 return;
2297}
2298
Mark Lordb67a1062008-03-31 19:35:13 -04002299static void mv_setup_ifctl(void __iomem *port_mmio, int want_gen2i)
2300{
2301 u32 ifctl = readl(port_mmio + SATA_INTERFACE_CFG);
2302
2303 ifctl = (ifctl & 0xf7f) | 0x9b1000; /* from chip spec */
2304 if (want_gen2i)
2305 ifctl |= (1 << 7); /* enable gen2i speed */
2306 writelfl(ifctl, port_mmio + SATA_INTERFACE_CFG);
2307}
2308
Mark Lordb5624682008-03-31 19:34:40 -04002309/*
2310 * Caller must ensure that EDMA is not active,
2311 * by first doing mv_stop_edma() where needed.
2312 */
Mark Lorde12bef52008-03-31 19:33:56 -04002313static void mv_reset_channel(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzikc9d39132005-11-13 17:47:51 -05002314 unsigned int port_no)
Brett Russ20f733e2005-09-01 18:26:17 -04002315{
Jeff Garzikc9d39132005-11-13 17:47:51 -05002316 void __iomem *port_mmio = mv_port_base(mmio, port_no);
Brett Russ20f733e2005-09-01 18:26:17 -04002317
Mark Lord0d8be5c2008-04-16 14:56:12 -04002318 mv_stop_edma_engine(port_mmio);
Brett Russ31961942005-09-30 01:36:00 -04002319 writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002320
Mark Lordb67a1062008-03-31 19:35:13 -04002321 if (!IS_GEN_I(hpriv)) {
2322 /* Enable 3.0gb/s link speed */
2323 mv_setup_ifctl(port_mmio, 1);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002324 }
Mark Lordb67a1062008-03-31 19:35:13 -04002325 /*
2326 * Strobing ATA_RST here causes a hard reset of the SATA transport,
2327 * link, and physical layers. It resets all SATA interface registers
2328 * (except for SATA_INTERFACE_CFG), and issues a COMRESET to the dev.
Brett Russ20f733e2005-09-01 18:26:17 -04002329 */
Mark Lordb67a1062008-03-31 19:35:13 -04002330 writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
2331 udelay(25); /* allow reset propagation */
Brett Russ31961942005-09-30 01:36:00 -04002332 writelfl(0, port_mmio + EDMA_CMD_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04002333
Jeff Garzikc9d39132005-11-13 17:47:51 -05002334 hpriv->ops->phy_errata(hpriv, mmio, port_no);
2335
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04002336 if (IS_GEN_I(hpriv))
Jeff Garzikc9d39132005-11-13 17:47:51 -05002337 mdelay(1);
2338}
2339
Mark Lorde49856d2008-04-16 14:59:07 -04002340static void mv_pmp_select(struct ata_port *ap, int pmp)
Jeff Garzikc9d39132005-11-13 17:47:51 -05002341{
Mark Lorde49856d2008-04-16 14:59:07 -04002342 if (sata_pmp_supported(ap)) {
2343 void __iomem *port_mmio = mv_ap_base(ap);
2344 u32 reg = readl(port_mmio + SATA_IFCTL_OFS);
2345 int old = reg & 0xf;
Jeff Garzikc9d39132005-11-13 17:47:51 -05002346
Mark Lorde49856d2008-04-16 14:59:07 -04002347 if (old != pmp) {
2348 reg = (reg & ~0xf) | pmp;
2349 writelfl(reg, port_mmio + SATA_IFCTL_OFS);
2350 }
Tejun Heoda3dbb12007-07-16 14:29:40 +09002351 }
Brett Russ20f733e2005-09-01 18:26:17 -04002352}
2353
Mark Lorde49856d2008-04-16 14:59:07 -04002354static int mv_pmp_hardreset(struct ata_link *link, unsigned int *class,
2355 unsigned long deadline)
Jeff Garzik22374672005-11-17 10:59:48 -05002356{
Mark Lorde49856d2008-04-16 14:59:07 -04002357 mv_pmp_select(link->ap, sata_srst_pmp(link));
2358 return sata_std_hardreset(link, class, deadline);
2359}
Jeff Garzik0ea9e172007-07-13 17:06:45 -04002360
Mark Lorde49856d2008-04-16 14:59:07 -04002361static int mv_softreset(struct ata_link *link, unsigned int *class,
2362 unsigned long deadline)
2363{
2364 mv_pmp_select(link->ap, sata_srst_pmp(link));
2365 return ata_sff_softreset(link, class, deadline);
Jeff Garzik22374672005-11-17 10:59:48 -05002366}
2367
Tejun Heocc0680a2007-08-06 18:36:23 +09002368static int mv_hardreset(struct ata_link *link, unsigned int *class,
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002369 unsigned long deadline)
2370{
Tejun Heocc0680a2007-08-06 18:36:23 +09002371 struct ata_port *ap = link->ap;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002372 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lordb5624682008-03-31 19:34:40 -04002373 struct mv_port_priv *pp = ap->private_data;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002374 void __iomem *mmio = hpriv->base;
Mark Lord0d8be5c2008-04-16 14:56:12 -04002375 int rc, attempts = 0, extra = 0;
2376 u32 sstatus;
2377 bool online;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002378
Mark Lorde12bef52008-03-31 19:33:56 -04002379 mv_reset_channel(hpriv, mmio, ap->port_no);
Mark Lordb5624682008-03-31 19:34:40 -04002380 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002381
Mark Lord0d8be5c2008-04-16 14:56:12 -04002382 /* Workaround for errata FEr SATA#10 (part 2) */
2383 do {
Mark Lord17c5aab2008-04-16 14:56:51 -04002384 const unsigned long *timing =
2385 sata_ehc_deb_timing(&link->eh_context);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002386
Mark Lord17c5aab2008-04-16 14:56:51 -04002387 rc = sata_link_hardreset(link, timing, deadline + extra,
2388 &online, NULL);
2389 if (rc)
Mark Lord0d8be5c2008-04-16 14:56:12 -04002390 return rc;
Mark Lord0d8be5c2008-04-16 14:56:12 -04002391 sata_scr_read(link, SCR_STATUS, &sstatus);
2392 if (!IS_GEN_I(hpriv) && ++attempts >= 5 && sstatus == 0x121) {
2393 /* Force 1.5gb/s link speed and try again */
2394 mv_setup_ifctl(mv_ap_base(ap), 0);
2395 if (time_after(jiffies + HZ, deadline))
2396 extra = HZ; /* only extend it once, max */
2397 }
2398 } while (sstatus != 0x0 && sstatus != 0x113 && sstatus != 0x123);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002399
Mark Lord17c5aab2008-04-16 14:56:51 -04002400 return rc;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002401}
2402
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002403static void mv_eh_freeze(struct ata_port *ap)
Brett Russ20f733e2005-09-01 18:26:17 -04002404{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002405 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lord1cfd19a2008-04-19 15:05:50 -04002406 unsigned int shift, hardport, port = ap->port_no;
Mark Lord352fab72008-04-19 14:43:42 -04002407 u32 main_mask;
Brett Russ31961942005-09-30 01:36:00 -04002408
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002409 /* FIXME: handle coalescing completion events properly */
Brett Russ31961942005-09-30 01:36:00 -04002410
Mark Lord1cfd19a2008-04-19 15:05:50 -04002411 mv_stop_edma(ap);
2412 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
Brett Russ31961942005-09-30 01:36:00 -04002413
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002414 /* disable assertion of portN err, done events */
Mark Lord352fab72008-04-19 14:43:42 -04002415 main_mask = readl(hpriv->main_mask_reg_addr);
2416 main_mask &= ~((DONE_IRQ | ERR_IRQ) << shift);
2417 writelfl(main_mask, hpriv->main_mask_reg_addr);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002418}
2419
2420static void mv_eh_thaw(struct ata_port *ap)
2421{
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002422 struct mv_host_priv *hpriv = ap->host->private_data;
Mark Lord1cfd19a2008-04-19 15:05:50 -04002423 unsigned int shift, hardport, port = ap->port_no;
2424 void __iomem *hc_mmio = mv_hc_base_from_port(hpriv->base, port);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002425 void __iomem *port_mmio = mv_ap_base(ap);
Mark Lord352fab72008-04-19 14:43:42 -04002426 u32 main_mask, hc_irq_cause;
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002427
2428 /* FIXME: handle coalescing completion events properly */
2429
Mark Lord1cfd19a2008-04-19 15:05:50 -04002430 MV_PORT_TO_SHIFT_AND_HARDPORT(port, shift, hardport);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002431
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002432 /* clear EDMA errors on this port */
2433 writel(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2434
2435 /* clear pending irq events */
2436 hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
Mark Lord1cfd19a2008-04-19 15:05:50 -04002437 hc_irq_cause &= ~((DEV_IRQ | DMA_IRQ) << hardport);
2438 writelfl(hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
Jeff Garzikbdd4ddd2007-07-12 14:34:26 -04002439
2440 /* enable assertion of portN err, done events */
Mark Lord352fab72008-04-19 14:43:42 -04002441 main_mask = readl(hpriv->main_mask_reg_addr);
2442 main_mask |= ((DONE_IRQ | ERR_IRQ) << shift);
2443 writelfl(main_mask, hpriv->main_mask_reg_addr);
Brett Russ31961942005-09-30 01:36:00 -04002444}
2445
Brett Russ05b308e2005-10-05 17:08:53 -04002446/**
2447 * mv_port_init - Perform some early initialization on a single port.
2448 * @port: libata data structure storing shadow register addresses
2449 * @port_mmio: base address of the port
2450 *
2451 * Initialize shadow register mmio addresses, clear outstanding
2452 * interrupts on the port, and unmask interrupts for the future
2453 * start of the port.
2454 *
2455 * LOCKING:
2456 * Inherited from caller.
2457 */
Brett Russ31961942005-09-30 01:36:00 -04002458static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
2459{
Tejun Heo0d5ff562007-02-01 15:06:36 +09002460 void __iomem *shd_base = port_mmio + SHD_BLK_OFS;
Brett Russ31961942005-09-30 01:36:00 -04002461 unsigned serr_ofs;
2462
Jeff Garzik8b260242005-11-12 12:32:50 -05002463 /* PIO related setup
Brett Russ31961942005-09-30 01:36:00 -04002464 */
2465 port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
Jeff Garzik8b260242005-11-12 12:32:50 -05002466 port->error_addr =
Brett Russ31961942005-09-30 01:36:00 -04002467 port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
2468 port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
2469 port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
2470 port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
2471 port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
2472 port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
Jeff Garzik8b260242005-11-12 12:32:50 -05002473 port->status_addr =
Brett Russ31961942005-09-30 01:36:00 -04002474 port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
2475 /* special case: control/altstatus doesn't have ATA_REG_ address */
2476 port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
2477
2478 /* unused: */
Randy Dunlap8d9db2d2007-02-16 01:40:06 -08002479 port->cmd_addr = port->bmdma_addr = port->scr_addr = NULL;
Brett Russ20f733e2005-09-01 18:26:17 -04002480
Brett Russ31961942005-09-30 01:36:00 -04002481 /* Clear any currently outstanding port interrupt conditions */
2482 serr_ofs = mv_scr_offset(SCR_ERROR);
2483 writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
2484 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
2485
Mark Lord646a4da2008-01-26 18:30:37 -05002486 /* unmask all non-transient EDMA error interrupts */
2487 writelfl(~EDMA_ERR_IRQ_TRANSIENT, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04002488
Jeff Garzik8b260242005-11-12 12:32:50 -05002489 VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
Brett Russ31961942005-09-30 01:36:00 -04002490 readl(port_mmio + EDMA_CFG_OFS),
2491 readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
2492 readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
Brett Russ20f733e2005-09-01 18:26:17 -04002493}
2494
Tejun Heo4447d352007-04-17 23:44:08 +09002495static int mv_chip_id(struct ata_host *host, unsigned int board_idx)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002496{
Tejun Heo4447d352007-04-17 23:44:08 +09002497 struct pci_dev *pdev = to_pci_dev(host->dev);
2498 struct mv_host_priv *hpriv = host->private_data;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002499 u32 hp_flags = hpriv->hp_flags;
2500
Jeff Garzik5796d1c2007-10-26 00:03:37 -04002501 switch (board_idx) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05002502 case chip_5080:
2503 hpriv->ops = &mv5xxx_ops;
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04002504 hp_flags |= MV_HP_GEN_I;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002505
Auke Kok44c10132007-06-08 15:46:36 -07002506 switch (pdev->revision) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05002507 case 0x1:
2508 hp_flags |= MV_HP_ERRATA_50XXB0;
2509 break;
2510 case 0x3:
2511 hp_flags |= MV_HP_ERRATA_50XXB2;
2512 break;
2513 default:
2514 dev_printk(KERN_WARNING, &pdev->dev,
2515 "Applying 50XXB2 workarounds to unknown rev\n");
2516 hp_flags |= MV_HP_ERRATA_50XXB2;
2517 break;
2518 }
2519 break;
2520
2521 case chip_504x:
2522 case chip_508x:
2523 hpriv->ops = &mv5xxx_ops;
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04002524 hp_flags |= MV_HP_GEN_I;
Jeff Garzik47c2b672005-11-12 21:13:17 -05002525
Auke Kok44c10132007-06-08 15:46:36 -07002526 switch (pdev->revision) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05002527 case 0x0:
2528 hp_flags |= MV_HP_ERRATA_50XXB0;
2529 break;
2530 case 0x3:
2531 hp_flags |= MV_HP_ERRATA_50XXB2;
2532 break;
2533 default:
2534 dev_printk(KERN_WARNING, &pdev->dev,
2535 "Applying B2 workarounds to unknown rev\n");
2536 hp_flags |= MV_HP_ERRATA_50XXB2;
2537 break;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002538 }
2539 break;
2540
2541 case chip_604x:
2542 case chip_608x:
Jeff Garzik47c2b672005-11-12 21:13:17 -05002543 hpriv->ops = &mv6xxx_ops;
Jeff Garzikee9ccdf2007-07-12 15:51:22 -04002544 hp_flags |= MV_HP_GEN_II;
Jeff Garzik47c2b672005-11-12 21:13:17 -05002545
Auke Kok44c10132007-06-08 15:46:36 -07002546 switch (pdev->revision) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05002547 case 0x7:
2548 hp_flags |= MV_HP_ERRATA_60X1B2;
2549 break;
2550 case 0x9:
2551 hp_flags |= MV_HP_ERRATA_60X1C0;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002552 break;
2553 default:
2554 dev_printk(KERN_WARNING, &pdev->dev,
Jeff Garzik47c2b672005-11-12 21:13:17 -05002555 "Applying B2 workarounds to unknown rev\n");
2556 hp_flags |= MV_HP_ERRATA_60X1B2;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002557 break;
2558 }
2559 break;
2560
Jeff Garzike4e7b892006-01-31 12:18:41 -05002561 case chip_7042:
Mark Lord02a121d2007-12-01 13:07:22 -05002562 hp_flags |= MV_HP_PCIE;
Mark Lord306b30f2007-12-04 14:07:52 -05002563 if (pdev->vendor == PCI_VENDOR_ID_TTI &&
2564 (pdev->device == 0x2300 || pdev->device == 0x2310))
2565 {
Mark Lord4e520032007-12-11 12:58:05 -05002566 /*
2567 * Highpoint RocketRAID PCIe 23xx series cards:
2568 *
2569 * Unconfigured drives are treated as "Legacy"
2570 * by the BIOS, and it overwrites sector 8 with
2571 * a "Lgcy" metadata block prior to Linux boot.
2572 *
2573 * Configured drives (RAID or JBOD) leave sector 8
2574 * alone, but instead overwrite a high numbered
2575 * sector for the RAID metadata. This sector can
2576 * be determined exactly, by truncating the physical
2577 * drive capacity to a nice even GB value.
2578 *
2579 * RAID metadata is at: (dev->n_sectors & ~0xfffff)
2580 *
2581 * Warn the user, lest they think we're just buggy.
2582 */
2583 printk(KERN_WARNING DRV_NAME ": Highpoint RocketRAID"
2584 " BIOS CORRUPTS DATA on all attached drives,"
2585 " regardless of if/how they are configured."
2586 " BEWARE!\n");
2587 printk(KERN_WARNING DRV_NAME ": For data safety, do not"
2588 " use sectors 8-9 on \"Legacy\" drives,"
2589 " and avoid the final two gigabytes on"
2590 " all RocketRAID BIOS initialized drives.\n");
Mark Lord306b30f2007-12-04 14:07:52 -05002591 }
Jeff Garzike4e7b892006-01-31 12:18:41 -05002592 case chip_6042:
2593 hpriv->ops = &mv6xxx_ops;
Jeff Garzike4e7b892006-01-31 12:18:41 -05002594 hp_flags |= MV_HP_GEN_IIE;
2595
Auke Kok44c10132007-06-08 15:46:36 -07002596 switch (pdev->revision) {
Jeff Garzike4e7b892006-01-31 12:18:41 -05002597 case 0x0:
2598 hp_flags |= MV_HP_ERRATA_XX42A0;
2599 break;
2600 case 0x1:
2601 hp_flags |= MV_HP_ERRATA_60X1C0;
2602 break;
2603 default:
2604 dev_printk(KERN_WARNING, &pdev->dev,
2605 "Applying 60X1C0 workarounds to unknown rev\n");
2606 hp_flags |= MV_HP_ERRATA_60X1C0;
2607 break;
2608 }
2609 break;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002610 case chip_soc:
2611 hpriv->ops = &mv_soc_ops;
2612 hp_flags |= MV_HP_ERRATA_60X1C0;
2613 break;
Jeff Garzike4e7b892006-01-31 12:18:41 -05002614
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002615 default:
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002616 dev_printk(KERN_ERR, host->dev,
Jeff Garzik5796d1c2007-10-26 00:03:37 -04002617 "BUG: invalid board index %u\n", board_idx);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002618 return 1;
2619 }
2620
2621 hpriv->hp_flags = hp_flags;
Mark Lord02a121d2007-12-01 13:07:22 -05002622 if (hp_flags & MV_HP_PCIE) {
2623 hpriv->irq_cause_ofs = PCIE_IRQ_CAUSE_OFS;
2624 hpriv->irq_mask_ofs = PCIE_IRQ_MASK_OFS;
2625 hpriv->unmask_all_irqs = PCIE_UNMASK_ALL_IRQS;
2626 } else {
2627 hpriv->irq_cause_ofs = PCI_IRQ_CAUSE_OFS;
2628 hpriv->irq_mask_ofs = PCI_IRQ_MASK_OFS;
2629 hpriv->unmask_all_irqs = PCI_UNMASK_ALL_IRQS;
2630 }
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002631
2632 return 0;
2633}
2634
Brett Russ05b308e2005-10-05 17:08:53 -04002635/**
Jeff Garzik47c2b672005-11-12 21:13:17 -05002636 * mv_init_host - Perform some early initialization of the host.
Tejun Heo4447d352007-04-17 23:44:08 +09002637 * @host: ATA host to initialize
2638 * @board_idx: controller index
Brett Russ05b308e2005-10-05 17:08:53 -04002639 *
2640 * If possible, do an early global reset of the host. Then do
2641 * our port init and clear/unmask all/relevant host interrupts.
2642 *
2643 * LOCKING:
2644 * Inherited from caller.
2645 */
Tejun Heo4447d352007-04-17 23:44:08 +09002646static int mv_init_host(struct ata_host *host, unsigned int board_idx)
Brett Russ20f733e2005-09-01 18:26:17 -04002647{
2648 int rc = 0, n_hc, port, hc;
Tejun Heo4447d352007-04-17 23:44:08 +09002649 struct mv_host_priv *hpriv = host->private_data;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002650 void __iomem *mmio = hpriv->base;
Jeff Garzik47c2b672005-11-12 21:13:17 -05002651
Tejun Heo4447d352007-04-17 23:44:08 +09002652 rc = mv_chip_id(host, board_idx);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002653 if (rc)
Mark Lord352fab72008-04-19 14:43:42 -04002654 goto done;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002655
2656 if (HAS_PCI(host)) {
Mark Lord352fab72008-04-19 14:43:42 -04002657 hpriv->main_cause_reg_addr = mmio + HC_MAIN_IRQ_CAUSE_OFS;
2658 hpriv->main_mask_reg_addr = mmio + HC_MAIN_IRQ_MASK_OFS;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002659 } else {
Mark Lord352fab72008-04-19 14:43:42 -04002660 hpriv->main_cause_reg_addr = mmio + HC_SOC_MAIN_IRQ_CAUSE_OFS;
2661 hpriv->main_mask_reg_addr = mmio + HC_SOC_MAIN_IRQ_MASK_OFS;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002662 }
Mark Lord352fab72008-04-19 14:43:42 -04002663
2664 /* global interrupt mask: 0 == mask everything */
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002665 writel(0, hpriv->main_mask_reg_addr);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002666
Tejun Heo4447d352007-04-17 23:44:08 +09002667 n_hc = mv_get_hc_count(host->ports[0]->flags);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002668
Tejun Heo4447d352007-04-17 23:44:08 +09002669 for (port = 0; port < host->n_ports; port++)
Jeff Garzik47c2b672005-11-12 21:13:17 -05002670 hpriv->ops->read_preamp(hpriv, port, mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04002671
Jeff Garzikc9d39132005-11-13 17:47:51 -05002672 rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
Jeff Garzik47c2b672005-11-12 21:13:17 -05002673 if (rc)
Brett Russ20f733e2005-09-01 18:26:17 -04002674 goto done;
Brett Russ20f733e2005-09-01 18:26:17 -04002675
Jeff Garzik522479f2005-11-12 22:14:02 -05002676 hpriv->ops->reset_flash(hpriv, mmio);
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002677 hpriv->ops->reset_bus(host, mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -05002678 hpriv->ops->enable_leds(hpriv, mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04002679
Tejun Heo4447d352007-04-17 23:44:08 +09002680 for (port = 0; port < host->n_ports; port++) {
Tejun Heocbcdd872007-08-18 13:14:55 +09002681 struct ata_port *ap = host->ports[port];
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002682 void __iomem *port_mmio = mv_port_base(mmio, port);
Tejun Heocbcdd872007-08-18 13:14:55 +09002683
2684 mv_port_init(&ap->ioaddr, port_mmio);
2685
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002686#ifdef CONFIG_PCI
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002687 if (HAS_PCI(host)) {
2688 unsigned int offset = port_mmio - mmio;
2689 ata_port_pbar_desc(ap, MV_PRIMARY_BAR, -1, "mmio");
2690 ata_port_pbar_desc(ap, MV_PRIMARY_BAR, offset, "port");
2691 }
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002692#endif
Brett Russ20f733e2005-09-01 18:26:17 -04002693 }
2694
2695 for (hc = 0; hc < n_hc; hc++) {
Brett Russ31961942005-09-30 01:36:00 -04002696 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2697
2698 VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
2699 "(before clear)=0x%08x\n", hc,
2700 readl(hc_mmio + HC_CFG_OFS),
2701 readl(hc_mmio + HC_IRQ_CAUSE_OFS));
2702
2703 /* Clear any currently outstanding hc interrupt conditions */
2704 writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04002705 }
2706
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002707 if (HAS_PCI(host)) {
2708 /* Clear any currently outstanding host interrupt conditions */
2709 writelfl(0, mmio + hpriv->irq_cause_ofs);
Brett Russ31961942005-09-30 01:36:00 -04002710
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002711 /* and unmask interrupt generation for host regs */
2712 writelfl(hpriv->unmask_all_irqs, mmio + hpriv->irq_mask_ofs);
2713 if (IS_GEN_I(hpriv))
2714 writelfl(~HC_MAIN_MASKED_IRQS_5,
2715 hpriv->main_mask_reg_addr);
2716 else
2717 writelfl(~HC_MAIN_MASKED_IRQS,
2718 hpriv->main_mask_reg_addr);
Jeff Garzikfb621e22007-02-25 04:19:45 -05002719
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002720 VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
2721 "PCI int cause/mask=0x%08x/0x%08x\n",
2722 readl(hpriv->main_cause_reg_addr),
2723 readl(hpriv->main_mask_reg_addr),
2724 readl(mmio + hpriv->irq_cause_ofs),
2725 readl(mmio + hpriv->irq_mask_ofs));
2726 } else {
2727 writelfl(~HC_MAIN_MASKED_IRQS_SOC,
2728 hpriv->main_mask_reg_addr);
2729 VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x\n",
2730 readl(hpriv->main_cause_reg_addr),
2731 readl(hpriv->main_mask_reg_addr));
2732 }
Brett Russ31961942005-09-30 01:36:00 -04002733done:
Brett Russ20f733e2005-09-01 18:26:17 -04002734 return rc;
2735}
2736
Byron Bradleyfbf14e22008-02-10 21:17:30 +00002737static int mv_create_dma_pools(struct mv_host_priv *hpriv, struct device *dev)
2738{
2739 hpriv->crqb_pool = dmam_pool_create("crqb_q", dev, MV_CRQB_Q_SZ,
2740 MV_CRQB_Q_SZ, 0);
2741 if (!hpriv->crqb_pool)
2742 return -ENOMEM;
2743
2744 hpriv->crpb_pool = dmam_pool_create("crpb_q", dev, MV_CRPB_Q_SZ,
2745 MV_CRPB_Q_SZ, 0);
2746 if (!hpriv->crpb_pool)
2747 return -ENOMEM;
2748
2749 hpriv->sg_tbl_pool = dmam_pool_create("sg_tbl", dev, MV_SG_TBL_SZ,
2750 MV_SG_TBL_SZ, 0);
2751 if (!hpriv->sg_tbl_pool)
2752 return -ENOMEM;
2753
2754 return 0;
2755}
2756
Lennert Buytenhek15a32632008-03-27 14:51:39 -04002757static void mv_conf_mbus_windows(struct mv_host_priv *hpriv,
2758 struct mbus_dram_target_info *dram)
2759{
2760 int i;
2761
2762 for (i = 0; i < 4; i++) {
2763 writel(0, hpriv->base + WINDOW_CTRL(i));
2764 writel(0, hpriv->base + WINDOW_BASE(i));
2765 }
2766
2767 for (i = 0; i < dram->num_cs; i++) {
2768 struct mbus_dram_window *cs = dram->cs + i;
2769
2770 writel(((cs->size - 1) & 0xffff0000) |
2771 (cs->mbus_attr << 8) |
2772 (dram->mbus_dram_target_id << 4) | 1,
2773 hpriv->base + WINDOW_CTRL(i));
2774 writel(cs->base, hpriv->base + WINDOW_BASE(i));
2775 }
2776}
2777
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002778/**
2779 * mv_platform_probe - handle a positive probe of an soc Marvell
2780 * host
2781 * @pdev: platform device found
2782 *
2783 * LOCKING:
2784 * Inherited from caller.
2785 */
2786static int mv_platform_probe(struct platform_device *pdev)
2787{
2788 static int printed_version;
2789 const struct mv_sata_platform_data *mv_platform_data;
2790 const struct ata_port_info *ppi[] =
2791 { &mv_port_info[chip_soc], NULL };
2792 struct ata_host *host;
2793 struct mv_host_priv *hpriv;
2794 struct resource *res;
2795 int n_ports, rc;
2796
2797 if (!printed_version++)
2798 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
2799
2800 /*
2801 * Simple resource validation ..
2802 */
2803 if (unlikely(pdev->num_resources != 2)) {
2804 dev_err(&pdev->dev, "invalid number of resources\n");
2805 return -EINVAL;
2806 }
2807
2808 /*
2809 * Get the register base first
2810 */
2811 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
2812 if (res == NULL)
2813 return -EINVAL;
2814
2815 /* allocate host */
2816 mv_platform_data = pdev->dev.platform_data;
2817 n_ports = mv_platform_data->n_ports;
2818
2819 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
2820 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
2821
2822 if (!host || !hpriv)
2823 return -ENOMEM;
2824 host->private_data = hpriv;
2825 hpriv->n_ports = n_ports;
2826
2827 host->iomap = NULL;
Saeed Bisharaf1cb0ea2008-02-18 07:42:28 -11002828 hpriv->base = devm_ioremap(&pdev->dev, res->start,
2829 res->end - res->start + 1);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002830 hpriv->base -= MV_SATAHC0_REG_BASE;
2831
Lennert Buytenhek15a32632008-03-27 14:51:39 -04002832 /*
2833 * (Re-)program MBUS remapping windows if we are asked to.
2834 */
2835 if (mv_platform_data->dram != NULL)
2836 mv_conf_mbus_windows(hpriv, mv_platform_data->dram);
2837
Byron Bradleyfbf14e22008-02-10 21:17:30 +00002838 rc = mv_create_dma_pools(hpriv, &pdev->dev);
2839 if (rc)
2840 return rc;
2841
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002842 /* initialize adapter */
2843 rc = mv_init_host(host, chip_soc);
2844 if (rc)
2845 return rc;
2846
2847 dev_printk(KERN_INFO, &pdev->dev,
2848 "slots %u ports %d\n", (unsigned)MV_MAX_Q_DEPTH,
2849 host->n_ports);
2850
2851 return ata_host_activate(host, platform_get_irq(pdev, 0), mv_interrupt,
2852 IRQF_SHARED, &mv6_sht);
2853}
2854
2855/*
2856 *
2857 * mv_platform_remove - unplug a platform interface
2858 * @pdev: platform device
2859 *
2860 * A platform bus SATA device has been unplugged. Perform the needed
2861 * cleanup. Also called on module unload for any active devices.
2862 */
2863static int __devexit mv_platform_remove(struct platform_device *pdev)
2864{
2865 struct device *dev = &pdev->dev;
2866 struct ata_host *host = dev_get_drvdata(dev);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002867
2868 ata_host_detach(host);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002869 return 0;
2870}
2871
2872static struct platform_driver mv_platform_driver = {
2873 .probe = mv_platform_probe,
2874 .remove = __devexit_p(mv_platform_remove),
2875 .driver = {
2876 .name = DRV_NAME,
2877 .owner = THIS_MODULE,
2878 },
2879};
2880
2881
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002882#ifdef CONFIG_PCI
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002883static int mv_pci_init_one(struct pci_dev *pdev,
2884 const struct pci_device_id *ent);
2885
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002886
2887static struct pci_driver mv_pci_driver = {
2888 .name = DRV_NAME,
2889 .id_table = mv_pci_tbl,
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002890 .probe = mv_pci_init_one,
Saeed Bishara7bb3c522008-01-30 11:50:45 -11002891 .remove = ata_pci_remove_one,
2892};
2893
2894/*
2895 * module options
2896 */
2897static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
2898
2899
2900/* move to PCI layer or libata core? */
2901static int pci_go_64(struct pci_dev *pdev)
2902{
2903 int rc;
2904
2905 if (!pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
2906 rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
2907 if (rc) {
2908 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
2909 if (rc) {
2910 dev_printk(KERN_ERR, &pdev->dev,
2911 "64-bit DMA enable failed\n");
2912 return rc;
2913 }
2914 }
2915 } else {
2916 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
2917 if (rc) {
2918 dev_printk(KERN_ERR, &pdev->dev,
2919 "32-bit DMA enable failed\n");
2920 return rc;
2921 }
2922 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
2923 if (rc) {
2924 dev_printk(KERN_ERR, &pdev->dev,
2925 "32-bit consistent DMA enable failed\n");
2926 return rc;
2927 }
2928 }
2929
2930 return rc;
2931}
2932
Brett Russ05b308e2005-10-05 17:08:53 -04002933/**
2934 * mv_print_info - Dump key info to kernel log for perusal.
Tejun Heo4447d352007-04-17 23:44:08 +09002935 * @host: ATA host to print info about
Brett Russ05b308e2005-10-05 17:08:53 -04002936 *
2937 * FIXME: complete this.
2938 *
2939 * LOCKING:
2940 * Inherited from caller.
2941 */
Tejun Heo4447d352007-04-17 23:44:08 +09002942static void mv_print_info(struct ata_host *host)
Brett Russ31961942005-09-30 01:36:00 -04002943{
Tejun Heo4447d352007-04-17 23:44:08 +09002944 struct pci_dev *pdev = to_pci_dev(host->dev);
2945 struct mv_host_priv *hpriv = host->private_data;
Auke Kok44c10132007-06-08 15:46:36 -07002946 u8 scc;
Jeff Garzikc1e4fe72007-07-09 12:29:31 -04002947 const char *scc_s, *gen;
Brett Russ31961942005-09-30 01:36:00 -04002948
2949 /* Use this to determine the HW stepping of the chip so we know
2950 * what errata to workaround
2951 */
Brett Russ31961942005-09-30 01:36:00 -04002952 pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
2953 if (scc == 0)
2954 scc_s = "SCSI";
2955 else if (scc == 0x01)
2956 scc_s = "RAID";
2957 else
Jeff Garzikc1e4fe72007-07-09 12:29:31 -04002958 scc_s = "?";
2959
2960 if (IS_GEN_I(hpriv))
2961 gen = "I";
2962 else if (IS_GEN_II(hpriv))
2963 gen = "II";
2964 else if (IS_GEN_IIE(hpriv))
2965 gen = "IIE";
2966 else
2967 gen = "?";
Brett Russ31961942005-09-30 01:36:00 -04002968
Jeff Garzika9524a72005-10-30 14:39:11 -05002969 dev_printk(KERN_INFO, &pdev->dev,
Jeff Garzikc1e4fe72007-07-09 12:29:31 -04002970 "Gen-%s %u slots %u ports %s mode IRQ via %s\n",
2971 gen, (unsigned)MV_MAX_Q_DEPTH, host->n_ports,
Brett Russ31961942005-09-30 01:36:00 -04002972 scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
2973}
2974
Brett Russ05b308e2005-10-05 17:08:53 -04002975/**
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002976 * mv_pci_init_one - handle a positive probe of a PCI Marvell host
Brett Russ05b308e2005-10-05 17:08:53 -04002977 * @pdev: PCI device found
2978 * @ent: PCI device ID entry for the matched host
2979 *
2980 * LOCKING:
2981 * Inherited from caller.
2982 */
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05002983static int mv_pci_init_one(struct pci_dev *pdev,
2984 const struct pci_device_id *ent)
Brett Russ20f733e2005-09-01 18:26:17 -04002985{
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002986 static int printed_version;
Brett Russ20f733e2005-09-01 18:26:17 -04002987 unsigned int board_idx = (unsigned int)ent->driver_data;
Tejun Heo4447d352007-04-17 23:44:08 +09002988 const struct ata_port_info *ppi[] = { &mv_port_info[board_idx], NULL };
2989 struct ata_host *host;
2990 struct mv_host_priv *hpriv;
2991 int n_ports, rc;
Brett Russ20f733e2005-09-01 18:26:17 -04002992
Jeff Garzika9524a72005-10-30 14:39:11 -05002993 if (!printed_version++)
2994 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
Brett Russ20f733e2005-09-01 18:26:17 -04002995
Tejun Heo4447d352007-04-17 23:44:08 +09002996 /* allocate host */
2997 n_ports = mv_get_hc_count(ppi[0]->flags) * MV_PORTS_PER_HC;
2998
2999 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
3000 hpriv = devm_kzalloc(&pdev->dev, sizeof(*hpriv), GFP_KERNEL);
3001 if (!host || !hpriv)
3002 return -ENOMEM;
3003 host->private_data = hpriv;
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003004 hpriv->n_ports = n_ports;
Tejun Heo4447d352007-04-17 23:44:08 +09003005
3006 /* acquire resources */
Tejun Heo24dc5f32007-01-20 16:00:28 +09003007 rc = pcim_enable_device(pdev);
3008 if (rc)
Brett Russ20f733e2005-09-01 18:26:17 -04003009 return rc;
Brett Russ20f733e2005-09-01 18:26:17 -04003010
Tejun Heo0d5ff562007-02-01 15:06:36 +09003011 rc = pcim_iomap_regions(pdev, 1 << MV_PRIMARY_BAR, DRV_NAME);
3012 if (rc == -EBUSY)
Tejun Heo24dc5f32007-01-20 16:00:28 +09003013 pcim_pin_device(pdev);
Tejun Heo0d5ff562007-02-01 15:06:36 +09003014 if (rc)
Tejun Heo24dc5f32007-01-20 16:00:28 +09003015 return rc;
Tejun Heo4447d352007-04-17 23:44:08 +09003016 host->iomap = pcim_iomap_table(pdev);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003017 hpriv->base = host->iomap[MV_PRIMARY_BAR];
Brett Russ20f733e2005-09-01 18:26:17 -04003018
Jeff Garzikd88184f2007-02-26 01:26:06 -05003019 rc = pci_go_64(pdev);
3020 if (rc)
3021 return rc;
3022
Mark Lordda2fa9b2008-01-26 18:32:45 -05003023 rc = mv_create_dma_pools(hpriv, &pdev->dev);
3024 if (rc)
3025 return rc;
3026
Brett Russ20f733e2005-09-01 18:26:17 -04003027 /* initialize adapter */
Tejun Heo4447d352007-04-17 23:44:08 +09003028 rc = mv_init_host(host, board_idx);
Tejun Heo24dc5f32007-01-20 16:00:28 +09003029 if (rc)
3030 return rc;
Brett Russ20f733e2005-09-01 18:26:17 -04003031
Brett Russ31961942005-09-30 01:36:00 -04003032 /* Enable interrupts */
Tejun Heo6a59dcf2007-02-24 15:12:31 +09003033 if (msi && pci_enable_msi(pdev))
Brett Russ31961942005-09-30 01:36:00 -04003034 pci_intx(pdev, 1);
Brett Russ20f733e2005-09-01 18:26:17 -04003035
Brett Russ31961942005-09-30 01:36:00 -04003036 mv_dump_pci_cfg(pdev, 0x68);
Tejun Heo4447d352007-04-17 23:44:08 +09003037 mv_print_info(host);
Brett Russ20f733e2005-09-01 18:26:17 -04003038
Tejun Heo4447d352007-04-17 23:44:08 +09003039 pci_set_master(pdev);
Jeff Garzikea8b4db2007-07-17 02:21:50 -04003040 pci_try_set_mwi(pdev);
Tejun Heo4447d352007-04-17 23:44:08 +09003041 return ata_host_activate(host, pdev->irq, mv_interrupt, IRQF_SHARED,
Jeff Garzikc5d3e452007-07-11 18:30:50 -04003042 IS_GEN_I(hpriv) ? &mv5_sht : &mv6_sht);
Brett Russ20f733e2005-09-01 18:26:17 -04003043}
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003044#endif
Brett Russ20f733e2005-09-01 18:26:17 -04003045
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003046static int mv_platform_probe(struct platform_device *pdev);
3047static int __devexit mv_platform_remove(struct platform_device *pdev);
3048
Brett Russ20f733e2005-09-01 18:26:17 -04003049static int __init mv_init(void)
3050{
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003051 int rc = -ENODEV;
3052#ifdef CONFIG_PCI
3053 rc = pci_register_driver(&mv_pci_driver);
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003054 if (rc < 0)
3055 return rc;
3056#endif
3057 rc = platform_driver_register(&mv_platform_driver);
3058
3059#ifdef CONFIG_PCI
3060 if (rc < 0)
3061 pci_unregister_driver(&mv_pci_driver);
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003062#endif
3063 return rc;
Brett Russ20f733e2005-09-01 18:26:17 -04003064}
3065
3066static void __exit mv_exit(void)
3067{
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003068#ifdef CONFIG_PCI
Brett Russ20f733e2005-09-01 18:26:17 -04003069 pci_unregister_driver(&mv_pci_driver);
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003070#endif
Saeed Bisharaf351b2d2008-02-01 18:08:03 -05003071 platform_driver_unregister(&mv_platform_driver);
Brett Russ20f733e2005-09-01 18:26:17 -04003072}
3073
3074MODULE_AUTHOR("Brett Russ");
3075MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
3076MODULE_LICENSE("GPL");
3077MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
3078MODULE_VERSION(DRV_VERSION);
Mark Lord17c5aab2008-04-16 14:56:51 -04003079MODULE_ALIAS("platform:" DRV_NAME);
Brett Russ20f733e2005-09-01 18:26:17 -04003080
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003081#ifdef CONFIG_PCI
Jeff Garzikddef9bb2006-02-02 16:17:06 -05003082module_param(msi, int, 0444);
3083MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
Saeed Bishara7bb3c522008-01-30 11:50:45 -11003084#endif
Jeff Garzikddef9bb2006-02-02 16:17:06 -05003085
Brett Russ20f733e2005-09-01 18:26:17 -04003086module_init(mv_init);
3087module_exit(mv_exit);