blob: 8c9b3896d6f53db3b7bdf9df38780ed6d249515e [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;
Justin Waters28e3abe2016-01-15 10:24:35 -050042 u32 tx_deemph_gen1;
43 u32 tx_deemph_gen2_3p5db;
44 u32 tx_deemph_gen2_6db;
45 u32 tx_swing_full;
46 u32 tx_swing_low;
Sean Crossbb389192013-09-26 11:24:47 +080047};
48
Marek Vasutfa33a6d2013-12-12 22:50:02 +010049/* PCIe Root Complex registers (memory-mapped) */
50#define PCIE_RC_LCR 0x7c
51#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
52#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
53#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
54
Bjorn Helgaas2393f792015-06-12 17:27:43 -050055#define PCIE_RC_LCSR 0x80
56
Sean Crossbb389192013-09-26 11:24:47 +080057/* PCIe Port Logic registers (memory-mapped) */
58#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020059#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
60#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
61#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080062#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
63#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010064#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
65#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080066
67#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
68#define PCIE_PHY_CTRL_DATA_LOC 0
69#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
70#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
71#define PCIE_PHY_CTRL_WR_LOC 18
72#define PCIE_PHY_CTRL_RD_LOC 19
73
74#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
75#define PCIE_PHY_STAT_ACK_LOC 16
76
Marek Vasutfa33a6d2013-12-12 22:50:02 +010077#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
78#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
79
Sean Crossbb389192013-09-26 11:24:47 +080080/* PHY registers (not memory-mapped) */
81#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -030082#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +080083
84#define PHY_RX_OVRD_IN_LO 0x1005
85#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
86#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
87
88static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
89{
90 u32 val;
91 u32 max_iterations = 10;
92 u32 wait_counter = 0;
93
94 do {
95 val = readl(dbi_base + PCIE_PHY_STAT);
96 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
97 wait_counter++;
98
99 if (val == exp_val)
100 return 0;
101
102 udelay(1);
103 } while (wait_counter < max_iterations);
104
105 return -ETIMEDOUT;
106}
107
108static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
109{
110 u32 val;
111 int ret;
112
113 val = addr << PCIE_PHY_CTRL_DATA_LOC;
114 writel(val, dbi_base + PCIE_PHY_CTRL);
115
116 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
117 writel(val, dbi_base + PCIE_PHY_CTRL);
118
119 ret = pcie_phy_poll_ack(dbi_base, 1);
120 if (ret)
121 return ret;
122
123 val = addr << PCIE_PHY_CTRL_DATA_LOC;
124 writel(val, dbi_base + PCIE_PHY_CTRL);
125
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500126 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800127}
128
129/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800130static int pcie_phy_read(void __iomem *dbi_base, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800131{
132 u32 val, phy_ctl;
133 int ret;
134
135 ret = pcie_phy_wait_ack(dbi_base, addr);
136 if (ret)
137 return ret;
138
139 /* assert Read signal */
140 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
141 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
142
143 ret = pcie_phy_poll_ack(dbi_base, 1);
144 if (ret)
145 return ret;
146
147 val = readl(dbi_base + PCIE_PHY_STAT);
148 *data = val & 0xffff;
149
150 /* deassert Read signal */
151 writel(0x00, dbi_base + PCIE_PHY_CTRL);
152
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500153 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800154}
155
156static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
157{
158 u32 var;
159 int ret;
160
161 /* write addr */
162 /* cap addr */
163 ret = pcie_phy_wait_ack(dbi_base, addr);
164 if (ret)
165 return ret;
166
167 var = data << PCIE_PHY_CTRL_DATA_LOC;
168 writel(var, dbi_base + PCIE_PHY_CTRL);
169
170 /* capture data */
171 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
172 writel(var, dbi_base + PCIE_PHY_CTRL);
173
174 ret = pcie_phy_poll_ack(dbi_base, 1);
175 if (ret)
176 return ret;
177
178 /* deassert cap data */
179 var = data << PCIE_PHY_CTRL_DATA_LOC;
180 writel(var, dbi_base + PCIE_PHY_CTRL);
181
182 /* wait for ack de-assertion */
183 ret = pcie_phy_poll_ack(dbi_base, 0);
184 if (ret)
185 return ret;
186
187 /* assert wr signal */
188 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
189 writel(var, dbi_base + PCIE_PHY_CTRL);
190
191 /* wait for ack */
192 ret = pcie_phy_poll_ack(dbi_base, 1);
193 if (ret)
194 return ret;
195
196 /* deassert wr signal */
197 var = data << PCIE_PHY_CTRL_DATA_LOC;
198 writel(var, dbi_base + PCIE_PHY_CTRL);
199
200 /* wait for ack de-assertion */
201 ret = pcie_phy_poll_ack(dbi_base, 0);
202 if (ret)
203 return ret;
204
205 writel(0x0, dbi_base + PCIE_PHY_CTRL);
206
207 return 0;
208}
209
Lucas Stach53eeb482016-01-15 19:56:47 +0100210static void imx6_pcie_reset_phy(struct pcie_port *pp)
211{
212 u32 tmp;
213
214 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
215 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
216 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
217 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
218
219 usleep_range(2000, 3000);
220
221 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
222 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
223 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
224 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
225}
226
Sean Crossbb389192013-09-26 11:24:47 +0800227/* Added for PCI abort handling */
228static int imx6q_pcie_abort_handler(unsigned long addr,
229 unsigned int fsr, struct pt_regs *regs)
230{
Sean Crossbb389192013-09-26 11:24:47 +0800231 return 0;
232}
233
234static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
235{
236 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200237 u32 val, gpr1, gpr12;
238
239 /*
240 * If the bootloader already enabled the link we need some special
241 * handling to get the core back into a state where it is safe to
242 * touch it for configuration. As there is no dedicated reset signal
243 * wired up for MX6QDL, we need to manually force LTSSM into "detect"
244 * state before completely disabling LTSSM, which is a prerequisite
245 * for core configuration.
246 *
247 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we have a strong
248 * indication that the bootloader activated the link.
249 */
250 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
251 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
252
253 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
254 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
255 val = readl(pp->dbi_base + PCIE_PL_PFLR);
256 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
257 val |= PCIE_PL_PFLR_FORCE_LINK;
258 writel(val, pp->dbi_base + PCIE_PL_PFLR);
259
260 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
261 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
262 }
Sean Crossbb389192013-09-26 11:24:47 +0800263
264 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
265 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800266 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
267 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
268
Sean Crossbb389192013-09-26 11:24:47 +0800269 return 0;
270}
271
272static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
273{
274 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
275 int ret;
276
Lucas Stach57526132014-03-28 17:52:55 +0100277 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800278 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100279 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
280 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800281 }
282
Lucas Stach57526132014-03-28 17:52:55 +0100283 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800284 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100285 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
286 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800287 }
288
Lucas Stach57526132014-03-28 17:52:55 +0100289 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800290 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100291 dev_err(pp->dev, "unable to enable pcie clock\n");
292 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800293 }
294
Tim Harvey3fce0e82014-08-07 23:36:40 -0700295 /* power up core phy and enable ref clock */
296 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
297 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
Richard Zhua2fa6f62014-10-27 13:17:32 +0800298 /*
299 * the async reset input need ref clock to sync internally,
300 * when the ref clock comes after reset, internal synced
301 * reset time is too short, cannot meet the requirement.
302 * add one ~10us delay here.
303 */
304 udelay(10);
Tim Harvey3fce0e82014-08-07 23:36:40 -0700305 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
306 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
307
Richard Zhua2fa6f62014-10-27 13:17:32 +0800308 /* allow the clocks to stabilize */
309 usleep_range(200, 500);
310
Richard Zhubc9ef772013-12-12 22:50:03 +0100311 /* Some boards don't have PCIe reset GPIO. */
Petr Štetiar5c5fb402015-11-27 11:56:34 +0100312 if (imx6_pcie->reset_gpio) {
313 gpiod_set_value_cansleep(imx6_pcie->reset_gpio, 0);
Richard Zhubc9ef772013-12-12 22:50:03 +0100314 msleep(100);
Petr Štetiar5c5fb402015-11-27 11:56:34 +0100315 gpiod_set_value_cansleep(imx6_pcie->reset_gpio, 1);
Richard Zhubc9ef772013-12-12 22:50:03 +0100316 }
Sean Crossbb389192013-09-26 11:24:47 +0800317 return 0;
318
Lucas Stach57526132014-03-28 17:52:55 +0100319err_pcie:
320 clk_disable_unprepare(imx6_pcie->pcie_bus);
321err_pcie_bus:
322 clk_disable_unprepare(imx6_pcie->pcie_phy);
323err_pcie_phy:
Sean Crossbb389192013-09-26 11:24:47 +0800324 return ret;
325
326}
327
328static void imx6_pcie_init_phy(struct pcie_port *pp)
329{
330 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
331
332 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
333 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
334
335 /* configure constant input signal to the pcie ctrl and phy */
336 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
337 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
338 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
339 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
340
341 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500342 IMX6Q_GPR8_TX_DEEMPH_GEN1,
343 imx6_pcie->tx_deemph_gen1 << 0);
Sean Crossbb389192013-09-26 11:24:47 +0800344 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500345 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
346 imx6_pcie->tx_deemph_gen2_3p5db << 6);
Sean Crossbb389192013-09-26 11:24:47 +0800347 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500348 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
349 imx6_pcie->tx_deemph_gen2_6db << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800350 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500351 IMX6Q_GPR8_TX_SWING_FULL,
352 imx6_pcie->tx_swing_full << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800353 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500354 IMX6Q_GPR8_TX_SWING_LOW,
355 imx6_pcie->tx_swing_low << 25);
Sean Crossbb389192013-09-26 11:24:47 +0800356}
357
Marek Vasut66a60f92013-12-12 22:50:01 +0100358static int imx6_pcie_wait_for_link(struct pcie_port *pp)
359{
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500360 unsigned int retries;
Marek Vasut66a60f92013-12-12 22:50:01 +0100361
Lucas Stach4d107d32016-01-25 16:50:02 -0600362 /*
363 * Test if the PHY reports that the link is up and also that the LTSSM
364 * training finished. There are three possible states of the link when
365 * this code is called:
366 * 1) The link is DOWN (unlikely)
367 * The link didn't come up yet for some reason. This usually means
368 * we have a real problem somewhere, if it happens with a peripheral
369 * connected. This state calls for inspection of the DEBUG registers.
370 * 2) The link is UP, but still in LTSSM training
371 * Wait for the training to finish, which should take a very short
372 * time. If the training does not finish, we have a problem and we
373 * need to inspect the DEBUG registers. If the training does finish,
374 * the link is up and operating correctly.
375 * 3) The link is UP and no longer in LTSSM training
376 * The link is up and operating correctly.
377 */
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500378 for (retries = 0; retries < 200; retries++) {
Lucas Stach4d107d32016-01-25 16:50:02 -0600379 u32 reg = readl(pp->dbi_base + PCIE_PHY_DEBUG_R1);
380 if ((reg & PCIE_PHY_DEBUG_R1_XMLH_LINK_UP) &&
381 !(reg & PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING))
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500382 return 0;
Lucas Stach4d107d32016-01-25 16:50:02 -0600383 usleep_range(1000, 2000);
Marek Vasut66a60f92013-12-12 22:50:01 +0100384 }
385
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500386 return -EINVAL;
Marek Vasut66a60f92013-12-12 22:50:01 +0100387}
388
Troy Kiskya0427462015-06-12 14:30:16 -0500389static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
390{
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500391 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500392 unsigned int retries;
393
394 for (retries = 0; retries < 200; retries++) {
395 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
396 /* Test if the speed change finished. */
397 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
398 return 0;
399 usleep_range(100, 1000);
400 }
401
402 dev_err(pp->dev, "Speed change timeout\n");
403 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800404}
405
Lucas Stachd1dc9742014-03-28 17:52:59 +0100406static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
407{
408 struct pcie_port *pp = arg;
409
410 return dw_handle_msi_irq(pp);
411}
412
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500413static int imx6_pcie_establish_link(struct pcie_port *pp)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100414{
415 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500416 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500417 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100418
419 /*
420 * Force Gen1 operation when starting the link. In case the link is
421 * started in Gen2 mode, there is a possibility the devices on the
422 * bus will not be detected at all. This happens with PCIe switches.
423 */
424 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
425 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
426 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
427 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
428
429 /* Start LTSSM. */
430 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
431 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
432
433 ret = imx6_pcie_wait_for_link(pp);
Lucas Stach54a47a82016-01-25 16:49:53 -0600434 if (ret) {
435 dev_info(pp->dev, "Link never came up\n");
436 goto err_reset_phy;
437 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100438
439 /* Allow Gen2 mode after the link is up. */
440 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
441 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
442 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
443 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
444
445 /*
446 * Start Directed Speed Change so the best possible speed both link
447 * partners support can be negotiated.
448 */
449 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
450 tmp |= PORT_LOGIC_SPEED_CHANGE;
451 writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
452
Troy Kiskya0427462015-06-12 14:30:16 -0500453 ret = imx6_pcie_wait_for_speed_change(pp);
454 if (ret) {
455 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600456 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100457 }
458
459 /* Make sure link training is finished as well! */
Troy Kiskya0427462015-06-12 14:30:16 -0500460 ret = imx6_pcie_wait_for_link(pp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100461 if (ret) {
462 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600463 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100464 }
465
Bjorn Helgaas2393f792015-06-12 17:27:43 -0500466 tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
Troy Kiskya0427462015-06-12 14:30:16 -0500467 dev_dbg(pp->dev, "Link up, Gen=%i\n", (tmp >> 16) & 0xf);
Lucas Stach54a47a82016-01-25 16:49:53 -0600468
Troy Kiskya0427462015-06-12 14:30:16 -0500469 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600470
471err_reset_phy:
472 dev_dbg(pp->dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
473 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
474 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
475 imx6_pcie_reset_phy(pp);
476
477 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100478}
479
Sean Crossbb389192013-09-26 11:24:47 +0800480static void imx6_pcie_host_init(struct pcie_port *pp)
481{
Sean Crossbb389192013-09-26 11:24:47 +0800482 imx6_pcie_assert_core_reset(pp);
483
484 imx6_pcie_init_phy(pp);
485
486 imx6_pcie_deassert_core_reset(pp);
487
488 dw_pcie_setup_rc(pp);
489
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500490 imx6_pcie_establish_link(pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100491
492 if (IS_ENABLED(CONFIG_PCI_MSI))
493 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800494}
495
496static int imx6_pcie_link_up(struct pcie_port *pp)
497{
Lucas Stach4d107d32016-01-25 16:50:02 -0600498 return readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) &
499 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800500}
501
502static struct pcie_host_ops imx6_pcie_host_ops = {
503 .link_up = imx6_pcie_link_up,
504 .host_init = imx6_pcie_host_init,
505};
506
Sachin Kamat44cb5e92014-05-30 12:08:48 +0530507static int __init imx6_add_pcie_port(struct pcie_port *pp,
Sean Crossbb389192013-09-26 11:24:47 +0800508 struct platform_device *pdev)
509{
510 int ret;
511
Lucas Stachd1dc9742014-03-28 17:52:59 +0100512 if (IS_ENABLED(CONFIG_PCI_MSI)) {
513 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
514 if (pp->msi_irq <= 0) {
515 dev_err(&pdev->dev, "failed to get MSI irq\n");
516 return -ENODEV;
517 }
518
519 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900520 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200521 IRQF_SHARED | IRQF_NO_THREAD,
522 "mx6-pcie-msi", pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100523 if (ret) {
524 dev_err(&pdev->dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300525 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100526 }
527 }
528
Sean Crossbb389192013-09-26 11:24:47 +0800529 pp->root_bus_nr = -1;
530 pp->ops = &imx6_pcie_host_ops;
531
Sean Crossbb389192013-09-26 11:24:47 +0800532 ret = dw_pcie_host_init(pp);
533 if (ret) {
534 dev_err(&pdev->dev, "failed to initialize host\n");
535 return ret;
536 }
537
538 return 0;
539}
540
541static int __init imx6_pcie_probe(struct platform_device *pdev)
542{
543 struct imx6_pcie *imx6_pcie;
544 struct pcie_port *pp;
Sean Crossbb389192013-09-26 11:24:47 +0800545 struct resource *dbi_base;
Justin Waters28e3abe2016-01-15 10:24:35 -0500546 struct device_node *node = pdev->dev.of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800547 int ret;
548
549 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
550 if (!imx6_pcie)
551 return -ENOMEM;
552
553 pp = &imx6_pcie->pp;
554 pp->dev = &pdev->dev;
555
556 /* Added for PCI abort handling */
557 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
558 "imprecise external abort");
559
560 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800561 pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200562 if (IS_ERR(pp->dbi_base))
563 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800564
565 /* Fetch GPIOs */
Petr Štetiar5c5fb402015-11-27 11:56:34 +0100566 imx6_pcie->reset_gpio = devm_gpiod_get_optional(&pdev->dev, "reset",
567 GPIOD_OUT_LOW);
Sean Crossbb389192013-09-26 11:24:47 +0800568
Sean Crossbb389192013-09-26 11:24:47 +0800569 /* Fetch clocks */
Lucas Stach57526132014-03-28 17:52:55 +0100570 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
571 if (IS_ERR(imx6_pcie->pcie_phy)) {
Sean Crossbb389192013-09-26 11:24:47 +0800572 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100573 "pcie_phy clock source missing or invalid\n");
574 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800575 }
576
Lucas Stach57526132014-03-28 17:52:55 +0100577 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
578 if (IS_ERR(imx6_pcie->pcie_bus)) {
Sean Crossbb389192013-09-26 11:24:47 +0800579 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100580 "pcie_bus clock source missing or invalid\n");
581 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800582 }
583
Lucas Stach57526132014-03-28 17:52:55 +0100584 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
585 if (IS_ERR(imx6_pcie->pcie)) {
Sean Crossbb389192013-09-26 11:24:47 +0800586 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100587 "pcie clock source missing or invalid\n");
588 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800589 }
590
591 /* Grab GPR config register range */
592 imx6_pcie->iomuxc_gpr =
593 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
594 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
595 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200596 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800597 }
598
Justin Waters28e3abe2016-01-15 10:24:35 -0500599 /* Grab PCIe PHY Tx Settings */
600 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
601 &imx6_pcie->tx_deemph_gen1))
602 imx6_pcie->tx_deemph_gen1 = 0;
603
604 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
605 &imx6_pcie->tx_deemph_gen2_3p5db))
606 imx6_pcie->tx_deemph_gen2_3p5db = 0;
607
608 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
609 &imx6_pcie->tx_deemph_gen2_6db))
610 imx6_pcie->tx_deemph_gen2_6db = 20;
611
612 if (of_property_read_u32(node, "fsl,tx-swing-full",
613 &imx6_pcie->tx_swing_full))
614 imx6_pcie->tx_swing_full = 127;
615
616 if (of_property_read_u32(node, "fsl,tx-swing-low",
617 &imx6_pcie->tx_swing_low))
618 imx6_pcie->tx_swing_low = 127;
619
Sean Crossbb389192013-09-26 11:24:47 +0800620 ret = imx6_add_pcie_port(pp, pdev);
621 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200622 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800623
624 platform_set_drvdata(pdev, imx6_pcie);
625 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800626}
627
Lucas Stach3e3e4062014-07-31 20:16:05 +0200628static void imx6_pcie_shutdown(struct platform_device *pdev)
629{
630 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
631
632 /* bring down link, so bootloader gets clean state in case of reboot */
633 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
634}
635
Sean Crossbb389192013-09-26 11:24:47 +0800636static const struct of_device_id imx6_pcie_of_match[] = {
637 { .compatible = "fsl,imx6q-pcie", },
638 {},
639};
640MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
641
642static struct platform_driver imx6_pcie_driver = {
643 .driver = {
644 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530645 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800646 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200647 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800648};
649
650/* Freescale PCIe driver does not allow module unload */
651
652static int __init imx6_pcie_init(void)
653{
654 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
655}
Lucas Stach61da50d2014-09-05 09:36:48 -0600656module_init(imx6_pcie_init);
Sean Crossbb389192013-09-26 11:24:47 +0800657
658MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
659MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
660MODULE_LICENSE("GPL v2");