blob: 69f47ef2a2646099c6ec5c2dea4f3b68eb967f62 [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 {
Petr Štetiar5c5fb402015-11-27 11:56:34 +010035 struct gpio_desc *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
Fabio Estevam111feb72015-09-11 09:08:53 -030077#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +080078
79#define PHY_RX_OVRD_IN_LO 0x1005
80#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
81#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
82
83static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
84{
85 u32 val;
86 u32 max_iterations = 10;
87 u32 wait_counter = 0;
88
89 do {
90 val = readl(dbi_base + PCIE_PHY_STAT);
91 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
92 wait_counter++;
93
94 if (val == exp_val)
95 return 0;
96
97 udelay(1);
98 } while (wait_counter < max_iterations);
99
100 return -ETIMEDOUT;
101}
102
103static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
104{
105 u32 val;
106 int ret;
107
108 val = addr << PCIE_PHY_CTRL_DATA_LOC;
109 writel(val, dbi_base + PCIE_PHY_CTRL);
110
111 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
112 writel(val, dbi_base + PCIE_PHY_CTRL);
113
114 ret = pcie_phy_poll_ack(dbi_base, 1);
115 if (ret)
116 return ret;
117
118 val = addr << PCIE_PHY_CTRL_DATA_LOC;
119 writel(val, dbi_base + PCIE_PHY_CTRL);
120
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500121 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800122}
123
124/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800125static int pcie_phy_read(void __iomem *dbi_base, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800126{
127 u32 val, phy_ctl;
128 int ret;
129
130 ret = pcie_phy_wait_ack(dbi_base, addr);
131 if (ret)
132 return ret;
133
134 /* assert Read signal */
135 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
136 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
137
138 ret = pcie_phy_poll_ack(dbi_base, 1);
139 if (ret)
140 return ret;
141
142 val = readl(dbi_base + PCIE_PHY_STAT);
143 *data = val & 0xffff;
144
145 /* deassert Read signal */
146 writel(0x00, dbi_base + PCIE_PHY_CTRL);
147
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500148 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800149}
150
151static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
152{
153 u32 var;
154 int ret;
155
156 /* write addr */
157 /* cap addr */
158 ret = pcie_phy_wait_ack(dbi_base, addr);
159 if (ret)
160 return ret;
161
162 var = data << PCIE_PHY_CTRL_DATA_LOC;
163 writel(var, dbi_base + PCIE_PHY_CTRL);
164
165 /* capture data */
166 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
167 writel(var, dbi_base + PCIE_PHY_CTRL);
168
169 ret = pcie_phy_poll_ack(dbi_base, 1);
170 if (ret)
171 return ret;
172
173 /* deassert cap data */
174 var = data << PCIE_PHY_CTRL_DATA_LOC;
175 writel(var, dbi_base + PCIE_PHY_CTRL);
176
177 /* wait for ack de-assertion */
178 ret = pcie_phy_poll_ack(dbi_base, 0);
179 if (ret)
180 return ret;
181
182 /* assert wr signal */
183 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
184 writel(var, dbi_base + PCIE_PHY_CTRL);
185
186 /* wait for ack */
187 ret = pcie_phy_poll_ack(dbi_base, 1);
188 if (ret)
189 return ret;
190
191 /* deassert wr signal */
192 var = data << PCIE_PHY_CTRL_DATA_LOC;
193 writel(var, dbi_base + PCIE_PHY_CTRL);
194
195 /* wait for ack de-assertion */
196 ret = pcie_phy_poll_ack(dbi_base, 0);
197 if (ret)
198 return ret;
199
200 writel(0x0, dbi_base + PCIE_PHY_CTRL);
201
202 return 0;
203}
204
Lucas Stach53eeb482016-01-15 19:56:47 +0100205static void imx6_pcie_reset_phy(struct pcie_port *pp)
206{
207 u32 tmp;
208
209 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
210 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
211 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
212 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
213
214 usleep_range(2000, 3000);
215
216 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
217 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
218 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
219 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
220}
221
Sean Crossbb389192013-09-26 11:24:47 +0800222/* Added for PCI abort handling */
223static int imx6q_pcie_abort_handler(unsigned long addr,
224 unsigned int fsr, struct pt_regs *regs)
225{
Sean Crossbb389192013-09-26 11:24:47 +0800226 return 0;
227}
228
229static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
230{
231 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200232 u32 val, gpr1, gpr12;
233
234 /*
235 * If the bootloader already enabled the link we need some special
236 * handling to get the core back into a state where it is safe to
237 * touch it for configuration. As there is no dedicated reset signal
238 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
239 * state before completely disabling LTSSM, which is a prerequisite
240 * for core configuration.
241 *
242 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
243 * indication that the bootloader activated the link.
244 */
245 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
246 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
247
248 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
249 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
250 val = readl(pp->dbi_base + PCIE_PL_PFLR);
251 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
252 val |= PCIE_PL_PFLR_FORCE_LINK;
253 writel(val, pp->dbi_base + PCIE_PL_PFLR);
254
255 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
256 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
257 }
Sean Crossbb389192013-09-26 11:24:47 +0800258
259 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
260 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800261 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
262 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
263
Sean Crossbb389192013-09-26 11:24:47 +0800264 return 0;
265}
266
267static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
268{
269 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
270 int ret;
271
Lucas Stach57526132014-03-28 17:52:55 +0100272 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800273 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100274 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
275 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800276 }
277
Lucas Stach57526132014-03-28 17:52:55 +0100278 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800279 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100280 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
281 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800282 }
283
Lucas Stach57526132014-03-28 17:52:55 +0100284 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800285 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100286 dev_err(pp->dev, "unable to enable pcie clock\n");
287 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800288 }
289
Tim Harvey3fce0e82014-08-07 23:36:40 -0700290 /* power up core phy and enable ref clock */
291 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
292 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
Richard Zhua2fa6f62014-10-27 13:17:32 +0800293 /*
294 * the async reset input need ref clock to sync internally,
295 * when the ref clock comes after reset, internal synced
296 * reset time is too short, cannot meet the requirement.
297 * add one ~10us delay here.
298 */
299 udelay(10);
Tim Harvey3fce0e82014-08-07 23:36:40 -0700300 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
301 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
302
Richard Zhua2fa6f62014-10-27 13:17:32 +0800303 /* allow the clocks to stabilize */
304 usleep_range(200, 500);
305
Richard Zhubc9ef772013-12-12 22:50:03 +0100306 /* Some boards don't have PCIe reset GPIO. */
Petr Štetiar5c5fb402015-11-27 11:56:34 +0100307 if (imx6_pcie->reset_gpio) {
308 gpiod_set_value_cansleep(imx6_pcie->reset_gpio, 0);
Richard Zhubc9ef772013-12-12 22:50:03 +0100309 msleep(100);
Petr Štetiar5c5fb402015-11-27 11:56:34 +0100310 gpiod_set_value_cansleep(imx6_pcie->reset_gpio, 1);
Richard Zhubc9ef772013-12-12 22:50:03 +0100311 }
Sean Crossbb389192013-09-26 11:24:47 +0800312 return 0;
313
Lucas Stach57526132014-03-28 17:52:55 +0100314err_pcie:
315 clk_disable_unprepare(imx6_pcie->pcie_bus);
316err_pcie_bus:
317 clk_disable_unprepare(imx6_pcie->pcie_phy);
318err_pcie_phy:
Sean Crossbb389192013-09-26 11:24:47 +0800319 return ret;
320
321}
322
323static void imx6_pcie_init_phy(struct pcie_port *pp)
324{
325 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
326
327 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
328 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
329
330 /* configure constant input signal to the pcie ctrl and phy */
331 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
332 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
333 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
334 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
335
336 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
337 IMX6Q_GPR8_TX_DEEMPH_GEN1, 0 << 0);
338 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
339 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB, 0 << 6);
340 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
341 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB, 20 << 12);
342 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
343 IMX6Q_GPR8_TX_SWING_FULL, 127 << 18);
344 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
345 IMX6Q_GPR8_TX_SWING_LOW, 127 << 25);
346}
347
Marek Vasut66a60f92013-12-12 22:50:01 +0100348static int imx6_pcie_wait_for_link(struct pcie_port *pp)
349{
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500350 unsigned int retries;
Marek Vasut66a60f92013-12-12 22:50:01 +0100351
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500352 for (retries = 0; retries < 200; retries++) {
353 if (dw_pcie_link_up(pp))
354 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100355 usleep_range(100, 1000);
Marek Vasut66a60f92013-12-12 22:50:01 +0100356 }
357
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500358 return -EINVAL;
Marek Vasut66a60f92013-12-12 22:50:01 +0100359}
360
Troy Kiskya0427462015-06-12 14:30:16 -0500361static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
362{
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500363 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500364 unsigned int retries;
365
366 for (retries = 0; retries < 200; retries++) {
367 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
368 /* Test if the speed change finished. */
369 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
370 return 0;
371 usleep_range(100, 1000);
372 }
373
374 dev_err(pp->dev, "Speed change timeout\n");
375 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800376}
377
Lucas Stachd1dc9742014-03-28 17:52:59 +0100378static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
379{
380 struct pcie_port *pp = arg;
381
382 return dw_handle_msi_irq(pp);
383}
384
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500385static int imx6_pcie_establish_link(struct pcie_port *pp)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100386{
387 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500388 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500389 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100390
391 /*
392 * Force Gen1 operation when starting the link. In case the link is
393 * started in Gen2 mode, there is a possibility the devices on the
394 * bus will not be detected at all. This happens with PCIe switches.
395 */
396 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
397 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
398 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
399 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
400
401 /* Start LTSSM. */
402 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
403 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
404
405 ret = imx6_pcie_wait_for_link(pp);
Lucas Stach54a47a82016-01-25 16:49:53 -0600406 if (ret) {
407 dev_info(pp->dev, "Link never came up\n");
408 goto err_reset_phy;
409 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100410
411 /* Allow Gen2 mode after the link is up. */
412 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
413 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
414 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
415 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
416
417 /*
418 * Start Directed Speed Change so the best possible speed both link
419 * partners support can be negotiated.
420 */
421 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
422 tmp |= PORT_LOGIC_SPEED_CHANGE;
423 writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
424
Troy Kiskya0427462015-06-12 14:30:16 -0500425 ret = imx6_pcie_wait_for_speed_change(pp);
426 if (ret) {
427 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600428 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100429 }
430
431 /* Make sure link training is finished as well! */
Troy Kiskya0427462015-06-12 14:30:16 -0500432 ret = imx6_pcie_wait_for_link(pp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100433 if (ret) {
434 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600435 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100436 }
437
Bjorn Helgaas2393f792015-06-12 17:27:43 -0500438 tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
Troy Kiskya0427462015-06-12 14:30:16 -0500439 dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
Lucas Stach54a47a82016-01-25 16:49:53 -0600440
Troy Kiskya0427462015-06-12 14:30:16 -0500441 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600442
443err_reset_phy:
444 dev_dbg(pp->dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
445 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
446 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
447 imx6_pcie_reset_phy(pp);
448
449 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100450}
451
Sean Crossbb389192013-09-26 11:24:47 +0800452static void imx6_pcie_host_init(struct pcie_port *pp)
453{
Sean Crossbb389192013-09-26 11:24:47 +0800454 imx6_pcie_assert_core_reset(pp);
455
456 imx6_pcie_init_phy(pp);
457
458 imx6_pcie_deassert_core_reset(pp);
459
460 dw_pcie_setup_rc(pp);
461
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500462 imx6_pcie_establish_link(pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100463
464 if (IS_ENABLED(CONFIG_PCI_MSI))
465 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800466}
467
468static 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
Fabio Estevam111feb72015-09-11 09:08:53 -0300514 if (rx_valid & PCIE_PHY_RX_ASIC_OUT_VALID)
Sean Crossbb389192013-09-26 11:24:47 +0800515 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
Sean Crossbb389192013-09-26 11:24:47 +0800520 return 0;
521}
522
523static struct pcie_host_ops imx6_pcie_host_ops = {
524 .link_up = imx6_pcie_link_up,
525 .host_init = imx6_pcie_host_init,
526};
527
Sachin Kamat44cb5e92014-05-30 12:08:48 +0530528static int __init imx6_add_pcie_port(struct pcie_port *pp,
Sean Crossbb389192013-09-26 11:24:47 +0800529 struct platform_device *pdev)
530{
531 int ret;
532
Lucas Stachd1dc9742014-03-28 17:52:59 +0100533 if (IS_ENABLED(CONFIG_PCI_MSI)) {
534 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
535 if (pp->msi_irq <= 0) {
536 dev_err(&pdev->dev, "failed to get MSI irq\n");
537 return -ENODEV;
538 }
539
540 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900541 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200542 IRQF_SHARED | IRQF_NO_THREAD,
543 "mx6-pcie-msi", pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100544 if (ret) {
545 dev_err(&pdev->dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300546 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100547 }
548 }
549
Sean Crossbb389192013-09-26 11:24:47 +0800550 pp->root_bus_nr = -1;
551 pp->ops = &imx6_pcie_host_ops;
552
Sean Crossbb389192013-09-26 11:24:47 +0800553 ret = dw_pcie_host_init(pp);
554 if (ret) {
555 dev_err(&pdev->dev, "failed to initialize host\n");
556 return ret;
557 }
558
559 return 0;
560}
561
562static int __init imx6_pcie_probe(struct platform_device *pdev)
563{
564 struct imx6_pcie *imx6_pcie;
565 struct pcie_port *pp;
Sean Crossbb389192013-09-26 11:24:47 +0800566 struct resource *dbi_base;
567 int ret;
568
569 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
570 if (!imx6_pcie)
571 return -ENOMEM;
572
573 pp = &imx6_pcie->pp;
574 pp->dev = &pdev->dev;
575
576 /* Added for PCI abort handling */
577 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
578 "imprecise external abort");
579
580 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800581 pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200582 if (IS_ERR(pp->dbi_base))
583 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800584
585 /* Fetch GPIOs */
Petr Štetiar5c5fb402015-11-27 11:56:34 +0100586 imx6_pcie->reset_gpio = devm_gpiod_get_optional(&pdev->dev, "reset",
587 GPIOD_OUT_LOW);
Sean Crossbb389192013-09-26 11:24:47 +0800588
Sean Crossbb389192013-09-26 11:24:47 +0800589 /* Fetch clocks */
Lucas Stach57526132014-03-28 17:52:55 +0100590 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
591 if (IS_ERR(imx6_pcie->pcie_phy)) {
Sean Crossbb389192013-09-26 11:24:47 +0800592 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100593 "pcie_phy clock source missing or invalid\n");
594 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800595 }
596
Lucas Stach57526132014-03-28 17:52:55 +0100597 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
598 if (IS_ERR(imx6_pcie->pcie_bus)) {
Sean Crossbb389192013-09-26 11:24:47 +0800599 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100600 "pcie_bus clock source missing or invalid\n");
601 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800602 }
603
Lucas Stach57526132014-03-28 17:52:55 +0100604 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
605 if (IS_ERR(imx6_pcie->pcie)) {
Sean Crossbb389192013-09-26 11:24:47 +0800606 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100607 "pcie clock source missing or invalid\n");
608 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800609 }
610
611 /* Grab GPR config register range */
612 imx6_pcie->iomuxc_gpr =
613 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
614 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
615 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200616 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800617 }
618
619 ret = imx6_add_pcie_port(pp, pdev);
620 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200621 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800622
623 platform_set_drvdata(pdev, imx6_pcie);
624 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800625}
626
Lucas Stach3e3e4062014-07-31 20:16:05 +0200627static void imx6_pcie_shutdown(struct platform_device *pdev)
628{
629 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
630
631 /* bring down link, so bootloader gets clean state in case of reboot */
632 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
633}
634
Sean Crossbb389192013-09-26 11:24:47 +0800635static const struct of_device_id imx6_pcie_of_match[] = {
636 { .compatible = "fsl,imx6q-pcie", },
637 {},
638};
639MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
640
641static struct platform_driver imx6_pcie_driver = {
642 .driver = {
643 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530644 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800645 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200646 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800647};
648
649/* Freescale PCIe driver does not allow module unload */
650
651static int __init imx6_pcie_init(void)
652{
653 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
654}
Lucas Stach61da50d2014-09-05 09:36:48 -0600655module_init(imx6_pcie_init);
Sean Crossbb389192013-09-26 11:24:47 +0800656
657MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
658MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
659MODULE_LICENSE("GPL v2");