blob: 20aae4469ee4019ab27650ddb00e7cdbf9880cd2 [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{
Lucas Stach415b6182017-05-22 17:06:30 -0500257 unsigned long pc = instruction_pointer(regs);
258 unsigned long instr = *(unsigned long *)pc;
259 int reg = (instr >> 12) & 15;
260
261 /*
262 * If the instruction being executed was a read,
263 * make it look like it read all-ones.
264 */
265 if ((instr & 0x0c100000) == 0x04100000) {
266 unsigned long val;
267
268 if (instr & 0x00400000)
269 val = 255;
270 else
271 val = -1;
272
273 regs->uregs[reg] = val;
274 regs->ARM_pc += 4;
275 return 0;
276 }
277
278 if ((instr & 0x0e100090) == 0x00100090) {
279 regs->uregs[reg] = -1;
280 regs->ARM_pc += 4;
281 return 0;
282 }
283
284 return 1;
Sean Crossbb389192013-09-26 11:24:47 +0800285}
286
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500287static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800288{
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200289 struct device *dev = imx6_pcie->pci->dev;
290
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500291 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700292 case IMX7D:
293 reset_control_assert(imx6_pcie->pciephy_reset);
294 reset_control_assert(imx6_pcie->apps_reset);
295 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500296 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500297 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
298 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
299 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
300 /* Force PCIe PHY reset */
301 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
302 IMX6SX_GPR5_PCIE_BTNRST_RESET,
303 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500304 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500305 case IMX6QP:
306 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
307 IMX6Q_GPR1_PCIE_SW_RST,
308 IMX6Q_GPR1_PCIE_SW_RST);
309 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500310 case IMX6Q:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500311 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
312 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
313 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
314 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
315 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500316 }
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200317
318 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
319 int ret = regulator_disable(imx6_pcie->vpcie);
320
321 if (ret)
322 dev_err(dev, "failed to disable vpcie regulator: %d\n",
323 ret);
324 }
Sean Crossbb389192013-09-26 11:24:47 +0800325}
326
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100327static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
328{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530329 struct dw_pcie *pci = imx6_pcie->pci;
330 struct device *dev = pci->dev;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500331 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500332
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500333 switch (imx6_pcie->variant) {
334 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500335 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
336 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500337 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500338 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500339 }
340
341 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
342 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500343 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500344 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500345 case IMX6Q:
346 /* power up core phy and enable ref clock */
347 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
348 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
349 /*
350 * the async reset input need ref clock to sync internally,
351 * when the ref clock comes after reset, internal synced
352 * reset time is too short, cannot meet the requirement.
353 * add one ~10us delay here.
354 */
355 udelay(10);
356 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
357 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
358 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700359 case IMX7D:
360 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500361 }
362
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500363 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100364}
365
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700366static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
367{
368 u32 val;
369 unsigned int retries;
370 struct device *dev = imx6_pcie->pci->dev;
371
372 for (retries = 0; retries < PHY_PLL_LOCK_WAIT_MAX_RETRIES; retries++) {
373 regmap_read(imx6_pcie->iomuxc_gpr, IOMUXC_GPR22, &val);
374
375 if (val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED)
376 return;
377
378 usleep_range(PHY_PLL_LOCK_WAIT_USLEEP_MIN,
379 PHY_PLL_LOCK_WAIT_USLEEP_MAX);
380 }
381
382 dev_err(dev, "PCIe PLL lock timeout\n");
383}
384
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500385static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800386{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530387 struct dw_pcie *pci = imx6_pcie->pci;
388 struct device *dev = pci->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800389 int ret;
390
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200391 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
392 ret = regulator_enable(imx6_pcie->vpcie);
393 if (ret) {
394 dev_err(dev, "failed to enable vpcie regulator: %d\n",
395 ret);
396 return;
397 }
398 }
399
Lucas Stach57526132014-03-28 17:52:55 +0100400 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800401 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500402 dev_err(dev, "unable to enable pcie_phy clock\n");
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200403 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800404 }
405
Lucas Stach57526132014-03-28 17:52:55 +0100406 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800407 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500408 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100409 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800410 }
411
Lucas Stach57526132014-03-28 17:52:55 +0100412 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800413 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500414 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100415 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800416 }
417
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100418 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
419 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500420 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100421 goto err_ref_clk;
422 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700423
Richard Zhua2fa6f62014-10-27 13:17:32 +0800424 /* allow the clocks to stabilize */
425 usleep_range(200, 500);
426
Richard Zhubc9ef772013-12-12 22:50:03 +0100427 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300428 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500429 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
430 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100431 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500432 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
433 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100434 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500435
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500436 switch (imx6_pcie->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700437 case IMX7D:
438 reset_control_deassert(imx6_pcie->pciephy_reset);
439 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
440 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500441 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500442 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
443 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500444 break;
445 case IMX6QP:
446 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
447 IMX6Q_GPR1_PCIE_SW_RST, 0);
448
449 usleep_range(200, 500);
450 break;
451 case IMX6Q: /* Nothing to do */
452 break;
453 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500454
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500455 return;
Sean Crossbb389192013-09-26 11:24:47 +0800456
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100457err_ref_clk:
458 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100459err_pcie:
460 clk_disable_unprepare(imx6_pcie->pcie_bus);
461err_pcie_bus:
462 clk_disable_unprepare(imx6_pcie->pcie_phy);
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200463err_pcie_phy:
464 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
465 ret = regulator_disable(imx6_pcie->vpcie);
466 if (ret)
467 dev_err(dev, "failed to disable vpcie regulator: %d\n",
468 ret);
469 }
Sean Crossbb389192013-09-26 11:24:47 +0800470}
471
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500472static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800473{
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700474 switch (imx6_pcie->variant) {
475 case IMX7D:
476 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
477 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
478 break;
479 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500480 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
481 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
482 IMX6SX_GPR12_PCIE_RX_EQ_2);
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700483 /* FALLTHROUGH */
484 default:
485 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
486 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500487
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700488 /* configure constant input signal to the pcie ctrl and phy */
489 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
490 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
Sean Crossbb389192013-09-26 11:24:47 +0800491
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700492 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
493 IMX6Q_GPR8_TX_DEEMPH_GEN1,
494 imx6_pcie->tx_deemph_gen1 << 0);
495 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
496 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
497 imx6_pcie->tx_deemph_gen2_3p5db << 6);
498 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
499 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
500 imx6_pcie->tx_deemph_gen2_6db << 12);
501 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
502 IMX6Q_GPR8_TX_SWING_FULL,
503 imx6_pcie->tx_swing_full << 18);
504 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
505 IMX6Q_GPR8_TX_SWING_LOW,
506 imx6_pcie->tx_swing_low << 25);
507 break;
508 }
509
Sean Crossbb389192013-09-26 11:24:47 +0800510 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
511 IMX6Q_GPR12_DEVICE_TYPE, PCI_EXP_TYPE_ROOT_PORT << 12);
Sean Crossbb389192013-09-26 11:24:47 +0800512}
513
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500514static int imx6_pcie_wait_for_link(struct imx6_pcie *imx6_pcie)
Marek Vasut66a60f92013-12-12 22:50:01 +0100515{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530516 struct dw_pcie *pci = imx6_pcie->pci;
517 struct device *dev = pci->dev;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500518
Joao Pinto886bc5c2016-03-10 14:44:35 -0600519 /* check if the link is up or not */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530520 if (!dw_pcie_wait_for_link(pci))
Joao Pinto886bc5c2016-03-10 14:44:35 -0600521 return 0;
Marek Vasut66a60f92013-12-12 22:50:01 +0100522
Bjorn Helgaas13957652016-10-06 13:35:18 -0500523 dev_dbg(dev, "DEBUG_R0: 0x%08x, DEBUG_R1: 0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530524 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
525 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Joao Pinto886bc5c2016-03-10 14:44:35 -0600526 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100527}
528
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500529static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500530{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530531 struct dw_pcie *pci = imx6_pcie->pci;
532 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500533 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500534 unsigned int retries;
535
536 for (retries = 0; retries < 200; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530537 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500538 /* Test if the speed change finished. */
539 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
540 return 0;
541 usleep_range(100, 1000);
542 }
543
Bjorn Helgaas13957652016-10-06 13:35:18 -0500544 dev_err(dev, "Speed change timeout\n");
Troy Kiskya0427462015-06-12 14:30:16 -0500545 return -EINVAL;
Sean Crossbb389192013-09-26 11:24:47 +0800546}
547
Lucas Stachd1dc9742014-03-28 17:52:59 +0100548static irqreturn_t imx6_pcie_msi_handler(int irq, void *arg)
549{
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500550 struct imx6_pcie *imx6_pcie = arg;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530551 struct dw_pcie *pci = imx6_pcie->pci;
552 struct pcie_port *pp = &pci->pp;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100553
554 return dw_handle_msi_irq(pp);
555}
556
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500557static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100558{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530559 struct dw_pcie *pci = imx6_pcie->pci;
560 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500561 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500562 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100563
564 /*
565 * Force Gen1 operation when starting the link. In case the link is
566 * started in Gen2 mode, there is a possibility the devices on the
567 * bus will not be detected at all. This happens with PCIe switches.
568 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530569 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100570 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
571 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530572 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100573
574 /* Start LTSSM. */
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700575 if (imx6_pcie->variant == IMX7D)
576 reset_control_deassert(imx6_pcie->apps_reset);
577 else
578 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
579 IMX6Q_GPR12_PCIE_CTL_2, 1 << 10);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100580
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500581 ret = imx6_pcie_wait_for_link(imx6_pcie);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200582 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600583 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100584
Tim Harveya5fcec42016-04-19 19:52:44 -0500585 if (imx6_pcie->link_gen == 2) {
586 /* Allow Gen2 mode after the link is up. */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530587 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500588 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
589 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530590 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100591
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700592 /*
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700593 * Start Directed Speed Change so the best possible
594 * speed both link partners support can be negotiated.
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700595 */
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700596 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
597 tmp |= PORT_LOGIC_SPEED_CHANGE;
598 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700599
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700600 if (imx6_pcie->variant != IMX7D) {
601 /*
602 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
603 * from i.MX6 family when no link speed transition
604 * occurs and we go Gen1 -> yep, Gen1. The difference
605 * is that, in such case, it will not be cleared by HW
606 * which will cause the following code to report false
607 * failure.
608 */
609
610 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
611 if (ret) {
612 dev_err(dev, "Failed to bring link up!\n");
613 goto err_reset_phy;
614 }
615 }
616
617 /* Make sure link training is finished as well! */
618 ret = imx6_pcie_wait_for_link(imx6_pcie);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700619 if (ret) {
620 dev_err(dev, "Failed to bring link up!\n");
621 goto err_reset_phy;
622 }
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700623 } else {
624 dev_info(dev, "Link: Gen2 disabled\n");
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100625 }
626
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530627 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500628 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500629 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600630
631err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500632 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530633 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
634 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500635 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600636 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100637}
638
Bjorn Andersson4a301762017-07-15 23:39:45 -0700639static int imx6_pcie_host_init(struct pcie_port *pp)
Sean Crossbb389192013-09-26 11:24:47 +0800640{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530641 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
642 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Sean Crossbb389192013-09-26 11:24:47 +0800643
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500644 imx6_pcie_assert_core_reset(imx6_pcie);
645 imx6_pcie_init_phy(imx6_pcie);
646 imx6_pcie_deassert_core_reset(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800647 dw_pcie_setup_rc(pp);
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500648 imx6_pcie_establish_link(imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100649
650 if (IS_ENABLED(CONFIG_PCI_MSI))
651 dw_pcie_msi_init(pp);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700652
653 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800654}
655
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530656static int imx6_pcie_link_up(struct dw_pcie *pci)
Sean Crossbb389192013-09-26 11:24:47 +0800657{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530658 return dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1) &
Lucas Stach4d107d32016-01-25 16:50:02 -0600659 PCIE_PHY_DEBUG_R1_XMLH_LINK_UP;
Sean Crossbb389192013-09-26 11:24:47 +0800660}
661
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800662static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
Sean Crossbb389192013-09-26 11:24:47 +0800663 .host_init = imx6_pcie_host_init,
664};
665
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700666static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
667 struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800668{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530669 struct dw_pcie *pci = imx6_pcie->pci;
670 struct pcie_port *pp = &pci->pp;
671 struct device *dev = &pdev->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800672 int ret;
673
Lucas Stachd1dc9742014-03-28 17:52:59 +0100674 if (IS_ENABLED(CONFIG_PCI_MSI)) {
675 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
676 if (pp->msi_irq <= 0) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500677 dev_err(dev, "failed to get MSI irq\n");
Lucas Stachd1dc9742014-03-28 17:52:59 +0100678 return -ENODEV;
679 }
680
Bjorn Helgaas13957652016-10-06 13:35:18 -0500681 ret = devm_request_irq(dev, pp->msi_irq,
Jingoo Hand88a7ef2014-11-12 12:25:09 +0900682 imx6_pcie_msi_handler,
Grygorii Strashko8ff0ef92015-12-10 21:18:20 +0200683 IRQF_SHARED | IRQF_NO_THREAD,
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500684 "mx6-pcie-msi", imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100685 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500686 dev_err(dev, "failed to request MSI irq\n");
Fabio Estevam89b2d4f2015-09-11 09:08:52 -0300687 return ret;
Lucas Stachd1dc9742014-03-28 17:52:59 +0100688 }
689 }
690
Sean Crossbb389192013-09-26 11:24:47 +0800691 pp->root_bus_nr = -1;
692 pp->ops = &imx6_pcie_host_ops;
693
Sean Crossbb389192013-09-26 11:24:47 +0800694 ret = dw_pcie_host_init(pp);
695 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500696 dev_err(dev, "failed to initialize host\n");
Sean Crossbb389192013-09-26 11:24:47 +0800697 return ret;
698 }
699
700 return 0;
701}
702
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530703static const struct dw_pcie_ops dw_pcie_ops = {
704 .link_up = imx6_pcie_link_up,
705};
706
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700707static int imx6_pcie_probe(struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800708{
Bjorn Helgaas13957652016-10-06 13:35:18 -0500709 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530710 struct dw_pcie *pci;
Sean Crossbb389192013-09-26 11:24:47 +0800711 struct imx6_pcie *imx6_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800712 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500713 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800714 int ret;
715
Bjorn Helgaas13957652016-10-06 13:35:18 -0500716 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +0800717 if (!imx6_pcie)
718 return -ENOMEM;
719
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530720 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
721 if (!pci)
722 return -ENOMEM;
723
724 pci->dev = dev;
725 pci->ops = &dw_pcie_ops;
Sean Crossbb389192013-09-26 11:24:47 +0800726
Guenter Roeckc0464062017-02-25 02:08:12 -0800727 imx6_pcie->pci = pci;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500728 imx6_pcie->variant =
Bjorn Helgaas13957652016-10-06 13:35:18 -0500729 (enum imx6_pcie_variants)of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500730
Sean Crossbb389192013-09-26 11:24:47 +0800731 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530732 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
733 if (IS_ERR(pci->dbi_base))
734 return PTR_ERR(pci->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +0800735
736 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500737 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
738 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500739 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300740 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500741 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500742 imx6_pcie->gpio_active_high ?
743 GPIOF_OUT_INIT_HIGH :
744 GPIOF_OUT_INIT_LOW,
745 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300746 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500747 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300748 return ret;
749 }
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700750 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
751 return imx6_pcie->reset_gpio;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300752 }
Sean Crossbb389192013-09-26 11:24:47 +0800753
Sean Crossbb389192013-09-26 11:24:47 +0800754 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -0500755 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Lucas Stach57526132014-03-28 17:52:55 +0100756 if (IS_ERR(imx6_pcie->pcie_phy)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500757 dev_err(dev, "pcie_phy clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100758 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800759 }
760
Bjorn Helgaas13957652016-10-06 13:35:18 -0500761 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Lucas Stach57526132014-03-28 17:52:55 +0100762 if (IS_ERR(imx6_pcie->pcie_bus)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500763 dev_err(dev, "pcie_bus clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100764 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800765 }
766
Bjorn Helgaas13957652016-10-06 13:35:18 -0500767 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Lucas Stach57526132014-03-28 17:52:55 +0100768 if (IS_ERR(imx6_pcie->pcie)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500769 dev_err(dev, "pcie clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +0100770 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800771 }
772
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700773 switch (imx6_pcie->variant) {
774 case IMX6SX:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500775 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500776 "pcie_inbound_axi");
777 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
Andrey Smirnov21b72452017-02-07 07:50:25 -0800778 dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500779 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
780 }
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700781 break;
782 case IMX7D:
783 imx6_pcie->pciephy_reset = devm_reset_control_get(dev,
784 "pciephy");
785 if (IS_ERR(imx6_pcie->pciephy_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100786 dev_err(dev, "Failed to get PCIEPHY reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700787 return PTR_ERR(imx6_pcie->pciephy_reset);
788 }
789
790 imx6_pcie->apps_reset = devm_reset_control_get(dev, "apps");
791 if (IS_ERR(imx6_pcie->apps_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +0100792 dev_err(dev, "Failed to get PCIE APPS reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700793 return PTR_ERR(imx6_pcie->apps_reset);
794 }
795 break;
796 default:
797 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500798 }
799
Sean Crossbb389192013-09-26 11:24:47 +0800800 /* Grab GPR config register range */
801 imx6_pcie->iomuxc_gpr =
802 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
803 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500804 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -0200805 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +0800806 }
807
Justin Waters28e3abe2016-01-15 10:24:35 -0500808 /* Grab PCIe PHY Tx Settings */
809 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
810 &imx6_pcie->tx_deemph_gen1))
811 imx6_pcie->tx_deemph_gen1 = 0;
812
813 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
814 &imx6_pcie->tx_deemph_gen2_3p5db))
815 imx6_pcie->tx_deemph_gen2_3p5db = 0;
816
817 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
818 &imx6_pcie->tx_deemph_gen2_6db))
819 imx6_pcie->tx_deemph_gen2_6db = 20;
820
821 if (of_property_read_u32(node, "fsl,tx-swing-full",
822 &imx6_pcie->tx_swing_full))
823 imx6_pcie->tx_swing_full = 127;
824
825 if (of_property_read_u32(node, "fsl,tx-swing-low",
826 &imx6_pcie->tx_swing_low))
827 imx6_pcie->tx_swing_low = 127;
828
Tim Harveya5fcec42016-04-19 19:52:44 -0500829 /* Limit link speed */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -0500830 ret = of_property_read_u32(node, "fsl,max-link-speed",
Tim Harveya5fcec42016-04-19 19:52:44 -0500831 &imx6_pcie->link_gen);
832 if (ret)
833 imx6_pcie->link_gen = 1;
834
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200835 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
836 if (IS_ERR(imx6_pcie->vpcie)) {
837 if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER)
838 return -EPROBE_DEFER;
839 imx6_pcie->vpcie = NULL;
840 }
841
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +0530842 platform_set_drvdata(pdev, imx6_pcie);
843
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500844 ret = imx6_add_pcie_port(imx6_pcie, pdev);
Sean Crossbb389192013-09-26 11:24:47 +0800845 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -0200846 return ret;
Sean Crossbb389192013-09-26 11:24:47 +0800847
Sean Crossbb389192013-09-26 11:24:47 +0800848 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800849}
850
Lucas Stach3e3e4062014-07-31 20:16:05 +0200851static void imx6_pcie_shutdown(struct platform_device *pdev)
852{
853 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
854
855 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500856 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +0200857}
858
Sean Crossbb389192013-09-26 11:24:47 +0800859static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500860 { .compatible = "fsl,imx6q-pcie", .data = (void *)IMX6Q, },
861 { .compatible = "fsl,imx6sx-pcie", .data = (void *)IMX6SX, },
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500862 { .compatible = "fsl,imx6qp-pcie", .data = (void *)IMX6QP, },
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700863 { .compatible = "fsl,imx7d-pcie", .data = (void *)IMX7D, },
Sean Crossbb389192013-09-26 11:24:47 +0800864 {},
865};
Sean Crossbb389192013-09-26 11:24:47 +0800866
867static struct platform_driver imx6_pcie_driver = {
868 .driver = {
869 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +0530870 .of_match_table = imx6_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -0500871 .suppress_bind_attrs = true,
Sean Crossbb389192013-09-26 11:24:47 +0800872 },
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700873 .probe = imx6_pcie_probe,
Lucas Stach3e3e4062014-07-31 20:16:05 +0200874 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +0800875};
876
Sean Crossbb389192013-09-26 11:24:47 +0800877static int __init imx6_pcie_init(void)
878{
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700879 /*
880 * Since probe() can be deferred we need to make sure that
881 * hook_fault_code is not called after __init memory is freed
882 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
883 * we can install the handler here without risking it
884 * accessing some uninitialized driver state.
885 */
Lucas Stach415b6182017-05-22 17:06:30 -0500886 hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
887 "external abort on non-linefetch");
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700888
889 return platform_driver_register(&imx6_pcie_driver);
Sean Crossbb389192013-09-26 11:24:47 +0800890}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -0400891device_initcall(imx6_pcie_init);