blob: 33e3873543882888f13483b7f7a6c443144342e7 [file] [log] [blame]
Brett Russ20f733e2005-09-01 18:26:17 -04001/*
2 * sata_mv.c - Marvell SATA support
3 *
Jeff Garzik8b260242005-11-12 12:32:50 -05004 * Copyright 2005: EMC Corporation, all rights reserved.
Jeff Garzike2b1be52005-11-18 14:04:23 -05005 * Copyright 2005 Red Hat, Inc. All rights reserved.
Brett Russ20f733e2005-09-01 18:26:17 -04006 *
7 * Please ALWAYS copy linux-ide@vger.kernel.org on emails.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; version 2 of the License.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 *
22 */
23
24#include <linux/kernel.h>
25#include <linux/module.h>
26#include <linux/pci.h>
27#include <linux/init.h>
28#include <linux/blkdev.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
31#include <linux/sched.h>
32#include <linux/dma-mapping.h>
Jeff Garzika9524a72005-10-30 14:39:11 -050033#include <linux/device.h>
Brett Russ20f733e2005-09-01 18:26:17 -040034#include <scsi/scsi_host.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050035#include <scsi/scsi_cmnd.h>
Brett Russ20f733e2005-09-01 18:26:17 -040036#include <linux/libata.h>
37#include <asm/io.h>
38
39#define DRV_NAME "sata_mv"
Jeff Garzike2b1be52005-11-18 14:04:23 -050040#define DRV_VERSION "0.5"
Brett Russ20f733e2005-09-01 18:26:17 -040041
42enum {
43 /* BAR's are enumerated in terms of pci_resource_start() terms */
44 MV_PRIMARY_BAR = 0, /* offset 0x10: memory space */
45 MV_IO_BAR = 2, /* offset 0x18: IO space */
46 MV_MISC_BAR = 3, /* offset 0x1c: FLASH, NVRAM, SRAM */
47
48 MV_MAJOR_REG_AREA_SZ = 0x10000, /* 64KB */
49 MV_MINOR_REG_AREA_SZ = 0x2000, /* 8KB */
50
51 MV_PCI_REG_BASE = 0,
52 MV_IRQ_COAL_REG_BASE = 0x18000, /* 6xxx part only */
53 MV_SATAHC0_REG_BASE = 0x20000,
Jeff Garzik522479f2005-11-12 22:14:02 -050054 MV_FLASH_CTL = 0x1046c,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -050055 MV_GPIO_PORT_CTL = 0x104f0,
56 MV_RESET_CFG = 0x180d8,
Brett Russ20f733e2005-09-01 18:26:17 -040057
58 MV_PCI_REG_SZ = MV_MAJOR_REG_AREA_SZ,
59 MV_SATAHC_REG_SZ = MV_MAJOR_REG_AREA_SZ,
60 MV_SATAHC_ARBTR_REG_SZ = MV_MINOR_REG_AREA_SZ, /* arbiter */
61 MV_PORT_REG_SZ = MV_MINOR_REG_AREA_SZ,
62
Brett Russ31961942005-09-30 01:36:00 -040063 MV_USE_Q_DEPTH = ATA_DEF_QUEUE,
Brett Russ20f733e2005-09-01 18:26:17 -040064
Brett Russ31961942005-09-30 01:36:00 -040065 MV_MAX_Q_DEPTH = 32,
66 MV_MAX_Q_DEPTH_MASK = MV_MAX_Q_DEPTH - 1,
67
68 /* CRQB needs alignment on a 1KB boundary. Size == 1KB
69 * CRPB needs alignment on a 256B boundary. Size == 256B
70 * SG count of 176 leads to MV_PORT_PRIV_DMA_SZ == 4KB
71 * ePRD (SG) entries need alignment on a 16B boundary. Size == 16B
72 */
73 MV_CRQB_Q_SZ = (32 * MV_MAX_Q_DEPTH),
74 MV_CRPB_Q_SZ = (8 * MV_MAX_Q_DEPTH),
75 MV_MAX_SG_CT = 176,
76 MV_SG_TBL_SZ = (16 * MV_MAX_SG_CT),
77 MV_PORT_PRIV_DMA_SZ = (MV_CRQB_Q_SZ + MV_CRPB_Q_SZ + MV_SG_TBL_SZ),
78
Brett Russ20f733e2005-09-01 18:26:17 -040079 MV_PORTS_PER_HC = 4,
80 /* == (port / MV_PORTS_PER_HC) to determine HC from 0-7 port */
81 MV_PORT_HC_SHIFT = 2,
Brett Russ31961942005-09-30 01:36:00 -040082 /* == (port % MV_PORTS_PER_HC) to determine hard port from 0-7 port */
Brett Russ20f733e2005-09-01 18:26:17 -040083 MV_PORT_MASK = 3,
84
85 /* Host Flags */
86 MV_FLAG_DUAL_HC = (1 << 30), /* two SATA Host Controllers */
87 MV_FLAG_IRQ_COALESCE = (1 << 29), /* IRQ coalescing capability */
Brett Russ31961942005-09-30 01:36:00 -040088 MV_COMMON_FLAGS = (ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
Jeff Garzik50630192005-12-13 02:29:45 -050089 ATA_FLAG_SATA_RESET | ATA_FLAG_MMIO |
90 ATA_FLAG_NO_ATAPI),
Jeff Garzik47c2b672005-11-12 21:13:17 -050091 MV_6XXX_FLAGS = MV_FLAG_IRQ_COALESCE,
Brett Russ20f733e2005-09-01 18:26:17 -040092
Brett Russ31961942005-09-30 01:36:00 -040093 CRQB_FLAG_READ = (1 << 0),
94 CRQB_TAG_SHIFT = 1,
95 CRQB_CMD_ADDR_SHIFT = 8,
96 CRQB_CMD_CS = (0x2 << 11),
97 CRQB_CMD_LAST = (1 << 15),
98
99 CRPB_FLAG_STATUS_SHIFT = 8,
100
101 EPRD_FLAG_END_OF_TBL = (1 << 31),
102
Brett Russ20f733e2005-09-01 18:26:17 -0400103 /* PCI interface registers */
104
Brett Russ31961942005-09-30 01:36:00 -0400105 PCI_COMMAND_OFS = 0xc00,
106
Brett Russ20f733e2005-09-01 18:26:17 -0400107 PCI_MAIN_CMD_STS_OFS = 0xd30,
108 STOP_PCI_MASTER = (1 << 2),
109 PCI_MASTER_EMPTY = (1 << 3),
110 GLOB_SFT_RST = (1 << 4),
111
Jeff Garzik522479f2005-11-12 22:14:02 -0500112 MV_PCI_MODE = 0xd00,
113 MV_PCI_EXP_ROM_BAR_CTL = 0xd2c,
114 MV_PCI_DISC_TIMER = 0xd04,
115 MV_PCI_MSI_TRIGGER = 0xc38,
116 MV_PCI_SERR_MASK = 0xc28,
117 MV_PCI_XBAR_TMOUT = 0x1d04,
118 MV_PCI_ERR_LOW_ADDRESS = 0x1d40,
119 MV_PCI_ERR_HIGH_ADDRESS = 0x1d44,
120 MV_PCI_ERR_ATTRIBUTE = 0x1d48,
121 MV_PCI_ERR_COMMAND = 0x1d50,
122
123 PCI_IRQ_CAUSE_OFS = 0x1d58,
124 PCI_IRQ_MASK_OFS = 0x1d5c,
Brett Russ20f733e2005-09-01 18:26:17 -0400125 PCI_UNMASK_ALL_IRQS = 0x7fffff, /* bits 22-0 */
126
127 HC_MAIN_IRQ_CAUSE_OFS = 0x1d60,
128 HC_MAIN_IRQ_MASK_OFS = 0x1d64,
129 PORT0_ERR = (1 << 0), /* shift by port # */
130 PORT0_DONE = (1 << 1), /* shift by port # */
131 HC0_IRQ_PEND = 0x1ff, /* bits 0-8 = HC0's ports */
132 HC_SHIFT = 9, /* bits 9-17 = HC1's ports */
133 PCI_ERR = (1 << 18),
134 TRAN_LO_DONE = (1 << 19), /* 6xxx: IRQ coalescing */
135 TRAN_HI_DONE = (1 << 20), /* 6xxx: IRQ coalescing */
136 PORTS_0_7_COAL_DONE = (1 << 21), /* 6xxx: IRQ coalescing */
137 GPIO_INT = (1 << 22),
138 SELF_INT = (1 << 23),
139 TWSI_INT = (1 << 24),
140 HC_MAIN_RSVD = (0x7f << 25), /* bits 31-25 */
Jeff Garzik8b260242005-11-12 12:32:50 -0500141 HC_MAIN_MASKED_IRQS = (TRAN_LO_DONE | TRAN_HI_DONE |
Brett Russ20f733e2005-09-01 18:26:17 -0400142 PORTS_0_7_COAL_DONE | GPIO_INT | TWSI_INT |
143 HC_MAIN_RSVD),
144
145 /* SATAHC registers */
146 HC_CFG_OFS = 0,
147
148 HC_IRQ_CAUSE_OFS = 0x14,
Brett Russ31961942005-09-30 01:36:00 -0400149 CRPB_DMA_DONE = (1 << 0), /* shift by port # */
Brett Russ20f733e2005-09-01 18:26:17 -0400150 HC_IRQ_COAL = (1 << 4), /* IRQ coalescing */
151 DEV_IRQ = (1 << 8), /* shift by port # */
152
153 /* Shadow block registers */
Brett Russ31961942005-09-30 01:36:00 -0400154 SHD_BLK_OFS = 0x100,
155 SHD_CTL_AST_OFS = 0x20, /* ofs from SHD_BLK_OFS */
Brett Russ20f733e2005-09-01 18:26:17 -0400156
157 /* SATA registers */
158 SATA_STATUS_OFS = 0x300, /* ctrl, err regs follow status */
159 SATA_ACTIVE_OFS = 0x350,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500160 PHY_MODE3 = 0x310,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500161 PHY_MODE4 = 0x314,
162 PHY_MODE2 = 0x330,
Jeff Garzikc9d39132005-11-13 17:47:51 -0500163 MV5_PHY_MODE = 0x74,
164 MV5_LT_MODE = 0x30,
165 MV5_PHY_CTL = 0x0C,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500166 SATA_INTERFACE_CTL = 0x050,
167
168 MV_M2_PREAMP_MASK = 0x7e0,
Brett Russ20f733e2005-09-01 18:26:17 -0400169
170 /* Port registers */
171 EDMA_CFG_OFS = 0,
Brett Russ31961942005-09-30 01:36:00 -0400172 EDMA_CFG_Q_DEPTH = 0, /* queueing disabled */
173 EDMA_CFG_NCQ = (1 << 5),
174 EDMA_CFG_NCQ_GO_ON_ERR = (1 << 14), /* continue on error */
175 EDMA_CFG_RD_BRST_EXT = (1 << 11), /* read burst 512B */
176 EDMA_CFG_WR_BUFF_LEN = (1 << 13), /* write buffer 512B */
Brett Russ20f733e2005-09-01 18:26:17 -0400177
178 EDMA_ERR_IRQ_CAUSE_OFS = 0x8,
179 EDMA_ERR_IRQ_MASK_OFS = 0xc,
180 EDMA_ERR_D_PAR = (1 << 0),
181 EDMA_ERR_PRD_PAR = (1 << 1),
182 EDMA_ERR_DEV = (1 << 2),
183 EDMA_ERR_DEV_DCON = (1 << 3),
184 EDMA_ERR_DEV_CON = (1 << 4),
185 EDMA_ERR_SERR = (1 << 5),
186 EDMA_ERR_SELF_DIS = (1 << 7),
187 EDMA_ERR_BIST_ASYNC = (1 << 8),
188 EDMA_ERR_CRBQ_PAR = (1 << 9),
189 EDMA_ERR_CRPB_PAR = (1 << 10),
190 EDMA_ERR_INTRL_PAR = (1 << 11),
191 EDMA_ERR_IORDY = (1 << 12),
192 EDMA_ERR_LNK_CTRL_RX = (0xf << 13),
193 EDMA_ERR_LNK_CTRL_RX_2 = (1 << 15),
194 EDMA_ERR_LNK_DATA_RX = (0xf << 17),
195 EDMA_ERR_LNK_CTRL_TX = (0x1f << 21),
196 EDMA_ERR_LNK_DATA_TX = (0x1f << 26),
197 EDMA_ERR_TRANS_PROTO = (1 << 31),
Jeff Garzik8b260242005-11-12 12:32:50 -0500198 EDMA_ERR_FATAL = (EDMA_ERR_D_PAR | EDMA_ERR_PRD_PAR |
Brett Russ20f733e2005-09-01 18:26:17 -0400199 EDMA_ERR_DEV_DCON | EDMA_ERR_CRBQ_PAR |
200 EDMA_ERR_CRPB_PAR | EDMA_ERR_INTRL_PAR |
Jeff Garzik8b260242005-11-12 12:32:50 -0500201 EDMA_ERR_IORDY | EDMA_ERR_LNK_CTRL_RX_2 |
Brett Russ20f733e2005-09-01 18:26:17 -0400202 EDMA_ERR_LNK_DATA_RX |
Jeff Garzik8b260242005-11-12 12:32:50 -0500203 EDMA_ERR_LNK_DATA_TX |
Brett Russ20f733e2005-09-01 18:26:17 -0400204 EDMA_ERR_TRANS_PROTO),
205
Brett Russ31961942005-09-30 01:36:00 -0400206 EDMA_REQ_Q_BASE_HI_OFS = 0x10,
207 EDMA_REQ_Q_IN_PTR_OFS = 0x14, /* also contains BASE_LO */
Brett Russ31961942005-09-30 01:36:00 -0400208
209 EDMA_REQ_Q_OUT_PTR_OFS = 0x18,
210 EDMA_REQ_Q_PTR_SHIFT = 5,
211
212 EDMA_RSP_Q_BASE_HI_OFS = 0x1c,
213 EDMA_RSP_Q_IN_PTR_OFS = 0x20,
214 EDMA_RSP_Q_OUT_PTR_OFS = 0x24, /* also contains BASE_LO */
Brett Russ31961942005-09-30 01:36:00 -0400215 EDMA_RSP_Q_PTR_SHIFT = 3,
216
Brett Russ20f733e2005-09-01 18:26:17 -0400217 EDMA_CMD_OFS = 0x28,
218 EDMA_EN = (1 << 0),
219 EDMA_DS = (1 << 1),
220 ATA_RST = (1 << 2),
221
Jeff Garzikc9d39132005-11-13 17:47:51 -0500222 EDMA_IORDY_TMOUT = 0x34,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500223 EDMA_ARB_CFG = 0x38,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500224
Brett Russ31961942005-09-30 01:36:00 -0400225 /* Host private flags (hp_flags) */
226 MV_HP_FLAG_MSI = (1 << 0),
Jeff Garzik47c2b672005-11-12 21:13:17 -0500227 MV_HP_ERRATA_50XXB0 = (1 << 1),
228 MV_HP_ERRATA_50XXB2 = (1 << 2),
229 MV_HP_ERRATA_60X1B2 = (1 << 3),
230 MV_HP_ERRATA_60X1C0 = (1 << 4),
231 MV_HP_50XX = (1 << 5),
Brett Russ20f733e2005-09-01 18:26:17 -0400232
Brett Russ31961942005-09-30 01:36:00 -0400233 /* Port private flags (pp_flags) */
234 MV_PP_FLAG_EDMA_EN = (1 << 0),
235 MV_PP_FLAG_EDMA_DS_ACT = (1 << 1),
236};
237
Jeff Garzikc9d39132005-11-13 17:47:51 -0500238#define IS_50XX(hpriv) ((hpriv)->hp_flags & MV_HP_50XX)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500239#define IS_60XX(hpriv) (((hpriv)->hp_flags & MV_HP_50XX) == 0)
240
Jeff Garzik095fec82005-11-12 09:50:49 -0500241enum {
242 /* Our DMA boundary is determined by an ePRD being unable to handle
243 * anything larger than 64KB
244 */
245 MV_DMA_BOUNDARY = 0xffffU,
246
247 EDMA_REQ_Q_BASE_LO_MASK = 0xfffffc00U,
248
249 EDMA_RSP_Q_BASE_LO_MASK = 0xffffff00U,
250};
251
Jeff Garzik522479f2005-11-12 22:14:02 -0500252enum chip_type {
253 chip_504x,
254 chip_508x,
255 chip_5080,
256 chip_604x,
257 chip_608x,
258};
259
Brett Russ31961942005-09-30 01:36:00 -0400260/* Command ReQuest Block: 32B */
261struct mv_crqb {
262 u32 sg_addr;
263 u32 sg_addr_hi;
264 u16 ctrl_flags;
265 u16 ata_cmd[11];
266};
267
268/* Command ResPonse Block: 8B */
269struct mv_crpb {
270 u16 id;
271 u16 flags;
272 u32 tmstmp;
273};
274
275/* EDMA Physical Region Descriptor (ePRD); A.K.A. SG */
276struct mv_sg {
277 u32 addr;
278 u32 flags_size;
279 u32 addr_hi;
280 u32 reserved;
Brett Russ20f733e2005-09-01 18:26:17 -0400281};
282
283struct mv_port_priv {
Brett Russ31961942005-09-30 01:36:00 -0400284 struct mv_crqb *crqb;
285 dma_addr_t crqb_dma;
286 struct mv_crpb *crpb;
287 dma_addr_t crpb_dma;
288 struct mv_sg *sg_tbl;
289 dma_addr_t sg_tbl_dma;
Brett Russ20f733e2005-09-01 18:26:17 -0400290
Brett Russ31961942005-09-30 01:36:00 -0400291 unsigned req_producer; /* cp of req_in_ptr */
292 unsigned rsp_consumer; /* cp of rsp_out_ptr */
293 u32 pp_flags;
Brett Russ20f733e2005-09-01 18:26:17 -0400294};
295
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500296struct mv_port_signal {
297 u32 amps;
298 u32 pre;
299};
300
Jeff Garzik47c2b672005-11-12 21:13:17 -0500301struct mv_host_priv;
302struct mv_hw_ops {
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500303 void (*phy_errata)(struct mv_host_priv *hpriv, void __iomem *mmio,
304 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500305 void (*enable_leds)(struct mv_host_priv *hpriv, void __iomem *mmio);
306 void (*read_preamp)(struct mv_host_priv *hpriv, int idx,
307 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500308 int (*reset_hc)(struct mv_host_priv *hpriv, void __iomem *mmio,
309 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500310 void (*reset_flash)(struct mv_host_priv *hpriv, void __iomem *mmio);
311 void (*reset_bus)(struct pci_dev *pdev, void __iomem *mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500312};
313
Brett Russ20f733e2005-09-01 18:26:17 -0400314struct mv_host_priv {
Brett Russ31961942005-09-30 01:36:00 -0400315 u32 hp_flags;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500316 struct mv_port_signal signal[8];
Jeff Garzik47c2b672005-11-12 21:13:17 -0500317 const struct mv_hw_ops *ops;
Brett Russ20f733e2005-09-01 18:26:17 -0400318};
319
320static void mv_irq_clear(struct ata_port *ap);
321static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
322static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500323static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in);
324static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val);
Brett Russ20f733e2005-09-01 18:26:17 -0400325static void mv_phy_reset(struct ata_port *ap);
Jeff Garzik22374672005-11-17 10:59:48 -0500326static void __mv_phy_reset(struct ata_port *ap, int can_sleep);
Brett Russ31961942005-09-30 01:36:00 -0400327static void mv_host_stop(struct ata_host_set *host_set);
328static int mv_port_start(struct ata_port *ap);
329static void mv_port_stop(struct ata_port *ap);
330static void mv_qc_prep(struct ata_queued_cmd *qc);
331static int mv_qc_issue(struct ata_queued_cmd *qc);
Brett Russ20f733e2005-09-01 18:26:17 -0400332static irqreturn_t mv_interrupt(int irq, void *dev_instance,
333 struct pt_regs *regs);
Brett Russ31961942005-09-30 01:36:00 -0400334static void mv_eng_timeout(struct ata_port *ap);
Brett Russ20f733e2005-09-01 18:26:17 -0400335static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
336
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500337static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
338 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500339static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
340static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
341 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500342static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
343 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500344static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
345static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500346
Jeff Garzik2a47ce02005-11-12 23:05:14 -0500347static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
348 unsigned int port);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500349static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio);
350static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
351 void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500352static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
353 unsigned int n_hc);
Jeff Garzik522479f2005-11-12 22:14:02 -0500354static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio);
355static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio);
Jeff Garzikc9d39132005-11-13 17:47:51 -0500356static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
357 unsigned int port_no);
358static void mv_stop_and_reset(struct ata_port *ap);
Jeff Garzik47c2b672005-11-12 21:13:17 -0500359
Jeff Garzik193515d2005-11-07 00:59:37 -0500360static struct scsi_host_template mv_sht = {
Brett Russ20f733e2005-09-01 18:26:17 -0400361 .module = THIS_MODULE,
362 .name = DRV_NAME,
363 .ioctl = ata_scsi_ioctl,
364 .queuecommand = ata_scsi_queuecmd,
365 .eh_strategy_handler = ata_scsi_error,
Brett Russ31961942005-09-30 01:36:00 -0400366 .can_queue = MV_USE_Q_DEPTH,
Brett Russ20f733e2005-09-01 18:26:17 -0400367 .this_id = ATA_SHT_THIS_ID,
Jeff Garzik22374672005-11-17 10:59:48 -0500368 .sg_tablesize = MV_MAX_SG_CT / 2,
Brett Russ20f733e2005-09-01 18:26:17 -0400369 .max_sectors = ATA_MAX_SECTORS,
370 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
371 .emulated = ATA_SHT_EMULATED,
Brett Russ31961942005-09-30 01:36:00 -0400372 .use_clustering = ATA_SHT_USE_CLUSTERING,
Brett Russ20f733e2005-09-01 18:26:17 -0400373 .proc_name = DRV_NAME,
374 .dma_boundary = MV_DMA_BOUNDARY,
375 .slave_configure = ata_scsi_slave_config,
376 .bios_param = ata_std_bios_param,
Brett Russ20f733e2005-09-01 18:26:17 -0400377};
378
Jeff Garzikc9d39132005-11-13 17:47:51 -0500379static const struct ata_port_operations mv5_ops = {
380 .port_disable = ata_port_disable,
381
382 .tf_load = ata_tf_load,
383 .tf_read = ata_tf_read,
384 .check_status = ata_check_status,
385 .exec_command = ata_exec_command,
386 .dev_select = ata_std_dev_select,
387
388 .phy_reset = mv_phy_reset,
389
390 .qc_prep = mv_qc_prep,
391 .qc_issue = mv_qc_issue,
392
393 .eng_timeout = mv_eng_timeout,
394
395 .irq_handler = mv_interrupt,
396 .irq_clear = mv_irq_clear,
397
398 .scr_read = mv5_scr_read,
399 .scr_write = mv5_scr_write,
400
401 .port_start = mv_port_start,
402 .port_stop = mv_port_stop,
403 .host_stop = mv_host_stop,
404};
405
406static const struct ata_port_operations mv6_ops = {
Brett Russ20f733e2005-09-01 18:26:17 -0400407 .port_disable = ata_port_disable,
408
409 .tf_load = ata_tf_load,
410 .tf_read = ata_tf_read,
411 .check_status = ata_check_status,
412 .exec_command = ata_exec_command,
413 .dev_select = ata_std_dev_select,
414
415 .phy_reset = mv_phy_reset,
416
Brett Russ31961942005-09-30 01:36:00 -0400417 .qc_prep = mv_qc_prep,
418 .qc_issue = mv_qc_issue,
Brett Russ20f733e2005-09-01 18:26:17 -0400419
Brett Russ31961942005-09-30 01:36:00 -0400420 .eng_timeout = mv_eng_timeout,
Brett Russ20f733e2005-09-01 18:26:17 -0400421
422 .irq_handler = mv_interrupt,
423 .irq_clear = mv_irq_clear,
424
425 .scr_read = mv_scr_read,
426 .scr_write = mv_scr_write,
427
Brett Russ31961942005-09-30 01:36:00 -0400428 .port_start = mv_port_start,
429 .port_stop = mv_port_stop,
430 .host_stop = mv_host_stop,
Brett Russ20f733e2005-09-01 18:26:17 -0400431};
432
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100433static const struct ata_port_info mv_port_info[] = {
Brett Russ20f733e2005-09-01 18:26:17 -0400434 { /* chip_504x */
435 .sht = &mv_sht,
Brett Russ31961942005-09-30 01:36:00 -0400436 .host_flags = MV_COMMON_FLAGS,
437 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikc9d39132005-11-13 17:47:51 -0500438 .udma_mask = 0x7f, /* udma0-6 */
439 .port_ops = &mv5_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400440 },
441 { /* chip_508x */
442 .sht = &mv_sht,
Brett Russ31961942005-09-30 01:36:00 -0400443 .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
444 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikc9d39132005-11-13 17:47:51 -0500445 .udma_mask = 0x7f, /* udma0-6 */
446 .port_ops = &mv5_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400447 },
Jeff Garzik47c2b672005-11-12 21:13:17 -0500448 { /* chip_5080 */
449 .sht = &mv_sht,
450 .host_flags = (MV_COMMON_FLAGS | MV_FLAG_DUAL_HC),
451 .pio_mask = 0x1f, /* pio0-4 */
Jeff Garzikc9d39132005-11-13 17:47:51 -0500452 .udma_mask = 0x7f, /* udma0-6 */
453 .port_ops = &mv5_ops,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500454 },
Brett Russ20f733e2005-09-01 18:26:17 -0400455 { /* chip_604x */
456 .sht = &mv_sht,
Brett Russ31961942005-09-30 01:36:00 -0400457 .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS),
458 .pio_mask = 0x1f, /* pio0-4 */
459 .udma_mask = 0x7f, /* udma0-6 */
Jeff Garzikc9d39132005-11-13 17:47:51 -0500460 .port_ops = &mv6_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400461 },
462 { /* chip_608x */
463 .sht = &mv_sht,
Jeff Garzik8b260242005-11-12 12:32:50 -0500464 .host_flags = (MV_COMMON_FLAGS | MV_6XXX_FLAGS |
Brett Russ31961942005-09-30 01:36:00 -0400465 MV_FLAG_DUAL_HC),
466 .pio_mask = 0x1f, /* pio0-4 */
467 .udma_mask = 0x7f, /* udma0-6 */
Jeff Garzikc9d39132005-11-13 17:47:51 -0500468 .port_ops = &mv6_ops,
Brett Russ20f733e2005-09-01 18:26:17 -0400469 },
470};
471
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500472static const struct pci_device_id mv_pci_tbl[] = {
Brett Russ20f733e2005-09-01 18:26:17 -0400473 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5040), 0, 0, chip_504x},
474 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5041), 0, 0, chip_504x},
Jeff Garzik47c2b672005-11-12 21:13:17 -0500475 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5080), 0, 0, chip_5080},
Brett Russ20f733e2005-09-01 18:26:17 -0400476 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x5081), 0, 0, chip_508x},
477
478 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6040), 0, 0, chip_604x},
479 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6041), 0, 0, chip_604x},
480 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6080), 0, 0, chip_608x},
481 {PCI_DEVICE(PCI_VENDOR_ID_MARVELL, 0x6081), 0, 0, chip_608x},
Jeff Garzik29179532005-11-11 08:08:03 -0500482
483 {PCI_DEVICE(PCI_VENDOR_ID_ADAPTEC2, 0x0241), 0, 0, chip_604x},
Brett Russ20f733e2005-09-01 18:26:17 -0400484 {} /* terminate list */
485};
486
487static struct pci_driver mv_pci_driver = {
488 .name = DRV_NAME,
489 .id_table = mv_pci_tbl,
490 .probe = mv_init_one,
491 .remove = ata_pci_remove_one,
492};
493
Jeff Garzik47c2b672005-11-12 21:13:17 -0500494static const struct mv_hw_ops mv5xxx_ops = {
495 .phy_errata = mv5_phy_errata,
496 .enable_leds = mv5_enable_leds,
497 .read_preamp = mv5_read_preamp,
498 .reset_hc = mv5_reset_hc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500499 .reset_flash = mv5_reset_flash,
500 .reset_bus = mv5_reset_bus,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500501};
502
503static const struct mv_hw_ops mv6xxx_ops = {
504 .phy_errata = mv6_phy_errata,
505 .enable_leds = mv6_enable_leds,
506 .read_preamp = mv6_read_preamp,
507 .reset_hc = mv6_reset_hc,
Jeff Garzik522479f2005-11-12 22:14:02 -0500508 .reset_flash = mv6_reset_flash,
509 .reset_bus = mv_reset_pci_bus,
Jeff Garzik47c2b672005-11-12 21:13:17 -0500510};
511
Brett Russ20f733e2005-09-01 18:26:17 -0400512/*
Jeff Garzikddef9bb2006-02-02 16:17:06 -0500513 * module options
514 */
515static int msi; /* Use PCI msi; either zero (off, default) or non-zero */
516
517
518/*
Brett Russ20f733e2005-09-01 18:26:17 -0400519 * Functions
520 */
521
522static inline void writelfl(unsigned long data, void __iomem *addr)
523{
524 writel(data, addr);
525 (void) readl(addr); /* flush to avoid PCI posted write */
526}
527
Brett Russ20f733e2005-09-01 18:26:17 -0400528static inline void __iomem *mv_hc_base(void __iomem *base, unsigned int hc)
529{
530 return (base + MV_SATAHC0_REG_BASE + (hc * MV_SATAHC_REG_SZ));
531}
532
Jeff Garzikc9d39132005-11-13 17:47:51 -0500533static inline unsigned int mv_hc_from_port(unsigned int port)
534{
535 return port >> MV_PORT_HC_SHIFT;
536}
537
538static inline unsigned int mv_hardport_from_port(unsigned int port)
539{
540 return port & MV_PORT_MASK;
541}
542
543static inline void __iomem *mv_hc_base_from_port(void __iomem *base,
544 unsigned int port)
545{
546 return mv_hc_base(base, mv_hc_from_port(port));
547}
548
Brett Russ20f733e2005-09-01 18:26:17 -0400549static inline void __iomem *mv_port_base(void __iomem *base, unsigned int port)
550{
Jeff Garzikc9d39132005-11-13 17:47:51 -0500551 return mv_hc_base_from_port(base, port) +
Jeff Garzik8b260242005-11-12 12:32:50 -0500552 MV_SATAHC_ARBTR_REG_SZ +
Jeff Garzikc9d39132005-11-13 17:47:51 -0500553 (mv_hardport_from_port(port) * MV_PORT_REG_SZ);
Brett Russ20f733e2005-09-01 18:26:17 -0400554}
555
556static inline void __iomem *mv_ap_base(struct ata_port *ap)
557{
558 return mv_port_base(ap->host_set->mmio_base, ap->port_no);
559}
560
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500561static inline int mv_get_hc_count(unsigned long host_flags)
Brett Russ20f733e2005-09-01 18:26:17 -0400562{
Jeff Garzikbca1c4e2005-11-12 12:48:15 -0500563 return ((host_flags & MV_FLAG_DUAL_HC) ? 2 : 1);
Brett Russ20f733e2005-09-01 18:26:17 -0400564}
565
566static void mv_irq_clear(struct ata_port *ap)
567{
568}
569
Brett Russ05b308e2005-10-05 17:08:53 -0400570/**
571 * mv_start_dma - Enable eDMA engine
572 * @base: port base address
573 * @pp: port private data
574 *
575 * Verify the local cache of the eDMA state is accurate with an
576 * assert.
577 *
578 * LOCKING:
579 * Inherited from caller.
580 */
Brett Russafb0edd2005-10-05 17:08:42 -0400581static void mv_start_dma(void __iomem *base, struct mv_port_priv *pp)
Brett Russ31961942005-09-30 01:36:00 -0400582{
Brett Russafb0edd2005-10-05 17:08:42 -0400583 if (!(MV_PP_FLAG_EDMA_EN & pp->pp_flags)) {
584 writelfl(EDMA_EN, base + EDMA_CMD_OFS);
585 pp->pp_flags |= MV_PP_FLAG_EDMA_EN;
586 }
587 assert(EDMA_EN & readl(base + EDMA_CMD_OFS));
Brett Russ31961942005-09-30 01:36:00 -0400588}
589
Brett Russ05b308e2005-10-05 17:08:53 -0400590/**
591 * mv_stop_dma - Disable eDMA engine
592 * @ap: ATA channel to manipulate
593 *
594 * Verify the local cache of the eDMA state is accurate with an
595 * assert.
596 *
597 * LOCKING:
598 * Inherited from caller.
599 */
Brett Russ31961942005-09-30 01:36:00 -0400600static void mv_stop_dma(struct ata_port *ap)
601{
602 void __iomem *port_mmio = mv_ap_base(ap);
603 struct mv_port_priv *pp = ap->private_data;
Brett Russ31961942005-09-30 01:36:00 -0400604 u32 reg;
605 int i;
606
Brett Russafb0edd2005-10-05 17:08:42 -0400607 if (MV_PP_FLAG_EDMA_EN & pp->pp_flags) {
608 /* Disable EDMA if active. The disable bit auto clears.
Brett Russ31961942005-09-30 01:36:00 -0400609 */
Brett Russ31961942005-09-30 01:36:00 -0400610 writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
611 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
Brett Russafb0edd2005-10-05 17:08:42 -0400612 } else {
613 assert(!(EDMA_EN & readl(port_mmio + EDMA_CMD_OFS)));
614 }
Jeff Garzik8b260242005-11-12 12:32:50 -0500615
Brett Russ31961942005-09-30 01:36:00 -0400616 /* now properly wait for the eDMA to stop */
617 for (i = 1000; i > 0; i--) {
618 reg = readl(port_mmio + EDMA_CMD_OFS);
619 if (!(EDMA_EN & reg)) {
620 break;
621 }
622 udelay(100);
623 }
624
Brett Russ31961942005-09-30 01:36:00 -0400625 if (EDMA_EN & reg) {
626 printk(KERN_ERR "ata%u: Unable to stop eDMA\n", ap->id);
Brett Russafb0edd2005-10-05 17:08:42 -0400627 /* FIXME: Consider doing a reset here to recover */
Brett Russ31961942005-09-30 01:36:00 -0400628 }
629}
630
Jeff Garzik8a70f8d2005-10-05 17:19:47 -0400631#ifdef ATA_DEBUG
Brett Russ31961942005-09-30 01:36:00 -0400632static void mv_dump_mem(void __iomem *start, unsigned bytes)
633{
Brett Russ31961942005-09-30 01:36:00 -0400634 int b, w;
635 for (b = 0; b < bytes; ) {
636 DPRINTK("%p: ", start + b);
637 for (w = 0; b < bytes && w < 4; w++) {
638 printk("%08x ",readl(start + b));
639 b += sizeof(u32);
640 }
641 printk("\n");
642 }
Brett Russ31961942005-09-30 01:36:00 -0400643}
Jeff Garzik8a70f8d2005-10-05 17:19:47 -0400644#endif
645
Brett Russ31961942005-09-30 01:36:00 -0400646static void mv_dump_pci_cfg(struct pci_dev *pdev, unsigned bytes)
647{
648#ifdef ATA_DEBUG
649 int b, w;
650 u32 dw;
651 for (b = 0; b < bytes; ) {
652 DPRINTK("%02x: ", b);
653 for (w = 0; b < bytes && w < 4; w++) {
654 (void) pci_read_config_dword(pdev,b,&dw);
655 printk("%08x ",dw);
656 b += sizeof(u32);
657 }
658 printk("\n");
659 }
660#endif
661}
662static void mv_dump_all_regs(void __iomem *mmio_base, int port,
663 struct pci_dev *pdev)
664{
665#ifdef ATA_DEBUG
Jeff Garzik8b260242005-11-12 12:32:50 -0500666 void __iomem *hc_base = mv_hc_base(mmio_base,
Brett Russ31961942005-09-30 01:36:00 -0400667 port >> MV_PORT_HC_SHIFT);
668 void __iomem *port_base;
669 int start_port, num_ports, p, start_hc, num_hcs, hc;
670
671 if (0 > port) {
672 start_hc = start_port = 0;
673 num_ports = 8; /* shld be benign for 4 port devs */
674 num_hcs = 2;
675 } else {
676 start_hc = port >> MV_PORT_HC_SHIFT;
677 start_port = port;
678 num_ports = num_hcs = 1;
679 }
Jeff Garzik8b260242005-11-12 12:32:50 -0500680 DPRINTK("All registers for port(s) %u-%u:\n", start_port,
Brett Russ31961942005-09-30 01:36:00 -0400681 num_ports > 1 ? num_ports - 1 : start_port);
682
683 if (NULL != pdev) {
684 DPRINTK("PCI config space regs:\n");
685 mv_dump_pci_cfg(pdev, 0x68);
686 }
687 DPRINTK("PCI regs:\n");
688 mv_dump_mem(mmio_base+0xc00, 0x3c);
689 mv_dump_mem(mmio_base+0xd00, 0x34);
690 mv_dump_mem(mmio_base+0xf00, 0x4);
691 mv_dump_mem(mmio_base+0x1d00, 0x6c);
692 for (hc = start_hc; hc < start_hc + num_hcs; hc++) {
693 hc_base = mv_hc_base(mmio_base, port >> MV_PORT_HC_SHIFT);
694 DPRINTK("HC regs (HC %i):\n", hc);
695 mv_dump_mem(hc_base, 0x1c);
696 }
697 for (p = start_port; p < start_port + num_ports; p++) {
698 port_base = mv_port_base(mmio_base, p);
699 DPRINTK("EDMA regs (port %i):\n",p);
700 mv_dump_mem(port_base, 0x54);
701 DPRINTK("SATA regs (port %i):\n",p);
702 mv_dump_mem(port_base+0x300, 0x60);
703 }
704#endif
705}
706
Brett Russ20f733e2005-09-01 18:26:17 -0400707static unsigned int mv_scr_offset(unsigned int sc_reg_in)
708{
709 unsigned int ofs;
710
711 switch (sc_reg_in) {
712 case SCR_STATUS:
713 case SCR_CONTROL:
714 case SCR_ERROR:
715 ofs = SATA_STATUS_OFS + (sc_reg_in * sizeof(u32));
716 break;
717 case SCR_ACTIVE:
718 ofs = SATA_ACTIVE_OFS; /* active is not with the others */
719 break;
720 default:
721 ofs = 0xffffffffU;
722 break;
723 }
724 return ofs;
725}
726
727static u32 mv_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
728{
729 unsigned int ofs = mv_scr_offset(sc_reg_in);
730
731 if (0xffffffffU != ofs) {
732 return readl(mv_ap_base(ap) + ofs);
733 } else {
734 return (u32) ofs;
735 }
736}
737
738static void mv_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
739{
740 unsigned int ofs = mv_scr_offset(sc_reg_in);
741
742 if (0xffffffffU != ofs) {
743 writelfl(val, mv_ap_base(ap) + ofs);
744 }
745}
746
Brett Russ05b308e2005-10-05 17:08:53 -0400747/**
748 * mv_host_stop - Host specific cleanup/stop routine.
749 * @host_set: host data structure
750 *
751 * Disable ints, cleanup host memory, call general purpose
752 * host_stop.
753 *
754 * LOCKING:
755 * Inherited from caller.
756 */
Brett Russ31961942005-09-30 01:36:00 -0400757static void mv_host_stop(struct ata_host_set *host_set)
758{
759 struct mv_host_priv *hpriv = host_set->private_data;
760 struct pci_dev *pdev = to_pci_dev(host_set->dev);
761
762 if (hpriv->hp_flags & MV_HP_FLAG_MSI) {
763 pci_disable_msi(pdev);
764 } else {
765 pci_intx(pdev, 0);
766 }
767 kfree(hpriv);
768 ata_host_stop(host_set);
769}
770
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500771static inline void mv_priv_free(struct mv_port_priv *pp, struct device *dev)
772{
773 dma_free_coherent(dev, MV_PORT_PRIV_DMA_SZ, pp->crpb, pp->crpb_dma);
774}
775
Brett Russ05b308e2005-10-05 17:08:53 -0400776/**
777 * mv_port_start - Port specific init/start routine.
778 * @ap: ATA channel to manipulate
779 *
780 * Allocate and point to DMA memory, init port private memory,
781 * zero indices.
782 *
783 * LOCKING:
784 * Inherited from caller.
785 */
Brett Russ31961942005-09-30 01:36:00 -0400786static int mv_port_start(struct ata_port *ap)
787{
788 struct device *dev = ap->host_set->dev;
789 struct mv_port_priv *pp;
790 void __iomem *port_mmio = mv_ap_base(ap);
791 void *mem;
792 dma_addr_t mem_dma;
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500793 int rc = -ENOMEM;
Brett Russ31961942005-09-30 01:36:00 -0400794
795 pp = kmalloc(sizeof(*pp), GFP_KERNEL);
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500796 if (!pp)
797 goto err_out;
Brett Russ31961942005-09-30 01:36:00 -0400798 memset(pp, 0, sizeof(*pp));
799
Jeff Garzik8b260242005-11-12 12:32:50 -0500800 mem = dma_alloc_coherent(dev, MV_PORT_PRIV_DMA_SZ, &mem_dma,
Brett Russ31961942005-09-30 01:36:00 -0400801 GFP_KERNEL);
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500802 if (!mem)
803 goto err_out_pp;
Brett Russ31961942005-09-30 01:36:00 -0400804 memset(mem, 0, MV_PORT_PRIV_DMA_SZ);
805
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500806 rc = ata_pad_alloc(ap, dev);
807 if (rc)
808 goto err_out_priv;
809
Jeff Garzik8b260242005-11-12 12:32:50 -0500810 /* First item in chunk of DMA memory:
Brett Russ31961942005-09-30 01:36:00 -0400811 * 32-slot command request table (CRQB), 32 bytes each in size
812 */
813 pp->crqb = mem;
814 pp->crqb_dma = mem_dma;
815 mem += MV_CRQB_Q_SZ;
816 mem_dma += MV_CRQB_Q_SZ;
817
Jeff Garzik8b260242005-11-12 12:32:50 -0500818 /* Second item:
Brett Russ31961942005-09-30 01:36:00 -0400819 * 32-slot command response table (CRPB), 8 bytes each in size
820 */
821 pp->crpb = mem;
822 pp->crpb_dma = mem_dma;
823 mem += MV_CRPB_Q_SZ;
824 mem_dma += MV_CRPB_Q_SZ;
825
826 /* Third item:
827 * Table of scatter-gather descriptors (ePRD), 16 bytes each
828 */
829 pp->sg_tbl = mem;
830 pp->sg_tbl_dma = mem_dma;
831
Jeff Garzik8b260242005-11-12 12:32:50 -0500832 writelfl(EDMA_CFG_Q_DEPTH | EDMA_CFG_RD_BRST_EXT |
Brett Russ31961942005-09-30 01:36:00 -0400833 EDMA_CFG_WR_BUFF_LEN, port_mmio + EDMA_CFG_OFS);
834
835 writel((pp->crqb_dma >> 16) >> 16, port_mmio + EDMA_REQ_Q_BASE_HI_OFS);
Jeff Garzik8b260242005-11-12 12:32:50 -0500836 writelfl(pp->crqb_dma & EDMA_REQ_Q_BASE_LO_MASK,
Brett Russ31961942005-09-30 01:36:00 -0400837 port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
838
839 writelfl(0, port_mmio + EDMA_REQ_Q_OUT_PTR_OFS);
840 writelfl(0, port_mmio + EDMA_RSP_Q_IN_PTR_OFS);
841
842 writel((pp->crpb_dma >> 16) >> 16, port_mmio + EDMA_RSP_Q_BASE_HI_OFS);
Jeff Garzik8b260242005-11-12 12:32:50 -0500843 writelfl(pp->crpb_dma & EDMA_RSP_Q_BASE_LO_MASK,
Brett Russ31961942005-09-30 01:36:00 -0400844 port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
845
846 pp->req_producer = pp->rsp_consumer = 0;
847
848 /* Don't turn on EDMA here...do it before DMA commands only. Else
849 * we'll be unable to send non-data, PIO, etc due to restricted access
850 * to shadow regs.
851 */
852 ap->private_data = pp;
853 return 0;
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500854
855err_out_priv:
856 mv_priv_free(pp, dev);
857err_out_pp:
858 kfree(pp);
859err_out:
860 return rc;
Brett Russ31961942005-09-30 01:36:00 -0400861}
862
Brett Russ05b308e2005-10-05 17:08:53 -0400863/**
864 * mv_port_stop - Port specific cleanup/stop routine.
865 * @ap: ATA channel to manipulate
866 *
867 * Stop DMA, cleanup port memory.
868 *
869 * LOCKING:
870 * This routine uses the host_set lock to protect the DMA stop.
871 */
Brett Russ31961942005-09-30 01:36:00 -0400872static void mv_port_stop(struct ata_port *ap)
873{
874 struct device *dev = ap->host_set->dev;
875 struct mv_port_priv *pp = ap->private_data;
Brett Russafb0edd2005-10-05 17:08:42 -0400876 unsigned long flags;
Brett Russ31961942005-09-30 01:36:00 -0400877
Brett Russafb0edd2005-10-05 17:08:42 -0400878 spin_lock_irqsave(&ap->host_set->lock, flags);
Brett Russ31961942005-09-30 01:36:00 -0400879 mv_stop_dma(ap);
Brett Russafb0edd2005-10-05 17:08:42 -0400880 spin_unlock_irqrestore(&ap->host_set->lock, flags);
Brett Russ31961942005-09-30 01:36:00 -0400881
882 ap->private_data = NULL;
Jeff Garzik6037d6b2005-11-04 22:08:00 -0500883 ata_pad_free(ap, dev);
884 mv_priv_free(pp, dev);
Brett Russ31961942005-09-30 01:36:00 -0400885 kfree(pp);
886}
887
Brett Russ05b308e2005-10-05 17:08:53 -0400888/**
889 * mv_fill_sg - Fill out the Marvell ePRD (scatter gather) entries
890 * @qc: queued command whose SG list to source from
891 *
892 * Populate the SG list and mark the last entry.
893 *
894 * LOCKING:
895 * Inherited from caller.
896 */
Brett Russ31961942005-09-30 01:36:00 -0400897static void mv_fill_sg(struct ata_queued_cmd *qc)
898{
899 struct mv_port_priv *pp = qc->ap->private_data;
Jeff Garzik972c26b2005-10-18 22:14:54 -0400900 unsigned int i = 0;
901 struct scatterlist *sg;
Brett Russ31961942005-09-30 01:36:00 -0400902
Jeff Garzik972c26b2005-10-18 22:14:54 -0400903 ata_for_each_sg(sg, qc) {
Brett Russ31961942005-09-30 01:36:00 -0400904 dma_addr_t addr;
Jeff Garzik22374672005-11-17 10:59:48 -0500905 u32 sg_len, len, offset;
Brett Russ31961942005-09-30 01:36:00 -0400906
Jeff Garzik972c26b2005-10-18 22:14:54 -0400907 addr = sg_dma_address(sg);
908 sg_len = sg_dma_len(sg);
Brett Russ31961942005-09-30 01:36:00 -0400909
Jeff Garzik22374672005-11-17 10:59:48 -0500910 while (sg_len) {
911 offset = addr & MV_DMA_BOUNDARY;
912 len = sg_len;
913 if ((offset + sg_len) > 0x10000)
914 len = 0x10000 - offset;
Jeff Garzik972c26b2005-10-18 22:14:54 -0400915
Jeff Garzik22374672005-11-17 10:59:48 -0500916 pp->sg_tbl[i].addr = cpu_to_le32(addr & 0xffffffff);
917 pp->sg_tbl[i].addr_hi = cpu_to_le32((addr >> 16) >> 16);
Mark Lord63af2a52006-03-29 09:50:31 -0500918 pp->sg_tbl[i].flags_size = cpu_to_le32(len & 0xffff);
Jeff Garzik22374672005-11-17 10:59:48 -0500919
920 sg_len -= len;
921 addr += len;
922
923 if (!sg_len && ata_sg_is_last(sg, qc))
924 pp->sg_tbl[i].flags_size |= cpu_to_le32(EPRD_FLAG_END_OF_TBL);
925
926 i++;
927 }
Brett Russ31961942005-09-30 01:36:00 -0400928 }
929}
930
931static inline unsigned mv_inc_q_index(unsigned *index)
932{
933 *index = (*index + 1) & MV_MAX_Q_DEPTH_MASK;
934 return *index;
935}
936
937static inline void mv_crqb_pack_cmd(u16 *cmdw, u8 data, u8 addr, unsigned last)
938{
939 *cmdw = data | (addr << CRQB_CMD_ADDR_SHIFT) | CRQB_CMD_CS |
940 (last ? CRQB_CMD_LAST : 0);
941}
942
Brett Russ05b308e2005-10-05 17:08:53 -0400943/**
944 * mv_qc_prep - Host specific command preparation.
945 * @qc: queued command to prepare
946 *
947 * This routine simply redirects to the general purpose routine
948 * if command is not DMA. Else, it handles prep of the CRQB
949 * (command request block), does some sanity checking, and calls
950 * the SG load routine.
951 *
952 * LOCKING:
953 * Inherited from caller.
954 */
Brett Russ31961942005-09-30 01:36:00 -0400955static void mv_qc_prep(struct ata_queued_cmd *qc)
956{
957 struct ata_port *ap = qc->ap;
958 struct mv_port_priv *pp = ap->private_data;
959 u16 *cw;
960 struct ata_taskfile *tf;
961 u16 flags = 0;
962
963 if (ATA_PROT_DMA != qc->tf.protocol) {
964 return;
Brett Russ20f733e2005-09-01 18:26:17 -0400965 }
966
Brett Russ31961942005-09-30 01:36:00 -0400967 /* the req producer index should be the same as we remember it */
Jeff Garzik8b260242005-11-12 12:32:50 -0500968 assert(((readl(mv_ap_base(qc->ap) + EDMA_REQ_Q_IN_PTR_OFS) >>
Brett Russ31961942005-09-30 01:36:00 -0400969 EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
970 pp->req_producer);
971
972 /* Fill in command request block
973 */
974 if (!(qc->tf.flags & ATA_TFLAG_WRITE)) {
975 flags |= CRQB_FLAG_READ;
976 }
977 assert(MV_MAX_Q_DEPTH > qc->tag);
978 flags |= qc->tag << CRQB_TAG_SHIFT;
979
Jeff Garzik8b260242005-11-12 12:32:50 -0500980 pp->crqb[pp->req_producer].sg_addr =
Brett Russ31961942005-09-30 01:36:00 -0400981 cpu_to_le32(pp->sg_tbl_dma & 0xffffffff);
Jeff Garzik8b260242005-11-12 12:32:50 -0500982 pp->crqb[pp->req_producer].sg_addr_hi =
Brett Russ31961942005-09-30 01:36:00 -0400983 cpu_to_le32((pp->sg_tbl_dma >> 16) >> 16);
984 pp->crqb[pp->req_producer].ctrl_flags = cpu_to_le16(flags);
985
986 cw = &pp->crqb[pp->req_producer].ata_cmd[0];
987 tf = &qc->tf;
988
989 /* Sadly, the CRQB cannot accomodate all registers--there are
990 * only 11 bytes...so we must pick and choose required
991 * registers based on the command. So, we drop feature and
992 * hob_feature for [RW] DMA commands, but they are needed for
993 * NCQ. NCQ will drop hob_nsect.
994 */
995 switch (tf->command) {
996 case ATA_CMD_READ:
997 case ATA_CMD_READ_EXT:
998 case ATA_CMD_WRITE:
999 case ATA_CMD_WRITE_EXT:
Jens Axboec15d85c2006-02-15 15:59:25 +01001000 case ATA_CMD_WRITE_FUA_EXT:
Brett Russ31961942005-09-30 01:36:00 -04001001 mv_crqb_pack_cmd(cw++, tf->hob_nsect, ATA_REG_NSECT, 0);
1002 break;
1003#ifdef LIBATA_NCQ /* FIXME: remove this line when NCQ added */
1004 case ATA_CMD_FPDMA_READ:
1005 case ATA_CMD_FPDMA_WRITE:
Jeff Garzik8b260242005-11-12 12:32:50 -05001006 mv_crqb_pack_cmd(cw++, tf->hob_feature, ATA_REG_FEATURE, 0);
Brett Russ31961942005-09-30 01:36:00 -04001007 mv_crqb_pack_cmd(cw++, tf->feature, ATA_REG_FEATURE, 0);
1008 break;
1009#endif /* FIXME: remove this line when NCQ added */
1010 default:
1011 /* The only other commands EDMA supports in non-queued and
1012 * non-NCQ mode are: [RW] STREAM DMA and W DMA FUA EXT, none
1013 * of which are defined/used by Linux. If we get here, this
1014 * driver needs work.
1015 *
1016 * FIXME: modify libata to give qc_prep a return value and
1017 * return error here.
1018 */
1019 BUG_ON(tf->command);
1020 break;
1021 }
1022 mv_crqb_pack_cmd(cw++, tf->nsect, ATA_REG_NSECT, 0);
1023 mv_crqb_pack_cmd(cw++, tf->hob_lbal, ATA_REG_LBAL, 0);
1024 mv_crqb_pack_cmd(cw++, tf->lbal, ATA_REG_LBAL, 0);
1025 mv_crqb_pack_cmd(cw++, tf->hob_lbam, ATA_REG_LBAM, 0);
1026 mv_crqb_pack_cmd(cw++, tf->lbam, ATA_REG_LBAM, 0);
1027 mv_crqb_pack_cmd(cw++, tf->hob_lbah, ATA_REG_LBAH, 0);
1028 mv_crqb_pack_cmd(cw++, tf->lbah, ATA_REG_LBAH, 0);
1029 mv_crqb_pack_cmd(cw++, tf->device, ATA_REG_DEVICE, 0);
1030 mv_crqb_pack_cmd(cw++, tf->command, ATA_REG_CMD, 1); /* last */
1031
1032 if (!(qc->flags & ATA_QCFLAG_DMAMAP)) {
1033 return;
1034 }
1035 mv_fill_sg(qc);
1036}
1037
Brett Russ05b308e2005-10-05 17:08:53 -04001038/**
1039 * mv_qc_issue - Initiate a command to the host
1040 * @qc: queued command to start
1041 *
1042 * This routine simply redirects to the general purpose routine
1043 * if command is not DMA. Else, it sanity checks our local
1044 * caches of the request producer/consumer indices then enables
1045 * DMA and bumps the request producer index.
1046 *
1047 * LOCKING:
1048 * Inherited from caller.
1049 */
Brett Russ31961942005-09-30 01:36:00 -04001050static int mv_qc_issue(struct ata_queued_cmd *qc)
1051{
1052 void __iomem *port_mmio = mv_ap_base(qc->ap);
1053 struct mv_port_priv *pp = qc->ap->private_data;
1054 u32 in_ptr;
1055
1056 if (ATA_PROT_DMA != qc->tf.protocol) {
1057 /* We're about to send a non-EDMA capable command to the
1058 * port. Turn off EDMA so there won't be problems accessing
1059 * shadow block, etc registers.
1060 */
1061 mv_stop_dma(qc->ap);
1062 return ata_qc_issue_prot(qc);
1063 }
1064
1065 in_ptr = readl(port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
1066
1067 /* the req producer index should be the same as we remember it */
1068 assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
1069 pp->req_producer);
1070 /* until we do queuing, the queue should be empty at this point */
1071 assert(((in_ptr >> EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
Jeff Garzik8b260242005-11-12 12:32:50 -05001072 ((readl(port_mmio + EDMA_REQ_Q_OUT_PTR_OFS) >>
Brett Russ31961942005-09-30 01:36:00 -04001073 EDMA_REQ_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK));
1074
1075 mv_inc_q_index(&pp->req_producer); /* now incr producer index */
1076
Brett Russafb0edd2005-10-05 17:08:42 -04001077 mv_start_dma(port_mmio, pp);
Brett Russ31961942005-09-30 01:36:00 -04001078
1079 /* and write the request in pointer to kick the EDMA to life */
1080 in_ptr &= EDMA_REQ_Q_BASE_LO_MASK;
1081 in_ptr |= pp->req_producer << EDMA_REQ_Q_PTR_SHIFT;
1082 writelfl(in_ptr, port_mmio + EDMA_REQ_Q_IN_PTR_OFS);
1083
1084 return 0;
1085}
1086
Brett Russ05b308e2005-10-05 17:08:53 -04001087/**
1088 * mv_get_crpb_status - get status from most recently completed cmd
1089 * @ap: ATA channel to manipulate
1090 *
1091 * This routine is for use when the port is in DMA mode, when it
1092 * will be using the CRPB (command response block) method of
1093 * returning command completion information. We assert indices
1094 * are good, grab status, and bump the response consumer index to
1095 * prove that we're up to date.
1096 *
1097 * LOCKING:
1098 * Inherited from caller.
1099 */
Brett Russ31961942005-09-30 01:36:00 -04001100static u8 mv_get_crpb_status(struct ata_port *ap)
1101{
1102 void __iomem *port_mmio = mv_ap_base(ap);
1103 struct mv_port_priv *pp = ap->private_data;
1104 u32 out_ptr;
1105
1106 out_ptr = readl(port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
1107
1108 /* the response consumer index should be the same as we remember it */
Jeff Garzik8b260242005-11-12 12:32:50 -05001109 assert(((out_ptr >> EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
Brett Russ31961942005-09-30 01:36:00 -04001110 pp->rsp_consumer);
1111
1112 /* increment our consumer index... */
1113 pp->rsp_consumer = mv_inc_q_index(&pp->rsp_consumer);
Jeff Garzik8b260242005-11-12 12:32:50 -05001114
Brett Russ31961942005-09-30 01:36:00 -04001115 /* and, until we do NCQ, there should only be 1 CRPB waiting */
Jeff Garzik8b260242005-11-12 12:32:50 -05001116 assert(((readl(port_mmio + EDMA_RSP_Q_IN_PTR_OFS) >>
1117 EDMA_RSP_Q_PTR_SHIFT) & MV_MAX_Q_DEPTH_MASK) ==
Brett Russ31961942005-09-30 01:36:00 -04001118 pp->rsp_consumer);
1119
1120 /* write out our inc'd consumer index so EDMA knows we're caught up */
1121 out_ptr &= EDMA_RSP_Q_BASE_LO_MASK;
1122 out_ptr |= pp->rsp_consumer << EDMA_RSP_Q_PTR_SHIFT;
1123 writelfl(out_ptr, port_mmio + EDMA_RSP_Q_OUT_PTR_OFS);
1124
1125 /* Return ATA status register for completed CRPB */
1126 return (pp->crpb[pp->rsp_consumer].flags >> CRPB_FLAG_STATUS_SHIFT);
Brett Russ20f733e2005-09-01 18:26:17 -04001127}
1128
Brett Russ05b308e2005-10-05 17:08:53 -04001129/**
1130 * mv_err_intr - Handle error interrupts on the port
1131 * @ap: ATA channel to manipulate
1132 *
1133 * In most cases, just clear the interrupt and move on. However,
1134 * some cases require an eDMA reset, which is done right before
1135 * the COMRESET in mv_phy_reset(). The SERR case requires a
1136 * clear of pending errors in the SATA SERROR register. Finally,
1137 * if the port disabled DMA, update our cached copy to match.
1138 *
1139 * LOCKING:
1140 * Inherited from caller.
1141 */
Brett Russ20f733e2005-09-01 18:26:17 -04001142static void mv_err_intr(struct ata_port *ap)
1143{
Brett Russ31961942005-09-30 01:36:00 -04001144 void __iomem *port_mmio = mv_ap_base(ap);
Brett Russ20f733e2005-09-01 18:26:17 -04001145 u32 edma_err_cause, serr = 0;
1146
Brett Russ20f733e2005-09-01 18:26:17 -04001147 edma_err_cause = readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1148
1149 if (EDMA_ERR_SERR & edma_err_cause) {
1150 serr = scr_read(ap, SCR_ERROR);
1151 scr_write_flush(ap, SCR_ERROR, serr);
1152 }
Brett Russafb0edd2005-10-05 17:08:42 -04001153 if (EDMA_ERR_SELF_DIS & edma_err_cause) {
1154 struct mv_port_priv *pp = ap->private_data;
1155 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1156 }
1157 DPRINTK(KERN_ERR "ata%u: port error; EDMA err cause: 0x%08x "
1158 "SERR: 0x%08x\n", ap->id, edma_err_cause, serr);
Brett Russ20f733e2005-09-01 18:26:17 -04001159
1160 /* Clear EDMA now that SERR cleanup done */
1161 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1162
1163 /* check for fatal here and recover if needed */
1164 if (EDMA_ERR_FATAL & edma_err_cause) {
Jeff Garzikc9d39132005-11-13 17:47:51 -05001165 mv_stop_and_reset(ap);
Brett Russ20f733e2005-09-01 18:26:17 -04001166 }
1167}
1168
Brett Russ05b308e2005-10-05 17:08:53 -04001169/**
1170 * mv_host_intr - Handle all interrupts on the given host controller
1171 * @host_set: host specific structure
1172 * @relevant: port error bits relevant to this host controller
1173 * @hc: which host controller we're to look at
1174 *
1175 * Read then write clear the HC interrupt status then walk each
1176 * port connected to the HC and see if it needs servicing. Port
1177 * success ints are reported in the HC interrupt status reg, the
1178 * port error ints are reported in the higher level main
1179 * interrupt status register and thus are passed in via the
1180 * 'relevant' argument.
1181 *
1182 * LOCKING:
1183 * Inherited from caller.
1184 */
Brett Russ20f733e2005-09-01 18:26:17 -04001185static void mv_host_intr(struct ata_host_set *host_set, u32 relevant,
1186 unsigned int hc)
1187{
1188 void __iomem *mmio = host_set->mmio_base;
1189 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
Brett Russ20f733e2005-09-01 18:26:17 -04001190 struct ata_queued_cmd *qc;
1191 u32 hc_irq_cause;
Brett Russ31961942005-09-30 01:36:00 -04001192 int shift, port, port0, hard_port, handled;
Jeff Garzika7dac442005-10-30 04:44:42 -05001193 unsigned int err_mask;
Brett Russ31961942005-09-30 01:36:00 -04001194 u8 ata_status = 0;
Brett Russ20f733e2005-09-01 18:26:17 -04001195
1196 if (hc == 0) {
1197 port0 = 0;
1198 } else {
1199 port0 = MV_PORTS_PER_HC;
1200 }
1201
1202 /* we'll need the HC success int register in most cases */
1203 hc_irq_cause = readl(hc_mmio + HC_IRQ_CAUSE_OFS);
1204 if (hc_irq_cause) {
Brett Russ31961942005-09-30 01:36:00 -04001205 writelfl(~hc_irq_cause, hc_mmio + HC_IRQ_CAUSE_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04001206 }
1207
1208 VPRINTK("ENTER, hc%u relevant=0x%08x HC IRQ cause=0x%08x\n",
1209 hc,relevant,hc_irq_cause);
1210
1211 for (port = port0; port < port0 + MV_PORTS_PER_HC; port++) {
Mark Lord63af2a52006-03-29 09:50:31 -05001212 struct ata_port *ap = host_set->ports[port];
1213 struct mv_port_priv *pp = ap->private_data;
Brett Russ20f733e2005-09-01 18:26:17 -04001214 hard_port = port & MV_PORT_MASK; /* range 0-3 */
Brett Russ31961942005-09-30 01:36:00 -04001215 handled = 0; /* ensure ata_status is set if handled++ */
Brett Russ20f733e2005-09-01 18:26:17 -04001216
Mark Lord63af2a52006-03-29 09:50:31 -05001217 /* Note that DEV_IRQ might happen spuriously during EDMA,
1218 * and should be ignored in such cases. We could mask it,
1219 * but it's pretty rare and may not be worth the overhead.
1220 */
1221 if (pp->pp_flags & MV_PP_FLAG_EDMA_EN) {
1222 /* EDMA: check for response queue interrupt */
1223 if ((CRPB_DMA_DONE << hard_port) & hc_irq_cause) {
1224 ata_status = mv_get_crpb_status(ap);
1225 handled = 1;
1226 }
1227 } else {
1228 /* PIO: check for device (drive) interrupt */
1229 if ((DEV_IRQ << hard_port) & hc_irq_cause) {
1230 ata_status = readb((void __iomem *)
Brett Russ20f733e2005-09-01 18:26:17 -04001231 ap->ioaddr.status_addr);
Mark Lord63af2a52006-03-29 09:50:31 -05001232 handled = 1;
1233 }
Brett Russ20f733e2005-09-01 18:26:17 -04001234 }
1235
Mark Lord63af2a52006-03-29 09:50:31 -05001236 if (ap->flags & (ATA_FLAG_PORT_DISABLED | ATA_FLAG_NOINTR))
Jeff Garzika2c91a82005-11-17 05:44:44 -05001237 continue;
1238
Jeff Garzika7dac442005-10-30 04:44:42 -05001239 err_mask = ac_err_mask(ata_status);
1240
Brett Russ31961942005-09-30 01:36:00 -04001241 shift = port << 1; /* (port * 2) */
Brett Russ20f733e2005-09-01 18:26:17 -04001242 if (port >= MV_PORTS_PER_HC) {
1243 shift++; /* skip bit 8 in the HC Main IRQ reg */
1244 }
1245 if ((PORT0_ERR << shift) & relevant) {
1246 mv_err_intr(ap);
Jeff Garzika7dac442005-10-30 04:44:42 -05001247 err_mask |= AC_ERR_OTHER;
Mark Lord63af2a52006-03-29 09:50:31 -05001248 handled = 1;
Brett Russ20f733e2005-09-01 18:26:17 -04001249 }
Jeff Garzik8b260242005-11-12 12:32:50 -05001250
Mark Lord63af2a52006-03-29 09:50:31 -05001251 if (handled) {
Brett Russ20f733e2005-09-01 18:26:17 -04001252 qc = ata_qc_from_tag(ap, ap->active_tag);
Mark Lord63af2a52006-03-29 09:50:31 -05001253 if (qc && (qc->flags & ATA_QCFLAG_ACTIVE)) {
Brett Russ20f733e2005-09-01 18:26:17 -04001254 VPRINTK("port %u IRQ found for qc, "
1255 "ata_status 0x%x\n", port,ata_status);
Brett Russ20f733e2005-09-01 18:26:17 -04001256 /* mark qc status appropriately */
Albert Leea22e2eb2005-12-05 15:38:02 +08001257 if (!(qc->tf.ctl & ATA_NIEN)) {
1258 qc->err_mask |= err_mask;
1259 ata_qc_complete(qc);
1260 }
Brett Russ20f733e2005-09-01 18:26:17 -04001261 }
1262 }
1263 }
1264 VPRINTK("EXIT\n");
1265}
1266
Brett Russ05b308e2005-10-05 17:08:53 -04001267/**
Jeff Garzik8b260242005-11-12 12:32:50 -05001268 * mv_interrupt -
Brett Russ05b308e2005-10-05 17:08:53 -04001269 * @irq: unused
1270 * @dev_instance: private data; in this case the host structure
1271 * @regs: unused
1272 *
1273 * Read the read only register to determine if any host
1274 * controllers have pending interrupts. If so, call lower level
1275 * routine to handle. Also check for PCI errors which are only
1276 * reported here.
1277 *
Jeff Garzik8b260242005-11-12 12:32:50 -05001278 * LOCKING:
Brett Russ05b308e2005-10-05 17:08:53 -04001279 * This routine holds the host_set lock while processing pending
1280 * interrupts.
1281 */
Brett Russ20f733e2005-09-01 18:26:17 -04001282static irqreturn_t mv_interrupt(int irq, void *dev_instance,
1283 struct pt_regs *regs)
1284{
1285 struct ata_host_set *host_set = dev_instance;
1286 unsigned int hc, handled = 0, n_hcs;
Brett Russ31961942005-09-30 01:36:00 -04001287 void __iomem *mmio = host_set->mmio_base;
Brett Russ20f733e2005-09-01 18:26:17 -04001288 u32 irq_stat;
1289
Brett Russ20f733e2005-09-01 18:26:17 -04001290 irq_stat = readl(mmio + HC_MAIN_IRQ_CAUSE_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04001291
1292 /* check the cases where we either have nothing pending or have read
1293 * a bogus register value which can indicate HW removal or PCI fault
1294 */
1295 if (!irq_stat || (0xffffffffU == irq_stat)) {
1296 return IRQ_NONE;
1297 }
1298
Brett Russ31961942005-09-30 01:36:00 -04001299 n_hcs = mv_get_hc_count(host_set->ports[0]->flags);
Brett Russ20f733e2005-09-01 18:26:17 -04001300 spin_lock(&host_set->lock);
1301
1302 for (hc = 0; hc < n_hcs; hc++) {
1303 u32 relevant = irq_stat & (HC0_IRQ_PEND << (hc * HC_SHIFT));
1304 if (relevant) {
1305 mv_host_intr(host_set, relevant, hc);
Brett Russ31961942005-09-30 01:36:00 -04001306 handled++;
Brett Russ20f733e2005-09-01 18:26:17 -04001307 }
1308 }
1309 if (PCI_ERR & irq_stat) {
Brett Russ31961942005-09-30 01:36:00 -04001310 printk(KERN_ERR DRV_NAME ": PCI ERROR; PCI IRQ cause=0x%08x\n",
1311 readl(mmio + PCI_IRQ_CAUSE_OFS));
Brett Russ20f733e2005-09-01 18:26:17 -04001312
Brett Russafb0edd2005-10-05 17:08:42 -04001313 DPRINTK("All regs @ PCI error\n");
Brett Russ31961942005-09-30 01:36:00 -04001314 mv_dump_all_regs(mmio, -1, to_pci_dev(host_set->dev));
1315
1316 writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
1317 handled++;
1318 }
Brett Russ20f733e2005-09-01 18:26:17 -04001319 spin_unlock(&host_set->lock);
1320
1321 return IRQ_RETVAL(handled);
1322}
1323
Jeff Garzikc9d39132005-11-13 17:47:51 -05001324static void __iomem *mv5_phy_base(void __iomem *mmio, unsigned int port)
1325{
1326 void __iomem *hc_mmio = mv_hc_base_from_port(mmio, port);
1327 unsigned long ofs = (mv_hardport_from_port(port) + 1) * 0x100UL;
1328
1329 return hc_mmio + ofs;
1330}
1331
1332static unsigned int mv5_scr_offset(unsigned int sc_reg_in)
1333{
1334 unsigned int ofs;
1335
1336 switch (sc_reg_in) {
1337 case SCR_STATUS:
1338 case SCR_ERROR:
1339 case SCR_CONTROL:
1340 ofs = sc_reg_in * sizeof(u32);
1341 break;
1342 default:
1343 ofs = 0xffffffffU;
1344 break;
1345 }
1346 return ofs;
1347}
1348
1349static u32 mv5_scr_read(struct ata_port *ap, unsigned int sc_reg_in)
1350{
1351 void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
1352 unsigned int ofs = mv5_scr_offset(sc_reg_in);
1353
1354 if (ofs != 0xffffffffU)
1355 return readl(mmio + ofs);
1356 else
1357 return (u32) ofs;
1358}
1359
1360static void mv5_scr_write(struct ata_port *ap, unsigned int sc_reg_in, u32 val)
1361{
1362 void __iomem *mmio = mv5_phy_base(ap->host_set->mmio_base, ap->port_no);
1363 unsigned int ofs = mv5_scr_offset(sc_reg_in);
1364
1365 if (ofs != 0xffffffffU)
1366 writelfl(val, mmio + ofs);
1367}
1368
Jeff Garzik522479f2005-11-12 22:14:02 -05001369static void mv5_reset_bus(struct pci_dev *pdev, void __iomem *mmio)
1370{
1371 u8 rev_id;
1372 int early_5080;
1373
1374 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
1375
1376 early_5080 = (pdev->device == 0x5080) && (rev_id == 0);
1377
1378 if (!early_5080) {
1379 u32 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1380 tmp |= (1 << 0);
1381 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
1382 }
1383
1384 mv_reset_pci_bus(pdev, mmio);
1385}
1386
1387static void mv5_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
1388{
1389 writel(0x0fcfffff, mmio + MV_FLASH_CTL);
1390}
1391
Jeff Garzik47c2b672005-11-12 21:13:17 -05001392static void mv5_read_preamp(struct mv_host_priv *hpriv, int idx,
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001393 void __iomem *mmio)
1394{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001395 void __iomem *phy_mmio = mv5_phy_base(mmio, idx);
1396 u32 tmp;
1397
1398 tmp = readl(phy_mmio + MV5_PHY_MODE);
1399
1400 hpriv->signal[idx].pre = tmp & 0x1800; /* bits 12:11 */
1401 hpriv->signal[idx].amps = tmp & 0xe0; /* bits 7:5 */
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001402}
1403
Jeff Garzik47c2b672005-11-12 21:13:17 -05001404static void mv5_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001405{
Jeff Garzik522479f2005-11-12 22:14:02 -05001406 u32 tmp;
1407
1408 writel(0, mmio + MV_GPIO_PORT_CTL);
1409
1410 /* FIXME: handle MV_HP_ERRATA_50XXB2 errata */
1411
1412 tmp = readl(mmio + MV_PCI_EXP_ROM_BAR_CTL);
1413 tmp |= ~(1 << 0);
1414 writel(tmp, mmio + MV_PCI_EXP_ROM_BAR_CTL);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001415}
1416
Jeff Garzik2a47ce02005-11-12 23:05:14 -05001417static void mv5_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
1418 unsigned int port)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001419{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001420 void __iomem *phy_mmio = mv5_phy_base(mmio, port);
1421 const u32 mask = (1<<12) | (1<<11) | (1<<7) | (1<<6) | (1<<5);
1422 u32 tmp;
1423 int fix_apm_sq = (hpriv->hp_flags & MV_HP_ERRATA_50XXB0);
1424
1425 if (fix_apm_sq) {
1426 tmp = readl(phy_mmio + MV5_LT_MODE);
1427 tmp |= (1 << 19);
1428 writel(tmp, phy_mmio + MV5_LT_MODE);
1429
1430 tmp = readl(phy_mmio + MV5_PHY_CTL);
1431 tmp &= ~0x3;
1432 tmp |= 0x1;
1433 writel(tmp, phy_mmio + MV5_PHY_CTL);
1434 }
1435
1436 tmp = readl(phy_mmio + MV5_PHY_MODE);
1437 tmp &= ~mask;
1438 tmp |= hpriv->signal[port].pre;
1439 tmp |= hpriv->signal[port].amps;
1440 writel(tmp, phy_mmio + MV5_PHY_MODE);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001441}
1442
Jeff Garzikc9d39132005-11-13 17:47:51 -05001443
1444#undef ZERO
1445#define ZERO(reg) writel(0, port_mmio + (reg))
1446static void mv5_reset_hc_port(struct mv_host_priv *hpriv, void __iomem *mmio,
1447 unsigned int port)
Jeff Garzik47c2b672005-11-12 21:13:17 -05001448{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001449 void __iomem *port_mmio = mv_port_base(mmio, port);
1450
1451 writelfl(EDMA_DS, port_mmio + EDMA_CMD_OFS);
1452
1453 mv_channel_reset(hpriv, mmio, port);
1454
1455 ZERO(0x028); /* command */
1456 writel(0x11f, port_mmio + EDMA_CFG_OFS);
1457 ZERO(0x004); /* timer */
1458 ZERO(0x008); /* irq err cause */
1459 ZERO(0x00c); /* irq err mask */
1460 ZERO(0x010); /* rq bah */
1461 ZERO(0x014); /* rq inp */
1462 ZERO(0x018); /* rq outp */
1463 ZERO(0x01c); /* respq bah */
1464 ZERO(0x024); /* respq outp */
1465 ZERO(0x020); /* respq inp */
1466 ZERO(0x02c); /* test control */
1467 writel(0xbc, port_mmio + EDMA_IORDY_TMOUT);
1468}
1469#undef ZERO
1470
1471#define ZERO(reg) writel(0, hc_mmio + (reg))
1472static void mv5_reset_one_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
1473 unsigned int hc)
1474{
1475 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
1476 u32 tmp;
1477
1478 ZERO(0x00c);
1479 ZERO(0x010);
1480 ZERO(0x014);
1481 ZERO(0x018);
1482
1483 tmp = readl(hc_mmio + 0x20);
1484 tmp &= 0x1c1c1c1c;
1485 tmp |= 0x03030303;
1486 writel(tmp, hc_mmio + 0x20);
1487}
1488#undef ZERO
1489
1490static int mv5_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
1491 unsigned int n_hc)
1492{
1493 unsigned int hc, port;
1494
1495 for (hc = 0; hc < n_hc; hc++) {
1496 for (port = 0; port < MV_PORTS_PER_HC; port++)
1497 mv5_reset_hc_port(hpriv, mmio,
1498 (hc * MV_PORTS_PER_HC) + port);
1499
1500 mv5_reset_one_hc(hpriv, mmio, hc);
1501 }
1502
1503 return 0;
Jeff Garzik47c2b672005-11-12 21:13:17 -05001504}
1505
Jeff Garzik101ffae2005-11-12 22:17:49 -05001506#undef ZERO
1507#define ZERO(reg) writel(0, mmio + (reg))
1508static void mv_reset_pci_bus(struct pci_dev *pdev, void __iomem *mmio)
1509{
1510 u32 tmp;
1511
1512 tmp = readl(mmio + MV_PCI_MODE);
1513 tmp &= 0xff00ffff;
1514 writel(tmp, mmio + MV_PCI_MODE);
1515
1516 ZERO(MV_PCI_DISC_TIMER);
1517 ZERO(MV_PCI_MSI_TRIGGER);
1518 writel(0x000100ff, mmio + MV_PCI_XBAR_TMOUT);
1519 ZERO(HC_MAIN_IRQ_MASK_OFS);
1520 ZERO(MV_PCI_SERR_MASK);
1521 ZERO(PCI_IRQ_CAUSE_OFS);
1522 ZERO(PCI_IRQ_MASK_OFS);
1523 ZERO(MV_PCI_ERR_LOW_ADDRESS);
1524 ZERO(MV_PCI_ERR_HIGH_ADDRESS);
1525 ZERO(MV_PCI_ERR_ATTRIBUTE);
1526 ZERO(MV_PCI_ERR_COMMAND);
1527}
1528#undef ZERO
1529
1530static void mv6_reset_flash(struct mv_host_priv *hpriv, void __iomem *mmio)
1531{
1532 u32 tmp;
1533
1534 mv5_reset_flash(hpriv, mmio);
1535
1536 tmp = readl(mmio + MV_GPIO_PORT_CTL);
1537 tmp &= 0x3;
1538 tmp |= (1 << 5) | (1 << 6);
1539 writel(tmp, mmio + MV_GPIO_PORT_CTL);
1540}
1541
1542/**
1543 * mv6_reset_hc - Perform the 6xxx global soft reset
1544 * @mmio: base address of the HBA
1545 *
1546 * This routine only applies to 6xxx parts.
1547 *
1548 * LOCKING:
1549 * Inherited from caller.
1550 */
Jeff Garzikc9d39132005-11-13 17:47:51 -05001551static int mv6_reset_hc(struct mv_host_priv *hpriv, void __iomem *mmio,
1552 unsigned int n_hc)
Jeff Garzik101ffae2005-11-12 22:17:49 -05001553{
1554 void __iomem *reg = mmio + PCI_MAIN_CMD_STS_OFS;
1555 int i, rc = 0;
1556 u32 t;
1557
1558 /* Following procedure defined in PCI "main command and status
1559 * register" table.
1560 */
1561 t = readl(reg);
1562 writel(t | STOP_PCI_MASTER, reg);
1563
1564 for (i = 0; i < 1000; i++) {
1565 udelay(1);
1566 t = readl(reg);
1567 if (PCI_MASTER_EMPTY & t) {
1568 break;
1569 }
1570 }
1571 if (!(PCI_MASTER_EMPTY & t)) {
1572 printk(KERN_ERR DRV_NAME ": PCI master won't flush\n");
1573 rc = 1;
1574 goto done;
1575 }
1576
1577 /* set reset */
1578 i = 5;
1579 do {
1580 writel(t | GLOB_SFT_RST, reg);
1581 t = readl(reg);
1582 udelay(1);
1583 } while (!(GLOB_SFT_RST & t) && (i-- > 0));
1584
1585 if (!(GLOB_SFT_RST & t)) {
1586 printk(KERN_ERR DRV_NAME ": can't set global reset\n");
1587 rc = 1;
1588 goto done;
1589 }
1590
1591 /* clear reset and *reenable the PCI master* (not mentioned in spec) */
1592 i = 5;
1593 do {
1594 writel(t & ~(GLOB_SFT_RST | STOP_PCI_MASTER), reg);
1595 t = readl(reg);
1596 udelay(1);
1597 } while ((GLOB_SFT_RST & t) && (i-- > 0));
1598
1599 if (GLOB_SFT_RST & t) {
1600 printk(KERN_ERR DRV_NAME ": can't clear global reset\n");
1601 rc = 1;
1602 }
1603done:
1604 return rc;
1605}
1606
Jeff Garzik47c2b672005-11-12 21:13:17 -05001607static void mv6_read_preamp(struct mv_host_priv *hpriv, int idx,
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001608 void __iomem *mmio)
1609{
1610 void __iomem *port_mmio;
1611 u32 tmp;
1612
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001613 tmp = readl(mmio + MV_RESET_CFG);
1614 if ((tmp & (1 << 0)) == 0) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05001615 hpriv->signal[idx].amps = 0x7 << 8;
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001616 hpriv->signal[idx].pre = 0x1 << 5;
1617 return;
1618 }
1619
1620 port_mmio = mv_port_base(mmio, idx);
1621 tmp = readl(port_mmio + PHY_MODE2);
1622
1623 hpriv->signal[idx].amps = tmp & 0x700; /* bits 10:8 */
1624 hpriv->signal[idx].pre = tmp & 0xe0; /* bits 7:5 */
1625}
1626
Jeff Garzik47c2b672005-11-12 21:13:17 -05001627static void mv6_enable_leds(struct mv_host_priv *hpriv, void __iomem *mmio)
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001628{
Jeff Garzik47c2b672005-11-12 21:13:17 -05001629 writel(0x00000060, mmio + MV_GPIO_PORT_CTL);
Jeff Garzikba3fe8f2005-11-12 19:08:48 -05001630}
1631
Jeff Garzikc9d39132005-11-13 17:47:51 -05001632static void mv6_phy_errata(struct mv_host_priv *hpriv, void __iomem *mmio,
Jeff Garzik2a47ce02005-11-12 23:05:14 -05001633 unsigned int port)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001634{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001635 void __iomem *port_mmio = mv_port_base(mmio, port);
1636
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001637 u32 hp_flags = hpriv->hp_flags;
Jeff Garzik47c2b672005-11-12 21:13:17 -05001638 int fix_phy_mode2 =
1639 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001640 int fix_phy_mode4 =
Jeff Garzik47c2b672005-11-12 21:13:17 -05001641 hp_flags & (MV_HP_ERRATA_60X1B2 | MV_HP_ERRATA_60X1C0);
1642 u32 m2, tmp;
1643
1644 if (fix_phy_mode2) {
1645 m2 = readl(port_mmio + PHY_MODE2);
1646 m2 &= ~(1 << 16);
1647 m2 |= (1 << 31);
1648 writel(m2, port_mmio + PHY_MODE2);
1649
1650 udelay(200);
1651
1652 m2 = readl(port_mmio + PHY_MODE2);
1653 m2 &= ~((1 << 16) | (1 << 31));
1654 writel(m2, port_mmio + PHY_MODE2);
1655
1656 udelay(200);
1657 }
1658
1659 /* who knows what this magic does */
1660 tmp = readl(port_mmio + PHY_MODE3);
1661 tmp &= ~0x7F800000;
1662 tmp |= 0x2A800000;
1663 writel(tmp, port_mmio + PHY_MODE3);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001664
1665 if (fix_phy_mode4) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05001666 u32 m4;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001667
1668 m4 = readl(port_mmio + PHY_MODE4);
Jeff Garzik47c2b672005-11-12 21:13:17 -05001669
1670 if (hp_flags & MV_HP_ERRATA_60X1B2)
1671 tmp = readl(port_mmio + 0x310);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001672
1673 m4 = (m4 & ~(1 << 1)) | (1 << 0);
1674
1675 writel(m4, port_mmio + PHY_MODE4);
Jeff Garzik47c2b672005-11-12 21:13:17 -05001676
1677 if (hp_flags & MV_HP_ERRATA_60X1B2)
1678 writel(tmp, port_mmio + 0x310);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001679 }
1680
1681 /* Revert values of pre-emphasis and signal amps to the saved ones */
1682 m2 = readl(port_mmio + PHY_MODE2);
1683
1684 m2 &= ~MV_M2_PREAMP_MASK;
Jeff Garzik2a47ce02005-11-12 23:05:14 -05001685 m2 |= hpriv->signal[port].amps;
1686 m2 |= hpriv->signal[port].pre;
Jeff Garzik47c2b672005-11-12 21:13:17 -05001687 m2 &= ~(1 << 16);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001688
1689 writel(m2, port_mmio + PHY_MODE2);
1690}
1691
Jeff Garzikc9d39132005-11-13 17:47:51 -05001692static void mv_channel_reset(struct mv_host_priv *hpriv, void __iomem *mmio,
1693 unsigned int port_no)
Brett Russ20f733e2005-09-01 18:26:17 -04001694{
Jeff Garzikc9d39132005-11-13 17:47:51 -05001695 void __iomem *port_mmio = mv_port_base(mmio, port_no);
Brett Russ20f733e2005-09-01 18:26:17 -04001696
Brett Russ31961942005-09-30 01:36:00 -04001697 writelfl(ATA_RST, port_mmio + EDMA_CMD_OFS);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001698
1699 if (IS_60XX(hpriv)) {
1700 u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
1701 ifctl |= (1 << 12) | (1 << 7);
1702 writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
1703 }
1704
Brett Russ20f733e2005-09-01 18:26:17 -04001705 udelay(25); /* allow reset propagation */
1706
1707 /* Spec never mentions clearing the bit. Marvell's driver does
1708 * clear the bit, however.
1709 */
Brett Russ31961942005-09-30 01:36:00 -04001710 writelfl(0, port_mmio + EDMA_CMD_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04001711
Jeff Garzikc9d39132005-11-13 17:47:51 -05001712 hpriv->ops->phy_errata(hpriv, mmio, port_no);
1713
1714 if (IS_50XX(hpriv))
1715 mdelay(1);
1716}
1717
1718static void mv_stop_and_reset(struct ata_port *ap)
1719{
1720 struct mv_host_priv *hpriv = ap->host_set->private_data;
1721 void __iomem *mmio = ap->host_set->mmio_base;
1722
1723 mv_stop_dma(ap);
1724
1725 mv_channel_reset(hpriv, mmio, ap->port_no);
1726
Jeff Garzik22374672005-11-17 10:59:48 -05001727 __mv_phy_reset(ap, 0);
1728}
1729
1730static inline void __msleep(unsigned int msec, int can_sleep)
1731{
1732 if (can_sleep)
1733 msleep(msec);
1734 else
1735 mdelay(msec);
Jeff Garzikc9d39132005-11-13 17:47:51 -05001736}
1737
1738/**
Jeff Garzik22374672005-11-17 10:59:48 -05001739 * __mv_phy_reset - Perform eDMA reset followed by COMRESET
Jeff Garzikc9d39132005-11-13 17:47:51 -05001740 * @ap: ATA channel to manipulate
1741 *
1742 * Part of this is taken from __sata_phy_reset and modified to
1743 * not sleep since this routine gets called from interrupt level.
1744 *
1745 * LOCKING:
1746 * Inherited from caller. This is coded to safe to call at
1747 * interrupt level, i.e. it does not sleep.
1748 */
Jeff Garzik22374672005-11-17 10:59:48 -05001749static void __mv_phy_reset(struct ata_port *ap, int can_sleep)
Jeff Garzikc9d39132005-11-13 17:47:51 -05001750{
1751 struct mv_port_priv *pp = ap->private_data;
Jeff Garzik22374672005-11-17 10:59:48 -05001752 struct mv_host_priv *hpriv = ap->host_set->private_data;
Jeff Garzikc9d39132005-11-13 17:47:51 -05001753 void __iomem *port_mmio = mv_ap_base(ap);
1754 struct ata_taskfile tf;
1755 struct ata_device *dev = &ap->device[0];
1756 unsigned long timeout;
Jeff Garzik22374672005-11-17 10:59:48 -05001757 int retry = 5;
1758 u32 sstatus;
Jeff Garzikc9d39132005-11-13 17:47:51 -05001759
1760 VPRINTK("ENTER, port %u, mmio 0x%p\n", ap->port_no, port_mmio);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001761
Jeff Garzik095fec82005-11-12 09:50:49 -05001762 DPRINTK("S-regs after ATA_RST: SStat 0x%08x SErr 0x%08x "
Brett Russ31961942005-09-30 01:36:00 -04001763 "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
1764 mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
Brett Russ20f733e2005-09-01 18:26:17 -04001765
Jeff Garzik22374672005-11-17 10:59:48 -05001766 /* Issue COMRESET via SControl */
1767comreset_retry:
Brett Russ31961942005-09-30 01:36:00 -04001768 scr_write_flush(ap, SCR_CONTROL, 0x301);
Jeff Garzik22374672005-11-17 10:59:48 -05001769 __msleep(1, can_sleep);
1770
Brett Russ31961942005-09-30 01:36:00 -04001771 scr_write_flush(ap, SCR_CONTROL, 0x300);
Jeff Garzik22374672005-11-17 10:59:48 -05001772 __msleep(20, can_sleep);
1773
1774 timeout = jiffies + msecs_to_jiffies(200);
Brett Russ31961942005-09-30 01:36:00 -04001775 do {
Jeff Garzik22374672005-11-17 10:59:48 -05001776 sstatus = scr_read(ap, SCR_STATUS) & 0x3;
1777 if ((sstatus == 3) || (sstatus == 0))
Brett Russ31961942005-09-30 01:36:00 -04001778 break;
Jeff Garzik22374672005-11-17 10:59:48 -05001779
1780 __msleep(1, can_sleep);
Brett Russ31961942005-09-30 01:36:00 -04001781 } while (time_before(jiffies, timeout));
Brett Russ20f733e2005-09-01 18:26:17 -04001782
Jeff Garzik22374672005-11-17 10:59:48 -05001783 /* work around errata */
1784 if (IS_60XX(hpriv) &&
1785 (sstatus != 0x0) && (sstatus != 0x113) && (sstatus != 0x123) &&
1786 (retry-- > 0))
1787 goto comreset_retry;
Jeff Garzik095fec82005-11-12 09:50:49 -05001788
1789 DPRINTK("S-regs after PHY wake: SStat 0x%08x SErr 0x%08x "
Brett Russ31961942005-09-30 01:36:00 -04001790 "SCtrl 0x%08x\n", mv_scr_read(ap, SCR_STATUS),
1791 mv_scr_read(ap, SCR_ERROR), mv_scr_read(ap, SCR_CONTROL));
1792
1793 if (sata_dev_present(ap)) {
1794 ata_port_probe(ap);
1795 } else {
1796 printk(KERN_INFO "ata%u: no device found (phy stat %08x)\n",
1797 ap->id, scr_read(ap, SCR_STATUS));
1798 ata_port_disable(ap);
Brett Russ20f733e2005-09-01 18:26:17 -04001799 return;
1800 }
Brett Russ31961942005-09-30 01:36:00 -04001801 ap->cbl = ATA_CBL_SATA;
Brett Russ20f733e2005-09-01 18:26:17 -04001802
Jeff Garzik22374672005-11-17 10:59:48 -05001803 /* even after SStatus reflects that device is ready,
1804 * it seems to take a while for link to be fully
1805 * established (and thus Status no longer 0x80/0x7F),
1806 * so we poll a bit for that, here.
1807 */
1808 retry = 20;
1809 while (1) {
1810 u8 drv_stat = ata_check_status(ap);
1811 if ((drv_stat != 0x80) && (drv_stat != 0x7f))
1812 break;
1813 __msleep(500, can_sleep);
1814 if (retry-- <= 0)
1815 break;
1816 }
1817
Brett Russ20f733e2005-09-01 18:26:17 -04001818 tf.lbah = readb((void __iomem *) ap->ioaddr.lbah_addr);
1819 tf.lbam = readb((void __iomem *) ap->ioaddr.lbam_addr);
1820 tf.lbal = readb((void __iomem *) ap->ioaddr.lbal_addr);
1821 tf.nsect = readb((void __iomem *) ap->ioaddr.nsect_addr);
1822
1823 dev->class = ata_dev_classify(&tf);
1824 if (!ata_dev_present(dev)) {
1825 VPRINTK("Port disabled post-sig: No device present.\n");
1826 ata_port_disable(ap);
1827 }
Jeff Garzik095fec82005-11-12 09:50:49 -05001828
1829 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1830
1831 pp->pp_flags &= ~MV_PP_FLAG_EDMA_EN;
1832
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001833 VPRINTK("EXIT\n");
Brett Russ20f733e2005-09-01 18:26:17 -04001834}
1835
Jeff Garzik22374672005-11-17 10:59:48 -05001836static void mv_phy_reset(struct ata_port *ap)
1837{
1838 __mv_phy_reset(ap, 1);
1839}
1840
Brett Russ05b308e2005-10-05 17:08:53 -04001841/**
1842 * mv_eng_timeout - Routine called by libata when SCSI times out I/O
1843 * @ap: ATA channel to manipulate
1844 *
1845 * Intent is to clear all pending error conditions, reset the
1846 * chip/bus, fail the command, and move on.
1847 *
1848 * LOCKING:
1849 * This routine holds the host_set lock while failing the command.
1850 */
Brett Russ31961942005-09-30 01:36:00 -04001851static void mv_eng_timeout(struct ata_port *ap)
Brett Russ20f733e2005-09-01 18:26:17 -04001852{
Brett Russ31961942005-09-30 01:36:00 -04001853 struct ata_queued_cmd *qc;
1854 unsigned long flags;
1855
1856 printk(KERN_ERR "ata%u: Entering mv_eng_timeout\n",ap->id);
1857 DPRINTK("All regs @ start of eng_timeout\n");
Jeff Garzik8b260242005-11-12 12:32:50 -05001858 mv_dump_all_regs(ap->host_set->mmio_base, ap->port_no,
Brett Russ31961942005-09-30 01:36:00 -04001859 to_pci_dev(ap->host_set->dev));
1860
1861 qc = ata_qc_from_tag(ap, ap->active_tag);
1862 printk(KERN_ERR "mmio_base %p ap %p qc %p scsi_cmnd %p &cmnd %p\n",
Jeff Garzik8b260242005-11-12 12:32:50 -05001863 ap->host_set->mmio_base, ap, qc, qc->scsicmd,
Brett Russ31961942005-09-30 01:36:00 -04001864 &qc->scsicmd->cmnd);
1865
1866 mv_err_intr(ap);
Jeff Garzikc9d39132005-11-13 17:47:51 -05001867 mv_stop_and_reset(ap);
Brett Russ31961942005-09-30 01:36:00 -04001868
1869 if (!qc) {
1870 printk(KERN_ERR "ata%u: BUG: timeout without command\n",
1871 ap->id);
1872 } else {
1873 /* hack alert! We cannot use the supplied completion
1874 * function from inside the ->eh_strategy_handler() thread.
1875 * libata is the only user of ->eh_strategy_handler() in
1876 * any kernel, so the default scsi_done() assumes it is
1877 * not being called from the SCSI EH.
1878 */
1879 spin_lock_irqsave(&ap->host_set->lock, flags);
1880 qc->scsidone = scsi_finish_command;
Albert Leea22e2eb2005-12-05 15:38:02 +08001881 qc->err_mask |= AC_ERR_OTHER;
1882 ata_qc_complete(qc);
Brett Russ31961942005-09-30 01:36:00 -04001883 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1884 }
1885}
1886
Brett Russ05b308e2005-10-05 17:08:53 -04001887/**
1888 * mv_port_init - Perform some early initialization on a single port.
1889 * @port: libata data structure storing shadow register addresses
1890 * @port_mmio: base address of the port
1891 *
1892 * Initialize shadow register mmio addresses, clear outstanding
1893 * interrupts on the port, and unmask interrupts for the future
1894 * start of the port.
1895 *
1896 * LOCKING:
1897 * Inherited from caller.
1898 */
Brett Russ31961942005-09-30 01:36:00 -04001899static void mv_port_init(struct ata_ioports *port, void __iomem *port_mmio)
1900{
1901 unsigned long shd_base = (unsigned long) port_mmio + SHD_BLK_OFS;
1902 unsigned serr_ofs;
1903
Jeff Garzik8b260242005-11-12 12:32:50 -05001904 /* PIO related setup
Brett Russ31961942005-09-30 01:36:00 -04001905 */
1906 port->data_addr = shd_base + (sizeof(u32) * ATA_REG_DATA);
Jeff Garzik8b260242005-11-12 12:32:50 -05001907 port->error_addr =
Brett Russ31961942005-09-30 01:36:00 -04001908 port->feature_addr = shd_base + (sizeof(u32) * ATA_REG_ERR);
1909 port->nsect_addr = shd_base + (sizeof(u32) * ATA_REG_NSECT);
1910 port->lbal_addr = shd_base + (sizeof(u32) * ATA_REG_LBAL);
1911 port->lbam_addr = shd_base + (sizeof(u32) * ATA_REG_LBAM);
1912 port->lbah_addr = shd_base + (sizeof(u32) * ATA_REG_LBAH);
1913 port->device_addr = shd_base + (sizeof(u32) * ATA_REG_DEVICE);
Jeff Garzik8b260242005-11-12 12:32:50 -05001914 port->status_addr =
Brett Russ31961942005-09-30 01:36:00 -04001915 port->command_addr = shd_base + (sizeof(u32) * ATA_REG_STATUS);
1916 /* special case: control/altstatus doesn't have ATA_REG_ address */
1917 port->altstatus_addr = port->ctl_addr = shd_base + SHD_CTL_AST_OFS;
1918
1919 /* unused: */
Brett Russ20f733e2005-09-01 18:26:17 -04001920 port->cmd_addr = port->bmdma_addr = port->scr_addr = 0;
1921
Brett Russ31961942005-09-30 01:36:00 -04001922 /* Clear any currently outstanding port interrupt conditions */
1923 serr_ofs = mv_scr_offset(SCR_ERROR);
1924 writelfl(readl(port_mmio + serr_ofs), port_mmio + serr_ofs);
1925 writelfl(0, port_mmio + EDMA_ERR_IRQ_CAUSE_OFS);
1926
Brett Russ20f733e2005-09-01 18:26:17 -04001927 /* unmask all EDMA error interrupts */
Brett Russ31961942005-09-30 01:36:00 -04001928 writelfl(~0, port_mmio + EDMA_ERR_IRQ_MASK_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04001929
Jeff Garzik8b260242005-11-12 12:32:50 -05001930 VPRINTK("EDMA cfg=0x%08x EDMA IRQ err cause/mask=0x%08x/0x%08x\n",
Brett Russ31961942005-09-30 01:36:00 -04001931 readl(port_mmio + EDMA_CFG_OFS),
1932 readl(port_mmio + EDMA_ERR_IRQ_CAUSE_OFS),
1933 readl(port_mmio + EDMA_ERR_IRQ_MASK_OFS));
Brett Russ20f733e2005-09-01 18:26:17 -04001934}
1935
Jeff Garzik47c2b672005-11-12 21:13:17 -05001936static int mv_chip_id(struct pci_dev *pdev, struct mv_host_priv *hpriv,
Jeff Garzik522479f2005-11-12 22:14:02 -05001937 unsigned int board_idx)
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001938{
1939 u8 rev_id;
1940 u32 hp_flags = hpriv->hp_flags;
1941
1942 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
1943
1944 switch(board_idx) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05001945 case chip_5080:
1946 hpriv->ops = &mv5xxx_ops;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001947 hp_flags |= MV_HP_50XX;
1948
Jeff Garzik47c2b672005-11-12 21:13:17 -05001949 switch (rev_id) {
1950 case 0x1:
1951 hp_flags |= MV_HP_ERRATA_50XXB0;
1952 break;
1953 case 0x3:
1954 hp_flags |= MV_HP_ERRATA_50XXB2;
1955 break;
1956 default:
1957 dev_printk(KERN_WARNING, &pdev->dev,
1958 "Applying 50XXB2 workarounds to unknown rev\n");
1959 hp_flags |= MV_HP_ERRATA_50XXB2;
1960 break;
1961 }
1962 break;
1963
1964 case chip_504x:
1965 case chip_508x:
1966 hpriv->ops = &mv5xxx_ops;
1967 hp_flags |= MV_HP_50XX;
1968
1969 switch (rev_id) {
1970 case 0x0:
1971 hp_flags |= MV_HP_ERRATA_50XXB0;
1972 break;
1973 case 0x3:
1974 hp_flags |= MV_HP_ERRATA_50XXB2;
1975 break;
1976 default:
1977 dev_printk(KERN_WARNING, &pdev->dev,
1978 "Applying B2 workarounds to unknown rev\n");
1979 hp_flags |= MV_HP_ERRATA_50XXB2;
1980 break;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001981 }
1982 break;
1983
1984 case chip_604x:
1985 case chip_608x:
Jeff Garzik47c2b672005-11-12 21:13:17 -05001986 hpriv->ops = &mv6xxx_ops;
1987
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001988 switch (rev_id) {
Jeff Garzik47c2b672005-11-12 21:13:17 -05001989 case 0x7:
1990 hp_flags |= MV_HP_ERRATA_60X1B2;
1991 break;
1992 case 0x9:
1993 hp_flags |= MV_HP_ERRATA_60X1C0;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001994 break;
1995 default:
1996 dev_printk(KERN_WARNING, &pdev->dev,
Jeff Garzik47c2b672005-11-12 21:13:17 -05001997 "Applying B2 workarounds to unknown rev\n");
1998 hp_flags |= MV_HP_ERRATA_60X1B2;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05001999 break;
2000 }
2001 break;
2002
2003 default:
2004 printk(KERN_ERR DRV_NAME ": BUG: invalid board index %u\n", board_idx);
2005 return 1;
2006 }
2007
2008 hpriv->hp_flags = hp_flags;
2009
2010 return 0;
2011}
2012
Brett Russ05b308e2005-10-05 17:08:53 -04002013/**
Jeff Garzik47c2b672005-11-12 21:13:17 -05002014 * mv_init_host - Perform some early initialization of the host.
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002015 * @pdev: host PCI device
Brett Russ05b308e2005-10-05 17:08:53 -04002016 * @probe_ent: early data struct representing the host
2017 *
2018 * If possible, do an early global reset of the host. Then do
2019 * our port init and clear/unmask all/relevant host interrupts.
2020 *
2021 * LOCKING:
2022 * Inherited from caller.
2023 */
Jeff Garzik47c2b672005-11-12 21:13:17 -05002024static int mv_init_host(struct pci_dev *pdev, struct ata_probe_ent *probe_ent,
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002025 unsigned int board_idx)
Brett Russ20f733e2005-09-01 18:26:17 -04002026{
2027 int rc = 0, n_hc, port, hc;
2028 void __iomem *mmio = probe_ent->mmio_base;
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002029 struct mv_host_priv *hpriv = probe_ent->private_data;
2030
Jeff Garzik47c2b672005-11-12 21:13:17 -05002031 /* global interrupt mask */
2032 writel(0, mmio + HC_MAIN_IRQ_MASK_OFS);
2033
2034 rc = mv_chip_id(pdev, hpriv, board_idx);
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002035 if (rc)
2036 goto done;
2037
2038 n_hc = mv_get_hc_count(probe_ent->host_flags);
2039 probe_ent->n_ports = MV_PORTS_PER_HC * n_hc;
2040
Jeff Garzik47c2b672005-11-12 21:13:17 -05002041 for (port = 0; port < probe_ent->n_ports; port++)
2042 hpriv->ops->read_preamp(hpriv, port, mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04002043
Jeff Garzikc9d39132005-11-13 17:47:51 -05002044 rc = hpriv->ops->reset_hc(hpriv, mmio, n_hc);
Jeff Garzik47c2b672005-11-12 21:13:17 -05002045 if (rc)
Brett Russ20f733e2005-09-01 18:26:17 -04002046 goto done;
Brett Russ20f733e2005-09-01 18:26:17 -04002047
Jeff Garzik522479f2005-11-12 22:14:02 -05002048 hpriv->ops->reset_flash(hpriv, mmio);
2049 hpriv->ops->reset_bus(pdev, mmio);
Jeff Garzik47c2b672005-11-12 21:13:17 -05002050 hpriv->ops->enable_leds(hpriv, mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04002051
2052 for (port = 0; port < probe_ent->n_ports; port++) {
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002053 if (IS_60XX(hpriv)) {
Jeff Garzikc9d39132005-11-13 17:47:51 -05002054 void __iomem *port_mmio = mv_port_base(mmio, port);
2055
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002056 u32 ifctl = readl(port_mmio + SATA_INTERFACE_CTL);
2057 ifctl |= (1 << 12);
2058 writelfl(ifctl, port_mmio + SATA_INTERFACE_CTL);
2059 }
2060
Jeff Garzikc9d39132005-11-13 17:47:51 -05002061 hpriv->ops->phy_errata(hpriv, mmio, port);
Jeff Garzik2a47ce02005-11-12 23:05:14 -05002062 }
2063
2064 for (port = 0; port < probe_ent->n_ports; port++) {
2065 void __iomem *port_mmio = mv_port_base(mmio, port);
Brett Russ31961942005-09-30 01:36:00 -04002066 mv_port_init(&probe_ent->port[port], port_mmio);
Brett Russ20f733e2005-09-01 18:26:17 -04002067 }
2068
2069 for (hc = 0; hc < n_hc; hc++) {
Brett Russ31961942005-09-30 01:36:00 -04002070 void __iomem *hc_mmio = mv_hc_base(mmio, hc);
2071
2072 VPRINTK("HC%i: HC config=0x%08x HC IRQ cause "
2073 "(before clear)=0x%08x\n", hc,
2074 readl(hc_mmio + HC_CFG_OFS),
2075 readl(hc_mmio + HC_IRQ_CAUSE_OFS));
2076
2077 /* Clear any currently outstanding hc interrupt conditions */
2078 writelfl(0, hc_mmio + HC_IRQ_CAUSE_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04002079 }
2080
Brett Russ31961942005-09-30 01:36:00 -04002081 /* Clear any currently outstanding host interrupt conditions */
2082 writelfl(0, mmio + PCI_IRQ_CAUSE_OFS);
2083
2084 /* and unmask interrupt generation for host regs */
2085 writelfl(PCI_UNMASK_ALL_IRQS, mmio + PCI_IRQ_MASK_OFS);
2086 writelfl(~HC_MAIN_MASKED_IRQS, mmio + HC_MAIN_IRQ_MASK_OFS);
Brett Russ20f733e2005-09-01 18:26:17 -04002087
2088 VPRINTK("HC MAIN IRQ cause/mask=0x%08x/0x%08x "
Jeff Garzik8b260242005-11-12 12:32:50 -05002089 "PCI int cause/mask=0x%08x/0x%08x\n",
Brett Russ20f733e2005-09-01 18:26:17 -04002090 readl(mmio + HC_MAIN_IRQ_CAUSE_OFS),
2091 readl(mmio + HC_MAIN_IRQ_MASK_OFS),
2092 readl(mmio + PCI_IRQ_CAUSE_OFS),
2093 readl(mmio + PCI_IRQ_MASK_OFS));
Jeff Garzikbca1c4e2005-11-12 12:48:15 -05002094
Brett Russ31961942005-09-30 01:36:00 -04002095done:
Brett Russ20f733e2005-09-01 18:26:17 -04002096 return rc;
2097}
2098
Brett Russ05b308e2005-10-05 17:08:53 -04002099/**
2100 * mv_print_info - Dump key info to kernel log for perusal.
2101 * @probe_ent: early data struct representing the host
2102 *
2103 * FIXME: complete this.
2104 *
2105 * LOCKING:
2106 * Inherited from caller.
2107 */
Brett Russ31961942005-09-30 01:36:00 -04002108static void mv_print_info(struct ata_probe_ent *probe_ent)
2109{
2110 struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
2111 struct mv_host_priv *hpriv = probe_ent->private_data;
2112 u8 rev_id, scc;
2113 const char *scc_s;
2114
2115 /* Use this to determine the HW stepping of the chip so we know
2116 * what errata to workaround
2117 */
2118 pci_read_config_byte(pdev, PCI_REVISION_ID, &rev_id);
2119
2120 pci_read_config_byte(pdev, PCI_CLASS_DEVICE, &scc);
2121 if (scc == 0)
2122 scc_s = "SCSI";
2123 else if (scc == 0x01)
2124 scc_s = "RAID";
2125 else
2126 scc_s = "unknown";
2127
Jeff Garzika9524a72005-10-30 14:39:11 -05002128 dev_printk(KERN_INFO, &pdev->dev,
2129 "%u slots %u ports %s mode IRQ via %s\n",
Jeff Garzik8b260242005-11-12 12:32:50 -05002130 (unsigned)MV_MAX_Q_DEPTH, probe_ent->n_ports,
Brett Russ31961942005-09-30 01:36:00 -04002131 scc_s, (MV_HP_FLAG_MSI & hpriv->hp_flags) ? "MSI" : "INTx");
2132}
2133
Brett Russ05b308e2005-10-05 17:08:53 -04002134/**
2135 * mv_init_one - handle a positive probe of a Marvell host
2136 * @pdev: PCI device found
2137 * @ent: PCI device ID entry for the matched host
2138 *
2139 * LOCKING:
2140 * Inherited from caller.
2141 */
Brett Russ20f733e2005-09-01 18:26:17 -04002142static int mv_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
2143{
2144 static int printed_version = 0;
2145 struct ata_probe_ent *probe_ent = NULL;
2146 struct mv_host_priv *hpriv;
2147 unsigned int board_idx = (unsigned int)ent->driver_data;
2148 void __iomem *mmio_base;
Brett Russ31961942005-09-30 01:36:00 -04002149 int pci_dev_busy = 0, rc;
Brett Russ20f733e2005-09-01 18:26:17 -04002150
Jeff Garzika9524a72005-10-30 14:39:11 -05002151 if (!printed_version++)
2152 dev_printk(KERN_INFO, &pdev->dev, "version " DRV_VERSION "\n");
Brett Russ20f733e2005-09-01 18:26:17 -04002153
Brett Russ20f733e2005-09-01 18:26:17 -04002154 rc = pci_enable_device(pdev);
2155 if (rc) {
2156 return rc;
2157 }
2158
2159 rc = pci_request_regions(pdev, DRV_NAME);
2160 if (rc) {
2161 pci_dev_busy = 1;
2162 goto err_out;
2163 }
2164
Brett Russ20f733e2005-09-01 18:26:17 -04002165 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
2166 if (probe_ent == NULL) {
2167 rc = -ENOMEM;
2168 goto err_out_regions;
2169 }
2170
2171 memset(probe_ent, 0, sizeof(*probe_ent));
2172 probe_ent->dev = pci_dev_to_dev(pdev);
2173 INIT_LIST_HEAD(&probe_ent->node);
2174
Brett Russ31961942005-09-30 01:36:00 -04002175 mmio_base = pci_iomap(pdev, MV_PRIMARY_BAR, 0);
Brett Russ20f733e2005-09-01 18:26:17 -04002176 if (mmio_base == NULL) {
2177 rc = -ENOMEM;
2178 goto err_out_free_ent;
2179 }
2180
2181 hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
2182 if (!hpriv) {
2183 rc = -ENOMEM;
2184 goto err_out_iounmap;
2185 }
2186 memset(hpriv, 0, sizeof(*hpriv));
2187
2188 probe_ent->sht = mv_port_info[board_idx].sht;
2189 probe_ent->host_flags = mv_port_info[board_idx].host_flags;
2190 probe_ent->pio_mask = mv_port_info[board_idx].pio_mask;
2191 probe_ent->udma_mask = mv_port_info[board_idx].udma_mask;
2192 probe_ent->port_ops = mv_port_info[board_idx].port_ops;
2193
2194 probe_ent->irq = pdev->irq;
2195 probe_ent->irq_flags = SA_SHIRQ;
2196 probe_ent->mmio_base = mmio_base;
2197 probe_ent->private_data = hpriv;
2198
2199 /* initialize adapter */
Jeff Garzik47c2b672005-11-12 21:13:17 -05002200 rc = mv_init_host(pdev, probe_ent, board_idx);
Brett Russ20f733e2005-09-01 18:26:17 -04002201 if (rc) {
2202 goto err_out_hpriv;
2203 }
Brett Russ20f733e2005-09-01 18:26:17 -04002204
Brett Russ31961942005-09-30 01:36:00 -04002205 /* Enable interrupts */
Jeff Garzikddef9bb2006-02-02 16:17:06 -05002206 if (msi && pci_enable_msi(pdev) == 0) {
Brett Russ31961942005-09-30 01:36:00 -04002207 hpriv->hp_flags |= MV_HP_FLAG_MSI;
2208 } else {
2209 pci_intx(pdev, 1);
Brett Russ20f733e2005-09-01 18:26:17 -04002210 }
2211
Brett Russ31961942005-09-30 01:36:00 -04002212 mv_dump_pci_cfg(pdev, 0x68);
2213 mv_print_info(probe_ent);
Brett Russ20f733e2005-09-01 18:26:17 -04002214
Brett Russ31961942005-09-30 01:36:00 -04002215 if (ata_device_add(probe_ent) == 0) {
2216 rc = -ENODEV; /* No devices discovered */
2217 goto err_out_dev_add;
2218 }
2219
2220 kfree(probe_ent);
Brett Russ20f733e2005-09-01 18:26:17 -04002221 return 0;
2222
Brett Russ31961942005-09-30 01:36:00 -04002223err_out_dev_add:
2224 if (MV_HP_FLAG_MSI & hpriv->hp_flags) {
2225 pci_disable_msi(pdev);
2226 } else {
2227 pci_intx(pdev, 0);
2228 }
2229err_out_hpriv:
Brett Russ20f733e2005-09-01 18:26:17 -04002230 kfree(hpriv);
Brett Russ31961942005-09-30 01:36:00 -04002231err_out_iounmap:
2232 pci_iounmap(pdev, mmio_base);
2233err_out_free_ent:
Brett Russ20f733e2005-09-01 18:26:17 -04002234 kfree(probe_ent);
Brett Russ31961942005-09-30 01:36:00 -04002235err_out_regions:
Brett Russ20f733e2005-09-01 18:26:17 -04002236 pci_release_regions(pdev);
Brett Russ31961942005-09-30 01:36:00 -04002237err_out:
Brett Russ20f733e2005-09-01 18:26:17 -04002238 if (!pci_dev_busy) {
2239 pci_disable_device(pdev);
2240 }
2241
2242 return rc;
2243}
2244
2245static int __init mv_init(void)
2246{
2247 return pci_module_init(&mv_pci_driver);
2248}
2249
2250static void __exit mv_exit(void)
2251{
2252 pci_unregister_driver(&mv_pci_driver);
2253}
2254
2255MODULE_AUTHOR("Brett Russ");
2256MODULE_DESCRIPTION("SCSI low-level driver for Marvell SATA controllers");
2257MODULE_LICENSE("GPL");
2258MODULE_DEVICE_TABLE(pci, mv_pci_tbl);
2259MODULE_VERSION(DRV_VERSION);
2260
Jeff Garzikddef9bb2006-02-02 16:17:06 -05002261module_param(msi, int, 0444);
2262MODULE_PARM_DESC(msi, "Enable use of PCI MSI (0=off, 1=on)");
2263
Brett Russ20f733e2005-09-01 18:26:17 -04002264module_init(mv_init);
2265module_exit(mv_exit);