blob: 9717ef71e71b20b5feaf5e1d9d658137bda28f3f [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>
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070020#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
Sean Crossbb389192013-09-26 11:24:47 +080021#include <linux/module.h>
22#include <linux/of_gpio.h>
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050023#include <linux/of_device.h>
Sean Crossbb389192013-09-26 11:24:47 +080024#include <linux/pci.h>
25#include <linux/platform_device.h>
26#include <linux/regmap.h>
Quentin Schulzc26ebe92017-06-08 10:07:42 +020027#include <linux/regulator/consumer.h>
Sean Crossbb389192013-09-26 11:24:47 +080028#include <linux/resource.h>
29#include <linux/signal.h>
30#include <linux/types.h>
Lucas Stachd1dc9742014-03-28 17:52:59 +010031#include <linux/interrupt.h>
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070032#include <linux/reset.h>
Sean Crossbb389192013-09-26 11:24:47 +080033
34#include "pcie-designware.h"
35
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053036#define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
Sean Crossbb389192013-09-26 11:24:47 +080037
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050038enum imx6_pcie_variants {
39 IMX6Q,
Andrey Smirnov4d31c612016-05-02 14:09:10 -050040 IMX6SX,
41 IMX6QP,
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070042 IMX7D,
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050043};
44
Sean Crossbb389192013-09-26 11:24:47 +080045struct imx6_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053046 struct dw_pcie *pci;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030047 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050048 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010049 struct clk *pcie_bus;
50 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050051 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010052 struct clk *pcie;
Sean Crossbb389192013-09-26 11:24:47 +080053 struct regmap *iomuxc_gpr;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070054 struct reset_control *pciephy_reset;
55 struct reset_control *apps_reset;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050056 enum imx6_pcie_variants variant;
Justin Waters28e3abe2016-01-15 10:24:35 -050057 u32 tx_deemph_gen1;
58 u32 tx_deemph_gen2_3p5db;
59 u32 tx_deemph_gen2_6db;
60 u32 tx_swing_full;
61 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050062 int link_gen;
Quentin Schulzc26ebe92017-06-08 10:07:42 +020063 struct regulator *vpcie;
Sean Crossbb389192013-09-26 11:24:47 +080064};
65
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070066/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
67#define PHY_PLL_LOCK_WAIT_MAX_RETRIES 2000
68#define PHY_PLL_LOCK_WAIT_USLEEP_MIN 50
69#define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
70
Marek Vasutfa33a6d2013-12-12 22:50:02 +010071/* PCIe Root Complex registers (memory-mapped) */
72#define PCIE_RC_LCR 0x7c
73#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
74#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
75#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
76
Bjorn Helgaas2393f792015-06-12 17:27:43 -050077#define PCIE_RC_LCSR 0x80
78
Sean Crossbb389192013-09-26 11:24:47 +080079/* PCIe Port Logic registers (memory-mapped) */
80#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +020081#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
82#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
83#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +080084#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
85#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
Marek Vasut7f9f40c2013-12-12 22:49:59 +010086#define PCIE_PHY_DEBUG_R1_XMLH_LINK_IN_TRAINING (1 << 29)
87#define PCIE_PHY_DEBUG_R1_XMLH_LINK_UP (1 << 4)
Sean Crossbb389192013-09-26 11:24:47 +080088
89#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
90#define PCIE_PHY_CTRL_DATA_LOC 0
91#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
92#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
93#define PCIE_PHY_CTRL_WR_LOC 18
94#define PCIE_PHY_CTRL_RD_LOC 19
95
96#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
97#define PCIE_PHY_STAT_ACK_LOC 16
98
Marek Vasutfa33a6d2013-12-12 22:50:02 +010099#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
100#define PORT_LOGIC_SPEED_CHANGE (0x1 << 17)
101
Sean Crossbb389192013-09-26 11:24:47 +0800102/* PHY registers (not memory-mapped) */
103#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -0300104#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +0800105
106#define PHY_RX_OVRD_IN_LO 0x1005
107#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
108#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
109
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500110static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
Sean Crossbb389192013-09-26 11:24:47 +0800111{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530112 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800113 u32 val;
114 u32 max_iterations = 10;
115 u32 wait_counter = 0;
116
117 do {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530118 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800119 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
120 wait_counter++;
121
122 if (val == exp_val)
123 return 0;
124
125 udelay(1);
126 } while (wait_counter < max_iterations);
127
128 return -ETIMEDOUT;
129}
130
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500131static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
Sean Crossbb389192013-09-26 11:24:47 +0800132{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530133 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800134 u32 val;
135 int ret;
136
137 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530138 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800139
140 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530141 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800142
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500143 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800144 if (ret)
145 return ret;
146
147 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530148 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800149
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500150 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800151}
152
153/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500154static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800155{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530156 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800157 u32 val, phy_ctl;
158 int ret;
159
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500160 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800161 if (ret)
162 return ret;
163
164 /* assert Read signal */
165 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530166 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
Sean Crossbb389192013-09-26 11:24:47 +0800167
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500168 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800169 if (ret)
170 return ret;
171
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530172 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800173 *data = val & 0xffff;
174
175 /* deassert Read signal */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530176 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
Sean Crossbb389192013-09-26 11:24:47 +0800177
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500178 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800179}
180
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500181static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data)
Sean Crossbb389192013-09-26 11:24:47 +0800182{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530183 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800184 u32 var;
185 int ret;
186
187 /* write addr */
188 /* cap addr */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500189 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800190 if (ret)
191 return ret;
192
193 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530194 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800195
196 /* capture data */
197 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530198 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800199
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500200 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800201 if (ret)
202 return ret;
203
204 /* deassert cap data */
205 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530206 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800207
208 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500209 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800210 if (ret)
211 return ret;
212
213 /* assert wr signal */
214 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530215 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800216
217 /* wait for ack */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500218 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800219 if (ret)
220 return ret;
221
222 /* deassert wr signal */
223 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530224 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800225
226 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500227 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800228 if (ret)
229 return ret;
230
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530231 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
Sean Crossbb389192013-09-26 11:24:47 +0800232
233 return 0;
234}
235
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500236static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
Lucas Stach53eeb482016-01-15 19:56:47 +0100237{
238 u32 tmp;
239
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500240 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100241 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
242 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500243 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100244
245 usleep_range(2000, 3000);
246
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500247 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100248 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
249 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500250 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100251}
252
Sean Crossbb389192013-09-26 11:24:47 +0800253/* Added for PCI abort handling */
254static int imx6q_pcie_abort_handler(unsigned long addr,
255 unsigned int fsr, struct pt_regs *regs)
256{
Sean Crossbb389192013-09-26 11:24:47 +0800257 return 0;
258}
259
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500260static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800261{
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200262 struct device *dev = imx6_pcie->pci->dev;
263
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500264 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700265 case IMX7D:
266 reset_control_assert(imx6_pcie->pciephy_reset);
267 reset_control_assert(imx6_pcie->apps_reset);
268 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500269 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500270 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
271 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
272 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
273 /* Force PCIe PHY reset */
274 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
275 IMX6SX_GPR5_PCIE_BTNRST_RESET,
276 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500277 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500278 case IMX6QP:
279 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
280 IMX6Q_GPR1_PCIE_SW_RST,
281 IMX6Q_GPR1_PCIE_SW_RST);
282 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500283 case IMX6Q:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500284 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
285 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
286 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
287 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
288 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500289 }
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200290
291 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
292 int ret = regulator_disable(imx6_pcie->vpcie);
293
294 if (ret)
295 dev_err(dev, "failed to disable vpcie regulator: %d\n",
296 ret);
297 }
Sean Crossbb389192013-09-26 11:24:47 +0800298}
299
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100300static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
301{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530302 struct dw_pcie *pci = imx6_pcie->pci;
303 struct device *dev = pci->dev;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500304 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500305
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500306 switch (imx6_pcie->variant) {
307 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500308 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
309 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500310 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500311 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500312 }
313
314 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
315 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500316 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500317 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500318 case IMX6Q:
319 /* power up core phy and enable ref clock */
320 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
321 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
322 /*
323 * the async reset input need ref clock to sync internally,
324 * when the ref clock comes after reset, internal synced
325 * reset time is too short, cannot meet the requirement.
326 * add one ~10us delay here.
327 */
328 udelay(10);
329 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
330 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
331 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700332 case IMX7D:
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
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700339static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
340{
341 u32 val;
342 unsigned int retries;
343 struct device *dev = imx6_pcie->pci->dev;
344
345 for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES; retries++) {
346 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR22, &val);
347
348 if (val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED)
349 return;
350
351 usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN,
352 PHY_PLL_LOCK_WAIT_USLEEP_MAX);
353 }
354
355 dev_err(dev, "PCIe PLL lock timeout\n");
356}
357
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500358static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800359{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530360 struct dw_pcie *pci = imx6_pcie->pci;
361 struct device *dev = pci->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800362 int ret;
363
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200364 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
365 ret = regulator_enable(imx6_pcie->vpcie);
366 if (ret) {
367 dev_err(dev, "failed to enable vpcie regulator: %d\n",
368 ret);
369 return;
370 }
371 }
372
Lucas Stach57526132014-03-28 17:52:55 +0100373 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800374 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500375 dev_err(dev, "unable to enable pcie_phy clock\n");
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200376 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800377 }
378
Lucas Stach57526132014-03-28 17:52:55 +0100379 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800380 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500381 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100382 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800383 }
384
Lucas Stach57526132014-03-28 17:52:55 +0100385 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800386 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500387 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100388 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800389 }
390
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100391 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
392 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500393 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100394 goto err_ref_clk;
395 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700396
Richard Zhua2fa6f62014-10-27 13:17:32 +0800397 /* allow the clocks to stabilize */
398 usleep_range(200, 500);
399
Richard Zhubc9ef772013-12-12 22:50:03 +0100400 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300401 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500402 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
403 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100404 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500405 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
406 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100407 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500408
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500409 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700410 case IMX7D:
411 reset_control_deassert(imx6_pcie->pciephy_reset);
412 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
413 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500414 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500415 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
416 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500417 break;
418 case IMX6QP:
419 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
420 IMX6Q_GPR1_PCIE_SW_RST, 0);
421
422 usleep_range(200, 500);
423 break;
424 case IMX6Q: /* Nothing to do */
425 break;
426 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500427
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500428 return;
Sean Crossbb389192013-09-26 11:24:47 +0800429
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100430err_ref_clk:
431 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100432err_pcie:
433 clk_disable_unprepare(imx6_pcie->pcie_bus);
434err_pcie_bus:
435 clk_disable_unprepare(imx6_pcie->pcie_phy);
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200436err_pcie_phy:
437 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
438 ret = regulator_disable(imx6_pcie->vpcie);
439 if (ret)
440 dev_err(dev, "failed to disable vpcie regulator: %d\n",
441 ret);
442 }
Sean Crossbb389192013-09-26 11:24:47 +0800443}
444
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500445static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800446{
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700447 switch (imx6_pcie->variant) {
448 case IMX7D:
449 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
450 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
451 break;
452 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500453 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
454 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
455 IMX6SX_GPR12_PCIE_RX_EQ_2);
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700456 /* FALLTHROUGH */
457 default:
458 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
459 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500460
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700461 /* configure constant input signal to the pcie ctrl and phy */
462 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
463 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
Sean Crossbb389192013-09-26 11:24:47 +0800464
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700465 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
466 IMX6Q_GPR8_TX_DEEMPH_GEN1,
467 imx6_pcie->tx_deemph_gen1 << 0);
468 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
469 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
470 imx6_pcie->tx_deemph_gen2_3p5db << 6);
471 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
472 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
473 imx6_pcie->tx_deemph_gen2_6db << 12);
474 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
475 IMX6Q_GPR8_TX_SWING_FULL,
476 imx6_pcie->tx_swing_full << 18);
477 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
478 IMX6Q_GPR8_TX_SWING_LOW,
479 imx6_pcie->tx_swing_low << 25);
480 break;
481 }
482
Sean Crossbb389192013-09-26 11:24:47 +0800483 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
484 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800485}
486
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500487static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
Marek Vasut66a60f92013-12-12 22:50:01 +0100488{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530489 struct dw_pcie *pci = imx6_pcie->pci;
490 struct device *dev = pci->dev;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500491
Joao Pinto886bc5c2016-03-10 14:44:35 -0600492 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530493 if (!dw_pcie_wait_for_link(pci))
Joao Pinto886bc5c2016-03-10 14:44:35 -0600494 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100495
Bjorn Helgaas13957652016-10-06 13:35:18 -0500496 dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530497 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
498 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600499 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100500}
501
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500502static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500503{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530504 struct dw_pcie *pci = imx6_pcie->pci;
505 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500506 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500507 unsigned int retries;
508
509 for (retries = 0; retries < 200; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530510 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500511 /* Test if the speed change finished. */
512 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
513 return 0;
514 usleep_range(100, 1000);
515 }
516
Bjorn Helgaas13957652016-10-06 13:35:18 -0500517 dev_err(dev, "Speed change timeout\n");
Troy Kiskya0427462015-06-12 14:30:16 -0500518 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800519}
520
Lucas Stachd1dc9742014-03-28 17:52:59 +0100521static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
522{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500523 struct imx6_pcie *imx6_pcie = arg;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530524 struct dw_pcie *pci = imx6_pcie->pci;
525 struct pcie_port *pp = &pci->pp;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100526
527 return dw_handle_msi_irq(pp);
528}
529
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500530static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100531{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530532 struct dw_pcie *pci = imx6_pcie->pci;
533 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500534 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500535 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100536
537 /*
538 * Force Gen1 operation when starting the link. In case the link is
539 * started in Gen2 mode, there is a possibility the devices on the
540 * bus will not be detected at all. This happens with PCIe switches.
541 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530542 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100543 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
544 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530545 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100546
547 /* Start LTSSM. */
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700548 if (imx6_pcie->variant == IMX7D)
549 reset_control_deassert(imx6_pcie->apps_reset);
550 else
551 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
552 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100553
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500554 ret = imx6_pcie_wait_for_link(imx6_pcie);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200555 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600556 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100557
Tim Harveya5fcec42016-04-19 19:52:44 -0500558 if (imx6_pcie->link_gen == 2) {
559 /* Allow Gen2 mode after the link is up. */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530560 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500561 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
562 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530563 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100564
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700565 /*
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700566 * Start Directed Speed Change so the best possible
567 * speed both link partners support can be negotiated.
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700568 */
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700569 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
570 tmp |= PORT_LOGIC_SPEED_CHANGE;
571 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700572
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700573 if (imx6_pcie->variant != IMX7D) {
574 /*
575 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
576 * from i.MX6 family when no link speed transition
577 * occurs and we go Gen1 -> yep, Gen1. The difference
578 * is that, in such case, it will not be cleared by HW
579 * which will cause the following code to report false
580 * failure.
581 */
582
583 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
584 if (ret) {
585 dev_err(dev, "Failed to bring link up!\n");
586 goto err_reset_phy;
587 }
588 }
589
590 /* Make sure link training is finished as well! */
591 ret = imx6_pcie_wait_for_link(imx6_pcie);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700592 if (ret) {
593 dev_err(dev, "Failed to bring link up!\n");
594 goto err_reset_phy;
595 }
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700596 } else {
597 dev_info(dev, "Link: Gen2 disabled\n");
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100598 }
599
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530600 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500601 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500602 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600603
604err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500605 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530606 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
607 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500608 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600609 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100610}
611
Sean Crossbb389192013-09-26 11:24:47 +0800612static void imx6_pcie_host_init(struct pcie_port *pp)
613{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530614 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
615 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Sean Crossbb389192013-09-26 11:24:47 +0800616
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500617 imx6_pcie_assert_core_reset(imx6_pcie);
618 imx6_pcie_init_phy(imx6_pcie);
619 imx6_pcie_deassert_core_reset(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800620 dw_pcie_setup_rc(pp);
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500621 imx6_pcie_establish_link(imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100622
623 if (IS_ENABLED(CONFIG_PCI_MSI))
624 dw_pcie_msi_init(pp);
Sean Crossbb389192013-09-26 11:24:47 +0800625}
626
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530627static int imx6_pcie_link_up(struct dw_pcie *pci)
Sean Crossbb389192013-09-26 11:24:47 +0800628{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530629 return dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1) &
Lucas Stach4d107d32016-01-25 16:50:02 -0600630 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800631}
632
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530633static struct dw_pcie_host_ops imx6_pcie_host_ops = {
Sean Crossbb389192013-09-26 11:24:47 +0800634 .host_init = imx6_pcie_host_init,
635};
636
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700637static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
638 struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800639{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530640 struct dw_pcie *pci = imx6_pcie->pci;
641 struct pcie_port *pp = &pci->pp;
642 struct device *dev = &pdev->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800643 int ret;
644
Lucas Stachd1dc9742014-03-28 17:52:59 +0100645 if (IS_ENABLED(CONFIG_PCI_MSI)) {
646 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
647 if (pp->msi_irq <= 0) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500648 dev_err(dev, "failed to get MSI irq\n");
Lucas Stachd1dc9742014-03-28 17:52:59 +0100649 return -ENODEV;
650 }
651
Bjorn Helgaas13957652016-10-06 13:35:18 -0500652 ret = devm_request_irq(dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900653 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200654 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500655 "mx6-pcie-msi", imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100656 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500657 dev_err(dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300658 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100659 }
660 }
661
Sean Crossbb389192013-09-26 11:24:47 +0800662 pp->root_bus_nr = -1;
663 pp->ops = &imx6_pcie_host_ops;
664
Sean Crossbb389192013-09-26 11:24:47 +0800665 ret = dw_pcie_host_init(pp);
666 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500667 dev_err(dev, "failed to initialize host\n");
Sean Crossbb389192013-09-26 11:24:47 +0800668 return ret;
669 }
670
671 return 0;
672}
673
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530674static const struct dw_pcie_ops dw_pcie_ops = {
675 .link_up = imx6_pcie_link_up,
676};
677
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700678static int imx6_pcie_probe(struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800679{
Bjorn Helgaas13957652016-10-06 13:35:18 -0500680 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530681 struct dw_pcie *pci;
Sean Crossbb389192013-09-26 11:24:47 +0800682 struct imx6_pcie *imx6_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800683 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500684 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800685 int ret;
686
Bjorn Helgaas13957652016-10-06 13:35:18 -0500687 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +0800688 if (!imx6_pcie)
689 return -ENOMEM;
690
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530691 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
692 if (!pci)
693 return -ENOMEM;
694
695 pci->dev = dev;
696 pci->ops = &dw_pcie_ops;
Sean Crossbb389192013-09-26 11:24:47 +0800697
Guenter Roeckc0464062017-02-25 02:08:12 -0800698 imx6_pcie->pci = pci;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500699 imx6_pcie->variant =
Bjorn Helgaas13957652016-10-06 13:35:18 -0500700 (enum imx6_pcie_variants)of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500701
Sean Crossbb389192013-09-26 11:24:47 +0800702 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530703 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
704 if (IS_ERR(pci->dbi_base))
705 return PTR_ERR(pci->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800706
707 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500708 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
709 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500710 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300711 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500712 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500713 imx6_pcie->gpio_active_high ?
714 GPIOF_OUT_INIT_HIGH :
715 GPIOF_OUT_INIT_LOW,
716 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300717 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500718 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300719 return ret;
720 }
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700721 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
722 return imx6_pcie->reset_gpio;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300723 }
Sean Crossbb389192013-09-26 11:24:47 +0800724
Sean Crossbb389192013-09-26 11:24:47 +0800725 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -0500726 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Lucas Stach57526132014-03-28 17:52:55 +0100727 if (IS_ERR(imx6_pcie->pcie_phy)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500728 dev_err(dev, "pcie_phy clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100729 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800730 }
731
Bjorn Helgaas13957652016-10-06 13:35:18 -0500732 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Lucas Stach57526132014-03-28 17:52:55 +0100733 if (IS_ERR(imx6_pcie->pcie_bus)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500734 dev_err(dev, "pcie_bus clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100735 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800736 }
737
Bjorn Helgaas13957652016-10-06 13:35:18 -0500738 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Lucas Stach57526132014-03-28 17:52:55 +0100739 if (IS_ERR(imx6_pcie->pcie)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500740 dev_err(dev, "pcie clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100741 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800742 }
743
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700744 switch (imx6_pcie->variant) {
745 case IMX6SX:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500746 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500747 "pcie_inbound_axi");
748 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
Andrey Smirnov21b72452017-02-07 07:50:25 -0800749 dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500750 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
751 }
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700752 break;
753 case IMX7D:
754 imx6_pcie->pciephy_reset = devm_reset_control_get(dev,
755 "pciephy");
756 if (IS_ERR(imx6_pcie->pciephy_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100757 dev_err(dev, "Failed to get PCIEPHY reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700758 return PTR_ERR(imx6_pcie->pciephy_reset);
759 }
760
761 imx6_pcie->apps_reset = devm_reset_control_get(dev, "apps");
762 if (IS_ERR(imx6_pcie->apps_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100763 dev_err(dev, "Failed to get PCIE APPS reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700764 return PTR_ERR(imx6_pcie->apps_reset);
765 }
766 break;
767 default:
768 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500769 }
770
Sean Crossbb389192013-09-26 11:24:47 +0800771 /* Grab GPR config register range */
772 imx6_pcie->iomuxc_gpr =
773 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
774 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500775 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200776 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800777 }
778
Justin Waters28e3abe2016-01-15 10:24:35 -0500779 /* Grab PCIe PHY Tx Settings */
780 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
781 &imx6_pcie->tx_deemph_gen1))
782 imx6_pcie->tx_deemph_gen1 = 0;
783
784 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
785 &imx6_pcie->tx_deemph_gen2_3p5db))
786 imx6_pcie->tx_deemph_gen2_3p5db = 0;
787
788 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
789 &imx6_pcie->tx_deemph_gen2_6db))
790 imx6_pcie->tx_deemph_gen2_6db = 20;
791
792 if (of_property_read_u32(node, "fsl,tx-swing-full",
793 &imx6_pcie->tx_swing_full))
794 imx6_pcie->tx_swing_full = 127;
795
796 if (of_property_read_u32(node, "fsl,tx-swing-low",
797 &imx6_pcie->tx_swing_low))
798 imx6_pcie->tx_swing_low = 127;
799
Tim Harveya5fcec42016-04-19 19:52:44 -0500800 /* Limit link speed */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500801 ret = of_property_read_u32(node, "fsl,max-link-speed",
Tim Harveya5fcec42016-04-19 19:52:44 -0500802 &imx6_pcie->link_gen);
803 if (ret)
804 imx6_pcie->link_gen = 1;
805
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200806 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
807 if (IS_ERR(imx6_pcie->vpcie)) {
808 if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER)
809 return -EPROBE_DEFER;
810 imx6_pcie->vpcie = NULL;
811 }
812
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530813 platform_set_drvdata(pdev, imx6_pcie);
814
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500815 ret = imx6_add_pcie_port(imx6_pcie, pdev);
Sean Crossbb389192013-09-26 11:24:47 +0800816 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200817 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800818
Sean Crossbb389192013-09-26 11:24:47 +0800819 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800820}
821
Lucas Stach3e3e4062014-07-31 20:16:05 +0200822static void imx6_pcie_shutdown(struct platform_device *pdev)
823{
824 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
825
826 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500827 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200828}
829
Sean Crossbb389192013-09-26 11:24:47 +0800830static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500831 { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
832 { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500833 { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700834 { .compatible = "fsl,imx7d-pcie", .data = (void *)IMX7D, },
Sean Crossbb389192013-09-26 11:24:47 +0800835 {},
836};
Sean Crossbb389192013-09-26 11:24:47 +0800837
838static struct platform_driver imx6_pcie_driver = {
839 .driver = {
840 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530841 .of_match_table = imx6_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -0500842 .suppress_bind_attrs = true,
Sean Crossbb389192013-09-26 11:24:47 +0800843 },
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700844 .probe = imx6_pcie_probe,
Lucas Stach3e3e4062014-07-31 20:16:05 +0200845 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800846};
847
Sean Crossbb389192013-09-26 11:24:47 +0800848static int __init imx6_pcie_init(void)
849{
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700850 /*
851 * Since probe() can be deferred we need to make sure that
852 * hook_fault_code is not called after __init memory is freed
853 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
854 * we can install the handler here without risking it
855 * accessing some uninitialized driver state.
856 */
857 hook_fault_code(16 + 6, imx6q_pcie_abort_handler, SIGBUS, 0,
858 "imprecise external abort");
859
860 return platform_driver_register(&imx6_pcie_driver);
Sean Crossbb389192013-09-26 11:24:47 +0800861}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -0400862device_initcall(imx6_pcie_init);