blob: b741a36a67f3c0ba77acfaa3606ea2031a7742f7 [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>
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050022#include <linux/of_device.h>
Sean Crossbb389192013-09-26 11:24:47 +080023#include <linux/pci.h>
24#include <linux/platform_device.h>
25#include <linux/regmap.h>
26#include <linux/resource.h>
27#include <linux/signal.h>
28#include <linux/types.h>
Lucas Stachd1dc9742014-03-28 17:52:59 +010029#include <linux/interrupt.h>
Sean Crossbb389192013-09-26 11:24:47 +080030
31#include "pcie-designware.h"
32
33#define to_imx6_pcie(x) container_of(x, struct imx6_pcie, pp)
34
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050035enum imx6_pcie_variants {
36 IMX6Q,
Andrey Smirnov4d31c612016-05-02 14:09:10 -050037 IMX6SX,
38 IMX6QP,
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050039};
40
Sean Crossbb389192013-09-26 11:24:47 +080041struct imx6_pcie {
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030042 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050043 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010044 struct clk *pcie_bus;
45 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050046 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010047 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080048 struct pcie_port pp;
49 struct regmap *iomuxc_gpr;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050050 enum imx6_pcie_variants variant;
Sean Crossbb389192013-09-26 11:24:47 +080051 void __iomem *mem_base;
Justin Waters28e3abe2016-01-15 10:24:35 -050052 u32 tx_deemph_gen1;
53 u32 tx_deemph_gen2_3p5db;
54 u32 tx_deemph_gen2_6db;
55 u32 tx_swing_full;
56 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050057 int link_gen;
Sean Crossbb389192013-09-26 11:24:47 +080058};
59
Marek Vasutfa33a6d2013-12-12 22:50:02 +010060/* PCIe Root Complex registers (memory-mapped) */
61#define PCIE_RC_LCR 0x7c
62#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
63#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
64#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
65
Bjorn Helgaas2393f792015-06-12 17:27:43 -050066#define PCIE_RC_LCSR 0x80
67
Sean Crossbb389192013-09-26 11:24:47 +080068/* PCIe Port Logic registers (memory-mapped) */
69#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020070#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
71#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
72#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080073#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
74#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010075#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
76#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080077
78#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
79#define PCIE_PHY_CTRL_DATA_LOC 0
80#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
81#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
82#define PCIE_PHY_CTRL_WR_LOC 18
83#define PCIE_PHY_CTRL_RD_LOC 19
84
85#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
86#define PCIE_PHY_STAT_ACK_LOC 16
87
Marek Vasutfa33a6d2013-12-12 22:50:02 +010088#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
89#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
90
Sean Crossbb389192013-09-26 11:24:47 +080091/* PHY registers (not memory-mapped) */
92#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -030093#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +080094
95#define PHY_RX_OVRD_IN_LO 0x1005
96#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
97#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
98
99static int pcie_phy_poll_ack(void __iomem *dbi_base, int exp_val)
100{
101 u32 val;
102 u32 max_iterations = 10;
103 u32 wait_counter = 0;
104
105 do {
106 val = readl(dbi_base + PCIE_PHY_STAT);
107 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
108 wait_counter++;
109
110 if (val == exp_val)
111 return 0;
112
113 udelay(1);
114 } while (wait_counter < max_iterations);
115
116 return -ETIMEDOUT;
117}
118
119static int pcie_phy_wait_ack(void __iomem *dbi_base, int addr)
120{
121 u32 val;
122 int ret;
123
124 val = addr << PCIE_PHY_CTRL_DATA_LOC;
125 writel(val, dbi_base + PCIE_PHY_CTRL);
126
127 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
128 writel(val, dbi_base + PCIE_PHY_CTRL);
129
130 ret = pcie_phy_poll_ack(dbi_base, 1);
131 if (ret)
132 return ret;
133
134 val = addr << PCIE_PHY_CTRL_DATA_LOC;
135 writel(val, dbi_base + PCIE_PHY_CTRL);
136
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500137 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800138}
139
140/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bogicevic Sasaff3ce482015-12-27 13:21:11 -0800141static int pcie_phy_read(void __iomem *dbi_base, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800142{
143 u32 val, phy_ctl;
144 int ret;
145
146 ret = pcie_phy_wait_ack(dbi_base, addr);
147 if (ret)
148 return ret;
149
150 /* assert Read signal */
151 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
152 writel(phy_ctl, dbi_base + PCIE_PHY_CTRL);
153
154 ret = pcie_phy_poll_ack(dbi_base, 1);
155 if (ret)
156 return ret;
157
158 val = readl(dbi_base + PCIE_PHY_STAT);
159 *data = val & 0xffff;
160
161 /* deassert Read signal */
162 writel(0x00, dbi_base + PCIE_PHY_CTRL);
163
Fabio Estevam8d1ceb52015-08-20 01:31:58 -0500164 return pcie_phy_poll_ack(dbi_base, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800165}
166
167static int pcie_phy_write(void __iomem *dbi_base, int addr, int data)
168{
169 u32 var;
170 int ret;
171
172 /* write addr */
173 /* cap addr */
174 ret = pcie_phy_wait_ack(dbi_base, addr);
175 if (ret)
176 return ret;
177
178 var = data << PCIE_PHY_CTRL_DATA_LOC;
179 writel(var, dbi_base + PCIE_PHY_CTRL);
180
181 /* capture data */
182 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
183 writel(var, dbi_base + PCIE_PHY_CTRL);
184
185 ret = pcie_phy_poll_ack(dbi_base, 1);
186 if (ret)
187 return ret;
188
189 /* deassert cap data */
190 var = data << PCIE_PHY_CTRL_DATA_LOC;
191 writel(var, dbi_base + PCIE_PHY_CTRL);
192
193 /* wait for ack de-assertion */
194 ret = pcie_phy_poll_ack(dbi_base, 0);
195 if (ret)
196 return ret;
197
198 /* assert wr signal */
199 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
200 writel(var, dbi_base + PCIE_PHY_CTRL);
201
202 /* wait for ack */
203 ret = pcie_phy_poll_ack(dbi_base, 1);
204 if (ret)
205 return ret;
206
207 /* deassert wr signal */
208 var = data << PCIE_PHY_CTRL_DATA_LOC;
209 writel(var, dbi_base + PCIE_PHY_CTRL);
210
211 /* wait for ack de-assertion */
212 ret = pcie_phy_poll_ack(dbi_base, 0);
213 if (ret)
214 return ret;
215
216 writel(0x0, dbi_base + PCIE_PHY_CTRL);
217
218 return 0;
219}
220
Lucas Stach53eeb482016-01-15 19:56:47 +0100221static void imx6_pcie_reset_phy(struct pcie_port *pp)
222{
223 u32 tmp;
224
225 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
226 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
227 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
228 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
229
230 usleep_range(2000, 3000);
231
232 pcie_phy_read(pp->dbi_base, PHY_RX_OVRD_IN_LO, &tmp);
233 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
234 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
235 pcie_phy_write(pp->dbi_base, PHY_RX_OVRD_IN_LO, tmp);
236}
237
Sean Crossbb389192013-09-26 11:24:47 +0800238/* Added for PCI abort handling */
239static int imx6q_pcie_abort_handler(unsigned long addr,
240 unsigned int fsr, struct pt_regs *regs)
241{
Sean Crossbb389192013-09-26 11:24:47 +0800242 return 0;
243}
244
245static int imx6_pcie_assert_core_reset(struct pcie_port *pp)
246{
247 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200248 u32 val, gpr1, gpr12;
249
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500250 switch (imx6_pcie->variant) {
251 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500252 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
253 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
254 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
255 /* Force PCIe PHY reset */
256 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
257 IMX6SX_GPR5_PCIE_BTNRST_RESET,
258 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500259 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500260 case IMX6QP:
261 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
262 IMX6Q_GPR1_PCIE_SW_RST,
263 IMX6Q_GPR1_PCIE_SW_RST);
264 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500265 case IMX6Q:
266 /*
267 * If the bootloader already enabled the link we need some
268 * special handling to get the core back into a state where
269 * it is safe to touch it for configuration. As there is
270 * no dedicated reset signal wired up for MX6QDL, we need
271 * to manually force LTSSM into "detect" state before
272 * completely disabling LTSSM, which is a prerequisite for
273 * core configuration.
274 *
275 * If both LTSSM_ENABLE and REF_SSP_ENABLE are active we
276 * have a strong indication that the bootloader activated
277 * the link.
278 */
279 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1, &gpr1);
280 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, &gpr12);
281
282 if ((gpr1 & IMX6Q_GPR1_PCIE_REF_CLK_EN) &&
283 (gpr12 & IMX6Q_GPR12_PCIE_CTL_2)) {
284 val = readl(pp->dbi_base + PCIE_PL_PFLR);
285 val &= ~PCIE_PL_PFLR_LINK_STATE_MASK;
286 val |= PCIE_PL_PFLR_FORCE_LINK;
287 writel(val, pp->dbi_base + PCIE_PL_PFLR);
288
289 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
290 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
291 }
292
293 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
294 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
295 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
296 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
297 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500298 }
299
Sean Crossbb389192013-09-26 11:24:47 +0800300 return 0;
301}
302
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100303static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
304{
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500305 struct pcie_port *pp = &imx6_pcie->pp;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500306 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500307
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500308 switch (imx6_pcie->variant) {
309 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500310 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
311 if (ret) {
312 dev_err(pp->dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500313 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500314 }
315
316 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
317 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500318 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500319 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500320 case IMX6Q:
321 /* power up core phy and enable ref clock */
322 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
323 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
324 /*
325 * the async reset input need ref clock to sync internally,
326 * when the ref clock comes after reset, internal synced
327 * reset time is too short, cannot meet the requirement.
328 * add one ~10us delay here.
329 */
330 udelay(10);
331 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
332 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
333 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500334 }
335
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500336 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100337}
338
Sean Crossbb389192013-09-26 11:24:47 +0800339static int imx6_pcie_deassert_core_reset(struct pcie_port *pp)
340{
341 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
342 int ret;
343
Lucas Stach57526132014-03-28 17:52:55 +0100344 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800345 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100346 dev_err(pp->dev, "unable to enable pcie_phy clock\n");
347 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800348 }
349
Lucas Stach57526132014-03-28 17:52:55 +0100350 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800351 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100352 dev_err(pp->dev, "unable to enable pcie_bus clock\n");
353 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800354 }
355
Lucas Stach57526132014-03-28 17:52:55 +0100356 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800357 if (ret) {
Lucas Stach57526132014-03-28 17:52:55 +0100358 dev_err(pp->dev, "unable to enable pcie clock\n");
359 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800360 }
361
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100362 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
363 if (ret) {
364 dev_err(pp->dev, "unable to enable pcie ref clock\n");
365 goto err_ref_clk;
366 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700367
Richard Zhua2fa6f62014-10-27 13:17:32 +0800368 /* allow the clocks to stabilize */
369 usleep_range(200, 500);
370
Richard Zhubc9ef772013-12-12 22:50:03 +0100371 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300372 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500373 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
374 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100375 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500376 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
377 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100378 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500379
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500380 switch (imx6_pcie->variant) {
381 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500382 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
383 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500384 break;
385 case IMX6QP:
386 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
387 IMX6Q_GPR1_PCIE_SW_RST, 0);
388
389 usleep_range(200, 500);
390 break;
391 case IMX6Q: /* Nothing to do */
392 break;
393 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500394
Sean Crossbb389192013-09-26 11:24:47 +0800395 return 0;
396
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100397err_ref_clk:
398 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100399err_pcie:
400 clk_disable_unprepare(imx6_pcie->pcie_bus);
401err_pcie_bus:
402 clk_disable_unprepare(imx6_pcie->pcie_phy);
403err_pcie_phy:
Sean Crossbb389192013-09-26 11:24:47 +0800404 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800405}
406
407static void imx6_pcie_init_phy(struct pcie_port *pp)
408{
409 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
410
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500411 if (imx6_pcie->variant == IMX6SX)
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500412 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
413 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
414 IMX6SX_GPR12_PCIE_RX_EQ_2);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500415
Sean Crossbb389192013-09-26 11:24:47 +0800416 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
417 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
418
419 /* configure constant input signal to the pcie ctrl and phy */
420 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
421 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
422 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
423 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
424
425 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500426 IMX6Q_GPR8_TX_DEEMPH_GEN1,
427 imx6_pcie->tx_deemph_gen1 << 0);
Sean Crossbb389192013-09-26 11:24:47 +0800428 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500429 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
430 imx6_pcie->tx_deemph_gen2_3p5db << 6);
Sean Crossbb389192013-09-26 11:24:47 +0800431 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500432 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
433 imx6_pcie->tx_deemph_gen2_6db << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800434 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500435 IMX6Q_GPR8_TX_SWING_FULL,
436 imx6_pcie->tx_swing_full << 18);
Sean Crossbb389192013-09-26 11:24:47 +0800437 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
Justin Waters28e3abe2016-01-15 10:24:35 -0500438 IMX6Q_GPR8_TX_SWING_LOW,
439 imx6_pcie->tx_swing_low << 25);
Sean Crossbb389192013-09-26 11:24:47 +0800440}
441
Marek Vasut66a60f92013-12-12 22:50:01 +0100442static int imx6_pcie_wait_for_link(struct pcie_port *pp)
443{
Joao Pinto886bc5c2016-03-10 14:44:35 -0600444 /* check if the link is up or not */
445 if (!dw_pcie_wait_for_link(pp))
446 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100447
Bjorn Helgaas6cbb2472015-06-02 16:47:17 -0500448 dev_dbg(pp->dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
449 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
450 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600451 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100452}
453
Troy Kiskya0427462015-06-12 14:30:16 -0500454static int imx6_pcie_wait_for_speed_change(struct pcie_port *pp)
455{
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500456 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500457 unsigned int retries;
458
459 for (retries = 0; retries < 200; retries++) {
460 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
461 /* Test if the speed change finished. */
462 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
463 return 0;
464 usleep_range(100, 1000);
465 }
466
467 dev_err(pp->dev, "Speed change timeout\n");
468 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800469}
470
Lucas Stachd1dc9742014-03-28 17:52:59 +0100471static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
472{
473 struct pcie_port *pp = arg;
474
475 return dw_handle_msi_irq(pp);
476}
477
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500478static int imx6_pcie_establish_link(struct pcie_port *pp)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100479{
480 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pp);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500481 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500482 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100483
484 /*
485 * Force Gen1 operation when starting the link. In case the link is
486 * started in Gen2 mode, there is a possibility the devices on the
487 * bus will not be detected at all. This happens with PCIe switches.
488 */
489 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
490 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
491 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
492 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
493
494 /* Start LTSSM. */
495 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
496 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
497
498 ret = imx6_pcie_wait_for_link(pp);
Lucas Stach54a47a82016-01-25 16:49:53 -0600499 if (ret) {
500 dev_info(pp->dev, "Link never came up\n");
501 goto err_reset_phy;
502 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100503
Tim Harveya5fcec42016-04-19 19:52:44 -0500504 if (imx6_pcie->link_gen == 2) {
505 /* Allow Gen2 mode after the link is up. */
506 tmp = readl(pp->dbi_base + PCIE_RC_LCR);
507 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
508 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
509 writel(tmp, pp->dbi_base + PCIE_RC_LCR);
510 } else {
511 dev_info(pp->dev, "Link: Gen2 disabled\n");
512 }
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100513
514 /*
515 * Start Directed Speed Change so the best possible speed both link
516 * partners support can be negotiated.
517 */
518 tmp = readl(pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
519 tmp |= PORT_LOGIC_SPEED_CHANGE;
520 writel(tmp, pp->dbi_base + PCIE_LINK_WIDTH_SPEED_CONTROL);
521
Troy Kiskya0427462015-06-12 14:30:16 -0500522 ret = imx6_pcie_wait_for_speed_change(pp);
523 if (ret) {
524 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600525 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100526 }
527
528 /* Make sure link training is finished as well! */
Troy Kiskya0427462015-06-12 14:30:16 -0500529 ret = imx6_pcie_wait_for_link(pp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100530 if (ret) {
531 dev_err(pp->dev, "Failed to bring link up!\n");
Lucas Stach54a47a82016-01-25 16:49:53 -0600532 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100533 }
534
Bjorn Helgaas2393f792015-06-12 17:27:43 -0500535 tmp = readl(pp->dbi_base + PCIE_RC_LCSR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500536 dev_info(pp->dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500537 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600538
539err_reset_phy:
540 dev_dbg(pp->dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
541 readl(pp->dbi_base + PCIE_PHY_DEBUG_R0),
542 readl(pp->dbi_base + PCIE_PHY_DEBUG_R1));
543 imx6_pcie_reset_phy(pp);
544
545 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100546}
547
Sean Crossbb389192013-09-26 11:24:47 +0800548static void imx6_pcie_host_init(struct pcie_port *pp)
549{
Sean Crossbb389192013-09-26 11:24:47 +0800550 imx6_pcie_assert_core_reset(pp);
551
552 imx6_pcie_init_phy(pp);
553
554 imx6_pcie_deassert_core_reset(pp);
555
556 dw_pcie_setup_rc(pp);
557
Bjorn Helgaasfd5da202015-06-02 16:16:44 -0500558 imx6_pcie_establish_link(pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100559
560 if (IS_ENABLED(CONFIG_PCI_MSI))
561 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800562}
563
564static int imx6_pcie_link_up(struct pcie_port *pp)
565{
Lucas Stach4d107d32016-01-25 16:50:02 -0600566 return readl(pp->dbi_base + PCIE_PHY_DEBUG_R1) &
567 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800568}
569
570static struct pcie_host_ops imx6_pcie_host_ops = {
571 .link_up = imx6_pcie_link_up,
572 .host_init = imx6_pcie_host_init,
573};
574
Sachin Kamat44cb5e92014-05-30 12:08:48 +0530575static int __init imx6_add_pcie_port(struct pcie_port *pp,
Sean Crossbb389192013-09-26 11:24:47 +0800576 struct platform_device *pdev)
577{
578 int ret;
579
Lucas Stachd1dc9742014-03-28 17:52:59 +0100580 if (IS_ENABLED(CONFIG_PCI_MSI)) {
581 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
582 if (pp->msi_irq <= 0) {
583 dev_err(&pdev->dev, "failed to get MSI irq\n");
584 return -ENODEV;
585 }
586
587 ret = devm_request_irq(&pdev->dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900588 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200589 IRQF_SHARED | IRQF_NO_THREAD,
590 "mx6-pcie-msi", pp);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100591 if (ret) {
592 dev_err(&pdev->dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300593 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100594 }
595 }
596
Sean Crossbb389192013-09-26 11:24:47 +0800597 pp->root_bus_nr = -1;
598 pp->ops = &imx6_pcie_host_ops;
599
Sean Crossbb389192013-09-26 11:24:47 +0800600 ret = dw_pcie_host_init(pp);
601 if (ret) {
602 dev_err(&pdev->dev, "failed to initialize host\n");
603 return ret;
604 }
605
606 return 0;
607}
608
609static int __init imx6_pcie_probe(struct platform_device *pdev)
610{
611 struct imx6_pcie *imx6_pcie;
612 struct pcie_port *pp;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300613 struct device_node *np = pdev->dev.of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800614 struct resource *dbi_base;
Justin Waters28e3abe2016-01-15 10:24:35 -0500615 struct device_node *node = pdev->dev.of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800616 int ret;
617
618 imx6_pcie = devm_kzalloc(&pdev->dev, sizeof(*imx6_pcie), GFP_KERNEL);
619 if (!imx6_pcie)
620 return -ENOMEM;
621
622 pp = &imx6_pcie->pp;
623 pp->dev = &pdev->dev;
624
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500625 imx6_pcie->variant =
626 (enum imx6_pcie_variants)of_device_get_match_data(&pdev->dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500627
Sean Crossbb389192013-09-26 11:24:47 +0800628 /* Added for PCI abort handling */
629 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
630 "imprecise external abort");
631
632 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800633 pp->dbi_base = devm_ioremap_resource(&pdev->dev, dbi_base);
Fabio Estevamb391bf32013-12-02 01:39:35 -0200634 if (IS_ERR(pp->dbi_base))
635 return PTR_ERR(pp->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800636
637 /* Fetch GPIOs */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300638 imx6_pcie->reset_gpio = of_get_named_gpio(np, "reset-gpio", 0);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500639 imx6_pcie->gpio_active_high = of_property_read_bool(np,
640 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300641 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
642 ret = devm_gpio_request_one(&pdev->dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500643 imx6_pcie->gpio_active_high ?
644 GPIOF_OUT_INIT_HIGH :
645 GPIOF_OUT_INIT_LOW,
646 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300647 if (ret) {
648 dev_err(&pdev->dev, "unable to get reset gpio\n");
649 return ret;
650 }
651 }
Sean Crossbb389192013-09-26 11:24:47 +0800652
Sean Crossbb389192013-09-26 11:24:47 +0800653 /* Fetch clocks */
Lucas Stach57526132014-03-28 17:52:55 +0100654 imx6_pcie->pcie_phy = devm_clk_get(&pdev->dev, "pcie_phy");
655 if (IS_ERR(imx6_pcie->pcie_phy)) {
Sean Crossbb389192013-09-26 11:24:47 +0800656 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100657 "pcie_phy clock source missing or invalid\n");
658 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800659 }
660
Lucas Stach57526132014-03-28 17:52:55 +0100661 imx6_pcie->pcie_bus = devm_clk_get(&pdev->dev, "pcie_bus");
662 if (IS_ERR(imx6_pcie->pcie_bus)) {
Sean Crossbb389192013-09-26 11:24:47 +0800663 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100664 "pcie_bus clock source missing or invalid\n");
665 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800666 }
667
Lucas Stach57526132014-03-28 17:52:55 +0100668 imx6_pcie->pcie = devm_clk_get(&pdev->dev, "pcie");
669 if (IS_ERR(imx6_pcie->pcie)) {
Sean Crossbb389192013-09-26 11:24:47 +0800670 dev_err(&pdev->dev,
Lucas Stach57526132014-03-28 17:52:55 +0100671 "pcie clock source missing or invalid\n");
672 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800673 }
674
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500675 if (imx6_pcie->variant == IMX6SX) {
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500676 imx6_pcie->pcie_inbound_axi = devm_clk_get(&pdev->dev,
677 "pcie_inbound_axi");
678 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
679 dev_err(&pdev->dev,
680 "pcie_incbound_axi clock missing or invalid\n");
681 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
682 }
683 }
684
Sean Crossbb389192013-09-26 11:24:47 +0800685 /* Grab GPR config register range */
686 imx6_pcie->iomuxc_gpr =
687 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
688 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
689 dev_err(&pdev->dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200690 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800691 }
692
Justin Waters28e3abe2016-01-15 10:24:35 -0500693 /* Grab PCIe PHY Tx Settings */
694 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
695 &imx6_pcie->tx_deemph_gen1))
696 imx6_pcie->tx_deemph_gen1 = 0;
697
698 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
699 &imx6_pcie->tx_deemph_gen2_3p5db))
700 imx6_pcie->tx_deemph_gen2_3p5db = 0;
701
702 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
703 &imx6_pcie->tx_deemph_gen2_6db))
704 imx6_pcie->tx_deemph_gen2_6db = 20;
705
706 if (of_property_read_u32(node, "fsl,tx-swing-full",
707 &imx6_pcie->tx_swing_full))
708 imx6_pcie->tx_swing_full = 127;
709
710 if (of_property_read_u32(node, "fsl,tx-swing-low",
711 &imx6_pcie->tx_swing_low))
712 imx6_pcie->tx_swing_low = 127;
713
Tim Harveya5fcec42016-04-19 19:52:44 -0500714 /* Limit link speed */
715 ret = of_property_read_u32(pp->dev->of_node, "fsl,max-link-speed",
716 &imx6_pcie->link_gen);
717 if (ret)
718 imx6_pcie->link_gen = 1;
719
Sean Crossbb389192013-09-26 11:24:47 +0800720 ret = imx6_add_pcie_port(pp, pdev);
721 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200722 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800723
724 platform_set_drvdata(pdev, imx6_pcie);
725 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800726}
727
Lucas Stach3e3e4062014-07-31 20:16:05 +0200728static void imx6_pcie_shutdown(struct platform_device *pdev)
729{
730 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
731
732 /* bring down link, so bootloader gets clean state in case of reboot */
733 imx6_pcie_assert_core_reset(&imx6_pcie->pp);
734}
735
Sean Crossbb389192013-09-26 11:24:47 +0800736static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500737 { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
738 { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500739 { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
Sean Crossbb389192013-09-26 11:24:47 +0800740 {},
741};
742MODULE_DEVICE_TABLE(of, imx6_pcie_of_match);
743
744static struct platform_driver imx6_pcie_driver = {
745 .driver = {
746 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530747 .of_match_table = imx6_pcie_of_match,
Sean Crossbb389192013-09-26 11:24:47 +0800748 },
Lucas Stach3e3e4062014-07-31 20:16:05 +0200749 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800750};
751
752/* Freescale PCIe driver does not allow module unload */
753
754static int __init imx6_pcie_init(void)
755{
756 return platform_driver_probe(&imx6_pcie_driver, imx6_pcie_probe);
757}
Lucas Stach61da50d2014-09-05 09:36:48 -0600758module_init(imx6_pcie_init);
Sean Crossbb389192013-09-26 11:24:47 +0800759
760MODULE_AUTHOR("Sean Cross <xobs@kosagi.com>");
761MODULE_DESCRIPTION("Freescale i.MX6 PCIe host controller driver");
762MODULE_LICENSE("GPL v2");