blob: 233a196c6e6661c8dc9aea975a5cf15bf253c2fb [file] [log] [blame]
Sean Crossbb389192013-09-26 11:24:47 +08001/*
2 * PCIe host controller driver for Freescale i.MX6 SoCs
3 *
4 * Copyright (C) 2013 Kosagi
5 * http://www.kosagi.com
6 *
7 * Author: Sean Cross <xobs@kosagi.com>
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 version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/clk.h>
15#include <linux/delay.h>
16#include <linux/gpio.h>
17#include <linux/kernel.h>
18#include <linux/mfd/syscon.h>
19#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
20#include <linux/module.h>
21#include <linux/of_gpio.h>
22#include <linux/pci.h>
23#include <linux/platform_device.h>
24#include <linux/regmap.h>
25#include <linux/resource.h>
26#include <linux/signal.h>
27#include <linux/types.h>
Lucas Stachd1dc9742014-03-28 17:52:59 +010028#include <linux/interrupt.h>
Sean Crossbb389192013-09-26 11:24:47 +080029
30#include "pcie-designware.h"
31
32#define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
33
34struct imx6_pcie {
35 int reset_gpio;
Lucas Stach57526132014-03-28 17:52:55 +010036 struct clk *pcie_bus;
37 struct clk *pcie_phy;
38 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080039 struct pcie_port pp;
40 struct regmap *iomuxc_gpr;
41 void __iomem *mem_base;
42};
43
Marek Vasutfa33a6d2013-12-12 22:50:02 +010044/* PCIe Root Complex registers (memory-mapped) */
45#define PCIE_RC_LCR 0x7c
46#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
47#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
48#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
49
Bjorn Helgaas2393f792015-06-12 17:27:43 -050050#define PCIE_RC_LCSR 0x80
51
Sean Crossbb389192013-09-26 11:24:47 +080052/* PCIe Port Logic registers (memory-mapped) */
53#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020054#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
55#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
56#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080057#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
58#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010059#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
60#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080061
62#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
63#define PCIE_PHY_CTRL_DATA_LOC 0
64#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
65#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
66#define PCIE_PHY_CTRL_WR_LOC 18
67#define PCIE_PHY_CTRL_RD_LOC 19
68
69#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
70#define PCIE_PHY_STAT_ACK_LOC 16
71
Marek Vasutfa33a6d2013-12-12 22:50:02 +010072#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
73#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
74
Sean Crossbb389192013-09-26 11:24:47 +080075/* PHY registers (not memory-mapped) */
76#define PCIE_PHY_RX_ASIC_OUT 0x100D
77
78#define PHY_RX_OVRD_IN_LO 0x1005
79#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
80#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
81
82static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
83{
84 u32 val;
85 u32 max_iterations = 10;
86 u32 wait_counter = 0;
87
88 do {
89 val = readl(dbi_base + PCIE_PHY_STAT);
90 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
91 wait_counter++;
92
93 if (val == exp_val)
94 return 0;
95
96 udelay(1);
97 } while (wait_counter < max_iterations);
98
99 return -ETIMEDOUT;
100}
101
102static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
103{
104 u32 val;
105 int ret;
106
107 val = addr << PCIE_PHY_CTRL_DATA_LOC;
108 writel(val, dbi_base + PCIE_PHY_CTRL);
109
110 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
111 writel(val, dbi_base + PCIE_PHY_CTRL);
112
113 ret = pcie_phy_poll_ack(dbi_base, 1);
114 if (ret)
115 return ret;
116
117 val = addr << PCIE_PHY_CTRL_DATA_LOC;
118 writel(val, dbi_base + PCIE_PHY_CTRL);
119
120 ret = pcie_phy_poll_ack(dbi_base, 0);
121 if (ret)
122 return ret;
123
124 return 0;
125}
126
127/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
128static int pcie_phy_read(void __iomem *dbi_base, int addr , int *data)
129{
130 u32 val, phy_ctl;
131 int ret;
132
133 ret = pcie_phy_wait_ack(dbi_base, addr);
134 if (ret)
135 return ret;
136
137 /* assert Read signal */
138 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
139 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
140
141 ret = pcie_phy_poll_ack(dbi_base, 1);
142 if (ret)
143 return ret;
144
145 val = readl(dbi_base + PCIE_PHY_STAT);
146 *data = val & 0xffff;
147
148 /* deassert Read signal */
149 writel(0x00, dbi_base + PCIE_PHY_CTRL);
150
151 ret = pcie_phy_poll_ack(dbi_base, 0);
152 if (ret)
153 return ret;
154
155 return 0;
156}
157
158static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
159{
160 u32 var;
161 int ret;
162
163 /* write addr */
164 /* cap addr */
165 ret = pcie_phy_wait_ack(dbi_base, addr);
166 if (ret)
167 return ret;
168
169 var = data << PCIE_PHY_CTRL_DATA_LOC;
170 writel(var, dbi_base + PCIE_PHY_CTRL);
171
172 /* capture data */
173 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
174 writel(var, dbi_base + PCIE_PHY_CTRL);
175
176 ret = pcie_phy_poll_ack(dbi_base, 1);
177 if (ret)
178 return ret;
179
180 /* deassert cap data */
181 var = data << PCIE_PHY_CTRL_DATA_LOC;
182 writel(var, dbi_base + PCIE_PHY_CTRL);
183
184 /* wait for ack de-assertion */
185 ret = pcie_phy_poll_ack(dbi_base, 0);
186 if (ret)
187 return ret;
188
189 /* assert wr signal */
190 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
191 writel(var, dbi_base + PCIE_PHY_CTRL);
192
193 /* wait for ack */
194 ret = pcie_phy_poll_ack(dbi_base, 1);
195 if (ret)
196 return ret;
197
198 /* deassert wr signal */
199 var = data << PCIE_PHY_CTRL_DATA_LOC;
200 writel(var, dbi_base + PCIE_PHY_CTRL);
201
202 /* wait for ack de-assertion */
203 ret = pcie_phy_poll_ack(dbi_base, 0);
204 if (ret)
205 return ret;
206
207 writel(0x0, dbi_base + PCIE_PHY_CTRL);
208
209 return 0;
210}
211
212/* Added for PCI abort handling */
213static int imx6q_pcie_abort_handler(unsigned long addr,
214 unsigned int fsr, struct pt_regs *regs)
215{
Sean Crossbb389192013-09-26 11:24:47 +0800216 return 0;
217}
218
219static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
220{
221 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200222 u32 val, gpr1, gpr12;
223
224 /*
225 * If the bootloader already enabled the link we need some special
226 * handling to get the core back into a state where it is safe to
227 * touch it for configuration. As there is no dedicated reset signal
228 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
229 * state before completely disabling LTSSM, which is a prerequisite
230 * for core configuration.
231 *
232 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
233 * indication that the bootloader activated the link.
234 */
235 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
236 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
237
238 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
239 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
240 val = readl(pp->dbi_base + PCIE_PL_PFLR);
241 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
242 val |= PCIE_PL_PFLR_FORCE_LINK;
243 writel(val, pp->dbi_base + PCIE_PL_PFLR);
244
245 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
246 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
247 }
Sean Crossbb389192013-09-26 11:24:47 +0800248
249 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
250 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800251 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
252 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
253
Sean Crossbb389192013-09-26 11:24:47 +0800254 return 0;
255}
256
257static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
258{
259 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
260 int ret;
261
Lucas Stach57526132014-03-28 17:52:55 +0100262 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800263 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100264 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
265 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800266 }
267
Lucas Stach57526132014-03-28 17:52:55 +0100268 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800269 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100270 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
271 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800272 }
273
Lucas Stach57526132014-03-28 17:52:55 +0100274 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800275 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100276 dev_err(pp->dev, "unable to enable pcie clock\n");
277 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800278 }
279
Tim Harvey3fce0e82014-08-07 23:36:40 -0700280 /* power up core phy and enable ref clock */
281 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
282 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
Richard Zhua2fa6f62014-10-27 13:17:32 +0800283 /*
284 * the async reset input need ref clock to sync internally,
285 * when the ref clock comes after reset, internal synced
286 * reset time is too short, cannot meet the requirement.
287 * add one ~10us delay here.
288 */
289 udelay(10);
Tim Harvey3fce0e82014-08-07 23:36:40 -0700290 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
291 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
292
Richard Zhua2fa6f62014-10-27 13:17:32 +0800293 /* allow the clocks to stabilize */
294 usleep_range(200, 500);
295
Richard Zhubc9ef772013-12-12 22:50:03 +0100296 /* Some boards don't have PCIe reset GPIO. */
297 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
298 gpio_set_value(imx6_pcie->reset_gpio, 0);
299 msleep(100);
300 gpio_set_value(imx6_pcie->reset_gpio, 1);
301 }
Sean Crossbb389192013-09-26 11:24:47 +0800302 return 0;
303
Lucas Stach57526132014-03-28 17:52:55 +0100304err_pcie:
305 clk_disable_unprepare(imx6_pcie->pcie_bus);
306err_pcie_bus:
307 clk_disable_unprepare(imx6_pcie->pcie_phy);
308err_pcie_phy:
Sean Crossbb389192013-09-26 11:24:47 +0800309 return ret;
310
311}
312
313static void imx6_pcie_init_phy(struct pcie_port *pp)
314{
315 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
316
317 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
318 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
319
320 /* configure constant input signal to the pcie ctrl and phy */
321 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
322 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
323 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
324 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
325
326 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
327 IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
328 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
329 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
330 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
331 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
332 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
333 IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
334 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
335 IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
336}
337
Marek Vasut66a60f92013-12-12 22:50:01 +0100338static int imx6_pcie_wait_for_link(struct pcie_port *pp)
339{
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500340 unsigned int retries;
Marek Vasut66a60f92013-12-12 22:50:01 +0100341
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500342 for (retries = 0; retries < 200; retries++) {
343 if (dw_pcie_link_up(pp))
344 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100345 usleep_range(100, 1000);
Marek Vasut66a60f92013-12-12 22:50:01 +0100346 }
347
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500348 dev_err(pp->dev, "phy link never came up\n");
349 dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
350 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
351 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
352 return -EINVAL;
Marek Vasut66a60f92013-12-12 22:50:01 +0100353}
354
Troy Kiskya0427462015-06-12 14:30:16 -0500355static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
356{
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500357 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500358 unsigned int retries;
359
360 for (retries = 0; retries < 200; retries++) {
361 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
362 /* Test if the speed change finished. */
363 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
364 return 0;
365 usleep_range(100, 1000);
366 }
367
368 dev_err(pp->dev, "Speed change timeout\n");
369 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800370}
371
Lucas Stachd1dc9742014-03-28 17:52:59 +0100372static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
373{
374 struct pcie_port *pp = arg;
375
376 return dw_handle_msi_irq(pp);
377}
378
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500379static int imx6_pcie_establish_link(struct pcie_port *pp)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100380{
381 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500382 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500383 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100384
385 /*
386 * Force Gen1 operation when starting the link. In case the link is
387 * started in Gen2 mode, there is a possibility the devices on the
388 * bus will not be detected at all. This happens with PCIe switches.
389 */
390 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
391 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
392 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
393 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
394
395 /* Start LTSSM. */
396 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
397 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
398
399 ret = imx6_pcie_wait_for_link(pp);
400 if (ret)
401 return ret;
402
403 /* Allow Gen2 mode after the link is up. */
404 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
405 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
406 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
407 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
408
409 /*
410 * Start Directed Speed Change so the best possible speed both link
411 * partners support can be negotiated.
412 */
413 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
414 tmp |= PORT_LOGIC_SPEED_CHANGE;
415 writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
416
Troy Kiskya0427462015-06-12 14:30:16 -0500417 ret = imx6_pcie_wait_for_speed_change(pp);
418 if (ret) {
419 dev_err(pp->dev, "Failed to bring link up!\n");
420 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100421 }
422
423 /* Make sure link training is finished as well! */
Troy Kiskya0427462015-06-12 14:30:16 -0500424 ret = imx6_pcie_wait_for_link(pp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100425 if (ret) {
426 dev_err(pp->dev, "Failed to bring link up!\n");
Troy Kiskya0427462015-06-12 14:30:16 -0500427 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100428 }
429
Bjorn Helgaas2393f792015-06-12 17:27:43 -0500430 tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
Troy Kiskya0427462015-06-12 14:30:16 -0500431 dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
432 return 0;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100433}
434
Sean Crossbb389192013-09-26 11:24:47 +0800435static void imx6_pcie_host_init(struct pcie_port *pp)
436{
Sean Crossbb389192013-09-26 11:24:47 +0800437 imx6_pcie_assert_core_reset(pp);
438
439 imx6_pcie_init_phy(pp);
440
441 imx6_pcie_deassert_core_reset(pp);
442
443 dw_pcie_setup_rc(pp);
444
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500445 imx6_pcie_establish_link(pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100446
447 if (IS_ENABLED(CONFIG_PCI_MSI))
448 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800449}
450
Marek Vasut982aa232013-12-12 22:50:00 +0100451static void imx6_pcie_reset_phy(struct pcie_port *pp)
452{
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500453 u32 tmp;
Marek Vasut982aa232013-12-12 22:50:00 +0100454
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500455 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
456 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
457 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
458 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
Marek Vasut982aa232013-12-12 22:50:00 +0100459
460 usleep_range(2000, 3000);
461
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500462 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
463 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
Marek Vasut982aa232013-12-12 22:50:00 +0100464 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500465 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
Marek Vasut982aa232013-12-12 22:50:00 +0100466}
467
Sean Crossbb389192013-09-26 11:24:47 +0800468static int imx6_pcie_link_up(struct pcie_port *pp)
469{
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700470 u32 rc, debug_r0, rx_valid;
471 int count = 5;
Sean Crossbb389192013-09-26 11:24:47 +0800472
Marek Vasut7f9f40c2013-12-12 22:49:59 +0100473 /*
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700474 * Test if the PHY reports that the link is up and also that the LTSSM
475 * training finished. There are three possible states of the link when
476 * this code is called:
477 * 1) The link is DOWN (unlikely)
478 * The link didn't come up yet for some reason. This usually means
479 * we have a real problem somewhere. Reset the PHY and exit. This
480 * state calls for inspection of the DEBUG registers.
481 * 2) The link is UP, but still in LTSSM training
482 * Wait for the training to finish, which should take a very short
483 * time. If the training does not finish, we have a problem and we
484 * need to inspect the DEBUG registers. If the training does finish,
485 * the link is up and operating correctly.
486 * 3) The link is UP and no longer in LTSSM training
487 * The link is up and operating correctly.
Marek Vasut7f9f40c2013-12-12 22:49:59 +0100488 */
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700489 while (1) {
490 rc = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
491 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP))
492 break;
493 if (!(rc & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING))
494 return 1;
495 if (!count--)
496 break;
497 dev_dbg(pp->dev, "Link is up, but still in training\n");
498 /*
499 * Wait a little bit, then re-check if the link finished
500 * the training.
501 */
502 usleep_range(1000, 2000);
503 }
Sean Crossbb389192013-09-26 11:24:47 +0800504 /*
505 * From L0, initiate MAC entry to gen2 if EP/RC supports gen2.
506 * Wait 2ms (LTSSM timeout is 24ms, PHY lock is ~5us in gen2).
507 * If (MAC/LTSSM.state == Recovery.RcvrLock)
508 * && (PHY/rx_valid==0) then pulse PHY/rx_reset. Transition
509 * to gen2 is stuck
510 */
511 pcie_phy_read(pp->dbi_base, PCIE_PHY_RX_ASIC_OUT, &rx_valid);
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700512 debug_r0 = readl(pp->dbi_base + PCIE_PHY_DEBUG_R0);
Sean Crossbb389192013-09-26 11:24:47 +0800513
514 if (rx_valid & 0x01)
515 return 0;
516
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700517 if ((debug_r0 & 0x3f) != 0x0d)
Sean Crossbb389192013-09-26 11:24:47 +0800518 return 0;
519
520 dev_err(pp->dev, "transition to gen2 is stuck, reset PHY!\n");
Marek Vasutf95d3ae2014-02-19 13:22:18 -0700521 dev_dbg(pp->dev, "debug_r0=%08x debug_r1=%08x\n", debug_r0, rc);
Sean Crossbb389192013-09-26 11:24:47 +0800522
Marek Vasut982aa232013-12-12 22:50:00 +0100523 imx6_pcie_reset_phy(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800524
525 return 0;
526}
527
528static struct pcie_host_ops imx6_pcie_host_ops = {
529 .link_up = imx6_pcie_link_up,
530 .host_init = imx6_pcie_host_init,
531};
532
Sachin Kamat44cb5e92014-05-30 12:08:48 +0530533static int __init imx6_add_pcie_port(struct pcie_port *pp,
Sean Crossbb389192013-09-26 11:24:47 +0800534 struct platform_device *pdev)
535{
536 int ret;
537
Lucas Stachd1dc9742014-03-28 17:52:59 +0100538 if (IS_ENABLED(CONFIG_PCI_MSI)) {
539 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
540 if (pp->msi_irq <= 0) {
541 dev_err(&pdev->dev, "failed to get MSI irq\n");
542 return -ENODEV;
543 }
544
545 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900546 imx6_pcie_msi_handler,
547 IRQF_SHARED, "mx6-pcie-msi", pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100548 if (ret) {
549 dev_err(&pdev->dev, "failed to request MSI irq\n");
550 return -ENODEV;
551 }
552 }
553
Sean Crossbb389192013-09-26 11:24:47 +0800554 pp->root_bus_nr = -1;
555 pp->ops = &imx6_pcie_host_ops;
556
Sean Crossbb389192013-09-26 11:24:47 +0800557 ret = dw_pcie_host_init(pp);
558 if (ret) {
559 dev_err(&pdev->dev, "failed to initialize host\n");
560 return ret;
561 }
562
563 return 0;
564}
565
566static int __init imx6_pcie_probe(struct platform_device *pdev)
567{
568 struct imx6_pcie *imx6_pcie;
569 struct pcie_port *pp;
570 struct device_node *np = pdev->dev.of_node;
571 struct resource *dbi_base;
572 int ret;
573
574 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
575 if (!imx6_pcie)
576 return -ENOMEM;
577
578 pp = &imx6_pcie->pp;
579 pp->dev = &pdev->dev;
580
581 /* Added for PCI abort handling */
582 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
583 "imprecise external abort");
584
585 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800586 pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200587 if (IS_ERR(pp->dbi_base))
588 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800589
590 /* Fetch GPIOs */
591 imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
Marek Vasutc28f8a12013-12-12 22:49:58 +0100592 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
593 ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
594 GPIOF_OUT_INIT_LOW, "PCIe reset");
595 if (ret) {
596 dev_err(&pdev->dev, "unable to get reset gpio\n");
597 return ret;
598 }
Sean Crossbb389192013-09-26 11:24:47 +0800599 }
600
Sean Crossbb389192013-09-26 11:24:47 +0800601 /* Fetch clocks */
Lucas Stach57526132014-03-28 17:52:55 +0100602 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
603 if (IS_ERR(imx6_pcie->pcie_phy)) {
Sean Crossbb389192013-09-26 11:24:47 +0800604 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100605 "pcie_phy clock source missing or invalid\n");
606 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800607 }
608
Lucas Stach57526132014-03-28 17:52:55 +0100609 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
610 if (IS_ERR(imx6_pcie->pcie_bus)) {
Sean Crossbb389192013-09-26 11:24:47 +0800611 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100612 "pcie_bus clock source missing or invalid\n");
613 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800614 }
615
Lucas Stach57526132014-03-28 17:52:55 +0100616 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
617 if (IS_ERR(imx6_pcie->pcie)) {
Sean Crossbb389192013-09-26 11:24:47 +0800618 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100619 "pcie clock source missing or invalid\n");
620 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800621 }
622
623 /* Grab GPR config register range */
624 imx6_pcie->iomuxc_gpr =
625 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
626 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
627 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200628 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800629 }
630
631 ret = imx6_add_pcie_port(pp, pdev);
632 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200633 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800634
635 platform_set_drvdata(pdev, imx6_pcie);
636 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800637}
638
Lucas Stach3e3e4062014-07-31 20:16:05 +0200639static void imx6_pcie_shutdown(struct platform_device *pdev)
640{
641 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
642
643 /* bring down link, so bootloader gets clean state in case of reboot */
644 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
645}
646
Sean Crossbb389192013-09-26 11:24:47 +0800647static const struct of_device_id imx6_pcie_of_match[] = {
648 { .compatible = "fsl,imx6q-pcie", },
649 {},
650};
651MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
652
653static struct platform_driver imx6_pcie_driver = {
654 .driver = {
655 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530656 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800657 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200658 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800659};
660
661/* Freescale PCIe driver does not allow module unload */
662
663static int __init imx6_pcie_init(void)
664{
665 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
666}
Lucas Stach61da50d2014-09-05 09:36:48 -0600667module_init(imx6_pcie_init);
Sean Crossbb389192013-09-26 11:24:47 +0800668
669MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
670MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
671MODULE_LICENSE("GPL v2");