blob: 0cf1333c044032f6040512c3247223c127dabeef [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
Alexander A. Klimov7ecd4a82020-06-27 12:30:50 +02006 * https://www.kosagi.com
Sean Crossbb389192013-09-26 11:24:47 +08007 *
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 Smirnov76d6dc22019-04-14 17:46:31 -070055#define IMX6_PCIE_FLAG_SUPPORTS_SUSPEND BIT(2)
Andrey Smirnov2f532d072019-02-01 16:15:21 -080056
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -080057struct imx6_pcie_drvdata {
58 enum imx6_pcie_variants variant;
Andrey Smirnov2f532d072019-02-01 16:15:21 -080059 u32 flags;
Stefan Agner075af612019-07-26 16:40:07 +020060 int dbi_length;
Sean Crossbb389192013-09-26 11:24:47 +080061};
62
63struct imx6_pcie {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +053064 struct dw_pcie *pci;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -030065 int reset_gpio;
Petr Štetiar3ea8529a2016-04-19 19:42:07 -050066 bool gpio_active_high;
Lucas Stach57526132014-03-28 17:52:55 +010067 struct clk *pcie_bus;
68 struct clk *pcie_phy;
Christoph Fritze3c06cd2016-04-05 16:53:27 -050069 struct clk *pcie_inbound_axi;
Lucas Stach57526132014-03-28 17:52:55 +010070 struct clk *pcie;
Andrey Smirnov5278f652019-02-11 17:51:08 -080071 struct clk *pcie_aux;
Sean Crossbb389192013-09-26 11:24:47 +080072 struct regmap *iomuxc_gpr;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -080073 u32 controller_id;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070074 struct reset_control *pciephy_reset;
75 struct reset_control *apps_reset;
Leonard Crestezf4e833b2018-07-19 17:02:10 +030076 struct reset_control *turnoff_reset;
Justin Waters28e3abe2016-01-15 10:24:35 -050077 u32 tx_deemph_gen1;
78 u32 tx_deemph_gen2_3p5db;
79 u32 tx_deemph_gen2_6db;
80 u32 tx_swing_full;
81 u32 tx_swing_low;
Quentin Schulzc26ebe92017-06-08 10:07:42 +020082 struct regulator *vpcie;
Trent Piepho1df82ec2019-02-05 00:17:41 +000083 void __iomem *phy_base;
Leonard Crestez3f7ccee2018-10-08 18:06:21 +000084
85 /* power domain for pcie */
86 struct device *pd_pcie;
87 /* power domain for pcie phy */
88 struct device *pd_pcie_phy;
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -080089 const struct imx6_pcie_drvdata *drvdata;
Sean Crossbb389192013-09-26 11:24:47 +080090};
91
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070092/* Parameters for the waiting for PCIe PHY PLL to lock on i.MX7 */
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070093#define PHY_PLL_LOCK_WAIT_USLEEP_MAX 200
Andrey Smirnov9e303be2019-04-14 17:46:22 -070094#define PHY_PLL_LOCK_WAIT_TIMEOUT (2000 * PHY_PLL_LOCK_WAIT_USLEEP_MAX)
Andrey Smirnov9b3fe672017-03-28 08:42:49 -070095
Sean Crossbb389192013-09-26 11:24:47 +080096/* PCIe Port Logic registers (memory-mapped) */
97#define PL_OFFSET 0x700
Sean Crossbb389192013-09-26 11:24:47 +080098
99#define PCIE_PHY_CTRL (PL_OFFSET + 0x114)
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700100#define PCIE_PHY_CTRL_DATA(x) FIELD_PREP(GENMASK(15, 0), (x))
101#define PCIE_PHY_CTRL_CAP_ADR BIT(16)
102#define PCIE_PHY_CTRL_CAP_DAT BIT(17)
103#define PCIE_PHY_CTRL_WR BIT(18)
104#define PCIE_PHY_CTRL_RD BIT(19)
Sean Crossbb389192013-09-26 11:24:47 +0800105
106#define PCIE_PHY_STAT (PL_OFFSET + 0x110)
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700107#define PCIE_PHY_STAT_ACK BIT(16)
Sean Crossbb389192013-09-26 11:24:47 +0800108
109/* PHY registers (not memory-mapped) */
Lucas Stachf18f42d2018-07-31 12:21:49 +0200110#define PCIE_PHY_ATEOVRD 0x10
Andrey Smirnov276c76d2019-04-14 17:46:27 -0700111#define PCIE_PHY_ATEOVRD_EN BIT(2)
Lucas Stachf18f42d2018-07-31 12:21:49 +0200112#define PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT 0
113#define PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK 0x1
114
115#define PCIE_PHY_MPLL_OVRD_IN_LO 0x11
116#define PCIE_PHY_MPLL_MULTIPLIER_SHIFT 2
117#define PCIE_PHY_MPLL_MULTIPLIER_MASK 0x7f
Andrey Smirnov276c76d2019-04-14 17:46:27 -0700118#define PCIE_PHY_MPLL_MULTIPLIER_OVRD BIT(9)
Lucas Stachf18f42d2018-07-31 12:21:49 +0200119
Sean Crossbb389192013-09-26 11:24:47 +0800120#define PCIE_PHY_RX_ASIC_OUT 0x100D
Fabio Estevam111feb72015-09-11 09:08:53 -0300121#define PCIE_PHY_RX_ASIC_OUT_VALID (1 << 0)
Sean Crossbb389192013-09-26 11:24:47 +0800122
Trent Piepho1df82ec2019-02-05 00:17:41 +0000123/* iMX7 PCIe PHY registers */
124#define PCIE_PHY_CMN_REG4 0x14
125/* These are probably the bits that *aren't* DCC_FB_EN */
126#define PCIE_PHY_CMN_REG4_DCC_FB_EN 0x29
127
128#define PCIE_PHY_CMN_REG15 0x54
129#define PCIE_PHY_CMN_REG15_DLY_4 BIT(2)
130#define PCIE_PHY_CMN_REG15_PLL_PD BIT(5)
131#define PCIE_PHY_CMN_REG15_OVRD_PLL_PD BIT(7)
132
133#define PCIE_PHY_CMN_REG24 0x90
134#define PCIE_PHY_CMN_REG24_RX_EQ BIT(6)
135#define PCIE_PHY_CMN_REG24_RX_EQ_SEL BIT(3)
136
137#define PCIE_PHY_CMN_REG26 0x98
138#define PCIE_PHY_CMN_REG26_ATT_MODE 0xBC
139
Sean Crossbb389192013-09-26 11:24:47 +0800140#define PHY_RX_OVRD_IN_LO 0x1005
Andrey Smirnov276c76d2019-04-14 17:46:27 -0700141#define PHY_RX_OVRD_IN_LO_RX_DATA_EN BIT(5)
142#define PHY_RX_OVRD_IN_LO_RX_PLL_EN BIT(3)
Sean Crossbb389192013-09-26 11:24:47 +0800143
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700144static int pcie_phy_poll_ack(struct imx6_pcie *imx6_pcie, bool exp_val)
Sean Crossbb389192013-09-26 11:24:47 +0800145{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530146 struct dw_pcie *pci = imx6_pcie->pci;
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700147 bool val;
Sean Crossbb389192013-09-26 11:24:47 +0800148 u32 max_iterations = 10;
149 u32 wait_counter = 0;
150
151 do {
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700152 val = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT) &
153 PCIE_PHY_STAT_ACK;
Sean Crossbb389192013-09-26 11:24:47 +0800154 wait_counter++;
155
156 if (val == exp_val)
157 return 0;
158
159 udelay(1);
160 } while (wait_counter < max_iterations);
161
162 return -ETIMEDOUT;
163}
164
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500165static int pcie_phy_wait_ack(struct imx6_pcie *imx6_pcie, int addr)
Sean Crossbb389192013-09-26 11:24:47 +0800166{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530167 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800168 u32 val;
169 int ret;
170
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700171 val = PCIE_PHY_CTRL_DATA(addr);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530172 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800173
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700174 val |= PCIE_PHY_CTRL_CAP_ADR;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530175 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800176
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700177 ret = pcie_phy_poll_ack(imx6_pcie, true);
Sean Crossbb389192013-09-26 11:24:47 +0800178 if (ret)
179 return ret;
180
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700181 val = PCIE_PHY_CTRL_DATA(addr);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530182 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, val);
Sean Crossbb389192013-09-26 11:24:47 +0800183
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700184 return pcie_phy_poll_ack(imx6_pcie, false);
Sean Crossbb389192013-09-26 11:24:47 +0800185}
186
187/* Read from the 16-bit PCIe PHY control registers (not memory-mapped) */
Andrey Smirnov37d5d322019-04-14 17:46:30 -0700188static int pcie_phy_read(struct imx6_pcie *imx6_pcie, int addr, u16 *data)
Sean Crossbb389192013-09-26 11:24:47 +0800189{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530190 struct dw_pcie *pci = imx6_pcie->pci;
Andrey Smirnov37d5d322019-04-14 17:46:30 -0700191 u32 phy_ctl;
Sean Crossbb389192013-09-26 11:24:47 +0800192 int ret;
193
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500194 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800195 if (ret)
196 return ret;
197
198 /* assert Read signal */
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700199 phy_ctl = PCIE_PHY_CTRL_RD;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530200 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, phy_ctl);
Sean Crossbb389192013-09-26 11:24:47 +0800201
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700202 ret = pcie_phy_poll_ack(imx6_pcie, true);
Sean Crossbb389192013-09-26 11:24:47 +0800203 if (ret)
204 return ret;
205
Andrey Smirnov37d5d322019-04-14 17:46:30 -0700206 *data = dw_pcie_readl_dbi(pci, PCIE_PHY_STAT);
Sean Crossbb389192013-09-26 11:24:47 +0800207
208 /* deassert Read signal */
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530209 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x00);
Sean Crossbb389192013-09-26 11:24:47 +0800210
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700211 return pcie_phy_poll_ack(imx6_pcie, false);
Sean Crossbb389192013-09-26 11:24:47 +0800212}
213
Andrey Smirnov37d5d322019-04-14 17:46:30 -0700214static int pcie_phy_write(struct imx6_pcie *imx6_pcie, int addr, u16 data)
Sean Crossbb389192013-09-26 11:24:47 +0800215{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530216 struct dw_pcie *pci = imx6_pcie->pci;
Sean Crossbb389192013-09-26 11:24:47 +0800217 u32 var;
218 int ret;
219
220 /* write addr */
221 /* cap addr */
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500222 ret = pcie_phy_wait_ack(imx6_pcie, addr);
Sean Crossbb389192013-09-26 11:24:47 +0800223 if (ret)
224 return ret;
225
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700226 var = PCIE_PHY_CTRL_DATA(data);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530227 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800228
229 /* capture data */
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700230 var |= PCIE_PHY_CTRL_CAP_DAT;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530231 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800232
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700233 ret = pcie_phy_poll_ack(imx6_pcie, true);
Sean Crossbb389192013-09-26 11:24:47 +0800234 if (ret)
235 return ret;
236
237 /* deassert cap data */
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700238 var = PCIE_PHY_CTRL_DATA(data);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530239 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800240
241 /* wait for ack de-assertion */
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700242 ret = pcie_phy_poll_ack(imx6_pcie, false);
Sean Crossbb389192013-09-26 11:24:47 +0800243 if (ret)
244 return ret;
245
246 /* assert wr signal */
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700247 var = PCIE_PHY_CTRL_WR;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530248 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800249
250 /* wait for ack */
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700251 ret = pcie_phy_poll_ack(imx6_pcie, true);
Sean Crossbb389192013-09-26 11:24:47 +0800252 if (ret)
253 return ret;
254
255 /* deassert wr signal */
Andrey Smirnov3ca41332019-04-14 17:46:28 -0700256 var = PCIE_PHY_CTRL_DATA(data);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530257 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, var);
Sean Crossbb389192013-09-26 11:24:47 +0800258
259 /* wait for ack de-assertion */
Andrey Smirnovc2c708b2019-04-14 17:46:29 -0700260 ret = pcie_phy_poll_ack(imx6_pcie, false);
Sean Crossbb389192013-09-26 11:24:47 +0800261 if (ret)
262 return ret;
263
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530264 dw_pcie_writel_dbi(pci, PCIE_PHY_CTRL, 0x0);
Sean Crossbb389192013-09-26 11:24:47 +0800265
266 return 0;
267}
268
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500269static void imx6_pcie_reset_phy(struct imx6_pcie *imx6_pcie)
Lucas Stach53eeb482016-01-15 19:56:47 +0100270{
Andrey Smirnov37d5d322019-04-14 17:46:30 -0700271 u16 tmp;
Lucas Stach53eeb482016-01-15 19:56:47 +0100272
Andrey Smirnov2f532d072019-02-01 16:15:21 -0800273 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
274 return;
275
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500276 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100277 tmp |= (PHY_RX_OVRD_IN_LO_RX_DATA_EN |
278 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500279 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100280
281 usleep_range(2000, 3000);
282
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500283 pcie_phy_read(imx6_pcie, PHY_RX_OVRD_IN_LO, &tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100284 tmp &= ~(PHY_RX_OVRD_IN_LO_RX_DATA_EN |
285 PHY_RX_OVRD_IN_LO_RX_PLL_EN);
Bjorn Helgaas8bad7f22016-10-11 22:09:32 -0500286 pcie_phy_write(imx6_pcie, PHY_RX_OVRD_IN_LO, tmp);
Lucas Stach53eeb482016-01-15 19:56:47 +0100287}
288
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800289#ifdef CONFIG_ARM
Sean Crossbb389192013-09-26 11:24:47 +0800290/* Added for PCI abort handling */
291static int imx6q_pcie_abort_handler(unsigned long addr,
292 unsigned int fsr, struct pt_regs *regs)
293{
Lucas Stach415b6182017-05-22 17:06:30 -0500294 unsigned long pc = instruction_pointer(regs);
295 unsigned long instr = *(unsigned long *)pc;
296 int reg = (instr >> 12) & 15;
297
298 /*
299 * If the instruction being executed was a read,
300 * make it look like it read all-ones.
301 */
302 if ((instr & 0x0c100000) == 0x04100000) {
303 unsigned long val;
304
305 if (instr & 0x00400000)
306 val = 255;
307 else
308 val = -1;
309
310 regs->uregs[reg] = val;
311 regs->ARM_pc += 4;
312 return 0;
313 }
314
315 if ((instr & 0x0e100090) == 0x00100090) {
316 regs->uregs[reg] = -1;
317 regs->ARM_pc += 4;
318 return 0;
319 }
320
321 return 1;
Sean Crossbb389192013-09-26 11:24:47 +0800322}
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800323#endif
Sean Crossbb389192013-09-26 11:24:47 +0800324
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000325static int imx6_pcie_attach_pd(struct device *dev)
326{
327 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
328 struct device_link *link;
329
330 /* Do nothing when in a single power domain */
331 if (dev->pm_domain)
332 return 0;
333
334 imx6_pcie->pd_pcie = dev_pm_domain_attach_by_name(dev, "pcie");
335 if (IS_ERR(imx6_pcie->pd_pcie))
336 return PTR_ERR(imx6_pcie->pd_pcie);
Leonard Cresteza6093ad2019-01-31 14:59:50 -0600337 /* Do nothing when power domain missing */
338 if (!imx6_pcie->pd_pcie)
339 return 0;
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000340 link = device_link_add(dev, imx6_pcie->pd_pcie,
341 DL_FLAG_STATELESS |
342 DL_FLAG_PM_RUNTIME |
343 DL_FLAG_RPM_ACTIVE);
344 if (!link) {
345 dev_err(dev, "Failed to add device_link to pcie pd.\n");
346 return -EINVAL;
347 }
348
349 imx6_pcie->pd_pcie_phy = dev_pm_domain_attach_by_name(dev, "pcie_phy");
350 if (IS_ERR(imx6_pcie->pd_pcie_phy))
351 return PTR_ERR(imx6_pcie->pd_pcie_phy);
352
Leonard Cresteza4ace4f2019-01-31 14:59:56 -0600353 link = device_link_add(dev, imx6_pcie->pd_pcie_phy,
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000354 DL_FLAG_STATELESS |
355 DL_FLAG_PM_RUNTIME |
356 DL_FLAG_RPM_ACTIVE);
Leonard Cresteza4ace4f2019-01-31 14:59:56 -0600357 if (!link) {
358 dev_err(dev, "Failed to add device_link to pcie_phy pd.\n");
359 return -EINVAL;
Leonard Crestez3f7ccee2018-10-08 18:06:21 +0000360 }
361
362 return 0;
363}
364
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500365static void imx6_pcie_assert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800366{
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200367 struct device *dev = imx6_pcie->pci->dev;
368
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800369 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700370 case IMX7D:
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800371 case IMX8MQ:
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700372 reset_control_assert(imx6_pcie->pciephy_reset);
373 reset_control_assert(imx6_pcie->apps_reset);
374 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500375 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500376 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
377 IMX6SX_GPR12_PCIE_TEST_POWERDOWN,
378 IMX6SX_GPR12_PCIE_TEST_POWERDOWN);
379 /* Force PCIe PHY reset */
380 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
381 IMX6SX_GPR5_PCIE_BTNRST_RESET,
382 IMX6SX_GPR5_PCIE_BTNRST_RESET);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500383 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500384 case IMX6QP:
385 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
386 IMX6Q_GPR1_PCIE_SW_RST,
387 IMX6Q_GPR1_PCIE_SW_RST);
388 break;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500389 case IMX6Q:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500390 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
391 IMX6Q_GPR1_PCIE_TEST_PD, 1 << 18);
392 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
393 IMX6Q_GPR1_PCIE_REF_CLK_EN, 0 << 16);
394 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500395 }
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200396
397 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
398 int ret = regulator_disable(imx6_pcie->vpcie);
399
400 if (ret)
401 dev_err(dev, "failed to disable vpcie regulator: %d\n",
402 ret);
403 }
Sean Crossbb389192013-09-26 11:24:47 +0800404}
405
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800406static unsigned int imx6_pcie_grp_offset(const struct imx6_pcie *imx6_pcie)
407{
408 WARN_ON(imx6_pcie->drvdata->variant != IMX8MQ);
409 return imx6_pcie->controller_id == 1 ? IOMUXC_GPR16 : IOMUXC_GPR14;
410}
411
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100412static int imx6_pcie_enable_ref_clk(struct imx6_pcie *imx6_pcie)
413{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530414 struct dw_pcie *pci = imx6_pcie->pci;
415 struct device *dev = pci->dev;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800416 unsigned int offset;
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500417 int ret = 0;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500418
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800419 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500420 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500421 ret = clk_prepare_enable(imx6_pcie->pcie_inbound_axi);
422 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500423 dev_err(dev, "unable to enable pcie_axi clock\n");
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500424 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500425 }
426
427 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
428 IMX6SX_GPR12_PCIE_TEST_POWERDOWN, 0);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500429 break;
Gustavo A. R. Silva73abd0b2020-07-21 22:19:03 -0500430 case IMX6QP:
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500431 case IMX6Q:
432 /* power up core phy and enable ref clock */
433 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
434 IMX6Q_GPR1_PCIE_TEST_PD, 0 << 18);
435 /*
436 * the async reset input need ref clock to sync internally,
437 * when the ref clock comes after reset, internal synced
438 * reset time is too short, cannot meet the requirement.
439 * add one ~10us delay here.
440 */
Andrey Smirnov87cb3122019-04-14 17:46:32 -0700441 usleep_range(10, 100);
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500442 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
443 IMX6Q_GPR1_PCIE_REF_CLK_EN, 1 << 16);
444 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700445 case IMX7D:
446 break;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800447 case IMX8MQ:
Andrey Smirnov5278f652019-02-11 17:51:08 -0800448 ret = clk_prepare_enable(imx6_pcie->pcie_aux);
449 if (ret) {
450 dev_err(dev, "unable to enable pcie_aux clock\n");
451 break;
452 }
453
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800454 offset = imx6_pcie_grp_offset(imx6_pcie);
455 /*
456 * Set the over ride low and enabled
457 * make sure that REF_CLK is turned on.
458 */
459 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
460 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE,
461 0);
462 regmap_update_bits(imx6_pcie->iomuxc_gpr, offset,
463 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN,
464 IMX8MQ_GPR_PCIE_CLK_REQ_OVERRIDE_EN);
465 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500466 }
467
Andrey Smirnove6f1fef2016-05-02 14:08:21 -0500468 return ret;
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100469}
470
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700471static void imx7d_pcie_wait_for_phy_pll_lock(struct imx6_pcie *imx6_pcie)
472{
473 u32 val;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700474 struct device *dev = imx6_pcie->pci->dev;
475
Andrey Smirnov9e303be2019-04-14 17:46:22 -0700476 if (regmap_read_poll_timeout(imx6_pcie->iomuxc_gpr,
477 IOMUXC_GPR22, val,
478 val & IMX7D_GPR22_PCIE_PHY_PLL_LOCKED,
479 PHY_PLL_LOCK_WAIT_USLEEP_MAX,
480 PHY_PLL_LOCK_WAIT_TIMEOUT))
481 dev_err(dev, "PCIe PLL lock timeout\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700482}
483
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500484static void imx6_pcie_deassert_core_reset(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800485{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530486 struct dw_pcie *pci = imx6_pcie->pci;
487 struct device *dev = pci->dev;
Sean Crossbb389192013-09-26 11:24:47 +0800488 int ret;
489
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200490 if (imx6_pcie->vpcie && !regulator_is_enabled(imx6_pcie->vpcie)) {
491 ret = regulator_enable(imx6_pcie->vpcie);
492 if (ret) {
493 dev_err(dev, "failed to enable vpcie regulator: %d\n",
494 ret);
495 return;
496 }
497 }
498
Lucas Stach57526132014-03-28 17:52:55 +0100499 ret = clk_prepare_enable(imx6_pcie->pcie_phy);
Sean Crossbb389192013-09-26 11:24:47 +0800500 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500501 dev_err(dev, "unable to enable pcie_phy clock\n");
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200502 goto err_pcie_phy;
Sean Crossbb389192013-09-26 11:24:47 +0800503 }
504
Lucas Stach57526132014-03-28 17:52:55 +0100505 ret = clk_prepare_enable(imx6_pcie->pcie_bus);
Sean Crossbb389192013-09-26 11:24:47 +0800506 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500507 dev_err(dev, "unable to enable pcie_bus clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100508 goto err_pcie_bus;
Sean Crossbb389192013-09-26 11:24:47 +0800509 }
510
Lucas Stach57526132014-03-28 17:52:55 +0100511 ret = clk_prepare_enable(imx6_pcie->pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800512 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500513 dev_err(dev, "unable to enable pcie clock\n");
Lucas Stach57526132014-03-28 17:52:55 +0100514 goto err_pcie;
Sean Crossbb389192013-09-26 11:24:47 +0800515 }
516
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100517 ret = imx6_pcie_enable_ref_clk(imx6_pcie);
518 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -0500519 dev_err(dev, "unable to enable pcie ref clock\n");
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100520 goto err_ref_clk;
521 }
Tim Harvey3fce0e82014-08-07 23:36:40 -0700522
Richard Zhua2fa6f62014-10-27 13:17:32 +0800523 /* allow the clocks to stabilize */
524 usleep_range(200, 500);
525
Richard Zhubc9ef772013-12-12 22:50:03 +0100526 /* Some boards don't have PCIe reset GPIO. */
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -0300527 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500528 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
529 imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100530 msleep(100);
Petr Štetiar3ea8529a2016-04-19 19:42:07 -0500531 gpio_set_value_cansleep(imx6_pcie->reset_gpio,
532 !imx6_pcie->gpio_active_high);
Richard Zhubc9ef772013-12-12 22:50:03 +0100533 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500534
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800535 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800536 case IMX8MQ:
537 reset_control_deassert(imx6_pcie->pciephy_reset);
538 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700539 case IMX7D:
540 reset_control_deassert(imx6_pcie->pciephy_reset);
Trent Piepho1df82ec2019-02-05 00:17:41 +0000541
542 /* Workaround for ERR010728, failure of PCI-e PLL VCO to
543 * oscillate, especially when cold. This turns off "Duty-cycle
544 * Corrector" and other mysterious undocumented things.
545 */
546 if (likely(imx6_pcie->phy_base)) {
547 /* De-assert DCC_FB_EN */
548 writel(PCIE_PHY_CMN_REG4_DCC_FB_EN,
549 imx6_pcie->phy_base + PCIE_PHY_CMN_REG4);
550 /* Assert RX_EQS and RX_EQS_SEL */
551 writel(PCIE_PHY_CMN_REG24_RX_EQ_SEL
552 | PCIE_PHY_CMN_REG24_RX_EQ,
553 imx6_pcie->phy_base + PCIE_PHY_CMN_REG24);
554 /* Assert ATT_MODE */
555 writel(PCIE_PHY_CMN_REG26_ATT_MODE,
556 imx6_pcie->phy_base + PCIE_PHY_CMN_REG26);
557 } else {
558 dev_warn(dev, "Unable to apply ERR010728 workaround. DT missing fsl,imx7d-pcie-phy phandle ?\n");
559 }
560
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700561 imx7d_pcie_wait_for_phy_pll_lock(imx6_pcie);
562 break;
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500563 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500564 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR5,
565 IMX6SX_GPR5_PCIE_BTNRST_RESET, 0);
Andrey Smirnov4d31c612016-05-02 14:09:10 -0500566 break;
567 case IMX6QP:
568 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR1,
569 IMX6Q_GPR1_PCIE_SW_RST, 0);
570
571 usleep_range(200, 500);
572 break;
573 case IMX6Q: /* Nothing to do */
574 break;
575 }
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500576
Bjorn Helgaas9ab021b2016-10-06 13:35:17 -0500577 return;
Sean Crossbb389192013-09-26 11:24:47 +0800578
Bjorn Helgaas4d1821e2016-03-14 00:30:55 +0100579err_ref_clk:
580 clk_disable_unprepare(imx6_pcie->pcie);
Lucas Stach57526132014-03-28 17:52:55 +0100581err_pcie:
582 clk_disable_unprepare(imx6_pcie->pcie_bus);
583err_pcie_bus:
584 clk_disable_unprepare(imx6_pcie->pcie_phy);
Quentin Schulzc26ebe92017-06-08 10:07:42 +0200585err_pcie_phy:
586 if (imx6_pcie->vpcie && regulator_is_enabled(imx6_pcie->vpcie) > 0) {
587 ret = regulator_disable(imx6_pcie->vpcie);
588 if (ret)
589 dev_err(dev, "failed to disable vpcie regulator: %d\n",
590 ret);
591 }
Sean Crossbb389192013-09-26 11:24:47 +0800592}
593
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800594static void imx6_pcie_configure_type(struct imx6_pcie *imx6_pcie)
595{
596 unsigned int mask, val;
597
598 if (imx6_pcie->drvdata->variant == IMX8MQ &&
599 imx6_pcie->controller_id == 1) {
600 mask = IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE;
601 val = FIELD_PREP(IMX8MQ_GPR12_PCIE2_CTRL_DEVICE_TYPE,
602 PCI_EXP_TYPE_ROOT_PORT);
603 } else {
604 mask = IMX6Q_GPR12_DEVICE_TYPE;
605 val = FIELD_PREP(IMX6Q_GPR12_DEVICE_TYPE,
606 PCI_EXP_TYPE_ROOT_PORT);
607 }
608
609 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12, mask, val);
610}
611
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500612static void imx6_pcie_init_phy(struct imx6_pcie *imx6_pcie)
Sean Crossbb389192013-09-26 11:24:47 +0800613{
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800614 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800615 case IMX8MQ:
616 /*
617 * TODO: Currently this code assumes external
618 * oscillator is being used
619 */
620 regmap_update_bits(imx6_pcie->iomuxc_gpr,
621 imx6_pcie_grp_offset(imx6_pcie),
622 IMX8MQ_GPR_PCIE_REF_USE_PAD,
623 IMX8MQ_GPR_PCIE_REF_USE_PAD);
624 break;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700625 case IMX7D:
626 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
627 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL, 0);
628 break;
629 case IMX6SX:
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500630 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
631 IMX6SX_GPR12_PCIE_RX_EQ_MASK,
632 IMX6SX_GPR12_PCIE_RX_EQ_2);
Gustavo A. R. Silva73abd0b2020-07-21 22:19:03 -0500633 fallthrough;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700634 default:
635 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
636 IMX6Q_GPR12_PCIE_CTL_2, 0 << 10);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500637
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700638 /* configure constant input signal to the pcie ctrl and phy */
639 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
640 IMX6Q_GPR12_LOS_LEVEL, 9 << 4);
Sean Crossbb389192013-09-26 11:24:47 +0800641
Andrey Smirnov9b3fe672017-03-28 08:42:49 -0700642 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
643 IMX6Q_GPR8_TX_DEEMPH_GEN1,
644 imx6_pcie->tx_deemph_gen1 << 0);
645 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
646 IMX6Q_GPR8_TX_DEEMPH_GEN2_3P5DB,
647 imx6_pcie->tx_deemph_gen2_3p5db << 6);
648 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
649 IMX6Q_GPR8_TX_DEEMPH_GEN2_6DB,
650 imx6_pcie->tx_deemph_gen2_6db << 12);
651 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
652 IMX6Q_GPR8_TX_SWING_FULL,
653 imx6_pcie->tx_swing_full << 18);
654 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR8,
655 IMX6Q_GPR8_TX_SWING_LOW,
656 imx6_pcie->tx_swing_low << 25);
657 break;
658 }
659
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800660 imx6_pcie_configure_type(imx6_pcie);
Sean Crossbb389192013-09-26 11:24:47 +0800661}
662
Lucas Stachf18f42d2018-07-31 12:21:49 +0200663static int imx6_setup_phy_mpll(struct imx6_pcie *imx6_pcie)
664{
665 unsigned long phy_rate = clk_get_rate(imx6_pcie->pcie_phy);
666 int mult, div;
Andrey Smirnov37d5d322019-04-14 17:46:30 -0700667 u16 val;
Lucas Stachf18f42d2018-07-31 12:21:49 +0200668
Andrey Smirnov2f532d072019-02-01 16:15:21 -0800669 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_IMX6_PHY))
670 return 0;
671
Lucas Stachf18f42d2018-07-31 12:21:49 +0200672 switch (phy_rate) {
673 case 125000000:
674 /*
675 * The default settings of the MPLL are for a 125MHz input
676 * clock, so no need to reconfigure anything in that case.
677 */
678 return 0;
679 case 100000000:
680 mult = 25;
681 div = 0;
682 break;
683 case 200000000:
684 mult = 25;
685 div = 1;
686 break;
687 default:
688 dev_err(imx6_pcie->pci->dev,
689 "Unsupported PHY reference clock rate %lu\n", phy_rate);
690 return -EINVAL;
691 }
692
693 pcie_phy_read(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, &val);
694 val &= ~(PCIE_PHY_MPLL_MULTIPLIER_MASK <<
695 PCIE_PHY_MPLL_MULTIPLIER_SHIFT);
696 val |= mult << PCIE_PHY_MPLL_MULTIPLIER_SHIFT;
697 val |= PCIE_PHY_MPLL_MULTIPLIER_OVRD;
698 pcie_phy_write(imx6_pcie, PCIE_PHY_MPLL_OVRD_IN_LO, val);
699
700 pcie_phy_read(imx6_pcie, PCIE_PHY_ATEOVRD, &val);
701 val &= ~(PCIE_PHY_ATEOVRD_REF_CLKDIV_MASK <<
702 PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT);
703 val |= div << PCIE_PHY_ATEOVRD_REF_CLKDIV_SHIFT;
704 val |= PCIE_PHY_ATEOVRD_EN;
705 pcie_phy_write(imx6_pcie, PCIE_PHY_ATEOVRD, val);
706
707 return 0;
708}
709
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500710static int imx6_pcie_wait_for_speed_change(struct imx6_pcie *imx6_pcie)
Troy Kiskya0427462015-06-12 14:30:16 -0500711{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530712 struct dw_pcie *pci = imx6_pcie->pci;
713 struct device *dev = pci->dev;
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500714 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500715 unsigned int retries;
716
717 for (retries = 0; retries < 200; retries++) {
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530718 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
Troy Kiskya0427462015-06-12 14:30:16 -0500719 /* Test if the speed change finished. */
720 if (!(tmp & PORT_LOGIC_SPEED_CHANGE))
721 return 0;
722 usleep_range(100, 1000);
723 }
724
Bjorn Helgaas13957652016-10-06 13:35:18 -0500725 dev_err(dev, "Speed change timeout\n");
Andrey Smirnovc3776902019-04-14 17:46:24 -0700726 return -ETIMEDOUT;
Marek Vasut66a60f92013-12-12 22:50:01 +0100727}
728
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300729static void imx6_pcie_ltssm_enable(struct device *dev)
730{
731 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
732
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800733 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300734 case IMX6Q:
735 case IMX6SX:
736 case IMX6QP:
737 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
738 IMX6Q_GPR12_PCIE_CTL_2,
739 IMX6Q_GPR12_PCIE_CTL_2);
740 break;
741 case IMX7D:
Andrey Smirnov2d8ed462019-02-01 16:15:23 -0800742 case IMX8MQ:
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300743 reset_control_deassert(imx6_pcie->apps_reset);
744 break;
745 }
746}
747
Rob Herring886a9c12020-11-05 15:11:53 -0600748static int imx6_pcie_start_link(struct dw_pcie *pci)
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100749{
Rob Herring886a9c12020-11-05 15:11:53 -0600750 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530751 struct device *dev = pci->dev;
Rob Herring201a8df2020-08-20 21:54:08 -0600752 u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_EXP);
Bjorn Helgaas1c7fae12015-06-12 15:02:49 -0500753 u32 tmp;
Troy Kiskya0427462015-06-12 14:30:16 -0500754 int ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100755
756 /*
757 * Force Gen1 operation when starting the link. In case the link is
758 * started in Gen2 mode, there is a possibility the devices on the
759 * bus will not be detected at all. This happens with PCIe switches.
760 */
Rob Herring201a8df2020-08-20 21:54:08 -0600761 tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
762 tmp &= ~PCI_EXP_LNKCAP_SLS;
763 tmp |= PCI_EXP_LNKCAP_SLS_2_5GB;
764 dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100765
766 /* Start LTSSM. */
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300767 imx6_pcie_ltssm_enable(dev);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100768
Andrey Smirnovee6f3712019-04-14 17:46:23 -0700769 ret = dw_pcie_wait_for_link(pci);
Fabio Estevamcaf3f562016-12-27 12:40:43 -0200770 if (ret)
Lucas Stach54a47a82016-01-25 16:49:53 -0600771 goto err_reset_phy;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100772
Rob Herring39bc5002020-08-20 21:54:14 -0600773 if (pci->link_gen == 2) {
Tim Harveya5fcec42016-04-19 19:52:44 -0500774 /* Allow Gen2 mode after the link is up. */
Rob Herring201a8df2020-08-20 21:54:08 -0600775 tmp = dw_pcie_readl_dbi(pci, offset + PCI_EXP_LNKCAP);
776 tmp &= ~PCI_EXP_LNKCAP_SLS;
777 tmp |= PCI_EXP_LNKCAP_SLS_5_0GB;
778 dw_pcie_writel_dbi(pci, offset + PCI_EXP_LNKCAP, tmp);
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100779
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700780 /*
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700781 * Start Directed Speed Change so the best possible
782 * speed both link partners support can be negotiated.
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700783 */
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700784 tmp = dw_pcie_readl_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL);
785 tmp |= PORT_LOGIC_SPEED_CHANGE;
786 dw_pcie_writel_dbi(pci, PCIE_LINK_WIDTH_SPEED_CONTROL, tmp);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700787
Andrey Smirnov4c458bb2019-02-01 16:15:22 -0800788 if (imx6_pcie->drvdata->flags &
789 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE) {
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700790 /*
791 * On i.MX7, DIRECT_SPEED_CHANGE behaves differently
792 * from i.MX6 family when no link speed transition
793 * occurs and we go Gen1 -> yep, Gen1. The difference
794 * is that, in such case, it will not be cleared by HW
795 * which will cause the following code to report false
796 * failure.
797 */
798
799 ret = imx6_pcie_wait_for_speed_change(imx6_pcie);
800 if (ret) {
801 dev_err(dev, "Failed to bring link up!\n");
802 goto err_reset_phy;
803 }
804 }
805
806 /* Make sure link training is finished as well! */
Andrey Smirnovee6f3712019-04-14 17:46:23 -0700807 ret = dw_pcie_wait_for_link(pci);
Andrey Smirnove6dcd872017-03-28 08:42:51 -0700808 if (ret) {
809 dev_err(dev, "Failed to bring link up!\n");
810 goto err_reset_phy;
811 }
Andrey Smirnov93b226f2017-03-28 08:42:52 -0700812 } else {
813 dev_info(dev, "Link: Gen2 disabled\n");
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100814 }
815
Rob Herring201a8df2020-08-20 21:54:08 -0600816 tmp = dw_pcie_readw_dbi(pci, offset + PCI_EXP_LNKSTA);
817 dev_info(dev, "Link up, Gen%i\n", tmp & PCI_EXP_LNKSTA_CLS);
Troy Kiskya0427462015-06-12 14:30:16 -0500818 return 0;
Lucas Stach54a47a82016-01-25 16:49:53 -0600819
820err_reset_phy:
Bjorn Helgaas13957652016-10-06 13:35:18 -0500821 dev_dbg(dev, "PHY DEBUG_R0=0x%08x DEBUG_R1=0x%08x\n",
Andrey Smirnov60ef4b02019-04-14 17:46:26 -0700822 dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0),
823 dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG1));
Bjorn Helgaas2a6a85d2016-10-11 22:18:26 -0500824 imx6_pcie_reset_phy(imx6_pcie);
Lucas Stach54a47a82016-01-25 16:49:53 -0600825 return ret;
Marek Vasutfa33a6d2013-12-12 22:50:02 +0100826}
827
Bjorn Andersson4a301762017-07-15 23:39:45 -0700828static int imx6_pcie_host_init(struct pcie_port *pp)
Sean Crossbb389192013-09-26 11:24:47 +0800829{
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530830 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
831 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
Sean Crossbb389192013-09-26 11:24:47 +0800832
Bjorn Helgaase7d77052016-10-11 22:06:47 -0500833 imx6_pcie_assert_core_reset(imx6_pcie);
834 imx6_pcie_init_phy(imx6_pcie);
835 imx6_pcie_deassert_core_reset(imx6_pcie);
Lucas Stachf18f42d2018-07-31 12:21:49 +0200836 imx6_setup_phy_mpll(imx6_pcie);
Bjorn Andersson4a301762017-07-15 23:39:45 -0700837
838 return 0;
Sean Crossbb389192013-09-26 11:24:47 +0800839}
840
Jisheng Zhang4ab2e7c2017-06-05 16:53:46 +0800841static const struct dw_pcie_host_ops imx6_pcie_host_ops = {
Sean Crossbb389192013-09-26 11:24:47 +0800842 .host_init = imx6_pcie_host_init,
843};
844
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530845static const struct dw_pcie_ops dw_pcie_ops = {
Rob Herring886a9c12020-11-05 15:11:53 -0600846 .start_link = imx6_pcie_start_link,
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530847};
848
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300849#ifdef CONFIG_PM_SLEEP
850static void imx6_pcie_ltssm_disable(struct device *dev)
851{
852 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
853
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800854 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300855 case IMX6SX:
856 case IMX6QP:
857 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
858 IMX6Q_GPR12_PCIE_CTL_2, 0);
859 break;
860 case IMX7D:
861 reset_control_assert(imx6_pcie->apps_reset);
862 break;
863 default:
864 dev_err(dev, "ltssm_disable not supported\n");
865 }
866}
867
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300868static void imx6_pcie_pm_turnoff(struct imx6_pcie *imx6_pcie)
869{
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000870 struct device *dev = imx6_pcie->pci->dev;
871
872 /* Some variants have a turnoff reset in DT */
873 if (imx6_pcie->turnoff_reset) {
874 reset_control_assert(imx6_pcie->turnoff_reset);
875 reset_control_deassert(imx6_pcie->turnoff_reset);
876 goto pm_turnoff_sleep;
877 }
878
879 /* Others poke directly at IOMUXC registers */
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800880 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000881 case IMX6SX:
882 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
883 IMX6SX_GPR12_PCIE_PM_TURN_OFF,
884 IMX6SX_GPR12_PCIE_PM_TURN_OFF);
885 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
886 IMX6SX_GPR12_PCIE_PM_TURN_OFF, 0);
887 break;
888 default:
889 dev_err(dev, "PME_Turn_Off not implemented\n");
890 return;
891 }
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300892
893 /*
894 * Components with an upstream port must respond to
895 * PME_Turn_Off with PME_TO_Ack but we can't check.
896 *
897 * The standard recommends a 1-10ms timeout after which to
898 * proceed anyway as if acks were received.
899 */
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000900pm_turnoff_sleep:
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300901 usleep_range(1000, 10000);
902}
903
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300904static void imx6_pcie_clk_disable(struct imx6_pcie *imx6_pcie)
905{
906 clk_disable_unprepare(imx6_pcie->pcie);
907 clk_disable_unprepare(imx6_pcie->pcie_phy);
908 clk_disable_unprepare(imx6_pcie->pcie_bus);
909
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800910 switch (imx6_pcie->drvdata->variant) {
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000911 case IMX6SX:
912 clk_disable_unprepare(imx6_pcie->pcie_inbound_axi);
913 break;
914 case IMX7D:
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300915 regmap_update_bits(imx6_pcie->iomuxc_gpr, IOMUXC_GPR12,
916 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL,
917 IMX7D_GPR12_PCIE_PHY_REFCLK_SEL);
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000918 break;
Andrey Smirnov5278f652019-02-11 17:51:08 -0800919 case IMX8MQ:
920 clk_disable_unprepare(imx6_pcie->pcie_aux);
921 break;
Leonard Crestez9e56f0d2018-11-07 13:57:03 +0000922 default:
923 break;
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300924 }
925}
926
927static int imx6_pcie_suspend_noirq(struct device *dev)
928{
929 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
930
Andrey Smirnov76d6dc22019-04-14 17:46:31 -0700931 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300932 return 0;
933
Leonard Crestezf4e833b2018-07-19 17:02:10 +0300934 imx6_pcie_pm_turnoff(imx6_pcie);
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300935 imx6_pcie_clk_disable(imx6_pcie);
936 imx6_pcie_ltssm_disable(dev);
937
938 return 0;
939}
940
941static int imx6_pcie_resume_noirq(struct device *dev)
942{
943 int ret;
944 struct imx6_pcie *imx6_pcie = dev_get_drvdata(dev);
945 struct pcie_port *pp = &imx6_pcie->pci->pp;
946
Andrey Smirnov76d6dc22019-04-14 17:46:31 -0700947 if (!(imx6_pcie->drvdata->flags & IMX6_PCIE_FLAG_SUPPORTS_SUSPEND))
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300948 return 0;
949
950 imx6_pcie_assert_core_reset(imx6_pcie);
951 imx6_pcie_init_phy(imx6_pcie);
952 imx6_pcie_deassert_core_reset(imx6_pcie);
953 dw_pcie_setup_rc(pp);
954
Rob Herring886a9c12020-11-05 15:11:53 -0600955 ret = imx6_pcie_start_link(imx6_pcie->pci);
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +0300956 if (ret < 0)
957 dev_info(dev, "pcie link is down after resume.\n");
958
959 return 0;
960}
961#endif
962
963static const struct dev_pm_ops imx6_pcie_pm_ops = {
964 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(imx6_pcie_suspend_noirq,
965 imx6_pcie_resume_noirq)
966};
967
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -0700968static int imx6_pcie_probe(struct platform_device *pdev)
Sean Crossbb389192013-09-26 11:24:47 +0800969{
Bjorn Helgaas13957652016-10-06 13:35:18 -0500970 struct device *dev = &pdev->dev;
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530971 struct dw_pcie *pci;
Sean Crossbb389192013-09-26 11:24:47 +0800972 struct imx6_pcie *imx6_pcie;
Trent Piepho1df82ec2019-02-05 00:17:41 +0000973 struct device_node *np;
Sean Crossbb389192013-09-26 11:24:47 +0800974 struct resource *dbi_base;
Bjorn Helgaas13957652016-10-06 13:35:18 -0500975 struct device_node *node = dev->of_node;
Sean Crossbb389192013-09-26 11:24:47 +0800976 int ret;
Richard Zhu75cb8d22018-12-21 04:33:38 +0000977 u16 val;
Sean Crossbb389192013-09-26 11:24:47 +0800978
Bjorn Helgaas13957652016-10-06 13:35:18 -0500979 imx6_pcie = devm_kzalloc(dev, sizeof(*imx6_pcie), GFP_KERNEL);
Sean Crossbb389192013-09-26 11:24:47 +0800980 if (!imx6_pcie)
981 return -ENOMEM;
982
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +0530983 pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
984 if (!pci)
985 return -ENOMEM;
986
987 pci->dev = dev;
988 pci->ops = &dw_pcie_ops;
Rob Herring60f5b732020-11-05 15:11:56 -0600989 pci->pp.ops = &imx6_pcie_host_ops;
Sean Crossbb389192013-09-26 11:24:47 +0800990
Guenter Roeckc0464062017-02-25 02:08:12 -0800991 imx6_pcie->pci = pci;
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -0800992 imx6_pcie->drvdata = of_device_get_match_data(dev);
Christoph Fritze3c06cd2016-04-05 16:53:27 -0500993
Trent Piepho1df82ec2019-02-05 00:17:41 +0000994 /* Find the PHY if one is defined, only imx7d uses it */
995 np = of_parse_phandle(node, "fsl,imx7d-pcie-phy", 0);
996 if (np) {
997 struct resource res;
998
999 ret = of_address_to_resource(np, 0, &res);
1000 if (ret) {
1001 dev_err(dev, "Unable to map PCIe PHY\n");
1002 return ret;
1003 }
1004 imx6_pcie->phy_base = devm_ioremap_resource(dev, &res);
1005 if (IS_ERR(imx6_pcie->phy_base)) {
1006 dev_err(dev, "Unable to map PCIe PHY\n");
1007 return PTR_ERR(imx6_pcie->phy_base);
1008 }
1009 }
Sean Crossbb389192013-09-26 11:24:47 +08001010
Sean Crossbb389192013-09-26 11:24:47 +08001011 dbi_base = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Kishon Vijay Abraham I442ec4c2017-02-15 18:48:14 +05301012 pci->dbi_base = devm_ioremap_resource(dev, dbi_base);
1013 if (IS_ERR(pci->dbi_base))
1014 return PTR_ERR(pci->dbi_base);
Sean Crossbb389192013-09-26 11:24:47 +08001015
1016 /* Fetch GPIOs */
Bjorn Helgaasc5af4072016-10-06 13:35:18 -05001017 imx6_pcie->reset_gpio = of_get_named_gpio(node, "reset-gpio", 0);
1018 imx6_pcie->gpio_active_high = of_property_read_bool(node,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -05001019 "reset-gpio-active-high");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001020 if (gpio_is_valid(imx6_pcie->reset_gpio)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001021 ret = devm_gpio_request_one(dev, imx6_pcie->reset_gpio,
Petr Štetiar3ea8529a2016-04-19 19:42:07 -05001022 imx6_pcie->gpio_active_high ?
1023 GPIOF_OUT_INIT_HIGH :
1024 GPIOF_OUT_INIT_LOW,
1025 "PCIe reset");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001026 if (ret) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001027 dev_err(dev, "unable to get reset gpio\n");
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001028 return ret;
1029 }
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001030 } else if (imx6_pcie->reset_gpio == -EPROBE_DEFER) {
1031 return imx6_pcie->reset_gpio;
Fabio Estevamb2d7a9c2016-03-28 18:45:36 -03001032 }
Sean Crossbb389192013-09-26 11:24:47 +08001033
Sean Crossbb389192013-09-26 11:24:47 +08001034 /* Fetch clocks */
Bjorn Helgaas13957652016-10-06 13:35:18 -05001035 imx6_pcie->pcie_phy = devm_clk_get(dev, "pcie_phy");
Anson Huang61660db2020-08-11 09:29:24 +08001036 if (IS_ERR(imx6_pcie->pcie_phy))
1037 return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_phy),
1038 "pcie_phy clock source missing or invalid\n");
Sean Crossbb389192013-09-26 11:24:47 +08001039
Bjorn Helgaas13957652016-10-06 13:35:18 -05001040 imx6_pcie->pcie_bus = devm_clk_get(dev, "pcie_bus");
Anson Huang61660db2020-08-11 09:29:24 +08001041 if (IS_ERR(imx6_pcie->pcie_bus))
1042 return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_bus),
1043 "pcie_bus clock source missing or invalid\n");
Sean Crossbb389192013-09-26 11:24:47 +08001044
Bjorn Helgaas13957652016-10-06 13:35:18 -05001045 imx6_pcie->pcie = devm_clk_get(dev, "pcie");
Anson Huang61660db2020-08-11 09:29:24 +08001046 if (IS_ERR(imx6_pcie->pcie))
1047 return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie),
1048 "pcie clock source missing or invalid\n");
Sean Crossbb389192013-09-26 11:24:47 +08001049
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001050 switch (imx6_pcie->drvdata->variant) {
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001051 case IMX6SX:
Bjorn Helgaas13957652016-10-06 13:35:18 -05001052 imx6_pcie->pcie_inbound_axi = devm_clk_get(dev,
Christoph Fritze3c06cd2016-04-05 16:53:27 -05001053 "pcie_inbound_axi");
Anson Huang61660db2020-08-11 09:29:24 +08001054 if (IS_ERR(imx6_pcie->pcie_inbound_axi))
1055 return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_inbound_axi),
1056 "pcie_inbound_axi clock missing or invalid\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001057 break;
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001058 case IMX8MQ:
Andrey Smirnov5278f652019-02-11 17:51:08 -08001059 imx6_pcie->pcie_aux = devm_clk_get(dev, "pcie_aux");
Anson Huang61660db2020-08-11 09:29:24 +08001060 if (IS_ERR(imx6_pcie->pcie_aux))
1061 return dev_err_probe(dev, PTR_ERR(imx6_pcie->pcie_aux),
1062 "pcie_aux clock source missing or invalid\n");
Gustavo A. R. Silva73abd0b2020-07-21 22:19:03 -05001063 fallthrough;
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001064 case IMX7D:
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001065 if (dbi_base->start == IMX8MQ_PCIE2_BASE_ADDR)
1066 imx6_pcie->controller_id = 1;
1067
Philipp Zabel7c180582017-07-19 17:25:56 +02001068 imx6_pcie->pciephy_reset = devm_reset_control_get_exclusive(dev,
1069 "pciephy");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001070 if (IS_ERR(imx6_pcie->pciephy_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +01001071 dev_err(dev, "Failed to get PCIEPHY reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001072 return PTR_ERR(imx6_pcie->pciephy_reset);
1073 }
1074
Philipp Zabel7c180582017-07-19 17:25:56 +02001075 imx6_pcie->apps_reset = devm_reset_control_get_exclusive(dev,
1076 "apps");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001077 if (IS_ERR(imx6_pcie->apps_reset)) {
Colin Ian King72215472017-04-21 08:02:30 +01001078 dev_err(dev, "Failed to get PCIE APPS reset control\n");
Andrey Smirnov9b3fe672017-03-28 08:42:49 -07001079 return PTR_ERR(imx6_pcie->apps_reset);
1080 }
1081 break;
1082 default:
1083 break;
Christoph Fritze3c06cd2016-04-05 16:53:27 -05001084 }
1085
Leonard Crestezf4e833b2018-07-19 17:02:10 +03001086 /* Grab turnoff reset */
1087 imx6_pcie->turnoff_reset = devm_reset_control_get_optional_exclusive(dev, "turnoff");
1088 if (IS_ERR(imx6_pcie->turnoff_reset)) {
1089 dev_err(dev, "Failed to get TURNOFF reset control\n");
1090 return PTR_ERR(imx6_pcie->turnoff_reset);
1091 }
1092
Sean Crossbb389192013-09-26 11:24:47 +08001093 /* Grab GPR config register range */
1094 imx6_pcie->iomuxc_gpr =
1095 syscon_regmap_lookup_by_compatible("fsl,imx6q-iomuxc-gpr");
1096 if (IS_ERR(imx6_pcie->iomuxc_gpr)) {
Bjorn Helgaas13957652016-10-06 13:35:18 -05001097 dev_err(dev, "unable to find iomuxc registers\n");
Fabio Estevamb391bf32013-12-02 01:39:35 -02001098 return PTR_ERR(imx6_pcie->iomuxc_gpr);
Sean Crossbb389192013-09-26 11:24:47 +08001099 }
1100
Justin Waters28e3abe2016-01-15 10:24:35 -05001101 /* Grab PCIe PHY Tx Settings */
1102 if (of_property_read_u32(node, "fsl,tx-deemph-gen1",
1103 &imx6_pcie->tx_deemph_gen1))
1104 imx6_pcie->tx_deemph_gen1 = 0;
1105
1106 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-3p5db",
1107 &imx6_pcie->tx_deemph_gen2_3p5db))
1108 imx6_pcie->tx_deemph_gen2_3p5db = 0;
1109
1110 if (of_property_read_u32(node, "fsl,tx-deemph-gen2-6db",
1111 &imx6_pcie->tx_deemph_gen2_6db))
1112 imx6_pcie->tx_deemph_gen2_6db = 20;
1113
1114 if (of_property_read_u32(node, "fsl,tx-swing-full",
1115 &imx6_pcie->tx_swing_full))
1116 imx6_pcie->tx_swing_full = 127;
1117
1118 if (of_property_read_u32(node, "fsl,tx-swing-low",
1119 &imx6_pcie->tx_swing_low))
1120 imx6_pcie->tx_swing_low = 127;
1121
Tim Harveya5fcec42016-04-19 19:52:44 -05001122 /* Limit link speed */
Rob Herring39bc5002020-08-20 21:54:14 -06001123 pci->link_gen = 1;
1124 ret = of_property_read_u32(node, "fsl,max-link-speed", &pci->link_gen);
Tim Harveya5fcec42016-04-19 19:52:44 -05001125
Quentin Schulzc26ebe92017-06-08 10:07:42 +02001126 imx6_pcie->vpcie = devm_regulator_get_optional(&pdev->dev, "vpcie");
1127 if (IS_ERR(imx6_pcie->vpcie)) {
Thierry Reding2170a092019-08-29 12:53:16 +02001128 if (PTR_ERR(imx6_pcie->vpcie) != -ENODEV)
1129 return PTR_ERR(imx6_pcie->vpcie);
Quentin Schulzc26ebe92017-06-08 10:07:42 +02001130 imx6_pcie->vpcie = NULL;
1131 }
1132
Kishon Vijay Abraham I9bcf0a62017-02-15 18:48:11 +05301133 platform_set_drvdata(pdev, imx6_pcie);
1134
Leonard Crestez3f7ccee2018-10-08 18:06:21 +00001135 ret = imx6_pcie_attach_pd(dev);
1136 if (ret)
1137 return ret;
1138
Rob Herring60f5b732020-11-05 15:11:56 -06001139 ret = dw_pcie_host_init(&pci->pp);
Sean Crossbb389192013-09-26 11:24:47 +08001140 if (ret < 0)
Fabio Estevamb391bf32013-12-02 01:39:35 -02001141 return ret;
Sean Crossbb389192013-09-26 11:24:47 +08001142
Richard Zhu75cb8d22018-12-21 04:33:38 +00001143 if (pci_msi_enabled()) {
Rob Herring201a8df2020-08-20 21:54:08 -06001144 u8 offset = dw_pcie_find_capability(pci, PCI_CAP_ID_MSI);
1145 val = dw_pcie_readw_dbi(pci, offset + PCI_MSI_FLAGS);
Richard Zhu75cb8d22018-12-21 04:33:38 +00001146 val |= PCI_MSI_FLAGS_ENABLE;
Rob Herring201a8df2020-08-20 21:54:08 -06001147 dw_pcie_writew_dbi(pci, offset + PCI_MSI_FLAGS, val);
Richard Zhu75cb8d22018-12-21 04:33:38 +00001148 }
1149
Sean Crossbb389192013-09-26 11:24:47 +08001150 return 0;
Sean Crossbb389192013-09-26 11:24:47 +08001151}
1152
Lucas Stach3e3e4062014-07-31 20:16:05 +02001153static void imx6_pcie_shutdown(struct platform_device *pdev)
1154{
1155 struct imx6_pcie *imx6_pcie = platform_get_drvdata(pdev);
1156
1157 /* bring down link, so bootloader gets clean state in case of reboot */
Bjorn Helgaase7d77052016-10-11 22:06:47 -05001158 imx6_pcie_assert_core_reset(imx6_pcie);
Lucas Stach3e3e4062014-07-31 20:16:05 +02001159}
1160
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001161static const struct imx6_pcie_drvdata drvdata[] = {
1162 [IMX6Q] = {
1163 .variant = IMX6Q,
Andrey Smirnov4c458bb2019-02-01 16:15:22 -08001164 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1165 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
Stefan Agner075af612019-07-26 16:40:07 +02001166 .dbi_length = 0x200,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001167 },
1168 [IMX6SX] = {
1169 .variant = IMX6SX,
Andrey Smirnov4c458bb2019-02-01 16:15:22 -08001170 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
Andrey Smirnov76d6dc22019-04-14 17:46:31 -07001171 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE |
1172 IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001173 },
1174 [IMX6QP] = {
1175 .variant = IMX6QP,
Andrey Smirnov4c458bb2019-02-01 16:15:22 -08001176 .flags = IMX6_PCIE_FLAG_IMX6_PHY |
1177 IMX6_PCIE_FLAG_IMX6_SPEED_CHANGE,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001178 },
1179 [IMX7D] = {
1180 .variant = IMX7D,
Andrey Smirnov76d6dc22019-04-14 17:46:31 -07001181 .flags = IMX6_PCIE_FLAG_SUPPORTS_SUSPEND,
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001182 },
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001183 [IMX8MQ] = {
1184 .variant = IMX8MQ,
1185 },
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001186};
1187
Sean Crossbb389192013-09-26 11:24:47 +08001188static const struct of_device_id imx6_pcie_of_match[] = {
Andrey Smirnove8e4d4e2019-02-01 16:15:20 -08001189 { .compatible = "fsl,imx6q-pcie", .data = &drvdata[IMX6Q], },
1190 { .compatible = "fsl,imx6sx-pcie", .data = &drvdata[IMX6SX], },
1191 { .compatible = "fsl,imx6qp-pcie", .data = &drvdata[IMX6QP], },
1192 { .compatible = "fsl,imx7d-pcie", .data = &drvdata[IMX7D], },
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001193 { .compatible = "fsl,imx8mq-pcie", .data = &drvdata[IMX8MQ], } ,
Sean Crossbb389192013-09-26 11:24:47 +08001194 {},
1195};
Sean Crossbb389192013-09-26 11:24:47 +08001196
1197static struct platform_driver imx6_pcie_driver = {
1198 .driver = {
1199 .name = "imx6q-pcie",
Sachin Kamat8bcadbe2013-10-21 14:36:41 +05301200 .of_match_table = imx6_pcie_of_match,
Brian Norrisa5f40e82017-04-20 15:36:25 -05001201 .suppress_bind_attrs = true,
Leonard Crestez0ee2c1f2018-08-27 14:28:37 +03001202 .pm = &imx6_pcie_pm_ops,
Lucas Stach1b8df7aa72019-04-04 18:45:17 +02001203 .probe_type = PROBE_PREFER_ASYNCHRONOUS,
Sean Crossbb389192013-09-26 11:24:47 +08001204 },
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001205 .probe = imx6_pcie_probe,
Lucas Stach3e3e4062014-07-31 20:16:05 +02001206 .shutdown = imx6_pcie_shutdown,
Sean Crossbb389192013-09-26 11:24:47 +08001207};
1208
Stefan Agner075af612019-07-26 16:40:07 +02001209static void imx6_pcie_quirk(struct pci_dev *dev)
1210{
1211 struct pci_bus *bus = dev->bus;
1212 struct pcie_port *pp = bus->sysdata;
1213
1214 /* Bus parent is the PCI bridge, its parent is this platform driver */
1215 if (!bus->dev.parent || !bus->dev.parent->parent)
1216 return;
1217
1218 /* Make sure we only quirk devices associated with this driver */
1219 if (bus->dev.parent->parent->driver != &imx6_pcie_driver.driver)
1220 return;
1221
Rob Herring55254932020-07-21 20:25:00 -06001222 if (pci_is_root_bus(bus)) {
Stefan Agner075af612019-07-26 16:40:07 +02001223 struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1224 struct imx6_pcie *imx6_pcie = to_imx6_pcie(pci);
1225
1226 /*
1227 * Limit config length to avoid the kernel reading beyond
1228 * the register set and causing an abort on i.MX 6Quad
1229 */
1230 if (imx6_pcie->drvdata->dbi_length) {
1231 dev->cfg_size = imx6_pcie->drvdata->dbi_length;
1232 dev_info(&dev->dev, "Limiting cfg_size to %d\n",
1233 dev->cfg_size);
1234 }
1235 }
1236}
1237DECLARE_PCI_FIXUP_CLASS_HEADER(PCI_VENDOR_ID_SYNOPSYS, 0xabcd,
1238 PCI_CLASS_BRIDGE_PCI, 8, imx6_pcie_quirk);
1239
Sean Crossbb389192013-09-26 11:24:47 +08001240static int __init imx6_pcie_init(void)
1241{
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001242#ifdef CONFIG_ARM
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001243 /*
1244 * Since probe() can be deferred we need to make sure that
1245 * hook_fault_code is not called after __init memory is freed
1246 * by kernel and since imx6q_pcie_abort_handler() is a no-op,
1247 * we can install the handler here without risking it
1248 * accessing some uninitialized driver state.
1249 */
Lucas Stach415b6182017-05-22 17:06:30 -05001250 hook_fault_code(8, imx6q_pcie_abort_handler, SIGBUS, 0,
1251 "external abort on non-linefetch");
Andrey Smirnov2d8ed462019-02-01 16:15:23 -08001252#endif
Andrey Smirnovbde4a5a2017-03-28 08:42:50 -07001253
1254 return platform_driver_register(&imx6_pcie_driver);
Sean Crossbb389192013-09-26 11:24:47 +08001255}
Paul Gortmakerf90d8e82016-08-22 17:59:43 -04001256device_initcall(imx6_pcie_init);