blob: 021ef121a058ebd2b3c372d850d31b3a987ba3e7 [file] [log] [blame]
Bjorn Helgaas8cfab3c2018-01-26 12:50:27 -06001// SPDX-License-Identifier: GPL-2.0
Sean Crossbb389192013-09-26 11:24:47 +08002/*
3 * PCIe host controller driver for Freescale i.MX6 SoCs
4 *
5 * Copyright (C) 2013 Kosagi
6 * http://www.kosagi.com
7 *
8 * Author: Sean Cross <xobs@kosagi.com>
Sean Crossbb389192013-09-26 11:24:47 +08009 */
10
Andrey Smirnov2d8ed462019-02-01 16:15:23 -080011#include <linux/bitfield.h>
Sean Crossbb389192013-09-26 11:24:47 +080012#include <linux/clk.h>
13#include <linux/delay.h>
14#include <linux/gpio.h>
15#include <linux/kernel.h>
16#include <linux/mfd/syscon.h>
17#include <linux/mfd/syscon/imx6q-iomuxc-gpr.h>
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070018#include <linux/mfd/syscon/imx7-iomuxc-gpr.h>
Sean Crossbb389192013-09-26 11:24:47 +080019#include <linux/module.h>
20#include <linux/of_gpio.h>
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050021#include <linux/of_device.h>
Trent Piepho1df82ec2019-02-05 00:17:41 +000022#include <linux/of_address.h>
Sean Crossbb389192013-09-26 11:24:47 +080023#include <linux/pci.h>
24#include <linux/platform_device.h>
25#include <linux/regmap.h>
Quentin Schulzc26ebe92017-06-08 10:07:42 +020026#include <linux/regulator/consumer.h>
Sean Crossbb389192013-09-26 11:24:47 +080027#include <linux/resource.h>
28#include <linux/signal.h>
29#include <linux/types.h>
Lucas Stachd1dc9742014-03-28 17:52:59 +010030#include <linux/interrupt.h>
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070031#include <linux/reset.h>
Leonard Crestez3f7ccee2018-10-08 18:06:21 +000032#include <linux/pm_domain.h>
33#include <linux/pm_runtime.h>
Sean Crossbb389192013-09-26 11:24:47 +080034
35#include "pcie-designware.h"
36
Andrey Smirnov2d8ed462019-02-01 16:15:23 -080037#define IMX8MQ_GPR_PCIE_REF_USE_PAD BIT(9)
38#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN BIT(10)
39#define IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE BIT(11)
40#define IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE GENMASK(11, 8)
41#define IMX8MQ_PCIE2_BASE_ADDR 0x33c00000
42
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053043#define to_imx6_pcie(x) dev_get_drvdata((x)->dev)
Sean Crossbb389192013-09-26 11:24:47 +080044
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050045enum imx6_pcie_variants {
46 IMX6Q,
Andrey Smirnov4d31c612016-05-02 14:09:10 -050047 IMX6SX,
48 IMX6QP,
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070049 IMX7D,
Andrey Smirnov2d8ed462019-02-01 16:15:23 -080050 IMX8MQ,
Andrey Smirnove6f1fef2016-05-02 14:08:21 -050051};
52
Andrey Smirnov2f532d072019-02-01 16:15:21 -080053#define IMX6_PCIE_FLAG_IMX6_PHY BIT(0)
Andrey Smirnov4c458bb2019-02-01 16:15:22 -080054#define IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE BIT(1)
Andrey Smirnov2f532d072019-02-01 16:15:21 -080055
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -080056struct imx6_pcie_drvdata {
57 enum imx6_pcie_variants variant;
Andrey Smirnov2f532d072019-02-01 16:15:21 -080058 u32 flags;
Sean Crossbb389192013-09-26 11:24:47 +080059};
60
61struct imx6_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053062 struct dw_pcie *pci;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030063 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050064 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010065 struct clk *pcie_bus;
66 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050067 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010068 struct clk *pcie;
Andrey Smirnov5278f652019-02-11 17:51:08 -080069 struct clk *pcie_aux;
Sean Crossbb389192013-09-26 11:24:47 +080070 struct regmap *iomuxc_gpr;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -080071 u32 controller_id;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070072 struct reset_control *pciephy_reset;
73 struct reset_control *apps_reset;
Leonard Crestezf4e833b2018-07-19 17:02:10 +030074 struct reset_control *turnoff_reset;
Justin Waters28e3abe2016-01-15 10:24:35 -050075 u32 tx_deemph_gen1;
76 u32 tx_deemph_gen2_3p5db;
77 u32 tx_deemph_gen2_6db;
78 u32 tx_swing_full;
79 u32 tx_swing_low;
Tim Harveya5fcec42016-04-19 19:52:44 -050080 int link_gen;
Quentin Schulzc26ebe92017-06-08 10:07:42 +020081 struct regulator *vpcie;
Trent Piepho1df82ec2019-02-05 00:17:41 +000082 void __iomem *phy_base;
Leonard Crestez3f7ccee2018-10-08 18:06:21 +000083
84 /* power domain for pcie */
85 struct device *pd_pcie;
86 /* power domain for pcie phy */
87 struct device *pd_pcie_phy;
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -080088 const struct imx6_pcie_drvdata *drvdata;
Sean Crossbb389192013-09-26 11:24:47 +080089};
90
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070091/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070092#define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
Andrey Smirnov9e303be2019-04-14 17:46:22 -070093#define PHY_PLL_LOCK_WAIT_TIMEOUT (2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070094
Marek Vasutfa33a6d2013-12-12 22:50:02 +010095/* PCIe Root Complex registers (memory-mapped) */
Richard Zhu75cb8d22018-12-21 04:33:38 +000096#define PCIE_RC_IMX6_MSI_CAP 0x50
Marek Vasutfa33a6d2013-12-12 22:50:02 +010097#define PCIE_RC_LCR 0x7c
98#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1 0x1
99#define PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2 0x2
100#define PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK 0xf
101
Bjorn Helgaas2393f792015-06-12 17:27:43 -0500102#define PCIE_RC_LCSR 0x80
103
Sean Crossbb389192013-09-26 11:24:47 +0800104/* PCIe Port Logic registers (memory-mapped) */
105#define PL_OFFSET 0x700
Lucas Stach3e3e4062014-07-31 20:16:05 +0200106#define PCIE_PL_PFLR (PL_OFFSET + 0x08)
107#define PCIE_PL_PFLR_LINK_STATE_MASK (0x3f << 16)
108#define PCIE_PL_PFLR_FORCE_LINK (1 << 15)
Sean Crossbb389192013-09-26 11:24:47 +0800109#define PCIE_PHY_DEBUG_R0 (PL_OFFSET + 0x28)
110#define PCIE_PHY_DEBUG_R1 (PL_OFFSET + 0x2c)
111
112#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
113#define PCIE_PHY_CTRL_DATA_LOC 0
114#define PCIE_PHY_CTRL_CAP_ADR_LOC 16
115#define PCIE_PHY_CTRL_CAP_DAT_LOC 17
116#define PCIE_PHY_CTRL_WR_LOC 18
117#define PCIE_PHY_CTRL_RD_LOC 19
118
119#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
120#define PCIE_PHY_STAT_ACK_LOC 16
121
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100122#define PCIE_LINK_WIDTH_SPEED_CONTROL 0x80C
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100123
Sean Crossbb389192013-09-26 11:24:47 +0800124/* PHY registers (not memory-mapped) */
Lucas Stachf18f42d2018-07-31 12:21:49 +0200125#define PCIE_PHY_ATEOVRD 0x10
126#define PCIE_PHY_ATEOVRD_EN (0x1 << 2)
127#define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0
128#define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1
129
130#define PCIE_PHY_MPLL_OVRD_IN_LO 0x11
131#define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2
132#define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f
133#define PCIE_PHY_MPLL_MULTIPLIER_OVRD (0x1 << 9)
134
Sean Crossbb389192013-09-26 11:24:47 +0800135#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -0300136#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +0800137
Trent Piepho1df82ec2019-02-05 00:17:41 +0000138/* iMX7 PCIe PHY registers */
139#define PCIE_PHY_CMN_REG4 0x14
140/* These are probably the bits that *aren't* DCC_FB_EN */
141#define PCIE_PHY_CMN_REG4_DCC_FB_EN 0x29
142
143#define PCIE_PHY_CMN_REG15 0x54
144#define PCIE_PHY_CMN_REG15_DLY_4 BIT(2)
145#define PCIE_PHY_CMN_REG15_PLL_PD BIT(5)
146#define PCIE_PHY_CMN_REG15_OVRD_PLL_PD BIT(7)
147
148#define PCIE_PHY_CMN_REG24 0x90
149#define PCIE_PHY_CMN_REG24_RX_EQ BIT(6)
150#define PCIE_PHY_CMN_REG24_RX_EQ_SEL BIT(3)
151
152#define PCIE_PHY_CMN_REG26 0x98
153#define PCIE_PHY_CMN_REG26_ATT_MODE 0xBC
154
Sean Crossbb389192013-09-26 11:24:47 +0800155#define PHY_RX_OVRD_IN_LO 0x1005
156#define PHY_RX_OVRD_IN_LO_RX_DATA_EN (1 << 5)
157#define PHY_RX_OVRD_IN_LO_RX_PLL_EN (1 << 3)
158
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500159static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, int exp_val)
Sean Crossbb389192013-09-26 11:24:47 +0800160{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530161 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800162 u32 val;
163 u32 max_iterations = 10;
164 u32 wait_counter = 0;
165
166 do {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530167 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800168 val = (val >> PCIE_PHY_STAT_ACK_LOC) & 0x1;
169 wait_counter++;
170
171 if (val == exp_val)
172 return 0;
173
174 udelay(1);
175 } while (wait_counter < max_iterations);
176
177 return -ETIMEDOUT;
178}
179
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500180static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
Sean Crossbb389192013-09-26 11:24:47 +0800181{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530182 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800183 u32 val;
184 int ret;
185
186 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530187 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800188
189 val |= (0x1 << PCIE_PHY_CTRL_CAP_ADR_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530190 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800191
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500192 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800193 if (ret)
194 return ret;
195
196 val = addr << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530197 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800198
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500199 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800200}
201
202/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500203static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, int *data)
Sean Crossbb389192013-09-26 11:24:47 +0800204{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530205 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800206 u32 val, phy_ctl;
207 int ret;
208
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500209 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800210 if (ret)
211 return ret;
212
213 /* assert Read signal */
214 phy_ctl = 0x1 << PCIE_PHY_CTRL_RD_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530215 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
Sean Crossbb389192013-09-26 11:24:47 +0800216
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500217 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800218 if (ret)
219 return ret;
220
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530221 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800222 *data = val & 0xffff;
223
224 /* deassert Read signal */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530225 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
Sean Crossbb389192013-09-26 11:24:47 +0800226
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500227 return pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800228}
229
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500230static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, int data)
Sean Crossbb389192013-09-26 11:24:47 +0800231{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530232 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800233 u32 var;
234 int ret;
235
236 /* write addr */
237 /* cap addr */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500238 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800239 if (ret)
240 return ret;
241
242 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530243 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800244
245 /* capture data */
246 var |= (0x1 << PCIE_PHY_CTRL_CAP_DAT_LOC);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530247 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800248
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500249 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800250 if (ret)
251 return ret;
252
253 /* deassert cap data */
254 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530255 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800256
257 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500258 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800259 if (ret)
260 return ret;
261
262 /* assert wr signal */
263 var = 0x1 << PCIE_PHY_CTRL_WR_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530264 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800265
266 /* wait for ack */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500267 ret = pcie_phy_poll_ack(imx6_pcie, 1);
Sean Crossbb389192013-09-26 11:24:47 +0800268 if (ret)
269 return ret;
270
271 /* deassert wr signal */
272 var = data << PCIE_PHY_CTRL_DATA_LOC;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530273 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800274
275 /* wait for ack de-assertion */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500276 ret = pcie_phy_poll_ack(imx6_pcie, 0);
Sean Crossbb389192013-09-26 11:24:47 +0800277 if (ret)
278 return ret;
279
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530280 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
Sean Crossbb389192013-09-26 11:24:47 +0800281
282 return 0;
283}
284
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500285static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
Lucas Stach53eeb482016-01-15 19:56:47 +0100286{
287 u32 tmp;
288
Andrey Smirnov2f532d072019-02-01 16:15:21 -0800289 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
290 return;
291
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500292 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100293 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
294 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500295 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100296
297 usleep_range(2000, 3000);
298
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500299 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100300 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
301 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500302 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100303}
304
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800305#ifdef CONFIG_ARM
Sean Crossbb389192013-09-26 11:24:47 +0800306/* Added for PCI abort handling */
307static int imx6q_pcie_abort_handler(unsigned long addr,
308 unsigned int fsr, struct pt_regs *regs)
309{
Lucas Stach415b6182017-05-22 17:06:30 -0500310 unsigned long pc = instruction_pointer(regs);
311 unsigned long instr = *(unsigned long *)pc;
312 int reg = (instr >> 12) & 15;
313
314 /*
315 * If the instruction being executed was a read,
316 * make it look like it read all-ones.
317 */
318 if ((instr & 0x0c100000) == 0x04100000) {
319 unsigned long val;
320
321 if (instr & 0x00400000)
322 val = 255;
323 else
324 val = -1;
325
326 regs->uregs[reg] = val;
327 regs->ARM_pc += 4;
328 return 0;
329 }
330
331 if ((instr & 0x0e100090) == 0x00100090) {
332 regs->uregs[reg] = -1;
333 regs->ARM_pc += 4;
334 return 0;
335 }
336
337 return 1;
Sean Crossbb389192013-09-26 11:24:47 +0800338}
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800339#endif
Sean Crossbb389192013-09-26 11:24:47 +0800340
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000341static int imx6_pcie_attach_pd(struct device *dev)
342{
343 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
344 struct device_link *link;
345
346 /* Do nothing when in a single power domain */
347 if (dev->pm_domain)
348 return 0;
349
350 imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
351 if (IS_ERR(imx6_pcie->pd_pcie))
352 return PTR_ERR(imx6_pcie->pd_pcie);
Leonard Cresteza6093ad2019-01-31 14:59:50 -0600353 /* Do nothing when power domain missing */
354 if (!imx6_pcie->pd_pcie)
355 return 0;
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000356 link = device_link_add(dev, imx6_pcie->pd_pcie,
357 DL_FLAG_STATELESS |
358 DL_FLAG_PM_RUNTIME |
359 DL_FLAG_RPM_ACTIVE);
360 if (!link) {
361 dev_err(dev, "Failed to add device_link to pcie pd.\n");
362 return -EINVAL;
363 }
364
365 imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
366 if (IS_ERR(imx6_pcie->pd_pcie_phy))
367 return PTR_ERR(imx6_pcie->pd_pcie_phy);
368
Leonard Cresteza4ace4f2019-01-31 14:59:56 -0600369 link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000370 DL_FLAG_STATELESS |
371 DL_FLAG_PM_RUNTIME |
372 DL_FLAG_RPM_ACTIVE);
Leonard Cresteza4ace4f2019-01-31 14:59:56 -0600373 if (!link) {
374 dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
375 return -EINVAL;
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000376 }
377
378 return 0;
379}
380
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500381static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800382{
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200383 struct device *dev = imx6_pcie->pci->dev;
384
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800385 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700386 case IMX7D:
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800387 case IMX8MQ:
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700388 reset_control_assert(imx6_pcie->pciephy_reset);
389 reset_control_assert(imx6_pcie->apps_reset);
390 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500391 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500392 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
393 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
394 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
395 /* Force PCIe PHY reset */
396 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
397 IMX6SX_GPR5_PCIE_BTNRST_RESET,
398 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500399 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500400 case IMX6QP:
401 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
402 IMX6Q_GPR1_PCIE_SW_RST,
403 IMX6Q_GPR1_PCIE_SW_RST);
404 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500405 case IMX6Q:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500406 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
407 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
408 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
409 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
410 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500411 }
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200412
413 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
414 int ret = regulator_disable(imx6_pcie->vpcie);
415
416 if (ret)
417 dev_err(dev, "failed to disable vpcie regulator: %d\n",
418 ret);
419 }
Sean Crossbb389192013-09-26 11:24:47 +0800420}
421
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800422static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
423{
424 WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ);
425 return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
426}
427
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100428static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
429{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530430 struct dw_pcie *pci = imx6_pcie->pci;
431 struct device *dev = pci->dev;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800432 unsigned int offset;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500433 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500434
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800435 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500436 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500437 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
438 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500439 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500440 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500441 }
442
443 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
444 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500445 break;
Fabio Estevamc27fd682018-05-09 14:01:48 -0300446 case IMX6QP: /* FALLTHROUGH */
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500447 case IMX6Q:
448 /* power up core phy and enable ref clock */
449 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
450 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
451 /*
452 * the async reset input need ref clock to sync internally,
453 * when the ref clock comes after reset, internal synced
454 * reset time is too short, cannot meet the requirement.
455 * add one ~10us delay here.
456 */
457 udelay(10);
458 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
459 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
460 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700461 case IMX7D:
462 break;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800463 case IMX8MQ:
Andrey Smirnov5278f652019-02-11 17:51:08 -0800464 ret = clk_prepare_enable(imx6_pcie->pcie_aux);
465 if (ret) {
466 dev_err(dev, "unable to enable pcie_aux clock\n");
467 break;
468 }
469
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800470 offset = imx6_pcie_grp_offset(imx6_pcie);
471 /*
472 * Set the over ride low and enabled
473 * make sure that REF_CLK is turned on.
474 */
475 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
476 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
477 0);
478 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
479 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
480 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
481 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500482 }
483
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500484 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100485}
486
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700487static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
488{
489 u32 val;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700490 struct device *dev = imx6_pcie->pci->dev;
491
Andrey Smirnov9e303be2019-04-14 17:46:22 -0700492 if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
493 IOMUXC_GPR22, val,
494 val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
495 PHY_PLL_LOCK_WAIT_USLEEP_MAX,
496 PHY_PLL_LOCK_WAIT_TIMEOUT))
497 dev_err(dev, "PCIe PLL lock timeout\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700498}
499
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500500static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800501{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530502 struct dw_pcie *pci = imx6_pcie->pci;
503 struct device *dev = pci->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800504 int ret;
505
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200506 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
507 ret = regulator_enable(imx6_pcie->vpcie);
508 if (ret) {
509 dev_err(dev, "failed to enable vpcie regulator: %d\n",
510 ret);
511 return;
512 }
513 }
514
Lucas Stach57526132014-03-28 17:52:55 +0100515 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800516 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500517 dev_err(dev, "unable to enable pcie_phy clock\n");
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200518 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800519 }
520
Lucas Stach57526132014-03-28 17:52:55 +0100521 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800522 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500523 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100524 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800525 }
526
Lucas Stach57526132014-03-28 17:52:55 +0100527 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800528 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500529 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100530 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800531 }
532
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100533 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
534 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500535 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100536 goto err_ref_clk;
537 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700538
Richard Zhua2fa6f62014-10-27 13:17:32 +0800539 /* allow the clocks to stabilize */
540 usleep_range(200, 500);
541
Richard Zhubc9ef772013-12-12 22:50:03 +0100542 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300543 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500544 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
545 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100546 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500547 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
548 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100549 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500550
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800551 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800552 case IMX8MQ:
553 reset_control_deassert(imx6_pcie->pciephy_reset);
554 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700555 case IMX7D:
556 reset_control_deassert(imx6_pcie->pciephy_reset);
Trent Piepho1df82ec2019-02-05 00:17:41 +0000557
558 /* Workaround for ERR010728, failure of PCI-e PLL VCO to
559 * oscillate, especially when cold. This turns off "Duty-cycle
560 * Corrector" and other mysterious undocumented things.
561 */
562 if (likely(imx6_pcie->phy_base)) {
563 /* De-assert DCC_FB_EN */
564 writel(PCIE_PHY_CMN_REG4_DCC_FB_EN,
565 imx6_pcie->phy_base + PCIE_PHY_CMN_REG4);
566 /* Assert RX_EQS and RX_EQS_SEL */
567 writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL
568 | PCIE_PHY_CMN_REG24_RX_EQ,
569 imx6_pcie->phy_base + PCIE_PHY_CMN_REG24);
570 /* Assert ATT_MODE */
571 writel(PCIE_PHY_CMN_REG26_ATT_MODE,
572 imx6_pcie->phy_base + PCIE_PHY_CMN_REG26);
573 } else {
574 dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n");
575 }
576
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700577 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
578 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500579 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500580 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
581 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500582 break;
583 case IMX6QP:
584 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
585 IMX6Q_GPR1_PCIE_SW_RST, 0);
586
587 usleep_range(200, 500);
588 break;
589 case IMX6Q: /* Nothing to do */
590 break;
591 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500592
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500593 return;
Sean Crossbb389192013-09-26 11:24:47 +0800594
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100595err_ref_clk:
596 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100597err_pcie:
598 clk_disable_unprepare(imx6_pcie->pcie_bus);
599err_pcie_bus:
600 clk_disable_unprepare(imx6_pcie->pcie_phy);
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200601err_pcie_phy:
602 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
603 ret = regulator_disable(imx6_pcie->vpcie);
604 if (ret)
605 dev_err(dev, "failed to disable vpcie regulator: %d\n",
606 ret);
607 }
Sean Crossbb389192013-09-26 11:24:47 +0800608}
609
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800610static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie)
611{
612 unsigned int mask, val;
613
614 if (imx6_pcie->drvdata->variant == IMX8MQ &&
615 imx6_pcie->controller_id == 1) {
616 mask = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE;
617 val = FIELD_PREP(IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
618 PCI_EXP_TYPE_ROOT_PORT);
619 } else {
620 mask = IMX6Q_GPR12_DEVICE_TYPE;
621 val = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE,
622 PCI_EXP_TYPE_ROOT_PORT);
623 }
624
625 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, mask, val);
626}
627
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500628static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800629{
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800630 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800631 case IMX8MQ:
632 /*
633 * TODO: Currently this code assumes external
634 * oscillator is being used
635 */
636 regmap_update_bits(imx6_pcie->iomuxc_gpr,
637 imx6_pcie_grp_offset(imx6_pcie),
638 IMX8MQ_GPR_PCIE_REF_USE_PAD,
639 IMX8MQ_GPR_PCIE_REF_USE_PAD);
640 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700641 case IMX7D:
642 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
643 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
644 break;
645 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500646 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
647 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
648 IMX6SX_GPR12_PCIE_RX_EQ_2);
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700649 /* FALLTHROUGH */
650 default:
651 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
652 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500653
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700654 /* configure constant input signal to the pcie ctrl and phy */
655 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
656 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
Sean Crossbb389192013-09-26 11:24:47 +0800657
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700658 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
659 IMX6Q_GPR8_TX_DEEMPH_GEN1,
660 imx6_pcie->tx_deemph_gen1 << 0);
661 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
662 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
663 imx6_pcie->tx_deemph_gen2_3p5db << 6);
664 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
665 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
666 imx6_pcie->tx_deemph_gen2_6db << 12);
667 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
668 IMX6Q_GPR8_TX_SWING_FULL,
669 imx6_pcie->tx_swing_full << 18);
670 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
671 IMX6Q_GPR8_TX_SWING_LOW,
672 imx6_pcie->tx_swing_low << 25);
673 break;
674 }
675
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800676 imx6_pcie_configure_type(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800677}
678
Lucas Stachf18f42d2018-07-31 12:21:49 +0200679static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
680{
681 unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
682 int mult, div;
683 u32 val;
684
Andrey Smirnov2f532d072019-02-01 16:15:21 -0800685 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
686 return 0;
687
Lucas Stachf18f42d2018-07-31 12:21:49 +0200688 switch (phy_rate) {
689 case 125000000:
690 /*
691 * The default settings of the MPLL are for a 125MHz input
692 * clock, so no need to reconfigure anything in that case.
693 */
694 return 0;
695 case 100000000:
696 mult = 25;
697 div = 0;
698 break;
699 case 200000000:
700 mult = 25;
701 div = 1;
702 break;
703 default:
704 dev_err(imx6_pcie->pci->dev,
705 "Unsupported PHY reference clock rate %lu\n", phy_rate);
706 return -EINVAL;
707 }
708
709 pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
710 val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
711 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
712 val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
713 val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
714 pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
715
716 pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
717 val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
718 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
719 val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
720 val |= PCIE_PHY_ATEOVRD_EN;
721 pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
722
723 return 0;
724}
725
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500726static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500727{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530728 struct dw_pcie *pci = imx6_pcie->pci;
729 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500730 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500731 unsigned int retries;
732
733 for (retries = 0; retries < 200; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530734 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500735 /* Test if the speed change finished. */
736 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
737 return 0;
738 usleep_range(100, 1000);
739 }
740
Bjorn Helgaas13957652016-10-06 13:35:18 -0500741 dev_err(dev, "Speed change timeout\n");
Andrey Smirnovc3776902019-04-14 17:46:24 -0700742 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100743}
744
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300745static void imx6_pcie_ltssm_enable(struct device *dev)
746{
747 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
748
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800749 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300750 case IMX6Q:
751 case IMX6SX:
752 case IMX6QP:
753 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
754 IMX6Q_GPR12_PCIE_CTL_2,
755 IMX6Q_GPR12_PCIE_CTL_2);
756 break;
757 case IMX7D:
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800758 case IMX8MQ:
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300759 reset_control_deassert(imx6_pcie->apps_reset);
760 break;
761 }
762}
763
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500764static int imx6_pcie_establish_link(struct imx6_pcie *imx6_pcie)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100765{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530766 struct dw_pcie *pci = imx6_pcie->pci;
767 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500768 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500769 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100770
771 /*
772 * Force Gen1 operation when starting the link. In case the link is
773 * started in Gen2 mode, there is a possibility the devices on the
774 * bus will not be detected at all. This happens with PCIe switches.
775 */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530776 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100777 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
778 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN1;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530779 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100780
781 /* Start LTSSM. */
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300782 imx6_pcie_ltssm_enable(dev);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100783
Andrey Smirnovee6f3712019-04-14 17:46:23 -0700784 ret = dw_pcie_wait_for_link(pci);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200785 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600786 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100787
Tim Harveya5fcec42016-04-19 19:52:44 -0500788 if (imx6_pcie->link_gen == 2) {
789 /* Allow Gen2 mode after the link is up. */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530790 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCR);
Tim Harveya5fcec42016-04-19 19:52:44 -0500791 tmp &= ~PCIE_RC_LCR_MAX_LINK_SPEEDS_MASK;
792 tmp |= PCIE_RC_LCR_MAX_LINK_SPEEDS_GEN2;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530793 dw_pcie_writel_dbi(pci, PCIE_RC_LCR, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100794
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700795 /*
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700796 * Start Directed Speed Change so the best possible
797 * speed both link partners support can be negotiated.
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700798 */
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700799 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
800 tmp |= PORT_LOGIC_SPEED_CHANGE;
801 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700802
Andrey Smirnov4c458bb2019-02-01 16:15:22 -0800803 if (imx6_pcie->drvdata->flags &
804 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) {
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700805 /*
806 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
807 * from i.MX6 family when no link speed transition
808 * occurs and we go Gen1 -> yep, Gen1. The difference
809 * is that, in such case, it will not be cleared by HW
810 * which will cause the following code to report false
811 * failure.
812 */
813
814 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
815 if (ret) {
816 dev_err(dev, "Failed to bring link up!\n");
817 goto err_reset_phy;
818 }
819 }
820
821 /* Make sure link training is finished as well! */
Andrey Smirnovee6f3712019-04-14 17:46:23 -0700822 ret = dw_pcie_wait_for_link(pci);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700823 if (ret) {
824 dev_err(dev, "Failed to bring link up!\n");
825 goto err_reset_phy;
826 }
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700827 } else {
828 dev_info(dev, "Link: Gen2 disabled\n");
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100829 }
830
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530831 tmp = dw_pcie_readl_dbi(pci, PCIE_RC_LCSR);
Bjorn Helgaas13957652016-10-06 13:35:18 -0500832 dev_info(dev, "Link up, Gen%i\n", (tmp >> 16) & 0xf);
Troy Kiskya0427462015-06-12 14:30:16 -0500833 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600834
835err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500836 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530837 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R0),
838 dw_pcie_readl_dbi(pci, PCIE_PHY_DEBUG_R1));
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500839 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600840 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100841}
842
Bjorn Andersson4a301762017-07-15 23:39:45 -0700843static int imx6_pcie_host_init(struct pcie_port *pp)
Sean Crossbb389192013-09-26 11:24:47 +0800844{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530845 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
846 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Sean Crossbb389192013-09-26 11:24:47 +0800847
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500848 imx6_pcie_assert_core_reset(imx6_pcie);
849 imx6_pcie_init_phy(imx6_pcie);
850 imx6_pcie_deassert_core_reset(imx6_pcie);
Lucas Stachf18f42d2018-07-31 12:21:49 +0200851 imx6_setup_phy_mpll(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800852 dw_pcie_setup_rc(pp);
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500853 imx6_pcie_establish_link(imx6_pcie);
Lucas Stachd1dc9742014-03-28 17:52:59 +0100854
855 if (IS_ENABLED(CONFIG_PCI_MSI))
856 dw_pcie_msi_init(pp);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700857
858 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800859}
860
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800861static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
Sean Crossbb389192013-09-26 11:24:47 +0800862 .host_init = imx6_pcie_host_init,
863};
864
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700865static int imx6_add_pcie_port(struct imx6_pcie *imx6_pcie,
866 struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800867{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530868 struct dw_pcie *pci = imx6_pcie->pci;
869 struct pcie_port *pp = &pci->pp;
870 struct device *dev = &pdev->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800871 int ret;
872
Lucas Stachd1dc9742014-03-28 17:52:59 +0100873 if (IS_ENABLED(CONFIG_PCI_MSI)) {
874 pp->msi_irq = platform_get_irq_byname(pdev, "msi");
875 if (pp->msi_irq <= 0) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500876 dev_err(dev, "failed to get MSI irq\n");
Lucas Stachd1dc9742014-03-28 17:52:59 +0100877 return -ENODEV;
878 }
Lucas Stachd1dc9742014-03-28 17:52:59 +0100879 }
880
Sean Crossbb389192013-09-26 11:24:47 +0800881 pp->ops = &imx6_pcie_host_ops;
882
Sean Crossbb389192013-09-26 11:24:47 +0800883 ret = dw_pcie_host_init(pp);
884 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500885 dev_err(dev, "failed to initialize host\n");
Sean Crossbb389192013-09-26 11:24:47 +0800886 return ret;
887 }
888
889 return 0;
890}
891
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530892static const struct dw_pcie_ops dw_pcie_ops = {
Trent Piepho68bc10b2018-11-05 18:11:36 +0000893 /* No special ops needed, but pcie-designware still expects this struct */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530894};
895
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300896#ifdef CONFIG_PM_SLEEP
897static void imx6_pcie_ltssm_disable(struct device *dev)
898{
899 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
900
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800901 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300902 case IMX6SX:
903 case IMX6QP:
904 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
905 IMX6Q_GPR12_PCIE_CTL_2, 0);
906 break;
907 case IMX7D:
908 reset_control_assert(imx6_pcie->apps_reset);
909 break;
910 default:
911 dev_err(dev, "ltssm_disable not supported\n");
912 }
913}
914
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300915static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
916{
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000917 struct device *dev = imx6_pcie->pci->dev;
918
919 /* Some variants have a turnoff reset in DT */
920 if (imx6_pcie->turnoff_reset) {
921 reset_control_assert(imx6_pcie->turnoff_reset);
922 reset_control_deassert(imx6_pcie->turnoff_reset);
923 goto pm_turnoff_sleep;
924 }
925
926 /* Others poke directly at IOMUXC registers */
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800927 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000928 case IMX6SX:
929 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
930 IMX6SX_GPR12_PCIE_PM_TURN_OFF,
931 IMX6SX_GPR12_PCIE_PM_TURN_OFF);
932 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
933 IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
934 break;
935 default:
936 dev_err(dev, "PME_Turn_Off not implemented\n");
937 return;
938 }
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300939
940 /*
941 * Components with an upstream port must respond to
942 * PME_Turn_Off with PME_TO_Ack but we can't check.
943 *
944 * The standard recommends a 1-10ms timeout after which to
945 * proceed anyway as if acks were received.
946 */
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000947pm_turnoff_sleep:
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300948 usleep_range(1000, 10000);
949}
950
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300951static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
952{
953 clk_disable_unprepare(imx6_pcie->pcie);
954 clk_disable_unprepare(imx6_pcie->pcie_phy);
955 clk_disable_unprepare(imx6_pcie->pcie_bus);
956
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800957 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000958 case IMX6SX:
959 clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
960 break;
961 case IMX7D:
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300962 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
963 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
964 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000965 break;
Andrey Smirnov5278f652019-02-11 17:51:08 -0800966 case IMX8MQ:
967 clk_disable_unprepare(imx6_pcie->pcie_aux);
968 break;
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000969 default:
970 break;
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300971 }
972}
973
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000974static inline bool imx6_pcie_supports_suspend(struct imx6_pcie *imx6_pcie)
975{
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800976 return (imx6_pcie->drvdata->variant == IMX7D ||
977 imx6_pcie->drvdata->variant == IMX6SX);
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000978}
979
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300980static int imx6_pcie_suspend_noirq(struct device *dev)
981{
982 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
983
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000984 if (!imx6_pcie_supports_suspend(imx6_pcie))
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300985 return 0;
986
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300987 imx6_pcie_pm_turnoff(imx6_pcie);
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300988 imx6_pcie_clk_disable(imx6_pcie);
989 imx6_pcie_ltssm_disable(dev);
990
991 return 0;
992}
993
994static int imx6_pcie_resume_noirq(struct device *dev)
995{
996 int ret;
997 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
998 struct pcie_port *pp = &imx6_pcie->pci->pp;
999
Leonard Crestez9e56f0d2018-11-07 13:57:03 +00001000 if (!imx6_pcie_supports_suspend(imx6_pcie))
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +03001001 return 0;
1002
1003 imx6_pcie_assert_core_reset(imx6_pcie);
1004 imx6_pcie_init_phy(imx6_pcie);
1005 imx6_pcie_deassert_core_reset(imx6_pcie);
1006 dw_pcie_setup_rc(pp);
1007
1008 ret = imx6_pcie_establish_link(imx6_pcie);
1009 if (ret < 0)
1010 dev_info(dev, "pcie link is down after resume.\n");
1011
1012 return 0;
1013}
1014#endif
1015
1016static const struct dev_pm_ops imx6_pcie_pm_ops = {
1017 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
1018 imx6_pcie_resume_noirq)
1019};
1020
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001021static int imx6_pcie_probe(struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +08001022{
Bjorn Helgaas13957652016-10-06 13:35:18 -05001023 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301024 struct dw_pcie *pci;
Sean Crossbb389192013-09-26 11:24:47 +08001025 struct imx6_pcie *imx6_pcie;
Trent Piepho1df82ec2019-02-05 00:17:41 +00001026 struct device_node *np;
Sean Crossbb389192013-09-26 11:24:47 +08001027 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -05001028 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +08001029 int ret;
Richard Zhu75cb8d22018-12-21 04:33:38 +00001030 u16 val;
Sean Crossbb389192013-09-26 11:24:47 +08001031
Bjorn Helgaas13957652016-10-06 13:35:18 -05001032 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +08001033 if (!imx6_pcie)
1034 return -ENOMEM;
1035
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301036 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
1037 if (!pci)
1038 return -ENOMEM;
1039
1040 pci->dev = dev;
1041 pci->ops = &dw_pcie_ops;
Sean Crossbb389192013-09-26 11:24:47 +08001042
Guenter Roeckc0464062017-02-25 02:08:12 -08001043 imx6_pcie->pci = pci;
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001044 imx6_pcie->drvdata = of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -05001045
Trent Piepho1df82ec2019-02-05 00:17:41 +00001046 /* Find the PHY if one is defined, only imx7d uses it */
1047 np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0);
1048 if (np) {
1049 struct resource res;
1050
1051 ret = of_address_to_resource(np, 0, &res);
1052 if (ret) {
1053 dev_err(dev, "Unable to map PCIe PHY\n");
1054 return ret;
1055 }
1056 imx6_pcie->phy_base = devm_ioremap_resource(dev, &res);
1057 if (IS_ERR(imx6_pcie->phy_base)) {
1058 dev_err(dev, "Unable to map PCIe PHY\n");
1059 return PTR_ERR(imx6_pcie->phy_base);
1060 }
1061 }
Sean Crossbb389192013-09-26 11:24:47 +08001062
Sean Crossbb389192013-09-26 11:24:47 +08001063 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301064 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
1065 if (IS_ERR(pci->dbi_base))
1066 return PTR_ERR(pci->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +08001067
1068 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -05001069 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
1070 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -05001071 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001072 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001073 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -05001074 imx6_pcie->gpio_active_high ?
1075 GPIOF_OUT_INIT_HIGH :
1076 GPIOF_OUT_INIT_LOW,
1077 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001078 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001079 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001080 return ret;
1081 }
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001082 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
1083 return imx6_pcie->reset_gpio;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001084 }
Sean Crossbb389192013-09-26 11:24:47 +08001085
Sean Crossbb389192013-09-26 11:24:47 +08001086 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -05001087 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Lucas Stach57526132014-03-28 17:52:55 +01001088 if (IS_ERR(imx6_pcie->pcie_phy)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001089 dev_err(dev, "pcie_phy clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +01001090 return PTR_ERR(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +08001091 }
1092
Bjorn Helgaas13957652016-10-06 13:35:18 -05001093 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Lucas Stach57526132014-03-28 17:52:55 +01001094 if (IS_ERR(imx6_pcie->pcie_bus)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001095 dev_err(dev, "pcie_bus clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +01001096 return PTR_ERR(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +08001097 }
1098
Bjorn Helgaas13957652016-10-06 13:35:18 -05001099 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Lucas Stach57526132014-03-28 17:52:55 +01001100 if (IS_ERR(imx6_pcie->pcie)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001101 dev_err(dev, "pcie clock source missing or invalid\n");
Lucas Stach57526132014-03-28 17:52:55 +01001102 return PTR_ERR(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +08001103 }
1104
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001105 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001106 case IMX6SX:
Bjorn Helgaas13957652016-10-06 13:35:18 -05001107 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -05001108 "pcie_inbound_axi");
1109 if (IS_ERR(imx6_pcie->pcie_inbound_axi)) {
Andrey Smirnov21b72452017-02-07 07:50:25 -08001110 dev_err(dev, "pcie_inbound_axi clock missing or invalid\n");
Christoph Fritze3c06cd2016-04-05 16:53:27 -05001111 return PTR_ERR(imx6_pcie->pcie_inbound_axi);
1112 }
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001113 break;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001114 case IMX8MQ:
Andrey Smirnov5278f652019-02-11 17:51:08 -08001115 imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux");
1116 if (IS_ERR(imx6_pcie->pcie_aux)) {
1117 dev_err(dev, "pcie_aux clock source missing or invalid\n");
1118 return PTR_ERR(imx6_pcie->pcie_aux);
1119 }
1120 /* fall through */
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001121 case IMX7D:
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001122 if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR)
1123 imx6_pcie->controller_id = 1;
1124
Philipp Zabel7c180582017-07-19 17:25:56 +02001125 imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
1126 "pciephy");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001127 if (IS_ERR(imx6_pcie->pciephy_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +01001128 dev_err(dev, "Failed to get PCIEPHY reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001129 return PTR_ERR(imx6_pcie->pciephy_reset);
1130 }
1131
Philipp Zabel7c180582017-07-19 17:25:56 +02001132 imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
1133 "apps");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001134 if (IS_ERR(imx6_pcie->apps_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +01001135 dev_err(dev, "Failed to get PCIE APPS reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001136 return PTR_ERR(imx6_pcie->apps_reset);
1137 }
1138 break;
1139 default:
1140 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -05001141 }
1142
Leonard Crestezf4e833b2018-07-19 17:02:10 +03001143 /* Grab turnoff reset */
1144 imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
1145 if (IS_ERR(imx6_pcie->turnoff_reset)) {
1146 dev_err(dev, "Failed to get TURNOFF reset control\n");
1147 return PTR_ERR(imx6_pcie->turnoff_reset);
1148 }
1149
Sean Crossbb389192013-09-26 11:24:47 +08001150 /* Grab GPR config register range */
1151 imx6_pcie->iomuxc_gpr =
1152 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
1153 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001154 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -02001155 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +08001156 }
1157
Justin Waters28e3abe2016-01-15 10:24:35 -05001158 /* Grab PCIe PHY Tx Settings */
1159 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
1160 &imx6_pcie->tx_deemph_gen1))
1161 imx6_pcie->tx_deemph_gen1 = 0;
1162
1163 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
1164 &imx6_pcie->tx_deemph_gen2_3p5db))
1165 imx6_pcie->tx_deemph_gen2_3p5db = 0;
1166
1167 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
1168 &imx6_pcie->tx_deemph_gen2_6db))
1169 imx6_pcie->tx_deemph_gen2_6db = 20;
1170
1171 if (of_property_read_u32(node, "fsl,tx-swing-full",
1172 &imx6_pcie->tx_swing_full))
1173 imx6_pcie->tx_swing_full = 127;
1174
1175 if (of_property_read_u32(node, "fsl,tx-swing-low",
1176 &imx6_pcie->tx_swing_low))
1177 imx6_pcie->tx_swing_low = 127;
1178
Tim Harveya5fcec42016-04-19 19:52:44 -05001179 /* Limit link speed */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -05001180 ret = of_property_read_u32(node, "fsl,max-link-speed",
Tim Harveya5fcec42016-04-19 19:52:44 -05001181 &imx6_pcie->link_gen);
1182 if (ret)
1183 imx6_pcie->link_gen = 1;
1184
Quentin Schulzc26ebe92017-06-08 10:07:42 +02001185 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
1186 if (IS_ERR(imx6_pcie->vpcie)) {
1187 if (PTR_ERR(imx6_pcie->vpcie) == -EPROBE_DEFER)
1188 return -EPROBE_DEFER;
1189 imx6_pcie->vpcie = NULL;
1190 }
1191
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +05301192 platform_set_drvdata(pdev, imx6_pcie);
1193
Leonard Crestez3f7ccee2018-10-08 18:06:21 +00001194 ret = imx6_pcie_attach_pd(dev);
1195 if (ret)
1196 return ret;
1197
Bjorn Helgaase7d77052016-10-11 22:06:47 -05001198 ret = imx6_add_pcie_port(imx6_pcie, pdev);
Sean Crossbb389192013-09-26 11:24:47 +08001199 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -02001200 return ret;
Sean Crossbb389192013-09-26 11:24:47 +08001201
Richard Zhu75cb8d22018-12-21 04:33:38 +00001202 if (pci_msi_enabled()) {
1203 val = dw_pcie_readw_dbi(pci, PCIE_RC_IMX6_MSI_CAP +
1204 PCI_MSI_FLAGS);
1205 val |= PCI_MSI_FLAGS_ENABLE;
1206 dw_pcie_writew_dbi(pci, PCIE_RC_IMX6_MSI_CAP + PCI_MSI_FLAGS,
1207 val);
1208 }
1209
Sean Crossbb389192013-09-26 11:24:47 +08001210 return 0;
Sean Crossbb389192013-09-26 11:24:47 +08001211}
1212
Lucas Stach3e3e4062014-07-31 20:16:05 +02001213static void imx6_pcie_shutdown(struct platform_device *pdev)
1214{
1215 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1216
1217 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -05001218 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +02001219}
1220
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001221static const struct imx6_pcie_drvdata drvdata[] = {
1222 [IMX6Q] = {
1223 .variant = IMX6Q,
Andrey Smirnov4c458bb2019-02-01 16:15:22 -08001224 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1225 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001226 },
1227 [IMX6SX] = {
1228 .variant = IMX6SX,
Andrey Smirnov4c458bb2019-02-01 16:15:22 -08001229 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1230 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001231 },
1232 [IMX6QP] = {
1233 .variant = IMX6QP,
Andrey Smirnov4c458bb2019-02-01 16:15:22 -08001234 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1235 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001236 },
1237 [IMX7D] = {
1238 .variant = IMX7D,
1239 },
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001240 [IMX8MQ] = {
1241 .variant = IMX8MQ,
1242 },
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001243};
1244
Sean Crossbb389192013-09-26 11:24:47 +08001245static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001246 { .compatible = "fsl,imx6q-pcie", .data = &drvdata[IMX6Q], },
1247 { .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], },
1248 { .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], },
1249 { .compatible = "fsl,imx7d-pcie", .data = &drvdata[IMX7D], },
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001250 { .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], } ,
Sean Crossbb389192013-09-26 11:24:47 +08001251 {},
1252};
Sean Crossbb389192013-09-26 11:24:47 +08001253
1254static struct platform_driver imx6_pcie_driver = {
1255 .driver = {
1256 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +05301257 .of_match_table = imx6_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -05001258 .suppress_bind_attrs = true,
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +03001259 .pm = &imx6_pcie_pm_ops,
Sean Crossbb389192013-09-26 11:24:47 +08001260 },
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001261 .probe = imx6_pcie_probe,
Lucas Stach3e3e4062014-07-31 20:16:05 +02001262 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +08001263};
1264
Sean Crossbb389192013-09-26 11:24:47 +08001265static int __init imx6_pcie_init(void)
1266{
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001267#ifdef CONFIG_ARM
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001268 /*
1269 * Since probe() can be deferred we need to make sure that
1270 * hook_fault_code is not called after __init memory is freed
1271 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1272 * we can install the handler here without risking it
1273 * accessing some uninitialized driver state.
1274 */
Lucas Stach415b6182017-05-22 17:06:30 -05001275 hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1276 "external abort on non-linefetch");
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001277#endif
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001278
1279 return platform_driver_register(&imx6_pcie_driver);
Sean Crossbb389192013-09-26 11:24:47 +08001280}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -04001281device_initcall(imx6_pcie_init);